Skip to content

Commit

Permalink
ref(JingleSessionPC): emit track added/removed in RTC
Browse files Browse the repository at this point in the history
  • Loading branch information
paweldomas committed Feb 27, 2017
1 parent 7c561bc commit 122cce2
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 292 deletions.
62 changes: 53 additions & 9 deletions JitsiConference.js
Expand Up @@ -171,7 +171,7 @@ JitsiConference.prototype.leave = function () {
}

// FXIME check if should be conference.removeTrack instead
this.getLocalTracks().forEach(track => this.onTrackRemoved(track));
this.getLocalTracks().forEach(track => this.onLocalTrackRemoved(track));

this.rtc.closeAllDataChannels();
if(this.statistics)
Expand Down Expand Up @@ -395,12 +395,14 @@ JitsiConference.prototype.getTranscriber = function(){
if(this.transcriber === undefined){
this.transcriber = new Transcriber();
//add all existing local audio tracks to the transcriber
// FIXME accessing localTracks field directly
this.rtc.localTracks.forEach(function (localTrack) {
if(localTrack.isAudioTrack()){
this.transcriber.addTrack(localTrack);
}
}.bind(this));
//and all remote audio tracks
// FIXME accessing remoteTracks field directly
this.rtc.remoteTracks.forEach(function (remoteTrack){
if(remoteTrack.isAudioTrack()){
this.transcriber.addTrack(remoteTrack);
Expand Down Expand Up @@ -464,7 +466,7 @@ JitsiConference.prototype._fireMuteChangeEvent = function (track) {
* Clear JitsiLocalTrack properties and listeners.
* @param track the JitsiLocalTrack object.
*/
JitsiConference.prototype.onTrackRemoved = function (track) {
JitsiConference.prototype.onLocalTrackRemoved = function (track) {
track._setSSRC(null);
track._setConference(null);
this.rtc.removeLocalTrack(track);
Expand Down Expand Up @@ -532,7 +534,7 @@ JitsiConference.prototype.replaceTrack = function (oldTrack, newTrack) {
return this._doReplaceTrack(oldTrack, newTrack)
.then(() => {
if (oldTrack) {
this.onTrackRemoved(oldTrack);
this.onLocalTrackRemoved(oldTrack);
}
if (newTrack) {
// Now handle the addition of the newTrack at the JitsiConference level
Expand Down Expand Up @@ -896,16 +898,17 @@ JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
};

/**
* Notifies this JitsiConference that a JitsiRemoteTrack was added (into the
* ChatRoom of this JitsiConference).
* Notifies this JitsiConference that a JitsiRemoteTrack was added into
* the conference.
*
* @param {JitsiRemoteTrack} track the JitsiRemoteTrack which was added to this
* JitsiConference
*/
JitsiConference.prototype.onTrackAdded = function (track) {
var id = track.getParticipantId();
var participant = this.getParticipantById(id);
JitsiConference.prototype.onRemoteTrackAdded = function (track) {
const id = track.getParticipantId();
const participant = this.getParticipantById(id);
if (!participant) {
logger.error("No participant found for id: " + id);
return;
}

Expand All @@ -916,7 +919,7 @@ JitsiConference.prototype.onTrackAdded = function (track) {
this.transcriber.addTrack(track);
}

var emitter = this.eventEmitter;
const emitter = this.eventEmitter;
track.addEventListener(
JitsiTrackEvents.TRACK_MUTE_CHANGED,
function () {
Expand All @@ -936,6 +939,47 @@ JitsiConference.prototype.onTrackAdded = function (track) {
emitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
};

/**
* Notifies this JitsiConference that a JitsiRemoteTrack was removed from
* the conference.
*
* @param {JitsiRemoteTrack} removedTrack
*/
JitsiConference.prototype.onRemoteTrackRemoved = function (removedTrack) {
let consumed = false;

this.getParticipants().forEach(function(participant) {
const tracks = participant.getTracks();

for(let i = 0; i < tracks.length; i++) {
if(tracks[i] === removedTrack) {
// Since the tracks have been compared and are
// considered equal the result of splice can be ignored.
participant._tracks.splice(i, 1);

this.eventEmitter.emit(
JitsiConferenceEvents.TRACK_REMOVED, removedTrack);

if(this.transcriber){
this.transcriber.removeTrack(removedTrack);
}

consumed = true;

break;
}
}
}, this);

if (!consumed) {
logger.error(
"Failed to match remote track on remove"
+ " with any of the participants",
removedTrack.getStreamId(),
removedTrack.getParticipantId());
}
};

/**
* Handles incoming call event.
*/
Expand Down
44 changes: 7 additions & 37 deletions JitsiConferenceEventManager.js
Expand Up @@ -400,43 +400,13 @@ JitsiConferenceEventManager.prototype.setupRTCListeners = function () {
this.rtcForwarder
= new EventEmitterForwarder(rtc, this.conference.eventEmitter);

// FIXME REMOTE_TRACK_ADDED could come with JitsiRemoteTrack instance
rtc.addListener(RTCEvents.REMOTE_TRACK_ADDED,
function (data) {
const track = rtc.createRemoteTrack(data);
if (track) {
conference.onTrackAdded(track);
}
}
);
rtc.addListener(RTCEvents.REMOTE_TRACK_REMOVED,
function (streamId, trackId) {
conference.getParticipants().forEach(function(participant) {
const tracks = participant.getTracks();
for(let i = 0; i < tracks.length; i++) {
if(tracks[i]
&& tracks[i].getStreamId() == streamId
&& tracks[i].getTrackId() == trackId) {
const track = participant._tracks.splice(i, 1)[0];

// FIXME this can be done in RTC directly,
// before firing the event
rtc.removeRemoteTrack(
participant.getId(), track.getType());

conference.eventEmitter.emit(
JitsiConferenceEvents.TRACK_REMOVED, track);

if(conference.transcriber){
conference.transcriber.removeTrack(track);
}

return;
}
}
});
}
);
rtc.addListener(
RTCEvents.REMOTE_TRACK_ADDED,
conference.onRemoteTrackAdded.bind(conference));

rtc.addListener(
RTCEvents.REMOTE_TRACK_REMOVED,
conference.onRemoteTrackRemoved.bind(conference));

rtc.addListener(RTCEvents.DOMINANT_SPEAKER_CHANGED,
function (id) {
Expand Down
30 changes: 17 additions & 13 deletions modules/RTC/JitsiRemoteTrack.js
@@ -1,4 +1,4 @@
/* global Strophe */
/* global */

var JitsiTrack = require("./JitsiTrack");
import * as JitsiTrackEvents from "../../JitsiTrackEvents";
Expand All @@ -12,22 +12,25 @@ var ttfmTrackerVideoAttached = false;

/**
* Represents a single media track (either audio or video).
* @param rtc {RTC} the RTC service instance.
* @param ownerJid the MUC JID of the track owner
* @param stream WebRTC MediaStream, parent of the track
* @param track underlying WebRTC MediaStreamTrack for new JitsiRemoteTrack
* @param mediaType the MediaType of the JitsiRemoteTrack
* @param videoType the VideoType of the JitsiRemoteTrack
* @param ssrc the SSRC number of the Media Stream
* @param muted initial muted state of the JitsiRemoteTrack
* @param {RTC} rtc the RTC service instance.
* @param {JitsiConference} conference the conference to which this track
* belongs to
* @param {string} owner the endpoint ID of the track owner
* @param {MediaStream} stream WebRTC MediaStream, parent of the track
* @param {MediaStreamTrack} track underlying WebRTC MediaStreamTrack for
* the new JitsiRemoteTrack
* @param {MediaType} mediaType the type of the media
* @param {VideoType} videoType the type of the video if applicable
* @param {string} ssrc the SSRC number of the Media Stream
* @param {boolean} muted the initial muted state
* @constructor
*/
function JitsiRemoteTrack(rtc, conference, ownerJid, stream, track, mediaType, videoType,
function JitsiRemoteTrack(rtc, conference, owner, stream, track, mediaType, videoType,
ssrc, muted) {
JitsiTrack.call(
this, conference, stream, track, function () {}, mediaType, videoType, ssrc);
this.rtc = rtc;
this.peerjid = ownerJid;
this.owner = owner;
this.muted = muted;
// we want to mark whether the track has been ever muted
// to detect ttfm events for startmuted conferences, as it can significantly
Expand Down Expand Up @@ -98,10 +101,11 @@ JitsiRemoteTrack.prototype.isMuted = function () {

/**
* Returns the participant id which owns the track.
* @returns {string} the id of the participants.
* @returns {string} the id of the participants. It corresponds to the Colibri
* endpoint id/MUC nickname in case of Jitsi-meet.
*/
JitsiRemoteTrack.prototype.getParticipantId = function() {
return Strophe.getResourceFromJid(this.peerjid);
return this.owner;
};

/**
Expand Down

0 comments on commit 122cce2

Please sign in to comment.