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