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.

actions.js 28KB

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