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