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.

BandwidthLimiter.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import transform from 'sdp-transform';
  4. const logger = getLogger(__filename);
  5. /**
  6. * This will save a bandwidth limit (per mline) that should be used
  7. * and then, when given an SDP, will enforce that limit.
  8. * Note that this will affect *outgoing* bandwidth usage, but the SDP
  9. * it must modify to implement that is the *remote* description
  10. */
  11. export default class BandwidthLimiter {
  12. /**
  13. * Create a new BandwidthLimiter
  14. */
  15. constructor() {
  16. /**
  17. * @type {Map<String, Number>}
  18. * Map of mline media type to a bandwidth limit (in kbps).
  19. * Any mlines present in this map will have the associated
  20. * bandwidth limit (or 'null' for no limit) enforced, meaning
  21. * that it will potentially overwrite a limit already set in
  22. * the given sdp. However, if an mline is NOT present in
  23. * this map, any limit in the given sdp will not be touched.
  24. */
  25. this._bandwidthLimits = new Map();
  26. }
  27. /**
  28. * Set a bandwidth limit for given mline. If limitKbps is null,
  29. * the limit will be removed
  30. * @param {String} mediaType the mline media type to set the
  31. * bandwidth limit on
  32. * @param {Number} limitKbps the bandwidth limit, in kbps
  33. */
  34. setBandwidthLimit(mediaType, limitKbps) {
  35. this._bandwidthLimits.set(mediaType, limitKbps);
  36. }
  37. /**
  38. * Get the current bandwidth limit
  39. * @param {String} mediaType the mline media type for which to get the
  40. * bandwidth limit
  41. * @return {Number} the bandwidth limit in kbps if it exists, undefined
  42. * otherwise
  43. */
  44. getBandwidthLimit(mediaType) {
  45. return this._bandwidthLimits.get(mediaType);
  46. }
  47. /**
  48. * Enforce any configured bandwidth limits (or lack thereof) in the given
  49. * sdp
  50. * @param {String} sdp the session description
  51. * @returns {String} a potentially modified session description
  52. * with any configured bandwidth limits set
  53. */
  54. enforceBandwithLimit(sdp) {
  55. logger.debug('Enforcing any configured bandwidth limits');
  56. const desc = transform.parse(sdp);
  57. desc.media.forEach(mLine => {
  58. const limitKbps = this._bandwidthLimits.get(mLine.type);
  59. if (typeof limitKbps !== 'undefined') {
  60. if (limitKbps === null) {
  61. logger.debug(
  62. `Removing bandwidth limit for mline ${mLine.type}`);
  63. delete mLine.bandwidth;
  64. } else {
  65. logger.debug(`Enforcing limit ${limitKbps}kbps`
  66. + ` for mline ${mLine.type}`);
  67. mLine.bandwidth = [
  68. {
  69. type: 'AS',
  70. limit: limitKbps
  71. }
  72. ];
  73. }
  74. }
  75. });
  76. return transform.write(desc);
  77. }
  78. }