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

functions.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* @flow */
  2. import _ from 'lodash';
  3. import { _CONFIG_STORE_PREFIX } from './constants';
  4. import parseURLParams from './parseURLParams';
  5. declare var $: Object;
  6. /**
  7. * The config keys to whitelist, the keys that can be overridden.
  8. * Currently we can only whitelist the first part of the properties, like
  9. * 'p2p.useStunTurn' and 'p2p.enabled' we whitelist all p2p options.
  10. * The whitelist is used only for config.js.
  11. *
  12. * @private
  13. * @type Array
  14. */
  15. const WHITELISTED_KEYS = [
  16. '_peerConnStatusOutOfLastNTimeout',
  17. '_peerConnStatusRtcMuteTimeout',
  18. 'abTesting',
  19. 'autoRecord',
  20. 'autoRecordToken',
  21. 'avgRtpStatsN',
  22. 'callFlowsEnabled',
  23. 'callStatsConfIDNamespace',
  24. 'callStatsID',
  25. 'callStatsSecret',
  26. /**
  27. * The display name of the CallKit call representing the conference/meeting
  28. * associated with this config.js including while the call is ongoing in the
  29. * UI presented by CallKit and in the system-wide call history. The property
  30. * is meant for use cases in which the room name is not desirable as a
  31. * display name for CallKit purposes and the desired display name is not
  32. * provided in the form of a JWT callee. As the value is associated with a
  33. * conference/meeting, the value makes sense not as a deployment-wide
  34. * configuration, only as a runtime configuration override/overwrite
  35. * provided by, for example, Jitsi Meet SDK for iOS.
  36. *
  37. * @type string
  38. */
  39. 'callDisplayName',
  40. /**
  41. * The handle
  42. * ({@link https://developer.apple.com/documentation/callkit/cxhandle}) of
  43. * the CallKit call representing the conference/meeting associated with this
  44. * config.js. The property is meant for use cases in which the room URL is
  45. * not desirable as the handle for CallKit purposes. As the value is
  46. * associated with a conference/meeting, the value makes sense not as a
  47. * deployment-wide configuration, only as a runtime configuration
  48. * override/overwrite provided by, for example, Jitsi Meet SDK for iOS.
  49. *
  50. * @type string
  51. */
  52. 'callHandle',
  53. /**
  54. * The UUID of the CallKit call representing the conference/meeting
  55. * associated with this config.js. The property is meant for use cases in
  56. * which Jitsi Meet is to work with a CallKit call created outside of Jitsi
  57. * Meet and to be adopted by Jitsi Meet such as, for example, an incoming
  58. * and/or outgoing CallKit call created by Jitsi Meet SDK for iOS
  59. * clients/consumers prior to giving control to Jitsi Meet. As the value is
  60. * associated with a conference/meeting, the value makes sense not as a
  61. * deployment-wide configuration, only as a runtime configuration
  62. * override/overwrite provided by, for example, Jitsi Meet SDK for iOS.
  63. *
  64. * @type string
  65. */
  66. 'callUUID',
  67. 'channelLastN',
  68. 'constraints',
  69. 'debug',
  70. 'debugAudioLevels',
  71. 'defaultLanguage',
  72. 'desktopSharingChromeDisabled',
  73. 'desktopSharingChromeExtId',
  74. 'desktopSharingChromeMinExtVersion',
  75. 'desktopSharingChromeSources',
  76. 'desktopSharingFrameRate',
  77. 'desktopSharingFirefoxDisabled',
  78. 'desktopSharingSources',
  79. 'disable1On1Mode',
  80. 'disableAEC',
  81. 'disableAGC',
  82. 'disableAP',
  83. 'disableAudioLevels',
  84. 'disableH264',
  85. 'disableHPF',
  86. 'disableNS',
  87. 'disableRemoteControl',
  88. 'disableRtx',
  89. 'disableSuspendVideo',
  90. 'displayJids',
  91. 'e2eping',
  92. 'enableDisplayNameInStats',
  93. 'enableLayerSuspension',
  94. 'enableLipSync',
  95. 'enableLocalVideoFlip',
  96. 'enableRemb',
  97. 'enableStatsID',
  98. 'enableTalkWhileMuted',
  99. 'enableTcc',
  100. 'etherpad_base',
  101. 'failICE',
  102. 'fileRecordingsEnabled',
  103. 'firefox_fake_device',
  104. 'forceJVB121Ratio',
  105. 'gatherStats',
  106. 'googleApiApplicationClientID',
  107. 'hiddenDomain',
  108. 'hosts',
  109. 'iAmRecorder',
  110. 'iAmSipGateway',
  111. 'iceTransportPolicy',
  112. 'ignoreStartMuted',
  113. 'liveStreamingEnabled',
  114. 'minParticipants',
  115. 'nick',
  116. 'openBridgeChannel',
  117. 'p2p',
  118. 'preferH264',
  119. 'requireDisplayName',
  120. 'resolution',
  121. 'startAudioMuted',
  122. 'startAudioOnly',
  123. 'startBitrate',
  124. 'startScreenSharing',
  125. 'startVideoMuted',
  126. 'startWithAudioMuted',
  127. 'startWithVideoMuted',
  128. 'testing',
  129. 'useIPv6',
  130. 'useNicks',
  131. 'useStunTurn',
  132. 'webrtcIceTcpDisable',
  133. 'webrtcIceUdpDisable'
  134. ];
  135. const logger = require('jitsi-meet-logger').getLogger(__filename);
  136. // XXX The functions getRoomName and parseURLParams are split out of
  137. // functions.js because they are bundled in both app.bundle and
  138. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  139. // want all functions to be bundled in do_external_connect.
  140. export { default as getRoomName } from './getRoomName';
  141. export { parseURLParams };
  142. /**
  143. * Promise wrapper on obtain config method. When HttpConfigFetch will be moved
  144. * to React app it's better to use load config instead.
  145. *
  146. * @param {string} location - URL of the domain from which the config is to be
  147. * obtained.
  148. * @param {string} room - Room name.
  149. * @private
  150. * @returns {Promise<void>}
  151. */
  152. export function obtainConfig(location: string, room: string): Promise<void> {
  153. return new Promise((resolve, reject) =>
  154. _obtainConfig(location, room, (success, error) => {
  155. success ? resolve() : reject(error);
  156. })
  157. );
  158. }
  159. /**
  160. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  161. * of the room is included in JSON format:
  162. * {
  163. * "rooomName": "someroom12345"
  164. * }.
  165. *
  166. * @param {string} endpoint - The name of HTTP endpoint to which to send
  167. * the HTTP POST request.
  168. * @param {string} roomName - The name of the conference room for which config
  169. * is requested.
  170. * @param {Function} complete - The callback to invoke upon success or failure.
  171. * @returns {void}
  172. */
  173. function _obtainConfig(endpoint: string, roomName: string, complete: Function) {
  174. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  175. $.ajax(
  176. endpoint,
  177. {
  178. contentType: 'application/json',
  179. data: JSON.stringify({ roomName }),
  180. dataType: 'json',
  181. method: 'POST',
  182. error(jqXHR, textStatus, errorThrown) {
  183. logger.error('Get config error: ', jqXHR, errorThrown);
  184. complete(false, `Get config response status: ${textStatus}`);
  185. },
  186. success(data) {
  187. const { config, interfaceConfig, loggingConfig } = window;
  188. try {
  189. overrideConfigJSON(
  190. config, interfaceConfig, loggingConfig,
  191. data);
  192. complete(true);
  193. } catch (e) {
  194. logger.error('Parse config error: ', e);
  195. complete(false, e);
  196. }
  197. }
  198. }
  199. );
  200. }
  201. /* eslint-disable max-params, no-shadow */
  202. /**
  203. * Overrides JSON properties in {@code config} and
  204. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  205. * Overrides only the whitelisted keys.
  206. *
  207. * @param {Object} config - The config Object in which we'll be overriding
  208. * properties.
  209. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  210. * be overriding properties.
  211. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  212. * overriding properties.
  213. * @param {Object} json - Object containing configuration properties.
  214. * Destination object is selected based on root property name:
  215. * {
  216. * config: {
  217. * // config.js properties here
  218. * },
  219. * interfaceConfig: {
  220. * // interface_config.js properties here
  221. * },
  222. * loggingConfig: {
  223. * // logging_config.js properties here
  224. * }
  225. * }.
  226. * @returns {void}
  227. */
  228. export function overrideConfigJSON(
  229. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  230. json: Object) {
  231. for (const configName of Object.keys(json)) {
  232. let configObj;
  233. if (configName === 'config') {
  234. configObj = config;
  235. } else if (configName === 'interfaceConfig') {
  236. configObj = interfaceConfig;
  237. } else if (configName === 'loggingConfig') {
  238. configObj = loggingConfig;
  239. }
  240. if (configObj) {
  241. const configJSON
  242. = _getWhitelistedJSON(configName, json[configName]);
  243. if (!_.isEmpty(configJSON)) {
  244. logger.info(
  245. `Extending ${configName} with: ${
  246. JSON.stringify(configJSON)}`);
  247. // eslint-disable-next-line arrow-body-style
  248. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  249. // XXX We don't want to merge the arrays, we want to
  250. // overwrite them.
  251. return Array.isArray(oldValue) ? newValue : undefined;
  252. });
  253. }
  254. }
  255. }
  256. }
  257. /* eslint-enable max-params, no-shadow */
  258. /**
  259. * Whitelist only config.js, skips this for others configs
  260. * (interfaceConfig, loggingConfig).
  261. * Only extracts overridden values for keys we allow to be overridden.
  262. *
  263. * @param {string} configName - The config name, one of config,
  264. * interfaceConfig, loggingConfig.
  265. * @param {Object} configJSON - The object with keys and values to override.
  266. * @private
  267. * @returns {Object} - The result object only with the keys
  268. * that are whitelisted.
  269. */
  270. function _getWhitelistedJSON(configName, configJSON) {
  271. if (configName !== 'config') {
  272. return configJSON;
  273. }
  274. return _.pick(configJSON, WHITELISTED_KEYS);
  275. }
  276. /**
  277. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  278. * previously downloaded from a specific {@code baseURL} and stored with
  279. * {@link storeConfig}.
  280. *
  281. * @param {string} baseURL - The base URL from which the config.js was
  282. * previously downloaded and stored with {@code storeConfig}.
  283. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  284. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  285. * otherwise, {@code undefined}.
  286. */
  287. export function restoreConfig(baseURL: string): ?Object {
  288. let storage;
  289. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  290. try {
  291. // XXX Even reading the property localStorage of window may throw an
  292. // error (which is user agent-specific behavior).
  293. storage = window.localStorage;
  294. const config = storage.getItem(key);
  295. if (config) {
  296. return JSON.parse(config) || undefined;
  297. }
  298. } catch (e) {
  299. // Somehow incorrect data ended up in the storage. Clean it up.
  300. storage && storage.removeItem(key);
  301. }
  302. return undefined;
  303. }
  304. /* eslint-disable max-params */
  305. /**
  306. * Inspects the hash part of the location URI and overrides values specified
  307. * there in the corresponding config objects given as the arguments. The syntax
  308. * is: {@code https://server.com/room#config.debug=true
  309. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  310. *
  311. * In the hash part each parameter will be parsed to JSON and then the root
  312. * object will be matched with the corresponding config object given as the
  313. * argument to this function.
  314. *
  315. * @param {Object} config - This is the general config.
  316. * @param {Object} interfaceConfig - This is the interface config.
  317. * @param {Object} loggingConfig - The logging config.
  318. * @param {URI} location - The new location to which the app is navigating to.
  319. * @returns {void}
  320. */
  321. export function setConfigFromURLParams(
  322. config: ?Object,
  323. interfaceConfig: ?Object,
  324. loggingConfig: ?Object,
  325. location: Object) {
  326. const params = parseURLParams(location);
  327. const json = {};
  328. // At this point we have:
  329. // params = {
  330. // "config.disableAudioLevels": false,
  331. // "config.channelLastN": -1,
  332. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  333. // }
  334. // We want to have:
  335. // json = {
  336. // config: {
  337. // "disableAudioLevels": false,
  338. // "channelLastN": -1
  339. // },
  340. // interfaceConfig: {
  341. // "APP_NAME": "Jitsi Meet"
  342. // }
  343. // }
  344. config && (json.config = {});
  345. interfaceConfig && (json.interfaceConfig = {});
  346. loggingConfig && (json.loggingConfig = {});
  347. for (const param of Object.keys(params)) {
  348. let base = json;
  349. const names = param.split('.');
  350. const last = names.pop();
  351. for (const name of names) {
  352. base = base[name] = base[name] || {};
  353. }
  354. base[last] = params[param];
  355. }
  356. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  357. }
  358. /* eslint-enable max-params */