blob: cd4cbcd1c1533f666cfe5f20324221e8a8b9bb97 [file] [log] [blame]
philipelbe7a9e52016-05-19 12:19:35 +02001/*
2 * Copyright (c) 2016 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_FRAME_BUFFER2_H_
12#define MODULES_VIDEO_CODING_FRAME_BUFFER2_H_
philipelbe7a9e52016-05-19 12:19:35 +020013
14#include <array>
15#include <map>
16#include <memory>
philipelbe7a9e52016-05-19 12:19:35 +020017#include <utility>
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010018#include <vector>
philipelbe7a9e52016-05-19 12:19:35 +020019
Elad Alon69321dd2019-01-10 15:02:54 +010020#include "absl/container/inlined_vector.h"
Artem Titovd15a5752021-02-10 14:31:24 +010021#include "api/sequence_checker.h"
philipele7c891f2018-02-22 14:35:06 +010022#include "api/video/encoded_frame.h"
Jonas Orelande02f9ee2022-03-25 12:43:14 +010023#include "api/webrtc_key_value_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/video_coding/include/video_coding_defines.h"
25#include "modules/video_coding/inter_frame_delay.h"
Niels Möllerd9c2d942019-04-30 09:16:36 +020026#include "modules/video_coding/jitter_estimator.h"
Ilya Nikolaevskiy13717842019-01-14 13:24:22 +010027#include "modules/video_coding/utility/decoded_frames_history.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/event.h"
Johannes Kron2ddc39e2021-08-10 16:56:12 +020029#include "rtc_base/experiments/field_trial_parser.h"
“Michaelf9fc1712018-08-27 10:08:58 -050030#include "rtc_base/experiments/rtt_mult_experiment.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020031#include "rtc_base/numerics/sequence_number_util.h"
Markus Handell6deec382020-07-07 12:17:12 +020032#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010033#include "rtc_base/system/no_unique_address.h"
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +020034#include "rtc_base/task_queue.h"
35#include "rtc_base/task_utils/repeating_task.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/thread_annotations.h"
philipelbe7a9e52016-05-19 12:19:35 +020037
38namespace webrtc {
39
40class Clock;
philipela45102f2017-02-22 05:30:39 -080041class VCMReceiveStatisticsCallback;
philipelbe7a9e52016-05-19 12:19:35 +020042class VCMJitterEstimator;
43class VCMTiming;
44
45namespace video_coding {
46
philipelbe7a9e52016-05-19 12:19:35 +020047class FrameBuffer {
48 public:
49 FrameBuffer(Clock* clock,
philipela45102f2017-02-22 05:30:39 -080050 VCMTiming* timing,
Jonas Orelande02f9ee2022-03-25 12:43:14 +010051 VCMReceiveStatisticsCallback* stats_callback,
52 const WebRtcKeyValueConfig& field_trials);
philipelbe7a9e52016-05-19 12:19:35 +020053
Niels Möllerde953292020-09-29 09:46:21 +020054 FrameBuffer() = delete;
55 FrameBuffer(const FrameBuffer&) = delete;
56 FrameBuffer& operator=(const FrameBuffer&) = delete;
57
philipel266f0a42016-11-28 08:49:07 -080058 virtual ~FrameBuffer();
59
philipele0b2f152016-09-28 10:23:49 +020060 // Insert a frame into the frame buffer. Returns the picture id
61 // of the last continuous frame or -1 if there is no continuous frame.
philipele7c891f2018-02-22 14:35:06 +010062 int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame);
philipelbe7a9e52016-05-19 12:19:35 +020063
Evan Shrubsole3d29efd2021-12-07 14:11:45 +010064 using NextFrameCallback = std::function<void(std::unique_ptr<EncodedFrame>)>;
65 // Get the next frame for decoding. `handler` is invoked with the next frame
66 // or with nullptr if no frame is ready for decoding after `max_wait_time_ms`.
67 void NextFrame(int64_t max_wait_time_ms,
68 bool keyframe_required,
69 rtc::TaskQueue* callback_queue,
70 NextFrameCallback handler);
philipelbe7a9e52016-05-19 12:19:35 +020071
philipel4f6cd6a2016-08-03 10:59:32 +020072 // Tells the FrameBuffer which protection mode that is in use. Affects
73 // the frame timing.
74 // TODO(philipel): Remove this when new timing calculations has been
75 // implemented.
76 void SetProtectionMode(VCMVideoProtection mode);
77
philipel504c47d2016-06-30 17:33:02 +020078 // Stop the frame buffer, causing any sleeping thread in NextFrame to
79 // return immediately.
80 void Stop();
81
philipele21be1d2017-09-25 06:37:12 -070082 // Updates the RTT for jitter buffer estimation.
83 void UpdateRtt(int64_t rtt_ms);
84
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +010085 // Clears the FrameBuffer, removing all the buffered frames.
86 void Clear();
87
Johannes Kron111e9812020-10-26 13:54:40 +010088 int Size();
89
philipelbe7a9e52016-05-19 12:19:35 +020090 private:
philipele0b2f152016-09-28 10:23:49 +020091 struct FrameInfo {
Niels Möllerbe682d42018-03-27 08:31:45 +020092 FrameInfo();
93 FrameInfo(FrameInfo&&);
94 ~FrameInfo();
95
philipele0b2f152016-09-28 10:23:49 +020096 // Which other frames that have direct unfulfilled dependencies
97 // on this frame.
philipel9aa9b8d2021-02-15 13:31:29 +010098 absl::InlinedVector<int64_t, 8> dependent_frames;
philipele0b2f152016-09-28 10:23:49 +020099
100 // A frame is continiuous if it has all its referenced/indirectly
101 // referenced frames.
102 //
103 // How many unfulfilled frames this frame have until it becomes continuous.
104 size_t num_missing_continuous = 0;
105
106 // A frame is decodable if all its referenced frames have been decoded.
107 //
108 // How many unfulfilled frames this frame have until it becomes decodable.
109 size_t num_missing_decodable = 0;
110
111 // If this frame is continuous or not.
112 bool continuous = false;
113
philipele7c891f2018-02-22 14:35:06 +0100114 // The actual EncodedFrame.
115 std::unique_ptr<EncodedFrame> frame;
philipele0b2f152016-09-28 10:23:49 +0200116 };
117
philipel9aa9b8d2021-02-15 13:31:29 +0100118 using FrameMap = std::map<int64_t, FrameInfo>;
philipele0b2f152016-09-28 10:23:49 +0200119
Artem Titovdcd7fc72021-08-09 13:02:57 +0200120 // Check that the references of `frame` are valid.
philipele7c891f2018-02-22 14:35:06 +0100121 bool ValidReferences(const EncodedFrame& frame) const;
philipel112adf92017-06-15 09:06:21 -0700122
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100123 int64_t FindNextFrame(Timestamp now) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Evan Shrubsole0072c212021-11-10 11:41:14 +0100124 std::unique_ptr<EncodedFrame> GetNextFrame()
125 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200126
Markus Handell6deec382020-07-07 12:17:12 +0200127 void StartWaitForNextFrameOnQueue() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
128 void CancelCallback() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200129
philipele0b2f152016-09-28 10:23:49 +0200130 // Update all directly dependent and indirectly dependent frames and mark
131 // them as continuous if all their references has been fulfilled.
132 void PropagateContinuity(FrameMap::iterator start)
Markus Handell6deec382020-07-07 12:17:12 +0200133 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelbe7a9e52016-05-19 12:19:35 +0200134
philipele0b2f152016-09-28 10:23:49 +0200135 // Marks the frame as decoded and updates all directly dependent frames.
136 void PropagateDecodability(const FrameInfo& info)
Markus Handell6deec382020-07-07 12:17:12 +0200137 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelbe7a9e52016-05-19 12:19:35 +0200138
Artem Titovdcd7fc72021-08-09 13:02:57 +0200139 // Update the corresponding FrameInfo of `frame` and all FrameInfos that
140 // `frame` references.
141 // Return false if `frame` will never be decodable, true otherwise.
philipele7c891f2018-02-22 14:35:06 +0100142 bool UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200143 FrameMap::iterator info)
Markus Handell6deec382020-07-07 12:17:12 +0200144 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipele0b2f152016-09-28 10:23:49 +0200145
Markus Handell6deec382020-07-07 12:17:12 +0200146 void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelbe742702016-11-30 01:31:40 -0800147
Markus Handell6deec382020-07-07 12:17:12 +0200148 void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700149
Markus Handell6deec382020-07-07 12:17:12 +0200150 void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelfcc60062017-01-18 05:35:20 -0800151
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100152 // The cleaner solution would be to have the NextFrame function return a
153 // vector of frames, but until the decoding pipeline can support decoding
154 // multiple frames at the same time we combine all frames to one frame and
155 // return it. See bugs.webrtc.org/10064
Evan Shrubsole0072c212021-11-10 11:41:14 +0100156 std::unique_ptr<EncodedFrame> CombineAndDeleteFrames(
157 std::vector<std::unique_ptr<EncodedFrame>> frames) const;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100158
Mirko Bonadei20e4c802020-11-23 11:07:42 +0100159 RTC_NO_UNIQUE_ADDRESS SequenceChecker construction_checker_;
160 RTC_NO_UNIQUE_ADDRESS SequenceChecker callback_checker_;
Tommi430951a2020-05-19 23:27:29 +0200161
Ilya Nikolaevskiy6551faf2019-01-10 15:16:47 +0100162 // Stores only undecoded frames.
Markus Handell6deec382020-07-07 12:17:12 +0200163 FrameMap frames_ RTC_GUARDED_BY(mutex_);
164 DecodedFramesHistory decoded_frames_history_ RTC_GUARDED_BY(mutex_);
philipelbe7a9e52016-05-19 12:19:35 +0200165
Markus Handell6deec382020-07-07 12:17:12 +0200166 Mutex mutex_;
philipelbe7a9e52016-05-19 12:19:35 +0200167 Clock* const clock_;
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200168
Markus Handell6deec382020-07-07 12:17:12 +0200169 rtc::TaskQueue* callback_queue_ RTC_GUARDED_BY(mutex_);
170 RepeatingTaskHandle callback_task_ RTC_GUARDED_BY(mutex_);
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100171 NextFrameCallback frame_handler_ RTC_GUARDED_BY(mutex_);
Markus Handell6deec382020-07-07 12:17:12 +0200172 int64_t latest_return_time_ms_ RTC_GUARDED_BY(mutex_);
173 bool keyframe_required_ RTC_GUARDED_BY(mutex_);
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200174
Markus Handell6deec382020-07-07 12:17:12 +0200175 VCMJitterEstimator jitter_estimator_ RTC_GUARDED_BY(mutex_);
176 VCMTiming* const timing_ RTC_GUARDED_BY(mutex_);
177 VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(mutex_);
philipel9aa9b8d2021-02-15 13:31:29 +0100178 absl::optional<int64_t> last_continuous_frame_ RTC_GUARDED_BY(mutex_);
Markus Handell6deec382020-07-07 12:17:12 +0200179 std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(mutex_);
180 bool stopped_ RTC_GUARDED_BY(mutex_);
181 VCMVideoProtection protection_mode_ RTC_GUARDED_BY(mutex_);
philipela45102f2017-02-22 05:30:39 -0800182 VCMReceiveStatisticsCallback* const stats_callback_;
Markus Handell6deec382020-07-07 12:17:12 +0200183 int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(mutex_);
philipelbe7a9e52016-05-19 12:19:35 +0200184
“Michaeld3a4ebe2019-06-07 03:55:01 -0500185 // rtt_mult experiment settings.
186 const absl::optional<RttMultExperiment::Settings> rtt_mult_settings_;
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200187
188 // Maximum number of frames in the decode queue to allow pacing. If the
189 // queue grows beyond the max limit, pacing will be disabled and frames will
190 // be pushed to the decoder as soon as possible. This only has an effect
191 // when the low-latency rendering path is active, which is indicated by
192 // the frame's render time == 0.
193 FieldTrialParameter<unsigned> zero_playout_delay_max_decode_queue_size_;
philipelbe7a9e52016-05-19 12:19:35 +0200194};
195
196} // namespace video_coding
197} // namespace webrtc
198
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200199#endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_