blob: fc54373b4dbb502dc967d4889dc1eded1dd5b3bf [file] [log] [blame]
Alex Narest78609d52017-10-20 10:37:47 +02001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_BITRATE_ALLOCATION_STRATEGY_H_
12#define RTC_BASE_BITRATE_ALLOCATION_STRATEGY_H_
Alex Narest78609d52017-10-20 10:37:47 +020013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
Alex Narest78609d52017-10-20 10:37:47 +020015#include <string>
16#include <vector>
Yves Gerey988cc082018-10-23 12:03:01 +020017
Alex Narest78609d52017-10-20 10:37:47 +020018#include "api/array_view.h"
Sebastian Janssond28efe52018-10-18 12:26:46 +020019#include "rtc_base/experiments/field_trial_parser.h"
20#include "rtc_base/experiments/field_trial_units.h"
Alex Narest78609d52017-10-20 10:37:47 +020021
22namespace 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.
32class 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 Janssone2754c92018-10-24 16:02:26 +020060 // These are only used by AudioPriorityBitrateAllocationStrategy. They are
61 // exposed here to they can be unit tested.
Alex Narest78609d52017-10-20 10:37:47 +020062 static std::vector<uint32_t> SetAllBitratesToMinimum(
Sebastian Janssone2754c92018-10-24 16:02:26 +020063 const std::vector<BitrateAllocationStrategy::TrackConfig>& track_configs);
Alex Narest78609d52017-10-20 10:37:47 +020064 static std::vector<uint32_t> DistributeBitratesEvenly(
Sebastian Janssone2754c92018-10-24 16:02:26 +020065 const std::vector<BitrateAllocationStrategy::TrackConfig>& track_configs,
Alex Narest78609d52017-10-20 10:37:47 +020066 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 Janssone2754c92018-10-24 16:02:26 +020080 std::vector<BitrateAllocationStrategy::TrackConfig> track_configs) = 0;
Alex Narest78609d52017-10-20 10:37:47 +020081
82 virtual ~BitrateAllocationStrategy() = default;
83};
Sebastian Janssond28efe52018-10-18 12:26:46 +020084} // namespace rtc
Alex Narest78609d52017-10-20 10:37:47 +020085
Sebastian Janssond28efe52018-10-18 12:26:46 +020086namespace webrtc {
87struct 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
98namespace rtc {
Alex Narest78609d52017-10-20 10:37:47 +020099// 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.
103class 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 Janssone2754c92018-10-24 16:02:26 +0200110 std::vector<BitrateAllocationStrategy::TrackConfig> track_configs)
111 override;
Alex Narest78609d52017-10-20 10:37:47 +0200112
113 private:
Sebastian Janssond28efe52018-10-18 12:26:46 +0200114 webrtc::AudioPriorityConfig config_;
Alex Narest78609d52017-10-20 10:37:47 +0200115 std::string audio_track_id_;
116 uint32_t sufficient_audio_bitrate_;
117};
118} // namespace rtc
119
Steve Anton10542f22019-01-11 09:11:00 -0800120#endif // RTC_BASE_BITRATE_ALLOCATION_STRATEGY_H_