bitrate_allocator.h 6.7 KB

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