Skip to content

Commit 05562e6

Browse files
Johannes KronWebRTC LUCI CQ
authored andcommitted
Merge M93: Add max pre-decode queue size threshold for pacing
When pacing is enabled for the low latency rendering path, frames are sent to the decoder in regular intervals. In case of a jitter, these frames intervals could add up to create a large latency. Hence, disable frame pacing if the pre-decode queue grows beyond the threshold. The threshold for when to disable frame pacing is set through a field trial. The default value is high enough so that the behavior is not changed unless the field trial is specified. (cherry picked from commit 2ddc39e) Bug: chromium:1237402 Change-Id: I901fd579f68da286eca3d654118f60d3c55e21ce Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/228241 Reviewed-by: Ilya Nikolaevskiy <[email protected]> Commit-Queue: Johannes Kron <[email protected]> Cr-Original-Commit-Position: refs/heads/master@{#34705} Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/228540 Cr-Commit-Position: refs/branch-heads/4577@{#4} Cr-Branched-From: 5196931-refs/heads/master@{#34463}
1 parent caf19bc commit 05562e6

File tree

7 files changed

+119
-28
lines changed

7 files changed

+119
-28
lines changed

modules/video_coding/frame_buffer2.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ FrameBuffer::FrameBuffer(Clock* clock,
6363
last_log_non_decoded_ms_(-kLogNonDecodedIntervalMs),
6464
add_rtt_to_playout_delay_(
6565
webrtc::field_trial::IsEnabled("WebRTC-AddRttToPlayoutDelay")),
66-
rtt_mult_settings_(RttMultExperiment::GetRttMultValue()) {
66+
rtt_mult_settings_(RttMultExperiment::GetRttMultValue()),
67+
zero_playout_delay_max_decode_queue_size_("max_decode_queue_size",
68+
kMaxFramesBuffered) {
69+
ParseFieldTrial({&zero_playout_delay_max_decode_queue_size_},
70+
field_trial::FindFullName("WebRTC-ZeroPlayoutDelay"));
6771
callback_checker_.Detach();
6872
}
6973

@@ -212,7 +216,11 @@ int64_t FrameBuffer::FindNextFrame(int64_t now_ms) {
212216
if (frame->RenderTime() == -1) {
213217
frame->SetRenderTime(timing_->RenderTimeMs(frame->Timestamp(), now_ms));
214218
}
215-
wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
219+
bool too_many_frames_queued =
220+
frames_.size() > zero_playout_delay_max_decode_queue_size_ ? true
221+
: false;
222+
wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms,
223+
too_many_frames_queued);
216224

217225
// This will cause the frame buffer to prefer high framerate rather
218226
// than high resolution in the case of the decoder not decoding fast

modules/video_coding/frame_buffer2.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "modules/video_coding/jitter_estimator.h"
2626
#include "modules/video_coding/utility/decoded_frames_history.h"
2727
#include "rtc_base/event.h"
28+
#include "rtc_base/experiments/field_trial_parser.h"
2829
#include "rtc_base/experiments/rtt_mult_experiment.h"
2930
#include "rtc_base/numerics/sequence_number_util.h"
3031
#include "rtc_base/synchronization/mutex.h"
@@ -188,6 +189,13 @@ class FrameBuffer {
188189

189190
// rtt_mult experiment settings.
190191
const absl::optional<RttMultExperiment::Settings> rtt_mult_settings_;
192+
193+
// Maximum number of frames in the decode queue to allow pacing. If the
194+
// queue grows beyond the max limit, pacing will be disabled and frames will
195+
// be pushed to the decoder as soon as possible. This only has an effect
196+
// when the low-latency rendering path is active, which is indicated by
197+
// the frame's render time == 0.
198+
FieldTrialParameter<unsigned> zero_playout_delay_max_decode_queue_size_;
191199
};
192200

193201
} // namespace video_coding

modules/video_coding/frame_buffer2_unittest.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class VCMTimingFake : public VCMTiming {
5656
}
5757

5858
int64_t MaxWaitingTime(int64_t render_time_ms,
59-
int64_t now_ms) const override {
59+
int64_t now_ms,
60+
bool too_many_frames_queued) const override {
6061
return render_time_ms - now_ms - kDecodeTime;
6162
}
6263

modules/video_coding/receiver.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ VCMEncodedFrame* VCMReceiver::FrameForDecoding(uint16_t max_wait_time_ms,
141141
uint16_t new_max_wait_time =
142142
static_cast<uint16_t>(VCM_MAX(available_wait_time, 0));
143143
uint32_t wait_time_ms = rtc::saturated_cast<uint32_t>(
144-
timing_->MaxWaitingTime(render_time_ms, clock_->TimeInMilliseconds()));
144+
timing_->MaxWaitingTime(render_time_ms, clock_->TimeInMilliseconds(),
145+
/*too_many_frames_queued=*/false));
145146
if (new_max_wait_time < wait_time_ms) {
146147
// We're not allowed to wait until the frame is supposed to be rendered,
147148
// waiting as long as we're allowed to avoid busy looping, and then return

modules/video_coding/timing.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,19 @@ int VCMTiming::RequiredDecodeTimeMs() const {
210210
}
211211

212212
int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms,
213-
int64_t now_ms) const {
213+
int64_t now_ms,
214+
bool too_many_frames_queued) const {
214215
MutexLock lock(&mutex_);
215216

216217
if (render_time_ms == 0 && zero_playout_delay_min_pacing_->us() > 0) {
217218
// |render_time_ms| == 0 indicates that the frame should be decoded and
218219
// rendered as soon as possible. However, the decoder can be choked if too
219-
// many frames are sent at ones. Therefore, limit the interframe delay to
220-
// |zero_playout_delay_min_pacing_|.
220+
// many frames are sent at once. Therefore, limit the interframe delay to
221+
// |zero_playout_delay_min_pacing_| unless too many frames are queued in
222+
// which case the frames are sent to the decoder at once.
223+
if (too_many_frames_queued) {
224+
return 0;
225+
}
221226
int64_t earliest_next_decode_start_time =
222227
last_decode_scheduled_ts_ + zero_playout_delay_min_pacing_->ms();
223228
int64_t max_wait_time_ms = now_ms >= earliest_next_decode_start_time

modules/video_coding/timing.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ class VCMTiming {
8282
virtual int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
8383

8484
// Returns the maximum time in ms that we can wait for a frame to become
85-
// complete before we must pass it to the decoder.
86-
virtual int64_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
85+
// complete before we must pass it to the decoder. render_time_ms==0 indicates
86+
// that the frames should be processed as quickly as possible, with possibly
87+
// only a small delay added to make sure that the decoder is not overloaded.
88+
// In this case, the parameter too_many_frames_queued is used to signal that
89+
// the decode queue is full and that the frame should be decoded as soon as
90+
// possible.
91+
virtual int64_t MaxWaitingTime(int64_t render_time_ms,
92+
int64_t now_ms,
93+
bool too_many_frames_queued) const;
8794

8895
// Returns the current target delay which is required delay + decode time +
8996
// render delay.

modules/video_coding/timing_unittest.cc

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ TEST(ReceiverTimingTest, JitterDelay) {
3636
timing.set_render_delay(0);
3737
uint32_t wait_time_ms = timing.MaxWaitingTime(
3838
timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
39-
clock.TimeInMilliseconds());
39+
clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
4040
// First update initializes the render time. Since we have no decode delay
4141
// we get wait_time_ms = renderTime - now - renderDelay = jitter.
4242
EXPECT_EQ(jitter_delay_ms, wait_time_ms);
@@ -48,7 +48,7 @@ TEST(ReceiverTimingTest, JitterDelay) {
4848
timing.UpdateCurrentDelay(timestamp);
4949
wait_time_ms = timing.MaxWaitingTime(
5050
timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
51-
clock.TimeInMilliseconds());
51+
clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
5252
// Since we gradually increase the delay we only get 100 ms every second.
5353
EXPECT_EQ(jitter_delay_ms - 10, wait_time_ms);
5454

@@ -57,7 +57,7 @@ TEST(ReceiverTimingTest, JitterDelay) {
5757
timing.UpdateCurrentDelay(timestamp);
5858
wait_time_ms = timing.MaxWaitingTime(
5959
timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
60-
clock.TimeInMilliseconds());
60+
clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
6161
EXPECT_EQ(jitter_delay_ms, wait_time_ms);
6262

6363
// Insert frames without jitter, verify that this gives the exact wait time.
@@ -70,7 +70,7 @@ TEST(ReceiverTimingTest, JitterDelay) {
7070
timing.UpdateCurrentDelay(timestamp);
7171
wait_time_ms = timing.MaxWaitingTime(
7272
timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
73-
clock.TimeInMilliseconds());
73+
clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
7474
EXPECT_EQ(jitter_delay_ms, wait_time_ms);
7575

7676
// Add decode time estimates for 1 second.
@@ -85,7 +85,7 @@ TEST(ReceiverTimingTest, JitterDelay) {
8585
timing.UpdateCurrentDelay(timestamp);
8686
wait_time_ms = timing.MaxWaitingTime(
8787
timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
88-
clock.TimeInMilliseconds());
88+
clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
8989
EXPECT_EQ(jitter_delay_ms, wait_time_ms);
9090

9191
const int kMinTotalDelayMs = 200;
@@ -97,7 +97,7 @@ TEST(ReceiverTimingTest, JitterDelay) {
9797
timing.set_render_delay(kRenderDelayMs);
9898
wait_time_ms = timing.MaxWaitingTime(
9999
timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
100-
clock.TimeInMilliseconds());
100+
clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
101101
// We should at least have kMinTotalDelayMs - decodeTime (10) - renderTime
102102
// (10) to wait.
103103
EXPECT_EQ(kMinTotalDelayMs - kDecodeTimeMs - kRenderDelayMs, wait_time_ms);
@@ -140,16 +140,26 @@ TEST(ReceiverTimingTest, MaxWaitingTimeIsZeroForZeroRenderTime) {
140140
for (int i = 0; i < 10; ++i) {
141141
clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
142142
int64_t now_ms = clock.TimeInMilliseconds();
143-
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), 0);
143+
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
144+
/*too_many_frames_queued=*/false),
145+
0);
144146
}
145147
// Another frame submitted at the same time also returns a negative max
146148
// waiting time.
147149
int64_t now_ms = clock.TimeInMilliseconds();
148-
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), 0);
150+
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
151+
/*too_many_frames_queued=*/false),
152+
0);
149153
// MaxWaitingTime should be less than zero even if there's a burst of frames.
150-
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), 0);
151-
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), 0);
152-
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), 0);
154+
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
155+
/*too_many_frames_queued=*/false),
156+
0);
157+
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
158+
/*too_many_frames_queued=*/false),
159+
0);
160+
EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
161+
/*too_many_frames_queued=*/false),
162+
0);
153163
}
154164

