You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RTC.js 23KB

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