您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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