You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "api/video/video_bitrate_allocation.h"
  11. #include <cstdint>
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/numerics/safe_conversions.h"
  14. #include "rtc_base/strings/string_builder.h"
  15. namespace webrtc {
  16. VideoBitrateAllocation::VideoBitrateAllocation()
  17. : sum_(0), is_bw_limited_(false) {}
  18. bool VideoBitrateAllocation::SetBitrate(size_t spatial_index,
  19. size_t temporal_index,
  20. uint32_t bitrate_bps) {
  21. RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
  22. RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
  23. int64_t new_bitrate_sum_bps = sum_;
  24. absl::optional<uint32_t>& layer_bitrate =
  25. bitrates_[spatial_index][temporal_index];
  26. if (layer_bitrate) {
  27. RTC_DCHECK_LE(*layer_bitrate, sum_);
  28. new_bitrate_sum_bps -= *layer_bitrate;
  29. }
  30. new_bitrate_sum_bps += bitrate_bps;
  31. if (new_bitrate_sum_bps > kMaxBitrateBps)
  32. return false;
  33. layer_bitrate = bitrate_bps;
  34. sum_ = rtc::dchecked_cast<uint32_t>(new_bitrate_sum_bps);
  35. return true;
  36. }
  37. bool VideoBitrateAllocation::HasBitrate(size_t spatial_index,
  38. size_t temporal_index) const {
  39. RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
  40. RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
  41. return bitrates_[spatial_index][temporal_index].has_value();
  42. }
  43. uint32_t VideoBitrateAllocation::GetBitrate(size_t spatial_index,
  44. size_t temporal_index) const {
  45. RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
  46. RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
  47. return bitrates_[spatial_index][temporal_index].value_or(0);
  48. }
  49. // Whether the specific spatial layers has the bitrate set in any of its
  50. // temporal layers.
  51. bool VideoBitrateAllocation::IsSpatialLayerUsed(size_t spatial_index) const {
  52. RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
  53. for (size_t i = 0; i < kMaxTemporalStreams; ++i) {
  54. if (bitrates_[spatial_index][i].has_value())
  55. return true;
  56. }
  57. return false;
  58. }
  59. // Get the sum of all the temporal layer for a specific spatial layer.
  60. uint32_t VideoBitrateAllocation::GetSpatialLayerSum(
  61. size_t spatial_index) const {
  62. RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
  63. return GetTemporalLayerSum(spatial_index, kMaxTemporalStreams - 1);
  64. }
  65. uint32_t VideoBitrateAllocation::GetTemporalLayerSum(
  66. size_t spatial_index,
  67. size_t temporal_index) const {
  68. RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
  69. RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
  70. uint32_t sum = 0;
  71. for (size_t i = 0; i <= temporal_index; ++i) {
  72. sum += bitrates_[spatial_index][i].value_or(0);
  73. }
  74. return sum;
  75. }
  76. std::vector<uint32_t> VideoBitrateAllocation::GetTemporalLayerAllocation(
  77. size_t spatial_index) const {
  78. RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
  79. std::vector<uint32_t> temporal_rates;
  80. // Find the highest temporal layer with a defined bitrate in order to
  81. // determine the size of the temporal layer allocation.
  82. for (size_t i = kMaxTemporalStreams; i > 0; --i) {
  83. if (bitrates_[spatial_index][i - 1].has_value()) {
  84. temporal_rates.resize(i);
  85. break;
  86. }
  87. }
  88. for (size_t i = 0; i < temporal_rates.size(); ++i) {
  89. temporal_rates[i] = bitrates_[spatial_index][i].value_or(0);
  90. }
  91. return temporal_rates;
  92. }
  93. std::vector<absl::optional<VideoBitrateAllocation>>
  94. VideoBitrateAllocation::GetSimulcastAllocations() const {
  95. std::vector<absl::optional<VideoBitrateAllocation>> bitrates;
  96. for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
  97. absl::optional<VideoBitrateAllocation> layer_bitrate;
  98. if (IsSpatialLayerUsed(si)) {
  99. layer_bitrate = VideoBitrateAllocation();
  100. for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
  101. if (HasBitrate(si, tl))
  102. layer_bitrate->SetBitrate(0, tl, GetBitrate(si, tl));
  103. }
  104. }
  105. bitrates.push_back(layer_bitrate);
  106. }
  107. return bitrates;
  108. }
  109. bool VideoBitrateAllocation::operator==(
  110. const VideoBitrateAllocation& other) const {
  111. for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
  112. for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
  113. if (bitrates_[si][ti] != other.bitrates_[si][ti])
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. std::string VideoBitrateAllocation::ToString() const {
  120. if (sum_ == 0)
  121. return "VideoBitrateAllocation [ [] ]";
  122. // Max string length in practice is 260, but let's have some overhead and
  123. // round up to nearest power of two.
  124. char string_buf[512];
  125. rtc::SimpleStringBuilder ssb(string_buf);
  126. ssb << "VideoBitrateAllocation [";
  127. uint32_t spatial_cumulator = 0;
  128. for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
  129. RTC_DCHECK_LE(spatial_cumulator, sum_);
  130. if (spatial_cumulator == sum_)
  131. break;
  132. const uint32_t layer_sum = GetSpatialLayerSum(si);
  133. if (layer_sum == sum_ && si == 0) {
  134. ssb << " [";
  135. } else {
  136. if (si > 0)
  137. ssb << ",";
  138. ssb << '\n' << " [";
  139. }
  140. spatial_cumulator += layer_sum;
  141. uint32_t temporal_cumulator = 0;
  142. for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
  143. RTC_DCHECK_LE(temporal_cumulator, layer_sum);
  144. if (temporal_cumulator == layer_sum)
  145. break;
  146. if (ti > 0)
  147. ssb << ", ";
  148. uint32_t bitrate = bitrates_[si][ti].value_or(0);
  149. ssb << bitrate;
  150. temporal_cumulator += bitrate;
  151. }
  152. ssb << "]";
  153. }
  154. RTC_DCHECK_EQ(spatial_cumulator, sum_);
  155. ssb << " ]";
  156. return ssb.str();
  157. }
  158. } // namespace webrtc