選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

actions.js 16KB

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