blob: 37dc825bed3ba3a855ac2938911756a5d407f832 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 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
Rasmus Brandtc4d253c2022-05-25 12:03:35 +020011#include "modules/video_coding/timing/timing.h"
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000012
philipel5908c712015-12-21 08:23:20 -080013#include <algorithm>
14
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010015#include "api/units/time_delta.h"
Johannes Kron111e9812020-10-26 13:54:40 +010016#include "rtc_base/experiments/field_trial_parser.h"
Evan Shrubsoleeabaf8d2022-05-23 17:45:58 +020017#include "rtc_base/logging.h"
Karl Wiberg76b7f512018-03-22 15:29:03 +010018#include "rtc_base/time/timestamp_extrapolator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "system_wrappers/include/clock.h"
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000020
niklase@google.com470e71d2011-07-07 08:21:25 +000021namespace webrtc {
Johannes Kron23bfff32021-09-28 21:31:46 +020022namespace {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010023
Johannes Kron23bfff32021-09-28 21:31:46 +020024// Default pacing that is used for the low-latency renderer path.
25constexpr TimeDelta kZeroPlayoutDelayDefaultMinPacing = TimeDelta::Millis(8);
Johannes Kronbbf639e2022-06-15 12:27:23 +020026constexpr TimeDelta kLowLatencyStreamMaxPlayoutDelayThreshold =
27 TimeDelta::Millis(500);
Evan Shrubsolef6adc642022-04-26 11:10:21 +020028
Evan Shrubsolecc52f072022-05-24 15:00:00 +020029void CheckDelaysValid(TimeDelta min_delay, TimeDelta max_delay) {
30 if (min_delay > max_delay) {
31 RTC_LOG(LS_ERROR)
32 << "Playout delays set incorrectly: min playout delay (" << min_delay
33 << ") > max playout delay (" << max_delay
34 << "). This is undefined behaviour. Application writers should "
35 "ensure that the min delay is always less than or equals max "
36 "delay. If trying to use the playout delay header extensions "
37 "described in "
38 "https://webrtc.googlesource.com/src/+/refs/heads/main/docs/"
39 "native-code/rtp-hdrext/playout-delay/, be careful that a playout "
40 "delay hint or A/V sync settings may have caused this conflict.";
41 }
42}
43
Johannes Kron23bfff32021-09-28 21:31:46 +020044} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000045
Jonas Orelande62c2f22022-03-29 11:04:48 +020046VCMTiming::VCMTiming(Clock* clock, const FieldTrialsView& field_trials)
ilnik2edc6842017-07-06 03:06:50 -070047 : clock_(clock),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010048 ts_extrapolator_(
49 std::make_unique<TimestampExtrapolator>(clock_->CurrentTime())),
Rasmus Brandt23772262022-05-23 09:53:15 +020050 codec_timer_(std::make_unique<CodecTimer>()),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010051 render_delay_(kDefaultRenderDelay),
52 min_playout_delay_(TimeDelta::Zero()),
53 max_playout_delay_(TimeDelta::Seconds(10)),
54 jitter_delay_(TimeDelta::Zero()),
55 current_delay_(TimeDelta::Zero()),
ilnik2edc6842017-07-06 03:06:50 -070056 prev_frame_timestamp_(0),
Johannes Kron111e9812020-10-26 13:54:40 +010057 num_decoded_frames_(0),
Johannes Kron23bfff32021-09-28 21:31:46 +020058 zero_playout_delay_min_pacing_("min_pacing",
59 kZeroPlayoutDelayDefaultMinPacing),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010060 last_decode_scheduled_(Timestamp::Zero()) {
Johannes Kron985905d2021-06-29 11:37:06 +020061 ParseFieldTrial({&zero_playout_delay_min_pacing_},
Jonas Orelande02f9ee2022-03-25 12:43:14 +010062 field_trials.Lookup("WebRTC-ZeroPlayoutDelay"));
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000065void VCMTiming::Reset() {
Markus Handell6deec382020-07-07 12:17:12 +020066 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010067 ts_extrapolator_->Reset(clock_->CurrentTime());
Rasmus Brandt23772262022-05-23 09:53:15 +020068 codec_timer_ = std::make_unique<CodecTimer>();
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010069 render_delay_ = kDefaultRenderDelay;
70 min_playout_delay_ = TimeDelta::Zero();
71 jitter_delay_ = TimeDelta::Zero();
72 current_delay_ = TimeDelta::Zero();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000073 prev_frame_timestamp_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000074}
75
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010076void VCMTiming::set_render_delay(TimeDelta render_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020077 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010078 render_delay_ = render_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
80
Evan Shrubsoleeabaf8d2022-05-23 17:45:58 +020081TimeDelta VCMTiming::min_playout_delay() const {
82 MutexLock lock(&mutex_);
83 return min_playout_delay_;
84}
85
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010086void VCMTiming::set_min_playout_delay(TimeDelta min_playout_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020087 MutexLock lock(&mutex_);
Evan Shrubsolecc52f072022-05-24 15:00:00 +020088 if (min_playout_delay_ != min_playout_delay) {
89 CheckDelaysValid(min_playout_delay, max_playout_delay_);
90 min_playout_delay_ = min_playout_delay;
91 }
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010094void VCMTiming::set_max_playout_delay(TimeDelta max_playout_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020095 MutexLock lock(&mutex_);
Evan Shrubsoleeabaf8d2022-05-23 17:45:58 +020096 if (max_playout_delay_ != max_playout_delay) {
Evan Shrubsolecc52f072022-05-24 15:00:00 +020097 CheckDelaysValid(min_playout_delay_, max_playout_delay);
Evan Shrubsoleeabaf8d2022-05-23 17:45:58 +020098 max_playout_delay_ = max_playout_delay;
99 }
isheriff6b4b5f32016-06-08 00:24:21 -0700100}
101
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100102void VCMTiming::SetJitterDelay(TimeDelta jitter_delay) {
Markus Handell6deec382020-07-07 12:17:12 +0200103 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100104 if (jitter_delay != jitter_delay_) {
105 jitter_delay_ = jitter_delay;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000106 // When in initial state, set current delay to minimum delay.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100107 if (current_delay_.IsZero()) {
108 current_delay_ = jitter_delay_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000109 }
110 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000111}
112
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000113void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
Markus Handell6deec382020-07-07 12:17:12 +0200114 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100115 TimeDelta target_delay = TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100117 if (current_delay_.IsZero()) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000118 // Not initialized, set current delay to target.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100119 current_delay_ = target_delay;
120 } else if (target_delay != current_delay_) {
121 TimeDelta delay_diff = target_delay - current_delay_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000122 // Never change the delay with more than 100 ms every second. If we're
123 // changing the delay in too large steps we will get noticeable freezes. By
124 // limiting the change we can increase the delay in smaller steps, which
125 // will be experienced as the video is played in slow motion. When lowering
126 // the delay the video will be played at a faster pace.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100127 TimeDelta max_change = TimeDelta::Zero();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000128 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
129 // wrap
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100130 max_change =
131 TimeDelta::Millis(kDelayMaxChangeMsPerS *
132 (frame_timestamp + (static_cast<int64_t>(1) << 32) -
133 prev_frame_timestamp_) /
134 90000);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000135 } else {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100136 max_change =
137 TimeDelta::Millis(kDelayMaxChangeMsPerS *
138 (frame_timestamp - prev_frame_timestamp_) / 90000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 }
philipelfd5a20f2016-11-15 00:57:57 -0800140
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100141 if (max_change <= TimeDelta::Zero()) {
Ã…sa Persson8368d1a2018-01-05 12:44:45 +0100142 // Any changes less than 1 ms are truncated and will be postponed.
143 // Negative change will be due to reordering and should be ignored.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000144 return;
145 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100146 delay_diff = std::max(delay_diff, -max_change);
147 delay_diff = std::min(delay_diff, max_change);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000148
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100149 current_delay_ = current_delay_ + delay_diff;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000150 }
151 prev_frame_timestamp_ = frame_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152}
153
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100154void VCMTiming::UpdateCurrentDelay(Timestamp render_time,
155 Timestamp actual_decode_time) {
Markus Handell6deec382020-07-07 12:17:12 +0200156 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100157 TimeDelta target_delay = TargetDelayInternal();
158 TimeDelta delayed =
159 (actual_decode_time - render_time) + RequiredDecodeTime() + render_delay_;
Evan Shrubsole496ad522022-08-01 15:03:23 +0000160
161 // Only consider `delayed` as negative by more than a few microseconds.
162 if (delayed.ms() < 0) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000163 return;
164 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100165 if (current_delay_ + delayed <= target_delay) {
166 current_delay_ += delayed;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000167 } else {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100168 current_delay_ = target_delay;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000169 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000170}
171
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100172void VCMTiming::StopDecodeTimer(TimeDelta decode_time, Timestamp now) {
Markus Handell6deec382020-07-07 12:17:12 +0200173 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100174 codec_timer_->AddTiming(decode_time.ms(), now.ms());
175 RTC_DCHECK_GE(decode_time, TimeDelta::Zero());
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000176 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000177}
178
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100179void VCMTiming::IncomingTimestamp(uint32_t rtp_timestamp, Timestamp now) {
Markus Handell6deec382020-07-07 12:17:12 +0200180 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100181 ts_extrapolator_->Update(now, rtp_timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182}
183
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100184Timestamp VCMTiming::RenderTime(uint32_t frame_timestamp, Timestamp now) const {
Markus Handell6deec382020-07-07 12:17:12 +0200185 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100186 return RenderTimeInternal(frame_timestamp, now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700189void VCMTiming::SetLastDecodeScheduledTimestamp(
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100190 Timestamp last_decode_scheduled) {
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700191 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100192 last_decode_scheduled_ = last_decode_scheduled;
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700193}
194
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100195Timestamp VCMTiming::RenderTimeInternal(uint32_t frame_timestamp,
196 Timestamp now) const {
Johannes Kronbbf639e2022-06-15 12:27:23 +0200197 if (UseLowLatencyRendering()) {
Johannes Kron111e9812020-10-26 13:54:40 +0100198 // Render as soon as possible or with low-latency renderer algorithm.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100199 return Timestamp::Zero();
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200200 }
Niels Möller043725f2020-10-30 10:44:52 +0100201 // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const
202 // method; it mutates the object's wraparound state.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100203 Timestamp estimated_complete_time =
204 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp).value_or(now);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000205
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100206 // Make sure the actual delay stays in the range of `min_playout_delay_`
207 // and `max_playout_delay_`.
208 TimeDelta actual_delay =
209 current_delay_.Clamped(min_playout_delay_, max_playout_delay_);
210 return estimated_complete_time + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100213TimeDelta VCMTiming::RequiredDecodeTime() const {
isheriff6b4b5f32016-06-08 00:24:21 -0700214 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200215 RTC_DCHECK_GE(decode_time_ms, 0);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100216 return TimeDelta::Millis(decode_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000217}
218
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100219TimeDelta VCMTiming::MaxWaitingTime(Timestamp render_time,
220 Timestamp now,
221 bool too_many_frames_queued) const {
Markus Handell6deec382020-07-07 12:17:12 +0200222 MutexLock lock(&mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000223
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100224 if (render_time.IsZero() && zero_playout_delay_min_pacing_->us() > 0 &&
225 min_playout_delay_.IsZero() && max_playout_delay_ > TimeDelta::Zero()) {
226 // `render_time` == 0 indicates that the frame should be decoded and
Johannes Kron985905d2021-06-29 11:37:06 +0200227 // rendered as soon as possible. However, the decoder can be choked if too
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200228 // many frames are sent at once. Therefore, limit the interframe delay to
229 // |zero_playout_delay_min_pacing_| unless too many frames are queued in
230 // which case the frames are sent to the decoder at once.
231 if (too_many_frames_queued) {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100232 return TimeDelta::Zero();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200233 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100234 Timestamp earliest_next_decode_start_time =
235 last_decode_scheduled_ + zero_playout_delay_min_pacing_;
236 TimeDelta max_wait_time = now >= earliest_next_decode_start_time
237 ? TimeDelta::Zero()
238 : earliest_next_decode_start_time - now;
239 return max_wait_time;
Johannes Kron985905d2021-06-29 11:37:06 +0200240 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100241 return render_time - now - RequiredDecodeTime() - render_delay_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000242}
243
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100244TimeDelta VCMTiming::TargetVideoDelay() const {
Markus Handell6deec382020-07-07 12:17:12 +0200245 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000246 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000247}
248
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100249TimeDelta VCMTiming::TargetDelayInternal() const {
250 return std::max(min_playout_delay_,
251 jitter_delay_ + RequiredDecodeTime() + render_delay_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000252}
253
Johannes Kronbbf639e2022-06-15 12:27:23 +0200254VideoFrame::RenderParameters VCMTiming::RenderParameters() const {
255 MutexLock lock(&mutex_);
256 return {.use_low_latency_rendering = UseLowLatencyRendering(),
257 .max_composition_delay_in_frames = max_composition_delay_in_frames_};
258}
259
260bool VCMTiming::UseLowLatencyRendering() const {
261 // min_playout_delay_==0,
262 // max_playout_delay_<=kLowLatencyStreamMaxPlayoutDelayThreshold indicates
263 // that the low-latency path should be used, which means that frames should be
264 // decoded and rendered as soon as possible.
265 return min_playout_delay_.IsZero() &&
266 max_playout_delay_ <= kLowLatencyStreamMaxPlayoutDelayThreshold;
267}
268
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100269VCMTiming::VideoDelayTimings VCMTiming::GetTimings() const {
Markus Handell6deec382020-07-07 12:17:12 +0200270 MutexLock lock(&mutex_);
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100271 return VideoDelayTimings{.max_decode_duration = RequiredDecodeTime(),
272 .current_delay = current_delay_,
273 .target_delay = TargetDelayInternal(),
274 .jitter_buffer_delay = jitter_delay_,
275 .min_playout_delay = min_playout_delay_,
Evan Shrubsole8f1159b2022-03-22 12:12:17 +0100276 .max_playout_delay = max_playout_delay_,
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100277 .render_delay = render_delay_,
278 .num_decoded_frames = num_decoded_frames_};
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000279}
280
ilnik2edc6842017-07-06 03:06:50 -0700281void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
Markus Handell6deec382020-07-07 12:17:12 +0200282 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700283 timing_frame_info_.emplace(info);
284}
285
Danil Chapovalov0040b662018-06-18 10:48:16 +0200286absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
Markus Handell6deec382020-07-07 12:17:12 +0200287 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700288 return timing_frame_info_;
289}
290
Johannes Kron111e9812020-10-26 13:54:40 +0100291void VCMTiming::SetMaxCompositionDelayInFrames(
292 absl::optional<int> max_composition_delay_in_frames) {
293 MutexLock lock(&mutex_);
294 max_composition_delay_in_frames_ = max_composition_delay_in_frames;
295}
296
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000297} // namespace webrtc