2014-12-19 13:59:08 +00:00
|
|
|
var EventEmitter = require("events");
|
|
|
|
var RTCUtils = require("./RTCUtils.js");
|
2014-12-22 09:03:21 +00:00
|
|
|
var LocalStream = require("./LocalStream.js");
|
|
|
|
var DataChannels = require("./DataChannels");
|
|
|
|
var MediaStream = require("./MediaStream.js");
|
2015-01-28 14:35:22 +00:00
|
|
|
var DesktopSharingEventTypes
|
|
|
|
= require("../../service/desktopsharing/DesktopSharingEventTypes");
|
|
|
|
var MediaStreamType = require("../../service/RTC/MediaStreamTypes");
|
|
|
|
var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
|
|
|
|
var XMPPEvents = require("../../service/xmpp/XMPPEvents");
|
|
|
|
var UIEvents = require("../../service/UI/UIEvents");
|
2014-12-19 13:59:08 +00:00
|
|
|
|
|
|
|
var eventEmitter = new EventEmitter();
|
|
|
|
|
|
|
|
var RTC = {
|
|
|
|
rtcUtils: null,
|
|
|
|
localStreams: [],
|
|
|
|
remoteStreams: {},
|
|
|
|
localAudio: null,
|
|
|
|
localVideo: null,
|
|
|
|
addStreamListener: function (listener, eventType) {
|
|
|
|
eventEmitter.on(eventType, listener);
|
|
|
|
},
|
2015-01-27 09:56:22 +00:00
|
|
|
addListener: function (type, listener) {
|
|
|
|
eventEmitter.on(type, listener);
|
|
|
|
},
|
2014-12-19 13:59:08 +00:00
|
|
|
removeStreamListener: function (listener, eventType) {
|
|
|
|
if(!(eventType instanceof StreamEventTypes))
|
|
|
|
throw "Illegal argument";
|
|
|
|
|
|
|
|
eventEmitter.removeListener(eventType, listener);
|
|
|
|
},
|
2015-01-13 13:11:05 +00:00
|
|
|
createLocalStream: function (stream, type, change) {
|
2014-12-22 09:03:21 +00:00
|
|
|
|
2014-12-19 13:59:08 +00:00
|
|
|
var localStream = new LocalStream(stream, type, eventEmitter);
|
2015-01-12 13:23:29 +00:00
|
|
|
//in firefox we have only one stream object
|
|
|
|
if(this.localStreams.length == 0 ||
|
|
|
|
this.localStreams[0].getOriginalStream() != stream)
|
|
|
|
this.localStreams.push(localStream);
|
2014-12-19 13:59:08 +00:00
|
|
|
if(type == "audio")
|
|
|
|
{
|
|
|
|
this.localAudio = localStream;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.localVideo = localStream;
|
|
|
|
}
|
2015-01-13 13:11:05 +00:00
|
|
|
var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
|
|
|
|
if(change)
|
|
|
|
eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED;
|
|
|
|
|
|
|
|
eventEmitter.emit(eventType, localStream);
|
2014-12-19 13:59:08 +00:00
|
|
|
return localStream;
|
|
|
|
},
|
|
|
|
removeLocalStream: function (stream) {
|
|
|
|
for(var i = 0; i < this.localStreams.length; i++)
|
|
|
|
{
|
|
|
|
if(this.localStreams[i].getOriginalStream() === stream) {
|
|
|
|
delete this.localStreams[i];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
createRemoteStream: function (data, sid, thessrc) {
|
2015-01-19 16:54:41 +00:00
|
|
|
var remoteStream = new MediaStream(data, sid, thessrc,
|
2014-12-22 09:03:21 +00:00
|
|
|
this.getBrowserType());
|
2015-01-28 14:35:22 +00:00
|
|
|
var jid = data.peerjid || APP.xmpp.myJid();
|
2014-12-19 13:59:08 +00:00
|
|
|
if(!this.remoteStreams[jid]) {
|
|
|
|
this.remoteStreams[jid] = {};
|
|
|
|
}
|
|
|
|
this.remoteStreams[jid][remoteStream.type]= remoteStream;
|
2015-01-19 16:54:41 +00:00
|
|
|
eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, remoteStream);
|
|
|
|
console.debug("ADD remote stream ", remoteStream.type, " ", jid, " ", thessrc);
|
2014-12-19 13:59:08 +00:00
|
|
|
return remoteStream;
|
|
|
|
},
|
|
|
|
getBrowserType: function () {
|
|
|
|
return this.rtcUtils.browser;
|
|
|
|
},
|
|
|
|
getPCConstraints: function () {
|
|
|
|
return this.rtcUtils.pc_constraints;
|
|
|
|
},
|
|
|
|
getUserMediaWithConstraints:function(um, success_callback,
|
|
|
|
failure_callback, resolution,
|
|
|
|
bandwidth, fps, desktopStream)
|
|
|
|
{
|
|
|
|
return this.rtcUtils.getUserMediaWithConstraints(um, success_callback,
|
|
|
|
failure_callback, resolution, bandwidth, fps, desktopStream);
|
|
|
|
},
|
|
|
|
attachMediaStream: function (element, stream) {
|
|
|
|
this.rtcUtils.attachMediaStream(element, stream);
|
|
|
|
},
|
|
|
|
getStreamID: function (stream) {
|
|
|
|
return this.rtcUtils.getStreamID(stream);
|
|
|
|
},
|
|
|
|
getVideoSrc: function (element) {
|
|
|
|
return this.rtcUtils.getVideoSrc(element);
|
|
|
|
},
|
|
|
|
setVideoSrc: function (element, src) {
|
|
|
|
this.rtcUtils.setVideoSrc(element, src);
|
|
|
|
},
|
|
|
|
dispose: function() {
|
|
|
|
if (this.rtcUtils) {
|
|
|
|
this.rtcUtils = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
stop: function () {
|
|
|
|
this.dispose();
|
|
|
|
},
|
|
|
|
start: function () {
|
2015-01-13 13:11:05 +00:00
|
|
|
var self = this;
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.desktopsharing.addListener(
|
2015-01-13 13:11:05 +00:00
|
|
|
function (stream, isUsingScreenStream, callback) {
|
|
|
|
self.changeLocalVideo(stream, isUsingScreenStream, callback);
|
|
|
|
}, DesktopSharingEventTypes.NEW_STREAM_CREATED);
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.xmpp.addListener(XMPPEvents.CHANGED_STREAMS, function (jid, changedStreams) {
|
2015-01-20 15:56:00 +00:00
|
|
|
for(var i = 0; i < changedStreams.length; i++) {
|
|
|
|
var type = changedStreams[i].type;
|
|
|
|
if (type != "audio") {
|
|
|
|
var peerStreams = self.remoteStreams[jid];
|
|
|
|
if(!peerStreams)
|
|
|
|
continue;
|
|
|
|
var videoStream = peerStreams[MediaStreamType.VIDEO_TYPE];
|
|
|
|
if(!videoStream)
|
|
|
|
continue;
|
|
|
|
videoStream.videoType = changedStreams[i].type;
|
|
|
|
}
|
|
|
|
}
|
2015-01-24 14:28:02 +00:00
|
|
|
});
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.xmpp.addListener(XMPPEvents.CALL_INCOMING, function(event) {
|
2015-01-27 09:56:22 +00:00
|
|
|
DataChannels.init(event.peerconnection, eventEmitter);
|
2015-01-24 14:28:02 +00:00
|
|
|
});
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.UI.addListener(UIEvents.SELECTED_ENDPOINT,
|
2015-01-27 12:03:26 +00:00
|
|
|
DataChannels.handleSelectedEndpointEvent);
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.UI.addListener(UIEvents.PINNED_ENDPOINT,
|
2015-01-27 12:03:26 +00:00
|
|
|
DataChannels.handlePinnedEndpointEvent);
|
2014-12-19 13:59:08 +00:00
|
|
|
this.rtcUtils = new RTCUtils(this);
|
|
|
|
this.rtcUtils.obtainAudioAndVideoPermissions();
|
|
|
|
},
|
|
|
|
muteRemoteVideoStream: function (jid, value) {
|
|
|
|
var stream;
|
|
|
|
|
|
|
|
if(this.remoteStreams[jid] &&
|
|
|
|
this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE])
|
|
|
|
{
|
|
|
|
stream = this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!stream)
|
|
|
|
return false;
|
|
|
|
|
2015-01-13 09:33:45 +00:00
|
|
|
if (value != stream.muted) {
|
|
|
|
stream.setMute(value);
|
2014-12-19 13:59:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-01-12 13:23:29 +00:00
|
|
|
},
|
|
|
|
switchVideoStreams: function (new_stream) {
|
|
|
|
this.localVideo.stream = new_stream;
|
|
|
|
|
|
|
|
this.localStreams = [];
|
|
|
|
|
|
|
|
//in firefox we have only one stream object
|
2015-01-13 13:11:05 +00:00
|
|
|
if (this.localAudio.getOriginalStream() != new_stream)
|
2015-01-12 13:23:29 +00:00
|
|
|
this.localStreams.push(this.localAudio);
|
|
|
|
this.localStreams.push(this.localVideo);
|
2015-01-13 13:11:05 +00:00
|
|
|
},
|
|
|
|
changeLocalVideo: function (stream, isUsingScreenStream, callback) {
|
|
|
|
var oldStream = this.localVideo.getOriginalStream();
|
2015-01-20 15:56:00 +00:00
|
|
|
var type = (isUsingScreenStream? "screen" : "video");
|
2015-01-28 14:35:22 +00:00
|
|
|
this.localVideo = this.createLocalStream(stream, "video", true, type);
|
2015-01-13 13:11:05 +00:00
|
|
|
// Stop the stream to trigger onended event for old stream
|
|
|
|
oldStream.stop();
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.xmpp.switchStreams(stream, oldStream,callback);
|
2015-01-20 15:56:00 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Checks if video identified by given src is desktop stream.
|
|
|
|
* @param videoSrc eg.
|
|
|
|
* blob:https%3A//pawel.jitsi.net/9a46e0bd-131e-4d18-9c14-a9264e8db395
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isVideoSrcDesktop: function (jid) {
|
|
|
|
if(!jid)
|
|
|
|
return false;
|
|
|
|
var isDesktop = false;
|
|
|
|
var stream = null;
|
2015-01-28 14:35:22 +00:00
|
|
|
if (APP.xmpp.myJid() &&
|
|
|
|
APP.xmpp.myResource() === jid) {
|
2015-01-20 15:56:00 +00:00
|
|
|
// local video
|
|
|
|
stream = this.localVideo;
|
|
|
|
} else {
|
|
|
|
var peerStreams = this.remoteStreams[jid];
|
|
|
|
if(!peerStreams)
|
|
|
|
return false;
|
|
|
|
stream = peerStreams[MediaStreamType.VIDEO_TYPE];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stream)
|
|
|
|
isDesktop = (stream.videoType === "screen");
|
2014-12-19 13:59:08 +00:00
|
|
|
|
2015-01-20 15:56:00 +00:00
|
|
|
return isDesktop;
|
|
|
|
}
|
2014-12-19 13:59:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RTC;
|