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

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