Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

actions.js 14KB

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