blob: e68ea746985ee0e836cb3b64883ab9ce60176a59 [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
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
60 static std::vector<uint32_t> SetAllBitratesToMinimum(
61 const ArrayView<const TrackConfig*> track_configs);
62 static std::vector<uint32_t> DistributeBitratesEvenly(
63 const ArrayView<const TrackConfig*> track_configs,
64 uint32_t available_bitrate);
65
66 // Strategy is expected to allocate all available_bitrate up to the sum of
67 // max_bitrate_bps of all tracks. If available_bitrate is less than the sum of
68 // min_bitrate_bps of all tracks, tracks having enforce_min_bitrate set to
69 // false may get 0 allocation and are suppoused to pause, tracks with
70 // enforce_min_bitrate set to true are expecting to get min_bitrate_bps.
71 //
72 // If the strategy will allocate more than available_bitrate it may cause
73 // overuse of the currently available network capacity and may cause increase
74 // in RTT and packet loss. Allocating less than available bitrate may cause
75 // available_bitrate decrease.
76 virtual std::vector<uint32_t> AllocateBitrates(
77 uint32_t available_bitrate,
78 const ArrayView<const TrackConfig*> track_configs) = 0;
79
80 virtual ~BitrateAllocationStrategy() = default;
81};
Sebastian Janssond28efe52018-10-18 12:26:46 +020082} // namespace rtc
Alex Narest78609d52017-10-20 10:37:47 +020083
Sebastian Janssond28efe52018-10-18 12:26:46 +020084namespace webrtc {
85struct AudioPriorityConfig {
86 FieldTrialOptional<DataRate> min_rate;
87 FieldTrialOptional<DataRate> max_rate;
88 FieldTrialOptional<DataRate> target_rate;
89 AudioPriorityConfig();
90 AudioPriorityConfig(const AudioPriorityConfig&);
91 AudioPriorityConfig& operator=(const AudioPriorityConfig&) = default;
92 ~AudioPriorityConfig();
93};
94} // namespace webrtc
95
96namespace rtc {
Alex Narest78609d52017-10-20 10:37:47 +020097// Simple allocation strategy giving priority to audio until
98// sufficient_audio_bitrate is reached. Bitrate is distributed evenly between
99// the tracks after sufficient_audio_bitrate is reached. This implementation
100// does not pause tracks even if enforce_min_bitrate is false.
101class AudioPriorityBitrateAllocationStrategy
102 : public BitrateAllocationStrategy {
103 public:
104 AudioPriorityBitrateAllocationStrategy(std::string audio_track_id,
105 uint32_t sufficient_audio_bitrate);
106 std::vector<uint32_t> AllocateBitrates(
107 uint32_t available_bitrate,
108 const ArrayView<const TrackConfig*> track_configs) override;
109
110 private:
Sebastian Janssond28efe52018-10-18 12:26:46 +0200111 webrtc::AudioPriorityConfig config_;
Alex Narest78609d52017-10-20 10:37:47 +0200112 std::string audio_track_id_;
113 uint32_t sufficient_audio_bitrate_;
114};
115} // namespace rtc
116
117#endif // RTC_BASE_BITRATEALLOCATIONSTRATEGY_H_