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 24KB

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