blob: 99e525a5d7e5664683537c03d3ad1ea05018c1fc [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
Jonas Olssona4d87372019-07-05 19:08:33 +020013
philipel5908c712015-12-21 08:23:20 -080014#include <algorithm>
15
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 {
23// 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
Niels Möller043725f2020-10-30 10:44:52 +010027VCMTiming::VCMTiming(Clock* clock)
ilnik2edc6842017-07-06 03:06:50 -070028 : clock_(clock),
Niels Möller043725f2020-10-30 10:44:52 +010029 ts_extrapolator_(std::make_unique<TimestampExtrapolator>(
30 clock_->TimeInMilliseconds())),
31 codec_timer_(std::make_unique<VCMCodecTimer>()),
ilnik2edc6842017-07-06 03:06:50 -070032 render_delay_ms_(kDefaultRenderDelayMs),
33 min_playout_delay_ms_(0),
34 max_playout_delay_ms_(10000),
35 jitter_delay_ms_(0),
36 current_delay_ms_(0),
ilnik2edc6842017-07-06 03:06:50 -070037 prev_frame_timestamp_(0),
38 timing_frame_info_(),
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),
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -070043 last_decode_scheduled_ts_(0) {
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_);
wu@webrtc.orged4cb562014-05-06 04:50:49 +000052 ts_extrapolator_->Reset(clock_->TimeInMilliseconds());
Niels Möller043725f2020-10-30 10:44:52 +010053 codec_timer_ = std::make_unique<VCMCodecTimer>();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000054 render_delay_ms_ = kDefaultRenderDelayMs;
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000055 min_playout_delay_ms_ = 0;
56 jitter_delay_ms_ = 0;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000057 current_delay_ms_ = 0;
58 prev_frame_timestamp_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
isheriff6b4b5f32016-06-08 00:24:21 -070061void VCMTiming::set_render_delay(int render_delay_ms) {
Markus Handell6deec382020-07-07 12:17:12 +020062 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000063 render_delay_ms_ = render_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +000064}
65
isheriff6b4b5f32016-06-08 00:24:21 -070066void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) {
Markus Handell6deec382020-07-07 12:17:12 +020067 MutexLock lock(&mutex_);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000068 min_playout_delay_ms_ = min_playout_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +000069}
70
isheriff6b4b5f32016-06-08 00:24:21 -070071int VCMTiming::min_playout_delay() {
Markus Handell6deec382020-07-07 12:17:12 +020072 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -070073 return min_playout_delay_ms_;
74}
75
76void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) {
Markus Handell6deec382020-07-07 12:17:12 +020077 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -070078 max_playout_delay_ms_ = max_playout_delay_ms;
79}
80
81int VCMTiming::max_playout_delay() {
Markus Handell6deec382020-07-07 12:17:12 +020082 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -070083 return max_playout_delay_ms_;
84}
85
86void VCMTiming::SetJitterDelay(int jitter_delay_ms) {
Markus Handell6deec382020-07-07 12:17:12 +020087 MutexLock lock(&mutex_);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000088 if (jitter_delay_ms != jitter_delay_ms_) {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000089 jitter_delay_ms_ = jitter_delay_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000090 // When in initial state, set current delay to minimum delay.
91 if (current_delay_ms_ == 0) {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000092 current_delay_ms_ = jitter_delay_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000093 }
94 }
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
96
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000097void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
Markus Handell6deec382020-07-07 12:17:12 +020098 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -070099 int target_delay_ms = TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000101 if (current_delay_ms_ == 0) {
102 // Not initialized, set current delay to target.
103 current_delay_ms_ = target_delay_ms;
104 } else if (target_delay_ms != current_delay_ms_) {
philipel5908c712015-12-21 08:23:20 -0800105 int64_t delay_diff_ms =
106 static_cast<int64_t>(target_delay_ms) - current_delay_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000107 // Never change the delay with more than 100 ms every second. If we're
108 // changing the delay in too large steps we will get noticeable freezes. By
109 // limiting the change we can increase the delay in smaller steps, which
110 // will be experienced as the video is played in slow motion. When lowering
111 // the delay the video will be played at a faster pace.
112 int64_t max_change_ms = 0;
113 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
114 // wrap
philipel5908c712015-12-21 08:23:20 -0800115 max_change_ms = kDelayMaxChangeMsPerS *
116 (frame_timestamp + (static_cast<int64_t>(1) << 32) -
117 prev_frame_timestamp_) /
118 90000;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000119 } else {
120 max_change_ms = kDelayMaxChangeMsPerS *
philipel5908c712015-12-21 08:23:20 -0800121 (frame_timestamp - prev_frame_timestamp_) / 90000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000122 }
philipelfd5a20f2016-11-15 00:57:57 -0800123
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000124 if (max_change_ms <= 0) {
Åsa Persson8368d1a2018-01-05 12:44:45 +0100125 // Any changes less than 1 ms are truncated and will be postponed.
126 // Negative change will be due to reordering and should be ignored.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000127 return;
128 }
129 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms);
130 delay_diff_ms = std::min(delay_diff_ms, max_change_ms);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000131
isheriff6b4b5f32016-06-08 00:24:21 -0700132 current_delay_ms_ = current_delay_ms_ + delay_diff_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000133 }
134 prev_frame_timestamp_ = frame_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135}
136
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000137void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms,
138 int64_t actual_decode_time_ms) {
Markus Handell6deec382020-07-07 12:17:12 +0200139 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000140 uint32_t target_delay_ms = TargetDelayInternal();
magjed2943f012016-03-22 05:12:09 -0700141 int64_t delayed_ms =
142 actual_decode_time_ms -
143 (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000144 if (delayed_ms < 0) {
145 return;
146 }
147 if (current_delay_ms_ + delayed_ms <= target_delay_ms) {
isheriff6b4b5f32016-06-08 00:24:21 -0700148 current_delay_ms_ += delayed_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000149 } else {
150 current_delay_ms_ = target_delay_ms;
151 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000152}
153
Johannes Kronbfd343b2019-07-01 10:07:50 +0200154void VCMTiming::StopDecodeTimer(uint32_t /*time_stamp*/,
Åsa Persson8368d1a2018-01-05 12:44:45 +0100155 int32_t decode_time_ms,
156 int64_t now_ms,
Johannes Kronbfd343b2019-07-01 10:07:50 +0200157 int64_t /*render_time_ms*/) {
158 StopDecodeTimer(decode_time_ms, now_ms);
159}
160
161void VCMTiming::StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms) {
Markus Handell6deec382020-07-07 12:17:12 +0200162 MutexLock lock(&mutex_);
magjed2943f012016-03-22 05:12:09 -0700163 codec_timer_->AddTiming(decode_time_ms, now_ms);
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200164 RTC_DCHECK_GE(decode_time_ms, 0);
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000165 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166}
167
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000168void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) {
Markus Handell6deec382020-07-07 12:17:12 +0200169 MutexLock lock(&mutex_);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000170 ts_extrapolator_->Update(now_ms, time_stamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171}
172
philipel5908c712015-12-21 08:23:20 -0800173int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp,
174 int64_t now_ms) const {
Markus Handell6deec382020-07-07 12:17:12 +0200175 MutexLock lock(&mutex_);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200176 return RenderTimeMsInternal(frame_timestamp, now_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177}
178
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700179void VCMTiming::SetLastDecodeScheduledTimestamp(
180 int64_t last_decode_scheduled_ts) {
181 MutexLock lock(&mutex_);
182 last_decode_scheduled_ts_ = last_decode_scheduled_ts;
183}
184
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000185int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp,
186 int64_t now_ms) const {
Johannes Kron111e9812020-10-26 13:54:40 +0100187 constexpr int kLowLatencyRendererMaxPlayoutDelayMs = 500;
188 if (min_playout_delay_ms_ == 0 &&
189 (max_playout_delay_ms_ == 0 ||
190 (low_latency_renderer_enabled_ &&
191 max_playout_delay_ms_ <= kLowLatencyRendererMaxPlayoutDelayMs))) {
192 // Render as soon as possible or with low-latency renderer algorithm.
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200193 return 0;
194 }
Niels Möller043725f2020-10-30 10:44:52 +0100195 // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const
196 // method; it mutates the object's wraparound state.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000197 int64_t estimated_complete_time_ms =
philipel5908c712015-12-21 08:23:20 -0800198 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000199 if (estimated_complete_time_ms == -1) {
200 estimated_complete_time_ms = now_ms;
201 }
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000202
Artem Titovdcd7fc72021-08-09 13:02:57 +0200203 // Make sure the actual delay stays in the range of `min_playout_delay_ms_`
204 // and `max_playout_delay_ms_`.
isheriff6b4b5f32016-06-08 00:24:21 -0700205 int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_);
206 actual_delay = std::min(actual_delay, max_playout_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000207 return estimated_complete_time_ms + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
isheriff6b4b5f32016-06-08 00:24:21 -0700210int VCMTiming::RequiredDecodeTimeMs() const {
211 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200212 RTC_DCHECK_GE(decode_time_ms, 0);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000213 return decode_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214}
215
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700216int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms,
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200217 int64_t now_ms,
218 bool too_many_frames_queued) const {
Markus Handell6deec382020-07-07 12:17:12 +0200219 MutexLock lock(&mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220
Johannes Kronb2745ba2021-08-13 10:24:04 +0200221 if (render_time_ms == 0 && zero_playout_delay_min_pacing_->us() > 0 &&
222 min_playout_delay_ms_ == 0 && max_playout_delay_ms_ > 0) {
Artem Titovdcd7fc72021-08-09 13:02:57 +0200223 // `render_time_ms` == 0 indicates that the frame should be decoded and
Johannes Kron985905d2021-06-29 11:37:06 +0200224 // rendered as soon as possible. However, the decoder can be choked if too
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200225 // many frames are sent at once. Therefore, limit the interframe delay to
226 // |zero_playout_delay_min_pacing_| unless too many frames are queued in
227 // which case the frames are sent to the decoder at once.
228 if (too_many_frames_queued) {
229 return 0;
230 }
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700231 int64_t earliest_next_decode_start_time =
232 last_decode_scheduled_ts_ + zero_playout_delay_min_pacing_->ms();
233 int64_t max_wait_time_ms = now_ms >= earliest_next_decode_start_time
Johannes Kron985905d2021-06-29 11:37:06 +0200234 ? 0
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700235 : earliest_next_decode_start_time - now_ms;
Johannes Kron985905d2021-06-29 11:37:06 +0200236 return max_wait_time_ms;
237 }
238 return render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000239}
240
isheriff6b4b5f32016-06-08 00:24:21 -0700241int VCMTiming::TargetVideoDelay() const {
Markus Handell6deec382020-07-07 12:17:12 +0200242 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000243 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
isheriff6b4b5f32016-06-08 00:24:21 -0700246int VCMTiming::TargetDelayInternal() const {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000247 return std::max(min_playout_delay_ms_,
isheriff6b4b5f32016-06-08 00:24:21 -0700248 jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000249}
250
Johannes Kronbfd343b2019-07-01 10:07:50 +0200251bool VCMTiming::GetTimings(int* max_decode_ms,
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000252 int* current_delay_ms,
253 int* target_delay_ms,
254 int* jitter_buffer_ms,
255 int* min_playout_delay_ms,
256 int* render_delay_ms) const {
Markus Handell6deec382020-07-07 12:17:12 +0200257 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -0700258 *max_decode_ms = RequiredDecodeTimeMs();
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000259 *current_delay_ms = current_delay_ms_;
260 *target_delay_ms = TargetDelayInternal();
261 *jitter_buffer_ms = jitter_delay_ms_;
262 *min_playout_delay_ms = min_playout_delay_ms_;
263 *render_delay_ms = render_delay_ms_;
asapersson8d560882016-12-22 01:26:18 -0800264 return (num_decoded_frames_ > 0);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000265}
266
ilnik2edc6842017-07-06 03:06:50 -0700267void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
Markus Handell6deec382020-07-07 12:17:12 +0200268 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700269 timing_frame_info_.emplace(info);
270}
271
Danil Chapovalov0040b662018-06-18 10:48:16 +0200272absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
Markus Handell6deec382020-07-07 12:17:12 +0200273 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700274 return timing_frame_info_;
275}
276
Johannes Kron111e9812020-10-26 13:54:40 +0100277void VCMTiming::SetMaxCompositionDelayInFrames(
278 absl::optional<int> max_composition_delay_in_frames) {
279 MutexLock lock(&mutex_);
280 max_composition_delay_in_frames_ = max_composition_delay_in_frames;
281}
282
283absl::optional<int> VCMTiming::MaxCompositionDelayInFrames() const {
284 MutexLock lock(&mutex_);
285 return max_composition_delay_in_frames_;
286}
287
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000288} // namespace webrtc