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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* global __filename, APP, module */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var EventEmitter = require("events");
  4. var RTCBrowserType = require("./RTCBrowserType");
  5. var RTCEvents = require("../../service/RTC/RTCEvents.js");
  6. var RTCUtils = require("./RTCUtils.js");
  7. var JitsiTrack = require("./JitsiTrack");
  8. var JitsiLocalTrack = require("./JitsiLocalTrack.js");
  9. var DataChannels = require("./DataChannels");
  10. var JitsiRemoteTrack = require("./JitsiRemoteTrack.js");
  11. var MediaType = require("../../service/RTC/MediaType");
  12. var VideoType = require("../../service/RTC/VideoType");
  13. var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
  14. function createLocalTracks(tracksInfo, options) {
  15. var newTracks = [];
  16. var deviceId = null;
  17. tracksInfo.forEach(function(trackInfo){
  18. if (trackInfo.mediaType === MediaType.AUDIO) {
  19. deviceId = options.micDeviceId;
  20. } else if (trackInfo.videoType === VideoType.CAMERA){
  21. deviceId = options.cameraDeviceId;
  22. }
  23. var localTrack
  24. = new JitsiLocalTrack(
  25. trackInfo.stream,
  26. trackInfo.track,
  27. trackInfo.mediaType,
  28. trackInfo.videoType, trackInfo.resolution, deviceId);
  29. newTracks.push(localTrack);
  30. });
  31. return newTracks;
  32. }
  33. function RTC(room, options) {
  34. this.room = room;
  35. this.localTracks = [];
  36. //FIXME: We should support multiple streams per jid.
  37. this.remoteTracks = {};
  38. this.localAudio = null;
  39. this.localVideo = null;
  40. this.eventEmitter = new EventEmitter();
  41. var self = this;
  42. this.options = options || {};
  43. room.addPresenceListener("videomuted", function (values, from) {
  44. var videoTrack = self.getRemoteVideoTrack(from);
  45. if (videoTrack) {
  46. videoTrack.setMute(values.value == "true");
  47. }
  48. });
  49. room.addPresenceListener("audiomuted", function (values, from) {
  50. var audioTrack = self.getRemoteAudioTrack(from);
  51. if (audioTrack) {
  52. audioTrack.setMute(values.value == "true");
  53. }
  54. });
  55. room.addPresenceListener("videoType", function(data, from) {
  56. var videoTrack = self.getRemoteVideoTrack(from);
  57. if (videoTrack) {
  58. videoTrack._setVideoType(data.value);
  59. }
  60. });
  61. // Switch audio output device on all remote audio tracks. Local audio tracks
  62. // handle this event by themselves.
  63. if (RTCUtils.isDeviceChangeAvailable('output')) {
  64. RTCUtils.addListener(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  65. function (deviceId) {
  66. for (var key in self.remoteTracks) {
  67. if (self.remoteTracks.hasOwnProperty(key)
  68. && self.remoteTracks[key].audio) {
  69. self.remoteTracks[key].audio.setAudioOutput(deviceId);
  70. }
  71. }
  72. });
  73. }
  74. }
  75. /**
  76. * Creates the local MediaStreams.
  77. * @param {Object} [options] optional parameters
  78. * @param {Array} options.devices the devices that will be requested
  79. * @param {string} options.resolution resolution constraints
  80. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the
  81. * following structure {stream: the Media Stream,
  82. * type: "audio" or "video", videoType: "camera" or "desktop"}
  83. * will be returned trough the Promise, otherwise JitsiTrack objects will be
  84. * returned.
  85. * @param {string} options.cameraDeviceId
  86. * @param {string} options.micDeviceId
  87. * @returns {*} Promise object that will receive the new JitsiTracks
  88. */
  89. RTC.obtainAudioAndVideoPermissions = function (options) {
  90. return RTCUtils.obtainAudioAndVideoPermissions(options).then(
  91. function (tracksInfo) {
  92. return createLocalTracks(tracksInfo, options);
  93. });
  94. };
  95. RTC.prototype.onIncommingCall = function(event) {
  96. if(this.options.config.openSctp)
  97. this.dataChannels = new DataChannels(event.peerconnection,
  98. this.eventEmitter);
  99. // Add local Tracks to the ChatRoom
  100. this.localTracks.forEach(function(localTrack) {
  101. var ssrcInfo = null;
  102. if(localTrack.isVideoTrack() && localTrack.isMuted()) {
  103. /**
  104. * Handles issues when the stream is added before the peerconnection
  105. * is created. The peerconnection is created when second participant
  106. * enters the call. In that use case the track doesn't have
  107. * information about it's ssrcs and no jingle packets are sent. That
  108. * can cause inconsistent behavior later.
  109. *
  110. * For example:
  111. * If we mute the stream and than second participant enter it's
  112. * remote SDP won't include that track. On unmute we are not sending
  113. * any jingle packets which will brake the unmute.
  114. *
  115. * In order to solve issues like the above one here we have to
  116. * generate the ssrc information for the track .
  117. */
  118. localTrack._setSSRC(
  119. this.room.generateNewStreamSSRCInfo());
  120. ssrcInfo = {
  121. mtype: localTrack.getType(),
  122. type: "addMuted",
  123. ssrc: localTrack.ssrc,
  124. msid: localTrack.initialMSID
  125. };
  126. }
  127. try {
  128. this.room.addStream(
  129. localTrack.getOriginalStream(), function () {}, function () {},
  130. ssrcInfo, true);
  131. } catch(e) {
  132. GlobalOnErrorHandler.callErrorHandler(e);
  133. logger.error(e);
  134. }
  135. }.bind(this));
  136. };
  137. RTC.prototype.selectEndpoint = function (id) {
  138. if(this.dataChannels)
  139. this.dataChannels.sendSelectedEndpointMessage(id);
  140. };
  141. RTC.prototype.pinEndpoint = function (id) {
  142. if(this.dataChannels)
  143. this.dataChannels.sendPinnedEndpointMessage(id);
  144. };
  145. RTC.prototype.addListener = function (type, listener) {
  146. this.eventEmitter.on(type, listener);
  147. };
  148. RTC.prototype.removeListener = function (eventType, listener) {
  149. this.eventEmitter.removeListener(eventType, listener);
  150. };
  151. RTC.addListener = function (eventType, listener) {
  152. RTCUtils.addListener(eventType, listener);
  153. };
  154. RTC.removeListener = function (eventType, listener) {
  155. RTCUtils.removeListener(eventType, listener)
  156. };
  157. RTC.isRTCReady = function () {
  158. return RTCUtils.isRTCReady();
  159. };
  160. RTC.init = function (options) {
  161. this.options = options || {};
  162. return RTCUtils.init(this.options);
  163. };
  164. RTC.getDeviceAvailability = function () {
  165. return RTCUtils.getDeviceAvailability();
  166. };
  167. RTC.prototype.addLocalTrack = function (track) {
  168. if (!track)
  169. throw new Error('track must not be null nor undefined');
  170. this.localTracks.push(track);
  171. track._setRTC(this);
  172. if (track.isAudioTrack()) {
  173. this.localAudio = track;
  174. } else {
  175. this.localVideo = track;
  176. }
  177. };
  178. /**
  179. * Get local video track.
  180. * @returns {JitsiLocalTrack}
  181. */
  182. RTC.prototype.getLocalVideoTrack = function () {
  183. return this.localVideo;
  184. };
  185. /**
  186. * Gets JitsiRemoteTrack for AUDIO MediaType associated with given MUC nickname
  187. * (resource part of the JID).
  188. * @param resource the resource part of the MUC JID
  189. * @returns {JitsiRemoteTrack|null}
  190. */
  191. RTC.prototype.getRemoteAudioTrack = function (resource) {
  192. if (this.remoteTracks[resource])
  193. return this.remoteTracks[resource][MediaType.AUDIO];
  194. else
  195. return null;
  196. };
  197. /**
  198. * Gets JitsiRemoteTrack for VIDEO MediaType associated with given MUC nickname
  199. * (resource part of the JID).
  200. * @param resource the resource part of the MUC JID
  201. * @returns {JitsiRemoteTrack|null}
  202. */
  203. RTC.prototype.getRemoteVideoTrack = function (resource) {
  204. if (this.remoteTracks[resource])
  205. return this.remoteTracks[resource][MediaType.VIDEO];
  206. else
  207. return null;
  208. };
  209. /**
  210. * Set mute for all local audio streams attached to the conference.
  211. * @param value the mute value
  212. * @returns {Promise}
  213. */
  214. RTC.prototype.setAudioMute = function (value) {
  215. var mutePromises = [];
  216. for(var i = 0; i < this.localTracks.length; i++) {
  217. var track = this.localTracks[i];
  218. if(track.getType() !== MediaType.AUDIO) {
  219. continue;
  220. }
  221. // this is a Promise
  222. mutePromises.push(value ? track.mute() : track.unmute());
  223. }
  224. // we return a Promise from all Promises so we can wait for their execution
  225. return Promise.all(mutePromises);
  226. };
  227. RTC.prototype.removeLocalTrack = function (track) {
  228. var pos = this.localTracks.indexOf(track);
  229. if (pos === -1) {
  230. return;
  231. }
  232. this.localTracks.splice(pos, 1);
  233. if (track.isAudioTrack()) {
  234. this.localAudio = null;
  235. } else {
  236. this.localVideo = null;
  237. }
  238. };
  239. /**
  240. * Initializes a new JitsiRemoteTrack instance with the data provided by (a)
  241. * ChatRoom to XMPPEvents.REMOTE_TRACK_ADDED.
  242. *
  243. * @param {Object} event the data provided by (a) ChatRoom to
  244. * XMPPEvents.REMOTE_TRACK_ADDED to (a)
  245. */
  246. RTC.prototype.createRemoteTrack = function (event) {
  247. var ownerJid = event.owner;
  248. var remoteTrack = new JitsiRemoteTrack(
  249. this, ownerJid, event.stream, event.track,
  250. event.mediaType, event.videoType, event.ssrc, event.muted);
  251. var resource = Strophe.getResourceFromJid(ownerJid);
  252. var remoteTracks
  253. = this.remoteTracks[resource] || (this.remoteTracks[resource] = {});
  254. var mediaType = remoteTrack.getType();
  255. if (remoteTracks[mediaType]) {
  256. logger.warn("Overwriting remote track!", resource, mediaType);
  257. }
  258. remoteTracks[mediaType] = remoteTrack;
  259. return remoteTrack;
  260. };
  261. /**
  262. * Removes all JitsiRemoteTracks associated with given MUC nickname (resource
  263. * part of the JID).
  264. * @param resource the resource part of the MUC JID
  265. * @returns {JitsiRemoteTrack|null}
  266. */
  267. RTC.prototype.removeRemoteTracks = function (resource) {
  268. var remoteTracks = this.remoteTracks[resource];
  269. if(remoteTracks) {
  270. remoteTracks['audio'] && remoteTracks['audio'].dispose();
  271. remoteTracks['video'] && remoteTracks['video'].dispose();
  272. delete this.remoteTracks[resource];
  273. }
  274. };
  275. RTC.getPCConstraints = function () {
  276. return RTCUtils.pc_constraints;
  277. };
  278. RTC.attachMediaStream = function (elSelector, stream) {
  279. return RTCUtils.attachMediaStream(elSelector, stream);
  280. };
  281. RTC.getStreamID = function (stream) {
  282. return RTCUtils.getStreamID(stream);
  283. };
  284. /**
  285. * Returns true if retrieving the the list of input devices is supported and
  286. * false if not.
  287. */
  288. RTC.isDeviceListAvailable = function () {
  289. return RTCUtils.isDeviceListAvailable();
  290. };
  291. /**
  292. * Returns true if changing the input (camera / microphone) or output
  293. * (audio) device is supported and false if not.
  294. * @params {string} [deviceType] - type of device to change. Default is
  295. * undefined or 'input', 'output' - for audio output device change.
  296. * @returns {boolean} true if available, false otherwise.
  297. */
  298. RTC.isDeviceChangeAvailable = function (deviceType) {
  299. return RTCUtils.isDeviceChangeAvailable(deviceType);
  300. };
  301. /**
  302. * Returns currently used audio output device id, '' stands for default
  303. * device
  304. * @returns {string}
  305. */
  306. RTC.getAudioOutputDevice = function () {
  307. return RTCUtils.getAudioOutputDevice();
  308. };
  309. /**
  310. * Returns list of available media devices if its obtained, otherwise an
  311. * empty array is returned/
  312. * @returns {Array} list of available media devices.
  313. */
  314. RTC.getCurrentlyAvailableMediaDevices = function () {
  315. return RTCUtils.getCurrentlyAvailableMediaDevices();
  316. };
  317. /**
  318. * Returns event data for device to be reported to stats.
  319. * @returns {MediaDeviceInfo} device.
  320. */
  321. RTC.getEventDataForActiveDevice = function (device) {
  322. return RTCUtils.getEventDataForActiveDevice(device);
  323. };
  324. /**
  325. * Sets current audio output device.
  326. * @param {string} deviceId - id of 'audiooutput' device from
  327. * navigator.mediaDevices.enumerateDevices()
  328. * @returns {Promise} - resolves when audio output is changed, is rejected
  329. * otherwise
  330. */
  331. RTC.setAudioOutputDevice = function (deviceId) {
  332. return RTCUtils.setAudioOutputDevice(deviceId);
  333. };
  334. /**
  335. * Returns <tt>true<tt/> if given WebRTC MediaStream is considered a valid
  336. * "user" stream which means that it's not a "receive only" stream nor a "mixed"
  337. * JVB stream.
  338. *
  339. * Clients that implement Unified Plan, such as Firefox use recvonly
  340. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed to
  341. * Plan B where there are only 3 channels: audio, video and data.
  342. *
  343. * @param stream WebRTC MediaStream instance
  344. * @returns {boolean}
  345. */
  346. RTC.isUserStream = function (stream) {
  347. var streamId = RTCUtils.getStreamID(stream);
  348. return streamId && streamId !== "mixedmslabel" && streamId !== "default";
  349. };
  350. /**
  351. * Allows to receive list of available cameras/microphones.
  352. * @param {function} callback would receive array of devices as an argument
  353. */
  354. RTC.enumerateDevices = function (callback) {
  355. RTCUtils.enumerateDevices(callback);
  356. };
  357. /**
  358. * A method to handle stopping of the stream.
  359. * One point to handle the differences in various implementations.
  360. * @param mediaStream MediaStream object to stop.
  361. */
  362. RTC.stopMediaStream = function (mediaStream) {
  363. RTCUtils.stopMediaStream(mediaStream);
  364. };
  365. /**
  366. * Returns whether the desktop sharing is enabled or not.
  367. * @returns {boolean}
  368. */
  369. RTC.isDesktopSharingEnabled = function () {
  370. return RTCUtils.isDesktopSharingEnabled();
  371. };
  372. /**
  373. * Closes all currently opened data channels.
  374. */
  375. RTC.prototype.closeAllDataChannels = function () {
  376. this.dataChannels.closeAllChannels();
  377. };
  378. RTC.prototype.dispose = function() {
  379. };
  380. /*
  381. //FIXME Never used, but probably *should* be used for switching
  382. // between camera and screen, but has to be adjusted to work with tracks.
  383. // Current when switching to desktop we can see recv-only being advertised
  384. // because we do remove and add.
  385. //
  386. // Leaving it commented out, in order to not forget about FF specific
  387. // thing
  388. RTC.prototype.switchVideoTracks = function (newStream) {
  389. this.localVideo.stream = newStream;
  390. this.localTracks = [];
  391. //in firefox we have only one stream object
  392. if (this.localAudio.getOriginalStream() != newStream)
  393. this.localTracks.push(this.localAudio);
  394. this.localTracks.push(this.localVideo);
  395. };*/
  396. RTC.prototype.setAudioLevel = function (resource, audioLevel) {
  397. if(!resource)
  398. return;
  399. var audioTrack = this.getRemoteAudioTrack(resource);
  400. if(audioTrack) {
  401. audioTrack.setAudioLevel(audioLevel);
  402. }
  403. };
  404. /**
  405. * Searches in localTracks(session stores ssrc for audio and video) and
  406. * remoteTracks for the ssrc and returns the corresponding resource.
  407. * @param ssrc the ssrc to check.
  408. */
  409. RTC.prototype.getResourceBySSRC = function (ssrc) {
  410. if((this.localVideo && ssrc == this.localVideo.getSSRC())
  411. || (this.localAudio && ssrc == this.localAudio.getSSRC())) {
  412. return Strophe.getResourceFromJid(this.room.myroomjid);
  413. }
  414. var track = this.getRemoteTrackBySSRC(ssrc);
  415. return track? track.getParticipantId() : null;
  416. };
  417. /**
  418. * Searches in remoteTracks for the ssrc and returns the corresponding track.
  419. * @param ssrc the ssrc to check.
  420. */
  421. RTC.prototype.getRemoteTrackBySSRC = function (ssrc) {
  422. for (var resource in this.remoteTracks) {
  423. var track = this.getRemoteAudioTrack(resource);
  424. if(track && track.getSSRC() == ssrc) {
  425. return track;
  426. }
  427. track = this.getRemoteVideoTrack(resource);
  428. if(track && track.getSSRC() == ssrc) {
  429. return track;
  430. }
  431. }
  432. return null;
  433. };
  434. module.exports = RTC;