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.

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