Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1066)

Side by Side Diff: webrtc/modules/video_coding/utility/simulcast_state.cc

Issue 1913073002: Extract common simulcast logic from VP8 wrapper and simulcast adapter (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Address comments, added tests Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 *
10 */
11
12 #include "webrtc/modules/video_coding/utility/simulcast_state.h"
13
14 #include <algorithm>
15 #include <numeric>
16
17 #include "webrtc/base/checks.h"
18
19 namespace webrtc {
20
21 SimulcastState::SimulcastState(const VideoCodec& codec) : codec_(codec) {
22 RTC_DCHECK_EQ(kVideoCodecVP8, codec_.codecType)
23 << "Only VP8 Simulcast currently supported.";
24 if (codec_.numberOfSimulcastStreams == 0) {
25 codec_.numberOfSimulcastStreams = 1;
26 codec_.simulcastStream[0] = {
27 codec_.width,
28 codec_.height,
29 codec_.codecSpecific.VP8.numberOfTemporalLayers,
30 codec_.maxBitrate,
31 codec_.targetBitrate,
32 codec_.minBitrate,
33 codec_.qpMax};
34 }
35 int start_bitrate_left = codec_.startBitrate * 1000;
36 int sum_max_bitrate = 0;
37 for (int i = 0; i < codec_.numberOfSimulcastStreams; ++i) {
38 sum_max_bitrate += codec_.simulcastStream[i].maxBitrate * 1000;
39 int target_bitrate = codec_.simulcastStream[i].targetBitrate * 1000;
40 int start_bitrate = std::min(start_bitrate_left, target_bitrate);
41 streams_.push_back(Stream(i, &codec_, start_bitrate));
42 start_bitrate_left = std::max(0, start_bitrate_left - start_bitrate);
43 }
44 if (sum_max_bitrate == 0) {
45 // All but the last stream is disabled.
46 streams_.clear();
47 streams_.push_back(Stream(0, &codec_, codec_.startBitrate * 1000));
48 }
49 AllocateBitrate(codec_.startBitrate * 1000);
50 }
51
52 SimulcastState::~SimulcastState() {}
53
54 const std::vector<SimulcastState::Stream>& SimulcastState::Streams() const {
55 return streams_;
56 }
57
58 int SimulcastState::AllocateBitrate(int target_bitrate_bps) {
59 // Always allocate enough bitrate for the minimum bitrate of the first layer.
60 // Suspending below min bitrate is controlled outside the codec implementation
61 // and is not overriden by this.
62 int total_bitrate =
63 std::max(target_bitrate_bps,
64 static_cast<int>(streams_[0].config.minBitrate) * 1000);
65 int32_t bitrate_left = total_bitrate;
66
67 // Allocate min -> target bitrates as long as we have bitrate to spend.
68 int last_active_stream = 0;
69 for (auto& stream : streams_) {
70 stream.allocated_rate_bps = 0;
71 int target_bitrate =
72 std::max(stream.config.targetBitrate, stream.config.minBitrate) * 1000;
73 int allocated = IncreaseBitrateAllocation(
74 &stream, std::min(bitrate_left, target_bitrate));
75 bitrate_left -= allocated;
76 if (allocated <= 0) {
77 // Not enough bitrate for this stream, disable this and any following.
78 for (size_t i = stream.idx; i < streams_.size(); ++i) {
79 streams_[i].sending = false;
80 streams_[i].keyframe_request = false;
81 streams_[i].allocated_rate_bps = 0;
82 }
83 break;
84 }
85
86 if (allocated <= 0 || last_active_stream < stream.idx - 1) {
87 stream.sending = false;
88 stream.keyframe_request = false;
89 stream.allocated_rate_bps = 0;
90 continue; // Continue disabling all streams above this one.
91 }
92 last_active_stream = stream.idx;
93 }
94
95 // Spend additional bits on the highest-quality active layer, up to max
96 // bitrate.
97 // TODO(pbos): Consider spending additional bits on last_active_stream-1 down
98 // to 0 and not just the top layer when we have additional bitrate to spend.
99 if (bitrate_left > 0) {
100 bitrate_left -=
101 IncreaseBitrateAllocation(&streams_[last_active_stream], bitrate_left);
102 }
103
104 total_bitrate -= bitrate_left;
105 return total_bitrate;
106 }
107
108 // Try to increase bitrate allocation of this stream by bitrate_kbps_delta, and
109 // return how much of that budget that was allocated, if any.
110 int SimulcastState::IncreaseBitrateAllocation(Stream* stream,
111 int bitrate_bps_delta) {
112 int new_bitrate = stream->allocated_rate_bps + bitrate_bps_delta;
113 if (new_bitrate < static_cast<int>(stream->config.minBitrate * 1000))
114 return 0;
115
116 int allocated_bitrate = bitrate_bps_delta;
117 const int max_bitrate = stream->config.maxBitrate * 1000;
118 if (max_bitrate > 0 && new_bitrate > max_bitrate) {
119 RTC_DCHECK_LE(stream->allocated_rate_bps, new_bitrate);
120 allocated_bitrate -= new_bitrate - max_bitrate;
121 new_bitrate = max_bitrate;
122 }
123
124 if (!stream->sending && new_bitrate > 0) {
125 // This stream was previously disabled, request a keyframe.
126 stream->sending = true;
127 stream->keyframe_request = true;
128 }
129 stream->allocated_rate_bps = new_bitrate;
130
131 return allocated_bitrate;
132 }
133
134 void SimulcastState::RequestKeyFrame(uint8_t idx) {
135 RTC_DCHECK_LT(static_cast<size_t>(idx), streams_.size());
136 streams_[idx].keyframe_request = true;
137 }
138
139 bool SimulcastState::GetAndResetKeyFrameRequest(uint8_t idx) {
140 RTC_DCHECK_LT(static_cast<size_t>(idx), streams_.size());
141 bool keyframe = streams_[idx].keyframe_request;
142 if (keyframe)
143 streams_[idx].keyframe_request = false;
144 return keyframe;
145 }
146
147 size_t SimulcastState::NumStreams() const {
148 return streams_.size();
149 }
150
151 size_t SimulcastState::NumSendingStreams() const {
152 size_t count = 0;
153 for (const Stream& stream : streams_) {
154 if (stream.sending)
155 ++count;
156 }
157 return count;
158 }
159
160 void SimulcastState::SetSending(uint8_t idx, bool sending) {
161 RTC_DCHECK(idx < streams_.size());
162 if (sending && !streams_[idx].sending)
163 streams_[idx].keyframe_request = true;
164 streams_[idx].sending = sending;
165 }
166
167 bool SimulcastState::IsSending(uint8_t idx) const {
168 RTC_DCHECK(idx < streams_.size());
169 return streams_[idx].sending;
170 }
171
172 int SimulcastState::AllocatedRate(uint8_t idx) const {
173 RTC_DCHECK(idx < streams_.size());
174 return streams_[idx].allocated_rate_bps;
175 }
176
177 SimulcastState::Stream::Stream(uint8_t idx, VideoCodec* codec, int start_bps)
178 : idx(idx),
179 config(codec->simulcastStream[idx]),
180 start_bitrate(
181 std::max(static_cast<int>(config.minBitrate * 1000), start_bps)),
182 parent_codec(codec),
183 sending(start_bps >= start_bitrate),
184 keyframe_request(sending),
185 allocated_rate_bps(start_bitrate) {}
186
187 VideoCodec SimulcastState::Stream::AsCodec() const {
188 VideoCodec codec = *parent_codec;
189 codec.codecSpecific.VP8.numberOfTemporalLayers =
190 config.numberOfTemporalLayers;
191 codec.numberOfSimulcastStreams = 0;
192 codec.width = config.width;
193 codec.height = config.height;
194 codec.maxBitrate = config.maxBitrate;
195 codec.minBitrate = config.minBitrate;
196 codec.qpMax = config.qpMax;
197 codec.startBitrate = start_bitrate / 1000;
198 return codec;
199 }
200
201 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698