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 |
| 12 #include "webrtc/base/bitrateallocationstrategy.h" |
| 13 #include <utility> |
| 14 #include <algorithm> |
| 15 |
| 16 namespace rtc { |
| 17 |
| 18 BitrateAllocationStrategy::TrackAllocations |
| 19 BitrateAllocationStrategy::SetAllBitratesToMinimum( |
| 20 const TrackConfigs& track_configs) { |
| 21 TrackAllocations track_allocations; |
| 22 for (const auto& track_config : track_configs) { |
| 23 track_allocations[track_config.second.track_id] = |
| 24 track_config.second.min_bitrate_bps; |
| 25 } |
| 26 return track_allocations; |
| 27 } |
| 28 |
| 29 BitrateAllocationStrategy::TrackAllocations |
| 30 BitrateAllocationStrategy::DistributeBitratesEvenly( |
| 31 const TrackConfigs& track_configs, |
| 32 uint32_t available_bitrate) { |
| 33 TrackAllocations track_allocations = |
| 34 SetAllBitratesToMinimum(track_configs); |
| 35 uint32_t sum_min_bitrates = 0; |
| 36 uint32_t sum_max_bitrates = 0; |
| 37 for (const auto& track_config : track_configs) { |
| 38 sum_min_bitrates += track_config.second.min_bitrate_bps; |
| 39 sum_max_bitrates += track_config.second.max_bitrate_bps; |
| 40 } |
| 41 if (sum_min_bitrates>=available_bitrate) { |
| 42 return track_allocations; |
| 43 } else if (available_bitrate >= sum_max_bitrates) { |
| 44 for (const auto& track_config : track_configs) { |
| 45 track_allocations[track_config.second.track_id] = |
| 46 track_config.second.max_bitrate_bps; |
| 47 } |
| 48 return track_allocations; |
| 49 } else { |
| 50 std::multimap<uint32_t, TrackConfig> max_bitrate_sorted_configs; |
| 51 for (const auto& track_config : track_configs) { |
| 52 max_bitrate_sorted_configs.insert(std::pair<uint32_t, |
| 53 const TrackConfig&>( |
| 54 track_config.second.max_bitrate_bps, track_config.second)); |
| 55 } |
| 56 uint32_t total_available_increase = (available_bitrate - sum_min_bitrates); |
| 57 int processed_configs = 0; |
| 58 for (const auto& track_config_pair : max_bitrate_sorted_configs) { |
| 59 TrackConfig track_config = track_config_pair.second; |
| 60 uint32_t available_increase = total_available_increase/ |
| 61 (static_cast<uint32_t>(track_configs.size() - processed_configs)); |
| 62 uint32_t consumed_increase = std::min(track_config.max_bitrate_bps, |
| 63 available_increase); |
| 64 track_allocations[track_config.track_id] += 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 auto audio_track_config_pair = track_configs.find(audio_track_id_); |
| 83 const TrackConfig& audio_track_config = audio_track_config_pair->second; |
| 84 if (audio_track_config_pair == track_configs.end()) { |
| 85 return DistributeBitratesEvenly(track_configs, available_bitrate); |
| 86 } else { |
| 87 RTC_DCHECK(sufficient_audio_bitrate_>= |
| 88 audio_track_config.min_bitrate_bps); |
| 89 uint32_t sum_min_bitrates = 0; |
| 90 for (const auto& track_config : track_configs) { |
| 91 sum_min_bitrates += track_config.second.min_bitrate_bps; |
| 92 } |
| 93 if (available_bitrate <= sum_min_bitrates) { |
| 94 return SetAllBitratesToMinimum(track_configs); |
| 95 } else { |
| 96 if (available_bitrate <= sum_min_bitrates + sufficient_audio_bitrate_ - |
| 97 audio_track_config.min_bitrate_bps) { |
| 98 TrackAllocations track_allocations = |
| 99 SetAllBitratesToMinimum(track_configs); |
| 100 track_allocations[audio_track_id_] += |
| 101 available_bitrate - sum_min_bitrates; |
| 102 return track_allocations; |
| 103 } else { |
| 104 TrackConfig sufficient_audio_config(audio_track_config); |
| 105 sufficient_audio_config.min_bitrate_bps = sufficient_audio_bitrate_; |
| 106 TrackConfigs sufficient_track_configs(track_configs); |
| 107 sufficient_track_configs[audio_track_id_] = sufficient_audio_config; |
| 108 return DistributeBitratesEvenly(sufficient_track_configs, |
| 109 available_bitrate); |
| 110 } |
| 111 } |
| 112 } |
| 113 } |
| 114 |
| 115 } // namespace rtc |
OLD | NEW |