您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.web.ts 16KB

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