blob: b4a712b8b840a0b4290b291596812eab8e5e24d0 [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 {
22
Niels Möller043725f2020-10-30 10:44:52 +010023VCMTiming::VCMTiming(Clock* clock)
ilnik2edc6842017-07-06 03:06:50 -070024 : clock_(clock),
Niels Möller043725f2020-10-30 10:44:52 +010025 ts_extrapolator_(std::make_unique<TimestampExtrapolator>(
26 clock_->TimeInMilliseconds())),
27 codec_timer_(std::make_unique<VCMCodecTimer>()),
ilnik2edc6842017-07-06 03:06:50 -070028 render_delay_ms_(kDefaultRenderDelayMs),
29 min_playout_delay_ms_(0),
30 max_playout_delay_ms_(10000),
31 jitter_delay_ms_(0),
32 current_delay_ms_(0),
ilnik2edc6842017-07-06 03:06:50 -070033 prev_frame_timestamp_(0),
34 timing_frame_info_(),
Johannes Kron111e9812020-10-26 13:54:40 +010035 num_decoded_frames_(0),
Johannes Kron985905d2021-06-29 11:37:06 +020036 low_latency_renderer_enabled_("enabled", true),
37 zero_playout_delay_min_pacing_("min_pacing", TimeDelta::Millis(0)),
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -070038 last_decode_scheduled_ts_(0) {
Johannes Kron111e9812020-10-26 13:54:40 +010039 ParseFieldTrial({&low_latency_renderer_enabled_},
40 field_trial::FindFullName("WebRTC-LowLatencyRenderer"));
Johannes Kron985905d2021-06-29 11:37:06 +020041 ParseFieldTrial({&zero_playout_delay_min_pacing_},
42 field_trial::FindFullName("WebRTC-ZeroPlayoutDelay"));
niklase@google.com470e71d2011-07-07 08:21:25 +000043}
44
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000045void VCMTiming::Reset() {
Markus Handell6deec382020-07-07 12:17:12 +020046 MutexLock lock(&mutex_);
wu@webrtc.orged4cb562014-05-06 04:50:49 +000047 ts_extrapolator_->Reset(clock_->TimeInMilliseconds());
Niels Möller043725f2020-10-30 10:44:52 +010048 codec_timer_ = std::make_unique<VCMCodecTimer>();
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000049 render_delay_ms_ = kDefaultRenderDelayMs;
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000050 min_playout_delay_ms_ = 0;
51 jitter_delay_ms_ = 0;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000052 current_delay_ms_ = 0;
53 prev_frame_timestamp_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
55
isheriff6b4b5f32016-06-08 00:24:21 -070056void VCMTiming::set_render_delay(int render_delay_ms) {
Markus Handell6deec382020-07-07 12:17:12 +020057 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000058 render_delay_ms_ = render_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
isheriff6b4b5f32016-06-08 00:24:21 -070061void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) {
Markus Handell6deec382020-07-07 12:17:12 +020062 MutexLock lock(&mutex_);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000063 min_playout_delay_ms_ = min_playout_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +000064}
65
isheriff6b4b5f32016-06-08 00:24:21 -070066int VCMTiming::min_playout_delay() {
Markus Handell6deec382020-07-07 12:17:12 +020067 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -070068 return min_playout_delay_ms_;
69}
70
71void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) {
Markus Handell6deec382020-07-07 12:17:12 +020072 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -070073 max_playout_delay_ms_ = max_playout_delay_ms;
74}
75
76int VCMTiming::max_playout_delay() {
Markus Handell6deec382020-07-07 12:17:12 +020077 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -070078 return max_playout_delay_ms_;
79}
80
81void VCMTiming::SetJitterDelay(int jitter_delay_ms) {
Markus Handell6deec382020-07-07 12:17:12 +020082 MutexLock lock(&mutex_);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000083 if (jitter_delay_ms != jitter_delay_ms_) {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000084 jitter_delay_ms_ = jitter_delay_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000085 // When in initial state, set current delay to minimum delay.
86 if (current_delay_ms_ == 0) {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000087 current_delay_ms_ = jitter_delay_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000088 }
89 }
niklase@google.com470e71d2011-07-07 08:21:25 +000090}
91
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000092void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
Markus Handell6deec382020-07-07 12:17:12 +020093 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -070094 int target_delay_ms = TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +000095
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000096 if (current_delay_ms_ == 0) {
97 // Not initialized, set current delay to target.
98 current_delay_ms_ = target_delay_ms;
99 } else if (target_delay_ms != current_delay_ms_) {
philipel5908c712015-12-21 08:23:20 -0800100 int64_t delay_diff_ms =
101 static_cast<int64_t>(target_delay_ms) - current_delay_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000102 // Never change the delay with more than 100 ms every second. If we're
103 // changing the delay in too large steps we will get noticeable freezes. By
104 // limiting the change we can increase the delay in smaller steps, which
105 // will be experienced as the video is played in slow motion. When lowering
106 // the delay the video will be played at a faster pace.
107 int64_t max_change_ms = 0;
108 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
109 // wrap
philipel5908c712015-12-21 08:23:20 -0800110 max_change_ms = kDelayMaxChangeMsPerS *
111 (frame_timestamp + (static_cast<int64_t>(1) << 32) -
112 prev_frame_timestamp_) /
113 90000;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000114 } else {
115 max_change_ms = kDelayMaxChangeMsPerS *
philipel5908c712015-12-21 08:23:20 -0800116 (frame_timestamp - prev_frame_timestamp_) / 90000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117 }
philipelfd5a20f2016-11-15 00:57:57 -0800118
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000119 if (max_change_ms <= 0) {
Åsa Persson8368d1a2018-01-05 12:44:45 +0100120 // Any changes less than 1 ms are truncated and will be postponed.
121 // Negative change will be due to reordering and should be ignored.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000122 return;
123 }
124 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms);
125 delay_diff_ms = std::min(delay_diff_ms, max_change_ms);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000126
isheriff6b4b5f32016-06-08 00:24:21 -0700127 current_delay_ms_ = current_delay_ms_ + delay_diff_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000128 }
129 prev_frame_timestamp_ = frame_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130}
131
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000132void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms,
133 int64_t actual_decode_time_ms) {
Markus Handell6deec382020-07-07 12:17:12 +0200134 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000135 uint32_t target_delay_ms = TargetDelayInternal();
magjed2943f012016-03-22 05:12:09 -0700136 int64_t delayed_ms =
137 actual_decode_time_ms -
138 (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000139 if (delayed_ms < 0) {
140 return;
141 }
142 if (current_delay_ms_ + delayed_ms <= target_delay_ms) {
isheriff6b4b5f32016-06-08 00:24:21 -0700143 current_delay_ms_ += delayed_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000144 } else {
145 current_delay_ms_ = target_delay_ms;
146 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000147}
148
Johannes Kronbfd343b2019-07-01 10:07:50 +0200149void VCMTiming::StopDecodeTimer(uint32_t /*time_stamp*/,
Åsa Persson8368d1a2018-01-05 12:44:45 +0100150 int32_t decode_time_ms,
151 int64_t now_ms,
Johannes Kronbfd343b2019-07-01 10:07:50 +0200152 int64_t /*render_time_ms*/) {
153 StopDecodeTimer(decode_time_ms, now_ms);
154}
155
156void VCMTiming::StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms) {
Markus Handell6deec382020-07-07 12:17:12 +0200157 MutexLock lock(&mutex_);
magjed2943f012016-03-22 05:12:09 -0700158 codec_timer_->AddTiming(decode_time_ms, now_ms);
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200159 RTC_DCHECK_GE(decode_time_ms, 0);
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000160 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000163void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) {
Markus Handell6deec382020-07-07 12:17:12 +0200164 MutexLock lock(&mutex_);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000165 ts_extrapolator_->Update(now_ms, time_stamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166}
167
philipel5908c712015-12-21 08:23:20 -0800168int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp,
169 int64_t now_ms) const {
Markus Handell6deec382020-07-07 12:17:12 +0200170 MutexLock lock(&mutex_);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200171 return RenderTimeMsInternal(frame_timestamp, now_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172}
173
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700174void VCMTiming::SetLastDecodeScheduledTimestamp(
175 int64_t last_decode_scheduled_ts) {
176 MutexLock lock(&mutex_);
177 last_decode_scheduled_ts_ = last_decode_scheduled_ts;
178}
179
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000180int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp,
181 int64_t now_ms) const {
Johannes Kron111e9812020-10-26 13:54:40 +0100182 constexpr int kLowLatencyRendererMaxPlayoutDelayMs = 500;
183 if (min_playout_delay_ms_ == 0 &&
184 (max_playout_delay_ms_ == 0 ||
185 (low_latency_renderer_enabled_ &&
186 max_playout_delay_ms_ <= kLowLatencyRendererMaxPlayoutDelayMs))) {
187 // Render as soon as possible or with low-latency renderer algorithm.
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200188 return 0;
189 }
Niels Möller043725f2020-10-30 10:44:52 +0100190 // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const
191 // method; it mutates the object's wraparound state.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000192 int64_t estimated_complete_time_ms =
philipel5908c712015-12-21 08:23:20 -0800193 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000194 if (estimated_complete_time_ms == -1) {
195 estimated_complete_time_ms = now_ms;
196 }
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000197
Artem Titovdcd7fc72021-08-09 13:02:57 +0200198 // Make sure the actual delay stays in the range of `min_playout_delay_ms_`
199 // and `max_playout_delay_ms_`.
isheriff6b4b5f32016-06-08 00:24:21 -0700200 int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_);
201 actual_delay = std::min(actual_delay, max_playout_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000202 return estimated_complete_time_ms + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000203}
204
isheriff6b4b5f32016-06-08 00:24:21 -0700205int VCMTiming::RequiredDecodeTimeMs() const {
206 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200207 RTC_DCHECK_GE(decode_time_ms, 0);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000208 return decode_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209}
210
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700211int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms,
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200212 int64_t now_ms,
213 bool too_many_frames_queued) const {
Markus Handell6deec382020-07-07 12:17:12 +0200214 MutexLock lock(&mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000215
Johannes Kronb2745ba2021-08-13 10:24:04 +0200216 if (render_time_ms == 0 && zero_playout_delay_min_pacing_->us() > 0 &&
217 min_playout_delay_ms_ == 0 && max_playout_delay_ms_ > 0) {
Artem Titovdcd7fc72021-08-09 13:02:57 +0200218 // `render_time_ms` == 0 indicates that the frame should be decoded and
Johannes Kron985905d2021-06-29 11:37:06 +0200219 // rendered as soon as possible. However, the decoder can be choked if too
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200220 // many frames are sent at once. Therefore, limit the interframe delay to
221 // |zero_playout_delay_min_pacing_| unless too many frames are queued in
222 // which case the frames are sent to the decoder at once.
223 if (too_many_frames_queued) {
224 return 0;
225 }
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700226 int64_t earliest_next_decode_start_time =
227 last_decode_scheduled_ts_ + zero_playout_delay_min_pacing_->ms();
228 int64_t max_wait_time_ms = now_ms >= earliest_next_decode_start_time
Johannes Kron985905d2021-06-29 11:37:06 +0200229 ? 0
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700230 : earliest_next_decode_start_time - now_ms;
Johannes Kron985905d2021-06-29 11:37:06 +0200231 return max_wait_time_ms;
232 }
233 return render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
235
isheriff6b4b5f32016-06-08 00:24:21 -0700236int VCMTiming::TargetVideoDelay() const {
Markus Handell6deec382020-07-07 12:17:12 +0200237 MutexLock lock(&mutex_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000238 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000239}
240
isheriff6b4b5f32016-06-08 00:24:21 -0700241int VCMTiming::TargetDelayInternal() const {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000242 return std::max(min_playout_delay_ms_,
isheriff6b4b5f32016-06-08 00:24:21 -0700243 jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
Johannes Kronbfd343b2019-07-01 10:07:50 +0200246bool VCMTiming::GetTimings(int* max_decode_ms,
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000247 int* current_delay_ms,
248 int* target_delay_ms,
249 int* jitter_buffer_ms,
250 int* min_playout_delay_ms,
251 int* render_delay_ms) const {
Markus Handell6deec382020-07-07 12:17:12 +0200252 MutexLock lock(&mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -0700253 *max_decode_ms = RequiredDecodeTimeMs();
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000254 *current_delay_ms = current_delay_ms_;
255 *target_delay_ms = TargetDelayInternal();
256 *jitter_buffer_ms = jitter_delay_ms_;
257 *min_playout_delay_ms = min_playout_delay_ms_;
258 *render_delay_ms = render_delay_ms_;
asapersson8d560882016-12-22 01:26:18 -0800259 return (num_decoded_frames_ > 0);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000260}
261
ilnik2edc6842017-07-06 03:06:50 -0700262void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
Markus Handell6deec382020-07-07 12:17:12 +0200263 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700264 timing_frame_info_.emplace(info);
265}
266
Danil Chapovalov0040b662018-06-18 10:48:16 +0200267absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
Markus Handell6deec382020-07-07 12:17:12 +0200268 MutexLock lock(&mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700269 return timing_frame_info_;
270}
271
Johannes Kron111e9812020-10-26 13:54:40 +0100272void VCMTiming::SetMaxCompositionDelayInFrames(
273 absl::optional<int> max_composition_delay_in_frames) {
274 MutexLock lock(&mutex_);
275 max_composition_delay_in_frames_ = max_composition_delay_in_frames;
276}
277
278absl::optional<int> VCMTiming::MaxCompositionDelayInFrames() const {
279 MutexLock lock(&mutex_);
280 return max_composition_delay_in_frames_;
281}
282
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000283} // namespace webrtc