blob: 8f726d0d869dee1856bb6d2e924c0c6b534ddde2 [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
19#include "webrtc/base/constructormagic.h"
20#include "webrtc/base/criticalsection.h"
21#include "webrtc/base/event.h"
22#include "webrtc/base/thread_annotations.h"
philipele0b2f152016-09-28 10:23:49 +020023#include "webrtc/modules/video_coding/frame_object.h"
philipel4f6cd6a2016-08-03 10:59:32 +020024#include "webrtc/modules/video_coding/include/video_coding_defines.h"
25#include "webrtc/modules/video_coding/inter_frame_delay.h"
philipele0b2f152016-09-28 10:23:49 +020026#include "webrtc/modules/video_coding/sequence_number_util.h"
philipelbe7a9e52016-05-19 12:19:35 +020027
28namespace webrtc {
29
30class Clock;
31class VCMJitterEstimator;
32class VCMTiming;
33
34namespace video_coding {
35
philipelbe7a9e52016-05-19 12:19:35 +020036class FrameBuffer {
37 public:
philipel75562822016-09-05 10:57:41 +020038 enum ReturnReason { kFrameFound, kTimeout, kStopped };
39
philipelbe7a9e52016-05-19 12:19:35 +020040 FrameBuffer(Clock* clock,
41 VCMJitterEstimator* jitter_estimator,
philipel4f6cd6a2016-08-03 10:59:32 +020042 VCMTiming* timing);
philipelbe7a9e52016-05-19 12:19:35 +020043
philipele0b2f152016-09-28 10:23:49 +020044 // Insert a frame into the frame buffer. Returns the picture id
45 // of the last continuous frame or -1 if there is no continuous frame.
46 int InsertFrame(std::unique_ptr<FrameObject> frame);
philipelbe7a9e52016-05-19 12:19:35 +020047
48 // Get the next frame for decoding. Will return at latest after
philipel75562822016-09-05 10:57:41 +020049 // |max_wait_time_ms|.
philipele0b2f152016-09-28 10:23:49 +020050 // - If a frame is available within |max_wait_time_ms| it will return
philipel75562822016-09-05 10:57:41 +020051 // kFrameFound and set |frame_out| to the resulting frame.
52 // - If no frame is available after |max_wait_time_ms| it will return
53 // kTimeout.
54 // - If the FrameBuffer is stopped then it will return kStopped.
55 ReturnReason NextFrame(int64_t max_wait_time_ms,
56 std::unique_ptr<FrameObject>* frame_out);
philipelbe7a9e52016-05-19 12:19:35 +020057
philipel4f6cd6a2016-08-03 10:59:32 +020058 // Tells the FrameBuffer which protection mode that is in use. Affects
59 // the frame timing.
60 // TODO(philipel): Remove this when new timing calculations has been
61 // implemented.
62 void SetProtectionMode(VCMVideoProtection mode);
63
philipel504c47d2016-06-30 17:33:02 +020064 // Start the frame buffer, has no effect if the frame buffer is started.
65 // The frame buffer is started upon construction.
66 void Start();
67
68 // Stop the frame buffer, causing any sleeping thread in NextFrame to
69 // return immediately.
70 void Stop();
71
philipelbe7a9e52016-05-19 12:19:35 +020072 private:
philipele0b2f152016-09-28 10:23:49 +020073 struct FrameKey {
74 FrameKey() : picture_id(0), spatial_layer(0) {}
75 FrameKey(uint16_t picture_id, uint8_t spatial_layer)
76 : picture_id(picture_id), spatial_layer(spatial_layer) {}
philipelbe7a9e52016-05-19 12:19:35 +020077
philipele0b2f152016-09-28 10:23:49 +020078 bool operator<(const FrameKey& rhs) const {
79 if (picture_id == rhs.picture_id)
80 return spatial_layer < rhs.spatial_layer;
81 return AheadOf(rhs.picture_id, picture_id);
82 }
83
84 bool operator<=(const FrameKey& rhs) const { return !(rhs < *this); }
85
86 uint16_t picture_id;
87 uint8_t spatial_layer;
philipelbe7a9e52016-05-19 12:19:35 +020088 };
89
philipele0b2f152016-09-28 10:23:49 +020090 struct FrameInfo {
91 // The maximum number of frames that can depend on this frame.
92 static constexpr size_t kMaxNumDependentFrames = 8;
93
94 // Which other frames that have direct unfulfilled dependencies
95 // on this frame.
96 FrameKey dependent_frames[kMaxNumDependentFrames];
97 size_t num_dependent_frames = 0;
98
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
113 // The actual FrameObject.
114 std::unique_ptr<FrameObject> frame;
115 };
116
117 using FrameMap = std::map<FrameKey, FrameInfo>;
118
119 // Update all directly dependent and indirectly dependent frames and mark
120 // them as continuous if all their references has been fulfilled.
121 void PropagateContinuity(FrameMap::iterator start)
philipelbe7a9e52016-05-19 12:19:35 +0200122 EXCLUSIVE_LOCKS_REQUIRED(crit_);
123
philipele0b2f152016-09-28 10:23:49 +0200124 // Marks the frame as decoded and updates all directly dependent frames.
125 void PropagateDecodability(const FrameInfo& info)
126 EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200127
philipele0b2f152016-09-28 10:23:49 +0200128 // Advances |last_decoded_frame_it_| to |decoded| and removes old
129 // frame info.
130 void AdvanceLastDecodedFrame(FrameMap::iterator decoded)
131 EXCLUSIVE_LOCKS_REQUIRED(crit_);
132
133 // Update the corresponding FrameInfo of |frame| and all FrameInfos that
134 // |frame| references.
135 // Return false if |frame| will never be decodable, true otherwise.
136 bool UpdateFrameInfoWithIncomingFrame(const FrameObject& frame,
137 FrameMap::iterator info)
138 EXCLUSIVE_LOCKS_REQUIRED(crit_);
139
140 FrameMap frames_ GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200141
142 rtc::CriticalSection crit_;
143 Clock* const clock_;
philipele0b2f152016-09-28 10:23:49 +0200144 rtc::Event new_countinuous_frame_event_;
philipel4f6cd6a2016-08-03 10:59:32 +0200145 VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_);
146 VCMTiming* const timing_ GUARDED_BY(crit_);
147 VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_);
philipele0b2f152016-09-28 10:23:49 +0200148 FrameMap::iterator last_decoded_frame_it_ GUARDED_BY(crit_);
149 FrameMap::iterator last_continuous_frame_it_ GUARDED_BY(crit_);
150 int num_frames_history_ GUARDED_BY(crit_);
151 int num_frames_buffered_ GUARDED_BY(crit_);
philipel504c47d2016-06-30 17:33:02 +0200152 bool stopped_ GUARDED_BY(crit_);
philipel4f6cd6a2016-08-03 10:59:32 +0200153 VCMVideoProtection protection_mode_ GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200154
155 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
156};
157
158} // namespace video_coding
159} // namespace webrtc
160
161#endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_