fixup some logic, remove redundant call to clearQueue, increase first_video_packet_index when clearing video frames from front in queuePacket

This commit is contained in:
Isaac Connor 2018-08-17 16:06:03 -04:00
parent 53eae61883
commit abb6d4f7d9
1 changed files with 44 additions and 26 deletions

View File

@ -33,23 +33,32 @@ zm_packetqueue::~zm_packetqueue() {
clearQueue(); 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 ) { bool zm_packetqueue::queuePacket( ZMPacket* zm_packet ) {
if ( zm_packet->image_index != -1 ) { if ( zm_packet->image_index != -1 ) {
// If we can never queue the same packet, then they can never go past // If we can never queue the same packet, then they can never go past
if ( zm_packet->image_index == first_video_packet_index ) { 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;; ZMPacket *p = NULL;;
while ( pktQueue.size() && (p = pktQueue.front()) && ( p->image_index != zm_packet->image_index ) ) { while ( pktQueue.size() && (p = pktQueue.front()) && ( p->image_index != zm_packet->image_index ) ) {
if ( ( analysis_it != pktQueue.end() ) && ( *analysis_it == p ) ) { 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; ++analysis_it;
} }
pktQueue.pop_front(); pktQueue.pop_front();
if ( p->codec_type == AVMEDIA_TYPE_VIDEO ) { 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; video_packet_count -= 1;
first_video_packet_index += 1;
first_video_packet_index %= max_video_packet_count;
} else { } else {
Debug(2, "Deleteing audio frame(%d)", p->image_index); Debug(2, "Deleteing audio frame(%d)", p->image_index);
delete p; delete p;
@ -66,7 +75,14 @@ bool zm_packetqueue::queuePacket( ZMPacket* zm_packet ) {
first_video_packet_index %= max_video_packet_count; first_video_packet_index %= max_video_packet_count;
} else { } 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 ) { } else if ( first_video_packet_index == -1 ) {
// Initialize the first_video_packet indicator // Initialize the first_video_packet indicator
@ -74,18 +90,17 @@ bool zm_packetqueue::queuePacket( ZMPacket* zm_packet ) {
} // end if } // end if
} // end if queuing a video packet } // 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 ) { if ( zm_packet->codec_type == AVMEDIA_TYPE_VIDEO ) {
video_packet_count += 1; video_packet_count += 1;
if ( video_packet_count >= max_video_packet_count ) 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; return true;
} // end bool zm_packetqueue::queuePacket(ZMPacket* zm_packet) } // end bool zm_packetqueue::queuePacket(ZMPacket* zm_packet)
@ -104,6 +119,7 @@ ZMPacket* zm_packetqueue::popPacket( ) {
video_packet_count -= 1; video_packet_count -= 1;
if ( video_packet_count ) { if ( video_packet_count ) {
// There is another video packet, so it must be the next one // 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 += 1;
first_video_packet_index %= max_video_packet_count; first_video_packet_index %= max_video_packet_count;
} else { } else {
@ -116,10 +132,9 @@ ZMPacket* zm_packetqueue::popPacket( ) {
unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream_id ) { 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() ) { if ( pktQueue.empty() ) {
Debug(3, "Queue is empty");
return 0; return 0;
} }
frames_to_keep += 1; frames_to_keep += 1;
@ -131,12 +146,12 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream
std::list<ZMPacket *>::reverse_iterator it; std::list<ZMPacket *>::reverse_iterator it;
ZMPacket *packet = NULL; 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; ZMPacket *zm_packet = *it;
AVPacket *av_packet = &(zm_packet->packet); AVPacket *av_packet = &(zm_packet->packet);
Debug(4, "Looking at packet with stream index (%d) with keyframe(%d), frames_to_keep is (%d)", 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, frames_to_keep ); 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 // Want frames_to_keep video keyframes. Otherwise, we may not have enough
if ( av_packet->stream_index == stream_id ) { 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 ) { for ( ; it != pktQueue.rend(); ++it ) {
ZMPacket *zm_packet = *it; ZMPacket *zm_packet = *it;
AVPacket *av_packet = &(zm_packet->packet); 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 // 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 ( ( 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; break;
} }
packets_to_delete--; packets_to_delete--;
} }
if ( frames_to_keep ) { 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() ) { if ( it != pktQueue.rend() ) {
// We want to keep this packet, so advance to the next // We want to keep this packet, so advance to the next
it ++; ++it;
packets_to_delete--; packets_to_delete--;
} }
int delete_count = 0; int delete_count = 0;
if ( packets_to_delete > 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 ) { 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(); packet = pktQueue.front();
if ( *analysis_it == packet ) if ( *analysis_it == packet )
analysis_it ++; ++analysis_it;
if ( packet->codec_type == AVMEDIA_TYPE_VIDEO ) { if ( packet->codec_type == AVMEDIA_TYPE_VIDEO ) {
video_packet_count -= 1; video_packet_count -= 1;
if ( video_packet_count ) { if ( video_packet_count ) {
@ -204,14 +222,14 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream
} }
#endif #endif
Debug(3, "Deleted packets, resulting size is %d", pktQueue.size() ); Debug(3, "Deleted packets, resulting size is %d", pktQueue.size());
return delete_count; return delete_count;
} // end unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream_id ) } // end unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream_id )
void zm_packetqueue::clearQueue() { void zm_packetqueue::clearQueue() {
mutex.lock(); mutex.lock();
ZMPacket *packet = NULL; ZMPacket *packet = NULL;
while(!pktQueue.empty()) { while ( !pktQueue.empty() ) {
packet = pktQueue.front(); packet = pktQueue.front();
pktQueue.pop_front(); pktQueue.pop_front();
if ( packet->image_index == -1 ) if ( packet->image_index == -1 )