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