diff --git a/samples/js/apprtc/apprtc.py b/samples/js/apprtc/apprtc.py index bd609cef4a..e9e61c6edd 100644 --- a/samples/js/apprtc/apprtc.py +++ b/samples/js/apprtc/apprtc.py @@ -327,6 +327,10 @@ class MainPage(webapp2.RequestHandler): if debug == 'loopback': # set compat to false as DTLS does not work for loopback. compat = 'false' + # set stereo to false by default + stereo = 'false' + if self.request.get('stereo'): + stereo = self.request.get('stereo') # token_timeout for channel creation, default 30min, max 2 days, min 3min. @@ -390,7 +394,8 @@ class MainPage(webapp2.RequestHandler): 'pc_constraints': json.dumps(pc_constraints), 'offer_constraints': json.dumps(offer_constraints), 'media_constraints': json.dumps(media_constraints), - 'turn_url': turn_url + 'turn_url': turn_url, + 'stereo': stereo } if unittest: target_page = 'test/test_' + unittest + '.html' diff --git a/samples/js/apprtc/index.html b/samples/js/apprtc/index.html index e87bfd56c3..72a46dacad 100644 --- a/samples/js/apprtc/index.html +++ b/samples/js/apprtc/index.html @@ -125,6 +125,7 @@ 'OfferToReceiveVideo':true }}; var isVideoMuted = false; var isAudioMuted = false; + var stereo = false; function initialize() { console.log("Initializing; room={{ room_key }}."); @@ -137,6 +138,7 @@ // changing here. openChannel('{{ token }}'); requestTurn('{{ turn_url }}'); + stereo = {{ stereo }}; doGetUserMedia(); } @@ -292,15 +294,19 @@ function processSignalingMessage(message) { var msg = JSON.parse(message); - if (msg.type === 'offer') { // Callee creates PeerConnection if (!initiator && !started) maybeStart(); - + // Set Opus in Stereo, if stereo enabled. + if (stereo) + msg.sdp = addStereo(msg.sdp); pc.setRemoteDescription(new RTCSessionDescription(msg)); doAnswer(); } else if (msg.type === 'answer' && started) { + // Set Opus in Stereo, if stereo enabled. + if (stereo) + msg.sdp = addStereo(msg.sdp); pc.setRemoteDescription(new RTCSessionDescription(msg)); } else if (msg.type === 'candidate' && started) { var candidate = new RTCIceCandidate({sdpMLineIndex:msg.label, @@ -535,6 +541,39 @@ return sdp; } + // Set Opus in stereo if stereo is enabled. + function addStereo(sdp) { + var sdpLines = sdp.split('\r\n'); + + // Find opus payload. + for (var i = 0; i < sdpLines.length; i++) { + if (sdpLines[i].search('opus/48000') !== -1) { + var opusPayload = extractSdp(sdpLines[i], /:(\d+) opus\/48000/i); + break; + } + } + + // Find the payload in fmtp line. + for (var i = 0; i < sdpLines.length; i++) { + if (sdpLines[i].search('a=fmtp') !== -1) { + var payload = extractSdp(sdpLines[i], /a=fmtp:(\d+)/ ); + if (payload === opusPayload) { + var fmtpLineIndex = i; + break; + } + } + } + // No fmtp line found. + if (fmtpLineIndex === null) + return sdp; + + // append stereo=1 to fmtp line. + sdpLines[fmtpLineIndex] = sdpLines[fmtpLineIndex].concat(' stereo=1'); + + sdp = sdpLines.join('\r\n'); + return sdp; + } + function extractSdp(sdpLine, pattern) { var result = sdpLine.match(pattern); return (result && result.length == 2)? result[1]: null;