Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

actions.web.ts 17KB

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