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