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.

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