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.

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