Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

actions.js 9.2KB

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