blob: 64249a5190bae74870f0bbec65326096fd07213a [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>
Ilya Nikolaevskiy6551faf2019-01-10 15:16:47 +010017#include <set>
philipelbe7a9e52016-05-19 12:19:35 +020018#include <utility>
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010019#include <vector>
philipelbe7a9e52016-05-19 12:19:35 +020020
Elad Alon69321dd2019-01-10 15:02:54 +010021#include "absl/container/inlined_vector.h"
philipele7c891f2018-02-22 14:35:06 +010022#include "api/video/encoded_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/video_coding/include/video_coding_defines.h"
24#include "modules/video_coding/inter_frame_delay.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/constructor_magic.h"
26#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/event.h"
“Michaelf9fc1712018-08-27 10:08:58 -050028#include "rtc_base/experiments/rtt_mult_experiment.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020029#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/thread_annotations.h"
philipelbe7a9e52016-05-19 12:19:35 +020031
32namespace webrtc {
33
34class Clock;
philipela45102f2017-02-22 05:30:39 -080035class VCMReceiveStatisticsCallback;
philipelbe7a9e52016-05-19 12:19:35 +020036class VCMJitterEstimator;
37class VCMTiming;
38
39namespace video_coding {
40
philipelbe7a9e52016-05-19 12:19:35 +020041class FrameBuffer {
42 public:
philipel75562822016-09-05 10:57:41 +020043 enum ReturnReason { kFrameFound, kTimeout, kStopped };
44
philipelbe7a9e52016-05-19 12:19:35 +020045 FrameBuffer(Clock* clock,
46 VCMJitterEstimator* jitter_estimator,
philipela45102f2017-02-22 05:30:39 -080047 VCMTiming* timing,
48 VCMReceiveStatisticsCallback* stats_proxy);
philipelbe7a9e52016-05-19 12:19:35 +020049
philipel266f0a42016-11-28 08:49:07 -080050 virtual ~FrameBuffer();
51
philipele0b2f152016-09-28 10:23:49 +020052 // Insert a frame into the frame buffer. Returns the picture id
53 // of the last continuous frame or -1 if there is no continuous frame.
philipel97187112018-03-23 10:43:21 +010054 // TODO(philipel): Return a VideoLayerFrameId and not only the picture id.
philipele7c891f2018-02-22 14:35:06 +010055 int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame);
philipelbe7a9e52016-05-19 12:19:35 +020056
57 // Get the next frame for decoding. Will return at latest after
philipel75562822016-09-05 10:57:41 +020058 // |max_wait_time_ms|.
philipele0b2f152016-09-28 10:23:49 +020059 // - If a frame is available within |max_wait_time_ms| it will return
philipel75562822016-09-05 10:57:41 +020060 // kFrameFound and set |frame_out| to the resulting frame.
61 // - If no frame is available after |max_wait_time_ms| it will return
62 // kTimeout.
63 // - If the FrameBuffer is stopped then it will return kStopped.
64 ReturnReason NextFrame(int64_t max_wait_time_ms,
philipele7c891f2018-02-22 14:35:06 +010065 std::unique_ptr<EncodedFrame>* frame_out,
philipel3042c2d2017-08-18 04:55:02 -070066 bool keyframe_required = false);
philipelbe7a9e52016-05-19 12:19:35 +020067
philipel4f6cd6a2016-08-03 10:59:32 +020068 // Tells the FrameBuffer which protection mode that is in use. Affects
69 // the frame timing.
70 // TODO(philipel): Remove this when new timing calculations has been
71 // implemented.
72 void SetProtectionMode(VCMVideoProtection mode);
73
philipel504c47d2016-06-30 17:33:02 +020074 // Start the frame buffer, has no effect if the frame buffer is started.
75 // The frame buffer is started upon construction.
76 void Start();
77
78 // 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
philipelbe7a9e52016-05-19 12:19:35 +020088 private:
philipele0b2f152016-09-28 10:23:49 +020089 struct FrameInfo {
Niels Möllerbe682d42018-03-27 08:31:45 +020090 FrameInfo();
91 FrameInfo(FrameInfo&&);
92 ~FrameInfo();
93
philipele0b2f152016-09-28 10:23:49 +020094 // Which other frames that have direct unfulfilled dependencies
95 // on this frame.
Elad Alon69321dd2019-01-10 15:02:54 +010096 absl::InlinedVector<VideoLayerFrameId, 8> dependent_frames;
philipele0b2f152016-09-28 10:23:49 +020097
98 // A frame is continiuous if it has all its referenced/indirectly
99 // referenced frames.
100 //
101 // How many unfulfilled frames this frame have until it becomes continuous.
102 size_t num_missing_continuous = 0;
103
104 // A frame is decodable if all its referenced frames have been decoded.
105 //
106 // How many unfulfilled frames this frame have until it becomes decodable.
107 size_t num_missing_decodable = 0;
108
109 // If this frame is continuous or not.
110 bool continuous = false;
111
philipele7c891f2018-02-22 14:35:06 +0100112 // The actual EncodedFrame.
113 std::unique_ptr<EncodedFrame> frame;
philipele0b2f152016-09-28 10:23:49 +0200114 };
115
philipel0fa82a62018-03-19 15:34:53 +0100116 using FrameMap = std::map<VideoLayerFrameId, FrameInfo>;
philipele0b2f152016-09-28 10:23:49 +0200117
philipel112adf92017-06-15 09:06:21 -0700118 // Check that the references of |frame| are valid.
philipele7c891f2018-02-22 14:35:06 +0100119 bool ValidReferences(const EncodedFrame& frame) const;
philipel112adf92017-06-15 09:06:21 -0700120
gnishb2a318b2017-05-10 09:21:33 -0700121 // Updates the minimal and maximal playout delays
122 // depending on the frame.
philipele7c891f2018-02-22 14:35:06 +0100123 void UpdatePlayoutDelays(const EncodedFrame& frame)
danilchap56359be2017-09-07 07:53:45 -0700124 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
gnishb2a318b2017-05-10 09:21:33 -0700125
philipele0b2f152016-09-28 10:23:49 +0200126 // Update all directly dependent and indirectly dependent frames and mark
127 // them as continuous if all their references has been fulfilled.
128 void PropagateContinuity(FrameMap::iterator start)
danilchap56359be2017-09-07 07:53:45 -0700129 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200130
philipele0b2f152016-09-28 10:23:49 +0200131 // Marks the frame as decoded and updates all directly dependent frames.
132 void PropagateDecodability(const FrameInfo& info)
danilchap56359be2017-09-07 07:53:45 -0700133 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200134
Ilya Nikolaevskiy6551faf2019-01-10 15:16:47 +0100135 // Removes undecodable frames and moves decoded frame to the history.
philipele0b2f152016-09-28 10:23:49 +0200136 void AdvanceLastDecodedFrame(FrameMap::iterator decoded)
danilchap56359be2017-09-07 07:53:45 -0700137 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipele0b2f152016-09-28 10:23:49 +0200138
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.
philipele7c891f2018-02-22 14:35:06 +0100142 bool UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200143 FrameMap::iterator info)
danilchap56359be2017-09-07 07:53:45 -0700144 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipele0b2f152016-09-28 10:23:49 +0200145
danilchap56359be2017-09-07 07:53:45 -0700146 void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe742702016-11-30 01:31:40 -0800147
danilchap56359be2017-09-07 07:53:45 -0700148 void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
ilnik2edc6842017-07-06 03:06:50 -0700149
danilchap56359be2017-09-07 07:53:45 -0700150 void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelfcc60062017-01-18 05:35:20 -0800151
philipele7c891f2018-02-22 14:35:06 +0100152 bool HasBadRenderTiming(const EncodedFrame& frame, int64_t now_ms)
danilchap56359be2017-09-07 07:53:45 -0700153 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
stefan95e97542017-05-23 09:52:18 -0700154
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100155 // 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
159 EncodedFrame* CombineAndDeleteFrames(
160 const std::vector<EncodedFrame*>& frames) const;
161
Ilya Nikolaevskiy6551faf2019-01-10 15:16:47 +0100162 // Stores only undecoded frames.
danilchap56359be2017-09-07 07:53:45 -0700163 FrameMap frames_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy6551faf2019-01-10 15:16:47 +0100164 std::set<VideoLayerFrameId> decoded_frames_history_ RTC_GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200165
166 rtc::CriticalSection crit_;
167 Clock* const clock_;
tommi0a735642017-03-14 06:23:57 -0700168 rtc::Event new_continuous_frame_event_;
danilchap56359be2017-09-07 07:53:45 -0700169 VCMJitterEstimator* const jitter_estimator_ RTC_GUARDED_BY(crit_);
170 VCMTiming* const timing_ RTC_GUARDED_BY(crit_);
171 VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(crit_);
philipel6d216502018-10-22 14:36:45 +0200172 absl::optional<uint32_t> last_decoded_frame_timestamp_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy6551faf2019-01-10 15:16:47 +0100173 absl::optional<VideoLayerFrameId> last_decoded_frame_ RTC_GUARDED_BY(crit_);
174 absl::optional<VideoLayerFrameId> last_continuous_frame_
175 RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100176 std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(crit_);
danilchap56359be2017-09-07 07:53:45 -0700177 bool stopped_ RTC_GUARDED_BY(crit_);
178 VCMVideoProtection protection_mode_ RTC_GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800179 VCMReceiveStatisticsCallback* const stats_callback_;
danilchap56359be2017-09-07 07:53:45 -0700180 int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200181
182 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
philipelbe7a9e52016-05-19 12:19:35 +0200183};
184
185} // namespace video_coding
186} // namespace webrtc
187
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200188#endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_