| 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 | #include <assert.h> |
| 14 |
| 15 | #include <algorithm> |
| 16 |
| 17 | #include "rtc_base/experiments/field_trial_parser.h" |
| 18 | #include "rtc_base/time/timestamp_extrapolator.h" |
| 19 | #include "system_wrappers/include/clock.h" |
| 20 | #include "system_wrappers/include/field_trial.h" |
| 21 |
| 22 | namespace webrtc { |
| 23 |
| 24 | VCMTiming::VCMTiming(Clock* clock) |
| 25 | : clock_(clock), |
| 26 | ts_extrapolator_(std::make_unique<TimestampExtrapolator>( |
| 27 | clock_->TimeInMilliseconds())), |
| 28 | codec_timer_(std::make_unique<VCMCodecTimer>()), |
| 29 | render_delay_ms_(kDefaultRenderDelayMs), |
| 30 | min_playout_delay_ms_(0), |
| 31 | max_playout_delay_ms_(10000), |
| 32 | jitter_delay_ms_(0), |
| 33 | current_delay_ms_(0), |
| 34 | prev_frame_timestamp_(0), |
| 35 | timing_frame_info_(), |
| 36 | num_decoded_frames_(0), |
| 37 | low_latency_renderer_enabled_("enabled", true) { |
| 38 | ParseFieldTrial({&low_latency_renderer_enabled_}, |
| 39 | field_trial::FindFullName("WebRTC-LowLatencyRenderer")); |
| 40 | } |
| 41 |
| 42 | void VCMTiming::Reset() { |
| 43 | MutexLock lock(&mutex_); |
| 44 | ts_extrapolator_->Reset(clock_->TimeInMilliseconds()); |
| 45 | codec_timer_ = std::make_unique<VCMCodecTimer>(); |
| 46 | render_delay_ms_ = kDefaultRenderDelayMs; |
| 47 | min_playout_delay_ms_ = 0; |
| 48 | jitter_delay_ms_ = 0; |
| 49 | current_delay_ms_ = 0; |
| 50 | prev_frame_timestamp_ = 0; |
| 51 | } |
| 52 |
| 53 | void VCMTiming::set_render_delay(int render_delay_ms) { |
| 54 | MutexLock lock(&mutex_); |
| 55 | render_delay_ms_ = render_delay_ms; |
| 56 | } |
| 57 |
| 58 | void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) { |
| 59 | MutexLock lock(&mutex_); |
| 60 | min_playout_delay_ms_ = min_playout_delay_ms; |
| 61 | } |
| 62 |
| 63 | int VCMTiming::min_playout_delay() { |
| 64 | MutexLock lock(&mutex_); |
| 65 | return min_playout_delay_ms_; |
| 66 | } |
| 67 |
| 68 | void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) { |
| 69 | MutexLock lock(&mutex_); |
| 70 | max_playout_delay_ms_ = max_playout_delay_ms; |
| 71 | } |
| 72 |
| 73 | int VCMTiming::max_playout_delay() { |
| 74 | MutexLock lock(&mutex_); |
| 75 | return max_playout_delay_ms_; |
| 76 | } |
| 77 |
| 78 | void VCMTiming::SetJitterDelay(int jitter_delay_ms) { |
| 79 | MutexLock lock(&mutex_); |
| 80 | if (jitter_delay_ms != jitter_delay_ms_) { |
| 81 | jitter_delay_ms_ = jitter_delay_ms; |
| 82 | // When in initial state, set current delay to minimum delay. |
| 83 | if (current_delay_ms_ == 0) { |
| 84 | current_delay_ms_ = jitter_delay_ms_; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 |
| 89 | void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
| 90 | MutexLock lock(&mutex_); |
| 91 | int target_delay_ms = TargetDelayInternal(); |
| 92 |
| 93 | if (current_delay_ms_ == 0) { |
| 94 | // Not initialized, set current delay to target. |
| 95 | current_delay_ms_ = target_delay_ms; |
| 96 | } else if (target_delay_ms != current_delay_ms_) { |
| 97 | int64_t delay_diff_ms = |
| 98 | static_cast<int64_t>(target_delay_ms) - current_delay_ms_; |
| 99 | // Never change the delay with more than 100 ms every second. If we're |
| 100 | // changing the delay in too large steps we will get noticeable freezes. By |
| 101 | // limiting the change we can increase the delay in smaller steps, which |
| 102 | // will be experienced as the video is played in slow motion. When lowering |
| 103 | // the delay the video will be played at a faster pace. |
| 104 | int64_t max_change_ms = 0; |
| 105 | if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { |
| 106 | // wrap |
| 107 | max_change_ms = kDelayMaxChangeMsPerS * |
| 108 | (frame_timestamp + (static_cast<int64_t>(1) << 32) - |
| 109 | prev_frame_timestamp_) / |
| 110 | 90000; |
| 111 | } else { |
| 112 | max_change_ms = kDelayMaxChangeMsPerS * |
| 113 | (frame_timestamp - prev_frame_timestamp_) / 90000; |
| 114 | } |
| 115 |
| 116 | if (max_change_ms <= 0) { |
| 117 | // Any changes less than 1 ms are truncated and will be postponed. |
| 118 | // Negative change will be due to reordering and should be ignored. |
| 119 | return; |
| 120 | } |
| 121 | delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); |
| 122 | delay_diff_ms = std::min(delay_diff_ms, max_change_ms); |
| 123 |
| 124 | current_delay_ms_ = current_delay_ms_ + delay_diff_ms; |
| 125 | } |
| 126 | prev_frame_timestamp_ = frame_timestamp; |
| 127 | } |
| 128 |
| 129 | void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, |
| 130 | int64_t actual_decode_time_ms) { |
| 131 | MutexLock lock(&mutex_); |
| 132 | uint32_t target_delay_ms = TargetDelayInternal(); |
| 133 | int64_t delayed_ms = |
| 134 | actual_decode_time_ms - |
| 135 | (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_); |
| 136 | if (delayed_ms < 0) { |
| 137 | return; |
| 138 | } |
| 139 | if (current_delay_ms_ + delayed_ms <= target_delay_ms) { |
| 140 | current_delay_ms_ += delayed_ms; |
| 141 | } else { |
| 142 | current_delay_ms_ = target_delay_ms; |
| 143 | } |
| 144 | } |
| 145 |
| 146 | void VCMTiming::StopDecodeTimer(uint32_t /*time_stamp*/, |
| 147 | int32_t decode_time_ms, |
| 148 | int64_t now_ms, |
| 149 | int64_t /*render_time_ms*/) { |
| 150 | StopDecodeTimer(decode_time_ms, now_ms); |
| 151 | } |
| 152 |
| 153 | void VCMTiming::StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms) { |
| 154 | MutexLock lock(&mutex_); |
| 155 | codec_timer_->AddTiming(decode_time_ms, now_ms); |
| 156 | assert(decode_time_ms >= 0); |
| 157 | ++num_decoded_frames_; |
| 158 | } |
| 159 |
| 160 | void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { |
| 161 | MutexLock lock(&mutex_); |
| 162 | ts_extrapolator_->Update(now_ms, time_stamp); |
| 163 | } |
| 164 |
| 165 | int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, |
| 166 | int64_t now_ms) const { |
| 167 | MutexLock lock(&mutex_); |
| 168 | return RenderTimeMsInternal(frame_timestamp, now_ms); |
| 169 | } |
| 170 |
| 171 | int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, |
| 172 | int64_t now_ms) const { |
| 173 | constexpr int kLowLatencyRendererMaxPlayoutDelayMs = 500; |
| 174 | if (min_playout_delay_ms_ == 0 && |
| 175 | (max_playout_delay_ms_ == 0 || |
| 176 | (low_latency_renderer_enabled_ && |
| 177 | max_playout_delay_ms_ <= kLowLatencyRendererMaxPlayoutDelayMs))) { |
| 178 | // Render as soon as possible or with low-latency renderer algorithm. |
| 179 | return 0; |
| 180 | } |
| 181 | // Note that TimestampExtrapolator::ExtrapolateLocalTime is not a const |
| 182 | // method; it mutates the object's wraparound state. |
| 183 | int64_t estimated_complete_time_ms = |
| 184 | ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); |
| 185 | if (estimated_complete_time_ms == -1) { |
| 186 | estimated_complete_time_ms = now_ms; |
| 187 | } |
| 188 |
| 189 | // Make sure the actual delay stays in the range of |min_playout_delay_ms_| |
| 190 | // and |max_playout_delay_ms_|. |
| 191 | int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_); |
| 192 | actual_delay = std::min(actual_delay, max_playout_delay_ms_); |
| 193 | return estimated_complete_time_ms + actual_delay; |
| 194 | } |
| 195 |
| 196 | int VCMTiming::RequiredDecodeTimeMs() const { |
| 197 | const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); |
| 198 | assert(decode_time_ms >= 0); |
| 199 | return decode_time_ms; |
| 200 | } |
| 201 |
| 202 | int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, |
| 203 | int64_t now_ms) const { |
| 204 | MutexLock lock(&mutex_); |
| 205 |
| 206 | const int64_t max_wait_time_ms = |
| 207 | render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_; |
| 208 |
| 209 | return max_wait_time_ms; |
| 210 | } |
| 211 |
| 212 | int VCMTiming::TargetVideoDelay() const { |
| 213 | MutexLock lock(&mutex_); |
| 214 | return TargetDelayInternal(); |
| 215 | } |
| 216 |
| 217 | int VCMTiming::TargetDelayInternal() const { |
| 218 | return std::max(min_playout_delay_ms_, |
| 219 | jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_); |
| 220 | } |
| 221 |
| 222 | bool VCMTiming::GetTimings(int* max_decode_ms, |
| 223 | int* current_delay_ms, |
| 224 | int* target_delay_ms, |
| 225 | int* jitter_buffer_ms, |
| 226 | int* min_playout_delay_ms, |
| 227 | int* render_delay_ms) const { |
| 228 | MutexLock lock(&mutex_); |
| 229 | *max_decode_ms = RequiredDecodeTimeMs(); |
| 230 | *current_delay_ms = current_delay_ms_; |
| 231 | *target_delay_ms = TargetDelayInternal(); |
| 232 | *jitter_buffer_ms = jitter_delay_ms_; |
| 233 | *min_playout_delay_ms = min_playout_delay_ms_; |
| 234 | *render_delay_ms = render_delay_ms_; |
| 235 | return (num_decoded_frames_ > 0); |
| 236 | } |
| 237 |
| 238 | void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) { |
| 239 | MutexLock lock(&mutex_); |
| 240 | timing_frame_info_.emplace(info); |
| 241 | } |
| 242 |
| 243 | absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() { |
| 244 | MutexLock lock(&mutex_); |
| 245 | return timing_frame_info_; |
| 246 | } |
| 247 |
| 248 | void VCMTiming::SetMaxCompositionDelayInFrames( |
| 249 | absl::optional<int> max_composition_delay_in_frames) { |
| 250 | MutexLock lock(&mutex_); |
| 251 | max_composition_delay_in_frames_ = max_composition_delay_in_frames; |
| 252 | } |
| 253 |
| 254 | absl::optional<int> VCMTiming::MaxCompositionDelayInFrames() const { |
| 255 | MutexLock lock(&mutex_); |
| 256 | return max_composition_delay_in_frames_; |
| 257 | } |
| 258 |
| 259 | } // namespace webrtc |
| 260 |