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

functions.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. 'enableDisplayNameInStats',
  92. 'enableLayerSuspension',
  93. 'enableLipSync',
  94. 'enableLocalVideoFlip',
  95. 'enableRemb',
  96. 'enableStatsID',
  97. 'enableTalkWhileMuted',
  98. 'enableTcc',
  99. 'etherpad_base',
  100. 'failICE',
  101. 'fileRecordingsEnabled',
  102. 'firefox_fake_device',
  103. 'forceJVB121Ratio',
  104. 'gatherStats',
  105. 'googleApiApplicationClientID',
  106. 'hiddenDomain',
  107. 'hosts',
  108. 'iAmRecorder',
  109. 'iAmSipGateway',
  110. 'iceTransportPolicy',
  111. 'ignoreStartMuted',
  112. 'liveStreamingEnabled',
  113. 'minParticipants',
  114. 'nick',
  115. 'openBridgeChannel',
  116. 'p2p',
  117. 'preferH264',
  118. 'requireDisplayName',
  119. 'resolution',
  120. 'startAudioMuted',
  121. 'startAudioOnly',
  122. 'startBitrate',
  123. 'startScreenSharing',
  124. 'startVideoMuted',
  125. 'startWithAudioMuted',
  126. 'startWithVideoMuted',
  127. 'testing',
  128. 'useIPv6',
  129. 'useNicks',
  130. 'useStunTurn',
  131. 'webrtcIceTcpDisable',
  132. 'webrtcIceUdpDisable'
  133. ];
  134. const logger = require('jitsi-meet-logger').getLogger(__filename);
  135. // XXX The functions getRoomName and parseURLParams are split out of
  136. // functions.js because they are bundled in both app.bundle and
  137. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  138. // want all functions to be bundled in do_external_connect.
  139. export { default as getRoomName } from './getRoomName';
  140. export { parseURLParams };
  141. /**
  142. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  143. * of the room is included in JSON format:
  144. * {
  145. * "rooomName": "someroom12345"
  146. * }.
  147. *
  148. * @param {string} endpoint - The name of HTTP endpoint to which to send
  149. * the HTTP POST request.
  150. * @param {string} roomName - The name of the conference room for which config
  151. * is requested.
  152. * @param {Function} complete - The callback to invoke upon success or failure.
  153. * @returns {void}
  154. */
  155. export function obtainConfig(
  156. endpoint: string,
  157. roomName: string,
  158. complete: Function) {
  159. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  160. $.ajax(
  161. endpoint,
  162. {
  163. contentType: 'application/json',
  164. data: JSON.stringify({ roomName }),
  165. dataType: 'json',
  166. method: 'POST',
  167. error(jqXHR, textStatus, errorThrown) {
  168. logger.error('Get config error: ', jqXHR, errorThrown);
  169. complete(false, `Get config response status: ${textStatus}`);
  170. },
  171. success(data) {
  172. const { config, interfaceConfig, loggingConfig } = window;
  173. try {
  174. overrideConfigJSON(
  175. config, interfaceConfig, loggingConfig,
  176. data);
  177. complete(true);
  178. } catch (e) {
  179. logger.error('Parse config error: ', e);
  180. complete(false, e);
  181. }
  182. }
  183. }
  184. );
  185. }
  186. /* eslint-disable max-params, no-shadow */
  187. /**
  188. * Overrides JSON properties in {@code config} and
  189. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  190. * Overrides only the whitelisted keys.
  191. *
  192. * @param {Object} config - The config Object in which we'll be overriding
  193. * properties.
  194. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  195. * be overriding properties.
  196. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  197. * overriding properties.
  198. * @param {Object} json - Object containing configuration properties.
  199. * Destination object is selected based on root property name:
  200. * {
  201. * config: {
  202. * // config.js properties here
  203. * },
  204. * interfaceConfig: {
  205. * // interface_config.js properties here
  206. * },
  207. * loggingConfig: {
  208. * // logging_config.js properties here
  209. * }
  210. * }.
  211. * @returns {void}
  212. */
  213. export function overrideConfigJSON(
  214. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  215. json: Object) {
  216. for (const configName of Object.keys(json)) {
  217. let configObj;
  218. if (configName === 'config') {
  219. configObj = config;
  220. } else if (configName === 'interfaceConfig') {
  221. configObj = interfaceConfig;
  222. } else if (configName === 'loggingConfig') {
  223. configObj = loggingConfig;
  224. }
  225. if (configObj) {
  226. const configJSON
  227. = _getWhitelistedJSON(configName, json[configName]);
  228. if (!_.isEmpty(configJSON)) {
  229. logger.info(
  230. `Extending ${configName} with: ${
  231. JSON.stringify(configJSON)}`);
  232. // eslint-disable-next-line arrow-body-style
  233. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  234. // XXX We don't want to merge the arrays, we want to
  235. // overwrite them.
  236. return Array.isArray(oldValue) ? newValue : undefined;
  237. });
  238. }
  239. }
  240. }
  241. }
  242. /* eslint-enable max-params, no-shadow */
  243. /**
  244. * Whitelist only config.js, skips this for others configs
  245. * (interfaceConfig, loggingConfig).
  246. * Only extracts overridden values for keys we allow to be overridden.
  247. *
  248. * @param {string} configName - The config name, one of config,
  249. * interfaceConfig, loggingConfig.
  250. * @param {Object} configJSON - The object with keys and values to override.
  251. * @private
  252. * @returns {Object} - The result object only with the keys
  253. * that are whitelisted.
  254. */
  255. function _getWhitelistedJSON(configName, configJSON) {
  256. if (configName !== 'config') {
  257. return configJSON;
  258. }
  259. return _.pick(configJSON, WHITELISTED_KEYS);
  260. }
  261. /**
  262. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  263. * previously downloaded from a specific {@code baseURL} and stored with
  264. * {@link storeConfig}.
  265. *
  266. * @param {string} baseURL - The base URL from which the config.js was
  267. * previously downloaded and stored with {@code storeConfig}.
  268. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  269. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  270. * otherwise, {@code undefined}.
  271. */
  272. export function restoreConfig(baseURL: string): ?Object {
  273. let storage;
  274. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  275. try {
  276. // XXX Even reading the property localStorage of window may throw an
  277. // error (which is user agent-specific behavior).
  278. storage = window.localStorage;
  279. const config = storage.getItem(key);
  280. if (config) {
  281. return JSON.parse(config) || undefined;
  282. }
  283. } catch (e) {
  284. // Somehow incorrect data ended up in the storage. Clean it up.
  285. storage && storage.removeItem(key);
  286. }
  287. return undefined;
  288. }
  289. /* eslint-disable max-params */
  290. /**
  291. * Inspects the hash part of the location URI and overrides values specified
  292. * there in the corresponding config objects given as the arguments. The syntax
  293. * is: {@code https://server.com/room#config.debug=true
  294. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  295. *
  296. * In the hash part each parameter will be parsed to JSON and then the root
  297. * object will be matched with the corresponding config object given as the
  298. * argument to this function.
  299. *
  300. * @param {Object} config - This is the general config.
  301. * @param {Object} interfaceConfig - This is the interface config.
  302. * @param {Object} loggingConfig - The logging config.
  303. * @param {URI} location - The new location to which the app is navigating to.
  304. * @returns {void}
  305. */
  306. export function setConfigFromURLParams(
  307. config: ?Object,
  308. interfaceConfig: ?Object,
  309. loggingConfig: ?Object,
  310. location: Object) {
  311. const params = parseURLParams(location);
  312. const json = {};
  313. // At this point we have:
  314. // params = {
  315. // "config.disableAudioLevels": false,
  316. // "config.channelLastN": -1,
  317. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  318. // }
  319. // We want to have:
  320. // json = {
  321. // config: {
  322. // "disableAudioLevels": false,
  323. // "channelLastN": -1
  324. // },
  325. // interfaceConfig: {
  326. // "APP_NAME": "Jitsi Meet"
  327. // }
  328. // }
  329. config && (json.config = {});
  330. interfaceConfig && (json.interfaceConfig = {});
  331. loggingConfig && (json.loggingConfig = {});
  332. for (const param of Object.keys(params)) {
  333. let base = json;
  334. const names = param.split('.');
  335. const last = names.pop();
  336. for (const name of names) {
  337. base = base[name] = base[name] || {};
  338. }
  339. base[last] = params[param];
  340. }
  341. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  342. }
  343. /* eslint-enable max-params */