blob: 6ee1cf4d6f4504d2233319241619eaac654e18f6 [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
Rasmus Brandtc4d253c2022-05-25 12:03:35 +020011#ifndef MODULES_VIDEO_CODING_TIMING_TIMING_H_
12#define MODULES_VIDEO_CODING_TIMING_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"
Jonas Orelande62c2f22022-03-29 11:04:48 +020017#include "api/field_trials_view.h"
Johannes Kron985905d2021-06-29 11:37:06 +020018#include "api/units/time_delta.h"
Johannes Kronbbf639e2022-06-15 12:27:23 +020019#include "api/video/video_frame.h"
Niels Möller834a5542019-09-23 10:31:16 +020020#include "api/video/video_timing.h"
Rasmus Brandt23772262022-05-23 09:53:15 +020021#include "modules/video_coding/timing/codec_timer.h"
Johannes Kron111e9812020-10-26 13:54:40 +010022#include "rtc_base/experiments/field_trial_parser.h"
Markus Handell6deec382020-07-07 12:17:12 +020023#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread_annotations.h"
Niels Möller043725f2020-10-30 10:44:52 +010025#include "rtc_base/time/timestamp_extrapolator.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000027namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000028
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000029class Clock;
wu@webrtc.org66773a02014-05-07 17:09:44 +000030class TimestampExtrapolator;
niklase@google.com470e71d2011-07-07 08:21:25 +000031
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000032class VCMTiming {
33 public:
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010034 static constexpr auto kDefaultRenderDelay = TimeDelta::Millis(10);
35 static constexpr auto kDelayMaxChangeMsPerS = 100;
36
Jonas Orelande62c2f22022-03-29 11:04:48 +020037 VCMTiming(Clock* clock, const FieldTrialsView& field_trials);
Niels Möller043725f2020-10-30 10:44:52 +010038 virtual ~VCMTiming() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +000039
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000040 // Resets the timing to the initial state.
41 void Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000042
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000043 // Set the amount of time needed to render an image. Defaults to 10 ms.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010044 void set_render_delay(TimeDelta render_delay);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000046 // Set the minimum time the video must be delayed on the receiver to
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000047 // get the desired jitter buffer level.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010048 void SetJitterDelay(TimeDelta required_delay);
niklase@google.com470e71d2011-07-07 08:21:25 +000049
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010050 // Set/get the minimum playout delay from capture to render.
Evan Shrubsoleeabaf8d2022-05-23 17:45:58 +020051 TimeDelta min_playout_delay() const;
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010052 void set_min_playout_delay(TimeDelta min_playout_delay);
isheriff6b4b5f32016-06-08 00:24:21 -070053
Åsa Persson8368d1a2018-01-05 12:44:45 +010054 // Set/get the maximum playout delay from capture to render in ms.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010055 void set_max_playout_delay(TimeDelta max_playout_delay);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000057 // Increases or decreases the current delay to get closer to the target delay.
58 // Calculates how long it has been since the previous call to this function,
59 // and increases/decreases the delay in proportion to the time difference.
60 void UpdateCurrentDelay(uint32_t frame_timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000061
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000062 // Increases or decreases the current delay to get closer to the target delay.
63 // Given the actual decode time in ms and the render time in ms for a frame,
64 // this function calculates how late the frame is and increases the delay
65 // accordingly.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010066 void UpdateCurrentDelay(Timestamp render_time, Timestamp actual_decode_time);
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.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010070 void StopDecodeTimer(TimeDelta decode_time, Timestamp now);
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.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010074 void IncomingTimestamp(uint32_t rtp_timestamp, Timestamp last_packet_time);
Å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
Artem Titovdcd7fc72021-08-09 13:02:57 +020077 // `frame_timestamp` should be rendered, assuming that the system time
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010078 // currently is `now`.
79 virtual Timestamp RenderTime(uint32_t frame_timestamp, Timestamp now) 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
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010082 // complete before we must pass it to the decoder. render_time==0 indicates
Johannes Kron2ddc39e2021-08-10 16:56:12 +020083 // that the frames should be processed as quickly as possible, with possibly
84 // only a small delay added to make sure that the decoder is not overloaded.
85 // In this case, the parameter too_many_frames_queued is used to signal that
86 // the decode queue is full and that the frame should be decoded as soon as
87 // possible.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010088 virtual TimeDelta MaxWaitingTime(Timestamp render_time,
89 Timestamp now,
90 bool too_many_frames_queued) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000091
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000092 // Returns the current target delay which is required delay + decode time +
93 // render delay.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010094 TimeDelta TargetVideoDelay() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000095
asapersson8d560882016-12-22 01:26:18 -080096 // Return current timing information. Returns true if the first frame has been
97 // decoded, false otherwise.
Evan Shrubsole92e89d72022-03-22 10:55:15 +010098 struct VideoDelayTimings {
99 TimeDelta max_decode_duration;
100 TimeDelta current_delay;
101 TimeDelta target_delay;
102 TimeDelta jitter_buffer_delay;
103 TimeDelta min_playout_delay;
Evan Shrubsole8f1159b2022-03-22 12:12:17 +0100104 TimeDelta max_playout_delay;
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100105 TimeDelta render_delay;
106 size_t num_decoded_frames;
107 };
108 VideoDelayTimings GetTimings() const;
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000109
ilnik2edc6842017-07-06 03:06:50 -0700110 void SetTimingFrameInfo(const TimingFrameInfo& info);
Danil Chapovalov0040b662018-06-18 10:48:16 +0200111 absl::optional<TimingFrameInfo> GetTimingFrameInfo();
ilnik2edc6842017-07-06 03:06:50 -0700112
Johannes Kron111e9812020-10-26 13:54:40 +0100113 void SetMaxCompositionDelayInFrames(
114 absl::optional<int> max_composition_delay_in_frames);
Johannes Kronbbf639e2022-06-15 12:27:23 +0200115
116 VideoFrame::RenderParameters RenderParameters() const;
Johannes Kron111e9812020-10-26 13:54:40 +0100117
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700118 // Updates the last time a frame was scheduled for decoding.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100119 void SetLastDecodeScheduledTimestamp(Timestamp last_decode_scheduled);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000121 protected:
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100122 TimeDelta RequiredDecodeTime() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
123 Timestamp RenderTimeInternal(uint32_t frame_timestamp, Timestamp now) const
Markus Handell6deec382020-07-07 12:17:12 +0200124 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100125 TimeDelta TargetDelayInternal() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Johannes Kronbbf639e2022-06-15 12:27:23 +0200126 bool UseLowLatencyRendering() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000128 private:
Markus Handell6deec382020-07-07 12:17:12 +0200129 mutable Mutex mutex_;
pbos@webrtc.org04221002014-07-10 15:25:37 +0000130 Clock* const clock_;
Niels Möller043725f2020-10-30 10:44:52 +0100131 const std::unique_ptr<TimestampExtrapolator> ts_extrapolator_
Niels Möllerd3a3e9e2020-10-28 15:15:55 +0100132 RTC_PT_GUARDED_BY(mutex_);
Rasmus Brandt23772262022-05-23 09:53:15 +0200133 std::unique_ptr<CodecTimer> codec_timer_ RTC_GUARDED_BY(mutex_)
Niels Möllerd3a3e9e2020-10-28 15:15:55 +0100134 RTC_PT_GUARDED_BY(mutex_);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100135 TimeDelta render_delay_ RTC_GUARDED_BY(mutex_);
isheriff6b4b5f32016-06-08 00:24:21 -0700136 // Best-effort playout delay range for frames from capture to render.
Artem Titovdcd7fc72021-08-09 13:02:57 +0200137 // The receiver tries to keep the delay between `min_playout_delay_ms_`
138 // and `max_playout_delay_ms_` taking the network jitter into account.
isheriff6b4b5f32016-06-08 00:24:21 -0700139 // A special case is where min_playout_delay_ms_ = max_playout_delay_ms_ = 0,
140 // in which case the receiver tries to play the frames as they arrive.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100141 TimeDelta min_playout_delay_ RTC_GUARDED_BY(mutex_);
142 TimeDelta max_playout_delay_ RTC_GUARDED_BY(mutex_);
143 TimeDelta jitter_delay_ RTC_GUARDED_BY(mutex_);
144 TimeDelta current_delay_ RTC_GUARDED_BY(mutex_);
Markus Handell6deec382020-07-07 12:17:12 +0200145 uint32_t prev_frame_timestamp_ RTC_GUARDED_BY(mutex_);
146 absl::optional<TimingFrameInfo> timing_frame_info_ RTC_GUARDED_BY(mutex_);
147 size_t num_decoded_frames_ RTC_GUARDED_BY(mutex_);
Johannes Kron111e9812020-10-26 13:54:40 +0100148 absl::optional<int> max_composition_delay_in_frames_ RTC_GUARDED_BY(mutex_);
Johannes Kron985905d2021-06-29 11:37:06 +0200149 // Set by the field trial WebRTC-ZeroPlayoutDelay. The parameter min_pacing
150 // determines the minimum delay between frames scheduled for decoding that is
151 // used when min playout delay=0 and max playout delay>=0.
152 FieldTrialParameter<TimeDelta> zero_playout_delay_min_pacing_
153 RTC_GUARDED_BY(mutex_);
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700154 // Timestamp at which the last frame was scheduled to be sent to the decoder.
Johannes Kron985905d2021-06-29 11:37:06 +0200155 // Used only when the RTP header extension playout delay is set to min=0 ms
156 // which is indicated by a render time set to 0.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100157 Timestamp last_decode_scheduled_ RTC_GUARDED_BY(mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000158};
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000159} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000160
Rasmus Brandtc4d253c2022-05-25 12:03:35 +0200161#endif // MODULES_VIDEO_CODING_TIMING_TIMING_H_