From da74f8d5338e6df88bec022ed2df59bf373b0a39 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 10 Jul 2017 19:37:55 -0400 Subject: [PATCH] initial turn zma into a thread --- scripts/zmdc.pl.in | 2 +- scripts/zmpkg.pl.in | 2 +- src/CMakeLists.txt | 2 +- src/zm_event.cpp | 2 +- src/zm_monitor.cpp | 31 +++++++++++++++---------------- src/zmc.cpp | 18 ++++++++++++++++++ web/ajax/status.php | 3 ++- 7 files changed, 39 insertions(+), 21 deletions(-) diff --git a/scripts/zmdc.pl.in b/scripts/zmdc.pl.in index aaaee0ff8..2f99f0b06 100644 --- a/scripts/zmdc.pl.in +++ b/scripts/zmdc.pl.in @@ -91,7 +91,7 @@ delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; my @daemons = ( 'zmc', - 'zma', + #'zma', 'zmfilter.pl', 'zmaudit.pl', 'zmtrigger.pl', diff --git a/scripts/zmpkg.pl.in b/scripts/zmpkg.pl.in index 597d7ac1a..da50401b0 100644 --- a/scripts/zmpkg.pl.in +++ b/scripts/zmpkg.pl.in @@ -251,7 +251,7 @@ if ( $command =~ /^(?:start|restart)$/ ) } if ( $monitor->{Function} ne 'Monitor' ) { - runCommand( "zmdc.pl start zma -m $monitor->{Id}" ); + #runCommand( "zmdc.pl start zma -m $monitor->{Id}" ); } if ( $Config{ZM_OPT_CONTROL} ) { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aa0dbc370..c0459d55b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,7 +4,7 @@ configure_file(zm_config.h.in "${CMAKE_CURRENT_BINARY_DIR}/zm_config.h" @ONLY) # Group together all the source files that are used by all the binaries (zmc, zma, zmu, zms etc) -set(ZM_BIN_SRC_FILES zm_box.cpp zm_buffer.cpp zm_camera.cpp zm_comms.cpp zm_config.cpp zm_coord.cpp zm_curl_camera.cpp zm.cpp zm_db.cpp zm_logger.cpp zm_event.cpp zm_eventstream.cpp zm_exception.cpp zm_file_camera.cpp zm_ffmpeg_camera.cpp zm_image.cpp zm_jpeg.cpp zm_libvlc_camera.cpp zm_local_camera.cpp zm_monitor.cpp zm_monitorstream.cpp zm_ffmpeg.cpp zm_mpeg.cpp zm_packet.cpp zm_packetqueue.cpp zm_poly.cpp zm_regexp.cpp zm_remote_camera.cpp zm_remote_camera_http.cpp zm_remote_camera_rtsp.cpp zm_rtp.cpp zm_rtp_ctrl.cpp zm_rtp_data.cpp zm_rtp_source.cpp zm_rtsp.cpp zm_rtsp_auth.cpp zm_sdp.cpp zm_signal.cpp zm_stream.cpp zm_thread.cpp zm_time.cpp zm_timer.cpp zm_user.cpp zm_utils.cpp zm_video.cpp zm_videostore.cpp zm_zone.cpp zm_storage.cpp) +set(ZM_BIN_SRC_FILES zm_analysis_thread.cpp zm_box.cpp zm_buffer.cpp zm_camera.cpp zm_comms.cpp zm_config.cpp zm_coord.cpp zm_curl_camera.cpp zm.cpp zm_db.cpp zm_logger.cpp zm_event.cpp zm_eventstream.cpp zm_exception.cpp zm_file_camera.cpp zm_ffmpeg_camera.cpp zm_image.cpp zm_jpeg.cpp zm_libvlc_camera.cpp zm_local_camera.cpp zm_monitor.cpp zm_monitorstream.cpp zm_ffmpeg.cpp zm_mpeg.cpp zm_packet.cpp zm_packetqueue.cpp zm_poly.cpp zm_regexp.cpp zm_remote_camera.cpp zm_remote_camera_http.cpp zm_remote_camera_rtsp.cpp zm_rtp.cpp zm_rtp_ctrl.cpp zm_rtp_data.cpp zm_rtp_source.cpp zm_rtsp.cpp zm_rtsp_auth.cpp zm_sdp.cpp zm_signal.cpp zm_stream.cpp zm_thread.cpp zm_time.cpp zm_timer.cpp zm_user.cpp zm_utils.cpp zm_video.cpp zm_videostore.cpp zm_zone.cpp zm_storage.cpp) # A fix for cmake recompiling the source files for every target. add_library(zm STATIC ${ZM_BIN_SRC_FILES}) diff --git a/src/zm_event.cpp b/src/zm_event.cpp index f841c82c6..358ccb495 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -78,7 +78,7 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string static char sql[ZM_SQL_MED_BUFSIZ]; struct tm *stime = localtime( &start_time.tv_sec ); - snprintf( sql, sizeof(sql), "insert into Events ( MonitorId, StorageId, Name, StartTime, Width, Height, Cause, Notes, StateId, Orientation, Videoed ) values ( %d, %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s', %d, %d, %d )", + snprintf( sql, sizeof(sql), "insert into Events ( MonitorId, StorageId, Name, StartTime, Width, Height, Cause, Notes, StateId, Orientation, Videoed, DefaultVideo ) values ( %d, %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s', %d, %d, %d, '' )", monitor->Id(), storage->Id(), start_time.tv_sec, diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 1f1c2413e..820c3ff12 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -384,6 +384,7 @@ Monitor::Monitor( snprintf( monitor_dir, sizeof(monitor_dir), "%s/%d", storage->Path(), id ); struct stat statbuf; + // If we are going to actually do capture, then yes, we should stat this dir, otherwise not if ( stat( monitor_dir, &statbuf ) ) { if ( errno == ENOENT || errno == ENOTDIR ) { if ( mkdir( monitor_dir, 0755 ) ) { @@ -427,21 +428,18 @@ Monitor::Monitor( snprintf(video_store_data->event_file, sizeof(video_store_data->event_file), "nothing"); video_store_data->size = sizeof(VideoStoreData); //video_store_data->frameNumber = 0; - } else if ( purpose == ANALYSIS ) { - this->connect(); - if ( ! mem_ptr ) exit(-1); - shared_data->state = IDLE; - shared_data->last_read_time = 0; - shared_data->alarm_x = -1; - shared_data->alarm_y = -1; + + //} else if ( purpose == ANALYSIS ) { + + //this->connect(); } - if ( ( ! mem_ptr ) || ! shared_data->valid ) { - if ( purpose != QUERY ) { - Error( "Shared data not initialised by capture daemon for monitor %s", name ); - exit( -1 ); - } - } + //if ( ( ! mem_ptr ) || ! shared_data->valid ) { + //if ( purpose != QUERY ) { + //Error( "Shared data not initialised by capture daemon for monitor %s", name ); + //exit( -1 ); + //} + //} // Will this not happen every time a monitor is instantiated? Seems like all the calls to the Monitor constructor pass a zero for n_zones, then load zones after.. // In my storage areas branch, I took this out.. and didn't notice any problems. @@ -463,8 +461,8 @@ Monitor::Monitor( //Set video recording flag for event start constructor and easy reference in code videoRecording = ((GetOptVideoWriter() == H264PASSTHROUGH) && camera->SupportsNativeVideo()); - if ( purpose == ANALYSIS ) { - + //if ( purpose == ANALYSIS ) { +if ( 0 ) { while( shared_data->last_write_index == (unsigned int)image_buffer_count && shared_data->last_write_time == 0) { Warning( "Waiting for capture daemon" ); @@ -472,13 +470,14 @@ Monitor::Monitor( } ref_image.Assign( width, height, camera->Colours(), camera->SubpixelOrder(), image_buffer[shared_data->last_write_index].image->Buffer(), camera->ImageSize()); +} n_linked_monitors = 0; linked_monitors = 0; adaptive_skip = true; ReloadLinkedMonitors( p_linked_monitors ); - } + //} } bool Monitor::connect() { diff --git a/src/zmc.cpp b/src/zmc.cpp index 5915d4ef5..b6b6ccd99 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -70,6 +70,7 @@ possible, this should run at more or less constant speed. #include "zm_time.h" #include "zm_signal.h" #include "zm_monitor.h" +#include "zm_analysis_thread.h" void Usage() { fprintf( stderr, "zmc -d or -r -H -P -p or -f or -m \n" ); @@ -238,6 +239,7 @@ int main( int argc, char *argv[] ) { exit( -1 ); } + AnalysisThread **analysis_threads = new AnalysisThread *[n_monitors]; long *capture_delays = new long[n_monitors]; long *alarm_capture_delays = new long[n_monitors]; long *next_delays = new long[n_monitors]; @@ -246,6 +248,15 @@ int main( int argc, char *argv[] ) { last_capture_times[i].tv_sec = last_capture_times[i].tv_usec = 0; capture_delays[i] = monitors[i]->GetCaptureDelay(); alarm_capture_delays[i] = monitors[i]->GetAlarmCaptureDelay(); + + Monitor::Function function = monitors[0]->GetFunction(); + if ( function == Monitor::MODECT || function == Monitor::MOCORD ) { + Debug(1, "Starting an analysis thread for monitor (%d)", monitors[i]->Id() ); + analysis_threads[i] = new AnalysisThread( monitors[i] ); + analysis_threads[i]->start(); + } else { + analysis_threads[i] = NULL; + } } int result = 0; @@ -309,8 +320,15 @@ int main( int argc, char *argv[] ) { sigprocmask( SIG_UNBLOCK, &block_set, 0 ); } // end while ! zm_terminate for ( int i = 0; i < n_monitors; i++ ) { + if ( analysis_threads[i] ) { + analysis_threads[i]->stop(); + analysis_threads[i]->join(); + delete analysis_threads[i]; + analysis_threads[i] = 0; + } delete monitors[i]; } + delete [] analysis_threads; delete [] monitors; delete [] alarm_capture_delays; delete [] capture_delays; diff --git a/web/ajax/status.php b/web/ajax/status.php index fbd91cf08..c9e9115f0 100644 --- a/web/ajax/status.php +++ b/web/ajax/status.php @@ -359,7 +359,8 @@ function getNearEvents() { $eventId = $_REQUEST['id']; $event = dbFetchOne( 'select * from Events where Id = ?', NULL, array( $eventId ) ); - parseFilter( $_REQUEST['filter'] ); + if ( isset($_REQUEST['filter']) ) + parseFilter( $_REQUEST['filter'] ); parseSort(); if ( $user['MonitorIds'] )