Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BandwidthLimiter.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * Enforce any configured bandwidth limits (or lack thereof) in the given
  39. * sdp
  40. * @param {String} sdp the session description
  41. * @returns {String} a potentially modified session description
  42. * with any configured bandwidth limits set
  43. */
  44. enforceBandwithLimit(sdp) {
  45. logger.debug('Enforcing any configured bandwidth limits');
  46. const desc = transform.parse(sdp);
  47. desc.media.forEach(mLine => {
  48. const limitKbps = this._bandwidthLimits.get(mLine.type);
  49. if (typeof limitKbps !== 'undefined') {
  50. if (limitKbps === null) {
  51. logger.debug(
  52. `Removing bandwidth limit for mline ${mLine.type}`);
  53. delete mLine.bandwidth;
  54. } else {
  55. logger.debug(`Enforcing limit ${limitKbps}kbps`
  56. + ` for mline ${mLine.type}`);
  57. mLine.bandwidth = [
  58. {
  59. type: 'AS',
  60. limit: limitKbps
  61. }
  62. ];
  63. }
  64. }
  65. });
  66. return transform.write(desc);
  67. }
  68. }