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.

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