| Index: webrtc/modules/video_coding/utility/simulcast_state.cc
|
| diff --git a/webrtc/modules/video_coding/utility/simulcast_state.cc b/webrtc/modules/video_coding/utility/simulcast_state.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..fca0e982b361c5a16ef0471f25299d95e68e04de
|
| --- /dev/null
|
| +++ b/webrtc/modules/video_coding/utility/simulcast_state.cc
|
| @@ -0,0 +1,201 @@
|
| +/*
|
| + * 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_state.h"
|
| +
|
| +#include <algorithm>
|
| +#include <numeric>
|
| +
|
| +#include "webrtc/base/checks.h"
|
| +
|
| +namespace webrtc {
|
| +
|
| +SimulcastState::SimulcastState(const VideoCodec& codec) : codec_(codec) {
|
| + RTC_DCHECK_EQ(kVideoCodecVP8, codec_.codecType)
|
| + << "Only VP8 Simulcast currently supported.";
|
| + if (codec_.numberOfSimulcastStreams == 0) {
|
| + codec_.numberOfSimulcastStreams = 1;
|
| + codec_.simulcastStream[0] = {
|
| + codec_.width,
|
| + codec_.height,
|
| + codec_.codecSpecific.VP8.numberOfTemporalLayers,
|
| + codec_.maxBitrate,
|
| + codec_.targetBitrate,
|
| + codec_.minBitrate,
|
| + codec_.qpMax};
|
| + }
|
| + int start_bitrate_left = codec_.startBitrate * 1000;
|
| + int sum_max_bitrate = 0;
|
| + for (int i = 0; i < codec_.numberOfSimulcastStreams; ++i) {
|
| + sum_max_bitrate += codec_.simulcastStream[i].maxBitrate * 1000;
|
| + int target_bitrate = codec_.simulcastStream[i].targetBitrate * 1000;
|
| + int start_bitrate = std::min(start_bitrate_left, target_bitrate);
|
| + streams_.push_back(Stream(i, &codec_, start_bitrate));
|
| + start_bitrate_left = std::max(0, start_bitrate_left - start_bitrate);
|
| + }
|
| + if (sum_max_bitrate == 0) {
|
| + // All but the last stream is disabled.
|
| + streams_.clear();
|
| + streams_.push_back(Stream(0, &codec_, codec_.startBitrate * 1000));
|
| + }
|
| + AllocateBitrate(codec_.startBitrate * 1000);
|
| +}
|
| +
|
| +SimulcastState::~SimulcastState() {}
|
| +
|
| +const std::vector<SimulcastState::Stream>& SimulcastState::Streams() const {
|
| + return streams_;
|
| +}
|
| +
|
| +int SimulcastState::AllocateBitrate(int target_bitrate_bps) {
|
| + // 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 overriden by this.
|
| + int total_bitrate =
|
| + std::max(target_bitrate_bps,
|
| + static_cast<int>(streams_[0].config.minBitrate) * 1000);
|
| + int32_t bitrate_left = total_bitrate;
|
| +
|
| + // Allocate min -> target bitrates as long as we have bitrate to spend.
|
| + int last_active_stream = 0;
|
| + for (auto& stream : streams_) {
|
| + stream.allocated_rate_bps = 0;
|
| + int target_bitrate =
|
| + std::max(stream.config.targetBitrate, stream.config.minBitrate) * 1000;
|
| + int allocated = IncreaseBitrateAllocation(
|
| + &stream, std::min(bitrate_left, target_bitrate));
|
| + bitrate_left -= allocated;
|
| + if (allocated <= 0) {
|
| + // Not enough bitrate for this stream, disable this and any following.
|
| + for (size_t i = stream.idx; i < streams_.size(); ++i) {
|
| + streams_[i].sending = false;
|
| + streams_[i].keyframe_request = false;
|
| + streams_[i].allocated_rate_bps = 0;
|
| + }
|
| + break;
|
| + }
|
| +
|
| + if (allocated <= 0 || last_active_stream < stream.idx - 1) {
|
| + stream.sending = false;
|
| + stream.keyframe_request = false;
|
| + stream.allocated_rate_bps = 0;
|
| + continue; // Continue disabling all streams above this one.
|
| + }
|
| + last_active_stream = stream.idx;
|
| + }
|
| +
|
| + // Spend additional bits on the highest-quality active layer, up to max
|
| + // bitrate.
|
| + // TODO(pbos): Consider spending additional bits on last_active_stream-1 down
|
| + // to 0 and not just the top layer when we have additional bitrate to spend.
|
| + if (bitrate_left > 0) {
|
| + bitrate_left -=
|
| + IncreaseBitrateAllocation(&streams_[last_active_stream], bitrate_left);
|
| + }
|
| +
|
| + total_bitrate -= bitrate_left;
|
| + return total_bitrate;
|
| +}
|
| +
|
| +// Try to increase bitrate allocation of this stream by bitrate_kbps_delta, and
|
| +// return how much of that budget that was allocated, if any.
|
| +int SimulcastState::IncreaseBitrateAllocation(Stream* stream,
|
| + int bitrate_bps_delta) {
|
| + int new_bitrate = stream->allocated_rate_bps + bitrate_bps_delta;
|
| + if (new_bitrate < static_cast<int>(stream->config.minBitrate * 1000))
|
| + return 0;
|
| +
|
| + int allocated_bitrate = bitrate_bps_delta;
|
| + const int max_bitrate = stream->config.maxBitrate * 1000;
|
| + if (max_bitrate > 0 && new_bitrate > max_bitrate) {
|
| + RTC_DCHECK_LE(stream->allocated_rate_bps, new_bitrate);
|
| + allocated_bitrate -= new_bitrate - max_bitrate;
|
| + new_bitrate = max_bitrate;
|
| + }
|
| +
|
| + if (!stream->sending && new_bitrate > 0) {
|
| + // This stream was previously disabled, request a keyframe.
|
| + stream->sending = true;
|
| + stream->keyframe_request = true;
|
| + }
|
| + stream->allocated_rate_bps = new_bitrate;
|
| +
|
| + return allocated_bitrate;
|
| +}
|
| +
|
| +void SimulcastState::RequestKeyFrame(uint8_t idx) {
|
| + RTC_DCHECK_LT(static_cast<size_t>(idx), streams_.size());
|
| + streams_[idx].keyframe_request = true;
|
| +}
|
| +
|
| +bool SimulcastState::GetAndResetKeyFrameRequest(uint8_t idx) {
|
| + RTC_DCHECK_LT(static_cast<size_t>(idx), streams_.size());
|
| + bool keyframe = streams_[idx].keyframe_request;
|
| + if (keyframe)
|
| + streams_[idx].keyframe_request = false;
|
| + return keyframe;
|
| +}
|
| +
|
| +size_t SimulcastState::NumStreams() const {
|
| + return streams_.size();
|
| +}
|
| +
|
| +size_t SimulcastState::NumSendingStreams() const {
|
| + size_t count = 0;
|
| + for (const Stream& stream : streams_) {
|
| + if (stream.sending)
|
| + ++count;
|
| + }
|
| + return count;
|
| +}
|
| +
|
| +void SimulcastState::SetSending(uint8_t idx, bool sending) {
|
| + RTC_DCHECK(idx < streams_.size());
|
| + if (sending && !streams_[idx].sending)
|
| + streams_[idx].keyframe_request = true;
|
| + streams_[idx].sending = sending;
|
| +}
|
| +
|
| +bool SimulcastState::IsSending(uint8_t idx) const {
|
| + RTC_DCHECK(idx < streams_.size());
|
| + return streams_[idx].sending;
|
| +}
|
| +
|
| +int SimulcastState::AllocatedRate(uint8_t idx) const {
|
| + RTC_DCHECK(idx < streams_.size());
|
| + return streams_[idx].allocated_rate_bps;
|
| +}
|
| +
|
| +SimulcastState::Stream::Stream(uint8_t idx, VideoCodec* codec, int start_bps)
|
| + : idx(idx),
|
| + config(codec->simulcastStream[idx]),
|
| + start_bitrate(
|
| + std::max(static_cast<int>(config.minBitrate * 1000), start_bps)),
|
| + parent_codec(codec),
|
| + sending(start_bps >= start_bitrate),
|
| + keyframe_request(sending),
|
| + allocated_rate_bps(start_bitrate) {}
|
| +
|
| +VideoCodec SimulcastState::Stream::AsCodec() const {
|
| + VideoCodec codec = *parent_codec;
|
| + codec.codecSpecific.VP8.numberOfTemporalLayers =
|
| + config.numberOfTemporalLayers;
|
| + codec.numberOfSimulcastStreams = 0;
|
| + codec.width = config.width;
|
| + codec.height = config.height;
|
| + codec.maxBitrate = config.maxBitrate;
|
| + codec.minBitrate = config.minBitrate;
|
| + codec.qpMax = config.qpMax;
|
| + codec.startBitrate = start_bitrate / 1000;
|
| + return codec;
|
| +}
|
| +
|
| +} // namespace webrtc
|
|
|