modified lib-jitsi-meet dev repo
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RTC.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. const mediaTrack
  287. = this.remoteTracks[endpoint][trackMediaType];
  288. if (mediaTrack) {
  289. remoteTracks.push(mediaTrack);
  290. }
  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
  339. // execution.
  340. return Promise.all(mutePromises);
  341. }
  342. removeLocalTrack(track) {
  343. const pos = this.localTracks.indexOf(track);
  344. if (pos === -1) {
  345. return;
  346. }
  347. this.localTracks.splice(pos, 1);
  348. }
  349. /**
  350. * Initializes a new JitsiRemoteTrack instance with the data provided by
  351. * the signaling layer and SDP.
  352. *
  353. * @param {string} ownerEndpointId
  354. * @param {MediaStream} stream
  355. * @param {MediaStreamTrack} track
  356. * @param {MediaType} mediaType
  357. * @param {VideoType|undefined} videoType
  358. * @param {string} ssrc
  359. * @param {boolean} muted
  360. */
  361. _createRemoteTrack(ownerEndpointId,
  362. stream, track, mediaType, videoType, ssrc, muted) {
  363. const remoteTrack
  364. = new JitsiRemoteTrack(
  365. this, this.conference, ownerEndpointId, stream, track,
  366. mediaType, videoType, ssrc, muted);
  367. const remoteTracks
  368. = this.remoteTracks[ownerEndpointId]
  369. || (this.remoteTracks[ownerEndpointId] = {});
  370. if (remoteTracks[mediaType]) {
  371. logger.error(
  372. 'Overwriting remote track!', ownerEndpointId, mediaType);
  373. }
  374. remoteTracks[mediaType] = remoteTrack;
  375. this.eventEmitter.emit(RTCEvents.REMOTE_TRACK_ADDED, remoteTrack);
  376. }
  377. /**
  378. * Removes all JitsiRemoteTracks associated with given MUC nickname
  379. * (resource part of the JID). Returns array of removed tracks.
  380. *
  381. * @param {string} owner - The resource part of the MUC JID.
  382. * @returns {JitsiRemoteTrack[]}
  383. */
  384. removeRemoteTracks(owner) {
  385. const removedTracks = [];
  386. if (this.remoteTracks[owner]) {
  387. const removedAudioTrack
  388. = this.remoteTracks[owner][MediaType.AUDIO];
  389. const removedVideoTrack
  390. = this.remoteTracks[owner][MediaType.VIDEO];
  391. removedAudioTrack && removedTracks.push(removedAudioTrack);
  392. removedVideoTrack && removedTracks.push(removedVideoTrack);
  393. delete this.remoteTracks[owner];
  394. }
  395. return removedTracks;
  396. }
  397. /**
  398. * Finds remote track by it's stream and track ids.
  399. * @param {string} streamId the media stream id as defined by the WebRTC
  400. * @param {string} trackId the media track id as defined by the WebRTC
  401. * @return {JitsiRemoteTrack|undefined}
  402. * @private
  403. */
  404. _getRemoteTrackById(streamId, trackId) {
  405. let result = undefined;
  406. // .find will break the loop once the first match is found
  407. Object.keys(this.remoteTracks).find(endpoint => {
  408. const endpointTracks = this.remoteTracks[endpoint];
  409. return endpointTracks && Object.keys(endpointTracks).find(
  410. mediaType => {
  411. const mediaTrack = endpointTracks[mediaType];
  412. if (mediaTrack
  413. && mediaTrack.getStreamId() == streamId
  414. && mediaTrack.getTrackId() == trackId) {
  415. result = mediaTrack;
  416. return true;
  417. }
  418. return false;
  419. });
  420. });
  421. return result;
  422. }
  423. /**
  424. * Removes <tt>JitsiRemoteTrack</tt> identified by given stream and track
  425. * ids.
  426. *
  427. * @param {string} streamId media stream id as defined by the WebRTC
  428. * @param {string} trackId media track id as defined by the WebRTC
  429. * @returns {JitsiRemoteTrack|undefined} the track which has been removed or
  430. * <tt>undefined</tt> if no track matching given stream and track ids was
  431. * found.
  432. */
  433. _removeRemoteTrack(streamId, trackId) {
  434. const toBeRemoved = this._getRemoteTrackById(streamId, trackId);
  435. if (toBeRemoved) {
  436. toBeRemoved.dispose();
  437. delete this.remoteTracks[
  438. toBeRemoved.getParticipantId()][toBeRemoved.getType()];
  439. this.eventEmitter.emit(
  440. RTCEvents.REMOTE_TRACK_REMOVED, toBeRemoved);
  441. }
  442. return toBeRemoved;
  443. }
  444. static getPCConstraints() {
  445. return RTCUtils.pc_constraints;
  446. }
  447. static attachMediaStream(elSelector, stream) {
  448. return RTCUtils.attachMediaStream(elSelector, stream);
  449. }
  450. static getStreamID(stream) {
  451. return RTCUtils.getStreamID(stream);
  452. }
  453. /**
  454. * Returns true if retrieving the the list of input devices is supported
  455. * and false if not.
  456. */
  457. static isDeviceListAvailable() {
  458. return RTCUtils.isDeviceListAvailable();
  459. }
  460. /**
  461. * Returns true if changing the input (camera / microphone) or output
  462. * (audio) device is supported and false if not.
  463. * @params {string} [deviceType] - type of device to change. Default is
  464. * undefined or 'input', 'output' - for audio output device change.
  465. * @returns {boolean} true if available, false otherwise.
  466. */
  467. static isDeviceChangeAvailable(deviceType) {
  468. return RTCUtils.isDeviceChangeAvailable(deviceType);
  469. }
  470. /**
  471. * Returns currently used audio output device id, '' stands for default
  472. * device
  473. * @returns {string}
  474. */
  475. static getAudioOutputDevice() {
  476. return RTCUtils.getAudioOutputDevice();
  477. }
  478. /**
  479. * Returns list of available media devices if its obtained, otherwise an
  480. * empty array is returned/
  481. * @returns {Array} list of available media devices.
  482. */
  483. static getCurrentlyAvailableMediaDevices() {
  484. return RTCUtils.getCurrentlyAvailableMediaDevices();
  485. }
  486. /**
  487. * Returns event data for device to be reported to stats.
  488. * @returns {MediaDeviceInfo} device.
  489. */
  490. static getEventDataForActiveDevice(device) {
  491. return RTCUtils.getEventDataForActiveDevice(device);
  492. }
  493. /**
  494. * Sets current audio output device.
  495. * @param {string} deviceId - id of 'audiooutput' device from
  496. * navigator.mediaDevices.enumerateDevices()
  497. * @returns {Promise} - resolves when audio output is changed, is rejected
  498. * otherwise
  499. */
  500. static setAudioOutputDevice(deviceId) {
  501. return RTCUtils.setAudioOutputDevice(deviceId);
  502. }
  503. /**
  504. * Returns <tt>true<tt/> if given WebRTC MediaStream is considered a valid
  505. * "user" stream which means that it's not a "receive only" stream nor a
  506. * "mixed" JVB stream.
  507. *
  508. * Clients that implement Unified Plan, such as Firefox use recvonly
  509. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed
  510. * to Plan B where there are only 3 channels: audio, video and data.
  511. *
  512. * @param {MediaStream} stream the WebRTC MediaStream instance
  513. * @returns {boolean}
  514. */
  515. static isUserStream(stream) {
  516. return RTC.isUserStreamById(RTCUtils.getStreamID(stream));
  517. }
  518. /**
  519. * Returns <tt>true<tt/> if a WebRTC MediaStream identified by given stream
  520. * ID is considered a valid "user" stream which means that it's not a
  521. * "receive only" stream nor a "mixed" JVB stream.
  522. *
  523. * Clients that implement Unified Plan, such as Firefox use recvonly
  524. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed
  525. * to Plan B where there are only 3 channels: audio, video and data.
  526. *
  527. * @param {string} streamId the id of WebRTC MediaStream
  528. * @returns {boolean}
  529. */
  530. static isUserStreamById(streamId) {
  531. return streamId && streamId !== 'mixedmslabel'
  532. && streamId !== 'default';
  533. }
  534. /**
  535. * Allows to receive list of available cameras/microphones.
  536. * @param {function} callback would receive array of devices as an argument
  537. */
  538. static enumerateDevices(callback) {
  539. RTCUtils.enumerateDevices(callback);
  540. }
  541. /**
  542. * A method to handle stopping of the stream.
  543. * One point to handle the differences in various implementations.
  544. * @param mediaStream MediaStream object to stop.
  545. */
  546. static stopMediaStream(mediaStream) {
  547. RTCUtils.stopMediaStream(mediaStream);
  548. }
  549. /**
  550. * Returns whether the desktop sharing is enabled or not.
  551. * @returns {boolean}
  552. */
  553. static isDesktopSharingEnabled() {
  554. return RTCUtils.isDesktopSharingEnabled();
  555. }
  556. /**
  557. * Closes all currently opened data channels.
  558. */
  559. closeAllDataChannels() {
  560. if(this.dataChannels) {
  561. this.dataChannels.closeAllChannels();
  562. this.dataChannelsOpen = false;
  563. }
  564. }
  565. setAudioLevel(resource, audioLevel) {
  566. if(!resource) {
  567. return;
  568. }
  569. const audioTrack = this.getRemoteAudioTrack(resource);
  570. if(audioTrack) {
  571. audioTrack.setAudioLevel(audioLevel);
  572. }
  573. }
  574. /**
  575. * Searches in localTracks(session stores ssrc for audio and video) and
  576. * remoteTracks for the ssrc and returns the corresponding resource.
  577. * @param ssrc the ssrc to check.
  578. */
  579. getResourceBySSRC(ssrc) {
  580. if (this.getLocalTracks().find(
  581. localTrack => localTrack.getSSRC() == ssrc)) {
  582. return this.conference.myUserId();
  583. }
  584. const track = this.getRemoteTrackBySSRC(ssrc);
  585. return track ? track.getParticipantId() : null;
  586. }
  587. /**
  588. * Searches in remoteTracks for the ssrc and returns the corresponding
  589. * track.
  590. * @param ssrc the ssrc to check.
  591. * @return {JitsiRemoteTrack|undefined} return the first remote track that
  592. * matches given SSRC or <tt>undefined</tt> if no such track was found.
  593. */
  594. getRemoteTrackBySSRC(ssrc) {
  595. return this.getRemoteTracks().find(remoteTrack => ssrc == remoteTrack.getSSRC());
  596. }
  597. /**
  598. * Handles remote track mute / unmute events.
  599. * @param type {string} "audio" or "video"
  600. * @param isMuted {boolean} the new mute state
  601. * @param from {string} user id
  602. */
  603. handleRemoteTrackMute(type, isMuted, from) {
  604. const track = this.getRemoteTrackByType(type, from);
  605. if (track) {
  606. track.setMute(isMuted);
  607. }
  608. }
  609. /**
  610. * Handles remote track video type events
  611. * @param value {string} the new video type
  612. * @param from {string} user id
  613. */
  614. handleRemoteTrackVideoTypeChanged(value, from) {
  615. const videoTrack = this.getRemoteVideoTrack(from);
  616. if (videoTrack) {
  617. videoTrack._setVideoType(value);
  618. }
  619. }
  620. /**
  621. * Sends message via the datachannels.
  622. * @param to {string} the id of the endpoint that should receive the
  623. * message. If "" the message will be sent to all participants.
  624. * @param payload {object} the payload of the message.
  625. * @throws NetworkError or InvalidStateError or Error if the operation
  626. * fails or there is no data channel created
  627. */
  628. sendDataChannelMessage(to, payload) {
  629. if(this.dataChannels) {
  630. this.dataChannels.sendDataChannelMessage(to, payload);
  631. } else {
  632. throw new Error('Data channels support is disabled!');
  633. }
  634. }
  635. /**
  636. * Selects a new value for "lastN". The requested amount of videos are going
  637. * to be delivered after the value is in effect. Set to -1 for unlimited or
  638. * all available videos.
  639. * @param value {int} the new value for lastN.
  640. * @trows Error if there is no data channel created.
  641. */
  642. setLastN(value) {
  643. if (this.dataChannels) {
  644. this.dataChannels.sendSetLastNMessage(value);
  645. } else {
  646. throw new Error('Data channels support is disabled!');
  647. }
  648. }
  649. }