Skip to content

Commit

Permalink
Merge pull request #1644 from fippo/setcodecprefs-changes-make-life-s…
Browse files Browse the repository at this point in the history
…impler

e2ee: move setCodecPreferences out of videopipe
  • Loading branch information
fippo authored Nov 29, 2023
2 parents 16be3b1 + 165fd50 commit 4f021a8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
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

0 comments on commit 4f021a8

Please sign in to comment.