| 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 // Require a bitrate increase of max(10%, 20kbps) to resume paused streams. | 27 // Require a bitrate increase of max(10%, 20kbps) to resume paused streams. |
| 28 const double kToggleFactor = 0.1; | 28 const double kToggleFactor = 0.1; |
| 29 const uint32_t kMinToggleBitrateBps = 20000; | 29 const uint32_t kMinToggleBitrateBps = 20000; |
| 30 | 30 |
| 31 BitrateAllocator::BitrateAllocator() | 31 BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer) |
| 32 : bitrate_observer_configs_(), | 32 : limit_observer_(limit_observer), |
| 33 bitrate_observer_configs_(), |
| 33 last_bitrate_bps_(kDefaultBitrateBps), | 34 last_bitrate_bps_(kDefaultBitrateBps), |
| 34 last_non_zero_bitrate_bps_(kDefaultBitrateBps), | 35 last_non_zero_bitrate_bps_(kDefaultBitrateBps), |
| 35 last_fraction_loss_(0), | 36 last_fraction_loss_(0), |
| 36 last_rtt_(0) {} | 37 last_rtt_(0) {} |
| 37 | 38 |
| 38 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, | 39 void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, |
| 39 uint8_t fraction_loss, | 40 uint8_t fraction_loss, |
| 40 int64_t rtt) { | 41 int64_t rtt) { |
| 41 rtc::CritScope lock(&crit_sect_); | 42 rtc::CritScope lock(&crit_sect_); |
| 42 last_bitrate_bps_ = target_bitrate_bps; | 43 last_bitrate_bps_ = target_bitrate_bps; |
| 43 last_non_zero_bitrate_bps_ = | 44 last_non_zero_bitrate_bps_ = |
| 44 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; | 45 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; |
| 45 last_fraction_loss_ = fraction_loss; | 46 last_fraction_loss_ = fraction_loss; |
| 46 last_rtt_ = rtt; | 47 last_rtt_ = rtt; |
| 47 | 48 |
| 48 uint32_t allocated_bitrate_bps = 0; | |
| 49 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps); | 49 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps); |
| 50 for (const auto& kv : allocation) { | 50 for (const auto& kv : allocation) { |
| 51 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); | 51 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); |
| 52 allocated_bitrate_bps += kv.second; | |
| 53 } | 52 } |
| 54 last_allocation_ = allocation; | 53 last_allocation_ = allocation; |
| 55 return allocated_bitrate_bps; | |
| 56 } | 54 } |
| 57 | 55 |
| 58 int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, | 56 int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, |
| 59 uint32_t min_bitrate_bps, | 57 uint32_t min_bitrate_bps, |
| 60 uint32_t max_bitrate_bps, | 58 uint32_t max_bitrate_bps, |
| 59 uint32_t pad_up_bitrate_bps, |
| 61 bool enforce_min_bitrate) { | 60 bool enforce_min_bitrate) { |
| 62 rtc::CritScope lock(&crit_sect_); | 61 rtc::CritScope lock(&crit_sect_); |
| 63 auto it = FindObserverConfig(observer); | 62 auto it = FindObserverConfig(observer); |
| 64 | 63 |
| 65 // Update settings if the observer already exists, create a new one otherwise. | 64 // Update settings if the observer already exists, create a new one otherwise. |
| 66 if (it != bitrate_observer_configs_.end()) { | 65 if (it != bitrate_observer_configs_.end()) { |
| 67 it->min_bitrate_bps = min_bitrate_bps; | 66 it->min_bitrate_bps = min_bitrate_bps; |
| 68 it->max_bitrate_bps = max_bitrate_bps; | 67 it->max_bitrate_bps = max_bitrate_bps; |
| 68 it->pad_up_bitrate_bps = pad_up_bitrate_bps; |
| 69 it->enforce_min_bitrate = enforce_min_bitrate; | 69 it->enforce_min_bitrate = enforce_min_bitrate; |
| 70 } else { | 70 } else { |
| 71 bitrate_observer_configs_.push_back(ObserverConfig( | 71 bitrate_observer_configs_.push_back( |
| 72 observer, min_bitrate_bps, max_bitrate_bps, enforce_min_bitrate)); | 72 ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps, |
| 73 pad_up_bitrate_bps, enforce_min_bitrate)); |
| 73 } | 74 } |
| 74 | 75 |
| 75 ObserverAllocation allocation; | 76 ObserverAllocation allocation; |
| 76 if (last_bitrate_bps_ > 0) { | 77 if (last_bitrate_bps_ > 0) { |
| 77 // Calculate a new allocation and update all observers. | 78 // Calculate a new allocation and update all observers. |
| 78 allocation = AllocateBitrates(last_bitrate_bps_); | 79 allocation = AllocateBitrates(last_bitrate_bps_); |
| 79 for (const auto& kv : allocation) | 80 for (const auto& kv : allocation) |
| 80 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); | 81 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); |
| 81 } else { | 82 } else { |
| 82 // Currently, an encoder is not allowed to produce frames. | 83 // Currently, an encoder is not allowed to produce frames. |
| 83 // But we still have to return the initial config bitrate + let the | 84 // But we still have to return the initial config bitrate + let the |
| 84 // observer know that it can not produce frames. | 85 // observer know that it can not produce frames. |
| 85 allocation = AllocateBitrates(last_non_zero_bitrate_bps_); | 86 allocation = AllocateBitrates(last_non_zero_bitrate_bps_); |
| 86 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_); | 87 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_); |
| 87 } | 88 } |
| 89 UpdateAllocationLimits(); |
| 90 |
| 88 last_allocation_ = allocation; | 91 last_allocation_ = allocation; |
| 89 return allocation[observer]; | 92 return allocation[observer]; |
| 90 } | 93 } |
| 91 | 94 |
| 95 void BitrateAllocator::UpdateAllocationLimits() { |
| 96 uint32_t total_requested_padding_bitrate = 0; |
| 97 uint32_t total_requested_min_bitrate = 0; |
| 98 |
| 99 { |
| 100 rtc::CritScope lock(&crit_sect_); |
| 101 for (const auto& config : bitrate_observer_configs_) { |
| 102 if (config.enforce_min_bitrate) { |
| 103 total_requested_min_bitrate += config.min_bitrate_bps; |
| 104 } |
| 105 total_requested_padding_bitrate += config.pad_up_bitrate_bps; |
| 106 } |
| 107 } |
| 108 |
| 109 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate, |
| 110 total_requested_padding_bitrate); |
| 111 } |
| 112 |
| 92 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { | 113 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { |
| 93 rtc::CritScope lock(&crit_sect_); | 114 { |
| 94 auto it = FindObserverConfig(observer); | 115 rtc::CritScope lock(&crit_sect_); |
| 95 if (it != bitrate_observer_configs_.end()) { | 116 auto it = FindObserverConfig(observer); |
| 96 bitrate_observer_configs_.erase(it); | 117 if (it != bitrate_observer_configs_.end()) { |
| 118 bitrate_observer_configs_.erase(it); |
| 119 } |
| 97 } | 120 } |
| 121 UpdateAllocationLimits(); |
| 98 } | 122 } |
| 99 | 123 |
| 100 BitrateAllocator::ObserverConfigList::iterator | 124 BitrateAllocator::ObserverConfigList::iterator |
| 101 BitrateAllocator::FindObserverConfig( | 125 BitrateAllocator::FindObserverConfig( |
| 102 const BitrateAllocatorObserver* observer) { | 126 const BitrateAllocatorObserver* observer) { |
| 103 for (auto it = bitrate_observer_configs_.begin(); | 127 for (auto it = bitrate_observer_configs_.begin(); |
| 104 it != bitrate_observer_configs_.end(); ++it) { | 128 it != bitrate_observer_configs_.end(); ++it) { |
| 105 if (it->observer == observer) | 129 if (it->observer == observer) |
| 106 return it; | 130 return it; |
| 107 } | 131 } |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 uint32_t extra_bitrate_per_observer = (bitrate - sum_min_bitrates) / | 314 uint32_t extra_bitrate_per_observer = (bitrate - sum_min_bitrates) / |
| 291 static_cast<uint32_t>(bitrate_observer_configs_.size()); | 315 static_cast<uint32_t>(bitrate_observer_configs_.size()); |
| 292 for (const auto& observer_config : bitrate_observer_configs_) { | 316 for (const auto& observer_config : bitrate_observer_configs_) { |
| 293 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer < | 317 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer < |
| 294 MinBitrateWithHysteresis(observer_config)) | 318 MinBitrateWithHysteresis(observer_config)) |
| 295 return false; | 319 return false; |
| 296 } | 320 } |
| 297 return true; | 321 return true; |
| 298 } | 322 } |
| 299 } // namespace webrtc | 323 } // namespace webrtc |
| OLD | NEW |