blob: cffdf3e5cc65e5886be140eef67081b3e0961b17 [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"
Niels Möllerd9c2d942019-04-30 09:16:36 +020024#include "modules/video_coding/jitter_estimator.h"
Ilya Nikolaevskiy13717842019-01-14 13:24:22 +010025#include "modules/video_coding/utility/decoded_frames_history.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/constructor_magic.h"
27#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/event.h"
“Michaelf9fc1712018-08-27 10:08:58 -050029#include "rtc_base/experiments/rtt_mult_experiment.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020030#include "rtc_base/numerics/sequence_number_util.h"
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +020031#include "rtc_base/task_queue.h"
32#include "rtc_base/task_utils/repeating_task.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/thread_annotations.h"
philipelbe7a9e52016-05-19 12:19:35 +020034
35namespace webrtc {
36
37class Clock;
philipela45102f2017-02-22 05:30:39 -080038class VCMReceiveStatisticsCallback;
philipelbe7a9e52016-05-19 12:19:35 +020039class VCMJitterEstimator;
40class VCMTiming;
41
42namespace video_coding {
43
philipelbe7a9e52016-05-19 12:19:35 +020044class FrameBuffer {
45 public:
philipel75562822016-09-05 10:57:41 +020046 enum ReturnReason { kFrameFound, kTimeout, kStopped };
47
philipelbe7a9e52016-05-19 12:19:35 +020048 FrameBuffer(Clock* clock,
philipela45102f2017-02-22 05:30:39 -080049 VCMTiming* timing,
Sebastian Jansson1c747f52019-04-04 13:01:39 +020050 VCMReceiveStatisticsCallback* stats_callback);
philipelbe7a9e52016-05-19 12:19:35 +020051
philipel266f0a42016-11-28 08:49:07 -080052 virtual ~FrameBuffer();
53
philipele0b2f152016-09-28 10:23:49 +020054 // Insert a frame into the frame buffer. Returns the picture id
55 // of the last continuous frame or -1 if there is no continuous frame.
philipel97187112018-03-23 10:43:21 +010056 // TODO(philipel): Return a VideoLayerFrameId and not only the picture id.
philipele7c891f2018-02-22 14:35:06 +010057 int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame);
philipelbe7a9e52016-05-19 12:19:35 +020058
59 // Get the next frame for decoding. Will return at latest after
philipel75562822016-09-05 10:57:41 +020060 // |max_wait_time_ms|.
philipele0b2f152016-09-28 10:23:49 +020061 // - If a frame is available within |max_wait_time_ms| it will return
philipel75562822016-09-05 10:57:41 +020062 // kFrameFound and set |frame_out| to the resulting frame.
63 // - If no frame is available after |max_wait_time_ms| it will return
64 // kTimeout.
65 // - If the FrameBuffer is stopped then it will return kStopped.
66 ReturnReason NextFrame(int64_t max_wait_time_ms,
philipele7c891f2018-02-22 14:35:06 +010067 std::unique_ptr<EncodedFrame>* frame_out,
philipel3042c2d2017-08-18 04:55:02 -070068 bool keyframe_required = false);
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +020069 void NextFrame(
70 int64_t max_wait_time_ms,
71 bool keyframe_required,
72 rtc::TaskQueue* callback_queue,
73 std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)> handler);
philipelbe7a9e52016-05-19 12:19:35 +020074
philipel4f6cd6a2016-08-03 10:59:32 +020075 // Tells the FrameBuffer which protection mode that is in use. Affects
76 // the frame timing.
77 // TODO(philipel): Remove this when new timing calculations has been
78 // implemented.
79 void SetProtectionMode(VCMVideoProtection mode);
80
philipel504c47d2016-06-30 17:33:02 +020081 // Start the frame buffer, has no effect if the frame buffer is started.
82 // The frame buffer is started upon construction.
83 void Start();
84
85 // Stop the frame buffer, causing any sleeping thread in NextFrame to
86 // return immediately.
87 void Stop();
88
philipele21be1d2017-09-25 06:37:12 -070089 // Updates the RTT for jitter buffer estimation.
90 void UpdateRtt(int64_t rtt_ms);
91
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +010092 // Clears the FrameBuffer, removing all the buffered frames.
93 void Clear();
94
philipelbe7a9e52016-05-19 12:19:35 +020095 private:
philipele0b2f152016-09-28 10:23:49 +020096 struct FrameInfo {
Niels Möllerbe682d42018-03-27 08:31:45 +020097 FrameInfo();
98 FrameInfo(FrameInfo&&);
99 ~FrameInfo();
100
philipele0b2f152016-09-28 10:23:49 +0200101 // Which other frames that have direct unfulfilled dependencies
102 // on this frame.
Elad Alon69321dd2019-01-10 15:02:54 +0100103 absl::InlinedVector<VideoLayerFrameId, 8> dependent_frames;
philipele0b2f152016-09-28 10:23:49 +0200104
105 // A frame is continiuous if it has all its referenced/indirectly
106 // referenced frames.
107 //
108 // How many unfulfilled frames this frame have until it becomes continuous.
109 size_t num_missing_continuous = 0;
110
111 // A frame is decodable if all its referenced frames have been decoded.
112 //
113 // How many unfulfilled frames this frame have until it becomes decodable.
114 size_t num_missing_decodable = 0;
115
116 // If this frame is continuous or not.
117 bool continuous = false;
118
philipele7c891f2018-02-22 14:35:06 +0100119 // The actual EncodedFrame.
120 std::unique_ptr<EncodedFrame> frame;
philipele0b2f152016-09-28 10:23:49 +0200121 };
122
philipel0fa82a62018-03-19 15:34:53 +0100123 using FrameMap = std::map<VideoLayerFrameId, FrameInfo>;
philipele0b2f152016-09-28 10:23:49 +0200124
philipel112adf92017-06-15 09:06:21 -0700125 // Check that the references of |frame| are valid.
philipele7c891f2018-02-22 14:35:06 +0100126 bool ValidReferences(const EncodedFrame& frame) const;
philipel112adf92017-06-15 09:06:21 -0700127
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200128 int64_t FindNextFrame(int64_t now_ms) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
129 EncodedFrame* GetNextFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
130
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200131 void StartWaitForNextFrameOnQueue() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
132 void CancelCallback() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
133
philipele0b2f152016-09-28 10:23:49 +0200134 // Update all directly dependent and indirectly dependent frames and mark
135 // them as continuous if all their references has been fulfilled.
136 void PropagateContinuity(FrameMap::iterator start)
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 // Marks the frame as decoded and updates all directly dependent frames.
140 void PropagateDecodability(const FrameInfo& info)
danilchap56359be2017-09-07 07:53:45 -0700141 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200142
philipele0b2f152016-09-28 10:23:49 +0200143 // Update the corresponding FrameInfo of |frame| and all FrameInfos that
144 // |frame| references.
145 // Return false if |frame| will never be decodable, true otherwise.
philipele7c891f2018-02-22 14:35:06 +0100146 bool UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200147 FrameMap::iterator info)
danilchap56359be2017-09-07 07:53:45 -0700148 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipele0b2f152016-09-28 10:23:49 +0200149
danilchap56359be2017-09-07 07:53:45 -0700150 void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe742702016-11-30 01:31:40 -0800151
danilchap56359be2017-09-07 07:53:45 -0700152 void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
ilnik2edc6842017-07-06 03:06:50 -0700153
danilchap56359be2017-09-07 07:53:45 -0700154 void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelfcc60062017-01-18 05:35:20 -0800155
Ilya Nikolaevskiy48193b02019-03-25 11:40:34 +0100156 // Checks if the superframe, which current frame belongs to, is complete.
157 bool IsCompleteSuperFrame(const EncodedFrame& frame)
158 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
159
philipele7c891f2018-02-22 14:35:06 +0100160 bool HasBadRenderTiming(const EncodedFrame& frame, int64_t now_ms)
danilchap56359be2017-09-07 07:53:45 -0700161 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
stefan95e97542017-05-23 09:52:18 -0700162
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100163 // The cleaner solution would be to have the NextFrame function return a
164 // vector of frames, but until the decoding pipeline can support decoding
165 // multiple frames at the same time we combine all frames to one frame and
166 // return it. See bugs.webrtc.org/10064
167 EncodedFrame* CombineAndDeleteFrames(
168 const std::vector<EncodedFrame*>& frames) const;
169
Ilya Nikolaevskiy6551faf2019-01-10 15:16:47 +0100170 // Stores only undecoded frames.
danilchap56359be2017-09-07 07:53:45 -0700171 FrameMap frames_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy13717842019-01-14 13:24:22 +0100172 DecodedFramesHistory decoded_frames_history_ RTC_GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200173
174 rtc::CriticalSection crit_;
175 Clock* const clock_;
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200176
177 rtc::TaskQueue* callback_queue_ RTC_GUARDED_BY(crit_);
178 RepeatingTaskHandle callback_task_ RTC_GUARDED_BY(crit_);
179 std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)>
180 frame_handler_ RTC_GUARDED_BY(crit_);
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200181 int64_t latest_return_time_ms_ RTC_GUARDED_BY(crit_);
182 bool keyframe_required_ RTC_GUARDED_BY(crit_);
183
tommi0a735642017-03-14 06:23:57 -0700184 rtc::Event new_continuous_frame_event_;
Niels Möllerd9c2d942019-04-30 09:16:36 +0200185 VCMJitterEstimator jitter_estimator_ RTC_GUARDED_BY(crit_);
danilchap56359be2017-09-07 07:53:45 -0700186 VCMTiming* const timing_ RTC_GUARDED_BY(crit_);
187 VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy6551faf2019-01-10 15:16:47 +0100188 absl::optional<VideoLayerFrameId> last_continuous_frame_
189 RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100190 std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(crit_);
danilchap56359be2017-09-07 07:53:45 -0700191 bool stopped_ RTC_GUARDED_BY(crit_);
192 VCMVideoProtection protection_mode_ RTC_GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800193 VCMReceiveStatisticsCallback* const stats_callback_;
danilchap56359be2017-09-07 07:53:45 -0700194 int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200195
Elad Alone4b50232019-01-14 18:56:14 +0100196 const bool add_rtt_to_playout_delay_;
197
philipelbe7a9e52016-05-19 12:19:35 +0200198 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
philipelbe7a9e52016-05-19 12:19:35 +0200199};
200
201} // namespace video_coding
202} // namespace webrtc
203
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200204#endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_