diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index e42020567..a915ede39 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -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 add_packet) { } { std::unique_lock 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 &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; diff --git a/src/zm_packetqueue.h b/src/zm_packetqueue.h index 340af7ddf..e468e2369 100644 --- a/src/zm_packetqueue.h +++ b/src/zm_packetqueue.h @@ -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 &packet); int packet_count(int stream_id);