Vanilagy / webm-muxer

WebM multiplexer in pure TypeScript with support for WebCodecs API, video & audio.
https://vanilagy.github.io/webm-muxer/demo
MIT License
197 stars 12 forks source link

Live streaming support #12

Closed rafael2k closed 1 year ago

rafael2k commented 1 year ago

Would it be possible to get chunks of muxed data for live streaming? I use webm streaming created from ffmpeg (to an icecast2 server) and it works fine (I can play in a html5 video ou audio tag without problem), even thou the webm standard was not really conceived for live streaming...

Vanilagy commented 1 year ago

There already is a streaming target, documented in README, which is a callback that regularly gets called as new muxed chunks become available. Only gotcha is that the offsets it gets called with are not monotonically increasing (since it goes back to rewrite old parts), which is not what you'd want for streaming. But, if you ignored those, have you tried using that for streaming?

If it doesn't work, I could add an option that ensures that all data is written sequentially.

rafael2k commented 1 year ago

Sorry I did not realized that. I'll take a look and test. But I don't know if I understand about the offsets. I control the offsets written to the media chunks block in the code, right?

Vanilagy commented 1 year ago

The offset is the byte offset at which the data passed is written. It works like this, remember:

let muxer = new WebMMuxer({
    target: (data: Uint8Array, offset: number, done: boolean) => {
        // Do something with the data
    },
    ...
});

Every time the function is called, you'll get some bytes in form of a Uint8Array in data, and an integer byte offset in offset. This means that you'll have to write the bytes in data to your location (final "file") at position offset. If you're just interested in live streaming, you can try to ignore offset and simply send everything that you receive in data. However, remember that the writing is not sequential, since I jump back and forth to patch things I already wrote. That's why, if you want to live stream, you'll need to ignore any call of target with an offset smaller than the maximum of what you've received.

Again, you can see if this works. WebM actually was designed with live-streaming in mind, it just required me to mux it slightly differently (never jump back, set "size" fields to unknown, etc.). If it doesn't work for you as-is, I can try adding a mode for it!

rafael2k commented 1 year ago

Got it, thanks for the explanation. I think I can connect to an icecast2 server directly using PUT (I'm using this [1] as reference). Lemme see if I manage to write something fast to test live streaming.

[1] https://github.com/social-dist0rtion-protocol/wasm-stream/blob/master/src/icecast.js

Vanilagy commented 1 year ago

How's it going? Do you still need something, or can I close this issue for now?