From 1ba0a182f6fa0a3b0591216bf233e6c005a3f32f Mon Sep 17 00:00:00 2001 From: damencho Date: Fri, 30 Oct 2015 17:25:20 -0500 Subject: [PATCH] Makes sure we add/remove all onended events for media streams in one central place. --- modules/RTC/LocalStream.js | 9 ++++--- modules/RTC/RTC.js | 31 ++++++++++++++++++++++++ modules/UI/videolayout/LocalVideo.js | 5 ++-- modules/UI/videolayout/RemoteVideo.js | 10 ++++---- modules/desktopsharing/desktopsharing.js | 20 +++------------ 5 files changed, 49 insertions(+), 26 deletions(-) diff --git a/modules/RTC/LocalStream.js b/modules/RTC/LocalStream.js index acd18ac50..4fbe30401 100644 --- a/modules/RTC/LocalStream.js +++ b/modules/RTC/LocalStream.js @@ -39,9 +39,12 @@ function LocalStream(stream, type, eventEmitter, videoType, isGUMStream) { }; } - this.stream.onended = function () { - self.streamEnded(); - }; + APP.RTC.addMediaStreamInactiveHandler( + this.stream, + function () { + self.streamEnded(); + }); + if (RTCBrowserType.isFirefox()) { implementOnEndedHandling(this.stream); } diff --git a/modules/RTC/RTC.js b/modules/RTC/RTC.js index b788fb0c9..e8742cac0 100644 --- a/modules/RTC/RTC.js +++ b/modules/RTC/RTC.js @@ -266,6 +266,7 @@ var RTC = { /** * A method to handle stopping of the stream. * One point to handle the differences in various implementations. + * @param mediaStream MediaStream object to stop. */ stopMediaStream: function (mediaStream) { mediaStream.getAudioTracks().forEach(function (track) { @@ -285,6 +286,36 @@ var RTC = { if (mediaStream.stop) { mediaStream.stop(); } + }, + /** + * Adds onended handler to a MediaStream. + * @param mediaStream a MediaStream to attach onended handler + * @param handler the handler + */ + addMediaStreamInactiveHandler: function (mediaStream, handler) { + if (mediaStream.addEventListener) { + // chrome + mediaStream.onended = handler; + } else { + // themasys + mediaStream.attachEvent('ended', function () { + handler(mediaStream); + }); + } + }, + /** + * Removes onended handler. + * @param mediaStream the MediaStream to remove the handler from. + * @param handler the handler to remove. + */ + removeMediaStreamInactiveHandler: function (mediaStream, handler) { + if (mediaStream.removeEventListener) { + // chrome + mediaStream.onended = null; + } else { + // themasys + mediaStream.detachEvent('ended', handler); + } } }; diff --git a/modules/UI/videolayout/LocalVideo.js b/modules/UI/videolayout/LocalVideo.js index 4fd130420..0a6c49c5b 100644 --- a/modules/UI/videolayout/LocalVideo.js +++ b/modules/UI/videolayout/LocalVideo.js @@ -208,13 +208,14 @@ LocalVideo.prototype.changeVideo = function (stream, isMuted) { APP.RTC.attachMediaStream(localVideoSelector, stream.getOriginalStream()); // Add stream ended handler - stream.getOriginalStream().onended = function () { + APP.RTC.addMediaStreamInactiveHandler( + stream.getOriginalStream(), function () { // We have to re-select after attach when Temasys plugin is used, // because