blob: 69352de93ac6be483428b3b777edfe948d28ce0b [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
Niels Möller834a5542019-09-23 10:31:16 +020016#include "absl/types/optional.h"
17#include "api/video/video_timing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/codec_timer.h"
Johannes Kron111e9812020-10-26 13:54:40 +010019#include "rtc_base/experiments/field_trial_parser.h"
Markus Handell6deec382020-07-07 12:17:12 +020020#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/thread_annotations.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000023namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000025class Clock;
wu@webrtc.org66773a02014-05-07 17:09:44 +000026class TimestampExtrapolator;
niklase@google.com470e71d2011-07-07 08:21:25 +000027
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000028class VCMTiming {
29 public:
30 // The primary timing component should be passed
31 // if this is the dual timing component.
philipel5908c712015-12-21 08:23:20 -080032 explicit VCMTiming(Clock* clock, VCMTiming* master_timing = NULL);
philipelbe7a9e52016-05-19 12:19:35 +020033 virtual ~VCMTiming();
niklase@google.com470e71d2011-07-07 08:21:25 +000034
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000035 // Resets the timing to the initial state.
36 void Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000037
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000038 // Set the amount of time needed to render an image. Defaults to 10 ms.
isheriff6b4b5f32016-06-08 00:24:21 -070039 void set_render_delay(int render_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000040
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000041 // Set the minimum time the video must be delayed on the receiver to
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000042 // get the desired jitter buffer level.
isheriff6b4b5f32016-06-08 00:24:21 -070043 void SetJitterDelay(int required_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000044
Åsa Persson8368d1a2018-01-05 12:44:45 +010045 // Set/get the minimum playout delay from capture to render in ms.
isheriff6b4b5f32016-06-08 00:24:21 -070046 void set_min_playout_delay(int min_playout_delay_ms);
isheriff6b4b5f32016-06-08 00:24:21 -070047 int min_playout_delay();
48
Åsa Persson8368d1a2018-01-05 12:44:45 +010049 // Set/get the maximum playout delay from capture to render in ms.
isheriff6b4b5f32016-06-08 00:24:21 -070050 void set_max_playout_delay(int max_playout_delay_ms);
isheriff6b4b5f32016-06-08 00:24:21 -070051 int max_playout_delay();
niklase@google.com470e71d2011-07-07 08:21:25 +000052
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000053 // Increases or decreases the current delay to get closer to the target delay.
54 // Calculates how long it has been since the previous call to this function,
55 // and increases/decreases the delay in proportion to the time difference.
56 void UpdateCurrentDelay(uint32_t frame_timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000057
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000058 // Increases or decreases the current delay to get closer to the target delay.
59 // Given the actual decode time in ms and the render time in ms for a frame,
60 // this function calculates how late the frame is and increases the delay
61 // accordingly.
62 void UpdateCurrentDelay(int64_t render_time_ms,
63 int64_t actual_decode_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000064
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000065 // Stops the decoder timer, should be called when the decoder returns a frame
66 // or when the decoded frame callback is called.
Johannes Kronbfd343b2019-07-01 10:07:50 +020067 void StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms);
68 // TODO(kron): Remove once downstream projects has been changed to use the
69 // above function.
Åsa Persson8368d1a2018-01-05 12:44:45 +010070 void StopDecodeTimer(uint32_t time_stamp,
71 int32_t decode_time_ms,
72 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);
Åsa Persson8368d1a2018-01-05 12:44:45 +010078
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000079 // Returns the receiver system time when the frame with timestamp
Åsa Persson8368d1a2018-01-05 12:44:45 +010080 // |frame_timestamp| should be rendered, assuming that the system time
81 // currently is |now_ms|.
philipelbe7a9e52016-05-19 12:19:35 +020082 virtual int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000083
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000084 // Returns the maximum time in ms that we can wait for a frame to become
85 // complete before we must pass it to the decoder.
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010086 virtual int64_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000087
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000088 // Returns the current target delay which is required delay + decode time +
89 // render delay.
isheriff6b4b5f32016-06-08 00:24:21 -070090 int TargetVideoDelay() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000091
asapersson8d560882016-12-22 01:26:18 -080092 // Return current timing information. Returns true if the first frame has been
93 // decoded, false otherwise.
Johannes Kronbfd343b2019-07-01 10:07:50 +020094 virtual bool GetTimings(int* max_decode_ms,
philipela45102f2017-02-22 05:30:39 -080095 int* current_delay_ms,
96 int* target_delay_ms,
97 int* jitter_buffer_ms,
98 int* min_playout_delay_ms,
99 int* render_delay_ms) const;
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000100
ilnik2edc6842017-07-06 03:06:50 -0700101 void SetTimingFrameInfo(const TimingFrameInfo& info);
Danil Chapovalov0040b662018-06-18 10:48:16 +0200102 absl::optional<TimingFrameInfo> GetTimingFrameInfo();
ilnik2edc6842017-07-06 03:06:50 -0700103
Johannes Kron111e9812020-10-26 13:54:40 +0100104 void SetMaxCompositionDelayInFrames(
105 absl::optional<int> max_composition_delay_in_frames);
106 absl::optional<int> MaxCompositionDelayInFrames() const;
107
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000108 enum { kDefaultRenderDelayMs = 10 };
109 enum { kDelayMaxChangeMsPerS = 100 };
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000111 protected:
Markus Handell6deec382020-07-07 12:17:12 +0200112 int RequiredDecodeTimeMs() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
pbos@webrtc.org04221002014-07-10 15:25:37 +0000113 int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const
Markus Handell6deec382020-07-07 12:17:12 +0200114 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
115 int TargetDelayInternal() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000117 private:
Markus Handell6deec382020-07-07 12:17:12 +0200118 mutable Mutex mutex_;
pbos@webrtc.org04221002014-07-10 15:25:37 +0000119 Clock* const clock_;
Markus Handell6deec382020-07-07 12:17:12 +0200120 bool master_ RTC_GUARDED_BY(mutex_);
Niels Möllerd3a3e9e2020-10-28 15:15:55 +0100121 TimestampExtrapolator* ts_extrapolator_ RTC_GUARDED_BY(mutex_)
122 RTC_PT_GUARDED_BY(mutex_);
123 std::unique_ptr<VCMCodecTimer> codec_timer_ RTC_GUARDED_BY(mutex_)
124 RTC_PT_GUARDED_BY(mutex_);
Markus Handell6deec382020-07-07 12:17:12 +0200125 int render_delay_ms_ RTC_GUARDED_BY(mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -0700126 // Best-effort playout delay range for frames from capture to render.
127 // The receiver tries to keep the delay between |min_playout_delay_ms_|
128 // and |max_playout_delay_ms_| taking the network jitter into account.
129 // A special case is where min_playout_delay_ms_ = max_playout_delay_ms_ = 0,
130 // in which case the receiver tries to play the frames as they arrive.
Markus Handell6deec382020-07-07 12:17:12 +0200131 int min_playout_delay_ms_ RTC_GUARDED_BY(mutex_);
132 int max_playout_delay_ms_ RTC_GUARDED_BY(mutex_);
133 int jitter_delay_ms_ RTC_GUARDED_BY(mutex_);
134 int current_delay_ms_ RTC_GUARDED_BY(mutex_);
135 uint32_t prev_frame_timestamp_ RTC_GUARDED_BY(mutex_);
136 absl::optional<TimingFrameInfo> timing_frame_info_ RTC_GUARDED_BY(mutex_);
137 size_t num_decoded_frames_ RTC_GUARDED_BY(mutex_);
Johannes Kron111e9812020-10-26 13:54:40 +0100138 // Set by the field trial WebRTC-LowLatencyRenderer. The parameter enabled
139 // determines if the low-latency renderer algorithm should be used for the
140 // case min playout delay=0 and max playout delay>0.
141 FieldTrialParameter<bool> low_latency_renderer_enabled_
142 RTC_GUARDED_BY(mutex_);
143 absl::optional<int> max_composition_delay_in_frames_ RTC_GUARDED_BY(mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144};
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000145} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200147#endif // MODULES_VIDEO_CODING_TIMING_H_