blob: f45d620ae09d3c485a573dd76ad1b9e5e5faad86 [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);
Evan Shrubsolef6adc642022-04-26 11:10:21 +020025constexpr TimeDelta kLowLatencyRendererMaxPlayoutDelay = TimeDelta::Millis(500);
26
Johannes Kron23bfff32021-09-28 21:31:46 +020027} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000028
Jonas Orelande62c2f22022-03-29 11:04:48 +020029VCMTiming::VCMTiming(Clock* clock, const FieldTrialsView& field_trials)
ilnik2edc6842017-07-06 03:06:50 -070030 : clock_(clock),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010031 ts_extrapolator_(
32 std::make_unique<TimestampExtrapolator>(clock_->CurrentTime())),
Niels Möller043725f2020-10-30 10:44:52 +010033 codec_timer_(std::make_unique<VCMCodecTimer>()),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010034 render_delay_(kDefaultRenderDelay),
35 min_playout_delay_(TimeDelta::Zero()),
36 max_playout_delay_(TimeDelta::Seconds(10)),
37 jitter_delay_(TimeDelta::Zero()),
38 current_delay_(TimeDelta::Zero()),
ilnik2edc6842017-07-06 03:06:50 -070039 prev_frame_timestamp_(0),
Johannes Kron111e9812020-10-26 13:54:40 +010040 num_decoded_frames_(0),
Johannes Kron23bfff32021-09-28 21:31:46 +020041 zero_playout_delay_min_pacing_("min_pacing",
42 kZeroPlayoutDelayDefaultMinPacing),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010043 last_decode_scheduled_(Timestamp::Zero()) {
Johannes Kron985905d2021-06-29 11:37:06 +020044 ParseFieldTrial({&zero_playout_delay_min_pacing_},
Jonas Orelande02f9ee2022-03-25 12:43:14 +010045 field_trials.Lookup("WebRTC-ZeroPlayoutDelay"));
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000048void VCMTiming::Reset() {
Markus Handell6deec382020-07-07 12:17:12 +020049 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010050 ts_extrapolator_->Reset(clock_->CurrentTime());
Niels Möller043725f2020-10-30 10:44:52 +010051 codec_timer_ = std::make_unique<VCMCodecTimer>();
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010052 render_delay_ = kDefaultRenderDelay;
53 min_playout_delay_ = TimeDelta::Zero();
54 jitter_delay_ = TimeDelta::Zero();
55 current_delay_ = TimeDelta::Zero();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000056 prev_frame_timestamp_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010059void VCMTiming::set_render_delay(TimeDelta render_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020060 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010061 render_delay_ = render_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010064void VCMTiming::set_min_playout_delay(TimeDelta min_playout_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020065 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010066 min_playout_delay_ = min_playout_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010069void VCMTiming::set_max_playout_delay(TimeDelta max_playout_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020070 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010071 max_playout_delay_ = max_playout_delay;
isheriff6b4b5f32016-06-08 00:24:21 -070072}
73
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010074void VCMTiming::SetJitterDelay(TimeDelta jitter_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020075 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010076 if (jitter_delay != jitter_delay_) {
77 jitter_delay_ = jitter_delay;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000078 // When in initial state, set current delay to minimum delay.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010079 if (current_delay_.IsZero()) {
80 current_delay_ = jitter_delay_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000081 }
82 }
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000085void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
Markus Handell6deec382020-07-07 12:17:12 +020086 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010087 TimeDelta target_delay = TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +000088
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010089 if (current_delay_.IsZero()) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000090 // Not initialized, set current delay to target.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010091 current_delay_ = target_delay;
92 } else if (target_delay != current_delay_) {
93 TimeDelta delay_diff = target_delay - current_delay_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000094 // Never change the delay with more than 100 ms every second. If we're
95 // changing the delay in too large steps we will get noticeable freezes. By
96 // limiting the change we can increase the delay in smaller steps, which
97 // will be experienced as the video is played in slow motion. When lowering
98 // the delay the video will be played at a faster pace.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010099 TimeDelta max_change = TimeDelta::Zero();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000100 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
101 // wrap
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100102 max_change =
103 TimeDelta::Millis(kDelayMaxChangeMsPerS *
104 (frame_timestamp + (static_cast<int64_t>(1) << 32) -
105 prev_frame_timestamp_) /
106 90000);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000107 } else {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100108 max_change =
109 TimeDelta::Millis(kDelayMaxChangeMsPerS *
110 (frame_timestamp - prev_frame_timestamp_) / 90000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 }
philipelfd5a20f2016-11-15 00:57:57 -0800112
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100113 if (max_change <= TimeDelta::Zero()) {
Åsa Persson8368d1a2018-01-05 12:44:45 +0100114 // Any changes less than 1 ms are truncated and will be postponed.
115 // Negative change will be due to reordering and should be ignored.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000116 return;
117 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100118 delay_diff = std::max(delay_diff, -max_change);
119 delay_diff = std::min(delay_diff, max_change);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000120
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100121 current_delay_ = current_delay_ + delay_diff;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000122 }
123 prev_frame_timestamp_ = frame_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124}
125
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100126void VCMTiming::UpdateCurrentDelay(Timestamp render_time,
127 Timestamp actual_decode_time) {
Markus Handell6deec382020-07-07 12:17:12 +0200128 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100129 TimeDelta target_delay = TargetDelayInternal();
130 TimeDelta delayed =
131 (actual_decode_time - render_time) + RequiredDecodeTime() + render_delay_;
132 if (delayed < TimeDelta::Zero()) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000133 return;
134 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100135 if (current_delay_ + delayed <= target_delay) {
136 current_delay_ += delayed;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000137 } else {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100138 current_delay_ = target_delay;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000139 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000140}
141
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100142void VCMTiming::StopDecodeTimer(TimeDelta decode_time, Timestamp now) {
Markus Handell6deec382020-07-07 12:17:12 +0200143 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100144 codec_timer_->AddTiming(decode_time.ms(), now.ms());
145 RTC_DCHECK_GE(decode_time, TimeDelta::Zero());
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000146 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147}
148
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100149void VCMTiming::IncomingTimestamp(uint32_t rtp_timestamp, Timestamp now) {
Markus Handell6deec382020-07-07 12:17:12 +0200150 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100151 ts_extrapolator_->Update(now, rtp_timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000152}
153
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100154Timestamp VCMTiming::RenderTime(uint32_t frame_timestamp, Timestamp now) const {
Markus Handell6deec382020-07-07 12:17:12 +0200155 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100156 return RenderTimeInternal(frame_timestamp, now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000157}
158
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700159void VCMTiming::SetLastDecodeScheduledTimestamp(
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100160 Timestamp last_decode_scheduled) {
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700161 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100162 last_decode_scheduled_ = last_decode_scheduled;
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700163}
164
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100165Timestamp VCMTiming::RenderTimeInternal(uint32_t frame_timestamp,
166 Timestamp now) const {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100167 if (min_playout_delay_.IsZero() &&
168 (max_playout_delay_.IsZero() ||
Evan Shrubsolef6adc642022-04-26 11:10:21 +0200169 max_playout_delay_ <= kLowLatencyRendererMaxPlayoutDelay)) {
Johannes Kron111e9812020-10-26 13:54:40 +0100170 // Render as soon as possible or with low-latency renderer algorithm.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100171 return Timestamp::Zero();
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200172 }
Niels Möller043725f2020-10-30 10:44:52 +0100173 // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const
174 // method; it mutates the object's wraparound state.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100175 Timestamp estimated_complete_time =
176 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp).value_or(now);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000177
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100178 // Make sure the actual delay stays in the range of `min_playout_delay_`
179 // and `max_playout_delay_`.
180 TimeDelta actual_delay =
181 current_delay_.Clamped(min_playout_delay_, max_playout_delay_);
182 return estimated_complete_time + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000183}
184
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100185TimeDelta VCMTiming::RequiredDecodeTime() const {
isheriff6b4b5f32016-06-08 00:24:21 -0700186 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200187 RTC_DCHECK_GE(decode_time_ms, 0);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100188 return TimeDelta::Millis(decode_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
190
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100191TimeDelta VCMTiming::MaxWaitingTime(Timestamp render_time,
192 Timestamp now,
193 bool too_many_frames_queued) const {
Markus Handell6deec382020-07-07 12:17:12 +0200194 MutexLock lock(&mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100196 if (render_time.IsZero() && zero_playout_delay_min_pacing_->us() > 0 &&
197 min_playout_delay_.IsZero() && max_playout_delay_ > TimeDelta::Zero()) {
198 // `render_time` == 0 indicates that the frame should be decoded and
Johannes Kron985905d2021-06-29 11:37:06 +0200199 // rendered as soon as possible. However, the decoder can be choked if too
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200200 // many frames are sent at once. Therefore, limit the interframe delay to
201 // |zero_playout_delay_min_pacing_| unless too many frames are queued in
202 // which case the frames are sent to the decoder at once.
203 if (too_many_frames_queued) {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100204 return TimeDelta::Zero();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200205 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100206 Timestamp earliest_next_decode_start_time =
207 last_decode_scheduled_ + zero_playout_delay_min_pacing_;
208 TimeDelta max_wait_time = now >= earliest_next_decode_start_time
209 ? TimeDelta::Zero()
210 : earliest_next_decode_start_time - now;
211 return max_wait_time;
Johannes Kron985905d2021-06-29 11:37:06 +0200212 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100213 return render_time - now - RequiredDecodeTime() - render_delay_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214}
215
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100216TimeDelta VCMTiming::TargetVideoDelay() const {
Markus Handell6deec382020-07-07 12:17:12 +0200217 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000218 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000219}
220
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100221TimeDelta VCMTiming::TargetDelayInternal() const {
222 return std::max(min_playout_delay_,
223 jitter_delay_ + RequiredDecodeTime() + render_delay_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100226VCMTiming::VideoDelayTimings VCMTiming::GetTimings() const {
Markus Handell6deec382020-07-07 12:17:12 +0200227 MutexLock lock(&mutex_);
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100228 return VideoDelayTimings{.max_decode_duration = RequiredDecodeTime(),
229 .current_delay = current_delay_,
230 .target_delay = TargetDelayInternal(),
231 .jitter_buffer_delay = jitter_delay_,
232 .min_playout_delay = min_playout_delay_,
Evan Shrubsole8f1159b2022-03-22 12:12:17 +0100233 .max_playout_delay = max_playout_delay_,
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100234 .render_delay = render_delay_,
235 .num_decoded_frames = num_decoded_frames_};
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000236}
237
ilnik2edc6842017-07-06 03:06:50 -0700238void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
Markus Handell6deec382020-07-07 12:17:12 +0200239 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700240 timing_frame_info_.emplace(info);
241}
242
Danil Chapovalov0040b662018-06-18 10:48:16 +0200243absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
Markus Handell6deec382020-07-07 12:17:12 +0200244 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700245 return timing_frame_info_;
246}
247
Johannes Kron111e9812020-10-26 13:54:40 +0100248void VCMTiming::SetMaxCompositionDelayInFrames(
249 absl::optional<int> max_composition_delay_in_frames) {
250 MutexLock lock(&mutex_);
251 max_composition_delay_in_frames_ = max_composition_delay_in_frames;
252}
253
254absl::optional<int> VCMTiming::MaxCompositionDelayInFrames() const {
255 MutexLock lock(&mutex_);
256 return max_composition_delay_in_frames_;
257}
258
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000259} // namespace webrtc