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 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/main/source/timing.h" |
| 12 | |
| 13 | #include "webrtc/modules/video_coding/main/source/internal_defines.h" |
| 14 | #include "webrtc/modules/video_coding/main/source/jitter_buffer_common.h" |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/interface/clock.h" |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/interface/timestamp_extrapolator.h" |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 17 | |
| 18 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 21 | VCMTiming::VCMTiming(Clock* clock, |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 22 | VCMTiming* master_timing) |
| 23 | : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 24 | clock_(clock), |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 25 | master_(false), |
| 26 | ts_extrapolator_(), |
| 27 | codec_timer_(), |
| 28 | render_delay_ms_(kDefaultRenderDelayMs), |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 29 | min_playout_delay_ms_(0), |
| 30 | jitter_delay_ms_(0), |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 31 | current_delay_ms_(0), |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 32 | last_decode_ms_(0), |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 33 | prev_frame_timestamp_(0) { |
| 34 | if (master_timing == NULL) { |
| 35 | master_ = true; |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 36 | ts_extrapolator_ = new TimestampExtrapolator(clock_->TimeInMilliseconds()); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 37 | } else { |
| 38 | ts_extrapolator_ = master_timing->ts_extrapolator_; |
| 39 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } |
| 41 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 42 | VCMTiming::~VCMTiming() { |
| 43 | if (master_) { |
| 44 | delete ts_extrapolator_; |
| 45 | } |
| 46 | delete crit_sect_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 49 | void VCMTiming::Reset() { |
| 50 | CriticalSectionScoped cs(crit_sect_); |
wu@webrtc.org | ed4cb56 | 2014-05-06 04:50:49 +0000 | [diff] [blame] | 51 | ts_extrapolator_->Reset(clock_->TimeInMilliseconds()); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 52 | codec_timer_.Reset(); |
| 53 | render_delay_ms_ = kDefaultRenderDelayMs; |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 54 | min_playout_delay_ms_ = 0; |
| 55 | jitter_delay_ms_ = 0; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 56 | current_delay_ms_ = 0; |
| 57 | prev_frame_timestamp_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 60 | void VCMTiming::ResetDecodeTime() { |
| 61 | codec_timer_.Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 64 | void VCMTiming::set_render_delay(uint32_t render_delay_ms) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 65 | CriticalSectionScoped cs(crit_sect_); |
| 66 | render_delay_ms_ = render_delay_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 69 | void VCMTiming::set_min_playout_delay(uint32_t min_playout_delay_ms) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 70 | CriticalSectionScoped cs(crit_sect_); |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 71 | min_playout_delay_ms_ = min_playout_delay_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | } |
| 73 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 74 | void VCMTiming::SetJitterDelay(uint32_t jitter_delay_ms) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 75 | CriticalSectionScoped cs(crit_sect_); |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 76 | if (jitter_delay_ms != jitter_delay_ms_) { |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 77 | jitter_delay_ms_ = jitter_delay_ms; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 78 | // When in initial state, set current delay to minimum delay. |
| 79 | if (current_delay_ms_ == 0) { |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 80 | current_delay_ms_ = jitter_delay_ms_; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 85 | void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
| 86 | CriticalSectionScoped cs(crit_sect_); |
| 87 | uint32_t target_delay_ms = TargetDelayInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 89 | if (current_delay_ms_ == 0) { |
| 90 | // Not initialized, set current delay to target. |
| 91 | current_delay_ms_ = target_delay_ms; |
| 92 | } else if (target_delay_ms != current_delay_ms_) { |
| 93 | int64_t delay_diff_ms = static_cast<int64_t>(target_delay_ms) - |
| 94 | current_delay_ms_; |
| 95 | // Never change the delay with more than 100 ms every second. If we're |
| 96 | // changing the delay in too large steps we will get noticeable freezes. By |
| 97 | // limiting the change we can increase the delay in smaller steps, which |
| 98 | // will be experienced as the video is played in slow motion. When lowering |
| 99 | // the delay the video will be played at a faster pace. |
| 100 | int64_t max_change_ms = 0; |
| 101 | if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { |
| 102 | // wrap |
| 103 | max_change_ms = kDelayMaxChangeMsPerS * (frame_timestamp + |
| 104 | (static_cast<int64_t>(1) << 32) - prev_frame_timestamp_) / 90000; |
| 105 | } else { |
| 106 | max_change_ms = kDelayMaxChangeMsPerS * |
| 107 | (frame_timestamp - prev_frame_timestamp_) / 90000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | } |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 109 | if (max_change_ms <= 0) { |
| 110 | // Any changes less than 1 ms are truncated and |
| 111 | // will be postponed. Negative change will be due |
| 112 | // to reordering and should be ignored. |
| 113 | return; |
| 114 | } |
| 115 | delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); |
| 116 | delay_diff_ms = std::min(delay_diff_ms, max_change_ms); |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 117 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 118 | current_delay_ms_ = current_delay_ms_ + static_cast<int32_t>(delay_diff_ms); |
| 119 | } |
| 120 | prev_frame_timestamp_ = frame_timestamp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | } |
| 122 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 123 | void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, |
| 124 | int64_t actual_decode_time_ms) { |
| 125 | CriticalSectionScoped cs(crit_sect_); |
| 126 | uint32_t target_delay_ms = TargetDelayInternal(); |
| 127 | int64_t delayed_ms = actual_decode_time_ms - |
| 128 | (render_time_ms - MaxDecodeTimeMs() - render_delay_ms_); |
| 129 | if (delayed_ms < 0) { |
| 130 | return; |
| 131 | } |
| 132 | if (current_delay_ms_ + delayed_ms <= target_delay_ms) { |
| 133 | current_delay_ms_ += static_cast<uint32_t>(delayed_ms); |
| 134 | } else { |
| 135 | current_delay_ms_ = target_delay_ms; |
| 136 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 139 | int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp, |
| 140 | int64_t start_time_ms, |
| 141 | int64_t now_ms) { |
| 142 | CriticalSectionScoped cs(crit_sect_); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 143 | int32_t time_diff_ms = codec_timer_.StopTimer(start_time_ms, now_ms); |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 144 | assert(time_diff_ms >= 0); |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 145 | last_decode_ms_ = time_diff_ms; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 146 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 149 | void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { |
| 150 | CriticalSectionScoped cs(crit_sect_); |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 151 | ts_extrapolator_->Update(now_ms, time_stamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | } |
| 153 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 154 | int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) |
| 155 | const { |
| 156 | CriticalSectionScoped cs(crit_sect_); |
| 157 | const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 158 | return render_time_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | } |
| 160 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 161 | int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, |
| 162 | int64_t now_ms) const { |
| 163 | int64_t estimated_complete_time_ms = |
| 164 | ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 165 | if (estimated_complete_time_ms == -1) { |
| 166 | estimated_complete_time_ms = now_ms; |
| 167 | } |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 168 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 169 | // Make sure that we have at least the playout delay. |
| 170 | uint32_t actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 171 | return estimated_complete_time_ms + actual_delay; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | } |
| 173 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 174 | // Must be called from inside a critical section. |
| 175 | int32_t VCMTiming::MaxDecodeTimeMs(FrameType frame_type /*= kVideoFrameDelta*/) |
| 176 | const { |
| 177 | const int32_t decode_time_ms = codec_timer_.RequiredDecodeTimeMs(frame_type); |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 178 | assert(decode_time_ms >= 0); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 179 | return decode_time_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | } |
| 181 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 182 | uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) |
| 183 | const { |
| 184 | CriticalSectionScoped cs(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 186 | const int64_t max_wait_time_ms = render_time_ms - now_ms - |
| 187 | MaxDecodeTimeMs() - render_delay_ms_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 189 | if (max_wait_time_ms < 0) { |
| 190 | return 0; |
| 191 | } |
| 192 | return static_cast<uint32_t>(max_wait_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 193 | } |
| 194 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 195 | bool VCMTiming::EnoughTimeToDecode(uint32_t available_processing_time_ms) |
| 196 | const { |
| 197 | CriticalSectionScoped cs(crit_sect_); |
| 198 | int32_t max_decode_time_ms = MaxDecodeTimeMs(); |
| 199 | if (max_decode_time_ms < 0) { |
| 200 | // Haven't decoded any frames yet, try decoding one to get an estimate |
| 201 | // of the decode time. |
| 202 | return true; |
| 203 | } else if (max_decode_time_ms == 0) { |
| 204 | // Decode time is less than 1, set to 1 for now since |
| 205 | // we don't have any better precision. Count ticks later? |
| 206 | max_decode_time_ms = 1; |
| 207 | } |
| 208 | return static_cast<int32_t>(available_processing_time_ms) - |
| 209 | max_decode_time_ms > 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | } |
| 211 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 212 | uint32_t VCMTiming::TargetVideoDelay() const { |
| 213 | CriticalSectionScoped cs(crit_sect_); |
| 214 | return TargetDelayInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | } |
| 216 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 217 | uint32_t VCMTiming::TargetDelayInternal() const { |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 218 | return std::max(min_playout_delay_ms_, |
| 219 | jitter_delay_ms_ + MaxDecodeTimeMs() + render_delay_ms_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | } |
| 221 | |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 222 | void VCMTiming::GetTimings(int* decode_ms, |
| 223 | 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 | CriticalSectionScoped cs(crit_sect_); |
| 230 | *decode_ms = last_decode_ms_; |
| 231 | *max_decode_ms = MaxDecodeTimeMs(); |
| 232 | *current_delay_ms = current_delay_ms_; |
| 233 | *target_delay_ms = TargetDelayInternal(); |
| 234 | *jitter_buffer_ms = jitter_delay_ms_; |
| 235 | *min_playout_delay_ms = min_playout_delay_ms_; |
| 236 | *render_delay_ms = render_delay_ms_; |
| 237 | } |
| 238 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 239 | } // namespace webrtc |