forked from JumpingYang001/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move BitrateAllocation to api/ and rename it VideoBitrateAllocation
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
Showing
75 changed files
with
452 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.