From e73e6aaabc4af842be1f869e47d473ee76dfb130 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 26 Jan 2021 12:23:44 -0500 Subject: [PATCH] check for terminating before locking packetqueue. Handle when pre_event_count==0 --- src/zm_packetqueue.cpp | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index 366f308c2..1164f556f 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -428,6 +428,8 @@ int zm_packetqueue::packet_count(int stream_id) { // Returns a packet. Packet will be locked ZMPacket *zm_packetqueue::get_packet(packetqueue_iterator *it) { + if ( deleting or zm_terminate ) + return nullptr; Debug(4, "Locking in get_packet using it %p queue end? %d, packet %p", *it, (*it == pktQueue.end()), *(*it)); @@ -494,20 +496,22 @@ std::list::iterator zm_packetqueue::get_event_start_packet_it( ) { std::list::iterator it = snapshot_it; - dumpPacket( &((*it)->packet ) ); + dumpPacket(&((*it)->packet)); // Step one count back pre_event_count frames as the minimum // Do not assume that snapshot_it is video - Debug(1, "Checking for keyframe %p", *it); - Debug(1, "Checking for keyframe begin %p", *(pktQueue.begin())); // snapshot it might already point to the beginning while ( ( it != pktQueue.begin() ) and pre_event_count ) { - Debug(1, "Previous packet pre %d index %d keyframe %d", pre_event_count, (*it)->packet.stream_index, (*it)->keyframe); - dumpPacket( &((*it)->packet ) ); - if ( (*it)->packet.stream_index != video_stream_id ) { + Debug(1, "Previous packet pre_event_count %d stream_index %d keyframe %d", pre_event_count, (*it)->packet.stream_index, (*it)->keyframe); + dumpPacket(&((*it)->packet)); + if ( (*it)->packet.stream_index == video_stream_id ) { pre_event_count --; + if ( ! pre_event_count ) + break; } it--; } + // it either points to beginning or we have seen pre_event_count video packets. + if ( it == pktQueue.begin() ) { Debug(1, "Hit begin"); // hit end, the first packet in the queue should ALWAYS be a video keyframe. @@ -523,21 +527,20 @@ std::list::iterator zm_packetqueue::get_event_start_packet_it( } return it; } - Debug(1, "Checking for keyframe %p", *it); + + // Not at beginning, so must be pointing at a video keyframe or maybe pre_event_count == 0 if ( (*it)->keyframe ) { - Debug(1, "Returning"); - Debug(1, "Previous packet pre %d index %d keyframe %d", pre_event_count, (*it)->image_index, (*it)->keyframe); + dumpPacket(&((*it)->packet), "Found video keyframe, Returning"); return it; } - Debug(1, "Wasnt Checking for keyframe"); - while ( ( it-- != pktQueue.begin() ) and ! (*it)->keyframe ) { - Debug(1, "No keyframe"); - dumpPacket( &((*it)->packet ) ); + while ( it-- != pktQueue.begin() ) { + dumpPacket(&((*it)->packet), "No keyframe"); + if ( (*it)->packet.stream_index == video_stream_id and (*it)->keyframe ) + return it; // Success } - Debug(1, "Checking for keyframe"); if ( !(*it)->keyframe ) { - Warning("Hit end of packetqueue before satisfying pre_event_count. Needed %d more video frames", pre_event_count); + Warning("Hit end of packetqueue before satisfying pre_event_count. Needed %d more video frames", pre_event_count); } return it;