blob: 764f0be174be79e51df3845bdb8841981de0df33 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_TIMING_H_
12#define MODULES_VIDEO_CODING_TIMING_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
magjed2943f012016-03-22 05:12:09 -070014#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/video_coding/codec_timer.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/thread_annotations.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000020namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000021
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000022class Clock;
wu@webrtc.org66773a02014-05-07 17:09:44 +000023class TimestampExtrapolator;
niklase@google.com470e71d2011-07-07 08:21:25 +000024
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000025class VCMTiming {
26 public:
27 // The primary timing component should be passed
28 // if this is the dual timing component.
philipel5908c712015-12-21 08:23:20 -080029 explicit VCMTiming(Clock* clock, VCMTiming* master_timing = NULL);
philipelbe7a9e52016-05-19 12:19:35 +020030 virtual ~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();
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.
isheriff6b4b5f32016-06-08 00:24:21 -070036 void set_render_delay(int 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.
isheriff6b4b5f32016-06-08 00:24:21 -070040 void SetJitterDelay(int required_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000041
Åsa Persson8368d1a2018-01-05 12:44:45 +010042 // Set/get the minimum playout delay from capture to render in ms.
isheriff6b4b5f32016-06-08 00:24:21 -070043 void set_min_playout_delay(int min_playout_delay_ms);
isheriff6b4b5f32016-06-08 00:24:21 -070044 int min_playout_delay();
45
Åsa Persson8368d1a2018-01-05 12:44:45 +010046 // Set/get the maximum playout delay from capture to render in ms.
isheriff6b4b5f32016-06-08 00:24:21 -070047 void set_max_playout_delay(int max_playout_delay_ms);
isheriff6b4b5f32016-06-08 00:24:21 -070048 int max_playout_delay();
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 // Calculates how long it has been since the previous call to this function,
52 // and increases/decreases the delay in proportion to the time difference.
53 void UpdateCurrentDelay(uint32_t frame_timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000054
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000055 // Increases or decreases the current delay to get closer to the target delay.
56 // Given the actual decode time in ms and the render time in ms for a frame,
57 // this function calculates how late the frame is and increases the delay
58 // accordingly.
59 void UpdateCurrentDelay(int64_t render_time_ms,
60 int64_t actual_decode_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000061
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000062 // Stops the decoder timer, should be called when the decoder returns a frame
63 // or when the decoded frame callback is called.
Johannes Kronbfd343b2019-07-01 10:07:50 +020064 void StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms);
65 // TODO(kron): Remove once downstream projects has been changed to use the
66 // above function.
Åsa Persson8368d1a2018-01-05 12:44:45 +010067 void StopDecodeTimer(uint32_t time_stamp,
68 int32_t decode_time_ms,
69 int64_t now_ms,
70 int64_t render_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000071
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000072 // Used to report that a frame is passed to decoding. Updates the timestamp
73 // filter which is used to map between timestamps and receiver system time.
74 void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms);
Åsa Persson8368d1a2018-01-05 12:44:45 +010075
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000076 // Returns the receiver system time when the frame with timestamp
Åsa Persson8368d1a2018-01-05 12:44:45 +010077 // |frame_timestamp| should be rendered, assuming that the system time
78 // currently is |now_ms|.
philipelbe7a9e52016-05-19 12:19:35 +020079 virtual int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000080
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000081 // Returns the maximum time in ms that we can wait for a frame to become
82 // complete before we must pass it to the decoder.
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010083 virtual int64_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000084
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000085 // Returns the current target delay which is required delay + decode time +
86 // render delay.
isheriff6b4b5f32016-06-08 00:24:21 -070087 int TargetVideoDelay() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000088
asapersson8d560882016-12-22 01:26:18 -080089 // Return current timing information. Returns true if the first frame has been
90 // decoded, false otherwise.
Johannes Kronbfd343b2019-07-01 10:07:50 +020091 virtual bool GetTimings(int* max_decode_ms,
philipela45102f2017-02-22 05:30:39 -080092 int* current_delay_ms,
93 int* target_delay_ms,
94 int* jitter_buffer_ms,
95 int* min_playout_delay_ms,
96 int* render_delay_ms) const;
fischman@webrtc.org37bb4972013-10-23 23:59:45 +000097
ilnik2edc6842017-07-06 03:06:50 -070098 void SetTimingFrameInfo(const TimingFrameInfo& info);
Danil Chapovalov0040b662018-06-18 10:48:16 +020099 absl::optional<TimingFrameInfo> GetTimingFrameInfo();
ilnik2edc6842017-07-06 03:06:50 -0700100
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000101 enum { kDefaultRenderDelayMs = 10 };
102 enum { kDelayMaxChangeMsPerS = 100 };
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000104 protected:
danilchap56359be2017-09-07 07:53:45 -0700105 int RequiredDecodeTimeMs() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
pbos@webrtc.org04221002014-07-10 15:25:37 +0000106 int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const
danilchap56359be2017-09-07 07:53:45 -0700107 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
108 int TargetDelayInternal() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000110 private:
kthelgasond701dfd2017-03-27 07:24:57 -0700111 rtc::CriticalSection crit_sect_;
pbos@webrtc.org04221002014-07-10 15:25:37 +0000112 Clock* const clock_;
danilchap56359be2017-09-07 07:53:45 -0700113 bool master_ RTC_GUARDED_BY(crit_sect_);
114 TimestampExtrapolator* ts_extrapolator_ RTC_GUARDED_BY(crit_sect_);
115 std::unique_ptr<VCMCodecTimer> codec_timer_ RTC_GUARDED_BY(crit_sect_);
116 int render_delay_ms_ RTC_GUARDED_BY(crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -0700117 // Best-effort playout delay range for frames from capture to render.
118 // The receiver tries to keep the delay between |min_playout_delay_ms_|
119 // and |max_playout_delay_ms_| taking the network jitter into account.
120 // A special case is where min_playout_delay_ms_ = max_playout_delay_ms_ = 0,
121 // in which case the receiver tries to play the frames as they arrive.
danilchap56359be2017-09-07 07:53:45 -0700122 int min_playout_delay_ms_ RTC_GUARDED_BY(crit_sect_);
123 int max_playout_delay_ms_ RTC_GUARDED_BY(crit_sect_);
124 int jitter_delay_ms_ RTC_GUARDED_BY(crit_sect_);
125 int current_delay_ms_ RTC_GUARDED_BY(crit_sect_);
danilchap56359be2017-09-07 07:53:45 -0700126 uint32_t prev_frame_timestamp_ RTC_GUARDED_BY(crit_sect_);
Danil Chapovalov0040b662018-06-18 10:48:16 +0200127 absl::optional<TimingFrameInfo> timing_frame_info_ RTC_GUARDED_BY(crit_sect_);
danilchap56359be2017-09-07 07:53:45 -0700128 size_t num_decoded_frames_ RTC_GUARDED_BY(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129};
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000130} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200132#endif // MODULES_VIDEO_CODING_TIMING_H_