You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RTC.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* global APP */
  2. var EventEmitter = require("events");
  3. var RTCBrowserType = require("./RTCBrowserType");
  4. var RTCUtils = require("./RTCUtils.js");
  5. var JitsiTrack = require("./JitsiTrack");
  6. var JitsiLocalTrack = require("./JitsiLocalTrack.js");
  7. var DataChannels = require("./DataChannels");
  8. var JitsiRemoteTrack = require("./JitsiRemoteTrack.js");
  9. var DesktopSharingEventTypes
  10. = require("../../service/desktopsharing/DesktopSharingEventTypes");
  11. var MediaStreamType = require("../../service/RTC/MediaStreamTypes");
  12. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  13. var RTCEvents = require("../../service/RTC/RTCEvents.js");
  14. var desktopsharing = require("../desktopsharing/desktopsharing");
  15. function getMediaStreamUsage()
  16. {
  17. var result = {
  18. audio: true,
  19. video: true
  20. };
  21. /** There are some issues with the desktop sharing
  22. * when this property is enabled.
  23. * WARNING: We must change the implementation to start video/audio if we
  24. * receive from the focus that the peer is not muted.
  25. var isSecureConnection = window.location.protocol == "https:";
  26. if(config.disableEarlyMediaPermissionRequests || !isSecureConnection)
  27. {
  28. result = {
  29. audio: false,
  30. video: false
  31. };
  32. }
  33. **/
  34. return result;
  35. }
  36. var rtcReady = false;
  37. function RTC(room, options) {
  38. this.devices = {
  39. audio: true,
  40. video: true
  41. };
  42. this.room = room;
  43. this.localStreams = [];
  44. this.remoteStreams = {};
  45. this.localAudio = null;
  46. this.localVideo = null;
  47. this.eventEmitter = new EventEmitter();
  48. var self = this;
  49. this.options = options || {};
  50. desktopsharing.addListener(
  51. function (stream, isUsingScreenStream, callback) {
  52. self.changeLocalVideo(stream, isUsingScreenStream, callback);
  53. }, DesktopSharingEventTypes.NEW_STREAM_CREATED);
  54. room.addPresenceListener("videomuted", function (values, from) {
  55. if(self.remoteStreams[from])
  56. self.remoteStreams[from][JitsiTrack.VIDEO].setMute(values.value == "true");
  57. });
  58. room.addPresenceListener("audiomuted", function (values, from) {
  59. if(self.remoteStreams[from])
  60. self.remoteStreams[from][JitsiTrack.AUDIO].setMute(values.value == "true");
  61. });
  62. }
  63. /**
  64. * Creates the local MediaStreams.
  65. * @param options object for options (NOTE: currently only list of devices and resolution are supported)
  66. * @param dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  67. * type: "audio" or "video", isMuted: true/false, videoType: "camera" or "desktop"}
  68. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  69. * @returns {*} Promise object that will receive the new JitsiTracks
  70. */
  71. RTC.prototype.obtainAudioAndVideoPermissions = function (options, dontCreateJitsiTrack) {
  72. return RTCUtils.obtainAudioAndVideoPermissions(this,
  73. options.devices, getMediaStreamUsage(), options.resolution, dontCreateJitsiTrack);
  74. }
  75. RTC.prototype.onIncommingCall = function(event) {
  76. if(this.options.config.openSctp)
  77. this.dataChannels = new DataChannels(event.peerconnection, this.eventEmitter);
  78. for(var i = 0; i < this.localStreams.length; i++)
  79. if(this.localStreams[i].isStarted)
  80. {
  81. this.localStreams[i].start();
  82. }
  83. }
  84. RTC.prototype.selectedEndpoint = function (id) {
  85. if(this.dataChannels)
  86. this.dataChannels.handleSelectedEndpointEvent(id);
  87. }
  88. RTC.prototype.pinEndpoint = function (id) {
  89. if(this.dataChannels)
  90. this.dataChannels.handlePinnedEndpointEvent(id);
  91. }
  92. RTC.prototype.addStreamListener = function (listener, eventType) {
  93. this.eventEmitter.on(eventType, listener);
  94. };
  95. RTC.prototype.addListener = function (type, listener) {
  96. this.eventEmitter.on(type, listener);
  97. };
  98. RTC.prototype.removeListener = function (listener, eventType) {
  99. this.eventEmitter.removeListener(eventType, listener);
  100. };
  101. RTC.prototype.removeStreamListener = function (listener, eventType) {
  102. if(!(eventType instanceof StreamEventTypes))
  103. throw "Illegal argument";
  104. this.eventEmitter.removeListener(eventType, listener);
  105. };
  106. RTC.addRTCReadyListener = function (listener) {
  107. RTCUtils.eventEmitter.on(RTCEvents.RTC_READY, listener);
  108. }
  109. RTC.removeRTCReadyListener = function (listener) {
  110. RTCUtils.eventEmitter.removeListener(RTCEvents.RTC_READY, listener);
  111. }
  112. RTC.isRTCReady = function () {
  113. return rtcReady;
  114. }
  115. RTC.init = function (options) {
  116. // In case of IE we continue from 'onReady' callback
  117. // passed to RTCUtils constructor. It will be invoked by Temasys plugin
  118. // once it is initialized.
  119. var onReady = function () {
  120. rtcReady = true;
  121. RTCUtils.eventEmitter.emit(RTCEvents.RTC_READY, true);
  122. };
  123. RTCUtils.init(onReady, options || {});
  124. // Call onReady() if Temasys plugin is not used
  125. if (!RTCBrowserType.isTemasysPluginUsed()) {
  126. onReady();
  127. }
  128. }
  129. RTC.prototype.createLocalStreams = function (streams, change) {
  130. for (var i = 0; i < streams.length; i++) {
  131. var localStream = new JitsiLocalTrack(this, streams[i].stream,
  132. this.eventEmitter, streams[i].videoType,
  133. streams[i].isGUMStream);
  134. this.localStreams.push(localStream);
  135. if (streams[i].isMuted === true)
  136. localStream.setMute(true);
  137. if (streams[i].type == "audio") {
  138. this.localAudio = localStream;
  139. } else {
  140. this.localVideo = localStream;
  141. }
  142. var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
  143. if (change)
  144. eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED;
  145. this.eventEmitter.emit(eventType, localStream, streams[i].isMuted);
  146. }
  147. return this.localStreams;
  148. };
  149. RTC.prototype.removeLocalStream = function (stream) {
  150. for(var i = 0; i < this.localStreams.length; i++) {
  151. if(this.localStreams[i].getOriginalStream() === stream) {
  152. delete this.localStreams[i];
  153. return;
  154. }
  155. }
  156. };
  157. RTC.prototype.createRemoteStream = function (data, sid, thessrc) {
  158. var remoteStream = new JitsiRemoteTrack(this, data, sid, thessrc,
  159. this.eventEmitter);
  160. if(!data.peerjid)
  161. return;
  162. var jid = data.peerjid;
  163. if(!this.remoteStreams[jid]) {
  164. this.remoteStreams[jid] = {};
  165. }
  166. this.remoteStreams[jid][remoteStream.type]= remoteStream;
  167. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, remoteStream);
  168. return remoteStream;
  169. };
  170. RTC.getPCConstraints = function () {
  171. return RTCUtils.pc_constraints;
  172. };
  173. RTC.prototype.getUserMediaWithConstraints = function(um, success_callback,
  174. failure_callback, resolution,
  175. bandwidth, fps, desktopStream)
  176. {
  177. return RTCUtils.getUserMediaWithConstraints(this, um, success_callback,
  178. failure_callback, resolution, bandwidth, fps, desktopStream);
  179. };
  180. RTC.attachMediaStream = function (elSelector, stream) {
  181. RTCUtils.attachMediaStream(elSelector, stream);
  182. };
  183. RTC.getStreamID = function (stream) {
  184. return RTCUtils.getStreamID(stream);
  185. };
  186. RTC.getVideoSrc = function (element) {
  187. return RTCUtils.getVideoSrc(element);
  188. };
  189. RTC.setVideoSrc = function (element, src) {
  190. RTCUtils.setVideoSrc(element, src);
  191. };
  192. RTC.prototype.getVideoElementName = function () {
  193. return RTCBrowserType.isTemasysPluginUsed() ? 'object' : 'video';
  194. };
  195. RTC.prototype.dispose = function() {
  196. };
  197. RTC.prototype.muteRemoteVideoStream = function (jid, value) {
  198. var stream;
  199. if(this.remoteStreams[jid] &&
  200. this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  201. stream = this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  202. }
  203. if(!stream)
  204. return true;
  205. if (value != stream.muted) {
  206. stream.setMute(value);
  207. return true;
  208. }
  209. return false;
  210. };
  211. RTC.prototype.switchVideoStreams = function (new_stream) {
  212. this.localVideo.stream = new_stream;
  213. this.localStreams = [];
  214. //in firefox we have only one stream object
  215. if (this.localAudio.getOriginalStream() != new_stream)
  216. this.localStreams.push(this.localAudio);
  217. this.localStreams.push(this.localVideo);
  218. };
  219. /**
  220. * Creates <tt>JitsiTrack</tt> instance and replaces it with the local video.
  221. * The method also handles the sdp changes.
  222. * @param stream the new MediaStream received by the browser.
  223. * @param isUsingScreenStream <tt>true</tt> if the stream is for desktop stream.
  224. * @param callback - function that will be called after the operation is completed.
  225. */
  226. RTC.prototype.changeLocalVideo = function (stream, isUsingScreenStream, callback) {
  227. var oldStream = this.localVideo.getOriginalStream();
  228. var type = (isUsingScreenStream ? "screen" : "camera");
  229. var localCallback = callback;
  230. if(this.localVideo.isMuted() && this.localVideo.videoType !== type) {
  231. localCallback = function() {
  232. this.room.setVideoMute(false, function(mute) {
  233. this.eventEmitter.emit(RTCEvents.VIDEO_MUTE, mute);
  234. }.bind(this));
  235. callback();
  236. };
  237. }
  238. // FIXME: Workaround for FF/IE/Safari
  239. if (stream && stream.videoStream) {
  240. stream = stream.videoStream;
  241. }
  242. var videoStream = RTCUtils.createStream(stream, true);
  243. this.localVideo = this.createLocalStream(videoStream, "video", true, type);
  244. // Stop the stream to trigger onended event for old stream
  245. oldStream.stop();
  246. this.switchVideoStreams(videoStream, oldStream);
  247. this.room.switchStreams(videoStream, oldStream,localCallback);
  248. };
  249. /**
  250. * Creates <tt>JitsiTrack</tt> instance and replaces it with the local audio.
  251. * The method also handles the sdp changes.
  252. * @param stream the new MediaStream received by the browser.
  253. * @param callback - function that will be called after the operation is completed.
  254. */
  255. RTC.prototype.changeLocalAudio = function (stream, callback) {
  256. var oldStream = this.localAudio.getOriginalStream();
  257. var newStream = RTCUtils.createStream(stream);
  258. this.localAudio = this.createLocalStream(newStream, "audio", true);
  259. // Stop the stream to trigger onended event for old stream
  260. oldStream.stop();
  261. this.room.switchStreams(newStream, oldStream, callback, true);
  262. };
  263. RTC.prototype.isVideoMuted = function (jid) {
  264. if (jid === APP.xmpp.myJid()) {
  265. var localVideo = APP.RTC.localVideo;
  266. return (!localVideo || localVideo.isMuted());
  267. } else {
  268. if (!this.remoteStreams[jid] ||
  269. !this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  270. return null;
  271. }
  272. return this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE].muted;
  273. }
  274. };
  275. RTC.prototype.setVideoMute = function (mute, callback, options) {
  276. if (!this.localVideo)
  277. return;
  278. if (mute == this.localVideo.isMuted())
  279. {
  280. APP.xmpp.sendVideoInfoPresence(mute);
  281. if (callback)
  282. callback(mute);
  283. }
  284. else
  285. {
  286. this.localVideo.setMute(mute);
  287. this.room.setVideoMute(
  288. mute,
  289. callback,
  290. options);
  291. }
  292. };
  293. RTC.prototype.setDeviceAvailability = function (devices) {
  294. if(!devices)
  295. return;
  296. if(devices.audio === true || devices.audio === false)
  297. this.devices.audio = devices.audio;
  298. if(devices.video === true || devices.video === false)
  299. this.devices.video = devices.video;
  300. this.eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, this.devices);
  301. };
  302. module.exports = RTC;