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