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" |
Evan Shrubsole | eabaf8d | 2022-05-23 17:45:58 +0200 | [diff] [blame] | 17 | #include "rtc_base/logging.h" |
Karl Wiberg | 76b7f51 | 2018-03-22 15:29:03 +0100 | [diff] [blame] | 18 | #include "rtc_base/time/timestamp_extrapolator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "system_wrappers/include/clock.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); |
Evan Shrubsole | f6adc64 | 2022-04-26 11:10:21 +0200 | [diff] [blame] | 26 | constexpr TimeDelta kLowLatencyRendererMaxPlayoutDelay = TimeDelta::Millis(500); |
| 27 | |
Evan Shrubsole | cc52f07 | 2022-05-24 15:00:00 +0200 | [diff] [blame^] | 28 | void CheckDelaysValid(TimeDelta min_delay, TimeDelta max_delay) { |
| 29 | if (min_delay > max_delay) { |
| 30 | RTC_LOG(LS_ERROR) |
| 31 | << "Playout delays set incorrectly: min playout delay (" << min_delay |
| 32 | << ") > max playout delay (" << max_delay |
| 33 | << "). This is undefined behaviour. Application writers should " |
| 34 | "ensure that the min delay is always less than or equals max " |
| 35 | "delay. If trying to use the playout delay header extensions " |
| 36 | "described in " |
| 37 | "https://webrtc.googlesource.com/src/+/refs/heads/main/docs/" |
| 38 | "native-code/rtp-hdrext/playout-delay/, be careful that a playout " |
| 39 | "delay hint or A/V sync settings may have caused this conflict."; |
| 40 | } |
| 41 | } |
| 42 | |
Johannes Kron | 23bfff3 | 2021-09-28 21:31:46 +0200 | [diff] [blame] | 43 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 45 | VCMTiming::VCMTiming(Clock* clock, const FieldTrialsView& field_trials) |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 46 | : clock_(clock), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 47 | ts_extrapolator_( |
| 48 | std::make_unique<TimestampExtrapolator>(clock_->CurrentTime())), |
Rasmus Brandt | 2377226 | 2022-05-23 09:53:15 +0200 | [diff] [blame] | 49 | codec_timer_(std::make_unique<CodecTimer>()), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 50 | render_delay_(kDefaultRenderDelay), |
| 51 | min_playout_delay_(TimeDelta::Zero()), |
| 52 | max_playout_delay_(TimeDelta::Seconds(10)), |
| 53 | jitter_delay_(TimeDelta::Zero()), |
| 54 | current_delay_(TimeDelta::Zero()), |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 55 | prev_frame_timestamp_(0), |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 56 | num_decoded_frames_(0), |
Johannes Kron | 23bfff3 | 2021-09-28 21:31:46 +0200 | [diff] [blame] | 57 | zero_playout_delay_min_pacing_("min_pacing", |
| 58 | kZeroPlayoutDelayDefaultMinPacing), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 59 | last_decode_scheduled_(Timestamp::Zero()) { |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 60 | ParseFieldTrial({&zero_playout_delay_min_pacing_}, |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 61 | field_trials.Lookup("WebRTC-ZeroPlayoutDelay")); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 64 | void VCMTiming::Reset() { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 65 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 66 | ts_extrapolator_->Reset(clock_->CurrentTime()); |
Rasmus Brandt | 2377226 | 2022-05-23 09:53:15 +0200 | [diff] [blame] | 67 | codec_timer_ = std::make_unique<CodecTimer>(); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 68 | render_delay_ = kDefaultRenderDelay; |
| 69 | min_playout_delay_ = TimeDelta::Zero(); |
| 70 | jitter_delay_ = TimeDelta::Zero(); |
| 71 | current_delay_ = TimeDelta::Zero(); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 72 | prev_frame_timestamp_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 75 | void VCMTiming::set_render_delay(TimeDelta render_delay) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 76 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 77 | render_delay_ = render_delay; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Evan Shrubsole | eabaf8d | 2022-05-23 17:45:58 +0200 | [diff] [blame] | 80 | TimeDelta VCMTiming::min_playout_delay() const { |
| 81 | MutexLock lock(&mutex_); |
| 82 | return min_playout_delay_; |
| 83 | } |
| 84 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 85 | void VCMTiming::set_min_playout_delay(TimeDelta min_playout_delay) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 86 | MutexLock lock(&mutex_); |
Evan Shrubsole | cc52f07 | 2022-05-24 15:00:00 +0200 | [diff] [blame^] | 87 | if (min_playout_delay_ != min_playout_delay) { |
| 88 | CheckDelaysValid(min_playout_delay, max_playout_delay_); |
| 89 | min_playout_delay_ = min_playout_delay; |
| 90 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 93 | void VCMTiming::set_max_playout_delay(TimeDelta max_playout_delay) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 94 | MutexLock lock(&mutex_); |
Evan Shrubsole | eabaf8d | 2022-05-23 17:45:58 +0200 | [diff] [blame] | 95 | if (max_playout_delay_ != max_playout_delay) { |
Evan Shrubsole | cc52f07 | 2022-05-24 15:00:00 +0200 | [diff] [blame^] | 96 | CheckDelaysValid(min_playout_delay_, max_playout_delay); |
Evan Shrubsole | eabaf8d | 2022-05-23 17:45:58 +0200 | [diff] [blame] | 97 | max_playout_delay_ = max_playout_delay; |
| 98 | } |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 101 | void VCMTiming::SetJitterDelay(TimeDelta jitter_delay) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 102 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 103 | if (jitter_delay != jitter_delay_) { |
| 104 | jitter_delay_ = jitter_delay; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 105 | // When in initial state, set current delay to minimum delay. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 106 | if (current_delay_.IsZero()) { |
| 107 | current_delay_ = jitter_delay_; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | } |
| 111 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 112 | void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 113 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 114 | TimeDelta target_delay = TargetDelayInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 116 | if (current_delay_.IsZero()) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 117 | // Not initialized, set current delay to target. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 118 | current_delay_ = target_delay; |
| 119 | } else if (target_delay != current_delay_) { |
| 120 | TimeDelta delay_diff = target_delay - current_delay_; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 121 | // Never change the delay with more than 100 ms every second. If we're |
| 122 | // changing the delay in too large steps we will get noticeable freezes. By |
| 123 | // limiting the change we can increase the delay in smaller steps, which |
| 124 | // will be experienced as the video is played in slow motion. When lowering |
| 125 | // the delay the video will be played at a faster pace. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 126 | TimeDelta max_change = TimeDelta::Zero(); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 127 | if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { |
| 128 | // wrap |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 129 | max_change = |
| 130 | TimeDelta::Millis(kDelayMaxChangeMsPerS * |
| 131 | (frame_timestamp + (static_cast<int64_t>(1) << 32) - |
| 132 | prev_frame_timestamp_) / |
| 133 | 90000); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 134 | } else { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 135 | max_change = |
| 136 | TimeDelta::Millis(kDelayMaxChangeMsPerS * |
| 137 | (frame_timestamp - prev_frame_timestamp_) / 90000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 139 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 140 | if (max_change <= TimeDelta::Zero()) { |
Ã…sa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 141 | // Any changes less than 1 ms are truncated and will be postponed. |
| 142 | // Negative change will be due to reordering and should be ignored. |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 143 | return; |
| 144 | } |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 145 | delay_diff = std::max(delay_diff, -max_change); |
| 146 | delay_diff = std::min(delay_diff, max_change); |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 147 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 148 | current_delay_ = current_delay_ + delay_diff; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 149 | } |
| 150 | prev_frame_timestamp_ = frame_timestamp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 153 | void VCMTiming::UpdateCurrentDelay(Timestamp render_time, |
| 154 | Timestamp actual_decode_time) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 155 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 156 | TimeDelta target_delay = TargetDelayInternal(); |
| 157 | TimeDelta delayed = |
| 158 | (actual_decode_time - render_time) + RequiredDecodeTime() + render_delay_; |
| 159 | if (delayed < TimeDelta::Zero()) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 160 | return; |
| 161 | } |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 162 | if (current_delay_ + delayed <= target_delay) { |
| 163 | current_delay_ += delayed; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 164 | } else { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 165 | current_delay_ = target_delay; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 166 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 169 | void VCMTiming::StopDecodeTimer(TimeDelta decode_time, Timestamp now) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 170 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 171 | codec_timer_->AddTiming(decode_time.ms(), now.ms()); |
| 172 | RTC_DCHECK_GE(decode_time, TimeDelta::Zero()); |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 173 | ++num_decoded_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 176 | void VCMTiming::IncomingTimestamp(uint32_t rtp_timestamp, Timestamp now) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 177 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 178 | ts_extrapolator_->Update(now, rtp_timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 181 | Timestamp VCMTiming::RenderTime(uint32_t frame_timestamp, Timestamp now) const { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 182 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 183 | return RenderTimeInternal(frame_timestamp, now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Rezaul Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 186 | void VCMTiming::SetLastDecodeScheduledTimestamp( |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 187 | Timestamp last_decode_scheduled) { |
Rezaul Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 188 | MutexLock lock(&mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 189 | last_decode_scheduled_ = last_decode_scheduled; |
Rezaul Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 192 | Timestamp VCMTiming::RenderTimeInternal(uint32_t frame_timestamp, |
| 193 | Timestamp now) const { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 194 | if (min_playout_delay_.IsZero() && |
| 195 | (max_playout_delay_.IsZero() || |
Evan Shrubsole | f6adc64 | 2022-04-26 11:10:21 +0200 | [diff] [blame] | 196 | max_playout_delay_ <= kLowLatencyRendererMaxPlayoutDelay)) { |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 197 | // Render as soon as possible or with low-latency renderer algorithm. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 198 | return Timestamp::Zero(); |
Stefan Holmer | 812ceaf | 2018-05-15 13:00:10 +0200 | [diff] [blame] | 199 | } |
Niels Möller | 043725f | 2020-10-30 10:44:52 +0100 | [diff] [blame] | 200 | // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const |
| 201 | // method; it mutates the object's wraparound state. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 202 | Timestamp estimated_complete_time = |
| 203 | ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp).value_or(now); |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 204 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 205 | // Make sure the actual delay stays in the range of `min_playout_delay_` |
| 206 | // and `max_playout_delay_`. |
| 207 | TimeDelta actual_delay = |
| 208 | current_delay_.Clamped(min_playout_delay_, max_playout_delay_); |
| 209 | return estimated_complete_time + actual_delay; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 212 | TimeDelta VCMTiming::RequiredDecodeTime() const { |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 213 | const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); |
Mirko Bonadei | 25ab322 | 2021-07-08 20:08:20 +0200 | [diff] [blame] | 214 | RTC_DCHECK_GE(decode_time_ms, 0); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 215 | return TimeDelta::Millis(decode_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 218 | TimeDelta VCMTiming::MaxWaitingTime(Timestamp render_time, |
| 219 | Timestamp now, |
| 220 | bool too_many_frames_queued) const { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 221 | MutexLock lock(&mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 222 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 223 | if (render_time.IsZero() && zero_playout_delay_min_pacing_->us() > 0 && |
| 224 | min_playout_delay_.IsZero() && max_playout_delay_ > TimeDelta::Zero()) { |
| 225 | // `render_time` == 0 indicates that the frame should be decoded and |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 226 | // 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] | 227 | // many frames are sent at once. Therefore, limit the interframe delay to |
| 228 | // |zero_playout_delay_min_pacing_| unless too many frames are queued in |
| 229 | // which case the frames are sent to the decoder at once. |
| 230 | if (too_many_frames_queued) { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 231 | return TimeDelta::Zero(); |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 232 | } |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 233 | Timestamp earliest_next_decode_start_time = |
| 234 | last_decode_scheduled_ + zero_playout_delay_min_pacing_; |
| 235 | TimeDelta max_wait_time = now >= earliest_next_decode_start_time |
| 236 | ? TimeDelta::Zero() |
| 237 | : earliest_next_decode_start_time - now; |
| 238 | return max_wait_time; |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 239 | } |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 240 | return render_time - now - RequiredDecodeTime() - render_delay_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 243 | TimeDelta VCMTiming::TargetVideoDelay() const { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 244 | MutexLock lock(&mutex_); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 245 | return TargetDelayInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 248 | TimeDelta VCMTiming::TargetDelayInternal() const { |
| 249 | return std::max(min_playout_delay_, |
| 250 | jitter_delay_ + RequiredDecodeTime() + render_delay_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Evan Shrubsole | 92e89d7 | 2022-03-22 10:55:15 +0100 | [diff] [blame] | 253 | VCMTiming::VideoDelayTimings VCMTiming::GetTimings() const { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 254 | MutexLock lock(&mutex_); |
Evan Shrubsole | 92e89d7 | 2022-03-22 10:55:15 +0100 | [diff] [blame] | 255 | return VideoDelayTimings{.max_decode_duration = RequiredDecodeTime(), |
| 256 | .current_delay = current_delay_, |
| 257 | .target_delay = TargetDelayInternal(), |
| 258 | .jitter_buffer_delay = jitter_delay_, |
| 259 | .min_playout_delay = min_playout_delay_, |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 260 | .max_playout_delay = max_playout_delay_, |
Evan Shrubsole | 92e89d7 | 2022-03-22 10:55:15 +0100 | [diff] [blame] | 261 | .render_delay = render_delay_, |
| 262 | .num_decoded_frames = num_decoded_frames_}; |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 263 | } |
| 264 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 265 | void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 266 | MutexLock lock(&mutex_); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 267 | timing_frame_info_.emplace(info); |
| 268 | } |
| 269 | |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 270 | absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 271 | MutexLock lock(&mutex_); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 272 | return timing_frame_info_; |
| 273 | } |
| 274 | |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 275 | void VCMTiming::SetMaxCompositionDelayInFrames( |
| 276 | absl::optional<int> max_composition_delay_in_frames) { |
| 277 | MutexLock lock(&mutex_); |
| 278 | max_composition_delay_in_frames_ = max_composition_delay_in_frames; |
| 279 | } |
| 280 | |
| 281 | absl::optional<int> VCMTiming::MaxCompositionDelayInFrames() const { |
| 282 | MutexLock lock(&mutex_); |
| 283 | return max_composition_delay_in_frames_; |
| 284 | } |
| 285 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 286 | } // namespace webrtc |