Move clear packetqueue logic to it's own function and call it from the analysis thread.

This commit is contained in:
Isaac Connor 2021-02-18 19:25:40 -05:00
parent 5ad9244a73
commit 045cd219f8
3 changed files with 69 additions and 58 deletions

View File

@ -1950,7 +1950,7 @@ bool Monitor::Analyse() {
// If doing record, check to see if we need to close the event or not. // If doing record, check to see if we need to close the event or not.
if ( event ) { if ( event ) {
Debug(2, "Have event in mocord"); Debug(2, "Have event %" PRIu64 " in mocord", event->Id());
if ( section_length if ( section_length
&& ( ( timestamp->tv_sec - video_store_data->recording.tv_sec ) >= section_length ) && ( ( timestamp->tv_sec - video_store_data->recording.tv_sec ) >= section_length )
&& ( (function == MOCORD && (event_close_mode != CLOSE_TIME)) || ! ( timestamp->tv_sec % section_length ) ) && ( (function == MOCORD && (event_close_mode != CLOSE_TIME)) || ! ( timestamp->tv_sec % section_length ) )
@ -2229,6 +2229,7 @@ bool Monitor::Analyse() {
shared_data->last_read_time = time(nullptr); shared_data->last_read_time = time(nullptr);
analysis_image_count++; analysis_image_count++;
UpdateAnalysisFPS(); UpdateAnalysisFPS();
packetqueue.clearPackets(snap);
return true; return true;
} // end Monitor::Analyse } // end Monitor::Analyse

View File

@ -99,7 +99,15 @@ bool PacketQueue::queuePacket(ZMPacket* add_packet) {
--(*iterator_it); --(*iterator_it);
} }
} // end foreach iterator } // end foreach iterator
mutex.unlock();
// We signal on every packet because someday we may analyze sound
Debug(4, "packetqueue queuepacket, unlocked signalling");
condition.notify_all();
return true;
} // end bool PacketQueue::queuePacket(ZMPacket* zm_packet)
void PacketQueue::clearPackets(ZMPacket *add_packet) {
// Only do queueCleaning if we are adding a video keyframe, so that we guarantee that there is one. // Only do queueCleaning if we are adding a video keyframe, so that we guarantee that there is one.
// No good. Have to satisfy two conditions: // No good. Have to satisfy two conditions:
// 1. packetqueue starts with a video keyframe // 1. packetqueue starts with a video keyframe
@ -109,14 +117,20 @@ bool PacketQueue::queuePacket(ZMPacket* add_packet) {
// //
// So start at the beginning, counting video packets until the next keyframe. // So start at the beginning, counting video packets until the next keyframe.
// Then if deleting those packets doesn't break 1 and 2, then go ahead and delete them. // Then if deleting those packets doesn't break 1 and 2, then go ahead and delete them.
if ( add_packet->packet.stream_index == video_stream_id if ( ! (
add_packet->packet.stream_index == video_stream_id
and and
add_packet->keyframe add_packet->keyframe
and and
(packet_counts[video_stream_id] > max_video_packet_count) (packet_counts[video_stream_id] > max_video_packet_count)
and and
*(pktQueue.begin()) != add_packet *(pktQueue.begin()) != add_packet
)
) { ) {
return;
}
std::unique_lock<std::mutex> lck(mutex);
packetqueue_iterator it = pktQueue.begin(); packetqueue_iterator it = pktQueue.begin();
packetqueue_iterator next_front = pktQueue.begin(); packetqueue_iterator next_front = pktQueue.begin();
@ -171,15 +185,10 @@ bool PacketQueue::queuePacket(ZMPacket* add_packet) {
delete zm_packet; delete zm_packet;
} }
} // end if have at least max_video_packet_count video packets remaining } // end if have at least max_video_packet_count video packets remaining
} // end if this is a video keyframe
mutex.unlock();
// We signal on every packet because someday we may analyze sound // We signal on every packet because someday we may analyze sound
Debug(4, "packetqueue queuepacket, unlocked signalling");
condition.notify_all();
return true; return;
} // end bool PacketQueue::queuePacket(ZMPacket* zm_packet) } // end voidPacketQueue::clearPackets(ZMPacket* zm_packet)
ZMPacket* PacketQueue::popPacket( ) { ZMPacket* PacketQueue::popPacket( ) {
Debug(4, "pktQueue size %d", pktQueue.size()); Debug(4, "pktQueue size %d", pktQueue.size());

View File

@ -63,6 +63,7 @@ class PacketQueue {
unsigned int get_packet_count(int stream_id) const { return packet_counts[stream_id]; }; unsigned int get_packet_count(int stream_id) const { return packet_counts[stream_id]; };
void clear_unwanted_packets(timeval *recording, int pre_event_count, int mVideoStreamId); void clear_unwanted_packets(timeval *recording, int pre_event_count, int mVideoStreamId);
void clearPackets(ZMPacket *);
int packet_count(int stream_id); int packet_count(int stream_id);
bool increment_it(packetqueue_iterator *it); bool increment_it(packetqueue_iterator *it);