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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 11 | #ifndef RTC_BASE_BITRATE_ALLOCATION_STRATEGY_H_ |
| 12 | #define RTC_BASE_BITRATE_ALLOCATION_STRATEGY_H_ |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 17 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 19 | #include "rtc_base/experiments/field_trial_parser.h" |
| 20 | #include "rtc_base/experiments/field_trial_units.h" |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | // Pluggable strategy allows configuration of bitrate allocation per media |
| 25 | // track. |
| 26 | // |
| 27 | // The strategy should provide allocation for every track passed with |
| 28 | // track_configs in AllocateBitrates. The allocations are constrained by |
| 29 | // max_bitrate_bps, min_bitrate_bps defining the track supported range and |
| 30 | // enforce_min_bitrate indicating if the track my be paused by allocating 0 |
| 31 | // bitrate. |
| 32 | class BitrateAllocationStrategy { |
| 33 | public: |
| 34 | struct TrackConfig { |
| 35 | TrackConfig(uint32_t min_bitrate_bps, |
| 36 | uint32_t max_bitrate_bps, |
| 37 | bool enforce_min_bitrate, |
| 38 | std::string track_id) |
| 39 | : min_bitrate_bps(min_bitrate_bps), |
| 40 | max_bitrate_bps(max_bitrate_bps), |
| 41 | enforce_min_bitrate(enforce_min_bitrate), |
| 42 | track_id(track_id) {} |
| 43 | TrackConfig(const TrackConfig& track_config) = default; |
| 44 | virtual ~TrackConfig() = default; |
| 45 | TrackConfig() {} |
| 46 | |
| 47 | // Minimum bitrate supported by track. |
| 48 | uint32_t min_bitrate_bps; |
| 49 | |
| 50 | // Maximum bitrate supported by track. |
| 51 | uint32_t max_bitrate_bps; |
| 52 | |
| 53 | // True means track may not be paused by allocating 0 bitrate. |
| 54 | bool enforce_min_bitrate; |
| 55 | |
| 56 | // MediaStreamTrack ID as defined by application. May be empty. |
| 57 | std::string track_id; |
| 58 | }; |
| 59 | |
Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame] | 60 | // These are only used by AudioPriorityBitrateAllocationStrategy. They are |
| 61 | // exposed here to they can be unit tested. |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 62 | static std::vector<uint32_t> SetAllBitratesToMinimum( |
Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame] | 63 | const std::vector<BitrateAllocationStrategy::TrackConfig>& track_configs); |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 64 | static std::vector<uint32_t> DistributeBitratesEvenly( |
Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame] | 65 | const std::vector<BitrateAllocationStrategy::TrackConfig>& track_configs, |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 66 | uint32_t available_bitrate); |
| 67 | |
| 68 | // Strategy is expected to allocate all available_bitrate up to the sum of |
| 69 | // max_bitrate_bps of all tracks. If available_bitrate is less than the sum of |
| 70 | // min_bitrate_bps of all tracks, tracks having enforce_min_bitrate set to |
| 71 | // false may get 0 allocation and are suppoused to pause, tracks with |
| 72 | // enforce_min_bitrate set to true are expecting to get min_bitrate_bps. |
| 73 | // |
| 74 | // If the strategy will allocate more than available_bitrate it may cause |
| 75 | // overuse of the currently available network capacity and may cause increase |
| 76 | // in RTT and packet loss. Allocating less than available bitrate may cause |
| 77 | // available_bitrate decrease. |
| 78 | virtual std::vector<uint32_t> AllocateBitrates( |
| 79 | uint32_t available_bitrate, |
Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame] | 80 | std::vector<BitrateAllocationStrategy::TrackConfig> track_configs) = 0; |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 81 | |
| 82 | virtual ~BitrateAllocationStrategy() = default; |
| 83 | }; |
Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 84 | } // namespace rtc |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 85 | |
Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 86 | namespace webrtc { |
| 87 | struct AudioPriorityConfig { |
| 88 | FieldTrialOptional<DataRate> min_rate; |
| 89 | FieldTrialOptional<DataRate> max_rate; |
| 90 | FieldTrialOptional<DataRate> target_rate; |
| 91 | AudioPriorityConfig(); |
| 92 | AudioPriorityConfig(const AudioPriorityConfig&); |
| 93 | AudioPriorityConfig& operator=(const AudioPriorityConfig&) = default; |
| 94 | ~AudioPriorityConfig(); |
| 95 | }; |
| 96 | } // namespace webrtc |
| 97 | |
| 98 | namespace rtc { |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 99 | // Simple allocation strategy giving priority to audio until |
| 100 | // sufficient_audio_bitrate is reached. Bitrate is distributed evenly between |
| 101 | // the tracks after sufficient_audio_bitrate is reached. This implementation |
| 102 | // does not pause tracks even if enforce_min_bitrate is false. |
| 103 | class AudioPriorityBitrateAllocationStrategy |
| 104 | : public BitrateAllocationStrategy { |
| 105 | public: |
| 106 | AudioPriorityBitrateAllocationStrategy(std::string audio_track_id, |
| 107 | uint32_t sufficient_audio_bitrate); |
| 108 | std::vector<uint32_t> AllocateBitrates( |
| 109 | uint32_t available_bitrate, |
Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame] | 110 | std::vector<BitrateAllocationStrategy::TrackConfig> track_configs) |
| 111 | override; |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 112 | |
| 113 | private: |
Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 114 | webrtc::AudioPriorityConfig config_; |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 115 | std::string audio_track_id_; |
| 116 | uint32_t sufficient_audio_bitrate_; |
| 117 | }; |
| 118 | } // namespace rtc |
| 119 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 120 | #endif // RTC_BASE_BITRATE_ALLOCATION_STRATEGY_H_ |