From bd6deaca46e48a0a7726031f474d1f71e8bf6723 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Sat, 31 Mar 2018 13:52:02 +0530 Subject: [PATCH] sendrecv/js: Call getUserMedia on incoming call Instead of registering it on page load. This will allow us to add an option for users to override the default constraints later. This is also generally nicer because the browser won't open the webcam immediately when you load the page and keep recording from it. --- webrtc/sendrecv/js/webrtc.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/webrtc/sendrecv/js/webrtc.js b/webrtc/sendrecv/js/webrtc.js index bca5d7c798..865fcac6d2 100644 --- a/webrtc/sendrecv/js/webrtc.js +++ b/webrtc/sendrecv/js/webrtc.js @@ -13,6 +13,7 @@ var peer_connection = null; var rtc_configuration = {iceServers: [{urls: "stun:stun.services.mozilla.com"}, {urls: "stun:stun.l.google.com:19302"}]}; var ws_conn; +// Promise for local stream after constraints are approved by the user var local_stream; var peer_id; @@ -54,7 +55,10 @@ function onIncomingSDP(sdp) { if (sdp.type != "offer") return; setStatus("Got SDP offer, creating answer"); - peer_connection.createAnswer().then(onLocalDescription).catch(setStatus); + local_stream.then((stream) => { + peer_connection.createAnswer() + .then(onLocalDescription).catch(setStatus); + }).catch(errorUserMediaHandler); }).catch(setStatus); } @@ -130,6 +134,17 @@ function onServerError(event) { window.setTimeout(websocketServerConnect, 3000); } +function getLocalStream() { + var constraints = {video: true, audio: true}; + + // Add local stream + if (navigator.mediaDevices.getUserMedia) { + return navigator.mediaDevices.getUserMedia(constraints); + } else { + errorUserMediaHandler(); + } +} + function websocketServerConnect() { connect_attempts++; if (connect_attempts > 3) { @@ -156,17 +171,6 @@ function websocketServerConnect() { ws_conn.addEventListener('error', onServerError); ws_conn.addEventListener('message', onServerMessage); ws_conn.addEventListener('close', onServerClose); - - var constraints = {video: true, audio: true}; - - // Add local stream - if (navigator.mediaDevices.getUserMedia) { - navigator.mediaDevices.getUserMedia(constraints) - .then((stream) => { local_stream = stream }) - .catch(errorUserMediaHandler); - } else { - errorUserMediaHandler(); - } } function onRemoteStreamAdded(event) { @@ -192,7 +196,10 @@ function createCall(msg) { peer_connection = new RTCPeerConnection(rtc_configuration); peer_connection.onaddstream = onRemoteStreamAdded; /* Send our video/audio to the other peer */ - peer_connection.addStream(local_stream); + local_stream = getLocalStream().then((stream) => { + console.log('Adding local stream'); + peer_connection.addStream(stream); + }).catch(errorUserMediaHandler); if (!msg.sdp) { console.log("WARNING: First message wasn't an SDP message!?");