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

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