You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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