Skip to content

Commit

Permalink
refactor: ♻️start impl update buffers logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Banou26 committed Jan 22, 2024
1 parent 3af8109 commit 3f7fc35
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
59 changes: 53 additions & 6 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ fetch(VIDEO_URL, { headers: { Range: `bytes=0-1` } })
}
}
).then(res =>
toBufferedStream(3)(
// toBufferedStream(3)(
toStreamChunkSize(BUFFER_SIZE)(
res.body!
)
)
// )
),
subtitle: (title, language, subtitle) => {
// console.log('SUBTITLE HEADER', title, language, subtitle)
Expand Down Expand Up @@ -274,15 +274,62 @@ fetch(VIDEO_URL, { headers: { Range: `bytes=0-1` } })
await appendBuffer(chunk.buffer)
}

let chunks: Chunk[] = []

const PREVIOUS_BUFFER_COUNT = 1
const BUFFER_COUNT = 2

await appendBuffer(headerChunk.buffer)

const pull = async () => {
const chunk = await remuxer.read()
chunks = [...chunks, chunk]
return chunk
}

const updateBuffers = queuedDebounceWithLastCall(500, async () => {
const { currentTime } = video
const currentChunkIndex = chunks.findIndex(({ pts, duration }) => pts <= currentTime && pts + duration >= currentTime)

// buffer needed chunks to be at BUFFER_COUNT
for (let i = 0; i < currentChunkIndex + BUFFER_COUNT; i++) {
if (chunks[i]) continue
const chunk = await pull()
await appendBuffer(chunk.buffer)
}

// remove chunks before currentChunkIndex - PREVIOUS_BUFFER_COUNT
for (let i = 0; i < currentChunkIndex - PREVIOUS_BUFFER_COUNT; i++) {
if (!chunks[i]) continue
const { duration } = chunks[i]
await unbufferRange(0, duration)
chunks = chunks.filter((_, index) => index !== i)
}

// for (let i = currentChunkIndex - PREVIOUS_BUFFER_COUNT; i >= 0; i--) {
// if (!chunks[i]) continue
// const { pts, duration } = chunks[i]
// const start = pts
// const end = pts + duration
// await unbufferRange(start, end)
// chunks = chunks.filter((_, index) => index !== i)
// }

console.log('chunks', chunks)
})

await logAndAppend((await remuxer.read()))
await logAndAppend((await remuxer.read()))
await logAndAppend((await remuxer.read()))
await logAndAppend((await remuxer.read()))
appendBuffer((await pull()).buffer)

video.addEventListener('timeupdate', () => {
updateBuffers()
})

updateBuffers()

// await logAndAppend((await remuxer.read()))
// await logAndAppend((await remuxer.read()))
// await logAndAppend((await remuxer.read()))
// await logAndAppend((await remuxer.read()))
// await logAndAppend((await remuxer.read()))
// await logAndAppend((await remuxer.read()))
// await logAndAppend((await remuxer.read()))
Expand Down
5 changes: 3 additions & 2 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ const init = makeCallListener(async (
streamResultPromiseReject = reject
})

console.log('read')
reader?.read()
.then((result) => streamResultPromiseResolve(result))
.catch((err) => streamResultPromiseReject(err))
.then((result) => console.log('read result', result) || streamResultPromiseResolve(result))

Check failure on line 81 in src/worker/index.ts

View workflow job for this annotation

GitHub Actions / build

An expression of type 'void' cannot be tested for truthiness.
.catch((err) => console.log('read err', err) || streamResultPromiseReject(err))

Check failure on line 82 in src/worker/index.ts

View workflow job for this annotation

GitHub Actions / build

An expression of type 'void' cannot be tested for truthiness.

return (
streamResultPromise
Expand Down

0 comments on commit 3f7fc35

Please sign in to comment.