Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

actions.web.ts 14KB

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