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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /* global APP */
  2. import {
  3. createTrackMutedEvent,
  4. sendAnalytics
  5. } from '../../analytics';
  6. import { NOTIFICATION_TIMEOUT_TYPE, showErrorNotification, showNotification } from '../../notifications';
  7. import { getCurrentConference } from '../conference';
  8. import { getMultipleVideoSendingSupportFeatureFlag } from '../config';
  9. import { JitsiTrackErrors, JitsiTrackEvents } from '../lib-jitsi-meet';
  10. import { createLocalTrack } from '../lib-jitsi-meet/functions';
  11. import {
  12. CAMERA_FACING_MODE,
  13. MEDIA_TYPE,
  14. setAudioMuted,
  15. setScreenshareMuted,
  16. setVideoMuted,
  17. VIDEO_MUTISM_AUTHORITY,
  18. VIDEO_TYPE
  19. } from '../media';
  20. import { getLocalParticipant } from '../participants';
  21. import { updateSettings } from '../settings';
  22. import {
  23. SET_NO_SRC_DATA_NOTIFICATION_UID,
  24. TOGGLE_SCREENSHARING,
  25. TRACK_ADDED,
  26. TRACK_CREATE_CANCELED,
  27. TRACK_CREATE_ERROR,
  28. TRACK_MUTE_UNMUTE_FAILED,
  29. TRACK_NO_DATA_FROM_SOURCE,
  30. TRACK_REMOVED,
  31. TRACK_STOPPED,
  32. TRACK_UPDATED,
  33. TRACK_UPDATE_LAST_VIDEO_MEDIA_EVENT,
  34. TRACK_WILL_CREATE
  35. } from './actionTypes';
  36. import {
  37. createLocalTracksF,
  38. getLocalTrack,
  39. getLocalTracks,
  40. getLocalVideoTrack,
  41. getTrackByJitsiTrack
  42. } from './functions';
  43. import logger from './logger';
  44. /**
  45. * Add a given local track to the conference.
  46. *
  47. * @param {JitsiLocalTrack} newTrack - The local track to be added to the conference.
  48. * @returns {Function}
  49. */
  50. export function addLocalTrack(newTrack) {
  51. return async (dispatch, getState) => {
  52. const conference = getCurrentConference(getState());
  53. if (conference) {
  54. await conference.addTrack(newTrack);
  55. }
  56. const setMuted = newTrack.isVideoTrack()
  57. ? getMultipleVideoSendingSupportFeatureFlag(getState())
  58. && newTrack.getVideoType() === VIDEO_TYPE.DESKTOP
  59. ? setScreenshareMuted
  60. : setVideoMuted
  61. : setAudioMuted;
  62. const isMuted = newTrack.isMuted();
  63. logger.log(`Adding ${newTrack.getType()} track - ${isMuted ? 'muted' : 'unmuted'}`);
  64. await dispatch(setMuted(isMuted));
  65. return dispatch(_addTracks([ newTrack ]));
  66. };
  67. }
  68. /**
  69. * Requests the creating of the desired media type tracks. Desire is expressed
  70. * by base/media unless the function caller specifies desired media types
  71. * explicitly and thus override base/media. Dispatches a
  72. * {@code createLocalTracksA} action for the desired media types for which there
  73. * are no existing tracks yet.
  74. *
  75. * @returns {Function}
  76. */
  77. export function createDesiredLocalTracks(...desiredTypes) {
  78. return (dispatch, getState) => {
  79. const state = getState();
  80. dispatch(destroyLocalDesktopTrackIfExists());
  81. if (desiredTypes.length === 0) {
  82. const { video } = state['features/base/media'];
  83. // XXX: Always create the audio track early, even if it will be muted.
  84. // This fixes a timing issue when adding the track to the conference which
  85. // manifests primarily on iOS 15.
  86. desiredTypes.push(MEDIA_TYPE.AUDIO);
  87. // XXX When the app is coming into the foreground from the
  88. // background in order to handle a URL, it may realize the new
  89. // background state soon after it has tried to create the local
  90. // tracks requested by the URL. Ignore
  91. // VIDEO_MUTISM_AUTHORITY.BACKGROUND and create the local video
  92. // track if no other VIDEO_MUTISM_AUTHORITY has muted it. The local
  93. // video track will be muted until the app realizes the new
  94. // background state.
  95. // eslint-disable-next-line no-bitwise
  96. (video.muted & ~VIDEO_MUTISM_AUTHORITY.BACKGROUND)
  97. || desiredTypes.push(MEDIA_TYPE.VIDEO);
  98. }
  99. const availableTypes
  100. = getLocalTracks(
  101. state['features/base/tracks'],
  102. /* includePending */ true)
  103. .map(t => t.mediaType);
  104. // We need to create the desired tracks which are not already available.
  105. const createTypes
  106. = desiredTypes.filter(type => availableTypes.indexOf(type) === -1);
  107. createTypes.length
  108. && dispatch(createLocalTracksA({ devices: createTypes }));
  109. };
  110. }
  111. /**
  112. * Request to start capturing local audio and/or video. By default, the user
  113. * facing camera will be selected.
  114. *
  115. * @param {Object} [options] - For info @see JitsiMeetJS.createLocalTracks.
  116. * @returns {Function}
  117. */
  118. export function createLocalTracksA(options = {}) {
  119. return (dispatch, getState) => {
  120. const devices
  121. = options.devices || [ MEDIA_TYPE.AUDIO, MEDIA_TYPE.VIDEO ];
  122. const store = {
  123. dispatch,
  124. getState
  125. };
  126. // The following executes on React Native only at the time of this
  127. // writing. The effort to port Web's createInitialLocalTracksAndConnect
  128. // is significant and that's where the function createLocalTracksF got
  129. // born. I started with the idea a porting so that we could inherit the
  130. // ability to getUserMedia for audio only or video only if getUserMedia
  131. // for audio and video fails. Eventually though, I realized that on
  132. // mobile we do not have combined permission prompts implemented anyway
  133. // (either because there are no such prompts or it does not make sense
  134. // to implement them) and the right thing to do is to ask for each
  135. // device separately.
  136. for (const device of devices) {
  137. if (getLocalTrack(
  138. getState()['features/base/tracks'],
  139. device,
  140. /* includePending */ true)) {
  141. throw new Error(`Local track for ${device} already exists`);
  142. }
  143. const gumProcess
  144. = createLocalTracksF(
  145. {
  146. cameraDeviceId: options.cameraDeviceId,
  147. devices: [ device ],
  148. facingMode:
  149. options.facingMode || CAMERA_FACING_MODE.USER,
  150. micDeviceId: options.micDeviceId
  151. },
  152. store)
  153. .then(
  154. localTracks => {
  155. // Because GUM is called for 1 device (which is actually
  156. // a media type 'audio', 'video', 'screen', etc.) we
  157. // should not get more than one JitsiTrack.
  158. if (localTracks.length !== 1) {
  159. throw new Error(
  160. `Expected exactly 1 track, but was given ${
  161. localTracks.length} tracks for device: ${
  162. device}.`);
  163. }
  164. if (gumProcess.canceled) {
  165. return _disposeTracks(localTracks)
  166. .then(() =>
  167. dispatch(_trackCreateCanceled(device)));
  168. }
  169. return dispatch(trackAdded(localTracks[0]));
  170. },
  171. reason =>
  172. dispatch(
  173. gumProcess.canceled
  174. ? _trackCreateCanceled(device)
  175. : _onCreateLocalTracksRejected(
  176. reason,
  177. device)));
  178. /**
  179. * Cancels the {@code getUserMedia} process represented by this
  180. * {@code Promise}.
  181. *
  182. * @returns {Promise} This {@code Promise} i.e. {@code gumProcess}.
  183. */
  184. gumProcess.cancel = () => {
  185. gumProcess.canceled = true;
  186. return gumProcess;
  187. };
  188. dispatch({
  189. type: TRACK_WILL_CREATE,
  190. track: {
  191. gumProcess,
  192. local: true,
  193. mediaType: device
  194. }
  195. });
  196. }
  197. };
  198. }
  199. /**
  200. * Calls JitsiLocalTrack#dispose() on the given track or on all local tracks (if none are passed) ignoring errors if
  201. * track is already disposed. After that signals tracks to be removed.
  202. *
  203. * @param {JitsiLocalTrack|null} [track] - The local track that needs to be destroyed.
  204. * @returns {Function}
  205. */
  206. export function destroyLocalTracks(track = null) {
  207. if (track) {
  208. return dispatch => {
  209. dispatch(_disposeAndRemoveTracks([ track ]));
  210. };
  211. }
  212. return (dispatch, getState) => {
  213. // First wait until any getUserMedia in progress is settled and then get
  214. // rid of all local tracks.
  215. _cancelGUMProcesses(getState)
  216. .then(() =>
  217. dispatch(
  218. _disposeAndRemoveTracks(
  219. getState()['features/base/tracks']
  220. .filter(t => t.local)
  221. .map(t => t.jitsiTrack))));
  222. };
  223. }
  224. /**
  225. * Signals that the passed JitsiLocalTrack has triggered a no data from source event.
  226. *
  227. * @param {JitsiLocalTrack} track - The track.
  228. * @returns {{
  229. * type: TRACK_NO_DATA_FROM_SOURCE,
  230. * track: Track
  231. * }}
  232. */
  233. export function noDataFromSource(track) {
  234. return {
  235. type: TRACK_NO_DATA_FROM_SOURCE,
  236. track
  237. };
  238. }
  239. /**
  240. * Displays a no data from source video error if needed.
  241. *
  242. * @param {JitsiLocalTrack} jitsiTrack - The track.
  243. * @returns {Function}
  244. */
  245. export function showNoDataFromSourceVideoError(jitsiTrack) {
  246. return async (dispatch, getState) => {
  247. let notificationInfo;
  248. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], jitsiTrack);
  249. if (!track) {
  250. return;
  251. }
  252. if (track.isReceivingData) {
  253. notificationInfo = undefined;
  254. } else {
  255. const notificationAction = await dispatch(showErrorNotification({
  256. descriptionKey: 'dialog.cameraNotSendingData',
  257. titleKey: 'dialog.cameraNotSendingDataTitle'
  258. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  259. notificationInfo = {
  260. uid: notificationAction.uid
  261. };
  262. }
  263. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, notificationInfo));
  264. };
  265. }
  266. /**
  267. * Signals that the local participant is ending screensharing or beginning the screensharing flow.
  268. *
  269. * @param {boolean} enabled - The state to toggle screen sharing to.
  270. * @param {boolean} audioOnly - Only share system audio.
  271. * @param {boolean} ignoreDidHaveVideo - Whether or not to ignore if video was on when sharing started.
  272. * @param {Object} shareOptions - The options to be passed for capturing screenshare.
  273. * @returns {{
  274. * type: TOGGLE_SCREENSHARING,
  275. * on: boolean,
  276. * audioOnly: boolean,
  277. * ignoreDidHaveVideo: boolean,
  278. * shareOptions: Object
  279. * }}
  280. */
  281. export function toggleScreensharing(enabled, audioOnly = false, ignoreDidHaveVideo = false, shareOptions = {}) {
  282. return {
  283. type: TOGGLE_SCREENSHARING,
  284. enabled,
  285. audioOnly,
  286. ignoreDidHaveVideo,
  287. shareOptions
  288. };
  289. }
  290. /**
  291. * Replaces one track with another for one renegotiation instead of invoking
  292. * two renegotiations with a separate removeTrack and addTrack. Disposes the
  293. * removed track as well.
  294. *
  295. * @param {JitsiLocalTrack|null} oldTrack - The track to dispose.
  296. * @param {JitsiLocalTrack|null} newTrack - The track to use instead.
  297. * @param {JitsiConference} [conference] - The conference from which to remove
  298. * and add the tracks. If one is not provided, the conference in the redux store
  299. * will be used.
  300. * @returns {Function}
  301. */
  302. export function replaceLocalTrack(oldTrack, newTrack, conference) {
  303. return async (dispatch, getState) => {
  304. conference
  305. // eslint-disable-next-line no-param-reassign
  306. || (conference = getState()['features/base/conference'].conference);
  307. if (conference) {
  308. await conference.replaceTrack(oldTrack, newTrack);
  309. }
  310. return dispatch(replaceStoredTracks(oldTrack, newTrack));
  311. };
  312. }
  313. /**
  314. * Replaces a stored track with another.
  315. *
  316. * @param {JitsiLocalTrack|null} oldTrack - The track to dispose.
  317. * @param {JitsiLocalTrack|null} newTrack - The track to use instead.
  318. * @returns {Function}
  319. */
  320. function replaceStoredTracks(oldTrack, newTrack) {
  321. return async (dispatch, getState) => {
  322. // We call dispose after doing the replace because dispose will
  323. // try and do a new o/a after the track removes itself. Doing it
  324. // after means the JitsiLocalTrack.conference is already
  325. // cleared, so it won't try and do the o/a.
  326. if (oldTrack) {
  327. await dispatch(_disposeAndRemoveTracks([ oldTrack ]));
  328. }
  329. if (newTrack) {
  330. // The mute state of the new track should be reflected in the app's mute state. For example, if the
  331. // app is currently muted and changing to a new track that is not muted, the app's mute state
  332. // should be falsey. As such, emit a mute event here to set up the app to reflect the track's mute
  333. // state. If this is not done, the current mute state of the app will be reflected on the track,
  334. // not vice-versa.
  335. const setMuted = newTrack.isVideoTrack()
  336. ? getMultipleVideoSendingSupportFeatureFlag(getState())
  337. && newTrack.getVideoType() === VIDEO_TYPE.DESKTOP
  338. ? setScreenshareMuted
  339. : setVideoMuted
  340. : setAudioMuted;
  341. const isMuted = newTrack.isMuted();
  342. sendAnalytics(createTrackMutedEvent(newTrack.getType(), 'track.replaced', isMuted));
  343. logger.log(`Replace ${newTrack.getType()} track - ${isMuted ? 'muted' : 'unmuted'}`);
  344. await dispatch(setMuted(isMuted));
  345. await dispatch(_addTracks([ newTrack ]));
  346. }
  347. };
  348. }
  349. /**
  350. * Create an action for when a new track has been signaled to be added to the
  351. * conference.
  352. *
  353. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  354. * @returns {{ type: TRACK_ADDED, track: Track }}
  355. */
  356. export function trackAdded(track) {
  357. return async (dispatch, getState) => {
  358. track.on(
  359. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  360. () => dispatch(trackMutedChanged(track)));
  361. track.on(
  362. JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED,
  363. type => dispatch(trackVideoTypeChanged(track, type)));
  364. // participantId
  365. const local = track.isLocal();
  366. const mediaType = getMultipleVideoSendingSupportFeatureFlag(getState())
  367. && track.getVideoType() === VIDEO_TYPE.DESKTOP
  368. ? MEDIA_TYPE.SCREENSHARE
  369. : track.getType();
  370. let isReceivingData, noDataFromSourceNotificationInfo, participantId;
  371. if (local) {
  372. // Reset the no data from src notification state when we change the track, as it's context is set
  373. // on a per device basis.
  374. dispatch(setNoSrcDataNotificationUid());
  375. const participant = getLocalParticipant(getState);
  376. if (participant) {
  377. participantId = participant.id;
  378. }
  379. isReceivingData = track.isReceivingData();
  380. track.on(JitsiTrackEvents.NO_DATA_FROM_SOURCE, () => dispatch(noDataFromSource({ jitsiTrack: track })));
  381. if (!isReceivingData) {
  382. if (mediaType === MEDIA_TYPE.AUDIO) {
  383. const notificationAction = await dispatch(showNotification({
  384. descriptionKey: 'dialog.micNotSendingData',
  385. titleKey: 'dialog.micNotSendingDataTitle'
  386. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  387. // Set the notification ID so that other parts of the application know that this was
  388. // displayed in the context of the current device.
  389. // I.E. The no-audio-signal notification shouldn't be displayed if this was already shown.
  390. dispatch(setNoSrcDataNotificationUid(notificationAction.uid));
  391. noDataFromSourceNotificationInfo = { uid: notificationAction.uid };
  392. } else {
  393. const timeout = setTimeout(() => dispatch(
  394. showNoDataFromSourceVideoError(track)),
  395. NOTIFICATION_TIMEOUT_TYPE.MEDIUM);
  396. noDataFromSourceNotificationInfo = { timeout };
  397. }
  398. }
  399. track.on(JitsiTrackEvents.LOCAL_TRACK_STOPPED,
  400. () => dispatch({
  401. type: TRACK_STOPPED,
  402. track: {
  403. jitsiTrack: track
  404. }
  405. }));
  406. } else {
  407. participantId = track.getParticipantId();
  408. isReceivingData = true;
  409. }
  410. return dispatch({
  411. type: TRACK_ADDED,
  412. track: {
  413. jitsiTrack: track,
  414. isReceivingData,
  415. local,
  416. mediaType,
  417. mirror: _shouldMirror(track),
  418. muted: track.isMuted(),
  419. noDataFromSourceNotificationInfo,
  420. participantId,
  421. videoStarted: false,
  422. videoType: track.videoType
  423. }
  424. });
  425. };
  426. }
  427. /**
  428. * Create an action for when a track's muted state has been signaled to be
  429. * changed.
  430. *
  431. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  432. * @returns {{
  433. * type: TRACK_UPDATED,
  434. * track: Track
  435. * }}
  436. */
  437. export function trackMutedChanged(track) {
  438. return {
  439. type: TRACK_UPDATED,
  440. track: {
  441. jitsiTrack: track,
  442. muted: track.isMuted()
  443. }
  444. };
  445. }
  446. /**
  447. * Create an action for when a track's muted state change action has failed. This could happen because of
  448. * {@code getUserMedia} errors during unmute or replace track errors at the peerconnection level.
  449. *
  450. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  451. * @param {boolean} wasMuting - If the operation that failed was a mute operation or an unmute operation.
  452. * @returns {{
  453. * type: TRACK_MUTE_UNMUTE_FAILED,
  454. * track: Track
  455. * }}
  456. */
  457. export function trackMuteUnmuteFailed(track, wasMuting) {
  458. return {
  459. type: TRACK_MUTE_UNMUTE_FAILED,
  460. track,
  461. wasMuting
  462. };
  463. }
  464. /**
  465. * Create an action for when a track's no data from source notification information changes.
  466. *
  467. * @param {JitsiLocalTrack} track - JitsiTrack instance.
  468. * @param {Object} noDataFromSourceNotificationInfo - Information about no data from source notification.
  469. * @returns {{
  470. * type: TRACK_UPDATED,
  471. * track: Track
  472. * }}
  473. */
  474. export function trackNoDataFromSourceNotificationInfoChanged(track, noDataFromSourceNotificationInfo) {
  475. return {
  476. type: TRACK_UPDATED,
  477. track: {
  478. jitsiTrack: track,
  479. noDataFromSourceNotificationInfo
  480. }
  481. };
  482. }
  483. /**
  484. * Create an action for when a track has been signaled for removal from the
  485. * conference.
  486. *
  487. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  488. * @returns {{
  489. * type: TRACK_REMOVED,
  490. * track: Track
  491. * }}
  492. */
  493. export function trackRemoved(track) {
  494. track.removeAllListeners(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  495. track.removeAllListeners(JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED);
  496. track.removeAllListeners(JitsiTrackEvents.NO_DATA_FROM_SOURCE);
  497. return {
  498. type: TRACK_REMOVED,
  499. track: {
  500. jitsiTrack: track
  501. }
  502. };
  503. }
  504. /**
  505. * Signal that track's video started to play.
  506. *
  507. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  508. * @returns {{
  509. * type: TRACK_UPDATED,
  510. * track: Track
  511. * }}
  512. */
  513. export function trackVideoStarted(track) {
  514. return {
  515. type: TRACK_UPDATED,
  516. track: {
  517. jitsiTrack: track,
  518. videoStarted: true
  519. }
  520. };
  521. }
  522. /**
  523. * Create an action for when participant video type changes.
  524. *
  525. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  526. * @param {VIDEO_TYPE|undefined} videoType - Video type.
  527. * @returns {{
  528. * type: TRACK_UPDATED,
  529. * track: Track
  530. * }}
  531. */
  532. export function trackVideoTypeChanged(track, videoType) {
  533. return {
  534. type: TRACK_UPDATED,
  535. track: {
  536. jitsiTrack: track,
  537. videoType
  538. }
  539. };
  540. }
  541. /**
  542. * Create an action for when track streaming status changes.
  543. *
  544. * @param {(JitsiRemoteTrack)} track - JitsiTrack instance.
  545. * @param {string} streamingStatus - The new streaming status of the track.
  546. * @returns {{
  547. * type: TRACK_UPDATED,
  548. * track: Track
  549. * }}
  550. */
  551. export function trackStreamingStatusChanged(track, streamingStatus) {
  552. return {
  553. type: TRACK_UPDATED,
  554. track: {
  555. jitsiTrack: track,
  556. streamingStatus
  557. }
  558. };
  559. }
  560. /**
  561. * Signals passed tracks to be added.
  562. *
  563. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  564. * @private
  565. * @returns {Function}
  566. */
  567. function _addTracks(tracks) {
  568. return dispatch => Promise.all(tracks.map(t => dispatch(trackAdded(t))));
  569. }
  570. /**
  571. * Cancels and waits for any {@code getUserMedia} process/currently in progress
  572. * to complete/settle.
  573. *
  574. * @param {Function} getState - The redux store {@code getState} function used
  575. * to obtain the state.
  576. * @private
  577. * @returns {Promise} - A {@code Promise} resolved once all
  578. * {@code gumProcess.cancel()} {@code Promise}s are settled because all we care
  579. * about here is to be sure that the {@code getUserMedia} callbacks have
  580. * completed (i.e. Returned from the native side).
  581. */
  582. function _cancelGUMProcesses(getState) {
  583. const logError
  584. = error =>
  585. logger.error('gumProcess.cancel failed', JSON.stringify(error));
  586. return Promise.all(
  587. getState()['features/base/tracks']
  588. .filter(t => t.local)
  589. .map(({ gumProcess }) =>
  590. gumProcess && gumProcess.cancel().catch(logError)));
  591. }
  592. /**
  593. * Disposes passed tracks and signals them to be removed.
  594. *
  595. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  596. * @protected
  597. * @returns {Function}
  598. */
  599. export function _disposeAndRemoveTracks(tracks) {
  600. return dispatch =>
  601. _disposeTracks(tracks)
  602. .then(() =>
  603. Promise.all(tracks.map(t => dispatch(trackRemoved(t)))));
  604. }
  605. /**
  606. * Disposes passed tracks.
  607. *
  608. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  609. * @private
  610. * @returns {Promise} - A Promise resolved once {@link JitsiTrack.dispose()} is
  611. * done for every track from the list.
  612. */
  613. function _disposeTracks(tracks) {
  614. return Promise.all(
  615. tracks.map(t =>
  616. t.dispose()
  617. .catch(err => {
  618. // Track might be already disposed so ignore such an error.
  619. // Of course, re-throw any other error(s).
  620. if (err.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  621. throw err;
  622. }
  623. })));
  624. }
  625. /**
  626. * Implements the {@code Promise} rejection handler of
  627. * {@code createLocalTracksA} and {@code createLocalTracksF}.
  628. *
  629. * @param {Object} error - The {@code Promise} rejection reason.
  630. * @param {string} device - The device/{@code MEDIA_TYPE} associated with the
  631. * rejection.
  632. * @private
  633. * @returns {Function}
  634. */
  635. function _onCreateLocalTracksRejected(error, device) {
  636. return dispatch => {
  637. // If permissions are not allowed, alert the user.
  638. dispatch({
  639. type: TRACK_CREATE_ERROR,
  640. permissionDenied: error?.name === 'SecurityError',
  641. trackType: device
  642. });
  643. };
  644. }
  645. /**
  646. * Returns true if the provided {@code JitsiTrack} should be rendered as a
  647. * mirror.
  648. *
  649. * We only want to show a video in mirrored mode when:
  650. * 1) The video source is local, and not remote.
  651. * 2) The video source is a camera, not a desktop (capture).
  652. * 3) The camera is capturing the user, not the environment.
  653. *
  654. * TODO Similar functionality is part of lib-jitsi-meet. This function should be
  655. * removed after https://github.com/jitsi/lib-jitsi-meet/pull/187 is merged.
  656. *
  657. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  658. * @private
  659. * @returns {boolean}
  660. */
  661. function _shouldMirror(track) {
  662. return (
  663. track
  664. && track.isLocal()
  665. && track.isVideoTrack()
  666. // XXX The type of the return value of JitsiLocalTrack's
  667. // getCameraFacingMode happens to be named CAMERA_FACING_MODE as
  668. // well, it's defined by lib-jitsi-meet. Note though that the type
  669. // of the value on the right side of the equality check is defined
  670. // by jitsi-meet. The type definitions are surely compatible today
  671. // but that may not be the case tomorrow.
  672. && track.getCameraFacingMode() === CAMERA_FACING_MODE.USER);
  673. }
  674. /**
  675. * Signals that track create operation for given media track has been canceled.
  676. * Will clean up local track stub from the redux state which holds the
  677. * {@code gumProcess} reference.
  678. *
  679. * @param {MEDIA_TYPE} mediaType - The type of the media for which the track was
  680. * being created.
  681. * @private
  682. * @returns {{
  683. * type,
  684. * trackType: MEDIA_TYPE
  685. * }}
  686. */
  687. function _trackCreateCanceled(mediaType) {
  688. return {
  689. type: TRACK_CREATE_CANCELED,
  690. trackType: mediaType
  691. };
  692. }
  693. /**
  694. * If the local track if of type Desktop, it calls _disposeAndRemoveTracks) on it.
  695. *
  696. * @returns {Function}
  697. */
  698. export function destroyLocalDesktopTrackIfExists() {
  699. return (dispatch, getState) => {
  700. const videoTrack = getLocalVideoTrack(getState()['features/base/tracks']);
  701. const isDesktopTrack = videoTrack && videoTrack.videoType === VIDEO_TYPE.DESKTOP;
  702. if (isDesktopTrack) {
  703. dispatch(_disposeAndRemoveTracks([ videoTrack.jitsiTrack ]));
  704. }
  705. };
  706. }
  707. /**
  708. * Sets UID of the displayed no data from source notification. Used to track
  709. * if the notification was previously displayed in this context.
  710. *
  711. * @param {number} uid - Notification UID.
  712. * @returns {{
  713. * type: SET_NO_AUDIO_SIGNAL_UID,
  714. * uid: number
  715. * }}
  716. */
  717. export function setNoSrcDataNotificationUid(uid) {
  718. return {
  719. type: SET_NO_SRC_DATA_NOTIFICATION_UID,
  720. uid
  721. };
  722. }
  723. /**
  724. * Updates the last media event received for a video track.
  725. *
  726. * @param {JitsiRemoteTrack} track - JitsiTrack instance.
  727. * @param {string} name - The current media event name for the video.
  728. * @returns {{
  729. * type: TRACK_UPDATE_LAST_VIDEO_MEDIA_EVENT,
  730. * track: Track,
  731. * name: string
  732. * }}
  733. */
  734. export function updateLastTrackVideoMediaEvent(track, name) {
  735. return {
  736. type: TRACK_UPDATE_LAST_VIDEO_MEDIA_EVENT,
  737. track,
  738. name
  739. };
  740. }
  741. /**
  742. * Toggles the facingMode constraint on the video stream.
  743. *
  744. * @returns {Function}
  745. */
  746. export function toggleCamera() {
  747. return async (dispatch, getState) => {
  748. const state = getState();
  749. const tracks = state['features/base/tracks'];
  750. const localVideoTrack = getLocalVideoTrack(tracks).jitsiTrack;
  751. const currentFacingMode = localVideoTrack.getCameraFacingMode();
  752. /**
  753. * FIXME: Ideally, we should be dispatching {@code replaceLocalTrack} here,
  754. * but it seems to not trigger the re-rendering of the local video on Chrome;
  755. * could be due to a plan B vs unified plan issue. Therefore, we use the legacy
  756. * method defined in conference.js that manually takes care of updating the local
  757. * video as well.
  758. */
  759. await APP.conference.useVideoStream(null);
  760. const targetFacingMode = currentFacingMode === CAMERA_FACING_MODE.USER
  761. ? CAMERA_FACING_MODE.ENVIRONMENT
  762. : CAMERA_FACING_MODE.USER;
  763. // Update the flipX value so the environment facing camera is not flipped, before the new track is created.
  764. dispatch(updateSettings({ localFlipX: targetFacingMode === CAMERA_FACING_MODE.USER }));
  765. const newVideoTrack = await createLocalTrack('video', null, null, { facingMode: targetFacingMode });
  766. // FIXME: See above.
  767. await APP.conference.useVideoStream(newVideoTrack);
  768. };
  769. }