blob: 736b5e9ae4d6ba1b8e7fcbdf7102d5073796ab0f [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
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 04:47:3111#ifndef MODULES_VIDEO_CODING_TIMING_H_
12#define MODULES_VIDEO_CODING_TIMING_H_
niklase@google.com470e71d2011-07-07 08:21:2513
magjed2943f012016-03-22 12:12:0914#include <memory>
15
Niels Möller834a5542019-09-23 08:31:1616#include "absl/types/optional.h"
17#include "api/video/video_timing.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "modules/video_coding/codec_timer.h"
Johannes Kron111e9812020-10-26 12:54:4019#include "rtc_base/experiments/field_trial_parser.h"
Markus Handell6deec382020-07-07 10:17:1220#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "rtc_base/thread_annotations.h"
Niels Möller043725f2020-10-30 09:44:5222#include "rtc_base/time/timestamp_extrapolator.h"
niklase@google.com470e71d2011-07-07 08:21:2523
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4324namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:2525
stefan@webrtc.orga678a3b2013-01-21 07:42:1126class Clock;
wu@webrtc.org66773a02014-05-07 17:09:4427class TimestampExtrapolator;
niklase@google.com470e71d2011-07-07 08:21:2528
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4329class VCMTiming {
30 public:
Niels Möller043725f2020-10-30 09:44:5231 explicit VCMTiming(Clock* clock);
32 virtual ~VCMTiming() = default;
niklase@google.com470e71d2011-07-07 08:21:2533
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4334 // Resets the timing to the initial state.
35 void Reset();
niklase@google.com470e71d2011-07-07 08:21:2536
mikhal@webrtc.orgadc64a72013-05-30 16:20:1837 // Set the amount of time needed to render an image. Defaults to 10 ms.
isheriff6b4b5f32016-06-08 07:24:2138 void set_render_delay(int render_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:2539
mikhal@webrtc.orgadc64a72013-05-30 16:20:1840 // Set the minimum time the video must be delayed on the receiver to
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4341 // get the desired jitter buffer level.
isheriff6b4b5f32016-06-08 07:24:2142 void SetJitterDelay(int required_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:2543
Åsa Persson8368d1a2018-01-05 11:44:4544 // Set/get the minimum playout delay from capture to render in ms.
isheriff6b4b5f32016-06-08 07:24:2145 void set_min_playout_delay(int min_playout_delay_ms);
isheriff6b4b5f32016-06-08 07:24:2146 int min_playout_delay();
47
Åsa Persson8368d1a2018-01-05 11:44:4548 // Set/get the maximum playout delay from capture to render in ms.
isheriff6b4b5f32016-06-08 07:24:2149 void set_max_playout_delay(int max_playout_delay_ms);
isheriff6b4b5f32016-06-08 07:24:2150 int max_playout_delay();
niklase@google.com470e71d2011-07-07 08:21:2551
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4352 // Increases or decreases the current delay to get closer to the target delay.
53 // Calculates how long it has been since the previous call to this function,
54 // and increases/decreases the delay in proportion to the time difference.
55 void UpdateCurrentDelay(uint32_t frame_timestamp);
niklase@google.com470e71d2011-07-07 08:21:2556
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4357 // Increases or decreases the current delay to get closer to the target delay.
58 // Given the actual decode time in ms and the render time in ms for a frame,
59 // this function calculates how late the frame is and increases the delay
60 // accordingly.
61 void UpdateCurrentDelay(int64_t render_time_ms,
62 int64_t actual_decode_time_ms);
niklase@google.com470e71d2011-07-07 08:21:2563
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4364 // Stops the decoder timer, should be called when the decoder returns a frame
65 // or when the decoded frame callback is called.
Johannes Kronbfd343b2019-07-01 08:07:5066 void StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms);
67 // TODO(kron): Remove once downstream projects has been changed to use the
68 // above function.
Åsa Persson8368d1a2018-01-05 11:44:4569 void StopDecodeTimer(uint32_t time_stamp,
70 int32_t decode_time_ms,
71 int64_t now_ms,
72 int64_t render_time_ms);
niklase@google.com470e71d2011-07-07 08:21:2573
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4374 // Used to report that a frame is passed to decoding. Updates the timestamp
75 // filter which is used to map between timestamps and receiver system time.
76 void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms);
Åsa Persson8368d1a2018-01-05 11:44:4577
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4378 // Returns the receiver system time when the frame with timestamp
Åsa Persson8368d1a2018-01-05 11:44:4579 // |frame_timestamp| should be rendered, assuming that the system time
80 // currently is |now_ms|.
philipelbe7a9e52016-05-19 10:19:3581 virtual int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:2582
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4383 // 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.
Ilya Nikolaevskiy8c4fe162018-02-27 14:49:4785 virtual int64_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
niklase@google.com470e71d2011-07-07 08:21:2586
mikhal@webrtc.org2eaf98b2013-05-21 17:58:4387 // Returns the current target delay which is required delay + decode time +
88 // render delay.
isheriff6b4b5f32016-06-08 07:24:2189 int TargetVideoDelay() const;
niklase@google.com470e71d2011-07-07 08:21:2590
asapersson8d560882016-12-22 09:26:1891 // Return current timing information. Returns true if the first frame has been
92 // decoded, false otherwise.
Johannes Kronbfd343b2019-07-01 08:07:5093 virtual bool GetTimings(int* max_decode_ms,
philipela45102f2017-02-22 13:30:3994 int* current_delay_ms,
95 int* target_delay_ms,
96 int* jitter_buffer_ms,
97 int* min_playout_delay_ms,
98 int* render_delay_ms) const;
fischman@webrtc.org37bb4972013-10-23 23:59:4599
ilnik2edc6842017-07-06 10:06:50100 void SetTimingFrameInfo(const TimingFrameInfo& info);
Danil Chapovalov0040b662018-06-18 08:48:16101 absl::optional<TimingFrameInfo> GetTimingFrameInfo();
ilnik2edc6842017-07-06 10:06:50102
Johannes Kron111e9812020-10-26 12:54:40103 void SetMaxCompositionDelayInFrames(
104 absl::optional<int> max_composition_delay_in_frames);
105 absl::optional<int> MaxCompositionDelayInFrames() const;
106
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43107 enum { kDefaultRenderDelayMs = 10 };
108 enum { kDelayMaxChangeMsPerS = 100 };
niklase@google.com470e71d2011-07-07 08:21:25109
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43110 protected:
Markus Handell6deec382020-07-07 10:17:12111 int RequiredDecodeTimeMs() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
pbos@webrtc.org04221002014-07-10 15:25:37112 int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const
Markus Handell6deec382020-07-07 10:17:12113 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
114 int TargetDelayInternal() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
niklase@google.com470e71d2011-07-07 08:21:25115
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43116 private:
Markus Handell6deec382020-07-07 10:17:12117 mutable Mutex mutex_;
pbos@webrtc.org04221002014-07-10 15:25:37118 Clock* const clock_;
Niels Möller043725f2020-10-30 09:44:52119 const std::unique_ptr<TimestampExtrapolator> ts_extrapolator_
Niels Möllerd3a3e9e2020-10-28 14:15:55120 RTC_PT_GUARDED_BY(mutex_);
121 std::unique_ptr<VCMCodecTimer> codec_timer_ RTC_GUARDED_BY(mutex_)
122 RTC_PT_GUARDED_BY(mutex_);
Markus Handell6deec382020-07-07 10:17:12123 int render_delay_ms_ RTC_GUARDED_BY(mutex_);
isheriff6b4b5f32016-06-08 07:24:21124 // Best-effort playout delay range for frames from capture to render.
125 // The receiver tries to keep the delay between |min_playout_delay_ms_|
126 // and |max_playout_delay_ms_| taking the network jitter into account.
127 // A special case is where min_playout_delay_ms_ = max_playout_delay_ms_ = 0,
128 // in which case the receiver tries to play the frames as they arrive.
Markus Handell6deec382020-07-07 10:17:12129 int min_playout_delay_ms_ RTC_GUARDED_BY(mutex_);
130 int max_playout_delay_ms_ RTC_GUARDED_BY(mutex_);
131 int jitter_delay_ms_ RTC_GUARDED_BY(mutex_);
132 int current_delay_ms_ RTC_GUARDED_BY(mutex_);
133 uint32_t prev_frame_timestamp_ RTC_GUARDED_BY(mutex_);
134 absl::optional<TimingFrameInfo> timing_frame_info_ RTC_GUARDED_BY(mutex_);
135 size_t num_decoded_frames_ RTC_GUARDED_BY(mutex_);
Johannes Kron111e9812020-10-26 12:54:40136 // Set by the field trial WebRTC-LowLatencyRenderer. The parameter enabled
137 // determines if the low-latency renderer algorithm should be used for the
138 // case min playout delay=0 and max playout delay>0.
139 FieldTrialParameter<bool> low_latency_renderer_enabled_
140 RTC_GUARDED_BY(mutex_);
141 absl::optional<int> max_composition_delay_in_frames_ RTC_GUARDED_BY(mutex_);
niklase@google.com470e71d2011-07-07 08:21:25142};
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43143} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25144
Mirko Bonadei92ea95e2017-09-15 04:47:31145#endif // MODULES_VIDEO_CODING_TIMING_H_