Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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