niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_VIDEO_CODING_TIMING_H_ |
| 12 | #define MODULES_VIDEO_CODING_TIMING_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Niels Möller | 834a554 | 2019-09-23 10:31:16 +0200 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 17 | #include "api/units/time_delta.h" |
Niels Möller | 834a554 | 2019-09-23 10:31:16 +0200 | [diff] [blame] | 18 | #include "api/video/video_timing.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/video_coding/codec_timer.h" |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 20 | #include "rtc_base/experiments/field_trial_parser.h" |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 21 | #include "rtc_base/synchronization/mutex.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/thread_annotations.h" |
Niels Möller | 043725f | 2020-10-30 10:44:52 +0100 | [diff] [blame] | 23 | #include "rtc_base/time/timestamp_extrapolator.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 25 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 27 | class Clock; |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 28 | class TimestampExtrapolator; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 30 | class VCMTiming { |
| 31 | public: |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 32 | static constexpr auto kDefaultRenderDelay = TimeDelta::Millis(10); |
| 33 | static constexpr auto kDelayMaxChangeMsPerS = 100; |
| 34 | |
Niels Möller | 043725f | 2020-10-30 10:44:52 +0100 | [diff] [blame] | 35 | explicit VCMTiming(Clock* clock); |
| 36 | virtual ~VCMTiming() = default; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 38 | // Resets the timing to the initial state. |
| 39 | void Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 41 | // Set the amount of time needed to render an image. Defaults to 10 ms. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 42 | void set_render_delay(TimeDelta render_delay); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 44 | // Set the minimum time the video must be delayed on the receiver to |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 45 | // get the desired jitter buffer level. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 46 | void SetJitterDelay(TimeDelta required_delay); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 48 | // Set/get the minimum playout delay from capture to render. |
| 49 | void set_min_playout_delay(TimeDelta min_playout_delay); |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 50 | |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 51 | // Set/get the maximum playout delay from capture to render in ms. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 52 | void set_max_playout_delay(TimeDelta max_playout_delay); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 54 | // Increases or decreases the current delay to get closer to the target delay. |
| 55 | // Calculates how long it has been since the previous call to this function, |
| 56 | // and increases/decreases the delay in proportion to the time difference. |
| 57 | void UpdateCurrentDelay(uint32_t frame_timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 59 | // Increases or decreases the current delay to get closer to the target delay. |
| 60 | // Given the actual decode time in ms and the render time in ms for a frame, |
| 61 | // this function calculates how late the frame is and increases the delay |
| 62 | // accordingly. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 63 | void UpdateCurrentDelay(Timestamp render_time, Timestamp actual_decode_time); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 65 | // Stops the decoder timer, should be called when the decoder returns a frame |
| 66 | // or when the decoded frame callback is called. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 67 | void StopDecodeTimer(TimeDelta decode_time, Timestamp now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 69 | // Used to report that a frame is passed to decoding. Updates the timestamp |
| 70 | // filter which is used to map between timestamps and receiver system time. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 71 | void IncomingTimestamp(uint32_t rtp_timestamp, Timestamp last_packet_time); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 72 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 73 | // Returns the receiver system time when the frame with timestamp |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 74 | // `frame_timestamp` should be rendered, assuming that the system time |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 75 | // currently is `now`. |
| 76 | virtual Timestamp RenderTime(uint32_t frame_timestamp, Timestamp now) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 78 | // Returns the maximum time in ms that we can wait for a frame to become |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 79 | // complete before we must pass it to the decoder. render_time==0 indicates |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 80 | // that the frames should be processed as quickly as possible, with possibly |
| 81 | // only a small delay added to make sure that the decoder is not overloaded. |
| 82 | // In this case, the parameter too_many_frames_queued is used to signal that |
| 83 | // the decode queue is full and that the frame should be decoded as soon as |
| 84 | // possible. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 85 | virtual TimeDelta MaxWaitingTime(Timestamp render_time, |
| 86 | Timestamp now, |
| 87 | bool too_many_frames_queued) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 89 | // Returns the current target delay which is required delay + decode time + |
| 90 | // render delay. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 91 | TimeDelta TargetVideoDelay() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | |
asapersson | 8d56088 | 2016-12-22 01:26:18 -0800 | [diff] [blame] | 93 | // Return current timing information. Returns true if the first frame has been |
| 94 | // decoded, false otherwise. |
Evan Shrubsole | 92e89d7 | 2022-03-22 10:55:15 +0100 | [diff] [blame] | 95 | struct VideoDelayTimings { |
| 96 | TimeDelta max_decode_duration; |
| 97 | TimeDelta current_delay; |
| 98 | TimeDelta target_delay; |
| 99 | TimeDelta jitter_buffer_delay; |
| 100 | TimeDelta min_playout_delay; |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame^] | 101 | TimeDelta max_playout_delay; |
Evan Shrubsole | 92e89d7 | 2022-03-22 10:55:15 +0100 | [diff] [blame] | 102 | TimeDelta render_delay; |
| 103 | size_t num_decoded_frames; |
| 104 | }; |
| 105 | VideoDelayTimings GetTimings() const; |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 106 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 107 | void SetTimingFrameInfo(const TimingFrameInfo& info); |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 108 | absl::optional<TimingFrameInfo> GetTimingFrameInfo(); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 109 | |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 110 | void SetMaxCompositionDelayInFrames( |
| 111 | absl::optional<int> max_composition_delay_in_frames); |
| 112 | absl::optional<int> MaxCompositionDelayInFrames() const; |
| 113 | |
Rezaul Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 114 | // Updates the last time a frame was scheduled for decoding. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 115 | void SetLastDecodeScheduledTimestamp(Timestamp last_decode_scheduled); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 117 | protected: |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 118 | TimeDelta RequiredDecodeTime() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 119 | Timestamp RenderTimeInternal(uint32_t frame_timestamp, Timestamp now) const |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 120 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 121 | TimeDelta TargetDelayInternal() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 123 | private: |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 124 | mutable Mutex mutex_; |
pbos@webrtc.org | 0422100 | 2014-07-10 15:25:37 +0000 | [diff] [blame] | 125 | Clock* const clock_; |
Niels Möller | 043725f | 2020-10-30 10:44:52 +0100 | [diff] [blame] | 126 | const std::unique_ptr<TimestampExtrapolator> ts_extrapolator_ |
Niels Möller | d3a3e9e | 2020-10-28 15:15:55 +0100 | [diff] [blame] | 127 | RTC_PT_GUARDED_BY(mutex_); |
| 128 | std::unique_ptr<VCMCodecTimer> codec_timer_ RTC_GUARDED_BY(mutex_) |
| 129 | RTC_PT_GUARDED_BY(mutex_); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 130 | TimeDelta render_delay_ RTC_GUARDED_BY(mutex_); |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 131 | // Best-effort playout delay range for frames from capture to render. |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 132 | // The receiver tries to keep the delay between `min_playout_delay_ms_` |
| 133 | // and `max_playout_delay_ms_` taking the network jitter into account. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 134 | // A special case is where min_playout_delay_ms_ = max_playout_delay_ms_ = 0, |
| 135 | // in which case the receiver tries to play the frames as they arrive. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 136 | TimeDelta min_playout_delay_ RTC_GUARDED_BY(mutex_); |
| 137 | TimeDelta max_playout_delay_ RTC_GUARDED_BY(mutex_); |
| 138 | TimeDelta jitter_delay_ RTC_GUARDED_BY(mutex_); |
| 139 | TimeDelta current_delay_ RTC_GUARDED_BY(mutex_); |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 140 | uint32_t prev_frame_timestamp_ RTC_GUARDED_BY(mutex_); |
| 141 | absl::optional<TimingFrameInfo> timing_frame_info_ RTC_GUARDED_BY(mutex_); |
| 142 | size_t num_decoded_frames_ RTC_GUARDED_BY(mutex_); |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 143 | // Set by the field trial WebRTC-LowLatencyRenderer. The parameter enabled |
| 144 | // determines if the low-latency renderer algorithm should be used for the |
| 145 | // case min playout delay=0 and max playout delay>0. |
| 146 | FieldTrialParameter<bool> low_latency_renderer_enabled_ |
| 147 | RTC_GUARDED_BY(mutex_); |
| 148 | absl::optional<int> max_composition_delay_in_frames_ RTC_GUARDED_BY(mutex_); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 149 | // 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 Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 154 | // Timestamp at which the last frame was scheduled to be sent to the decoder. |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 155 | // 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 Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame] | 157 | Timestamp last_decode_scheduled_ RTC_GUARDED_BY(mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 158 | }; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 159 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 161 | #endif // MODULES_VIDEO_CODING_TIMING_H_ |