blob: d335a553c11030ee0f03e1e7c26cfcc5b0bbe174 [file] [log] [blame]
Erik Språng566124a2018-04-23 12:32:22 +02001/*
2 * Copyright (c) 2018 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 API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_
12#define API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_
13
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Erik Språng566124a2018-04-23 12:32:22 +020016#include <limits>
17#include <string>
18#include <vector>
19
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020020#include "absl/types/optional.h"
Erik Språngf93eda12019-01-16 17:10:57 +010021#include "api/video/video_codec_constants.h"
Mirko Bonadei66e76792019-04-02 11:33:59 +020022#include "rtc_base/system/rtc_export.h"
Erik Språng566124a2018-04-23 12:32:22 +020023
24namespace webrtc {
25
Erik Språng566124a2018-04-23 12:32:22 +020026// Class that describes how video bitrate, in bps, is allocated across temporal
27// and spatial layers. Not that bitrates are NOT cumulative. Depending on if
28// layers are dependent or not, it is up to the user to aggregate.
29// For each index, the bitrate can also both set and unset. This is used with a
30// set bps = 0 to signal an explicit "turn off" signal.
Mirko Bonadei66e76792019-04-02 11:33:59 +020031class RTC_EXPORT VideoBitrateAllocation {
Erik Språng566124a2018-04-23 12:32:22 +020032 public:
33 static constexpr uint32_t kMaxBitrateBps =
34 std::numeric_limits<uint32_t>::max();
35 VideoBitrateAllocation();
36
37 bool SetBitrate(size_t spatial_index,
38 size_t temporal_index,
39 uint32_t bitrate_bps);
40
41 bool HasBitrate(size_t spatial_index, size_t temporal_index) const;
42
43 uint32_t GetBitrate(size_t spatial_index, size_t temporal_index) const;
44
45 // Whether the specific spatial layers has the bitrate set in any of its
46 // temporal layers.
47 bool IsSpatialLayerUsed(size_t spatial_index) const;
48
49 // Get the sum of all the temporal layer for a specific spatial layer.
50 uint32_t GetSpatialLayerSum(size_t spatial_index) const;
51
52 // Sum of bitrates of temporal layers, from layer 0 to |temporal_index|
53 // inclusive, of specified spatial layer |spatial_index|. Bitrates of lower
54 // spatial layers are not included.
55 uint32_t GetTemporalLayerSum(size_t spatial_index,
56 size_t temporal_index) const;
57
58 // Returns a vector of the temporal layer bitrates for the specific spatial
59 // layer. Length of the returned vector is cropped to the highest temporal
60 // layer with a defined bitrate.
61 std::vector<uint32_t> GetTemporalLayerAllocation(size_t spatial_index) const;
62
Stefan Holmerf7044682018-07-17 10:16:41 +020063 // Returns one VideoBitrateAllocation for each spatial layer. This is used to
64 // configure simulcast streams. Note that the length of the returned vector is
65 // always kMaxSpatialLayers, the optional is unset for unused layers.
66 std::vector<absl::optional<VideoBitrateAllocation>> GetSimulcastAllocations()
67 const;
68
Erik Språng566124a2018-04-23 12:32:22 +020069 uint32_t get_sum_bps() const { return sum_; } // Sum of all bitrates.
70 uint32_t get_sum_kbps() const {
71 // Round down to not exceed the allocated bitrate.
72 return sum_ / 1000;
73 }
74
75 bool operator==(const VideoBitrateAllocation& other) const;
76 inline bool operator!=(const VideoBitrateAllocation& other) const {
77 return !(*this == other);
78 }
79
80 std::string ToString() const;
81
82 private:
83 uint32_t sum_;
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020084 absl::optional<uint32_t> bitrates_[kMaxSpatialLayers][kMaxTemporalStreams];
Erik Språng566124a2018-04-23 12:32:22 +020085};
86
87} // namespace webrtc
88
89#endif // API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_