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_MAIN_SOURCE_TIMING_H_
12#define WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_TIMING_H_
13
14#include "webrtc/base/thread_annotations.h"
15#include "webrtc/modules/video_coding/main/source/codec_timer.h"
16#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21class Clock;
22class TimestampExtrapolator;
23
24class VCMTiming {
25 public:
26  // The primary timing component should be passed
27  // if this is the dual timing component.
28  VCMTiming(Clock* clock,
29            VCMTiming* master_timing = NULL);
30  ~VCMTiming();
31
32  // Resets the timing to the initial state.
33  void Reset();
34  void ResetDecodeTime();
35
36  // Set the amount of time needed to render an image. Defaults to 10 ms.
37  void set_render_delay(uint32_t render_delay_ms);
38
39  // Set the minimum time the video must be delayed on the receiver to
40  // get the desired jitter buffer level.
41  void SetJitterDelay(uint32_t required_delay_ms);
42
43  // Set the minimum playout delay required to sync video with audio.
44  void set_min_playout_delay(uint32_t min_playout_delay);
45
46  // Increases or decreases the current delay to get closer to the target delay.
47  // Calculates how long it has been since the previous call to this function,
48  // and increases/decreases the delay in proportion to the time difference.
49  void UpdateCurrentDelay(uint32_t frame_timestamp);
50
51  // Increases or decreases the current delay to get closer to the target delay.
52  // Given the actual decode time in ms and the render time in ms for a frame,
53  // this function calculates how late the frame is and increases the delay
54  // accordingly.
55  void UpdateCurrentDelay(int64_t render_time_ms,
56                          int64_t actual_decode_time_ms);
57
58  // Stops the decoder timer, should be called when the decoder returns a frame
59  // or when the decoded frame callback is called.
60  int32_t StopDecodeTimer(uint32_t time_stamp,
61                          int64_t start_time_ms,
62                          int64_t now_ms);
63
64  // Used to report that a frame is passed to decoding. Updates the timestamp
65  // filter which is used to map between timestamps and receiver system time.
66  void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms);
67  // Returns the receiver system time when the frame with timestamp
68  // frame_timestamp should be rendered, assuming that the system time currently
69  // is now_ms.
70  int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
71
72  // Returns the maximum time in ms that we can wait for a frame to become
73  // complete before we must pass it to the decoder.
74  uint32_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
75
76  // Returns the current target delay which is required delay + decode time +
77  // render delay.
78  uint32_t TargetVideoDelay() const;
79
80  // Calculates whether or not there is enough time to decode a frame given a
81  // certain amount of processing time.
82  bool EnoughTimeToDecode(uint32_t available_processing_time_ms) const;
83
84  // Return current timing information.
85  void GetTimings(int* decode_ms,
86                  int* max_decode_ms,
87                  int* current_delay_ms,
88                  int* target_delay_ms,
89                  int* jitter_buffer_ms,
90                  int* min_playout_delay_ms,
91                  int* render_delay_ms) const;
92
93  enum { kDefaultRenderDelayMs = 10 };
94  enum { kDelayMaxChangeMsPerS = 100 };
95
96 protected:
97  int32_t MaxDecodeTimeMs(FrameType frame_type = kVideoFrameDelta) const
98      EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
99  int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const
100      EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
101  uint32_t TargetDelayInternal() const EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
102
103 private:
104  CriticalSectionWrapper* crit_sect_;
105  Clock* const clock_;
106  bool master_ GUARDED_BY(crit_sect_);
107  TimestampExtrapolator* ts_extrapolator_ GUARDED_BY(crit_sect_);
108  VCMCodecTimer codec_timer_ GUARDED_BY(crit_sect_);
109  uint32_t render_delay_ms_ GUARDED_BY(crit_sect_);
110  uint32_t min_playout_delay_ms_ GUARDED_BY(crit_sect_);
111  uint32_t jitter_delay_ms_ GUARDED_BY(crit_sect_);
112  uint32_t current_delay_ms_ GUARDED_BY(crit_sect_);
113  int last_decode_ms_ GUARDED_BY(crit_sect_);
114  uint32_t prev_frame_timestamp_ GUARDED_BY(crit_sect_);
115};
116}  // namespace webrtc
117
118#endif  // WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_TIMING_H_
119