Skip to content

Commit

Permalink
Move BitrateAllocation to api/ and rename it VideoBitrateAllocation
Browse files Browse the repository at this point in the history
Since the webrtc_common build target does not have visibility set, we
cannot easily use BitrateAllocation in other parts of Chromium.
This is currently blocking parts of chromium:794608, and I know of other
usage outside webrtc already, so moving it to api/ should be warranted.

Also, since there's some naming confusion and this class is video
specific rename it VideoBitrateAllocation. This also fits with the
standard interface for producing these: VideoBitrateAllocator.

Bug: chromium:794608
Change-Id: I4c0fae40f9365e860c605a76a4f67ecc9b9cf9fe
Reviewed-on: https://webrtc-review.googlesource.com/70783
Reviewed-by: Karl Wiberg <[email protected]>
Reviewed-by: Niels Moller <[email protected]>
Commit-Queue: Erik Språng <[email protected]>
Cr-Commit-Position: refs/heads/master@{#22986}
  • Loading branch information
Erik Språng authored and Commit Bot committed Apr 23, 2018
1 parent 5c14725 commit 566124a
Show file tree
Hide file tree
Showing 75 changed files with 452 additions and 348 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ rtc_static_library("webrtc_common") {
":typedefs",
"api:array_view",
"api:optional",
"api:video_bitrate_allocation",
"rtc_base:checks",
"rtc_base:deprecation",
"rtc_base:stringutils",
Expand Down
15 changes: 15 additions & 0 deletions api/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,21 @@ rtc_source_set("libjingle_peerconnection_test_api") {
]
}

rtc_source_set("video_bitrate_allocation") {
visibility = [ "*" ]
sources = [
"video/video_bitrate_allocation.cc",
"video/video_bitrate_allocation.h",
]
deps = [
":optional",
"..:typedefs",
"../rtc_base:checks",
"../rtc_base:safe_conversions",
"../rtc_base:stringutils",
]
}

if (rtc_include_tests) {
if (rtc_enable_protobuf) {
rtc_source_set("audioproc_f_api") {
Expand Down
168 changes: 168 additions & 0 deletions api/video/video_bitrate_allocation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

#include "api/video/video_bitrate_allocation.h"

#include <limits>

#include "rtc_base/checks.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "rtc_base/strings/string_builder.h"
#include "rtc_base/stringutils.h"

namespace webrtc {

VideoBitrateAllocation::VideoBitrateAllocation() : sum_(0) {}

bool VideoBitrateAllocation::SetBitrate(size_t spatial_index,
size_t temporal_index,
uint32_t bitrate_bps) {
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
int64_t new_bitrate_sum_bps = sum_;
rtc::Optional<uint32_t>& layer_bitrate =
bitrates_[spatial_index][temporal_index];
if (layer_bitrate) {
RTC_DCHECK_LE(*layer_bitrate, sum_);
new_bitrate_sum_bps -= *layer_bitrate;
}
new_bitrate_sum_bps += bitrate_bps;
if (new_bitrate_sum_bps > kMaxBitrateBps)
return false;

layer_bitrate = bitrate_bps;
sum_ = rtc::dchecked_cast<uint32_t>(new_bitrate_sum_bps);
return true;
}

bool VideoBitrateAllocation::HasBitrate(size_t spatial_index,
size_t temporal_index) const {
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
return bitrates_[spatial_index][temporal_index].has_value();
}

uint32_t VideoBitrateAllocation::GetBitrate(size_t spatial_index,
size_t temporal_index) const {
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
return bitrates_[spatial_index][temporal_index].value_or(0);
}

// Whether the specific spatial layers has the bitrate set in any of its
// temporal layers.
bool VideoBitrateAllocation::IsSpatialLayerUsed(size_t spatial_index) const {
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
for (size_t i = 0; i < kMaxTemporalStreams; ++i) {
if (bitrates_[spatial_index][i].has_value())
return true;
}
return false;
}

// Get the sum of all the temporal layer for a specific spatial layer.
uint32_t VideoBitrateAllocation::GetSpatialLayerSum(
size_t spatial_index) const {
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
return GetTemporalLayerSum(spatial_index, kMaxTemporalStreams - 1);
}

uint32_t VideoBitrateAllocation::GetTemporalLayerSum(
size_t spatial_index,
size_t temporal_index) const {
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
uint32_t sum = 0;
for (size_t i = 0; i <= temporal_index; ++i) {
sum += bitrates_[spatial_index][i].value_or(0);
}
return sum;
}

std::vector<uint32_t> VideoBitrateAllocation::GetTemporalLayerAllocation(
size_t spatial_index) const {
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
std::vector<uint32_t> temporal_rates;

// Find the highest temporal layer with a defined bitrate in order to
// determine the size of the temporal layer allocation.
for (size_t i = kMaxTemporalStreams; i > 0; --i) {
if (bitrates_[spatial_index][i - 1].has_value()) {
temporal_rates.resize(i);
break;
}
}

for (size_t i = 0; i < temporal_rates.size(); ++i) {
temporal_rates[i] = bitrates_[spatial_index][i].value_or(0);
}

return temporal_rates;
}

bool VideoBitrateAllocation::operator==(
const VideoBitrateAllocation& other) const {
for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
if (bitrates_[si][ti] != other.bitrates_[si][ti])
return false;
}
}
return true;
}

std::string VideoBitrateAllocation::ToString() const {
if (sum_ == 0)
return "VideoBitrateAllocation [ [] ]";

// Max string length in practice is 260, but let's have some overhead and
// round up to nearest power of two.
char string_buf[512];
rtc::SimpleStringBuilder ssb(string_buf);

ssb << "VideoBitrateAllocation [";
uint32_t spatial_cumulator = 0;
for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
RTC_DCHECK_LE(spatial_cumulator, sum_);
if (spatial_cumulator == sum_)
break;

const uint32_t layer_sum = GetSpatialLayerSum(si);
if (layer_sum == sum_) {
ssb << " [";
} else {
if (si > 0)
ssb << ",";
ssb << '\n' << " [";
}
spatial_cumulator += layer_sum;

uint32_t temporal_cumulator = 0;
for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
RTC_DCHECK_LE(temporal_cumulator, layer_sum);
if (temporal_cumulator == layer_sum)
break;

if (ti > 0)
ssb << ", ";

uint32_t bitrate = bitrates_[si][ti].value_or(0);
ssb << bitrate;
temporal_cumulator += bitrate;
}
ssb << "]";
}

RTC_DCHECK_EQ(spatial_cumulator, sum_);
ssb << " ]";
return ssb.str();
}

} // namespace webrtc
85 changes: 85 additions & 0 deletions api/video/video_bitrate_allocation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

