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

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