Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.web.ts 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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_PREJOIN_DEVICE_ERRORS,
  32. SET_PREJOIN_PAGE_VISIBILITY,
  33. SET_SKIP_PREJOIN_RELOAD
  34. } from './actionTypes';
  35. import {
  36. getDialOutConferenceUrl,
  37. getDialOutCountry,
  38. getFullDialOutNumber,
  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: IStore['dispatch'], getState: IStore['getState']) {
  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 as keyof typeof dialOutStatusToKeyMap]));
  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: IStore['dispatch'], getState: IStore['getState']) {
  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: any) {
  148. const notification: INotificationProps = {
  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: IStore['dispatch']) {
  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. * @param {string?} jid - The XMPP user's ID (e.g. {@code user@server.com}).
  187. * @param {string?} password - The XMPP user's password.
  188. * @returns {Function}
  189. */
  190. export function joinConference(options?: Object, ignoreJoiningInProgress = false,
  191. jid?: string, password?: string) {
  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. options && dispatch(updateConfig(options));
  202. dispatch(connect(jid, password)).then(async () => {
  203. // TODO keep this here till we move tracks and conference management from
  204. // conference.js to react.
  205. const state = getState();
  206. let localTracks = getLocalTracks(state['features/base/tracks']);
  207. // Do not signal audio/video tracks if the user joins muted.
  208. for (const track of localTracks) {
  209. // Always add the audio track on Safari because of a known issue where audio playout doesn't happen
  210. // if the user joins audio and video muted.
  211. if (track.muted && !(browser.isWebKitBased() && track.jitsiTrack
  212. && track.jitsiTrack.getType() === MEDIA_TYPE.AUDIO)) {
  213. try {
  214. await dispatch(replaceLocalTrack(track.jitsiTrack, null));
  215. } catch (error) {
  216. logger.error(`Failed to replace local track (${track.jitsiTrack}) with null: ${error}`);
  217. }
  218. }
  219. }
  220. // Re-fetch the local tracks after muted tracks have been removed above.
  221. // This is needed, because the tracks are effectively disposed by the replaceLocalTrack and should not be
  222. // used anymore.
  223. localTracks = getLocalTracks(getState()['features/base/tracks']);
  224. const jitsiTracks = localTracks.map((t: any) => t.jitsiTrack);
  225. APP.conference.startConference(jitsiTracks).catch(logger.error);
  226. })
  227. .catch(() => {
  228. // There is nothing to do here. This is handled and dispatched in base/connection/actions.
  229. });
  230. };
  231. }
  232. /**
  233. * Action used to set the flag for joining operation in progress.
  234. *
  235. * @param {boolean} value - The config options that override the default ones (if any).
  236. * @returns {Function}
  237. */
  238. export function setJoiningInProgress(value: boolean) {
  239. return {
  240. type: PREJOIN_JOINING_IN_PROGRESS,
  241. value
  242. };
  243. }
  244. /**
  245. * Joins the conference without audio.
  246. *
  247. * @returns {Function}
  248. */
  249. export function joinConferenceWithoutAudio() {
  250. return async function(dispatch: IStore['dispatch'], getState: IStore['getState']) {
  251. const state = getState();
  252. const { joiningInProgress } = state['features/prejoin'];
  253. if (joiningInProgress) {
  254. return;
  255. }
  256. dispatch(setJoiningInProgress(true));
  257. const tracks = state['features/base/tracks'];
  258. const audioTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  259. if (audioTrack) {
  260. try {
  261. await dispatch(replaceLocalTrack(audioTrack, null));
  262. } catch (error) {
  263. logger.error(`Failed to replace local audio with null: ${error}`);
  264. }
  265. }
  266. dispatch(joinConference({
  267. startSilent: true
  268. }, true));
  269. };
  270. }
  271. /**
  272. * Opens an external page with all the dial in numbers.
  273. *
  274. * @returns {Function}
  275. */
  276. export function openDialInPage() {
  277. return function(dispatch: IStore['dispatch'], getState: IStore['getState']) {
  278. const dialInPage = getDialInfoPageURL(getState());
  279. openURLInBrowser(dialInPage, true);
  280. };
  281. }
  282. /**
  283. * Action used to signal that the prejoin page has been initialized.
  284. *
  285. * @returns {Object}
  286. */
  287. function prejoinInitialized() {
  288. return {
  289. type: PREJOIN_INITIALIZED
  290. };
  291. }
  292. /**
  293. * Creates a new audio track based on a device id and replaces the current one.
  294. *
  295. * @param {string} deviceId - The deviceId of the microphone.
  296. * @returns {Function}
  297. */
  298. export function replaceAudioTrackById(deviceId: string) {
  299. return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  300. try {
  301. const tracks = getState()['features/base/tracks'];
  302. const newTrack = await createLocalTrack('audio', deviceId);
  303. const oldTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  304. const micDeviceId = newTrack.getDeviceId();
  305. logger.info(`Switching audio input device to ${micDeviceId}`);
  306. dispatch(replaceLocalTrack(oldTrack, newTrack)).then(() => {
  307. dispatch(updateSettings({
  308. micDeviceId
  309. }));
  310. });
  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: string) {
  324. return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  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. const cameraDeviceId = newTrack.getDeviceId();
  336. logger.info(`Switching camera to ${cameraDeviceId}`);
  337. dispatch(replaceLocalTrack(oldTrack, newTrack)).then(() => {
  338. dispatch(updateSettings({
  339. cameraDeviceId
  340. }));
  341. });
  342. wasVideoMuted && newTrack.mute();
  343. } catch (err) {
  344. dispatch(setDeviceStatusWarning('prejoin.videoTrackError'));
  345. logger.log('Error replacing video track', err);
  346. }
  347. };
  348. }
  349. /**
  350. * Sets the device status as OK with the corresponding text.
  351. *
  352. * @param {string} deviceStatusText - The text to be set.
  353. * @returns {Object}
  354. */
  355. export function setDeviceStatusOk(deviceStatusText: string) {
  356. return {
  357. type: SET_DEVICE_STATUS,
  358. value: {
  359. deviceStatusText,
  360. deviceStatusType: 'ok'
  361. }
  362. };
  363. }
  364. /**
  365. * Sets the device status as 'warning' with the corresponding text.
  366. *
  367. * @param {string} deviceStatusText - The text to be set.
  368. * @returns {Object}
  369. */
  370. export function setDeviceStatusWarning(deviceStatusText: string) {
  371. return {
  372. type: SET_DEVICE_STATUS,
  373. value: {
  374. deviceStatusText,
  375. deviceStatusType: 'warning'
  376. }
  377. };
  378. }
  379. /**
  380. * Action used to set the dial out status.
  381. *
  382. * @param {string} value - The status.
  383. * @returns {Object}
  384. */
  385. function setDialOutStatus(value: string) {
  386. return {
  387. type: SET_DIALOUT_STATUS,
  388. value
  389. };
  390. }
  391. /**
  392. * Action used to set the dial out country.
  393. *
  394. * @param {{ name: string, dialCode: string, code: string }} value - The country.
  395. * @returns {Object}
  396. */
  397. export function setDialOutCountry(value: Object) {
  398. return {
  399. type: SET_DIALOUT_COUNTRY,
  400. value
  401. };
  402. }
  403. /**
  404. * Action used to set the dial out number.
  405. *
  406. * @param {string} value - The dial out number.
  407. * @returns {Object}
  408. */
  409. export function setDialOutNumber(value: string) {
  410. return {
  411. type: SET_DIALOUT_NUMBER,
  412. value
  413. };
  414. }
  415. /**
  416. * Sets the visibility of the prejoin page when a client reload
  417. * is triggered as a result of call migration initiated by Jicofo.
  418. *
  419. * @param {boolean} value - The visibility value.
  420. * @returns {Object}
  421. */
  422. export function setSkipPrejoinOnReload(value: boolean) {
  423. return {
  424. type: SET_SKIP_PREJOIN_RELOAD,
  425. value
  426. };
  427. }
  428. /**
  429. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  430. *
  431. * @param {boolean} value - The value.
  432. * @returns {Object}
  433. */
  434. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  435. return {
  436. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  437. value
  438. };
  439. }
  440. /**
  441. * Action used to set the initial errors after creating the tracks.
  442. *
  443. * @param {Object} value - The track errors.
  444. * @returns {Object}
  445. */
  446. export function setPrejoinDeviceErrors(value: Object) {
  447. return {
  448. type: SET_PREJOIN_DEVICE_ERRORS,
  449. value
  450. };
  451. }
  452. /**
  453. * Action used to set the visibility of the prejoin page.
  454. *
  455. * @param {boolean} value - The value.
  456. * @returns {Object}
  457. */
  458. export function setPrejoinPageVisibility(value: boolean) {
  459. return {
  460. type: SET_PREJOIN_PAGE_VISIBILITY,
  461. value
  462. };
  463. }