blob: b5264bcc0805236ce849d7b1770db5093550edfd [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_PACKET_BUFFER_H_
12#define MODULES_VIDEO_CODING_PACKET_BUFFER_H_
philipelc707ab72016-04-01 02:01:54 -070013
philipel17deeb42016-08-11 15:09:26 +020014#include <memory>
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010015#include <queue>
philipel2c9f9f22017-06-13 02:47:28 -070016#include <set>
17#include <vector>
philipelc707ab72016-04-01 02:01:54 -070018
Mirko Bonadeid9708072019-01-25 20:26:48 +010019#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/include/module_common_types.h"
21#include "modules/video_coding/packet.h"
22#include "modules/video_coding/rtp_frame_reference_finder.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/critical_section.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020024#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/thread_annotations.h"
philipelc707ab72016-04-01 02:01:54 -070026
27namespace webrtc {
philipelb4d31082016-07-11 08:46:29 -070028
29class Clock;
30
philipelc707ab72016-04-01 02:01:54 -070031namespace video_coding {
32
philipelc707ab72016-04-01 02:01:54 -070033class RtpFrameObject;
34
Elad Alonb4643ad2019-02-22 11:19:50 +010035// A frame is assembled when all of its packets have been received.
36class OnAssembledFrameCallback {
philipelc707ab72016-04-01 02:01:54 -070037 public:
Elad Alonb4643ad2019-02-22 11:19:50 +010038 virtual ~OnAssembledFrameCallback() {}
39 virtual void OnAssembledFrame(std::unique_ptr<RtpFrameObject> frame) = 0;
philipelc707ab72016-04-01 02:01:54 -070040};
41
42class PacketBuffer {
43 public:
philipel17deeb42016-08-11 15:09:26 +020044 static rtc::scoped_refptr<PacketBuffer> Create(
45 Clock* clock,
46 size_t start_buffer_size,
47 size_t max_buffer_size,
Elad Alonb4643ad2019-02-22 11:19:50 +010048 OnAssembledFrameCallback* frame_callback);
philipel17deeb42016-08-11 15:09:26 +020049
50 virtual ~PacketBuffer();
51
Johannes Kronbd3f3052019-08-01 15:45:54 +020052 // Returns true unless the packet buffer is cleared, which means that a key
53 // frame request should be sent. The PacketBuffer will always take ownership
54 // of the |packet.dataPtr| when this function is called. Made virtual for
55 // testing.
philipel759e0b72016-11-30 01:32:05 -080056 virtual bool InsertPacket(VCMPacket* packet);
philipel17deeb42016-08-11 15:09:26 +020057 void ClearTo(uint16_t seq_num);
58 void Clear();
philipel2c9f9f22017-06-13 02:47:28 -070059 void PaddingReceived(uint16_t seq_num);
philipel17deeb42016-08-11 15:09:26 +020060
philipel3184f8e2017-05-18 08:08:53 -070061 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +020062 absl::optional<int64_t> LastReceivedPacketMs() const;
63 absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
philipel3184f8e2017-05-18 08:08:53 -070064
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010065 // Returns number of different frames seen in the packet buffer
66 int GetUniqueFramesSeen() const;
67
philipel17deeb42016-08-11 15:09:26 +020068 int AddRef() const;
69 int Release() const;
70
71 protected:
philipelc707ab72016-04-01 02:01:54 -070072 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
philipelb4d31082016-07-11 08:46:29 -070073 PacketBuffer(Clock* clock,
74 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070075 size_t max_buffer_size,
Elad Alonb4643ad2019-02-22 11:19:50 +010076 OnAssembledFrameCallback* frame_callback);
philipelc707ab72016-04-01 02:01:54 -070077
78 private:
79 friend RtpFrameObject;
80 // Since we want the packet buffer to be as packet type agnostic
81 // as possible we extract only the information needed in order
philipelf4139332016-04-20 10:26:34 +020082 // to determine whether a sequence of packets is continuous or not.
philipelc707ab72016-04-01 02:01:54 -070083 struct ContinuityInfo {
philipelf4139332016-04-20 10:26:34 +020084 // The sequence number of the packet.
philipelc707ab72016-04-01 02:01:54 -070085 uint16_t seq_num = 0;
philipelf4139332016-04-20 10:26:34 +020086
87 // If this is the first packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070088 bool frame_begin = false;
philipelf4139332016-04-20 10:26:34 +020089
90 // If this is the last packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070091 bool frame_end = false;
philipelf4139332016-04-20 10:26:34 +020092
93 // If this slot is currently used.
philipelc707ab72016-04-01 02:01:54 -070094 bool used = false;
philipelf4139332016-04-20 10:26:34 +020095
96 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -070097 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +020098
99 // If this packet has been used to create a frame already.
100 bool frame_created = false;
philipelc707ab72016-04-01 02:01:54 -0700101 };
102
philipelb4d31082016-07-11 08:46:29 -0700103 Clock* const clock_;
104
philipel02447bc2016-05-13 06:01:03 -0700105 // Tries to expand the buffer.
danilchap56359be2017-09-07 07:53:45 -0700106 bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200107
108 // Test if all previous packets has arrived for the given sequence number.
philipelaee3e0e2016-11-01 11:45:34 +0100109 bool PotentialNewFrame(uint16_t seq_num) const
danilchap56359be2017-09-07 07:53:45 -0700110 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200111
112 // Test if all packets of a frame has arrived, and if so, creates a frame.
philipelfd5a20f2016-11-15 00:57:57 -0800113 // Returns a vector of received frames.
114 std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -0700115 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200116
117 // Copy the bitstream for |frame| to |destination|.
philipel17deeb42016-08-11 15:09:26 +0200118 // Virtual for testing.
119 virtual bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination);
philipelf4139332016-04-20 10:26:34 +0200120
philipel02447bc2016-05-13 06:01:03 -0700121 // Get the packet with sequence number |seq_num|.
philipel17deeb42016-08-11 15:09:26 +0200122 // Virtual for testing.
philipelfd5a20f2016-11-15 00:57:57 -0800123 virtual VCMPacket* GetPacket(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -0700124 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700125
philipelf4139332016-04-20 10:26:34 +0200126 // Mark all slots used by |frame| as not used.
philipel17deeb42016-08-11 15:09:26 +0200127 // Virtual for testing.
128 virtual void ReturnFrame(RtpFrameObject* frame);
philipelc707ab72016-04-01 02:01:54 -0700129
danilchap56359be2017-09-07 07:53:45 -0700130 void UpdateMissingPackets(uint16_t seq_num)
131 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700132
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100133 // Counts unique received timestamps and updates |unique_frames_seen_|.
134 void OnTimestampReceived(uint32_t rtp_timestamp)
135 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
136
philipelc707ab72016-04-01 02:01:54 -0700137 rtc::CriticalSection crit_;
138
139 // Buffer size_ and max_size_ must always be a power of two.
danilchap56359be2017-09-07 07:53:45 -0700140 size_t size_ RTC_GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700141 const size_t max_size_;
142
philipelf4139332016-04-20 10:26:34 +0200143 // The fist sequence number currently in the buffer.
danilchap56359be2017-09-07 07:53:45 -0700144 uint16_t first_seq_num_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200145
philipelf4139332016-04-20 10:26:34 +0200146 // If the packet buffer has received its first packet.
danilchap56359be2017-09-07 07:53:45 -0700147 bool first_packet_received_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200148
philipelaee3e0e2016-11-01 11:45:34 +0100149 // If the buffer is cleared to |first_seq_num_|.
danilchap56359be2017-09-07 07:53:45 -0700150 bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(crit_);
philipelaee3e0e2016-11-01 11:45:34 +0100151
philipelf4139332016-04-20 10:26:34 +0200152 // Buffer that holds the inserted packets.
danilchap56359be2017-09-07 07:53:45 -0700153 std::vector<VCMPacket> data_buffer_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200154
155 // Buffer that holds the information about which slot that is currently in use
156 // and information needed to determine the continuity between packets.
danilchap56359be2017-09-07 07:53:45 -0700157 std::vector<ContinuityInfo> sequence_buffer_ RTC_GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700158
Elad Alonb4643ad2019-02-22 11:19:50 +0100159 // Called when all packets in a frame are received, allowing the frame
160 // to be assembled.
161 OnAssembledFrameCallback* const assembled_frame_callback_;
philipel17deeb42016-08-11 15:09:26 +0200162
philipel3184f8e2017-05-18 08:08:53 -0700163 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200164 absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(crit_);
165 absl::optional<int64_t> last_received_keyframe_packet_ms_
danilchap56359be2017-09-07 07:53:45 -0700166 RTC_GUARDED_BY(crit_);
philipel3184f8e2017-05-18 08:08:53 -0700167
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100168 int unique_frames_seen_ RTC_GUARDED_BY(crit_);
169
Danil Chapovalov0040b662018-06-18 10:48:16 +0200170 absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700171 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
danilchap56359be2017-09-07 07:53:45 -0700172 RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700173
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100174 // Indicates if we should require SPS, PPS, and IDR for a particular
175 // RTP timestamp to treat the corresponding frame as a keyframe.
176 const bool sps_pps_idr_is_h264_keyframe_;
177
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100178 // Stores several last seen unique timestamps for quick search.
179 std::set<uint32_t> rtp_timestamps_history_set_ RTC_GUARDED_BY(crit_);
180 // Stores the same unique timestamps in the order of insertion.
181 std::queue<uint32_t> rtp_timestamps_history_queue_ RTC_GUARDED_BY(crit_);
182
philipel17deeb42016-08-11 15:09:26 +0200183 mutable volatile int ref_count_ = 0;
philipelc707ab72016-04-01 02:01:54 -0700184};
185
186} // namespace video_coding
187} // namespace webrtc
188
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200189#endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_