| 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 |
| 47 | VideoCodecType codec() const { return video_header.codec; } |
| 48 | int width() const { return video_header.width; } |
| 49 | int height() const { return video_header.height; } |
| 50 |
| 51 | bool is_first_packet_in_frame() const { |
| 52 | return video_header.is_first_packet_in_frame; |
| 53 | } |
| 54 | 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 | void ForceSpsPpsIdrIsH264Keyframe(); |
| 98 |
| 99 | private: |
| 100 | Clock* const clock_; |
| 101 |
| 102 | // Clears with |mutex_| taken. |
| 103 | void ClearInternal() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 104 |
| 105 | // Tries to expand the buffer. |
| 106 | bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 107 |
| 108 | // Test if all previous packets has arrived for the given sequence number. |
| 109 | bool PotentialNewFrame(uint16_t seq_num) const |
| 110 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 111 |
| 112 | // Test if all packets of a frame has arrived, and if so, returns packets to |
| 113 | // create frames. |
| 114 | std::vector<std::unique_ptr<Packet>> FindFrames(uint16_t seq_num) |
| 115 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 116 |
| 117 | void UpdateMissingPackets(uint16_t seq_num) |
| 118 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 119 |
| 120 | mutable Mutex mutex_; |
| 121 |
| 122 | // buffer_.size() and max_size_ must always be a power of two. |
| 123 | const size_t max_size_; |
| 124 |
| 125 | // The fist sequence number currently in the buffer. |
| 126 | uint16_t first_seq_num_ RTC_GUARDED_BY(mutex_); |
| 127 |
| 128 | // If the packet buffer has received its first packet. |
| 129 | bool first_packet_received_ RTC_GUARDED_BY(mutex_); |
| 130 |
| 131 | // If the buffer is cleared to |first_seq_num_|. |
| 132 | bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(mutex_); |
| 133 |
| 134 | // Buffer that holds the the inserted packets and information needed to |
| 135 | // determine continuity between them. |
| 136 | std::vector<std::unique_ptr<Packet>> buffer_ RTC_GUARDED_BY(mutex_); |
| 137 |
| 138 | // Timestamp of the last received packet/keyframe packet. |
| 139 | absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(mutex_); |
| 140 | absl::optional<int64_t> last_received_keyframe_packet_ms_ |
| 141 | RTC_GUARDED_BY(mutex_); |
| 142 | absl::optional<uint32_t> last_received_keyframe_rtp_timestamp_ |
| 143 | RTC_GUARDED_BY(mutex_); |
| 144 |
| 145 | absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(mutex_); |
| 146 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_ |
| 147 | RTC_GUARDED_BY(mutex_); |
| 148 |
| 149 | // Indicates if we should require SPS, PPS, and IDR for a particular |
| 150 | // RTP timestamp to treat the corresponding frame as a keyframe. |
| 151 | bool sps_pps_idr_is_h264_keyframe_; |
| 152 | }; |
| 153 |
| 154 | } // namespace video_coding |
| 155 | } // namespace webrtc |
| 156 |
| 157 | #endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |
| 158 |