1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
11389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org#ifndef WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_TIMING_H_
12389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org#define WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_TIMING_H_
13b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
14389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org#include "webrtc/modules/video_coding/main/source/codec_timer.h"
15389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
16389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org#include "webrtc/typedefs.h"
17b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
18389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.orgnamespace webrtc {
19b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
201bb2146351979b6610107419b2a9c86cca2692a3stefan@webrtc.orgclass Clock;
21d2fb259b3bc61c68f368929510215a7ee7d00fdawu@webrtc.orgclass TimestampExtrapolator;
22b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
23389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.orgclass VCMTiming {
24389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org public:
25389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // The primary timing component should be passed
26389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // if this is the dual timing component.
27389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  VCMTiming(Clock* clock,
28389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org            VCMTiming* master_timing = NULL);
29389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  ~VCMTiming();
30389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
31389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Resets the timing to the initial state.
32389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  void Reset();
33389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  void ResetDecodeTime();
34389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
35e4775746d3e4d88591ed1acce4c82bd6b0754e8amikhal@webrtc.org  // Set the amount of time needed to render an image. Defaults to 10 ms.
36e4775746d3e4d88591ed1acce4c82bd6b0754e8amikhal@webrtc.org  void set_render_delay(uint32_t render_delay_ms);
37389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
38e4775746d3e4d88591ed1acce4c82bd6b0754e8amikhal@webrtc.org  // Set the minimum time the video must be delayed on the receiver to
39389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // get the desired jitter buffer level.
40e4775746d3e4d88591ed1acce4c82bd6b0754e8amikhal@webrtc.org  void SetJitterDelay(uint32_t required_delay_ms);
41389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
42e4775746d3e4d88591ed1acce4c82bd6b0754e8amikhal@webrtc.org  // Set the minimum playout delay required to sync video with audio.
43e4775746d3e4d88591ed1acce4c82bd6b0754e8amikhal@webrtc.org  void set_min_playout_delay(uint32_t min_playout_delay);
44389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
45389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Increases or decreases the current delay to get closer to the target delay.
46389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Calculates how long it has been since the previous call to this function,
47389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // and increases/decreases the delay in proportion to the time difference.
48389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  void UpdateCurrentDelay(uint32_t frame_timestamp);
49389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
50389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Increases or decreases the current delay to get closer to the target delay.
51389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Given the actual decode time in ms and the render time in ms for a frame,
52389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // this function calculates how late the frame is and increases the delay
53389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // accordingly.
54389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  void UpdateCurrentDelay(int64_t render_time_ms,
55389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org                          int64_t actual_decode_time_ms);
56389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
57389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Stops the decoder timer, should be called when the decoder returns a frame
58389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // or when the decoded frame callback is called.
59389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  int32_t StopDecodeTimer(uint32_t time_stamp,
60389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org                          int64_t start_time_ms,
61389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org                          int64_t now_ms);
62389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
63389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Used to report that a frame is passed to decoding. Updates the timestamp
64389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // filter which is used to map between timestamps and receiver system time.
65389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms);
66389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Returns the receiver system time when the frame with timestamp
67389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // frame_timestamp should be rendered, assuming that the system time currently
68389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // is now_ms.
69389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
70389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
71389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Returns the maximum time in ms that we can wait for a frame to become
72389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // complete before we must pass it to the decoder.
73389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  uint32_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
74389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
75389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Returns the current target delay which is required delay + decode time +
76389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // render delay.
77389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  uint32_t TargetVideoDelay() const;
78389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
79389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // Calculates whether or not there is enough time to decode a frame given a
80389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  // certain amount of processing time.
81389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  bool EnoughTimeToDecode(uint32_t available_processing_time_ms) const;
82389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
83ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org  // Return current timing information.
84ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org  void GetTimings(int* decode_ms,
85ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org                  int* max_decode_ms,
86ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org                  int* current_delay_ms,
87ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org                  int* target_delay_ms,
88ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org                  int* jitter_buffer_ms,
89ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org                  int* min_playout_delay_ms,
90ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org                  int* render_delay_ms) const;
91ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org
92389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  enum { kDefaultRenderDelayMs = 10 };
93389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  enum { kDelayMaxChangeMsPerS = 100 };
94389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
95389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org protected:
96389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  int32_t MaxDecodeTimeMs(FrameType frame_type = kVideoFrameDelta) const;
97389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const;
98389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  uint32_t TargetDelayInternal() const;
99389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org
100389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org private:
101389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  CriticalSectionWrapper* crit_sect_;
102389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  Clock* clock_;
103389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  bool master_;
104d2fb259b3bc61c68f368929510215a7ee7d00fdawu@webrtc.org  TimestampExtrapolator* ts_extrapolator_;
105389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  VCMCodecTimer codec_timer_;
106389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  uint32_t render_delay_ms_;
107e4775746d3e4d88591ed1acce4c82bd6b0754e8amikhal@webrtc.org  uint32_t min_playout_delay_ms_;
108e4775746d3e4d88591ed1acce4c82bd6b0754e8amikhal@webrtc.org  uint32_t jitter_delay_ms_;
109389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  uint32_t current_delay_ms_;
110ecfef1930d59e5b7dc14e71b1ac3ed892980d151fischman@webrtc.org  int last_decode_ms_;
111389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org  uint32_t prev_frame_timestamp_;
112b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
113389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org}  // namespace webrtc
114b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
115389bb404a8c8f40baff45720ed16682d3b63982emikhal@webrtc.org#endif  // WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_TIMING_H_
116