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.

functions.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // @flow
  2. import { toState } from '../redux';
  3. import { loadScript } from '../util';
  4. import JitsiMeetJS from './_';
  5. declare var APP: Object;
  6. const JitsiConferenceErrors = JitsiMeetJS.errors.conference;
  7. const JitsiConnectionErrors = JitsiMeetJS.errors.connection;
  8. const logger = require('jitsi-meet-logger').getLogger(__filename);
  9. /**
  10. * Creates a {@link JitsiLocalTrack} model from the given device id.
  11. *
  12. * @param {string} type - The media type of track being created. Expected values
  13. * are "video" or "audio".
  14. * @param {string} deviceId - The id of the target media source.
  15. * @returns {Promise<JitsiLocalTrack>}
  16. */
  17. export function createLocalTrack(type: string, deviceId: string) {
  18. return (
  19. JitsiMeetJS.createLocalTracks({
  20. cameraDeviceId: deviceId,
  21. devices: [ type ],
  22. // eslint-disable-next-line camelcase
  23. firefox_fake_device:
  24. window.config && window.config.firefox_fake_device,
  25. micDeviceId: deviceId
  26. })
  27. .then(([ jitsiLocalTrack ]) => jitsiLocalTrack));
  28. }
  29. /**
  30. * Determines whether analytics is enabled in a specific redux {@code store}.
  31. *
  32. * @param {Function|Object} stateful - The redux store, state, or
  33. * {@code getState} function.
  34. * @returns {boolean} If analytics is enabled, {@code true}; {@code false},
  35. * otherwise.
  36. */
  37. export function isAnalyticsEnabled(stateful: Function | Object) {
  38. const {
  39. analyticsScriptUrls,
  40. disableThirdPartyRequests
  41. } = toState(stateful)['features/base/config'];
  42. return (
  43. !disableThirdPartyRequests
  44. && Array.isArray(analyticsScriptUrls)
  45. && Boolean(analyticsScriptUrls.length));
  46. }
  47. /**
  48. * Determines whether a specific {@link JitsiConferenceErrors} instance
  49. * indicates a fatal {@link JitsiConference} error.
  50. *
  51. * FIXME Figure out the category of errors defined by the function and describe
  52. * that category. I've currently named the category fatal because it appears to
  53. * be used in the cases of unrecoverable errors that necessitate a reload.
  54. *
  55. * @param {Object|string} error - The {@code JitsiConferenceErrors} instance to
  56. * categorize/classify or an {@link Error}-like object.
  57. * @returns {boolean} If the specified {@code JitsiConferenceErrors} instance
  58. * indicates a fatal {@code JitsiConference} error, {@code true}; otherwise,
  59. * {@code false}.
  60. */
  61. export function isFatalJitsiConferenceError(error: Object | string) {
  62. if (typeof error !== 'string') {
  63. error = error.name; // eslint-disable-line no-param-reassign
  64. }
  65. return (
  66. error === JitsiConferenceErrors.FOCUS_DISCONNECTED
  67. || error === JitsiConferenceErrors.FOCUS_LEFT
  68. || error === JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  69. }
  70. /**
  71. * Determines whether a specific {@link JitsiConnectionErrors} instance
  72. * indicates a fatal {@link JitsiConnection} error.
  73. *
  74. * FIXME Figure out the category of errors defined by the function and describe
  75. * that category. I've currently named the category fatal because it appears to
  76. * be used in the cases of unrecoverable errors that necessitate a reload.
  77. *
  78. * @param {Object|string} error - The {@code JitsiConnectionErrors} instance to
  79. * categorize/classify or an {@link Error}-like object.
  80. * @returns {boolean} If the specified {@code JitsiConnectionErrors} instance
  81. * indicates a fatal {@code JitsiConnection} error, {@code true}; otherwise,
  82. * {@code false}.
  83. */
  84. export function isFatalJitsiConnectionError(error: Object | string) {
  85. if (typeof error !== 'string') {
  86. error = error.name; // eslint-disable-line no-param-reassign
  87. }
  88. return (
  89. error === JitsiConnectionErrors.CONNECTION_DROPPED_ERROR
  90. || error === JitsiConnectionErrors.OTHER_ERROR
  91. || error === JitsiConnectionErrors.SERVER_ERROR);
  92. }
  93. /**
  94. * Loads config.js from a specific remote server.
  95. *
  96. * @param {string} url - The URL to load.
  97. * @param {number} [timeout] - The timeout in milliseconds for the {@code url}
  98. * to load. If not specified, a default value deemed appropriate for the purpose
  99. * is used.
  100. * @returns {Promise<Object>}
  101. */
  102. export function loadConfig(
  103. url: string,
  104. timeout: ?number = 10 /* seconds */ * 1000 /* in milliseconds */
  105. ): Promise<Object> {
  106. let promise;
  107. if (typeof APP === 'undefined') {
  108. promise
  109. = loadScript(url, timeout)
  110. .then(() => {
  111. const { config } = window;
  112. // We don't want to pollute the global scope.
  113. window.config = undefined;
  114. if (typeof config !== 'object') {
  115. throw new Error('window.config is not an object');
  116. }
  117. return config;
  118. })
  119. .catch(err => {
  120. logger.error(`Failed to load config from ${url}`, err);
  121. throw err;
  122. });
  123. } else {
  124. // Return "the config.js file" from the global scope - that is how the
  125. // Web app on both the client and the server was implemented before the
  126. // React Native app was even conceived.
  127. promise = Promise.resolve(window.config);
  128. }
  129. return promise;
  130. }