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

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