Skip to content

Commit

Permalink
use the getReceivers method to know which streams to remove
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc committed May 23, 2018
1 parent edfbcac commit 66fde13
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
10 changes: 9 additions & 1 deletion app/script/calling/entities/FlowEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,15 @@ z.calling.entities.FlowEntity = class FlowEntity {

const isStateClosed = peerConnection.signalingState === z.calling.rtc.SIGNALING_STATE.CLOSED;
if (!isStateClosed) {
amplify.publish(z.event.WebApp.CALL.MEDIA.REMOVE_STREAM, peerConnection.getRemoteStreams());
let connectionMediaStreamTracks;
if (peerConnection.getReceivers) {
connectionMediaStreamTracks = peerConnection.getReceivers().map(receiver => receiver.track);
} else {
connectionMediaStreamTracks = peerConnection
.getRemoteStreams()
.reduce((tracks, stream) => tracks.concat(stream.getTracks()), []);
}
amplify.publish(z.event.WebApp.CALL.MEDIA.CONNECTION_CLOSED, connectionMediaStreamTracks);
peerConnection.close();

const logMessage = {
Expand Down
2 changes: 1 addition & 1 deletion app/script/event/WebApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ z.event.WebApp = {
MEDIA: {
ADD_STREAM: 'wire.webapp.call.media.add_stream',
CHOOSE_SCREEN: 'wire.webapp.call.media.choose_screen',
CONNECTION_CLOSED: 'wire.webapp.call.media.connection_closed',
MUTE_AUDIO: 'wire.webapp.call.media.mute_audio',
REMOVE_STREAM: 'wire.webapp.call.media.remove_stream',
TOGGLE: 'wire.webapp.call.media.toggle',
},
SIGNALING: {
Expand Down
27 changes: 22 additions & 5 deletions app/script/media/MediaStreamHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ z.media.MediaStreamHandler = class MediaStreamHandler {

this.request_hint_timeout = undefined;
amplify.subscribe(z.event.WebApp.CALL.MEDIA.ADD_STREAM, this.addRemoteMediaStream.bind(this));
amplify.subscribe(z.event.WebApp.CALL.MEDIA.REMOVE_STREAM, this.removeRemoteMediaStreams.bind(this));
amplify.subscribe(z.event.WebApp.CALL.MEDIA.CONNECTION_CLOSED, this.removeRemoteMediaStreamTracks.bind(this));
}

//##############################################################################
Expand Down Expand Up @@ -525,10 +525,27 @@ z.media.MediaStreamHandler = class MediaStreamHandler {
this.element_handler.add_media_element(mediaStreamInfo);
}

removeRemoteMediaStreams(streams) {
const streamIds = streams.map(stream => stream.id);
this.remote_media_streams.video.remove(stream => streamIds.includes(stream.id));
this.remote_media_streams.audio.remove(stream => streamIds.includes(stream.id));
/**
* Removes the given tracks from the streams containing them.
* If a stream ends up having no tracks, it gets filtered out from the array of streams
* removeRemoteMediaStreamTracks
*
* @param {MediaStreamTrack[]} remoteTracks - the tracks to remove
* @returns {void} - void
*/
removeRemoteMediaStreamTracks(remoteTracks) {
const removeTracks = (streams, tracks) => {
return streams
.map(stream => {
tracks.forEach(track => stream.removeTrack(track));
return stream;
})
.filter(stream => stream.getTracks().length > 0);
};

[this.remote_media_streams.video, this.remote_media_streams.audio].forEach(streams => {
streams(removeTracks(streams(), remoteTracks));
});
}

//##############################################################################
Expand Down

0 comments on commit 66fde13

Please sign in to comment.