From fe40c70536f0b42ec8937bd9ff8f4acda9e3f0b0 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Sun, 1 Apr 2018 00:41:40 +0530 Subject: [PATCH] sendrecv/js: Simplify local stream management Just use the fulfilled value of the promise directly instead of storing it separately --- webrtc/sendrecv/js/webrtc.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/webrtc/sendrecv/js/webrtc.js b/webrtc/sendrecv/js/webrtc.js index bc97373d40..a8386578d8 100644 --- a/webrtc/sendrecv/js/webrtc.js +++ b/webrtc/sendrecv/js/webrtc.js @@ -21,8 +21,6 @@ var peer_connection; var ws_conn; // Promise for local stream after constraints are approved by the user var local_stream_promise; -// MediaStream: the local stream -var local_stream; function getOurId() { return Math.floor(Math.random() * (9000 - 10) + 10).toString(); @@ -59,9 +57,7 @@ function setError(text) { function resetVideo() { // Release the webcam and mic - if (local_stream != null) - local_stream.stop(); - local_stream = null; + local_stream_promise.then(stream => { stream.stop(); }); // Reset the video element and stop showing the last received frame var videoElement = getVideoElement(); @@ -219,7 +215,6 @@ function errorUserMediaHandler() { function createCall(msg) { // Reset connection attempts because we connected successfully connect_attempts = 0; - local_stream = null; console.log('Creating RTCPeerConnection'); @@ -229,7 +224,7 @@ function createCall(msg) { local_stream_promise = getLocalStream().then((stream) => { console.log('Adding local stream'); peer_connection.addStream(stream); - local_stream = stream; + return stream; }).catch(setError); if (!msg.sdp) {