Skip to content

Commit

Permalink
Merge pull request #373 from jitsi/move_TPC
Browse files Browse the repository at this point in the history
Move TraceablePeerConnection to RTC
  • Loading branch information
bbaldino committed Feb 28, 2017
2 parents 886bc15 + c843889 commit 12a91dc
Show file tree
Hide file tree
Showing 15 changed files with 841 additions and 504 deletions.
2 changes: 1 addition & 1 deletion .jshintignore
Expand Up @@ -8,7 +8,6 @@ doc

modules/xmpp/xmpp.js
modules/xmpp/strophe.jingle.js
modules/xmpp/TraceablePeerConnection.js
modules/xmpp/SDPUtil.js
modules/xmpp/SDP.js
modules/xmpp/strophe.emuc.js
Expand All @@ -22,6 +21,7 @@ modules/RTC/adapter.screenshare.js
modules/statistics/statistics.js
modules/RTC/RTC.js
modules/RTC/RTCUtils.js
modules/RTC/TraceablePeerConnection.js
modules/RTC/DataChannels.js
modules/RTC/JitsiLocalTrack.js
modules/RTC/JitsiRemoteTrack.js
Expand Down
171 changes: 114 additions & 57 deletions JitsiConference.js
@@ -1,28 +1,30 @@
/* global Strophe, Promise */
/* global __filename, Strophe, Promise */

var logger = require("jitsi-meet-logger").getLogger(__filename);
import RTC from "./modules/RTC/RTC";
import * as MediaType from "./service/RTC/MediaType";
var XMPPEvents = require("./service/xmpp/XMPPEvents");
var EventEmitter = require("events");
import ComponentsVersions from "./modules/version/ComponentsVersions";
import ConnectionQuality from "./modules/connectivity/ConnectionQuality";
import { getLogger } from "jitsi-meet-logger";
import GlobalOnErrorHandler from "./modules/util/GlobalOnErrorHandler";
import EventEmitter from "events";
import * as JitsiConferenceErrors from "./JitsiConferenceErrors";
import JitsiConferenceEventManager from "./JitsiConferenceEventManager";
import * as JitsiConferenceEvents from "./JitsiConferenceEvents";
import JitsiDTMFManager from './modules/DTMF/JitsiDTMFManager';
import JitsiParticipant from "./JitsiParticipant";
var Statistics = require("./modules/statistics/statistics");
var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
import JitsiTrackError from "./JitsiTrackError";
import * as JitsiTrackErrors from "./JitsiTrackErrors";
import * as JitsiTrackEvents from "./JitsiTrackEvents";
var ComponentsVersions = require("./modules/version/ComponentsVersions");
var GlobalOnErrorHandler = require("./modules/util/GlobalOnErrorHandler");
var JitsiConferenceEventManager = require("./JitsiConferenceEventManager");
var VideoType = require('./service/RTC/VideoType');
var RTCBrowserType = require("./modules/RTC/RTCBrowserType.js");
var Transcriber = require("./modules/transcription/transcriber");
import * as MediaType from "./service/RTC/MediaType";
import ParticipantConnectionStatus
from "./modules/connectivity/ParticipantConnectionStatus";
import RTC from "./modules/RTC/RTC";
import RTCBrowserType from "./modules/RTC/RTCBrowserType.js";
import * as RTCEvents from "./service/RTC/RTCEvents";
import Statistics from "./modules/statistics/statistics";
import TalkMutedDetection from "./modules/TalkMutedDetection";
import ConnectionQuality from "./modules/connectivity/ConnectionQuality";
import Transcriber from "./modules/transcription/transcriber";
import VideoType from './service/RTC/VideoType';

const logger = getLogger(__filename);

/**
* Creates a JitsiConference object with the given name and properties.
Expand Down Expand Up @@ -170,7 +172,7 @@ JitsiConference.prototype.leave = function () {
this.participantConnectionStatus = null;
}

this.getLocalTracks().forEach(track => this.onTrackRemoved(track));
this.getLocalTracks().forEach(track => this.onLocalTrackRemoved(track));

this.rtc.closeAllDataChannels();
if (this.statistics)
Expand Down Expand Up @@ -262,17 +264,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|null}
*/
JitsiConference.prototype.getLocalAudioTrack = function () {
return this.rtc ? this.rtc.getLocalAudioTrack() : null;
};

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

/**
* Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
* in JitsiConferenceEvents.
Expand Down Expand Up @@ -394,17 +406,15 @@ JitsiConference.prototype.getTranscriber = function(){
if (this.transcriber === undefined){
this.transcriber = new Transcriber();
//add all existing local audio tracks to the transcriber
this.rtc.localTracks.forEach(function (localTrack) {
if (localTrack.isAudioTrack()){
this.transcriber.addTrack(localTrack);
}
}.bind(this));
const localAudioTracks = this.getLocalTracks(MediaType.AUDIO);
for (const localAudio of localAudioTracks) {
this.transcriber.addTrack(localAudio);
}
//and all remote audio tracks
this.rtc.remoteTracks.forEach(function (remoteTrack){
if (remoteTrack.isAudioTrack()){
this.transcriber.addTrack(remoteTrack);
}
}.bind(this));
const remoteAudioTracks = this.rtc.getRemoteTracks(MediaType.AUDIO);
for (const remoteTrack of remoteAudioTracks){
this.transcriber.addTrack(remoteTrack);
}
}
return this.transcriber;
};
Expand Down Expand Up @@ -463,15 +473,15 @@ 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);
track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
track.muteHandler);
track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
track.audioLevelHandler);
this.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
this.rtc.removeListener(RTCEvents.SENDRECV_STREAMS_CHANGED,
track.ssrcHandler);

// send event for stopping screen sharing
Expand Down Expand Up @@ -519,18 +529,19 @@ JitsiConference.prototype.replaceTrack = function (oldTrack, newTrack) {
newTrack.ssrcHandler = function (conference, ssrcMap) {
if (ssrcMap[this.getMSID()]) {
this._setSSRC(ssrcMap[this.getMSID()]);
conference.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
conference.rtc.removeListener(
RTCEvents.SENDRECV_STREAMS_CHANGED,
this.ssrcHandler);
}
}.bind(newTrack, this);
this.room.addListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
this.rtc.addListener(RTCEvents.SENDRECV_STREAMS_CHANGED,
newTrack.ssrcHandler);
}
// Now replace the stream at the lower levels
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 @@ -895,16 +906,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 @@ -915,7 +927,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 @@ -935,6 +947,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 Expand Up @@ -978,14 +1031,14 @@ function (jingleSession, jingleOffer, now) {
label: crossRegion
});
try {
jingleSession.initialize(false /* initiator */,this.room);
jingleSession.initialize(false /* initiator */, this.room, this.rtc);
} catch (error) {
GlobalOnErrorHandler.callErrorHandler(error);
}

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 @@ -1081,13 +1134,13 @@ 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
// it reads the values
self.room.addListener(
XMPPEvents.SENDRECV_STREAMS_CHANGED, localTrack.ssrcHandler);
self.rtc.addListener(
RTCEvents.SENDRECV_STREAMS_CHANGED, localTrack.ssrcHandler);
});
};

Expand Down Expand Up @@ -1134,21 +1187,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

0 comments on commit 12a91dc

Please sign in to comment.