Chromium Code Reviews| 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..a89a1c44583b50f3e294e1b3b03f03e293c0c313 |
| --- /dev/null |
| +++ b/webrtc/modules/video_coding/utility/simulcast_state.cc |
| @@ -0,0 +1,220 @@ |
| +/* |
| + * 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), |
| + sum_max_bitrate_(0), |
| + sum_target_bitrate_(0), |
| + sum_allocated_bitrate_(0) { |
| + 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; |
| + for (int i = 0; i < codec_.numberOfSimulcastStreams; ++i) { |
| + sum_max_bitrate_ += codec_.simulcastStream[i].maxBitrate; |
| + int target_bitrate = codec_.simulcastStream[i].targetBitrate; |
| + sum_target_bitrate_ += target_bitrate; |
| + 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)); |
| + sum_target_bitrate_ = codec_.targetBitrate; |
| + } |
| + sum_allocated_bitrate_ = sum_target_bitrate_; |
| + AllocateBitrate(codec_.startBitrate); |
| +} |
| + |
| +SimulcastState::~SimulcastState() {} |
| + |
| +int SimulcastState::AllocateBitrate(int target_bitrate_kbps) { |
| + // 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. |
| + sum_allocated_bitrate_ = std::max( |
| + target_bitrate_kbps, static_cast<int>(streams_[0].config.minBitrate)); |
| + int32_t bitrate_left = sum_allocated_bitrate_; |
| + |
| + // Allocate min -> target bitrates as long as we have bitrate to spend. |
| + int last_active_stream = 0; |
| + for (auto stream = streams_.begin(); stream != streams_.end(); ++stream) { |
|
pbos-webrtc
2016/04/29 21:23:27
auto& stream : streams_, or auto& it
|
| + stream->allocated_rate_kbps = 0; |
| + int target_bitrate = |
| + std::max(stream->config.targetBitrate, stream->config.minBitrate); |
| + int allocated = IncreaseBitrateAllocation( |
| + &*stream, std::min(bitrate_left, target_bitrate)); |
| + bitrate_left -= allocated; |
| + if (allocated <= 0 || last_active_stream < stream->idx - 1) { |
| + // Not enough bitrate for this stream, or for some stream before this one; |
|
pbos-webrtc
2016/04/29 21:23:27
Can you do disabling in a loop outside this one? T
|
| + // disable it. |
| + stream->sending = false; |
| + stream->keyframe_request = false; |
| + stream->allocated_rate_kbps = 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) { |
| + Stream* highest_stream = &streams_[last_active_stream]; |
|
pbos-webrtc
2016/04/29 21:23:27
put &streams_[...] inside the below call
|
| + bitrate_left -= IncreaseBitrateAllocation(&*highest_stream, bitrate_left); |
| + } |
| + |
| + sum_allocated_bitrate_ -= bitrate_left; |
| + return sum_allocated_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_kbps_delta) { |
| + int new_bitrate = stream->allocated_rate_kbps + bitrate_kbps_delta; |
| + if (new_bitrate < static_cast<int>(stream->config.minBitrate)) |
| + return 0; |
| + |
| + int allocated_bitrate = bitrate_kbps_delta; |
| + const int max_bitrate = stream->config.maxBitrate; |
| + if (max_bitrate > 0 && new_bitrate > max_bitrate) { |
| + RTC_DCHECK_LE(stream->allocated_rate_kbps, 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_kbps = 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; |
| +} |
| + |
| +std::vector<int> SimulcastState::GetStreamRates() const { |
| + std::vector<int> rates; |
| + for (const Stream& stream : streams_) |
| + rates.push_back(stream.allocated_rate_kbps); |
| + return rates; |
| +} |
| + |
| +int SimulcastState::SumMaxBitrate() const { |
| + return sum_max_bitrate_; |
| +} |
| + |
| +int SimulcastState::SumTargetBitrate() const { |
| + return sum_target_bitrate_; |
| +} |
| + |
| +int SimulcastState::SumAllocatedBitrate() const { |
|
pbos-webrtc
2016/04/29 21:23:27
Where do we actually need this?
|
| + return sum_allocated_bitrate_; |
| +} |
| + |
| +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; |
| +} |
| + |
| +bool SimulcastState::AnyKeyFrameRequested() const { |
|
pbos-webrtc
2016/04/29 21:23:27
Is this used anywhere or should we just GetAndRese
|
| + for (const Stream& stream : streams_) { |
| + if (stream.keyframe_request) |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +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_kbps; |
| +} |
| + |
| +SimulcastState::Stream::Stream(uint8_t idx, VideoCodec* codec, int start_kbps) |
| + : idx(idx), |
| + config(codec->simulcastStream[idx]), |
| + start_bitrate(std::max(static_cast<int>(config.minBitrate), start_kbps)), |
| + parent_codec(codec), |
| + sending(start_kbps >= start_bitrate), |
| + keyframe_request(sending), |
| + allocated_rate_kbps(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; |
| + return codec; |
| +} |
| + |
| +} // namespace webrtc |