2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Monitor Class Interfaces, $Date$, $Revision$
|
|
|
|
// Copyright (C) 2003 Philip Coombes
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ZM_MONITOR_H
|
|
|
|
#define ZM_MONITOR_H
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
static struct timezone dummy_tz; // To avoid declaring pointless one each time we use gettimeofday
|
|
|
|
|
|
|
|
#include "zm_coord.h"
|
|
|
|
#include "zm_image.h"
|
|
|
|
#include "zm_zone.h"
|
|
|
|
#include "zm_camera.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// This is the main class for monitors. Each monitor is associated
|
|
|
|
// with a camera and is effectivaly a collector for events.
|
|
|
|
//
|
|
|
|
class Monitor
|
|
|
|
{
|
|
|
|
public:
|
2003-06-12 22:29:54 +08:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
QUERY=0,
|
|
|
|
CAPTURE,
|
|
|
|
ANALYSIS
|
2003-09-23 17:52:45 +08:00
|
|
|
} Purpose;
|
2003-06-12 22:29:54 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
typedef enum
|
|
|
|
{
|
2003-09-23 17:52:45 +08:00
|
|
|
CONTINUOUS=0,
|
2004-01-08 21:01:04 +08:00
|
|
|
TRIGGERED
|
2003-09-23 17:52:45 +08:00
|
|
|
} RunMode;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
OFF=1,
|
|
|
|
MONITOR,
|
|
|
|
MODECT,
|
|
|
|
RECORD,
|
|
|
|
MOCORD
|
2003-03-26 19:57:29 +08:00
|
|
|
} Function;
|
|
|
|
|
2003-05-02 23:03:16 +08:00
|
|
|
typedef enum { ROTATE_0=1, ROTATE_90, ROTATE_180, ROTATE_270 } Orientation;
|
|
|
|
|
2003-09-23 17:52:45 +08:00
|
|
|
typedef enum { IDLE, ALARM, ALERT, TAPE } State;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2004-02-16 04:07:16 +08:00
|
|
|
protected:
|
2004-02-16 03:53:10 +08:00
|
|
|
static bool initialised;
|
|
|
|
static bool record_event_stats;
|
|
|
|
static bool record_diag_images;
|
|
|
|
static bool opt_adaptive_skip;
|
|
|
|
static bool create_analysis_images;
|
|
|
|
static bool blend_alarmed_images;
|
2004-02-16 04:07:16 +08:00
|
|
|
static bool timestamp_on_capture;
|
2004-02-16 03:53:10 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
protected:
|
|
|
|
// These are read from the DB and thereafter remain unchanged
|
|
|
|
int id;
|
|
|
|
char *name;
|
2003-10-19 18:11:07 +08:00
|
|
|
Function function; // What the monitor is doing
|
2003-05-02 23:03:16 +08:00
|
|
|
unsigned int width; // Normally the same as the camera, but not if partly rotated
|
|
|
|
unsigned int height; // Normally the same as the camera, but not if partly rotated
|
2003-09-23 17:52:45 +08:00
|
|
|
RunMode run_mode; // Whether the monitor is running continuously or is triggered
|
2003-05-02 23:03:16 +08:00
|
|
|
Orientation orientation; // Whether the image has to be rotated at all
|
2003-03-26 19:57:29 +08:00
|
|
|
char label_format[64]; // The format of the timestamp on the images
|
|
|
|
Coord label_coord; // The coordinates of the timestamp on the images
|
2003-04-07 19:16:34 +08:00
|
|
|
int image_buffer_count; // Size of circular image buffer, at least twice the size of the pre_event_count
|
2003-03-26 19:57:29 +08:00
|
|
|
int warmup_count; // How many images to process before looking for events
|
|
|
|
int pre_event_count; // How many images to hold and prepend to an alarm event
|
|
|
|
int post_event_count; // How many unalarmed images must occur before the alarm state is reset
|
2003-09-23 17:52:45 +08:00
|
|
|
int section_length; // How long events should last in continuous modes
|
2003-10-08 22:49:26 +08:00
|
|
|
int frame_skip; // How many frames to skip in continuous modes
|
2003-03-27 06:12:25 +08:00
|
|
|
int capture_delay; // How long we wait between capture frames
|
2003-03-26 19:57:29 +08:00
|
|
|
int fps_report_interval;// How many images should be captured/processed between reporting the current FPS
|
|
|
|
int ref_blend_perc; // Percentage of new image going into reference image.
|
|
|
|
|
|
|
|
double fps;
|
|
|
|
Image image;
|
2004-01-15 05:26:47 +08:00
|
|
|
Image ref_image;
|
|
|
|
|
|
|
|
Purpose purpose; // What this monitor has been created to do
|
2003-03-26 19:57:29 +08:00
|
|
|
int event_count;
|
|
|
|
int image_count;
|
|
|
|
int first_alarm_count;
|
|
|
|
int last_alarm_count;
|
|
|
|
int buffer_count;
|
|
|
|
State state;
|
|
|
|
int n_zones;
|
|
|
|
Zone **zones;
|
|
|
|
Event *event;
|
|
|
|
time_t start_time;
|
|
|
|
time_t last_fps_time;
|
|
|
|
int shmid;
|
|
|
|
|
|
|
|
typedef struct Snapshot
|
|
|
|
{
|
|
|
|
struct timeval *timestamp;
|
|
|
|
Image *image;
|
|
|
|
};
|
|
|
|
|
|
|
|
Snapshot *image_buffer;
|
|
|
|
|
2003-03-26 21:18:26 +08:00
|
|
|
typedef enum { FORCE_NEUTRAL, FORCE_ON, FORCE_OFF } ForceState;
|
2003-06-25 17:47:09 +08:00
|
|
|
typedef enum { GET_SETTINGS=0x0001, SET_SETTINGS=0x0002 } Action;
|
2003-03-26 21:18:26 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
bool valid;
|
2003-03-26 19:57:29 +08:00
|
|
|
State state;
|
2003-06-25 17:47:09 +08:00
|
|
|
ForceState force_state;
|
2003-03-26 19:57:29 +08:00
|
|
|
int last_write_index;
|
|
|
|
int last_read_index;
|
2003-07-06 04:40:38 +08:00
|
|
|
time_t last_image_time;
|
2003-03-26 19:57:29 +08:00
|
|
|
int last_event;
|
2003-06-25 17:47:09 +08:00
|
|
|
int action;
|
|
|
|
int brightness;
|
|
|
|
int hue;
|
|
|
|
int colour;
|
|
|
|
int contrast;
|
2003-06-12 22:29:54 +08:00
|
|
|
} SharedData;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2003-06-12 22:29:54 +08:00
|
|
|
SharedData *shared_data;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
|
|
|
Camera *camera;
|
2004-02-16 03:53:10 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static void Initialise()
|
|
|
|
{
|
|
|
|
initialised = true;
|
|
|
|
|
|
|
|
record_event_stats = (bool)config.Item( ZM_RECORD_EVENT_STATS );
|
|
|
|
record_diag_images = (bool)config.Item( ZM_RECORD_DIAG_IMAGES );
|
|
|
|
opt_adaptive_skip = (bool)config.Item( ZM_OPT_ADAPTIVE_SKIP );
|
|
|
|
create_analysis_images = (bool)config.Item( ZM_CREATE_ANALYSIS_IMAGES );
|
|
|
|
blend_alarmed_images = (bool)config.Item( ZM_BLEND_ALARMED_IMAGES );
|
2004-02-16 04:07:16 +08:00
|
|
|
timestamp_on_capture = (bool)config.Item( ZM_TIMESTAMP_ON_CAPTURE );
|
2004-02-16 03:53:10 +08:00
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
public:
|
2003-10-08 22:49:26 +08:00
|
|
|
Monitor( int p_id, char *p_name, int p_function, int p_device, int p_channel, int p_format, int p_width, int p_height, int p_palette, int p_orientation, char *p_label_format, const Coord &p_label_coord, int p_image_buffer_count, int p_warmup_count, int p_pre_event_count, int p_post_event_count, int p_section_length, int p_frame_skip, int p_capture_delay, int p_fps_report_interval, int p_ref_blend_perc, Purpose p_purpose=QUERY, int p_n_zones=0, Zone *p_zones[]=0 );
|
|
|
|
Monitor( int p_id, char *p_name, int p_function, const char *p_host, const char *p_port, const char *p_path, int p_width, int p_height, int p_palette, int p_orientation, char *p_label_format, const Coord &p_label_coord, int p_image_buffer_count, int p_warmup_count, int p_pre_event_count, int p_post_event_count, int p_section_length, int p_frame_skip, int p_capture_delay, int p_fps_report_interval, int p_ref_blend_perc, Purpose p_purpose=QUERY, int p_n_zones=0, Zone *p_zones[]=0 );
|
2003-03-26 19:57:29 +08:00
|
|
|
~Monitor();
|
|
|
|
|
2004-02-16 03:53:10 +08:00
|
|
|
void Setup();
|
2003-06-12 22:29:54 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
void AddZones( int p_n_zones, Zone *p_zones[] );
|
|
|
|
|
2003-06-12 22:29:54 +08:00
|
|
|
inline int ShmValid() const
|
|
|
|
{
|
|
|
|
return( shared_data->valid );
|
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
inline int Id() const
|
|
|
|
{
|
|
|
|
return( id );
|
|
|
|
}
|
|
|
|
inline char *Name() const
|
|
|
|
{
|
|
|
|
return( name );
|
|
|
|
}
|
|
|
|
State GetState() const;
|
2004-02-18 23:35:51 +08:00
|
|
|
int GetImage( int index=-1, int scale=100 ) const;
|
2003-03-26 19:57:29 +08:00
|
|
|
struct timeval GetTimestamp( int index=-1 ) const;
|
2003-03-27 06:12:25 +08:00
|
|
|
int GetCaptureDelay() const { return( capture_delay ); }
|
2003-03-26 19:57:29 +08:00
|
|
|
unsigned int GetLastReadIndex() const;
|
|
|
|
unsigned int GetLastWriteIndex() const;
|
|
|
|
unsigned int GetLastEvent() const;
|
|
|
|
double GetFPS() const;
|
2003-03-26 21:18:26 +08:00
|
|
|
void ForceAlarmOn();
|
|
|
|
void ForceAlarmOff();
|
|
|
|
void CancelForced();
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2004-02-16 04:07:16 +08:00
|
|
|
inline void TimestampImage( Image *ts_image, time_t ts_time ) const
|
|
|
|
{
|
|
|
|
if ( label_format[0] )
|
|
|
|
{
|
|
|
|
static char label_time_text[256];
|
|
|
|
static char label_text[256];
|
|
|
|
|
|
|
|
strftime( label_time_text, sizeof(label_time_text), label_format, localtime( &ts_time ) );
|
|
|
|
sprintf( label_text, label_time_text, name );
|
|
|
|
|
|
|
|
ts_image->Annotate( label_text, label_coord );
|
|
|
|
}
|
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
int Brightness( int p_brightness=-1 );
|
|
|
|
int Hue( int p_hue=-1 );
|
|
|
|
int Colour( int p_colour=-1 );
|
|
|
|
int Contrast( int p_contrast=-1 );
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
bool DumpSettings( char *output, bool verbose );
|
|
|
|
void DumpZoneImage();
|
|
|
|
|
2003-05-02 23:03:16 +08:00
|
|
|
unsigned int Width() const { return( width ); }
|
|
|
|
unsigned int Height() const { return( height ); }
|
2003-03-26 19:57:29 +08:00
|
|
|
inline int PreCapture()
|
|
|
|
{
|
|
|
|
return( camera->PreCapture() );
|
|
|
|
}
|
|
|
|
inline int PostCapture()
|
|
|
|
{
|
|
|
|
if ( camera->PostCapture( image ) == 0 )
|
|
|
|
{
|
2003-05-02 23:03:16 +08:00
|
|
|
if ( orientation != ROTATE_0 )
|
|
|
|
{
|
|
|
|
image.Rotate( (orientation-1)*90 );
|
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
int index = image_count%image_buffer_count;
|
|
|
|
|
2003-09-23 17:52:45 +08:00
|
|
|
if ( index == shared_data->last_read_index && function > MONITOR )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
|
|
|
Warning(( "Buffer overrun at index %d\n", index ));
|
|
|
|
}
|
2004-02-16 04:07:16 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
gettimeofday( image_buffer[index].timestamp, &dummy_tz );
|
2004-02-16 04:07:16 +08:00
|
|
|
if ( timestamp_on_capture )
|
|
|
|
{
|
|
|
|
TimestampImage( &image, image_buffer[index].timestamp->tv_sec );
|
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
image_buffer[index].image->CopyBuffer( image );
|
|
|
|
|
2003-06-12 22:29:54 +08:00
|
|
|
shared_data->last_write_index = index;
|
2003-07-06 04:40:38 +08:00
|
|
|
shared_data->last_image_time = image_buffer[index].timestamp->tv_sec;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
|
|
|
image_count++;
|
|
|
|
|
|
|
|
if ( image_count && !(image_count%fps_report_interval) )
|
|
|
|
{
|
2004-02-16 04:07:16 +08:00
|
|
|
time_t now = image_buffer[index].timestamp->tv_sec;
|
2003-03-26 19:57:29 +08:00
|
|
|
fps = double(fps_report_interval)/(now-last_fps_time);
|
2003-03-28 01:15:00 +08:00
|
|
|
Info(( "%s: %d - Capturing at %.2f fps", name, image_count, fps ));
|
2003-03-26 19:57:29 +08:00
|
|
|
last_fps_time = now;
|
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
|
|
|
|
if ( shared_data->action & GET_SETTINGS )
|
|
|
|
{
|
|
|
|
shared_data->brightness = camera->Brightness();
|
|
|
|
shared_data->hue = camera->Hue();
|
|
|
|
shared_data->colour = camera->Colour();
|
|
|
|
shared_data->contrast = camera->Contrast();
|
|
|
|
shared_data->action &= ~GET_SETTINGS;
|
|
|
|
}
|
|
|
|
if ( shared_data->action & SET_SETTINGS )
|
|
|
|
{
|
|
|
|
camera->Brightness( shared_data->brightness );
|
|
|
|
camera->Hue( shared_data->hue );
|
|
|
|
camera->Colour( shared_data->colour );
|
|
|
|
camera->Contrast( shared_data->contrast );
|
|
|
|
shared_data->action &= ~SET_SETTINGS;
|
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Ready()
|
|
|
|
{
|
2003-09-23 17:52:45 +08:00
|
|
|
return( function > MONITOR && image_count > warmup_count );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2004-01-15 05:26:47 +08:00
|
|
|
void DumpImage( Image *dump_image ) const;
|
2003-03-26 19:57:29 +08:00
|
|
|
bool Analyse();
|
|
|
|
|
2004-01-15 05:26:47 +08:00
|
|
|
unsigned int Compare( const Image &comp_image );
|
2003-03-26 19:57:29 +08:00
|
|
|
void ReloadZones();
|
2003-09-23 17:52:45 +08:00
|
|
|
static int Load( int device, Monitor **&monitors, Purpose purpose=QUERY );
|
|
|
|
static int Load( const char *host, const char*port, const char*path, Monitor **&monitors, Purpose purpose=QUERY );
|
|
|
|
static Monitor *Load( int id, bool load_zones=false, Purpose purpose=QUERY );
|
2003-10-19 18:11:07 +08:00
|
|
|
void StreamImages( unsigned long idle=5000, unsigned long refresh=50, time_t ttl=0, int scale=1, FILE *fd=stdout );
|
2003-03-26 19:57:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ZM_MONITOR_H
|