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.8KB

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