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

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