blob: 46681a3a0733528f8478abc941f05b696716a58e [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
Henrik Kjellander2557b862015-11-18 22:00:21 +010011#ifndef WEBRTC_MODULES_VIDEO_CODING_TIMING_H_
12#define WEBRTC_MODULES_VIDEO_CODING_TIMING_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000014#include "webrtc/base/thread_annotations.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010015#include "webrtc/modules/video_coding/codec_timer.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010016#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000017#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000019namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000020
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000021class Clock;
wu@webrtc.org66773a02014-05-07 17:09:44 +000022class TimestampExtrapolator;
niklase@google.com470e71d2011-07-07 08:21:25 +000023
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000024class VCMTiming {
25 public:
26 // The primary timing component should be passed
27 // if this is the dual timing component.
28 VCMTiming(Clock* clock,
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000029 VCMTiming* master_timing = NULL);
30 ~VCMTiming();
niklase@google.com470e71d2011-07-07 08:21:25 +000031
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000032 // Resets the timing to the initial state.
33 void Reset();
34 void ResetDecodeTime();
niklase@google.com470e71d2011-07-07 08:21:25 +000035
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000036 // Set the amount of time needed to render an image. Defaults to 10 ms.
37 void set_render_delay(uint32_t render_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000038
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000039 // Set the minimum time the video must be delayed on the receiver to
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000040 // get the desired jitter buffer level.
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000041 void SetJitterDelay(uint32_t required_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000042
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000043 // Set the minimum playout delay required to sync video with audio.
44 void set_min_playout_delay(uint32_t min_playout_delay);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000046 // 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);
niklase@google.com470e71d2011-07-07 08:21:25 +000050
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000051 // 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);
niklase@google.com470e71d2011-07-07 08:21:25 +000057
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000058 // 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,
Per327d8ba2015-11-10 14:00:27 +010061 int32_t decode_time_ms,
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +000062 int64_t now_ms,
63 int64_t render_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000064
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +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.
67 void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms);
68 // Returns the receiver system time when the frame with timestamp
69 // frame_timestamp should be rendered, assuming that the system time currently
70 // is now_ms.
71 int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000072
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000073 // Returns the maximum time in ms that we can wait for a frame to become
74 // complete before we must pass it to the decoder.
75 uint32_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000076
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000077 // Returns the current target delay which is required delay + decode time +
78 // render delay.
79 uint32_t TargetVideoDelay() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000080
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000081 // Calculates whether or not there is enough time to decode a frame given a
82 // certain amount of processing time.
83 bool EnoughTimeToDecode(uint32_t available_processing_time_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000084
fischman@webrtc.org37bb4972013-10-23 23:59:45 +000085 // Return current timing information.
86 void GetTimings(int* decode_ms,
87 int* max_decode_ms,
88 int* current_delay_ms,
89 int* target_delay_ms,
90 int* jitter_buffer_ms,
91 int* min_playout_delay_ms,
92 int* render_delay_ms) const;
93
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000094 enum { kDefaultRenderDelayMs = 10 };
95 enum { kDelayMaxChangeMsPerS = 100 };
niklase@google.com470e71d2011-07-07 08:21:25 +000096
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000097 protected:
pbos@webrtc.org04221002014-07-10 15:25:37 +000098 int32_t MaxDecodeTimeMs(FrameType frame_type = kVideoFrameDelta) const
99 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
100 int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const
101 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
102 uint32_t TargetDelayInternal() const EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000104 private:
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000105 void UpdateHistograms() const;
106
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000107 CriticalSectionWrapper* crit_sect_;
pbos@webrtc.org04221002014-07-10 15:25:37 +0000108 Clock* const clock_;
109 bool master_ GUARDED_BY(crit_sect_);
110 TimestampExtrapolator* ts_extrapolator_ GUARDED_BY(crit_sect_);
111 VCMCodecTimer codec_timer_ GUARDED_BY(crit_sect_);
112 uint32_t render_delay_ms_ GUARDED_BY(crit_sect_);
113 uint32_t min_playout_delay_ms_ GUARDED_BY(crit_sect_);
114 uint32_t jitter_delay_ms_ GUARDED_BY(crit_sect_);
115 uint32_t current_delay_ms_ GUARDED_BY(crit_sect_);
116 int last_decode_ms_ GUARDED_BY(crit_sect_);
117 uint32_t prev_frame_timestamp_ GUARDED_BY(crit_sect_);
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000118
119 // Statistics.
120 size_t num_decoded_frames_ GUARDED_BY(crit_sect_);
121 size_t num_delayed_decoded_frames_ GUARDED_BY(crit_sect_);
122 int64_t first_decoded_frame_ms_ GUARDED_BY(crit_sect_);
123 uint64_t sum_missed_render_deadline_ms_ GUARDED_BY(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000124};
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000125} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
Henrik Kjellander2557b862015-11-18 22:00:21 +0100127#endif // WEBRTC_MODULES_VIDEO_CODING_TIMING_H_