123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- /* global APP */
- var EventEmitter = require("events");
- var RTCBrowserType = require("./RTCBrowserType");
- 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");
- var desktopsharing = require("../desktopsharing/desktopsharing");
-
- function getMediaStreamUsage()
- {
- var result = {
- audio: true,
- video: true
- };
-
- /** There are some issues with the desktop sharing
- * when this property is enabled.
- * 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)
- {
- result = {
- audio: false,
- video: false
- };
-
- }
- **/
-
- return result;
- }
-
-
- function RTC(options)
- {
- this.rtcUtils = null;
- this.devices = {
- audio: true,
- video: true
- };
- this.localStreams = [];
- this.remoteStreams = {};
- this.localAudio = null;
- this.localVideo = null;
- this.eventEmitter = new EventEmitter();
- var self = this;
- this.options = options || {};
- desktopsharing.addListener(
- function (stream, isUsingScreenStream, callback) {
- self.changeLocalVideo(stream, isUsingScreenStream, callback);
- }, DesktopSharingEventTypes.NEW_STREAM_CREATED);
-
- // 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 () {
- self.eventEmitter.emit(RTCEvents.RTC_READY, true);
- };
-
- this.rtcUtils = new RTCUtils(this, onReady);
-
- // Call onReady() if Temasys plugin is not used
- if (!RTCBrowserType.isTemasysPluginUsed()) {
- onReady();
- }
- }
-
- RTC.prototype.obtainAudioAndVideoPermissions = function (options) {
- return this.rtcUtils.obtainAudioAndVideoPermissions(
- null, getMediaStreamUsage(), options.resolution);
- }
-
- RTC.prototype.onIncommingCall = function(event) {
- DataChannels.init(event.peerconnection, self.eventEmitter);
- }
-
- RTC.prototype.selectedEndpoint = function (id) {
- DataChannels.handleSelectedEndpointEvent(id);
- }
-
- RTC.prototype.pinEndpoint = function (id) {
- DataChannels.handlePinnedEndpointEvent(id);
- }
-
- RTC.prototype.addStreamListener = function (listener, eventType) {
- this.eventEmitter.on(eventType, listener);
- };
-
- RTC.prototype.addListener = function (type, listener) {
- this.eventEmitter.on(type, listener);
- };
-
- RTC.prototype.removeStreamListener = function (listener, eventType) {
- if(!(eventType instanceof StreamEventTypes))
- throw "Illegal argument";
-
- this.removeListener(eventType, listener);
- };
-
- RTC.prototype.createLocalStreams = function (streams, change) {
- for (var i = 0; i < streams.length; i++) {
- var localStream = new LocalStream(this, streams[i].stream,
- streams[i].type, this.eventEmitter, streams[i].videoType,
- streams[i].isGUMStream);
- this.localStreams.push(localStream);
- if (streams[i].isMuted === true)
- localStream.setMute(true);
-
- if (streams[i].type == "audio") {
- this.localAudio = localStream;
- } else {
- this.localVideo = localStream;
- }
- var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
- if (change)
- eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED;
-
- this.eventEmitter.emit(eventType, localStream, streams[i].isMuted);
- }
- return this.localStreams;
- };
-
- RTC.prototype.removeLocalStream = function (stream) {
- for(var i = 0; i < this.localStreams.length; i++) {
- if(this.localStreams[i].getOriginalStream() === stream) {
- delete this.localStreams[i];
- return;
- }
- }
- };
-
- RTC.prototype.createRemoteStream = function (data, sid, thessrc) {
- var remoteStream = new MediaStream(data, sid, thessrc,
- RTCBrowserType.getBrowserType(), this.eventEmitter);
- if(data.peerjid)
- return;
- var jid = data.peerjid;
- if(!this.remoteStreams[jid]) {
- this.remoteStreams[jid] = {};
- }
- this.remoteStreams[jid][remoteStream.type]= remoteStream;
- this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, remoteStream);
- return remoteStream;
- };
-
- RTC.prototype.getPCConstraints = function () {
- return this.rtcUtils.pc_constraints;
- };
-
- RTC.prototype.getUserMediaWithConstraints = function(um, success_callback,
- failure_callback, resolution,
- bandwidth, fps, desktopStream)
- {
- return this.rtcUtils.getUserMediaWithConstraints(um, success_callback,
- failure_callback, resolution, bandwidth, fps, desktopStream);
- };
-
- RTC.prototype.attachMediaStream = function (elSelector, stream) {
- this.rtcUtils.attachMediaStream(elSelector, stream);
- };
-
- RTC.prototype.getStreamID = function (stream) {
- return this.rtcUtils.getStreamID(stream);
- };
-
- RTC.prototype.getVideoSrc = function (element) {
- return this.rtcUtils.getVideoSrc(element);
- };
-
- RTC.prototype.setVideoSrc = function (element, src) {
- this.rtcUtils.setVideoSrc(element, src);
- };
-
- RTC.prototype.getVideoElementName = function () {
- return RTCBrowserType.isTemasysPluginUsed() ? 'object' : 'video';
- };
-
- RTC.prototype.dispose = function() {
- if (this.rtcUtils) {
- this.rtcUtils = null;
- }
- };
-
- RTC.prototype.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 true;
-
- if (value != stream.muted) {
- stream.setMute(value);
- return true;
- }
- return false;
- };
-
- RTC.prototype.switchVideoStreams = function (new_stream) {
- this.localVideo.stream = new_stream;
-
- this.localStreams = [];
-
- //in firefox we have only one stream object
- if (this.localAudio.getOriginalStream() != new_stream)
- this.localStreams.push(this.localAudio);
- this.localStreams.push(this.localVideo);
- };
-
- RTC.prototype.changeLocalVideo = function (stream, isUsingScreenStream, callback) {
- var oldStream = this.localVideo.getOriginalStream();
- var type = (isUsingScreenStream ? "screen" : "camera");
- var localCallback = callback;
-
- if(this.localVideo.isMuted() && this.localVideo.videoType !== type) {
- localCallback = function() {
- APP.xmpp.setVideoMute(false, function(mute) {
- self.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);
- // Stop the stream to trigger onended event for old stream
- oldStream.stop();
-
- this.switchVideoStreams(videoStream, oldStream);
-
- APP.xmpp.switchStreams(videoStream, oldStream,localCallback);
- };
-
- RTC.prototype.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);
- };
-
- RTC.prototype.isVideoMuted = function (jid) {
- if (jid === APP.xmpp.myJid()) {
- var localVideo = APP.RTC.localVideo;
- return (!localVideo || localVideo.isMuted());
- } else {
- if (!this.remoteStreams[jid] ||
- !this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
- return null;
- }
- return this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE].muted;
- }
- };
-
- RTC.prototype.setVideoMute = function (mute, callback, options) {
- if (!this.localVideo)
- return;
-
- if (mute == this.localVideo.isMuted())
- {
- APP.xmpp.sendVideoInfoPresence(mute);
- if (callback)
- callback(mute);
- }
- else
- {
- this.localVideo.setMute(mute);
- APP.xmpp.setVideoMute(
- mute,
- callback,
- options);
- }
- };
-
- RTC.prototype.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;
- this.eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, this.devices);
- };
-
- module.exports = RTC;
|