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

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