選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RTC.js 26KB

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