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

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