blob: c311bc8f2f1c7e897757237372b4f883ca012a96 [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
philipele7c891f2018-02-22 14:35:06 +010020#include "api/video/encoded_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/video_coding/include/video_coding_defines.h"
22#include "modules/video_coding/inter_frame_delay.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/constructormagic.h"
24#include "rtc_base/criticalsection.h"
25#include "rtc_base/event.h"
“Michaelf9fc1712018-08-27 10:08:58 -050026#include "rtc_base/experiments/rtt_mult_experiment.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020027#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/thread_annotations.h"
philipelbe7a9e52016-05-19 12:19:35 +020029
30namespace webrtc {
31
32class Clock;
philipela45102f2017-02-22 05:30:39 -080033class VCMReceiveStatisticsCallback;
philipelbe7a9e52016-05-19 12:19:35 +020034class VCMJitterEstimator;
35class VCMTiming;
36
37namespace video_coding {
38
philipelbe7a9e52016-05-19 12:19:35 +020039class FrameBuffer {
40 public:
philipel75562822016-09-05 10:57:41 +020041 enum ReturnReason { kFrameFound, kTimeout, kStopped };
42
philipelbe7a9e52016-05-19 12:19:35 +020043 FrameBuffer(Clock* clock,
44 VCMJitterEstimator* jitter_estimator,
philipela45102f2017-02-22 05:30:39 -080045 VCMTiming* timing,
46 VCMReceiveStatisticsCallback* stats_proxy);
philipelbe7a9e52016-05-19 12:19:35 +020047
philipel266f0a42016-11-28 08:49:07 -080048 virtual ~FrameBuffer();
49
philipele0b2f152016-09-28 10:23:49 +020050 // Insert a frame into the frame buffer. Returns the picture id
51 // of the last continuous frame or -1 if there is no continuous frame.
philipel97187112018-03-23 10:43:21 +010052 // TODO(philipel): Return a VideoLayerFrameId and not only the picture id.
philipele7c891f2018-02-22 14:35:06 +010053 int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame);
philipelbe7a9e52016-05-19 12:19:35 +020054
55 // Get the next frame for decoding. Will return at latest after
philipel75562822016-09-05 10:57:41 +020056 // |max_wait_time_ms|.
philipele0b2f152016-09-28 10:23:49 +020057 // - If a frame is available within |max_wait_time_ms| it will return
philipel75562822016-09-05 10:57:41 +020058 // kFrameFound and set |frame_out| to the resulting frame.
59 // - If no frame is available after |max_wait_time_ms| it will return
60 // kTimeout.
61 // - If the FrameBuffer is stopped then it will return kStopped.
62 ReturnReason NextFrame(int64_t max_wait_time_ms,
philipele7c891f2018-02-22 14:35:06 +010063 std::unique_ptr<EncodedFrame>* frame_out,
philipel3042c2d2017-08-18 04:55:02 -070064 bool keyframe_required = false);
philipelbe7a9e52016-05-19 12:19:35 +020065
philipel4f6cd6a2016-08-03 10:59:32 +020066 // Tells the FrameBuffer which protection mode that is in use. Affects
67 // the frame timing.
68 // TODO(philipel): Remove this when new timing calculations has been
69 // implemented.
70 void SetProtectionMode(VCMVideoProtection mode);
71
philipel504c47d2016-06-30 17:33:02 +020072 // Start the frame buffer, has no effect if the frame buffer is started.
73 // The frame buffer is started upon construction.
74 void Start();
75
76 // Stop the frame buffer, causing any sleeping thread in NextFrame to
77 // return immediately.
78 void Stop();
79
philipele21be1d2017-09-25 06:37:12 -070080 // Updates the RTT for jitter buffer estimation.
81 void UpdateRtt(int64_t rtt_ms);
82
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +010083 // Clears the FrameBuffer, removing all the buffered frames.
84 void Clear();
85
philipelbe7a9e52016-05-19 12:19:35 +020086 private:
philipele0b2f152016-09-28 10:23:49 +020087 struct FrameInfo {
Niels Möllerbe682d42018-03-27 08:31:45 +020088 FrameInfo();
89 FrameInfo(FrameInfo&&);
90 ~FrameInfo();
91
philipele0b2f152016-09-28 10:23:49 +020092 // The maximum number of frames that can depend on this frame.
93 static constexpr size_t kMaxNumDependentFrames = 8;
94
95 // Which other frames that have direct unfulfilled dependencies
96 // on this frame.
philipel112adf92017-06-15 09:06:21 -070097 // TODO(philipel): Add simple modify/access functions to prevent adding too
98 // many |dependent_frames|.
philipel0fa82a62018-03-19 15:34:53 +010099 VideoLayerFrameId dependent_frames[kMaxNumDependentFrames];
philipele0b2f152016-09-28 10:23:49 +0200100 size_t num_dependent_frames = 0;
101
102 // A frame is continiuous if it has all its referenced/indirectly
103 // referenced frames.
104 //
105 // How many unfulfilled frames this frame have until it becomes continuous.
106 size_t num_missing_continuous = 0;
107
108 // A frame is decodable if all its referenced frames have been decoded.
109 //
110 // How many unfulfilled frames this frame have until it becomes decodable.
111 size_t num_missing_decodable = 0;
112
113 // If this frame is continuous or not.
114 bool continuous = false;
115
philipele7c891f2018-02-22 14:35:06 +0100116 // The actual EncodedFrame.
117 std::unique_ptr<EncodedFrame> frame;
philipele0b2f152016-09-28 10:23:49 +0200118 };
119
philipel0fa82a62018-03-19 15:34:53 +0100120 using FrameMap = std::map<VideoLayerFrameId, FrameInfo>;
philipele0b2f152016-09-28 10:23:49 +0200121
philipel112adf92017-06-15 09:06:21 -0700122 // Check that the references of |frame| are valid.
philipele7c891f2018-02-22 14:35:06 +0100123 bool ValidReferences(const EncodedFrame& frame) const;
philipel112adf92017-06-15 09:06:21 -0700124
gnishb2a318b2017-05-10 09:21:33 -0700125 // Updates the minimal and maximal playout delays
126 // depending on the frame.
philipele7c891f2018-02-22 14:35:06 +0100127 void UpdatePlayoutDelays(const EncodedFrame& frame)
danilchap56359be2017-09-07 07:53:45 -0700128 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
gnishb2a318b2017-05-10 09:21:33 -0700129
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)
danilchap56359be2017-09-07 07:53:45 -0700133 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
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)
danilchap56359be2017-09-07 07:53:45 -0700137 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200138
philipele0b2f152016-09-28 10:23:49 +0200139 // Advances |last_decoded_frame_it_| to |decoded| and removes old
140 // frame info.
141 void AdvanceLastDecodedFrame(FrameMap::iterator decoded)
danilchap56359be2017-09-07 07:53:45 -0700142 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipele0b2f152016-09-28 10:23:49 +0200143
144 // Update the corresponding FrameInfo of |frame| and all FrameInfos that
145 // |frame| references.
146 // Return false if |frame| will never be decodable, true otherwise.
philipele7c891f2018-02-22 14:35:06 +0100147 bool UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200148 FrameMap::iterator info)
danilchap56359be2017-09-07 07:53:45 -0700149 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipele0b2f152016-09-28 10:23:49 +0200150
danilchap56359be2017-09-07 07:53:45 -0700151 void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe742702016-11-30 01:31:40 -0800152
danilchap56359be2017-09-07 07:53:45 -0700153 void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
ilnik2edc6842017-07-06 03:06:50 -0700154
danilchap56359be2017-09-07 07:53:45 -0700155 void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelfcc60062017-01-18 05:35:20 -0800156
philipele7c891f2018-02-22 14:35:06 +0100157 bool HasBadRenderTiming(const EncodedFrame& frame, int64_t now_ms)
danilchap56359be2017-09-07 07:53:45 -0700158 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
stefan95e97542017-05-23 09:52:18 -0700159
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100160 // The cleaner solution would be to have the NextFrame function return a
161 // vector of frames, but until the decoding pipeline can support decoding
162 // multiple frames at the same time we combine all frames to one frame and
163 // return it. See bugs.webrtc.org/10064
164 EncodedFrame* CombineAndDeleteFrames(
165 const std::vector<EncodedFrame*>& frames) const;
166
danilchap56359be2017-09-07 07:53:45 -0700167 FrameMap frames_ RTC_GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200168
169 rtc::CriticalSection crit_;
170 Clock* const clock_;
tommi0a735642017-03-14 06:23:57 -0700171 rtc::Event new_continuous_frame_event_;
danilchap56359be2017-09-07 07:53:45 -0700172 VCMJitterEstimator* const jitter_estimator_ RTC_GUARDED_BY(crit_);
173 VCMTiming* const timing_ RTC_GUARDED_BY(crit_);
174 VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(crit_);
philipel6d216502018-10-22 14:36:45 +0200175 absl::optional<uint32_t> last_decoded_frame_timestamp_ RTC_GUARDED_BY(crit_);
danilchap56359be2017-09-07 07:53:45 -0700176 FrameMap::iterator last_decoded_frame_it_ RTC_GUARDED_BY(crit_);
177 FrameMap::iterator last_continuous_frame_it_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100178 std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(crit_);
danilchap56359be2017-09-07 07:53:45 -0700179 int num_frames_history_ RTC_GUARDED_BY(crit_);
180 int num_frames_buffered_ RTC_GUARDED_BY(crit_);
181 bool stopped_ RTC_GUARDED_BY(crit_);
182 VCMVideoProtection protection_mode_ RTC_GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800183 VCMReceiveStatisticsCallback* const stats_callback_;
danilchap56359be2017-09-07 07:53:45 -0700184 int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200185
186 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
philipelbe7a9e52016-05-19 12:19:35 +0200187};
188
189} // namespace video_coding
190} // namespace webrtc
191
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200192#endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_