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

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