155165
TEST(ReceiverTimingTest, MaxWaitingTimeZeroDelayPacingExperiment) {
@@ -168,27 +178,38 @@ TEST(ReceiverTimingTest, MaxWaitingTimeZeroDelayPacingExperiment) {
168178
for (int i = 0; i < 10; ++i) {
169179
clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
170180
int64_t now_ms = clock.TimeInMilliseconds();
171-
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), 0);
181+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
182+
/*too_many_frames_queued=*/false),
183+
0);
172184
timing.SetLastDecodeScheduledTimestamp(now_ms);
173185
}
174186
// Another frame submitted at the same time is paced according to the field
175187
// trial setting.
176188
int64_t now_ms = clock.TimeInMilliseconds();
177-
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), kMinPacingMs);
189+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
190+
/*too_many_frames_queued=*/false),
191+
kMinPacingMs);
178192
// If there's a burst of frames, the wait time is calculated based on next
179193
// decode time.
180-
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), kMinPacingMs);
181-
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), kMinPacingMs);
194+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
195+
/*too_many_frames_queued=*/false),
196+
kMinPacingMs);
197+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
198+
/*too_many_frames_queued=*/false),
199+
kMinPacingMs);
182200
// Allow a few ms to pass, this should be subtracted from the MaxWaitingTime.
183201
constexpr int64_t kTwoMs = 2;
184202
clock.AdvanceTimeMilliseconds(kTwoMs);
185203
now_ms = clock.TimeInMilliseconds();
186-
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms),
204+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
205+
/*too_many_frames_queued=*/false),
187206
kMinPacingMs - kTwoMs);
188207
// A frame is decoded at the current time, the wait time should be restored to
189208
// pacing delay.
190209
timing.SetLastDecodeScheduledTimestamp(now_ms);
191-
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms), kMinPacingMs);
210+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
211+
/*too_many_frames_queued=*/false),
212+
kMinPacingMs);
192213
}
193214

