Skip to content

Commit

Permalink
multiple: allow using setCodecPreferences to select codec
Browse files Browse the repository at this point in the history
  • Loading branch information
fippo committed Jul 4, 2024
1 parent b168cec commit fa718e5
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/content/peerconnection/multiple/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const video1 = document.querySelector('video#video1');
const video2 = document.querySelector('video#video2');
const video3 = document.querySelector('video#video3');

// eslint-disable-next-line prefer-const
let preferredVideoCodecMimeType = 'video/VP8';

let localStream;
let pc1Local;
let pc1Remote;
Expand All @@ -31,6 +34,20 @@ const offerOptions = {
offerToReceiveVideo: 1
};

const supportsSetCodecPreferences = window.RTCRtpTransceiver &&
'setCodecPreferences' in window.RTCRtpTransceiver.prototype;
function maybeSetCodecPreferences(trackEvent) {
if (!supportsSetCodecPreferences) return;
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 gotStream(stream) {
console.log('Received local stream');
video1.srcObject = stream;
Expand Down Expand Up @@ -94,10 +111,10 @@ function onCreateSessionDescriptionError(error) {
console.log(`Failed to create session description: ${error.toString()}`);
}

function gotDescription1Local(desc) {
async function gotDescription1Local(desc) {
pc1Local.setLocalDescription(desc);
console.log(`Offer from pc1Local\n${desc.sdp}`);
pc1Remote.setRemoteDescription(desc);
await pc1Remote.setRemoteDescription(desc);
// Since the 'remote' side has no media stream we need
// to pass in the right constraints in order for it to
// accept the incoming offer of audio and video.
Expand All @@ -110,10 +127,10 @@ function gotDescription1Remote(desc) {
pc1Local.setRemoteDescription(desc);
}

function gotDescription2Local(desc) {
async function gotDescription2Local(desc) {
pc2Local.setLocalDescription(desc);
console.log(`Offer from pc2Local\n${desc.sdp}`);
pc2Remote.setRemoteDescription(desc);
await pc2Remote.setRemoteDescription(desc);
// Since the 'remote' side has no media stream we need
// to pass in the right constraints in order for it to
// accept the incoming offer of audio and video.
Expand All @@ -139,13 +156,15 @@ function hangup() {
}

function gotRemoteStream1(e) {
maybeSetCodecPreferences(e);
if (video2.srcObject !== e.streams[0]) {
video2.srcObject = e.streams[0];
console.log('pc1: received remote stream');
}
}

function gotRemoteStream2(e) {
maybeSetCodecPreferences(e);
if (video3.srcObject !== e.streams[0]) {
video3.srcObject = e.streams[0];
console.log('pc2: received remote stream');
Expand Down

0 comments on commit fa718e5

Please sign in to comment.