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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 {
  9. createLocalTracksF,
  10. getLocalAudioTrack,
  11. getLocalTracks,
  12. getLocalVideoTrack,
  13. trackAdded,
  14. replaceLocalTrack
  15. } from '../base/tracks';
  16. import { openURLInBrowser } from '../base/util';
  17. import { executeDialOutRequest, executeDialOutStatusRequest, getDialInfoPageURL } from '../invite/functions';
  18. import { NOTIFICATION_TIMEOUT_TYPE, showErrorNotification } from '../notifications';
  19. import {
  20. PREJOIN_JOINING_IN_PROGRESS,
  21. PREJOIN_INITIALIZED,
  22. SET_DIALOUT_COUNTRY,
  23. SET_DIALOUT_NUMBER,
  24. SET_DIALOUT_STATUS,
  25. SET_PREJOIN_DISPLAY_NAME_REQUIRED,
  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. SET_DEVICE_STATUS
  32. } from './actionTypes';
  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. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  100. return onFail();
  101. }
  102. case DIAL_OUT_STATUS.FAILED: {
  103. dispatch(showErrorNotification({
  104. titleKey: 'prejoin.errorDialOutFailed'
  105. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  106. return onFail();
  107. }
  108. }
  109. } catch (err) {
  110. dispatch(showErrorNotification({
  111. titleKey: 'prejoin.errorDialOutStatus'
  112. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  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 = uuidv4();
  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, NOTIFICATION_TIMEOUT_TYPE.LONG));
  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. * @param {boolean} ignoreJoiningInProgress - If true we won't check the joiningInProgress flag.
  184. * @returns {Function}
  185. */
  186. export function joinConference(options?: Object, ignoreJoiningInProgress: boolean = false) {
  187. return async function(dispatch: Function, getState: Function) {
  188. if (!ignoreJoiningInProgress) {
  189. const state = getState();
  190. const { joiningInProgress } = state['features/prejoin'];
  191. if (joiningInProgress) {
  192. return;
  193. }
  194. dispatch(setJoiningInProgress(true));
  195. }
  196. const state = getState();
  197. let localTracks = getLocalTracks(state['features/base/tracks']);
  198. options && dispatch(updateConfig(options));
  199. // Do not signal audio/video tracks if the user joins muted.
  200. for (const track of localTracks) {
  201. // Always add the audio track on Safari because of a known issue where audio playout doesn't happen
  202. // if the user joins audio and video muted.
  203. if (track.muted
  204. && !(browser.isWebKitBased() && track.jitsiTrack && track.jitsiTrack.getType() === MEDIA_TYPE.AUDIO)) {
  205. try {
  206. await dispatch(replaceLocalTrack(track.jitsiTrack, null));
  207. } catch (error) {
  208. logger.error(`Failed to replace local track (${track.jitsiTrack}) with null: ${error}`);
  209. }
  210. }
  211. }
  212. // Re-fetch the local tracks after muted tracks have been removed above.
  213. // This is needed, because the tracks are effectively disposed by the replaceLocalTrack and should not be used
  214. // anymore.
  215. localTracks = getLocalTracks(getState()['features/base/tracks']);
  216. const jitsiTracks = localTracks.map(t => t.jitsiTrack);
  217. APP.conference.prejoinStart(jitsiTracks);
  218. };
  219. }
  220. /**
  221. * Action used to set the flag for joining operation in progress.
  222. *
  223. * @param {boolean} value - The config options that override the default ones (if any).
  224. * @returns {Function}
  225. */
  226. export function setJoiningInProgress(value: boolean) {
  227. return {
  228. type: PREJOIN_JOINING_IN_PROGRESS,
  229. value
  230. };
  231. }
  232. /**
  233. * Joins the conference without audio.
  234. *
  235. * @returns {Function}
  236. */
  237. export function joinConferenceWithoutAudio() {
  238. return async function(dispatch: Function, getState: Function) {
  239. const state = getState();
  240. const { joiningInProgress } = state['features/prejoin'];
  241. if (joiningInProgress) {
  242. return;
  243. }
  244. dispatch(setJoiningInProgress(true));
  245. const tracks = state['features/base/tracks'];
  246. const audioTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  247. if (audioTrack) {
  248. try {
  249. await dispatch(replaceLocalTrack(audioTrack, null));
  250. } catch (error) {
  251. logger.error(`Failed to replace local audio with null: ${error}`);
  252. }
  253. }
  254. dispatch(joinConference({
  255. startSilent: true
  256. }, true));
  257. };
  258. }
  259. /**
  260. * Initializes the 'precallTest' and executes one test, storing the results.
  261. *
  262. * @param {Object} conferenceOptions - The conference options.
  263. * @returns {Function}
  264. */
  265. export function makePrecallTest(conferenceOptions: Object) {
  266. return async function(dispatch: Function) {
  267. try {
  268. await JitsiMeetJS.precallTest.init(conferenceOptions);
  269. const results = await JitsiMeetJS.precallTest.execute();
  270. dispatch(setPrecallTestResults(results));
  271. } catch (error) {
  272. logger.debug('Failed to execute pre call test - ', error);
  273. }
  274. };
  275. }
  276. /**
  277. * Opens an external page with all the dial in numbers.
  278. *
  279. * @returns {Function}
  280. */
  281. export function openDialInPage() {
  282. return function(dispatch: Function, getState: Function) {
  283. const dialInPage = getDialInfoPageURL(getState());
  284. openURLInBrowser(dialInPage, true);
  285. };
  286. }
  287. /**
  288. * Action used to signal that the prejoin page has been initialized.
  289. *
  290. * @returns {Object}
  291. */
  292. function prejoinInitialized() {
  293. return {
  294. type: PREJOIN_INITIALIZED
  295. };
  296. }
  297. /**
  298. * Creates a new audio track based on a device id and replaces the current one.
  299. *
  300. * @param {string} deviceId - The deviceId of the microphone.
  301. * @returns {Function}
  302. */
  303. export function replaceAudioTrackById(deviceId: string) {
  304. return async (dispatch: Function, getState: Function) => {
  305. try {
  306. const tracks = getState()['features/base/tracks'];
  307. const newTrack = await createLocalTrack('audio', deviceId);
  308. const oldTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  309. dispatch(replaceLocalTrack(oldTrack, newTrack));
  310. } catch (err) {
  311. dispatch(setDeviceStatusWarning('prejoin.audioTrackError'));
  312. logger.log('Error replacing audio track', err);
  313. }
  314. };
  315. }
  316. /**
  317. * Creates a new video track based on a device id and replaces the current one.
  318. *
  319. * @param {string} deviceId - The deviceId of the camera.
  320. * @returns {Function}
  321. */
  322. export function replaceVideoTrackById(deviceId: Object) {
  323. return async (dispatch: Function, getState: Function) => {
  324. try {
  325. const tracks = getState()['features/base/tracks'];
  326. const wasVideoMuted = isVideoMutedByUser(getState());
  327. const [ newTrack ] = await createLocalTracksF(
  328. { cameraDeviceId: deviceId,
  329. devices: [ 'video' ] },
  330. { dispatch,
  331. getState }
  332. );
  333. const oldTrack = getLocalVideoTrack(tracks)?.jitsiTrack;
  334. dispatch(replaceLocalTrack(oldTrack, newTrack));
  335. wasVideoMuted && newTrack.mute();
  336. } catch (err) {
  337. dispatch(setDeviceStatusWarning('prejoin.videoTrackError'));
  338. logger.log('Error replacing video track', err);
  339. }
  340. };
  341. }
  342. /**
  343. * Sets the device status as OK with the corresponding text.
  344. *
  345. * @param {string} deviceStatusText - The text to be set.
  346. * @returns {Object}
  347. */
  348. export function setDeviceStatusOk(deviceStatusText: string) {
  349. return {
  350. type: SET_DEVICE_STATUS,
  351. value: {
  352. deviceStatusText,
  353. deviceStatusType: 'ok'
  354. }
  355. };
  356. }
  357. /**
  358. * Sets the device status as 'warning' with the corresponding text.
  359. *
  360. * @param {string} deviceStatusText - The text to be set.
  361. * @returns {Object}
  362. */
  363. export function setDeviceStatusWarning(deviceStatusText: string) {
  364. return {
  365. type: SET_DEVICE_STATUS,
  366. value: {
  367. deviceStatusText,
  368. deviceStatusType: 'warning'
  369. }
  370. };
  371. }
  372. /**
  373. * Action used to set the dial out status.
  374. *
  375. * @param {string} value - The status.
  376. * @returns {Object}
  377. */
  378. function setDialOutStatus(value: string) {
  379. return {
  380. type: SET_DIALOUT_STATUS,
  381. value
  382. };
  383. }
  384. /**
  385. * Action used to set the dial out country.
  386. *
  387. * @param {{ name: string, dialCode: string, code: string }} value - The country.
  388. * @returns {Object}
  389. */
  390. export function setDialOutCountry(value: Object) {
  391. return {
  392. type: SET_DIALOUT_COUNTRY,
  393. value
  394. };
  395. }
  396. /**
  397. * Action used to set the stance of the display name.
  398. *
  399. * @returns {Object}
  400. */
  401. export function setPrejoinDisplayNameRequired() {
  402. return {
  403. type: SET_PREJOIN_DISPLAY_NAME_REQUIRED
  404. };
  405. }
  406. /**
  407. * Action used to set the dial out number.
  408. *
  409. * @param {string} value - The dial out number.
  410. * @returns {Object}
  411. */
  412. export function setDialOutNumber(value: string) {
  413. return {
  414. type: SET_DIALOUT_NUMBER,
  415. value
  416. };
  417. }
  418. /**
  419. * Sets the visibility of the prejoin page when a client reload
  420. * is triggered as a result of call migration initiated by Jicofo.
  421. *
  422. * @param {boolean} value - The visibility value.
  423. * @returns {Object}
  424. */
  425. export function setSkipPrejoinOnReload(value: boolean) {
  426. return {
  427. type: SET_SKIP_PREJOIN_RELOAD,
  428. value
  429. };
  430. }
  431. /**
  432. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  433. *
  434. * @param {boolean} value - The value.
  435. * @returns {Object}
  436. */
  437. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  438. return {
  439. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  440. value
  441. };
  442. }
  443. /**
  444. * Action used to set data from precall test.
  445. *
  446. * @param {Object} value - The precall test results.
  447. * @returns {Object}
  448. */
  449. export function setPrecallTestResults(value: Object) {
  450. return {
  451. type: SET_PRECALL_TEST_RESULTS,
  452. value
  453. };
  454. }
  455. /**
  456. * Action used to set the initial errors after creating the tracks.
  457. *
  458. * @param {Object} value - The track errors.
  459. * @returns {Object}
  460. */
  461. export function setPrejoinDeviceErrors(value: Object) {
  462. return {
  463. type: SET_PREJOIN_DEVICE_ERRORS,
  464. value
  465. };
  466. }
  467. /**
  468. * Action used to set the visibility of the prejoin page.
  469. *
  470. * @param {boolean} value - The value.
  471. * @returns {Object}
  472. */
  473. export function setPrejoinPageVisibility(value: boolean) {
  474. return {
  475. type: SET_PREJOIN_PAGE_VISIBILITY,
  476. value
  477. };
  478. }