blob: 41a4945d0b204af299c77905d83ce7f111e1069b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
18namespace webrtc
19{
20
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +000021class TickTimeBase;
niklase@google.com470e71d2011-07-07 08:21:25 +000022class VCMTimestampExtrapolator;
23
24class VCMTiming
25{
26public:
27 // The primary timing component should be passed
28 // if this is the dual timing component.
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +000029 VCMTiming(TickTimeBase* clock,
30 WebRtc_Word32 vcmId = 0,
niklase@google.com470e71d2011-07-07 08:21:25 +000031 WebRtc_Word32 timingId = 0,
32 VCMTiming* masterTiming = NULL);
33 ~VCMTiming();
34
35 // Resets the timing to the initial state.
36 void Reset(WebRtc_Word64 nowMs = -1);
37 void ResetDecodeTime();
38
39 // The amount of time needed to render an image. Defaults to 10 ms.
40 void SetRenderDelay(WebRtc_UWord32 renderDelayMs);
41
42 // The minimum time the video must be delayed on the receiver to
43 // get the desired jitter buffer level.
44 void SetRequiredDelay(WebRtc_UWord32 requiredDelayMs);
45
46 // Minimum total delay required to sync video with audio.
47 void SetMinimumTotalDelay(WebRtc_UWord32 minTotalDelayMs);
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.
52 void UpdateCurrentDelay(WebRtc_UWord32 frameTimestamp);
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.
57 void UpdateCurrentDelay(WebRtc_Word64 renderTimeMs, WebRtc_Word64 actualDecodeTimeMs);
58
59 // Stops the decoder timer, should be called when the decoder returns a frame
60 // or when the decoded frame callback is called.
61 WebRtc_Word32 StopDecodeTimer(WebRtc_UWord32 timeStamp,
62 WebRtc_Word64 startTimeMs,
63 WebRtc_Word64 nowMs);
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.
67 void IncomingTimestamp(WebRtc_UWord32 timeStamp, WebRtc_Word64 lastPacketTimeMs);
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.
71 WebRtc_Word64 RenderTimeMs(WebRtc_UWord32 frameTimestamp, WebRtc_Word64 nowMs) const;
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.
75 WebRtc_UWord32 MaxWaitingTime(WebRtc_Word64 renderTimeMs, WebRtc_Word64 nowMs) const;
76
77 // Returns the current target delay which is required delay + decode time + render
78 // delay.
79 WebRtc_UWord32 TargetVideoDelay() const;
80
81 // Calculates whether or not there is enough time to decode a frame given a
82 // certain amount of processing time.
83 bool EnoughTimeToDecode(WebRtc_UWord32 availableProcessingTimeMs) const;
84
85 enum { kDefaultRenderDelayMs = 10 };
86 enum { kDelayMaxChangeMsPerS = 100 };
87
88protected:
89 WebRtc_Word32 MaxDecodeTimeMs(FrameType frameType = kVideoFrameDelta) const;
90 WebRtc_Word64 RenderTimeMsInternal(WebRtc_UWord32 frameTimestamp,
91 WebRtc_Word64 nowMs) const;
92 WebRtc_UWord32 TargetDelayInternal() const;
93
94private:
stefan@webrtc.org7889a9b2011-12-12 08:18:24 +000095 CriticalSectionWrapper* _critSect;
niklase@google.com470e71d2011-07-07 08:21:25 +000096 WebRtc_Word32 _vcmId;
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +000097 TickTimeBase* _clock;
niklase@google.com470e71d2011-07-07 08:21:25 +000098 WebRtc_Word32 _timingId;
99 bool _master;
100 VCMTimestampExtrapolator* _tsExtrapolator;
101 VCMCodecTimer _codecTimer;
102 WebRtc_UWord32 _renderDelayMs;
103 WebRtc_UWord32 _minTotalDelayMs;
104 WebRtc_UWord32 _requiredDelayMs;
105 WebRtc_UWord32 _currentDelayMs;
106 WebRtc_UWord32 _prevFrameTimestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107};
108
109} // namespace webrtc
110
111#endif // WEBRTC_MODULES_VIDEO_CODING_TIMING_H_