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

actions.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. dispatch(setPrejoinPageVisibility(false));
  225. APP.conference.prejoinStart(jitsiTracks);
  226. };
  227. }
  228. /**
  229. * Action used to set the flag for joining operation in progress.
  230. *
  231. * @param {boolean} value - The config options that override the default ones (if any).
  232. * @returns {Function}
  233. */
  234. export function setJoiningInProgress(value: boolean) {
  235. return {
  236. type: PREJOIN_JOINING_IN_PROGRESS,
  237. value
  238. };
  239. }
  240. /**
  241. * Joins the conference without audio.
  242. *
  243. * @returns {Function}
  244. */
  245. export function joinConferenceWithoutAudio() {
  246. return async function(dispatch: Function, getState: Function) {
  247. const state = getState();
  248. const { joiningInProgress } = state['features/prejoin'];
  249. if (joiningInProgress) {
  250. return;
  251. }
  252. dispatch(setJoiningInProgress(true));
  253. const tracks = state['features/base/tracks'];
  254. const audioTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  255. if (audioTrack) {
  256. try {
  257. await dispatch(replaceLocalTrack(audioTrack, null));
  258. } catch (error) {
  259. logger.error(`Failed to replace local audio with null: ${error}`);
  260. }
  261. }
  262. dispatch(joinConference({
  263. startSilent: true
  264. }, true));
  265. };
  266. }
  267. /**
  268. * Initializes the 'precallTest' and executes one test, storing the results.
  269. *
  270. * @param {Object} conferenceOptions - The conference options.
  271. * @returns {Function}
  272. */
  273. export function makePrecallTest(conferenceOptions: Object) {
  274. return async function(dispatch: Function) {
  275. try {
  276. await JitsiMeetJS.precallTest.init(conferenceOptions);
  277. const results = await JitsiMeetJS.precallTest.execute();
  278. dispatch(setPrecallTestResults(results));
  279. } catch (error) {
  280. logger.debug('Failed to execute pre call test - ', error);
  281. }
  282. };
  283. }
  284. /**
  285. * Opens an external page with all the dial in numbers.
  286. *
  287. * @returns {Function}
  288. */
  289. export function openDialInPage() {
  290. return function(dispatch: Function, getState: Function) {
  291. const dialInPage = getDialInfoPageURL(getState());
  292. openURLInBrowser(dialInPage, true);
  293. };
  294. }
  295. /**
  296. * Action used to signal that the prejoin page has been initialized.
  297. *
  298. * @returns {Object}
  299. */
  300. function prejoinInitialized() {
  301. return {
  302. type: PREJOIN_INITIALIZED
  303. };
  304. }
  305. /**
  306. * Creates a new audio track based on a device id and replaces the current one.
  307. *
  308. * @param {string} deviceId - The deviceId of the microphone.
  309. * @returns {Function}
  310. */
  311. export function replaceAudioTrackById(deviceId: string) {
  312. return async (dispatch: Function, getState: Function) => {
  313. try {
  314. const tracks = getState()['features/base/tracks'];
  315. const newTrack = await createLocalTrack('audio', deviceId);
  316. const oldTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  317. dispatch(replaceLocalTrack(oldTrack, newTrack));
  318. } catch (err) {
  319. dispatch(setDeviceStatusWarning('prejoin.audioTrackError'));
  320. logger.log('Error replacing audio track', err);
  321. }
  322. };
  323. }
  324. /**
  325. * Creates a new video track based on a device id and replaces the current one.
  326. *
  327. * @param {string} deviceId - The deviceId of the camera.
  328. * @returns {Function}
  329. */
  330. export function replaceVideoTrackById(deviceId: Object) {
  331. return async (dispatch: Function, getState: Function) => {
  332. try {
  333. const tracks = getState()['features/base/tracks'];
  334. const wasVideoMuted = isVideoMutedByUser(getState());
  335. const [ newTrack ] = await createLocalTracksF(
  336. { cameraDeviceId: deviceId,
  337. devices: [ 'video' ] },
  338. { dispatch,
  339. getState }
  340. );
  341. const oldTrack = getLocalVideoTrack(tracks)?.jitsiTrack;
  342. dispatch(replaceLocalTrack(oldTrack, newTrack));
  343. wasVideoMuted && newTrack.mute();
  344. } catch (err) {
  345. dispatch(setDeviceStatusWarning('prejoin.videoTrackError'));
  346. logger.log('Error replacing video track', err);
  347. }
  348. };
  349. }
  350. /**
  351. * Sets the device status as OK with the corresponding text.
  352. *
  353. * @param {string} deviceStatusText - The text to be set.
  354. * @returns {Object}
  355. */
  356. export function setDeviceStatusOk(deviceStatusText: string) {
  357. return {
  358. type: SET_DEVICE_STATUS,
  359. value: {
  360. deviceStatusText,
  361. deviceStatusType: 'ok'
  362. }
  363. };
  364. }
  365. /**
  366. * Sets the device status as 'warning' with the corresponding text.
  367. *
  368. * @param {string} deviceStatusText - The text to be set.
  369. * @returns {Object}
  370. */
  371. export function setDeviceStatusWarning(deviceStatusText: string) {
  372. return {
  373. type: SET_DEVICE_STATUS,
  374. value: {
  375. deviceStatusText,
  376. deviceStatusType: 'warning'
  377. }
  378. };
  379. }
  380. /**
  381. * Action used to set the dial out status.
  382. *
  383. * @param {string} value - The status.
  384. * @returns {Object}
  385. */
  386. function setDialOutStatus(value: string) {
  387. return {
  388. type: SET_DIALOUT_STATUS,
  389. value
  390. };
  391. }
  392. /**
  393. * Action used to set the dial out country.
  394. *
  395. * @param {{ name: string, dialCode: string, code: string }} value - The country.
  396. * @returns {Object}
  397. */
  398. export function setDialOutCountry(value: Object) {
  399. return {
  400. type: SET_DIALOUT_COUNTRY,
  401. value
  402. };
  403. }
  404. /**
  405. * Action used to set the stance of the display name.
  406. *
  407. * @returns {Object}
  408. */
  409. export function setPrejoinDisplayNameRequired() {
  410. return {
  411. type: SET_PREJOIN_DISPLAY_NAME_REQUIRED
  412. };
  413. }
  414. /**
  415. * Action used to set the dial out number.
  416. *
  417. * @param {string} value - The dial out number.
  418. * @returns {Object}
  419. */
  420. export function setDialOutNumber(value: string) {
  421. return {
  422. type: SET_DIALOUT_NUMBER,
  423. value
  424. };
  425. }
  426. /**
  427. * Sets the visibility of the prejoin page for future uses.
  428. *
  429. * @param {boolean} value - The visibility value.
  430. * @returns {Object}
  431. */
  432. export function setSkipPrejoin(value: boolean) {
  433. return {
  434. type: SET_SKIP_PREJOIN,
  435. value
  436. };
  437. }
  438. /**
  439. * Sets the visibility of the prejoin page when a client reload
  440. * is triggered as a result of call migration initiated by Jicofo.
  441. *
  442. * @param {boolean} value - The visibility value.
  443. * @returns {Object}
  444. */
  445. export function setSkipPrejoinOnReload(value: boolean) {
  446. return {
  447. type: SET_SKIP_PREJOIN_RELOAD,
  448. value
  449. };
  450. }
  451. /**
  452. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  453. *
  454. * @param {boolean} value - The value.
  455. * @returns {Object}
  456. */
  457. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  458. return {
  459. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  460. value
  461. };
  462. }
  463. /**
  464. * Action used to set data from precall test.
  465. *
  466. * @param {Object} value - The precall test results.
  467. * @returns {Object}
  468. */
  469. export function setPrecallTestResults(value: Object) {
  470. return {
  471. type: SET_PRECALL_TEST_RESULTS,
  472. value
  473. };
  474. }
  475. /**
  476. * Action used to set the initial errors after creating the tracks.
  477. *
  478. * @param {Object} value - The track errors.
  479. * @returns {Object}
  480. */
  481. export function setPrejoinDeviceErrors(value: Object) {
  482. return {
  483. type: SET_PREJOIN_DEVICE_ERRORS,
  484. value
  485. };
  486. }
  487. /**
  488. * Action used to set the visibility of the prejoin page.
  489. *
  490. * @param {boolean} value - The value.
  491. * @returns {Object}
  492. */
  493. export function setPrejoinPageVisibility(value: boolean) {
  494. return {
  495. type: SET_PREJOIN_PAGE_VISIBILITY,
  496. value
  497. };
  498. }