blob: 1dca5e605afd467877b745ef8a048e07688043ba [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
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000011#ifndef WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_TIMING_H_
12#define WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_TIMING_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000014#include "webrtc/modules/video_coding/main/source/codec_timer.h"
15#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
16#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000018namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000019
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000020class Clock;
wu@webrtc.org66773a02014-05-07 17:09:44 +000021class TimestampExtrapolator;
niklase@google.com470e71d2011-07-07 08:21:25 +000022
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000023class VCMTiming {
24 public:
25 // The primary timing component should be passed
26 // if this is the dual timing component.
27 VCMTiming(Clock* clock,
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000028 VCMTiming* master_timing = NULL);
29 ~VCMTiming();
niklase@google.com470e71d2011-07-07 08:21:25 +000030
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000031 // Resets the timing to the initial state.
32 void Reset();
33 void ResetDecodeTime();
niklase@google.com470e71d2011-07-07 08:21:25 +000034
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000035 // Set the amount of time needed to render an image. Defaults to 10 ms.
36 void set_render_delay(uint32_t render_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000037
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000038 // Set the minimum time the video must be delayed on the receiver to
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000039 // get the desired jitter buffer level.
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000040 void SetJitterDelay(uint32_t required_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000041
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000042 // Set the minimum playout delay required to sync video with audio.
43 void set_min_playout_delay(uint32_t min_playout_delay);
niklase@google.com470e71d2011-07-07 08:21:25 +000044
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000045 // Increases or decreases the current delay to get closer to the target delay.
46 // Calculates how long it has been since the previous call to this function,
47 // and increases/decreases the delay in proportion to the time difference.
48 void UpdateCurrentDelay(uint32_t frame_timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000049
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000050 // Increases or decreases the current delay to get closer to the target delay.
51 // Given the actual decode time in ms and the render time in ms for a frame,
52 // this function calculates how late the frame is and increases the delay
53 // accordingly.
54 void UpdateCurrentDelay(int64_t render_time_ms,
55 int64_t actual_decode_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000057 // Stops the decoder timer, should be called when the decoder returns a frame
58 // or when the decoded frame callback is called.
59 int32_t StopDecodeTimer(uint32_t time_stamp,
60 int64_t start_time_ms,
61 int64_t now_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000063 // Used to report that a frame is passed to decoding. Updates the timestamp
64 // filter which is used to map between timestamps and receiver system time.
65 void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms);
66 // Returns the receiver system time when the frame with timestamp
67 // frame_timestamp should be rendered, assuming that the system time currently
68 // is now_ms.
69 int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000070
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000071 // Returns the maximum time in ms that we can wait for a frame to become
72 // complete before we must pass it to the decoder.
73 uint32_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000074
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000075 // Returns the current target delay which is required delay + decode time +
76 // render delay.
77 uint32_t TargetVideoDelay() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000078
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000079 // Calculates whether or not there is enough time to decode a frame given a
80 // certain amount of processing time.
81 bool EnoughTimeToDecode(uint32_t available_processing_time_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000082
fischman@webrtc.org37bb4972013-10-23 23:59:45 +000083 // Return current timing information.
84 void GetTimings(int* decode_ms,
85 int* max_decode_ms,
86 int* current_delay_ms,
87 int* target_delay_ms,
88 int* jitter_buffer_ms,
89 int* min_playout_delay_ms,
90 int* render_delay_ms) const;
91
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000092 enum { kDefaultRenderDelayMs = 10 };
93 enum { kDelayMaxChangeMsPerS = 100 };
niklase@google.com470e71d2011-07-07 08:21:25 +000094
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000095 protected:
96 int32_t MaxDecodeTimeMs(FrameType frame_type = kVideoFrameDelta) const;
97 int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const;
98 uint32_t TargetDelayInternal() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000099
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000100 private:
101 CriticalSectionWrapper* crit_sect_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000102 Clock* clock_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000103 bool master_;
wu@webrtc.org66773a02014-05-07 17:09:44 +0000104 TimestampExtrapolator* ts_extrapolator_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000105 VCMCodecTimer codec_timer_;
106 uint32_t render_delay_ms_;
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000107 uint32_t min_playout_delay_ms_;
108 uint32_t jitter_delay_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000109 uint32_t current_delay_ms_;
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000110 int last_decode_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000111 uint32_t prev_frame_timestamp_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112};
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000113} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000115#endif // WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_TIMING_H_