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

e2ee: move setCodecPreferences out of videopipe #1644

Merged
Merged
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
29 changes: 29 additions & 0 deletions src/content/insertable-streams/endtoend-encryption/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ let localStream;
// eslint-disable-next-line no-unused-vars
let remoteStream;

// Preferring a certain codec is an expert option without GUI.
// Use opus by default.
// eslint-disable-next-line prefer-const
let preferredAudioCodecMimeType = 'audio/opus';
// Use VP8 by default to limit depacketization issues.
// eslint-disable-next-line prefer-const
let preferredVideoCodecMimeType = 'video/VP8';

let hasEnoughAPIs = !!window.RTCRtpScriptTransform;

if (!hasEnoughAPIs) {
Expand Down Expand Up @@ -141,6 +149,25 @@ function setupReceiverTransform(receiver) {
}, [readable, writable]);
}

function maybeSetCodecPreferences(trackEvent) {
if (!'setCodecPreferences' in window.RTCRtpTransceiver.prototype) return;
if (trackEvent.track.kind === 'audio' && preferredAudioCodecMimeType ) {
const {codecs} = RTCRtpReceiver.getCapabilities('audio');
const selectedCodecIndex = codecs.findIndex(c => c.mimeType === preferredAudioCodecMimeType);
const selectedCodec = codecs[selectedCodecIndex];
codecs.splice(selectedCodecIndex, 1);
codecs.unshift(selectedCodec);
trackEvent.transceiver.setCodecPreferences(codecs);
} else if (trackEvent.track.kind === 'video' && preferredVideoCodecMimeType) {
const {codecs} = RTCRtpReceiver.getCapabilities('video');
const selectedCodecIndex = codecs.findIndex(c => c.mimeType === preferredVideoCodecMimeType);
const selectedCodec = codecs[selectedCodecIndex];
codecs.splice(selectedCodecIndex, 1);
codecs.unshift(selectedCodec);
trackEvent.transceiver.setCodecPreferences(codecs);
}
}

function call() {
callButton.disabled = true;
hangupButton.disabled = false;
Expand All @@ -151,13 +178,15 @@ function call() {
// to both places.
startToMiddle = new VideoPipe(localStream, true, false, e => {
// Do not setup the receiver transform.
maybeSetCodecPreferences(e);
videoMonitor.srcObject = e.streams[0];
});
startToMiddle.pc1.getSenders().forEach(setupSenderTransform);
startToMiddle.negotiate();

startToEnd = new VideoPipe(localStream, true, true, e => {
setupReceiverTransform(e.receiver);
maybeSetCodecPreferences(e);
gotRemoteStream(e.streams[0]);
});
startToEnd.pc1.getSenders().forEach(setupSenderTransform);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,14 @@
//
'use strict';

// Preferring a certain codec is an expert option without GUI.
// Use opus by default.
// eslint-disable-next-line prefer-const
let preferredAudioCodecMimeType = 'audio/opus';
// Use VP8 by default to limit depacketization issues.
// eslint-disable-next-line prefer-const
let preferredVideoCodecMimeType = 'video/VP8';

function VideoPipe(stream, forceSend, forceReceive, handler) {
this.pc1 = new RTCPeerConnection({
encodedInsertableStreams: forceSend,
});
this.pc2 = new RTCPeerConnection({
encodedInsertableStreams: forceReceive,
});
if ('setCodecPreferences' in window.RTCRtpTransceiver.prototype) {
this.pc2.ontrack = (e) => {
if (e.track.kind === 'audio' && preferredAudioCodecMimeType ) {
const {codecs} = RTCRtpReceiver.getCapabilities('audio');
const selectedCodecIndex = codecs.findIndex(c => c.mimeType === preferredAudioCodecMimeType);
const selectedCodec = codecs[selectedCodecIndex];
codecs.splice(selectedCodecIndex, 1);
codecs.unshift(selectedCodec);
e.transceiver.setCodecPreferences(codecs);
} else if (e.track.kind === 'video' && preferredVideoCodecMimeType) {
const {codecs} = RTCRtpReceiver.getCapabilities('video');
const selectedCodecIndex = codecs.findIndex(c => c.mimeType === preferredVideoCodecMimeType);
const selectedCodec = codecs[selectedCodecIndex];
codecs.splice(selectedCodecIndex, 1);
codecs.unshift(selectedCodec);
e.transceiver.setCodecPreferences(codecs);
}
handler(e);
};
} else {
this.pc2.ontrack = handler;
}
this.pc2.ontrack = handler;
stream.getTracks().forEach((track) => this.pc1.addTrack(track, stream));
}

Expand Down
Loading