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

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