Vanilagy/webm-muxer

StreamTarget delivers chunks with position < current byte offset

timkonieczny opened this issue · 2 comments

Hey @Vanilagy,

thanks for putting together this library - it's super helpful!

I'm recording / muxing audio and video and processing chunks via StreamTarget. I'm writing the chunks to a file as they arrive via onData, always making sure to only write the chunk when the position matches the current total offset (sum of the length of all written chunks). This works fine, in the end I get a valid, playable video.

However, sometimes onData delivers chunks where the position arg is smaller than the current total offset. At the moment I'm just discarding these chunks. Is this the right approach or should I do something with these chunks?

Here's an example:

Chunk 1, position:    0, size:  8040, total offset after writing:  8040
Chunk 2, position: 4220, size:  4104, discarded because position < total offset
Chunk 3, position: 8040, size 20460, total offset after writing: 28500
...

Cheers!

Hi!

Discarding the chunks is not correct. The semantics are simple: You need to write the data to the specified position. The data chunks that StreamTarget spits out are not always sequential! The correct action is to overwrite the previously written data when new chunks come in that overlap old chunks.

If the incoming position is larger than the current total offset (meaning there would be a gap), you can fill that gap with any values you'd like, preferably zeroes. However, that scenario should not happen with this library.

The scenario where position < total offset is fully intentional though.

Great, thanks! I was just wondering about partly overwriting existing chunks. I'll close this issue.