blob: 51528a6cbbe33396baa00b69728457e2d58a755f [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"
Johannes Kronf7de74c2021-04-30 13:10:56 +020021#include "api/units/timestamp.h"
philipelb5e47852019-09-20 11:30:12 +020022#include "api/video/encoded_image.h"
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010023#include "modules/rtp_rtcp/source/rtp_packet_received.h"
24#include "modules/rtp_rtcp/source/rtp_video_header.h"
Danil Chapovalove3c48842019-12-02 15:54:27 +010025#include "rtc_base/copy_on_write_buffer.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020026#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/thread_annotations.h"
philipelc707ab72016-04-01 02:01:54 -070028
29namespace webrtc {
30namespace video_coding {
31
philipelc707ab72016-04-01 02:01:54 -070032class PacketBuffer {
33 public:
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010034 struct Packet {
35 Packet() = default;
36 Packet(const RtpPacketReceived& rtp_packet,
philipel9599b3c2021-05-11 11:30:52 +020037 const RTPVideoHeader& video_header);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010038 Packet(const Packet&) = delete;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010039 Packet(Packet&&) = delete;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010040 Packet& operator=(const Packet&) = delete;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010041 Packet& operator=(Packet&&) = delete;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010042 ~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 Chapovalovf4306eb2020-03-20 12:30:31 +010055 // 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 Chapovalovaa3f5da2019-11-14 14:57:33 +010058 bool marker_bit = false;
59 uint8_t payload_type = 0;
60 uint16_t seq_num = 0;
61 uint32_t timestamp = 0;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010062 int times_nacked = -1;
63
Danil Chapovalove3c48842019-12-02 15:54:27 +010064 rtc::CopyOnWriteBuffer video_payload;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010065 RTPVideoHeader video_header;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010066 };
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020067 struct InsertResult {
Danil Chapovalov810b4ca2020-03-19 13:56:11 +010068 std::vector<std::unique_ptr<Packet>> packets;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020069 // 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 Titovdcd7fc72021-08-09 13:02:57 +020074 // Both `start_buffer_size` and `max_buffer_size` must be a power of 2.
philipelce423ce2021-04-12 13:42:03 +020075 PacketBuffer(size_t start_buffer_size, size_t max_buffer_size);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020076 ~PacketBuffer();
philipel17deeb42016-08-11 15:09:26 +020077
philipeldad500a2021-04-14 16:23:34 +020078 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();
philipel17deeb42016-08-11 15:09:26 +020083
Eldar Rello2127aaa2020-08-07 12:08:14 +030084 void ForceSpsPpsIdrIsH264Keyframe();
philipel3184f8e2017-05-18 08:08:53 -070085
philipelc707ab72016-04-01 02:01:54 -070086 private:
philipeldad500a2021-04-14 16:23:34 +020087 void ClearInternal();
Markus Handell3eac1112020-05-27 17:08:41 +020088
philipel02447bc2016-05-13 06:01:03 -070089 // Tries to expand the buffer.
philipeldad500a2021-04-14 16:23:34 +020090 bool ExpandBufferSize();
philipelf4139332016-04-20 10:26:34 +020091
92 // Test if all previous packets has arrived for the given sequence number.
philipeldad500a2021-04-14 16:23:34 +020093 bool PotentialNewFrame(uint16_t seq_num) const;
philipelf4139332016-04-20 10:26:34 +020094
Danil Chapovalov810b4ca2020-03-19 13:56:11 +010095 // Test if all packets of a frame has arrived, and if so, returns packets to
96 // create frames.
philipeldad500a2021-04-14 16:23:34 +020097 std::vector<std::unique_ptr<Packet>> FindFrames(uint16_t seq_num);
philipelc707ab72016-04-01 02:01:54 -070098
philipeldad500a2021-04-14 16:23:34 +020099 void UpdateMissingPackets(uint16_t seq_num);
philipelc707ab72016-04-01 02:01:54 -0700100
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200101 // buffer_.size() and max_size_ must always be a power of two.
philipelc707ab72016-04-01 02:01:54 -0700102 const size_t max_size_;
103
philipelf4139332016-04-20 10:26:34 +0200104 // The fist sequence number currently in the buffer.
philipeldad500a2021-04-14 16:23:34 +0200105 uint16_t first_seq_num_;
philipelf4139332016-04-20 10:26:34 +0200106
philipelf4139332016-04-20 10:26:34 +0200107 // If the packet buffer has received its first packet.
philipeldad500a2021-04-14 16:23:34 +0200108 bool first_packet_received_;
philipelf4139332016-04-20 10:26:34 +0200109
Artem Titovdcd7fc72021-08-09 13:02:57 +0200110 // If the buffer is cleared to `first_seq_num_`.
philipeldad500a2021-04-14 16:23:34 +0200111 bool is_cleared_to_first_seq_num_;
philipelaee3e0e2016-11-01 11:45:34 +0100112
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200113 // Buffer that holds the the inserted packets and information needed to
114 // determine continuity between them.
philipeldad500a2021-04-14 16:23:34 +0200115 std::vector<std::unique_ptr<Packet>> buffer_;
philipelc707ab72016-04-01 02:01:54 -0700116
philipeldad500a2021-04-14 16:23:34 +0200117 absl::optional<uint16_t> newest_inserted_seq_num_;
118 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_;
philipel2c9f9f22017-06-13 02:47:28 -0700119
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100120 // Indicates if we should require SPS, PPS, and IDR for a particular
121 // RTP timestamp to treat the corresponding frame as a keyframe.
Eldar Rello2127aaa2020-08-07 12:08:14 +0300122 bool sps_pps_idr_is_h264_keyframe_;
philipelc707ab72016-04-01 02:01:54 -0700123};
124
125} // namespace video_coding
126} // namespace webrtc
127
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200128#endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_