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.

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