Line data Source code
1 : /*
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_PACKET_BUFFER_H_
12 : #define WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_
13 :
14 : #include <vector>
15 : #include <memory>
16 :
17 : #include "webrtc/base/criticalsection.h"
18 : #include "webrtc/base/scoped_ref_ptr.h"
19 : #include "webrtc/base/thread_annotations.h"
20 : #include "webrtc/modules/include/module_common_types.h"
21 : #include "webrtc/modules/video_coding/packet.h"
22 : #include "webrtc/modules/video_coding/rtp_frame_reference_finder.h"
23 : #include "webrtc/modules/video_coding/sequence_number_util.h"
24 :
25 : namespace webrtc {
26 :
27 : class Clock;
28 :
29 : namespace video_coding {
30 :
31 : class FrameObject;
32 : class RtpFrameObject;
33 :
34 : // A received frame is a frame which has received all its packets.
35 0 : class OnReceivedFrameCallback {
36 : public:
37 0 : virtual ~OnReceivedFrameCallback() {}
38 : virtual void OnReceivedFrame(std::unique_ptr<RtpFrameObject> frame) = 0;
39 : };
40 :
41 : class PacketBuffer {
42 : public:
43 : static rtc::scoped_refptr<PacketBuffer> Create(
44 : Clock* clock,
45 : size_t start_buffer_size,
46 : size_t max_buffer_size,
47 : OnReceivedFrameCallback* frame_callback);
48 :
49 : virtual ~PacketBuffer();
50 :
51 : // Returns true if |packet| is inserted into the packet buffer, false
52 : // otherwise. The PacketBuffer will always take ownership of the
53 : // |packet.dataPtr| when this function is called. Made virtual for testing.
54 : virtual bool InsertPacket(VCMPacket* packet);
55 : void ClearTo(uint16_t seq_num);
56 : void Clear();
57 :
58 : int AddRef() const;
59 : int Release() const;
60 :
61 : protected:
62 : // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
63 : PacketBuffer(Clock* clock,
64 : size_t start_buffer_size,
65 : size_t max_buffer_size,
66 : OnReceivedFrameCallback* frame_callback);
67 :
68 : private:
69 : friend RtpFrameObject;
70 : // Since we want the packet buffer to be as packet type agnostic
71 : // as possible we extract only the information needed in order
72 : // to determine whether a sequence of packets is continuous or not.
73 0 : struct ContinuityInfo {
74 : // The sequence number of the packet.
75 : uint16_t seq_num = 0;
76 :
77 : // If this is the first packet of the frame.
78 : bool frame_begin = false;
79 :
80 : // If this is the last packet of the frame.
81 : bool frame_end = false;
82 :
83 : // If this slot is currently used.
84 : bool used = false;
85 :
86 : // If all its previous packets have been inserted into the packet buffer.
87 : bool continuous = false;
88 :
89 : // If this packet has been used to create a frame already.
90 : bool frame_created = false;
91 : };
92 :
93 : Clock* const clock_;
94 :
95 : // Tries to expand the buffer.
96 : bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_);
97 :
98 : // Test if all previous packets has arrived for the given sequence number.
99 : bool PotentialNewFrame(uint16_t seq_num) const
100 : EXCLUSIVE_LOCKS_REQUIRED(crit_);
101 :
102 : // Test if all packets of a frame has arrived, and if so, creates a frame.
103 : // Returns a vector of received frames.
104 : std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
105 : EXCLUSIVE_LOCKS_REQUIRED(crit_);
106 :
107 : // Copy the bitstream for |frame| to |destination|.
108 : // Virtual for testing.
109 : virtual bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination);
110 :
111 : // Get the packet with sequence number |seq_num|.
112 : // Virtual for testing.
113 : virtual VCMPacket* GetPacket(uint16_t seq_num)
114 : EXCLUSIVE_LOCKS_REQUIRED(crit_);
115 :
116 : // Mark all slots used by |frame| as not used.
117 : // Virtual for testing.
118 : virtual void ReturnFrame(RtpFrameObject* frame);
119 :
120 : rtc::CriticalSection crit_;
121 :
122 : // Buffer size_ and max_size_ must always be a power of two.
123 : size_t size_ GUARDED_BY(crit_);
124 : const size_t max_size_;
125 :
126 : // The fist sequence number currently in the buffer.
127 : uint16_t first_seq_num_ GUARDED_BY(crit_);
128 :
129 : // If the packet buffer has received its first packet.
130 : bool first_packet_received_ GUARDED_BY(crit_);
131 :
132 : // If the buffer is cleared to |first_seq_num_|.
133 : bool is_cleared_to_first_seq_num_ GUARDED_BY(crit_);
134 :
135 : // Buffer that holds the inserted packets.
136 : std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_);
137 :
138 : // Buffer that holds the information about which slot that is currently in use
139 : // and information needed to determine the continuity between packets.
140 : std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_);
141 :
142 : // Called when a received frame is found.
143 : OnReceivedFrameCallback* const received_frame_callback_;
144 :
145 : mutable volatile int ref_count_ = 0;
146 : };
147 :
148 : } // namespace video_coding
149 : } // namespace webrtc
150 :
151 : #endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_
|