Makes sure we add/remove all onended events for media streams in one central place.

This commit is contained in:
damencho 2015-10-30 17:25:20 -05:00
parent 11e1197901
commit 1ba0a182f6
5 changed files with 49 additions and 26 deletions

View File

@ -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);
}

View File

@ -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);
}
}
};

View File

@ -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 <video> element is replaced with <object>
localVideo = $('#' + localVideo.id)[0];
localVideoContainer.removeChild(localVideo);
self.VideoLayout.updateRemovedVideo(APP.xmpp.myResource());
};
});
};
LocalVideo.prototype.joined = function (jid) {

View File

@ -229,12 +229,12 @@ RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
APP.RTC.attachMediaStream(sel, stream);
}
stream.onended = function () {
console.log('stream ended', this);
APP.RTC.addMediaStreamInactiveHandler(
stream, function () {
console.log('stream ended', this);
self.removeRemoteStreamElement(stream, isVideo, newElementId);
};
self.removeRemoteStreamElement(stream, isVideo, newElementId);
});
// Add click handler.
var onClickHandler = function (event) {

View File

@ -54,12 +54,8 @@ function onEndedHandler(stream) {
if (!switchInProgress && isUsingScreenStream) {
APP.desktopsharing.toggleScreenSharing();
}
//FIXME: to be verified
if (stream.removeEventListener) {
stream.removeEventListener('ended', onEndedHandler);
} else {
stream.detachEvent('ended', onEndedHandler);
}
APP.RTC.removeMediaStreamInactiveHandler(stream, onEndedHandler);
}
module.exports = {
@ -113,16 +109,8 @@ module.exports = {
isUsingScreenStream = true;
// Hook 'ended' event to restore camera
// when screen stream stops
//FIXME: to be verified
if (stream.addEventListener) {
stream.addEventListener('ended', function () {
onEndedHandler(stream);
});
} else {
stream.attachEvent('ended', function () {
onEndedHandler(stream);
});
}
APP.RTC.addMediaStreamInactiveHandler(
stream, onEndedHandler);
newStreamCreated(stream);
},
getDesktopStreamFailed);