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 MODULES_VIDEO_CODING_PACKET_BUFFER_H_
12 #define MODULES_VIDEO_CODING_PACKET_BUFFER_H_
13 
14 #include <memory>
15 #include <queue>
16 #include <set>
17 #include <vector>
18 
19 #include "absl/base/attributes.h"
20 #include "api/rtp_packet_info.h"
21 #include "api/video/encoded_image.h"
22 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
23 #include "modules/rtp_rtcp/source/rtp_video_header.h"
24 #include "rtc_base/copy_on_write_buffer.h"
25 #include "rtc_base/numerics/sequence_number_util.h"
26 #include "rtc_base/synchronization/mutex.h"
27 #include "rtc_base/thread_annotations.h"
28 #include "system_wrappers/include/clock.h"
29 
30 namespace webrtc {
31 namespace video_coding {
32 
33 class PacketBuffer {
34  public:
35   struct Packet {
36     Packet() = default;
37     Packet(const RtpPacketReceived& rtp_packet,
38            const RTPVideoHeader& video_header,
39            int64_t ntp_time_ms,
40            int64_t receive_time_ms);
41     Packet(const Packet&) = delete;
42     Packet(Packet&&) = delete;
43     Packet& operator=(const Packet&) = delete;
44     Packet& operator=(Packet&&) = delete;
45     ~Packet() = default;
46 
codecPacket47     VideoCodecType codec() const { return video_header.codec; }
widthPacket48     int width() const { return video_header.width; }
heightPacket49     int height() const { return video_header.height; }
50 
is_first_packet_in_framePacket51     bool is_first_packet_in_frame() const {
52       return video_header.is_first_packet_in_frame;
53     }
is_last_packet_in_framePacket54     bool is_last_packet_in_frame() const {
55       return video_header.is_last_packet_in_frame;
56     }
57 
58     // If all its previous packets have been inserted into the packet buffer.
59     // Set and used internally by the PacketBuffer.
60     bool continuous = false;
61     bool marker_bit = false;
62     uint8_t payload_type = 0;
63     uint16_t seq_num = 0;
64     uint32_t timestamp = 0;
65     // NTP time of the capture time in local timebase in milliseconds.
66     int64_t ntp_time_ms = -1;
67     int times_nacked = -1;
68 
69     rtc::CopyOnWriteBuffer video_payload;
70     RTPVideoHeader video_header;
71 
72     RtpPacketInfo packet_info;
73   };
74   struct InsertResult {
75     std::vector<std::unique_ptr<Packet>> packets;
76     // Indicates if the packet buffer was cleared, which means that a key
77     // frame request should be sent.
78     bool buffer_cleared = false;
79   };
80 
81   // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
82   PacketBuffer(Clock* clock, size_t start_buffer_size, size_t max_buffer_size);
83   ~PacketBuffer();
84 
85   InsertResult InsertPacket(std::unique_ptr<Packet> packet) ABSL_MUST_USE_RESULT
86       RTC_LOCKS_EXCLUDED(mutex_);
87   InsertResult InsertPadding(uint16_t seq_num) ABSL_MUST_USE_RESULT
88       RTC_LOCKS_EXCLUDED(mutex_);
89   void ClearTo(uint16_t seq_num) RTC_LOCKS_EXCLUDED(mutex_);
90   void Clear() RTC_LOCKS_EXCLUDED(mutex_);
91 
92   // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
93   absl::optional<int64_t> LastReceivedPacketMs() const
94       RTC_LOCKS_EXCLUDED(mutex_);
95   absl::optional<int64_t> LastReceivedKeyframePacketMs() const
96       RTC_LOCKS_EXCLUDED(mutex_);
97 
98  private:
99   Clock* const clock_;
100 
101   // Clears with |mutex_| taken.
102   void ClearInternal() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
103 
104   // Tries to expand the buffer.
105   bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
106 
107   // Test if all previous packets has arrived for the given sequence number.
108   bool PotentialNewFrame(uint16_t seq_num) const
109       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
110 
111   // Test if all packets of a frame has arrived, and if so, returns packets to
112   // create frames.
113   std::vector<std::unique_ptr<Packet>> FindFrames(uint16_t seq_num)
114       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
115 
116   void UpdateMissingPackets(uint16_t seq_num)
117       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
118 
119   mutable Mutex mutex_;
120 
121   // buffer_.size() and max_size_ must always be a power of two.
122   const size_t max_size_;
123 
124   // The fist sequence number currently in the buffer.
125   uint16_t first_seq_num_ RTC_GUARDED_BY(mutex_);
126 
127   // If the packet buffer has received its first packet.
128   bool first_packet_received_ RTC_GUARDED_BY(mutex_);
129 
130   // If the buffer is cleared to |first_seq_num_|.
131   bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(mutex_);
132 
133   // Buffer that holds the the inserted packets and information needed to
134   // determine continuity between them.
135   std::vector<std::unique_ptr<Packet>> buffer_ RTC_GUARDED_BY(mutex_);
136 
137   // Timestamp of the last received packet/keyframe packet.
138   absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(mutex_);
139   absl::optional<int64_t> last_received_keyframe_packet_ms_
140       RTC_GUARDED_BY(mutex_);
141   absl::optional<uint32_t> last_received_keyframe_rtp_timestamp_
142       RTC_GUARDED_BY(mutex_);
143 
144   absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(mutex_);
145   std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
146       RTC_GUARDED_BY(mutex_);
147 
148   // Indicates if we should require SPS, PPS, and IDR for a particular
149   // RTP timestamp to treat the corresponding frame as a keyframe.
150   const bool sps_pps_idr_is_h264_keyframe_;
151 };
152 
153 }  // namespace video_coding
154 }  // namespace webrtc
155 
156 #endif  // MODULES_VIDEO_CODING_PACKET_BUFFER_H_
157