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

JitsiLocalTrack.js 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. import { getLogger } from '@jitsi/logger';
  2. import JitsiTrackError from '../../JitsiTrackError';
  3. import {
  4. TRACK_IS_DISPOSED,
  5. TRACK_NO_STREAM_FOUND
  6. } from '../../JitsiTrackErrors';
  7. import {
  8. LOCAL_TRACK_STOPPED,
  9. NO_DATA_FROM_SOURCE,
  10. TRACK_MUTE_CHANGED
  11. } from '../../JitsiTrackEvents';
  12. import CameraFacingMode from '../../service/RTC/CameraFacingMode';
  13. import { MediaType } from '../../service/RTC/MediaType';
  14. import RTCEvents from '../../service/RTC/RTCEvents';
  15. import { VideoType } from '../../service/RTC/VideoType';
  16. import {
  17. NO_BYTES_SENT,
  18. TRACK_UNMUTED,
  19. createNoDataFromSourceEvent
  20. } from '../../service/statistics/AnalyticsEvents';
  21. import browser from '../browser';
  22. import FeatureFlags from '../flags/FeatureFlags';
  23. import Statistics from '../statistics/statistics';
  24. import JitsiTrack from './JitsiTrack';
  25. import RTCUtils from './RTCUtils';
  26. const logger = getLogger(__filename);
  27. /**
  28. * Represents a single media track(either audio or video).
  29. * One <tt>JitsiLocalTrack</tt> corresponds to one WebRTC MediaStreamTrack.
  30. */
  31. export default class JitsiLocalTrack extends JitsiTrack {
  32. /**
  33. * Constructs a new JitsiLocalTrack instance.
  34. *
  35. * @constructor
  36. * @param {Object} trackInfo
  37. * @param {number} trackInfo.rtcId - The ID assigned by the RTC module.
  38. * @param {Object} trackInfo.stream - The WebRTC MediaStream, parent of the track.
  39. * @param {Object} trackInfo.track - The underlying WebRTC MediaStreamTrack for new JitsiLocalTrack.
  40. * @param {string} trackInfo.mediaType - The MediaType of the JitsiLocalTrack.
  41. * @param {string} trackInfo.videoType - The VideoType of the JitsiLocalTrack.
  42. * @param {Array<Object>} trackInfo.effects - The effects to be applied to the JitsiLocalTrack.
  43. * @param {number} trackInfo.resolution - The the video resolution if it's a video track
  44. * @param {string} trackInfo.deviceId - The ID of the local device for this track.
  45. * @param {string} trackInfo.facingMode - Thehe camera facing mode used in getUserMedia call (for mobile only).
  46. * @param {string} trackInfo.sourceId - The id of the desktop sharing source, which is the Chrome media source ID,
  47. * returned by Desktop Picker on Electron. NOTE: defined for desktop sharing tracks only.
  48. * @param {string} trackInfo.sourceType - The type of source the track originates from.
  49. */
  50. constructor({
  51. deviceId,
  52. facingMode,
  53. mediaType,
  54. resolution,
  55. rtcId,
  56. sourceId,
  57. sourceType,
  58. stream,
  59. track,
  60. videoType,
  61. effects = []
  62. }) {
  63. super(
  64. /* conference */ null,
  65. stream,
  66. track,
  67. /* streamInactiveHandler */ () => this.emit(LOCAL_TRACK_STOPPED, this),
  68. mediaType,
  69. videoType);
  70. this._setEffectInProgress = false;
  71. const effect = effects.find(e => e.isEnabled(this));
  72. if (effect) {
  73. this._startStreamEffect(effect);
  74. }
  75. const displaySurface = videoType === VideoType.DESKTOP
  76. ? track.getSettings().displaySurface
  77. : null;
  78. /**
  79. * Track metadata.
  80. */
  81. this.metadata = {
  82. timestamp: Date.now(),
  83. ...displaySurface ? { displaySurface } : {}
  84. };
  85. /**
  86. * The ID assigned by the RTC module on instance creation.
  87. *
  88. * @type {number}
  89. */
  90. this.rtcId = rtcId;
  91. this.sourceId = sourceId;
  92. this.sourceType = sourceType ?? displaySurface;
  93. // Get the resolution from the track itself because it cannot be
  94. // certain which resolution webrtc has fallen back to using.
  95. this.resolution = this.getHeight();
  96. this.maxEnabledResolution = resolution;
  97. // Cache the constraints of the track in case of any this track
  98. // model needs to call getUserMedia again, such as when unmuting.
  99. this._constraints = track.getConstraints();
  100. // Safari returns an empty constraints object, construct the constraints using getSettings.
  101. if (!Object.keys(this._constraints).length && videoType === VideoType.CAMERA) {
  102. this._constraints = {
  103. height: this.getHeight(),
  104. width: this.getWidth()
  105. };
  106. }
  107. this.deviceId = deviceId;
  108. /**
  109. * The <tt>Promise</tt> which represents the progress of a previously
  110. * queued/scheduled {@link _setMuted} (from the point of view of
  111. * {@link _queueSetMuted}).
  112. *
  113. * @private
  114. * @type {Promise}
  115. */
  116. this._prevSetMuted = Promise.resolve();
  117. /**
  118. * The facing mode of the camera from which this JitsiLocalTrack
  119. * instance was obtained.
  120. *
  121. * @private
  122. * @type {CameraFacingMode|undefined}
  123. */
  124. this._facingMode = facingMode;
  125. // Currently there is no way to know the MediaStreamTrack ended due to
  126. // to device disconnect in Firefox through e.g. "readyState" property.
  127. // Instead we will compare current track's label with device labels from
  128. // enumerateDevices() list.
  129. this._trackEnded = false;
  130. /**
  131. * Indicates whether data has been sent or not.
  132. */
  133. this._hasSentData = false;
  134. /**
  135. * Used only for detection of audio problems. We want to check only once
  136. * whether the track is sending data ot not. This flag is set to false
  137. * after the check.
  138. */
  139. this._testDataSent = true;
  140. // Currently there is no way to determine with what device track was
  141. // created (until getConstraints() support), however we can associate
  142. // tracks with real devices obtained from enumerateDevices() call as
  143. // soon as it's called.
  144. // NOTE: this.deviceId corresponds to the device id specified in GUM constraints and this._realDeviceId seems to
  145. // correspond to the id of a matching device from the available device list.
  146. this._realDeviceId = this.deviceId === '' ? undefined : this.deviceId;
  147. // The source name that will be signaled for this track.
  148. this._sourceName = null;
  149. this._trackMutedTS = 0;
  150. this._onDeviceListWillChange = devices => {
  151. const oldRealDeviceId = this._realDeviceId;
  152. this._setRealDeviceIdFromDeviceList(devices);
  153. if (
  154. // Mark track as ended for those browsers that do not support
  155. // "readyState" property. We do not touch tracks created with
  156. // default device ID "".
  157. (typeof this.getTrack().readyState === 'undefined'
  158. && typeof this._realDeviceId !== 'undefined'
  159. && !devices.find(d => d.deviceId === this._realDeviceId))
  160. // If there was an associated realDeviceID and after the device change the realDeviceId is undefined
  161. // then the associated device has been disconnected and the _trackEnded flag needs to be set. In
  162. // addition on some Chrome versions the readyState property is set after the device change event is
  163. // triggered which causes issues in jitsi-meet with the selection of a new device because we don't
  164. // detect that the old one was removed.
  165. || (typeof oldRealDeviceId !== 'undefined' && typeof this._realDeviceId === 'undefined')
  166. ) {
  167. this._trackEnded = true;
  168. }
  169. };
  170. // Subscribe each created local audio track to
  171. // RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED event. This is different from
  172. // handling this event for remote tracks (which are handled in RTC.js),
  173. // because there might be local tracks not attached to a conference.
  174. if (this.isAudioTrack() && RTCUtils.isDeviceChangeAvailable('output')) {
  175. this._onAudioOutputDeviceChanged = this.setAudioOutput.bind(this);
  176. RTCUtils.addListener(
  177. RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  178. this._onAudioOutputDeviceChanged);
  179. }
  180. RTCUtils.addListener(RTCEvents.DEVICE_LIST_WILL_CHANGE, this._onDeviceListWillChange);
  181. this._initNoDataFromSourceHandlers();
  182. }
  183. /**
  184. * Adds stream to conference and marks it as "unmute" operation.
  185. *
  186. * @private
  187. * @returns {Promise}
  188. */
  189. _addStreamToConferenceAsUnmute() {
  190. if (!this.conference) {
  191. return Promise.resolve();
  192. }
  193. // FIXME it would be good to not included conference as part of this process. Only TraceablePeerConnections to
  194. // which the track is attached should care about this action. The TPCs to which the track is not attached can
  195. // sync up when track is re-attached. A problem with that is that the "modify sources" queue is part of the
  196. // JingleSessionPC and it would be excluded from the process. One solution would be to extract class between
  197. // TPC and JingleSessionPC which would contain the queue and would notify the signaling layer when local SSRCs
  198. // are changed. This would help to separate XMPP from the RTC module.
  199. return new Promise((resolve, reject) => {
  200. this.conference._addLocalTrackToPc(this)
  201. .then(resolve, error => reject(new Error(error)));
  202. });
  203. }
  204. /**
  205. * Fires NO_DATA_FROM_SOURCE event and logs it to analytics
  206. *
  207. * @private
  208. * @returns {void}
  209. */
  210. _fireNoDataFromSourceEvent() {
  211. const value = !this.isReceivingData();
  212. this.emit(NO_DATA_FROM_SOURCE, value);
  213. logger.debug(`NO_DATA_FROM_SOURCE event with value ${value} detected for track: ${this}`);
  214. // FIXME: Should we report all of those events
  215. Statistics.sendAnalytics(createNoDataFromSourceEvent(this.getType(), value));
  216. }
  217. /**
  218. * Sets handlers to the MediaStreamTrack object that will detect camera issues.
  219. *
  220. * @private
  221. * @returns {void}
  222. */
  223. _initNoDataFromSourceHandlers() {
  224. if (!this._isNoDataFromSourceEventsEnabled()) {
  225. return;
  226. }
  227. this._setHandler('track_mute', () => {
  228. this._trackMutedTS = window.performance.now();
  229. this._fireNoDataFromSourceEvent();
  230. });
  231. this._setHandler('track_unmute', () => {
  232. this._fireNoDataFromSourceEvent();
  233. Statistics.sendAnalyticsAndLog(
  234. TRACK_UNMUTED,
  235. {
  236. 'media_type': this.getType(),
  237. 'track_type': 'local',
  238. value: window.performance.now() - this._trackMutedTS
  239. });
  240. });
  241. if (this.isVideoTrack() && this.videoType === VideoType.CAMERA) {
  242. this._setHandler('track_ended', () => {
  243. if (!this.isReceivingData()) {
  244. this._fireNoDataFromSourceEvent();
  245. }
  246. });
  247. }
  248. }
  249. /**
  250. * Returns true if no data from source events are enabled for this JitsiLocalTrack and false otherwise.
  251. *
  252. * @private
  253. * @returns {boolean} - True if no data from source events are enabled for this JitsiLocalTrack and false otherwise.
  254. */
  255. _isNoDataFromSourceEventsEnabled() {
  256. // Disable the events for screen sharing.
  257. return !this.isVideoTrack() || this.videoType !== VideoType.DESKTOP;
  258. }
  259. /**
  260. * Initializes a new Promise to execute {@link #_setMuted}. May be called multiple times in a row and the
  261. * invocations of {@link #_setMuted} and, consequently, {@link #mute} and/or {@link #unmute} will be resolved in a
  262. * serialized fashion.
  263. *
  264. * @param {boolean} muted - The value to invoke <tt>_setMuted</tt> with.
  265. * @private
  266. * @returns {Promise}
  267. */
  268. _queueSetMuted(muted) {
  269. const setMuted = this._setMuted.bind(this, muted);
  270. this._prevSetMuted = this._prevSetMuted.then(setMuted, setMuted);
  271. return this._prevSetMuted;
  272. }
  273. /**
  274. * Removes stream from conference and marks it as "mute" operation.
  275. *
  276. * @param {Function} successCallback - Callback that will be called when the operation is successful.
  277. * @param {Function} errorCallback - Callback that will be called when the operation fails.
  278. * @private
  279. * @returns {Promise}
  280. */
  281. _removeStreamFromConferenceAsMute(successCallback, errorCallback) {
  282. if (!this.conference) {
  283. successCallback();
  284. return;
  285. }
  286. this.conference._removeLocalTrackFromPc(this).then(
  287. successCallback,
  288. error => errorCallback(new Error(error)));
  289. }
  290. /**
  291. * Sends mute status for a track to conference if any.
  292. *
  293. * @param {boolean} mute - If track is muted.
  294. * @private
  295. * @returns {void}
  296. */
  297. _sendMuteStatus(mute) {
  298. if (this.conference) {
  299. this.conference._setTrackMuteStatus(this.getType(), this, mute) && this.conference.room.sendPresence();
  300. }
  301. }
  302. /**
  303. * Mutes / unmutes this track.
  304. *
  305. * @param {boolean} muted - If <tt>true</tt>, this track will be muted; otherwise, this track will be unmuted.
  306. * @private
  307. * @returns {Promise}
  308. */
  309. _setMuted(muted) {
  310. if (this.isMuted() === muted
  311. && !(this.videoType === VideoType.DESKTOP && FeatureFlags.isMultiStreamSendSupportEnabled())) {
  312. return Promise.resolve();
  313. }
  314. if (this.disposed) {
  315. return Promise.reject(new JitsiTrackError(TRACK_IS_DISPOSED));
  316. }
  317. let promise = Promise.resolve();
  318. // A function that will print info about muted status transition
  319. const logMuteInfo = () => logger.info(`Mute ${this}: ${muted}`);
  320. // In React Native we mute the camera by setting track.enabled but that doesn't
  321. // work for screen-share tracks, so do the remove-as-mute for those.
  322. const doesVideoMuteByStreamRemove
  323. = browser.isReactNative() ? this.videoType === VideoType.DESKTOP : browser.doesVideoMuteByStreamRemove();
  324. // In the multi-stream mode, desktop tracks are muted from jitsi-meet instead of being removed from the
  325. // conference. This is needed because we don't want the client to signal a source-remove to the remote peer for
  326. // the desktop track when screenshare is stopped. Later when screenshare is started again, the same sender will
  327. // be re-used without the need for signaling a new ssrc through source-add.
  328. if (this.isAudioTrack()
  329. || (this.videoType === VideoType.DESKTOP && !FeatureFlags.isMultiStreamSendSupportEnabled())
  330. || !doesVideoMuteByStreamRemove) {
  331. logMuteInfo();
  332. // If we have a stream effect that implements its own mute functionality, prioritize it before
  333. // normal mute e.g. the stream effect that implements system audio sharing has a custom
  334. // mute state in which if the user mutes, system audio still has to go through.
  335. if (this._streamEffect && this._streamEffect.setMuted) {
  336. this._streamEffect.setMuted(muted);
  337. } else if (this.track) {
  338. this.track.enabled = !muted;
  339. }
  340. } else if (muted) {
  341. promise = new Promise((resolve, reject) => {
  342. logMuteInfo();
  343. this._removeStreamFromConferenceAsMute(
  344. () => {
  345. if (this._streamEffect) {
  346. this._stopStreamEffect();
  347. }
  348. // FIXME: Maybe here we should set the SRC for the
  349. // containers to something
  350. // We don't want any events to be fired on this stream
  351. this._unregisterHandlers();
  352. this.stopStream();
  353. this._setStream(null);
  354. resolve();
  355. },
  356. reject);
  357. });
  358. } else {
  359. logMuteInfo();
  360. // This path is only for camera.
  361. const streamOptions = {
  362. cameraDeviceId: this.getDeviceId(),
  363. devices: [ MediaType.VIDEO ],
  364. effects: this._streamEffect ? [ this._streamEffect ] : [],
  365. facingMode: this.getCameraFacingMode()
  366. };
  367. promise
  368. = RTCUtils.obtainAudioAndVideoPermissions(Object.assign(
  369. {},
  370. streamOptions,
  371. { constraints: { video: this._constraints } }));
  372. promise = promise.then(streamsInfo => {
  373. const streamInfo = streamsInfo.find(info => info.track.kind === this.getType());
  374. if (streamInfo) {
  375. this._setStream(streamInfo.stream);
  376. this.track = streamInfo.track;
  377. // This is not good when video type changes after
  378. // unmute, but let's not crash here
  379. if (this.videoType !== streamInfo.videoType) {
  380. logger.warn(
  381. `${this}: video type has changed after unmute!`,
  382. this.videoType, streamInfo.videoType);
  383. this.videoType = streamInfo.videoType;
  384. }
  385. } else {
  386. throw new JitsiTrackError(TRACK_NO_STREAM_FOUND);
  387. }
  388. if (this._streamEffect) {
  389. this._startStreamEffect(this._streamEffect);
  390. }
  391. this.containers.map(cont => RTCUtils.attachMediaStream(cont, this.stream).catch(() => {
  392. logger.error(`Attach media failed for ${this} on video unmute!`);
  393. }));
  394. return this._addStreamToConferenceAsUnmute();
  395. });
  396. }
  397. return promise
  398. .then(() => {
  399. this._sendMuteStatus(muted);
  400. // Send the videoType message to the bridge.
  401. this.isVideoTrack() && this.conference && this.conference._sendBridgeVideoTypeMessage(this);
  402. this.emit(TRACK_MUTE_CHANGED, this);
  403. });
  404. }
  405. /**
  406. * Sets real device ID by comparing track information with device information. This is temporary solution until
  407. * getConstraints() method will be implemented in browsers.
  408. *
  409. * @param {MediaDeviceInfo[]} devices - The list of devices obtained from enumerateDevices() call.
  410. * @private
  411. * @returns {void}
  412. */
  413. _setRealDeviceIdFromDeviceList(devices) {
  414. const track = this.getTrack();
  415. const kind = `${track.kind}input`;
  416. // We need to match by deviceId as well, in case of multiple devices with the same label.
  417. let device = devices.find(d => d.kind === kind && d.label === track.label && d.deviceId === this.deviceId);
  418. if (!device && this._realDeviceId === 'default') { // the default device has been changed.
  419. // If the default device was 'A' and the default device is changed to 'B' the label for the track will
  420. // remain 'Default - A' but the label for the device in the device list will be updated to 'A'. That's
  421. // why in order to match it we need to remove the 'Default - ' part.
  422. const label = (track.label || '').replace('Default - ', '');
  423. device = devices.find(d => d.kind === kind && d.label === label);
  424. }
  425. if (device) {
  426. this._realDeviceId = device.deviceId;
  427. } else {
  428. this._realDeviceId = undefined;
  429. }
  430. }
  431. /**
  432. * Sets the stream property of JitsiLocalTrack object and sets all stored handlers to it.
  433. *
  434. * @param {MediaStream} stream - The new MediaStream.
  435. * @private
  436. * @returns {void}
  437. */
  438. _setStream(stream) {
  439. super._setStream(stream);
  440. if (stream) {
  441. // Store the MSID for video mute/unmute purposes.
  442. this.storedMSID = this.getMSID();
  443. logger.debug(`Setting new MSID: ${this.storedMSID} on ${this}`);
  444. } else {
  445. logger.debug(`Setting 'null' stream on ${this}`);
  446. }
  447. }
  448. /**
  449. * Starts the effect process and returns the modified stream.
  450. *
  451. * @param {Object} effect - Represents effect instance
  452. * @private
  453. * @returns {void}
  454. */
  455. _startStreamEffect(effect) {
  456. this._streamEffect = effect;
  457. this._originalStream = this.stream;
  458. this._setStream(this._streamEffect.startEffect(this._originalStream));
  459. this.track = this.stream.getTracks()[0];
  460. }
  461. /**
  462. * Stops the effect process and returns the original stream.
  463. *
  464. * @private
  465. * @returns {void}
  466. */
  467. _stopStreamEffect() {
  468. if (this._streamEffect) {
  469. this._streamEffect.stopEffect();
  470. this._setStream(this._originalStream);
  471. this._originalStream = null;
  472. this.track = this.stream ? this.stream.getTracks()[0] : null;
  473. }
  474. }
  475. /**
  476. * Switches the camera facing mode if the WebRTC implementation supports the custom MediaStreamTrack._switchCamera
  477. * method. Currently, the method in question is implemented in react-native-webrtc only. When such a WebRTC
  478. * implementation is executing, the method is the preferred way to switch between the front/user-facing and the
  479. * back/environment-facing cameras because it will likely be (as is the case of react-native-webrtc) noticeably
  480. * faster that creating a new MediaStreamTrack via a new getUserMedia call with the switched facingMode constraint
  481. * value. Moreover, the approach with a new getUserMedia call may not even work: WebRTC on Android and iOS is
  482. * either very slow to open the camera a second time or plainly freezes attempting to do that.
  483. *
  484. * @returns {void}
  485. */
  486. _switchCamera() {
  487. if (this.isVideoTrack()
  488. && this.videoType === VideoType.CAMERA
  489. && typeof this.track._switchCamera === 'function') {
  490. this.track._switchCamera();
  491. this._facingMode
  492. = this._facingMode === CameraFacingMode.ENVIRONMENT
  493. ? CameraFacingMode.USER
  494. : CameraFacingMode.ENVIRONMENT;
  495. }
  496. }
  497. /**
  498. * Stops the currently used effect (if there is one) and starts the passed effect (if there is one).
  499. *
  500. * @param {Object|undefined} effect - The new effect to be set.
  501. * @private
  502. * @returns {void}
  503. */
  504. _switchStreamEffect(effect) {
  505. if (this._streamEffect) {
  506. this._stopStreamEffect();
  507. this._streamEffect = undefined;
  508. }
  509. if (effect) {
  510. this._startStreamEffect(effect);
  511. }
  512. }
  513. /**
  514. * @inheritdoc
  515. *
  516. * Stops sending the media track. And removes it from the HTML. NOTE: Works for local tracks only.
  517. *
  518. * @extends JitsiTrack#dispose
  519. * @returns {Promise}
  520. */
  521. async dispose() {
  522. // Remove the effect instead of stopping it so that the original stream is restored
  523. // on both the local track and on the peerconnection.
  524. if (this._streamEffect) {
  525. await this.setEffect();
  526. }
  527. if (this.conference) {
  528. await this.conference.removeTrack(this);
  529. }
  530. if (this.stream) {
  531. this.stopStream();
  532. this.detach();
  533. }
  534. RTCUtils.removeListener(RTCEvents.DEVICE_LIST_WILL_CHANGE, this._onDeviceListWillChange);
  535. if (this._onAudioOutputDeviceChanged) {
  536. RTCUtils.removeListener(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  537. this._onAudioOutputDeviceChanged);
  538. }
  539. return super.dispose();
  540. }
  541. /**
  542. * Returns facing mode for video track from camera. For other cases (e.g. audio track or 'desktop' video track)
  543. * returns undefined.
  544. *
  545. * @returns {CameraFacingMode|undefined}
  546. */
  547. getCameraFacingMode() {
  548. if (this.isVideoTrack() && this.videoType === VideoType.CAMERA) {
  549. // MediaStreamTrack#getSettings() is not implemented in many
  550. // browsers, so we need feature checking here. Progress on the
  551. // respective browser's implementation can be tracked at
  552. // https://bugs.chromium.org/p/webrtc/issues/detail?id=2481 for
  553. // Chromium and https://bugzilla.mozilla.org/show_bug.cgi?id=1213517
  554. // for Firefox. Even if a browser implements getSettings() already,
  555. // it might still not return anything for 'facingMode'.
  556. const trackSettings = this.track.getSettings?.();
  557. if (trackSettings && 'facingMode' in trackSettings) {
  558. return trackSettings.facingMode;
  559. }
  560. if (typeof this._facingMode !== 'undefined') {
  561. return this._facingMode;
  562. }
  563. // In most cases we are showing a webcam. So if we've gotten here,
  564. // it should be relatively safe to assume that we are probably
  565. // showing the user-facing camera.
  566. return CameraFacingMode.USER;
  567. }
  568. return undefined;
  569. }
  570. /**
  571. * Returns device id associated with track.
  572. *
  573. * @returns {string}
  574. */
  575. getDeviceId() {
  576. return this._realDeviceId || this.deviceId;
  577. }
  578. /**
  579. * Get the duration of the track.
  580. *
  581. * @returns {Number} the duration of the track in seconds
  582. */
  583. getDuration() {
  584. return (Date.now() / 1000) - (this.metadata.timestamp / 1000);
  585. }
  586. /**
  587. * Returns the participant id which owns the track.
  588. *
  589. * @returns {string} the id of the participants. It corresponds to the
  590. * Colibri endpoint id/MUC nickname in case of Jitsi-meet.
  591. */
  592. getParticipantId() {
  593. return this.conference && this.conference.myUserId();
  594. }
  595. /**
  596. * Returns the source name associated with the jitsi track.
  597. *
  598. * @returns {string | null} source name
  599. */
  600. getSourceName() {
  601. return this._sourceName;
  602. }
  603. /**
  604. * Returns if associated MediaStreamTrack is in the 'ended' state
  605. *
  606. * @returns {boolean}
  607. */
  608. isEnded() {
  609. if (this.isVideoTrack() && this.isMuted()) {
  610. // If a video track is muted the readyState will be ended, that's why we need to rely only on the
  611. // _trackEnded flag.
  612. return this._trackEnded;
  613. }
  614. return this.getTrack().readyState === 'ended' || this._trackEnded;
  615. }
  616. /**
  617. * Returns <tt>true</tt>.
  618. *
  619. * @returns {boolean} <tt>true</tt>
  620. */
  621. isLocal() {
  622. return true;
  623. }
  624. /**
  625. * Returns <tt>true</tt> - if the stream is muted and <tt>false</tt> otherwise.
  626. *
  627. * @returns {boolean} <tt>true</tt> - if the stream is muted and <tt>false</tt> otherwise.
  628. */
  629. isMuted() {
  630. // this.stream will be null when we mute local video on Chrome
  631. if (!this.stream) {
  632. return true;
  633. }
  634. if (this.isVideoTrack() && !this.isActive()) {
  635. return true;
  636. }
  637. // If currently used stream effect has its own muted state, use that.
  638. if (this._streamEffect && this._streamEffect.isMuted) {
  639. return this._streamEffect.isMuted();
  640. }
  641. return !this.track || !this.track.enabled;
  642. }
  643. /**
  644. * Checks whether the attached MediaStream is receiving data from source or not. If the stream property is null
  645. * (because of mute or another reason) this method will return false.
  646. * NOTE: This method doesn't indicate problem with the streams directly. For example in case of video mute the
  647. * method will return false or if the user has disposed the track.
  648. *
  649. * @returns {boolean} true if the stream is receiving data and false this otherwise.
  650. */
  651. isReceivingData() {
  652. if (this.isVideoTrack()
  653. && (this.isMuted() || this._stopStreamInProgress || this.videoType === VideoType.DESKTOP)) {
  654. return true;
  655. }
  656. if (!this.stream) {
  657. return false;
  658. }
  659. // In older version of the spec there is no muted property and readyState can have value muted. In the latest
  660. // versions readyState can have values "live" and "ended" and there is muted boolean property. If the stream is
  661. // muted that means that we aren't receiving any data from the source. We want to notify the users for error if
  662. // the stream is muted or ended on it's creation.
  663. // For video blur enabled use the original video stream
  664. const stream = this._effectEnabled ? this._originalStream : this.stream;
  665. return stream.getTracks().some(track =>
  666. (!('readyState' in track) || track.readyState === 'live')
  667. && (!('muted' in track) || track.muted !== true));
  668. }
  669. /**
  670. * Asynchronously mutes this track.
  671. *
  672. * @returns {Promise}
  673. */
  674. mute() {
  675. return this._queueSetMuted(true);
  676. }
  677. /**
  678. * Handles bytes sent statistics. NOTE: used only for audio tracks to detect audio issues.
  679. *
  680. * @param {TraceablePeerConnection} tpc - The peerconnection that is reporting the bytes sent stat.
  681. * @param {number} bytesSent - The new value.
  682. * @returns {void}
  683. */
  684. onByteSentStatsReceived(tpc, bytesSent) {
  685. if (bytesSent > 0) {
  686. this._hasSentData = true;
  687. }
  688. const iceConnectionState = tpc.getConnectionState();
  689. if (this._testDataSent && iceConnectionState === 'connected') {
  690. setTimeout(() => {
  691. if (!this._hasSentData) {
  692. logger.warn(`${this} 'bytes sent' <= 0: \
  693. ${bytesSent}`);
  694. Statistics.analytics.sendEvent(NO_BYTES_SENT, { 'media_type': this.getType() });
  695. }
  696. }, 3000);
  697. this._testDataSent = false;
  698. }
  699. }
  700. /**
  701. * Sets the JitsiConference object associated with the track. This is temp solution.
  702. *
  703. * @param conference - JitsiConference object.
  704. * @returns {void}
  705. */
  706. setConference(conference) {
  707. this.conference = conference;
  708. }
  709. /**
  710. * Sets the effect and switches between the modified stream and original one.
  711. *
  712. * @param {Object} effect - Represents the effect instance to be used.
  713. * @returns {Promise}
  714. */
  715. setEffect(effect) {
  716. if (typeof this._streamEffect === 'undefined' && typeof effect === 'undefined') {
  717. return Promise.resolve();
  718. }
  719. if (typeof effect !== 'undefined' && !effect.isEnabled(this)) {
  720. return Promise.reject(new Error('Incompatible effect instance!'));
  721. }
  722. if (this._setEffectInProgress === true) {
  723. return Promise.reject(new Error('setEffect already in progress!'));
  724. }
  725. // In case we have an audio track that is being enhanced with an effect, we still want it to be applied,
  726. // even if the track is muted. Where as for video the actual track doesn't exists if it's muted.
  727. if (this.isMuted() && !this.isAudioTrack()) {
  728. this._streamEffect = effect;
  729. return Promise.resolve();
  730. }
  731. const conference = this.conference;
  732. if (!conference) {
  733. this._switchStreamEffect(effect);
  734. if (this.isVideoTrack()) {
  735. this.containers.forEach(cont => {
  736. RTCUtils.attachMediaStream(cont, this.stream).catch(() => {
  737. logger.error(`Attach media failed for ${this} when trying to set effect.`);
  738. });
  739. });
  740. }
  741. return Promise.resolve();
  742. }
  743. this._setEffectInProgress = true;
  744. return conference._removeLocalTrackFromPc(this)
  745. .then(() => {
  746. this._switchStreamEffect(effect);
  747. if (this.isVideoTrack()) {
  748. this.containers.forEach(cont => {
  749. RTCUtils.attachMediaStream(cont, this.stream).catch(() => {
  750. logger.error(`Attach media failed for ${this} when trying to set effect.`);
  751. });
  752. });
  753. }
  754. return conference._addLocalTrackToPc(this);
  755. })
  756. .then(() => {
  757. this._setEffectInProgress = false;
  758. })
  759. .catch(error => {
  760. // Any error will be not recovarable and will trigger CONFERENCE_FAILED event. But let's try to cleanup
  761. // everyhting related to the effect functionality.
  762. this._setEffectInProgress = false;
  763. this._switchStreamEffect();
  764. logger.error('Failed to switch to the new stream!', error);
  765. throw error;
  766. });
  767. }
  768. /**
  769. * Sets the source name to be used for signaling the jitsi track.
  770. *
  771. * @param {string} name The source name.
  772. */
  773. setSourceName(name) {
  774. this._sourceName = name;
  775. }
  776. /**
  777. * Stops the associated MediaStream.
  778. *
  779. * @returns {void}
  780. */
  781. stopStream() {
  782. /**
  783. * Indicates that we are executing {@link #stopStream} i.e.
  784. * {@link RTCUtils#stopMediaStream} for the <tt>MediaStream</tt>
  785. * associated with this <tt>JitsiTrack</tt> instance.
  786. *
  787. * @private
  788. * @type {boolean}
  789. */
  790. this._stopStreamInProgress = true;
  791. try {
  792. RTCUtils.stopMediaStream(this.stream);
  793. } finally {
  794. this._stopStreamInProgress = false;
  795. }
  796. }
  797. /**
  798. * Creates a text representation of this local track instance.
  799. *
  800. * @return {string}
  801. */
  802. toString() {
  803. return `LocalTrack[${this.rtcId},${this.getType()}]`;
  804. }
  805. /**
  806. * Asynchronously unmutes this track.
  807. *
  808. * @returns {Promise}
  809. */
  810. unmute() {
  811. return this._queueSetMuted(false);
  812. }
  813. }