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 | |
| 11 | #ifndef WEBRTC_MODULES_VIDEO_CODING_TIMING_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_TIMING_H_ |
| 13 | |
| 14 | #include "typedefs.h" |
| 15 | #include "critical_section_wrapper.h" |
| 16 | #include "codec_timer.h" |
| 17 | |
| 18 | namespace webrtc |
| 19 | { |
| 20 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 21 | class Clock; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | class VCMTimestampExtrapolator; |
| 23 | |
| 24 | class VCMTiming |
| 25 | { |
| 26 | public: |
| 27 | // The primary timing component should be passed |
| 28 | // if this is the dual timing component. |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 29 | VCMTiming(Clock* clock, |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 30 | int32_t vcmId = 0, |
| 31 | int32_t timingId = 0, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | VCMTiming* masterTiming = NULL); |
| 33 | ~VCMTiming(); |
| 34 | |
| 35 | // Resets the timing to the initial state. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 36 | void Reset(int64_t nowMs = -1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | void ResetDecodeTime(); |
| 38 | |
| 39 | // The amount of time needed to render an image. Defaults to 10 ms. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 40 | void SetRenderDelay(uint32_t renderDelayMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
| 42 | // The minimum time the video must be delayed on the receiver to |
| 43 | // get the desired jitter buffer level. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 44 | void SetRequiredDelay(uint32_t requiredDelayMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
| 46 | // Minimum total delay required to sync video with audio. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 47 | void SetMinimumTotalDelay(uint32_t minTotalDelayMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
| 49 | // Increases or decreases the current delay to get closer to the target delay. |
| 50 | // Calculates how long it has been since the previous call to this function, |
| 51 | // and increases/decreases the delay in proportion to the time difference. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 52 | void UpdateCurrentDelay(uint32_t frameTimestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | |
| 54 | // Increases or decreases the current delay to get closer to the target delay. |
| 55 | // Given the actual decode time in ms and the render time in ms for a frame, this |
| 56 | // function calculates how late the frame is and increases the delay accordingly. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 57 | void UpdateCurrentDelay(int64_t renderTimeMs, int64_t actualDecodeTimeMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
| 59 | // Stops the decoder timer, should be called when the decoder returns a frame |
| 60 | // or when the decoded frame callback is called. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 61 | int32_t StopDecodeTimer(uint32_t timeStamp, |
| 62 | int64_t startTimeMs, |
| 63 | int64_t nowMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | |
| 65 | // Used to report that a frame is passed to decoding. Updates the timestamp filter |
| 66 | // which is used to map between timestamps and receiver system time. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 67 | void IncomingTimestamp(uint32_t timeStamp, int64_t lastPacketTimeMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
| 69 | // Returns the receiver system time when the frame with timestamp frameTimestamp |
| 70 | // should be rendered, assuming that the system time currently is nowMs. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 71 | int64_t RenderTimeMs(uint32_t frameTimestamp, int64_t nowMs) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
| 73 | // Returns the maximum time in ms that we can wait for a frame to become complete |
| 74 | // before we must pass it to the decoder. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 75 | uint32_t MaxWaitingTime(int64_t renderTimeMs, int64_t nowMs) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
| 77 | // Returns the current target delay which is required delay + decode time + render |
| 78 | // delay. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 79 | uint32_t TargetVideoDelay() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | |
| 81 | // Calculates whether or not there is enough time to decode a frame given a |
| 82 | // certain amount of processing time. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 83 | bool EnoughTimeToDecode(uint32_t availableProcessingTimeMs) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 85 | // Set the max allowed video delay. |
| 86 | void SetMaxVideoDelay(int maxVideoDelayMs); |
| 87 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | enum { kDefaultRenderDelayMs = 10 }; |
| 89 | enum { kDelayMaxChangeMsPerS = 100 }; |
| 90 | |
| 91 | protected: |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 92 | int32_t MaxDecodeTimeMs(FrameType frameType = kVideoFrameDelta) const; |
| 93 | int64_t RenderTimeMsInternal(uint32_t frameTimestamp, |
| 94 | int64_t nowMs) const; |
| 95 | uint32_t TargetDelayInternal() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
| 97 | private: |
mikhal@webrtc.org | dbd6a6d | 2013-04-17 16:23:22 +0000 | [diff] [blame^] | 98 | CriticalSectionWrapper* _critSect; |
| 99 | int32_t _vcmId; |
| 100 | Clock* _clock; |
| 101 | int32_t _timingId; |
| 102 | bool _master; |
| 103 | VCMTimestampExtrapolator* _tsExtrapolator; |
| 104 | VCMCodecTimer _codecTimer; |
| 105 | uint32_t _renderDelayMs; |
| 106 | uint32_t _minTotalDelayMs; |
| 107 | uint32_t _requiredDelayMs; |
| 108 | uint32_t _currentDelayMs; |
| 109 | uint32_t _prevFrameTimestamp; |
| 110 | int _maxVideoDelayMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace webrtc |
| 114 | |
| 115 | #endif // WEBRTC_MODULES_VIDEO_CODING_TIMING_H_ |