blob: 4b7f6472a6b74239d3b96db5192a67f691d386d7 [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
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000021class Clock;
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.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000029 VCMTiming(Clock* clock,
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000030 int32_t vcmId = 0,
31 int32_t timingId = 0,
niklase@google.com470e71d2011-07-07 08:21:25 +000032 VCMTiming* masterTiming = NULL);
33 ~VCMTiming();
34
35 // Resets the timing to the initial state.
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000036 void Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000037 void ResetDecodeTime();
38
39 // The amount of time needed to render an image. Defaults to 10 ms.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000040 void SetRenderDelay(uint32_t renderDelayMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000041
42 // The minimum time the video must be delayed on the receiver to
43 // get the desired jitter buffer level.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000044 void SetRequiredDelay(uint32_t requiredDelayMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
46 // Minimum total delay required to sync video with audio.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000047 void SetMinimumTotalDelay(uint32_t minTotalDelayMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000048
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.org7b859cc2013-04-02 15:54:38 +000052 void UpdateCurrentDelay(uint32_t frameTimestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000053
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.org7b859cc2013-04-02 15:54:38 +000057 void UpdateCurrentDelay(int64_t renderTimeMs, int64_t actualDecodeTimeMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000058
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.org7b859cc2013-04-02 15:54:38 +000061 int32_t StopDecodeTimer(uint32_t timeStamp,
62 int64_t startTimeMs,
63 int64_t nowMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000064
mikhal@webrtc.org759b0412013-05-07 16:36:00 +000065 // Used to report that a frame is passed to decoding. Updates the timestamp
66 // filter which is used to map between timestamps and receiver system time.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000067 void IncomingTimestamp(uint32_t timeStamp, int64_t lastPacketTimeMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000068
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.org7b859cc2013-04-02 15:54:38 +000071 int64_t RenderTimeMs(uint32_t frameTimestamp, int64_t nowMs) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000072
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.org7b859cc2013-04-02 15:54:38 +000075 uint32_t MaxWaitingTime(int64_t renderTimeMs, int64_t nowMs) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000076
77 // Returns the current target delay which is required delay + decode time + render
78 // delay.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000079 uint32_t TargetVideoDelay() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000080
81 // Calculates whether or not there is enough time to decode a frame given a
82 // certain amount of processing time.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000083 bool EnoughTimeToDecode(uint32_t availableProcessingTimeMs) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000084
85 enum { kDefaultRenderDelayMs = 10 };
86 enum { kDelayMaxChangeMsPerS = 100 };
87
88protected:
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000089 int32_t MaxDecodeTimeMs(FrameType frameType = kVideoFrameDelta) const;
mikhal@webrtc.org759b0412013-05-07 16:36:00 +000090 int64_t RenderTimeMsInternal(uint32_t frameTimestamp, int64_t nowMs) const;
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000091 uint32_t TargetDelayInternal() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000092
93private:
mikhal@webrtc.orgdbd6a6d2013-04-17 16:23:22 +000094 CriticalSectionWrapper* _critSect;
95 int32_t _vcmId;
96 Clock* _clock;
97 int32_t _timingId;
98 bool _master;
99 VCMTimestampExtrapolator* _tsExtrapolator;
100 VCMCodecTimer _codecTimer;
101 uint32_t _renderDelayMs;
102 uint32_t _minTotalDelayMs;
103 uint32_t _requiredDelayMs;
104 uint32_t _currentDelayMs;
105 uint32_t _prevFrameTimestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106};
107
108} // namespace webrtc
109
110#endif // WEBRTC_MODULES_VIDEO_CODING_TIMING_H_