Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

JitsiLocalTrack.js 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. if (this.isVideoTrack()) {
  354. this.containers.forEach(cont => RTCUtils.attachMediaStream(cont, this.stream));
  355. }
  356. return Promise.resolve();
  357. }
  358. this._setEffectInProgress = true;
  359. if (browser.usesUnifiedPlan()) {
  360. this._switchStreamEffect(effect);
  361. if (this.isVideoTrack()) {
  362. this.containers.forEach(cont => RTCUtils.attachMediaStream(cont, this.stream));
  363. }
  364. return conference.replaceTrack(this, this)
  365. .then(() => {
  366. this._setEffectInProgress = false;
  367. })
  368. .catch(error => {
  369. this._setEffectInProgress = false;
  370. this._switchStreamEffect();
  371. logger.error('Failed to switch to the new stream!', error);
  372. throw error;
  373. });
  374. }
  375. // TODO: Create new JingleSessionPC method for replacing a stream in JitsiLocalTrack without offer answer.
  376. return conference.removeTrack(this)
  377. .then(() => {
  378. this._switchStreamEffect(effect);
  379. if (this.isVideoTrack()) {
  380. this.containers.forEach(cont => RTCUtils.attachMediaStream(cont, this.stream));
  381. }
  382. return conference.addTrack(this);
  383. })
  384. .then(() => {
  385. this._setEffectInProgress = false;
  386. })
  387. .catch(error => {
  388. // Any error will be not recovarable and will trigger CONFERENCE_FAILED event. But let's try to cleanup
  389. // everyhting related to the effect functionality.
  390. this._setEffectInProgress = false;
  391. this._switchStreamEffect();
  392. logger.error('Failed to switch to the new stream!', error);
  393. throw error;
  394. });
  395. }
  396. /**
  397. * Asynchronously mutes this track.
  398. *
  399. * @returns {Promise}
  400. */
  401. mute() {
  402. return this._queueSetMuted(true);
  403. }
  404. /**
  405. * Asynchronously unmutes this track.
  406. *
  407. * @returns {Promise}
  408. */
  409. unmute() {
  410. return this._queueSetMuted(false);
  411. }
  412. /**
  413. * Initializes a new Promise to execute {@link #_setMuted}. May be called
  414. * multiple times in a row and the invocations of {@link #_setMuted} and,
  415. * consequently, {@link #mute} and/or {@link #unmute} will be resolved in a
  416. * serialized fashion.
  417. *
  418. * @param {boolean} muted - The value to invoke <tt>_setMuted</tt> with.
  419. * @returns {Promise}
  420. */
  421. _queueSetMuted(muted) {
  422. const setMuted = this._setMuted.bind(this, muted);
  423. this._prevSetMuted = this._prevSetMuted.then(setMuted, setMuted);
  424. return this._prevSetMuted;
  425. }
  426. /**
  427. * Mutes / unmutes this track.
  428. *
  429. * @param {boolean} muted - If <tt>true</tt>, this track will be muted;
  430. * otherwise, this track will be unmuted.
  431. * @private
  432. * @returns {Promise}
  433. */
  434. _setMuted(muted) {
  435. if (this.isMuted() === muted) {
  436. return Promise.resolve();
  437. }
  438. if (this.disposed) {
  439. return Promise.reject(new JitsiTrackError(TRACK_IS_DISPOSED));
  440. }
  441. let promise = Promise.resolve();
  442. // A function that will print info about muted status transition
  443. const logMuteInfo = () => logger.info(`Mute ${this}: ${muted}`);
  444. if (this.isAudioTrack()
  445. || this.videoType === VideoType.DESKTOP
  446. || !browser.doesVideoMuteByStreamRemove()) {
  447. logMuteInfo();
  448. // If we have a stream effect that implements its own mute functionality, prioritize it before
  449. // normal mute e.g. the stream effect that implements system audio sharing has a custom
  450. // mute state in which if the user mutes, system audio still has to go through.
  451. if (this._streamEffect && this._streamEffect.setMuted) {
  452. this._streamEffect.setMuted(muted);
  453. } else if (this.track) {
  454. this.track.enabled = !muted;
  455. }
  456. } else if (muted) {
  457. promise = new Promise((resolve, reject) => {
  458. logMuteInfo();
  459. this._removeStreamFromConferenceAsMute(
  460. () => {
  461. if (this._streamEffect) {
  462. this._stopStreamEffect();
  463. }
  464. // FIXME: Maybe here we should set the SRC for the
  465. // containers to something
  466. // We don't want any events to be fired on this stream
  467. this._unregisterHandlers();
  468. this.stopStream();
  469. this._setStream(null);
  470. resolve();
  471. },
  472. reject);
  473. });
  474. } else {
  475. logMuteInfo();
  476. // This path is only for camera.
  477. const streamOptions = {
  478. cameraDeviceId: this.getDeviceId(),
  479. devices: [ MediaType.VIDEO ],
  480. effects: this._streamEffect ? [ this._streamEffect ] : [],
  481. facingMode: this.getCameraFacingMode()
  482. };
  483. if (browser.usesNewGumFlow()) {
  484. promise
  485. = RTCUtils.newObtainAudioAndVideoPermissions(Object.assign(
  486. {},
  487. streamOptions,
  488. { constraints: { video: this._constraints } }));
  489. } else {
  490. if (this.resolution) {
  491. streamOptions.resolution = this.resolution;
  492. }
  493. promise
  494. = RTCUtils.obtainAudioAndVideoPermissions(streamOptions);
  495. }
  496. promise = promise.then(streamsInfo => {
  497. // The track kind for presenter track is video as well.
  498. const mediaType = this.getType() === MediaType.PRESENTER ? MediaType.VIDEO : this.getType();
  499. const streamInfo
  500. = browser.usesNewGumFlow()
  501. ? streamsInfo.find(
  502. info => info.track.kind === mediaType)
  503. : streamsInfo.find(
  504. info => info.mediaType === mediaType);
  505. if (streamInfo) {
  506. this._setStream(streamInfo.stream);
  507. this.track = streamInfo.track;
  508. // This is not good when video type changes after
  509. // unmute, but let's not crash here
  510. if (this.videoType !== streamInfo.videoType) {
  511. logger.warn(
  512. `${this}: video type has changed after unmute!`,
  513. this.videoType, streamInfo.videoType);
  514. this.videoType = streamInfo.videoType;
  515. }
  516. } else {
  517. throw new JitsiTrackError(TRACK_NO_STREAM_FOUND);
  518. }
  519. if (this._streamEffect) {
  520. this._startStreamEffect(this._streamEffect);
  521. }
  522. this.containers.map(
  523. cont => RTCUtils.attachMediaStream(cont, this.stream));
  524. return this._addStreamToConferenceAsUnmute();
  525. });
  526. }
  527. return promise
  528. .then(() => this._sendMuteStatus(muted))
  529. .then(() => this.emit(TRACK_MUTE_CHANGED, this));
  530. }
  531. /**
  532. * Adds stream to conference and marks it as "unmute" operation.
  533. *
  534. * @private
  535. * @returns {Promise}
  536. */
  537. _addStreamToConferenceAsUnmute() {
  538. if (!this.conference) {
  539. return Promise.resolve();
  540. }
  541. // FIXME it would be good to not included conference as part of this
  542. // process. Only TraceablePeerConnections to which the track is attached
  543. // should care about this action. The TPCs to which the track is not
  544. // attached can sync up when track is re-attached.
  545. // A problem with that is that the "modify sources" queue is part of
  546. // the JingleSessionPC and it would be excluded from the process. One
  547. // solution would be to extract class between TPC and JingleSessionPC
  548. // which would contain the queue and would notify the signaling layer
  549. // when local SSRCs are changed. This would help to separate XMPP from
  550. // the RTC module.
  551. return new Promise((resolve, reject) => {
  552. this.conference._addLocalTrackAsUnmute(this)
  553. .then(resolve, error => reject(new Error(error)));
  554. });
  555. }
  556. /**
  557. * Removes stream from conference and marks it as "mute" operation.
  558. *
  559. * @param {Function} successCallback will be called on success
  560. * @param {Function} errorCallback will be called on error
  561. * @private
  562. */
  563. _removeStreamFromConferenceAsMute(successCallback, errorCallback) {
  564. if (!this.conference) {
  565. successCallback();
  566. return;
  567. }
  568. this.conference._removeLocalTrackAsMute(this).then(
  569. successCallback,
  570. error => errorCallback(new Error(error)));
  571. }
  572. /**
  573. * Sends mute status for a track to conference if any.
  574. *
  575. * @param {boolean} mute - If track is muted.
  576. * @private
  577. * @returns {Promise}
  578. */
  579. _sendMuteStatus(mute) {
  580. if (!this.conference || !this.conference.room) {
  581. return Promise.resolve();
  582. }
  583. return new Promise(resolve => {
  584. this.conference.room[
  585. this.isAudioTrack()
  586. ? 'setAudioMute'
  587. : 'setVideoMute'](mute, resolve);
  588. });
  589. }
  590. /**
  591. * @inheritdoc
  592. *
  593. * Stops sending the media track. And removes it from the HTML.
  594. * NOTE: Works for local tracks only.
  595. *
  596. * @extends JitsiTrack#dispose
  597. * @returns {Promise}
  598. */
  599. dispose() {
  600. this._switchStreamEffect();
  601. let promise = Promise.resolve();
  602. if (this.conference) {
  603. promise = this.conference.removeTrack(this);
  604. }
  605. if (this.stream) {
  606. this.stopStream();
  607. this.detach();
  608. }
  609. RTCUtils.removeListener(RTCEvents.DEVICE_LIST_WILL_CHANGE, this._onDeviceListWillChange);
  610. if (this._onAudioOutputDeviceChanged) {
  611. RTCUtils.removeListener(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  612. this._onAudioOutputDeviceChanged);
  613. }
  614. return promise.then(() => super.dispose());
  615. }
  616. /**
  617. * Returns <tt>true</tt> - if the stream is muted and <tt>false</tt>
  618. * otherwise.
  619. *
  620. * @returns {boolean} <tt>true</tt> - if the stream is muted and
  621. * <tt>false</tt> otherwise.
  622. */
  623. isMuted() {
  624. // this.stream will be null when we mute local video on Chrome
  625. if (!this.stream) {
  626. return true;
  627. }
  628. if (this.isVideoTrack() && !this.isActive()) {
  629. return true;
  630. }
  631. // If currently used stream effect has its own muted state, use that.
  632. if (this._streamEffect && this._streamEffect.isMuted) {
  633. return this._streamEffect.isMuted();
  634. }
  635. return !this.track || !this.track.enabled;
  636. }
  637. /**
  638. * Sets the JitsiConference object associated with the track. This is temp
  639. * solution.
  640. *
  641. * @param conference the JitsiConference object
  642. */
  643. _setConference(conference) {
  644. this.conference = conference;
  645. // We want to keep up with postponed events which should have been fired
  646. // on "attach" call, but for local track we not always have the
  647. // conference before attaching. However this may result in duplicated
  648. // events if they have been triggered on "attach" already.
  649. for (let i = 0; i < this.containers.length; i++) {
  650. this._maybeFireTrackAttached(this.containers[i]);
  651. }
  652. }
  653. /**
  654. * Returns <tt>true</tt>.
  655. *
  656. * @returns {boolean} <tt>true</tt>
  657. */
  658. isLocal() {
  659. return true;
  660. }
  661. /**
  662. * Returns device id associated with track.
  663. *
  664. * @returns {string}
  665. */
  666. getDeviceId() {
  667. return this._realDeviceId || this.deviceId;
  668. }
  669. /**
  670. * Returns the participant id which owns the track.
  671. *
  672. * @returns {string} the id of the participants. It corresponds to the
  673. * Colibri endpoint id/MUC nickname in case of Jitsi-meet.
  674. */
  675. getParticipantId() {
  676. return this.conference && this.conference.myUserId();
  677. }
  678. /**
  679. * Handles bytes sent statistics.
  680. *
  681. * @param {TraceablePeerConnection} tpc the source of the "bytes sent" stat
  682. * @param {number} bytesSent the new value
  683. * NOTE: used only for audio tracks to detect audio issues.
  684. */
  685. _onByteSentStatsReceived(tpc, bytesSent) {
  686. if (bytesSent > 0) {
  687. this._hasSentData = true;
  688. }
  689. const iceConnectionState = tpc.getConnectionState();
  690. if (this._testDataSent && iceConnectionState === 'connected') {
  691. setTimeout(() => {
  692. if (!this._hasSentData) {
  693. logger.warn(`${this} 'bytes sent' <= 0: \
  694. ${bytesSent}`);
  695. Statistics.analytics.sendEvent(NO_BYTES_SENT, { 'media_type': this.getType() });
  696. }
  697. }, 3000);
  698. this._testDataSent = false;
  699. }
  700. }
  701. /**
  702. * Returns facing mode for video track from camera. For other cases (e.g.
  703. * audio track or 'desktop' video track) returns undefined.
  704. *
  705. * @returns {CameraFacingMode|undefined}
  706. */
  707. getCameraFacingMode() {
  708. if (this.isVideoTrack() && this.videoType === VideoType.CAMERA) {
  709. // MediaStreamTrack#getSettings() is not implemented in many
  710. // browsers, so we need feature checking here. Progress on the
  711. // respective browser's implementation can be tracked at
  712. // https://bugs.chromium.org/p/webrtc/issues/detail?id=2481 for
  713. // Chromium and https://bugzilla.mozilla.org/show_bug.cgi?id=1213517
  714. // for Firefox. Even if a browser implements getSettings() already,
  715. // it might still not return anything for 'facingMode'.
  716. let trackSettings;
  717. try {
  718. trackSettings = this.track.getSettings();
  719. } catch (e) {
  720. // XXX React-native-webrtc, for example, defines
  721. // MediaStreamTrack#getSettings() but the implementation throws
  722. // a "Not implemented" Error.
  723. }
  724. if (trackSettings && 'facingMode' in trackSettings) {
  725. return trackSettings.facingMode;
  726. }
  727. if (typeof this._facingMode !== 'undefined') {
  728. return this._facingMode;
  729. }
  730. // In most cases we are showing a webcam. So if we've gotten here,
  731. // it should be relatively safe to assume that we are probably
  732. // showing the user-facing camera.
  733. return CameraFacingMode.USER;
  734. }
  735. return undefined;
  736. }
  737. /**
  738. * Stops the associated MediaStream.
  739. */
  740. stopStream() {
  741. /**
  742. * Indicates that we are executing {@link #stopStream} i.e.
  743. * {@link RTCUtils#stopMediaStream} for the <tt>MediaStream</tt>
  744. * associated with this <tt>JitsiTrack</tt> instance.
  745. *
  746. * @private
  747. * @type {boolean}
  748. */
  749. this._stopStreamInProgress = true;
  750. try {
  751. RTCUtils.stopMediaStream(this.stream);
  752. } finally {
  753. this._stopStreamInProgress = false;
  754. }
  755. }
  756. /**
  757. * Switches the camera facing mode if the WebRTC implementation supports the
  758. * custom MediaStreamTrack._switchCamera method. Currently, the method in
  759. * question is implemented in react-native-webrtc only. When such a WebRTC
  760. * implementation is executing, the method is the preferred way to switch
  761. * between the front/user-facing and the back/environment-facing cameras
  762. * because it will likely be (as is the case of react-native-webrtc)
  763. * noticeably faster that creating a new MediaStreamTrack via a new
  764. * getUserMedia call with the switched facingMode constraint value.
  765. * Moreover, the approach with a new getUserMedia call may not even work:
  766. * WebRTC on Android and iOS is either very slow to open the camera a second
  767. * time or plainly freezes attempting to do that.
  768. */
  769. _switchCamera() {
  770. if (this.isVideoTrack()
  771. && this.videoType === VideoType.CAMERA
  772. && typeof this.track._switchCamera === 'function') {
  773. this.track._switchCamera();
  774. this._facingMode
  775. = this._facingMode === CameraFacingMode.ENVIRONMENT
  776. ? CameraFacingMode.USER
  777. : CameraFacingMode.ENVIRONMENT;
  778. }
  779. }
  780. /**
  781. * Checks whether the attached MediaStream is receiving data from source or
  782. * not. If the stream property is null(because of mute or another reason)
  783. * this method will return false.
  784. * NOTE: This method doesn't indicate problem with the streams directly.
  785. * For example in case of video mute the method will return false or if the
  786. * user has disposed the track.
  787. *
  788. * @returns {boolean} true if the stream is receiving data and false
  789. * this otherwise.
  790. */
  791. isReceivingData() {
  792. if (this.isVideoTrack()
  793. && (this.isMuted() || this._stopStreamInProgress || this.videoType === VideoType.DESKTOP)) {
  794. return true;
  795. }
  796. if (!this.stream) {
  797. return false;
  798. }
  799. // In older version of the spec there is no muted property and
  800. // readyState can have value muted. In the latest versions
  801. // readyState can have values "live" and "ended" and there is
  802. // muted boolean property. If the stream is muted that means that
  803. // we aren't receiving any data from the source. We want to notify
  804. // the users for error if the stream is muted or ended on it's
  805. // creation.
  806. // For video blur enabled use the original video stream
  807. const stream = this._effectEnabled ? this._originalStream : this.stream;
  808. return stream.getTracks().some(track =>
  809. (!('readyState' in track) || track.readyState === 'live')
  810. && (!('muted' in track) || track.muted !== true));
  811. }
  812. /**
  813. * Creates a text representation of this local track instance.
  814. *
  815. * @return {string}
  816. */
  817. toString() {
  818. return `LocalTrack[${this.rtcId},${this.getType()}]`;
  819. }
  820. }