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" |
| 15 | #include "webrtc/modules/video_coding/main/source/timestamp_extrapolator.h" |
| 16 | #include "webrtc/system_wrappers/interface/clock.h" |
| 17 | #include "webrtc/system_wrappers/interface/trace.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 21 | VCMTiming::VCMTiming(Clock* clock, |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 22 | WebRtc_Word32 vcmId, |
| 23 | WebRtc_Word32 timingId, |
| 24 | VCMTiming* masterTiming) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | : |
stefan@webrtc.org | 7889a9b | 2011-12-12 08:18:24 +0000 | [diff] [blame] | 26 | _critSect(CriticalSectionWrapper::CreateCriticalSection()), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | _vcmId(vcmId), |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 28 | _clock(clock), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | _timingId(timingId), |
| 30 | _master(false), |
| 31 | _tsExtrapolator(), |
| 32 | _codecTimer(), |
| 33 | _renderDelayMs(kDefaultRenderDelayMs), |
| 34 | _minTotalDelayMs(0), |
| 35 | _requiredDelayMs(0), |
| 36 | _currentDelayMs(0), |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame^] | 37 | _prevFrameTimestamp(0), |
| 38 | _maxVideoDelayMs(kMaxVideoDelayMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | { |
| 40 | if (masterTiming == NULL) |
| 41 | { |
| 42 | _master = true; |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 43 | _tsExtrapolator = new VCMTimestampExtrapolator(_clock, vcmId, timingId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | else |
| 46 | { |
| 47 | _tsExtrapolator = masterTiming->_tsExtrapolator; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | VCMTiming::~VCMTiming() |
| 52 | { |
| 53 | if (_master) |
| 54 | { |
| 55 | delete _tsExtrapolator; |
| 56 | } |
stefan@webrtc.org | 7889a9b | 2011-12-12 08:18:24 +0000 | [diff] [blame] | 57 | delete _critSect; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
| 61 | VCMTiming::Reset(WebRtc_Word64 nowMs /* = -1 */) |
| 62 | { |
| 63 | CriticalSectionScoped cs(_critSect); |
| 64 | if (nowMs > -1) |
| 65 | { |
| 66 | _tsExtrapolator->Reset(nowMs); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | _tsExtrapolator->Reset(); |
| 71 | } |
| 72 | _codecTimer.Reset(); |
| 73 | _renderDelayMs = kDefaultRenderDelayMs; |
| 74 | _minTotalDelayMs = 0; |
| 75 | _requiredDelayMs = 0; |
| 76 | _currentDelayMs = 0; |
| 77 | _prevFrameTimestamp = 0; |
| 78 | } |
| 79 | |
| 80 | void VCMTiming::ResetDecodeTime() |
| 81 | { |
| 82 | _codecTimer.Reset(); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | VCMTiming::SetRenderDelay(WebRtc_UWord32 renderDelayMs) |
| 87 | { |
| 88 | CriticalSectionScoped cs(_critSect); |
| 89 | _renderDelayMs = renderDelayMs; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | VCMTiming::SetMinimumTotalDelay(WebRtc_UWord32 minTotalDelayMs) |
| 94 | { |
| 95 | CriticalSectionScoped cs(_critSect); |
| 96 | _minTotalDelayMs = minTotalDelayMs; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | VCMTiming::SetRequiredDelay(WebRtc_UWord32 requiredDelayMs) |
| 101 | { |
| 102 | CriticalSectionScoped cs(_critSect); |
| 103 | if (requiredDelayMs != _requiredDelayMs) |
| 104 | { |
| 105 | if (_master) |
| 106 | { |
| 107 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCoding, VCMId(_vcmId, _timingId), |
| 108 | "Desired jitter buffer level: %u ms", requiredDelayMs); |
| 109 | } |
| 110 | _requiredDelayMs = requiredDelayMs; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void VCMTiming::UpdateCurrentDelay(WebRtc_UWord32 frameTimestamp) |
| 115 | { |
| 116 | CriticalSectionScoped cs(_critSect); |
| 117 | WebRtc_UWord32 targetDelayMs = TargetDelayInternal(); |
| 118 | |
| 119 | // Make sure we try to sync with audio |
| 120 | if (targetDelayMs < _minTotalDelayMs) |
| 121 | { |
| 122 | targetDelayMs = _minTotalDelayMs; |
| 123 | } |
| 124 | |
| 125 | if (_currentDelayMs == 0) |
| 126 | { |
| 127 | // Not initialized, set current delay to target. |
| 128 | _currentDelayMs = targetDelayMs; |
| 129 | } |
| 130 | else if (targetDelayMs != _currentDelayMs) |
| 131 | { |
| 132 | WebRtc_Word64 delayDiffMs = static_cast<WebRtc_Word64>(targetDelayMs) - |
| 133 | _currentDelayMs; |
| 134 | // Never change the delay with more than 100 ms every second. If we're changing the |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame^] | 135 | // delay in too large steps we will get noticeable freezes. By limiting the change we |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | // can increase the delay in smaller steps, which will be experienced as the video is |
| 137 | // played in slow motion. When lowering the delay the video will be played at a faster |
| 138 | // pace. |
| 139 | WebRtc_Word64 maxChangeMs = 0; |
| 140 | if (frameTimestamp < 0x0000ffff && _prevFrameTimestamp > 0xffff0000) |
| 141 | { |
| 142 | // wrap |
| 143 | maxChangeMs = kDelayMaxChangeMsPerS * (frameTimestamp + |
| 144 | (static_cast<WebRtc_Word64>(1)<<32) - _prevFrameTimestamp) / 90000; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | maxChangeMs = kDelayMaxChangeMsPerS * |
| 149 | (frameTimestamp - _prevFrameTimestamp) / 90000; |
| 150 | } |
| 151 | if (maxChangeMs <= 0) |
| 152 | { |
| 153 | // Any changes less than 1 ms are truncated and |
| 154 | // will be postponed. Negative change will be due |
| 155 | // to reordering and should be ignored. |
| 156 | return; |
| 157 | } |
| 158 | else if (delayDiffMs < -maxChangeMs) |
| 159 | { |
| 160 | delayDiffMs = -maxChangeMs; |
| 161 | } |
| 162 | else if (delayDiffMs > maxChangeMs) |
| 163 | { |
| 164 | delayDiffMs = maxChangeMs; |
| 165 | } |
| 166 | _currentDelayMs = _currentDelayMs + static_cast<WebRtc_Word32>(delayDiffMs); |
| 167 | } |
| 168 | _prevFrameTimestamp = frameTimestamp; |
| 169 | } |
| 170 | |
| 171 | void VCMTiming::UpdateCurrentDelay(WebRtc_Word64 renderTimeMs, |
| 172 | WebRtc_Word64 actualDecodeTimeMs) |
| 173 | { |
| 174 | CriticalSectionScoped cs(_critSect); |
| 175 | WebRtc_UWord32 targetDelayMs = TargetDelayInternal(); |
| 176 | // Make sure we try to sync with audio |
| 177 | if (targetDelayMs < _minTotalDelayMs) |
| 178 | { |
| 179 | targetDelayMs = _minTotalDelayMs; |
| 180 | } |
| 181 | WebRtc_Word64 delayedMs = actualDecodeTimeMs - |
| 182 | (renderTimeMs - MaxDecodeTimeMs() - _renderDelayMs); |
| 183 | if (delayedMs < 0) |
| 184 | { |
| 185 | return; |
| 186 | } |
| 187 | else if (_currentDelayMs + delayedMs <= targetDelayMs) |
| 188 | { |
| 189 | _currentDelayMs += static_cast<WebRtc_UWord32>(delayedMs); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | _currentDelayMs = targetDelayMs; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | WebRtc_Word32 |
| 198 | VCMTiming::StopDecodeTimer(WebRtc_UWord32 timeStamp, |
| 199 | WebRtc_Word64 startTimeMs, |
| 200 | WebRtc_Word64 nowMs) |
| 201 | { |
| 202 | CriticalSectionScoped cs(_critSect); |
| 203 | const WebRtc_Word32 maxDecTime = MaxDecodeTimeMs(); |
| 204 | WebRtc_Word32 timeDiffMs = _codecTimer.StopTimer(startTimeMs, nowMs); |
| 205 | if (timeDiffMs < 0) |
| 206 | { |
| 207 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCoding, VCMId(_vcmId, _timingId), |
| 208 | "Codec timer error: %d", timeDiffMs); |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 209 | assert(false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (_master) |
| 213 | { |
| 214 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCoding, VCMId(_vcmId, _timingId), |
| 215 | "Frame decoded: timeStamp=%u decTime=%d maxDecTime=%u, at %u", |
| 216 | timeStamp, timeDiffMs, maxDecTime, MaskWord64ToUWord32(nowMs)); |
| 217 | } |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | void |
| 222 | VCMTiming::IncomingTimestamp(WebRtc_UWord32 timeStamp, WebRtc_Word64 nowMs) |
| 223 | { |
| 224 | CriticalSectionScoped cs(_critSect); |
| 225 | _tsExtrapolator->Update(nowMs, timeStamp, _master); |
| 226 | } |
| 227 | |
| 228 | WebRtc_Word64 |
| 229 | VCMTiming::RenderTimeMs(WebRtc_UWord32 frameTimestamp, WebRtc_Word64 nowMs) const |
| 230 | { |
| 231 | CriticalSectionScoped cs(_critSect); |
| 232 | const WebRtc_Word64 renderTimeMs = RenderTimeMsInternal(frameTimestamp, nowMs); |
| 233 | if (renderTimeMs < 0) |
| 234 | { |
| 235 | return renderTimeMs; |
| 236 | } |
| 237 | if (_master) |
| 238 | { |
| 239 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCoding, VCMId(_vcmId, _timingId), |
| 240 | "Render frame %u at %u. Render delay %u, required delay %u," |
| 241 | " max decode time %u, min total delay %u", |
| 242 | frameTimestamp, MaskWord64ToUWord32(renderTimeMs), _renderDelayMs, |
| 243 | _requiredDelayMs, MaxDecodeTimeMs(),_minTotalDelayMs); |
| 244 | } |
| 245 | return renderTimeMs; |
| 246 | } |
| 247 | |
| 248 | WebRtc_Word64 |
| 249 | VCMTiming::RenderTimeMsInternal(WebRtc_UWord32 frameTimestamp, WebRtc_Word64 nowMs) const |
| 250 | { |
| 251 | WebRtc_Word64 estimatedCompleteTimeMs = |
| 252 | _tsExtrapolator->ExtrapolateLocalTime(frameTimestamp); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame^] | 253 | if (estimatedCompleteTimeMs - nowMs > _maxVideoDelayMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 254 | { |
| 255 | if (_master) |
| 256 | { |
| 257 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCoding, VCMId(_vcmId, _timingId), |
| 258 | "Timestamp arrived 2 seconds early, reset statistics", |
| 259 | frameTimestamp, estimatedCompleteTimeMs); |
| 260 | } |
| 261 | return -1; |
| 262 | } |
| 263 | if (_master) |
| 264 | { |
| 265 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCoding, VCMId(_vcmId, _timingId), |
| 266 | "ExtrapolateLocalTime(%u)=%u ms", |
| 267 | frameTimestamp, MaskWord64ToUWord32(estimatedCompleteTimeMs)); |
| 268 | } |
| 269 | if (estimatedCompleteTimeMs == -1) |
| 270 | { |
| 271 | estimatedCompleteTimeMs = nowMs; |
| 272 | } |
| 273 | |
| 274 | return estimatedCompleteTimeMs + _currentDelayMs; |
| 275 | } |
| 276 | |
| 277 | // Must be called from inside a critical section |
| 278 | WebRtc_Word32 |
| 279 | VCMTiming::MaxDecodeTimeMs(FrameType frameType /*= kVideoFrameDelta*/) const |
| 280 | { |
| 281 | const WebRtc_Word32 decodeTimeMs = _codecTimer.RequiredDecodeTimeMs(frameType); |
| 282 | |
| 283 | if (decodeTimeMs < 0) |
| 284 | { |
| 285 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCoding, VCMId(_vcmId, _timingId), |
| 286 | "Negative maximum decode time: %d", decodeTimeMs); |
| 287 | return -1; |
| 288 | } |
| 289 | return decodeTimeMs; |
| 290 | } |
| 291 | |
| 292 | WebRtc_UWord32 |
| 293 | VCMTiming::MaxWaitingTime(WebRtc_Word64 renderTimeMs, WebRtc_Word64 nowMs) const |
| 294 | { |
| 295 | CriticalSectionScoped cs(_critSect); |
| 296 | |
| 297 | const WebRtc_Word64 maxWaitTimeMs = renderTimeMs - nowMs - |
| 298 | MaxDecodeTimeMs() - _renderDelayMs; |
| 299 | |
| 300 | if (maxWaitTimeMs < 0) |
| 301 | { |
| 302 | return 0; |
| 303 | } |
| 304 | return static_cast<WebRtc_UWord32>(maxWaitTimeMs); |
| 305 | } |
| 306 | |
| 307 | bool |
| 308 | VCMTiming::EnoughTimeToDecode(WebRtc_UWord32 availableProcessingTimeMs) const |
| 309 | { |
| 310 | CriticalSectionScoped cs(_critSect); |
| 311 | WebRtc_Word32 maxDecodeTimeMs = MaxDecodeTimeMs(); |
| 312 | if (maxDecodeTimeMs < 0) |
| 313 | { |
| 314 | // Haven't decoded any frames yet, try decoding one to get an estimate |
| 315 | // of the decode time. |
| 316 | return true; |
| 317 | } |
| 318 | else if (maxDecodeTimeMs == 0) |
| 319 | { |
| 320 | // Decode time is less than 1, set to 1 for now since |
| 321 | // we don't have any better precision. Count ticks later? |
| 322 | maxDecodeTimeMs = 1; |
| 323 | } |
| 324 | return static_cast<WebRtc_Word32>(availableProcessingTimeMs) - maxDecodeTimeMs > 0; |
| 325 | } |
| 326 | |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame^] | 327 | void VCMTiming::SetMaxVideoDelay(int maxVideoDelayMs) |
| 328 | { |
| 329 | CriticalSectionScoped cs(_critSect); |
| 330 | _maxVideoDelayMs = maxVideoDelayMs; |
| 331 | } |
| 332 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 333 | WebRtc_UWord32 |
| 334 | VCMTiming::TargetVideoDelay() const |
| 335 | { |
| 336 | CriticalSectionScoped cs(_critSect); |
| 337 | return TargetDelayInternal(); |
| 338 | } |
| 339 | |
| 340 | WebRtc_UWord32 |
| 341 | VCMTiming::TargetDelayInternal() const |
| 342 | { |
| 343 | return _requiredDelayMs + MaxDecodeTimeMs() + _renderDelayMs; |
| 344 | } |
| 345 | |
| 346 | } |