您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // @flow
  2. import uuid from 'uuid';
  3. import { getRoomName } from '../base/conference';
  4. import { getDialOutStatusUrl, getDialOutUrl } from '../base/config/functions';
  5. import { createLocalTrack } from '../base/lib-jitsi-meet';
  6. import { openURLInBrowser } from '../base/util';
  7. import { executeDialOutRequest, executeDialOutStatusRequest, getDialInfoPageURL } from '../invite/functions';
  8. import { showErrorNotification } from '../notifications';
  9. import {
  10. ADD_PREJOIN_AUDIO_TRACK,
  11. ADD_PREJOIN_CONTENT_SHARING_TRACK,
  12. ADD_PREJOIN_VIDEO_TRACK,
  13. PREJOIN_START_CONFERENCE,
  14. SET_DEVICE_STATUS,
  15. SET_DIALOUT_COUNTRY,
  16. SET_DIALOUT_NUMBER,
  17. SET_DIALOUT_STATUS,
  18. SET_SKIP_PREJOIN,
  19. SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  20. SET_PREJOIN_AUDIO_DISABLED,
  21. SET_PREJOIN_AUDIO_MUTED,
  22. SET_PREJOIN_DEVICE_ERRORS,
  23. SET_PREJOIN_PAGE_VISIBILITY,
  24. SET_PREJOIN_VIDEO_DISABLED,
  25. SET_PREJOIN_VIDEO_MUTED
  26. } from './actionTypes';
  27. import {
  28. getFullDialOutNumber,
  29. getAudioTrack,
  30. getDialOutConferenceUrl,
  31. getDialOutCountry,
  32. getVideoTrack,
  33. isJoinByPhoneDialogVisible
  34. } from './functions';
  35. import logger from './logger';
  36. const dialOutStatusToKeyMap = {
  37. INITIATED: 'presenceStatus.calling',
  38. RINGING: 'presenceStatus.ringing'
  39. };
  40. const DIAL_OUT_STATUS = {
  41. INITIATED: 'INITIATED',
  42. RINGING: 'RINGING',
  43. CONNECTED: 'CONNECTED',
  44. DISCONNECTED: 'DISCONNECTED',
  45. FAILED: 'FAILED'
  46. };
  47. /**
  48. * The time interval used between requests while polling for dial out status.
  49. */
  50. const STATUS_REQ_FREQUENCY = 2000;
  51. /**
  52. * The maximum number of retries while polling for dial out status.
  53. */
  54. const STATUS_REQ_CAP = 45;
  55. /**
  56. * Action used to add an audio track to the store.
  57. *
  58. * @param {Object} value - The track to be added.
  59. * @returns {Object}
  60. */
  61. export function addPrejoinAudioTrack(value: Object) {
  62. return {
  63. type: ADD_PREJOIN_AUDIO_TRACK,
  64. value
  65. };
  66. }
  67. /**
  68. * Action used to add a video track to the store.
  69. *
  70. * @param {Object} value - The track to be added.
  71. * @returns {Object}
  72. */
  73. export function addPrejoinVideoTrack(value: Object) {
  74. return {
  75. type: ADD_PREJOIN_VIDEO_TRACK,
  76. value
  77. };
  78. }
  79. /**
  80. * Action used to add a content sharing track to the store.
  81. *
  82. * @param {Object} value - The track to be added.
  83. * @returns {Object}
  84. */
  85. export function addPrejoinContentSharingTrack(value: Object) {
  86. return {
  87. type: ADD_PREJOIN_CONTENT_SHARING_TRACK,
  88. value
  89. };
  90. }
  91. /**
  92. * Polls for status change after dial out.
  93. * Changes dialog message based on response, closes the dialog if there is an error,
  94. * joins the meeting when CONNECTED.
  95. *
  96. * @param {string} reqId - The request id used to correlate the dial out request with this one.
  97. * @param {Function} onSuccess - Success handler.
  98. * @param {Function} onFail - Fail handler.
  99. * @param {number} count - The number of retried calls. When it hits STATUS_REQ_CAP it should no longer make requests.
  100. * @returns {Function}
  101. */
  102. function pollForStatus(
  103. reqId: string,
  104. onSuccess: Function,
  105. onFail: Function,
  106. count = 0) {
  107. return async function(dispatch: Function, getState: Function) {
  108. const state = getState();
  109. try {
  110. if (!isJoinByPhoneDialogVisible(state)) {
  111. return;
  112. }
  113. const res = await executeDialOutStatusRequest(getDialOutStatusUrl(state), reqId);
  114. switch (res) {
  115. case DIAL_OUT_STATUS.INITIATED:
  116. case DIAL_OUT_STATUS.RINGING: {
  117. dispatch(setDialOutStatus(dialOutStatusToKeyMap[res]));
  118. if (count < STATUS_REQ_CAP) {
  119. return setTimeout(() => {
  120. dispatch(pollForStatus(reqId, onSuccess, onFail, count + 1));
  121. }, STATUS_REQ_FREQUENCY);
  122. }
  123. return onFail();
  124. }
  125. case DIAL_OUT_STATUS.CONNECTED: {
  126. return onSuccess();
  127. }
  128. case DIAL_OUT_STATUS.DISCONNECTED: {
  129. dispatch(showErrorNotification({
  130. titleKey: 'prejoin.errorDialOutDisconnected'
  131. }));
  132. return onFail();
  133. }
  134. case DIAL_OUT_STATUS.FAILED: {
  135. dispatch(showErrorNotification({
  136. titleKey: 'prejoin.errorDialOutFailed'
  137. }));
  138. return onFail();
  139. }
  140. }
  141. } catch (err) {
  142. dispatch(showErrorNotification({
  143. titleKey: 'prejoin.errorDialOutStatus'
  144. }));
  145. logger.error('Error getting dial out status', err);
  146. onFail();
  147. }
  148. };
  149. }
  150. /**
  151. * Action used for joining the meeting with phone audio.
  152. * A dial out connection is tried and a polling mechanism is used for getting the status.
  153. * If the connection succeeds the `onSuccess` callback is executed.
  154. * If the phone connection fails or the number is invalid the `onFail` callback is executed.
  155. *
  156. * @param {Function} onSuccess - Success handler.
  157. * @param {Function} onFail - Fail handler.
  158. * @returns {Function}
  159. */
  160. export function dialOut(onSuccess: Function, onFail: Function) {
  161. return async function(dispatch: Function, getState: Function) {
  162. const state = getState();
  163. const reqId = uuid.v4();
  164. const url = getDialOutUrl(state);
  165. const conferenceUrl = getDialOutConferenceUrl(state);
  166. const phoneNumber = getFullDialOutNumber(state);
  167. const countryCode = getDialOutCountry(state).code.toUpperCase();
  168. const body = {
  169. conferenceUrl,
  170. countryCode,
  171. name: phoneNumber,
  172. phoneNumber
  173. };
  174. try {
  175. await executeDialOutRequest(url, body, reqId);
  176. dispatch(pollForStatus(reqId, onSuccess, onFail));
  177. } catch (err) {
  178. const notification = {
  179. titleKey: 'prejoin.errorDialOut',
  180. titleArguments: undefined
  181. };
  182. if (err.status) {
  183. if (err.messageKey === 'validation.failed') {
  184. notification.titleKey = 'prejoin.errorValidation';
  185. } else {
  186. notification.titleKey = 'prejoin.errorStatusCode';
  187. notification.titleArguments = { status: err.status };
  188. }
  189. }
  190. dispatch(showErrorNotification(notification));
  191. logger.error('Error dialing out', err);
  192. onFail();
  193. }
  194. };
  195. }
  196. /**
  197. * Adds all the newly created tracks to store on init.
  198. *
  199. * @param {Object[]} tracks - The newly created tracks.
  200. * @param {Object} errors - The errors from creating the tracks.
  201. *
  202. * @returns {Function}
  203. */
  204. export function initPrejoin(tracks: Object[], errors: Object) {
  205. return async function(dispatch: Function) {
  206. const audioTrack = tracks.find(t => t.isAudioTrack());
  207. const videoTrack = tracks.find(t => t.isVideoTrack());
  208. dispatch(setPrejoinDeviceErrors(errors));
  209. if (audioTrack) {
  210. dispatch(addPrejoinAudioTrack(audioTrack));
  211. } else {
  212. dispatch(setAudioDisabled());
  213. }
  214. if (videoTrack) {
  215. if (videoTrack.videoType === 'desktop') {
  216. dispatch(addPrejoinContentSharingTrack(videoTrack));
  217. dispatch(setPrejoinVideoDisabled(true));
  218. } else {
  219. dispatch(addPrejoinVideoTrack(videoTrack));
  220. }
  221. } else {
  222. dispatch(setPrejoinVideoDisabled(true));
  223. }
  224. };
  225. }
  226. /**
  227. * Joins the conference.
  228. *
  229. * @returns {Function}
  230. */
  231. export function joinConference() {
  232. return function(dispatch: Function) {
  233. dispatch(setPrejoinPageVisibility(false));
  234. dispatch(startConference());
  235. };
  236. }
  237. /**
  238. * Joins the conference without audio.
  239. *
  240. * @returns {Function}
  241. */
  242. export function joinConferenceWithoutAudio() {
  243. return async function(dispatch: Function, getState: Function) {
  244. const audioTrack = getAudioTrack(getState());
  245. if (audioTrack) {
  246. await dispatch(replacePrejoinAudioTrack(null));
  247. }
  248. dispatch(setAudioDisabled());
  249. dispatch(joinConference());
  250. };
  251. }
  252. /**
  253. * Opens an external page with all the dial in numbers.
  254. *
  255. * @returns {Function}
  256. */
  257. export function openDialInPage() {
  258. return function(dispatch: Function, getState: Function) {
  259. const state = getState();
  260. const locationURL = state['features/base/connection'].locationURL;
  261. const roomName = getRoomName(state);
  262. const dialInPage = getDialInfoPageURL(roomName, locationURL);
  263. openURLInBrowser(dialInPage, true);
  264. };
  265. }
  266. /**
  267. * Replaces the existing audio track with a new one.
  268. *
  269. * @param {Object} track - The new track.
  270. * @returns {Function}
  271. */
  272. export function replacePrejoinAudioTrack(track: Object) {
  273. return async (dispatch: Function, getState: Function) => {
  274. const oldTrack = getAudioTrack(getState());
  275. oldTrack && await oldTrack.dispose();
  276. dispatch(addPrejoinAudioTrack(track));
  277. };
  278. }
  279. /**
  280. * Creates a new audio track based on a device id and replaces the current one.
  281. *
  282. * @param {string} deviceId - The deviceId of the microphone.
  283. * @returns {Function}
  284. */
  285. export function replaceAudioTrackById(deviceId: string) {
  286. return async (dispatch: Function) => {
  287. try {
  288. const track = await createLocalTrack('audio', deviceId);
  289. dispatch(replacePrejoinAudioTrack(track));
  290. } catch (err) {
  291. dispatch(setDeviceStatusWarning('prejoin.audioTrackError'));
  292. logger.log('Error replacing audio track', err);
  293. }
  294. };
  295. }
  296. /**
  297. * Replaces the existing video track with a new one.
  298. *
  299. * @param {Object} track - The new track.
  300. * @returns {Function}
  301. */
  302. export function replacePrejoinVideoTrack(track: Object) {
  303. return async (dispatch: Function, getState: Function) => {
  304. const oldTrack = getVideoTrack(getState());
  305. oldTrack && await oldTrack.dispose();
  306. dispatch(addPrejoinVideoTrack(track));
  307. };
  308. }
  309. /**
  310. * Creates a new video track based on a device id and replaces the current one.
  311. *
  312. * @param {string} deviceId - The deviceId of the camera.
  313. * @returns {Function}
  314. */
  315. export function replaceVideoTrackById(deviceId: Object) {
  316. return async (dispatch: Function) => {
  317. try {
  318. const track = await createLocalTrack('video', deviceId);
  319. dispatch(replacePrejoinVideoTrack(track));
  320. } catch (err) {
  321. dispatch(setDeviceStatusWarning('prejoin.videoTrackError'));
  322. logger.log('Error replacing video track', err);
  323. }
  324. };
  325. }
  326. /**
  327. * Action used to mark audio muted.
  328. *
  329. * @param {boolean} value - True for muted.
  330. * @returns {Object}
  331. */
  332. export function setPrejoinAudioMuted(value: boolean) {
  333. return {
  334. type: SET_PREJOIN_AUDIO_MUTED,
  335. value
  336. };
  337. }
  338. /**
  339. * Action used to mark video disabled.
  340. *
  341. * @param {boolean} value - True for muted.
  342. * @returns {Object}
  343. */
  344. export function setPrejoinVideoDisabled(value: boolean) {
  345. return {
  346. type: SET_PREJOIN_VIDEO_DISABLED,
  347. value
  348. };
  349. }
  350. /**
  351. * Action used to mark video muted.
  352. *
  353. * @param {boolean} value - True for muted.
  354. * @returns {Object}
  355. */
  356. export function setPrejoinVideoMuted(value: boolean) {
  357. return {
  358. type: SET_PREJOIN_VIDEO_MUTED,
  359. value
  360. };
  361. }
  362. /**
  363. * Action used to mark audio as disabled.
  364. *
  365. * @returns {Object}
  366. */
  367. export function setAudioDisabled() {
  368. return {
  369. type: SET_PREJOIN_AUDIO_DISABLED
  370. };
  371. }
  372. /**
  373. * Sets the device status as OK with the corresponding text.
  374. *
  375. * @param {string} deviceStatusText - The text to be set.
  376. * @returns {Object}
  377. */
  378. export function setDeviceStatusOk(deviceStatusText: string) {
  379. return {
  380. type: SET_DEVICE_STATUS,
  381. value: {
  382. deviceStatusText,
  383. deviceStatusType: 'ok'
  384. }
  385. };
  386. }
  387. /**
  388. * Sets the device status as 'warning' with the corresponding text.
  389. *
  390. * @param {string} deviceStatusText - The text to be set.
  391. * @returns {Object}
  392. */
  393. export function setDeviceStatusWarning(deviceStatusText: string) {
  394. return {
  395. type: SET_DEVICE_STATUS,
  396. value: {
  397. deviceStatusText,
  398. deviceStatusType: 'warning'
  399. }
  400. };
  401. }
  402. /**
  403. * Action used to set the dial out status.
  404. *
  405. * @param {string} value - The status.
  406. * @returns {Object}
  407. */
  408. function setDialOutStatus(value: string) {
  409. return {
  410. type: SET_DIALOUT_STATUS,
  411. value
  412. };
  413. }
  414. /**
  415. * Action used to set the dial out country.
  416. *
  417. * @param {{ name: string, dialCode: string, code: string }} value - The country.
  418. * @returns {Object}
  419. */
  420. export function setDialOutCountry(value: Object) {
  421. return {
  422. type: SET_DIALOUT_COUNTRY,
  423. value
  424. };
  425. }
  426. /**
  427. * Action used to set the dial out number.
  428. *
  429. * @param {string} value - The dial out number.
  430. * @returns {Object}
  431. */
  432. export function setDialOutNumber(value: string) {
  433. return {
  434. type: SET_DIALOUT_NUMBER,
  435. value
  436. };
  437. }
  438. /**
  439. * Sets the visibility of the prejoin page for future uses.
  440. *
  441. * @param {boolean} value - The visibility value.
  442. * @returns {Object}
  443. */
  444. export function setSkipPrejoin(value: boolean) {
  445. return {
  446. type: SET_SKIP_PREJOIN,
  447. value
  448. };
  449. }
  450. /**
  451. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  452. *
  453. * @param {boolean} value - The value.
  454. * @returns {Object}
  455. */
  456. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  457. return {
  458. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  459. value
  460. };
  461. }
  462. /**
  463. * Action used to set the initial errors after creating the tracks.
  464. *
  465. * @param {Object} value - The track errors.
  466. * @returns {Object}
  467. */
  468. export function setPrejoinDeviceErrors(value: Object) {
  469. return {
  470. type: SET_PREJOIN_DEVICE_ERRORS,
  471. value
  472. };
  473. }
  474. /**
  475. * Action used to set the visiblity of the prejoin page.
  476. *
  477. * @param {boolean} value - The value.
  478. * @returns {Object}
  479. */
  480. export function setPrejoinPageVisibility(value: boolean) {
  481. return {
  482. type: SET_PREJOIN_PAGE_VISIBILITY,
  483. value
  484. };
  485. }
  486. /**
  487. * Action used to mark the start of the conference.
  488. *
  489. * @returns {Object}
  490. */
  491. function startConference() {
  492. return {
  493. type: PREJOIN_START_CONFERENCE
  494. };
  495. }