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

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