Skip to content

Commit

Permalink
ref(RTC): cleanup local track access
Browse files Browse the repository at this point in the history
  • Loading branch information
paweldomas committed Feb 27, 2017
1 parent ea51796 commit bed6c84
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 78 deletions.
61 changes: 36 additions & 25 deletions JitsiConference.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,27 @@ JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
JitsiConference.prototype.getLocalTracks = function (mediaType) {
let tracks = [];
if (this.rtc) {
tracks = this.rtc.localTracks.slice();
}
if (mediaType !== undefined) {
tracks = tracks.filter(
(track) => {
return track && track.getType && track.getType() === mediaType;
});
tracks = this.rtc.getLocalTracks(mediaType);
}
return tracks;
};

/**
* Obtains local audio track.
* @return {JitsiLocalTrack|undefined}
*/
JitsiConference.prototype.getLocalAudioTrack = function () {
return this.rtc ? this.rtc.getLocalAudioTrack() : undefined;
};

/**
* Obtains local video track.
* @return {JitsiLocalTrack|undefined}
*/
JitsiConference.prototype.getLocalVideoTrack = function () {
return this.rtc ? this.rtc.getLocalVideoTrack() : undefined;
};

/**
* Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
* in JitsiConferenceEvents.
Expand Down Expand Up @@ -394,18 +404,15 @@ 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));
this.getLocalTracks(MediaType.AUDIO).forEach(
function (localAudio) {
this.transcriber.addTrack(localAudio);
}, this);
//and all remote audio tracks
this.rtc.getRemoteTracks(MediaType.AUDIO).forEach(
function (remoteTrack){
this.transcriber.addTrack(remoteTrack);
}.bind(this)
);
}, this);
}
return this.transcriber;
};
Expand Down Expand Up @@ -1029,7 +1036,7 @@ function (jingleSession, jingleOffer, now) {

this.rtc.initializeDataChannels(jingleSession.peerconnection);
// Add local Tracks to the ChatRoom
this.rtc.localTracks.forEach(function(localTrack) {
this.getLocalTracks().forEach(function(localTrack) {
var ssrcInfo = null;
/**
* We don't do this for Firefox because, on Firefox, we keep the
Expand Down Expand Up @@ -1125,7 +1132,7 @@ JitsiConference.prototype.onCallEnded
// will learn what their SSRC from the new PeerConnection which will be
// created on incoming call event.
var self = this;
this.rtc.localTracks.forEach(function(localTrack) {
this.getLocalTracks().forEach(function(localTrack) {
// Reset SSRC as it will no longer be valid
localTrack._setSSRC(null);
// Bind the handler to fetch new SSRC, it will un register itself once
Expand Down Expand Up @@ -1178,21 +1185,25 @@ JitsiConference.prototype.myUserId = function () {
};

JitsiConference.prototype.sendTones = function (tones, duration, pause) {
// FIXME P2P 'dtmfManager' must be cleared, after switching jingleSessions
if (!this.dtmfManager) {
var connection = this.xmpp.connection.jingle.activecall.peerconnection;
if (!connection) {
logger.warn("cannot sendTones: no conneciton");
if (!this.jingleSession) {
logger.warn("cannot sendTones: no jingle session");
return;
}

var tracks = this.getLocalTracks().filter(function (track) {
return track.isAudioTrack();
});
if (!tracks.length) {
const peerConnection = this.jingleSession.peerconnection;
if (!peerConnection) {
logger.warn("cannot sendTones: no peer connection");
return;
}

const localAudio = this.getLocalAudioTrack();
if (!localAudio) {
logger.warn("cannot sendTones: no local audio stream");
return;
}
this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
this.dtmfManager = new JitsiDTMFManager(localAudio, peerConnection);
}

this.dtmfManager.sendTones(tones, duration, pause);
Expand Down
6 changes: 3 additions & 3 deletions JitsiConferenceEventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,9 @@ JitsiConferenceEventManager.prototype.setupStatisticsListeners = function () {
});

conference.statistics.addByteSentStatsListener(function (stats) {
conference.getLocalTracks().forEach(function (track) {
var ssrc = track.getSSRC();
if(!track.isAudioTrack() || !ssrc || !stats.hasOwnProperty(ssrc))
conference.getLocalTracks(MediaType.AUDIO).forEach(function (track) {
const ssrc = track.getSSRC();
if(!ssrc || !stats.hasOwnProperty(ssrc))
return;

track._setByteSent(stats[ssrc]);
Expand Down
9 changes: 0 additions & 9 deletions modules/RTC/JitsiTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,6 @@ JitsiTrack.prototype.dispose = function () {
JitsiTrack.prototype.isScreenSharing = function() {
};

/**
* FIXME remove hack in SDP.js and this method
* Returns id of the track.
* @returns {string|null} id of the track or null if this is fake track.
*/
JitsiTrack.prototype._getId = function () {
return this.getTrackId();
};

