From abb6d4f7d96c49ff78011bc5d2c3ff5956cf642d Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 17 Aug 2018 16:06:03 -0400 Subject: [PATCH] fixup some logic, remove redundant call to clearQueue, increase first_video_packet_index when clearing video frames from front in queuePacket --- src/zm_packetqueue.cpp | 70 ++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index 8f36e0cd7..79798bbc1 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -33,23 +33,32 @@ zm_packetqueue::~zm_packetqueue() { clearQueue(); } +/* Enqueues the given packet. Will maintain the analysis_it pointer and image packet counts. + * If we have reached our max image packet count, it will pop off as many packets as are needed. + * Thus it will ensure that the same packet never gets queued twice. + */ + bool zm_packetqueue::queuePacket( ZMPacket* zm_packet ) { if ( zm_packet->image_index != -1 ) { // If we can never queue the same packet, then they can never go past if ( zm_packet->image_index == first_video_packet_index ) { - Debug(2, "queuing packet that is already on the queue(%d)", zm_packet->image_index ); + Debug(2, "queuing packet that is already on the queue(%d)", zm_packet->image_index); ZMPacket *p = NULL;; while ( pktQueue.size() && (p = pktQueue.front()) && ( p->image_index != zm_packet->image_index ) ) { if ( ( analysis_it != pktQueue.end() ) && ( *analysis_it == p ) ) { - Debug(2, "Increasing analysis_it"); + Debug(2, "Increasing analysis_it, meaning analysis is not keeping up"); ++analysis_it; } pktQueue.pop_front(); if ( p->codec_type == AVMEDIA_TYPE_VIDEO ) { - Debug(2, "Descreasing video_packet_count (%d)", video_packet_count); + Debug(2, "Decreasing video_packet_count (%d), popped (%d)", + video_packet_count, p->image_index); video_packet_count -= 1; + first_video_packet_index += 1; + first_video_packet_index %= max_video_packet_count; + } else { Debug(2, "Deleteing audio frame(%d)", p->image_index); delete p; @@ -66,7 +75,14 @@ bool zm_packetqueue::queuePacket( ZMPacket* zm_packet ) { first_video_packet_index %= max_video_packet_count; } else { - Error("SHould have found the packet!"); + Error("SHould have found the packet! front packet index was %d, new packet index is %d ", + p->image_index, zm_packet->image_index + ); + } + if ( analysis_it == pktQueue.end() ) { + // Analsys_it should only point to end when queue is empty + Debug(2,"pointing analysis_it to begining"); + analysis_it = pktQueue.begin(); } } else if ( first_video_packet_index == -1 ) { // Initialize the first_video_packet indicator @@ -74,18 +90,17 @@ bool zm_packetqueue::queuePacket( ZMPacket* zm_packet ) { } // end if } // end if queuing a video packet - pktQueue.push_back( zm_packet ); + pktQueue.push_back(zm_packet); + +#if 0 + // This code should not be neccessary. Taken care of by the above code that ensure that no packet appears twice if ( zm_packet->codec_type == AVMEDIA_TYPE_VIDEO ) { video_packet_count += 1; if ( video_packet_count >= max_video_packet_count ) - clearQueue( max_video_packet_count, video_stream_id ); + clearQueue(max_video_packet_count, video_stream_id); } +#endif - if ( analysis_it == pktQueue.end() ) { - // ANalsys_it should only point to end when queue it empty - Debug(2,"pointing analysis_it to begining"); - analysis_it = pktQueue.begin(); - } return true; } // end bool zm_packetqueue::queuePacket(ZMPacket* zm_packet) @@ -104,6 +119,7 @@ ZMPacket* zm_packetqueue::popPacket( ) { video_packet_count -= 1; if ( video_packet_count ) { // There is another video packet, so it must be the next one + Debug(2,"Incrementing first video packet index was (%d)", first_video_packet_index); first_video_packet_index += 1; first_video_packet_index %= max_video_packet_count; } else { @@ -116,10 +132,9 @@ ZMPacket* zm_packetqueue::popPacket( ) { unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream_id ) { - Debug(3, "Clearing all but %d frames, queue has %d", frames_to_keep, pktQueue.size() ); + Debug(3, "Clearing all but %d frames, queue has %d", frames_to_keep, pktQueue.size()); if ( pktQueue.empty() ) { - Debug(3, "Queue is empty"); return 0; } frames_to_keep += 1; @@ -131,12 +146,12 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream std::list::reverse_iterator it; ZMPacket *packet = NULL; - for ( it = pktQueue.rbegin(); it != pktQueue.rend() && frames_to_keep; ++it ) { + for ( it = pktQueue.rbegin(); frames_to_keep && (it != pktQueue.rend()); ++it ) { ZMPacket *zm_packet = *it; AVPacket *av_packet = &(zm_packet->packet); - Debug(4, "Looking at packet with stream index (%d) with keyframe(%d), frames_to_keep is (%d)", - av_packet->stream_index, zm_packet->keyframe, frames_to_keep ); + Debug(4, "Looking at packet with stream index (%d) with keyframe(%d), Image_index(%d) frames_to_keep is (%d)", + av_packet->stream_index, zm_packet->keyframe, zm_packet->image_index, frames_to_keep ); // Want frames_to_keep video keyframes. Otherwise, we may not have enough if ( av_packet->stream_index == stream_id ) { @@ -145,38 +160,41 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream } } - // Make sure we start on a keyframe + // Make sure we start on a keyframe for ( ; it != pktQueue.rend(); ++it ) { ZMPacket *zm_packet = *it; AVPacket *av_packet = &(zm_packet->packet); - Debug(5, "Looking for keyframe at packet with stream index (%d) with keyframe (%d), frames_to_keep is (%d)", av_packet->stream_index, ( av_packet->flags & AV_PKT_FLAG_KEY ), frames_to_keep ); + Debug(5, "Looking for keyframe at packet with stream index (%d) with keyframe (%d), image_index(%d) frames_to_keep is (%d)", + av_packet->stream_index, ( av_packet->flags & AV_PKT_FLAG_KEY ), zm_packet->image_index, frames_to_keep ); // Want frames_to_keep video keyframes. Otherwise, we may not have enough if ( ( av_packet->stream_index == stream_id) && ( av_packet->flags & AV_PKT_FLAG_KEY ) ) { - Debug(4, "Found keyframe at packet with stream index (%d) with keyframe (%d), frames_to_keep is (%d)", av_packet->stream_index, ( av_packet->flags & AV_PKT_FLAG_KEY ), frames_to_keep ); + Debug(4, "Found keyframe at packet with stream index (%d) with keyframe (%d), frames_to_keep is (%d)", + av_packet->stream_index, ( av_packet->flags & AV_PKT_FLAG_KEY ), frames_to_keep ); break; } packets_to_delete--; } if ( frames_to_keep ) { - Debug(3, "Hit end of queue, still need (%d) video frames", frames_to_keep ); + Debug(3, "Hit end of queue, still need (%d) video frames", frames_to_keep); } if ( it != pktQueue.rend() ) { // We want to keep this packet, so advance to the next - it ++; + ++it; packets_to_delete--; } int delete_count = 0; if ( packets_to_delete > 0 ) { - Debug(4, "Deleting packets from the front, count is (%d)", packets_to_delete ); + Debug(4, "Deleting packets from the front, count is (%d)", packets_to_delete); while ( --packets_to_delete ) { - Debug(4, "Deleting a packet from the front, count is (%d), queue size is %d", delete_count, pktQueue.size() ); + Debug(4, "Deleting a packet from the front, count is (%d), queue size is %d", + delete_count, pktQueue.size()); packet = pktQueue.front(); if ( *analysis_it == packet ) - analysis_it ++; + ++analysis_it; if ( packet->codec_type == AVMEDIA_TYPE_VIDEO ) { video_packet_count -= 1; if ( video_packet_count ) { @@ -204,14 +222,14 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream } #endif - Debug(3, "Deleted packets, resulting size is %d", pktQueue.size() ); + Debug(3, "Deleted packets, resulting size is %d", pktQueue.size()); return delete_count; } // end unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream_id ) void zm_packetqueue::clearQueue() { mutex.lock(); ZMPacket *packet = NULL; - while(!pktQueue.empty()) { + while ( !pktQueue.empty() ) { packet = pktQueue.front(); pktQueue.pop_front(); if ( packet->image_index == -1 )