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" |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 21 | #include "api/video/encoded_frame.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/video_coding/include/video_coding_defines.h" |
| 23 | #include "modules/video_coding/inter_frame_delay.h" |
Ilya Nikolaevskiy | 1371784 | 2019-01-14 13:24:22 +0100 | [diff] [blame] | 24 | #include "modules/video_coding/utility/decoded_frames_history.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/constructor_magic.h" |
| 26 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/event.h" |
“Michael | f9fc171 | 2018-08-27 10:08:58 -0500 | [diff] [blame] | 28 | #include "rtc_base/experiments/rtt_mult_experiment.h" |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 29 | #include "rtc_base/numerics/sequence_number_util.h" |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 30 | #include "rtc_base/task_queue.h" |
| 31 | #include "rtc_base/task_utils/repeating_task.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "rtc_base/thread_annotations.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 33 | |
| 34 | namespace webrtc { |
| 35 | |
| 36 | class Clock; |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 37 | class VCMReceiveStatisticsCallback; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 38 | class VCMJitterEstimator; |
| 39 | class VCMTiming; |
| 40 | |
| 41 | namespace video_coding { |
| 42 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 43 | class FrameBuffer { |
| 44 | public: |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 45 | enum ReturnReason { kFrameFound, kTimeout, kStopped }; |
| 46 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 47 | FrameBuffer(Clock* clock, |
| 48 | VCMJitterEstimator* jitter_estimator, |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 49 | VCMTiming* timing, |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 50 | VCMReceiveStatisticsCallback* stats_callback); |
| 51 | |
| 52 | FrameBuffer(Clock* clock, |
| 53 | TaskQueueFactory* task_queue_factory, |
| 54 | VCMJitterEstimator* jitter_estimator, |
| 55 | VCMTiming* timing, |
| 56 | VCMReceiveStatisticsCallback* stats_callback); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 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 | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 62 | // TODO(philipel): Return a VideoLayerFrameId and not only the picture id. |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 63 | int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 64 | |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 65 | void InsertFrame(std::unique_ptr<EncodedFrame> frame, |
| 66 | std::function<void(int64_t)> picture_id_handler); |
| 67 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 68 | // Get the next frame for decoding. Will return at latest after |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 69 | // |max_wait_time_ms|. |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 70 | // - If a frame is available within |max_wait_time_ms| it will return |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 71 | // kFrameFound and set |frame_out| to the resulting frame. |
| 72 | // - If no frame is available after |max_wait_time_ms| it will return |
| 73 | // kTimeout. |
| 74 | // - If the FrameBuffer is stopped then it will return kStopped. |
| 75 | ReturnReason NextFrame(int64_t max_wait_time_ms, |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 76 | std::unique_ptr<EncodedFrame>* frame_out, |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame] | 77 | bool keyframe_required = false); |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 78 | void NextFrame( |
| 79 | int64_t max_wait_time_ms, |
| 80 | bool keyframe_required, |
| 81 | std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)> handler); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 82 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 83 | // Tells the FrameBuffer which protection mode that is in use. Affects |
| 84 | // the frame timing. |
| 85 | // TODO(philipel): Remove this when new timing calculations has been |
| 86 | // implemented. |
| 87 | void SetProtectionMode(VCMVideoProtection mode); |
| 88 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 89 | // Start the frame buffer, has no effect if the frame buffer is started. |
| 90 | // The frame buffer is started upon construction. |
| 91 | void Start(); |
| 92 | |
| 93 | // Stop the frame buffer, causing any sleeping thread in NextFrame to |
| 94 | // return immediately. |
| 95 | void Stop(); |
| 96 | |
philipel | e21be1d | 2017-09-25 06:37:12 -0700 | [diff] [blame] | 97 | // Updates the RTT for jitter buffer estimation. |
| 98 | void UpdateRtt(int64_t rtt_ms); |
| 99 | |
Ilya Nikolaevskiy | e6a2d94 | 2018-11-07 14:32:28 +0100 | [diff] [blame] | 100 | // Clears the FrameBuffer, removing all the buffered frames. |
| 101 | void Clear(); |
| 102 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 103 | private: |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 104 | struct FrameInfo { |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 105 | FrameInfo(); |
| 106 | FrameInfo(FrameInfo&&); |
| 107 | ~FrameInfo(); |
| 108 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 109 | // Which other frames that have direct unfulfilled dependencies |
| 110 | // on this frame. |
Elad Alon | 69321dd | 2019-01-10 15:02:54 +0100 | [diff] [blame] | 111 | absl::InlinedVector<VideoLayerFrameId, 8> dependent_frames; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 112 | |
| 113 | // A frame is continiuous if it has all its referenced/indirectly |
| 114 | // referenced frames. |
| 115 | // |
| 116 | // How many unfulfilled frames this frame have until it becomes continuous. |
| 117 | size_t num_missing_continuous = 0; |
| 118 | |
| 119 | // A frame is decodable if all its referenced frames have been decoded. |
| 120 | // |
| 121 | // How many unfulfilled frames this frame have until it becomes decodable. |
| 122 | size_t num_missing_decodable = 0; |
| 123 | |
| 124 | // If this frame is continuous or not. |
| 125 | bool continuous = false; |
| 126 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 127 | // The actual EncodedFrame. |
| 128 | std::unique_ptr<EncodedFrame> frame; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 131 | using FrameMap = std::map<VideoLayerFrameId, FrameInfo>; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 132 | |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 133 | void SafePost(std::function<void()> func); |
| 134 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 135 | // Check that the references of |frame| are valid. |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 136 | bool ValidReferences(const EncodedFrame& frame) const; |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 137 | |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 138 | void NextFrameOnQueue() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 139 | int64_t UpdateFramesToDecode(int64_t now_ms) |
| 140 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 141 | EncodedFrame* GetFrameToDecode() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 142 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 143 | // Update all directly dependent and indirectly dependent frames and mark |
| 144 | // them as continuous if all their references has been fulfilled. |
| 145 | void PropagateContinuity(FrameMap::iterator start) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 146 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 147 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 148 | // Marks the frame as decoded and updates all directly dependent frames. |
| 149 | void PropagateDecodability(const FrameInfo& info) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 150 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 151 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 152 | // Update the corresponding FrameInfo of |frame| and all FrameInfos that |
| 153 | // |frame| references. |
| 154 | // Return false if |frame| will never be decodable, true otherwise. |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 155 | bool UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame, |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 156 | FrameMap::iterator info) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 157 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 158 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 159 | void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 160 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 161 | void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 162 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 163 | void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 164 | |
Ilya Nikolaevskiy | 48193b0 | 2019-03-25 11:40:34 +0100 | [diff] [blame] | 165 | // Checks if the superframe, which current frame belongs to, is complete. |
| 166 | bool IsCompleteSuperFrame(const EncodedFrame& frame) |
| 167 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 168 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 169 | bool HasBadRenderTiming(const EncodedFrame& frame, int64_t now_ms) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 170 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 171 | |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 172 | // The cleaner solution would be to have the NextFrame function return a |
| 173 | // vector of frames, but until the decoding pipeline can support decoding |
| 174 | // multiple frames at the same time we combine all frames to one frame and |
| 175 | // return it. See bugs.webrtc.org/10064 |
| 176 | EncodedFrame* CombineAndDeleteFrames( |
| 177 | const std::vector<EncodedFrame*>& frames) const; |
| 178 | |
Ilya Nikolaevskiy | 6551faf | 2019-01-10 15:16:47 +0100 | [diff] [blame] | 179 | // Stores only undecoded frames. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 180 | FrameMap frames_ RTC_GUARDED_BY(crit_); |
Ilya Nikolaevskiy | 1371784 | 2019-01-14 13:24:22 +0100 | [diff] [blame] | 181 | DecodedFramesHistory decoded_frames_history_ RTC_GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 182 | |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 183 | // TODO(srte): Remove this lock when always running on task queue. |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 184 | rtc::CriticalSection crit_; |
| 185 | Clock* const clock_; |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 186 | const bool use_task_queue_; |
| 187 | |
| 188 | RepeatingTaskHandle callback_task_ RTC_GUARDED_BY(crit_); |
| 189 | std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)> |
| 190 | frame_handler_ RTC_GUARDED_BY(crit_); |
| 191 | int64_t latest_return_time_ms_ RTC_GUARDED_BY(crit_); |
| 192 | bool keyframe_required_ RTC_GUARDED_BY(crit_); |
| 193 | |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 194 | rtc::Event new_continuous_frame_event_; |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 195 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 196 | VCMJitterEstimator* const jitter_estimator_ RTC_GUARDED_BY(crit_); |
| 197 | VCMTiming* const timing_ RTC_GUARDED_BY(crit_); |
| 198 | VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(crit_); |
Ilya Nikolaevskiy | 6551faf | 2019-01-10 15:16:47 +0100 | [diff] [blame] | 199 | absl::optional<VideoLayerFrameId> last_continuous_frame_ |
| 200 | RTC_GUARDED_BY(crit_); |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 201 | std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(crit_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 202 | bool stopped_ RTC_GUARDED_BY(crit_); |
| 203 | VCMVideoProtection protection_mode_ RTC_GUARDED_BY(crit_); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 204 | VCMReceiveStatisticsCallback* const stats_callback_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 205 | int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 206 | |
Elad Alon | e4b5023 | 2019-01-14 18:56:14 +0100 | [diff] [blame] | 207 | const bool add_rtt_to_playout_delay_; |
| 208 | |
Sebastian Jansson | 13943b7 | 2019-04-02 15:08:14 +0200 | [diff] [blame] | 209 | // Defined last so it is destroyed before other members. |
| 210 | rtc::TaskQueue task_queue_; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 211 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | } // namespace video_coding |
| 215 | } // namespace webrtc |
| 216 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 217 | #endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |