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

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