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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. dispatch(replaceLocalTrack(oldTrack, newTrack)).then(() => {
  312. dispatch(updateSettings({
  313. micDeviceId: newTrack.getDeviceId()
  314. }));
  315. });
  316. } catch (err) {
  317. dispatch(setDeviceStatusWarning('prejoin.audioTrackError'));
  318. logger.log('Error replacing audio track', err);
  319. }
  320. };
  321. }
  322. /**
  323. * Creates a new video track based on a device id and replaces the current one.
  324. *
  325. * @param {string} deviceId - The deviceId of the camera.
  326. * @returns {Function}
  327. */
  328. export function replaceVideoTrackById(deviceId: Object) {
  329. return async (dispatch: Function, getState: Function) => {
  330. try {
  331. const tracks = getState()['features/base/tracks'];
  332. const wasVideoMuted = isVideoMutedByUser(getState());
  333. const [ newTrack ] = await createLocalTracksF(
  334. { cameraDeviceId: deviceId,
  335. devices: [ 'video' ] },
  336. { dispatch,
  337. getState }
  338. );
  339. const oldTrack = getLocalVideoTrack(tracks)?.jitsiTrack;
  340. dispatch(replaceLocalTrack(oldTrack, newTrack)).then(() => {
  341. dispatch(updateSettings({
  342. cameraDeviceId: newTrack.getDeviceId()
  343. }));
  344. });
  345. wasVideoMuted && newTrack.mute();
  346. } catch (err) {
  347. dispatch(setDeviceStatusWarning('prejoin.videoTrackError'));
  348. logger.log('Error replacing video track', err);
  349. }
  350. };
  351. }
  352. /**
  353. * Sets the device status as OK with the corresponding text.
  354. *
  355. * @param {string} deviceStatusText - The text to be set.
  356. * @returns {Object}
  357. */
  358. export function setDeviceStatusOk(deviceStatusText: string) {
  359. return {
  360. type: SET_DEVICE_STATUS,
  361. value: {
  362. deviceStatusText,
  363. deviceStatusType: 'ok'
  364. }
  365. };
  366. }
  367. /**
  368. * Sets the device status as 'warning' with the corresponding text.
  369. *
  370. * @param {string} deviceStatusText - The text to be set.
  371. * @returns {Object}
  372. */
  373. export function setDeviceStatusWarning(deviceStatusText: string) {
  374. return {
  375. type: SET_DEVICE_STATUS,
  376. value: {
  377. deviceStatusText,
  378. deviceStatusType: 'warning'
  379. }
  380. };
  381. }
  382. /**
  383. * Action used to set the dial out status.
  384. *
  385. * @param {string} value - The status.
  386. * @returns {Object}
  387. */
  388. function setDialOutStatus(value: string) {
  389. return {
  390. type: SET_DIALOUT_STATUS,
  391. value
  392. };
  393. }
  394. /**
  395. * Action used to set the dial out country.
  396. *
  397. * @param {{ name: string, dialCode: string, code: string }} value - The country.
  398. * @returns {Object}
  399. */
  400. export function setDialOutCountry(value: Object) {
  401. return {
  402. type: SET_DIALOUT_COUNTRY,
  403. value
  404. };
  405. }
  406. /**
  407. * Action used to set the stance of the display name.
  408. *
  409. * @returns {Object}
  410. */
  411. export function setPrejoinDisplayNameRequired() {
  412. return {
  413. type: SET_PREJOIN_DISPLAY_NAME_REQUIRED
  414. };
  415. }
  416. /**
  417. * Action used to set the dial out number.
  418. *
  419. * @param {string} value - The dial out number.
  420. * @returns {Object}
  421. */
  422. export function setDialOutNumber(value: string) {
  423. return {
  424. type: SET_DIALOUT_NUMBER,
  425. value
  426. };
  427. }
  428. /**
  429. * Sets the visibility of the prejoin page when a client reload
  430. * is triggered as a result of call migration initiated by Jicofo.
  431. *
  432. * @param {boolean} value - The visibility value.
  433. * @returns {Object}
  434. */
  435. export function setSkipPrejoinOnReload(value: boolean) {
  436. return {
  437. type: SET_SKIP_PREJOIN_RELOAD,
  438. value
  439. };
  440. }
  441. /**
  442. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  443. *
  444. * @param {boolean} value - The value.
  445. * @returns {Object}
  446. */
  447. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  448. return {
  449. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  450. value
  451. };
  452. }
  453. /**
  454. * Action used to set data from precall test.
  455. *
  456. * @param {Object} value - The precall test results.
  457. * @returns {Object}
  458. */
  459. export function setPrecallTestResults(value: Object) {
  460. return {
  461. type: SET_PRECALL_TEST_RESULTS,
  462. value
  463. };
  464. }
  465. /**
  466. * Action used to set the initial errors after creating the tracks.
  467. *
  468. * @param {Object} value - The track errors.
  469. * @returns {Object}
  470. */
  471. export function setPrejoinDeviceErrors(value: Object) {
  472. return {
  473. type: SET_PREJOIN_DEVICE_ERRORS,
  474. value
  475. };
  476. }
  477. /**
  478. * Action used to set the visibility of the prejoin page.
  479. *
  480. * @param {boolean} value - The value.
  481. * @returns {Object}
  482. */
  483. export function setPrejoinPageVisibility(value: boolean) {
  484. return {
  485. type: SET_PREJOIN_PAGE_VISIBILITY,
  486. value
  487. };
  488. }