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.

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. var newTracks = [];
  18. var deviceId = null;
  19. tracksInfo.forEach(function(trackInfo){
  20. if (trackInfo.mediaType === MediaType.AUDIO) {
  21. deviceId = options.micDeviceId;
  22. } else if (trackInfo.videoType === VideoType.CAMERA){
  23. deviceId = options.cameraDeviceId;
  24. }
  25. var 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. function (tracksInfo) {
  91. var 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. * Elects the participant with the given id to be the pinned participant in
  162. * order to always receive video for this participant (even when last n is
  163. * enabled).
  164. * @param id {string} the user id
  165. * @throws NetworkError or InvalidStateError or Error if the operation fails.
  166. */
  167. pinEndpoint (id) {
  168. if(this.dataChannels) {
  169. this.dataChannels.sendPinnedEndpointMessage(id);
  170. } else {
  171. // FIXME: cache value while there is no data channel created
  172. // and send the cached state once channel is created
  173. throw new Error("Data channels support is disabled!");
  174. }
  175. }
  176. static addListener (eventType, listener) {
  177. RTCUtils.addListener(eventType, listener);
  178. }
  179. static removeListener (eventType, listener) {
  180. RTCUtils.removeListener(eventType, listener);
  181. }
  182. static isRTCReady () {
  183. return RTCUtils.isRTCReady();
  184. }
  185. static init (options = {}) {
  186. this.options = options;
  187. return RTCUtils.init(this.options);
  188. }
  189. static getDeviceAvailability () {
  190. return RTCUtils.getDeviceAvailability();
  191. }
  192. /**
  193. * Creates new <tt>TraceablePeerConnection</tt>
  194. * @param {SignalingLayer} signaling the signaling layer that will
  195. * provide information about the media or participants which is not carried
  196. * over SDP.
  197. * @param {Object} iceConfig an object describing the ICE config like
  198. * defined in the WebRTC specification.
  199. * @param {Object} options the config options
  200. * @param {boolean} options.disableSimulcast if set to 'true' will disable
  201. * the simulcast
  202. * @param {boolean} options.disableRtx if set to 'true' will disable the RTX
  203. * @param {boolean} options.preferH264 if set to 'true' H264 will be
  204. * preferred over other video codecs.
  205. * @return {TraceablePeerConnection}
  206. */
  207. createPeerConnection (signaling, iceConfig, options) {
  208. const newConnection
  209. = new TraceablePeerConnection(
  210. this,
  211. this.peerConnectionIdCounter,
  212. signaling, iceConfig, RTC.getPCConstraints(), options);
  213. this.peerConnections.set(newConnection.id, newConnection);
  214. this.peerConnectionIdCounter += 1;
  215. return newConnection;
  216. }
  217. /**
  218. * Removed given peer connection from this RTC module instance.
  219. * @param {TraceablePeerConnection} traceablePeerConnection
  220. * @return {boolean} <tt>true</tt> if the given peer connection was removed
  221. * successfully or <tt>false</tt> if there was no peer connection mapped in
  222. * this RTC instance.
  223. */
  224. _removePeerConnection (traceablePeerConnection) {
  225. const id = traceablePeerConnection.id;
  226. if (this.peerConnections.has(id)) {
  227. // NOTE Remote tracks are not removed here.
  228. this.peerConnections.delete(id);
  229. return true;
  230. } else {
  231. return false;
  232. }
  233. }
  234. addLocalTrack (track) {
  235. if (!track)
  236. throw new Error('track must not be null nor undefined');
  237. this.localTracks.push(track);
  238. track.conference = this.conference;
  239. }
  240. /**
  241. * Get local video track.
  242. * @returns {JitsiLocalTrack|undefined}
  243. */
  244. getLocalVideoTrack () {
  245. const localVideo = this.getLocalTracks(MediaType.VIDEO);
  246. return localVideo.length ? localVideo[0] : undefined;
  247. }
  248. /**
  249. * Get local audio track.
  250. * @returns {JitsiLocalTrack|undefined}
  251. */
  252. getLocalAudioTrack () {
  253. const localAudio = this.getLocalTracks(MediaType.AUDIO);
  254. return localAudio.length ? localAudio[0] : undefined;
  255. }
  256. /**
  257. * Returns the local tracks of the given media type, or all local tracks if
  258. * no specific type is given.
  259. * @param {MediaType} [mediaType] optional media type filter
  260. * (audio or video).
  261. */
  262. getLocalTracks (mediaType) {
  263. let tracks = this.localTracks.slice();
  264. if (mediaType !== undefined) {
  265. tracks = tracks.filter(
  266. (track) => { return track.getType() === mediaType; });
  267. }
  268. return tracks;
  269. }
  270. /**
  271. * Obtains all remote tracks currently known to this RTC module instance.
  272. * @param {MediaType} [mediaType] the remote tracks will be filtered
  273. * by their media type if this argument is specified.
  274. * @return {Array<JitsiRemoteTrack>}
  275. */
  276. getRemoteTracks (mediaType) {
  277. const remoteTracks = [];
  278. const remoteEndpoints = Object.keys(this.remoteTracks);
  279. for (const endpoint of remoteEndpoints) {
  280. const endpointMediaTypes = Object.keys(this.remoteTracks[endpoint]);
  281. for (const trackMediaType of endpointMediaTypes) {
  282. // per media type filtering
  283. if (mediaType && mediaType !== trackMediaType) {
  284. continue;
  285. }
  286. const mediaTrack = this.remoteTracks[endpoint][trackMediaType];
  287. if (mediaTrack) {
  288. remoteTracks.push(mediaTrack);
  289. }
  290. }
  291. }
  292. return remoteTracks;
  293. }
  294. /**
  295. * Gets JitsiRemoteTrack for the passed MediaType associated with given MUC
  296. * nickname (resource part of the JID).
  297. * @param type audio or video.
  298. * @param resource the resource part of the MUC JID
  299. * @returns {JitsiRemoteTrack|null}
  300. */
  301. getRemoteTrackByType (type, resource) {
  302. if (this.remoteTracks[resource])
  303. return this.remoteTracks[resource][type];
  304. else
  305. return null;
  306. }
  307. /**
  308. * Gets JitsiRemoteTrack for AUDIO MediaType associated with given MUC nickname
  309. * (resource part of the JID).
  310. * @param resource the resource part of the MUC JID
  311. * @returns {JitsiRemoteTrack|null}
  312. */
  313. getRemoteAudioTrack (resource) {
  314. return this.getRemoteTrackByType(MediaType.AUDIO, resource);
  315. }
  316. /**
  317. * Gets JitsiRemoteTrack for VIDEO MediaType associated with given MUC nickname
  318. * (resource part of the JID).
  319. * @param resource the resource part of the MUC JID
  320. * @returns {JitsiRemoteTrack|null}
  321. */
  322. getRemoteVideoTrack (resource) {
  323. return this.getRemoteTrackByType(MediaType.VIDEO, resource);
  324. }
  325. /**
  326. * Set mute for all local audio streams attached to the conference.
  327. * @param value the mute value
  328. * @returns {Promise}
  329. */
  330. setAudioMute (value) {
  331. const mutePromises = [];
  332. this.getLocalTracks(MediaType.AUDIO).forEach(function(audioTrack){
  333. // this is a Promise
  334. mutePromises.push(value ? audioTrack.mute() : audioTrack.unmute());
  335. });
  336. // we return a Promise from all Promises so we can wait for their execution
  337. return Promise.all(mutePromises);
  338. }
  339. removeLocalTrack (track) {
  340. const pos = this.localTracks.indexOf(track);
  341. if (pos === -1) {
  342. return;
  343. }
  344. this.localTracks.splice(pos, 1);
  345. }
  346. /**
  347. * Initializes a new JitsiRemoteTrack instance with the data provided by
  348. * the signaling layer and SDP.
  349. *
  350. * @param {string} ownerEndpointId
  351. * @param {MediaStream} stream
  352. * @param {MediaStreamTrack} track
  353. * @param {MediaType} mediaType
  354. * @param {VideoType|undefined} videoType
  355. * @param {string} ssrc
  356. * @param {boolean} muted
  357. */
  358. _createRemoteTrack (ownerEndpointId,
  359. stream, track, mediaType, videoType, ssrc, muted) {
  360. const remoteTrack
  361. = new JitsiRemoteTrack(
  362. this, this.conference, ownerEndpointId, stream, track,
  363. mediaType, videoType, ssrc, muted);
  364. const remoteTracks
  365. = this.remoteTracks[ownerEndpointId]
  366. || (this.remoteTracks[ownerEndpointId] = {});
  367. if (remoteTracks[mediaType]) {
  368. logger.error(
  369. "Overwriting remote track!", ownerEndpointId, mediaType);
  370. }
  371. remoteTracks[mediaType] = remoteTrack;
  372. this.eventEmitter.emit(RTCEvents.REMOTE_TRACK_ADDED, remoteTrack);
  373. }
  374. /**
  375. * Removes all JitsiRemoteTracks associated with given MUC nickname
  376. * (resource part of the JID). Returns array of removed tracks.
  377. *
  378. * @param {string} owner - The resource part of the MUC JID.
  379. * @returns {JitsiRemoteTrack[]}
  380. */
  381. removeRemoteTracks (owner) {
  382. const removedTracks = [];
  383. if (this.remoteTracks[owner]) {
  384. const removedAudioTrack
  385. = this.remoteTracks[owner][MediaType.AUDIO];
  386. const removedVideoTrack
  387. = this.remoteTracks[owner][MediaType.VIDEO];
  388. removedAudioTrack && removedTracks.push(removedAudioTrack);
  389. removedVideoTrack && removedTracks.push(removedVideoTrack);
  390. delete this.remoteTracks[owner];
  391. }
  392. return removedTracks;
  393. }
  394. /**
  395. * Finds remote track by it's stream and track ids.
  396. * @param {string} streamId the media stream id as defined by the WebRTC
  397. * @param {string} trackId the media track id as defined by the WebRTC
  398. * @return {JitsiRemoteTrack|undefined}
  399. * @private
  400. */
  401. _getRemoteTrackById (streamId, trackId) {
  402. let result = undefined;
  403. // .find will break the loop once the first match is found
  404. Object.keys(this.remoteTracks).find((endpoint) => {
  405. const endpointTracks = this.remoteTracks[endpoint];
  406. return endpointTracks && Object.keys(endpointTracks).find(
  407. (mediaType) => {
  408. const mediaTrack = endpointTracks[mediaType];
  409. if (mediaTrack
  410. && mediaTrack.getStreamId() == streamId
  411. && mediaTrack.getTrackId() == trackId) {
  412. result = mediaTrack;
  413. return true;
  414. } else {
  415. return false;
  416. }
  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. dispose () { }
  564. setAudioLevel (resource, audioLevel) {
  565. if(!resource)
  566. return;
  567. var 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 => { return 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(function (remoteTrack) {
  594. return ssrc == remoteTrack.getSSRC();
  595. });
  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. var 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. var 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. }