Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [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 | |
| 14 | #include <limits> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "api/optional.h" |
| 19 | #include "typedefs.h" // NOLINT(build/include) |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | // TODO(sprang): Move back to common_types when include of this is removed. |
| 24 | enum : int { kMaxSimulcastStreams = 4 }; |
| 25 | enum : int { kMaxSpatialLayers = 5 }; |
| 26 | enum : int { kMaxTemporalStreams = 4 }; |
| 27 | |
| 28 | // Class that describes how video bitrate, in bps, is allocated across temporal |
| 29 | // and spatial layers. Not that bitrates are NOT cumulative. Depending on if |
| 30 | // layers are dependent or not, it is up to the user to aggregate. |
| 31 | // For each index, the bitrate can also both set and unset. This is used with a |
| 32 | // set bps = 0 to signal an explicit "turn off" signal. |
| 33 | class VideoBitrateAllocation { |
| 34 | public: |
| 35 | static constexpr uint32_t kMaxBitrateBps = |
| 36 | std::numeric_limits<uint32_t>::max(); |
| 37 | VideoBitrateAllocation(); |
| 38 | |
| 39 | bool SetBitrate(size_t spatial_index, |
| 40 | size_t temporal_index, |
| 41 | uint32_t bitrate_bps); |
| 42 | |
| 43 | bool HasBitrate(size_t spatial_index, size_t temporal_index) const; |
| 44 | |
| 45 | uint32_t GetBitrate(size_t spatial_index, size_t temporal_index) const; |
| 46 | |
| 47 | // Whether the specific spatial layers has the bitrate set in any of its |
| 48 | // temporal layers. |
| 49 | bool IsSpatialLayerUsed(size_t spatial_index) const; |
| 50 | |
| 51 | // Get the sum of all the temporal layer for a specific spatial layer. |
| 52 | uint32_t GetSpatialLayerSum(size_t spatial_index) const; |
| 53 | |
| 54 | // Sum of bitrates of temporal layers, from layer 0 to |temporal_index| |
| 55 | // inclusive, of specified spatial layer |spatial_index|. Bitrates of lower |
| 56 | // spatial layers are not included. |
| 57 | uint32_t GetTemporalLayerSum(size_t spatial_index, |
| 58 | size_t temporal_index) const; |
| 59 | |
| 60 | // Returns a vector of the temporal layer bitrates for the specific spatial |
| 61 | // layer. Length of the returned vector is cropped to the highest temporal |
| 62 | // layer with a defined bitrate. |
| 63 | std::vector<uint32_t> GetTemporalLayerAllocation(size_t spatial_index) const; |
| 64 | |
| 65 | uint32_t get_sum_bps() const { return sum_; } // Sum of all bitrates. |
| 66 | uint32_t get_sum_kbps() const { |
| 67 | // Round down to not exceed the allocated bitrate. |
| 68 | return sum_ / 1000; |
| 69 | } |
| 70 | |
| 71 | bool operator==(const VideoBitrateAllocation& other) const; |
| 72 | inline bool operator!=(const VideoBitrateAllocation& other) const { |
| 73 | return !(*this == other); |
| 74 | } |
| 75 | |
| 76 | std::string ToString() const; |
| 77 | |
| 78 | private: |
| 79 | uint32_t sum_; |
| 80 | rtc::Optional<uint32_t> bitrates_[kMaxSpatialLayers][kMaxTemporalStreams]; |
| 81 | }; |
| 82 | |
| 83 | } // namespace webrtc |
| 84 | |
| 85 | #endif // API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_ |