Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [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 <algorithm> |
| 13 | #include <utility> |
| 14 | |
| 15 | namespace rtc { |
| 16 | |
| 17 | std::vector<uint32_t> BitrateAllocationStrategy::SetAllBitratesToMinimum( |
| 18 | const ArrayView<const TrackConfig*> track_configs) { |
| 19 | std::vector<uint32_t> track_allocations; |
| 20 | for (const auto* track_config : track_configs) { |
| 21 | track_allocations.push_back(track_config->min_bitrate_bps); |
| 22 | } |
| 23 | return track_allocations; |
| 24 | } |
| 25 | |
| 26 | std::vector<uint32_t> BitrateAllocationStrategy::DistributeBitratesEvenly( |
| 27 | const ArrayView<const TrackConfig*> track_configs, |
| 28 | uint32_t available_bitrate) { |
| 29 | std::vector<uint32_t> track_allocations = |
| 30 | SetAllBitratesToMinimum(track_configs); |
| 31 | uint32_t sum_min_bitrates = 0; |
| 32 | uint32_t sum_max_bitrates = 0; |
| 33 | for (const auto* track_config : track_configs) { |
| 34 | sum_min_bitrates += track_config->min_bitrate_bps; |
| 35 | sum_max_bitrates += track_config->max_bitrate_bps; |
| 36 | } |
| 37 | if (sum_min_bitrates >= available_bitrate) { |
| 38 | return track_allocations; |
| 39 | } else if (available_bitrate >= sum_max_bitrates) { |
| 40 | auto track_allocations_it = track_allocations.begin(); |
| 41 | for (const auto* track_config : track_configs) { |
| 42 | *track_allocations_it++ = track_config->max_bitrate_bps; |
| 43 | } |
| 44 | return track_allocations; |
| 45 | } else { |
| 46 | // If sum_min_bitrates < available_bitrate < sum_max_bitrates allocate |
| 47 | // bitrates evenly up to max_bitrate_bps starting from the track with the |
| 48 | // lowest max_bitrate_bps. Remainder of available bitrate split evenly among |
| 49 | // remaining tracks. |
| 50 | std::multimap<uint32_t, size_t> max_bitrate_sorted_configs; |
| 51 | for (const TrackConfig** track_configs_it = track_configs.begin(); |
| 52 | track_configs_it != track_configs.end(); ++track_configs_it) { |
| 53 | max_bitrate_sorted_configs.insert( |
| 54 | std::make_pair((*track_configs_it)->max_bitrate_bps, |
| 55 | track_configs_it - track_configs.begin())); |
| 56 | } |
| 57 | uint32_t total_available_increase = available_bitrate - sum_min_bitrates; |
| 58 | int processed_configs = 0; |
| 59 | for (const auto& track_config_pair : max_bitrate_sorted_configs) { |
| 60 | uint32_t available_increase = |
| 61 | total_available_increase / |
| 62 | (static_cast<uint32_t>(track_configs.size() - processed_configs)); |
| 63 | uint32_t consumed_increase = |
| 64 | std::min(track_configs[track_config_pair.second]->max_bitrate_bps - |
| 65 | track_configs[track_config_pair.second]->min_bitrate_bps, |
| 66 | available_increase); |
| 67 | track_allocations[track_config_pair.second] += consumed_increase; |
| 68 | total_available_increase -= consumed_increase; |
| 69 | ++processed_configs; |
| 70 | } |
| 71 | return track_allocations; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | AudioPriorityBitrateAllocationStrategy::AudioPriorityBitrateAllocationStrategy( |
| 76 | std::string audio_track_id, |
| 77 | uint32_t sufficient_audio_bitrate) |
| 78 | : audio_track_id_(audio_track_id), |
| 79 | sufficient_audio_bitrate_(sufficient_audio_bitrate) {} |
| 80 | |
| 81 | std::vector<uint32_t> AudioPriorityBitrateAllocationStrategy::AllocateBitrates( |
| 82 | uint32_t available_bitrate, |
| 83 | const ArrayView<const TrackConfig*> track_configs) { |
| 84 | const TrackConfig* audio_track_config = NULL; |
| 85 | size_t audio_config_index = 0; |
| 86 | uint32_t sum_min_bitrates = 0; |
| 87 | |
| 88 | for (const auto*& track_config : track_configs) { |
| 89 | sum_min_bitrates += track_config->min_bitrate_bps; |
| 90 | if (track_config->track_id == audio_track_id_) { |
| 91 | audio_track_config = track_config; |
| 92 | audio_config_index = &track_config - &track_configs[0]; |
| 93 | } |
| 94 | } |
| 95 | if (audio_track_config == nullptr) { |
| 96 | return DistributeBitratesEvenly(track_configs, available_bitrate); |
| 97 | } |
| 98 | auto safe_sufficient_audio_bitrate = std::min( |
| 99 | std::max(audio_track_config->min_bitrate_bps, sufficient_audio_bitrate_), |
| 100 | audio_track_config->max_bitrate_bps); |
| 101 | if (available_bitrate <= sum_min_bitrates) { |
| 102 | return SetAllBitratesToMinimum(track_configs); |
| 103 | } else { |
| 104 | if (available_bitrate <= sum_min_bitrates + safe_sufficient_audio_bitrate - |
| 105 | audio_track_config->min_bitrate_bps) { |
| 106 | std::vector<uint32_t> track_allocations = |
| 107 | SetAllBitratesToMinimum(track_configs); |
| 108 | track_allocations[audio_config_index] += |
| 109 | available_bitrate - sum_min_bitrates; |
| 110 | return track_allocations; |
| 111 | } else { |
| 112 | // Setting audio track minimum to safe_sufficient_audio_bitrate will |
| 113 | // allow using DistributeBitratesEvenly to allocate at least sufficient |
| 114 | // bitrate for audio and the rest evenly. |
| 115 | TrackConfig sufficient_track_config(*track_configs[audio_config_index]); |
| 116 | sufficient_track_config.min_bitrate_bps = safe_sufficient_audio_bitrate; |
| 117 | track_configs[audio_config_index] = &sufficient_track_config; |
| 118 | std::vector<uint32_t> track_allocations = |
| 119 | DistributeBitratesEvenly(track_configs, available_bitrate); |
| 120 | return track_allocations; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } // namespace rtc |