Chromium Code Reviews| Index: webrtc/modules/video_coding/utility/simulcast_rate_allocator.cc |
| diff --git a/webrtc/modules/video_coding/utility/simulcast_rate_allocator.cc b/webrtc/modules/video_coding/utility/simulcast_rate_allocator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..165515ad9a5de2dccb6094df33aeeb904cd2cc5a |
| --- /dev/null |
| +++ b/webrtc/modules/video_coding/utility/simulcast_rate_allocator.cc |
| @@ -0,0 +1,70 @@ |
| +/* |
| + * Copyright (c) 2016 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 "webrtc/modules/video_coding/utility/simulcast_rate_allocator.h" |
| + |
| +#include <algorithm> |
| + |
| +namespace webrtc { |
| + |
| +webrtc::SimulcastRateAllocator::SimulcastRateAllocator(const VideoCodec& codec) |
| + : codec_(codec) {} |
| + |
| +std::vector<uint32_t> webrtc::SimulcastRateAllocator::GetAllocation( |
| + uint32_t bitrate_kbps) const { |
| + // Always allocate enough bitrate for the minimum bitrate of the first layer. |
| + // Suspending below min bitrate is controlled outside the codec implementation |
| + // and is not overridden by this. |
| + const uint32_t min_bitrate_bps = codec_.numberOfSimulcastStreams == 0 |
| + ? codec_.minBitrate |
| + : codec_.simulcastStream[0].minBitrate; |
| + uint32_t left_to_allocate = std::max(min_bitrate_bps, bitrate_kbps); |
| + if (codec_.maxBitrate) |
| + left_to_allocate = std::min(left_to_allocate, codec_.maxBitrate); |
| + |
| + if (codec_.numberOfSimulcastStreams == 0) { |
| + // No simulcast, just set the target as this has been capped already. |
| + return std::vector<uint32_t>(1, left_to_allocate); |
| + } |
| + |
| + // Initialize bitrates with zeroes. |
| + std::vector<uint32_t> allocated_bitrates_bps(codec_.numberOfSimulcastStreams, |
| + 0); |
| + |
| + // First try to allocate up to the target bitrate for each substream. |
| + uint32_t layer = 0; |
|
noahric
2016/08/29 21:32:09
size_t for an array index?
sprang_webrtc
2016/08/30 13:27:24
Done.
|
| + for (; layer < codec_.numberOfSimulcastStreams; ++layer) { |
| + const SimulcastStream& stream = codec_.simulcastStream[layer]; |
| + if (left_to_allocate < stream.minBitrate) |
| + break; |
| + uint32_t allocation = std::min(left_to_allocate, stream.targetBitrate); |
| + allocated_bitrates_bps[layer] = allocation; |
| + left_to_allocate -= allocation; |
| + } |
| + |
| + // Next, try to distribute remaining bitrate, up to max bitrate, across the |
| + // enabled layers, starting with the highest. |
| + for (; layer --> 0;) { |
|
noahric
2016/08/29 21:32:09
Can you rewrite this? As written, the code looks w
sprang_webrtc
2016/08/30 13:27:24
Aww. I was was aware this was going to be shot dow
noahric
2016/08/30 18:14:33
Ah, yeah :( You can also just make active_layer an
|
| + if (left_to_allocate == 0) |
| + break; |
| + const SimulcastStream& stream = codec_.simulcastStream[layer]; |
| + uint32_t allocation = std::min( |
| + left_to_allocate, stream.maxBitrate - allocated_bitrates_bps[layer]); |
| + left_to_allocate -= allocation; |
| + allocated_bitrates_bps[layer] += allocation; |
| + } |
| + return allocated_bitrates_bps; |
| +} |
| + |
| +const VideoCodec& webrtc::SimulcastRateAllocator::GetCodec() const { |
| + return codec_; |
| +} |
| + |
| +} // namespace webrtc |