blob: 72d1414b7314060b901e701dc67316585be6b129 [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"
Johannes Kron111e9812020-10-26 13:54:40 +010019#include "system_wrappers/include/field_trial.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);
26} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000027
Niels Möller043725f2020-10-30 10:44:52 +010028VCMTiming::VCMTiming(Clock* clock)
ilnik2edc6842017-07-06 03:06:50 -070029 : clock_(clock),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010030 ts_extrapolator_(
31 std::make_unique<TimestampExtrapolator>(clock_->CurrentTime())),
Niels Möller043725f2020-10-30 10:44:52 +010032 codec_timer_(std::make_unique<VCMCodecTimer>()),
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010033 render_delay_(kDefaultRenderDelay),
34 min_playout_delay_(TimeDelta::Zero()),
35 max_playout_delay_(TimeDelta::Seconds(10)),
36 jitter_delay_(TimeDelta::Zero()),
37 current_delay_(TimeDelta::Zero()),
ilnik2edc6842017-07-06 03:06:50 -070038 prev_frame_timestamp_(0),
Johannes Kron111e9812020-10-26 13:54:40 +010039 num_decoded_frames_(0),
Johannes Kron985905d2021-06-29 11:37:06 +020040 low_latency_renderer_enabled_("enabled", true),
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 Kron111e9812020-10-26 13:54:40 +010044 ParseFieldTrial({&low_latency_renderer_enabled_},
45 field_trial::FindFullName("WebRTC-LowLatencyRenderer"));
Johannes Kron985905d2021-06-29 11:37:06 +020046 ParseFieldTrial({&zero_playout_delay_min_pacing_},
47 field_trial::FindFullName("WebRTC-ZeroPlayoutDelay"));
niklase@google.com470e71d2011-07-07 08:21:25 +000048}
49
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000050void VCMTiming::Reset() {
Markus Handell6deec382020-07-07 12:17:12 +020051 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010052 ts_extrapolator_->Reset(clock_->CurrentTime());
Niels Möller043725f2020-10-30 10:44:52 +010053 codec_timer_ = std::make_unique<VCMCodecTimer>();
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010054 render_delay_ = kDefaultRenderDelay;
55 min_playout_delay_ = TimeDelta::Zero();
56 jitter_delay_ = TimeDelta::Zero();
57 current_delay_ = TimeDelta::Zero();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000058 prev_frame_timestamp_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010061void VCMTiming::set_render_delay(TimeDelta render_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020062 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010063 render_delay_ = render_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +000064}
65
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010066void VCMTiming::set_min_playout_delay(TimeDelta min_playout_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020067 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010068 min_playout_delay_ = min_playout_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +000069}
70
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010071void VCMTiming::set_max_playout_delay(TimeDelta max_playout_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020072 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010073 max_playout_delay_ = max_playout_delay;
isheriff6b4b5f32016-06-08 00:24:21 -070074}
75
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010076void VCMTiming::SetJitterDelay(TimeDelta jitter_delay) {
Markus Handell6deec382020-07-07 12:17:12 +020077 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010078 if (jitter_delay != jitter_delay_) {
79 jitter_delay_ = jitter_delay;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000080 // When in initial state, set current delay to minimum delay.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010081 if (current_delay_.IsZero()) {
82 current_delay_ = jitter_delay_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000083 }
84 }
niklase@google.com470e71d2011-07-07 08:21:25 +000085}
86
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000087void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
Markus Handell6deec382020-07-07 12:17:12 +020088 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010089 TimeDelta target_delay = TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +000090
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010091 if (current_delay_.IsZero()) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000092 // Not initialized, set current delay to target.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010093 current_delay_ = target_delay;
94 } else if (target_delay != current_delay_) {
95 TimeDelta delay_diff = target_delay - current_delay_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000096 // Never change the delay with more than 100 ms every second. If we're
97 // changing the delay in too large steps we will get noticeable freezes. By
98 // limiting the change we can increase the delay in smaller steps, which
99 // will be experienced as the video is played in slow motion. When lowering
100 // the delay the video will be played at a faster pace.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100101 TimeDelta max_change = TimeDelta::Zero();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000102 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
103 // wrap
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100104 max_change =
105 TimeDelta::Millis(kDelayMaxChangeMsPerS *
106 (frame_timestamp + (static_cast<int64_t>(1) << 32) -
107 prev_frame_timestamp_) /
108 90000);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000109 } else {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100110 max_change =
111 TimeDelta::Millis(kDelayMaxChangeMsPerS *
112 (frame_timestamp - prev_frame_timestamp_) / 90000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 }
philipelfd5a20f2016-11-15 00:57:57 -0800114
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100115 if (max_change <= TimeDelta::Zero()) {
Åsa Persson8368d1a2018-01-05 12:44:45 +0100116 // Any changes less than 1 ms are truncated and will be postponed.
117 // Negative change will be due to reordering and should be ignored.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000118 return;
119 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100120 delay_diff = std::max(delay_diff, -max_change);
121 delay_diff = std::min(delay_diff, max_change);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000122
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100123 current_delay_ = current_delay_ + delay_diff;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000124 }
125 prev_frame_timestamp_ = frame_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126}
127
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100128void VCMTiming::UpdateCurrentDelay(Timestamp render_time,
129 Timestamp actual_decode_time) {
Markus Handell6deec382020-07-07 12:17:12 +0200130 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100131 TimeDelta target_delay = TargetDelayInternal();
132 TimeDelta delayed =
133 (actual_decode_time - render_time) + RequiredDecodeTime() + render_delay_;
134 if (delayed < TimeDelta::Zero()) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000135 return;
136 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100137 if (current_delay_ + delayed <= target_delay) {
138 current_delay_ += delayed;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000139 } else {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100140 current_delay_ = target_delay;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000141 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000142}
143
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100144void VCMTiming::StopDecodeTimer(TimeDelta decode_time, Timestamp now) {
Markus Handell6deec382020-07-07 12:17:12 +0200145 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100146 codec_timer_->AddTiming(decode_time.ms(), now.ms());
147 RTC_DCHECK_GE(decode_time, TimeDelta::Zero());
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000148 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000149}
150
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100151void VCMTiming::IncomingTimestamp(uint32_t rtp_timestamp, Timestamp now) {
Markus Handell6deec382020-07-07 12:17:12 +0200152 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100153 ts_extrapolator_->Update(now, rtp_timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000154}
155
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100156Timestamp VCMTiming::RenderTime(uint32_t frame_timestamp, Timestamp now) const {
Markus Handell6deec382020-07-07 12:17:12 +0200157 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100158 return RenderTimeInternal(frame_timestamp, now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000159}
160
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700161void VCMTiming::SetLastDecodeScheduledTimestamp(
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100162 Timestamp last_decode_scheduled) {
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700163 MutexLock lock(&mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100164 last_decode_scheduled_ = last_decode_scheduled;
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700165}
166
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100167Timestamp VCMTiming::RenderTimeInternal(uint32_t frame_timestamp,
168 Timestamp now) const {
169 constexpr TimeDelta kLowLatencyRendererMaxPlayoutDelay =
170 TimeDelta::Millis(500);
171 if (min_playout_delay_.IsZero() &&
172 (max_playout_delay_.IsZero() ||
Johannes Kron111e9812020-10-26 13:54:40 +0100173 (low_latency_renderer_enabled_ &&
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100174 max_playout_delay_ <= kLowLatencyRendererMaxPlayoutDelay))) {
Johannes Kron111e9812020-10-26 13:54:40 +0100175 // Render as soon as possible or with low-latency renderer algorithm.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100176 return Timestamp::Zero();
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200177 }
Niels Möller043725f2020-10-30 10:44:52 +0100178 // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const
179 // method; it mutates the object's wraparound state.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100180 Timestamp estimated_complete_time =
181 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp).value_or(now);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000182
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100183 // Make sure the actual delay stays in the range of `min_playout_delay_`
184 // and `max_playout_delay_`.
185 TimeDelta actual_delay =
186 current_delay_.Clamped(min_playout_delay_, max_playout_delay_);
187 return estimated_complete_time + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188}
189
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100190TimeDelta VCMTiming::RequiredDecodeTime() const {
isheriff6b4b5f32016-06-08 00:24:21 -0700191 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200192 RTC_DCHECK_GE(decode_time_ms, 0);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100193 return TimeDelta::Millis(decode_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194}
195
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100196TimeDelta VCMTiming::MaxWaitingTime(Timestamp render_time,
197 Timestamp now,
198 bool too_many_frames_queued) const {
Markus Handell6deec382020-07-07 12:17:12 +0200199 MutexLock lock(&mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100201 if (render_time.IsZero() && zero_playout_delay_min_pacing_->us() > 0 &&
202 min_playout_delay_.IsZero() && max_playout_delay_ > TimeDelta::Zero()) {
203 // `render_time` == 0 indicates that the frame should be decoded and
Johannes Kron985905d2021-06-29 11:37:06 +0200204 // rendered as soon as possible. However, the decoder can be choked if too
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200205 // many frames are sent at once. Therefore, limit the interframe delay to
206 // |zero_playout_delay_min_pacing_| unless too many frames are queued in
207 // which case the frames are sent to the decoder at once.
208 if (too_many_frames_queued) {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100209 return TimeDelta::Zero();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200210 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100211 Timestamp earliest_next_decode_start_time =
212 last_decode_scheduled_ + zero_playout_delay_min_pacing_;
213 TimeDelta max_wait_time = now >= earliest_next_decode_start_time
214 ? TimeDelta::Zero()
215 : earliest_next_decode_start_time - now;
216 return max_wait_time;
Johannes Kron985905d2021-06-29 11:37:06 +0200217 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100218 return render_time - now - RequiredDecodeTime() - render_delay_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219}
220
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100221TimeDelta VCMTiming::TargetVideoDelay() const {
Markus Handell6deec382020-07-07 12:17:12 +0200222 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000223 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100226TimeDelta VCMTiming::TargetDelayInternal() const {
227 return std::max(min_playout_delay_,
228 jitter_delay_ + RequiredDecodeTime() + render_delay_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000229}
230
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100231VCMTiming::VideoDelayTimings VCMTiming::GetTimings() const {
Markus Handell6deec382020-07-07 12:17:12 +0200232 MutexLock lock(&mutex_);
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100233 return VideoDelayTimings{.max_decode_duration = RequiredDecodeTime(),
234 .current_delay = current_delay_,
235 .target_delay = TargetDelayInternal(),
236 .jitter_buffer_delay = jitter_delay_,
237 .min_playout_delay = min_playout_delay_,
Evan Shrubsole8f1159b2022-03-22 12:12:17 +0100238 .max_playout_delay = max_playout_delay_,
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100239 .render_delay = render_delay_,
240 .num_decoded_frames = num_decoded_frames_};
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000241}
242
ilnik2edc6842017-07-06 03:06:50 -0700243void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
Markus Handell6deec382020-07-07 12:17:12 +0200244 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700245 timing_frame_info_.emplace(info);
246}
247
Danil Chapovalov0040b662018-06-18 10:48:16 +0200248absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
Markus Handell6deec382020-07-07 12:17:12 +0200249 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700250 return timing_frame_info_;
251}
252
Johannes Kron111e9812020-10-26 13:54:40 +0100253void VCMTiming::SetMaxCompositionDelayInFrames(
254 absl::optional<int> max_composition_delay_in_frames) {
255 MutexLock lock(&mutex_);
256 max_composition_delay_in_frames_ = max_composition_delay_in_frames;
257}
258
259absl::optional<int> VCMTiming::MaxCompositionDelayInFrames() const {
260 MutexLock lock(&mutex_);
261 return max_composition_delay_in_frames_;
262}
263
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000264} // namespace webrtc