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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // @flow
  2. import {
  3. ADD_PREJOIN_AUDIO_TRACK,
  4. ADD_PREJOIN_CONTENT_SHARING_TRACK,
  5. ADD_PREJOIN_VIDEO_TRACK,
  6. PREJOIN_START_CONFERENCE,
  7. SET_DEVICE_STATUS,
  8. SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  9. SET_PREJOIN_AUDIO_DISABLED,
  10. SET_PREJOIN_AUDIO_MUTED,
  11. SET_PREJOIN_DEVICE_ERRORS,
  12. SET_PREJOIN_NAME,
  13. SET_PREJOIN_PAGE_VISIBILITY,
  14. SET_PREJOIN_VIDEO_DISABLED,
  15. SET_PREJOIN_VIDEO_MUTED
  16. } from './actionTypes';
  17. import { createLocalTrack } from '../base/lib-jitsi-meet';
  18. import { getAudioTrack, getVideoTrack } from './functions';
  19. import logger from './logger';
  20. /**
  21. * Action used to add an audio track to the store.
  22. *
  23. * @param {Object} value - The track to be added.
  24. * @returns {Object}
  25. */
  26. export function addPrejoinAudioTrack(value: Object) {
  27. return {
  28. type: ADD_PREJOIN_AUDIO_TRACK,
  29. value
  30. };
  31. }
  32. /**
  33. * Action used to add a video track to the store.
  34. *
  35. * @param {Object} value - The track to be added.
  36. * @returns {Object}
  37. */
  38. export function addPrejoinVideoTrack(value: Object) {
  39. return {
  40. type: ADD_PREJOIN_VIDEO_TRACK,
  41. value
  42. };
  43. }
  44. /**
  45. * Action used to add a content sharing track to the store.
  46. *
  47. * @param {Object} value - The track to be added.
  48. * @returns {Object}
  49. */
  50. export function addPrejoinContentSharingTrack(value: Object) {
  51. return {
  52. type: ADD_PREJOIN_CONTENT_SHARING_TRACK,
  53. value
  54. };
  55. }
  56. /**
  57. * Adds all the newly created tracks to store on init.
  58. *
  59. * @param {Object[]} tracks - The newly created tracks.
  60. * @param {Object} errors - The errors from creating the tracks.
  61. *
  62. * @returns {Function}
  63. */
  64. export function initPrejoin(tracks: Object[], errors: Object) {
  65. return async function(dispatch: Function) {
  66. const audioTrack = tracks.find(t => t.isAudioTrack());
  67. const videoTrack = tracks.find(t => t.isVideoTrack());
  68. dispatch(setPrejoinDeviceErrors(errors));
  69. if (audioTrack) {
  70. dispatch(addPrejoinAudioTrack(audioTrack));
  71. } else {
  72. dispatch(setAudioDisabled());
  73. }
  74. if (videoTrack) {
  75. if (videoTrack.videoType === 'desktop') {
  76. dispatch(addPrejoinContentSharingTrack(videoTrack));
  77. dispatch(setPrejoinVideoDisabled(true));
  78. } else {
  79. dispatch(addPrejoinVideoTrack(videoTrack));
  80. }
  81. } else {
  82. dispatch(setPrejoinVideoDisabled(true));
  83. }
  84. };
  85. }
  86. /**
  87. * Joins the conference.
  88. *
  89. * @returns {Function}
  90. */
  91. export function joinConference() {
  92. return function(dispatch: Function) {
  93. dispatch(setPrejoinPageVisibility(false));
  94. dispatch(startConference());
  95. };
  96. }
  97. /**
  98. * Joins the conference without audio.
  99. *
  100. * @returns {Function}
  101. */
  102. export function joinConferenceWithoutAudio() {
  103. return async function(dispatch: Function, getState: Function) {
  104. const audioTrack = getAudioTrack(getState());
  105. if (audioTrack) {
  106. await dispatch(replacePrejoinAudioTrack(null));
  107. }
  108. dispatch(setAudioDisabled());
  109. dispatch(joinConference());
  110. };
  111. }
  112. /**
  113. * Replaces the existing audio track with a new one.
  114. *
  115. * @param {Object} track - The new track.
  116. * @returns {Function}
  117. */
  118. export function replacePrejoinAudioTrack(track: Object) {
  119. return async (dispatch: Function, getState: Function) => {
  120. const oldTrack = getAudioTrack(getState());
  121. oldTrack && await oldTrack.dispose();
  122. dispatch(addPrejoinAudioTrack(track));
  123. };
  124. }
  125. /**
  126. * Creates a new audio track based on a device id and replaces the current one.
  127. *
  128. * @param {string} deviceId - The deviceId of the microphone.
  129. * @returns {Function}
  130. */
  131. export function replaceAudioTrackById(deviceId: string) {
  132. return async (dispatch: Function) => {
  133. try {
  134. const track = await createLocalTrack('audio', deviceId);
  135. dispatch(replacePrejoinAudioTrack(track));
  136. } catch (err) {
  137. dispatch(setDeviceStatusWarning('prejoin.audioTrackError'));
  138. logger.log('Error replacing audio track', err);
  139. }
  140. };
  141. }
  142. /**
  143. * Replaces the existing video track with a new one.
  144. *
  145. * @param {Object} track - The new track.
  146. * @returns {Function}
  147. */
  148. export function replacePrejoinVideoTrack(track: Object) {
  149. return async (dispatch: Function, getState: Function) => {
  150. const oldTrack = getVideoTrack(getState());
  151. oldTrack && await oldTrack.dispose();
  152. dispatch(addPrejoinVideoTrack(track));
  153. };
  154. }
  155. /**
  156. * Creates a new video track based on a device id and replaces the current one.
  157. *
  158. * @param {string} deviceId - The deviceId of the camera.
  159. * @returns {Function}
  160. */
  161. export function replaceVideoTrackById(deviceId: Object) {
  162. return async (dispatch: Function) => {
  163. try {
  164. const track = await createLocalTrack('video', deviceId);
  165. dispatch(replacePrejoinVideoTrack(track));
  166. } catch (err) {
  167. dispatch(setDeviceStatusWarning('prejoin.videoTrackError'));
  168. logger.log('Error replacing video track', err);
  169. }
  170. };
  171. }
  172. /**
  173. * Action used to mark audio muted.
  174. *
  175. * @param {boolean} value - True for muted.
  176. * @returns {Object}
  177. */
  178. export function setPrejoinAudioMuted(value: boolean) {
  179. return {
  180. type: SET_PREJOIN_AUDIO_MUTED,
  181. value
  182. };
  183. }
  184. /**
  185. * Action used to mark video disabled.
  186. *
  187. * @param {boolean} value - True for muted.
  188. * @returns {Object}
  189. */
  190. export function setPrejoinVideoDisabled(value: boolean) {
  191. return {
  192. type: SET_PREJOIN_VIDEO_DISABLED,
  193. value
  194. };
  195. }
  196. /**
  197. * Action used to mark video muted.
  198. *
  199. * @param {boolean} value - True for muted.
  200. * @returns {Object}
  201. */
  202. export function setPrejoinVideoMuted(value: boolean) {
  203. return {
  204. type: SET_PREJOIN_VIDEO_MUTED,
  205. value
  206. };
  207. }
  208. /**
  209. * Action used to mark audio as disabled.
  210. *
  211. * @returns {Object}
  212. */
  213. export function setAudioDisabled() {
  214. return {
  215. type: SET_PREJOIN_AUDIO_DISABLED
  216. };
  217. }
  218. /**
  219. * Sets the device status as OK with the corresponding text.
  220. *
  221. * @param {string} deviceStatusText - The text to be set.
  222. * @returns {Object}
  223. */
  224. export function setDeviceStatusOk(deviceStatusText: string) {
  225. return {
  226. type: SET_DEVICE_STATUS,
  227. value: {
  228. deviceStatusText,
  229. deviceStatusType: 'ok'
  230. }
  231. };
  232. }
  233. /**
  234. * Sets the device status as 'warning' with the corresponding text.
  235. *
  236. * @param {string} deviceStatusText - The text to be set.
  237. * @returns {Object}
  238. */
  239. export function setDeviceStatusWarning(deviceStatusText: string) {
  240. return {
  241. type: SET_DEVICE_STATUS,
  242. value: {
  243. deviceStatusText,
  244. deviceStatusType: 'warning'
  245. }
  246. };
  247. }
  248. /**
  249. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  250. *
  251. * @param {boolean} value - The value.
  252. * @returns {Object}
  253. */
  254. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  255. return {
  256. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  257. value
  258. };
  259. }
  260. /**
  261. * Action used to set the initial errors after creating the tracks.
  262. *
  263. * @param {Object} value - The track errors.
  264. * @returns {Object}
  265. */
  266. export function setPrejoinDeviceErrors(value: Object) {
  267. return {
  268. type: SET_PREJOIN_DEVICE_ERRORS,
  269. value
  270. };
  271. }
  272. /**
  273. * Action used to set the name of the guest user.
  274. *
  275. * @param {string} value - The name.
  276. * @returns {Object}
  277. */
  278. export function setPrejoinName(value: string) {
  279. return {
  280. type: SET_PREJOIN_NAME,
  281. value
  282. };
  283. }
  284. /**
  285. * Action used to set the visiblity of the prejoin page.
  286. *
  287. * @param {boolean} value - The value.
  288. * @returns {Object}
  289. */
  290. export function setPrejoinPageVisibility(value: boolean) {
  291. return {
  292. type: SET_PREJOIN_PAGE_VISIBILITY,
  293. value
  294. };
  295. }
  296. /**
  297. * Action used to mark the start of the conference.
  298. *
  299. * @returns {Object}
  300. */
  301. function startConference() {
  302. return {
  303. type: PREJOIN_START_CONFERENCE
  304. };
  305. }