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> |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 15 | #include <queue> |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 16 | #include <set> |
| 17 | #include <vector> |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 18 | |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 19 | #include "absl/base/attributes.h" |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 20 | #include "api/rtp_packet_info.h" |
Johannes Kron | f7de74c | 2021-04-30 13:10:56 +0200 | [diff] [blame] | 21 | #include "api/units/timestamp.h" |
philipel | b5e4785 | 2019-09-20 11:30:12 +0200 | [diff] [blame] | 22 | #include "api/video/encoded_image.h" |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 23 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
| 24 | #include "modules/rtp_rtcp/source/rtp_video_header.h" |
Danil Chapovalov | e3c4884 | 2019-12-02 15:54:27 +0100 | [diff] [blame] | 25 | #include "rtc_base/copy_on_write_buffer.h" |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 26 | #include "rtc_base/numerics/sequence_number_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/thread_annotations.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | namespace video_coding { |
| 31 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 32 | class PacketBuffer { |
| 33 | public: |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 34 | struct Packet { |
| 35 | Packet() = default; |
| 36 | Packet(const RtpPacketReceived& rtp_packet, |
philipel | 9599b3c | 2021-05-11 11:30:52 +0200 | [diff] [blame] | 37 | const RTPVideoHeader& video_header); |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 38 | Packet(const Packet&) = delete; |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 39 | Packet(Packet&&) = delete; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 40 | Packet& operator=(const Packet&) = delete; |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 41 | Packet& operator=(Packet&&) = delete; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 42 | ~Packet() = default; |
| 43 | |
| 44 | VideoCodecType codec() const { return video_header.codec; } |
| 45 | int width() const { return video_header.width; } |
| 46 | int height() const { return video_header.height; } |
| 47 | |
| 48 | bool is_first_packet_in_frame() const { |
| 49 | return video_header.is_first_packet_in_frame; |
| 50 | } |
| 51 | bool is_last_packet_in_frame() const { |
| 52 | return video_header.is_last_packet_in_frame; |
| 53 | } |
| 54 | |
Danil Chapovalov | f4306eb | 2020-03-20 12:30:31 +0100 | [diff] [blame] | 55 | // If all its previous packets have been inserted into the packet buffer. |
| 56 | // Set and used internally by the PacketBuffer. |
| 57 | bool continuous = false; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 58 | bool marker_bit = false; |
| 59 | uint8_t payload_type = 0; |
| 60 | uint16_t seq_num = 0; |
| 61 | uint32_t timestamp = 0; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 62 | int times_nacked = -1; |
| 63 | |
Danil Chapovalov | e3c4884 | 2019-12-02 15:54:27 +0100 | [diff] [blame] | 64 | rtc::CopyOnWriteBuffer video_payload; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 65 | RTPVideoHeader video_header; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 66 | }; |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 67 | struct InsertResult { |
Danil Chapovalov | 810b4ca | 2020-03-19 13:56:11 +0100 | [diff] [blame] | 68 | std::vector<std::unique_ptr<Packet>> packets; |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 69 | // Indicates if the packet buffer was cleared, which means that a key |
| 70 | // frame request should be sent. |
| 71 | bool buffer_cleared = false; |
| 72 | }; |
| 73 | |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 74 | // Both `start_buffer_size` and `max_buffer_size` must be a power of 2. |
philipel | ce423ce | 2021-04-12 13:42:03 +0200 | [diff] [blame] | 75 | PacketBuffer(size_t start_buffer_size, size_t max_buffer_size); |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 76 | ~PacketBuffer(); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 77 | |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 78 | ABSL_MUST_USE_RESULT InsertResult |
| 79 | InsertPacket(std::unique_ptr<Packet> packet); |
| 80 | ABSL_MUST_USE_RESULT InsertResult InsertPadding(uint16_t seq_num); |
| 81 | void ClearTo(uint16_t seq_num); |
| 82 | void Clear(); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 83 | |
Eldar Rello | 2127aaa | 2020-08-07 12:08:14 +0300 | [diff] [blame] | 84 | void ForceSpsPpsIdrIsH264Keyframe(); |
Tommi | 96c1a9b | 2022-09-29 12:24:02 +0200 | [diff] [blame^] | 85 | void ResetSpsPpsIdrIsH264Keyframe(); |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 86 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 87 | private: |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 88 | void ClearInternal(); |
Markus Handell | 3eac111 | 2020-05-27 17:08:41 +0200 | [diff] [blame] | 89 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 90 | // Tries to expand the buffer. |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 91 | bool ExpandBufferSize(); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 92 | |
| 93 | // Test if all previous packets has arrived for the given sequence number. |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 94 | bool PotentialNewFrame(uint16_t seq_num) const; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 95 | |
Danil Chapovalov | 810b4ca | 2020-03-19 13:56:11 +0100 | [diff] [blame] | 96 | // Test if all packets of a frame has arrived, and if so, returns packets to |
| 97 | // create frames. |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 98 | std::vector<std::unique_ptr<Packet>> FindFrames(uint16_t seq_num); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 99 | |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 100 | void UpdateMissingPackets(uint16_t seq_num); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 101 | |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 102 | // buffer_.size() and max_size_ must always be a power of two. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 103 | const size_t max_size_; |
| 104 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 105 | // The fist sequence number currently in the buffer. |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 106 | uint16_t first_seq_num_; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 107 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 108 | // If the packet buffer has received its first packet. |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 109 | bool first_packet_received_; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 110 | |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 111 | // If the buffer is cleared to `first_seq_num_`. |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 112 | bool is_cleared_to_first_seq_num_; |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 113 | |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 114 | // Buffer that holds the the inserted packets and information needed to |
| 115 | // determine continuity between them. |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 116 | std::vector<std::unique_ptr<Packet>> buffer_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 117 | |
philipel | dad500a | 2021-04-14 16:23:34 +0200 | [diff] [blame] | 118 | absl::optional<uint16_t> newest_inserted_seq_num_; |
| 119 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_; |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 120 | |
Jared Siskin | 3f659b1 | 2022-06-22 06:35:38 -0700 | [diff] [blame] | 121 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> received_padding_; |
| 122 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 123 | // Indicates if we should require SPS, PPS, and IDR for a particular |
| 124 | // RTP timestamp to treat the corresponding frame as a keyframe. |
Eldar Rello | 2127aaa | 2020-08-07 12:08:14 +0300 | [diff] [blame] | 125 | bool sps_pps_idr_is_h264_keyframe_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | } // namespace video_coding |
| 129 | } // namespace webrtc |
| 130 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 131 | #endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |