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

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