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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import JitsiMeetJS from '../lib-jitsi-meet';
  2. import {
  3. CAMERA_FACING_MODE,
  4. MEDIA_TYPE
  5. } from '../media';
  6. import { getLocalParticipant } from '../participants';
  7. import {
  8. TRACK_ADDED,
  9. TRACK_REMOVED,
  10. TRACK_UPDATED
  11. } from './actionTypes';
  12. import './middleware';
  13. import './reducer';
  14. const JitsiTrackErrors = JitsiMeetJS.errors.track;
  15. const JitsiTrackEvents = JitsiMeetJS.events.track;
  16. /**
  17. * Request to start capturing local audio and/or video. By default, the user
  18. * facing camera will be selected.
  19. *
  20. * @param {Object} [options] - For info @see JitsiMeetJS.createLocalTracks.
  21. * @returns {Function}
  22. */
  23. export function createLocalTracks(options = {}) {
  24. return dispatch =>
  25. JitsiMeetJS.createLocalTracks({
  26. devices: options.devices || [ MEDIA_TYPE.AUDIO, MEDIA_TYPE.VIDEO ],
  27. facingMode: options.facingMode || CAMERA_FACING_MODE.USER,
  28. cameraDeviceId: options.cameraDeviceId,
  29. micDeviceId: options.micDeviceId
  30. })
  31. .then(localTracks => dispatch(_updateLocalTracks(localTracks)))
  32. .catch(err => {
  33. console.error(
  34. `JitsiMeetJS.createLocalTracks.catch rejection reason: ${err}`);
  35. });
  36. }
  37. /**
  38. * Calls JitsiLocalTrack#dispose() on all local tracks ignoring errors when
  39. * track is already disposed. After that signals tracks to be removed.
  40. *
  41. * @returns {Function}
  42. */
  43. export function destroyLocalTracks() {
  44. return (dispatch, getState) =>
  45. dispatch(
  46. _disposeAndRemoveTracks(
  47. getState()['features/base/tracks']
  48. .filter(t => t.local)
  49. .map(t => t.jitsiTrack)));
  50. }
  51. /**
  52. * Returns true if the provided JitsiTrack should be rendered as a mirror.
  53. *
  54. * We only want to show a video in mirrored mode when:
  55. * 1) The video source is local, and not remote.
  56. * 2) The video source is a camera, not a desktop (capture).
  57. * 3) The camera is capturing the user, not the environment.
  58. *
  59. * TODO Similar functionality is part of lib-jitsi-meet. This function should be
  60. * removed after https://github.com/jitsi/lib-jitsi-meet/pull/187 is merged.
  61. *
  62. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  63. * @private
  64. * @returns {boolean}
  65. */
  66. function _shouldMirror(track) {
  67. return (
  68. track
  69. && track.isLocal()
  70. && track.isVideoTrack()
  71. // XXX Type of the return value of
  72. // JitsiLocalTrack#getCameraFacingMode() happens to be named
  73. // CAMERA_FACING_MODE as well, it's defined by lib-jitsi-meet. Note
  74. // though that the type of the value on the right side of the
  75. // equality check is defined by jitsi-meet-react. The type
  76. // definitions are surely compatible today but that may not be the
  77. // case tomorrow.
  78. && track.getCameraFacingMode() === CAMERA_FACING_MODE.USER
  79. && !track.isScreenSharing()
  80. );
  81. }
  82. /**
  83. * Create an action for when a new track has been signaled to be added to the
  84. * conference.
  85. *
  86. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  87. * @returns {{ type: TRACK_ADDED, track: Track }}
  88. */
  89. export function trackAdded(track) {
  90. return (dispatch, getState) => {
  91. track.on(
  92. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  93. () => dispatch(trackMutedChanged(track)));
  94. track.on(
  95. JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED,
  96. type => dispatch(trackVideoTypeChanged(track, type)));
  97. // participantId
  98. let participantId;
  99. if (track.isLocal()) {
  100. const participant = getLocalParticipant(getState);
  101. if (participant) {
  102. participantId = participant.id;
  103. }
  104. } else {
  105. participantId = track.getParticipantId();
  106. }
  107. return dispatch({
  108. type: TRACK_ADDED,
  109. track: {
  110. jitsiTrack: track,
  111. local: track.isLocal(),
  112. mediaType: track.getType(),
  113. mirrorVideo: _shouldMirror(track),
  114. muted: track.isMuted(),
  115. participantId,
  116. videoStarted: false,
  117. videoType: track.videoType
  118. }
  119. });
  120. };
  121. }
  122. /**
  123. * Create an action for when a track's muted state has been signaled to be
  124. * changed.
  125. *
  126. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  127. * @returns {{ type: TRACK_UPDATED, track: Track }}
  128. */
  129. export function trackMutedChanged(track) {
  130. return {
  131. type: TRACK_UPDATED,
  132. track: {
  133. jitsiTrack: track,
  134. muted: track.isMuted()
  135. }
  136. };
  137. }
  138. /**
  139. * Create an action for when a track has been signaled for removal from the
  140. * conference.
  141. *
  142. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  143. * @returns {{ type: TRACK_REMOVED, track: Track }}
  144. */
  145. export function trackRemoved(track) {
  146. return {
  147. type: TRACK_REMOVED,
  148. track: {
  149. jitsiTrack: track
  150. }
  151. };
  152. }
  153. /**
  154. * Signal that track's video started to play.
  155. *
  156. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  157. * @returns {{ type: TRACK_UPDATED, track: Track }}
  158. */
  159. export function trackVideoStarted(track) {
  160. return {
  161. type: TRACK_UPDATED,
  162. track: {
  163. jitsiTrack: track,
  164. videoStarted: true
  165. }
  166. };
  167. }
  168. /**
  169. * Create an action for when participant video type changes.
  170. *
  171. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} track - JitsiTrack instance.
  172. * @param {VIDEO_TYPE|undefined} videoType - Video type.
  173. * @returns {{ type: TRACK_UPDATED, track: Track }}
  174. */
  175. export function trackVideoTypeChanged(track, videoType) {
  176. return {
  177. type: TRACK_UPDATED,
  178. track: {
  179. jitsiTrack: track,
  180. videoType
  181. }
  182. };
  183. }
  184. /**
  185. * Signals passed tracks to be added.
  186. *
  187. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  188. * @private
  189. * @returns {Function}
  190. */
  191. function _addTracks(tracks) {
  192. return dispatch =>
  193. Promise.all(tracks.map(t => dispatch(trackAdded(t))));
  194. }
  195. /**
  196. * Disposes passed tracks and signals them to be removed.
  197. *
  198. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} tracks - List of tracks.
  199. * @private
  200. * @returns {Function}
  201. */
  202. function _disposeAndRemoveTracks(tracks) {
  203. return dispatch =>
  204. Promise.all(
  205. tracks.map(t =>
  206. t.dispose()
  207. .catch(err => {
  208. // Track might be already disposed so ignore such an
  209. // error. Of course, re-throw any other error(s).
  210. if (err.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  211. throw err;
  212. }
  213. })
  214. ))
  215. .then(Promise.all(tracks.map(t => dispatch(trackRemoved(t)))));
  216. }
  217. /**
  218. * Determines which local media tracks should be added, and which - removed.
  219. *
  220. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} currentTracks - List of
  221. * existing media tracks.
  222. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} newTracks - List of new media
  223. * tracks.
  224. * @private
  225. * @returns {{
  226. * tracksToAdd: JitsiLocalTrack[],
  227. * tracksToRemove: JitsiLocalTrack[]
  228. * }}
  229. */
  230. function _getLocalTracksToChange(currentTracks, newTracks) {
  231. const currentLocalAudio
  232. = currentTracks.find(t => t.isLocal() && t.isAudioTrack());
  233. const currentLocalVideo
  234. = currentTracks.find(t => t.isLocal() && t.isVideoTrack());
  235. const newLocalAudio = newTracks.find(t => t.isLocal() && t.isAudioTrack());
  236. const newLocalVideo = newTracks.find(t => t.isLocal() && t.isVideoTrack());
  237. const tracksToRemove = [];
  238. const tracksToAdd = [];
  239. if (newLocalAudio) {
  240. tracksToAdd.push(newLocalAudio);
  241. currentLocalAudio && tracksToRemove.push(currentLocalAudio);
  242. }
  243. if (newLocalVideo) {
  244. tracksToAdd.push(newLocalVideo);
  245. currentLocalVideo && tracksToRemove.push(currentLocalVideo);
  246. }
  247. return {
  248. tracksToAdd,
  249. tracksToRemove
  250. };
  251. }
  252. /**
  253. * Set new local tracks replacing any existing tracks that were previously
  254. * available. Currently only one audio and one video local tracks are allowed.
  255. *
  256. * @param {(JitsiLocalTrack|JitsiRemoteTrack)[]} [newTracks=[]] - List of new
  257. * media tracks.
  258. * @returns {Function}
  259. */
  260. function _updateLocalTracks(newTracks = []) {
  261. return (dispatch, getState) => {
  262. const tracks
  263. = getState()['features/base/tracks'].map(t => t.jitsiTrack);
  264. const { tracksToAdd, tracksToRemove }
  265. = _getLocalTracksToChange(tracks, newTracks);
  266. return dispatch(_disposeAndRemoveTracks(tracksToRemove))
  267. .then(() => dispatch(_addTracks(tracksToAdd)));
  268. };
  269. }