modified lib-jitsi-meet dev repo
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /* global __filename */
  2. import DataChannels from './DataChannels';
  3. import { getLogger } from 'jitsi-meet-logger';
  4. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  5. import JitsiLocalTrack from './JitsiLocalTrack.js';
  6. import JitsiRemoteTrack from './JitsiRemoteTrack.js';
  7. import JitsiTrackError from '../../JitsiTrackError';
  8. import * as JitsiTrackErrors from '../../JitsiTrackErrors';
  9. import Listenable from '../util/Listenable';
  10. import * as MediaType from '../../service/RTC/MediaType';
  11. import RTCEvents from '../../service/RTC/RTCEvents.js';
  12. import RTCUtils from './RTCUtils.js';
  13. import TraceablePeerConnection from './TraceablePeerConnection';
  14. import VideoType from '../../service/RTC/VideoType';
  15. const logger = getLogger(__filename);
  16. /**
  17. *
  18. * @param tracksInfo
  19. * @param options
  20. */
  21. function createLocalTracks(tracksInfo, options) {
  22. const newTracks = [];
  23. let deviceId = null;
  24. tracksInfo.forEach(trackInfo => {
  25. if (trackInfo.mediaType === MediaType.AUDIO) {
  26. deviceId = options.micDeviceId;
  27. } else if (trackInfo.videoType === VideoType.CAMERA) {
  28. deviceId = options.cameraDeviceId;
  29. }
  30. const localTrack
  31. = new JitsiLocalTrack(
  32. trackInfo.stream,
  33. trackInfo.track,
  34. trackInfo.mediaType,
  35. trackInfo.videoType,
  36. trackInfo.resolution,
  37. deviceId,
  38. options.facingMode);
  39. newTracks.push(localTrack);
  40. });
  41. return newTracks;
  42. }
  43. /**
  44. *
  45. */
  46. export default class RTC extends Listenable {
  47. /**
  48. *
  49. * @param conference
  50. * @param options
  51. */
  52. constructor(conference, options = {}) {
  53. super();
  54. this.conference = conference;
  55. /**
  56. * A map of active <tt>TraceablePeerConnection</tt>.
  57. * @type {Map.<number, TraceablePeerConnection>}
  58. */
  59. this.peerConnections = new Map();
  60. /**
  61. * The counter used to generated id numbers assigned to peer connections
  62. * @type {number}
  63. */
  64. this.peerConnectionIdCounter = 1;
  65. this.localTracks = [];
  66. // FIXME: We should support multiple streams per jid.
  67. this.remoteTracks = {};
  68. this.options = options;
  69. // A flag whether we had received that the data channel had opened
  70. // we can get this flag out of sync if for some reason data channel got
  71. // closed from server, a desired behaviour so we can see errors when
  72. // this happen
  73. this.dataChannelsOpen = false;
  74. // Switch audio output device on all remote audio tracks. Local audio
  75. // tracks handle this event by themselves.
  76. if (RTCUtils.isDeviceChangeAvailable('output')) {
  77. RTCUtils.addListener(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  78. deviceId => {
  79. const remoteAudioTracks
  80. = this.getRemoteTracks(MediaType.AUDIO);
  81. for (const track of remoteAudioTracks) {
  82. track.setAudioOutput(deviceId);
  83. }
  84. });
  85. }
  86. }
  87. /**
  88. * Creates the local MediaStreams.
  89. * @param {Object} [options] optional parameters
  90. * @param {Array} options.devices the devices that will be requested
  91. * @param {string} options.resolution resolution constraints
  92. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with
  93. * the following structure {stream: the Media Stream, type: "audio" or
  94. * "video", videoType: "camera" or "desktop"} will be returned trough the
  95. * Promise, otherwise JitsiTrack objects will be returned.
  96. * @param {string} options.cameraDeviceId
  97. * @param {string} options.micDeviceId
  98. * @returns {*} Promise object that will receive the new JitsiTracks
  99. */
  100. static obtainAudioAndVideoPermissions(options) {
  101. return RTCUtils.obtainAudioAndVideoPermissions(options).then(
  102. tracksInfo => {
  103. const tracks = createLocalTracks(tracksInfo, options);
  104. return tracks.some(track => !track._isReceivingData())
  105. ? Promise.reject(
  106. new JitsiTrackError(
  107. JitsiTrackErrors.NO_DATA_FROM_SOURCE))
  108. : tracks;
  109. });
  110. }
  111. /**
  112. * Initializes the data channels of this instance.
  113. * @param peerconnection the associated PeerConnection.
  114. */
  115. initializeDataChannels(peerconnection) {
  116. if (this.options.config.openSctp) {
  117. this.dataChannels = new DataChannels(peerconnection,
  118. this.eventEmitter);
  119. this._dataChannelOpenListener = () => {
  120. // mark that dataChannel is opened
  121. this.dataChannelsOpen = true;
  122. // when the data channel becomes available, tell the bridge
  123. // about video selections so that it can do adaptive simulcast,
  124. // we want the notification to trigger even if userJid
  125. // is undefined, or null.
  126. // XXX why do we not do the same for pinned endpoints?
  127. try {
  128. this.dataChannels.sendSelectedEndpointMessage(
  129. this.selectedEndpoint);
  130. } catch (error) {
  131. GlobalOnErrorHandler.callErrorHandler(error);
  132. logger.error('Cannot sendSelectedEndpointMessage ',
  133. this.selectedEndpoint, '. Error: ', error);
  134. }
  135. this.removeListener(RTCEvents.DATA_CHANNEL_OPEN,
  136. this._dataChannelOpenListener);
  137. this._dataChannelOpenListener = null;
  138. };
  139. this.addListener(RTCEvents.DATA_CHANNEL_OPEN,
  140. this._dataChannelOpenListener);
  141. this.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  142. lastNEndpoints => {
  143. this._lastNEndpoints = lastNEndpoints;
  144. }
  145. );
  146. }
  147. }
  148. /**
  149. * Should be called when current media session ends and after the
  150. * PeerConnection has been closed using PeerConnection.close() method.
  151. */
  152. onCallEnded() {
  153. if (this.dataChannels) {
  154. // DataChannels are not explicitly closed as the PeerConnection
  155. // is closed on call ended which triggers data channel onclose
  156. // events. The reference is cleared to disable any logic related
  157. // to the data channels.
  158. this.dataChannels = null;
  159. this.dataChannelsOpen = false;
  160. }
  161. }
  162. /**
  163. * Elects the participant with the given id to be the selected participant
  164. * in order to always receive video for this participant (even when last n
  165. * is enabled).
  166. * If there is no data channel we store it and send it through the channel
  167. * once it is created.
  168. * @param id {string} the user id.
  169. * @throws NetworkError or InvalidStateError or Error if the operation
  170. * fails.
  171. */
  172. selectEndpoint(id) {
  173. // cache the value if channel is missing, till we open it
  174. this.selectedEndpoint = id;
  175. if (this.dataChannels && this.dataChannelsOpen) {
  176. this.dataChannels.sendSelectedEndpointMessage(id);
  177. }
  178. }
  179. /**
  180. * Elects the participant with the given id to be the pinned participant in
  181. * order to always receive video for this participant (even when last n is
  182. * enabled).
  183. * @param id {string} the user id
  184. * @throws NetworkError or InvalidStateError or Error if the operation
  185. * fails.
  186. */
  187. pinEndpoint(id) {
  188. if (this.dataChannels) {
  189. this.dataChannels.sendPinnedEndpointMessage(id);
  190. } else {
  191. // FIXME: cache value while there is no data channel created
  192. // and send the cached state once channel is created
  193. throw new Error('Data channels support is disabled!');
  194. }
  195. }
  196. /**
  197. *
  198. * @param eventType
  199. * @param listener
  200. */
  201. static addListener(eventType, listener) {
  202. RTCUtils.addListener(eventType, listener);
  203. }
  204. /**
  205. *
  206. * @param eventType
  207. * @param listener
  208. */
  209. static removeListener(eventType, listener) {
  210. RTCUtils.removeListener(eventType, listener);
  211. }
  212. /**
  213. *
  214. */
  215. static isRTCReady() {
  216. return RTCUtils.isRTCReady();
  217. }
  218. /**
  219. *
  220. * @param options
  221. */
  222. static init(options = {}) {
  223. this.options = options;
  224. return RTCUtils.init(this.options);
  225. }
  226. /**
  227. *
  228. */
  229. static getDeviceAvailability() {
  230. return RTCUtils.getDeviceAvailability();
  231. }
  232. /**
  233. * Creates new <tt>TraceablePeerConnection</tt>
  234. * @param {SignalingLayer} signaling the signaling layer that will
  235. * provide information about the media or participants which is not carried
  236. * over SDP.
  237. * @param {Object} iceConfig an object describing the ICE config like
  238. * defined in the WebRTC specification.
  239. * @param {Object} options the config options
  240. * @param {boolean} options.disableSimulcast if set to 'true' will disable
  241. * the simulcast
  242. * @param {boolean} options.disableRtx if set to 'true' will disable the RTX
  243. * @param {boolean} options.preferH264 if set to 'true' H264 will be
  244. * preferred over other video codecs.
  245. * @return {TraceablePeerConnection}
  246. */
  247. createPeerConnection(signaling, iceConfig, options) {
  248. const newConnection
  249. = new TraceablePeerConnection(
  250. this,
  251. this.peerConnectionIdCounter,
  252. signaling, iceConfig, RTC.getPCConstraints(), options);
  253. this.peerConnections.set(newConnection.id, newConnection);
  254. this.peerConnectionIdCounter += 1;
  255. return newConnection;
  256. }
  257. /**
  258. * Removed given peer connection from this RTC module instance.
  259. * @param {TraceablePeerConnection} traceablePeerConnection
  260. * @return {boolean} <tt>true</tt> if the given peer connection was removed
  261. * successfully or <tt>false</tt> if there was no peer connection mapped in
  262. * this RTC instance.
  263. */
  264. _removePeerConnection(traceablePeerConnection) {
  265. const id = traceablePeerConnection.id;
  266. if (this.peerConnections.has(id)) {
  267. // NOTE Remote tracks are not removed here.
  268. this.peerConnections.delete(id);
  269. return true;
  270. }
  271. return false;
  272. }
  273. /**
  274. *
  275. * @param track
  276. */
  277. addLocalTrack(track) {
  278. if (!track) {
  279. throw new Error('track must not be null nor undefined');
  280. }
  281. this.localTracks.push(track);
  282. track.conference = this.conference;
  283. }
  284. /**
  285. * Get local video track.
  286. * @returns {JitsiLocalTrack|undefined}
  287. */
  288. getLocalVideoTrack() {
  289. const localVideo = this.getLocalTracks(MediaType.VIDEO);
  290. return localVideo.length ? localVideo[0] : undefined;
  291. }
  292. /**
  293. * Get local audio track.
  294. * @returns {JitsiLocalTrack|undefined}
  295. */
  296. getLocalAudioTrack() {
  297. const localAudio = this.getLocalTracks(MediaType.AUDIO);
  298. return localAudio.length ? localAudio[0] : undefined;
  299. }
  300. /**
  301. * Returns the local tracks of the given media type, or all local tracks if
  302. * no specific type is given.
  303. * @param {MediaType} [mediaType] optional media type filter
  304. * (audio or video).
  305. */
  306. getLocalTracks(mediaType) {
  307. let tracks = this.localTracks.slice();
  308. if (mediaType !== undefined) {
  309. tracks = tracks.filter(
  310. track => track.getType() === mediaType);
  311. }
  312. return tracks;
  313. }
  314. /**
  315. * Obtains all remote tracks currently known to this RTC module instance.
  316. * @param {MediaType} [mediaType] the remote tracks will be filtered
  317. * by their media type if this argument is specified.
  318. * @return {Array<JitsiRemoteTrack>}
  319. */
  320. getRemoteTracks(mediaType) {
  321. const remoteTracks = [];
  322. const remoteEndpoints = Object.keys(this.remoteTracks);
  323. for (const endpoint of remoteEndpoints) {
  324. const endpointMediaTypes = Object.keys(this.remoteTracks[endpoint]);
  325. for (const trackMediaType of endpointMediaTypes) {
  326. // per media type filtering
  327. if (!mediaType || mediaType === trackMediaType) {
  328. const mediaTrack
  329. = this.remoteTracks[endpoint][trackMediaType];
  330. if (mediaTrack) {
  331. remoteTracks.push(mediaTrack);
  332. }
  333. }
  334. }
  335. }
  336. return remoteTracks;
  337. }
  338. /**
  339. * Gets JitsiRemoteTrack for the passed MediaType associated with given MUC
  340. * nickname (resource part of the JID).
  341. * @param type audio or video.
  342. * @param resource the resource part of the MUC JID
  343. * @returns {JitsiRemoteTrack|null}
  344. */
  345. getRemoteTrackByType(type, resource) {
  346. if (this.remoteTracks[resource]) {
  347. return this.remoteTracks[resource][type];
  348. }
  349. return null;
  350. }
  351. /**
  352. * Gets JitsiRemoteTrack for AUDIO MediaType associated with given MUC
  353. * nickname (resource part of the JID).
  354. * @param resource the resource part of the MUC JID
  355. * @returns {JitsiRemoteTrack|null}
  356. */
  357. getRemoteAudioTrack(resource) {
  358. return this.getRemoteTrackByType(MediaType.AUDIO, resource);
  359. }
  360. /**
  361. * Gets JitsiRemoteTrack for VIDEO MediaType associated with given MUC
  362. * nickname (resource part of the JID).
  363. * @param resource the resource part of the MUC JID
  364. * @returns {JitsiRemoteTrack|null}
  365. */
  366. getRemoteVideoTrack(resource) {
  367. return this.getRemoteTrackByType(MediaType.VIDEO, resource);
  368. }
  369. /**
  370. * Set mute for all local audio streams attached to the conference.
  371. * @param value the mute value
  372. * @returns {Promise}
  373. */
  374. setAudioMute(value) {
  375. const mutePromises = [];
  376. this.getLocalTracks(MediaType.AUDIO).forEach(audioTrack => {
  377. // this is a Promise
  378. mutePromises.push(value ? audioTrack.mute() : audioTrack.unmute());
  379. });
  380. // We return a Promise from all Promises so we can wait for their
  381. // execution.
  382. return Promise.all(mutePromises);
  383. }
  384. /**
  385. *
  386. * @param track
  387. */
  388. removeLocalTrack(track) {
  389. const pos = this.localTracks.indexOf(track);
  390. if (pos === -1) {
  391. return;
  392. }
  393. this.localTracks.splice(pos, 1);
  394. }
  395. /* eslint-disable max-params */
  396. /**
  397. * Initializes a new JitsiRemoteTrack instance with the data provided by
  398. * the signaling layer and SDP.
  399. *
  400. * @param {string} ownerEndpointId
  401. * @param {MediaStream} stream
  402. * @param {MediaStreamTrack} track
  403. * @param {MediaType} mediaType
  404. * @param {VideoType|undefined} videoType
  405. * @param {string} ssrc
  406. * @param {boolean} muted
  407. */
  408. _createRemoteTrack(
  409. ownerEndpointId,
  410. stream,
  411. track,
  412. mediaType,
  413. videoType,
  414. ssrc,
  415. muted) {
  416. const remoteTrack
  417. = new JitsiRemoteTrack(
  418. this,
  419. this.conference,
  420. ownerEndpointId,
  421. stream,
  422. track,
  423. mediaType,
  424. videoType,
  425. ssrc,
  426. muted);
  427. const remoteTracks
  428. = this.remoteTracks[ownerEndpointId]
  429. || (this.remoteTracks[ownerEndpointId] = {});
  430. if (remoteTracks[mediaType]) {
  431. logger.error(
  432. 'Overwriting remote track!',
  433. ownerEndpointId,
  434. mediaType);
  435. }
  436. remoteTracks[mediaType] = remoteTrack;
  437. this.eventEmitter.emit(RTCEvents.REMOTE_TRACK_ADDED, remoteTrack);
  438. }
  439. /* eslint-enable max-params */
  440. /**
  441. * Removes all JitsiRemoteTracks associated with given MUC nickname
  442. * (resource part of the JID). Returns array of removed tracks.
  443. *
  444. * @param {string} owner - The resource part of the MUC JID.
  445. * @returns {JitsiRemoteTrack[]}
  446. */
  447. removeRemoteTracks(owner) {
  448. const removedTracks = [];
  449. if (this.remoteTracks[owner]) {
  450. const removedAudioTrack
  451. = this.remoteTracks[owner][MediaType.AUDIO];
  452. const removedVideoTrack
  453. = this.remoteTracks[owner][MediaType.VIDEO];
  454. removedAudioTrack && removedTracks.push(removedAudioTrack);
  455. removedVideoTrack && removedTracks.push(removedVideoTrack);
  456. delete this.remoteTracks[owner];
  457. }
  458. return removedTracks;
  459. }
  460. /**
  461. * Finds remote track by it's stream and track ids.
  462. * @param {string} streamId the media stream id as defined by the WebRTC
  463. * @param {string} trackId the media track id as defined by the WebRTC
  464. * @return {JitsiRemoteTrack|undefined}
  465. * @private
  466. */
  467. _getRemoteTrackById(streamId, trackId) {
  468. let result;
  469. // .find will break the loop once the first match is found
  470. Object.keys(this.remoteTracks).find(endpoint => {
  471. const endpointTracks = this.remoteTracks[endpoint];
  472. return endpointTracks && Object.keys(endpointTracks).find(
  473. mediaType => {
  474. const mediaTrack = endpointTracks[mediaType];
  475. if (mediaTrack
  476. && mediaTrack.getStreamId() === streamId
  477. && mediaTrack.getTrackId() === trackId) {
  478. result = mediaTrack;
  479. return true;
  480. }
  481. return false;
  482. });
  483. });
  484. return result;
  485. }
  486. /**
  487. * Removes <tt>JitsiRemoteTrack</tt> identified by given stream and track
  488. * ids.
  489. *
  490. * @param {string} streamId media stream id as defined by the WebRTC
  491. * @param {string} trackId media track id as defined by the WebRTC
  492. * @returns {JitsiRemoteTrack|undefined} the track which has been removed or
  493. * <tt>undefined</tt> if no track matching given stream and track ids was
  494. * found.
  495. */
  496. _removeRemoteTrack(streamId, trackId) {
  497. const toBeRemoved = this._getRemoteTrackById(streamId, trackId);
  498. if (toBeRemoved) {
  499. toBeRemoved.dispose();
  500. delete this.remoteTracks[
  501. toBeRemoved.getParticipantId()][toBeRemoved.getType()];
  502. this.eventEmitter.emit(
  503. RTCEvents.REMOTE_TRACK_REMOVED, toBeRemoved);
  504. }
  505. return toBeRemoved;
  506. }
  507. /**
  508. *
  509. */
  510. static getPCConstraints() {
  511. return RTCUtils.pcConstraints;
  512. }
  513. /**
  514. *
  515. * @param elSelector
  516. * @param stream
  517. */
  518. static attachMediaStream(elSelector, stream) {
  519. return RTCUtils.attachMediaStream(elSelector, stream);
  520. }
  521. /**
  522. *
  523. * @param stream
  524. */
  525. static getStreamID(stream) {
  526. return RTCUtils.getStreamID(stream);
  527. }
  528. /**
  529. * Returns true if retrieving the the list of input devices is supported
  530. * and false if not.
  531. */
  532. static isDeviceListAvailable() {
  533. return RTCUtils.isDeviceListAvailable();
  534. }
  535. /**
  536. * Returns true if changing the input (camera / microphone) or output
  537. * (audio) device is supported and false if not.
  538. * @params {string} [deviceType] - type of device to change. Default is
  539. * undefined or 'input', 'output' - for audio output device change.
  540. * @returns {boolean} true if available, false otherwise.
  541. */
  542. static isDeviceChangeAvailable(deviceType) {
  543. return RTCUtils.isDeviceChangeAvailable(deviceType);
  544. }
  545. /**
  546. * Returns currently used audio output device id, '' stands for default
  547. * device
  548. * @returns {string}
  549. */
  550. static getAudioOutputDevice() {
  551. return RTCUtils.getAudioOutputDevice();
  552. }
  553. /**
  554. * Returns list of available media devices if its obtained, otherwise an
  555. * empty array is returned/
  556. * @returns {Array} list of available media devices.
  557. */
  558. static getCurrentlyAvailableMediaDevices() {
  559. return RTCUtils.getCurrentlyAvailableMediaDevices();
  560. }
  561. /**
  562. * Returns event data for device to be reported to stats.
  563. * @returns {MediaDeviceInfo} device.
  564. */
  565. static getEventDataForActiveDevice(device) {
  566. return RTCUtils.getEventDataForActiveDevice(device);
  567. }
  568. /**
  569. * Sets current audio output device.
  570. * @param {string} deviceId - id of 'audiooutput' device from
  571. * navigator.mediaDevices.enumerateDevices()
  572. * @returns {Promise} - resolves when audio output is changed, is rejected
  573. * otherwise
  574. */
  575. static setAudioOutputDevice(deviceId) {
  576. return RTCUtils.setAudioOutputDevice(deviceId);
  577. }
  578. /**
  579. * Returns <tt>true<tt/> if given WebRTC MediaStream is considered a valid
  580. * "user" stream which means that it's not a "receive only" stream nor a
  581. * "mixed" JVB stream.
  582. *
  583. * Clients that implement Unified Plan, such as Firefox use recvonly
  584. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed
  585. * to Plan B where there are only 3 channels: audio, video and data.
  586. *
  587. * @param {MediaStream} stream the WebRTC MediaStream instance
  588. * @returns {boolean}
  589. */
  590. static isUserStream(stream) {
  591. return RTC.isUserStreamById(RTCUtils.getStreamID(stream));
  592. }
  593. /**
  594. * Returns <tt>true<tt/> if a WebRTC MediaStream identified by given stream
  595. * ID is considered a valid "user" stream which means that it's not a
  596. * "receive only" stream nor a "mixed" JVB stream.
  597. *
  598. * Clients that implement Unified Plan, such as Firefox use recvonly
  599. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed
  600. * to Plan B where there are only 3 channels: audio, video and data.
  601. *
  602. * @param {string} streamId the id of WebRTC MediaStream
  603. * @returns {boolean}
  604. */
  605. static isUserStreamById(streamId) {
  606. return streamId && streamId !== 'mixedmslabel'
  607. && streamId !== 'default';
  608. }
  609. /**
  610. * Allows to receive list of available cameras/microphones.
  611. * @param {function} callback would receive array of devices as an argument
  612. */
  613. static enumerateDevices(callback) {
  614. RTCUtils.enumerateDevices(callback);
  615. }
  616. /**
  617. * A method to handle stopping of the stream.
  618. * One point to handle the differences in various implementations.
  619. * @param mediaStream MediaStream object to stop.
  620. */
  621. static stopMediaStream(mediaStream) {
  622. RTCUtils.stopMediaStream(mediaStream);
  623. }
  624. /**
  625. * Returns whether the desktop sharing is enabled or not.
  626. * @returns {boolean}
  627. */
  628. static isDesktopSharingEnabled() {
  629. return RTCUtils.isDesktopSharingEnabled();
  630. }
  631. /**
  632. * Closes all currently opened data channels.
  633. */
  634. closeAllDataChannels() {
  635. if (this.dataChannels) {
  636. this.dataChannels.closeAllChannels();
  637. this.dataChannelsOpen = false;
  638. }
  639. }
  640. /**
  641. *
  642. * @param resource
  643. * @param audioLevel
  644. */
  645. setAudioLevel(resource, audioLevel) {
  646. if (!resource) {
  647. return;
  648. }
  649. const audioTrack = this.getRemoteAudioTrack(resource);
  650. if (audioTrack) {
  651. audioTrack.setAudioLevel(audioLevel);
  652. }
  653. }
  654. /**
  655. * Searches in localTracks(session stores ssrc for audio and video) and
  656. * remoteTracks for the ssrc and returns the corresponding resource.
  657. * @param ssrc the ssrc to check.
  658. */
  659. getResourceBySSRC(ssrc) {
  660. // FIXME: Convert the SSRCs in whole project to use the same type.
  661. // Now we are using number and string.
  662. if (this.getLocalTracks().find(
  663. // eslint-disable-next-line eqeqeq
  664. localTrack => localTrack.getSSRC() == ssrc)) {
  665. return this.conference.myUserId();
  666. }
  667. const track = this.getRemoteTrackBySSRC(ssrc);
  668. return track ? track.getParticipantId() : null;
  669. }
  670. /**
  671. * Searches in remoteTracks for the ssrc and returns the corresponding
  672. * track.
  673. * @param ssrc the ssrc to check.
  674. * @return {JitsiRemoteTrack|undefined} return the first remote track that
  675. * matches given SSRC or <tt>undefined</tt> if no such track was found.
  676. */
  677. getRemoteTrackBySSRC(ssrc) {
  678. // FIXME: Convert the SSRCs in whole project to use the same type.
  679. // Now we are using number and string.
  680. // eslint-disable-next-line eqeqeq
  681. return this.getRemoteTracks().find(t => ssrc == t.getSSRC());
  682. }
  683. /**
  684. * Handles remote track mute / unmute events.
  685. * @param type {string} "audio" or "video"
  686. * @param isMuted {boolean} the new mute state
  687. * @param from {string} user id
  688. */
  689. handleRemoteTrackMute(type, isMuted, from) {
  690. const track = this.getRemoteTrackByType(type, from);
  691. if (track) {
  692. track.setMute(isMuted);
  693. }
  694. }
  695. /**
  696. * Handles remote track video type events
  697. * @param value {string} the new video type
  698. * @param from {string} user id
  699. */
  700. handleRemoteTrackVideoTypeChanged(value, from) {
  701. const videoTrack = this.getRemoteVideoTrack(from);
  702. if (videoTrack) {
  703. videoTrack._setVideoType(value);
  704. }
  705. }
  706. /**
  707. * Sends message via the datachannels.
  708. * @param to {string} the id of the endpoint that should receive the
  709. * message. If "" the message will be sent to all participants.
  710. * @param payload {object} the payload of the message.
  711. * @throws NetworkError or InvalidStateError or Error if the operation
  712. * fails or there is no data channel created
  713. */
  714. sendDataChannelMessage(to, payload) {
  715. if (this.dataChannels) {
  716. this.dataChannels.sendDataChannelMessage(to, payload);
  717. } else {
  718. throw new Error('Data channels support is disabled!');
  719. }
  720. }
  721. /**
  722. * Selects a new value for "lastN". The requested amount of videos are going
  723. * to be delivered after the value is in effect. Set to -1 for unlimited or
  724. * all available videos.
  725. * @param value {int} the new value for lastN.
  726. * @trows Error if there is no data channel created.
  727. */
  728. setLastN(value) {
  729. if (this.dataChannels) {
  730. this.dataChannels.sendSetLastNMessage(value);
  731. } else {
  732. throw new Error('Data channels support is disabled!');
  733. }
  734. }
  735. /**
  736. * Indicates if the endpoint id is currently included in the last N.
  737. *
  738. * @param {string} id the endpoint id that we check for last N
  739. * @returns {boolean} true if the endpoint id is in the last N or if we
  740. * don't have data channel support, otherwise we return false
  741. */
  742. isInLastN(id) {
  743. return !this._lastNEndpoints // lastNEndpoints not initialised yet
  744. || this._lastNEndpoints.indexOf(id) > -1;
  745. }
  746. }