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 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import BridgeChannel from './BridgeChannel';
  4. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  5. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  6. import JitsiLocalTrack from './JitsiLocalTrack';
  7. import Listenable from '../util/Listenable';
  8. import { safeCounterIncrement } from '../util/MathUtil';
  9. import * as MediaType from '../../service/RTC/MediaType';
  10. import browser from '../browser';
  11. import RTCEvents from '../../service/RTC/RTCEvents';
  12. import RTCUtils from './RTCUtils';
  13. import Statistics from '../statistics/statistics';
  14. import TraceablePeerConnection from './TraceablePeerConnection';
  15. import VideoType from '../../service/RTC/VideoType';
  16. const logger = getLogger(__filename);
  17. /**
  18. * The counter used to generated id numbers assigned to peer connections
  19. * @type {number}
  20. */
  21. let peerConnectionIdCounter = 0;
  22. /**
  23. * The counter used to generate id number for the local
  24. * <code>MediaStreamTrack</code>s.
  25. * @type {number}
  26. */
  27. let rtcTrackIdCounter = 0;
  28. /**
  29. *
  30. * @param tracksInfo
  31. * @param options
  32. */
  33. function createLocalTracks(tracksInfo, options) {
  34. const newTracks = [];
  35. let deviceId = null;
  36. tracksInfo.forEach(trackInfo => {
  37. if (trackInfo.mediaType === MediaType.AUDIO) {
  38. deviceId = options.micDeviceId;
  39. } else if (trackInfo.videoType === VideoType.CAMERA) {
  40. deviceId = options.cameraDeviceId;
  41. }
  42. rtcTrackIdCounter = safeCounterIncrement(rtcTrackIdCounter);
  43. const localTrack = new JitsiLocalTrack({
  44. ...trackInfo,
  45. deviceId,
  46. facingMode: options.facingMode,
  47. rtcId: rtcTrackIdCounter,
  48. effects: options.effects
  49. });
  50. newTracks.push(localTrack);
  51. });
  52. return newTracks;
  53. }
  54. /**
  55. * Creates {@code JitsiLocalTrack} instances from the passed in meta information
  56. * about MedieaTracks.
  57. *
  58. * @param {Object[]} mediaStreamMetaData - An array of meta information with
  59. * MediaTrack instances. Each can look like:
  60. * {{
  61. * stream: MediaStream instance that holds a track with audio or video,
  62. * track: MediaTrack within the MediaStream,
  63. * videoType: "camera" or "desktop" or falsy,
  64. * sourceId: ID of the desktopsharing source,
  65. * sourceType: The desktopsharing source type,
  66. * effects: Array of effect types
  67. * }}
  68. */
  69. function _newCreateLocalTracks(mediaStreamMetaData = []) {
  70. return mediaStreamMetaData.map(metaData => {
  71. const {
  72. sourceId,
  73. sourceType,
  74. stream,
  75. track,
  76. videoType,
  77. effects
  78. } = metaData;
  79. const { deviceId, facingMode } = track.getSettings();
  80. // FIXME Move rtcTrackIdCounter to a static method in JitsiLocalTrack
  81. // so RTC does not need to handle ID management. This move would be
  82. // safer to do once the old createLocalTracks is removed.
  83. rtcTrackIdCounter = safeCounterIncrement(rtcTrackIdCounter);
  84. return new JitsiLocalTrack({
  85. deviceId,
  86. facingMode,
  87. mediaType: track.kind,
  88. rtcId: rtcTrackIdCounter,
  89. sourceId,
  90. sourceType,
  91. stream,
  92. track,
  93. videoType: videoType || null,
  94. effects
  95. });
  96. });
  97. }
  98. /**
  99. *
  100. */
  101. export default class RTC extends Listenable {
  102. /**
  103. *
  104. * @param conference
  105. * @param options
  106. */
  107. constructor(conference, options = {}) {
  108. super();
  109. this.conference = conference;
  110. /**
  111. * A map of active <tt>TraceablePeerConnection</tt>.
  112. * @type {Map.<number, TraceablePeerConnection>}
  113. */
  114. this.peerConnections = new Map();
  115. this.localTracks = [];
  116. this.options = options;
  117. // BridgeChannel instance.
  118. // @private
  119. // @type {BridgeChannel}
  120. this._channel = null;
  121. // A flag whether we had received that the channel had opened we can
  122. // get this flag out of sync if for some reason channel got closed
  123. // from server, a desired behaviour so we can see errors when this
  124. // happen.
  125. // @private
  126. // @type {boolean}
  127. this._channelOpen = false;
  128. /**
  129. * The value specified to the last invocation of setLastN before the
  130. * channel completed opening. If non-null, the value will be sent
  131. * through a channel (once) as soon as it opens and will then be
  132. * discarded.
  133. * @private
  134. * @type {number}
  135. */
  136. this._lastN = -1;
  137. /**
  138. * Defines the last N endpoints list. It can be null or an array once
  139. * initialised with a channel last N event.
  140. * @type {Array<string>|null}
  141. * @private
  142. */
  143. this._lastNEndpoints = null;
  144. /*
  145. * Holds the sender video constraints signaled from the bridge.
  146. */
  147. this._senderVideoConstraints = {};
  148. /**
  149. * The number representing the maximum video height the local client
  150. * should receive from the bridge.
  151. *
  152. * @type {number|undefined}
  153. * @private
  154. */
  155. this._maxFrameHeight = undefined;
  156. /**
  157. * The endpoint ID of currently pinned participant or <tt>null</tt> if
  158. * no user is pinned.
  159. * @type {string|null}
  160. * @private
  161. */
  162. this._pinnedEndpoint = null;
  163. /**
  164. * The endpoint IDs of currently selected participants.
  165. *
  166. * @type {Array}
  167. * @private
  168. */
  169. this._selectedEndpoints = [];
  170. // The last N change listener.
  171. this._lastNChangeListener = this._onLastNChanged.bind(this);
  172. this._onDeviceListChanged = this._onDeviceListChanged.bind(this);
  173. this._updateAudioOutputForAudioTracks
  174. = this._updateAudioOutputForAudioTracks.bind(this);
  175. // Switch audio output device on all remote audio tracks. Local audio
  176. // tracks handle this event by themselves.
  177. if (RTCUtils.isDeviceChangeAvailable('output')) {
  178. RTCUtils.addListener(
  179. RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  180. this._updateAudioOutputForAudioTracks
  181. );
  182. RTCUtils.addListener(
  183. RTCEvents.DEVICE_LIST_CHANGED,
  184. this._onDeviceListChanged
  185. );
  186. }
  187. }
  188. /**
  189. * Removes any listeners and stored state from this {@code RTC} instance.
  190. *
  191. * @returns {void}
  192. */
  193. destroy() {
  194. RTCUtils.removeListener(
  195. RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  196. this._updateAudioOutputForAudioTracks
  197. );
  198. RTCUtils.removeListener(
  199. RTCEvents.DEVICE_LIST_CHANGED,
  200. this._onDeviceListChanged
  201. );
  202. this.removeListener(
  203. RTCEvents.LASTN_ENDPOINT_CHANGED,
  204. this._lastNChangeListener
  205. );
  206. if (this._channelOpenListener) {
  207. this.removeListener(
  208. RTCEvents.DATA_CHANNEL_OPEN,
  209. this._channelOpenListener
  210. );
  211. }
  212. }
  213. /**
  214. * Exposes the private helper for converting a WebRTC MediaStream to a
  215. * JitsiLocalTrack.
  216. *
  217. * @param {Array<Object>} tracksInfo
  218. * @returns {Array<JitsiLocalTrack>}
  219. */
  220. static newCreateLocalTracks(tracksInfo) {
  221. return _newCreateLocalTracks(tracksInfo);
  222. }
  223. /**
  224. * Creates the local MediaStreams.
  225. * @param {object} [options] Optional parameters.
  226. * @param {array} options.devices The devices that will be requested.
  227. * @param {string} options.resolution Resolution constraints.
  228. * @param {string} options.cameraDeviceId
  229. * @param {string} options.micDeviceId
  230. * @returns {*} Promise object that will receive the new JitsiTracks
  231. */
  232. static obtainAudioAndVideoPermissions(options) {
  233. const usesNewGumFlow = browser.usesNewGumFlow();
  234. const obtainMediaPromise = usesNewGumFlow
  235. ? RTCUtils.newObtainAudioAndVideoPermissions(options)
  236. : RTCUtils.obtainAudioAndVideoPermissions(options);
  237. return obtainMediaPromise.then(tracksInfo => {
  238. if (usesNewGumFlow) {
  239. return _newCreateLocalTracks(tracksInfo);
  240. }
  241. return createLocalTracks(tracksInfo, options);
  242. });
  243. }
  244. /**
  245. * Initializes the bridge channel of this instance.
  246. * At least one of both, peerconnection or wsUrl parameters, must be
  247. * given.
  248. * @param {RTCPeerConnection} [peerconnection] WebRTC peer connection
  249. * instance.
  250. * @param {string} [wsUrl] WebSocket URL.
  251. */
  252. initializeBridgeChannel(peerconnection, wsUrl) {
  253. this._channel = new BridgeChannel(
  254. peerconnection, wsUrl, this.eventEmitter, this._senderVideoConstraintsChanged.bind(this));
  255. this._channelOpenListener = () => {
  256. // Mark that channel as opened.
  257. this._channelOpen = true;
  258. // When the channel becomes available, tell the bridge about
  259. // video selections so that it can do adaptive simulcast,
  260. // we want the notification to trigger even if userJid
  261. // is undefined, or null.
  262. try {
  263. this._channel.sendPinnedEndpointMessage(
  264. this._pinnedEndpoint);
  265. this._channel.sendSelectedEndpointsMessage(
  266. this._selectedEndpoints);
  267. if (typeof this._maxFrameHeight !== 'undefined') {
  268. this._channel.sendReceiverVideoConstraintMessage(
  269. this._maxFrameHeight);
  270. }
  271. } catch (error) {
  272. GlobalOnErrorHandler.callErrorHandler(error);
  273. logger.error(
  274. `Cannot send selected(${this._selectedEndpoint})`
  275. + `pinned(${this._pinnedEndpoint})`
  276. + `frameHeight(${this._maxFrameHeight}) endpoint message`,
  277. error);
  278. }
  279. this.removeListener(RTCEvents.DATA_CHANNEL_OPEN,
  280. this._channelOpenListener);
  281. this._channelOpenListener = null;
  282. // If setLastN was invoked before the bridge channel completed
  283. // opening, apply the specified value now that the channel
  284. // is open. NOTE that -1 is the default value assumed by both
  285. // RTC module and the JVB.
  286. if (this._lastN !== -1) {
  287. this._channel.sendSetLastNMessage(this._lastN);
  288. }
  289. };
  290. this.addListener(RTCEvents.DATA_CHANNEL_OPEN,
  291. this._channelOpenListener);
  292. // Add Last N change listener.
  293. this.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  294. this._lastNChangeListener);
  295. }
  296. /**
  297. * Callback invoked when the list of known audio and video devices has
  298. * been updated. Attempts to update the known available audio output
  299. * devices.
  300. *
  301. * @private
  302. * @returns {void}
  303. */
  304. _onDeviceListChanged() {
  305. this._updateAudioOutputForAudioTracks(RTCUtils.getAudioOutputDevice());
  306. }
  307. /**
  308. * Notifies this instance that the sender video constraints signaled from the bridge have changed.
  309. *
  310. * @param {Object} senderVideoConstraints the sender video constraints from the bridge.
  311. * @private
  312. */
  313. _senderVideoConstraintsChanged(senderVideoConstraints) {
  314. this._senderVideoConstraints = senderVideoConstraints;
  315. this.eventEmitter.emit(RTCEvents.SENDER_VIDEO_CONSTRAINTS_CHANGED);
  316. }
  317. /**
  318. * Receives events when Last N had changed.
  319. * @param {array} lastNEndpoints The new Last N endpoints.
  320. * @private
  321. */
  322. _onLastNChanged(lastNEndpoints = []) {
  323. const oldLastNEndpoints = this._lastNEndpoints || [];
  324. let leavingLastNEndpoints = [];
  325. let enteringLastNEndpoints = [];
  326. this._lastNEndpoints = lastNEndpoints;
  327. leavingLastNEndpoints = oldLastNEndpoints.filter(
  328. id => !this.isInLastN(id));
  329. enteringLastNEndpoints = lastNEndpoints.filter(
  330. id => oldLastNEndpoints.indexOf(id) === -1);
  331. this.conference.eventEmitter.emit(
  332. JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  333. leavingLastNEndpoints,
  334. enteringLastNEndpoints);
  335. }
  336. /**
  337. * Should be called when current media session ends and after the
  338. * PeerConnection has been closed using PeerConnection.close() method.
  339. */
  340. onCallEnded() {
  341. if (this._channel) {
  342. // The BridgeChannel is not explicitly closed as the PeerConnection
  343. // is closed on call ended which triggers datachannel onclose
  344. // events. If using a WebSocket, the channel must be closed since
  345. // it is not managed by the PeerConnection.
  346. // The reference is cleared to disable any logic related to the
  347. // channel.
  348. if (this._channel && this._channel.mode === 'websocket') {
  349. this._channel.close();
  350. }
  351. this._channel = null;
  352. this._channelOpen = false;
  353. }
  354. }
  355. /**
  356. * Sets the maximum video size the local participant should receive from
  357. * remote participants. Will cache the value and send it through the channel
  358. * once it is created.
  359. *
  360. * @param {number} maxFrameHeightPixels the maximum frame height, in pixels,
  361. * this receiver is willing to receive.
  362. * @returns {void}
  363. */
  364. setReceiverVideoConstraint(maxFrameHeight) {
  365. this._maxFrameHeight = maxFrameHeight;
  366. if (this._channel && this._channelOpen) {
  367. this._channel.sendReceiverVideoConstraintMessage(maxFrameHeight);
  368. }
  369. }
  370. /**
  371. * Elects the participants with the given ids to be the selected
  372. * participants in order to always receive video for this participant (even
  373. * when last n is enabled). If there is no channel we store it and send it
  374. * through the channel once it is created.
  375. *
  376. * @param {Array<string>} ids - The user ids.
  377. * @throws NetworkError or InvalidStateError or Error if the operation
  378. * fails.
  379. * @returns {void}
  380. */
  381. selectEndpoints(ids) {
  382. this._selectedEndpoints = ids;
  383. if (this._channel && this._channelOpen) {
  384. this._channel.sendSelectedEndpointsMessage(ids);
  385. }
  386. }
  387. /**
  388. * Elects the participant with the given id to be the pinned participant in
  389. * order to always receive video for this participant (even when last n is
  390. * enabled).
  391. * @param {stirng} id The user id.
  392. * @throws NetworkError or InvalidStateError or Error if the operation
  393. * fails.
  394. */
  395. pinEndpoint(id) {
  396. // Cache the value if channel is missing, till we open it.
  397. this._pinnedEndpoint = id;
  398. if (this._channel && this._channelOpen) {
  399. this._channel.sendPinnedEndpointMessage(id);
  400. }
  401. }
  402. /**
  403. *
  404. * @param eventType
  405. * @param listener
  406. */
  407. static addListener(eventType, listener) {
  408. RTCUtils.addListener(eventType, listener);
  409. }
  410. /**
  411. *
  412. * @param eventType
  413. * @param listener
  414. */
  415. static removeListener(eventType, listener) {
  416. RTCUtils.removeListener(eventType, listener);
  417. }
  418. /**
  419. *
  420. * @param options
  421. */
  422. static init(options = {}) {
  423. this.options = options;
  424. return RTCUtils.init(this.options);
  425. }
  426. /* eslint-disable max-params */
  427. /**
  428. * Creates new <tt>TraceablePeerConnection</tt>
  429. * @param {SignalingLayer} signaling The signaling layer that will
  430. * provide information about the media or participants which is not
  431. * carried over SDP.
  432. * @param {object} iceConfig An object describing the ICE config like
  433. * defined in the WebRTC specification.
  434. * @param {boolean} isP2P Indicates whether or not the new TPC will be used
  435. * in a peer to peer type of session.
  436. * @param {object} options The config options.
  437. * @param {boolean} options.disableSimulcast If set to 'true' will disable
  438. * the simulcast.
  439. * @param {boolean} options.disableRtx If set to 'true' will disable the
  440. * RTX.
  441. * @param {boolean} options.disableH264 If set to 'true' H264 will be
  442. * disabled by removing it from the SDP.
  443. * @param {boolean} options.preferH264 If set to 'true' H264 will be
  444. * preferred over other video codecs.
  445. * @param {boolean} options.startSilent If set to 'true' no audio will be sent or received.
  446. * @return {TraceablePeerConnection}
  447. */
  448. createPeerConnection(signaling, iceConfig, isP2P, options) {
  449. const pcConstraints = RTC.getPCConstraints(isP2P);
  450. if (typeof options.abtestSuspendVideo !== 'undefined') {
  451. RTCUtils.setSuspendVideo(pcConstraints, options.abtestSuspendVideo);
  452. Statistics.analytics.addPermanentProperties(
  453. { abtestSuspendVideo: options.abtestSuspendVideo });
  454. }
  455. // FIXME: We should rename iceConfig to pcConfig.
  456. if (browser.supportsInsertableStreams()) {
  457. logger.debug('E2EE - setting insertable streams constraints');
  458. iceConfig.encodedInsertableStreams = true;
  459. iceConfig.forceEncodedAudioInsertableStreams = true; // legacy, to be removed in M85.
  460. iceConfig.forceEncodedVideoInsertableStreams = true; // legacy, to be removed in M85.
  461. }
  462. if (browser.supportsSdpSemantics()) {
  463. iceConfig.sdpSemantics = 'plan-b';
  464. }
  465. // Set the RTCBundlePolicy to max-bundle so that only one set of ice candidates is generated.
  466. // The default policy generates separate ice candidates for audio and video connections.
  467. // This change is necessary for Unified plan to work properly on Chrome and Safari.
  468. iceConfig.bundlePolicy = 'max-bundle';
  469. peerConnectionIdCounter = safeCounterIncrement(peerConnectionIdCounter);
  470. const newConnection
  471. = new TraceablePeerConnection(
  472. this,
  473. peerConnectionIdCounter,
  474. signaling,
  475. iceConfig, pcConstraints,
  476. isP2P, options);
  477. this.peerConnections.set(newConnection.id, newConnection);
  478. return newConnection;
  479. }
  480. /* eslint-enable max-params */
  481. /**
  482. * Removed given peer connection from this RTC module instance.
  483. * @param {TraceablePeerConnection} traceablePeerConnection
  484. * @return {boolean} <tt>true</tt> if the given peer connection was removed
  485. * successfully or <tt>false</tt> if there was no peer connection mapped in
  486. * this RTC instance.
  487. */
  488. _removePeerConnection(traceablePeerConnection) {
  489. const id = traceablePeerConnection.id;
  490. if (this.peerConnections.has(id)) {
  491. // NOTE Remote tracks are not removed here.
  492. this.peerConnections.delete(id);
  493. return true;
  494. }
  495. return false;
  496. }
  497. /**
  498. *
  499. * @param track
  500. */
  501. addLocalTrack(track) {
  502. if (!track) {
  503. throw new Error('track must not be null nor undefined');
  504. }
  505. this.localTracks.push(track);
  506. track.conference = this.conference;
  507. }
  508. /**
  509. * Returns the current value for "lastN" - the amount of videos are going
  510. * to be delivered. When set to -1 for unlimited or all available videos.
  511. * @return {number}
  512. */
  513. getLastN() {
  514. return this._lastN;
  515. }
  516. /**
  517. * @return {Object} The sender video constraints signaled from the brridge.
  518. */
  519. getSenderVideoConstraints() {
  520. return this._senderVideoConstraints;
  521. }
  522. /**
  523. * Get local video track.
  524. * @returns {JitsiLocalTrack|undefined}
  525. */
  526. getLocalVideoTrack() {
  527. const localVideo = this.getLocalTracks(MediaType.VIDEO);
  528. return localVideo.length ? localVideo[0] : undefined;
  529. }
  530. /**
  531. * Get local audio track.
  532. * @returns {JitsiLocalTrack|undefined}
  533. */
  534. getLocalAudioTrack() {
  535. const localAudio = this.getLocalTracks(MediaType.AUDIO);
  536. return localAudio.length ? localAudio[0] : undefined;
  537. }
  538. /**
  539. * Returns the local tracks of the given media type, or all local tracks if
  540. * no specific type is given.
  541. * @param {MediaType} [mediaType] Optional media type filter.
  542. * (audio or video).
  543. */
  544. getLocalTracks(mediaType) {
  545. let tracks = this.localTracks.slice();
  546. if (mediaType !== undefined) {
  547. tracks = tracks.filter(
  548. track => track.getType() === mediaType);
  549. }
  550. return tracks;
  551. }
  552. /**
  553. * Obtains all remote tracks currently known to this RTC module instance.
  554. * @param {MediaType} [mediaType] The remote tracks will be filtered
  555. * by their media type if this argument is specified.
  556. * @return {Array<JitsiRemoteTrack>}
  557. */
  558. getRemoteTracks(mediaType) {
  559. let remoteTracks = [];
  560. for (const tpc of this.peerConnections.values()) {
  561. const pcRemoteTracks = tpc.getRemoteTracks(undefined, mediaType);
  562. if (pcRemoteTracks) {
  563. remoteTracks = remoteTracks.concat(pcRemoteTracks);
  564. }
  565. }
  566. return remoteTracks;
  567. }
  568. /**
  569. * Set mute for all local audio streams attached to the conference.
  570. * @param value The mute value.
  571. * @returns {Promise}
  572. */
  573. setAudioMute(value) {
  574. const mutePromises = [];
  575. this.getLocalTracks(MediaType.AUDIO).forEach(audioTrack => {
  576. // this is a Promise
  577. mutePromises.push(value ? audioTrack.mute() : audioTrack.unmute());
  578. });
  579. // We return a Promise from all Promises so we can wait for their
  580. // execution.
  581. return Promise.all(mutePromises);
  582. }
  583. /**
  584. *
  585. * @param track
  586. */
  587. removeLocalTrack(track) {
  588. const pos = this.localTracks.indexOf(track);
  589. if (pos === -1) {
  590. return;
  591. }
  592. this.localTracks.splice(pos, 1);
  593. }
  594. /**
  595. * Removes all JitsiRemoteTracks associated with given MUC nickname
  596. * (resource part of the JID). Returns array of removed tracks.
  597. *
  598. * @param {string} Owner The resource part of the MUC JID.
  599. * @returns {JitsiRemoteTrack[]}
  600. */
  601. removeRemoteTracks(owner) {
  602. let removedTracks = [];
  603. for (const tpc of this.peerConnections.values()) {
  604. const pcRemovedTracks = tpc.removeRemoteTracks(owner);
  605. removedTracks = removedTracks.concat(pcRemovedTracks);
  606. }
  607. logger.debug(
  608. `Removed remote tracks for ${owner}`
  609. + ` count: ${removedTracks.length}`);
  610. return removedTracks;
  611. }
  612. /**
  613. *
  614. */
  615. static getPCConstraints(isP2P) {
  616. const pcConstraints
  617. = isP2P ? RTCUtils.p2pPcConstraints : RTCUtils.pcConstraints;
  618. if (!pcConstraints) {
  619. return {};
  620. }
  621. return JSON.parse(JSON.stringify(pcConstraints));
  622. }
  623. /**
  624. *
  625. * @param elSelector
  626. * @param stream
  627. */
  628. static attachMediaStream(elSelector, stream) {
  629. return RTCUtils.attachMediaStream(elSelector, stream);
  630. }
  631. /**
  632. * Returns the id of the given stream.
  633. * @param {MediaStream} stream
  634. */
  635. static getStreamID(stream) {
  636. return RTCUtils.getStreamID(stream);
  637. }
  638. /**
  639. * Returns the id of the given track.
  640. * @param {MediaStreamTrack} track
  641. */
  642. static getTrackID(track) {
  643. return RTCUtils.getTrackID(track);
  644. }
  645. /**
  646. * Returns true if retrieving the the list of input devices is supported
  647. * and false if not.
  648. */
  649. static isDeviceListAvailable() {
  650. return RTCUtils.isDeviceListAvailable();
  651. }
  652. /**
  653. * Returns true if changing the input (camera / microphone) or output
  654. * (audio) device is supported and false if not.
  655. * @param {string} [deviceType] Type of device to change. Default is
  656. * undefined or 'input', 'output' - for audio output device change.
  657. * @returns {boolean} true if available, false otherwise.
  658. */
  659. static isDeviceChangeAvailable(deviceType) {
  660. return RTCUtils.isDeviceChangeAvailable(deviceType);
  661. }
  662. /**
  663. * Returns whether the current execution environment supports WebRTC (for
  664. * use within this library).
  665. *
  666. * @returns {boolean} {@code true} if WebRTC is supported in the current
  667. * execution environment (for use within this library); {@code false},
  668. * otherwise.
  669. */
  670. static isWebRtcSupported() {
  671. return browser.isSupported();
  672. }
  673. /**
  674. * Returns currently used audio output device id, '' stands for default
  675. * device
  676. * @returns {string}
  677. */
  678. static getAudioOutputDevice() {
  679. return RTCUtils.getAudioOutputDevice();
  680. }
  681. /**
  682. * Returns list of available media devices if its obtained, otherwise an
  683. * empty array is returned/
  684. * @returns {array} list of available media devices.
  685. */
  686. static getCurrentlyAvailableMediaDevices() {
  687. return RTCUtils.getCurrentlyAvailableMediaDevices();
  688. }
  689. /**
  690. * Returns event data for device to be reported to stats.
  691. * @returns {MediaDeviceInfo} device.
  692. */
  693. static getEventDataForActiveDevice(device) {
  694. return RTCUtils.getEventDataForActiveDevice(device);
  695. }
  696. /**
  697. * Sets current audio output device.
  698. * @param {string} deviceId Id of 'audiooutput' device from
  699. * navigator.mediaDevices.enumerateDevices().
  700. * @returns {Promise} resolves when audio output is changed, is rejected
  701. * otherwise
  702. */
  703. static setAudioOutputDevice(deviceId) {
  704. return RTCUtils.setAudioOutputDevice(deviceId);
  705. }
  706. /**
  707. * Returns <tt>true<tt/> if given WebRTC MediaStream is considered a valid
  708. * "user" stream which means that it's not a "receive only" stream nor a
  709. * "mixed" JVB stream.
  710. *
  711. * Clients that implement Unified Plan, such as Firefox use recvonly
  712. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed
  713. * to Plan B where there are only 3 channels: audio, video and data.
  714. *
  715. * @param {MediaStream} stream The WebRTC MediaStream instance.
  716. * @returns {boolean}
  717. */
  718. static isUserStream(stream) {
  719. return RTC.isUserStreamById(RTCUtils.getStreamID(stream));
  720. }
  721. /**
  722. * Returns <tt>true<tt/> if a WebRTC MediaStream identified by given stream
  723. * ID is considered a valid "user" stream which means that it's not a
  724. * "receive only" stream nor a "mixed" JVB stream.
  725. *
  726. * Clients that implement Unified Plan, such as Firefox use recvonly
  727. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed
  728. * to Plan B where there are only 3 channels: audio, video and data.
  729. *
  730. * @param {string} streamId The id of WebRTC MediaStream.
  731. * @returns {boolean}
  732. */
  733. static isUserStreamById(streamId) {
  734. return streamId && streamId !== 'mixedmslabel'
  735. && streamId !== 'default';
  736. }
  737. /**
  738. * Allows to receive list of available cameras/microphones.
  739. * @param {function} callback Would receive array of devices as an
  740. * argument.
  741. */
  742. static enumerateDevices(callback) {
  743. RTCUtils.enumerateDevices(callback);
  744. }
  745. /**
  746. * A method to handle stopping of the stream.
  747. * One point to handle the differences in various implementations.
  748. * @param {MediaStream} mediaStream MediaStream object to stop.
  749. */
  750. static stopMediaStream(mediaStream) {
  751. RTCUtils.stopMediaStream(mediaStream);
  752. }
  753. /**
  754. * Returns whether the desktop sharing is enabled or not.
  755. * @returns {boolean}
  756. */
  757. static isDesktopSharingEnabled() {
  758. return RTCUtils.isDesktopSharingEnabled();
  759. }
  760. /**
  761. * Closes the currently opened bridge channel.
  762. */
  763. closeBridgeChannel() {
  764. if (this._channel) {
  765. this._channel.close();
  766. this._channelOpen = false;
  767. this.removeListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  768. this._lastNChangeListener);
  769. }
  770. }
  771. /* eslint-disable max-params */
  772. /**
  773. *
  774. * @param {TraceablePeerConnection} tpc
  775. * @param {number} ssrc
  776. * @param {number} audioLevel
  777. * @param {boolean} isLocal
  778. */
  779. setAudioLevel(tpc, ssrc, audioLevel, isLocal) {
  780. const track = tpc.getTrackBySSRC(ssrc);
  781. if (!track) {
  782. return;
  783. } else if (!track.isAudioTrack()) {
  784. logger.warn(`Received audio level for non-audio track: ${ssrc}`);
  785. return;
  786. } else if (track.isLocal() !== isLocal) {
  787. logger.error(
  788. `${track} was expected to ${isLocal ? 'be' : 'not be'} local`);
  789. }
  790. track.setAudioLevel(audioLevel, tpc);
  791. }
  792. /* eslint-enable max-params */
  793. /**
  794. * Sends message via the bridge channel.
  795. * @param {string} to The id of the endpoint that should receive the
  796. * message. If "" the message will be sent to all participants.
  797. * @param {object} payload The payload of the message.
  798. * @throws NetworkError or InvalidStateError or Error if the operation
  799. * fails or there is no data channel created.
  800. */
  801. sendChannelMessage(to, payload) {
  802. if (this._channel) {
  803. this._channel.sendMessage(to, payload);
  804. } else {
  805. throw new Error('Channel support is disabled!');
  806. }
  807. }
  808. /**
  809. * Selects a new value for "lastN". The requested amount of videos are going
  810. * to be delivered after the value is in effect. Set to -1 for unlimited or
  811. * all available videos.
  812. * @param {number} value the new value for lastN.
  813. */
  814. setLastN(value) {
  815. if (this._lastN !== value) {
  816. this._lastN = value;
  817. if (this._channel && this._channelOpen) {
  818. this._channel.sendSetLastNMessage(value);
  819. }
  820. this.eventEmitter.emit(RTCEvents.LASTN_VALUE_CHANGED, value);
  821. }
  822. }
  823. /**
  824. * Indicates if the endpoint id is currently included in the last N.
  825. * @param {string} id The endpoint id that we check for last N.
  826. * @returns {boolean} true if the endpoint id is in the last N or if we
  827. * don't have bridge channel support, otherwise we return false.
  828. */
  829. isInLastN(id) {
  830. return !this._lastNEndpoints // lastNEndpoints not initialised yet.
  831. || this._lastNEndpoints.indexOf(id) > -1;
  832. }
  833. /**
  834. * Updates the target audio output device for all remote audio tracks.
  835. *
  836. * @param {string} deviceId - The device id of the audio ouput device to
  837. * use for all remote tracks.
  838. * @private
  839. * @returns {void}
  840. */
  841. _updateAudioOutputForAudioTracks(deviceId) {
  842. const remoteAudioTracks = this.getRemoteTracks(MediaType.AUDIO);
  843. for (const track of remoteAudioTracks) {
  844. track.setAudioOutput(deviceId);
  845. }
  846. }
  847. }