From 98b3621681986e70c47cb76a5d2cc05e18f712cf Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 31 May 2017 20:44:43 -0400 Subject: [PATCH] add Debug, and fix logic deleting frames before the appropriate keyframe --- src/zm_packetqueue.cpp | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index f60b11df5..d4520caba 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -60,14 +60,12 @@ ZMPacket* zm_packetqueue::popPacket( ) { unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream_id ) { - Debug(3, "Clearing all but %d frames", frames_to_keep ); + Debug(3, "Clearing all but %d frames, queue has %d", frames_to_keep, pktQueue.size() ); frames_to_keep += 1; if ( pktQueue.empty() ) { Debug(3, "Queue is empty"); return 0; - } else { - Debug(3, "Queue has (%d)", pktQueue.size() ); } list::reverse_iterator it; @@ -77,18 +75,19 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream ZMPacket *zm_packet = *it; AVPacket *av_packet = &(zm_packet->packet); - Debug(3, "Looking 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, "Looking 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 ); // 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 ) ) { - if (!frames_to_keep) - break; frames_to_keep --; } } + if ( frames_to_keep ) { + Debug(3, "Hit end of queue, still need (%d) video keyframes", frames_to_keep ); + } unsigned int delete_count = 0; while ( it != pktQueue.rend() ) { - Debug(3, "Deleting a packet from the front, count is (%d)", delete_count ); + Debug(4, "Deleting a packet from the front, count is (%d)", delete_count ); packet = pktQueue.front(); pktQueue.pop_front(); @@ -96,6 +95,7 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream delete_count += 1; } + Debug(3, "Deleted (%d) packets", delete_count ); return delete_count; } // end unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream_id ) @@ -123,10 +123,10 @@ void zm_packetqueue::clear_unwanted_packets( timeval *recording_started, int mVi // Step 2 - pop packets until we get to the packet in step 2 list::reverse_iterator it; + Debug(3, "Looking for keyframe after start recording stream id (%d)", mVideoStreamId ); for ( it = pktQueue.rbegin(); it != pktQueue.rend(); ++ it ) { ZMPacket *zm_packet = *it; AVPacket *av_packet = &(zm_packet->packet); -Debug(1, "Looking for keyframe after start" ); if ( ( av_packet->flags & AV_PKT_FLAG_KEY ) && @@ -134,7 +134,7 @@ Debug(1, "Looking for keyframe after start" ); && timercmp( &(zm_packet->timestamp), recording_started, < ) ) { -Debug(1, "Found keyframe before start" ); + Debug(3, "Found keyframe before start with stream index (%d) with keyframe (%d)", av_packet->stream_index, ( av_packet->flags & AV_PKT_FLAG_KEY ) ); break; } } @@ -143,10 +143,29 @@ Debug(1, "Found keyframe before start" ); return; } + ZMPacket *zm_packet = *it; + AVPacket *av_packet = &(zm_packet->packet); + Debug(3, "Found packet before start with stream index (%d) with keyframe (%d), distance(%d), size(%d)", + av_packet->stream_index, + ( av_packet->flags & AV_PKT_FLAG_KEY ), + distance( it, pktQueue.rend() ), + pktQueue.size() ); + + unsigned int deleted_frames = 0; ZMPacket *packet = NULL; - while ( pktQueue.rend() != it ) { + while ( distance( it, pktQueue.rend() ) > 1 ) { + //while ( pktQueue.rend() != it ) { packet = pktQueue.front(); pktQueue.pop_front(); delete packet; + deleted_frames += 1; + } + + zm_packet = pktQueue.front(); + av_packet = &(zm_packet->packet); + if ( ( ! ( av_packet->flags & AV_PKT_FLAG_KEY ) ) || ( av_packet->stream_index != mVideoStreamId ) ) { + Error( "Done looking for keyframe. Deleted %d frames. Remaining frames in queue: %d stream of head packet is (%d), keyframe (%d), distance(%d), packets(%d)", deleted_frames, pktQueue.size(), av_packet->stream_index, ( av_packet->flags & AV_PKT_FLAG_KEY ), distance( it, pktQueue.rend() ), pktQueue.size() ); + } else { + Debug(1, "Done looking for keyframe. Deleted %d frames. Remaining frames in queue: %d stream of head packet is (%d), keyframe (%d), distance(%d), packets(%d)", deleted_frames, pktQueue.size(), av_packet->stream_index, ( av_packet->flags & AV_PKT_FLAG_KEY ), distance( it, pktQueue.rend() ), pktQueue.size() ); } }