2016-04-01 02:01:54 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef MODULES_VIDEO_CODING_PACKET_BUFFER_H_
|
|
|
|
|
#define MODULES_VIDEO_CODING_PACKET_BUFFER_H_
|
2016-04-01 02:01:54 -07:00
|
|
|
|
2016-08-11 15:09:26 +02:00
|
|
|
#include <memory>
|
2018-02-21 15:57:09 +01:00
|
|
|
#include <queue>
|
2017-06-13 02:47:28 -07:00
|
|
|
#include <set>
|
|
|
|
|
#include <vector>
|
2016-04-01 02:01:54 -07:00
|
|
|
|
2019-10-22 17:12:42 +02:00
|
|
|
#include "absl/base/attributes.h"
|
2019-11-14 14:57:33 +01:00
|
|
|
#include "api/rtp_packet_info.h"
|
2021-04-30 13:10:56 +02:00
|
|
|
#include "api/units/timestamp.h"
|
2019-09-20 11:30:12 +02:00
|
|
|
#include "api/video/encoded_image.h"
|
2019-11-14 14:57:33 +01:00
|
|
|
#include "modules/rtp_rtcp/source/rtp_packet_received.h"
|
|
|
|
|
#include "modules/rtp_rtcp/source/rtp_video_header.h"
|
2019-12-02 15:54:27 +01:00
|
|
|
#include "rtc_base/copy_on_write_buffer.h"
|
2017-10-25 13:07:09 +02:00
|
|
|
#include "rtc_base/numerics/sequence_number_util.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/thread_annotations.h"
|
2016-04-01 02:01:54 -07:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace video_coding {
|
|
|
|
|
|
|
|
|
|
class PacketBuffer {
|
|
|
|
|
public:
|
2019-11-14 14:57:33 +01:00
|
|
|
struct Packet {
|
|
|
|
|
Packet() = default;
|
|
|
|
|
Packet(const RtpPacketReceived& rtp_packet,
|
2024-07-22 15:25:11 +02:00
|
|
|
int64_t sequence_number,
|
2021-05-11 11:30:52 +02:00
|
|
|
const RTPVideoHeader& video_header);
|
2019-11-14 14:57:33 +01:00
|
|
|
Packet(const Packet&) = delete;
|
2020-01-24 16:04:35 +01:00
|
|
|
Packet(Packet&&) = delete;
|
2019-11-14 14:57:33 +01:00
|
|
|
Packet& operator=(const Packet&) = delete;
|
2020-01-24 16:04:35 +01:00
|
|
|
Packet& operator=(Packet&&) = delete;
|
2019-11-14 14:57:33 +01:00
|
|
|
~Packet() = default;
|
|
|
|
|
|
|
|
|
|
VideoCodecType codec() const { return video_header.codec; }
|
|
|
|
|
int width() const { return video_header.width; }
|
|
|
|
|
int height() const { return video_header.height; }
|
|
|
|
|
|
|
|
|
|
bool is_first_packet_in_frame() const {
|
|
|
|
|
return video_header.is_first_packet_in_frame;
|
|
|
|
|
}
|
|
|
|
|
bool is_last_packet_in_frame() const {
|
|
|
|
|
return video_header.is_last_packet_in_frame;
|
|
|
|
|
}
|
2024-07-22 15:25:11 +02:00
|
|
|
uint16_t seq_num() const { return static_cast<uint16_t>(sequence_number); }
|
2019-11-14 14:57:33 +01:00
|
|
|
|
2020-03-20 12:30:31 +01:00
|
|
|
// If all its previous packets have been inserted into the packet buffer.
|
|
|
|
|
// Set and used internally by the PacketBuffer.
|
|
|
|
|
bool continuous = false;
|
2019-11-14 14:57:33 +01:00
|
|
|
bool marker_bit = false;
|
|
|
|
|
uint8_t payload_type = 0;
|
2024-07-22 15:25:11 +02:00
|
|
|
int64_t sequence_number = 0;
|
2019-11-14 14:57:33 +01:00
|
|
|
uint32_t timestamp = 0;
|
|
|
|
|
int times_nacked = -1;
|
|
|
|
|
|
2019-12-02 15:54:27 +01:00
|
|
|
rtc::CopyOnWriteBuffer video_payload;
|
2019-11-14 14:57:33 +01:00
|
|
|
RTPVideoHeader video_header;
|
|
|
|
|
};
|
2019-10-22 17:12:42 +02:00
|
|
|
struct InsertResult {
|
2020-03-19 13:56:11 +01:00
|
|
|
std::vector<std::unique_ptr<Packet>> packets;
|
2019-10-22 17:12:42 +02:00
|
|
|
// Indicates if the packet buffer was cleared, which means that a key
|
|
|
|
|
// frame request should be sent.
|
|
|
|
|
bool buffer_cleared = false;
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-09 13:02:57 +02:00
|
|
|
// Both `start_buffer_size` and `max_buffer_size` must be a power of 2.
|
2021-04-12 13:42:03 +02:00
|
|
|
PacketBuffer(size_t start_buffer_size, size_t max_buffer_size);
|
2019-10-18 11:17:03 +02:00
|
|
|
~PacketBuffer();
|
2016-08-11 15:09:26 +02:00
|
|
|
|
2021-04-14 16:23:34 +02:00
|
|
|
ABSL_MUST_USE_RESULT InsertResult
|
|
|
|
|
InsertPacket(std::unique_ptr<Packet> packet);
|
|
|
|
|
ABSL_MUST_USE_RESULT InsertResult InsertPadding(uint16_t seq_num);
|
|
|
|
|
void ClearTo(uint16_t seq_num);
|
|
|
|
|
void Clear();
|
2016-08-11 15:09:26 +02:00
|
|
|
|
2020-08-07 12:08:14 +03:00
|
|
|
void ForceSpsPpsIdrIsH264Keyframe();
|
2022-09-29 12:24:02 +02:00
|
|
|
void ResetSpsPpsIdrIsH264Keyframe();
|
2017-05-18 08:08:53 -07:00
|
|
|
|
2016-04-01 02:01:54 -07:00
|
|
|
private:
|
2021-04-14 16:23:34 +02:00
|
|
|
void ClearInternal();
|
2020-05-27 17:08:41 +02:00
|
|
|
|
2016-05-13 06:01:03 -07:00
|
|
|
// Tries to expand the buffer.
|
2021-04-14 16:23:34 +02:00
|
|
|
bool ExpandBufferSize();
|
2016-04-20 10:26:34 +02:00
|
|
|
|
|
|
|
|
// Test if all previous packets has arrived for the given sequence number.
|
2021-04-14 16:23:34 +02:00
|
|
|
bool PotentialNewFrame(uint16_t seq_num) const;
|
2016-04-20 10:26:34 +02:00
|
|
|
|
2020-03-19 13:56:11 +01:00
|
|
|
// Test if all packets of a frame has arrived, and if so, returns packets to
|
|
|
|
|
// create frames.
|
2021-04-14 16:23:34 +02:00
|
|
|
std::vector<std::unique_ptr<Packet>> FindFrames(uint16_t seq_num);
|
2016-04-01 02:01:54 -07:00
|
|
|
|
2021-04-14 16:23:34 +02:00
|
|
|
void UpdateMissingPackets(uint16_t seq_num);
|
2016-04-01 02:01:54 -07:00
|
|
|
|
2019-10-18 11:17:03 +02:00
|
|
|
// buffer_.size() and max_size_ must always be a power of two.
|
2016-04-01 02:01:54 -07:00
|
|
|
const size_t max_size_;
|
|
|
|
|
|
2016-04-20 10:26:34 +02:00
|
|
|
// The fist sequence number currently in the buffer.
|
2021-04-14 16:23:34 +02:00
|
|
|
uint16_t first_seq_num_;
|
2016-04-20 10:26:34 +02:00
|
|
|
|
|
|
|
|
// If the packet buffer has received its first packet.
|
2021-04-14 16:23:34 +02:00
|
|
|
bool first_packet_received_;
|
2016-04-20 10:26:34 +02:00
|
|
|
|
2021-08-09 13:02:57 +02:00
|
|
|
// If the buffer is cleared to `first_seq_num_`.
|
2021-04-14 16:23:34 +02:00
|
|
|
bool is_cleared_to_first_seq_num_;
|
2016-11-01 11:45:34 +01:00
|
|
|
|
2019-10-18 11:17:03 +02:00
|
|
|
// Buffer that holds the the inserted packets and information needed to
|
|
|
|
|
// determine continuity between them.
|
2021-04-14 16:23:34 +02:00
|
|
|
std::vector<std::unique_ptr<Packet>> buffer_;
|
2016-04-01 02:01:54 -07:00
|
|
|
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<uint16_t> newest_inserted_seq_num_;
|
2021-04-14 16:23:34 +02:00
|
|
|
std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_;
|
2017-06-13 02:47:28 -07:00
|
|
|
|
2022-06-22 06:35:38 -07:00
|
|
|
std::set<uint16_t, DescendingSeqNumComp<uint16_t>> received_padding_;
|
|
|
|
|
|
2017-11-02 14:28:06 +01:00
|
|
|
// Indicates if we should require SPS, PPS, and IDR for a particular
|
|
|
|
|
// RTP timestamp to treat the corresponding frame as a keyframe.
|
2020-08-07 12:08:14 +03:00
|
|
|
bool sps_pps_idr_is_h264_keyframe_;
|
2016-04-01 02:01:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace video_coding
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_
|