modified lib-jitsi-meet dev repo
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RTC.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 => track.getType() === mediaType);
  269. }
  270. return tracks;
  271. }
  272. /**
  273. * Obtains all remote tracks currently known to this RTC module instance.
  274. * @param {MediaType} [mediaType] the remote tracks will be filtered
  275. * by their media type if this argument is specified.
  276. * @return {Array<JitsiRemoteTrack>}
  277. */
  278. getRemoteTracks(mediaType) {
  279. const remoteTracks = [];
  280. const remoteEndpoints = Object.keys(this.remoteTracks);
  281. for (const endpoint of remoteEndpoints) {
  282. const endpointMediaTypes = Object.keys(this.remoteTracks[endpoint]);
  283. for (const trackMediaType of endpointMediaTypes) {
  284. // per media type filtering
  285. if (mediaType && mediaType !== trackMediaType) {
  286. continue;
  287. }
  288. const mediaTrack = this.remoteTracks[endpoint][trackMediaType];
  289. if (mediaTrack) {
  290. remoteTracks.push(mediaTrack);
  291. }
  292. }
  293. }
  294. return remoteTracks;
  295. }
  296. /**
  297. * Gets JitsiRemoteTrack for the passed MediaType associated with given MUC
  298. * nickname (resource part of the JID).
  299. * @param type audio or video.
  300. * @param resource the resource part of the MUC JID
  301. * @returns {JitsiRemoteTrack|null}
  302. */
  303. getRemoteTrackByType(type, resource) {
  304. if (this.remoteTracks[resource]) {
  305. return this.remoteTracks[resource][type];
  306. } else {
  307. return null;
  308. }
  309. }
  310. /**
  311. * Gets JitsiRemoteTrack for AUDIO MediaType associated with given MUC nickname
  312. * (resource part of the JID).
  313. * @param resource the resource part of the MUC JID
  314. * @returns {JitsiRemoteTrack|null}
  315. */
  316. getRemoteAudioTrack(resource) {
  317. return this.getRemoteTrackByType(MediaType.AUDIO, resource);
  318. }
  319. /**
  320. * Gets JitsiRemoteTrack for VIDEO MediaType associated with given MUC nickname
  321. * (resource part of the JID).
  322. * @param resource the resource part of the MUC JID
  323. * @returns {JitsiRemoteTrack|null}
  324. */
  325. getRemoteVideoTrack(resource) {
  326. return this.getRemoteTrackByType(MediaType.VIDEO, resource);
  327. }
  328. /**
  329. * Set mute for all local audio streams attached to the conference.
  330. * @param value the mute value
  331. * @returns {Promise}
  332. */
  333. setAudioMute(value) {
  334. const mutePromises = [];
  335. this.getLocalTracks(MediaType.AUDIO).forEach(function(audioTrack) {
  336. // this is a Promise
  337. mutePromises.push(value ? audioTrack.mute() : audioTrack.unmute());
  338. });
  339. // we return a Promise from all Promises so we can wait for their 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. } else {
  418. return false;
  419. }
  420. });
  421. });
  422. return result;
  423. }
  424. /**
  425. * Removes <tt>JitsiRemoteTrack</tt> identified by given stream and track
  426. * ids.
  427. *
  428. * @param {string} streamId media stream id as defined by the WebRTC
  429. * @param {string} trackId media track id as defined by the WebRTC
  430. * @returns {JitsiRemoteTrack|undefined} the track which has been removed or
  431. * <tt>undefined</tt> if no track matching given stream and track ids was
  432. * found.
  433. */
  434. _removeRemoteTrack(streamId, trackId) {
  435. const toBeRemoved = this._getRemoteTrackById(streamId, trackId);
  436. if (toBeRemoved) {
  437. toBeRemoved.dispose();
  438. delete this.remoteTracks[
  439. toBeRemoved.getParticipantId()][toBeRemoved.getType()];
  440. this.eventEmitter.emit(
  441. RTCEvents.REMOTE_TRACK_REMOVED, toBeRemoved);
  442. }
  443. return toBeRemoved;
  444. }
  445. static getPCConstraints() {
  446. return RTCUtils.pc_constraints;
  447. }
  448. static attachMediaStream(elSelector, stream) {
  449. return RTCUtils.attachMediaStream(elSelector, stream);
  450. }
  451. static getStreamID(stream) {
  452. return RTCUtils.getStreamID(stream);
  453. }
  454. /**
  455. * Returns true if retrieving the the list of input devices is supported
  456. * and false if not.
  457. */
  458. static isDeviceListAvailable() {
  459. return RTCUtils.isDeviceListAvailable();
  460. }
  461. /**
  462. * Returns true if changing the input (camera / microphone) or output
  463. * (audio) device is supported and false if not.
  464. * @params {string} [deviceType] - type of device to change. Default is
  465. * undefined or 'input', 'output' - for audio output device change.
  466. * @returns {boolean} true if available, false otherwise.
  467. */
  468. static isDeviceChangeAvailable(deviceType) {
  469. return RTCUtils.isDeviceChangeAvailable(deviceType);
  470. }
  471. /**
  472. * Returns currently used audio output device id, '' stands for default
  473. * device
  474. * @returns {string}
  475. */
  476. static getAudioOutputDevice() {
  477. return RTCUtils.getAudioOutputDevice();
  478. }
  479. /**
  480. * Returns list of available media devices if its obtained, otherwise an
  481. * empty array is returned/
  482. * @returns {Array} list of available media devices.
  483. */
  484. static getCurrentlyAvailableMediaDevices() {
  485. return RTCUtils.getCurrentlyAvailableMediaDevices();
  486. }
  487. /**
  488. * Returns event data for device to be reported to stats.
  489. * @returns {MediaDeviceInfo} device.
  490. */
  491. static getEventDataForActiveDevice(device) {
  492. return RTCUtils.getEventDataForActiveDevice(device);
  493. }
  494. /**
  495. * Sets current audio output device.
  496. * @param {string} deviceId - id of 'audiooutput' device from
  497. * navigator.mediaDevices.enumerateDevices()
  498. * @returns {Promise} - resolves when audio output is changed, is rejected
  499. * otherwise
  500. */
  501. static setAudioOutputDevice(deviceId) {
  502. return RTCUtils.setAudioOutputDevice(deviceId);
  503. }
  504. /**
  505. * Returns <tt>true<tt/> if given WebRTC MediaStream is considered a valid
  506. * "user" stream which means that it's not a "receive only" stream nor a
  507. * "mixed" JVB stream.
  508. *
  509. * Clients that implement Unified Plan, such as Firefox use recvonly
  510. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed
  511. * to Plan B where there are only 3 channels: audio, video and data.
  512. *
  513. * @param {MediaStream} stream the WebRTC MediaStream instance
  514. * @returns {boolean}
  515. */
  516. static isUserStream(stream) {
  517. return RTC.isUserStreamById(RTCUtils.getStreamID(stream));
  518. }
  519. /**
  520. * Returns <tt>true<tt/> if a WebRTC MediaStream identified by given stream
  521. * ID is considered a valid "user" stream which means that it's not a
  522. * "receive only" stream nor a "mixed" JVB stream.
  523. *
  524. * Clients that implement Unified Plan, such as Firefox use recvonly
  525. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed
  526. * to Plan B where there are only 3 channels: audio, video and data.
  527. *
  528. * @param {string} streamId the id of WebRTC MediaStream
  529. * @returns {boolean}
  530. */
  531. static isUserStreamById(streamId) {
  532. return streamId && streamId !== "mixedmslabel"
  533. && streamId !== "default";
  534. }
  535. /**
  536. * Allows to receive list of available cameras/microphones.
  537. * @param {function} callback would receive array of devices as an argument
  538. */
  539. static enumerateDevices(callback) {
  540. RTCUtils.enumerateDevices(callback);
  541. }
  542. /**
  543. * A method to handle stopping of the stream.
  544. * One point to handle the differences in various implementations.
  545. * @param mediaStream MediaStream object to stop.
  546. */
  547. static stopMediaStream(mediaStream) {
  548. RTCUtils.stopMediaStream(mediaStream);
  549. }
  550. /**
  551. * Returns whether the desktop sharing is enabled or not.
  552. * @returns {boolean}
  553. */
  554. static isDesktopSharingEnabled() {
  555. return RTCUtils.isDesktopSharingEnabled();
  556. }
  557. /**
  558. * Closes all currently opened data channels.
  559. */
  560. closeAllDataChannels() {
  561. if(this.dataChannels) {
  562. this.dataChannels.closeAllChannels();
  563. this.dataChannelsOpen = false;
  564. }
  565. }
  566. dispose() { }
  567. setAudioLevel(resource, audioLevel) {
  568. if(!resource) {
  569. return;
  570. }
  571. var audioTrack = this.getRemoteAudioTrack(resource);
  572. if(audioTrack) {
  573. audioTrack.setAudioLevel(audioLevel);
  574. }
  575. }
  576. /**
  577. * Searches in localTracks(session stores ssrc for audio and video) and
  578. * remoteTracks for the ssrc and returns the corresponding resource.
  579. * @param ssrc the ssrc to check.
  580. */
  581. getResourceBySSRC(ssrc) {
  582. if (this.getLocalTracks().find(
  583. localTrack => localTrack.getSSRC() == ssrc)) {
  584. return this.conference.myUserId();
  585. }
  586. const track = this.getRemoteTrackBySSRC(ssrc);
  587. return track ? track.getParticipantId() : null;
  588. }
  589. /**
  590. * Searches in remoteTracks for the ssrc and returns the corresponding
  591. * track.
  592. * @param ssrc the ssrc to check.
  593. * @return {JitsiRemoteTrack|undefined} return the first remote track that
  594. * matches given SSRC or <tt>undefined</tt> if no such track was found.
  595. */
  596. getRemoteTrackBySSRC(ssrc) {
  597. return this.getRemoteTracks().find(function(remoteTrack) {
  598. return ssrc == remoteTrack.getSSRC();
  599. });
  600. }
  601. /**
  602. * Handles remote track mute / unmute events.
  603. * @param type {string} "audio" or "video"
  604. * @param isMuted {boolean} the new mute state
  605. * @param from {string} user id
  606. */
  607. handleRemoteTrackMute(type, isMuted, from) {
  608. var track = this.getRemoteTrackByType(type, from);
  609. if (track) {
  610. track.setMute(isMuted);
  611. }
  612. }
  613. /**
  614. * Handles remote track video type events
  615. * @param value {string} the new video type
  616. * @param from {string} user id
  617. */
  618. handleRemoteTrackVideoTypeChanged(value, from) {
  619. var videoTrack = this.getRemoteVideoTrack(from);
  620. if (videoTrack) {
  621. videoTrack._setVideoType(value);
  622. }
  623. }
  624. /**
  625. * Sends message via the datachannels.
  626. * @param to {string} the id of the endpoint that should receive the
  627. * message. If "" the message will be sent to all participants.
  628. * @param payload {object} the payload of the message.
  629. * @throws NetworkError or InvalidStateError or Error if the operation
  630. * fails or there is no data channel created
  631. */
  632. sendDataChannelMessage(to, payload) {
  633. if(this.dataChannels) {
  634. this.dataChannels.sendDataChannelMessage(to, payload);
  635. } else {
  636. throw new Error("Data channels support is disabled!");
  637. }
  638. }
  639. /**
  640. * Selects a new value for "lastN". The requested amount of videos are going
  641. * to be delivered after the value is in effect. Set to -1 for unlimited or
  642. * all available videos.
  643. * @param value {int} the new value for lastN.
  644. * @trows Error if there is no data channel created.
  645. */
  646. setLastN(value) {
  647. if (this.dataChannels) {
  648. this.dataChannels.sendSetLastNMessage(value);
  649. } else {
  650. throw new Error("Data channels support is disabled!");
  651. }
  652. }
  653. }