OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 * | 9 * |
10 */ | 10 */ |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 // Allow packets to be transmitted in up to 2 times max video bitrate if the | 22 // Allow packets to be transmitted in up to 2 times max video bitrate if the |
23 // bandwidth estimate allows it. | 23 // bandwidth estimate allows it. |
24 const int kTransmissionMaxBitrateMultiplier = 2; | 24 const int kTransmissionMaxBitrateMultiplier = 2; |
25 const int kDefaultBitrateBps = 300000; | 25 const int kDefaultBitrateBps = 300000; |
26 | 26 |
27 BitrateAllocator::BitrateAllocator() | 27 BitrateAllocator::BitrateAllocator() |
28 : bitrate_observer_configs_(), | 28 : bitrate_observer_configs_(), |
29 enforce_min_bitrate_(true), | 29 enforce_min_bitrate_(true), |
30 last_bitrate_bps_(kDefaultBitrateBps), | 30 last_bitrate_bps_(kDefaultBitrateBps), |
| 31 last_non_zero_bitrate_bps_(kDefaultBitrateBps), |
31 last_fraction_loss_(0), | 32 last_fraction_loss_(0), |
32 last_rtt_(0) {} | 33 last_rtt_(0) {} |
33 | 34 |
34 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate, | 35 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate, |
35 uint8_t fraction_loss, | 36 uint8_t fraction_loss, |
36 int64_t rtt) { | 37 int64_t rtt) { |
37 rtc::CritScope lock(&crit_sect_); | 38 rtc::CritScope lock(&crit_sect_); |
38 last_bitrate_bps_ = bitrate; | 39 last_bitrate_bps_ = bitrate; |
| 40 last_non_zero_bitrate_bps_ = |
| 41 bitrate > 0 ? bitrate : last_non_zero_bitrate_bps_; |
39 last_fraction_loss_ = fraction_loss; | 42 last_fraction_loss_ = fraction_loss; |
40 last_rtt_ = rtt; | 43 last_rtt_ = rtt; |
41 | 44 |
42 uint32_t allocated_bitrate_bps = 0; | 45 uint32_t allocated_bitrate_bps = 0; |
43 ObserverAllocation allocation = AllocateBitrates(); | 46 ObserverAllocation allocation = AllocateBitrates(bitrate); |
44 for (const auto& kv : allocation) { | 47 for (const auto& kv : allocation) { |
45 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); | 48 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); |
46 allocated_bitrate_bps += kv.second; | 49 allocated_bitrate_bps += kv.second; |
47 } | 50 } |
48 return allocated_bitrate_bps; | 51 return allocated_bitrate_bps; |
49 } | 52 } |
50 | 53 |
51 int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, | 54 int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, |
52 uint32_t min_bitrate_bps, | 55 uint32_t min_bitrate_bps, |
53 uint32_t max_bitrate_bps, | 56 uint32_t max_bitrate_bps, |
(...skipping 12 matching lines...) Expand all Loading... |
66 if (it != bitrate_observer_configs_.end()) { | 69 if (it != bitrate_observer_configs_.end()) { |
67 // Update current configuration. | 70 // Update current configuration. |
68 it->min_bitrate_bps = min_bitrate_bps; | 71 it->min_bitrate_bps = min_bitrate_bps; |
69 it->max_bitrate_bps = max_bitrate_bps; | 72 it->max_bitrate_bps = max_bitrate_bps; |
70 } else { | 73 } else { |
71 // Add new settings. | 74 // Add new settings. |
72 bitrate_observer_configs_.push_back(ObserverConfig( | 75 bitrate_observer_configs_.push_back(ObserverConfig( |
73 observer, min_bitrate_bps, max_bitrate_bps, enforce_min_bitrate)); | 76 observer, min_bitrate_bps, max_bitrate_bps, enforce_min_bitrate)); |
74 } | 77 } |
75 | 78 |
76 ObserverAllocation allocation = AllocateBitrates(); | |
77 int new_observer_bitrate_bps = 0; | 79 int new_observer_bitrate_bps = 0; |
78 for (auto& kv : allocation) { | 80 if (last_bitrate_bps_ > 0) { // We have a bitrate to allocate. |
79 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); | 81 ObserverAllocation allocation = AllocateBitrates(last_bitrate_bps_); |
80 if (kv.first == observer) | 82 for (auto& kv : allocation) { |
81 new_observer_bitrate_bps = kv.second; | 83 // Update all observers with the new allocation. |
| 84 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); |
| 85 if (kv.first == observer) |
| 86 new_observer_bitrate_bps = kv.second; |
| 87 } |
| 88 } else { |
| 89 // Currently, an encoder is not allowed to produce frames. |
| 90 // But we still have to return the initial config bitrate + let the |
| 91 // observer know that it can not produce frames. |
| 92 ObserverAllocation allocation = |
| 93 AllocateBitrates(last_non_zero_bitrate_bps_); |
| 94 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_); |
| 95 new_observer_bitrate_bps = allocation[observer]; |
82 } | 96 } |
83 return new_observer_bitrate_bps; | 97 return new_observer_bitrate_bps; |
84 } | 98 } |
85 | 99 |
86 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { | 100 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { |
87 rtc::CritScope lock(&crit_sect_); | 101 rtc::CritScope lock(&crit_sect_); |
88 auto it = FindObserverConfig(observer); | 102 auto it = FindObserverConfig(observer); |
89 if (it != bitrate_observer_configs_.end()) { | 103 if (it != bitrate_observer_configs_.end()) { |
90 bitrate_observer_configs_.erase(it); | 104 bitrate_observer_configs_.erase(it); |
91 } | 105 } |
92 } | 106 } |
93 | 107 |
94 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) { | 108 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) { |
95 enforce_min_bitrate_ = enforce_min_bitrate; | 109 enforce_min_bitrate_ = enforce_min_bitrate; |
96 } | 110 } |
97 | 111 |
98 BitrateAllocator::ObserverConfigList::iterator | 112 BitrateAllocator::ObserverConfigList::iterator |
99 BitrateAllocator::FindObserverConfig( | 113 BitrateAllocator::FindObserverConfig( |
100 const BitrateAllocatorObserver* observer) { | 114 const BitrateAllocatorObserver* observer) { |
101 for (auto it = bitrate_observer_configs_.begin(); | 115 for (auto it = bitrate_observer_configs_.begin(); |
102 it != bitrate_observer_configs_.end(); ++it) { | 116 it != bitrate_observer_configs_.end(); ++it) { |
103 if (it->observer == observer) | 117 if (it->observer == observer) |
104 return it; | 118 return it; |
105 } | 119 } |
106 return bitrate_observer_configs_.end(); | 120 return bitrate_observer_configs_.end(); |
107 } | 121 } |
108 | 122 |
109 BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates() { | 123 BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates( |
| 124 uint32_t bitrate) { |
110 if (bitrate_observer_configs_.empty()) | 125 if (bitrate_observer_configs_.empty()) |
111 return ObserverAllocation(); | 126 return ObserverAllocation(); |
112 | 127 |
113 if (last_bitrate_bps_ == 0) | 128 if (bitrate == 0) |
114 return ZeroRateAllocation(); | 129 return ZeroRateAllocation(); |
115 | 130 |
116 uint32_t sum_min_bitrates = 0; | 131 uint32_t sum_min_bitrates = 0; |
117 for (const auto& observer_config : bitrate_observer_configs_) | 132 for (const auto& observer_config : bitrate_observer_configs_) |
118 sum_min_bitrates += observer_config.min_bitrate_bps; | 133 sum_min_bitrates += observer_config.min_bitrate_bps; |
119 if (last_bitrate_bps_ <= sum_min_bitrates) | 134 if (bitrate <= sum_min_bitrates) |
120 return LowRateAllocation(last_bitrate_bps_); | 135 return LowRateAllocation(bitrate); |
121 | 136 |
122 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates); | 137 return NormalRateAllocation(bitrate, sum_min_bitrates); |
123 } | 138 } |
124 | 139 |
125 BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation( | 140 BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation( |
126 uint32_t bitrate, | 141 uint32_t bitrate, |
127 uint32_t sum_min_bitrates) { | 142 uint32_t sum_min_bitrates) { |
128 uint32_t num_remaining_observers = | 143 uint32_t num_remaining_observers = |
129 static_cast<uint32_t>(bitrate_observer_configs_.size()); | 144 static_cast<uint32_t>(bitrate_observer_configs_.size()); |
130 RTC_DCHECK_GT(num_remaining_observers, 0u); | 145 RTC_DCHECK_GT(num_remaining_observers, 0u); |
131 | 146 |
132 uint32_t bitrate_per_observer = | 147 uint32_t bitrate_per_observer = |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 for (const auto& observer_config : bitrate_observer_configs_) { | 198 for (const auto& observer_config : bitrate_observer_configs_) { |
184 uint32_t allocated_bitrate = | 199 uint32_t allocated_bitrate = |
185 std::min(remainder, observer_config.min_bitrate_bps); | 200 std::min(remainder, observer_config.min_bitrate_bps); |
186 allocation[observer_config.observer] = allocated_bitrate; | 201 allocation[observer_config.observer] = allocated_bitrate; |
187 remainder -= allocated_bitrate; | 202 remainder -= allocated_bitrate; |
188 } | 203 } |
189 } | 204 } |
190 return allocation; | 205 return allocation; |
191 } | 206 } |
192 } // namespace webrtc | 207 } // namespace webrtc |
OLD | NEW |