diff --git a/src/zm_event.cpp b/src/zm_event.cpp index b21822297..0c43b40f9 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -296,10 +296,11 @@ void Event::updateNotes(const StringSetMap &newNoteSetMap) { } // end if update } // void Event::updateNotes(const StringSetMap &newNoteSetMap) -void Event::AddPacket(ZMLockedPacket *packetlock) { +void Event::AddPacket(const std::shared_ptr&packet) { { std::unique_lock lck(packet_queue_mutex); - packet_queue.push(packetlock); + + packet_queue.push(std::move(packet)); } packet_queue_condition.notify_one(); } @@ -683,11 +684,10 @@ void Event::Run() { if (storage != monitor->getStorage()) delete storage; - // The idea is to process the queue no matter what so that all packets get processed. // We only break if the queue is empty while (true) { - ZMLockedPacket * packet_lock = nullptr; + std::shared_ptr packet = nullptr; { std::unique_lock lck(packet_queue_mutex); @@ -697,14 +697,13 @@ void Event::Run() { // Neccessary because we don't hold the lock in the while condition } if (!packet_queue.empty()) { - // Packets on this queue are locked. They are locked by analysis thread - packet_lock = packet_queue.front(); + packet = packet_queue.front(); packet_queue.pop(); } } // end lock scope - if (packet_lock) { - this->AddPacket_(packet_lock->packet_); - delete packet_lock; + if (packet) { + Debug(1, "Adding packet %d", packet->image_index); + this->AddPacket_(packet); } } // end while } // end Run() diff --git a/src/zm_event.h b/src/zm_event.h index f26cd9733..f2515b5c1 100644 --- a/src/zm_event.h +++ b/src/zm_event.h @@ -106,7 +106,7 @@ class Event { void createNotes(std::string ¬es); - std::queue packet_queue; + std::queue> packet_queue; std::mutex packet_queue_mutex; std::condition_variable packet_queue_condition; @@ -135,7 +135,7 @@ class Event { SystemTimePoint EndTime() const { return end_time; } TimePoint::duration Duration() const { return end_time - start_time; }; - void AddPacket(ZMLockedPacket *); + void AddPacket(const std::shared_ptr &p); void AddPacket_(const std::shared_ptr &p); bool WritePacket(const std::shared_ptr &p); bool SendFrameImage(const Image *image, bool alarm_frame=false); diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 2c058c1e7..0eda55d77 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -2263,7 +2263,7 @@ bool Monitor::Analyse() { } if (event) { - event->AddPacket(packet_lock); + event->AddPacket(snap); } else { // In the case where people have pre-alarm frames, the web ui will generate the frame images // from the mp4. So no one will notice anyways. @@ -2283,9 +2283,9 @@ bool Monitor::Analyse() { // Free up the decoded frame as well, we won't be using it for anything at this time. snap->out_frame = nullptr; - delete packet_lock; } } // end scope for event_lock + delete packet_lock; packetqueue.increment_it(analysis_it); shared_data->last_read_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); @@ -2892,7 +2892,10 @@ Event * Monitor::openEvent( // Write out starting packets, do not modify packetqueue it will garbage collect itself while (starting_packet_lock && (*start_it != *analysis_it) && !zm_terminate) { ZM_DUMP_PACKET(starting_packet_lock->packet_->packet, "Queuing packet for event"); - event->AddPacket(starting_packet_lock); + + event->AddPacket(starting_packet); + delete starting_packet_lock; + starting_packet_lock = nullptr; packetqueue.increment_it(start_it); if ((*start_it) != *analysis_it) {