Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiLocalTrack.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /* global __filename, Promise */
  2. import CameraFacingMode from '../../service/RTC/CameraFacingMode';
  3. import { getLogger } from 'jitsi-meet-logger';
  4. import JitsiTrack from './JitsiTrack';
  5. import JitsiTrackError from '../../JitsiTrackError';
  6. import * as JitsiTrackErrors from '../../JitsiTrackErrors';
  7. import * as JitsiTrackEvents from '../../JitsiTrackEvents';
  8. import * as MediaType from '../../service/RTC/MediaType';
  9. import RTCBrowserType from './RTCBrowserType';
  10. import RTCEvents from '../../service/RTC/RTCEvents';
  11. import RTCUtils from './RTCUtils';
  12. import Statistics from '../statistics/statistics';
  13. import VideoType from '../../service/RTC/VideoType';
  14. const logger = getLogger(__filename);
  15. /**
  16. * Represents a single media track(either audio or video).
  17. * One <tt>JitsiLocalTrack</tt> corresponds to one WebRTC MediaStreamTrack.
  18. */
  19. export default class JitsiLocalTrack extends JitsiTrack {
  20. /**
  21. * Constructs new JitsiLocalTrack instanse.
  22. * @param {Object} trackInfo
  23. * @param {number} trackInfo.rtcId the ID assigned by the RTC module
  24. * @param trackInfo.stream WebRTC MediaStream, parent of the track
  25. * @param trackInfo.track underlying WebRTC MediaStreamTrack for new
  26. * JitsiRemoteTrack
  27. * @param trackInfo.mediaType the MediaType of the JitsiRemoteTrack
  28. * @param trackInfo.videoType the VideoType of the JitsiRemoteTrack
  29. * @param trackInfo.resolution the video resolution if it's a video track
  30. * @param trackInfo.deviceId the ID of the local device for this track
  31. * @param trackInfo.facingMode the camera facing mode used in getUserMedia
  32. * call
  33. * @param {sourceId} trackInfo.sourceId - The id of the desktop sharing
  34. * source. NOTE: defined for desktop sharing tracks only.
  35. * @constructor
  36. */
  37. constructor(trackInfo) {
  38. const {
  39. rtcId,
  40. stream,
  41. track,
  42. mediaType,
  43. videoType,
  44. resolution,
  45. deviceId,
  46. facingMode,
  47. sourceId,
  48. sourceType
  49. } = trackInfo;
  50. super(
  51. null /* RTC */,
  52. stream,
  53. track,
  54. () => {
  55. if (!this.dontFireRemoveEvent) {
  56. this.emit(JitsiTrackEvents.LOCAL_TRACK_STOPPED);
  57. }
  58. this.dontFireRemoveEvent = false;
  59. } /* inactiveHandler */,
  60. mediaType,
  61. videoType);
  62. /**
  63. * The ID assigned by the RTC module on instance creation.
  64. * @type {number}
  65. */
  66. this.rtcId = rtcId;
  67. this.dontFireRemoveEvent = false;
  68. this.resolution = resolution;
  69. this.sourceId = sourceId;
  70. this.sourceType = sourceType;
  71. // FIXME: currently firefox is ignoring our constraints about
  72. // resolutions so we do not store it, to avoid wrong reporting of local
  73. // track resolution
  74. if (RTCBrowserType.isFirefox()) {
  75. this.resolution = null;
  76. }
  77. this.deviceId = deviceId;
  78. this.storedMSID = this.getMSID();
  79. /**
  80. * The <tt>Promise</tt> which represents the progress of a previously
  81. * queued/scheduled {@link _setMute} (from the point of view of
  82. * {@link _queueSetMute}).
  83. *
  84. * @private
  85. * @type {Promise}
  86. */
  87. this._prevSetMute = Promise.resolve();
  88. /**
  89. * The facing mode of the camera from which this JitsiLocalTrack
  90. * instance was obtained.
  91. *
  92. * @private
  93. * @type {CameraFacingMode|undefined}
  94. */
  95. this._facingMode = facingMode;
  96. // Currently there is no way to know the MediaStreamTrack ended due to
  97. // to device disconnect in Firefox through e.g. "readyState" property.
  98. // Instead we will compare current track's label with device labels from
  99. // enumerateDevices() list.
  100. this._trackEnded = false;
  101. /**
  102. * The value of bytes sent received from the statistics module.
  103. */
  104. this._bytesSent = null;
  105. /**
  106. * Used only for detection of audio problems. We want to check only once
  107. * whether the track is sending bytes ot not. This flag is set to false
  108. * after the check.
  109. */
  110. this._testByteSent = true;
  111. // Currently there is no way to determine with what device track was
  112. // created (until getConstraints() support), however we can associate
  113. // tracks with real devices obtained from enumerateDevices() call as
  114. // soon as it's called.
  115. this._realDeviceId = this.deviceId === '' ? undefined : this.deviceId;
  116. /**
  117. * Indicates that we have called RTCUtils.stopMediaStream for the
  118. * MediaStream related to this JitsiTrack object.
  119. */
  120. this.stopStreamInProgress = false;
  121. /**
  122. * On mute event we are waiting for 3s to check if the stream is going
  123. * to be still muted before firing the event for camera issue detected
  124. * (NO_DATA_FROM_SOURCE).
  125. */
  126. this._noDataFromSourceTimeout = null;
  127. this._onDeviceListChanged = devices => {
  128. this._setRealDeviceIdFromDeviceList(devices);
  129. // Mark track as ended for those browsers that do not support
  130. // "readyState" property. We do not touch tracks created with
  131. // default device ID "".
  132. if (typeof this.getTrack().readyState === 'undefined'
  133. && typeof this._realDeviceId !== 'undefined'
  134. && !devices.find(d => d.deviceId === this._realDeviceId)) {
  135. this._trackEnded = true;
  136. }
  137. };
  138. // Subscribe each created local audio track to
  139. // RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED event. This is different from
  140. // handling this event for remote tracks (which are handled in RTC.js),
  141. // because there might be local tracks not attached to a conference.
  142. if (this.isAudioTrack() && RTCUtils.isDeviceChangeAvailable('output')) {
  143. this._onAudioOutputDeviceChanged = this.setAudioOutput.bind(this);
  144. RTCUtils.addListener(
  145. RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  146. this._onAudioOutputDeviceChanged);
  147. }
  148. RTCUtils.addListener(
  149. RTCEvents.DEVICE_LIST_CHANGED,
  150. this._onDeviceListChanged);
  151. this._initNoDataFromSourceHandlers();
  152. }
  153. /**
  154. * Returns if associated MediaStreamTrack is in the 'ended' state
  155. * @returns {boolean}
  156. */
  157. isEnded() {
  158. return this.getTrack().readyState === 'ended' || this._trackEnded;
  159. }
  160. /**
  161. * Sets handlers to the MediaStreamTrack object that will detect camera
  162. * issues.
  163. */
  164. _initNoDataFromSourceHandlers() {
  165. if (this.isVideoTrack() && this.videoType === VideoType.CAMERA) {
  166. const _onNoDataFromSourceError
  167. = this._onNoDataFromSourceError.bind(this);
  168. this._setHandler('track_mute', () => {
  169. if (this._checkForCameraIssues()) {
  170. const now = window.performance.now();
  171. this._noDataFromSourceTimeout
  172. = setTimeout(_onNoDataFromSourceError, 3000);
  173. this._setHandler('track_unmute', () => {
  174. this._clearNoDataFromSourceMuteResources();
  175. Statistics.sendEventToAll(
  176. `${this.getType()}.track_unmute`,
  177. { value: window.performance.now() - now });
  178. });
  179. }
  180. });
  181. this._setHandler('track_ended', _onNoDataFromSourceError);
  182. }
  183. }
  184. /**
  185. * Clears all timeouts and handlers set on MediaStreamTrack mute event.
  186. * FIXME: Change the name of the method with better one.
  187. */
  188. _clearNoDataFromSourceMuteResources() {
  189. if (this._noDataFromSourceTimeout) {
  190. clearTimeout(this._noDataFromSourceTimeout);
  191. this._noDataFromSourceTimeout = null;
  192. }
  193. this._setHandler('track_unmute', undefined);
  194. }
  195. /**
  196. * Called when potential camera issue is detected. Clears the handlers and
  197. * timeouts set on MediaStreamTrack muted event. Verifies that the camera
  198. * issue persists and fires NO_DATA_FROM_SOURCE event.
  199. */
  200. _onNoDataFromSourceError() {
  201. this._clearNoDataFromSourceMuteResources();
  202. if (this._checkForCameraIssues()) {
  203. this._fireNoDataFromSourceEvent();
  204. }
  205. }
  206. /**
  207. * Fires JitsiTrackEvents.NO_DATA_FROM_SOURCE and logs it to analytics and
  208. * callstats.
  209. */
  210. _fireNoDataFromSourceEvent() {
  211. this.emit(JitsiTrackEvents.NO_DATA_FROM_SOURCE);
  212. const eventName = `${this.getType()}.no_data_from_source`;
  213. Statistics.analytics.sendEvent(eventName);
  214. const log = { name: eventName };
  215. if (this.isAudioTrack()) {
  216. log.isReceivingData = this._isReceivingData();
  217. }
  218. Statistics.sendLog(JSON.stringify(log));
  219. }
  220. /**
  221. * Sets real device ID by comparing track information with device
  222. * information. This is temporary solution until getConstraints() method
  223. * will be implemented in browsers.
  224. * @param {MediaDeviceInfo[]} devices - list of devices obtained from
  225. * enumerateDevices() call
  226. */
  227. _setRealDeviceIdFromDeviceList(devices) {
  228. const track = this.getTrack();
  229. // FIXME for temasys video track, label refers to id not the actual
  230. // device
  231. const device = devices.find(
  232. d => d.kind === `${track.kind}input` && d.label === track.label);
  233. if (device) {
  234. this._realDeviceId = device.deviceId;
  235. }
  236. }
  237. /**
  238. * Sets the stream property of JitsiLocalTrack object and sets all stored
  239. * handlers to it.
  240. * @param {MediaStream} stream the new stream.
  241. */
  242. _setStream(stream) {
  243. super._setStream(stream);
  244. // Store the MSID for video mute/unmute purposes
  245. if (stream) {
  246. this.storedMSID = this.getMSID();
  247. logger.debug(`Setting new MSID: ${this.storedMSID} on ${this}`);
  248. } else {
  249. logger.debug(`Setting 'null' stream on ${this}`);
  250. }
  251. }
  252. /**
  253. * Mutes the track. Will reject the Promise if there is mute/unmute
  254. * operation in progress.
  255. * @returns {Promise}
  256. */
  257. mute() {
  258. return this._queueSetMute(true);
  259. }
  260. /**
  261. * Unmutes the track. Will reject the Promise if there is mute/unmute
  262. * operation in progress.
  263. * @returns {Promise}
  264. */
  265. unmute() {
  266. return this._queueSetMute(false);
  267. }
  268. /**
  269. * Initializes a new Promise to execute {@link _setMute}. May be called
  270. * multiple times in a row and the invocations of {@link _setMute} and,
  271. * consequently, {@link mute} and/or {@link unmute} will be resolved in a
  272. * serialized fashion.
  273. *
  274. * @param {boolean} mute - Whether to mute or unmute this track.
  275. * @returns {Promise}
  276. */
  277. _queueSetMute(mute) {
  278. const setMute = this._setMute.bind(this, mute);
  279. this._prevSetMute = this._prevSetMute.then(setMute, setMute);
  280. return this._prevSetMute;
  281. }
  282. /**
  283. * Mutes / unmutes the track.
  284. *
  285. * @param {boolean} mute - If true the track will be muted. Otherwise the
  286. * track will be unmuted.
  287. * @private
  288. * @returns {Promise}
  289. */
  290. _setMute(mute) {
  291. if (this.isMuted() === mute) {
  292. return Promise.resolve();
  293. }
  294. let promise = Promise.resolve();
  295. this.dontFireRemoveEvent = false;
  296. // A function that will print info about muted status transition
  297. const logMuteInfo = () => logger.info(`Mute ${this}: ${mute}`);
  298. if (this.isAudioTrack()
  299. || this.videoType === VideoType.DESKTOP
  300. || !RTCBrowserType.doesVideoMuteByStreamRemove()) {
  301. logMuteInfo();
  302. if (this.track) {
  303. this.track.enabled = !mute;
  304. }
  305. } else if (mute) {
  306. this.dontFireRemoveEvent = true;
  307. promise = new Promise((resolve, reject) => {
  308. logMuteInfo();
  309. this._removeStreamFromConferenceAsMute(() => {
  310. // FIXME: Maybe here we should set the SRC for the
  311. // containers to something
  312. this._stopMediaStream();
  313. this._setStream(null);
  314. resolve();
  315. }, err => {
  316. reject(err);
  317. });
  318. });
  319. } else {
  320. logMuteInfo();
  321. // This path is only for camera.
  322. const streamOptions = {
  323. cameraDeviceId: this.getDeviceId(),
  324. devices: [ MediaType.VIDEO ],
  325. facingMode: this.getCameraFacingMode()
  326. };
  327. if (this.resolution) {
  328. streamOptions.resolution = this.resolution;
  329. }
  330. promise = RTCUtils.obtainAudioAndVideoPermissions(streamOptions)
  331. .then(streamsInfo => {
  332. const mediaType = this.getType();
  333. const streamInfo = streamsInfo.find(
  334. info => info.mediaType === mediaType);
  335. if (streamInfo) {
  336. this._setStream(streamInfo.stream);
  337. this.track = streamInfo.track;
  338. // This is not good when video type changes after
  339. // unmute, but let's not crash here
  340. if (this.videoType !== streamInfo.videoType) {
  341. logger.warn(
  342. `${this}: video type has changed after unmute!`,
  343. this.videoType, streamInfo.videoType);
  344. this.videoType = streamInfo.videoType;
  345. }
  346. } else {
  347. throw new JitsiTrackError(
  348. JitsiTrackErrors.TRACK_NO_STREAM_FOUND);
  349. }
  350. this.containers = this.containers.map(
  351. cont => RTCUtils.attachMediaStream(cont, this.stream));
  352. return this._addStreamToConferenceAsUnmute();
  353. });
  354. }
  355. return promise
  356. .then(() => this._sendMuteStatus(mute))
  357. .then(() => {
  358. this.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, this);
  359. });
  360. }
  361. /**
  362. * Adds stream to conference and marks it as "unmute" operation.
  363. *
  364. * @private
  365. * @returns {Promise}
  366. */
  367. _addStreamToConferenceAsUnmute() {
  368. if (!this.conference) {
  369. return Promise.resolve();
  370. }
  371. // FIXME it would be good to not included conference as part of this
  372. // process. Only TraceablePeerConnections to which the track is attached
  373. // should care about this action. The TPCs to which the track is not
  374. // attached can sync up when track is re-attached.
  375. // A problem with that is that the "modify sources" queue is part of
  376. // the JingleSessionPC and it would be excluded from the process. One
  377. // solution would be to extract class between TPC and JingleSessionPC
  378. // which would contain the queue and would notify the signaling layer
  379. // when local SSRCs are changed. This would help to separate XMPP from
  380. // the RTC module.
  381. return new Promise((resolve, reject) => {
  382. this.conference._addLocalTrackAsUnmute(this)
  383. .then(resolve, error => reject(new Error(error)));
  384. });
  385. }
  386. /**
  387. * Removes stream from conference and marks it as "mute" operation.
  388. * @param {Function} successCallback will be called on success
  389. * @param {Function} errorCallback will be called on error
  390. * @private
  391. */
  392. _removeStreamFromConferenceAsMute(successCallback, errorCallback) {
  393. if (!this.conference) {
  394. successCallback();
  395. return;
  396. }
  397. this.conference._removeLocalTrackAsMute(this).then(
  398. successCallback,
  399. error => errorCallback(new Error(error)));
  400. }
  401. /**
  402. * Sends mute status for a track to conference if any.
  403. *
  404. * @param {boolean} mute - If track is muted.
  405. * @private
  406. * @returns {Promise}
  407. */
  408. _sendMuteStatus(mute) {
  409. if (!this.conference || !this.conference.room) {
  410. return Promise.resolve();
  411. }
  412. return new Promise(resolve => {
  413. this.conference.room[
  414. this.isAudioTrack()
  415. ? 'setAudioMute'
  416. : 'setVideoMute'](mute, resolve);
  417. });
  418. }
  419. /**
  420. * @inheritdoc
  421. *
  422. * Stops sending the media track. And removes it from the HTML.
  423. * NOTE: Works for local tracks only.
  424. *
  425. * @extends JitsiTrack#dispose
  426. * @returns {Promise}
  427. */
  428. dispose() {
  429. let promise = Promise.resolve();
  430. if (this.conference) {
  431. promise = this.conference.removeTrack(this);
  432. }
  433. if (this.stream) {
  434. this._stopMediaStream();
  435. this.detach();
  436. }
  437. RTCUtils.removeListener(RTCEvents.DEVICE_LIST_CHANGED,
  438. this._onDeviceListChanged);
  439. if (this._onAudioOutputDeviceChanged) {
  440. RTCUtils.removeListener(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  441. this._onAudioOutputDeviceChanged);
  442. }
  443. return promise.then(() => super.dispose());
  444. }
  445. /**
  446. * Returns <tt>true</tt> - if the stream is muted
  447. * and <tt>false</tt> otherwise.
  448. * @returns {boolean} <tt>true</tt> - if the stream is muted
  449. * and <tt>false</tt> otherwise.
  450. */
  451. isMuted() {
  452. // this.stream will be null when we mute local video on Chrome
  453. if (!this.stream) {
  454. return true;
  455. }
  456. if (this.isVideoTrack() && !this.isActive()) {
  457. return true;
  458. }
  459. return !this.track || !this.track.enabled;
  460. }
  461. /**
  462. * Sets the JitsiConference object associated with the track. This is temp
  463. * solution.
  464. * @param conference the JitsiConference object
  465. */
  466. _setConference(conference) {
  467. this.conference = conference;
  468. // We want to keep up with postponed events which should have been fired
  469. // on "attach" call, but for local track we not always have the
  470. // conference before attaching. However this may result in duplicated
  471. // events if they have been triggered on "attach" already.
  472. for (let i = 0; i < this.containers.length; i++) {
  473. this._maybeFireTrackAttached(this.containers[i]);
  474. }
  475. }
  476. /**
  477. * Returns <tt>true</tt>.
  478. * @returns {boolean} <tt>true</tt>
  479. */
  480. isLocal() {
  481. return true;
  482. }
  483. /**
  484. * Returns device id associated with track.
  485. * @returns {string}
  486. */
  487. getDeviceId() {
  488. return this._realDeviceId || this.deviceId;
  489. }
  490. /**
  491. * Returns the participant id which owns the track.
  492. * @returns {string} the id of the participants. It corresponds to the
  493. * Colibri endpoint id/MUC nickname in case of Jitsi-meet.
  494. */
  495. getParticipantId() {
  496. return this.conference && this.conference.myUserId();
  497. }
  498. /**
  499. * Sets the value of bytes sent statistic.
  500. * @param {TraceablePeerConnection} tpc the source of the "bytes sent" stat
  501. * @param {number} bytesSent the new value
  502. * NOTE: used only for audio tracks to detect audio issues.
  503. */
  504. _setByteSent(tpc, bytesSent) {
  505. this._bytesSent = bytesSent;
  506. const iceConnectionState = tpc.getConnectionState();
  507. if (this._testByteSent && iceConnectionState === 'connected') {
  508. setTimeout(() => {
  509. if (this._bytesSent <= 0) {
  510. // FIXME: Remove ${this}
  511. logger.warn(`${this} 'bytes sent' <= 0: \
  512. ${this._bytesSent}`);
  513. // we are not receiving anything from the microphone
  514. this._fireNoDataFromSourceEvent();
  515. }
  516. }, 3000);
  517. this._testByteSent = false;
  518. }
  519. }
  520. /**
  521. * Returns facing mode for video track from camera. For other cases (e.g.
  522. * audio track or 'desktop' video track) returns undefined.
  523. *
  524. * @returns {CameraFacingMode|undefined}
  525. */
  526. getCameraFacingMode() {
  527. if (this.isVideoTrack() && this.videoType === VideoType.CAMERA) {
  528. // MediaStreamTrack#getSettings() is not implemented in many
  529. // browsers, so we need feature checking here. Progress on the
  530. // respective browser's implementation can be tracked at
  531. // https://bugs.chromium.org/p/webrtc/issues/detail?id=2481 for
  532. // Chromium and https://bugzilla.mozilla.org/show_bug.cgi?id=1213517
  533. // for Firefox. Even if a browser implements getSettings() already,
  534. // it might still not return anything for 'facingMode'.
  535. let trackSettings;
  536. try {
  537. trackSettings = this.track.getSettings();
  538. } catch (e) {
  539. // XXX React-native-webrtc, for example, defines
  540. // MediaStreamTrack#getSettings() but the implementation throws
  541. // a "Not implemented" Error.
  542. }
  543. if (trackSettings && 'facingMode' in trackSettings) {
  544. return trackSettings.facingMode;
  545. }
  546. if (typeof this._facingMode !== 'undefined') {
  547. return this._facingMode;
  548. }
  549. // In most cases we are showing a webcam. So if we've gotten here,
  550. // it should be relatively safe to assume that we are probably
  551. // showing the user-facing camera.
  552. return CameraFacingMode.USER;
  553. }
  554. return undefined;
  555. }
  556. /**
  557. * Stops the associated MediaStream.
  558. */
  559. _stopMediaStream() {
  560. this.stopStreamInProgress = true;
  561. RTCUtils.stopMediaStream(this.stream);
  562. this.stopStreamInProgress = false;
  563. }
  564. /**
  565. * Switches the camera facing mode if the WebRTC implementation supports the
  566. * custom MediaStreamTrack._switchCamera method. Currently, the method in
  567. * question is implemented in react-native-webrtc only. When such a WebRTC
  568. * implementation is executing, the method is the preferred way to switch
  569. * between the front/user-facing and the back/environment-facing cameras
  570. * because it will likely be (as is the case of react-native-webrtc)
  571. * noticeably faster that creating a new MediaStreamTrack via a new
  572. * getUserMedia call with the switched facingMode constraint value.
  573. * Moreover, the approach with a new getUserMedia call may not even work:
  574. * WebRTC on Android and iOS is either very slow to open the camera a second
  575. * time or plainly freezes attempting to do that.
  576. */
  577. _switchCamera() {
  578. if (this.isVideoTrack()
  579. && this.videoType === VideoType.CAMERA
  580. && typeof this.track._switchCamera === 'function') {
  581. this.track._switchCamera();
  582. this._facingMode
  583. = this._facingMode === CameraFacingMode.ENVIRONMENT
  584. ? CameraFacingMode.USER
  585. : CameraFacingMode.ENVIRONMENT;
  586. }
  587. }
  588. /**
  589. * Detects camera issues on ended and mute events from MediaStreamTrack.
  590. * @returns {boolean} true if an issue is detected and false otherwise
  591. */
  592. _checkForCameraIssues() {
  593. if (!this.isVideoTrack() || this.stopStreamInProgress
  594. || this.videoType === VideoType.DESKTOP) {
  595. return false;
  596. }
  597. return !this._isReceivingData();
  598. }
  599. /**
  600. * Checks whether the attached MediaStream is receiving data from source or
  601. * not. If the stream property is null(because of mute or another reason)
  602. * this method will return false.
  603. * NOTE: This method doesn't indicate problem with the streams directly.
  604. * For example in case of video mute the method will return false or if the
  605. * user has disposed the track.
  606. * @returns {boolean} true if the stream is receiving data and false
  607. * this otherwise.
  608. */
  609. _isReceivingData() {
  610. if (!this.stream) {
  611. return false;
  612. }
  613. // In older version of the spec there is no muted property and
  614. // readyState can have value muted. In the latest versions
  615. // readyState can have values "live" and "ended" and there is
  616. // muted boolean property. If the stream is muted that means that
  617. // we aren't receiving any data from the source. We want to notify
  618. // the users for error if the stream is muted or ended on it's
  619. // creation.
  620. return this.stream.getTracks().some(track =>
  621. (!('readyState' in track) || track.readyState === 'live')
  622. && (!('muted' in track) || track.muted !== true));
  623. }
  624. /**
  625. * Creates a text representation of this local track instance.
  626. * @return {string}
  627. */
  628. toString() {
  629. return `LocalTrack[${this.rtcId},${this.getType()}]`;
  630. }
  631. }