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" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | |
| 13 | #include <cstdint> |
| 14 | |
| 15 | #include "test/gtest.h" |
Alex Narest | d0e196b | 2017-11-22 17:22:35 +0100 | [diff] [blame] | 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*> |
| 20 | MakeTrackConfigPtrsVector( |
| 21 | const std::vector<BitrateAllocationStrategy::TrackConfig>& track_configs) { |
| 22 | std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*> |
| 23 | track_config_ptrs(track_configs.size()); |
| 24 | int i = 0; |
| 25 | for (const auto& c : track_configs) { |
| 26 | track_config_ptrs[i++] = &c; |
| 27 | } |
| 28 | return track_config_ptrs; |
| 29 | } |
| 30 | |
| 31 | TEST(BitrateAllocationStrategyTest, SetAllBitratesToMinimum) { |
| 32 | const std::string audio_track_id = "audio_track"; |
| 33 | constexpr uint32_t min_audio_bitrate = 6000; |
| 34 | constexpr uint32_t max_audio_bitrate = 64000; |
| 35 | const std::string video_track_id = "video_track"; |
| 36 | constexpr uint32_t min_video_bitrate = 30000; |
| 37 | constexpr uint32_t max_video_bitrate = 300000; |
| 38 | constexpr uint32_t min_other_bitrate = 3000; |
| 39 | constexpr uint32_t max_other_bitrate = 30000; |
| 40 | |
| 41 | std::vector<BitrateAllocationStrategy::TrackConfig> track_configs = { |
| 42 | BitrateAllocationStrategy::TrackConfig( |
| 43 | min_audio_bitrate, max_audio_bitrate, false, audio_track_id), |
| 44 | BitrateAllocationStrategy::TrackConfig( |
| 45 | min_video_bitrate, max_video_bitrate, false, video_track_id), |
| 46 | BitrateAllocationStrategy::TrackConfig(min_other_bitrate, |
| 47 | max_other_bitrate, false, "")}; |
| 48 | |
Alex Narest | d0e196b | 2017-11-22 17:22:35 +0100 | [diff] [blame] | 49 | std::vector<uint32_t> allocations = |
Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame] | 50 | BitrateAllocationStrategy::SetAllBitratesToMinimum(track_configs); |
Alex Narest | d0e196b | 2017-11-22 17:22:35 +0100 | [diff] [blame] | 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 | |
Alex Narest | d0e196b | 2017-11-22 17:22:35 +0100 | [diff] [blame] | 79 | std::vector<uint32_t> allocations = |
Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame] | 80 | BitrateAllocationStrategy::DistributeBitratesEvenly(track_configs, |
Alex Narest | d0e196b | 2017-11-22 17:22:35 +0100 | [diff] [blame] | 81 | available_bitrate); |
| 82 | EXPECT_EQ(min_audio_bitrate + even_bitrate_increase, allocations[0]); |
| 83 | EXPECT_EQ(min_video_bitrate + even_bitrate_increase, allocations[1]); |
| 84 | EXPECT_EQ(min_other_bitrate + even_bitrate_increase, allocations[2]); |
| 85 | } |
| 86 | |
| 87 | std::vector<uint32_t> RunAudioPriorityAllocation( |
| 88 | uint32_t sufficient_audio_bitrate, |
| 89 | std::string audio_track_id, |
| 90 | uint32_t min_audio_bitrate, |
| 91 | uint32_t max_audio_bitrate, |
| 92 | std::string video_track_id, |
| 93 | uint32_t min_video_bitrate, |
| 94 | uint32_t max_video_bitrate, |
| 95 | uint32_t min_other_bitrate, |
| 96 | uint32_t max_other_bitrate, |
| 97 | uint32_t available_bitrate) { |
| 98 | AudioPriorityBitrateAllocationStrategy allocation_strategy( |
| 99 | audio_track_id, sufficient_audio_bitrate); |
| 100 | std::vector<BitrateAllocationStrategy::TrackConfig> track_configs = { |
| 101 | BitrateAllocationStrategy::TrackConfig( |
| 102 | min_audio_bitrate, max_audio_bitrate, false, audio_track_id), |
| 103 | BitrateAllocationStrategy::TrackConfig( |
| 104 | min_video_bitrate, max_video_bitrate, false, video_track_id), |
| 105 | BitrateAllocationStrategy::TrackConfig(min_other_bitrate, |
| 106 | max_other_bitrate, false, "")}; |
| 107 | |
Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame] | 108 | return allocation_strategy.AllocateBitrates(available_bitrate, track_configs); |
Alex Narest | d0e196b | 2017-11-22 17:22:35 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Test that when the available bitrate is less than the sum of the minimum |
| 112 | // bitrates, the minimum bitrate is allocated for each track. |
| 113 | TEST(AudioPriorityBitrateAllocationStrategyTest, MinAllocateBitrate) { |
| 114 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 115 | const std::string audio_track_id = "audio_track"; |
| 116 | constexpr uint32_t min_audio_bitrate = 6000; |
| 117 | constexpr uint32_t max_audio_bitrate = 64000; |
| 118 | const std::string video_track_id = "video_track"; |
| 119 | constexpr uint32_t min_video_bitrate = 30000; |
| 120 | constexpr uint32_t max_video_bitrate = 300000; |
| 121 | constexpr uint32_t min_other_bitrate = 3000; |
| 122 | constexpr uint32_t max_other_bitrate = 30000; |
| 123 | constexpr uint32_t available_bitrate = 10000; |
| 124 | |
| 125 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 126 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 127 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 128 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 129 | EXPECT_EQ(min_audio_bitrate, allocations[0]); |
| 130 | EXPECT_EQ(min_video_bitrate, allocations[1]); |
| 131 | EXPECT_EQ(min_other_bitrate, allocations[2]); |
| 132 | } |
| 133 | |
| 134 | // Test that when the available bitrate is more than the sum of the max |
| 135 | // bitrates, the max bitrate is allocated for each track. |
| 136 | TEST(AudioPriorityBitrateAllocationStrategyTest, MaxAllocateBitrate) { |
| 137 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 138 | const std::string audio_track_id = "audio_track"; |
| 139 | constexpr uint32_t min_audio_bitrate = 6000; |
| 140 | constexpr uint32_t max_audio_bitrate = 64000; |
| 141 | const std::string video_track_id = "video_track"; |
| 142 | constexpr uint32_t min_video_bitrate = 30000; |
| 143 | constexpr uint32_t max_video_bitrate = 300000; |
| 144 | constexpr uint32_t min_other_bitrate = 3000; |
| 145 | constexpr uint32_t max_other_bitrate = 30000; |
| 146 | constexpr uint32_t available_bitrate = 400000; |
| 147 | |
| 148 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 149 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 150 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 151 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 152 | |
| 153 | // TODO(bugs.webrtc.org/8541): Until the bug is fixed not audio streams will |
| 154 | // get up to kTransmissionMaxBitrateMultiplier*max_bitrate |
| 155 | constexpr uint32_t video_bitrate = |
| 156 | (available_bitrate - max_audio_bitrate - max_other_bitrate * 2); |
| 157 | EXPECT_EQ(max_audio_bitrate, allocations[0]); |
| 158 | EXPECT_EQ(video_bitrate, allocations[1]); |
| 159 | EXPECT_EQ(max_other_bitrate * 2, allocations[2]); |
| 160 | } |
| 161 | |
| 162 | // Test that audio track will get up to sufficient bitrate before video and |
| 163 | // other bitrate will be allocated. |
| 164 | TEST(AudioPriorityBitrateAllocationStrategyTest, AudioPriorityAllocateBitrate) { |
| 165 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 166 | const std::string audio_track_id = "audio_track"; |
| 167 | constexpr uint32_t min_audio_bitrate = 6000; |
| 168 | constexpr uint32_t max_audio_bitrate = 64000; |
| 169 | const std::string video_track_id = "video_track"; |
| 170 | constexpr uint32_t min_video_bitrate = 30000; |
| 171 | constexpr uint32_t max_video_bitrate = 300000; |
| 172 | constexpr uint32_t min_other_bitrate = 3000; |
| 173 | constexpr uint32_t max_other_bitrate = 30000; |
| 174 | constexpr uint32_t available_bitrate = 49000; |
| 175 | |
| 176 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 177 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 178 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 179 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 180 | EXPECT_EQ(sufficient_audio_bitrate, allocations[0]); |
| 181 | EXPECT_EQ(min_video_bitrate, allocations[1]); |
| 182 | EXPECT_EQ(min_other_bitrate, allocations[2]); |
| 183 | } |
| 184 | |
| 185 | // Test that bitrate will be allocated evenly after sufficient audio bitrate is |
| 186 | // allocated. |
| 187 | TEST(AudioPriorityBitrateAllocationStrategyTest, EvenAllocateBitrate) { |
| 188 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 189 | const std::string audio_track_id = "audio_track"; |
| 190 | constexpr uint32_t min_audio_bitrate = 6000; |
| 191 | constexpr uint32_t max_audio_bitrate = 64000; |
| 192 | const std::string video_track_id = "video_track"; |
| 193 | constexpr uint32_t min_video_bitrate = 30000; |
| 194 | constexpr uint32_t max_video_bitrate = 300000; |
| 195 | constexpr uint32_t min_other_bitrate = 3000; |
| 196 | constexpr uint32_t max_other_bitrate = 30000; |
| 197 | constexpr uint32_t available_bitrate = 52000; |
| 198 | constexpr uint32_t even_bitrate_increase = |
| 199 | (available_bitrate - sufficient_audio_bitrate - min_video_bitrate - |
| 200 | min_other_bitrate) / |
| 201 | 3; |
| 202 | |
| 203 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 204 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 205 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 206 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 207 | EXPECT_EQ(sufficient_audio_bitrate + even_bitrate_increase, allocations[0]); |
| 208 | EXPECT_EQ(min_video_bitrate + even_bitrate_increase, allocations[1]); |
| 209 | EXPECT_EQ(min_other_bitrate + even_bitrate_increase, allocations[2]); |
| 210 | } |
| 211 | |
| 212 | // Test that bitrate will be allocated to video after audio and other max |
| 213 | // allocation. |
| 214 | TEST(AudioPriorityBitrateAllocationStrategyTest, VideoAllocateBitrate) { |
| 215 | constexpr uint32_t sufficient_audio_bitrate = 16000; |
| 216 | const std::string audio_track_id = "audio_track"; |
| 217 | constexpr uint32_t min_audio_bitrate = 6000; |
| 218 | constexpr uint32_t max_audio_bitrate = 64000; |
| 219 | const std::string video_track_id = "video_track"; |
| 220 | constexpr uint32_t min_video_bitrate = 30000; |
| 221 | constexpr uint32_t max_video_bitrate = 300000; |
| 222 | constexpr uint32_t min_other_bitrate = 3000; |
| 223 | constexpr uint32_t max_other_bitrate = 30000; |
| 224 | constexpr uint32_t available_bitrate = 200000; |
| 225 | constexpr uint32_t video_bitrate = |
| 226 | available_bitrate - max_audio_bitrate - max_other_bitrate; |
| 227 | |
| 228 | std::vector<uint32_t> allocations = RunAudioPriorityAllocation( |
| 229 | sufficient_audio_bitrate, audio_track_id, min_audio_bitrate, |
| 230 | max_audio_bitrate, video_track_id, min_video_bitrate, max_video_bitrate, |
| 231 | min_other_bitrate, max_other_bitrate, available_bitrate); |
| 232 | EXPECT_EQ(max_audio_bitrate, allocations[0]); |
| 233 | EXPECT_EQ(video_bitrate, allocations[1]); |
| 234 | EXPECT_EQ(max_other_bitrate, allocations[2]); |
| 235 | } |
| 236 | |
| 237 | } // namespace rtc |