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" |
philipel | b5e4785 | 2019-09-20 11:30:12 +0200 | [diff] [blame] | 21 | #include "api/video/encoded_image.h" |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 22 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
| 23 | #include "modules/rtp_rtcp/source/rtp_video_header.h" |
Danil Chapovalov | e3c4884 | 2019-12-02 15:54:27 +0100 | [diff] [blame] | 24 | #include "rtc_base/copy_on_write_buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/critical_section.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" |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 28 | #include "system_wrappers/include/clock.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | namespace video_coding { |
| 32 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 33 | class PacketBuffer { |
| 34 | public: |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 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; |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 42 | Packet(Packet&&) = delete; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 43 | Packet& operator=(const Packet&) = delete; |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 44 | Packet& operator=(Packet&&) = delete; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 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 | bool marker_bit = false; |
| 59 | uint8_t payload_type = 0; |
| 60 | uint16_t seq_num = 0; |
| 61 | uint32_t timestamp = 0; |
| 62 | // NTP time of the capture time in local timebase in milliseconds. |
| 63 | int64_t ntp_time_ms = -1; |
| 64 | int times_nacked = -1; |
| 65 | |
Danil Chapovalov | e3c4884 | 2019-12-02 15:54:27 +0100 | [diff] [blame] | 66 | rtc::CopyOnWriteBuffer video_payload; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 67 | RTPVideoHeader video_header; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 68 | |
| 69 | RtpPacketInfo packet_info; |
| 70 | }; |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 71 | struct InsertResult { |
Danil Chapovalov | 810b4ca | 2020-03-19 13:56:11 +0100 | [diff] [blame^] | 72 | std::vector<std::unique_ptr<Packet>> packets; |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 73 | // Indicates if the packet buffer was cleared, which means that a key |
| 74 | // frame request should be sent. |
| 75 | bool buffer_cleared = false; |
| 76 | }; |
| 77 | |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 78 | // Both |start_buffer_size| and |max_buffer_size| must be a power of 2. |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 79 | PacketBuffer(Clock* clock, size_t start_buffer_size, size_t max_buffer_size); |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 80 | ~PacketBuffer(); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 81 | |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 82 | InsertResult InsertPacket(std::unique_ptr<Packet> packet) |
| 83 | ABSL_MUST_USE_RESULT; |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 84 | InsertResult InsertPadding(uint16_t seq_num) ABSL_MUST_USE_RESULT; |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 85 | void ClearTo(uint16_t seq_num); |
| 86 | void Clear(); |
| 87 | |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 88 | // Timestamp (not RTP timestamp) of the last received packet/keyframe packet. |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 89 | absl::optional<int64_t> LastReceivedPacketMs() const; |
| 90 | absl::optional<int64_t> LastReceivedKeyframePacketMs() const; |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 91 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 92 | private: |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 93 | struct StoredPacket { |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 94 | uint16_t seq_num() const { return packet->seq_num; } |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 95 | |
| 96 | // If this is the first packet of the frame. |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 97 | bool frame_begin() const { return packet->is_first_packet_in_frame(); } |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 98 | |
| 99 | // If this is the last packet of the frame. |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 100 | bool frame_end() const { return packet->is_last_packet_in_frame(); } |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 101 | |
| 102 | // If this slot is currently used. |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 103 | bool used() const { return packet != nullptr; } |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 104 | |
| 105 | // If all its previous packets have been inserted into the packet buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 106 | bool continuous = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 107 | |
Danil Chapovalov | 97ffbef | 2020-01-24 16:04:35 +0100 | [diff] [blame] | 108 | std::unique_ptr<Packet> packet; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 111 | Clock* const clock_; |
| 112 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 113 | // Tries to expand the buffer. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 114 | bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 115 | |
| 116 | // Test if all previous packets has arrived for the given sequence number. |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 117 | bool PotentialNewFrame(uint16_t seq_num) const |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 118 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 119 | |
Danil Chapovalov | 810b4ca | 2020-03-19 13:56:11 +0100 | [diff] [blame^] | 120 | // Test if all packets of a frame has arrived, and if so, returns packets to |
| 121 | // create frames. |
| 122 | std::vector<std::unique_ptr<Packet>> FindFrames(uint16_t seq_num) |
Johannes Kron | a370556 | 2019-08-26 16:37:11 +0200 | [diff] [blame] | 123 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 124 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 125 | void UpdateMissingPackets(uint16_t seq_num) |
| 126 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 127 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 128 | rtc::CriticalSection crit_; |
| 129 | |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 130 | // buffer_.size() and max_size_ must always be a power of two. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 131 | const size_t max_size_; |
| 132 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 133 | // The fist sequence number currently in the buffer. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 134 | uint16_t first_seq_num_ RTC_GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 135 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 136 | // If the packet buffer has received its first packet. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 137 | bool first_packet_received_ RTC_GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 138 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 139 | // If the buffer is cleared to |first_seq_num_|. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 140 | bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(crit_); |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 141 | |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 142 | // Buffer that holds the the inserted packets and information needed to |
| 143 | // determine continuity between them. |
| 144 | std::vector<StoredPacket> buffer_ RTC_GUARDED_BY(crit_); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 145 | |
Danil Chapovalov | c9e532a | 2019-12-10 17:03:00 +0100 | [diff] [blame] | 146 | // Timestamp of the last received packet/keyframe packet. |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 147 | absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(crit_); |
| 148 | absl::optional<int64_t> last_received_keyframe_packet_ms_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 149 | RTC_GUARDED_BY(crit_); |
Danil Chapovalov | c9e532a | 2019-12-10 17:03:00 +0100 | [diff] [blame] | 150 | absl::optional<uint32_t> last_received_keyframe_rtp_timestamp_ |
| 151 | RTC_GUARDED_BY(crit_); |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 152 | |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 153 | absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(crit_); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 154 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 155 | RTC_GUARDED_BY(crit_); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 156 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 157 | // Indicates if we should require SPS, PPS, and IDR for a particular |
| 158 | // RTP timestamp to treat the corresponding frame as a keyframe. |
| 159 | const bool sps_pps_idr_is_h264_keyframe_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // namespace video_coding |
| 163 | } // namespace webrtc |
| 164 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 165 | #endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |