blob: 4950dac3ffa7bf08e8fdb73b506f2cc675ab3d0a [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
11#ifndef WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_
12#define WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_
13
14#include <array>
15#include <map>
16#include <memory>
philipelbe7a9e52016-05-19 12:19:35 +020017#include <utility>
18
philipele0b2f152016-09-28 10:23:49 +020019#include "webrtc/modules/video_coding/frame_object.h"
philipel4f6cd6a2016-08-03 10:59:32 +020020#include "webrtc/modules/video_coding/include/video_coding_defines.h"
21#include "webrtc/modules/video_coding/inter_frame_delay.h"
philipele0b2f152016-09-28 10:23:49 +020022#include "webrtc/modules/video_coding/sequence_number_util.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020023#include "webrtc/rtc_base/constructormagic.h"
24#include "webrtc/rtc_base/criticalsection.h"
25#include "webrtc/rtc_base/event.h"
26#include "webrtc/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,
tkchin53959fc2017-08-17 11:01:46 -070060 std::unique_ptr<FrameObject>* frame_out);
philipelbe7a9e52016-05-19 12:19:35 +020061
philipel4f6cd6a2016-08-03 10:59:32 +020062 // Tells the FrameBuffer which protection mode that is in use. Affects
63 // the frame timing.
64 // TODO(philipel): Remove this when new timing calculations has been
65 // implemented.
66 void SetProtectionMode(VCMVideoProtection mode);
67
philipel504c47d2016-06-30 17:33:02 +020068 // Start the frame buffer, has no effect if the frame buffer is started.
69 // The frame buffer is started upon construction.
70 void Start();
71
72 // Stop the frame buffer, causing any sleeping thread in NextFrame to
73 // return immediately.
74 void Stop();
75
gustavogbf1e08d02017-08-09 05:43:08 -070076 // Updates the RTT for jitter buffer estimation.
77 void UpdateRtt(int64_t rtt_ms);
78
philipelbe7a9e52016-05-19 12:19:35 +020079 private:
philipele0b2f152016-09-28 10:23:49 +020080 struct FrameKey {
81 FrameKey() : picture_id(0), spatial_layer(0) {}
82 FrameKey(uint16_t picture_id, uint8_t spatial_layer)
83 : picture_id(picture_id), spatial_layer(spatial_layer) {}
philipelbe7a9e52016-05-19 12:19:35 +020084
philipele0b2f152016-09-28 10:23:49 +020085 bool operator<(const FrameKey& rhs) const {
86 if (picture_id == rhs.picture_id)
87 return spatial_layer < rhs.spatial_layer;
88 return AheadOf(rhs.picture_id, picture_id);
89 }
90
91 bool operator<=(const FrameKey& rhs) const { return !(rhs < *this); }
92
93 uint16_t picture_id;
94 uint8_t spatial_layer;
philipelbe7a9e52016-05-19 12:19:35 +020095 };
96
philipele0b2f152016-09-28 10:23:49 +020097 struct FrameInfo {
98 // The maximum number of frames that can depend on this frame.
99 static constexpr size_t kMaxNumDependentFrames = 8;
100
101 // Which other frames that have direct unfulfilled dependencies
102 // on this frame.
philipel112adf92017-06-15 09:06:21 -0700103 // TODO(philipel): Add simple modify/access functions to prevent adding too
104 // many |dependent_frames|.
philipele0b2f152016-09-28 10:23:49 +0200105 FrameKey dependent_frames[kMaxNumDependentFrames];
106 size_t num_dependent_frames = 0;
107
108 // A frame is continiuous if it has all its referenced/indirectly
109 // referenced frames.
110 //
111 // How many unfulfilled frames this frame have until it becomes continuous.
112 size_t num_missing_continuous = 0;
113
114 // A frame is decodable if all its referenced frames have been decoded.
115 //
116 // How many unfulfilled frames this frame have until it becomes decodable.
117 size_t num_missing_decodable = 0;
118
119 // If this frame is continuous or not.
120 bool continuous = false;
121
122 // The actual FrameObject.
123 std::unique_ptr<FrameObject> frame;
124 };
125
126 using FrameMap = std::map<FrameKey, FrameInfo>;
127
philipel112adf92017-06-15 09:06:21 -0700128 // Check that the references of |frame| are valid.
129 bool ValidReferences(const FrameObject& frame) const;
130
gnishb2a318b2017-05-10 09:21:33 -0700131 // Updates the minimal and maximal playout delays
132 // depending on the frame.
133 void UpdatePlayoutDelays(const FrameObject& frame)
134 EXCLUSIVE_LOCKS_REQUIRED(crit_);
135
philipele0b2f152016-09-28 10:23:49 +0200136 // Update all directly dependent and indirectly dependent frames and mark
137 // them as continuous if all their references has been fulfilled.
138 void PropagateContinuity(FrameMap::iterator start)
philipelbe7a9e52016-05-19 12:19:35 +0200139 EXCLUSIVE_LOCKS_REQUIRED(crit_);
140
philipele0b2f152016-09-28 10:23:49 +0200141 // Marks the frame as decoded and updates all directly dependent frames.
142 void PropagateDecodability(const FrameInfo& info)
143 EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200144
philipele0b2f152016-09-28 10:23:49 +0200145 // Advances |last_decoded_frame_it_| to |decoded| and removes old
146 // frame info.
147 void AdvanceLastDecodedFrame(FrameMap::iterator decoded)
148 EXCLUSIVE_LOCKS_REQUIRED(crit_);
149
150 // Update the corresponding FrameInfo of |frame| and all FrameInfos that
151 // |frame| references.
152 // Return false if |frame| will never be decodable, true otherwise.
153 bool UpdateFrameInfoWithIncomingFrame(const FrameObject& frame,
154 FrameMap::iterator info)
155 EXCLUSIVE_LOCKS_REQUIRED(crit_);
156
philipelbe742702016-11-30 01:31:40 -0800157 void UpdateJitterDelay() EXCLUSIVE_LOCKS_REQUIRED(crit_);
158
ilnik2edc6842017-07-06 03:06:50 -0700159 void UpdateTimingFrameInfo() EXCLUSIVE_LOCKS_REQUIRED(crit_);
160
philipelfcc60062017-01-18 05:35:20 -0800161 void ClearFramesAndHistory() EXCLUSIVE_LOCKS_REQUIRED(crit_);
162
stefan95e97542017-05-23 09:52:18 -0700163 bool HasBadRenderTiming(const FrameObject& frame, int64_t now_ms)
164 EXCLUSIVE_LOCKS_REQUIRED(crit_);
165
philipele0b2f152016-09-28 10:23:49 +0200166 FrameMap frames_ GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200167
168 rtc::CriticalSection crit_;
169 Clock* const clock_;
tommi0a735642017-03-14 06:23:57 -0700170 rtc::Event new_continuous_frame_event_;
philipel4f6cd6a2016-08-03 10:59:32 +0200171 VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_);
172 VCMTiming* const timing_ GUARDED_BY(crit_);
173 VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_);
philipelfcc60062017-01-18 05:35:20 -0800174 uint32_t last_decoded_frame_timestamp_ GUARDED_BY(crit_);
philipele0b2f152016-09-28 10:23:49 +0200175 FrameMap::iterator last_decoded_frame_it_ GUARDED_BY(crit_);
176 FrameMap::iterator last_continuous_frame_it_ GUARDED_BY(crit_);
philipel1c056252017-01-31 09:53:12 -0800177 FrameMap::iterator next_frame_it_ GUARDED_BY(crit_);
philipele0b2f152016-09-28 10:23:49 +0200178 int num_frames_history_ GUARDED_BY(crit_);
179 int num_frames_buffered_ GUARDED_BY(crit_);
philipel29f730e2017-03-15 08:10:08 -0700180 bool stopped_ GUARDED_BY(crit_);
philipel4f6cd6a2016-08-03 10:59:32 +0200181 VCMVideoProtection protection_mode_ GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800182 VCMReceiveStatisticsCallback* const stats_callback_;
philipel65e1f942017-07-24 08:26:53 -0700183 int64_t last_log_non_decoded_ms_ GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200184
185 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
philipelbe7a9e52016-05-19 12:19:35 +0200186};
187
188} // namespace video_coding
189} // namespace webrtc
190
191#endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_