Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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(localTracks => dispatch(_updateLocalTracks(localTracks)))
  79. .catch(({ gum }) => {
  80. // If permissions are not allowed, alert the user.
  81. if (gum
  82. && gum.error
  83. && gum.error.name === 'DOMException'
  84. && gum.error.message === 'NotAllowedError') {
  85. dispatch({
  86. type: TRACK_PERMISSION_ERROR,
  87. trackType: device
  88. });
  89. }
  90. });
  91. }
  92. };
  93. }
  94. /**
  95. * Calls JitsiLocalTrack#dispose() on all local tracks ignoring errors when
  96. * track is already disposed. After that signals tracks to be removed.
  97. *
  98. * @returns {Function}
  99. */
  100. export function destroyLocalTracks() {
  101. return (dispatch, getState) =>
  102. dispatch(
  103. _disposeAndRemoveTracks(
  104. getState()['features/base/tracks']
  105. .filter(t => t.local)
  106. .map(t => t.jitsiTrack)));
  107. }
  108. /**
  109. * Replaces one track with another for one renegotiation instead of invoking
  110. * two renegotiations with a separate removeTrack and addTrack. Disposes the
  111. * removed track as well.
  112. *
  113. * @param {JitsiLocalTrack|null} oldTrack - The track to dispose.
  114. * @param {JitsiLocalTrack|null} newTrack - The track to use instead.
  115. * @param {JitsiConference} [conference] - The conference from which to remove
  116. * and add the tracks. If one is not provided, the conference in the redux store
  117. * will be used.
  118. * @returns {Function}
  119. */
  120. export function replaceLocalTrack(oldTrack, newTrack, conference) {
  121. return (dispatch, getState) => {
  122. conference
  123. // eslint-disable-next-line no-param-reassign
  124. || (conference = getState()['features/base/conference'].conference);
  125. return conference.replaceTrack(oldTrack, newTrack)
  126. .then(() => {
  127. // We call dispose after doing the replace because dispose will
  128. // try and do a new o/a after the track removes itself. Doing it
  129. // after means the JitsiLocalTrack.conference is already
  130. // cleared, so it won't try and do the o/a.
  131. const disposePromise
  132. = oldTrack
  133. ? dispatch(_disposeAndRemoveTracks([ oldTrack ]))
  134. : Promise.resolve();
  135. return disposePromise
  136. .then(() => {
  137. if (newTrack) {
  138. // The mute state of the new track should be
  139. // reflected in the app's mute state. For example,
  140. // if the app is currently muted and changing to a
  141. // new track that is not muted, the app's mute
  142. // state should be falsey. As such, emit a mute
  143. // event here to set up the app to reflect the
  144. // track's mute state. If this is not done, the
  145. // current mute state of the app will be reflected
  146. // on the track, not vice-versa.
  147. const setMuted
  148. = newTrack.isVideoTrack()
  149. ? setVideoMuted
  150. : setAudioMuted;
  151. return dispatch(setMuted(newTrack.isMuted()));
  152. }
  153. })
  154. .then(() => {
  155. if (newTrack) {
  156. return dispatch(_addTracks([ newTrack ]));
  157. }
  158. });
  159. });
  160. };
  161. }
  162. /**
  163. * Create an action for when a new track has been signaled to be added to the
  164. * conference.
  165. *
  166. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  167. * @returns {{ type: TRACK_ADDED, track: Track }}
  168. */
  169. export function trackAdded(track) {
  170. return (dispatch, getState) => {
  171. track.on(
  172. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  173. () => dispatch(trackMutedChanged(track)));
  174. track.on(
  175. JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED,
  176. type => dispatch(trackVideoTypeChanged(track, type)));
  177. // participantId
  178. const local = track.isLocal();
  179. let participantId;
  180. if (local) {
  181. const participant = getLocalParticipant(getState);
  182. if (participant) {
  183. participantId = participant.id;
  184. }
  185. } else {
  186. participantId = track.getParticipantId();
  187. }
  188. return dispatch({
  189. type: TRACK_ADDED,
  190. track: {
  191. jitsiTrack: track,
  192. local,
  193. mediaType: track.getType(),
  194. mirror: _shouldMirror(track),
  195. muted: track.isMuted(),
  196. participantId,
  197. videoStarted: false,
  198. videoType: track.videoType
  199. }
  200. });
  201. };
  202. }
  203. /**
  204. * Create an action for when a track's muted state has been signaled to be
  205. * changed.
  206. *
  207. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  208. * @returns {{
  209. * type: TRACK_UPDATED,
  210. * track: Track
  211. * }}
  212. */
  213. export function trackMutedChanged(track) {
  214. return {
  215. type: TRACK_UPDATED,
  216. track: {
  217. jitsiTrack: track,
  218. muted: track.isMuted()
  219. }
  220. };
  221. }
  222. /**
  223. * Create an action for when a track has been signaled for removal from the
  224. * conference.
  225. *
  226. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  227. * @returns {{
  228. * type: TRACK_REMOVED,
  229. * track: Track
  230. * }}
  231. */
  232. export function trackRemoved(track) {
  233. track.removeAllListeners(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  234. track.removeAllListeners(JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED);
  235. return {
  236. type: TRACK_REMOVED,
  237. track: {
  238. jitsiTrack: track
  239. }
  240. };
  241. }
  242. /**
  243. * Signal that track's video started to play.
  244. *
  245. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  246. * @returns {{
  247. * type: TRACK_UPDATED,
  248. * track: Track
  249. * }}
  250. */
  251. export function trackVideoStarted(track) {
  252. return {
  253. type: TRACK_UPDATED,
  254. track: {
  255. jitsiTrack: track,
  256. videoStarted: true
  257. }
  258. };
  259. }
  260. /**
  261. * Create an action for when participant video type changes.
  262. *
  263. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  264. * @param {VIDEO_TYPE|undefined} videoType - Video type.
  265. * @returns {{
  266. * type: TRACK_UPDATED,
  267. * track: Track
  268. * }}
  269. */
  270. export function trackVideoTypeChanged(track, videoType) {
  271. return {
  272. type: TRACK_UPDATED,
  273. track: {
  274. jitsiTrack: track,
  275. videoType
  276. }
  277. };
  278. }
  279. /**
  280. * Signals passed tracks to be added.
  281. *
  282. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  283. * @private
  284. * @returns {Function}
  285. */
  286. function _addTracks(tracks) {
  287. return dispatch => Promise.all(tracks.map(t => dispatch(trackAdded(t))));
  288. }
  289. /**
  290. * Disposes passed tracks and signals them to be removed.
  291. *
  292. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  293. * @protected
  294. * @returns {Function}
  295. */
  296. export function _disposeAndRemoveTracks(tracks) {
  297. return dispatch =>
  298. Promise.all(
  299. tracks.map(t =>
  300. t.dispose()
  301. .catch(err => {
  302. // Track might be already disposed so ignore such an
  303. // error. Of course, re-throw any other error(s).
  304. if (err.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  305. throw err;
  306. }
  307. })
  308. ))
  309. .then(Promise.all(tracks.map(t => dispatch(trackRemoved(t)))));
  310. }
  311. /**
  312. * Finds the first <tt>JitsiLocalTrack</tt> in a specific array/list of
  313. * <tt>JitsiTrack</tt>s which is of a specific <tt>MEDIA_TYPE</tt>.
  314. *
  315. * @param {JitsiTrack[]} tracks - The array/list of <tt>JitsiTrack</tt>s to look
  316. * through.
  317. * @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the first
  318. * <tt>JitsiLocalTrack</tt> to be returned.
  319. * @private
  320. * @returns {JitsiLocalTrack} The first <tt>JitsiLocalTrack</tt>, if any, in the
  321. * specified <tt>tracks</tt> of the specified <tt>mediaType</tt>.
  322. */
  323. function _getLocalTrack(tracks, mediaType) {
  324. return tracks.find(track =>
  325. track.isLocal()
  326. // XXX JitsiTrack#getType() returns a MEDIA_TYPE value in the terms
  327. // of lib-jitsi-meet while mediaType is in the terms of jitsi-meet.
  328. && track.getType() === mediaType);
  329. }
  330. /**
  331. * Determines which local media tracks should be added and which removed.
  332. *
  333. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} currentTracks - List of
  334. * current/existing media tracks.
  335. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} newTracks - List of new media
  336. * tracks.
  337. * @private
  338. * @returns {{
  339. * tracksToAdd: JitsiLocalTrack[],
  340. * tracksToRemove: JitsiLocalTrack[]
  341. * }}
  342. */
  343. function _getLocalTracksToChange(currentTracks, newTracks) {
  344. const tracksToAdd = [];
  345. const tracksToRemove = [];
  346. for (const mediaType of [ MEDIA_TYPE.AUDIO, MEDIA_TYPE.VIDEO ]) {
  347. const newTrack = _getLocalTrack(newTracks, mediaType);
  348. if (newTrack) {
  349. const currentTrack = _getLocalTrack(currentTracks, mediaType);
  350. tracksToAdd.push(newTrack);
  351. currentTrack && tracksToRemove.push(currentTrack);
  352. }
  353. }
  354. return {
  355. tracksToAdd,
  356. tracksToRemove
  357. };
  358. }
  359. /**
  360. * Returns true if the provided JitsiTrack should be rendered as a mirror.
  361. *
  362. * We only want to show a video in mirrored mode when:
  363. * 1) The video source is local, and not remote.
  364. * 2) The video source is a camera, not a desktop (capture).
  365. * 3) The camera is capturing the user, not the environment.
  366. *
  367. * TODO Similar functionality is part of lib-jitsi-meet. This function should be
  368. * removed after https://github.com/jitsi/lib-jitsi-meet/pull/187 is merged.
  369. *
  370. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  371. * @private
  372. * @returns {boolean}
  373. */
  374. function _shouldMirror(track) {
  375. return (
  376. track
  377. && track.isLocal()
  378. && track.isVideoTrack()
  379. // XXX The type of the return value of JitsiLocalTrack's
  380. // getCameraFacingMode happens to be named CAMERA_FACING_MODE as
  381. // well, it's defined by lib-jitsi-meet. Note though that the type
  382. // of the value on the right side of the equality check is defined
  383. // by jitsi-meet. The type definitions are surely compatible today
  384. // but that may not be the case tomorrow.
  385. && track.getCameraFacingMode() === CAMERA_FACING_MODE.USER);
  386. }
  387. /**
  388. * Set new local tracks replacing any existing tracks that were previously
  389. * available. Currently only one audio and one video local tracks are allowed.
  390. *
  391. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} [newTracks=[]] - List of new
  392. * media tracks.
  393. * @private
  394. * @returns {Function}
  395. */
  396. function _updateLocalTracks(newTracks = []) {
  397. return (dispatch, getState) => {
  398. const tracks
  399. = getState()['features/base/tracks'].map(t => t.jitsiTrack);
  400. const { tracksToAdd, tracksToRemove }
  401. = _getLocalTracksToChange(tracks, newTracks);
  402. return dispatch(_disposeAndRemoveTracks(tracksToRemove))
  403. .then(() => dispatch(_addTracks(tracksToAdd)));
  404. };
  405. }