OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 // Finally, distribute the bitrate for the simulcast streams across the | 92 // Finally, distribute the bitrate for the simulcast streams across the |
93 // available temporal layers. | 93 // available temporal layers. |
94 for (int simulcast_id = 0; simulcast_id < num_spatial_streams; | 94 for (int simulcast_id = 0; simulcast_id < num_spatial_streams; |
95 ++simulcast_id) { | 95 ++simulcast_id) { |
96 auto tl_it = temporal_layers_.find(simulcast_id); | 96 auto tl_it = temporal_layers_.find(simulcast_id); |
97 if (tl_it == temporal_layers_.end()) | 97 if (tl_it == temporal_layers_.end()) |
98 continue; // TODO(sprang): If > 1 SS, assume default TL alloc? | 98 continue; // TODO(sprang): If > 1 SS, assume default TL alloc? |
99 | 99 |
100 uint32_t target_bitrate_kbps = | 100 uint32_t target_bitrate_kbps = |
101 allocated_bitrates_bps.GetBitrate(simulcast_id, 0) / 1000; | 101 allocated_bitrates_bps.GetBitrate(simulcast_id, 0) / 1000; |
| 102 const uint32_t expected_allocated_bitrate_kbps = target_bitrate_kbps; |
102 RTC_DCHECK_EQ( | 103 RTC_DCHECK_EQ( |
103 target_bitrate_kbps, | 104 target_bitrate_kbps, |
104 allocated_bitrates_bps.GetSpatialLayerSum(simulcast_id) / 1000); | 105 allocated_bitrates_bps.GetSpatialLayerSum(simulcast_id) / 1000); |
| 106 const int num_temporal_streams = std::max<uint8_t>( |
| 107 1, codec_.numberOfSimulcastStreams == 0 |
| 108 ? codec_.VP8().numberOfTemporalLayers |
| 109 : codec_.simulcastStream[0].numberOfTemporalLayers); |
| 110 |
105 uint32_t max_bitrate_kbps; | 111 uint32_t max_bitrate_kbps; |
106 if (num_spatial_streams == 1) { | 112 if (num_spatial_streams == 1) { |
107 max_bitrate_kbps = codec_.maxBitrate; | 113 max_bitrate_kbps = codec_.maxBitrate; |
108 const int num_temporal_streams = | |
109 codec_.numberOfSimulcastStreams == 0 | |
110 ? codec_.VP8().numberOfTemporalLayers | |
111 : codec_.simulcastStream[0].numberOfTemporalLayers; | |
112 | 114 |
113 // TODO(holmer): This is a temporary hack for screensharing, where we | 115 // TODO(holmer): This is a temporary hack for screensharing, where we |
114 // interpret the startBitrate as the encoder target bitrate. This is | 116 // interpret the startBitrate as the encoder target bitrate. This is |
115 // to allow for a different max bitrate, so if the codec can't meet | 117 // to allow for a different max bitrate, so if the codec can't meet |
116 // the target we still allow it to overshoot up to the max before dropping | 118 // the target we still allow it to overshoot up to the max before dropping |
117 // frames. This hack should be improved. | 119 // frames. This hack should be improved. |
118 if (codec_.mode == kScreensharing && codec_.targetBitrate > 0 && | 120 if (codec_.mode == kScreensharing && codec_.targetBitrate > 0 && |
119 num_temporal_streams == 2) { | 121 num_temporal_streams == 2) { |
120 int tl0_bitrate = std::min(codec_.targetBitrate, target_bitrate_kbps); | 122 int tl0_bitrate = std::min(codec_.targetBitrate, target_bitrate_kbps); |
121 max_bitrate_kbps = std::min(codec_.maxBitrate, target_bitrate_kbps); | 123 max_bitrate_kbps = std::min(codec_.maxBitrate, target_bitrate_kbps); |
122 target_bitrate_kbps = tl0_bitrate; | 124 target_bitrate_kbps = tl0_bitrate; |
123 } | 125 } |
124 } else { | 126 } else { |
125 max_bitrate_kbps = codec_.simulcastStream[simulcast_id].maxBitrate; | 127 max_bitrate_kbps = codec_.simulcastStream[simulcast_id].maxBitrate; |
126 } | 128 } |
127 | 129 |
128 std::vector<uint32_t> tl_allocation = tl_it->second->OnRatesUpdated( | 130 std::vector<uint32_t> tl_allocation = tl_it->second->OnRatesUpdated( |
129 target_bitrate_kbps, max_bitrate_kbps, framerate); | 131 target_bitrate_kbps, max_bitrate_kbps, framerate); |
| 132 RTC_DCHECK_GT(tl_allocation.size(), 0); |
| 133 RTC_DCHECK_LE(tl_allocation.size(), num_temporal_streams); |
130 | 134 |
| 135 uint64_t tl_allocation_sum_kbps = 0; |
131 for (size_t tl_index = 0; tl_index < tl_allocation.size(); ++tl_index) { | 136 for (size_t tl_index = 0; tl_index < tl_allocation.size(); ++tl_index) { |
| 137 uint32_t layer_rate_kbps = tl_allocation[tl_index]; |
132 allocated_bitrates_bps.SetBitrate(simulcast_id, tl_index, | 138 allocated_bitrates_bps.SetBitrate(simulcast_id, tl_index, |
133 tl_allocation[tl_index] * 1000); | 139 layer_rate_kbps * 1000); |
| 140 tl_allocation_sum_kbps += layer_rate_kbps; |
134 } | 141 } |
| 142 RTC_DCHECK_LE(tl_allocation_sum_kbps, expected_allocated_bitrate_kbps); |
135 } | 143 } |
136 | 144 |
137 return allocated_bitrates_bps; | 145 return allocated_bitrates_bps; |
138 } | 146 } |
139 | 147 |
140 uint32_t SimulcastRateAllocator::GetPreferredBitrateBps(uint32_t framerate) { | 148 uint32_t SimulcastRateAllocator::GetPreferredBitrateBps(uint32_t framerate) { |
| 149 // Create a temporary instance without temporal layers, as they may be |
| 150 // stateful, and updating the bitrate to max here can cause side effects. |
| 151 SimulcastRateAllocator temp_allocator(codec_, nullptr); |
141 BitrateAllocation allocation = | 152 BitrateAllocation allocation = |
142 GetAllocation(codec_.maxBitrate * 1000, framerate); | 153 temp_allocator.GetAllocation(codec_.maxBitrate * 1000, framerate); |
143 return allocation.get_sum_bps(); | 154 return allocation.get_sum_bps(); |
144 } | 155 } |
145 | 156 |
146 const VideoCodec& webrtc::SimulcastRateAllocator::GetCodec() const { | 157 const VideoCodec& webrtc::SimulcastRateAllocator::GetCodec() const { |
147 return codec_; | 158 return codec_; |
148 } | 159 } |
149 | 160 |
150 } // namespace webrtc | 161 } // namespace webrtc |
OLD | NEW |