blob: f4c7ee7043b4646a69f31b846c12d673ed784f67 [file] [log] [blame]
Alex Narestd0e196b2017-11-22 17:22:35 +01001/*
2 * Copyright 2017 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "rtc_base/bitrateallocationstrategy.h"
12#include "rtc_base/gunit.h"
13
14namespace rtc {
15
16std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
17MakeTrackConfigPtrsVector(
18 const std::vector<BitrateAllocationStrategy::TrackConfig>& track_configs) {
19 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
20 track_config_ptrs(track_configs.size());
21 int i = 0;
22 for (const auto& c : track_configs) {
23 track_config_ptrs[i++] = &c;
24 }
25 return track_config_ptrs;
26}
27
28TEST(BitrateAllocationStrategyTest, SetAllBitratesToMinimum) {
29 const std::string audio_track_id = "audio_track";
30 constexpr uint32_t min_audio_bitrate = 6000;
31 constexpr uint32_t max_audio_bitrate = 64000;
32 const std::string video_track_id = "video_track";
33 constexpr uint32_t min_video_bitrate = 30000;
34 constexpr uint32_t max_video_bitrate = 300000;
35 constexpr uint32_t min_other_bitrate = 3000;
36 constexpr uint32_t max_other_bitrate = 30000;
37
38 std::vector<BitrateAllocationStrategy::TrackConfig> track_configs = {
39 BitrateAllocationStrategy::TrackConfig(
40 min_audio_bitrate, max_audio_bitrate, false, audio_track_id),
41 BitrateAllocationStrategy::TrackConfig(
42 min_video_bitrate, max_video_bitrate, false, video_track_id),
43 BitrateAllocationStrategy::TrackConfig(min_other_bitrate,
44 max_other_bitrate, false, "")};
45
Alex Narestd0e196b2017-11-22 17:22:35 +010046 std::vector<uint32_t> allocations =
Sebastian Janssone2754c92018-10-24 16:02:26 +020047 BitrateAllocationStrategy::SetAllBitratesToMinimum(track_configs);
Alex Narestd0e196b2017-11-22 17:22:35 +010048 EXPECT_EQ(min_audio_bitrate, allocations[0]);
49 EXPECT_EQ(min_video_bitrate, allocations[1]);
50 EXPECT_EQ(min_other_bitrate, allocations[2]);
51}
52
53TEST(BitrateAllocationStrategyTest, DistributeBitratesEvenly) {
54 const std::string audio_track_id = "audio_track";
55 constexpr uint32_t min_audio_bitrate = 16000;
56 constexpr uint32_t max_audio_bitrate = 64000;
57 const std::string video_track_id = "video_track";
58 constexpr uint32_t min_video_bitrate = 30000;
59 constexpr uint32_t max_video_bitrate = 300000;
60 constexpr uint32_t min_other_bitrate = 3000;
61 constexpr uint32_t max_other_bitrate = 30000;
62 constexpr uint32_t available_bitrate = 52000;
63 constexpr uint32_t even_bitrate_increase =
64 (available_bitrate - min_audio_bitrate - min_video_bitrate -
65 min_other_bitrate) /
66 3;
67
68 std::vector<BitrateAllocationStrategy::TrackConfig> track_configs = {
69 BitrateAllocationStrategy::TrackConfig(
70 min_audio_bitrate, max_audio_bitrate, false, audio_track_id),
71 BitrateAllocationStrategy::TrackConfig(
72 min_video_bitrate, max_video_bitrate, false, video_track_id),
73 BitrateAllocationStrategy::TrackConfig(min_other_bitrate,
74 max_other_bitrate, false, "")};
75
Alex Narestd0e196b2017-11-22 17:22:35 +010076 std::vector<uint32_t> allocations =
Sebastian Janssone2754c92018-10-24 16:02:26 +020077 BitrateAllocationStrategy::DistributeBitratesEvenly(track_configs,
Alex Narestd0e196b2017-11-22 17:22:35 +010078 available_bitrate);
79 EXPECT_EQ(min_audio_bitrate + even_bitrate_increase, allocations[0]);
80 EXPECT_EQ(min_video_bitrate + even_bitrate_increase, allocations[1]);
81 EXPECT_EQ(min_other_bitrate + even_bitrate_increase, allocations[2]);
82}
83
84std::vector<uint32_t> RunAudioPriorityAllocation(
85 uint32_t sufficient_audio_bitrate,
86 std::string audio_track_id,
87 uint32_t min_audio_bitrate,
88 uint32_t max_audio_bitrate,
89 std::string video_track_id,
90 uint32_t min_video_bitrate,
91 uint32_t max_video_bitrate,
92 uint32_t min_other_bitrate,
93 uint32_t max_other_bitrate,
94 uint32_t available_bitrate) {
95 AudioPriorityBitrateAllocationStrategy allocation_strategy(
96 audio_track_id, sufficient_audio_bitrate);
97 std::vector<BitrateAllocationStrategy::TrackConfig> track_configs = {
98 BitrateAllocationStrategy::TrackConfig(
99 min_audio_bitrate, max_audio_bitrate, false, audio_track_id),
100 BitrateAllocationStrategy::TrackConfig(
101 min_video_bitrate, max_video_bitrate, false, video_track_id),
102 BitrateAllocationStrategy::TrackConfig(min_other_bitrate,
103 max_other_bitrate, false, "")};
104
Sebastian Janssone2754c92018-10-24 16:02:26 +0200105 return allocation_strategy.AllocateBitrates(available_bitrate, track_configs);
Alex Narestd0e196b2017-11-22 17:22:35 +0100106}
107
108// Test that when the available bitrate is less than the sum of the minimum
109// bitrates, the minimum bitrate is allocated for each track.
110TEST(AudioPriorityBitrateAllocationStrategyTest, MinAllocateBitrate) {
111 constexpr uint32_t sufficient_audio_bitrate = 16000;
112 const std::string audio_track_id = "audio_track";
113 constexpr uint32_t min_audio_bitrate = 6000;
114 constexpr uint32_t max_audio_bitrate = 64000;
115 const std::string video_track_id = "video_track";
116 constexpr uint32_t min_video_bitrate = 30000;
117 constexpr uint32_t max_video_bitrate = 300000;
118 constexpr uint32_t min_other_bitrate = 3000;
119 constexpr uint32_t max_other_bitrate = 30000;
120 constexpr uint32_t available_bitrate = 10000;
121
122 std::vector<uint32_t> allocations = RunAudioPriorityAllocation(
123 sufficient_audio_bitrate, audio_track_id, min_audio_bitrate,
124 max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate,
125 min_other_bitrate, max_other_bitrate, available_bitrate);
126 EXPECT_EQ(min_audio_bitrate, allocations[0]);
127 EXPECT_EQ(min_video_bitrate, allocations[1]);
128 EXPECT_EQ(min_other_bitrate, allocations[2]);
129}
130
131// Test that when the available bitrate is more than the sum of the max
132// bitrates, the max bitrate is allocated for each track.
133TEST(AudioPriorityBitrateAllocationStrategyTest, MaxAllocateBitrate) {
134 constexpr uint32_t sufficient_audio_bitrate = 16000;
135 const std::string audio_track_id = "audio_track";
136 constexpr uint32_t min_audio_bitrate = 6000;
137 constexpr uint32_t max_audio_bitrate = 64000;
138 const std::string video_track_id = "video_track";
139 constexpr uint32_t min_video_bitrate = 30000;
140 constexpr uint32_t max_video_bitrate = 300000;
141 constexpr uint32_t min_other_bitrate = 3000;
142 constexpr uint32_t max_other_bitrate = 30000;
143 constexpr uint32_t available_bitrate = 400000;
144
145 std::vector<uint32_t> allocations = RunAudioPriorityAllocation(
146 sufficient_audio_bitrate, audio_track_id, min_audio_bitrate,
147 max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate,
148 min_other_bitrate, max_other_bitrate, available_bitrate);
149
150 // TODO(bugs.webrtc.org/8541): Until the bug is fixed not audio streams will
151 // get up to kTransmissionMaxBitrateMultiplier*max_bitrate
152 constexpr uint32_t video_bitrate =
153 (available_bitrate - max_audio_bitrate - max_other_bitrate * 2);
154 EXPECT_EQ(max_audio_bitrate, allocations[0]);
155 EXPECT_EQ(video_bitrate, allocations[1]);
156 EXPECT_EQ(max_other_bitrate * 2, allocations[2]);
157}
158
159// Test that audio track will get up to sufficient bitrate before video and
160// other bitrate will be allocated.
161TEST(AudioPriorityBitrateAllocationStrategyTest, AudioPriorityAllocateBitrate) {
162 constexpr uint32_t sufficient_audio_bitrate = 16000;
163 const std::string audio_track_id = "audio_track";
164 constexpr uint32_t min_audio_bitrate = 6000;
165 constexpr uint32_t max_audio_bitrate = 64000;
166 const std::string video_track_id = "video_track";
167 constexpr uint32_t min_video_bitrate = 30000;
168 constexpr uint32_t max_video_bitrate = 300000;
169 constexpr uint32_t min_other_bitrate = 3000;
170 constexpr uint32_t max_other_bitrate = 30000;
171 constexpr uint32_t available_bitrate = 49000;
172
173 std::vector<uint32_t> allocations = RunAudioPriorityAllocation(
174 sufficient_audio_bitrate, audio_track_id, min_audio_bitrate,
175 max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate,
176 min_other_bitrate, max_other_bitrate, available_bitrate);
177 EXPECT_EQ(sufficient_audio_bitrate, allocations[0]);
178 EXPECT_EQ(min_video_bitrate, allocations[1]);
179 EXPECT_EQ(min_other_bitrate, allocations[2]);
180}
181
182// Test that bitrate will be allocated evenly after sufficient audio bitrate is
183// allocated.
184TEST(AudioPriorityBitrateAllocationStrategyTest, EvenAllocateBitrate) {
185 constexpr uint32_t sufficient_audio_bitrate = 16000;
186 const std::string audio_track_id = "audio_track";
187 constexpr uint32_t min_audio_bitrate = 6000;
188 constexpr uint32_t max_audio_bitrate = 64000;
189 const std::string video_track_id = "video_track";
190 constexpr uint32_t min_video_bitrate = 30000;
191 constexpr uint32_t max_video_bitrate = 300000;
192 constexpr uint32_t min_other_bitrate = 3000;
193 constexpr uint32_t max_other_bitrate = 30000;
194 constexpr uint32_t available_bitrate = 52000;
195 constexpr uint32_t even_bitrate_increase =
196 (available_bitrate - sufficient_audio_bitrate - min_video_bitrate -
197 min_other_bitrate) /
198 3;
199
200 std::vector<uint32_t> allocations = RunAudioPriorityAllocation(
201 sufficient_audio_bitrate, audio_track_id, min_audio_bitrate,
202 max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate,
203 min_other_bitrate, max_other_bitrate, available_bitrate);
204 EXPECT_EQ(sufficient_audio_bitrate + even_bitrate_increase, allocations[0]);
205 EXPECT_EQ(min_video_bitrate + even_bitrate_increase, allocations[1]);
206 EXPECT_EQ(min_other_bitrate + even_bitrate_increase, allocations[2]);
207}
208
209// Test that bitrate will be allocated to video after audio and other max
210// allocation.
211TEST(AudioPriorityBitrateAllocationStrategyTest, VideoAllocateBitrate) {
212 constexpr uint32_t sufficient_audio_bitrate = 16000;
213 const std::string audio_track_id = "audio_track";
214 constexpr uint32_t min_audio_bitrate = 6000;
215 constexpr uint32_t max_audio_bitrate = 64000;
216 const std::string video_track_id = "video_track";
217 constexpr uint32_t min_video_bitrate = 30000;
218 constexpr uint32_t max_video_bitrate = 300000;
219 constexpr uint32_t min_other_bitrate = 3000;
220 constexpr uint32_t max_other_bitrate = 30000;
221 constexpr uint32_t available_bitrate = 200000;
222 constexpr uint32_t video_bitrate =
223 available_bitrate - max_audio_bitrate - max_other_bitrate;
224
225 std::vector<uint32_t> allocations = RunAudioPriorityAllocation(
226 sufficient_audio_bitrate, audio_track_id, min_audio_bitrate,
227 max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate,
228 min_other_bitrate, max_other_bitrate, available_bitrate);
229 EXPECT_EQ(max_audio_bitrate, allocations[0]);
230 EXPECT_EQ(video_bitrate, allocations[1]);
231 EXPECT_EQ(max_other_bitrate, allocations[2]);
232}
233
234} // namespace rtc