video_bitrate_allocation.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_
  11. #define API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <limits>
  15. #include <string>
  16. #include <vector>
  17. #include "absl/types/optional.h"
  18. #include "api/video/video_codec_constants.h"
  19. #include "rtc_base/system/rtc_export.h"
  20. namespace webrtc {
  21. // Class that describes how video bitrate, in bps, is allocated across temporal
  22. // and spatial layers. Not that bitrates are NOT cumulative. Depending on if
  23. // layers are dependent or not, it is up to the user to aggregate.
  24. // For each index, the bitrate can also both set and unset. This is used with a
  25. // set bps = 0 to signal an explicit "turn off" signal.
  26. class RTC_EXPORT VideoBitrateAllocation {
  27. public:
  28. static constexpr uint32_t kMaxBitrateBps =
  29. std::numeric_limits<uint32_t>::max();
  30. VideoBitrateAllocation();
  31. bool SetBitrate(size_t spatial_index,
  32. size_t temporal_index,
  33. uint32_t bitrate_bps);
  34. bool HasBitrate(size_t spatial_index, size_t temporal_index) const;
  35. uint32_t GetBitrate(size_t spatial_index, size_t temporal_index) const;
  36. // Whether the specific spatial layers has the bitrate set in any of its
  37. // temporal layers.
  38. bool IsSpatialLayerUsed(size_t spatial_index) const;
  39. // Get the sum of all the temporal layer for a specific spatial layer.
  40. uint32_t GetSpatialLayerSum(size_t spatial_index) const;
  41. // Sum of bitrates of temporal layers, from layer 0 to |temporal_index|
  42. // inclusive, of specified spatial layer |spatial_index|. Bitrates of lower
  43. // spatial layers are not included.
  44. uint32_t GetTemporalLayerSum(size_t spatial_index,
  45. size_t temporal_index) const;
  46. // Returns a vector of the temporal layer bitrates for the specific spatial
  47. // layer. Length of the returned vector is cropped to the highest temporal
  48. // layer with a defined bitrate.
  49. std::vector<uint32_t> GetTemporalLayerAllocation(size_t spatial_index) const;
  50. // Returns one VideoBitrateAllocation for each spatial layer. This is used to
  51. // configure simulcast streams. Note that the length of the returned vector is
  52. // always kMaxSpatialLayers, the optional is unset for unused layers.
  53. std::vector<absl::optional<VideoBitrateAllocation>> GetSimulcastAllocations()
  54. const;
  55. uint32_t get_sum_bps() const { return sum_; } // Sum of all bitrates.
  56. uint32_t get_sum_kbps() const {
  57. // Round down to not exceed the allocated bitrate.
  58. return sum_ / 1000;
  59. }
  60. bool operator==(const VideoBitrateAllocation& other) const;
  61. inline bool operator!=(const VideoBitrateAllocation& other) const {
  62. return !(*this == other);
  63. }
  64. std::string ToString() const;
  65. // Indicates if the allocation has some layers/streams disabled due to
  66. // low available bandwidth.
  67. void set_bw_limited(bool limited) { is_bw_limited_ = limited; }
  68. bool is_bw_limited() const { return is_bw_limited_; }
  69. private:
  70. uint32_t sum_;
  71. absl::optional<uint32_t> bitrates_[kMaxSpatialLayers][kMaxTemporalStreams];
  72. bool is_bw_limited_;
  73. };
  74. } // namespace webrtc
  75. #endif // API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_