Alex Narest | d0e196b | 2017-11-22 17:22:35 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | namespace rtc { |
| 15 | |
| 16 | std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*> |
| 17 | MakeTrackConfigPtrsVector( |
| 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 | |
| 28 | TEST(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 | |
| 46 | std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*> |
| 47 | track_config_ptrs = MakeTrackConfigPtrsVector(track_configs); |
| 48 | |
| 49 | std::vector<uint32_t> allocations = |
| 50 | BitrateAllocationStrategy::SetAllBitratesToMinimum(track_config_ptrs); |
| 51 | EXPECT_EQ(min_audio_bitrate, allocations[0]); |
| 52 | EXPECT_EQ(min_video_bitrate, allocations[1]); |
| 53 | EXPECT_EQ(min_other_bitrate, allocations[2]); |
| 54 | } |
| 55 | |
| 56 | TEST(BitrateAllocationStrategyTest, DistributeBitratesEvenly) { |
| 57 | const std::string audio_track_id = "audio_track"; |
| 58 | constexpr uint32_t min_audio_bitrate = 16000; |
| 59 | constexpr uint32_t max_audio_bitrate = 64000; |
| 60 | const std::string video_track_id = "video_track"; |
| 61 | constexpr uint32_t min_video_bitrate = 30000; |
| 62 | constexpr uint32_t max_video_bitrate = 300000; |
| 63 | constexpr uint32_t min_other_bitrate = 3000; |
| 64 | constexpr uint32_t max_other_bitrate = 30000; |
| 65 | constexpr uint32_t available_bitrate = 52000; |
| 66 | constexpr uint32_t even_bitrate_increase = |
| 67 | (available_bitrate - min_audio_bitrate - min_video_bitrate - |
| 68 | min_other_bitrate) / |
| 69 | 3; |
| 70 | |
| 71 | std::vector<BitrateAllocationStrategy::TrackConfig> track_configs = { |
| 72 | BitrateAllocationStrategy::TrackConfig( |
| 73 | min_audio_bitrate, max_audio_bitrate, false, audio_track_id), |
| 74 | BitrateAllocationStrategy::TrackConfig( |
| 75 | min_video_bitrate, max_video_bitrate, false, video_track_id), |
| 76 | BitrateAllocationStrategy::TrackConfig(min_other_bitrate, |
| 77 | max_other_bitrate, false, "")}; |
| 78 | |
| 79 | std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*> |
| 80 | track_config_ptrs = MakeTrackConfigPtrsVector(track_configs); |
| 81 | |
| 82 | std::vector<uint32_t> allocations = |
| 83 | BitrateAllocationStrategy::DistributeBitratesEvenly(track_config_ptrs, |
| 84 | available_bitrate); |
| 85 | EXPECT_EQ(min_audio_bitrate + even_bitrate_increase, allocations[0]); |
| 86 | EXPECT_EQ(min_video_bitrate + even_bitrate_increase, allocations[1]); |
| 87 | EXPECT_EQ(min_other_bitrate + even_bitrate_increase, allocations[2]); |
| 88 | } |
| 89 | |
| 90 | std::vector<uint32_t> RunAudioPriorityAllocation( |
| 91 | uint32_t sufficient_audio_bitrate, |
| 92 | std::string audio_track_id, |
| 93 | uint32_t min_audio_bitrate, |
| 94 | uint32_t max_audio_bitrate, |
| 95 | std::string video_track_id, |
| 96 | uint32_t min_video_bitrate, |
| 97 | uint32_t max_video_bitrate, |
| 98 | uint32_t min_other_bitrate, |
| 99 | uint32_t max_other_bitrate, |
| 100 | uint32_t available_bitrate) { |
| 101 | AudioPriorityBitrateAllocationStrategy allocation_strategy( |
| 102 | audio_track_id, sufficient_audio_bitrate); |
| 103 | std::vector<BitrateAllocationStrategy::TrackConfig> track_configs = { |
| 104 | BitrateAllocationStrategy::TrackConfig( |
| 105 | min_audio_bitrate, max_audio_bitrate, false, audio_track_id), |
| 106 | BitrateAllocationStrategy::TrackConfig( |
| 107 | min_video_bitrate, max_video_bitrate, false, video_track_id), |
| 108 | BitrateAllocationStrategy::TrackConfig(min_other_bitrate, |
| 109 | max_other_bitrate, false, "")}; |
| 110 | |
| 111 | std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*> |
| 112 | track_config_ptrs = MakeTrackConfigPtrsVector(track_configs); |
| 113 | |
| 114 | return allocation_strategy.AllocateBitrates(available_bitrate, |
| 115 | track_config_ptrs); |
| 116 | } |
| 117 | |
| 118 | // Test that when the available bitrate is less than the sum of the minimum |
| 119 | // bitrates, the minimum bitrate is allocated for each track. |
| 120 | TEST(AudioPriorityBitrateAllocationStrategyTest, MinAllocateBitrate) { |
| 121 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 122 | const std::string audio_track_id = "audio_track"; |
| 123 | constexpr uint32_t min_audio_bitrate = 6000; |
| 124 | constexpr uint32_t max_audio_bitrate = 64000; |
| 125 | const std::string video_track_id = "video_track"; |
| 126 | constexpr uint32_t min_video_bitrate = 30000; |
| 127 | constexpr uint32_t max_video_bitrate = 300000; |
| 128 | constexpr uint32_t min_other_bitrate = 3000; |
| 129 | constexpr uint32_t max_other_bitrate = 30000; |
| 130 | constexpr uint32_t available_bitrate = 10000; |
| 131 | |
| 132 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 133 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 134 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 135 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 136 | EXPECT_EQ(min_audio_bitrate, allocations[0]); |
| 137 | EXPECT_EQ(min_video_bitrate, allocations[1]); |
| 138 | EXPECT_EQ(min_other_bitrate, allocations[2]); |
| 139 | } |
| 140 | |
| 141 | // Test that when the available bitrate is more than the sum of the max |
| 142 | // bitrates, the max bitrate is allocated for each track. |
| 143 | TEST(AudioPriorityBitrateAllocationStrategyTest, MaxAllocateBitrate) { |
| 144 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 145 | const std::string audio_track_id = "audio_track"; |
| 146 | constexpr uint32_t min_audio_bitrate = 6000; |
| 147 | constexpr uint32_t max_audio_bitrate = 64000; |
| 148 | const std::string video_track_id = "video_track"; |
| 149 | constexpr uint32_t min_video_bitrate = 30000; |
| 150 | constexpr uint32_t max_video_bitrate = 300000; |
| 151 | constexpr uint32_t min_other_bitrate = 3000; |
| 152 | constexpr uint32_t max_other_bitrate = 30000; |
| 153 | constexpr uint32_t available_bitrate = 400000; |
| 154 | |
| 155 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 156 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 157 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 158 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 159 | |
| 160 | // TODO(bugs.webrtc.org/8541): Until the bug is fixed not audio streams will |
| 161 | // get up to kTransmissionMaxBitrateMultiplier*max_bitrate |
| 162 | constexpr uint32_t video_bitrate = |
| 163 | (available_bitrate - max_audio_bitrate - max_other_bitrate * 2); |
| 164 | EXPECT_EQ(max_audio_bitrate, allocations[0]); |
| 165 | EXPECT_EQ(video_bitrate, allocations[1]); |
| 166 | EXPECT_EQ(max_other_bitrate * 2, allocations[2]); |
| 167 | } |
| 168 | |
| 169 | // Test that audio track will get up to sufficient bitrate before video and |
| 170 | // other bitrate will be allocated. |
| 171 | TEST(AudioPriorityBitrateAllocationStrategyTest, AudioPriorityAllocateBitrate) { |
| 172 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 173 | const std::string audio_track_id = "audio_track"; |
| 174 | constexpr uint32_t min_audio_bitrate = 6000; |
| 175 | constexpr uint32_t max_audio_bitrate = 64000; |
| 176 | const std::string video_track_id = "video_track"; |
| 177 | constexpr uint32_t min_video_bitrate = 30000; |
| 178 | constexpr uint32_t max_video_bitrate = 300000; |
| 179 | constexpr uint32_t min_other_bitrate = 3000; |
| 180 | constexpr uint32_t max_other_bitrate = 30000; |
| 181 | constexpr uint32_t available_bitrate = 49000; |
| 182 | |
| 183 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 184 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 185 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 186 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 187 | EXPECT_EQ(sufficient_audio_bitrate, allocations[0]); |
| 188 | EXPECT_EQ(min_video_bitrate, allocations[1]); |
| 189 | EXPECT_EQ(min_other_bitrate, allocations[2]); |
| 190 | } |
| 191 | |
| 192 | // Test that bitrate will be allocated evenly after sufficient audio bitrate is |
| 193 | // allocated. |
| 194 | TEST(AudioPriorityBitrateAllocationStrategyTest, EvenAllocateBitrate) { |
| 195 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 196 | const std::string audio_track_id = "audio_track"; |
| 197 | constexpr uint32_t min_audio_bitrate = 6000; |
| 198 | constexpr uint32_t max_audio_bitrate = 64000; |
| 199 | const std::string video_track_id = "video_track"; |
| 200 | constexpr uint32_t min_video_bitrate = 30000; |
| 201 | constexpr uint32_t max_video_bitrate = 300000; |
| 202 | constexpr uint32_t min_other_bitrate = 3000; |
| 203 | constexpr uint32_t max_other_bitrate = 30000; |
| 204 | constexpr uint32_t available_bitrate = 52000; |
| 205 | constexpr uint32_t even_bitrate_increase = |
| 206 | (available_bitrate - sufficient_audio_bitrate - min_video_bitrate - |
| 207 | min_other_bitrate) / |
| 208 | 3; |
| 209 | |
| 210 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 211 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 212 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 213 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 214 | EXPECT_EQ(sufficient_audio_bitrate + even_bitrate_increase, allocations[0]); |
| 215 | EXPECT_EQ(min_video_bitrate + even_bitrate_increase, allocations[1]); |
| 216 | EXPECT_EQ(min_other_bitrate + even_bitrate_increase, allocations[2]); |
| 217 | } |
| 218 | |
| 219 | // Test that bitrate will be allocated to video after audio and other max |
| 220 | // allocation. |
| 221 | TEST(AudioPriorityBitrateAllocationStrategyTest, VideoAllocateBitrate) { |
| 222 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 223 | const std::string audio_track_id = "audio_track"; |
| 224 | constexpr uint32_t min_audio_bitrate = 6000; |
| 225 | constexpr uint32_t max_audio_bitrate = 64000; |
| 226 | const std::string video_track_id = "video_track"; |
| 227 | constexpr uint32_t min_video_bitrate = 30000; |
| 228 | constexpr uint32_t max_video_bitrate = 300000; |
| 229 | constexpr uint32_t min_other_bitrate = 3000; |
| 230 | constexpr uint32_t max_other_bitrate = 30000; |
| 231 | constexpr uint32_t available_bitrate = 200000; |
| 232 | constexpr uint32_t video_bitrate = |
| 233 | available_bitrate - max_audio_bitrate - max_other_bitrate; |
| 234 | |
| 235 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 236 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 237 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 238 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 239 | EXPECT_EQ(max_audio_bitrate, allocations[0]); |
| 240 | EXPECT_EQ(video_bitrate, allocations[1]); |
| 241 | EXPECT_EQ(max_other_bitrate, allocations[2]); |
| 242 | } |
| 243 | |
| 244 | } // namespace rtc |