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.ts 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { IStore } from '../../app/types';
  2. import { showModeratedNotification } from '../../av-moderation/actions';
  3. import { shouldShowModeratedNotification } from '../../av-moderation/functions';
  4. import { isModerationNotificationDisplayed } from '../../notifications/functions';
  5. import {
  6. GUM_PENDING,
  7. SET_AUDIO_AVAILABLE,
  8. SET_AUDIO_MUTED,
  9. SET_AUDIO_UNMUTE_PERMISSIONS,
  10. SET_CAMERA_FACING_MODE,
  11. SET_INITIAL_GUM_PROMISE,
  12. SET_SCREENSHARE_MUTED,
  13. SET_VIDEO_AVAILABLE,
  14. SET_VIDEO_MUTED,
  15. SET_VIDEO_UNMUTE_PERMISSIONS,
  16. STORE_VIDEO_TRANSFORM,
  17. TOGGLE_CAMERA_FACING_MODE
  18. } from './actionTypes';
  19. import {
  20. MEDIA_TYPE,
  21. MediaType,
  22. SCREENSHARE_MUTISM_AUTHORITY,
  23. VIDEO_MUTISM_AUTHORITY
  24. } from './constants';
  25. import { IGUMPendingState } from './types';
  26. /**
  27. * Action to adjust the availability of the local audio.
  28. *
  29. * @param {boolean} available - True if the local audio is to be marked as
  30. * available or false if the local audio is not available.
  31. * @returns {{
  32. * type: SET_AUDIO_AVAILABLE,
  33. * available: boolean
  34. * }}
  35. */
  36. export function setAudioAvailable(available: boolean) {
  37. return {
  38. type: SET_AUDIO_AVAILABLE,
  39. available
  40. };
  41. }
  42. /**
  43. * Action to set the muted state of the local audio.
  44. *
  45. * @param {boolean} muted - True if the local audio is to be muted or false if
  46. * the local audio is to be unmuted.
  47. * @param {boolean} ensureTrack - True if we want to ensure that a new track is
  48. * created if missing.
  49. * @returns {{
  50. * type: SET_AUDIO_MUTED,
  51. * ensureTrack: boolean,
  52. * muted: boolean
  53. * }}
  54. */
  55. export function setAudioMuted(muted: boolean, ensureTrack = false) {
  56. return {
  57. type: SET_AUDIO_MUTED,
  58. ensureTrack,
  59. muted
  60. };
  61. }
  62. /**
  63. * Action to disable/enable the audio mute icon.
  64. *
  65. * @param {boolean} blocked - True if the audio mute icon needs to be disabled.
  66. * @param {boolean|undefined} skipNotification - True if we want to skip showing the notification.
  67. * @returns {Function}
  68. */
  69. export function setAudioUnmutePermissions(blocked: boolean, skipNotification = false) {
  70. return {
  71. type: SET_AUDIO_UNMUTE_PERMISSIONS,
  72. blocked,
  73. skipNotification
  74. };
  75. }
  76. /**
  77. * Action to set the facing mode of the local camera.
  78. *
  79. * @param {CAMERA_FACING_MODE} cameraFacingMode - The camera facing mode to set.
  80. * @returns {{
  81. * type: SET_CAMERA_FACING_MODE,
  82. * cameraFacingMode: CAMERA_FACING_MODE
  83. * }}
  84. */
  85. export function setCameraFacingMode(cameraFacingMode: string) {
  86. return {
  87. type: SET_CAMERA_FACING_MODE,
  88. cameraFacingMode
  89. };
  90. }
  91. /**
  92. * Sets the initial GUM promise.
  93. *
  94. * @param {Promise<Array<Object>> | undefined} promise - The promise.
  95. * @returns {{
  96. * type: SET_INITIAL_GUM_PROMISE,
  97. * promise: Promise
  98. * }}
  99. */
  100. export function setInitialGUMPromise(promise: Promise<{ errors: any; tracks: Array<any>; }> | null = null) {
  101. return {
  102. type: SET_INITIAL_GUM_PROMISE,
  103. promise
  104. };
  105. }
  106. /**
  107. * Action to set the muted state of the local screenshare.
  108. *
  109. * @param {boolean} muted - True if the local screenshare is to be enabled or false otherwise.
  110. * @param {number} authority - The {@link SCREENSHARE_MUTISM_AUTHORITY} which is muting/unmuting the local screenshare.
  111. * @param {boolean} ensureTrack - True if we want to ensure that a new track is created if missing.
  112. * @returns {Function}
  113. */
  114. export function setScreenshareMuted(
  115. muted: boolean,
  116. authority: number = SCREENSHARE_MUTISM_AUTHORITY.USER,
  117. ensureTrack = false) {
  118. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  119. const state = getState();
  120. // check for A/V Moderation when trying to unmute
  121. if (!muted && shouldShowModeratedNotification(MEDIA_TYPE.SCREENSHARE, state)) {
  122. if (!isModerationNotificationDisplayed(MEDIA_TYPE.SCREENSHARE, state)) {
  123. ensureTrack && dispatch(showModeratedNotification(MEDIA_TYPE.SCREENSHARE));
  124. }
  125. return;
  126. }
  127. const oldValue = state['features/base/media'].screenshare.muted;
  128. // eslint-disable-next-line no-bitwise
  129. const newValue = muted ? oldValue | authority : oldValue & ~authority;
  130. dispatch({
  131. type: SET_SCREENSHARE_MUTED,
  132. authority,
  133. ensureTrack,
  134. muted: newValue
  135. });
  136. };
  137. }
  138. /**
  139. * Action to adjust the availability of the local video.
  140. *
  141. * @param {boolean} available - True if the local video is to be marked as
  142. * available or false if the local video is not available.
  143. * @returns {{
  144. * type: SET_VIDEO_AVAILABLE,
  145. * available: boolean
  146. * }}
  147. */
  148. export function setVideoAvailable(available: boolean) {
  149. return {
  150. type: SET_VIDEO_AVAILABLE,
  151. available
  152. };
  153. }
  154. /**
  155. * Action to set the muted state of the local video.
  156. *
  157. * @param {boolean} muted - True if the local video is to be muted or false if
  158. * the local video is to be unmuted.
  159. * @param {number} authority - The {@link VIDEO_MUTISM_AUTHORITY} which is
  160. * muting/unmuting the local video.
  161. * @param {boolean} ensureTrack - True if we want to ensure that a new track is
  162. * created if missing.
  163. * @returns {Function}
  164. */
  165. export function setVideoMuted(
  166. muted: boolean | number,
  167. authority: number = VIDEO_MUTISM_AUTHORITY.USER,
  168. ensureTrack = false) {
  169. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  170. const state = getState();
  171. // check for A/V Moderation when trying to unmute
  172. if (!muted && shouldShowModeratedNotification(MEDIA_TYPE.VIDEO, state)) {
  173. if (!isModerationNotificationDisplayed(MEDIA_TYPE.VIDEO, state)) {
  174. ensureTrack && dispatch(showModeratedNotification(MEDIA_TYPE.VIDEO));
  175. }
  176. return;
  177. }
  178. const oldValue = state['features/base/media'].video.muted;
  179. // eslint-disable-next-line no-bitwise
  180. const newValue = muted ? oldValue | authority : oldValue & ~authority;
  181. dispatch({
  182. type: SET_VIDEO_MUTED,
  183. authority,
  184. ensureTrack,
  185. muted: newValue
  186. });
  187. };
  188. }
  189. /**
  190. * Action to disable/enable the video mute icon.
  191. *
  192. * @param {boolean} blocked - True if the video mute icon needs to be disabled.
  193. * @param {boolean|undefined} skipNotification - True if we want to skip showing the notification.
  194. * @returns {Function}
  195. */
  196. export function setVideoUnmutePermissions(blocked: boolean, skipNotification = false) {
  197. return {
  198. type: SET_VIDEO_UNMUTE_PERMISSIONS,
  199. blocked,
  200. skipNotification
  201. };
  202. }
  203. /**
  204. * Creates an action to store the last video {@link Transform} applied to a
  205. * stream.
  206. *
  207. * @param {string} streamId - The ID of the stream.
  208. * @param {Object} transform - The {@code Transform} to store.
  209. * @returns {{
  210. * type: STORE_VIDEO_TRANSFORM,
  211. * streamId: string,
  212. * transform: Object
  213. * }}
  214. */
  215. export function storeVideoTransform(streamId: string, transform: Object) {
  216. return {
  217. type: STORE_VIDEO_TRANSFORM,
  218. streamId,
  219. transform
  220. };
  221. }
  222. /**
  223. * Toggles the camera facing mode. Most commonly, for example, mobile devices
  224. * such as phones have a front/user-facing and a back/environment-facing
  225. * cameras. In contrast to setCameraFacingMode, allows the toggling to be
  226. * optimally and/or natively implemented without the overhead of separate reads
  227. * and writes of the current/effective camera facing mode.
  228. *
  229. * @returns {{
  230. * type: TOGGLE_CAMERA_FACING_MODE
  231. * }}
  232. */
  233. export function toggleCameraFacingMode() {
  234. return {
  235. type: TOGGLE_CAMERA_FACING_MODE
  236. };
  237. }
  238. /**
  239. * Sets the GUM pending status from unmute and initial track creation operation.
  240. *
  241. * @param {Array<MediaType>} mediaTypes - An array with the media types that GUM is called with.
  242. * @param {IGUMPendingState} status - The GUM status.
  243. * @returns {{
  244. * type: TOGGLE_CAMERA_FACING_MODE,
  245. * mediaTypes: Array<MediaType>,
  246. * status: IGUMPendingState
  247. * }}
  248. */
  249. export function gumPending(mediaTypes: Array<MediaType>, status: IGUMPendingState) {
  250. return {
  251. type: GUM_PENDING,
  252. mediaTypes,
  253. status
  254. };
  255. }