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" |
philipel | b5e4785 | 2019-09-20 11:30:12 +0200 | [diff] [blame] | 20 | #include "api/video/encoded_image.h" |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 21 | #include "modules/video_coding/frame_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/video_coding/packet.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/critical_section.h" |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 24 | #include "rtc_base/numerics/sequence_number_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/thread_annotations.h" |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 26 | #include "system_wrappers/include/clock.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | namespace video_coding { |
| 30 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 31 | class PacketBuffer { |
| 32 | public: |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 33 | struct InsertResult { |
| 34 | std::vector<std::unique_ptr<RtpFrameObject>> frames; |
| 35 | // Indicates if the packet buffer was cleared, which means that a key |
| 36 | // frame request should be sent. |
| 37 | bool buffer_cleared = false; |
| 38 | }; |
| 39 | |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 40 | // 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] | 41 | 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] | 42 | ~PacketBuffer(); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 43 | |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 44 | // The PacketBuffer will always take ownership of the |packet.dataPtr| when |
| 45 | // this function is called. |
| 46 | InsertResult InsertPacket(VCMPacket* packet) ABSL_MUST_USE_RESULT; |
| 47 | InsertResult InsertPadding(uint16_t seq_num) ABSL_MUST_USE_RESULT; |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 48 | void ClearTo(uint16_t seq_num); |
| 49 | void Clear(); |
| 50 | |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 51 | // Timestamp (not RTP timestamp) of the last received packet/keyframe packet. |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 52 | absl::optional<int64_t> LastReceivedPacketMs() const; |
| 53 | absl::optional<int64_t> LastReceivedKeyframePacketMs() const; |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 54 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 55 | private: |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 56 | struct StoredPacket { |
| 57 | uint16_t seq_num() const { return data.seqNum; } |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 58 | |
| 59 | // If this is the first packet of the frame. |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 60 | bool frame_begin() const { return data.is_first_packet_in_frame(); } |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 61 | |
| 62 | // If this is the last packet of the frame. |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 63 | bool frame_end() const { return data.is_last_packet_in_frame(); } |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 64 | |
| 65 | // If this slot is currently used. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 66 | bool used = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 67 | |
| 68 | // If all its previous packets have been inserted into the packet buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 69 | bool continuous = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 70 | |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 71 | VCMPacket data; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 74 | Clock* const clock_; |
| 75 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 76 | // Tries to expand the buffer. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 77 | bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 78 | |
| 79 | // Test if all previous packets has arrived for the given sequence number. |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 80 | bool PotentialNewFrame(uint16_t seq_num) const |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 81 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 82 | |
| 83 | // Test if all packets of a frame has arrived, and if so, creates a frame. |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 84 | // Returns a vector of received frames. |
| 85 | std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 86 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 87 | |
philipel | b5e4785 | 2019-09-20 11:30:12 +0200 | [diff] [blame] | 88 | rtc::scoped_refptr<EncodedImageBuffer> GetEncodedImageBuffer( |
| 89 | size_t frame_size, |
| 90 | uint16_t first_seq_num, |
| 91 | uint16_t last_seq_num) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 92 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 93 | // Get the packet with sequence number |seq_num|. |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 94 | VCMPacket* GetPacket(uint16_t seq_num) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 95 | |
Johannes Kron | a370556 | 2019-08-26 16:37:11 +0200 | [diff] [blame] | 96 | // Clears the packet buffer from |start_seq_num| to |stop_seq_num| where the |
| 97 | // endpoints are inclusive. |
| 98 | void ClearInterval(uint16_t start_seq_num, uint16_t stop_seq_num) |
| 99 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 100 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 101 | void UpdateMissingPackets(uint16_t seq_num) |
| 102 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 103 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 104 | rtc::CriticalSection crit_; |
| 105 | |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 106 | // buffer_.size() and max_size_ must always be a power of two. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 107 | const size_t max_size_; |
| 108 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 109 | // The fist sequence number currently in the buffer. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 110 | uint16_t first_seq_num_ RTC_GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 111 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 112 | // If the packet buffer has received its first packet. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 113 | bool first_packet_received_ RTC_GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 114 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 115 | // If the buffer is cleared to |first_seq_num_|. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 116 | bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(crit_); |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 117 | |
Danil Chapovalov | 4aae11d | 2019-10-18 11:17:03 +0200 | [diff] [blame] | 118 | // Buffer that holds the the inserted packets and information needed to |
| 119 | // determine continuity between them. |
| 120 | std::vector<StoredPacket> buffer_ RTC_GUARDED_BY(crit_); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 121 | |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 122 | // Timestamp (not RTP timestamp) of the last received packet/keyframe packet. |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 123 | absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(crit_); |
| 124 | absl::optional<int64_t> last_received_keyframe_packet_ms_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 125 | RTC_GUARDED_BY(crit_); |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 126 | |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 127 | absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(crit_); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 128 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 129 | RTC_GUARDED_BY(crit_); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 130 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 131 | // Indicates if we should require SPS, PPS, and IDR for a particular |
| 132 | // RTP timestamp to treat the corresponding frame as a keyframe. |
| 133 | const bool sps_pps_idr_is_h264_keyframe_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace video_coding |
| 137 | } // namespace webrtc |
| 138 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 139 | #endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |