|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 The WebRTC project authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Use of this source code is governed by a BSD-style license |
| 5 | + * that can be found in the LICENSE file in the root of the source |
| 6 | + * tree. |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +/* global MediaStreamTrackGenerator, EncodedVideoChunk */ |
| 12 | +if (typeof MediaStreamTrackGenerator === 'undefined') { |
| 13 | + alert( |
| 14 | + 'Your browser does not support the experimental MediaStreamTrack API ' + |
| 15 | + 'for Insertable Streams of Media. See the note at the bottom of the ' + |
| 16 | + 'page.'); |
| 17 | +} |
| 18 | + |
| 19 | +// Reader for the IVF file format as described by |
| 20 | +// https://wiki.multimedia.cx/index.php/Duck_IVF |
| 21 | +class IVF { |
| 22 | + constructor(file) { |
| 23 | + this.blob = file; |
| 24 | + this.offset = 0; |
| 25 | + } |
| 26 | + |
| 27 | + async readHeader() { |
| 28 | + if (this.offset !== 0) { |
| 29 | + console.error('readHeader called not at start of file.'); |
| 30 | + return; |
| 31 | + } |
| 32 | + this.offset = 32; |
| 33 | + |
| 34 | + const header = await this.blob.slice(0, 32).arrayBuffer(); |
| 35 | + const view = new DataView(header); |
| 36 | + const decoder = new TextDecoder('ascii'); |
| 37 | + return { |
| 38 | + codec: decoder.decode(header.slice(8, 12)), |
| 39 | + width: view.getUint16(12, true), |
| 40 | + height: view.getUint16(14, true), |
| 41 | + fpsDenominator: view.getUint32(16, true), |
| 42 | + fpsNumerator: view.getUint32(20, true), |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + async readFrame() { |
| 47 | + if (this.offset == this.blob.size) { |
| 48 | + return; // done. |
| 49 | + } else if (this.offset === 0) { |
| 50 | + console.error('readFrame called without reading header.'); |
| 51 | + return; |
| 52 | + } |
| 53 | + const header = await this.blob.slice(this.offset, this.offset + 12).arrayBuffer(); |
| 54 | + const view = new DataView(header); |
| 55 | + const frameLength = view.getUint32(0, true); |
| 56 | + const timestamp = view.getBigUint64(4, true); |
| 57 | + const currentOffset = this.offset; |
| 58 | + this.offset += 12 + frameLength; |
| 59 | + return { |
| 60 | + timestamp, |
| 61 | + data: new Uint8Array(await this.blob.slice(currentOffset + 12, currentOffset + 12 + frameLength).arrayBuffer()), |
| 62 | + }; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Translate between IVF fourcc codec names and WebCodec named. |
| 67 | +const IVF2WebCodecs = { |
| 68 | + VP80: 'vp8', |
| 69 | + VP90: 'vp09.00.10.08', |
| 70 | + H264: 'avc1.42E01F', |
| 71 | +}; |
| 72 | + |
| 73 | +const input = document.getElementById('input'); |
| 74 | +const localVideo = document.getElementById('localVideo'); |
| 75 | +const metadata = document.getElementById('metadata'); |
| 76 | + |
| 77 | +input.onchange = async (event) => { |
| 78 | + event.target.disabled = true; |
| 79 | + const file = event.target.files[0]; |
| 80 | + const ivf = new IVF(file); |
| 81 | + const generator = new MediaStreamTrackGenerator('video'); |
| 82 | + const writer = generator.writable.getWriter(); |
| 83 | + localVideo.srcObject = new MediaStream([generator]); |
| 84 | + |
| 85 | + const header = await ivf.readHeader(); |
| 86 | + if (header) { |
| 87 | + metadata.innerText = 'File metadata: ' + JSON.stringify(header, null, ' '); |
| 88 | + } else { |
| 89 | + metadata.innerText = 'Failed to load IVF file.'; |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const decoder = new VideoDecoder({ |
| 94 | + output: async (frame) => { |
| 95 | + await writer.write(frame); |
| 96 | + frame.close(); |
| 97 | + const nextFrame = await ivf.readFrame(); |
| 98 | + if (nextFrame) { |
| 99 | + decoder.decode(new EncodedVideoChunk({ |
| 100 | + timestamp: Number(nextFrame.timestamp - firstFrame.timestamp) * 1000, |
| 101 | + type: 'delta', |
| 102 | + data: nextFrame.data, |
| 103 | + })); |
| 104 | + } else { |
| 105 | + decoder.flush(); |
| 106 | + } |
| 107 | + }, |
| 108 | + error: e => console.error(e.message, e), |
| 109 | + }); |
| 110 | + decoder.configure({ |
| 111 | + codec: IVF2WebCodecs[header.codec], |
| 112 | + codedWidth: header.width, |
| 113 | + codedHeight: header.height, |
| 114 | + }); |
| 115 | + const firstFrame = await ivf.readFrame(); |
| 116 | + decoder.decode(new EncodedVideoChunk({ |
| 117 | + timestamp: 0, |
| 118 | + type: 'key', |
| 119 | + data: firstFrame.data, |
| 120 | + })); |
| 121 | +}; |
0 commit comments