Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit d0a8f51

Browse files
perkjCommit Bot
authored andcommitted
Reland Refactor reporting of VideoBitrateAllocation
Original description Move reporting of target bitrate to just after the encoder has been updated. Originall submitted as refs/heads/master@{#32275} Patch 1 contains the original cl ,patch 2 the fix to send rtcp even if BWE does not change. Bug: webrtc:12000 Change-Id: I16766e08229fe1f6f65f449e0e074bed03338693 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186948 Reviewed-by: Erik Språng <[email protected]> Commit-Queue: Per Kjellander <[email protected]> Cr-Commit-Position: refs/heads/master@{#32340}
1 parent d9603b2 commit d0a8f51

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
lines changed

api/video_codecs/video_encoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ class RTC_EXPORT VideoEncoder {
267267

268268
// Target bitrate, per spatial/temporal layer.
269269
// A target bitrate of 0bps indicates a layer should not be encoded at all.
270+
VideoBitrateAllocation target_bitrate;
271+
// Adjusted target bitrate, per spatial/temporal layer. May be lower or
272+
// higher than the target depending on encoder behaviour.
270273
VideoBitrateAllocation bitrate;
271274
// Target framerate, in fps. A value <= 0.0 is invalid and should be
272275
// interpreted as framerate target not available. In this case the encoder

video/video_stream_encoder.cc

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ void VideoStreamEncoder::ReconfigureEncoder() {
874874
last_encoder_rate_settings_.reset();
875875
rate_settings.rate_control.framerate_fps = GetInputFramerateFps();
876876

877-
SetEncoderRates(UpdateBitrateAllocationAndNotifyObserver(rate_settings));
877+
SetEncoderRates(UpdateBitrateAllocation(rate_settings));
878878
}
879879

880880
encoder_stats_observer_->OnEncoderReconfigured(encoder_config_, streams);
@@ -1053,7 +1053,7 @@ void VideoStreamEncoder::TraceFrameDropEnd() {
10531053
}
10541054

10551055
VideoStreamEncoder::EncoderRateSettings
1056-
VideoStreamEncoder::UpdateBitrateAllocationAndNotifyObserver(
1056+
VideoStreamEncoder::UpdateBitrateAllocation(
10571057
const EncoderRateSettings& rate_settings) {
10581058
VideoBitrateAllocation new_allocation;
10591059
// Only call allocators if bitrate > 0 (ie, not suspended), otherwise they
@@ -1064,24 +1064,8 @@ VideoStreamEncoder::UpdateBitrateAllocationAndNotifyObserver(
10641064
rate_settings.rate_control.framerate_fps));
10651065
}
10661066

1067-
if (bitrate_observer_ && new_allocation.get_sum_bps() > 0) {
1068-
if (encoder_ && encoder_initialized_) {
1069-
// Avoid too old encoder_info_.
1070-
const int64_t kMaxDiffMs = 100;
1071-
const bool updated_recently =
1072-
(last_encode_info_ms_ && ((clock_->TimeInMilliseconds() -
1073-
*last_encode_info_ms_) < kMaxDiffMs));
1074-
// Update allocation according to info from encoder.
1075-
bitrate_observer_->OnBitrateAllocationUpdated(
1076-
UpdateAllocationFromEncoderInfo(
1077-
new_allocation,
1078-
updated_recently ? encoder_info_ : encoder_->GetEncoderInfo()));
1079-
} else {
1080-
bitrate_observer_->OnBitrateAllocationUpdated(new_allocation);
1081-
}
1082-
}
1083-
10841067
EncoderRateSettings new_rate_settings = rate_settings;
1068+
new_rate_settings.rate_control.target_bitrate = new_allocation;
10851069
new_rate_settings.rate_control.bitrate = new_allocation;
10861070
// VideoBitrateAllocator subclasses may allocate a bitrate higher than the
10871071
// target in order to sustain the min bitrate of the video codec. In this
@@ -1102,9 +1086,6 @@ VideoStreamEncoder::UpdateBitrateAllocationAndNotifyObserver(
11021086
new_rate_settings.rate_control.bitrate = adjusted_allocation;
11031087
}
11041088

1105-
encoder_stats_observer_->OnBitrateAllocationUpdated(
1106-
send_codec_, new_rate_settings.rate_control.bitrate);
1107-
11081089
return new_rate_settings;
11091090
}
11101091

@@ -1147,11 +1128,22 @@ void VideoStreamEncoder::SetEncoderRates(
11471128

11481129
if (rate_control_changed) {
11491130
encoder_->SetRates(rate_settings.rate_control);
1131+
1132+
encoder_stats_observer_->OnBitrateAllocationUpdated(
1133+
send_codec_, rate_settings.rate_control.bitrate);
11501134
frame_encode_metadata_writer_.OnSetRates(
11511135
rate_settings.rate_control.bitrate,
11521136
static_cast<uint32_t>(rate_settings.rate_control.framerate_fps + 0.5));
11531137
stream_resource_manager_.SetEncoderRates(rate_settings.rate_control);
11541138
}
1139+
if (bitrate_observer_) {
1140+
bitrate_observer_->OnBitrateAllocationUpdated(
1141+
// Update allocation according to info from encoder. An encoder may
1142+
// choose to not use all layers due to for example HW.
1143+
UpdateAllocationFromEncoderInfo(
1144+
rate_settings.rate_control.target_bitrate,
1145+
encoder_->GetEncoderInfo()));
1146+
}
11551147
}
11561148

11571149
void VideoStreamEncoder::MaybeEncodeVideoFrame(const VideoFrame& video_frame,
@@ -1202,8 +1194,7 @@ void VideoStreamEncoder::MaybeEncodeVideoFrame(const VideoFrame& video_frame,
12021194
EncoderRateSettings new_rate_settings = *last_encoder_rate_settings_;
12031195
new_rate_settings.rate_control.framerate_fps =
12041196
static_cast<double>(framerate_fps);
1205-
SetEncoderRates(
1206-
UpdateBitrateAllocationAndNotifyObserver(new_rate_settings));
1197+
SetEncoderRates(UpdateBitrateAllocation(new_rate_settings));
12071198
}
12081199
last_parameters_update_ms_.emplace(now_ms);
12091200
}
@@ -1738,7 +1729,7 @@ void VideoStreamEncoder::OnBitrateUpdated(DataRate target_bitrate,
17381729
EncoderRateSettings new_rate_settings{
17391730
VideoBitrateAllocation(), static_cast<double>(framerate_fps),
17401731
link_allocation, target_bitrate, stable_target_bitrate};
1741-
SetEncoderRates(UpdateBitrateAllocationAndNotifyObserver(new_rate_settings));
1732+
SetEncoderRates(UpdateBitrateAllocation(new_rate_settings));
17421733

17431734
if (target_bitrate.bps() != 0)
17441735
encoder_target_bitrate_bps_ = target_bitrate.bps();

video/video_stream_encoder.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,8 @@ class VideoStreamEncoder : public VideoStreamEncoderInterface,
192192
void TraceFrameDropEnd();
193193

194194
// Returns a copy of |rate_settings| with the |bitrate| field updated using
195-
// the current VideoBitrateAllocator, and notifies any listeners of the new
196-
// allocation.
197-
EncoderRateSettings UpdateBitrateAllocationAndNotifyObserver(
195+
// the current VideoBitrateAllocator.
196+
EncoderRateSettings UpdateBitrateAllocation(
198197
const EncoderRateSettings& rate_settings) RTC_RUN_ON(&encoder_queue_);
199198

200199
uint32_t GetInputFramerateFps() RTC_RUN_ON(&encoder_queue_);

video/video_stream_encoder_unittest.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace webrtc {
5858

5959
using ::testing::_;
6060
using ::testing::AllOf;
61+
using ::testing::AtLeast;
6162
using ::testing::Eq;
6263
using ::testing::Field;
6364
using ::testing::Ge;
@@ -3950,9 +3951,9 @@ TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
39503951

39513952
// Called after a process interval.
39523953
EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3953-
.Times(1);
3954+
.Times(AtLeast(3));
39543955
const int64_t start_time_ms = CurrentTimeMs();
3955-
while (CurrentTimeMs() - start_time_ms < kProcessIntervalMs) {
3956+
while (CurrentTimeMs() - start_time_ms < 5 * kProcessIntervalMs) {
39563957
video_source_.IncomingCapturedFrame(
39573958
CreateFrame(CurrentTimeMs(), codec_width_, codec_height_));
39583959
WaitForEncodedFrame(CurrentTimeMs());

0 commit comments

Comments
 (0)