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

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