OLD | NEW |
---|---|
(Empty) | |
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 "webrtc/rtc_base/bitrateallocationstrategy.h" | |
12 #include <algorithm> | |
13 #include <utility> | |
14 | |
15 namespace rtc { | |
16 | |
17 BitrateAllocationStrategy::TrackAllocations | |
18 BitrateAllocationStrategy::SetAllBitratesToMinimum( | |
19 const TrackConfigs& track_configs) { | |
20 TrackAllocations track_allocations; | |
21 for (const auto& track_config : track_configs) { | |
22 track_allocations.push_back(track_config->min_bitrate_bps); | |
23 } | |
24 return track_allocations; | |
25 } | |
26 | |
27 BitrateAllocationStrategy::TrackAllocations | |
28 BitrateAllocationStrategy::DistributeBitratesEvenly( | |
stefan-webrtc
2017/09/05 10:42:47
This code is coming from BitrateAllocator, right?
alexnarest
2017/09/05 13:37:46
It is not copy/paste but the logic is similar.
| |
29 const TrackConfigs& track_configs, | |
30 uint32_t available_bitrate) { | |
31 TrackAllocations track_allocations = SetAllBitratesToMinimum(track_configs); | |
32 uint32_t sum_min_bitrates = 0; | |
33 uint32_t sum_max_bitrates = 0; | |
34 for (const auto& track_config : track_configs) { | |
35 sum_min_bitrates += track_config->min_bitrate_bps; | |
36 sum_max_bitrates += track_config->max_bitrate_bps; | |
37 } | |
38 if (sum_min_bitrates >= available_bitrate) { | |
39 return track_allocations; | |
40 } else if (available_bitrate >= sum_max_bitrates) { | |
41 auto track_allocations_it = track_allocations.begin(); | |
42 for (const auto& track_config : track_configs) { | |
43 *track_allocations_it++ = track_config->max_bitrate_bps; | |
44 } | |
45 return track_allocations; | |
46 } else { | |
47 std::multimap<uint32_t, size_t> max_bitrate_sorted_configs; | |
48 for (auto track_configs_it = track_configs.begin(); | |
49 track_configs_it != track_configs.end(); track_configs_it++) { | |
50 max_bitrate_sorted_configs.insert(std::pair<uint32_t, size_t>( | |
51 (*track_configs_it)->max_bitrate_bps, | |
52 track_configs_it - track_configs.begin())); | |
53 } | |
54 uint32_t total_available_increase = (available_bitrate - sum_min_bitrates); | |
55 int processed_configs = 0; | |
56 for (const auto& track_config_pair : max_bitrate_sorted_configs) { | |
57 uint32_t available_increase = | |
58 total_available_increase / | |
59 (static_cast<uint32_t>(track_configs.size() - processed_configs)); | |
60 uint32_t consumed_increase = | |
61 std::min(track_configs[track_config_pair.second]->max_bitrate_bps - | |
62 track_configs[track_config_pair.second]->min_bitrate_bps, | |
63 available_increase); | |
64 track_allocations[track_config_pair.second] += consumed_increase; | |
65 total_available_increase -= consumed_increase; | |
66 ++processed_configs; | |
67 } | |
68 return track_allocations; | |
69 } | |
70 } | |
71 | |
72 AudioPriorityBitrateAllocationStrategy::AudioPriorityBitrateAllocationStrategy( | |
73 std::string audio_track_id, | |
74 uint32_t sufficient_audio_bitrate) | |
75 : audio_track_id_(audio_track_id), | |
76 sufficient_audio_bitrate_(sufficient_audio_bitrate) {} | |
77 | |
78 BitrateAllocationStrategy::TrackAllocations | |
79 AudioPriorityBitrateAllocationStrategy::AllocateBitrates( | |
80 uint32_t available_bitrate, | |
81 const TrackConfigs& track_configs) { | |
82 TrackConfig* audio_track_config = NULL; | |
83 size_t audio_config_index = -1; | |
84 uint32_t sum_min_bitrates = 0; | |
85 for (const auto& track_config : track_configs) { | |
86 sum_min_bitrates += track_config->min_bitrate_bps; | |
87 if (track_config->track_id == audio_track_id_) { | |
88 audio_track_config = track_config.get(); | |
89 audio_config_index = &track_config - &track_configs[0]; | |
90 } | |
91 } | |
92 if (audio_track_config == NULL) { | |
93 return DistributeBitratesEvenly(track_configs, available_bitrate); | |
94 } | |
95 RTC_DCHECK(sufficient_audio_bitrate_ >= audio_track_config->min_bitrate_bps && | |
96 sufficient_audio_bitrate_ <= audio_track_config->max_bitrate_bps); | |
97 if (available_bitrate <= sum_min_bitrates) { | |
98 return SetAllBitratesToMinimum(track_configs); | |
99 } else { | |
100 if (available_bitrate <= sum_min_bitrates + sufficient_audio_bitrate_ - | |
101 audio_track_config->min_bitrate_bps) { | |
102 TrackAllocations track_allocations = | |
103 SetAllBitratesToMinimum(track_configs); | |
104 track_allocations[audio_config_index] += | |
105 available_bitrate - sum_min_bitrates; | |
106 return track_allocations; | |
107 } else { | |
108 uint32_t config_min_bitrate_bps = | |
109 track_configs[audio_config_index]->min_bitrate_bps; | |
110 track_configs[audio_config_index]->min_bitrate_bps = | |
111 sufficient_audio_bitrate_; | |
112 TrackAllocations track_allocations = | |
113 DistributeBitratesEvenly(track_configs, available_bitrate); | |
114 track_configs[audio_config_index]->min_bitrate_bps = | |
115 config_min_bitrate_bps; | |
116 return track_allocations; | |
117 } | |
118 } | |
119 } | |
120 | |
121 } // namespace rtc | |
OLD | NEW |