Erik Språng | 566124a | 2018-04-23 10:32:22 | [diff] [blame] | 1 | /* |
| 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 Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 16 | |
Erik Språng | 566124a | 2018-04-23 10:32:22 | [diff] [blame] | 17 | #include <limits> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 21 | #include "absl/types/optional.h" |
Erik Språng | f93eda1 | 2019-01-16 16:10:57 | [diff] [blame] | 22 | #include "api/video/video_codec_constants.h" |
Mirko Bonadei | 66e7679 | 2019-04-02 09:33:59 | [diff] [blame] | 23 | #include "rtc_base/system/rtc_export.h" |
Erik Språng | 566124a | 2018-04-23 10:32:22 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
Erik Språng | 566124a | 2018-04-23 10:32:22 | [diff] [blame] | 27 | // Class that describes how video bitrate, in bps, is allocated across temporal |
| 28 | // and spatial layers. Not that bitrates are NOT cumulative. Depending on if |
| 29 | // layers are dependent or not, it is up to the user to aggregate. |
| 30 | // For each index, the bitrate can also both set and unset. This is used with a |
| 31 | // set bps = 0 to signal an explicit "turn off" signal. |
Mirko Bonadei | 66e7679 | 2019-04-02 09:33:59 | [diff] [blame] | 32 | class RTC_EXPORT VideoBitrateAllocation { |
Erik Språng | 566124a | 2018-04-23 10:32:22 | [diff] [blame] | 33 | public: |
| 34 | static constexpr uint32_t kMaxBitrateBps = |
| 35 | std::numeric_limits<uint32_t>::max(); |
| 36 | VideoBitrateAllocation(); |
| 37 | |
| 38 | bool SetBitrate(size_t spatial_index, |
| 39 | size_t temporal_index, |
| 40 | uint32_t bitrate_bps); |
| 41 | |
| 42 | bool HasBitrate(size_t spatial_index, size_t temporal_index) const; |
| 43 | |
| 44 | uint32_t GetBitrate(size_t spatial_index, size_t temporal_index) const; |
| 45 | |
| 46 | // Whether the specific spatial layers has the bitrate set in any of its |
| 47 | // temporal layers. |
| 48 | bool IsSpatialLayerUsed(size_t spatial_index) const; |
| 49 | |
| 50 | // Get the sum of all the temporal layer for a specific spatial layer. |
| 51 | uint32_t GetSpatialLayerSum(size_t spatial_index) const; |
| 52 | |
Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 53 | // Sum of bitrates of temporal layers, from layer 0 to `temporal_index` |
| 54 | // inclusive, of specified spatial layer `spatial_index`. Bitrates of lower |
Erik Språng | 566124a | 2018-04-23 10:32:22 | [diff] [blame] | 55 | // spatial layers are not included. |
| 56 | uint32_t GetTemporalLayerSum(size_t spatial_index, |
| 57 | size_t temporal_index) const; |
| 58 | |
| 59 | // Returns a vector of the temporal layer bitrates for the specific spatial |
| 60 | // layer. Length of the returned vector is cropped to the highest temporal |
| 61 | // layer with a defined bitrate. |
| 62 | std::vector<uint32_t> GetTemporalLayerAllocation(size_t spatial_index) const; |
| 63 | |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 64 | // Returns one VideoBitrateAllocation for each spatial layer. This is used to |
| 65 | // configure simulcast streams. Note that the length of the returned vector is |
| 66 | // always kMaxSpatialLayers, the optional is unset for unused layers. |
| 67 | std::vector<absl::optional<VideoBitrateAllocation>> GetSimulcastAllocations() |
| 68 | const; |
| 69 | |
Erik Språng | 566124a | 2018-04-23 10:32:22 | [diff] [blame] | 70 | uint32_t get_sum_bps() const { return sum_; } // Sum of all bitrates. |
| 71 | uint32_t get_sum_kbps() const { |
| 72 | // Round down to not exceed the allocated bitrate. |
| 73 | return sum_ / 1000; |
| 74 | } |
| 75 | |
| 76 | bool operator==(const VideoBitrateAllocation& other) const; |
| 77 | inline bool operator!=(const VideoBitrateAllocation& other) const { |
| 78 | return !(*this == other); |
| 79 | } |
| 80 | |
| 81 | std::string ToString() const; |
| 82 | |
Ilya Nikolaevskiy | 5963c7c | 2019-10-09 16:06:58 | [diff] [blame] | 83 | // Indicates if the allocation has some layers/streams disabled due to |
| 84 | // low available bandwidth. |
| 85 | void set_bw_limited(bool limited) { is_bw_limited_ = limited; } |
| 86 | bool is_bw_limited() const { return is_bw_limited_; } |
| 87 | |
Erik Språng | 566124a | 2018-04-23 10:32:22 | [diff] [blame] | 88 | private: |
| 89 | uint32_t sum_; |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 90 | absl::optional<uint32_t> bitrates_[kMaxSpatialLayers][kMaxTemporalStreams]; |
Ilya Nikolaevskiy | 5963c7c | 2019-10-09 16:06:58 | [diff] [blame] | 91 | bool is_bw_limited_; |
Erik Språng | 566124a | 2018-04-23 10:32:22 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace webrtc |
| 95 | |
| 96 | #endif // API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_ |