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" |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 25 | #include "rtc_base/numerics/sequence_number_util.h" |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 26 | #include "rtc_base/synchronization/mutex.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 | |
Danil Chapovalov | f4306eb | 2020-03-20 12:30:31 +0100 | [diff] [blame] | 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; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 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 | |
Danil Chapovalov | e3c4884 | 2019-12-02 15:54:27 +0100 | [diff] [blame] | 69 | rtc::CopyOnWriteBuffer video_payload; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 70 | RTPVideoHeader video_header; |
Danil Chapovalov | aa3f5da | 2019-11-14 14:57:33 +0100 | [diff] [blame] | 71 | |
| 72 | RtpPacketInfo packet_info; |
| 73 | }; |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 74 | struct InsertResult { |
Danil Chapovalov | 810b4ca | 2020-03-19 13:56:11 +0100 | [diff] [blame] | 75 | std::vector<std::unique_ptr<Packet>> packets; |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 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 | |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 81 | // 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] | 82 | 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] | 83 | ~PacketBuffer(); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 84 | |
Mirko Bonadei | 08b6364 | 2020-08-31 09:06:35 +0200 | [diff] [blame^] | 85 | ABSL_MUST_USE_RESULT InsertResult InsertPacket(std::unique_ptr<Packet> packet) |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 86 | RTC_LOCKS_EXCLUDED(mutex_); |
Mirko Bonadei | 08b6364 | 2020-08-31 09:06:35 +0200 | [diff] [blame^] | 87 | ABSL_MUST_USE_RESULT InsertResult InsertPadding(uint16_t seq_num) |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 88 | RTC_LOCKS_EXCLUDED(mutex_); |
| 89 | void ClearTo(uint16_t seq_num) RTC_LOCKS_EXCLUDED(mutex_); |
| 90 | void Clear() RTC_LOCKS_EXCLUDED(mutex_); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 91 | |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 92 | // Timestamp (not RTP timestamp) of the last received packet/keyframe packet. |
Markus Handell | 3eac111 | 2020-05-27 17:08:41 +0200 | [diff] [blame] | 93 | absl::optional<int64_t> LastReceivedPacketMs() const |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 94 | RTC_LOCKS_EXCLUDED(mutex_); |
Markus Handell | 3eac111 | 2020-05-27 17:08:41 +0200 | [diff] [blame] | 95 | absl::optional<int64_t> LastReceivedKeyframePacketMs() const |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 96 | RTC_LOCKS_EXCLUDED(mutex_); |
Eldar Rello | 2127aaa | 2020-08-07 12:08:14 +0300 | [diff] [blame] | 97 | void ForceSpsPpsIdrIsH264Keyframe(); |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 98 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 99 | private: |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 100 | Clock* const clock_; |
| 101 | |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 102 | // Clears with |mutex_| taken. |
| 103 | void ClearInternal() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
Markus Handell | 3eac111 | 2020-05-27 17:08:41 +0200 | [diff] [blame] | 104 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 105 | // Tries to expand the buffer. |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 106 | bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 107 | |
| 108 | // Test if all previous packets has arrived for the given sequence number. |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 109 | bool PotentialNewFrame(uint16_t seq_num) const |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 110 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 111 | |
Danil Chapovalov | 810b4ca | 2020-03-19 13:56:11 +0100 | [diff] [blame] | 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) |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 115 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 116 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 117 | void UpdateMissingPackets(uint16_t seq_num) |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 118 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 119 | |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 120 | mutable Mutex mutex_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 121 | |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 122 | // buffer_.size() and max_size_ must always be a power of two. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 123 | const size_t max_size_; |
| 124 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 125 | // The fist sequence number currently in the buffer. |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 126 | uint16_t first_seq_num_ RTC_GUARDED_BY(mutex_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 127 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 128 | // If the packet buffer has received its first packet. |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 129 | bool first_packet_received_ RTC_GUARDED_BY(mutex_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 130 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 131 | // If the buffer is cleared to |first_seq_num_|. |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 132 | bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(mutex_); |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 133 | |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 134 | // Buffer that holds the the inserted packets and information needed to |
| 135 | // determine continuity between them. |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 136 | std::vector<std::unique_ptr<Packet>> buffer_ RTC_GUARDED_BY(mutex_); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 137 | |
Danil Chapovalov | c9e532a | 2019-12-10 17:03:00 +0100 | [diff] [blame] | 138 | // Timestamp of the last received packet/keyframe packet. |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 139 | absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(mutex_); |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 140 | absl::optional<int64_t> last_received_keyframe_packet_ms_ |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 141 | RTC_GUARDED_BY(mutex_); |
Danil Chapovalov | c9e532a | 2019-12-10 17:03:00 +0100 | [diff] [blame] | 142 | absl::optional<uint32_t> last_received_keyframe_rtp_timestamp_ |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 143 | RTC_GUARDED_BY(mutex_); |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 144 | |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 145 | absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(mutex_); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 146 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_ |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 147 | RTC_GUARDED_BY(mutex_); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 148 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 149 | // Indicates if we should require SPS, PPS, and IDR for a particular |
| 150 | // RTP timestamp to treat the corresponding frame as a keyframe. |
Eldar Rello | 2127aaa | 2020-08-07 12:08:14 +0300 | [diff] [blame] | 151 | bool sps_pps_idr_is_h264_keyframe_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | } // namespace video_coding |
| 155 | } // namespace webrtc |
| 156 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 157 | #endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |