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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. return !toState(stateful)['features/base/config'].disableThirdPartyRequests;
  39. }
  40. /**
  41. * Determines whether a specific {@link JitsiConferenceErrors} instance
  42. * indicates a fatal {@link JitsiConference} error.
  43. *
  44. * FIXME Figure out the category of errors defined by the function and describe
  45. * that category. I've currently named the category fatal because it appears to
  46. * be used in the cases of unrecoverable errors that necessitate a reload.
  47. *
  48. * @param {Object|string} error - The {@code JitsiConferenceErrors} instance to
  49. * categorize/classify or an {@link Error}-like object.
  50. * @returns {boolean} If the specified {@code JitsiConferenceErrors} instance
  51. * indicates a fatal {@code JitsiConference} error, {@code true}; otherwise,
  52. * {@code false}.
  53. */
  54. export function isFatalJitsiConferenceError(error: Object | string) {
  55. if (typeof error !== 'string') {
  56. error = error.name; // eslint-disable-line no-param-reassign
  57. }
  58. return (
  59. error === JitsiConferenceErrors.FOCUS_DISCONNECTED
  60. || error === JitsiConferenceErrors.FOCUS_LEFT
  61. || error === JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  62. }
  63. /**
  64. * Determines whether a specific {@link JitsiConnectionErrors} instance
  65. * indicates a fatal {@link JitsiConnection} error.
  66. *
  67. * FIXME Figure out the category of errors defined by the function and describe
  68. * that category. I've currently named the category fatal because it appears to
  69. * be used in the cases of unrecoverable errors that necessitate a reload.
  70. *
  71. * @param {Object|string} error - The {@code JitsiConnectionErrors} instance to
  72. * categorize/classify or an {@link Error}-like object.
  73. * @returns {boolean} If the specified {@code JitsiConnectionErrors} instance
  74. * indicates a fatal {@code JitsiConnection} error, {@code true}; otherwise,
  75. * {@code false}.
  76. */
  77. export function isFatalJitsiConnectionError(error: Object | string) {
  78. if (typeof error !== 'string') {
  79. error = error.name; // eslint-disable-line no-param-reassign
  80. }
  81. return (
  82. error === JitsiConnectionErrors.CONNECTION_DROPPED_ERROR
  83. || error === JitsiConnectionErrors.OTHER_ERROR
  84. || error === JitsiConnectionErrors.SERVER_ERROR);
  85. }
  86. /**
  87. * Loads config.js from a specific remote server.
  88. *
  89. * @param {string} url - The URL to load.
  90. * @param {number} [timeout] - The timeout in milliseconds for the {@code url}
  91. * to load. If not specified, a default value deemed appropriate for the purpose
  92. * is used.
  93. * @returns {Promise<Object>}
  94. */
  95. export function loadConfig(
  96. url: string,
  97. timeout: ?number = 10 /* seconds */ * 1000 /* in milliseconds */
  98. ): Promise<Object> {
  99. let promise;
  100. if (typeof APP === 'undefined') {
  101. promise
  102. = loadScript(url, timeout)
  103. .then(() => {
  104. const { config } = window;
  105. // We don't want to pollute the global scope.
  106. window.config = undefined;
  107. if (typeof config !== 'object') {
  108. throw new Error('window.config is not an object');
  109. }
  110. return config;
  111. })
  112. .catch(err => {
  113. logger.error(`Failed to load config from ${url}`, err);
  114. throw err;
  115. });
  116. } else {
  117. // Return "the config.js file" from the global scope - that is how the
  118. // Web app on both the client and the server was implemented before the
  119. // React Native app was even conceived.
  120. promise = Promise.resolve(window.config);
  121. }
  122. return promise;
  123. }