jiti-meet/modules/RTC/RTC.js

336 lines
11 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 = {
// Exposes DataChannels to public consumption (e.g. jitsi-meet-torture)
// without the necessity to require the module.
"DataChannels": DataChannels,
2014-12-19 13:59:08 +00:00
rtcUtils: null,
devices: {
audio: true,
video: true
},
2014-12-19 13:59:08 +00:00
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);
if(isMuted === true)
2015-07-30 15:51:50 +00:00
localStream.setMute(true);
if (MediaStreamType.AUDIO_TYPE === type) {
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;
},
createRemoteStream: function (data, ssrc) {
var jid = data.peerjid || APP.xmpp.myJid();
// check the video muted state from last stored presence if any
var muted = false;
var pres = APP.xmpp.getLastPresence(jid);
2015-09-11 03:26:29 +00:00
if (pres && pres.videoMuted) {
muted = pres.videoMuted;
}
var self = this;
[MediaStreamType.AUDIO_TYPE, MediaStreamType.VIDEO_TYPE].forEach(
function (type) {
var tracks =
type == MediaStreamType.AUDIO_TYPE
? data.stream.getAudioTracks() : data.stream.getVideoTracks();
if (!tracks || !Array.isArray(tracks) || !tracks.length) {
console.log("Not creating a(n) " + type + " stream: no tracks");
return;
}
var remoteStream = new MediaStream(data, ssrc,
RTCBrowserType.getBrowserType(), eventEmitter, muted, type);
if (!self.remoteStreams[jid]) {
self.remoteStreams[jid] = {};
}
self.remoteStreams[jid][type] = remoteStream;
eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED,
remoteStream);
});
2014-12-19 13:59:08 +00:00
},
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(
DesktopSharingEventTypes.NEW_STREAM_CREATED,
2015-01-13 13:11:05 +00:00
function (stream, isUsingScreenStream, callback) {
self.changeLocalVideo(stream, isUsingScreenStream, callback);
});
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());
};
2015-11-05 16:53:15 +00:00
this.rtcUtils = new RTCUtils(this, eventEmitter, 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;
},
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);
});
2015-11-05 16:53:15 +00:00
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);
// Stop the stream
this.stopMediaStream(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, MediaStreamType.AUDIO_TYPE, true);
// Stop the stream
this.stopMediaStream(oldStream);
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);
},
/**
* A method to handle stopping of the stream.
* One point to handle the differences in various implementations.
* @param mediaStream MediaStream object to stop.
*/
stopMediaStream: function (mediaStream) {
mediaStream.getTracks().forEach(function (track) {
// stop() not supported with IE
if (track.stop) {
track.stop();
}
});
// leave stop for implementation still using it
if (mediaStream.stop) {
mediaStream.stop();
}
},
/**
* Adds onended/inactive handler to a MediaStream.
* @param mediaStream a MediaStream to attach onended/inactive handler
* @param handler the handler
*/
addMediaStreamInactiveHandler: function (mediaStream, handler) {
if (mediaStream.addEventListener) {
// chrome
if(typeof mediaStream.active !== "undefined")
mediaStream.inactive = handler;
else
mediaStream.onended = handler;
} else {
// themasys
mediaStream.attachEvent('ended', function () {
handler(mediaStream);
});
}
},
/**
* Removes onended/inactive handler.
* @param mediaStream the MediaStream to remove the handler from.
* @param handler the handler to remove.
*/
removeMediaStreamInactiveHandler: function (mediaStream, handler) {
if (mediaStream.removeEventListener) {
// chrome
if(typeof mediaStream.active !== "undefined")
mediaStream.inactive = null;
else
mediaStream.onended = null;
} else {
// themasys
mediaStream.detachEvent('ended', handler);
}
}
2014-12-19 13:59:08 +00:00
};
module.exports = RTC;