| 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 |
| 11 | #include "modules/video_coding/timing.h" |
| 12 |
| 13 |
| 14 | #include <algorithm> |
| 15 |
| 16 | #include "rtc_base/experiments/field_trial_parser.h" |
| 17 | #include "rtc_base/time/timestamp_extrapolator.h" |
| 18 | #include "system_wrappers/include/clock.h" |
| 19 | #include "system_wrappers/include/field_trial.h" |
| 20 |
| 21 | namespace webrtc { |
| 22 | namespace { |
| 23 | // Default pacing that is used for the low-latency renderer path. |
| 24 | constexpr TimeDelta kZeroPlayoutDelayDefaultMinPacing = TimeDelta::Millis(8); |
| 25 | } // namespace |
| 26 |
| 27 | VCMTiming::VCMTiming(Clock* clock) |
| 28 | : clock_(clock), |
| 29 | ts_extrapolator_(std::make_unique<TimestampExtrapolator>( |
| 30 | clock_->TimeInMilliseconds())), |
| 31 | codec_timer_(std::make_unique<VCMCodecTimer>()), |
| 32 | 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), |
| 37 | prev_frame_timestamp_(0), |
| 38 | timing_frame_info_(), |
| 39 | num_decoded_frames_(0), |
| 40 | low_latency_renderer_enabled_("enabled", true), |
| 41 | zero_playout_delay_min_pacing_("min_pacing", |
| 42 | kZeroPlayoutDelayDefaultMinPacing), |
| 43 | last_decode_scheduled_ts_(0) { |
| 44 | ParseFieldTrial({&low_latency_renderer_enabled_}, |
| 45 | field_trial::FindFullName("WebRTC-LowLatencyRenderer")); |
| 46 | ParseFieldTrial({&zero_playout_delay_min_pacing_}, |
| 47 | field_trial::FindFullName("WebRTC-ZeroPlayoutDelay")); |
| 48 | } |
| 49 |
| 50 | void VCMTiming::Reset() { |
| 51 | MutexLock lock(&mutex_); |
| 52 | ts_extrapolator_->Reset(clock_->TimeInMilliseconds()); |
| 53 | codec_timer_ = std::make_unique<VCMCodecTimer>(); |
| 54 | render_delay_ms_ = kDefaultRenderDelayMs; |
| 55 | min_playout_delay_ms_ = 0; |
| 56 | jitter_delay_ms_ = 0; |
| 57 | current_delay_ms_ = 0; |
| 58 | prev_frame_timestamp_ = 0; |
| 59 | } |
| 60 |
| 61 | void VCMTiming::set_render_delay(int render_delay_ms) { |
| 62 | MutexLock lock(&mutex_); |
| 63 | render_delay_ms_ = render_delay_ms; |
| 64 | } |
| 65 |
| 66 | void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) { |
| 67 | MutexLock lock(&mutex_); |
| 68 | min_playout_delay_ms_ = min_playout_delay_ms; |
| 69 | } |
| 70 |
| 71 | int VCMTiming::min_playout_delay() { |
| 72 | MutexLock lock(&mutex_); |
| 73 | return min_playout_delay_ms_; |
| 74 | } |
| 75 |
| 76 | void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) { |
| 77 | MutexLock lock(&mutex_); |
| 78 | max_playout_delay_ms_ = max_playout_delay_ms; |
| 79 | } |
| 80 |
| 81 | int VCMTiming::max_playout_delay() { |
| 82 | MutexLock lock(&mutex_); |
| 83 | return max_playout_delay_ms_; |
| 84 | } |
| 85 |
| 86 | void VCMTiming::SetJitterDelay(int jitter_delay_ms) { |
| 87 | MutexLock lock(&mutex_); |
| 88 | if (jitter_delay_ms != jitter_delay_ms_) { |
| 89 | jitter_delay_ms_ = jitter_delay_ms; |
| 90 | // When in initial state, set current delay to minimum delay. |
| 91 | if (current_delay_ms_ == 0) { |
| 92 | current_delay_ms_ = jitter_delay_ms_; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 |
| 97 | void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
| 98 | MutexLock lock(&mutex_); |
| 99 | int target_delay_ms = TargetDelayInternal(); |
| 100 |
| 101 | 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_) { |
| 105 | int64_t delay_diff_ms = |
| 106 | static_cast<int64_t>(target_delay_ms) - current_delay_ms_; |
| 107 | // 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 |
| 115 | max_change_ms = kDelayMaxChangeMsPerS * |
| 116 | (frame_timestamp + (static_cast<int64_t>(1) << 32) - |
| 117 | prev_frame_timestamp_) / |
| 118 | 90000; |
| 119 | } else { |
| 120 | max_change_ms = kDelayMaxChangeMsPerS * |
| 121 | (frame_timestamp - prev_frame_timestamp_) / 90000; |
| 122 | } |
| 123 |
| 124 | if (max_change_ms <= 0) { |
| 125 | // Any changes less than 1 ms are truncated and will be postponed. |
| 126 | // Negative change will be due to reordering and should be ignored. |
| 127 | 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); |
| 131 |
| 132 | current_delay_ms_ = current_delay_ms_ + delay_diff_ms; |
| 133 | } |
| 134 | prev_frame_timestamp_ = frame_timestamp; |
| 135 | } |
| 136 |
| 137 | void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, |
| 138 | int64_t actual_decode_time_ms) { |
| 139 | MutexLock lock(&mutex_); |
| 140 | uint32_t target_delay_ms = TargetDelayInternal(); |
| 141 | int64_t delayed_ms = |
| 142 | actual_decode_time_ms - |
| 143 | (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_); |
| 144 | if (delayed_ms < 0) { |
| 145 | return; |
| 146 | } |
| 147 | if (current_delay_ms_ + delayed_ms <= target_delay_ms) { |
| 148 | current_delay_ms_ += delayed_ms; |
| 149 | } else { |
| 150 | current_delay_ms_ = target_delay_ms; |
| 151 | } |
| 152 | } |
| 153 |
| 154 | void VCMTiming::StopDecodeTimer(uint32_t /*time_stamp*/, |
| 155 | int32_t decode_time_ms, |
| 156 | int64_t now_ms, |
| 157 | int64_t /*render_time_ms*/) { |
| 158 | StopDecodeTimer(decode_time_ms, now_ms); |
| 159 | } |
| 160 |
| 161 | void VCMTiming::StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms) { |
| 162 | MutexLock lock(&mutex_); |
| 163 | codec_timer_->AddTiming(decode_time_ms, now_ms); |
| 164 | RTC_DCHECK_GE(decode_time_ms, 0); |
| 165 | ++num_decoded_frames_; |
| 166 | } |
| 167 |
| 168 | void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { |
| 169 | MutexLock lock(&mutex_); |
| 170 | ts_extrapolator_->Update(now_ms, time_stamp); |
| 171 | } |
| 172 |
| 173 | int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, |
| 174 | int64_t now_ms) const { |
| 175 | MutexLock lock(&mutex_); |
| 176 | return RenderTimeMsInternal(frame_timestamp, now_ms); |
| 177 | } |
| 178 |
| 179 | void VCMTiming::SetLastDecodeScheduledTimestamp( |
| 180 | int64_t last_decode_scheduled_ts) { |
| 181 | MutexLock lock(&mutex_); |
| 182 | last_decode_scheduled_ts_ = last_decode_scheduled_ts; |
| 183 | } |
| 184 |
| 185 | int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, |
| 186 | int64_t now_ms) const { |
| 187 | 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. |
| 193 | return 0; |
| 194 | } |
| 195 | // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const |
| 196 | // method; it mutates the object's wraparound state. |
| 197 | int64_t estimated_complete_time_ms = |
| 198 | ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); |
| 199 | if (estimated_complete_time_ms == -1) { |
| 200 | estimated_complete_time_ms = now_ms; |
| 201 | } |
| 202 |
| 203 | // Make sure the actual delay stays in the range of `min_playout_delay_ms_` |
| 204 | // and `max_playout_delay_ms_`. |
| 205 | int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_); |
| 206 | actual_delay = std::min(actual_delay, max_playout_delay_ms_); |
| 207 | return estimated_complete_time_ms + actual_delay; |
| 208 | } |
| 209 |
| 210 | int VCMTiming::RequiredDecodeTimeMs() const { |
| 211 | const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); |
| 212 | RTC_DCHECK_GE(decode_time_ms, 0); |
| 213 | return decode_time_ms; |
| 214 | } |
| 215 |
| 216 | int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, |
| 217 | int64_t now_ms, |
| 218 | bool too_many_frames_queued) const { |
| 219 | MutexLock lock(&mutex_); |
| 220 |
| 221 | if (render_time_ms == 0 && zero_playout_delay_min_pacing_->us() > 0 && |
| 222 | min_playout_delay_ms_ == 0 && max_playout_delay_ms_ > 0) { |
| 223 | // `render_time_ms` == 0 indicates that the frame should be decoded and |
| 224 | // rendered as soon as possible. However, the decoder can be choked if too |
| 225 | // 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 | } |
| 231 | 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 |
| 234 | ? 0 |
| 235 | : earliest_next_decode_start_time - now_ms; |
| 236 | return max_wait_time_ms; |
| 237 | } |
| 238 | return render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_; |
| 239 | } |
| 240 |
| 241 | int VCMTiming::TargetVideoDelay() const { |
| 242 | MutexLock lock(&mutex_); |
| 243 | return TargetDelayInternal(); |
| 244 | } |
| 245 |
| 246 | int VCMTiming::TargetDelayInternal() const { |
| 247 | return std::max(min_playout_delay_ms_, |
| 248 | jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_); |
| 249 | } |
| 250 |
| 251 | bool VCMTiming::GetTimings(int* max_decode_ms, |
| 252 | 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 { |
| 257 | MutexLock lock(&mutex_); |
| 258 | *max_decode_ms = RequiredDecodeTimeMs(); |
| 259 | *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_; |
| 264 | return (num_decoded_frames_ > 0); |
| 265 | } |
| 266 |
| 267 | void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) { |
| 268 | MutexLock lock(&mutex_); |
| 269 | timing_frame_info_.emplace(info); |
| 270 | } |
| 271 |
| 272 | absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() { |
| 273 | MutexLock lock(&mutex_); |
| 274 | return timing_frame_info_; |
| 275 | } |
| 276 |
| 277 | void 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 |
| 283 | absl::optional<int> VCMTiming::MaxCompositionDelayInFrames() const { |
| 284 | MutexLock lock(&mutex_); |
| 285 | return max_composition_delay_in_frames_; |
| 286 | } |
| 287 |
| 288 | } // namespace webrtc |
| 289 |