-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
fippo
wants to merge
3
commits into
webrtc:gh-pages
Choose a base branch
from
fippo:video-replay
base: gh-pages
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add video_replay sample #1593
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://github.com/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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
decoder.decode(new EncodedVideoChunk({ | ||
timestamp: Number(nextFrame.timestamp - firstFrame.timestamp) * 1000, | ||
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
|
||
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, | ||
})); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.