blob: e1e47ba1631df81163f1e82fbe89e3a48e2eeead [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
11#ifndef RTC_BASE_BITRATEALLOCATIONSTRATEGY_H_
12#define RTC_BASE_BITRATEALLOCATIONSTRATEGY_H_
13
Alex Narest78609d52017-10-20 10:37:47 +020014#include <string>
15#include <vector>
16#include "api/array_view.h"
Sebastian Janssond28efe52018-10-18 12:26:46 +020017#include "rtc_base/experiments/field_trial_parser.h"
18#include "rtc_base/experiments/field_trial_units.h"
Alex Narest78609d52017-10-20 10:37:47 +020019
20namespace rtc {
21
22// Pluggable strategy allows configuration of bitrate allocation per media
23// track.
24//
25// The strategy should provide allocation for every track passed with
26// track_configs in AllocateBitrates. The allocations are constrained by
27// max_bitrate_bps, min_bitrate_bps defining the track supported range and
28// enforce_min_bitrate indicating if the track my be paused by allocating 0
29// bitrate.
30class BitrateAllocationStrategy {
31 public:
32 struct TrackConfig {
33 TrackConfig(uint32_t min_bitrate_bps,
34 uint32_t max_bitrate_bps,
35 bool enforce_min_bitrate,
36 std::string track_id)
37 : min_bitrate_bps(min_bitrate_bps),
38 max_bitrate_bps(max_bitrate_bps),
39 enforce_min_bitrate(enforce_min_bitrate),
40 track_id(track_id) {}
41 TrackConfig(const TrackConfig& track_config) = default;
42 virtual ~TrackConfig() = default;
43 TrackConfig() {}
44
45 // Minimum bitrate supported by track.
46 uint32_t min_bitrate_bps;
47
48 // Maximum bitrate supported by track.
49 uint32_t max_bitrate_bps;
50
51 // True means track may not be paused by allocating 0 bitrate.
52 bool enforce_min_bitrate;
53
54 // MediaStreamTrack ID as defined by application. May be empty.
55 std::string track_id;
56 };
57
58 static std::vector<uint32_t> SetAllBitratesToMinimum(
59 const ArrayView<const TrackConfig*> track_configs);
60 static std::vector<uint32_t> DistributeBitratesEvenly(
61 const ArrayView<const TrackConfig*> track_configs,
62 uint32_t available_bitrate);
63
64 // Strategy is expected to allocate all available_bitrate up to the sum of
65 // max_bitrate_bps of all tracks. If available_bitrate is less than the sum of
66 // min_bitrate_bps of all tracks, tracks having enforce_min_bitrate set to
67 // false may get 0 allocation and are suppoused to pause, tracks with
68 // enforce_min_bitrate set to true are expecting to get min_bitrate_bps.
69 //
70 // If the strategy will allocate more than available_bitrate it may cause
71 // overuse of the currently available network capacity and may cause increase
72 // in RTT and packet loss. Allocating less than available bitrate may cause
73 // available_bitrate decrease.
74 virtual std::vector<uint32_t> AllocateBitrates(
75 uint32_t available_bitrate,
76 const ArrayView<const TrackConfig*> track_configs) = 0;
77
78 virtual ~BitrateAllocationStrategy() = default;
79};
Sebastian Janssond28efe52018-10-18 12:26:46 +020080} // namespace rtc
Alex Narest78609d52017-10-20 10:37:47 +020081
Sebastian Janssond28efe52018-10-18 12:26:46 +020082namespace webrtc {
83struct AudioPriorityConfig {
84 FieldTrialOptional<DataRate> min_rate;
85 FieldTrialOptional<DataRate> max_rate;
86 FieldTrialOptional<DataRate> target_rate;
87 AudioPriorityConfig();
88 AudioPriorityConfig(const AudioPriorityConfig&);
89 AudioPriorityConfig& operator=(const AudioPriorityConfig&) = default;
90 ~AudioPriorityConfig();
91};
92} // namespace webrtc
93
94namespace rtc {
Alex Narest78609d52017-10-20 10:37:47 +020095// Simple allocation strategy giving priority to audio until
96// sufficient_audio_bitrate is reached. Bitrate is distributed evenly between
97// the tracks after sufficient_audio_bitrate is reached. This implementation
98// does not pause tracks even if enforce_min_bitrate is false.
99class AudioPriorityBitrateAllocationStrategy
100 : public BitrateAllocationStrategy {
101 public:
102 AudioPriorityBitrateAllocationStrategy(std::string audio_track_id,
103 uint32_t sufficient_audio_bitrate);
104 std::vector<uint32_t> AllocateBitrates(
105 uint32_t available_bitrate,
106 const ArrayView<const TrackConfig*> track_configs) override;
107
108 private:
Sebastian Janssond28efe52018-10-18 12:26:46 +0200109 webrtc::AudioPriorityConfig config_;
Alex Narest78609d52017-10-20 10:37:47 +0200110 std::string audio_track_id_;
111 uint32_t sufficient_audio_bitrate_;
112};
113} // namespace rtc
114
115#endif // RTC_BASE_BITRATEALLOCATIONSTRATEGY_H_