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> |
| 18 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 19 | #include "api/video/encoded_frame.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/video_coding/include/video_coding_defines.h" |
| 21 | #include "modules/video_coding/inter_frame_delay.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/constructormagic.h" |
| 23 | #include "rtc_base/criticalsection.h" |
| 24 | #include "rtc_base/event.h" |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 25 | #include "rtc_base/numerics/sequence_number_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/thread_annotations.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | class Clock; |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 31 | class VCMReceiveStatisticsCallback; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 32 | class VCMJitterEstimator; |
| 33 | class VCMTiming; |
| 34 | |
| 35 | namespace video_coding { |
| 36 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 37 | class FrameBuffer { |
| 38 | public: |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 39 | enum ReturnReason { kFrameFound, kTimeout, kStopped }; |
| 40 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 41 | FrameBuffer(Clock* clock, |
| 42 | VCMJitterEstimator* jitter_estimator, |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 43 | VCMTiming* timing, |
| 44 | VCMReceiveStatisticsCallback* stats_proxy); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 45 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 46 | virtual ~FrameBuffer(); |
| 47 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 48 | // Insert a frame into the frame buffer. Returns the picture id |
| 49 | // of the last continuous frame or -1 if there is no continuous frame. |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 50 | // TODO(philipel): Return a VideoLayerFrameId and not only the picture id. |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 51 | int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 52 | |
| 53 | // Get the next frame for decoding. Will return at latest after |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 54 | // |max_wait_time_ms|. |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 55 | // - If a frame is available within |max_wait_time_ms| it will return |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 56 | // kFrameFound and set |frame_out| to the resulting frame. |
| 57 | // - If no frame is available after |max_wait_time_ms| it will return |
| 58 | // kTimeout. |
| 59 | // - If the FrameBuffer is stopped then it will return kStopped. |
| 60 | ReturnReason NextFrame(int64_t max_wait_time_ms, |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 61 | std::unique_ptr<EncodedFrame>* frame_out, |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame] | 62 | bool keyframe_required = false); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 63 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 64 | // Tells the FrameBuffer which protection mode that is in use. Affects |
| 65 | // the frame timing. |
| 66 | // TODO(philipel): Remove this when new timing calculations has been |
| 67 | // implemented. |
| 68 | void SetProtectionMode(VCMVideoProtection mode); |
| 69 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 70 | // Start the frame buffer, has no effect if the frame buffer is started. |
| 71 | // The frame buffer is started upon construction. |
| 72 | void Start(); |
| 73 | |
| 74 | // Stop the frame buffer, causing any sleeping thread in NextFrame to |
| 75 | // return immediately. |
| 76 | void Stop(); |
| 77 | |
philipel | e21be1d | 2017-09-25 06:37:12 -0700 | [diff] [blame] | 78 | // Updates the RTT for jitter buffer estimation. |
| 79 | void UpdateRtt(int64_t rtt_ms); |
| 80 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 81 | private: |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 82 | struct FrameInfo { |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame^] | 83 | FrameInfo(); |
| 84 | FrameInfo(FrameInfo&&); |
| 85 | ~FrameInfo(); |
| 86 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 87 | // The maximum number of frames that can depend on this frame. |
| 88 | static constexpr size_t kMaxNumDependentFrames = 8; |
| 89 | |
| 90 | // Which other frames that have direct unfulfilled dependencies |
| 91 | // on this frame. |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 92 | // TODO(philipel): Add simple modify/access functions to prevent adding too |
| 93 | // many |dependent_frames|. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 94 | VideoLayerFrameId dependent_frames[kMaxNumDependentFrames]; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 95 | size_t num_dependent_frames = 0; |
| 96 | |
| 97 | // A frame is continiuous if it has all its referenced/indirectly |
| 98 | // referenced frames. |
| 99 | // |
| 100 | // How many unfulfilled frames this frame have until it becomes continuous. |
| 101 | size_t num_missing_continuous = 0; |
| 102 | |
| 103 | // A frame is decodable if all its referenced frames have been decoded. |
| 104 | // |
| 105 | // How many unfulfilled frames this frame have until it becomes decodable. |
| 106 | size_t num_missing_decodable = 0; |
| 107 | |
| 108 | // If this frame is continuous or not. |
| 109 | bool continuous = false; |
| 110 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 111 | // The actual EncodedFrame. |
| 112 | std::unique_ptr<EncodedFrame> frame; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 113 | }; |
| 114 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 115 | using FrameMap = std::map<VideoLayerFrameId, FrameInfo>; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 116 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 117 | // Check that the references of |frame| are valid. |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 118 | bool ValidReferences(const EncodedFrame& frame) const; |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 119 | |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 120 | // Updates the minimal and maximal playout delays |
| 121 | // depending on the frame. |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 122 | void UpdatePlayoutDelays(const EncodedFrame& frame) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 123 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 124 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 125 | // Update all directly dependent and indirectly dependent frames and mark |
| 126 | // them as continuous if all their references has been fulfilled. |
| 127 | void PropagateContinuity(FrameMap::iterator start) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 128 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 129 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 130 | // Marks the frame as decoded and updates all directly dependent frames. |
| 131 | void PropagateDecodability(const FrameInfo& info) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 132 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 133 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 134 | // Advances |last_decoded_frame_it_| to |decoded| and removes old |
| 135 | // frame info. |
| 136 | void AdvanceLastDecodedFrame(FrameMap::iterator decoded) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 137 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 138 | |
| 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) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 144 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 145 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 146 | void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 147 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 148 | void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 149 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 150 | void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
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) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 153 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 154 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 155 | FrameMap frames_ RTC_GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 156 | |
| 157 | rtc::CriticalSection crit_; |
| 158 | Clock* const clock_; |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 159 | rtc::Event new_continuous_frame_event_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 160 | VCMJitterEstimator* const jitter_estimator_ RTC_GUARDED_BY(crit_); |
| 161 | VCMTiming* const timing_ RTC_GUARDED_BY(crit_); |
| 162 | VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(crit_); |
| 163 | uint32_t last_decoded_frame_timestamp_ RTC_GUARDED_BY(crit_); |
| 164 | FrameMap::iterator last_decoded_frame_it_ RTC_GUARDED_BY(crit_); |
| 165 | FrameMap::iterator last_continuous_frame_it_ RTC_GUARDED_BY(crit_); |
| 166 | FrameMap::iterator next_frame_it_ RTC_GUARDED_BY(crit_); |
| 167 | int num_frames_history_ RTC_GUARDED_BY(crit_); |
| 168 | int num_frames_buffered_ RTC_GUARDED_BY(crit_); |
| 169 | bool stopped_ RTC_GUARDED_BY(crit_); |
| 170 | VCMVideoProtection protection_mode_ RTC_GUARDED_BY(crit_); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 171 | VCMReceiveStatisticsCallback* const stats_callback_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 172 | int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 173 | |
| 174 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | } // namespace video_coding |
| 178 | } // namespace webrtc |
| 179 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 180 | #endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |