jiti-meet/modules/RTC/RTC.js

276 lines
9.4 KiB
JavaScript
Raw Normal View History

2015-07-28 19:42:01 +00:00
/* global APP */
2014-12-19 13:59:08 +00:00
var EventEmitter = require("events");
var RTCBrowserType = require("./RTCBrowserType");
2014-12-19 13:59:08 +00:00
var RTCUtils = require("./RTCUtils.js");
var LocalStream = require("./LocalStream.js");
var DataChannels = require("./DataChannels");
var MediaStream = require("./MediaStream.js");
var DesktopSharingEventTypes
= require("../../service/desktopsharing/DesktopSharingEventTypes");
var MediaStreamType = require("../../service/RTC/MediaStreamTypes");
var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
var RTCEvents = require("../../service/RTC/RTCEvents.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();
function getMediaStreamUsage()
{
var result = {
2015-05-19 15:03:01 +00:00
audio: true,
video: true
};
/** There are some issues with the desktop sharing
* when this property is enabled.
2015-05-19 15:03:01 +00:00
* WARNING: We must change the implementation to start video/audio if we
* receive from the focus that the peer is not muted.
var isSecureConnection = window.location.protocol == "https:";
if(config.disableEarlyMediaPermissionRequests || !isSecureConnection)
{
2015-05-19 15:03:01 +00:00
result = {
audio: false,
video: false
};
}
**/
return result;
}
2014-12-19 13:59:08 +00:00
var RTC = {
rtcUtils: null,
devices: {
audio: true,
video: true
},
2014-12-19 13:59:08 +00:00
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);
},
createLocalStream: function (stream, type, change, videoType, isMuted, isGUMStream) {
var localStream = new LocalStream(stream, type, eventEmitter, videoType, isGUMStream);
//in firefox we have only one stream object
if(this.localStreams.length == 0 ||
this.localStreams[0].getOriginalStream() != stream)
this.localStreams.push(localStream);
if(isMuted === true)
2015-07-30 15:51:50 +00:00
localStream.setMute(true);
2015-07-30 15:51:50 +00:00
if(type == "audio") {
2014-12-19 13:59:08 +00:00
this.localAudio = localStream;
2015-07-30 15:51:50 +00:00
} else {
2014-12-19 13:59:08 +00:00
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, isMuted);
2014-12-19 13:59:08 +00:00
return localStream;
},
removeLocalStream: function (stream) {
2015-07-28 19:42:01 +00:00
for(var i = 0; i < this.localStreams.length; i++) {
2014-12-19 13:59:08 +00:00
if(this.localStreams[i].getOriginalStream() === stream) {
delete this.localStreams[i];
return;
}
}
},
createRemoteStream: function (data, sid, thessrc) {
var remoteStream = new MediaStream(data, sid, thessrc,
RTCBrowserType.getBrowserType(), eventEmitter);
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;
eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, remoteStream);
2014-12-19 13:59:08 +00:00
return remoteStream;
},
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 (elSelector, stream) {
this.rtcUtils.attachMediaStream(elSelector, stream);
2014-12-19 13:59:08 +00:00
},
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);
},
getVideoElementName: function () {
return RTCBrowserType.isTemasysPluginUsed() ? 'object' : 'video';
},
2014-12-19 13:59:08 +00:00
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;
APP.desktopsharing.addListener(
2015-01-13 13:11:05 +00:00
function (stream, isUsingScreenStream, callback) {
self.changeLocalVideo(stream, isUsingScreenStream, callback);
}, DesktopSharingEventTypes.NEW_STREAM_CREATED);
APP.xmpp.addListener(XMPPEvents.CALL_INCOMING, function(event) {
2015-01-27 09:56:22 +00:00
DataChannels.init(event.peerconnection, eventEmitter);
});
2015-07-20 23:01:50 +00:00
APP.UI.addListener(UIEvents.SELECTED_ENDPOINT,
DataChannels.handleSelectedEndpointEvent);
APP.UI.addListener(UIEvents.PINNED_ENDPOINT,
DataChannels.handlePinnedEndpointEvent);
// In case of IE we continue from 'onReady' callback
// passed to RTCUtils constructor. It will be invoked by Temasys plugin
// once it is initialized.
var onReady = function () {
eventEmitter.emit(RTCEvents.RTC_READY, true);
self.rtcUtils.obtainAudioAndVideoPermissions(
null, null, getMediaStreamUsage());
};
this.rtcUtils = new RTCUtils(this, onReady);
// Call onReady() if Temasys plugin is not used
if (!RTCBrowserType.isTemasysPluginUsed()) {
onReady();
}
2014-12-19 13:59:08 +00:00
},
muteRemoteVideoStream: function (jid, value) {
var stream;
if(this.remoteStreams[jid] &&
2015-07-28 19:42:01 +00:00
this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
2014-12-19 13:59:08 +00:00
stream = this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
}
if(!stream)
2015-03-30 14:19:35 +00:00
return true;
2014-12-19 13:59:08 +00:00
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;
},
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)
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();
var type = (isUsingScreenStream ? "screen" : "camera");
var localCallback = callback;
2015-07-28 19:42:01 +00:00
if(this.localVideo.isMuted() && this.localVideo.videoType !== type) {
localCallback = function() {
2015-07-18 04:39:09 +00:00
APP.xmpp.setVideoMute(false, function(mute) {
eventEmitter.emit(RTCEvents.VIDEO_MUTE, mute);
});
callback();
};
}
// FIXME: Workaround for FF/IE/Safari
if (stream && stream.videoStream) {
stream = stream.videoStream;
}
var videoStream = this.rtcUtils.createStream(stream, true);
this.localVideo = this.createLocalStream(videoStream, "video", true, type);
2015-01-13 13:11:05 +00:00
// Stop the stream to trigger onended event for old stream
oldStream.stop();
this.switchVideoStreams(videoStream, oldStream);
APP.xmpp.switchStreams(videoStream, oldStream,localCallback);
},
changeLocalAudio: function (stream, callback) {
var oldStream = this.localAudio.getOriginalStream();
var newStream = this.rtcUtils.createStream(stream);
this.localAudio = this.createLocalStream(newStream, "audio", true);
// Stop the stream to trigger onended event for old stream
oldStream.stop();
APP.xmpp.switchStreams(newStream, oldStream, callback, true);
},
isVideoMuted: function (jid) {
if (jid === APP.xmpp.myJid()) {
var localVideo = APP.RTC.localVideo;
return (!localVideo || localVideo.isMuted());
2015-07-28 19:42:01 +00:00
} else {
if (!APP.RTC.remoteStreams[jid] ||
!APP.RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
return null;
}
return APP.RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE].muted;
}
},
setVideoMute: function (mute, callback, options) {
if (!this.localVideo)
return;
if (mute == APP.RTC.localVideo.isMuted())
{
APP.xmpp.sendVideoInfoPresence(mute);
if (callback)
callback(mute);
}
else
{
APP.RTC.localVideo.setMute(mute);
APP.xmpp.setVideoMute(
mute,
callback,
options);
}
},
setDeviceAvailability: function (devices) {
if(!devices)
return;
if(devices.audio === true || devices.audio === false)
this.devices.audio = devices.audio;
if(devices.video === true || devices.video === false)
this.devices.video = devices.video;
eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, this.devices);
}
2014-12-19 13:59:08 +00:00
};
module.exports = RTC;