philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |
| 12 | #define MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 13 | |
| 14 | #include <array> |
| 15 | #include <map> |
| 16 | #include <memory> |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 17 | #include <utility> |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 18 | #include <vector> |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 19 | |
Elad Alon | 69321dd | 2019-01-10 15:02:54 +0100 | [diff] [blame] | 20 | #include "absl/container/inlined_vector.h" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 21 | #include "api/sequence_checker.h" |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 22 | #include "api/video/encoded_frame.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/video_coding/include/video_coding_defines.h" |
| 24 | #include "modules/video_coding/inter_frame_delay.h" |
Niels Möller | d9c2d94 | 2019-04-30 09:16:36 +0200 | [diff] [blame] | 25 | #include "modules/video_coding/jitter_estimator.h" |
Ilya Nikolaevskiy | 1371784 | 2019-01-14 13:24:22 +0100 | [diff] [blame] | 26 | #include "modules/video_coding/utility/decoded_frames_history.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/event.h" |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 28 | #include "rtc_base/experiments/field_trial_parser.h" |
“Michael | f9fc171 | 2018-08-27 10:08:58 -0500 | [diff] [blame] | 29 | #include "rtc_base/experiments/rtt_mult_experiment.h" |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 30 | #include "rtc_base/numerics/sequence_number_util.h" |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 31 | #include "rtc_base/synchronization/mutex.h" |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 32 | #include "rtc_base/system/no_unique_address.h" |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 33 | #include "rtc_base/task_queue.h" |
| 34 | #include "rtc_base/task_utils/repeating_task.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 35 | #include "rtc_base/thread_annotations.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 36 | |
| 37 | namespace webrtc { |
| 38 | |
| 39 | class Clock; |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 40 | class VCMReceiveStatisticsCallback; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 41 | class VCMJitterEstimator; |
| 42 | class VCMTiming; |
| 43 | |
| 44 | namespace video_coding { |
| 45 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 46 | class FrameBuffer { |
| 47 | public: |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 48 | enum ReturnReason { kFrameFound, kTimeout, kStopped }; |
| 49 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 50 | FrameBuffer(Clock* clock, |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 51 | VCMTiming* timing, |
Sebastian Jansson | 1c747f5 | 2019-04-04 13:01:39 +0200 | [diff] [blame] | 52 | VCMReceiveStatisticsCallback* stats_callback); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 53 | |
Niels Möller | de95329 | 2020-09-29 09:46:21 +0200 | [diff] [blame] | 54 | FrameBuffer() = delete; |
| 55 | FrameBuffer(const FrameBuffer&) = delete; |
| 56 | FrameBuffer& operator=(const FrameBuffer&) = delete; |
| 57 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 58 | virtual ~FrameBuffer(); |
| 59 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 60 | // 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. |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 62 | int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 63 | |
| 64 | // Get the next frame for decoding. Will return at latest after |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 65 | // `max_wait_time_ms`. |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 66 | void NextFrame( |
| 67 | int64_t max_wait_time_ms, |
| 68 | bool keyframe_required, |
| 69 | rtc::TaskQueue* callback_queue, |
| 70 | std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)> handler); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 71 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 72 | // 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 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 78 | // Stop the frame buffer, causing any sleeping thread in NextFrame to |
| 79 | // return immediately. |
| 80 | void Stop(); |
| 81 | |
philipel | e21be1d | 2017-09-25 06:37:12 -0700 | [diff] [blame] | 82 | // Updates the RTT for jitter buffer estimation. |
| 83 | void UpdateRtt(int64_t rtt_ms); |
| 84 | |
Ilya Nikolaevskiy | e6a2d94 | 2018-11-07 14:32:28 +0100 | [diff] [blame] | 85 | // Clears the FrameBuffer, removing all the buffered frames. |
| 86 | void Clear(); |
| 87 | |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 88 | int Size(); |
| 89 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 90 | private: |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 91 | struct FrameInfo { |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 92 | FrameInfo(); |
| 93 | FrameInfo(FrameInfo&&); |
| 94 | ~FrameInfo(); |
| 95 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 96 | // Which other frames that have direct unfulfilled dependencies |
| 97 | // on this frame. |
philipel | 9aa9b8d | 2021-02-15 13:31:29 +0100 | [diff] [blame] | 98 | absl::InlinedVector<int64_t, 8> dependent_frames; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 99 | |
| 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 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 114 | // The actual EncodedFrame. |
| 115 | std::unique_ptr<EncodedFrame> frame; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
philipel | 9aa9b8d | 2021-02-15 13:31:29 +0100 | [diff] [blame] | 118 | using FrameMap = std::map<int64_t, FrameInfo>; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 119 | |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 120 | // Check that the references of `frame` are valid. |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 121 | bool ValidReferences(const EncodedFrame& frame) const; |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 122 | |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 123 | int64_t FindNextFrame(int64_t now_ms) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
Evan Shrubsole | 0072c21 | 2021-11-10 11:41:14 +0100 | [diff] [blame^] | 124 | std::unique_ptr<EncodedFrame> GetNextFrame() |
| 125 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
Sebastian Jansson | 1c747f5 | 2019-04-04 13:01:39 +0200 | [diff] [blame] | 126 | |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 127 | void StartWaitForNextFrameOnQueue() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 128 | void CancelCallback() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 129 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 130 | // 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 Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 133 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 134 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 135 | // Marks the frame as decoded and updates all directly dependent frames. |
| 136 | void PropagateDecodability(const FrameInfo& info) |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 137 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 138 | |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 139 | // Update the corresponding FrameInfo of `frame` and all FrameInfos that |
| 140 | // `frame` references. |
| 141 | // Return false if `frame` will never be decodable, true otherwise. |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 142 | bool UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame, |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 143 | FrameMap::iterator info) |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 144 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 145 | |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 146 | void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 147 | |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 148 | void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 149 | |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 150 | void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 151 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 152 | bool HasBadRenderTiming(const EncodedFrame& frame, int64_t now_ms) |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 153 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 154 | |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 155 | // The cleaner solution would be to have the NextFrame function return a |
| 156 | // vector of frames, but until the decoding pipeline can support decoding |
| 157 | // multiple frames at the same time we combine all frames to one frame and |
| 158 | // return it. See bugs.webrtc.org/10064 |
Evan Shrubsole | 0072c21 | 2021-11-10 11:41:14 +0100 | [diff] [blame^] | 159 | std::unique_ptr<EncodedFrame> CombineAndDeleteFrames( |
| 160 | std::vector<std::unique_ptr<EncodedFrame>> frames) const; |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 161 | |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 162 | RTC_NO_UNIQUE_ADDRESS SequenceChecker construction_checker_; |
| 163 | RTC_NO_UNIQUE_ADDRESS SequenceChecker callback_checker_; |
Tommi | 430951a | 2020-05-19 23:27:29 +0200 | [diff] [blame] | 164 | |
Ilya Nikolaevskiy | 6551faf | 2019-01-10 15:16:47 +0100 | [diff] [blame] | 165 | // Stores only undecoded frames. |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 166 | FrameMap frames_ RTC_GUARDED_BY(mutex_); |
| 167 | DecodedFramesHistory decoded_frames_history_ RTC_GUARDED_BY(mutex_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 168 | |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 169 | Mutex mutex_; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 170 | Clock* const clock_; |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 171 | |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 172 | rtc::TaskQueue* callback_queue_ RTC_GUARDED_BY(mutex_); |
| 173 | RepeatingTaskHandle callback_task_ RTC_GUARDED_BY(mutex_); |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 174 | std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)> |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 175 | frame_handler_ RTC_GUARDED_BY(mutex_); |
| 176 | int64_t latest_return_time_ms_ RTC_GUARDED_BY(mutex_); |
| 177 | bool keyframe_required_ RTC_GUARDED_BY(mutex_); |
Sebastian Jansson | 1c747f5 | 2019-04-04 13:01:39 +0200 | [diff] [blame] | 178 | |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 179 | VCMJitterEstimator jitter_estimator_ RTC_GUARDED_BY(mutex_); |
| 180 | VCMTiming* const timing_ RTC_GUARDED_BY(mutex_); |
| 181 | VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(mutex_); |
philipel | 9aa9b8d | 2021-02-15 13:31:29 +0100 | [diff] [blame] | 182 | absl::optional<int64_t> last_continuous_frame_ RTC_GUARDED_BY(mutex_); |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 183 | std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(mutex_); |
| 184 | bool stopped_ RTC_GUARDED_BY(mutex_); |
| 185 | VCMVideoProtection protection_mode_ RTC_GUARDED_BY(mutex_); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 186 | VCMReceiveStatisticsCallback* const stats_callback_; |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 187 | int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(mutex_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 188 | |
“Michael | d3a4ebe | 2019-06-07 03:55:01 -0500 | [diff] [blame] | 189 | // rtt_mult experiment settings. |
| 190 | const absl::optional<RttMultExperiment::Settings> rtt_mult_settings_; |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 191 | |
| 192 | // Maximum number of frames in the decode queue to allow pacing. If the |
| 193 | // queue grows beyond the max limit, pacing will be disabled and frames will |
| 194 | // be pushed to the decoder as soon as possible. This only has an effect |
| 195 | // when the low-latency rendering path is active, which is indicated by |
| 196 | // the frame's render time == 0. |
| 197 | FieldTrialParameter<unsigned> zero_playout_delay_max_decode_queue_size_; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | } // namespace video_coding |
| 201 | } // namespace webrtc |
| 202 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 203 | #endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |