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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import {
  2. createTrackMutedEvent,
  3. sendAnalytics
  4. } from '../../analytics';
  5. import { JitsiTrackErrors, JitsiTrackEvents } from '../lib-jitsi-meet';
  6. import {
  7. CAMERA_FACING_MODE,
  8. MEDIA_TYPE,
  9. setAudioMuted,
  10. setVideoMuted
  11. } from '../media';
  12. import { getLocalParticipant } from '../participants';
  13. import {
  14. TOGGLE_SCREENSHARING,
  15. TRACK_ADDED,
  16. TRACK_CREATE_CANCELED,
  17. TRACK_CREATE_ERROR,
  18. TRACK_REMOVED,
  19. TRACK_UPDATED,
  20. TRACK_WILL_CREATE
  21. } from './actionTypes';
  22. import { createLocalTracksF, getLocalTrack, getLocalTracks } from './functions';
  23. const logger = require('jitsi-meet-logger').getLogger(__filename);
  24. /**
  25. * Requests the creating of the desired media type tracks. Desire is expressed
  26. * by base/media unless the function caller specifies desired media types
  27. * explicitly and thus override base/media. Dispatches a
  28. * {@code createLocalTracksA} action for the desired media types for which there
  29. * are no existing tracks yet.
  30. *
  31. * @returns {Function}
  32. */
  33. export function createDesiredLocalTracks(...desiredTypes) {
  34. return (dispatch, getState) => {
  35. const state = getState();
  36. if (desiredTypes.length === 0) {
  37. const { audio, video } = state['features/base/media'];
  38. audio.muted || desiredTypes.push(MEDIA_TYPE.AUDIO);
  39. video.muted || desiredTypes.push(MEDIA_TYPE.VIDEO);
  40. }
  41. const availableTypes
  42. = getLocalTracks(
  43. state['features/base/tracks'],
  44. /* includePending */ true)
  45. .map(t => t.mediaType);
  46. // We need to create the desired tracks which are not already available.
  47. const createTypes
  48. = desiredTypes.filter(type => availableTypes.indexOf(type) === -1);
  49. createTypes.length
  50. && dispatch(createLocalTracksA({ devices: createTypes }));
  51. };
  52. }
  53. /**
  54. * Request to start capturing local audio and/or video. By default, the user
  55. * facing camera will be selected.
  56. *
  57. * @param {Object} [options] - For info @see JitsiMeetJS.createLocalTracks.
  58. * @returns {Function}
  59. */
  60. export function createLocalTracksA(options = {}) {
  61. return (dispatch, getState) => {
  62. const devices
  63. = options.devices || [ MEDIA_TYPE.AUDIO, MEDIA_TYPE.VIDEO ];
  64. const store = {
  65. dispatch,
  66. getState
  67. };
  68. // The following executes on React Native only at the time of this
  69. // writing. The effort to port Web's createInitialLocalTracksAndConnect
  70. // is significant and that's where the function createLocalTracksF got
  71. // born. I started with the idea a porting so that we could inherit the
  72. // ability to getUserMedia for audio only or video only if getUserMedia
  73. // for audio and video fails. Eventually though, I realized that on
  74. // mobile we do not have combined permission prompts implemented anyway
  75. // (either because there are no such prompts or it does not make sense
  76. // to implement them) and the right thing to do is to ask for each
  77. // device separately.
  78. for (const device of devices) {
  79. if (getLocalTrack(
  80. getState()['features/base/tracks'],
  81. device,
  82. /* includePending */ true)) {
  83. throw new Error(`Local track for ${device} already exists`);
  84. }
  85. const gumProcess
  86. = createLocalTracksF(
  87. {
  88. cameraDeviceId: options.cameraDeviceId,
  89. devices: [ device ],
  90. facingMode:
  91. options.facingMode || CAMERA_FACING_MODE.USER,
  92. micDeviceId: options.micDeviceId
  93. },
  94. /* firePermissionPromptIsShownEvent */ false,
  95. store)
  96. .then(
  97. localTracks => {
  98. // Because GUM is called for 1 device (which is actually
  99. // a media type 'audio', 'video', 'screen', etc.) we
  100. // should not get more than one JitsiTrack.
  101. if (localTracks.length !== 1) {
  102. throw new Error(
  103. `Expected exactly 1 track, but was given ${
  104. localTracks.length} tracks for device: ${
  105. device}.`);
  106. }
  107. if (gumProcess.canceled) {
  108. return _disposeTracks(localTracks)
  109. .then(() =>
  110. dispatch(_trackCreateCanceled(device)));
  111. }
  112. return dispatch(trackAdded(localTracks[0]));
  113. },
  114. reason =>
  115. dispatch(
  116. gumProcess.canceled
  117. ? _trackCreateCanceled(device)
  118. : _onCreateLocalTracksRejected(
  119. reason,
  120. device)));
  121. /**
  122. * Cancels the {@code getUserMedia} process represented by this
  123. * {@code Promise}.
  124. *
  125. * @returns {Promise} This {@code Promise} i.e. {@code gumProcess}.
  126. */
  127. gumProcess.cancel = () => {
  128. gumProcess.canceled = true;
  129. return gumProcess;
  130. };
  131. dispatch({
  132. type: TRACK_WILL_CREATE,
  133. track: {
  134. gumProcess,
  135. local: true,
  136. mediaType: device
  137. }
  138. });
  139. }
  140. };
  141. }
  142. /**
  143. * Calls JitsiLocalTrack#dispose() on all local tracks ignoring errors when
  144. * track is already disposed. After that signals tracks to be removed.
  145. *
  146. * @returns {Function}
  147. */
  148. export function destroyLocalTracks() {
  149. return (dispatch, getState) => {
  150. // First wait until any getUserMedia in progress is settled and then get
  151. // rid of all local tracks.
  152. _cancelGUMProcesses(getState)
  153. .then(() =>
  154. dispatch(
  155. _disposeAndRemoveTracks(
  156. getState()['features/base/tracks']
  157. .filter(t => t.local)
  158. .map(t => t.jitsiTrack))));
  159. };
  160. }
  161. /**
  162. * Signals that the local participant is ending screensharing or beginning the
  163. * screensharing flow.
  164. *
  165. * @returns {{
  166. * type: TOGGLE_SCREENSHARING,
  167. * }}
  168. */
  169. export function toggleScreensharing() {
  170. return {
  171. type: TOGGLE_SCREENSHARING
  172. };
  173. }
  174. /**
  175. * Replaces one track with another for one renegotiation instead of invoking
  176. * two renegotiations with a separate removeTrack and addTrack. Disposes the
  177. * removed track as well.
  178. *
  179. * @param {JitsiLocalTrack|null} oldTrack - The track to dispose.
  180. * @param {JitsiLocalTrack|null} newTrack - The track to use instead.
  181. * @param {JitsiConference} [conference] - The conference from which to remove
  182. * and add the tracks. If one is not provided, the conference in the redux store
  183. * will be used.
  184. * @returns {Function}
  185. */
  186. export function replaceLocalTrack(oldTrack, newTrack, conference) {
  187. return (dispatch, getState) => {
  188. conference
  189. // eslint-disable-next-line no-param-reassign
  190. || (conference = getState()['features/base/conference'].conference);
  191. return conference.replaceTrack(oldTrack, newTrack)
  192. .then(() => {
  193. // We call dispose after doing the replace because dispose will
  194. // try and do a new o/a after the track removes itself. Doing it
  195. // after means the JitsiLocalTrack.conference is already
  196. // cleared, so it won't try and do the o/a.
  197. const disposePromise
  198. = oldTrack
  199. ? dispatch(_disposeAndRemoveTracks([ oldTrack ]))
  200. : Promise.resolve();
  201. return disposePromise
  202. .then(() => {
  203. if (newTrack) {
  204. // The mute state of the new track should be
  205. // reflected in the app's mute state. For example,
  206. // if the app is currently muted and changing to a
  207. // new track that is not muted, the app's mute
  208. // state should be falsey. As such, emit a mute
  209. // event here to set up the app to reflect the
  210. // track's mute state. If this is not done, the
  211. // current mute state of the app will be reflected
  212. // on the track, not vice-versa.
  213. const setMuted
  214. = newTrack.isVideoTrack()
  215. ? setVideoMuted
  216. : setAudioMuted;
  217. const isMuted = newTrack.isMuted();
  218. sendAnalytics(createTrackMutedEvent(
  219. newTrack.getType(),
  220. 'track.replaced',
  221. isMuted));
  222. logger.log(`Replace ${newTrack.getType()} track - ${
  223. isMuted ? 'muted' : 'unmuted'}`);
  224. return dispatch(setMuted(isMuted));
  225. }
  226. })
  227. .then(() => {
  228. if (newTrack) {
  229. return dispatch(_addTracks([ newTrack ]));
  230. }
  231. });
  232. });
  233. };
  234. }
  235. /**
  236. * Create an action for when a new track has been signaled to be added to the
  237. * conference.
  238. *
  239. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  240. * @returns {{ type: TRACK_ADDED, track: Track }}
  241. */
  242. export function trackAdded(track) {
  243. return (dispatch, getState) => {
  244. track.on(
  245. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  246. () => dispatch(trackMutedChanged(track)));
  247. track.on(
  248. JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED,
  249. type => dispatch(trackVideoTypeChanged(track, type)));
  250. // participantId
  251. const local = track.isLocal();
  252. let participantId;
  253. if (local) {
  254. const participant = getLocalParticipant(getState);
  255. if (participant) {
  256. participantId = participant.id;
  257. }
  258. } else {
  259. participantId = track.getParticipantId();
  260. }
  261. return dispatch({
  262. type: TRACK_ADDED,
  263. track: {
  264. jitsiTrack: track,
  265. local,
  266. mediaType: track.getType(),
  267. mirror: _shouldMirror(track),
  268. muted: track.isMuted(),
  269. participantId,
  270. videoStarted: false,
  271. videoType: track.videoType
  272. }
  273. });
  274. };
  275. }
  276. /**
  277. * Create an action for when a track's muted state has been signaled to be
  278. * changed.
  279. *
  280. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  281. * @returns {{
  282. * type: TRACK_UPDATED,
  283. * track: Track
  284. * }}
  285. */
  286. export function trackMutedChanged(track) {
  287. return {
  288. type: TRACK_UPDATED,
  289. track: {
  290. jitsiTrack: track,
  291. muted: track.isMuted()
  292. }
  293. };
  294. }
  295. /**
  296. * Create an action for when a track has been signaled for removal from the
  297. * conference.
  298. *
  299. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  300. * @returns {{
  301. * type: TRACK_REMOVED,
  302. * track: Track
  303. * }}
  304. */
  305. export function trackRemoved(track) {
  306. track.removeAllListeners(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  307. track.removeAllListeners(JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED);
  308. return {
  309. type: TRACK_REMOVED,
  310. track: {
  311. jitsiTrack: track
  312. }
  313. };
  314. }
  315. /**
  316. * Signal that track's video started to play.
  317. *
  318. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  319. * @returns {{
  320. * type: TRACK_UPDATED,
  321. * track: Track
  322. * }}
  323. */
  324. export function trackVideoStarted(track) {
  325. return {
  326. type: TRACK_UPDATED,
  327. track: {
  328. jitsiTrack: track,
  329. videoStarted: true
  330. }
  331. };
  332. }
  333. /**
  334. * Create an action for when participant video type changes.
  335. *
  336. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  337. * @param {VIDEO_TYPE|undefined} videoType - Video type.
  338. * @returns {{
  339. * type: TRACK_UPDATED,
  340. * track: Track
  341. * }}
  342. */
  343. export function trackVideoTypeChanged(track, videoType) {
  344. return {
  345. type: TRACK_UPDATED,
  346. track: {
  347. jitsiTrack: track,
  348. videoType
  349. }
  350. };
  351. }
  352. /**
  353. * Signals passed tracks to be added.
  354. *
  355. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  356. * @private
  357. * @returns {Function}
  358. */
  359. function _addTracks(tracks) {
  360. return dispatch => Promise.all(tracks.map(t => dispatch(trackAdded(t))));
  361. }
  362. /**
  363. * Cancels and waits for any {@code getUserMedia} process/currently in progress
  364. * to complete/settle.
  365. *
  366. * @param {Function} getState - The redux store {@code getState} function used
  367. * to obtain the state.
  368. * @private
  369. * @returns {Promise} - A {@code Promise} resolved once all
  370. * {@code gumProcess.cancel()} {@code Promise}s are settled because all we care
  371. * about here is to be sure that the {@code getUserMedia} callbacks have
  372. * completed (i.e. returned from the native side).
  373. */
  374. function _cancelGUMProcesses(getState) {
  375. const logError
  376. = error =>
  377. logger.error('gumProcess.cancel failed', JSON.stringify(error));
  378. return Promise.all(
  379. getState()['features/base/tracks']
  380. .filter(t => t.local)
  381. .map(({ gumProcess }) =>
  382. gumProcess && gumProcess.cancel().catch(logError)));
  383. }
  384. /**
  385. * Disposes passed tracks and signals them to be removed.
  386. *
  387. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  388. * @protected
  389. * @returns {Function}
  390. */
  391. export function _disposeAndRemoveTracks(tracks) {
  392. return dispatch =>
  393. _disposeTracks(tracks)
  394. .then(() =>
  395. Promise.all(tracks.map(t => dispatch(trackRemoved(t)))));
  396. }
  397. /**
  398. * Disposes passed tracks.
  399. *
  400. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  401. * @private
  402. * @returns {Promise} - A Promise resolved once {@link JitsiTrack.dispose()} is
  403. * done for every track from the list.
  404. */
  405. function _disposeTracks(tracks) {
  406. return Promise.all(
  407. tracks.map(t =>
  408. t.dispose()
  409. .catch(err => {
  410. // Track might be already disposed so ignore such an error.
  411. // Of course, re-throw any other error(s).
  412. if (err.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  413. throw err;
  414. }
  415. })));
  416. }
  417. /**
  418. * Implements the {@code Promise} rejection handler of
  419. * {@code createLocalTracksA} and {@code createLocalTracksF}.
  420. *
  421. * @param {Object} reason - The {@code Promise} rejection reason.
  422. * @param {string} device - The device/{@code MEDIA_TYPE} associated with the
  423. * rejection.
  424. * @private
  425. * @returns {Function}
  426. */
  427. function _onCreateLocalTracksRejected({ gum }, device) {
  428. return dispatch => {
  429. // If permissions are not allowed, alert the user.
  430. if (gum) {
  431. const { error } = gum;
  432. if (error) {
  433. // FIXME For whatever reason (which is probably an
  434. // implementation fault), react-native-webrtc will give the
  435. // error in one of the following formats depending on whether it
  436. // is attached to a remote debugger or not. (The remote debugger
  437. // scenario suggests that react-native-webrtc is at fault
  438. // because the remote debugger is Google Chrome and then its
  439. // JavaScript engine will define DOMException. I suspect I wrote
  440. // react-native-webrtc to return the error in the alternative
  441. // format if DOMException is not defined.)
  442. let trackPermissionError;
  443. switch (error.name) {
  444. case 'DOMException':
  445. trackPermissionError = error.message === 'NotAllowedError';
  446. break;
  447. case 'NotAllowedError':
  448. trackPermissionError = error instanceof DOMException;
  449. break;
  450. }
  451. dispatch({
  452. type: TRACK_CREATE_ERROR,
  453. permissionDenied: trackPermissionError,
  454. trackType: device
  455. });
  456. }
  457. }
  458. };
  459. }
  460. /**
  461. * Returns true if the provided {@code JitsiTrack} should be rendered as a
  462. * mirror.
  463. *
  464. * We only want to show a video in mirrored mode when:
  465. * 1) The video source is local, and not remote.
  466. * 2) The video source is a camera, not a desktop (capture).
  467. * 3) The camera is capturing the user, not the environment.
  468. *
  469. * TODO Similar functionality is part of lib-jitsi-meet. This function should be
  470. * removed after https://github.com/jitsi/lib-jitsi-meet/pull/187 is merged.
  471. *
  472. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  473. * @private
  474. * @returns {boolean}
  475. */
  476. function _shouldMirror(track) {
  477. return (
  478. track
  479. && track.isLocal()
  480. && track.isVideoTrack()
  481. // XXX The type of the return value of JitsiLocalTrack's
  482. // getCameraFacingMode happens to be named CAMERA_FACING_MODE as
  483. // well, it's defined by lib-jitsi-meet. Note though that the type
  484. // of the value on the right side of the equality check is defined
  485. // by jitsi-meet. The type definitions are surely compatible today
  486. // but that may not be the case tomorrow.
  487. && track.getCameraFacingMode() === CAMERA_FACING_MODE.USER);
  488. }
  489. /**
  490. * Signals that track create operation for given media track has been canceled.
  491. * Will clean up local track stub from the redux state which holds the
  492. * {@code gumProcess} reference.
  493. *
  494. * @param {MEDIA_TYPE} mediaType - The type of the media for which the track was
  495. * being created.
  496. * @private
  497. * @returns {{
  498. * type,
  499. * trackType: MEDIA_TYPE
  500. * }}
  501. */
  502. function _trackCreateCanceled(mediaType) {
  503. return {
  504. type: TRACK_CREATE_CANCELED,
  505. trackType: mediaType
  506. };
  507. }