blob: da7127964832811356eebb7d29fcb0e52f486cee [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/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"
Karl Wiberg76b7f512018-03-22 15:29:03 +010017#include "rtc_base/time/timestamp_extrapolator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "system_wrappers/include/clock.h"
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000019
niklase@google.com470e71d2011-07-07 08:21:25 +000020namespace webrtc {
Johannes Kron23bfff32021-09-28 21:31:46 +020021namespace {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010022
Johannes Kron23bfff32021-09-28 21:31:46 +020023// Default pacing that is used for the low-latency renderer path.
24constexpr TimeDelta kZeroPlayoutDelayDefaultMinPacing = TimeDelta::Millis(8);
25} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000026
Jonas Orelande02f9ee2022-03-25 12:43:14 +010027VCMTiming::VCMTiming(Clock* clock, const WebRtcKeyValueConfig& field_trials)
ilnik2edc6842017-07-06 03:06:50 -070028 : clock_(clock),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010029 ts_extrapolator_(
30 std::make_unique<TimestampExtrapolator>(clock_->CurrentTime())),
Niels Möller043725f2020-10-30 10:44:52 +010031 codec_timer_(std::make_unique<VCMCodecTimer>()),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010032 render_delay_(kDefaultRenderDelay),
33 min_playout_delay_(TimeDelta::Zero()),
34 max_playout_delay_(TimeDelta::Seconds(10)),
35 jitter_delay_(TimeDelta::Zero()),
36 current_delay_(TimeDelta::Zero()),
ilnik2edc6842017-07-06 03:06:50 -070037 prev_frame_timestamp_(0),
Johannes Kron111e9812020-10-26 13:54:40 +010038 num_decoded_frames_(0),
Johannes Kron985905d2021-06-29 11:37:06 +020039 low_latency_renderer_enabled_("enabled", true),
Johannes Kron23bfff32021-09-28 21:31:46 +020040 zero_playout_delay_min_pacing_("min_pacing",
41 kZeroPlayoutDelayDefaultMinPacing),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010042 last_decode_scheduled_(Timestamp::Zero()) {
Johannes Kron111e9812020-10-26 13:54:40 +010043 ParseFieldTrial({&low_latency_renderer_enabled_},
Jonas Orelande02f9ee2022-03-25 12:43:14 +010044 field_trials.Lookup("WebRTC-LowLatencyRenderer"));
Johannes Kron985905d2021-06-29 11:37:06 +020045 ParseFieldTrial({&zero_playout_delay_min_pacing_},
Jonas Orelande02f9ee2022-03-25 12:43:14 +010046 field_trials.Lookup("WebRTC-ZeroPlayoutDelay"));
niklase@google.com470e71d2011-07-07 08:21:25 +000047}
48
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000049void VCMTiming::Reset() {
Markus Handell6deec382020-07-07 12:17:12 +020050 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010051 ts_extrapolator_->Reset(clock_->CurrentTime());
Niels Möller043725f2020-10-30 10:44:52 +010052 codec_timer_ = std::make_unique<VCMCodecTimer>();
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010053 render_delay_ = kDefaultRenderDelay;
54 min_playout_delay_ = TimeDelta::Zero();
55 jitter_delay_ = TimeDelta::Zero();
56 current_delay_ = TimeDelta::Zero();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000057 prev_frame_timestamp_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000058}
59
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010060void VCMTiming::set_render_delay(TimeDelta render_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020061 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010062 render_delay_ = render_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010065void VCMTiming::set_min_playout_delay(TimeDelta min_playout_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020066 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010067 min_playout_delay_ = min_playout_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010070void VCMTiming::set_max_playout_delay(TimeDelta max_playout_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020071 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010072 max_playout_delay_ = max_playout_delay;
isheriff6b4b5f32016-06-08 00:24:21 -070073}
74
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010075void VCMTiming::SetJitterDelay(TimeDelta jitter_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020076 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010077 if (jitter_delay != jitter_delay_) {
78 jitter_delay_ = jitter_delay;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000079 // When in initial state, set current delay to minimum delay.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010080 if (current_delay_.IsZero()) {
81 current_delay_ = jitter_delay_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000082 }
83 }
niklase@google.com470e71d2011-07-07 08:21:25 +000084}
85
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000086void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
Markus Handell6deec382020-07-07 12:17:12 +020087 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010088 TimeDelta target_delay = TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +000089
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010090 if (current_delay_.IsZero()) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000091 // Not initialized, set current delay to target.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010092 current_delay_ = target_delay;
93 } else if (target_delay != current_delay_) {
94 TimeDelta delay_diff = target_delay - current_delay_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000095 // Never change the delay with more than 100 ms every second. If we're
96 // changing the delay in too large steps we will get noticeable freezes. By
97 // limiting the change we can increase the delay in smaller steps, which
98 // will be experienced as the video is played in slow motion. When lowering
99 // the delay the video will be played at a faster pace.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100100 TimeDelta max_change = TimeDelta::Zero();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000101 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
102 // wrap
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100103 max_change =
104 TimeDelta::Millis(kDelayMaxChangeMsPerS *
105 (frame_timestamp + (static_cast<int64_t>(1) << 32) -
106 prev_frame_timestamp_) /
107 90000);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000108 } else {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100109 max_change =
110 TimeDelta::Millis(kDelayMaxChangeMsPerS *
111 (frame_timestamp - prev_frame_timestamp_) / 90000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 }
philipelfd5a20f2016-11-15 00:57:57 -0800113
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100114 if (max_change <= TimeDelta::Zero()) {
Åsa Persson8368d1a2018-01-05 12:44:45 +0100115 // Any changes less than 1 ms are truncated and will be postponed.
116 // Negative change will be due to reordering and should be ignored.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000117 return;
118 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100119 delay_diff = std::max(delay_diff, -max_change);
120 delay_diff = std::min(delay_diff, max_change);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000121
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100122 current_delay_ = current_delay_ + delay_diff;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000123 }
124 prev_frame_timestamp_ = frame_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
126
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100127void VCMTiming::UpdateCurrentDelay(Timestamp render_time,
128 Timestamp actual_decode_time) {
Markus Handell6deec382020-07-07 12:17:12 +0200129 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100130 TimeDelta target_delay = TargetDelayInternal();
131 TimeDelta delayed =
132 (actual_decode_time - render_time) + RequiredDecodeTime() + render_delay_;
133 if (delayed < TimeDelta::Zero()) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000134 return;
135 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100136 if (current_delay_ + delayed <= target_delay) {
137 current_delay_ += delayed;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000138 } else {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100139 current_delay_ = target_delay;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000140 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
142
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100143void VCMTiming::StopDecodeTimer(TimeDelta decode_time, Timestamp now) {
Markus Handell6deec382020-07-07 12:17:12 +0200144 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100145 codec_timer_->AddTiming(decode_time.ms(), now.ms());
146 RTC_DCHECK_GE(decode_time, TimeDelta::Zero());
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000147 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000148}
149
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100150void VCMTiming::IncomingTimestamp(uint32_t rtp_timestamp, Timestamp now) {
Markus Handell6deec382020-07-07 12:17:12 +0200151 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100152 ts_extrapolator_->Update(now, rtp_timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000153}
154
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100155Timestamp VCMTiming::RenderTime(uint32_t frame_timestamp, Timestamp now) const {
Markus Handell6deec382020-07-07 12:17:12 +0200156 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100157 return RenderTimeInternal(frame_timestamp, now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000158}
159
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700160void VCMTiming::SetLastDecodeScheduledTimestamp(
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100161 Timestamp last_decode_scheduled) {
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700162 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100163 last_decode_scheduled_ = last_decode_scheduled;
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700164}
165
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100166Timestamp VCMTiming::RenderTimeInternal(uint32_t frame_timestamp,
167 Timestamp now) const {
168 constexpr TimeDelta kLowLatencyRendererMaxPlayoutDelay =
169 TimeDelta::Millis(500);
170 if (min_playout_delay_.IsZero() &&
171 (max_playout_delay_.IsZero() ||
Johannes Kron111e9812020-10-26 13:54:40 +0100172 (low_latency_renderer_enabled_ &&
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100173 max_playout_delay_ <= kLowLatencyRendererMaxPlayoutDelay))) {
Johannes Kron111e9812020-10-26 13:54:40 +0100174 // Render as soon as possible or with low-latency renderer algorithm.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100175 return Timestamp::Zero();
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200176 }
Niels Möller043725f2020-10-30 10:44:52 +0100177 // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const
178 // method; it mutates the object's wraparound state.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100179 Timestamp estimated_complete_time =
180 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp).value_or(now);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000181
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100182 // Make sure the actual delay stays in the range of `min_playout_delay_`
183 // and `max_playout_delay_`.
184 TimeDelta actual_delay =
185 current_delay_.Clamped(min_playout_delay_, max_playout_delay_);
186 return estimated_complete_time + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100189TimeDelta VCMTiming::RequiredDecodeTime() const {
isheriff6b4b5f32016-06-08 00:24:21 -0700190 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200191 RTC_DCHECK_GE(decode_time_ms, 0);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100192 return TimeDelta::Millis(decode_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100195TimeDelta VCMTiming::MaxWaitingTime(Timestamp render_time,
196 Timestamp now,
197 bool too_many_frames_queued) const {
Markus Handell6deec382020-07-07 12:17:12 +0200198 MutexLock lock(&mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100200 if (render_time.IsZero() && zero_playout_delay_min_pacing_->us() > 0 &&
201 min_playout_delay_.IsZero() && max_playout_delay_ > TimeDelta::Zero()) {
202 // `render_time` == 0 indicates that the frame should be decoded and
Johannes Kron985905d2021-06-29 11:37:06 +0200203 // rendered as soon as possible. However, the decoder can be choked if too
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200204 // many frames are sent at once. Therefore, limit the interframe delay to
205 // |zero_playout_delay_min_pacing_| unless too many frames are queued in
206 // which case the frames are sent to the decoder at once.
207 if (too_many_frames_queued) {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100208 return TimeDelta::Zero();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200209 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100210 Timestamp earliest_next_decode_start_time =
211 last_decode_scheduled_ + zero_playout_delay_min_pacing_;
212 TimeDelta max_wait_time = now >= earliest_next_decode_start_time
213 ? TimeDelta::Zero()
214 : earliest_next_decode_start_time - now;
215 return max_wait_time;
Johannes Kron985905d2021-06-29 11:37:06 +0200216 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100217 return render_time - now - RequiredDecodeTime() - render_delay_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000218}
219
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100220TimeDelta VCMTiming::TargetVideoDelay() const {
Markus Handell6deec382020-07-07 12:17:12 +0200221 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000222 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000223}
224
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100225TimeDelta VCMTiming::TargetDelayInternal() const {
226 return std::max(min_playout_delay_,
227 jitter_delay_ + RequiredDecodeTime() + render_delay_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228}
229
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100230VCMTiming::VideoDelayTimings VCMTiming::GetTimings() const {
Markus Handell6deec382020-07-07 12:17:12 +0200231 MutexLock lock(&mutex_);
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100232 return VideoDelayTimings{.max_decode_duration = RequiredDecodeTime(),
233 .current_delay = current_delay_,
234 .target_delay = TargetDelayInternal(),
235 .jitter_buffer_delay = jitter_delay_,
236 .min_playout_delay = min_playout_delay_,
Evan Shrubsole8f1159b2022-03-22 12:12:17 +0100237 .max_playout_delay = max_playout_delay_,
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100238 .render_delay = render_delay_,
239 .num_decoded_frames = num_decoded_frames_};
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000240}
241
ilnik2edc6842017-07-06 03:06:50 -0700242void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
Markus Handell6deec382020-07-07 12:17:12 +0200243 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700244 timing_frame_info_.emplace(info);
245}
246
Danil Chapovalov0040b662018-06-18 10:48:16 +0200247absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
Markus Handell6deec382020-07-07 12:17:12 +0200248 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700249 return timing_frame_info_;
250}
251
Johannes Kron111e9812020-10-26 13:54:40 +0100252void VCMTiming::SetMaxCompositionDelayInFrames(
253 absl::optional<int> max_composition_delay_in_frames) {
254 MutexLock lock(&mutex_);
255 max_composition_delay_in_frames_ = max_composition_delay_in_frames;
256}
257
258absl::optional<int> VCMTiming::MaxCompositionDelayInFrames() const {
259 MutexLock lock(&mutex_);
260 return max_composition_delay_in_frames_;
261}
262
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000263} // namespace webrtc