Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JitsiLocalTrack.js 32KB

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