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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // @flow
  2. declare var JitsiMeetJS: Object;
  3. declare var APP: Object;
  4. import uuid from 'uuid';
  5. import { getDialOutStatusUrl, getDialOutUrl, updateConfig } from '../base/config';
  6. import { isIosMobileBrowser } from '../base/environment/utils';
  7. import { createLocalTrack } from '../base/lib-jitsi-meet';
  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 { 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,
  29. SET_SKIP_PREJOIN_RELOAD,
  30. SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  31. SET_PRECALL_TEST_RESULTS,
  32. SET_PREJOIN_DEVICE_ERRORS,
  33. SET_PREJOIN_PAGE_VISIBILITY,
  34. SET_DEVICE_STATUS
  35. } from './actionTypes';
  36. import {
  37. getFullDialOutNumber,
  38. getDialOutConferenceUrl,
  39. getDialOutCountry,
  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: Function, getState: Function) {
  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]));
  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. }));
  103. return onFail();
  104. }
  105. case DIAL_OUT_STATUS.FAILED: {
  106. dispatch(showErrorNotification({
  107. titleKey: 'prejoin.errorDialOutFailed'
  108. }));
  109. return onFail();
  110. }
  111. }
  112. } catch (err) {
  113. dispatch(showErrorNotification({
  114. titleKey: 'prejoin.errorDialOutStatus'
  115. }));
  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: Function, getState: Function) {
  133. const state = getState();
  134. const reqId = uuid.v4();
  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) {
  149. const notification = {
  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));
  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: Function) {
  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. * @returns {Function}
  188. */
  189. export function joinConference(options?: Object, ignoreJoiningInProgress: boolean = false) {
  190. return async function(dispatch: Function, getState: Function) {
  191. if (!ignoreJoiningInProgress) {
  192. const state = getState();
  193. const { joiningInProgress } = state['features/prejoin'];
  194. if (joiningInProgress) {
  195. return;
  196. }
  197. dispatch(setJoiningInProgress(true));
  198. }
  199. const state = getState();
  200. const { userSelectedSkipPrejoin } = state['features/prejoin'];
  201. let localTracks = getLocalTracks(state['features/base/tracks']);
  202. options && dispatch(updateConfig(options));
  203. userSelectedSkipPrejoin && dispatch(updateSettings({
  204. userSelectedSkipPrejoin
  205. }));
  206. // Do not signal audio/video tracks if the user joins muted.
  207. for (const track of localTracks) {
  208. // Always add the audio track on mobile Safari because of a known issue where audio playout doesn't happen
  209. // if the user joins audio and video muted.
  210. if (track.muted
  211. && !(isIosMobileBrowser() && track.jitsiTrack && track.jitsiTrack.getType() === MEDIA_TYPE.AUDIO)) {
  212. try {
  213. await dispatch(replaceLocalTrack(track.jitsiTrack, null));
  214. } catch (error) {
  215. logger.error(`Failed to replace local track (${track.jitsiTrack}) with null: ${error}`);
  216. }
  217. }
  218. }
  219. // Re-fetch the local tracks after muted tracks have been removed above.
  220. // This is needed, because the tracks are effectively disposed by the replaceLocalTrack and should not be used
  221. // anymore.
  222. localTracks = getLocalTracks(getState()['features/base/tracks']);
  223. const jitsiTracks = localTracks.map(t => t.jitsiTrack);
  224. APP.conference.prejoinStart(jitsiTracks);
  225. };
  226. }
  227. /**
  228. * Action used to set the flag for joining operation in progress.
  229. *
  230. * @param {boolean} value - The config options that override the default ones (if any).
  231. * @returns {Function}
  232. */
  233. export function setJoiningInProgress(value: boolean) {
  234. return {
  235. type: PREJOIN_JOINING_IN_PROGRESS,
  236. value
  237. };
  238. }
  239. /**
  240. * Joins the conference without audio.
  241. *
  242. * @returns {Function}
  243. */
  244. export function joinConferenceWithoutAudio() {
  245. return async function(dispatch: Function, getState: Function) {
  246. const state = getState();
  247. const { joiningInProgress } = state['features/prejoin'];
  248. if (joiningInProgress) {
  249. return;
  250. }
  251. dispatch(setJoiningInProgress(true));
  252. const tracks = state['features/base/tracks'];
  253. const audioTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  254. if (audioTrack) {
  255. try {
  256. await dispatch(replaceLocalTrack(audioTrack, null));
  257. } catch (error) {
  258. logger.error(`Failed to replace local audio with null: ${error}`);
  259. }
  260. }
  261. dispatch(joinConference({
  262. startSilent: true
  263. }, true));
  264. };
  265. }
  266. /**
  267. * Initializes the 'precallTest' and executes one test, storing the results.
  268. *
  269. * @param {Object} conferenceOptions - The conference options.
  270. * @returns {Function}
  271. */
  272. export function makePrecallTest(conferenceOptions: Object) {
  273. return async function(dispatch: Function) {
  274. try {
  275. await JitsiMeetJS.precallTest.init(conferenceOptions);
  276. const results = await JitsiMeetJS.precallTest.execute();
  277. dispatch(setPrecallTestResults(results));
  278. } catch (error) {
  279. logger.debug('Failed to execute pre call test - ', error);
  280. }
  281. };
  282. }
  283. /**
  284. * Opens an external page with all the dial in numbers.
  285. *
  286. * @returns {Function}
  287. */
  288. export function openDialInPage() {
  289. return function(dispatch: Function, getState: Function) {
  290. const dialInPage = getDialInfoPageURL(getState());
  291. openURLInBrowser(dialInPage, true);
  292. };
  293. }
  294. /**
  295. * Action used to signal that the prejoin page has been initialized.
  296. *
  297. * @returns {Object}
  298. */
  299. function prejoinInitialized() {
  300. return {
  301. type: PREJOIN_INITIALIZED
  302. };
  303. }
  304. /**
  305. * Creates a new audio track based on a device id and replaces the current one.
  306. *
  307. * @param {string} deviceId - The deviceId of the microphone.
  308. * @returns {Function}
  309. */
  310. export function replaceAudioTrackById(deviceId: string) {
  311. return async (dispatch: Function, getState: Function) => {
  312. try {
  313. const tracks = getState()['features/base/tracks'];
  314. const newTrack = await createLocalTrack('audio', deviceId);
  315. const oldTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  316. dispatch(replaceLocalTrack(oldTrack, newTrack));
  317. } catch (err) {
  318. dispatch(setDeviceStatusWarning('prejoin.audioTrackError'));
  319. logger.log('Error replacing audio track', err);
  320. }
  321. };
  322. }
  323. /**
  324. * Creates a new video track based on a device id and replaces the current one.
  325. *
  326. * @param {string} deviceId - The deviceId of the camera.
  327. * @returns {Function}
  328. */
  329. export function replaceVideoTrackById(deviceId: Object) {
  330. return async (dispatch: Function, getState: Function) => {
  331. try {
  332. const tracks = getState()['features/base/tracks'];
  333. const wasVideoMuted = isVideoMutedByUser(getState());
  334. const [ newTrack ] = await createLocalTracksF(
  335. { cameraDeviceId: deviceId,
  336. devices: [ 'video' ] },
  337. { dispatch,
  338. getState }
  339. );
  340. const oldTrack = getLocalVideoTrack(tracks)?.jitsiTrack;
  341. dispatch(replaceLocalTrack(oldTrack, newTrack));
  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 stance of the display name.
  405. *
  406. * @returns {Object}
  407. */
  408. export function setPrejoinDisplayNameRequired() {
  409. return {
  410. type: SET_PREJOIN_DISPLAY_NAME_REQUIRED
  411. };
  412. }
  413. /**
  414. * Action used to set the dial out number.
  415. *
  416. * @param {string} value - The dial out number.
  417. * @returns {Object}
  418. */
  419. export function setDialOutNumber(value: string) {
  420. return {
  421. type: SET_DIALOUT_NUMBER,
  422. value
  423. };
  424. }
  425. /**
  426. * Sets the visibility of the prejoin page for future uses.
  427. *
  428. * @param {boolean} value - The visibility value.
  429. * @returns {Object}
  430. */
  431. export function setSkipPrejoin(value: boolean) {
  432. return {
  433. type: SET_SKIP_PREJOIN,
  434. value
  435. };
  436. }
  437. /**
  438. * Sets the visibility of the prejoin page when a client reload
  439. * is triggered as a result of call migration initiated by Jicofo.
  440. *
  441. * @param {boolean} value - The visibility value.
  442. * @returns {Object}
  443. */
  444. export function setSkipPrejoinOnReload(value: boolean) {
  445. return {
  446. type: SET_SKIP_PREJOIN_RELOAD,
  447. value
  448. };
  449. }
  450. /**
  451. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  452. *
  453. * @param {boolean} value - The value.
  454. * @returns {Object}
  455. */
  456. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  457. return {
  458. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  459. value
  460. };
  461. }
  462. /**
  463. * Action used to set data from precall test.
  464. *
  465. * @param {Object} value - The precall test results.
  466. * @returns {Object}
  467. */
  468. export function setPrecallTestResults(value: Object) {
  469. return {
  470. type: SET_PRECALL_TEST_RESULTS,
  471. value
  472. };
  473. }
  474. /**
  475. * Action used to set the initial errors after creating the tracks.
  476. *
  477. * @param {Object} value - The track errors.
  478. * @returns {Object}
  479. */
  480. export function setPrejoinDeviceErrors(value: Object) {
  481. return {
  482. type: SET_PREJOIN_DEVICE_ERRORS,
  483. value
  484. };
  485. }
  486. /**
  487. * Action used to set the visibility of the prejoin page.
  488. *
  489. * @param {boolean} value - The value.
  490. * @returns {Object}
  491. */
  492. export function setPrejoinPageVisibility(value: boolean) {
  493. return {
  494. type: SET_PREJOIN_PAGE_VISIBILITY,
  495. value
  496. };
  497. }