/**
* Returns id of the track.
* @returns {string|null} id of the track or null if this is fake track.
Expand Down
65 changes: 36 additions & 29 deletions modules/RTC/RTC.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ export default class RTC extends Listenable {
this.localTracks = [];
//FIXME: We should support multiple streams per jid.
this.remoteTracks = {};
this.localAudio = null;
this.localVideo = null;
this.options = options;
// A flag whether we had received that the data channel had opened
// we can get this flag out of sync if for some reason data channel got
Expand Down Expand Up @@ -258,20 +256,39 @@ export default class RTC extends Listenable {
this.localTracks.push(track);

track.conference = this.conference;

if (track.isAudioTrack()) {
this.localAudio = track;
} else {
this.localVideo = track;
}
}

/**
* Get local video track.
* @returns {JitsiLocalTrack}
* @returns {JitsiLocalTrack|undefined}
*/
getLocalVideoTrack () {
return this.localVideo;
const localVideo = this.getLocalTracks(MediaType.VIDEO);
return localVideo.length ? localVideo[0] : undefined;
}

/**
* Get local audio track.
* @returns {JitsiLocalTrack|undefined}
*/
getLocalAudioTrack () {
const localAudio = this.getLocalTracks(MediaType.AUDIO);
return localAudio.length ? localAudio[0] : undefined;
}

/**
* Returns the local tracks of the given media type, or all local tracks if
* no specific type is given.
* @param {MediaType} [mediaType] optional media type filter
* (audio or video).
*/
getLocalTracks (mediaType) {
let tracks = this.localTracks.slice();
if (mediaType !== undefined) {
tracks = tracks.filter(
(track) => { return track.getType() === mediaType; });
}
return tracks;
}

/**
Expand Down Expand Up @@ -345,32 +362,22 @@ export default class RTC extends Listenable {
* @returns {Promise}
*/
setAudioMute (value) {
var mutePromises = [];
for(var i = 0; i < this.localTracks.length; i++) {
var track = this.localTracks[i];
if(track.getType() !== MediaType.AUDIO) {
continue;
}
const mutePromises = [];
this.getLocalTracks(MediaType.AUDIO).forEach(function(audioTrack){
// this is a Promise
mutePromises.push(value ? track.mute() : track.unmute());
}
mutePromises.push(value ? audioTrack.mute() : audioTrack.unmute());
});
// we return a Promise from all Promises so we can wait for their execution
return Promise.all(mutePromises);
}

removeLocalTrack (track) {
var pos = this.localTracks.indexOf(track);
const pos = this.localTracks.indexOf(track);
if (pos === -1) {
return;
}

this.localTracks.splice(pos, 1);

if (track.isAudioTrack()) {
this.localAudio = null;
} else {
this.localVideo = null;
}
}

/**
Expand Down Expand Up @@ -638,13 +645,13 @@ export default class RTC extends Listenable {
* @param ssrc the ssrc to check.
*/
getResourceBySSRC (ssrc) {
if((this.localVideo && ssrc == this.localVideo.getSSRC())
|| (this.localAudio && ssrc == this.localAudio.getSSRC())) {
if (this.getLocalTracks().find(
function(localTrack){ return localTrack.getSSRC() == ssrc; })) {
return this.conference.myUserId();
}

var track = this.getRemoteTrackBySSRC(ssrc);
return track? track.getParticipantId() : null;
const track = this.getRemoteTrackBySSRC(ssrc);
return track ? track.getParticipantId() : null;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions modules/connectivity/ConnectionQuality.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {getLogger} from "jitsi-meet-logger";
import RTCBrowserType from "../RTC/RTCBrowserType";

var XMPPEvents = require('../../service/xmpp/XMPPEvents');
var MediaType = require('../../service/RTC/MediaType');
var VideoType = require('../../service/RTC/VideoType');
var Resolutions = require("../../service/RTC/Resolutions");

Expand Down Expand Up @@ -359,8 +358,7 @@ export default class ConnectionQuality {
// about the resolution, and they look at their local rendered
// resolution instead. Consider removing this.
let localVideoTrack
= this._conference.getLocalTracks(MediaType.VIDEO)
.find(track => track.isVideoTrack());
= this._conference.getLocalVideoTrack();
if (localVideoTrack && localVideoTrack.resolution) {
data.resolution = localVideoTrack.resolution;
}
Expand Down Expand Up @@ -390,9 +388,8 @@ export default class ConnectionQuality {
let key;
let updateLocalConnectionQuality
= !this._conference.isConnectionInterrupted();
let localVideoTrack =
this._conference.getLocalTracks(MediaType.VIDEO)
.find(track => track.isVideoTrack());
let localVideoTrack
= this._conference.getLocalVideoTrack();
let videoType = localVideoTrack ? localVideoTrack.videoType : undefined;
let isMuted = localVideoTrack ? localVideoTrack.isMuted() : true;
let resolution = localVideoTrack ? localVideoTrack.resolution : null;
Expand Down
14 changes: 8 additions & 6 deletions modules/xmpp/SDP.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,14 @@ SDP.prototype.toJingle = function (elem, thecreator) {
elem.c('parameter');
elem.attrs({name: "cname", value:Math.random().toString(36).substring(7)});
elem.up();
var msid = null;
if(mline.media == "audio") {
// FIXME what is this ? global APP.RTC in SDP ?
msid = APP.RTC.localAudio._getId();
} else {
msid = APP.RTC.localVideo._getId();
// FIXME what case does this code handle ? remove ???
let msid = null;
// FIXME what is this ? global APP.RTC in SDP ?
const localTrack = APP.RTC.getLocalTracks(mline.media);
if(localTrack) {
// FIXME before this changes the track id was accessed,
// but msid stands for the stream id, makes no sense ?
msid = localTrack.getTrackId();
}
if(msid != null) {
msid = SDPUtil.filter_special_chars(msid);
Expand Down

0 comments on commit bed6c84

Please sign in to comment.