blob: 32eba8a3cb406fe00985752317eee09d2b3643c6 [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>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/frame_object.h"
20#include "modules/video_coding/include/video_coding_defines.h"
21#include "modules/video_coding/inter_frame_delay.h"
22#include "modules/video_coding/sequence_number_util.h"
23#include "rtc_base/constructormagic.h"
24#include "rtc_base/criticalsection.h"
25#include "rtc_base/event.h"
26#include "rtc_base/thread_annotations.h"
philipelbe7a9e52016-05-19 12:19:35 +020027
28namespace webrtc {
29
30class Clock;
philipela45102f2017-02-22 05:30:39 -080031class VCMReceiveStatisticsCallback;
philipelbe7a9e52016-05-19 12:19:35 +020032class VCMJitterEstimator;
33class VCMTiming;
34
35namespace video_coding {
36
philipelbe7a9e52016-05-19 12:19:35 +020037class FrameBuffer {
38 public:
philipel75562822016-09-05 10:57:41 +020039 enum ReturnReason { kFrameFound, kTimeout, kStopped };
40
philipelbe7a9e52016-05-19 12:19:35 +020041 FrameBuffer(Clock* clock,
42 VCMJitterEstimator* jitter_estimator,
philipela45102f2017-02-22 05:30:39 -080043 VCMTiming* timing,
44 VCMReceiveStatisticsCallback* stats_proxy);
philipelbe7a9e52016-05-19 12:19:35 +020045
philipel266f0a42016-11-28 08:49:07 -080046 virtual ~FrameBuffer();
47
philipele0b2f152016-09-28 10:23:49 +020048 // Insert a frame into the frame buffer. Returns the picture id
49 // of the last continuous frame or -1 if there is no continuous frame.
50 int InsertFrame(std::unique_ptr<FrameObject> frame);
philipelbe7a9e52016-05-19 12:19:35 +020051
52 // Get the next frame for decoding. Will return at latest after
philipel75562822016-09-05 10:57:41 +020053 // |max_wait_time_ms|.
philipele0b2f152016-09-28 10:23:49 +020054 // - If a frame is available within |max_wait_time_ms| it will return
philipel75562822016-09-05 10:57:41 +020055 // kFrameFound and set |frame_out| to the resulting frame.
56 // - If no frame is available after |max_wait_time_ms| it will return
57 // kTimeout.
58 // - If the FrameBuffer is stopped then it will return kStopped.
59 ReturnReason NextFrame(int64_t max_wait_time_ms,
philipel3042c2d2017-08-18 04:55:02 -070060 std::unique_ptr<FrameObject>* frame_out,
61 bool keyframe_required = false);
philipelbe7a9e52016-05-19 12:19:35 +020062
philipel4f6cd6a2016-08-03 10:59:32 +020063 // Tells the FrameBuffer which protection mode that is in use. Affects
64 // the frame timing.
65 // TODO(philipel): Remove this when new timing calculations has been
66 // implemented.
67 void SetProtectionMode(VCMVideoProtection mode);
68
philipel504c47d2016-06-30 17:33:02 +020069 // Start the frame buffer, has no effect if the frame buffer is started.
70 // The frame buffer is started upon construction.
71 void Start();
72
73 // Stop the frame buffer, causing any sleeping thread in NextFrame to
74 // return immediately.
75 void Stop();
76
philipelbe7a9e52016-05-19 12:19:35 +020077 private:
philipele0b2f152016-09-28 10:23:49 +020078 struct FrameKey {
philipel3b3c9c42017-09-11 09:38:36 -070079 FrameKey() : picture_id(-1), spatial_layer(0) {}
80 FrameKey(int64_t picture_id, uint8_t spatial_layer)
philipele0b2f152016-09-28 10:23:49 +020081 : picture_id(picture_id), spatial_layer(spatial_layer) {}
philipelbe7a9e52016-05-19 12:19:35 +020082
philipele0b2f152016-09-28 10:23:49 +020083 bool operator<(const FrameKey& rhs) const {
84 if (picture_id == rhs.picture_id)
85 return spatial_layer < rhs.spatial_layer;
philipel3b3c9c42017-09-11 09:38:36 -070086 return picture_id < rhs.picture_id;
philipele0b2f152016-09-28 10:23:49 +020087 }
88
89 bool operator<=(const FrameKey& rhs) const { return !(rhs < *this); }
90
philipel3b3c9c42017-09-11 09:38:36 -070091 int64_t picture_id;
philipele0b2f152016-09-28 10:23:49 +020092 uint8_t spatial_layer;
philipelbe7a9e52016-05-19 12:19:35 +020093 };
94
philipele0b2f152016-09-28 10:23:49 +020095 struct FrameInfo {
96 // The maximum number of frames that can depend on this frame.
97 static constexpr size_t kMaxNumDependentFrames = 8;
98
99 // Which other frames that have direct unfulfilled dependencies
100 // on this frame.
philipel112adf92017-06-15 09:06:21 -0700101 // TODO(philipel): Add simple modify/access functions to prevent adding too
102 // many |dependent_frames|.
philipele0b2f152016-09-28 10:23:49 +0200103 FrameKey dependent_frames[kMaxNumDependentFrames];
104 size_t num_dependent_frames = 0;
105
106 // A frame is continiuous if it has all its referenced/indirectly
107 // referenced frames.
108 //
109 // How many unfulfilled frames this frame have until it becomes continuous.
110 size_t num_missing_continuous = 0;
111
112 // A frame is decodable if all its referenced frames have been decoded.
113 //
114 // How many unfulfilled frames this frame have until it becomes decodable.
115 size_t num_missing_decodable = 0;
116
117 // If this frame is continuous or not.
118 bool continuous = false;
119
120 // The actual FrameObject.
121 std::unique_ptr<FrameObject> frame;
122 };
123
124 using FrameMap = std::map<FrameKey, FrameInfo>;
125
philipel112adf92017-06-15 09:06:21 -0700126 // Check that the references of |frame| are valid.
127 bool ValidReferences(const FrameObject& frame) const;
128
gnishb2a318b2017-05-10 09:21:33 -0700129 // Updates the minimal and maximal playout delays
130 // depending on the frame.
131 void UpdatePlayoutDelays(const FrameObject& frame)
danilchap56359be2017-09-07 07:53:45 -0700132 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
gnishb2a318b2017-05-10 09:21:33 -0700133
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 // Advances |last_decoded_frame_it_| to |decoded| and removes old
144 // frame info.
145 void AdvanceLastDecodedFrame(FrameMap::iterator decoded)
danilchap56359be2017-09-07 07:53:45 -0700146 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipele0b2f152016-09-28 10:23:49 +0200147
148 // Update the corresponding FrameInfo of |frame| and all FrameInfos that
149 // |frame| references.
150 // Return false if |frame| will never be decodable, true otherwise.
151 bool UpdateFrameInfoWithIncomingFrame(const FrameObject& frame,
152 FrameMap::iterator info)
danilchap56359be2017-09-07 07:53:45 -0700153 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipele0b2f152016-09-28 10:23:49 +0200154
danilchap56359be2017-09-07 07:53:45 -0700155 void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe742702016-11-30 01:31:40 -0800156
danilchap56359be2017-09-07 07:53:45 -0700157 void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
ilnik2edc6842017-07-06 03:06:50 -0700158
danilchap56359be2017-09-07 07:53:45 -0700159 void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelfcc60062017-01-18 05:35:20 -0800160
stefan95e97542017-05-23 09:52:18 -0700161 bool HasBadRenderTiming(const FrameObject& frame, int64_t now_ms)
danilchap56359be2017-09-07 07:53:45 -0700162 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
stefan95e97542017-05-23 09:52:18 -0700163
danilchap56359be2017-09-07 07:53:45 -0700164 FrameMap frames_ 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_);
172 uint32_t last_decoded_frame_timestamp_ RTC_GUARDED_BY(crit_);
173 FrameMap::iterator last_decoded_frame_it_ RTC_GUARDED_BY(crit_);
174 FrameMap::iterator last_continuous_frame_it_ RTC_GUARDED_BY(crit_);
175 FrameMap::iterator next_frame_it_ RTC_GUARDED_BY(crit_);
176 int num_frames_history_ RTC_GUARDED_BY(crit_);
177 int num_frames_buffered_ RTC_GUARDED_BY(crit_);
178 bool stopped_ RTC_GUARDED_BY(crit_);
179 VCMVideoProtection protection_mode_ RTC_GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800180 VCMReceiveStatisticsCallback* const stats_callback_;
danilchap56359be2017-09-07 07:53:45 -0700181 int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200182
183 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
philipelbe7a9e52016-05-19 12:19:35 +0200184};
185
186} // namespace video_coding
187} // namespace webrtc
188
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200189#endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_