bitrate_allocator.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef CALL_BITRATE_ALLOCATOR_H_
  11. #define CALL_BITRATE_ALLOCATOR_H_
  12. #include <stdint.h>
  13. #include <map>
  14. #include <memory>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. #include "api/call/bitrate_allocation.h"
  19. #include "api/transport/network_types.h"
  20. #include "rtc_base/synchronization/sequence_checker.h"
  21. namespace webrtc {
  22. class Clock;
  23. // Used by all send streams with adaptive bitrate, to get the currently
  24. // allocated bitrate for the send stream. The current network properties are
  25. // given at the same time, to let the send stream decide about possible loss
  26. // protection.
  27. class BitrateAllocatorObserver {
  28. public:
  29. // Returns the amount of protection used by the BitrateAllocatorObserver
  30. // implementation, as bitrate in bps.
  31. virtual uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) = 0;
  32. protected:
  33. virtual ~BitrateAllocatorObserver() {}
  34. };
  35. // Struct describing parameters for how a media stream should get bitrate
  36. // allocated to it.
  37. struct MediaStreamAllocationConfig {
  38. // Minimum bitrate supported by track. 0 equals no min bitrate.
  39. uint32_t min_bitrate_bps;
  40. // Maximum bitrate supported by track. 0 equals no max bitrate.
  41. uint32_t max_bitrate_bps;
  42. uint32_t pad_up_bitrate_bps;
  43. int64_t priority_bitrate_bps;
  44. // True means track may not be paused by allocating 0 bitrate will allocate at
  45. // least |min_bitrate_bps| for this observer, even if the BWE is too low,
  46. // false will allocate 0 to the observer if BWE doesn't allow
  47. // |min_bitrate_bps|.
  48. bool enforce_min_bitrate;
  49. // The amount of bitrate allocated to this observer relative to all other
  50. // observers. If an observer has twice the bitrate_priority of other
  51. // observers, it should be allocated twice the bitrate above its min.
  52. double bitrate_priority;
  53. };
  54. // Interface used for mocking
  55. class BitrateAllocatorInterface {
  56. public:
  57. virtual void AddObserver(BitrateAllocatorObserver* observer,
  58. MediaStreamAllocationConfig config) = 0;
  59. virtual void RemoveObserver(BitrateAllocatorObserver* observer) = 0;
  60. virtual int GetStartBitrate(BitrateAllocatorObserver* observer) const = 0;
  61. protected:
  62. virtual ~BitrateAllocatorInterface() = default;
  63. };
  64. namespace bitrate_allocator_impl {
  65. struct AllocatableTrack {
  66. AllocatableTrack(BitrateAllocatorObserver* observer,
  67. MediaStreamAllocationConfig allocation_config)
  68. : observer(observer),
  69. config(allocation_config),
  70. allocated_bitrate_bps(-1),
  71. media_ratio(1.0) {}
  72. BitrateAllocatorObserver* observer;
  73. MediaStreamAllocationConfig config;
  74. int64_t allocated_bitrate_bps;
  75. double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
  76. uint32_t LastAllocatedBitrate() const;
  77. // The minimum bitrate required by this observer, including
  78. // enable-hysteresis if the observer is in a paused state.
  79. uint32_t MinBitrateWithHysteresis() const;
  80. };
  81. } // namespace bitrate_allocator_impl
  82. // Usage: this class will register multiple RtcpBitrateObserver's one at each
  83. // RTCP module. It will aggregate the results and run one bandwidth estimation
  84. // and push the result to the encoders via BitrateAllocatorObserver(s).
  85. class BitrateAllocator : public BitrateAllocatorInterface {
  86. public:
  87. // Used to get notified when send stream limits such as the minimum send
  88. // bitrate and max padding bitrate is changed.
  89. class LimitObserver {
  90. public:
  91. virtual void OnAllocationLimitsChanged(BitrateAllocationLimits limits) = 0;
  92. protected:
  93. virtual ~LimitObserver() = default;
  94. };
  95. explicit BitrateAllocator(LimitObserver* limit_observer);
  96. ~BitrateAllocator() override;
  97. void UpdateStartRate(uint32_t start_rate_bps);
  98. // Allocate target_bitrate across the registered BitrateAllocatorObservers.
  99. void OnNetworkEstimateChanged(TargetTransferRate msg);
  100. // Set the configuration used by the bandwidth management.
  101. // |observer| updates bitrates if already in use.
  102. // |config| is the configuration to use for allocation.
  103. // Note that |observer|->OnBitrateUpdated() will be called
  104. // within the scope of this method with the current rtt, fraction_loss and
  105. // available bitrate and that the bitrate in OnBitrateUpdated will be zero if
  106. // the |observer| is currently not allowed to send data.
  107. void AddObserver(BitrateAllocatorObserver* observer,
  108. MediaStreamAllocationConfig config) override;
  109. // Removes a previously added observer, but will not trigger a new bitrate
  110. // allocation.
  111. void RemoveObserver(BitrateAllocatorObserver* observer) override;
  112. // Returns initial bitrate allocated for |observer|. If |observer| is not in
  113. // the list of added observers, a best guess is returned.
  114. int GetStartBitrate(BitrateAllocatorObserver* observer) const override;
  115. private:
  116. using AllocatableTrack = bitrate_allocator_impl::AllocatableTrack;
  117. // Calculates the minimum requested send bitrate and max padding bitrate and
  118. // calls LimitObserver::OnAllocationLimitsChanged.
  119. void UpdateAllocationLimits() RTC_RUN_ON(&sequenced_checker_);
  120. // Allow packets to be transmitted in up to 2 times max video bitrate if the
  121. // bandwidth estimate allows it.
  122. // TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in
  123. // video send stream.
  124. static uint8_t GetTransmissionMaxBitrateMultiplier();
  125. SequenceChecker sequenced_checker_;
  126. LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
  127. // Stored in a list to keep track of the insertion order.
  128. std::vector<AllocatableTrack> allocatable_tracks_
  129. RTC_GUARDED_BY(&sequenced_checker_);
  130. uint32_t last_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
  131. uint32_t last_stable_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
  132. uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
  133. uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
  134. int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
  135. int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
  136. // Number of mute events based on too low BWE, not network up/down.
  137. int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
  138. int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
  139. BitrateAllocationLimits current_limits_ RTC_GUARDED_BY(&sequenced_checker_);
  140. };
  141. } // namespace webrtc
  142. #endif // CALL_BITRATE_ALLOCATOR_H_