194215
TEST(ReceiverTimingTest, DefaultMaxWaitingTimeUnaffectedByPacingExperiment) {
@@ -206,16 +227,56 @@ TEST(ReceiverTimingTest, DefaultMaxWaitingTimeUnaffectedByPacingExperiment) {
206227
int64_t render_time_ms = now_ms + 30;
207228
// Estimate the internal processing delay from the first frame.
208229
int64_t estimated_processing_delay =
209-
(render_time_ms - now_ms) - timing.MaxWaitingTime(render_time_ms, now_ms);
230+
(render_time_ms - now_ms) -
231+
timing.MaxWaitingTime(render_time_ms, now_ms,
232+
/*too_many_frames_queued=*/false);
210233
EXPECT_GT(estimated_processing_delay, 0);
211234

212235
// Any other frame submitted at the same time should be scheduled according to
213236
// its render time.
214237
for (int i = 0; i < 5; ++i) {
215238
render_time_ms += kTimeDeltaMs;
216-
EXPECT_EQ(timing.MaxWaitingTime(render_time_ms, now_ms),
239+
EXPECT_EQ(timing.MaxWaitingTime(render_time_ms, now_ms,
240+
/*too_many_frames_queued=*/false),
217241
render_time_ms - now_ms - estimated_processing_delay);
218242
}
219243
}
220244

245+
TEST(ReceiverTiminTest, MaxWaitingTimeReturnsZeroIfTooManyFramesQueuedIsTrue) {
246+
// The minimum pacing is enabled by a field trial and active if the RTP
247+
// playout delay header extension is set to min==0.
248+
constexpr int64_t kMinPacingMs = 3;
249+
test::ScopedFieldTrials override_field_trials(
250+
"WebRTC-ZeroPlayoutDelay/min_pacing:3ms/");
251+
constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us.
252+
constexpr int64_t kTimeDeltaMs = 1000.0 / 60.0;
253+
constexpr int64_t kZeroRenderTimeMs = 0;
254+
SimulatedClock clock(kStartTimeUs);
255+
VCMTiming timing(&clock);
256+
timing.Reset();
257+
// MaxWaitingTime() returns zero for evenly spaced video frames.
258+
for (int i = 0; i < 10; ++i) {
259+
clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
260+
int64_t now_ms = clock.TimeInMilliseconds();
261+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
262+
/*too_many_frames_queued=*/false),
263+
0);
264+
timing.SetLastDecodeScheduledTimestamp(now_ms);
265+
}
266+
// Another frame submitted at the same time is paced according to the field
267+
// trial setting.
268+
int64_t now_ms = clock.TimeInMilliseconds();
269+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
270+
/*too_many_frames_queued=*/false),
271+
kMinPacingMs);
272+
// MaxWaitingTime returns 0 even if there's a burst of frames if
273+
// too_many_frames_queued is set to true.
274+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
275+
/*too_many_frames_queued=*/true),
276+
0);
277+
EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
278+
/*too_many_frames_queued=*/true),
279+
0);
280+
}
281+
221282
} // namespace webrtc

0 commit comments

Comments
 (0)