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.

JitsiLocalTrack.js 30KB

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