@@ -21,6 +21,9 @@ const video1 = document.querySelector('video#video1');
2121const video2 = document . querySelector ( 'video#video2' ) ;
2222const video3 = document . querySelector ( 'video#video3' ) ;
2323
24+ // eslint-disable-next-line prefer-const
25+ let preferredVideoCodecMimeType = 'video/VP8' ;
26+
2427let localStream ;
2528let pc1Local ;
2629let pc1Remote ;
@@ -31,6 +34,20 @@ const offerOptions = {
3134 offerToReceiveVideo : 1
3235} ;
3336
37+ const supportsSetCodecPreferences = window . RTCRtpTransceiver &&
38+ 'setCodecPreferences' in window . RTCRtpTransceiver . prototype ;
39+ function maybeSetCodecPreferences ( trackEvent ) {
40+ if ( ! supportsSetCodecPreferences ) return ;
41+ if ( trackEvent . track . kind === 'video' && preferredVideoCodecMimeType ) {
42+ const { codecs} = RTCRtpReceiver . getCapabilities ( 'video' ) ;
43+ const selectedCodecIndex = codecs . findIndex ( c => c . mimeType === preferredVideoCodecMimeType ) ;
44+ const selectedCodec = codecs [ selectedCodecIndex ] ;
45+ codecs . splice ( selectedCodecIndex , 1 ) ;
46+ codecs . unshift ( selectedCodec ) ;
47+ trackEvent . transceiver . setCodecPreferences ( codecs ) ;
48+ }
49+ }
50+
3451function gotStream ( stream ) {
3552 console . log ( 'Received local stream' ) ;
3653 video1 . srcObject = stream ;
@@ -94,10 +111,10 @@ function onCreateSessionDescriptionError(error) {
94111 console . log ( `Failed to create session description: ${ error . toString ( ) } ` ) ;
95112}
96113
97- function gotDescription1Local ( desc ) {
114+ async function gotDescription1Local ( desc ) {
98115 pc1Local . setLocalDescription ( desc ) ;
99116 console . log ( `Offer from pc1Local\n${ desc . sdp } ` ) ;
100- pc1Remote . setRemoteDescription ( desc ) ;
117+ await pc1Remote . setRemoteDescription ( desc ) ;
101118 // Since the 'remote' side has no media stream we need
102119 // to pass in the right constraints in order for it to
103120 // accept the incoming offer of audio and video.
@@ -110,10 +127,10 @@ function gotDescription1Remote(desc) {
110127 pc1Local . setRemoteDescription ( desc ) ;
111128}
112129
113- function gotDescription2Local ( desc ) {
130+ async function gotDescription2Local ( desc ) {
114131 pc2Local . setLocalDescription ( desc ) ;
115132 console . log ( `Offer from pc2Local\n${ desc . sdp } ` ) ;
116- pc2Remote . setRemoteDescription ( desc ) ;
133+ await pc2Remote . setRemoteDescription ( desc ) ;
117134 // Since the 'remote' side has no media stream we need
118135 // to pass in the right constraints in order for it to
119136 // accept the incoming offer of audio and video.
@@ -139,13 +156,15 @@ function hangup() {
139156}
140157
141158function gotRemoteStream1 ( e ) {
159+ maybeSetCodecPreferences ( e ) ;
142160 if ( video2 . srcObject !== e . streams [ 0 ] ) {
143161 video2 . srcObject = e . streams [ 0 ] ;
144162 console . log ( 'pc1: received remote stream' ) ;
145163 }
146164}
147165
148166function gotRemoteStream2 ( e ) {
167+ maybeSetCodecPreferences ( e ) ;
149168 if ( video3 . srcObject !== e . streams [ 0 ] ) {
150169 video3 . srcObject = e . streams [ 0 ] ;
151170 console . log ( 'pc2: received remote stream' ) ;
0 commit comments