Detect out of order packets

pull/3609/head
Isaac Connor 2022-08-10 17:57:11 -04:00
parent 7801d8e3b8
commit 1e1d665690
2 changed files with 24 additions and 2 deletions

View File

@ -33,7 +33,8 @@ PacketQueue::PacketQueue():
packet_counts(nullptr),
deleting(false),
keep_keyframes(false),
warned_count(0)
warned_count(0),
has_out_of_order_packets_(false)
{
}
@ -86,6 +87,24 @@ bool PacketQueue::queuePacket(std::shared_ptr<ZMPacket> add_packet) {
}
{
std::unique_lock<std::mutex> lck(mutex);
if (deleting or zm_terminate) return false;
if (!has_out_of_order_packets_ and (add_packet->packet.dts != AV_NOPTS_VALUE)) {
auto rit = pktQueue.rbegin();
// Find the previous packet for the stream, and check dts
while (rit != pktQueue.rend()) {
if ((*rit)->packet.stream_index == add_packet->packet.stream_index) {
if ((*rit)->packet.dts >= add_packet->packet.dts) {
Debug(1, "Have out of order packets");
ZM_DUMP_PACKET((*rit)->packet, "queued_packet");
ZM_DUMP_PACKET(add_packet->packet, "add_packet");
has_out_of_order_packets_ = true;
}
break;
}
rit++;
} // end while
}
pktQueue.push_back(add_packet);
packet_counts[add_packet->packet.stream_index] += 1;
@ -297,7 +316,7 @@ void PacketQueue::clearPackets(const std::shared_ptr<ZMPacket> &add_packet) {
}
#endif
if (zm_packet->packet->stream_index == video_stream_id) {
if (zm_packet->packet.stream_index == video_stream_id) {
keyframe_interval_count++;
if (zm_packet->keyframe) {
Debug(4, "Have a video keyframe so setting next front to it. Keyframe interval so far is %d", keyframe_interval_count);
@ -358,6 +377,7 @@ void PacketQueue::stop() {
void PacketQueue::clear() {
deleting = true;
// Why are we notifying?
condition.notify_all();
if (!packet_counts) // special case, not initialised
return;

View File

@ -47,6 +47,7 @@ class PacketQueue {
std::mutex mutex;
std::condition_variable condition;
int warned_count;
bool has_out_of_order_packets_;
public:
PacketQueue();
@ -65,6 +66,7 @@ class PacketQueue {
void dumpQueue();
unsigned int size();
unsigned int get_packet_count(int stream_id) const { return packet_counts[stream_id]; };
bool has_out_of_order_packets() const { return has_out_of_order_packets_; };
void clearPackets(const std::shared_ptr<ZMPacket> &packet);
int packet_count(int stream_id);