blob: bfd6778c123c5bfb7ea2750f4f1cd89e756ed531 [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
magjed2943f012016-03-22 05:12:09 -070014#include <memory>
15
kthelgasond701dfd2017-03-27 07:24:57 -070016#include "webrtc/base/criticalsection.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000017#include "webrtc/base/thread_annotations.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010018#include "webrtc/modules/video_coding/codec_timer.h"
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000019#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000021namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000023class Clock;
wu@webrtc.org66773a02014-05-07 17:09:44 +000024class TimestampExtrapolator;
niklase@google.com470e71d2011-07-07 08:21:25 +000025
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000026class VCMTiming {
27 public:
28 // The primary timing component should be passed
29 // if this is the dual timing component.
philipel5908c712015-12-21 08:23:20 -080030 explicit VCMTiming(Clock* clock, VCMTiming* master_timing = NULL);
philipelbe7a9e52016-05-19 12:19:35 +020031 virtual ~VCMTiming();
niklase@google.com470e71d2011-07-07 08:21:25 +000032
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000033 // Resets the timing to the initial state.
34 void Reset();
35 void ResetDecodeTime();
niklase@google.com470e71d2011-07-07 08:21:25 +000036
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000037 // Set the amount of time needed to render an image. Defaults to 10 ms.
isheriff6b4b5f32016-06-08 00:24:21 -070038 void set_render_delay(int render_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000039
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000040 // Set the minimum time the video must be delayed on the receiver to
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000041 // get the desired jitter buffer level.
isheriff6b4b5f32016-06-08 00:24:21 -070042 void SetJitterDelay(int required_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000043
isheriff6b4b5f32016-06-08 00:24:21 -070044 // Set the minimum playout delay from capture to render in ms.
45 void set_min_playout_delay(int min_playout_delay_ms);
46
47 // Returns the minimum playout delay from capture to render in ms.
48 int min_playout_delay();
49
50 // Set the maximum playout delay from capture to render in ms.
51 void set_max_playout_delay(int max_playout_delay_ms);
52
53 // Returns the maximum playout delay from capture to render in ms.
54 int max_playout_delay();
niklase@google.com470e71d2011-07-07 08:21:25 +000055
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000056 // Increases or decreases the current delay to get closer to the target delay.
57 // Calculates how long it has been since the previous call to this function,
58 // and increases/decreases the delay in proportion to the time difference.
59 void UpdateCurrentDelay(uint32_t frame_timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000060
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000061 // Increases or decreases the current delay to get closer to the target delay.
62 // Given the actual decode time in ms and the render time in ms for a frame,
63 // this function calculates how late the frame is and increases the delay
64 // accordingly.
65 void UpdateCurrentDelay(int64_t render_time_ms,
66 int64_t actual_decode_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000068 // Stops the decoder timer, should be called when the decoder returns a frame
69 // or when the decoded frame callback is called.
70 int32_t StopDecodeTimer(uint32_t time_stamp,
Per327d8ba2015-11-10 14:00:27 +010071 int32_t decode_time_ms,
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +000072 int64_t now_ms,
73 int64_t render_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000075 // Used to report that a frame is passed to decoding. Updates the timestamp
76 // filter which is used to map between timestamps and receiver system time.
77 void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms);
78 // Returns the receiver system time when the frame with timestamp
79 // frame_timestamp should be rendered, assuming that the system time currently
80 // is now_ms.
philipelbe7a9e52016-05-19 12:19:35 +020081 virtual int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000082
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000083 // Returns the maximum time in ms that we can wait for a frame to become
84 // complete before we must pass it to the decoder.
philipelbe7a9e52016-05-19 12:19:35 +020085 virtual uint32_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000086
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000087 // Returns the current target delay which is required delay + decode time +
88 // render delay.
isheriff6b4b5f32016-06-08 00:24:21 -070089 int TargetVideoDelay() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000090
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000091 // Calculates whether or not there is enough time to decode a frame given a
92 // certain amount of processing time.
93 bool EnoughTimeToDecode(uint32_t available_processing_time_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000094
asapersson8d560882016-12-22 01:26:18 -080095 // Return current timing information. Returns true if the first frame has been
96 // decoded, false otherwise.
philipela45102f2017-02-22 05:30:39 -080097 virtual bool GetTimings(int* decode_ms,
98 int* max_decode_ms,
99 int* current_delay_ms,
100 int* target_delay_ms,
101 int* jitter_buffer_ms,
102 int* min_playout_delay_ms,
103 int* render_delay_ms) const;
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000104
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000105 enum { kDefaultRenderDelayMs = 10 };
106 enum { kDelayMaxChangeMsPerS = 100 };
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000108 protected:
isheriff6b4b5f32016-06-08 00:24:21 -0700109 int RequiredDecodeTimeMs() const EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
pbos@webrtc.org04221002014-07-10 15:25:37 +0000110 int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const
111 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -0700112 int TargetDelayInternal() const EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000114 private:
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000115 void UpdateHistograms() const;
116
kthelgasond701dfd2017-03-27 07:24:57 -0700117 rtc::CriticalSection crit_sect_;
pbos@webrtc.org04221002014-07-10 15:25:37 +0000118 Clock* const clock_;
119 bool master_ GUARDED_BY(crit_sect_);
120 TimestampExtrapolator* ts_extrapolator_ GUARDED_BY(crit_sect_);
magjed2943f012016-03-22 05:12:09 -0700121 std::unique_ptr<VCMCodecTimer> codec_timer_ GUARDED_BY(crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -0700122 int render_delay_ms_ GUARDED_BY(crit_sect_);
123 // Best-effort playout delay range for frames from capture to render.
124 // The receiver tries to keep the delay between |min_playout_delay_ms_|
125 // and |max_playout_delay_ms_| taking the network jitter into account.
126 // A special case is where min_playout_delay_ms_ = max_playout_delay_ms_ = 0,
127 // in which case the receiver tries to play the frames as they arrive.
128 int min_playout_delay_ms_ GUARDED_BY(crit_sect_);
129 int max_playout_delay_ms_ GUARDED_BY(crit_sect_);
130 int jitter_delay_ms_ GUARDED_BY(crit_sect_);
131 int current_delay_ms_ GUARDED_BY(crit_sect_);
pbos@webrtc.org04221002014-07-10 15:25:37 +0000132 int last_decode_ms_ GUARDED_BY(crit_sect_);
133 uint32_t prev_frame_timestamp_ GUARDED_BY(crit_sect_);
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000134
135 // Statistics.
136 size_t num_decoded_frames_ GUARDED_BY(crit_sect_);
137 size_t num_delayed_decoded_frames_ GUARDED_BY(crit_sect_);
138 int64_t first_decoded_frame_ms_ GUARDED_BY(crit_sect_);
139 uint64_t sum_missed_render_deadline_ms_ GUARDED_BY(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000140};
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000141} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
Henrik Kjellander2557b862015-11-18 22:00:21 +0100143#endif // WEBRTC_MODULES_VIDEO_CODING_TIMING_H_