blob: 9226cc38480e2c23f8e00c1c28d3615acd8d1bd3 [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"
Artem Titovd15a5752021-02-10 14:31:24 +010021#include "api/sequence_checker.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"
Niels Möllerd9c2d942019-04-30 09:16:36 +020025#include "modules/video_coding/jitter_estimator.h"
Ilya Nikolaevskiy13717842019-01-14 13:24:22 +010026#include "modules/video_coding/utility/decoded_frames_history.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"
Markus Handell6deec382020-07-07 12:17:12 +020030#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010031#include "rtc_base/system/no_unique_address.h"
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +020032#include "rtc_base/task_queue.h"
33#include "rtc_base/task_utils/repeating_task.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/thread_annotations.h"
philipelbe7a9e52016-05-19 12:19:35 +020035
36namespace webrtc {
37
38class Clock;
philipela45102f2017-02-22 05:30:39 -080039class VCMReceiveStatisticsCallback;
philipelbe7a9e52016-05-19 12:19:35 +020040class VCMJitterEstimator;
41class VCMTiming;
42
43namespace video_coding {
44
philipelbe7a9e52016-05-19 12:19:35 +020045class FrameBuffer {
46 public:
philipel75562822016-09-05 10:57:41 +020047 enum ReturnReason { kFrameFound, kTimeout, kStopped };
48
philipelbe7a9e52016-05-19 12:19:35 +020049 FrameBuffer(Clock* clock,
philipela45102f2017-02-22 05:30:39 -080050 VCMTiming* timing,
Sebastian Jansson1c747f52019-04-04 13:01:39 +020051 VCMReceiveStatisticsCallback* stats_callback);
philipelbe7a9e52016-05-19 12:19:35 +020052
Niels Möllerde953292020-09-29 09:46:21 +020053 FrameBuffer() = delete;
54 FrameBuffer(const FrameBuffer&) = delete;
55 FrameBuffer& operator=(const FrameBuffer&) = delete;
56
philipel266f0a42016-11-28 08:49:07 -080057 virtual ~FrameBuffer();
58
philipele0b2f152016-09-28 10:23:49 +020059 // Insert a frame into the frame buffer. Returns the picture id
60 // of the last continuous frame or -1 if there is no continuous frame.
philipele7c891f2018-02-22 14:35:06 +010061 int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame);
philipelbe7a9e52016-05-19 12:19:35 +020062
63 // Get the next frame for decoding. Will return at latest after
Artem Titovdcd7fc72021-08-09 13:02:57 +020064 // `max_wait_time_ms`.
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +020065 void NextFrame(
66 int64_t max_wait_time_ms,
67 bool keyframe_required,
68 rtc::TaskQueue* callback_queue,
69 std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)> handler);
philipelbe7a9e52016-05-19 12:19:35 +020070
philipel4f6cd6a2016-08-03 10:59:32 +020071 // Tells the FrameBuffer which protection mode that is in use. Affects
72 // the frame timing.
73 // TODO(philipel): Remove this when new timing calculations has been
74 // implemented.
75 void SetProtectionMode(VCMVideoProtection mode);
76
philipel504c47d2016-06-30 17:33:02 +020077 // Stop the frame buffer, causing any sleeping thread in NextFrame to
78 // return immediately.
79 void Stop();
80
philipele21be1d2017-09-25 06:37:12 -070081 // Updates the RTT for jitter buffer estimation.
82 void UpdateRtt(int64_t rtt_ms);
83
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +010084 // Clears the FrameBuffer, removing all the buffered frames.
85 void Clear();
86
Johannes Kron111e9812020-10-26 13:54:40 +010087 int Size();
88
philipelbe7a9e52016-05-19 12:19:35 +020089 private:
philipele0b2f152016-09-28 10:23:49 +020090 struct FrameInfo {
Niels Möllerbe682d42018-03-27 08:31:45 +020091 FrameInfo();
92 FrameInfo(FrameInfo&&);
93 ~FrameInfo();
94
philipele0b2f152016-09-28 10:23:49 +020095 // Which other frames that have direct unfulfilled dependencies
96 // on this frame.
philipel9aa9b8d2021-02-15 13:31:29 +010097 absl::InlinedVector<int64_t, 8> dependent_frames;
philipele0b2f152016-09-28 10:23:49 +020098
99 // A frame is continiuous if it has all its referenced/indirectly
100 // referenced frames.
101 //
102 // How many unfulfilled frames this frame have until it becomes continuous.
103 size_t num_missing_continuous = 0;
104
105 // A frame is decodable if all its referenced frames have been decoded.
106 //
107 // How many unfulfilled frames this frame have until it becomes decodable.
108 size_t num_missing_decodable = 0;
109
110 // If this frame is continuous or not.
111 bool continuous = false;
112
philipele7c891f2018-02-22 14:35:06 +0100113 // The actual EncodedFrame.
114 std::unique_ptr<EncodedFrame> frame;
philipele0b2f152016-09-28 10:23:49 +0200115 };
116
philipel9aa9b8d2021-02-15 13:31:29 +0100117 using FrameMap = std::map<int64_t, FrameInfo>;
philipele0b2f152016-09-28 10:23:49 +0200118
Artem Titovdcd7fc72021-08-09 13:02:57 +0200119 // Check that the references of `frame` are valid.
philipele7c891f2018-02-22 14:35:06 +0100120 bool ValidReferences(const EncodedFrame& frame) const;
philipel112adf92017-06-15 09:06:21 -0700121
Markus Handell6deec382020-07-07 12:17:12 +0200122 int64_t FindNextFrame(int64_t now_ms) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
123 EncodedFrame* GetNextFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200124
Markus Handell6deec382020-07-07 12:17:12 +0200125 void StartWaitForNextFrameOnQueue() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
126 void CancelCallback() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200127
philipele0b2f152016-09-28 10:23:49 +0200128 // Update all directly dependent and indirectly dependent frames and mark
129 // them as continuous if all their references has been fulfilled.
130 void PropagateContinuity(FrameMap::iterator start)
Markus Handell6deec382020-07-07 12:17:12 +0200131 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelbe7a9e52016-05-19 12:19:35 +0200132
philipele0b2f152016-09-28 10:23:49 +0200133 // Marks the frame as decoded and updates all directly dependent frames.
134 void PropagateDecodability(const FrameInfo& info)
Markus Handell6deec382020-07-07 12:17:12 +0200135 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelbe7a9e52016-05-19 12:19:35 +0200136
Artem Titovdcd7fc72021-08-09 13:02:57 +0200137 // Update the corresponding FrameInfo of `frame` and all FrameInfos that
138 // `frame` references.
139 // Return false if `frame` will never be decodable, true otherwise.
philipele7c891f2018-02-22 14:35:06 +0100140 bool UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200141 FrameMap::iterator info)
Markus Handell6deec382020-07-07 12:17:12 +0200142 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipele0b2f152016-09-28 10:23:49 +0200143
Markus Handell6deec382020-07-07 12:17:12 +0200144 void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelbe742702016-11-30 01:31:40 -0800145
Markus Handell6deec382020-07-07 12:17:12 +0200146 void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
ilnik2edc6842017-07-06 03:06:50 -0700147
Markus Handell6deec382020-07-07 12:17:12 +0200148 void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelfcc60062017-01-18 05:35:20 -0800149
philipele7c891f2018-02-22 14:35:06 +0100150 bool HasBadRenderTiming(const EncodedFrame& frame, int64_t now_ms)
Markus Handell6deec382020-07-07 12:17:12 +0200151 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
stefan95e97542017-05-23 09:52:18 -0700152
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100153 // The cleaner solution would be to have the NextFrame function return a
154 // vector of frames, but until the decoding pipeline can support decoding
155 // multiple frames at the same time we combine all frames to one frame and
156 // return it. See bugs.webrtc.org/10064
157 EncodedFrame* CombineAndDeleteFrames(
158 const std::vector<EncodedFrame*>& frames) const;
159
Mirko Bonadei20e4c802020-11-23 11:07:42 +0100160 RTC_NO_UNIQUE_ADDRESS SequenceChecker construction_checker_;
161 RTC_NO_UNIQUE_ADDRESS SequenceChecker callback_checker_;
Tommi430951a2020-05-19 23:27:29 +0200162
Ilya Nikolaevskiy6551faf2019-01-10 15:16:47 +0100163 // Stores only undecoded frames.
Markus Handell6deec382020-07-07 12:17:12 +0200164 FrameMap frames_ RTC_GUARDED_BY(mutex_);
165 DecodedFramesHistory decoded_frames_history_ RTC_GUARDED_BY(mutex_);
philipelbe7a9e52016-05-19 12:19:35 +0200166
Markus Handell6deec382020-07-07 12:17:12 +0200167 Mutex mutex_;
philipelbe7a9e52016-05-19 12:19:35 +0200168 Clock* const clock_;
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200169
Markus Handell6deec382020-07-07 12:17:12 +0200170 rtc::TaskQueue* callback_queue_ RTC_GUARDED_BY(mutex_);
171 RepeatingTaskHandle callback_task_ RTC_GUARDED_BY(mutex_);
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200172 std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)>
Markus Handell6deec382020-07-07 12:17:12 +0200173 frame_handler_ RTC_GUARDED_BY(mutex_);
174 int64_t latest_return_time_ms_ RTC_GUARDED_BY(mutex_);
175 bool keyframe_required_ RTC_GUARDED_BY(mutex_);
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200176
Markus Handell6deec382020-07-07 12:17:12 +0200177 VCMJitterEstimator jitter_estimator_ RTC_GUARDED_BY(mutex_);
178 VCMTiming* const timing_ RTC_GUARDED_BY(mutex_);
179 VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(mutex_);
philipel9aa9b8d2021-02-15 13:31:29 +0100180 absl::optional<int64_t> last_continuous_frame_ RTC_GUARDED_BY(mutex_);
Markus Handell6deec382020-07-07 12:17:12 +0200181 std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(mutex_);
182 bool stopped_ RTC_GUARDED_BY(mutex_);
183 VCMVideoProtection protection_mode_ RTC_GUARDED_BY(mutex_);
philipela45102f2017-02-22 05:30:39 -0800184 VCMReceiveStatisticsCallback* const stats_callback_;
Markus Handell6deec382020-07-07 12:17:12 +0200185 int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(mutex_);
philipelbe7a9e52016-05-19 12:19:35 +0200186
Elad Alone4b50232019-01-14 18:56:14 +0100187 const bool add_rtt_to_playout_delay_;
188
“Michaeld3a4ebe2019-06-07 03:55:01 -0500189 // rtt_mult experiment settings.
190 const absl::optional<RttMultExperiment::Settings> rtt_mult_settings_;
philipelbe7a9e52016-05-19 12:19:35 +0200191};
192
193} // namespace video_coding
194} // namespace webrtc
195
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200196#endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_