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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* @flow */
  2. import _ from 'lodash';
  3. import parseURLParams from './parseURLParams';
  4. declare var $: Object;
  5. /**
  6. * The config keys to whitelist, the keys that can be overridden.
  7. * Currently we can only whitelist the first part of the properties, like
  8. * 'p2p.useStunTurn' and 'p2p.enabled' we whitelist all p2p options.
  9. * The whitelist is used only for config.js.
  10. *
  11. * @private
  12. * @type Array
  13. */
  14. const WHITELISTED_KEYS = [
  15. '_peerConnStatusOutOfLastNTimeout',
  16. '_peerConnStatusRtcMuteTimeout',
  17. 'abTesting',
  18. 'alwaysVisibleToolbar',
  19. 'autoEnableDesktopSharing',
  20. 'autoRecord',
  21. 'autoRecordToken',
  22. 'avgRtpStatsN',
  23. 'callStatsConfIDNamespace',
  24. 'callStatsID',
  25. 'callStatsSecret',
  26. 'channelLastN',
  27. 'constraints',
  28. 'debug',
  29. 'debugAudioLevels',
  30. 'defaultLanguage',
  31. 'desktopSharingChromeDisabled',
  32. 'desktopSharingChromeExtId',
  33. 'desktopSharingChromeMinExtVersion',
  34. 'desktopSharingChromeSources',
  35. 'desktopSharingFrameRate',
  36. 'desktopSharingFirefoxDisabled',
  37. 'desktopSharingSources',
  38. 'disable1On1Mode',
  39. 'disableAEC',
  40. 'disableAGC',
  41. 'disableAP',
  42. 'disableAudioLevels',
  43. 'disableDesktopSharing',
  44. 'disableDesktopSharing',
  45. 'disableH264',
  46. 'disableHPF',
  47. 'disableNS',
  48. 'disableRemoteControl',
  49. 'disableRtx',
  50. 'disableSuspendVideo',
  51. 'displayJids',
  52. 'enableDisplayNameInStats',
  53. 'enableLipSync',
  54. 'enableLocalVideoFlip',
  55. 'enableRecording',
  56. 'enableStatsID',
  57. 'enableTalkWhileMuted',
  58. 'enableUserRolesBasedOnToken',
  59. 'etherpad_base',
  60. 'failICE',
  61. 'firefox_fake_device',
  62. 'forceJVB121Ratio',
  63. 'gatherStats',
  64. 'googleApiApplicationClientID',
  65. 'hiddenDomain',
  66. 'hosts',
  67. 'iAmRecorder',
  68. 'iAmSipGateway',
  69. 'iceTransportPolicy',
  70. 'ignoreStartMuted',
  71. 'nick',
  72. 'openBridgeChannel',
  73. 'p2p',
  74. 'preferH264',
  75. 'recordingType',
  76. 'requireDisplayName',
  77. 'resolution',
  78. 'startAudioMuted',
  79. 'startAudioOnly',
  80. 'startBitrate',
  81. 'startScreenSharing',
  82. 'startVideoMuted',
  83. 'startWithAudioMuted',
  84. 'startWithVideoMuted',
  85. 'testing',
  86. 'useIPv6',
  87. 'useNicks',
  88. 'useStunTurn',
  89. 'webrtcIceTcpDisable',
  90. 'webrtcIceUdpDisable'
  91. ];
  92. const logger = require('jitsi-meet-logger').getLogger(__filename);
  93. // XXX The functions getRoomName and parseURLParams are split out of
  94. // functions.js because they are bundled in both app.bundle and
  95. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  96. // want all functions to be bundled in do_external_connect.
  97. export { default as getRoomName } from './getRoomName';
  98. export { parseURLParams };
  99. /**
  100. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  101. * of the room is included in JSON format:
  102. * {
  103. * "rooomName": "someroom12345"
  104. * }.
  105. *
  106. * @param {string} endpoint - The name of HTTP endpoint to which to send
  107. * the HTTP POST request.
  108. * @param {string} roomName - The name of the conference room for which config
  109. * is requested.
  110. * @param {Function} complete - The callback to invoke upon success or failure.
  111. * @returns {void}
  112. */
  113. export function obtainConfig(
  114. endpoint: string,
  115. roomName: string,
  116. complete: Function) {
  117. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  118. $.ajax(
  119. endpoint,
  120. {
  121. contentType: 'application/json',
  122. data: JSON.stringify({ roomName }),
  123. dataType: 'json',
  124. method: 'POST',
  125. error(jqXHR, textStatus, errorThrown) {
  126. logger.error('Get config error: ', jqXHR, errorThrown);
  127. complete(false, `Get config response status: ${textStatus}`);
  128. },
  129. success(data) {
  130. const { config, interfaceConfig, loggingConfig } = window;
  131. try {
  132. overrideConfigJSON(
  133. config, interfaceConfig, loggingConfig,
  134. data);
  135. complete(true);
  136. } catch (e) {
  137. logger.error('Parse config error: ', e);
  138. complete(false, e);
  139. }
  140. }
  141. }
  142. );
  143. }
  144. /* eslint-disable max-params, no-shadow */
  145. /**
  146. * Overrides JSON properties in {@code config} and
  147. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  148. * Overrides only the whitelisted keys.
  149. *
  150. * @param {Object} config - The config Object in which we'll be overriding
  151. * properties.
  152. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  153. * be overriding properties.
  154. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  155. * overriding properties.
  156. * @param {Object} json - Object containing configuration properties.
  157. * Destination object is selected based on root property name:
  158. * {
  159. * config: {
  160. * // config.js properties here
  161. * },
  162. * interfaceConfig: {
  163. * // interface_config.js properties here
  164. * },
  165. * loggingConfig: {
  166. * // logging_config.js properties here
  167. * }
  168. * }.
  169. * @returns {void}
  170. */
  171. export function overrideConfigJSON(
  172. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  173. json: Object) {
  174. for (const configName of Object.keys(json)) {
  175. let configObj;
  176. if (configName === 'config') {
  177. configObj = config;
  178. } else if (configName === 'interfaceConfig') {
  179. configObj = interfaceConfig;
  180. } else if (configName === 'loggingConfig') {
  181. configObj = loggingConfig;
  182. }
  183. if (configObj) {
  184. const configJSON
  185. = _getWhitelistedJSON(configName, json[configName]);
  186. if (!_.isEmpty(configJSON)) {
  187. logger.info(
  188. `Extending ${configName} with: ${
  189. JSON.stringify(configJSON)}`);
  190. // eslint-disable-next-line arrow-body-style
  191. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  192. // XXX We don't want to merge the arrays, we want to
  193. // overwrite them.
  194. return Array.isArray(oldValue) ? newValue : undefined;
  195. });
  196. }
  197. }
  198. }
  199. }
  200. /* eslint-enable max-params, no-shadow */
  201. /**
  202. * Whitelist only config.js, skips this for others configs
  203. * (interfaceConfig, loggingConfig).
  204. * Only extracts overridden values for keys we allow to be overridden.
  205. *
  206. * @param {string} configName - The config name, one of config,
  207. * interfaceConfig, loggingConfig.
  208. * @param {Object} configJSON - The object with keys and values to override.
  209. * @private
  210. * @returns {Object} - The result object only with the keys
  211. * that are whitelisted.
  212. */
  213. function _getWhitelistedJSON(configName, configJSON) {
  214. if (configName !== 'config') {
  215. return configJSON;
  216. }
  217. return _.pick(configJSON, WHITELISTED_KEYS);
  218. }
  219. /* eslint-disable max-params */
  220. /**
  221. * Inspects the hash part of the location URI and overrides values specified
  222. * there in the corresponding config objects given as the arguments. The syntax
  223. * is: {@code https://server.com/room#config.debug=true
  224. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  225. *
  226. * In the hash part each parameter will be parsed to JSON and then the root
  227. * object will be matched with the corresponding config object given as the
  228. * argument to this function.
  229. *
  230. * @param {Object} config - This is the general config.
  231. * @param {Object} interfaceConfig - This is the interface config.
  232. * @param {Object} loggingConfig - The logging config.
  233. * @param {URI} location - The new location to which the app is navigating to.
  234. * @returns {void}
  235. */
  236. export function setConfigFromURLParams(
  237. config: ?Object,
  238. interfaceConfig: ?Object,
  239. loggingConfig: ?Object,
  240. location: Object) {
  241. const params = parseURLParams(location);
  242. const json = {};
  243. // At this point we have:
  244. // params = {
  245. // "config.disableAudioLevels": false,
  246. // "config.channelLastN": -1,
  247. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  248. // }
  249. // We want to have:
  250. // json = {
  251. // config: {
  252. // "disableAudioLevels": false,
  253. // "channelLastN": -1
  254. // },
  255. // interfaceConfig: {
  256. // "APP_NAME": "Jitsi Meet"
  257. // }
  258. // }
  259. config && (json.config = {});
  260. interfaceConfig && (json.interfaceConfig = {});
  261. loggingConfig && (json.loggingConfig = {});
  262. for (const param of Object.keys(params)) {
  263. let base = json;
  264. const names = param.split('.');
  265. const last = names.pop();
  266. for (const name of names) {
  267. base = base[name] = base[name] || {};
  268. }
  269. base[last] = params[param];
  270. }
  271. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  272. }
  273. /* eslint-enable max-params */