|
| 1 | +import MP4Box from 'https://cdn.jsdelivr.net/npm/mp4box@0.5.3/+esm'; |
| 2 | + |
| 3 | +// From: https://w3c.github.io/webcodecs/samples/video-decode-display/ |
| 4 | + |
| 5 | +// Wraps an MP4Box File as a WritableStream underlying sink. |
| 6 | +class MP4FileSink { |
| 7 | + #setStatus = null; |
| 8 | + #file = null; |
| 9 | + #offset = 0; |
| 10 | + |
| 11 | + constructor(file, setStatus) { |
| 12 | + this.#file = file; |
| 13 | + this.#setStatus = setStatus; |
| 14 | + } |
| 15 | + |
| 16 | + write(chunk) { |
| 17 | + // MP4Box.js requires buffers to be ArrayBuffers, but we have a Uint8Array. |
| 18 | + const buffer = new ArrayBuffer(chunk.byteLength); |
| 19 | + new Uint8Array(buffer).set(chunk); |
| 20 | + |
| 21 | + // Inform MP4Box where in the file this chunk is from. |
| 22 | + buffer.fileStart = this.#offset; |
| 23 | + this.#offset += buffer.byteLength; |
| 24 | + |
| 25 | + // Append chunk. |
| 26 | + this.#setStatus("fetch", (this.#offset / (1024 ** 2)).toFixed(1) + " MiB"); |
| 27 | + this.#file.appendBuffer(buffer); |
| 28 | + } |
| 29 | + |
| 30 | + close() { |
| 31 | + this.#setStatus("fetch", "Done"); |
| 32 | + this.#file.flush(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Demuxes the first video track of an MP4 file using MP4Box, calling |
| 37 | +// `onConfig()` and `onChunk()` with appropriate WebCodecs objects. |
| 38 | +export class MP4Demuxer { |
| 39 | + #onConfig = null; |
| 40 | + #onChunk = null; |
| 41 | + #setStatus = null; |
| 42 | + #file = null; |
| 43 | + |
| 44 | + constructor(uri, {onConfig, onChunk, setStatus}) { |
| 45 | + this.#onConfig = onConfig; |
| 46 | + this.#onChunk = onChunk; |
| 47 | + this.#setStatus = setStatus; |
| 48 | + |
| 49 | + // Configure an MP4Box File for demuxing. |
| 50 | + this.#file = MP4Box.createFile(); |
| 51 | + this.#file.onError = error => setStatus("demux", error); |
| 52 | + this.#file.onReady = this.#onReady.bind(this); |
| 53 | + this.#file.onSamples = this.#onSamples.bind(this); |
| 54 | + |
| 55 | + // Fetch the file and pipe the data through. |
| 56 | + const fileSink = new MP4FileSink(this.#file, setStatus); |
| 57 | + fetch(uri).then(response => { |
| 58 | + // highWaterMark should be large enough for smooth streaming, but lower is |
| 59 | + // better for memory usage. |
| 60 | + response.body.pipeTo(new WritableStream(fileSink, {highWaterMark: 2})); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + // Get the appropriate `description` for a specific track. Assumes that the |
| 65 | + // track is H.264, H.265, VP8, VP9, or AV1. |
| 66 | + #description(track) { |
| 67 | + const trak = this.#file.getTrackById(track.id); |
| 68 | + for (const entry of trak.mdia.minf.stbl.stsd.entries) { |
| 69 | + const box = entry.avcC || entry.hvcC || entry.vpcC || entry.av1C; |
| 70 | + if (box) { |
| 71 | + const stream = new MP4Box.DataStream(undefined, 0, MP4Box.DataStream.BIG_ENDIAN); |
| 72 | + box.write(stream); |
| 73 | + return new Uint8Array(stream.buffer, 8); // Remove the box header. |
| 74 | + } |
| 75 | + } |
| 76 | + throw new Error("avcC, hvcC, vpcC, or av1C box not found"); |
| 77 | + } |
| 78 | + |
| 79 | + #onReady(info) { |
| 80 | + this.#setStatus("demux", "Ready"); |
| 81 | + const track = info.videoTracks[0]; |
| 82 | + |
| 83 | + // Generate and emit an appropriate VideoDecoderConfig. |
| 84 | + this.#onConfig({ |
| 85 | + // Browser doesn't support parsing full vp8 codec (eg: `vp08.00.41.08`), |
| 86 | + // they only support `vp8`. |
| 87 | + codec: track.codec.startsWith('vp08') ? 'vp8' : track.codec, |
| 88 | + codedHeight: track.video.height, |
| 89 | + codedWidth: track.video.width, |
| 90 | + description: this.#description(track), |
| 91 | + }); |
| 92 | + |
| 93 | + // Start demuxing. |
| 94 | + this.#file.setExtractionOptions(track.id); |
| 95 | + this.#file.start(); |
| 96 | + } |
| 97 | + |
| 98 | + #onSamples(track_id, ref, samples) { |
| 99 | + // Generate and emit an EncodedVideoChunk for each demuxed sample. |
| 100 | + for (const sample of samples) { |
| 101 | + this.#onChunk(new EncodedVideoChunk({ |
| 102 | + type: sample.is_sync ? "key" : "delta", |
| 103 | + timestamp: 1e6 * sample.cts / sample.timescale, |
| 104 | + duration: 1e6 * sample.duration / sample.timescale, |
| 105 | + data: sample.data |
| 106 | + })); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments