Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add video_replay sample #1593

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ <h2 id="getusermedia"><a href="https://developer.mozilla.org/en-US/docs/Web/API/
<li><a href="src/content/getusermedia/getdisplaymedia/">Screensharing with getDisplayMedia</a></li>

<li><a href="src/content/getusermedia/pan-tilt-zoom/">Control camera pan, tilt, and zoom</a></li>

<li><a href="src/content/getusermedia/exposure/">Control exposure</a></li>
</ul>
<h2 id="devices">Devices:</h2>
Expand Down Expand Up @@ -210,8 +210,9 @@ <h2 id="capture">Insertable Streams:</h2>
<li><a href="src/content/insertable-streams/video-processing">Video processing using MediaStream Insertable Streams</a></li> (Experimental)
<li><a href="src/content/insertable-streams/audio-processing">Audio processing using MediaStream Insertable Streams</a></li> (Experimental)
<li><a href="src/content/insertable-streams/video-crop">Video cropping using MediaStream Insertable Streams in a Worker</a></li> (Experimental)
<li><a href="src/content/insertable-streams/webgpu">Integrations with WebGPU for custom video rendering:</a></li> (Experimental)
</ul>
<li><a href="src/content/insertable-streams/webgpu">Integrations with WebGPU for custom video rendering</a></li> (Experimental)
<li><a href="src/content/insertable-streams/video-replay">Play a IVF file generated by video_replay in the browser</a></li> (Experimental)
</ul>

</section>

Expand Down
66 changes: 66 additions & 0 deletions src/content/insertable-streams/video-replay/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<!--
* Copyright (c) 2023 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
-->
<html>
<head>

<meta charset="utf-8">
<meta name="description" content="WebRTC code samples">
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1, maximum-scale=1">
<meta itemprop="description" content="Client-side WebRTC code samples">
<meta itemprop="image" content="../../../images/webrtc-icon-192x192.png">
<meta itemprop="name" content="WebRTC code samples">
<meta name="mobile-web-app-capable" content="yes">
<meta id="theme-color" name="theme-color" content="#ffffff">

<base target="_blank">

<title>Insertable Streams - video_replay in the browser using WebCodecs</title>

<link rel="icon" sizes="192x192" href="../../../images/webrtc-icon-192x192.png">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../../../css/main.css"/>
</head>

<body>

<div id="container">
<h1><a href="//webrtc.github.io/samples/" title="WebRTC samples homepage">WebRTC samples</a>
<span>video_replay for Chrome</span></h1>

<p>
This sample shows how how load an IVF file generated by libWebRTC's
<a href="https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/rtc_tools/video_replay.cc">
video_replay tool</a> in the browser using WebCodecs and MediaStreamTrackGenerator.
This is useful since the browser uses different decoders for video than the native libWebRTC ones.
</p>

<video id="localVideo" playsinline autoplay muted></video>

<div class="box">
<label for="input">IVF file to load:</label>
<input id="input" type="file">
</div>
<div id="metadata">
</div>

<p>
<b>Note</b>: This sample is using an experimental API that has not yet been standardized. As
of 2023-02-27, this API is available in the latest version of Chrome based browsers.
</p>
<a href="https:/webrtc/samples/tree/gh-pages/src/content/insertable-streams/video-replay"
title="View source for this page on GitHub" id="viewSource">View source on GitHub</a>

</div>

<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="js/main.js" async></script>

<script src="../../../js/lib/ga.js"></script>
</body>
</html>
124 changes: 124 additions & 0 deletions src/content/insertable-streams/video-replay/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2023 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/

'use strict';

/* global MediaStreamTrackGenerator, EncodedVideoChunk */
if (typeof MediaStreamTrackGenerator === 'undefined') {
alert(
'Your browser does not support the experimental MediaStreamTrack API ' +
'for Insertable Streams of Media. See the note at the bottom of the ' +
'page.');
}

// Reader for the IVF file format as described by
// https://wiki.multimedia.cx/index.php/Duck_IVF
class IVF {
constructor(file) {
this.blob = file;
this.offset = 0;
}

async readHeader() {
if (this.offset !== 0) {
console.error('readHeader called not at start of file.');
return;
}
this.offset = 32;

const header = await this.blob.slice(0, 32).arrayBuffer();
const view = new DataView(header);
const decoder = new TextDecoder('ascii');
return {
codec: decoder.decode(header.slice(8, 12)),
width: view.getUint16(12, true),
height: view.getUint16(14, true),
fpsDenominator: view.getUint32(16, true),
fpsNumerator: view.getUint32(20, true),
};
}

async readFrame() {
if (this.offset == this.blob.size) {
return; // done.
} else if (this.offset === 0) {
console.error('readFrame called without reading header.');
return;
}
const header = await this.blob.slice(this.offset, this.offset + 12).arrayBuffer();
const view = new DataView(header);
const frameLength = view.getUint32(0, true);
const timestamp = view.getBigUint64(4, true);
const currentOffset = this.offset;
this.offset += 12 + frameLength;
return {
timestamp,
data: new Uint8Array(await this.blob.slice(currentOffset + 12, currentOffset + 12 + frameLength).arrayBuffer()),
};
}
}

// Translate between IVF fourcc codec names and WebCodec named.
const IVF2WebCodecs = {
VP80: 'vp8',
VP90: 'vp09.00.10.08',
H264: 'avc1.42E01F',
AV01: 'av01.0.08M.08.0.110.09', // AV1 Main Profile, level 4.0, Main tier, 8-bit content, non-monochrome, with 4:2:0 chroma subsampling
};

const input = document.getElementById('input');
const localVideo = document.getElementById('localVideo');
const metadata = document.getElementById('metadata');
input.onchange = async (event) => {
event.target.disabled = true;
const file = event.target.files[0];
const ivf = new IVF(file);
const generator = new MediaStreamTrackGenerator('video');
const writer = generator.writable.getWriter();
localVideo.srcObject = new MediaStream([generator]);

const header = await ivf.readHeader();
if (header) {
metadata.innerText = 'File metadata: ' + JSON.stringify(header, null, ' ');
} else {
metadata.innerText = 'Failed to load IVF file.';
return;
}

const decoder = new VideoDecoder({
output: async (frame) => {
await writer.write(frame);
frame.close();
const nextFrame = await ivf.readFrame();
if (nextFrame) {

Check failure on line 98 in src/content/insertable-streams/video-replay/js/main.js

View workflow job for this annotation

GitHub Actions / lint

Block must not be padded by blank lines

decoder.decode(new EncodedVideoChunk({
timestamp: Number(nextFrame.timestamp - firstFrame.timestamp) * 1000,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is correct or it takes the headers timescale into account. But I don't have a great sample file to check at hand.

type: 'delta',
data: nextFrame.data,
}));
} else {
decoder.flush();
}
},
error: e => console.error(e.message, e),
});
VideoDecoder.isConfigSupported({codec: IVF2WebCodecs[header.codec], codedWidth: header.width, codedHeight: header.height})
.then(config => console.log(config))

Check failure on line 112 in src/content/insertable-streams/video-replay/js/main.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 6 spaces but found 2

Check failure on line 112 in src/content/insertable-streams/video-replay/js/main.js

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
decoder.configure({
codec: IVF2WebCodecs[header.codec],
codedWidth: header.width,
codedHeight: header.height,
});
const firstFrame = await ivf.readFrame();
decoder.decode(new EncodedVideoChunk({
timestamp: 0,
type: 'key',
data: firstFrame.data,
}));
};
Loading