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 16 matching lines...) Expand all Loading... | |
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(LimitObserver* limit_observer) | 31 BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer) |
32 : limit_observer_(limit_observer), | 32 : limit_observer_(limit_observer), |
33 bitrate_observer_configs_(), | 33 bitrate_observer_configs_(), |
34 last_bitrate_bps_(kDefaultBitrateBps), | 34 last_bitrate_bps_(kDefaultBitrateBps), |
35 last_non_zero_bitrate_bps_(kDefaultBitrateBps), | 35 last_non_zero_bitrate_bps_(kDefaultBitrateBps), |
36 last_fraction_loss_(0), | 36 last_fraction_loss_(0), |
37 last_rtt_(0) {} | 37 last_rtt_(0) { |
38 thread_checker_.DetachFromThread(); | |
39 } | |
38 | 40 |
39 void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, | 41 void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, |
40 uint8_t fraction_loss, | 42 uint8_t fraction_loss, |
41 int64_t rtt) { | 43 int64_t rtt) { |
42 rtc::CritScope lock(&crit_sect_); | 44 RTC_DCHECK_RUN_ON(&thread_checker_); |
43 last_bitrate_bps_ = target_bitrate_bps; | 45 last_bitrate_bps_ = target_bitrate_bps; |
44 last_non_zero_bitrate_bps_ = | 46 last_non_zero_bitrate_bps_ = |
45 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; | 47 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; |
46 last_fraction_loss_ = fraction_loss; | 48 last_fraction_loss_ = fraction_loss; |
47 last_rtt_ = rtt; | 49 last_rtt_ = rtt; |
48 | 50 |
49 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps); | 51 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps); |
50 for (const auto& kv : allocation) { | 52 for (const auto& kv : allocation) { |
51 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); | 53 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); |
52 } | 54 } |
53 last_allocation_ = allocation; | 55 last_allocation_ = allocation; |
54 } | 56 } |
55 | 57 |
56 void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, | 58 void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, |
57 uint32_t min_bitrate_bps, | 59 uint32_t min_bitrate_bps, |
58 uint32_t max_bitrate_bps, | 60 uint32_t max_bitrate_bps, |
59 uint32_t pad_up_bitrate_bps, | 61 uint32_t pad_up_bitrate_bps, |
60 bool enforce_min_bitrate) { | 62 bool enforce_min_bitrate) { |
61 rtc::CritScope lock(&crit_sect_); | 63 RTC_DCHECK_RUN_ON(&thread_checker_); |
62 auto it = FindObserverConfig(observer); | 64 auto it = FindObserverConfig(observer); |
63 | 65 |
64 // Update settings if the observer already exists, create a new one otherwise. | 66 // Update settings if the observer already exists, create a new one otherwise. |
65 if (it != bitrate_observer_configs_.end()) { | 67 if (it != bitrate_observer_configs_.end()) { |
66 it->min_bitrate_bps = min_bitrate_bps; | 68 it->min_bitrate_bps = min_bitrate_bps; |
67 it->max_bitrate_bps = max_bitrate_bps; | 69 it->max_bitrate_bps = max_bitrate_bps; |
68 it->pad_up_bitrate_bps = pad_up_bitrate_bps; | 70 it->pad_up_bitrate_bps = pad_up_bitrate_bps; |
69 it->enforce_min_bitrate = enforce_min_bitrate; | 71 it->enforce_min_bitrate = enforce_min_bitrate; |
70 } else { | 72 } else { |
71 bitrate_observer_configs_.push_back( | 73 bitrate_observer_configs_.push_back( |
(...skipping 13 matching lines...) Expand all Loading... | |
85 // observer know that it can not produce frames. | 87 // observer know that it can not produce frames. |
86 allocation = AllocateBitrates(last_non_zero_bitrate_bps_); | 88 allocation = AllocateBitrates(last_non_zero_bitrate_bps_); |
87 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_); | 89 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_); |
88 } | 90 } |
89 UpdateAllocationLimits(); | 91 UpdateAllocationLimits(); |
90 | 92 |
91 last_allocation_ = allocation; | 93 last_allocation_ = allocation; |
92 } | 94 } |
93 | 95 |
94 void BitrateAllocator::UpdateAllocationLimits() { | 96 void BitrateAllocator::UpdateAllocationLimits() { |
97 RTC_DCHECK_RUN_ON(&thread_checker_); | |
95 uint32_t total_requested_padding_bitrate = 0; | 98 uint32_t total_requested_padding_bitrate = 0; |
96 uint32_t total_requested_min_bitrate = 0; | 99 uint32_t total_requested_min_bitrate = 0; |
97 | 100 |
98 { | |
99 rtc::CritScope lock(&crit_sect_); | |
100 for (const auto& config : bitrate_observer_configs_) { | 101 for (const auto& config : bitrate_observer_configs_) { |
tommi
2016/06/29 14:49:41
run git cl format
| |
101 if (config.enforce_min_bitrate) { | 102 if (config.enforce_min_bitrate) { |
102 total_requested_min_bitrate += config.min_bitrate_bps; | 103 total_requested_min_bitrate += config.min_bitrate_bps; |
103 } | 104 } |
104 total_requested_padding_bitrate += config.pad_up_bitrate_bps; | 105 total_requested_padding_bitrate += config.pad_up_bitrate_bps; |
105 } | 106 } |
106 } | |
107 | 107 |
108 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate, | 108 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate, |
109 total_requested_padding_bitrate); | 109 total_requested_padding_bitrate); |
110 } | 110 } |
111 | 111 |
112 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { | 112 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { |
113 { | 113 RTC_DCHECK_RUN_ON(&thread_checker_); |
114 rtc::CritScope lock(&crit_sect_); | 114 auto it = FindObserverConfig(observer); |
115 auto it = FindObserverConfig(observer); | 115 if (it != bitrate_observer_configs_.end()) { |
116 if (it != bitrate_observer_configs_.end()) { | 116 bitrate_observer_configs_.erase(it); |
117 bitrate_observer_configs_.erase(it); | |
118 } | 117 } |
tommi
2016/06/29 14:49:41
please :)
| |
119 } | 118 |
120 UpdateAllocationLimits(); | 119 UpdateAllocationLimits(); |
121 } | 120 } |
122 | 121 |
123 int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) { | 122 int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) { |
124 rtc::CritScope lock(&crit_sect_); | 123 RTC_DCHECK_RUN_ON(&thread_checker_); |
125 const auto& it = last_allocation_.find(observer); | 124 const auto& it = last_allocation_.find(observer); |
126 if (it != last_allocation_.end()) | 125 if (it != last_allocation_.end()) |
127 return it->second; | 126 return it->second; |
128 | 127 |
129 // This is a new observer that has not yet been started. Assume that if it is | 128 // This is a new observer that has not yet been started. Assume that if it is |
130 // added, all observers would split the available bitrate evenly. | 129 // added, all observers would split the available bitrate evenly. |
131 return last_non_zero_bitrate_bps_ / | 130 return last_non_zero_bitrate_bps_ / |
132 static_cast<int>((bitrate_observer_configs_.size() + 1)); | 131 static_cast<int>((bitrate_observer_configs_.size() + 1)); |
133 } | 132 } |
134 | 133 |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
325 uint32_t extra_bitrate_per_observer = (bitrate - sum_min_bitrates) / | 324 uint32_t extra_bitrate_per_observer = (bitrate - sum_min_bitrates) / |
326 static_cast<uint32_t>(bitrate_observer_configs_.size()); | 325 static_cast<uint32_t>(bitrate_observer_configs_.size()); |
327 for (const auto& observer_config : bitrate_observer_configs_) { | 326 for (const auto& observer_config : bitrate_observer_configs_) { |
328 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer < | 327 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer < |
329 MinBitrateWithHysteresis(observer_config)) | 328 MinBitrateWithHysteresis(observer_config)) |
330 return false; | 329 return false; |
331 } | 330 } |
332 return true; | 331 return true; |
333 } | 332 } |
334 } // namespace webrtc | 333 } // namespace webrtc |
OLD | NEW |