#ifndef API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_
#define API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_

#include <limits>
#include <string>
#include <vector>

#include "api/optional.h"
#include "typedefs.h" // NOLINT(build/include)

namespace webrtc {

// TODO(sprang): Move back to common_types when include of this is removed.
enum : int { kMaxSimulcastStreams = 4 };
enum : int { kMaxSpatialLayers = 5 };
enum : int { kMaxTemporalStreams = 4 };

// Class that describes how video bitrate, in bps, is allocated across temporal
// and spatial layers. Not that bitrates are NOT cumulative. Depending on if
// layers are dependent or not, it is up to the user to aggregate.
// For each index, the bitrate can also both set and unset. This is used with a
// set bps = 0 to signal an explicit "turn off" signal.
class VideoBitrateAllocation {
public:
static constexpr uint32_t kMaxBitrateBps =
std::numeric_limits<uint32_t>::max();
VideoBitrateAllocation();

bool SetBitrate(size_t spatial_index,
size_t temporal_index,
uint32_t bitrate_bps);

bool HasBitrate(size_t spatial_index, size_t temporal_index) const;

uint32_t GetBitrate(size_t spatial_index, size_t temporal_index) const;

// Whether the specific spatial layers has the bitrate set in any of its
// temporal layers.
bool IsSpatialLayerUsed(size_t spatial_index) const;

// Get the sum of all the temporal layer for a specific spatial layer.
uint32_t GetSpatialLayerSum(size_t spatial_index) const;

// Sum of bitrates of temporal layers, from layer 0 to |temporal_index|
// inclusive, of specified spatial layer |spatial_index|. Bitrates of lower
// spatial layers are not included.
uint32_t GetTemporalLayerSum(size_t spatial_index,
size_t temporal_index) const;

// Returns a vector of the temporal layer bitrates for the specific spatial
// layer. Length of the returned vector is cropped to the highest temporal
// layer with a defined bitrate.
std::vector<uint32_t> GetTemporalLayerAllocation(size_t spatial_index) const;

uint32_t get_sum_bps() const { return sum_; } // Sum of all bitrates.
uint32_t get_sum_kbps() const {
// Round down to not exceed the allocated bitrate.
return sum_ / 1000;
}

bool operator==(const VideoBitrateAllocation& other) const;
inline bool operator!=(const VideoBitrateAllocation& other) const {
return !(*this == other);
}

std::string ToString() const;

private:
uint32_t sum_;
rtc::Optional<uint32_t> bitrates_[kMaxSpatialLayers][kMaxTemporalStreams];
};

} // namespace webrtc

#endif // API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_
2 changes: 1 addition & 1 deletion api/video_codecs/video_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int32_t VideoEncoder::SetRates(uint32_t bitrate, uint32_t framerate) {
}

int32_t VideoEncoder::SetRateAllocation(
const BitrateAllocation& allocation,
const VideoBitrateAllocation& allocation,
uint32_t framerate) {
return SetRates(allocation.get_sum_kbps(), framerate);
}
Expand Down
2 changes: 1 addition & 1 deletion api/video_codecs/video_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class VideoEncoder {

// Default fallback: Just use the sum of bitrates as the single target rate.
// TODO(sprang): Remove this default implementation when we remove SetRates().
virtual int32_t SetRateAllocation(const BitrateAllocation& allocation,
virtual int32_t SetRateAllocation(const VideoBitrateAllocation& allocation,
uint32_t framerate);

// Any encoder implementation wishing to use the WebRTC provided
Expand Down
2 changes: 1 addition & 1 deletion call/call_perf_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ TEST_F(CallPerfTest, MAYBE_KeepsHighBitrateWhenReconfiguringSender) {
return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
}

int32_t SetRateAllocation(const BitrateAllocation& rate_allocation,
int32_t SetRateAllocation(const VideoBitrateAllocation& rate_allocation,
uint32_t framerate) override {
last_set_bitrate_kbps_ = rate_allocation.get_sum_kbps();
if (encoder_inits_ == 1 &&
Expand Down
Loading

0 comments on commit 566124a

Please sign in to comment.