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

Update video-analyzer to spec. #1646

Open
wants to merge 4 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
61 changes: 14 additions & 47 deletions src/content/insertable-streams/video-analyzer/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@
const banner = document.querySelector('#banner');


const supportsInsertableStreams =
!!RTCRtpSender.prototype.createEncodedStreams;

if (!supportsInsertableStreams) {
banner.innerText = 'Your browser does not support Insertable Streams. ' +
if (!window.RTCRtpScriptTransform) {
banner.innerText = 'Your browser does not support WebRTC encoded transforms. ' +
'This sample will not work.';
startButton.disabled = true;
}
Expand All @@ -62,6 +59,8 @@
let localStream;
let pc1;
let pc2;
const worker = new Worker('js/worker.js');

const offerOptions = {
offerToReceiveAudio: 0,
offerToReceiveVideo: 1
Expand Down Expand Up @@ -170,11 +169,7 @@

function gotRemoteTrack(e) {
console.log('pc2 received remote stream');
const frameStreams = e.receiver.createEncodedStreams();
frameStreams.readable.pipeThrough(new TransformStream({
transform: videoAnalyzer
}))
.pipeTo(frameStreams.writable);
e.receiver.transform = new RTCRtpScriptTransform(worker, {});

Check failure on line 172 in src/content/insertable-streams/video-analyzer/js/main.js

View workflow job for this annotation

GitHub Actions / lint

'RTCRtpScriptTransform' is not defined
remoteVideo.srcObject = e.streams[0];
}

Expand Down Expand Up @@ -237,49 +232,21 @@
const interFrameSizeDisplay = document.querySelector('#interframe-size');
const videoSizeDisplay = document.querySelector('#video-size');
const duplicateCountDisplay = document.querySelector('#duplicate-count');
let keyFrameCount = 0;
let interFrameCount = 0;
let keyFrameLastSize = 0;
let interFrameLastSize = 0;
let duplicateCount = 0;
let prevFrameType;
let prevFrameTimestamp;
let prevFrameSynchronizationSource;

function videoAnalyzer(encodedFrame, controller) {
const view = new DataView(encodedFrame.data);
// We assume that the video is VP8.
// TODO: Check the codec to see that it is.
// The lowest value bit in the first byte is the keyframe indicator.
// https://tools.ietf.org/html/rfc6386#section-9.1
const keyframeBit = view.getUint8(0) & 0x01;
// console.log(view.getUint8(0).toString(16));
if (keyframeBit === 0) {
keyFrameCount++;
keyFrameLastSize = encodedFrame.data.byteLength;
} else {
interFrameCount++;
interFrameLastSize = encodedFrame.data.byteLength;
}
if (encodedFrame.type === prevFrameType &&
encodedFrame.timestamp === prevFrameTimestamp &&
encodedFrame.synchronizationSource === prevFrameSynchronizationSource) {
duplicateCount++;
}
prevFrameType = encodedFrame.type;
prevFrameTimestamp = encodedFrame.timestamp;
prevFrameSynchronizationSource = encodedFrame.synchronizationSource;
controller.enqueue(encodedFrame);
}

// Update the display of the counters once a second.
setInterval(() => {
worker.onmessage = ({data: {
keyFrameCount,
interFrameCount,
keyFrameLastSize,
interFrameLastSize,
duplicateCount
}}) => {
// Update the display of the counters
keyFrameCountDisplay.innerText = keyFrameCount;
keyFrameSizeDisplay.innerText = keyFrameLastSize;
interFrameCountDisplay.innerText = interFrameCount;
interFrameSizeDisplay.innerText = interFrameLastSize;
duplicateCountDisplay.innerText = duplicateCount;
}, 500);
};

remoteVideo.addEventListener('resize', () => {
console.log(`Remote video size changed to ${remoteVideo.videoWidth}x${remoteVideo.videoHeight}`);
Expand Down
52 changes: 52 additions & 0 deletions src/content/insertable-streams/video-analyzer/js/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
onrtctransform = async ({transformer: {readable, writable, options}}) => {

Check failure on line 1 in src/content/insertable-streams/video-analyzer/js/worker.js

View workflow job for this annotation

GitHub Actions / lint

'onrtctransform' is not defined
await readable.pipeThrough(new TransformStream({
transform: videoAnalyzer
})).pipeTo(writable);
};

// accept shimming
onmessage = ({data}) => data.rtctransform && onrtctransform({transformer: data.rtctransform});

Check failure on line 8 in src/content/insertable-streams/video-analyzer/js/worker.js

View workflow job for this annotation

GitHub Actions / lint

'onrtctransform' is not defined

let keyFrameCount = 0;
let interFrameCount = 0;
let keyFrameLastSize = 0;
let interFrameLastSize = 0;
let duplicateCount = 0;
let prevFrameType;
let prevFrameTimestamp;
let prevFrameSynchronizationSource;

function videoAnalyzer(encodedFrame, controller) {
const view = new DataView(encodedFrame.data);
// We assume that the video is VP8.
// TODO: Check the codec to see that it is.
// The lowest value bit in the first byte is the keyframe indicator.
// https://tools.ietf.org/html/rfc6386#section-9.1
const keyframeBit = view.getUint8(0) & 0x01;
// console.log(view.getUint8(0).toString(16));
if (keyframeBit === 0) {
keyFrameCount++;
keyFrameLastSize = encodedFrame.data.byteLength;
} else {
interFrameCount++;
interFrameLastSize = encodedFrame.data.byteLength;
}
if (encodedFrame.type === prevFrameType &&
encodedFrame.timestamp === prevFrameTimestamp &&
encodedFrame.synchronizationSource === prevFrameSynchronizationSource) {
duplicateCount++;
}
prevFrameType = encodedFrame.type;
prevFrameTimestamp = encodedFrame.timestamp;
prevFrameSynchronizationSource = encodedFrame.synchronizationSource;
controller.enqueue(encodedFrame);
}

// Update the display of the counters once a second.
setInterval(() => self.postMessage({
keyFrameCount,
keyFrameLastSize,
interFrameCount,
interFrameLastSize,
duplicateCount
}), 500);
Loading