blob: f78147c78e5be9d4a5853ebb3c4fe03a1bf5546e [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_PACKET_BUFFER_H_
12#define MODULES_VIDEO_CODING_PACKET_BUFFER_H_
philipelc707ab72016-04-01 02:01:54 -070013
philipel17deeb42016-08-11 15:09:26 +020014#include <memory>
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010015#include <queue>
philipel2c9f9f22017-06-13 02:47:28 -070016#include <set>
17#include <vector>
philipelc707ab72016-04-01 02:01:54 -070018
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020019#include "absl/base/attributes.h"
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010020#include "api/rtp_packet_info.h"
philipelb5e47852019-09-20 11:30:12 +020021#include "api/video/encoded_image.h"
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010022#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h"
23#include "modules/rtp_rtcp/source/rtp_packet_received.h"
24#include "modules/rtp_rtcp/source/rtp_video_header.h"
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020025#include "modules/video_coding/frame_object.h"
Danil Chapovalove3c48842019-12-02 15:54:27 +010026#include "rtc_base/copy_on_write_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/critical_section.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020028#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/thread_annotations.h"
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020030#include "system_wrappers/include/clock.h"
philipelc707ab72016-04-01 02:01:54 -070031
32namespace webrtc {
33namespace video_coding {
34
philipelc707ab72016-04-01 02:01:54 -070035class PacketBuffer {
36 public:
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010037 struct Packet {
38 Packet() = default;
39 Packet(const RtpPacketReceived& rtp_packet,
40 const RTPVideoHeader& video_header,
41 int64_t ntp_time_ms,
42 int64_t receive_time_ms);
43 Packet(const Packet&) = delete;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010044 Packet(Packet&&) = delete;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010045 Packet& operator=(const Packet&) = delete;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010046 Packet& operator=(Packet&&) = delete;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010047 ~Packet() = default;
48
49 VideoCodecType codec() const { return video_header.codec; }
50 int width() const { return video_header.width; }
51 int height() const { return video_header.height; }
52
53 bool is_first_packet_in_frame() const {
54 return video_header.is_first_packet_in_frame;
55 }
56 bool is_last_packet_in_frame() const {
57 return video_header.is_last_packet_in_frame;
58 }
59
60 bool marker_bit = false;
61 uint8_t payload_type = 0;
62 uint16_t seq_num = 0;
63 uint32_t timestamp = 0;
64 // NTP time of the capture time in local timebase in milliseconds.
65 int64_t ntp_time_ms = -1;
66 int times_nacked = -1;
67
Danil Chapovalove3c48842019-12-02 15:54:27 +010068 rtc::CopyOnWriteBuffer video_payload;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010069 RTPVideoHeader video_header;
70 absl::optional<RtpGenericFrameDescriptor> generic_descriptor;
71
72 RtpPacketInfo packet_info;
73 };
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020074 struct InsertResult {
75 std::vector<std::unique_ptr<RtpFrameObject>> frames;
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 Chapovalovf7457e52019-09-20 17:57:15 +020081 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020082 PacketBuffer(Clock* clock, size_t start_buffer_size, size_t max_buffer_size);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020083 ~PacketBuffer();
philipel17deeb42016-08-11 15:09:26 +020084
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010085 InsertResult InsertPacket(std::unique_ptr<Packet> packet)
86 ABSL_MUST_USE_RESULT;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020087 InsertResult InsertPadding(uint16_t seq_num) ABSL_MUST_USE_RESULT;
philipel17deeb42016-08-11 15:09:26 +020088 void ClearTo(uint16_t seq_num);
89 void Clear();
90
philipel3184f8e2017-05-18 08:08:53 -070091 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +020092 absl::optional<int64_t> LastReceivedPacketMs() const;
93 absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
philipel3184f8e2017-05-18 08:08:53 -070094
philipelc707ab72016-04-01 02:01:54 -070095 private:
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020096 struct StoredPacket {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010097 uint16_t seq_num() const { return packet->seq_num; }
philipelf4139332016-04-20 10:26:34 +020098
99 // If this is the first packet of the frame.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100100 bool frame_begin() const { return packet->is_first_packet_in_frame(); }
philipelf4139332016-04-20 10:26:34 +0200101
102 // If this is the last packet of the frame.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100103 bool frame_end() const { return packet->is_last_packet_in_frame(); }
philipelf4139332016-04-20 10:26:34 +0200104
105 // If this slot is currently used.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100106 bool used() const { return packet != nullptr; }
philipelf4139332016-04-20 10:26:34 +0200107
108 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -0700109 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +0200110
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100111 std::unique_ptr<Packet> packet;
philipelc707ab72016-04-01 02:01:54 -0700112 };
113
philipelb4d31082016-07-11 08:46:29 -0700114 Clock* const clock_;
115
philipel02447bc2016-05-13 06:01:03 -0700116 // Tries to expand the buffer.
danilchap56359be2017-09-07 07:53:45 -0700117 bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200118
119 // Test if all previous packets has arrived for the given sequence number.
philipelaee3e0e2016-11-01 11:45:34 +0100120 bool PotentialNewFrame(uint16_t seq_num) const
danilchap56359be2017-09-07 07:53:45 -0700121 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200122
123 // Test if all packets of a frame has arrived, and if so, creates a frame.
philipelfd5a20f2016-11-15 00:57:57 -0800124 // Returns a vector of received frames.
125 std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -0700126 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200127
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100128 std::unique_ptr<RtpFrameObject> AssembleFrame(uint16_t first_seq_num,
129 uint16_t last_seq_num)
130 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200131
philipel02447bc2016-05-13 06:01:03 -0700132 // Get the packet with sequence number |seq_num|.
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100133 const Packet& GetPacket(uint16_t seq_num) const
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100134 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700135
Johannes Krona3705562019-08-26 16:37:11 +0200136 // Clears the packet buffer from |start_seq_num| to |stop_seq_num| where the
137 // endpoints are inclusive.
138 void ClearInterval(uint16_t start_seq_num, uint16_t stop_seq_num)
139 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelc707ab72016-04-01 02:01:54 -0700140
danilchap56359be2017-09-07 07:53:45 -0700141 void UpdateMissingPackets(uint16_t seq_num)
142 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700143
philipelc707ab72016-04-01 02:01:54 -0700144 rtc::CriticalSection crit_;
145
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200146 // buffer_.size() and max_size_ must always be a power of two.
philipelc707ab72016-04-01 02:01:54 -0700147 const size_t max_size_;
148
philipelf4139332016-04-20 10:26:34 +0200149 // The fist sequence number currently in the buffer.
danilchap56359be2017-09-07 07:53:45 -0700150 uint16_t first_seq_num_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200151
philipelf4139332016-04-20 10:26:34 +0200152 // If the packet buffer has received its first packet.
danilchap56359be2017-09-07 07:53:45 -0700153 bool first_packet_received_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200154
philipelaee3e0e2016-11-01 11:45:34 +0100155 // If the buffer is cleared to |first_seq_num_|.
danilchap56359be2017-09-07 07:53:45 -0700156 bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(crit_);
philipelaee3e0e2016-11-01 11:45:34 +0100157
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200158 // Buffer that holds the the inserted packets and information needed to
159 // determine continuity between them.
160 std::vector<StoredPacket> buffer_ RTC_GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700161
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100162 // Timestamp of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200163 absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(crit_);
164 absl::optional<int64_t> last_received_keyframe_packet_ms_
danilchap56359be2017-09-07 07:53:45 -0700165 RTC_GUARDED_BY(crit_);
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100166 absl::optional<uint32_t> last_received_keyframe_rtp_timestamp_
167 RTC_GUARDED_BY(crit_);
philipel3184f8e2017-05-18 08:08:53 -0700168
Danil Chapovalov0040b662018-06-18 10:48:16 +0200169 absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700170 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
danilchap56359be2017-09-07 07:53:45 -0700171 RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700172
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100173 // Indicates if we should require SPS, PPS, and IDR for a particular
174 // RTP timestamp to treat the corresponding frame as a keyframe.
175 const bool sps_pps_idr_is_h264_keyframe_;
philipelc707ab72016-04-01 02:01:54 -0700176};
177
178} // namespace video_coding
179} // namespace webrtc
180
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200181#endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_