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