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

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