blob: 7772167da82a2e9f275588cbc3cb004afed41c5c [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
Elad Alon69321dd2019-01-10 15:02:54 +010020#include "absl/container/inlined_vector.h"
philipele7c891f2018-02-22 14:35:06 +010021#include "api/video/encoded_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/include/video_coding_defines.h"
23#include "modules/video_coding/inter_frame_delay.h"
Ilya Nikolaevskiy13717842019-01-14 13:24:22 +010024#include "modules/video_coding/utility/decoded_frames_history.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"
Sebastian Jansson13943b72019-04-02 15:08:14 +020030#include "rtc_base/task_queue.h"
31#include "rtc_base/task_utils/repeating_task.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/thread_annotations.h"
philipelbe7a9e52016-05-19 12:19:35 +020033
34namespace webrtc {
35
36class Clock;
philipela45102f2017-02-22 05:30:39 -080037class VCMReceiveStatisticsCallback;
philipelbe7a9e52016-05-19 12:19:35 +020038class VCMJitterEstimator;
39class VCMTiming;
40
41namespace video_coding {
42
philipelbe7a9e52016-05-19 12:19:35 +020043class FrameBuffer {
44 public:
philipel75562822016-09-05 10:57:41 +020045 enum ReturnReason { kFrameFound, kTimeout, kStopped };
46
philipelbe7a9e52016-05-19 12:19:35 +020047 FrameBuffer(Clock* clock,
48 VCMJitterEstimator* jitter_estimator,
philipela45102f2017-02-22 05:30:39 -080049 VCMTiming* timing,
Sebastian Jansson13943b72019-04-02 15:08:14 +020050 VCMReceiveStatisticsCallback* stats_callback);
51
52 FrameBuffer(Clock* clock,
53 TaskQueueFactory* task_queue_factory,
54 VCMJitterEstimator* jitter_estimator,
55 VCMTiming* timing,
56 VCMReceiveStatisticsCallback* stats_callback);
philipelbe7a9e52016-05-19 12:19:35 +020057
philipel266f0a42016-11-28 08:49:07 -080058 virtual ~FrameBuffer();
59
philipele0b2f152016-09-28 10:23:49 +020060 // 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.
philipel97187112018-03-23 10:43:21 +010062 // TODO(philipel): Return a VideoLayerFrameId and not only the picture id.
philipele7c891f2018-02-22 14:35:06 +010063 int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame);
philipelbe7a9e52016-05-19 12:19:35 +020064
Sebastian Jansson13943b72019-04-02 15:08:14 +020065 void InsertFrame(std::unique_ptr<EncodedFrame> frame,
66 std::function<void(int64_t)> picture_id_handler);
67
philipelbe7a9e52016-05-19 12:19:35 +020068 // Get the next frame for decoding. Will return at latest after
philipel75562822016-09-05 10:57:41 +020069 // |max_wait_time_ms|.
philipele0b2f152016-09-28 10:23:49 +020070 // - If a frame is available within |max_wait_time_ms| it will return
philipel75562822016-09-05 10:57:41 +020071 // 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,
philipele7c891f2018-02-22 14:35:06 +010076 std::unique_ptr<EncodedFrame>* frame_out,
philipel3042c2d2017-08-18 04:55:02 -070077 bool keyframe_required = false);
Sebastian Jansson13943b72019-04-02 15:08:14 +020078 void NextFrame(
79 int64_t max_wait_time_ms,
80 bool keyframe_required,
81 std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)> handler);
philipelbe7a9e52016-05-19 12:19:35 +020082
philipel4f6cd6a2016-08-03 10:59:32 +020083 // 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
philipel504c47d2016-06-30 17:33:02 +020089 // 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
philipele21be1d2017-09-25 06:37:12 -070097 // Updates the RTT for jitter buffer estimation.
98 void UpdateRtt(int64_t rtt_ms);
99
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100100 // Clears the FrameBuffer, removing all the buffered frames.
101 void Clear();
102
philipelbe7a9e52016-05-19 12:19:35 +0200103 private:
philipele0b2f152016-09-28 10:23:49 +0200104 struct FrameInfo {
Niels Möllerbe682d42018-03-27 08:31:45 +0200105 FrameInfo();
106 FrameInfo(FrameInfo&&);
107 ~FrameInfo();
108
philipele0b2f152016-09-28 10:23:49 +0200109 // Which other frames that have direct unfulfilled dependencies
110 // on this frame.
Elad Alon69321dd2019-01-10 15:02:54 +0100111 absl::InlinedVector<VideoLayerFrameId, 8> dependent_frames;
philipele0b2f152016-09-28 10:23:49 +0200112
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
philipele7c891f2018-02-22 14:35:06 +0100127 // The actual EncodedFrame.
128 std::unique_ptr<EncodedFrame> frame;
philipele0b2f152016-09-28 10:23:49 +0200129 };
130
philipel0fa82a62018-03-19 15:34:53 +0100131 using FrameMap = std::map<VideoLayerFrameId, FrameInfo>;
philipele0b2f152016-09-28 10:23:49 +0200132
Sebastian Jansson13943b72019-04-02 15:08:14 +0200133 void SafePost(std::function<void()> func);
134
philipel112adf92017-06-15 09:06:21 -0700135 // Check that the references of |frame| are valid.
philipele7c891f2018-02-22 14:35:06 +0100136 bool ValidReferences(const EncodedFrame& frame) const;
philipel112adf92017-06-15 09:06:21 -0700137
Sebastian Jansson13943b72019-04-02 15:08:14 +0200138 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
philipele0b2f152016-09-28 10:23:49 +0200143 // 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)
danilchap56359be2017-09-07 07:53:45 -0700146 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200147
philipele0b2f152016-09-28 10:23:49 +0200148 // Marks the frame as decoded and updates all directly dependent frames.
149 void PropagateDecodability(const FrameInfo& info)
danilchap56359be2017-09-07 07:53:45 -0700150 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200151
philipele0b2f152016-09-28 10:23:49 +0200152 // Update the corresponding FrameInfo of |frame| and all FrameInfos that
153 // |frame| references.
154 // Return false if |frame| will never be decodable, true otherwise.
philipele7c891f2018-02-22 14:35:06 +0100155 bool UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200156 FrameMap::iterator info)
danilchap56359be2017-09-07 07:53:45 -0700157 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipele0b2f152016-09-28 10:23:49 +0200158
danilchap56359be2017-09-07 07:53:45 -0700159 void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe742702016-11-30 01:31:40 -0800160
danilchap56359be2017-09-07 07:53:45 -0700161 void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
ilnik2edc6842017-07-06 03:06:50 -0700162
danilchap56359be2017-09-07 07:53:45 -0700163 void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelfcc60062017-01-18 05:35:20 -0800164
Ilya Nikolaevskiy48193b02019-03-25 11:40:34 +0100165 // Checks if the superframe, which current frame belongs to, is complete.
166 bool IsCompleteSuperFrame(const EncodedFrame& frame)
167 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
168
philipele7c891f2018-02-22 14:35:06 +0100169 bool HasBadRenderTiming(const EncodedFrame& frame, int64_t now_ms)
danilchap56359be2017-09-07 07:53:45 -0700170 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
stefan95e97542017-05-23 09:52:18 -0700171
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100172 // 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 Nikolaevskiy6551faf2019-01-10 15:16:47 +0100179 // Stores only undecoded frames.
danilchap56359be2017-09-07 07:53:45 -0700180 FrameMap frames_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy13717842019-01-14 13:24:22 +0100181 DecodedFramesHistory decoded_frames_history_ RTC_GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200182
Sebastian Jansson13943b72019-04-02 15:08:14 +0200183 // TODO(srte): Remove this lock when always running on task queue.
philipelbe7a9e52016-05-19 12:19:35 +0200184 rtc::CriticalSection crit_;
185 Clock* const clock_;
Sebastian Jansson13943b72019-04-02 15:08:14 +0200186 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
tommi0a735642017-03-14 06:23:57 -0700194 rtc::Event new_continuous_frame_event_;
Sebastian Jansson13943b72019-04-02 15:08:14 +0200195
danilchap56359be2017-09-07 07:53:45 -0700196 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 Nikolaevskiy6551faf2019-01-10 15:16:47 +0100199 absl::optional<VideoLayerFrameId> last_continuous_frame_
200 RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100201 std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(crit_);
danilchap56359be2017-09-07 07:53:45 -0700202 bool stopped_ RTC_GUARDED_BY(crit_);
203 VCMVideoProtection protection_mode_ RTC_GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800204 VCMReceiveStatisticsCallback* const stats_callback_;
danilchap56359be2017-09-07 07:53:45 -0700205 int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200206
Elad Alone4b50232019-01-14 18:56:14 +0100207 const bool add_rtt_to_playout_delay_;
208
Sebastian Jansson13943b72019-04-02 15:08:14 +0200209 // Defined last so it is destroyed before other members.
210 rtc::TaskQueue task_queue_;
philipelbe7a9e52016-05-19 12:19:35 +0200211 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
philipelbe7a9e52016-05-19 12:19:35 +0200212};
213
214} // namespace video_coding
215} // namespace webrtc
216
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200217#endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_