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.

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