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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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.selectEndpoint = function (id) {
  83. if(this.dataChannels)
  84. this.dataChannels.sendSelectedEndpointMessage(id);
  85. };
  86. RTC.prototype.pinEndpoint = function (id) {
  87. if(this.dataChannels)
  88. this.dataChannels.sendPinnedEndpointMessage(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). Returns array of removed tracks.
  216. *
  217. * @param {string} resource - The resource part of the MUC JID.
  218. * @returns {JitsiRemoteTrack[]}
  219. */
  220. RTC.prototype.removeRemoteTracks = function (resource) {
  221. var removedTracks = [];
  222. var removedAudioTrack = this.removeRemoteTrack(resource, MediaType.AUDIO);
  223. var removedVideoTrack = this.removeRemoteTrack(resource, MediaType.VIDEO);
  224. removedAudioTrack && removedTracks.push(removedAudioTrack);
  225. removedVideoTrack && removedTracks.push(removedVideoTrack);
  226. delete this.remoteTracks[resource];
  227. return removedTracks;
  228. };
  229. /**
  230. * Removes specified track type associated with given MUC nickname
  231. * (resource part of the JID). Returns removed track if any.
  232. *
  233. * @param {string} resource - The resource part of the MUC JID.
  234. * @param {string} mediaType - Type of track to remove.
  235. * @returns {JitsiRemoteTrack|undefined}
  236. */
  237. RTC.prototype.removeRemoteTrack = function (resource, mediaType) {
  238. var remoteTracksForResource = this.remoteTracks[resource];
  239. if (remoteTracksForResource && remoteTracksForResource[mediaType]) {
  240. var track = remoteTracksForResource[mediaType];
  241. track.dispose();
  242. delete remoteTracksForResource[mediaType];
  243. return track;
  244. }
  245. };
  246. RTC.getPCConstraints = function () {
  247. return RTCUtils.pc_constraints;
  248. };
  249. RTC.attachMediaStream = function (elSelector, stream) {
  250. return RTCUtils.attachMediaStream(elSelector, stream);
  251. };
  252. RTC.getStreamID = function (stream) {
  253. return RTCUtils.getStreamID(stream);
  254. };
  255. /**
  256. * Returns true if retrieving the the list of input devices is supported and
  257. * false if not.
  258. */
  259. RTC.isDeviceListAvailable = function () {
  260. return RTCUtils.isDeviceListAvailable();
  261. };
  262. /**
  263. * Returns true if changing the input (camera / microphone) or output
  264. * (audio) device is supported and false if not.
  265. * @params {string} [deviceType] - type of device to change. Default is
  266. * undefined or 'input', 'output' - for audio output device change.
  267. * @returns {boolean} true if available, false otherwise.
  268. */
  269. RTC.isDeviceChangeAvailable = function (deviceType) {
  270. return RTCUtils.isDeviceChangeAvailable(deviceType);
  271. };
  272. /**
  273. * Returns currently used audio output device id, '' stands for default
  274. * device
  275. * @returns {string}
  276. */
  277. RTC.getAudioOutputDevice = function () {
  278. return RTCUtils.getAudioOutputDevice();
  279. };
  280. /**
  281. * Returns list of available media devices if its obtained, otherwise an
  282. * empty array is returned/
  283. * @returns {Array} list of available media devices.
  284. */
  285. RTC.getCurrentlyAvailableMediaDevices = function () {
  286. return RTCUtils.getCurrentlyAvailableMediaDevices();
  287. };
  288. /**
  289. * Returns event data for device to be reported to stats.
  290. * @returns {MediaDeviceInfo} device.
  291. */
  292. RTC.getEventDataForActiveDevice = function (device) {
  293. return RTCUtils.getEventDataForActiveDevice(device);
  294. };
  295. /**
  296. * Sets current audio output device.
  297. * @param {string} deviceId - id of 'audiooutput' device from
  298. * navigator.mediaDevices.enumerateDevices()
  299. * @returns {Promise} - resolves when audio output is changed, is rejected
  300. * otherwise
  301. */
  302. RTC.setAudioOutputDevice = function (deviceId) {
  303. return RTCUtils.setAudioOutputDevice(deviceId);
  304. };
  305. /**
  306. * Returns <tt>true<tt/> if given WebRTC MediaStream is considered a valid
  307. * "user" stream which means that it's not a "receive only" stream nor a "mixed"
  308. * JVB stream.
  309. *
  310. * Clients that implement Unified Plan, such as Firefox use recvonly
  311. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed to
  312. * Plan B where there are only 3 channels: audio, video and data.
  313. *
  314. * @param stream WebRTC MediaStream instance
  315. * @returns {boolean}
  316. */
  317. RTC.isUserStream = function (stream) {
  318. var streamId = RTCUtils.getStreamID(stream);
  319. return streamId && streamId !== "mixedmslabel" && streamId !== "default";
  320. };
  321. /**
  322. * Allows to receive list of available cameras/microphones.
  323. * @param {function} callback would receive array of devices as an argument
  324. */
  325. RTC.enumerateDevices = function (callback) {
  326. RTCUtils.enumerateDevices(callback);
  327. };
  328. /**
  329. * A method to handle stopping of the stream.
  330. * One point to handle the differences in various implementations.
  331. * @param mediaStream MediaStream object to stop.
  332. */
  333. RTC.stopMediaStream = function (mediaStream) {
  334. RTCUtils.stopMediaStream(mediaStream);
  335. };
  336. /**
  337. * Returns whether the desktop sharing is enabled or not.
  338. * @returns {boolean}
  339. */
  340. RTC.isDesktopSharingEnabled = function () {
  341. return RTCUtils.isDesktopSharingEnabled();
  342. };
  343. /**
  344. * Closes all currently opened data channels.
  345. */
  346. RTC.prototype.closeAllDataChannels = function () {
  347. if(this.dataChannels)
  348. this.dataChannels.closeAllChannels();
  349. };
  350. RTC.prototype.dispose = function() {
  351. };
  352. /*
  353. //FIXME Never used, but probably *should* be used for switching
  354. // between camera and screen, but has to be adjusted to work with tracks.
  355. // Current when switching to desktop we can see recv-only being advertised
  356. // because we do remove and add.
  357. //
  358. // Leaving it commented out, in order to not forget about FF specific
  359. // thing
  360. RTC.prototype.switchVideoTracks = function (newStream) {
  361. this.localVideo.stream = newStream;
  362. this.localTracks = [];
  363. //in firefox we have only one stream object
  364. if (this.localAudio.getOriginalStream() != newStream)
  365. this.localTracks.push(this.localAudio);
  366. this.localTracks.push(this.localVideo);
  367. };*/
  368. RTC.prototype.setAudioLevel = function (resource, audioLevel) {
  369. if(!resource)
  370. return;
  371. var audioTrack = this.getRemoteAudioTrack(resource);
  372. if(audioTrack) {
  373. audioTrack.setAudioLevel(audioLevel);
  374. }
  375. };
  376. /**
  377. * Searches in localTracks(session stores ssrc for audio and video) and
  378. * remoteTracks for the ssrc and returns the corresponding resource.
  379. * @param ssrc the ssrc to check.
  380. */
  381. RTC.prototype.getResourceBySSRC = function (ssrc) {
  382. if((this.localVideo && ssrc == this.localVideo.getSSRC())
  383. || (this.localAudio && ssrc == this.localAudio.getSSRC())) {
  384. return this.conference.myUserId();
  385. }
  386. var track = this.getRemoteTrackBySSRC(ssrc);
  387. return track? track.getParticipantId() : null;
  388. };
  389. /**
  390. * Searches in remoteTracks for the ssrc and returns the corresponding track.
  391. * @param ssrc the ssrc to check.
  392. */
  393. RTC.prototype.getRemoteTrackBySSRC = function (ssrc) {
  394. for (var resource in this.remoteTracks) {
  395. var track = this.getRemoteAudioTrack(resource);
  396. if(track && track.getSSRC() == ssrc) {
  397. return track;
  398. }
  399. track = this.getRemoteVideoTrack(resource);
  400. if(track && track.getSSRC() == ssrc) {
  401. return track;
  402. }
  403. }
  404. return null;
  405. };
  406. /**
  407. * Handles remote track mute / unmute events.
  408. * @param type {string} "audio" or "video"
  409. * @param isMuted {boolean} the new mute state
  410. * @param from {string} user id
  411. */
  412. RTC.prototype.handleRemoteTrackMute = function (type, isMuted, from) {
  413. var track = this.getRemoteTrackByType(type, from);
  414. if (track) {
  415. track.setMute(isMuted);
  416. }
  417. }
  418. /**
  419. * Handles remote track video type events
  420. * @param value {string} the new video type
  421. * @param from {string} user id
  422. */
  423. RTC.prototype.handleRemoteTrackVideoTypeChanged = function (value, from) {
  424. var videoTrack = this.getRemoteVideoTrack(from);
  425. if (videoTrack) {
  426. videoTrack._setVideoType(value);
  427. }
  428. }
  429. /**
  430. * Sends broadcast message via the datachannels.
  431. * @param payload {object} the payload of the message.
  432. */
  433. RTC.prototype.sendDataChannelBroadcast = function (payload) {
  434. if(this.dataChannels) {
  435. this.dataChannels.sendBroadcastMessage(payload);
  436. }
  437. }
  438. module.exports = RTC;