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-09-20 11:30:12 +02:00
|
|
|
#include "api/video/encoded_image.h"
|
2019-10-22 17:12:42 +02:00
|
|
|
#include "modules/video_coding/frame_object.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/video_coding/packet.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/critical_section.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"
|
2019-10-22 17:12:42 +02:00
|
|
|
#include "system_wrappers/include/clock.h"
|
2016-04-01 02:01:54 -07:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace video_coding {
|
|
|
|
|
|
|
|
|
|
class PacketBuffer {
|
|
|
|
|
public:
|
2019-10-22 17:12:42 +02:00
|
|
|
struct InsertResult {
|
|
|
|
|
std::vector<std::unique_ptr<RtpFrameObject>> frames;
|
|
|
|
|
// Indicates if the packet buffer was cleared, which means that a key
|
|
|
|
|
// frame request should be sent.
|
|
|
|
|
bool buffer_cleared = false;
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-20 17:57:15 +02:00
|
|
|
// Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
|
2019-10-22 17:12:42 +02:00
|
|
|
PacketBuffer(Clock* clock, 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
|
|
|
|
2019-10-22 17:12:42 +02:00
|
|
|
// The PacketBuffer will always take ownership of the |packet.dataPtr| when
|
|
|
|
|
// this function is called.
|
|
|
|
|
InsertResult InsertPacket(VCMPacket* packet) ABSL_MUST_USE_RESULT;
|
|
|
|
|
InsertResult InsertPadding(uint16_t seq_num) ABSL_MUST_USE_RESULT;
|
2016-08-11 15:09:26 +02:00
|
|
|
void ClearTo(uint16_t seq_num);
|
|
|
|
|
void Clear();
|
|
|
|
|
|
2017-05-18 08:08:53 -07:00
|
|
|
// Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
|
2018-06-18 10:48:16 +02:00
|
|
|
absl::optional<int64_t> LastReceivedPacketMs() const;
|
|
|
|
|
absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
|
2017-05-18 08:08:53 -07:00
|
|
|
|
2016-04-01 02:01:54 -07:00
|
|
|
private:
|
2019-10-18 11:17:03 +02:00
|
|
|
struct StoredPacket {
|
|
|
|
|
uint16_t seq_num() const { return data.seqNum; }
|
2016-04-20 10:26:34 +02:00
|
|
|
|
|
|
|
|
// If this is the first packet of the frame.
|
2019-10-18 11:17:03 +02:00
|
|
|
bool frame_begin() const { return data.is_first_packet_in_frame(); }
|
2016-04-20 10:26:34 +02:00
|
|
|
|
|
|
|
|
// If this is the last packet of the frame.
|
2019-10-18 11:17:03 +02:00
|
|
|
bool frame_end() const { return data.is_last_packet_in_frame(); }
|
2016-04-20 10:26:34 +02:00
|
|
|
|
|
|
|
|
// If this slot is currently used.
|
2016-04-01 02:01:54 -07:00
|
|
|
bool used = false;
|
2016-04-20 10:26:34 +02:00
|
|
|
|
|
|
|
|
// If all its previous packets have been inserted into the packet buffer.
|
2016-04-01 02:01:54 -07:00
|
|
|
bool continuous = false;
|
2016-04-20 10:26:34 +02:00
|
|
|
|
2019-10-18 11:17:03 +02:00
|
|
|
VCMPacket data;
|
2016-04-01 02:01:54 -07:00
|
|
|
};
|
|
|
|
|
|
2016-07-11 08:46:29 -07:00
|
|
|
Clock* const clock_;
|
|
|
|
|
|
2016-05-13 06:01:03 -07:00
|
|
|
// Tries to expand the buffer.
|
2017-09-07 07:53:45 -07:00
|
|
|
bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2016-04-20 10:26:34 +02:00
|
|
|
|
|
|
|
|
// Test if all previous packets has arrived for the given sequence number.
|
2016-11-01 11:45:34 +01:00
|
|
|
bool PotentialNewFrame(uint16_t seq_num) const
|
2017-09-07 07:53:45 -07:00
|
|
|
RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2016-04-20 10:26:34 +02:00
|
|
|
|
|
|
|
|
// Test if all packets of a frame has arrived, and if so, creates a frame.
|
2016-11-15 00:57:57 -08:00
|
|
|
// Returns a vector of received frames.
|
|
|
|
|
std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
|
2017-09-07 07:53:45 -07:00
|
|
|
RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2016-04-20 10:26:34 +02:00
|
|
|
|
2019-11-08 17:30:29 +01:00
|
|
|
std::unique_ptr<RtpFrameObject> AssembleFrame(uint16_t first_seq_num,
|
|
|
|
|
uint16_t last_seq_num)
|
|
|
|
|
RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2016-04-20 10:26:34 +02:00
|
|
|
|
2016-05-13 06:01:03 -07:00
|
|
|
// Get the packet with sequence number |seq_num|.
|
2019-11-08 17:30:29 +01:00
|
|
|
const VCMPacket& GetPacket(uint16_t seq_num) const
|
|
|
|
|
RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2016-05-13 06:01:03 -07:00
|
|
|
|
2019-08-26 16:37:11 +02:00
|
|
|
// Clears the packet buffer from |start_seq_num| to |stop_seq_num| where the
|
|
|
|
|
// endpoints are inclusive.
|
|
|
|
|
void ClearInterval(uint16_t start_seq_num, uint16_t stop_seq_num)
|
|
|
|
|
RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2016-04-01 02:01:54 -07:00
|
|
|
|
2017-09-07 07:53:45 -07:00
|
|
|
void UpdateMissingPackets(uint16_t seq_num)
|
|
|
|
|
RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2017-06-13 02:47:28 -07:00
|
|
|
|
2016-04-01 02:01:54 -07:00
|
|
|
rtc::CriticalSection crit_;
|
|
|
|
|
|
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.
|
2017-09-07 07:53:45 -07:00
|
|
|
uint16_t first_seq_num_ RTC_GUARDED_BY(crit_);
|
2016-04-20 10:26:34 +02:00
|
|
|
|
|
|
|
|
// If the packet buffer has received its first packet.
|
2017-09-07 07:53:45 -07:00
|
|
|
bool first_packet_received_ RTC_GUARDED_BY(crit_);
|
2016-04-20 10:26:34 +02:00
|
|
|
|
2016-11-01 11:45:34 +01:00
|
|
|
// If the buffer is cleared to |first_seq_num_|.
|
2017-09-07 07:53:45 -07:00
|
|
|
bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(crit_);
|
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.
|
|
|
|
|
std::vector<StoredPacket> buffer_ RTC_GUARDED_BY(crit_);
|
2016-04-01 02:01:54 -07:00
|
|
|
|
2017-05-18 08:08:53 -07:00
|
|
|
// Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
|
2018-06-18 10:48:16 +02:00
|
|
|
absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(crit_);
|
|
|
|
|
absl::optional<int64_t> last_received_keyframe_packet_ms_
|
2017-09-07 07:53:45 -07:00
|
|
|
RTC_GUARDED_BY(crit_);
|
2017-05-18 08:08:53 -07:00
|
|
|
|
2018-06-18 10:48:16 +02:00
|
|
|
absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(crit_);
|
2017-06-13 02:47:28 -07:00
|
|
|
std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
|
2017-09-07 07:53:45 -07:00
|
|
|
RTC_GUARDED_BY(crit_);
|
2017-06-13 02:47:28 -07:00
|
|
|
|
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.
|
|
|
|
|
const 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_
|