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 |
11 #include "webrtc/modules/video_coding/utility/simulcast_rate_allocator.h" | 11 #include "webrtc/modules/video_coding/utility/simulcast_rate_allocator.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <memory> | |
15 #include <vector> | |
16 #include <utility> | |
17 | |
18 #include "webrtc/base/checks.h" | |
19 | 14 |
20 namespace webrtc { | 15 namespace webrtc { |
21 | 16 |
22 SimulcastRateAllocator::SimulcastRateAllocator( | 17 webrtc::SimulcastRateAllocator::SimulcastRateAllocator(const VideoCodec& codec) |
23 const VideoCodec& codec, | 18 : codec_(codec) {} |
24 std::unique_ptr<TemporalLayersFactory> tl_factory) | |
25 : codec_(codec), tl_factory_(std::move(tl_factory)) { | |
26 if (tl_factory_.get()) | |
27 tl_factory_->SetListener(this); | |
28 } | |
29 | 19 |
30 void SimulcastRateAllocator::OnTemporalLayersCreated(int simulcast_id, | 20 std::vector<uint32_t> webrtc::SimulcastRateAllocator::GetAllocation( |
31 TemporalLayers* layers) { | 21 uint32_t bitrate_kbps) const { |
32 RTC_DCHECK(temporal_layers_.find(simulcast_id) == temporal_layers_.end()); | 22 // Always allocate enough bitrate for the minimum bitrate of the first layer. |
33 temporal_layers_[simulcast_id] = layers; | 23 // Suspending below min bitrate is controlled outside the codec implementation |
34 } | 24 // and is not overridden by this. |
| 25 const uint32_t min_bitrate_bps = codec_.numberOfSimulcastStreams == 0 |
| 26 ? codec_.minBitrate |
| 27 : codec_.simulcastStream[0].minBitrate; |
| 28 uint32_t left_to_allocate = std::max(min_bitrate_bps, bitrate_kbps); |
| 29 if (codec_.maxBitrate) |
| 30 left_to_allocate = std::min(left_to_allocate, codec_.maxBitrate); |
35 | 31 |
36 BitrateAllocation SimulcastRateAllocator::GetAllocation( | 32 if (codec_.numberOfSimulcastStreams < 2) { |
37 uint32_t total_bitrate_bps, | |
38 uint32_t framerate) { | |
39 uint32_t left_to_allocate = total_bitrate_bps; | |
40 if (codec_.maxBitrate && codec_.maxBitrate * 1000 < left_to_allocate) | |
41 left_to_allocate = codec_.maxBitrate * 1000; | |
42 | |
43 BitrateAllocation allocated_bitrates_bps; | |
44 if (codec_.numberOfSimulcastStreams == 0) { | |
45 // No simulcast, just set the target as this has been capped already. | 33 // No simulcast, just set the target as this has been capped already. |
46 allocated_bitrates_bps.SetBitrate( | 34 return std::vector<uint32_t>(1, left_to_allocate); |
47 0, 0, std::max(codec_.minBitrate * 1000, left_to_allocate)); | |
48 } else { | |
49 // Always allocate enough bitrate for the minimum bitrate of the first | |
50 // layer. Suspending below min bitrate is controlled outside the codec | |
51 // implementation and is not overridden by this. | |
52 left_to_allocate = | |
53 std::max(codec_.simulcastStream[0].minBitrate * 1000, left_to_allocate); | |
54 | |
55 // Begin by allocating bitrate to simulcast streams, putting all bitrate in | |
56 // temporal layer 0. We'll then distribute this bitrate, across potential | |
57 // temporal layers, when stream allocation is done. | |
58 | |
59 // Allocate up to the target bitrate for each simulcast layer. | |
60 size_t layer = 0; | |
61 for (; layer < codec_.numberOfSimulcastStreams; ++layer) { | |
62 const SimulcastStream& stream = codec_.simulcastStream[layer]; | |
63 if (left_to_allocate < stream.minBitrate * 1000) | |
64 break; | |
65 uint32_t allocation = | |
66 std::min(left_to_allocate, stream.targetBitrate * 1000); | |
67 allocated_bitrates_bps.SetBitrate(layer, 0, allocation); | |
68 RTC_DCHECK_LE(allocation, left_to_allocate); | |
69 left_to_allocate -= allocation; | |
70 } | |
71 | |
72 // Next, try allocate remaining bitrate, up to max bitrate, in top stream. | |
73 // TODO(sprang): Allocate up to max bitrate for all layers once we have a | |
74 // better idea of possible performance implications. | |
75 if (left_to_allocate > 0) { | |
76 size_t active_layer = layer - 1; | |
77 const SimulcastStream& stream = codec_.simulcastStream[active_layer]; | |
78 uint32_t bitrate_bps = | |
79 allocated_bitrates_bps.GetSpatialLayerSum(active_layer); | |
80 uint32_t allocation = | |
81 std::min(left_to_allocate, stream.maxBitrate * 1000 - bitrate_bps); | |
82 bitrate_bps += allocation; | |
83 RTC_DCHECK_LE(allocation, left_to_allocate); | |
84 left_to_allocate -= allocation; | |
85 allocated_bitrates_bps.SetBitrate(active_layer, 0, bitrate_bps); | |
86 } | |
87 } | 35 } |
88 | 36 |
89 const int num_spatial_streams = | 37 // Initialize bitrates with zeroes. |
90 std::max(1, static_cast<int>(codec_.numberOfSimulcastStreams)); | 38 std::vector<uint32_t> allocated_bitrates_bps(codec_.numberOfSimulcastStreams, |
| 39 0); |
91 | 40 |
92 // Finally, distribute the bitrate for the simulcast streams across the | 41 // First try to allocate up to the target bitrate for each substream. |
93 // available temporal layers. | 42 size_t layer = 0; |
94 for (int simulcast_id = 0; simulcast_id < num_spatial_streams; | 43 for (; layer < codec_.numberOfSimulcastStreams; ++layer) { |
95 ++simulcast_id) { | 44 const SimulcastStream& stream = codec_.simulcastStream[layer]; |
96 auto tl_it = temporal_layers_.find(simulcast_id); | 45 if (left_to_allocate < stream.minBitrate) |
97 if (tl_it == temporal_layers_.end()) | 46 break; |
98 continue; // TODO(sprang): If > 1 SS, assume default TL alloc? | 47 uint32_t allocation = std::min(left_to_allocate, stream.targetBitrate); |
| 48 allocated_bitrates_bps[layer] = allocation; |
| 49 left_to_allocate -= allocation; |
| 50 } |
99 | 51 |
100 uint32_t target_bitrate_kbps = | 52 // Next, try allocate remaining bitrate, up to max bitrate, in top layer. |
101 allocated_bitrates_bps.GetBitrate(simulcast_id, 0) / 1000; | 53 // TODO(sprang): Allocate up to max bitrate for all layers once we have a |
102 RTC_DCHECK_EQ( | 54 // better idea of possible performance implications. |
103 target_bitrate_kbps, | 55 if (left_to_allocate > 0) { |
104 allocated_bitrates_bps.GetSpatialLayerSum(simulcast_id) / 1000); | 56 size_t active_layer = layer - 1; |
105 uint32_t max_bitrate_kbps; | 57 const SimulcastStream& stream = codec_.simulcastStream[active_layer]; |
106 if (codec_.numberOfSimulcastStreams == 0) { | 58 uint32_t allocation = |
107 max_bitrate_kbps = codec_.maxBitrate; | 59 std::min(left_to_allocate, |
108 | 60 stream.maxBitrate - allocated_bitrates_bps[active_layer]); |
109 // TODO(holmer): This is a temporary hack for screensharing, where we | 61 left_to_allocate -= allocation; |
110 // interpret the startBitrate as the encoder target bitrate. This is | 62 allocated_bitrates_bps[active_layer] += allocation; |
111 // to allow for a different max bitrate, so if the codec can't meet | |
112 // the target we still allow it to overshoot up to the max before dropping | |
113 // frames. This hack should be improved. | |
114 if (codec_.mode == kScreensharing && codec_.targetBitrate > 0 && | |
115 (codec_.codecSpecific.VP8.numberOfTemporalLayers == 2 || | |
116 codec_.simulcastStream[0].numberOfTemporalLayers == 2)) { | |
117 int tl0_bitrate = std::min(codec_.targetBitrate, target_bitrate_kbps); | |
118 max_bitrate_kbps = std::min(codec_.maxBitrate, target_bitrate_kbps); | |
119 target_bitrate_kbps = tl0_bitrate; | |
120 } | |
121 } else { | |
122 max_bitrate_kbps = codec_.simulcastStream[simulcast_id].maxBitrate; | |
123 } | |
124 | |
125 std::vector<uint32_t> tl_allocation = tl_it->second->OnRatesUpdated( | |
126 target_bitrate_kbps, max_bitrate_kbps, framerate); | |
127 | |
128 for (size_t tl_index = 0; tl_index < tl_allocation.size(); ++tl_index) { | |
129 allocated_bitrates_bps.SetBitrate(simulcast_id, tl_index, | |
130 tl_allocation[tl_index] * 1000); | |
131 } | |
132 } | 63 } |
133 | 64 |
134 return allocated_bitrates_bps; | 65 return allocated_bitrates_bps; |
135 } | 66 } |
136 | 67 |
137 uint32_t SimulcastRateAllocator::GetPreferredBitrateBps(uint32_t framerate) { | 68 uint32_t SimulcastRateAllocator::GetPreferedBitrate() const { |
138 BitrateAllocation allocation = | 69 std::vector<uint32_t> rates = GetAllocation(codec_.maxBitrate); |
139 GetAllocation(codec_.maxBitrate * 1000, framerate); | 70 uint32_t preferred_bitrate = 0; |
140 return allocation.get_sum_bps(); | 71 for (const uint32_t& rate : rates) { |
| 72 preferred_bitrate += rate; |
| 73 } |
| 74 return preferred_bitrate; |
141 } | 75 } |
142 | 76 |
143 const VideoCodec& webrtc::SimulcastRateAllocator::GetCodec() const { | 77 const VideoCodec& webrtc::SimulcastRateAllocator::GetCodec() const { |
144 return codec_; | 78 return codec_; |
145 } | 79 } |
146 | 80 |
147 } // namespace webrtc | 81 } // namespace webrtc |
OLD | NEW |