niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/timing.h" |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 12 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 15 | #include "api/units/time_delta.h" |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 16 | #include "rtc_base/experiments/field_trial_parser.h" |
Karl Wiberg | 76b7f51 | 2018-03-22 15:29:03 +0100 | [diff] [blame] | 17 | #include "rtc_base/time/timestamp_extrapolator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "system_wrappers/include/clock.h" |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 19 | #include "system_wrappers/include/field_trial.h" |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | namespace webrtc { |
Johannes Kron | 23bfff3 | 2021-09-28 21:31:46 +0200 | [diff] [blame] | 22 | namespace { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 23 | |
Johannes Kron | 23bfff3 | 2021-09-28 21:31:46 +0200 | [diff] [blame] | 24 | // Default pacing that is used for the low-latency renderer path. |
| 25 | constexpr TimeDelta kZeroPlayoutDelayDefaultMinPacing = TimeDelta::Millis(8); |
| 26 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
Niels Möller | 043725f | 2020-10-30 10:44:52 +0100 | [diff] [blame] | 28 | VCMTiming::VCMTiming(Clock* clock) |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 29 | : clock_(clock), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 30 | ts_extrapolator_( |
| 31 | std::make_unique<TimestampExtrapolator>(clock_->CurrentTime())), |
Niels Möller | 043725f | 2020-10-30 10:44:52 +0100 | [diff] [blame] | 32 | codec_timer_(std::make_unique<VCMCodecTimer>()), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 33 | 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()), |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 38 | prev_frame_timestamp_(0), |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 39 | num_decoded_frames_(0), |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 40 | low_latency_renderer_enabled_("enabled", true), |
Johannes Kron | 23bfff3 | 2021-09-28 21:31:46 +0200 | [diff] [blame] | 41 | zero_playout_delay_min_pacing_("min_pacing", |
| 42 | kZeroPlayoutDelayDefaultMinPacing), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 43 | last_decode_scheduled_(Timestamp::Zero()) { |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 44 | ParseFieldTrial({&low_latency_renderer_enabled_}, |
| 45 | field_trial::FindFullName("WebRTC-LowLatencyRenderer")); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 46 | ParseFieldTrial({&zero_playout_delay_min_pacing_}, |
| 47 | field_trial::FindFullName("WebRTC-ZeroPlayoutDelay")); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | } |
| 49 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 50 | void VCMTiming::Reset() { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 51 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 52 | ts_extrapolator_->Reset(clock_->CurrentTime()); |
Niels Möller | 043725f | 2020-10-30 10:44:52 +0100 | [diff] [blame] | 53 | codec_timer_ = std::make_unique<VCMCodecTimer>(); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 54 | render_delay_ = kDefaultRenderDelay; |
| 55 | min_playout_delay_ = TimeDelta::Zero(); |
| 56 | jitter_delay_ = TimeDelta::Zero(); |
| 57 | current_delay_ = TimeDelta::Zero(); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 58 | prev_frame_timestamp_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 61 | void VCMTiming::set_render_delay(TimeDelta render_delay) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 62 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 63 | render_delay_ = render_delay; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 66 | void VCMTiming::set_min_playout_delay(TimeDelta min_playout_delay) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 67 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 68 | min_playout_delay_ = min_playout_delay; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 71 | void VCMTiming::set_max_playout_delay(TimeDelta max_playout_delay) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 72 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 73 | max_playout_delay_ = max_playout_delay; |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 76 | void VCMTiming::SetJitterDelay(TimeDelta jitter_delay) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 77 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 78 | if (jitter_delay != jitter_delay_) { |
| 79 | jitter_delay_ = jitter_delay; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 80 | // When in initial state, set current delay to minimum delay. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 81 | if (current_delay_.IsZero()) { |
| 82 | current_delay_ = jitter_delay_; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | } |
| 86 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 87 | void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 88 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 89 | TimeDelta target_delay = TargetDelayInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 91 | if (current_delay_.IsZero()) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 92 | // Not initialized, set current delay to target. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 93 | current_delay_ = target_delay; |
| 94 | } else if (target_delay != current_delay_) { |
| 95 | TimeDelta delay_diff = target_delay - current_delay_; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 96 | // 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 Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 101 | TimeDelta max_change = TimeDelta::Zero(); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 102 | if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { |
| 103 | // wrap |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 104 | max_change = |
| 105 | TimeDelta::Millis(kDelayMaxChangeMsPerS * |
| 106 | (frame_timestamp + (static_cast<int64_t>(1) << 32) - |
| 107 | prev_frame_timestamp_) / |
| 108 | 90000); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 109 | } else { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 110 | max_change = |
| 111 | TimeDelta::Millis(kDelayMaxChangeMsPerS * |
| 112 | (frame_timestamp - prev_frame_timestamp_) / 90000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 114 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 115 | if (max_change <= TimeDelta::Zero()) { |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 116 | // 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.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 120 | delay_diff = std::max(delay_diff, -max_change); |
| 121 | delay_diff = std::min(delay_diff, max_change); |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 122 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 123 | current_delay_ = current_delay_ + delay_diff; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 124 | } |
| 125 | prev_frame_timestamp_ = frame_timestamp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 128 | void VCMTiming::UpdateCurrentDelay(Timestamp render_time, |
| 129 | Timestamp actual_decode_time) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 130 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 131 | TimeDelta target_delay = TargetDelayInternal(); |
| 132 | TimeDelta delayed = |
| 133 | (actual_decode_time - render_time) + RequiredDecodeTime() + render_delay_; |
| 134 | if (delayed < TimeDelta::Zero()) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 135 | return; |
| 136 | } |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 137 | if (current_delay_ + delayed <= target_delay) { |
| 138 | current_delay_ += delayed; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 139 | } else { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 140 | current_delay_ = target_delay; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 141 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 144 | void VCMTiming::StopDecodeTimer(TimeDelta decode_time, Timestamp now) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 145 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 146 | codec_timer_->AddTiming(decode_time.ms(), now.ms()); |
| 147 | RTC_DCHECK_GE(decode_time, TimeDelta::Zero()); |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 148 | ++num_decoded_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 151 | void VCMTiming::IncomingTimestamp(uint32_t rtp_timestamp, Timestamp now) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 152 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 153 | ts_extrapolator_->Update(now, rtp_timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 156 | Timestamp VCMTiming::RenderTime(uint32_t frame_timestamp, Timestamp now) const { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 157 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 158 | return RenderTimeInternal(frame_timestamp, now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Rezaul Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 161 | void VCMTiming::SetLastDecodeScheduledTimestamp( |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 162 | Timestamp last_decode_scheduled) { |
Rezaul Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 163 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 164 | last_decode_scheduled_ = last_decode_scheduled; |
Rezaul Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 167 | Timestamp 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 Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 173 | (low_latency_renderer_enabled_ && |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 174 | max_playout_delay_ <= kLowLatencyRendererMaxPlayoutDelay))) { |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 175 | // Render as soon as possible or with low-latency renderer algorithm. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 176 | return Timestamp::Zero(); |
Stefan Holmer | 812ceaf | 2018-05-15 13:00:10 +0200 | [diff] [blame] | 177 | } |
Niels Möller | 043725f | 2020-10-30 10:44:52 +0100 | [diff] [blame] | 178 | // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const |
| 179 | // method; it mutates the object's wraparound state. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 180 | Timestamp estimated_complete_time = |
| 181 | ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp).value_or(now); |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 182 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 183 | // 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 190 | TimeDelta VCMTiming::RequiredDecodeTime() const { |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 191 | const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); |
Mirko Bonadei | 25ab322 | 2021-07-08 20:08:20 +0200 | [diff] [blame] | 192 | RTC_DCHECK_GE(decode_time_ms, 0); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 193 | return TimeDelta::Millis(decode_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 196 | TimeDelta VCMTiming::MaxWaitingTime(Timestamp render_time, |
| 197 | Timestamp now, |
| 198 | bool too_many_frames_queued) const { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 199 | MutexLock lock(&mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 200 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 201 | 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 Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 204 | // rendered as soon as possible. However, the decoder can be choked if too |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 205 | // 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 Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 209 | return TimeDelta::Zero(); |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 210 | } |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 211 | 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 Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 217 | } |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 218 | return render_time - now - RequiredDecodeTime() - render_delay_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 221 | TimeDelta VCMTiming::TargetVideoDelay() const { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 222 | MutexLock lock(&mutex_); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 223 | return TargetDelayInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 226 | TimeDelta VCMTiming::TargetDelayInternal() const { |
| 227 | return std::max(min_playout_delay_, |
| 228 | jitter_delay_ + RequiredDecodeTime() + render_delay_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Evan Shrubsole | 92e89d7 | 2022-03-22 10:55:15 +0100 | [diff] [blame] | 231 | VCMTiming::VideoDelayTimings VCMTiming::GetTimings() const { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 232 | MutexLock lock(&mutex_); |
Evan Shrubsole | 92e89d7 | 2022-03-22 10:55:15 +0100 | [diff] [blame] | 233 | 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 Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame^] | 238 | .max_playout_delay = max_playout_delay_, |
Evan Shrubsole | 92e89d7 | 2022-03-22 10:55:15 +0100 | [diff] [blame] | 239 | .render_delay = render_delay_, |
| 240 | .num_decoded_frames = num_decoded_frames_}; |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 241 | } |
| 242 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 243 | void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 244 | MutexLock lock(&mutex_); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 245 | timing_frame_info_.emplace(info); |
| 246 | } |
| 247 | |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 248 | absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 249 | MutexLock lock(&mutex_); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 250 | return timing_frame_info_; |
| 251 | } |
| 252 | |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 253 | void 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 | |
| 259 | absl::optional<int> VCMTiming::MaxCompositionDelayInFrames() const { |
| 260 | MutexLock lock(&mutex_); |
| 261 | return max_composition_delay_in_frames_; |
| 262 | } |
| 263 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 264 | } // namespace webrtc |