2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Monitor Class Implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2008-12-06 07:14:20 +08:00
|
|
|
#include <arpa/inet.h>
|
2007-08-30 02:11:09 +08:00
|
|
|
#include <glob.h>
|
2003-03-26 19:57:29 +08:00
|
|
|
|
|
|
|
#include "zm.h"
|
|
|
|
#include "zm_db.h"
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm_time.h"
|
2004-03-04 23:05:54 +08:00
|
|
|
#include "zm_mpeg.h"
|
2007-08-30 02:11:09 +08:00
|
|
|
#include "zm_signal.h"
|
2003-03-26 19:57:29 +08:00
|
|
|
#include "zm_monitor.h"
|
|
|
|
#include "zm_local_camera.h"
|
|
|
|
#include "zm_remote_camera.h"
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm_remote_camera_http.h"
|
2008-10-17 00:11:49 +08:00
|
|
|
#if HAVE_LIBAVFORMAT
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm_remote_camera_rtsp.h"
|
2008-10-17 00:11:49 +08:00
|
|
|
#endif // HAVE_LIBAVFORMAT
|
2005-10-18 05:55:02 +08:00
|
|
|
#include "zm_file_camera.h"
|
2009-01-20 23:01:32 +08:00
|
|
|
#if HAVE_LIBAVFORMAT
|
|
|
|
#include "zm_ffmpeg_camera.h"
|
|
|
|
#endif // HAVE_LIBAVFORMAT
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
#if ZM_MEM_MAPPED
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#else // ZM_MEM_MAPPED
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#endif // ZM_MEM_MAPPED
|
|
|
|
|
2006-01-23 02:31:55 +08:00
|
|
|
Monitor::MonitorLink::MonitorLink( int p_id, const char *p_name ) : id( p_id )
|
|
|
|
{
|
|
|
|
strncpy( name, p_name, sizeof(name) );
|
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
#if ZM_MEM_MAPPED
|
|
|
|
map_fd = -1;
|
2009-02-13 05:32:00 +08:00
|
|
|
snprintf( mem_file, sizeof(mem_file), "%s/.zm.mmap.%d", config.path_map, id );
|
2008-07-14 23:21:16 +08:00
|
|
|
#else // ZM_MEM_MAPPED
|
|
|
|
shm_id = 0;
|
|
|
|
#endif // ZM_MEM_MAPPED
|
|
|
|
mem_size = 0;
|
|
|
|
mem_ptr = 0;
|
2006-01-23 02:31:55 +08:00
|
|
|
|
|
|
|
last_event = 0;
|
|
|
|
last_state = IDLE;
|
|
|
|
|
|
|
|
last_connect_time = 0;
|
|
|
|
connected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Monitor::MonitorLink::~MonitorLink()
|
|
|
|
{
|
|
|
|
disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Monitor::MonitorLink::connect()
|
|
|
|
{
|
|
|
|
if ( !last_connect_time || (time( 0 ) - last_connect_time) > 60 )
|
|
|
|
{
|
|
|
|
last_connect_time = time( 0 );
|
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
mem_size = sizeof(SharedData) + sizeof(TriggerData);
|
|
|
|
|
|
|
|
Debug( 1, "link.mem.size=%d", mem_size );
|
|
|
|
#if ZM_MEM_MAPPED
|
|
|
|
map_fd = open( mem_file, O_RDWR, (mode_t)0600 );
|
|
|
|
if ( map_fd < 0 )
|
|
|
|
{
|
2008-09-01 23:31:20 +08:00
|
|
|
Debug( 3, "Can't open linked memory map file %s: %s", mem_file, strerror(errno) );
|
2008-07-14 23:21:16 +08:00
|
|
|
disconnect();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat map_stat;
|
|
|
|
if ( fstat( map_fd, &map_stat ) < 0 )
|
|
|
|
{
|
2008-09-01 23:31:20 +08:00
|
|
|
Error( "Can't stat linked memory map file %s: %s", mem_file, strerror(errno) );
|
2008-07-14 23:21:16 +08:00
|
|
|
disconnect();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( map_stat.st_size == 0 )
|
|
|
|
{
|
2008-09-01 23:31:20 +08:00
|
|
|
Error( "Linked memory map file %s is empty: %s", mem_file, strerror(errno) );
|
2008-07-14 23:21:16 +08:00
|
|
|
disconnect();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
else if ( map_stat.st_size < mem_size )
|
|
|
|
{
|
2008-09-01 23:31:20 +08:00
|
|
|
Error( "Got unexpected memory map file size %ld, expected %d", map_stat.st_size, mem_size );
|
2008-07-14 23:21:16 +08:00
|
|
|
disconnect();
|
|
|
|
return( false );
|
|
|
|
}
|
2006-01-23 02:31:55 +08:00
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
mem_ptr = (unsigned char *)mmap( NULL, mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, map_fd, 0 );
|
|
|
|
if ( mem_ptr == MAP_FAILED )
|
|
|
|
{
|
2008-09-01 23:31:20 +08:00
|
|
|
Error( "Can't map file %s (%d bytes) to memory: %s", mem_file, mem_size, strerror(errno) );
|
2008-07-14 23:21:16 +08:00
|
|
|
disconnect();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
#else // ZM_MEM_MAPPED
|
|
|
|
shm_id = shmget( (config.shm_key&0xffff0000)|id, mem_size, 0700 );
|
2006-01-23 02:31:55 +08:00
|
|
|
if ( shm_id < 0 )
|
|
|
|
{
|
2008-09-01 23:31:20 +08:00
|
|
|
Debug( 3, "Can't shmget link memory: %s", strerror(errno) );
|
2006-01-23 02:31:55 +08:00
|
|
|
connected = false;
|
|
|
|
return( false );
|
|
|
|
}
|
2008-07-14 23:21:16 +08:00
|
|
|
mem_ptr = (unsigned char *)shmat( shm_id, 0, 0 );
|
|
|
|
if ( mem_ptr < 0 )
|
2006-01-23 02:31:55 +08:00
|
|
|
{
|
2008-09-01 23:31:20 +08:00
|
|
|
Debug( 3, "Can't shmat link memory: %s", strerror(errno) );
|
2006-01-23 02:31:55 +08:00
|
|
|
connected = false;
|
|
|
|
return( false );
|
|
|
|
}
|
2008-07-14 23:21:16 +08:00
|
|
|
#endif // ZM_MEM_MAPPED
|
2006-01-23 02:31:55 +08:00
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
shared_data = (SharedData *)mem_ptr;
|
2006-01-23 02:31:55 +08:00
|
|
|
trigger_data = (TriggerData *)((char *)shared_data + sizeof(SharedData));
|
|
|
|
|
|
|
|
if ( !shared_data->valid )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Linked memory not initialised by capture daemon" );
|
2006-01-23 02:31:55 +08:00
|
|
|
disconnect();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
last_state = shared_data->state;
|
|
|
|
last_event = shared_data->last_event;
|
|
|
|
connected = true;
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Monitor::MonitorLink::disconnect()
|
|
|
|
{
|
|
|
|
if ( connected )
|
|
|
|
{
|
|
|
|
connected = false;
|
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
#if ZM_MEM_MAPPED
|
|
|
|
if ( mem_ptr > 0 )
|
|
|
|
{
|
|
|
|
msync( mem_ptr, mem_size, MS_ASYNC );
|
|
|
|
munmap( mem_ptr, mem_size );
|
|
|
|
}
|
|
|
|
if ( map_fd >= 0 )
|
|
|
|
close( map_fd );
|
|
|
|
|
|
|
|
map_fd = -1;
|
|
|
|
#else // ZM_MEM_MAPPED
|
2006-01-23 02:31:55 +08:00
|
|
|
struct shmid_ds shm_data;
|
|
|
|
if ( shmctl( shm_id, IPC_STAT, &shm_data ) < 0 )
|
|
|
|
{
|
2008-07-14 23:21:16 +08:00
|
|
|
Debug( 3, "Can't shmctl: %s", strerror(errno) );
|
2006-01-23 02:31:55 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
shm_id = 0;
|
2006-01-23 02:31:55 +08:00
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
if ( shm_data.shm_nattch <= 1 )
|
|
|
|
{
|
|
|
|
if ( shmctl( shm_id, IPC_RMID, 0 ) < 0 )
|
|
|
|
{
|
|
|
|
Debug( 3, "Can't shmctl: %s", strerror(errno) );
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
}
|
2006-01-23 02:31:55 +08:00
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
if ( shmdt( mem_ptr ) < 0 )
|
|
|
|
{
|
|
|
|
Debug( 3, "Can't shmdt: %s", strerror(errno) );
|
|
|
|
return( false );
|
|
|
|
}
|
2006-01-23 02:31:55 +08:00
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
#endif // ZM_MEM_MAPPED
|
|
|
|
mem_size = 0;
|
|
|
|
mem_ptr = 0;
|
2006-01-23 02:31:55 +08:00
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Monitor::MonitorLink::isAlarmed()
|
|
|
|
{
|
|
|
|
if ( !connected )
|
|
|
|
{
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( shared_data->state == ALARM );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Monitor::MonitorLink::inAlarm()
|
|
|
|
{
|
|
|
|
if ( !connected )
|
|
|
|
{
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( shared_data->state == ALARM || shared_data->state == ALERT );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Monitor::MonitorLink::hasAlarmed()
|
|
|
|
{
|
|
|
|
if ( shared_data->state == ALARM || shared_data->state == ALERT )
|
|
|
|
{
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
else if( shared_data->last_event != last_event )
|
|
|
|
{
|
|
|
|
last_event = shared_data->last_event;
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
2003-09-23 17:52:45 +08:00
|
|
|
Monitor::Monitor(
|
|
|
|
int p_id,
|
2006-01-23 02:31:55 +08:00
|
|
|
const char *p_name,
|
2003-09-23 17:52:45 +08:00
|
|
|
int p_function,
|
2005-12-23 00:46:25 +08:00
|
|
|
bool p_enabled,
|
2006-01-23 02:31:55 +08:00
|
|
|
const char *p_linked_monitors,
|
2005-10-18 05:55:02 +08:00
|
|
|
Camera *p_camera,
|
2003-09-23 17:52:45 +08:00
|
|
|
int p_orientation,
|
2006-01-23 02:31:55 +08:00
|
|
|
const char *p_event_prefix,
|
|
|
|
const char *p_label_format,
|
2003-09-23 17:52:45 +08:00
|
|
|
const Coord &p_label_coord,
|
|
|
|
int p_image_buffer_count,
|
|
|
|
int p_warmup_count,
|
|
|
|
int p_pre_event_count,
|
|
|
|
int p_post_event_count,
|
2007-08-30 02:11:09 +08:00
|
|
|
int p_stream_replay_buffer,
|
2004-09-25 20:37:45 +08:00
|
|
|
int p_alarm_frame_count,
|
2003-09-23 17:52:45 +08:00
|
|
|
int p_section_length,
|
2003-10-08 22:49:26 +08:00
|
|
|
int p_frame_skip,
|
2003-09-23 17:52:45 +08:00
|
|
|
int p_capture_delay,
|
2006-03-31 20:49:05 +08:00
|
|
|
int p_alarm_capture_delay,
|
2003-09-23 17:52:45 +08:00
|
|
|
int p_fps_report_interval,
|
|
|
|
int p_ref_blend_perc,
|
2005-02-24 18:43:29 +08:00
|
|
|
bool p_track_motion,
|
2007-09-05 23:05:28 +08:00
|
|
|
Rgb p_signal_check_colour,
|
2003-09-23 17:52:45 +08:00
|
|
|
Purpose p_purpose,
|
|
|
|
int p_n_zones,
|
|
|
|
Zone *p_zones[]
|
|
|
|
) : id( p_id ),
|
|
|
|
function( (Function)p_function ),
|
2005-12-23 00:46:25 +08:00
|
|
|
enabled( p_enabled ),
|
2005-10-18 05:55:02 +08:00
|
|
|
width( (p_orientation==ROTATE_90||p_orientation==ROTATE_270)?p_camera->Height():p_camera->Width() ),
|
|
|
|
height( (p_orientation==ROTATE_90||p_orientation==ROTATE_270)?p_camera->Width():p_camera->Height() ),
|
2008-03-13 21:36:12 +08:00
|
|
|
orientation( (Orientation)p_orientation ),
|
2003-09-23 17:52:45 +08:00
|
|
|
label_coord( p_label_coord ),
|
|
|
|
image_buffer_count( p_image_buffer_count ),
|
|
|
|
warmup_count( p_warmup_count ),
|
|
|
|
pre_event_count( p_pre_event_count ),
|
|
|
|
post_event_count( p_post_event_count ),
|
2007-08-30 02:11:09 +08:00
|
|
|
stream_replay_buffer( p_stream_replay_buffer ),
|
2003-09-23 17:52:45 +08:00
|
|
|
section_length( p_section_length ),
|
2003-10-08 22:49:26 +08:00
|
|
|
frame_skip( p_frame_skip ),
|
2003-09-23 17:52:45 +08:00
|
|
|
capture_delay( p_capture_delay ),
|
2006-03-31 20:49:05 +08:00
|
|
|
alarm_capture_delay( p_alarm_capture_delay ),
|
2008-03-13 21:36:12 +08:00
|
|
|
alarm_frame_count( p_alarm_frame_count ),
|
2003-09-23 17:52:45 +08:00
|
|
|
fps_report_interval( p_fps_report_interval ),
|
|
|
|
ref_blend_perc( p_ref_blend_perc ),
|
2005-02-24 18:43:29 +08:00
|
|
|
track_motion( p_track_motion ),
|
2007-09-05 23:05:28 +08:00
|
|
|
signal_check_colour( p_signal_check_colour ),
|
2005-10-18 05:55:02 +08:00
|
|
|
image( width, height, p_camera->Colours() ),
|
|
|
|
ref_image( width, height, p_camera->Colours() ),
|
2003-09-23 17:52:45 +08:00
|
|
|
purpose( p_purpose ),
|
2008-03-13 21:36:12 +08:00
|
|
|
camera( p_camera ),
|
2003-09-23 17:52:45 +08:00
|
|
|
n_zones( p_n_zones ),
|
|
|
|
zones( p_zones )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2006-01-23 02:31:55 +08:00
|
|
|
strncpy( name, p_name, sizeof(name) );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2004-09-23 21:43:10 +08:00
|
|
|
strncpy( event_prefix, p_event_prefix, sizeof(event_prefix) );
|
2004-04-20 00:02:17 +08:00
|
|
|
strncpy( label_format, p_label_format, sizeof(label_format) );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2006-11-16 19:34:53 +08:00
|
|
|
// Change \n to actual line feeds
|
|
|
|
char *token_ptr = label_format;
|
|
|
|
const char *token_string = "\n";
|
2008-03-13 21:36:12 +08:00
|
|
|
while( ( token_ptr = strstr( token_ptr, token_string ) ) )
|
2006-11-16 19:34:53 +08:00
|
|
|
{
|
|
|
|
if ( *(token_ptr+1) )
|
|
|
|
{
|
|
|
|
*token_ptr = '\n';
|
|
|
|
token_ptr++;
|
|
|
|
strcpy( token_ptr, token_ptr+1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*token_ptr = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
fps = 0.0;
|
|
|
|
event_count = 0;
|
|
|
|
image_count = 0;
|
2005-12-23 00:46:25 +08:00
|
|
|
ready_count = warmup_count;
|
2003-03-26 19:57:29 +08:00
|
|
|
first_alarm_count = 0;
|
|
|
|
last_alarm_count = 0;
|
|
|
|
state = IDLE;
|
|
|
|
|
2004-09-25 20:37:45 +08:00
|
|
|
if ( alarm_frame_count < 1 )
|
|
|
|
alarm_frame_count = 1;
|
|
|
|
else if ( alarm_frame_count > MAX_PRE_ALARM_FRAMES )
|
|
|
|
alarm_frame_count = MAX_PRE_ALARM_FRAMES;
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
auto_resume_time = 0;
|
|
|
|
|
2008-09-25 19:18:55 +08:00
|
|
|
if ( strcmp( config.event_close_mode, "time" ) == 0 )
|
|
|
|
event_close_mode = CLOSE_TIME;
|
|
|
|
else if ( strcmp( config.event_close_mode, "alarm" ) == 0 )
|
|
|
|
event_close_mode = CLOSE_ALARM;
|
|
|
|
else
|
|
|
|
event_close_mode = CLOSE_IDLE;
|
|
|
|
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "monitor purpose=%d", purpose );
|
2003-06-12 22:29:54 +08:00
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
mem_size = sizeof(SharedData)
|
2006-01-21 00:10:08 +08:00
|
|
|
+ sizeof(TriggerData)
|
|
|
|
+ (image_buffer_count*sizeof(struct timeval))
|
|
|
|
+ (image_buffer_count*camera->ImageSize());
|
2005-11-22 18:39:51 +08:00
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
Debug( 1, "mem.size=%d", mem_size );
|
|
|
|
#if ZM_MEM_MAPPED
|
|
|
|
snprintf( mem_file, sizeof(mem_file), "%s/.zm.mmap.%d", config.path_map, id );
|
|
|
|
map_fd = open( mem_file, O_RDWR|O_CREAT, (mode_t)0600 );
|
|
|
|
if ( map_fd < 0 )
|
2008-09-01 23:31:20 +08:00
|
|
|
Fatal( "Can't open memory map file %s, probably not enough space free: %s", mem_file, strerror(errno) );
|
2008-07-14 23:21:16 +08:00
|
|
|
struct stat map_stat;
|
|
|
|
if ( fstat( map_fd, &map_stat ) < 0 )
|
2008-09-01 23:31:20 +08:00
|
|
|
Fatal( "Can't stat memory map file %s: %s", mem_file, strerror(errno) );
|
2008-07-14 23:21:16 +08:00
|
|
|
if ( map_stat.st_size == 0 )
|
|
|
|
{
|
|
|
|
// Allocate the size
|
|
|
|
if ( ftruncate( map_fd, mem_size ) < 0 )
|
2008-09-01 23:31:20 +08:00
|
|
|
Fatal( "Can't extend memory map file %s to %d bytes: %s", mem_file, mem_size, strerror(errno) );
|
2008-07-14 23:21:16 +08:00
|
|
|
}
|
|
|
|
else if ( map_stat.st_size != mem_size )
|
|
|
|
{
|
2008-09-01 23:31:20 +08:00
|
|
|
Error( "Got unexpected memory map file size %ld, expected %d", map_stat.st_size, mem_size );
|
2008-07-14 23:21:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mem_ptr = (unsigned char *)mmap( NULL, mem_size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, map_fd, 0 );
|
|
|
|
if ( mem_ptr == MAP_FAILED )
|
|
|
|
if ( errno == EAGAIN )
|
|
|
|
{
|
2008-09-01 23:31:20 +08:00
|
|
|
Debug( 1, "Unable to map file %s (%d bytes) to locked memory, trying unlocked", mem_file, mem_size );
|
2008-07-14 23:21:16 +08:00
|
|
|
mem_ptr = (unsigned char *)mmap( NULL, mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, map_fd, 0 );
|
|
|
|
}
|
|
|
|
if ( mem_ptr == MAP_FAILED )
|
2008-09-01 23:31:20 +08:00
|
|
|
Fatal( "Can't map file %s (%d bytes) to memory: %s(%d)", mem_file, mem_size, strerror(errno), errno );
|
2008-07-14 23:21:16 +08:00
|
|
|
#else // ZM_MEM_MAPPED
|
|
|
|
shm_id = shmget( (config.shm_key&0xffff0000)|id, mem_size, IPC_CREAT|0700 );
|
2006-01-21 00:10:08 +08:00
|
|
|
if ( shm_id < 0 )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't shmget, probably not enough shared memory space free: %s", strerror(errno));
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
2008-07-14 23:21:16 +08:00
|
|
|
mem_ptr = (unsigned char *)shmat( shm_id, 0, 0 );
|
|
|
|
if ( mem_ptr < 0 )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't shmat: %s", strerror(errno));
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
2008-07-14 23:21:16 +08:00
|
|
|
#endif // ZM_MEM_MAPPED
|
2005-11-22 18:39:51 +08:00
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
shared_data = (SharedData *)mem_ptr;
|
2005-11-22 18:39:51 +08:00
|
|
|
trigger_data = (TriggerData *)((char *)shared_data + sizeof(SharedData));
|
|
|
|
struct timeval *shared_timestamps = (struct timeval *)((char *)trigger_data + sizeof(TriggerData));
|
|
|
|
unsigned char *shared_images = (unsigned char *)((char *)shared_timestamps + (image_buffer_count*sizeof(struct timeval)));
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2003-09-23 17:52:45 +08:00
|
|
|
if ( purpose == CAPTURE )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-07-14 23:21:16 +08:00
|
|
|
memset( mem_ptr, 0, mem_size );
|
2004-12-29 01:41:55 +08:00
|
|
|
shared_data->size = sizeof(SharedData);
|
2003-06-12 22:29:54 +08:00
|
|
|
shared_data->valid = true;
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->active = enabled;
|
2006-01-16 01:39:18 +08:00
|
|
|
shared_data->signal = false;
|
2003-06-12 22:29:54 +08:00
|
|
|
shared_data->state = IDLE;
|
|
|
|
shared_data->last_write_index = image_buffer_count;
|
|
|
|
shared_data->last_read_index = image_buffer_count;
|
2008-07-21 23:20:52 +08:00
|
|
|
shared_data->last_write_time = 0;
|
2003-06-12 22:29:54 +08:00
|
|
|
shared_data->last_event = 0;
|
2003-06-25 17:47:09 +08:00
|
|
|
shared_data->action = (Action)0;
|
|
|
|
shared_data->brightness = -1;
|
|
|
|
shared_data->hue = -1;
|
|
|
|
shared_data->colour = -1;
|
|
|
|
shared_data->contrast = -1;
|
2005-02-24 18:43:29 +08:00
|
|
|
shared_data->alarm_x = -1;
|
|
|
|
shared_data->alarm_y = -1;
|
2004-12-29 01:41:55 +08:00
|
|
|
trigger_data->size = sizeof(TriggerData);
|
|
|
|
trigger_data->trigger_state = TRIGGER_CANCEL;
|
|
|
|
trigger_data->trigger_score = 0;
|
|
|
|
trigger_data->trigger_cause[0] = 0;
|
|
|
|
trigger_data->trigger_text[0] = 0;
|
2005-05-20 00:22:22 +08:00
|
|
|
trigger_data->trigger_showtext[0] = 0;
|
2003-06-12 22:29:54 +08:00
|
|
|
}
|
2007-08-30 02:11:09 +08:00
|
|
|
else if ( purpose == ANALYSIS )
|
|
|
|
{
|
|
|
|
shared_data->state = IDLE;
|
2008-07-21 23:20:52 +08:00
|
|
|
shared_data->last_read_time = 0;
|
2007-08-30 02:11:09 +08:00
|
|
|
shared_data->alarm_x = -1;
|
|
|
|
shared_data->alarm_y = -1;
|
|
|
|
}
|
2006-01-23 02:31:55 +08:00
|
|
|
|
2003-06-12 22:29:54 +08:00
|
|
|
if ( !shared_data->valid )
|
|
|
|
{
|
2007-12-30 02:50:53 +08:00
|
|
|
if ( purpose != QUERY )
|
|
|
|
{
|
2008-07-21 23:20:52 +08:00
|
|
|
Error( "Shared data not initialised by capture daemon" );
|
2007-12-30 02:50:53 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-21 23:20:52 +08:00
|
|
|
Warning( "Shared data not initialised by capture daemon, some query functions may not be available or produce invalid results" );
|
2007-12-30 02:50:53 +08:00
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
image_buffer = new Snapshot[image_buffer_count];
|
|
|
|
for ( int i = 0; i < image_buffer_count; i++ )
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
image_buffer[i].timestamp = &(shared_timestamps[i]);
|
|
|
|
image_buffer[i].image = new Image( width, height, camera->Colours(), &(shared_images[i*camera->ImageSize()]) );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
if ( !n_zones )
|
|
|
|
{
|
|
|
|
n_zones = 1;
|
|
|
|
zones = new Zone *[1];
|
2005-12-02 00:34:20 +08:00
|
|
|
Coord coords[4] = { Coord( 0, 0 ), Coord( width-1, 0 ), Coord( width-1, height-1 ), Coord( 0, height-1 ) };
|
2005-11-30 01:51:06 +08:00
|
|
|
zones[0] = new Zone( this, 0, "All", Zone::ACTIVE, Polygon( sizeof(coords)/sizeof(*coords), coords ), RGB_RED, Zone::BLOBS );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
start_time = last_fps_time = time( 0 );
|
|
|
|
|
|
|
|
event = 0;
|
|
|
|
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Monitor %s has function %d", name, function );
|
|
|
|
Debug( 1, "Monitor %s LBF = '%s', LBX = %d, LBY = %d", name, label_format, label_coord.X(), label_coord.Y() );
|
|
|
|
Debug( 1, "Monitor %s IBC = %d, WUC = %d, pEC = %d, PEC = %d, EAF = %d, FRI = %d, RBP = %d, FM = %d", name, image_buffer_count, warmup_count, pre_event_count, post_event_count, alarm_frame_count, fps_report_interval, ref_blend_perc, track_motion );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2003-09-23 17:52:45 +08:00
|
|
|
if ( purpose == ANALYSIS )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
|
|
|
static char path[PATH_MAX];
|
|
|
|
|
2005-05-16 17:27:06 +08:00
|
|
|
strncpy( path, config.dir_events, sizeof(path) );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
|
|
|
struct stat statbuf;
|
|
|
|
errno = 0;
|
|
|
|
stat( path, &statbuf );
|
|
|
|
if ( errno == ENOENT || errno == ENOTDIR )
|
|
|
|
{
|
|
|
|
if ( mkdir( path, 0755 ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't make %s: %s", path, strerror(errno));
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-16 17:27:06 +08:00
|
|
|
snprintf( path, sizeof(path), "%s/%d", config.dir_events, id );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
stat( path, &statbuf );
|
|
|
|
if ( errno == ENOENT || errno == ENOTDIR )
|
|
|
|
{
|
|
|
|
if ( mkdir( path, 0755 ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't make %s: %s", path, strerror(errno));
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2004-09-07 16:45:00 +08:00
|
|
|
char temp_path[PATH_MAX];
|
|
|
|
snprintf( temp_path, sizeof(temp_path), "%d", id );
|
2008-12-06 02:13:26 +08:00
|
|
|
if ( chdir( config.dir_events ) < 0 )
|
|
|
|
Fatal( "Can't change directory to '%s': %s", config.dir_events, strerror(errno) );
|
|
|
|
if ( symlink( temp_path, name ) < 0 )
|
|
|
|
Fatal( "Can't symlink '%s' to '%s': %s", temp_path, name, strerror(errno) );
|
2009-01-22 01:37:39 +08:00
|
|
|
if ( chdir( ".." ) < 0 )
|
|
|
|
Fatal( "Can't change to parent directory: %s", strerror(errno) );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2003-06-12 22:29:54 +08:00
|
|
|
while( shared_data->last_write_index == image_buffer_count )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Waiting for capture daemon" );
|
2003-06-12 22:29:54 +08:00
|
|
|
sleep( 1 );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2003-06-12 22:29:54 +08:00
|
|
|
ref_image.Assign( width, height, camera->Colours(), image_buffer[shared_data->last_write_index].image->Buffer() );
|
2006-01-23 02:31:55 +08:00
|
|
|
|
|
|
|
n_linked_monitors = 0;
|
|
|
|
linked_monitors = 0;
|
|
|
|
ReloadLinkedMonitors( p_linked_monitors );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
srand( getpid() * time( 0 ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2006-01-23 02:31:55 +08:00
|
|
|
Monitor::~Monitor()
|
|
|
|
{
|
|
|
|
closeEvent();
|
|
|
|
|
|
|
|
for ( int i = 0; i < image_buffer_count; i++ )
|
|
|
|
{
|
|
|
|
delete image_buffer[i].image;
|
|
|
|
}
|
|
|
|
delete[] image_buffer;
|
|
|
|
|
|
|
|
for ( int i = 0; i < n_zones; i++ )
|
|
|
|
{
|
|
|
|
delete zones[i];
|
|
|
|
}
|
|
|
|
delete[] zones;
|
|
|
|
|
|
|
|
delete camera;
|
|
|
|
|
|
|
|
if ( purpose == ANALYSIS )
|
|
|
|
{
|
|
|
|
shared_data->state = state = IDLE;
|
|
|
|
shared_data->last_read_index = image_buffer_count;
|
2008-07-21 23:20:52 +08:00
|
|
|
shared_data->last_read_time = 0;
|
2006-01-23 02:31:55 +08:00
|
|
|
}
|
|
|
|
else if ( purpose == CAPTURE )
|
|
|
|
{
|
|
|
|
shared_data->valid = false;
|
2008-07-14 23:21:16 +08:00
|
|
|
memset( mem_ptr, 0, mem_size );
|
2006-01-23 02:31:55 +08:00
|
|
|
}
|
|
|
|
|
2008-07-14 23:21:16 +08:00
|
|
|
#if ZM_MEM_MAPPED
|
|
|
|
if ( msync( mem_ptr, mem_size, MS_INVALIDATE ) < 0 )
|
|
|
|
Fatal( "Can't msync: %s", strerror(errno) );
|
|
|
|
if ( munmap( mem_ptr, mem_size ) < 0 )
|
|
|
|
Fatal( "Can't munmap: %s", strerror(errno) );
|
|
|
|
close( map_fd );
|
2009-02-13 05:32:00 +08:00
|
|
|
unlink( mem_file );
|
2008-07-14 23:21:16 +08:00
|
|
|
#else // ZM_MEM_MAPPED
|
|
|
|
struct shmid_ds shm_data;
|
|
|
|
if ( shmctl( shm_id, IPC_STAT, &shm_data ) < 0 )
|
|
|
|
{
|
|
|
|
Error( "Can't shmctl: %s", strerror(errno) );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
if ( shm_data.shm_nattch <= 1 )
|
|
|
|
{
|
|
|
|
if ( shmctl( shm_id, IPC_RMID, 0 ) < 0 )
|
|
|
|
{
|
|
|
|
Error( "Can't shmctl: %s", strerror(errno) );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // ZM_MEM_MAPPED
|
2006-01-23 02:31:55 +08:00
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
void Monitor::AddZones( int p_n_zones, Zone *p_zones[] )
|
|
|
|
{
|
2005-11-30 01:51:06 +08:00
|
|
|
for ( int i = 0; i < n_zones; i++ )
|
|
|
|
delete zones[i];
|
|
|
|
delete[] zones;
|
2003-03-26 19:57:29 +08:00
|
|
|
n_zones = p_n_zones;
|
|
|
|
zones = p_zones;
|
|
|
|
}
|
|
|
|
|
|
|
|
Monitor::State Monitor::GetState() const
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
return( shared_data->state );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2004-02-18 23:35:51 +08:00
|
|
|
int Monitor::GetImage( int index, int scale ) const
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
|
|
|
if ( index < 0 || index > image_buffer_count )
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
index = shared_data->last_write_index;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2004-02-18 23:35:51 +08:00
|
|
|
|
2005-11-20 20:17:45 +08:00
|
|
|
if ( index != image_buffer_count )
|
2004-02-18 23:35:51 +08:00
|
|
|
{
|
2005-11-20 20:17:45 +08:00
|
|
|
Snapshot *snap = &image_buffer[index];
|
|
|
|
Image snap_image( *(snap->image) );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( scale != ZM_SCALE_BASE )
|
2005-11-20 20:17:45 +08:00
|
|
|
{
|
|
|
|
snap_image.Scale( scale );
|
|
|
|
}
|
|
|
|
|
|
|
|
static char filename[PATH_MAX];
|
|
|
|
snprintf( filename, sizeof(filename), "%s.jpg", name );
|
|
|
|
if ( !config.timestamp_on_capture )
|
|
|
|
{
|
2006-12-25 00:50:01 +08:00
|
|
|
TimestampImage( &snap_image, snap->timestamp );
|
2005-11-20 20:17:45 +08:00
|
|
|
}
|
|
|
|
snap_image.WriteJpeg( filename );
|
|
|
|
}
|
|
|
|
else
|
2004-02-16 04:07:16 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Unable to generate image, no images in buffer" );
|
2004-02-16 04:07:16 +08:00
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval Monitor::GetTimestamp( int index ) const
|
|
|
|
{
|
|
|
|
if ( index < 0 || index > image_buffer_count )
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
index = shared_data->last_write_index;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2003-06-12 22:29:54 +08:00
|
|
|
|
2005-11-20 20:17:45 +08:00
|
|
|
if ( index != image_buffer_count )
|
|
|
|
{
|
|
|
|
Snapshot *snap = &image_buffer[index];
|
|
|
|
|
|
|
|
return( *(snap->timestamp) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static struct timeval null_tv = { 0, 0 };
|
|
|
|
|
|
|
|
return( null_tv );
|
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Monitor::GetLastReadIndex() const
|
|
|
|
{
|
2005-11-20 20:17:45 +08:00
|
|
|
return( shared_data->last_read_index!=image_buffer_count?shared_data->last_read_index:-1 );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Monitor::GetLastWriteIndex() const
|
|
|
|
{
|
2005-11-20 20:17:45 +08:00
|
|
|
return( shared_data->last_write_index!=image_buffer_count?shared_data->last_write_index:-1 );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Monitor::GetLastEvent() const
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
return( shared_data->last_event );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double Monitor::GetFPS() const
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
int index1 = shared_data->last_write_index;
|
2005-11-20 20:17:45 +08:00
|
|
|
if ( index1 == image_buffer_count )
|
|
|
|
{
|
|
|
|
return( 0.0 );
|
|
|
|
}
|
2004-03-04 23:05:54 +08:00
|
|
|
Snapshot *snap1 = &image_buffer[index1];
|
|
|
|
if ( !snap1->timestamp || !snap1->timestamp->tv_sec )
|
|
|
|
{
|
|
|
|
return( 0.0 );
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
struct timeval time1 = *snap1->timestamp;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
int image_count = image_buffer_count;
|
|
|
|
int index2 = (index1+1)%image_buffer_count;
|
2005-11-20 20:17:45 +08:00
|
|
|
if ( index2 == image_buffer_count )
|
|
|
|
{
|
|
|
|
return( 0.0 );
|
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
Snapshot *snap2 = &image_buffer[index2];
|
2008-07-16 16:35:59 +08:00
|
|
|
while ( !snap2->timestamp || !snap2->timestamp->tv_sec )
|
2004-03-04 23:05:54 +08:00
|
|
|
{
|
|
|
|
if ( index1 == index2 )
|
|
|
|
{
|
|
|
|
return( 0.0 );
|
|
|
|
}
|
|
|
|
index2 = (index2+1)%image_buffer_count;
|
|
|
|
snap2 = &image_buffer[index2];
|
|
|
|
image_count--;
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
struct timeval time2 = *snap2->timestamp;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
double time_diff = tvDiffSec( time2, time1 );
|
|
|
|
|
|
|
|
double curr_fps = image_count/time_diff;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2004-01-15 05:26:47 +08:00
|
|
|
return( curr_fps );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2004-12-29 01:41:55 +08:00
|
|
|
void Monitor::ForceAlarmOn( int force_score, const char *force_cause, const char *force_text )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2004-12-29 01:41:55 +08:00
|
|
|
trigger_data->trigger_state = TRIGGER_ON;
|
|
|
|
trigger_data->trigger_score = force_score;
|
|
|
|
strncpy( trigger_data->trigger_cause, force_cause, sizeof(trigger_data->trigger_cause) );
|
|
|
|
strncpy( trigger_data->trigger_text, force_text, sizeof(trigger_data->trigger_text) );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2003-03-26 21:18:26 +08:00
|
|
|
void Monitor::ForceAlarmOff()
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2004-12-29 01:41:55 +08:00
|
|
|
trigger_data->trigger_state = TRIGGER_OFF;
|
2003-03-26 21:18:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Monitor::CancelForced()
|
|
|
|
{
|
2004-12-29 01:41:55 +08:00
|
|
|
trigger_data->trigger_state = TRIGGER_CANCEL;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
void Monitor::actionReload()
|
|
|
|
{
|
|
|
|
shared_data->action |= RELOAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Monitor::actionEnable()
|
|
|
|
{
|
|
|
|
shared_data->action |= RELOAD;
|
|
|
|
|
|
|
|
static char sql[BUFSIZ];
|
|
|
|
snprintf( sql, sizeof(sql), "update Monitors set Enabled = 1 where Id = '%d'", id );
|
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2005-12-23 00:46:25 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Monitor::actionDisable()
|
|
|
|
{
|
|
|
|
shared_data->action |= RELOAD;
|
|
|
|
|
|
|
|
static char sql[BUFSIZ];
|
|
|
|
snprintf( sql, sizeof(sql), "update Monitors set Enabled = 0 where Id = '%d'", id );
|
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2005-12-23 00:46:25 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Monitor::actionSuspend()
|
2005-02-24 18:43:29 +08:00
|
|
|
{
|
|
|
|
shared_data->action |= SUSPEND;
|
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
void Monitor::actionResume()
|
2005-02-24 18:43:29 +08:00
|
|
|
{
|
|
|
|
shared_data->action |= RESUME;
|
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
int Monitor::actionBrightness( int p_brightness )
|
2003-06-25 17:47:09 +08:00
|
|
|
{
|
2003-09-23 17:52:45 +08:00
|
|
|
if ( purpose != CAPTURE )
|
2003-06-25 17:47:09 +08:00
|
|
|
{
|
|
|
|
if ( p_brightness >= 0 )
|
|
|
|
{
|
|
|
|
shared_data->brightness = p_brightness;
|
|
|
|
shared_data->action |= SET_SETTINGS;
|
|
|
|
int wait_loops = 10;
|
|
|
|
while ( shared_data->action & SET_SETTINGS )
|
|
|
|
{
|
|
|
|
if ( wait_loops-- )
|
|
|
|
usleep( 100000 );
|
|
|
|
else
|
2007-04-03 17:47:08 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Timed out waiting to set brightness" );
|
2003-06-25 17:47:09 +08:00
|
|
|
return( -1 );
|
2007-04-03 17:47:08 +08:00
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shared_data->action |= GET_SETTINGS;
|
|
|
|
int wait_loops = 10;
|
|
|
|
while ( shared_data->action & GET_SETTINGS )
|
|
|
|
{
|
|
|
|
if ( wait_loops-- )
|
|
|
|
usleep( 100000 );
|
|
|
|
else
|
2007-04-03 17:47:08 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Timed out waiting to get brightness" );
|
2003-06-25 17:47:09 +08:00
|
|
|
return( -1 );
|
2007-04-03 17:47:08 +08:00
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return( shared_data->brightness );
|
|
|
|
}
|
|
|
|
return( camera->Brightness( p_brightness ) );
|
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
int Monitor::actionContrast( int p_contrast )
|
2003-06-25 17:47:09 +08:00
|
|
|
{
|
2003-09-23 17:52:45 +08:00
|
|
|
if ( purpose != CAPTURE )
|
2003-06-25 17:47:09 +08:00
|
|
|
{
|
|
|
|
if ( p_contrast >= 0 )
|
|
|
|
{
|
|
|
|
shared_data->contrast = p_contrast;
|
|
|
|
shared_data->action |= SET_SETTINGS;
|
|
|
|
int wait_loops = 10;
|
|
|
|
while ( shared_data->action & SET_SETTINGS )
|
|
|
|
{
|
|
|
|
if ( wait_loops-- )
|
|
|
|
usleep( 100000 );
|
|
|
|
else
|
2007-04-03 17:47:08 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Timed out waiting to set contrast" );
|
2003-06-25 17:47:09 +08:00
|
|
|
return( -1 );
|
2007-04-03 17:47:08 +08:00
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shared_data->action |= GET_SETTINGS;
|
|
|
|
int wait_loops = 10;
|
|
|
|
while ( shared_data->action & GET_SETTINGS )
|
|
|
|
{
|
|
|
|
if ( wait_loops-- )
|
|
|
|
usleep( 100000 );
|
|
|
|
else
|
2007-04-03 17:47:08 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Timed out waiting to get contrast" );
|
2003-06-25 17:47:09 +08:00
|
|
|
return( -1 );
|
2007-04-03 17:47:08 +08:00
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return( shared_data->contrast );
|
|
|
|
}
|
|
|
|
return( camera->Contrast( p_contrast ) );
|
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
int Monitor::actionHue( int p_hue )
|
2003-06-25 17:47:09 +08:00
|
|
|
{
|
2003-09-23 17:52:45 +08:00
|
|
|
if ( purpose != CAPTURE )
|
2003-06-25 17:47:09 +08:00
|
|
|
{
|
|
|
|
if ( p_hue >= 0 )
|
|
|
|
{
|
|
|
|
shared_data->hue = p_hue;
|
|
|
|
shared_data->action |= SET_SETTINGS;
|
|
|
|
int wait_loops = 10;
|
|
|
|
while ( shared_data->action & SET_SETTINGS )
|
|
|
|
{
|
|
|
|
if ( wait_loops-- )
|
|
|
|
usleep( 100000 );
|
|
|
|
else
|
2007-04-03 17:47:08 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Timed out waiting to set hue" );
|
2003-06-25 17:47:09 +08:00
|
|
|
return( -1 );
|
2007-04-03 17:47:08 +08:00
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shared_data->action |= GET_SETTINGS;
|
|
|
|
int wait_loops = 10;
|
|
|
|
while ( shared_data->action & GET_SETTINGS )
|
|
|
|
{
|
|
|
|
if ( wait_loops-- )
|
|
|
|
usleep( 100000 );
|
|
|
|
else
|
2007-04-03 17:47:08 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Timed out waiting to get hue" );
|
2003-06-25 17:47:09 +08:00
|
|
|
return( -1 );
|
2007-04-03 17:47:08 +08:00
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return( shared_data->hue );
|
|
|
|
}
|
|
|
|
return( camera->Hue( p_hue ) );
|
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
int Monitor::actionColour( int p_colour )
|
2003-06-25 17:47:09 +08:00
|
|
|
{
|
2003-09-23 17:52:45 +08:00
|
|
|
if ( purpose != CAPTURE )
|
2003-06-25 17:47:09 +08:00
|
|
|
{
|
|
|
|
if ( p_colour >= 0 )
|
|
|
|
{
|
|
|
|
shared_data->colour = p_colour;
|
|
|
|
shared_data->action |= SET_SETTINGS;
|
|
|
|
int wait_loops = 10;
|
|
|
|
while ( shared_data->action & SET_SETTINGS )
|
|
|
|
{
|
|
|
|
if ( wait_loops-- )
|
|
|
|
usleep( 100000 );
|
|
|
|
else
|
2007-04-03 17:47:08 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Timed out waiting to set colour" );
|
2003-06-25 17:47:09 +08:00
|
|
|
return( -1 );
|
2007-04-03 17:47:08 +08:00
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shared_data->action |= GET_SETTINGS;
|
|
|
|
int wait_loops = 10;
|
|
|
|
while ( shared_data->action & GET_SETTINGS )
|
|
|
|
{
|
|
|
|
if ( wait_loops-- )
|
|
|
|
usleep( 100000 );
|
|
|
|
else
|
2007-04-03 17:47:08 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Timed out waiting to get colour" );
|
2003-06-25 17:47:09 +08:00
|
|
|
return( -1 );
|
2007-04-03 17:47:08 +08:00
|
|
|
}
|
2003-06-25 17:47:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return( shared_data->colour );
|
|
|
|
}
|
|
|
|
return( camera->Colour( p_colour ) );
|
|
|
|
}
|
|
|
|
|
2005-11-30 01:51:06 +08:00
|
|
|
void Monitor::DumpZoneImage( const char *zone_string )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2005-11-30 01:51:06 +08:00
|
|
|
int exclude_id = 0;
|
|
|
|
int extra_colour = 0;
|
|
|
|
Polygon extra_zone;
|
|
|
|
|
|
|
|
if ( zone_string )
|
|
|
|
{
|
|
|
|
if ( !Zone::ParseZoneString( zone_string, exclude_id, extra_colour, extra_zone ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Failed to parse zone string, ignoring" );
|
2005-11-30 01:51:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-12 22:29:54 +08:00
|
|
|
int index = shared_data->last_write_index;
|
2003-03-26 19:57:29 +08:00
|
|
|
Snapshot *snap = &image_buffer[index];
|
2004-01-15 05:26:47 +08:00
|
|
|
Image *snap_image = snap->image;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2004-01-15 05:26:47 +08:00
|
|
|
Image zone_image( *snap_image );
|
2003-03-26 19:57:29 +08:00
|
|
|
zone_image.Colourise();
|
|
|
|
for( int i = 0; i < n_zones; i++ )
|
|
|
|
{
|
2005-11-30 01:51:06 +08:00
|
|
|
if ( exclude_id && zones[i]->Id() == exclude_id )
|
|
|
|
continue;
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
Rgb colour;
|
|
|
|
if ( zones[i]->IsActive() )
|
|
|
|
{
|
|
|
|
colour = RGB_RED;
|
|
|
|
}
|
|
|
|
else if ( zones[i]->IsInclusive() )
|
|
|
|
{
|
2005-11-30 01:51:06 +08:00
|
|
|
colour = RGB_ORANGE;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
else if ( zones[i]->IsExclusive() )
|
|
|
|
{
|
2005-11-30 01:51:06 +08:00
|
|
|
colour = RGB_PURPLE;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2003-07-07 19:20:06 +08:00
|
|
|
else if ( zones[i]->IsPreclusive() )
|
|
|
|
{
|
2005-11-30 01:51:06 +08:00
|
|
|
colour = RGB_BLUE;
|
2003-07-07 19:20:06 +08:00
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
colour = RGB_WHITE;
|
|
|
|
}
|
2005-11-30 01:51:06 +08:00
|
|
|
zone_image.Fill( colour, 2, zones[i]->GetPolygon() );
|
2005-12-02 00:34:20 +08:00
|
|
|
zone_image.Outline( colour, zones[i]->GetPolygon() );
|
2005-11-30 01:51:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( extra_zone.getNumCoords() )
|
|
|
|
{
|
|
|
|
zone_image.Fill( extra_colour, 2, extra_zone );
|
|
|
|
zone_image.Outline( extra_colour, extra_zone );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2005-11-30 01:51:06 +08:00
|
|
|
|
2004-02-16 04:07:16 +08:00
|
|
|
static char filename[PATH_MAX];
|
2008-07-14 21:54:50 +08:00
|
|
|
snprintf( filename, sizeof(filename), "Zones%d.jpg", id );
|
2003-03-26 19:57:29 +08:00
|
|
|
zone_image.WriteJpeg( filename );
|
|
|
|
}
|
|
|
|
|
2004-01-15 05:26:47 +08:00
|
|
|
void Monitor::DumpImage( Image *dump_image ) const
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
|
|
|
if ( image_count && !(image_count%10) )
|
|
|
|
{
|
2004-02-16 04:07:16 +08:00
|
|
|
static char filename[PATH_MAX];
|
2004-04-20 00:02:17 +08:00
|
|
|
static char new_filename[PATH_MAX];
|
|
|
|
snprintf( filename, sizeof(filename), "%s.jpg", name );
|
|
|
|
snprintf( new_filename, sizeof(new_filename), "%s-new.jpg", name );
|
2004-01-15 05:26:47 +08:00
|
|
|
dump_image->WriteJpeg( new_filename );
|
2003-03-26 19:57:29 +08:00
|
|
|
rename( new_filename, filename );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-15 06:47:02 +08:00
|
|
|
bool Monitor::CheckSignal( const Image *image )
|
|
|
|
{
|
|
|
|
static bool static_undef = true;
|
|
|
|
static unsigned char red_val;
|
|
|
|
static unsigned char green_val;
|
|
|
|
static unsigned char blue_val;
|
|
|
|
|
|
|
|
if ( camera->IsLocal() && config.signal_check_points > 0 )
|
|
|
|
{
|
|
|
|
if ( static_undef )
|
|
|
|
{
|
|
|
|
static_undef = false;
|
2007-09-05 23:05:28 +08:00
|
|
|
red_val = RGB_RED_VAL(signal_check_colour);
|
|
|
|
green_val = RGB_GREEN_VAL(signal_check_colour);
|
|
|
|
blue_val = RGB_BLUE_VAL(signal_check_colour);
|
2006-01-15 06:47:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char *buffer = image->Buffer();
|
|
|
|
int pixels = image->Pixels();
|
|
|
|
int colours = image->Colours();
|
|
|
|
|
|
|
|
for ( int i = 0; i < config.signal_check_points; i++ )
|
|
|
|
{
|
|
|
|
int index = (rand()*pixels)/RAND_MAX;
|
|
|
|
const unsigned char *ptr = buffer+(index*colours);
|
|
|
|
if ( (RED(ptr) != red_val) || (GREEN(ptr) != green_val) || (BLUE(ptr) != blue_val) )
|
|
|
|
{
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
bool Monitor::Analyse()
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
if ( shared_data->last_read_index == shared_data->last_write_index )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval now;
|
2008-03-13 21:36:12 +08:00
|
|
|
gettimeofday( &now, NULL );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
|
|
|
if ( image_count && !(image_count%fps_report_interval) )
|
|
|
|
{
|
|
|
|
fps = double(fps_report_interval)/(now.tv_sec-last_fps_time);
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %d - Processing at %.2f fps", name, image_count, fps );
|
2003-03-26 19:57:29 +08:00
|
|
|
last_fps_time = now.tv_sec;
|
|
|
|
}
|
|
|
|
|
2003-04-22 22:25:31 +08:00
|
|
|
int index;
|
2005-05-16 17:27:06 +08:00
|
|
|
if ( config.opt_adaptive_skip )
|
2003-04-17 23:39:44 +08:00
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
int read_margin = shared_data->last_read_index - shared_data->last_write_index;
|
2003-04-22 22:16:50 +08:00
|
|
|
if ( read_margin < 0 ) read_margin += image_buffer_count;
|
2003-04-17 23:39:44 +08:00
|
|
|
|
2003-04-22 22:16:50 +08:00
|
|
|
int step = 1;
|
|
|
|
if ( read_margin > 0 )
|
|
|
|
{
|
|
|
|
step = (9*image_buffer_count)/(5*read_margin);
|
|
|
|
}
|
2003-04-21 04:52:14 +08:00
|
|
|
|
2003-06-12 22:29:54 +08:00
|
|
|
int pending_frames = shared_data->last_write_index - shared_data->last_read_index;
|
2003-04-22 22:16:50 +08:00
|
|
|
if ( pending_frames < 0 ) pending_frames += image_buffer_count;
|
|
|
|
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 4, "RI:%d, WI: %d, PF = %d, RM = %d, Step = %d", shared_data->last_read_index, shared_data->last_write_index, pending_frames, read_margin, step );
|
2003-04-22 22:16:50 +08:00
|
|
|
if ( step <= pending_frames )
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
index = (shared_data->last_read_index+step)%image_buffer_count;
|
2003-04-22 22:16:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( pending_frames )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Approaching buffer overrun, consider slowing capture, simplifying analysis or increasing ring buffer size" );
|
2003-04-22 22:16:50 +08:00
|
|
|
}
|
2003-06-12 22:29:54 +08:00
|
|
|
index = shared_data->last_write_index%image_buffer_count;
|
2003-04-22 22:16:50 +08:00
|
|
|
}
|
2003-04-17 23:39:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
index = shared_data->last_write_index%image_buffer_count;
|
2003-04-17 23:39:44 +08:00
|
|
|
}
|
2003-04-22 22:16:50 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
Snapshot *snap = &image_buffer[index];
|
|
|
|
struct timeval *timestamp = snap->timestamp;
|
2004-01-15 05:26:47 +08:00
|
|
|
Image *snap_image = snap->image;
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( shared_data->action )
|
2005-02-24 18:43:29 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( shared_data->action & RELOAD )
|
2005-02-24 18:43:29 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "Received reload indication at count %d", image_count );
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->action &= ~RELOAD;
|
|
|
|
Reload();
|
2005-02-24 18:43:29 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( shared_data->action & SUSPEND )
|
2004-12-29 01:41:55 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( Active() )
|
2004-02-20 17:47:49 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "Received suspend indication at count %d", image_count );
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->active = false;
|
2006-01-02 04:30:36 +08:00
|
|
|
//closeEvent();
|
2004-02-20 17:47:49 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( config.max_suspend_time )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
auto_resume_time = now.tv_sec + config.max_suspend_time;
|
2003-09-23 17:52:45 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->action &= ~SUSPEND;
|
2003-09-23 17:52:45 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( shared_data->action & RESUME )
|
2003-09-23 17:52:45 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( Enabled() && !Active() )
|
2005-02-24 18:43:29 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "Received resume indication at count %d", image_count );
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->active = true;
|
|
|
|
ref_image = *snap_image;
|
|
|
|
ready_count = image_count+(warmup_count/2);
|
|
|
|
shared_data->alarm_x = shared_data->alarm_y = -1;
|
2005-02-24 18:43:29 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->action &= ~RESUME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( auto_resume_time && (now.tv_sec >= auto_resume_time) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "Auto resuming at count %d", image_count );
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->active = true;
|
|
|
|
ref_image = *snap_image;
|
|
|
|
ready_count = image_count+(warmup_count/2);
|
|
|
|
auto_resume_time = 0;
|
|
|
|
}
|
|
|
|
|
2006-01-15 06:47:02 +08:00
|
|
|
static bool static_undef = true;
|
2005-12-23 00:46:25 +08:00
|
|
|
static struct timeval **timestamps;
|
|
|
|
static Image **images;
|
|
|
|
static int last_section_mod = 0;
|
2006-01-15 06:47:02 +08:00
|
|
|
static bool last_signal;
|
|
|
|
|
|
|
|
if ( static_undef )
|
|
|
|
{
|
|
|
|
static_undef = false;
|
|
|
|
timestamps = new struct timeval *[pre_event_count];
|
|
|
|
images = new Image *[pre_event_count];
|
|
|
|
last_signal = shared_data->signal;
|
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
|
|
|
|
if ( Enabled() )
|
|
|
|
{
|
2006-01-15 06:47:02 +08:00
|
|
|
bool signal = shared_data->signal;
|
|
|
|
bool signal_change = (signal != last_signal);
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( trigger_data->trigger_state != TRIGGER_OFF )
|
|
|
|
{
|
|
|
|
unsigned int score = 0;
|
|
|
|
if ( Ready() )
|
2003-09-23 17:52:45 +08:00
|
|
|
{
|
2008-10-06 03:07:43 +08:00
|
|
|
std::string cause;
|
|
|
|
Event::StringSetMap noteSetMap;
|
2006-01-23 03:38:41 +08:00
|
|
|
|
|
|
|
if ( trigger_data->trigger_state == TRIGGER_ON )
|
|
|
|
{
|
|
|
|
score += trigger_data->trigger_score;
|
|
|
|
if ( !event )
|
|
|
|
{
|
2008-10-06 03:07:43 +08:00
|
|
|
if ( cause.length() )
|
|
|
|
cause += ", ";
|
|
|
|
cause += trigger_data->trigger_cause;
|
2006-01-23 03:38:41 +08:00
|
|
|
}
|
2008-10-06 03:07:43 +08:00
|
|
|
Event::StringSet noteSet;
|
|
|
|
noteSet.insert( trigger_data->trigger_text );
|
|
|
|
noteSetMap[trigger_data->trigger_cause] = noteSet;
|
2006-01-23 03:38:41 +08:00
|
|
|
}
|
2006-01-15 06:47:02 +08:00
|
|
|
if ( signal_change )
|
|
|
|
{
|
2006-01-23 03:38:41 +08:00
|
|
|
score += 100;
|
2008-10-06 03:07:43 +08:00
|
|
|
const char *signalText;
|
2006-01-15 06:47:02 +08:00
|
|
|
if ( !signal )
|
2008-10-06 03:07:43 +08:00
|
|
|
signalText = "Lost";
|
2006-01-15 06:47:02 +08:00
|
|
|
else
|
2008-10-06 03:07:43 +08:00
|
|
|
signalText = "Reacquired";
|
|
|
|
Warning( "%s: %s", SIGNAL_CAUSE, signalText );
|
2006-01-15 06:47:02 +08:00
|
|
|
if ( event )
|
|
|
|
{
|
|
|
|
closeEvent();
|
|
|
|
shared_data->state = state = IDLE;
|
|
|
|
last_section_mod = 0;
|
|
|
|
}
|
2006-01-23 03:38:41 +08:00
|
|
|
if ( !event )
|
|
|
|
{
|
2008-10-06 03:07:43 +08:00
|
|
|
if ( cause.length() )
|
|
|
|
cause += ", ";
|
|
|
|
cause += SIGNAL_CAUSE;
|
2006-01-23 03:38:41 +08:00
|
|
|
}
|
2008-10-06 03:07:43 +08:00
|
|
|
Event::StringSet noteSet;
|
|
|
|
noteSet.insert( signalText );
|
|
|
|
noteSetMap[SIGNAL_CAUSE] = noteSet;
|
2006-01-15 06:47:02 +08:00
|
|
|
shared_data->active = signal;
|
|
|
|
ref_image = *snap_image;
|
|
|
|
}
|
2006-01-23 03:38:41 +08:00
|
|
|
else if ( Active() && function != RECORD && function != NODECT )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-10-06 03:07:43 +08:00
|
|
|
Event::StringSet zoneSet;
|
|
|
|
int motion_score = DetectMotion( *snap_image, zoneSet );
|
|
|
|
if ( motion_score )
|
2006-01-23 03:38:41 +08:00
|
|
|
{
|
2008-10-06 03:07:43 +08:00
|
|
|
if ( !event )
|
|
|
|
{
|
2006-01-23 03:38:41 +08:00
|
|
|
score += motion_score;
|
2008-10-06 03:07:43 +08:00
|
|
|
if ( cause.length() )
|
|
|
|
cause += ", ";
|
|
|
|
cause += MOTION_CAUSE;
|
2006-01-23 03:38:41 +08:00
|
|
|
}
|
2008-10-06 03:07:43 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
score += motion_score;
|
|
|
|
}
|
|
|
|
noteSetMap[MOTION_CAUSE] = zoneSet;
|
|
|
|
}
|
2006-01-23 03:38:41 +08:00
|
|
|
shared_data->active = signal;
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
2006-01-23 03:38:41 +08:00
|
|
|
if ( n_linked_monitors > 0 )
|
2006-01-23 02:31:55 +08:00
|
|
|
{
|
2006-01-23 03:38:41 +08:00
|
|
|
bool first_link = true;
|
2008-10-06 03:07:43 +08:00
|
|
|
Event::StringSet noteSet;
|
2006-01-23 02:31:55 +08:00
|
|
|
for ( int i = 0; i < n_linked_monitors; i++ )
|
|
|
|
{
|
|
|
|
if ( linked_monitors[i]->isConnected() )
|
|
|
|
{
|
|
|
|
if ( linked_monitors[i]->hasAlarmed() )
|
|
|
|
{
|
2006-01-23 03:38:41 +08:00
|
|
|
if ( !event )
|
2006-01-23 02:31:55 +08:00
|
|
|
{
|
2006-01-23 03:38:41 +08:00
|
|
|
if ( first_link )
|
|
|
|
{
|
2008-10-06 03:07:43 +08:00
|
|
|
if ( cause.length() )
|
|
|
|
cause += ", ";
|
|
|
|
cause += LINKED_CAUSE;
|
2006-01-23 03:38:41 +08:00
|
|
|
first_link = false;
|
|
|
|
}
|
2006-01-23 02:31:55 +08:00
|
|
|
}
|
2008-10-06 03:07:43 +08:00
|
|
|
noteSet.insert( linked_monitors[i]->Name() );
|
2006-01-23 02:31:55 +08:00
|
|
|
score += 50;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
linked_monitors[i]->connect();
|
|
|
|
}
|
|
|
|
}
|
2008-10-06 03:07:43 +08:00
|
|
|
if ( noteSet.size() > 0 )
|
|
|
|
noteSetMap[LINKED_CAUSE] = noteSet;
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
2006-01-15 06:47:02 +08:00
|
|
|
if ( (!signal_change && signal) && (function == RECORD || function == MOCORD) )
|
2005-12-23 00:46:25 +08:00
|
|
|
{
|
|
|
|
if ( event )
|
2003-09-23 17:52:45 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
int section_mod = timestamp->tv_sec%section_length;
|
|
|
|
if ( section_mod < last_section_mod )
|
2004-09-25 20:37:45 +08:00
|
|
|
{
|
2008-09-25 19:18:55 +08:00
|
|
|
if ( state == IDLE || state == TAPE || event_close_mode == CLOSE_TIME )
|
2005-12-23 00:46:25 +08:00
|
|
|
{
|
|
|
|
if ( state == IDLE || state == TAPE )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Ending event %d", name, image_count, event->Id() );
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Force closing event %d", name, image_count, event->Id() );
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
closeEvent();
|
|
|
|
last_section_mod = 0;
|
|
|
|
}
|
2004-09-25 20:37:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
last_section_mod = section_mod;
|
2004-09-25 20:37:45 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
if ( !event )
|
|
|
|
{
|
2005-12-27 22:40:15 +08:00
|
|
|
shared_data->state = state = TAPE;
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
// Create event
|
2008-10-06 03:07:43 +08:00
|
|
|
event = new Event( this, *timestamp, "Continuous", noteSetMap );
|
2005-12-23 06:51:22 +08:00
|
|
|
shared_data->last_event = event->Id();
|
2004-09-25 20:37:45 +08:00
|
|
|
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Starting new event %d", name, image_count, event->Id() );
|
2004-09-25 20:37:45 +08:00
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
//if ( config.overlap_timed_events )
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( false )
|
2004-09-25 20:37:45 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
int pre_index = ((index+image_buffer_count)-pre_event_count)%image_buffer_count;
|
|
|
|
for ( int i = 0; i < pre_event_count; i++ )
|
|
|
|
{
|
|
|
|
timestamps[i] = image_buffer[pre_index].timestamp;
|
|
|
|
images[i] = image_buffer[pre_index].image;
|
|
|
|
|
|
|
|
pre_index = (pre_index+1)%image_buffer_count;
|
|
|
|
}
|
|
|
|
event->AddFrames( pre_event_count, images, timestamps );
|
2004-09-25 20:37:45 +08:00
|
|
|
}
|
2003-09-23 17:52:45 +08:00
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( score )
|
2004-09-25 20:37:45 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( (state == IDLE || state == TAPE || state == PREALARM ) )
|
2003-09-23 17:52:45 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( Event::PreAlarmCount() >= (alarm_frame_count-1) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Gone into alarm state", name, image_count );
|
2005-12-27 22:40:15 +08:00
|
|
|
shared_data->state = state = ALARM;
|
2006-01-15 06:47:02 +08:00
|
|
|
if ( signal_change || (function != MOCORD && state != ALERT) )
|
2005-12-23 00:46:25 +08:00
|
|
|
{
|
|
|
|
int pre_index;
|
|
|
|
if ( alarm_frame_count > 1 )
|
|
|
|
pre_index = ((index+image_buffer_count)-((alarm_frame_count-1)+pre_event_count))%image_buffer_count;
|
|
|
|
else
|
|
|
|
pre_index = ((index+image_buffer_count)-pre_event_count)%image_buffer_count;
|
2008-09-26 17:45:59 +08:00
|
|
|
|
2008-10-06 03:07:43 +08:00
|
|
|
event = new Event( this, *(image_buffer[pre_index].timestamp), cause, noteSetMap );
|
2005-12-23 06:51:22 +08:00
|
|
|
shared_data->last_event = event->Id();
|
2005-12-23 00:46:25 +08:00
|
|
|
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Creating new event %d", name, image_count, event->Id() );
|
2007-06-05 22:12:14 +08:00
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
for ( int i = 0; i < pre_event_count; i++ )
|
|
|
|
{
|
|
|
|
timestamps[i] = image_buffer[pre_index].timestamp;
|
|
|
|
images[i] = image_buffer[pre_index].image;
|
|
|
|
|
|
|
|
pre_index = (pre_index+1)%image_buffer_count;
|
|
|
|
}
|
|
|
|
event->AddFrames( pre_event_count, images, timestamps );
|
|
|
|
if ( alarm_frame_count )
|
|
|
|
{
|
|
|
|
event->SavePreAlarmFrames();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( state != PREALARM )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Gone into prealarm state", name, image_count );
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->state = state = PREALARM;
|
|
|
|
}
|
2003-09-23 17:52:45 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
else if ( state == ALERT )
|
2003-09-23 17:52:45 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Gone back into alarm state", name, image_count );
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->state = state = ALARM;
|
2003-09-23 17:52:45 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
last_alarm_count = image_count;
|
2004-09-25 20:37:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( state == ALARM )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Gone into alert state", name, image_count );
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->state = state = ALERT;
|
|
|
|
}
|
|
|
|
else if ( state == ALERT )
|
|
|
|
{
|
|
|
|
if ( image_count-last_alarm_count > post_event_count )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Left alarm state (%d) - %d(%d) images", name, image_count, event->Id(), event->Frames(), event->AlarmFrames() );
|
2008-10-06 03:07:43 +08:00
|
|
|
if ( function != MOCORD || event_close_mode == CLOSE_ALARM || event->Cause() != "Signal" )
|
2004-03-30 19:05:47 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->state = state = IDLE;
|
|
|
|
delete event;
|
|
|
|
event = 0;
|
2004-03-30 19:05:47 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
else
|
2003-11-21 18:38:41 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->state = state = TAPE;
|
2003-11-21 18:38:41 +08:00
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( state == PREALARM )
|
2004-03-30 19:05:47 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( function != MOCORD )
|
|
|
|
{
|
|
|
|
shared_data->state = state = IDLE;
|
|
|
|
}
|
2004-09-25 20:37:45 +08:00
|
|
|
else
|
2005-12-23 00:46:25 +08:00
|
|
|
{
|
|
|
|
shared_data->state = state = TAPE;
|
|
|
|
}
|
2004-03-30 19:05:47 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( Event::PreAlarmCount() )
|
|
|
|
Event::EmptyPreAlarmFrames();
|
|
|
|
}
|
|
|
|
if ( state != IDLE )
|
|
|
|
{
|
|
|
|
if ( state == PREALARM || state == ALARM )
|
2004-03-30 19:05:47 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( config.create_analysis_images )
|
|
|
|
{
|
|
|
|
bool got_anal_image = false;
|
|
|
|
Image alarm_image( *snap_image );
|
|
|
|
for( int i = 0; i < n_zones; i++ )
|
|
|
|
{
|
|
|
|
if ( zones[i]->Alarmed() )
|
|
|
|
{
|
|
|
|
if ( zones[i]->AlarmImage() )
|
|
|
|
{
|
|
|
|
alarm_image.Overlay( *(zones[i]->AlarmImage()) );
|
|
|
|
got_anal_image = true;
|
|
|
|
}
|
|
|
|
if ( config.record_event_stats && state == ALARM )
|
|
|
|
{
|
|
|
|
zones[i]->RecordStats( event );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( got_anal_image )
|
|
|
|
{
|
|
|
|
if ( state == PREALARM )
|
|
|
|
Event::AddPreAlarmFrame( snap_image, *timestamp, score, &alarm_image );
|
|
|
|
else
|
|
|
|
event->AddFrame( snap_image, *timestamp, score, &alarm_image );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( state == PREALARM )
|
|
|
|
Event::AddPreAlarmFrame( snap_image, *timestamp, score );
|
|
|
|
else
|
|
|
|
event->AddFrame( snap_image, *timestamp, score );
|
|
|
|
}
|
|
|
|
}
|
2004-09-25 20:37:45 +08:00
|
|
|
else
|
2005-12-23 00:46:25 +08:00
|
|
|
{
|
|
|
|
for( int i = 0; i < n_zones; i++ )
|
|
|
|
{
|
|
|
|
if ( zones[i]->Alarmed() )
|
|
|
|
{
|
|
|
|
if ( config.record_event_stats && state == ALARM )
|
|
|
|
{
|
|
|
|
zones[i]->RecordStats( event );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( state == PREALARM )
|
|
|
|
Event::AddPreAlarmFrame( snap_image, *timestamp, score );
|
|
|
|
else
|
|
|
|
event->AddFrame( snap_image, *timestamp, score );
|
|
|
|
}
|
2008-10-06 03:07:43 +08:00
|
|
|
if ( event && noteSetMap.size() > 0 )
|
|
|
|
event->updateNotes( noteSetMap );
|
2004-03-30 19:05:47 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
else if ( state == ALERT )
|
|
|
|
{
|
2008-09-26 17:45:59 +08:00
|
|
|
event->AddFrame( snap_image, *timestamp );
|
2008-10-06 03:07:43 +08:00
|
|
|
if ( noteSetMap.size() > 0 )
|
|
|
|
event->updateNotes( noteSetMap );
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
else if ( state == TAPE )
|
2005-11-20 20:17:45 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( !(image_count%(frame_skip+1)) )
|
2005-11-20 20:17:45 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( config.bulk_frame_interval > 1 )
|
|
|
|
{
|
2008-10-28 20:52:09 +08:00
|
|
|
event->AddFrame( snap_image, *timestamp, (event->Frames()<pre_event_count?0:-1) );
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
else
|
2005-11-20 20:17:45 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
event->AddFrame( snap_image, *timestamp );
|
2005-11-20 20:17:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( function == RECORD || function == MOCORD )
|
2003-10-08 22:49:26 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( state == IDLE || state == TAPE )
|
2004-03-04 23:13:10 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
int section_mod = timestamp->tv_sec%section_length;
|
|
|
|
if ( section_mod < last_section_mod )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Ending event %d", name, image_count, event->Id() );
|
2005-12-23 00:46:25 +08:00
|
|
|
closeEvent();
|
|
|
|
last_section_mod = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
last_section_mod = section_mod;
|
|
|
|
}
|
2004-03-04 23:13:10 +08:00
|
|
|
}
|
2003-10-08 22:49:26 +08:00
|
|
|
}
|
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2006-11-16 19:34:53 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( event )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "%s: %03d - Closing event %d", name, image_count, event->Id() );
|
2006-11-16 19:34:53 +08:00
|
|
|
closeEvent();
|
|
|
|
}
|
|
|
|
shared_data->state = state = IDLE;
|
|
|
|
last_section_mod = 0;
|
|
|
|
}
|
2006-01-15 06:47:02 +08:00
|
|
|
if ( !signal_change && (function == MODECT || function == MOCORD) && (config.blend_alarmed_images || state != ALARM) )
|
2003-09-23 17:52:45 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
ref_image.Blend( *snap_image, ref_blend_perc );
|
2003-09-23 17:52:45 +08:00
|
|
|
}
|
2006-01-15 06:47:02 +08:00
|
|
|
last_signal = signal;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2003-03-27 17:31:13 +08:00
|
|
|
|
2003-06-12 22:29:54 +08:00
|
|
|
shared_data->last_read_index = index%image_buffer_count;
|
2008-07-21 23:20:52 +08:00
|
|
|
shared_data->last_read_time = image_buffer[index].timestamp->tv_sec;
|
2003-03-26 19:57:29 +08:00
|
|
|
image_count++;
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
void Monitor::Reload()
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Reloading monitor %s", name );
|
2005-12-23 00:46:25 +08:00
|
|
|
|
2006-01-15 06:47:02 +08:00
|
|
|
closeEvent();
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
static char sql[BUFSIZ];
|
2007-09-05 23:05:28 +08:00
|
|
|
snprintf( sql, sizeof(sql), "select Function+0, Enabled, LinkedMonitors, EventPrefix, LabelFormat, LabelX, LabelY, WarmupCount, PreEventCount, PostEventCount, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion, SignalCheckColour from Monitors where Id = '%d'", id );
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2005-12-23 00:46:25 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
2005-12-23 00:46:25 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
int n_monitors = mysql_num_rows( result );
|
|
|
|
if ( n_monitors != 1 )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Bogus number of monitors, %d, returned. Can't reload", n_monitors );
|
2005-12-23 00:46:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2006-01-15 06:47:02 +08:00
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( MYSQL_ROW dbrow = mysql_fetch_row( result ) )
|
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
function = (Function)atoi(dbrow[index++]);
|
|
|
|
enabled = atoi(dbrow[index++]);
|
2006-01-23 02:31:55 +08:00
|
|
|
const char *p_linked_monitors = dbrow[index++];
|
2005-12-23 00:46:25 +08:00
|
|
|
strncpy( event_prefix, dbrow[index++], sizeof(event_prefix) );
|
|
|
|
strncpy( label_format, dbrow[index++], sizeof(label_format) );
|
|
|
|
label_coord = Coord( atoi(dbrow[index]), atoi(dbrow[index+1]) ); index += 2;
|
|
|
|
warmup_count = atoi(dbrow[index++]);
|
|
|
|
pre_event_count = atoi(dbrow[index++]);
|
|
|
|
post_event_count = atoi(dbrow[index++]);
|
|
|
|
alarm_frame_count = atoi(dbrow[index++]);
|
|
|
|
section_length = atoi(dbrow[index++]);
|
|
|
|
frame_skip = atoi(dbrow[index++]);
|
2006-03-24 02:21:26 +08:00
|
|
|
capture_delay = (dbrow[index]&&atof(dbrow[index])>0.0)?int(DT_PREC_3/atof(dbrow[index])):0; index++;
|
2006-03-31 20:49:05 +08:00
|
|
|
alarm_capture_delay = (dbrow[index]&&atof(dbrow[index])>0.0)?int(DT_PREC_3/atof(dbrow[index])):0; index++;
|
2005-12-23 00:46:25 +08:00
|
|
|
fps_report_interval = atoi(dbrow[index++]);
|
|
|
|
ref_blend_perc = atoi(dbrow[index++]);
|
|
|
|
track_motion = atoi(dbrow[index++]);
|
2007-09-05 23:05:28 +08:00
|
|
|
if ( dbrow[index][0] == '#' )
|
|
|
|
signal_check_colour = strtol(dbrow[index]+1,0,16);
|
|
|
|
else
|
|
|
|
signal_check_colour = strtol(dbrow[index],0,16);
|
|
|
|
index++;
|
2005-12-23 00:46:25 +08:00
|
|
|
|
|
|
|
shared_data->state = state = IDLE;
|
|
|
|
shared_data->alarm_x = shared_data->alarm_y = -1;
|
|
|
|
if ( enabled )
|
|
|
|
shared_data->active = true;
|
|
|
|
ready_count = image_count+warmup_count;
|
2006-01-23 02:31:55 +08:00
|
|
|
|
|
|
|
ReloadLinkedMonitors( p_linked_monitors );
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
if ( mysql_errno( &dbconn ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
|
2005-12-23 00:46:25 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
mysql_free_result( result );
|
|
|
|
|
|
|
|
ReloadZones();
|
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
void Monitor::ReloadZones()
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Reloading zones for monitor %s", name );
|
2003-03-26 19:57:29 +08:00
|
|
|
for( int i = 0; i < n_zones; i++ )
|
|
|
|
{
|
|
|
|
delete zones[i];
|
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
delete[] zones;
|
|
|
|
zones = 0;
|
2003-03-26 19:57:29 +08:00
|
|
|
n_zones = Zone::Load( this, zones );
|
2005-12-23 00:46:25 +08:00
|
|
|
//DumpZoneImage();
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2006-01-23 02:31:55 +08:00
|
|
|
void Monitor::ReloadLinkedMonitors( const char *p_linked_monitors )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Reloading linked monitors for monitor %s, '%s'", name, p_linked_monitors );
|
2006-01-23 02:31:55 +08:00
|
|
|
if ( n_linked_monitors )
|
|
|
|
{
|
2008-02-11 06:14:32 +08:00
|
|
|
for( int i = 0; i < n_linked_monitors; i++ )
|
2006-01-23 02:31:55 +08:00
|
|
|
{
|
|
|
|
delete linked_monitors[i];
|
|
|
|
}
|
|
|
|
delete[] linked_monitors;
|
|
|
|
linked_monitors = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n_linked_monitors = 0;
|
|
|
|
if ( p_linked_monitors )
|
|
|
|
{
|
|
|
|
int n_link_ids = 0;
|
|
|
|
int link_ids[256];
|
|
|
|
|
|
|
|
char link_id_str[8];
|
|
|
|
char *dest_ptr = link_id_str;
|
|
|
|
const char *src_ptr = p_linked_monitors;
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
dest_ptr = link_id_str;
|
|
|
|
while( *src_ptr >= '0' && *src_ptr <= '9' )
|
|
|
|
{
|
|
|
|
if ( (dest_ptr-link_id_str) < (sizeof(link_id_str)-1) )
|
|
|
|
{
|
|
|
|
*dest_ptr++ = *src_ptr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add the link monitor
|
|
|
|
if ( dest_ptr != link_id_str )
|
|
|
|
{
|
|
|
|
*dest_ptr = '\0';
|
|
|
|
int link_id = atoi(link_id_str);
|
|
|
|
if ( link_id > 0 )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Found linked monitor id %d", link_id );
|
2006-01-23 02:31:55 +08:00
|
|
|
int j;
|
|
|
|
for ( j = 0; j < n_link_ids; j++ )
|
|
|
|
{
|
|
|
|
if ( link_ids[j] == link_id )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( j == n_link_ids ) // Not already found
|
|
|
|
{
|
|
|
|
link_ids[n_link_ids++] = link_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !*src_ptr )
|
|
|
|
break;
|
|
|
|
while( *src_ptr && (*src_ptr < '0' || *src_ptr > '9') )
|
|
|
|
src_ptr++;
|
|
|
|
if ( !*src_ptr )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( n_link_ids > 0 )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Linking to %d monitors", n_link_ids );
|
2006-01-23 02:31:55 +08:00
|
|
|
n_linked_monitors = n_link_ids;
|
|
|
|
linked_monitors = new MonitorLink *[n_linked_monitors];
|
|
|
|
for ( int i = 0; i < n_linked_monitors; i++ )
|
|
|
|
{
|
|
|
|
static char sql[BUFSIZ];
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Checking linked monitor %d", link_ids[i] );
|
2006-01-23 02:31:55 +08:00
|
|
|
|
|
|
|
snprintf( sql, sizeof(sql), "select Id, Name from Monitors where Id = %d and Function != 'None' and Function != 'Monitor' and Enabled = 1", link_ids[i] );
|
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2006-01-23 02:31:55 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
2006-01-23 02:31:55 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
int n_monitors = mysql_num_rows( result );
|
|
|
|
if ( n_monitors == 1 )
|
|
|
|
{
|
|
|
|
MYSQL_ROW dbrow = mysql_fetch_row( result );
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Linking to monitor %d", link_ids[i] );
|
2006-01-23 02:31:55 +08:00
|
|
|
linked_monitors[i] = new MonitorLink( link_ids[i], dbrow[1] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Can't link to monitor %d, invalid id, function or not enabled", link_ids[i] );
|
2006-01-23 02:31:55 +08:00
|
|
|
}
|
|
|
|
mysql_free_result( result );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-18 05:55:02 +08:00
|
|
|
int Monitor::LoadLocalMonitors( const char *device, Monitor **&monitors, Purpose purpose )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-07-04 04:39:47 +08:00
|
|
|
static char sql[BUFSIZ];
|
2005-10-17 18:58:25 +08:00
|
|
|
if ( !device[0] )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-07-21 23:20:52 +08:00
|
|
|
strncpy( sql, "select Id, Name, Function+0, Enabled, LinkedMonitors, Device, Channel, Format, Method, Width, Height, Palette, Orientation+0, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion, SignalCheckColour from Monitors where Function != 'None' and Type = 'Local' order by Device, Channel", sizeof(sql) );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-21 23:20:52 +08:00
|
|
|
snprintf( sql, sizeof(sql), "select Id, Name, Function+0, Enabled, LinkedMonitors, Device, Channel, Format, Method, Width, Height, Palette, Orientation+0, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion, SignalCheckColour from Monitors where Function != 'None' and Type = 'Local' and Device = '%s' order by Channel", device );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
int n_monitors = mysql_num_rows( result );
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got %d monitors", n_monitors );
|
2003-03-26 19:57:29 +08:00
|
|
|
delete[] monitors;
|
|
|
|
monitors = new Monitor *[n_monitors];
|
|
|
|
for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )
|
|
|
|
{
|
2006-01-23 02:31:55 +08:00
|
|
|
int col = 0;
|
|
|
|
|
|
|
|
int id = atoi(dbrow[col]); col++;
|
|
|
|
const char *name = dbrow[col]; col++;
|
|
|
|
int function = atoi(dbrow[col]); col++;
|
|
|
|
int enabled = atoi(dbrow[col]); col++;
|
|
|
|
const char *linked_monitors = dbrow[col]; col++;
|
|
|
|
|
|
|
|
const char *device = dbrow[col]; col++;
|
|
|
|
int channel = atoi(dbrow[col]); col++;
|
|
|
|
int format = atoi(dbrow[col]); col++;
|
2008-07-21 23:20:52 +08:00
|
|
|
const char *method = dbrow[col]; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
|
|
|
|
int width = atoi(dbrow[col]); col++;
|
|
|
|
int height = atoi(dbrow[col]); col++;
|
|
|
|
int palette = atoi(dbrow[col]); col++;
|
|
|
|
Orientation orientation = (Orientation)atoi(dbrow[col]); col++;
|
|
|
|
int brightness = atoi(dbrow[col]); col++;
|
|
|
|
int contrast = atoi(dbrow[col]); col++;
|
|
|
|
int hue = atoi(dbrow[col]); col++;
|
|
|
|
int colour = atoi(dbrow[col]); col++;
|
|
|
|
|
|
|
|
const char *event_prefix = dbrow[col]; col++;
|
|
|
|
const char *label_format = dbrow[col]; col++;
|
|
|
|
|
|
|
|
int label_x = atoi(dbrow[col]); col++;
|
|
|
|
int label_y = atoi(dbrow[col]); col++;
|
|
|
|
|
|
|
|
int image_buffer_count = atoi(dbrow[col]); col++;
|
|
|
|
int warmup_count = atoi(dbrow[col]); col++;
|
|
|
|
int pre_event_count = atoi(dbrow[col]); col++;
|
|
|
|
int post_event_count = atoi(dbrow[col]); col++;
|
2007-08-30 02:11:09 +08:00
|
|
|
int stream_replay_buffer = atoi(dbrow[col]); col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int alarm_frame_count = atoi(dbrow[col]); col++;
|
|
|
|
int section_length = atoi(dbrow[col]); col++;
|
|
|
|
int frame_skip = atoi(dbrow[col]); col++;
|
2006-03-24 02:21:26 +08:00
|
|
|
int capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
2006-03-31 20:49:05 +08:00
|
|
|
int alarm_capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int fps_report_interval = atoi(dbrow[col]); col++;
|
|
|
|
int ref_blend_perc = atoi(dbrow[col]); col++;
|
|
|
|
int track_motion = atoi(dbrow[col]); col++;
|
2007-09-05 23:05:28 +08:00
|
|
|
int signal_check_colour;
|
|
|
|
if ( dbrow[col][0] == '#' )
|
|
|
|
signal_check_colour = strtol(dbrow[col]+1,0,16);
|
|
|
|
else
|
|
|
|
signal_check_colour = strtol(dbrow[col],0,16);
|
|
|
|
col++;
|
2005-10-18 05:55:02 +08:00
|
|
|
|
|
|
|
int cam_width = ((orientation==ROTATE_90||orientation==ROTATE_270)?height:width);
|
|
|
|
int cam_height = ((orientation==ROTATE_90||orientation==ROTATE_270)?width:height);
|
|
|
|
|
|
|
|
Camera *camera = new LocalCamera(
|
2008-07-16 16:35:59 +08:00
|
|
|
id,
|
2008-07-21 23:20:52 +08:00
|
|
|
device,
|
|
|
|
channel,
|
|
|
|
format,
|
|
|
|
method,
|
2005-10-18 05:55:02 +08:00
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
|
|
|
);
|
|
|
|
|
2003-10-08 22:49:26 +08:00
|
|
|
monitors[i] = new Monitor(
|
2006-01-23 02:31:55 +08:00
|
|
|
id,
|
|
|
|
name,
|
|
|
|
function,
|
|
|
|
enabled,
|
|
|
|
linked_monitors,
|
2005-10-18 05:55:02 +08:00
|
|
|
camera,
|
|
|
|
orientation,
|
2006-01-23 02:31:55 +08:00
|
|
|
event_prefix,
|
|
|
|
label_format,
|
|
|
|
Coord( label_x, label_y ),
|
|
|
|
image_buffer_count,
|
|
|
|
warmup_count,
|
|
|
|
pre_event_count,
|
|
|
|
post_event_count,
|
2007-08-30 02:11:09 +08:00
|
|
|
stream_replay_buffer,
|
2006-01-23 02:31:55 +08:00
|
|
|
alarm_frame_count,
|
|
|
|
section_length,
|
|
|
|
frame_skip,
|
|
|
|
capture_delay,
|
2006-03-31 20:49:05 +08:00
|
|
|
alarm_capture_delay,
|
2006-01-23 02:31:55 +08:00
|
|
|
fps_report_interval,
|
|
|
|
ref_blend_perc,
|
|
|
|
track_motion,
|
2007-09-05 23:05:28 +08:00
|
|
|
signal_check_colour,
|
2003-10-08 22:49:26 +08:00
|
|
|
purpose
|
|
|
|
);
|
2003-03-26 19:57:29 +08:00
|
|
|
Zone **zones = 0;
|
|
|
|
int n_zones = Zone::Load( monitors[i], zones );
|
|
|
|
monitors[i]->AddZones( n_zones, zones );
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Loaded monitor %d(%s), %d zones", id, name, n_zones );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
if ( mysql_errno( &dbconn ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
// Yadda yadda
|
|
|
|
mysql_free_result( result );
|
|
|
|
|
|
|
|
return( n_monitors );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
int Monitor::LoadRemoteMonitors( const char *protocol, const char *host, const char *port, const char *path, Monitor **&monitors, Purpose purpose )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-07-04 04:39:47 +08:00
|
|
|
static char sql[BUFSIZ];
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( !protocol )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
strncpy( sql, "select Id, Name, Function+0, Enabled, LinkedMonitors, Protocol, Method, Host, Port, Path, SubPath, Width, Height, Palette, Orientation+0, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion from Monitors where Function != 'None' and Type = 'Remote'", sizeof(sql) );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
snprintf( sql, sizeof(sql), "select Id, Name, Function+0, Enabled, LinkedMonitors, Protocol, Method, Host, Port, Path, SubPath, Width, Height, Palette, Orientation+0, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion from Monitors where Function != 'None' and Type = 'Remote' and Protocol = '%s' and Host = '%s' and Port = '%s' and Path = '%s'", protocol, host, port, path );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
int n_monitors = mysql_num_rows( result );
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got %d monitors", n_monitors );
|
2003-03-26 19:57:29 +08:00
|
|
|
delete[] monitors;
|
|
|
|
monitors = new Monitor *[n_monitors];
|
|
|
|
for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )
|
|
|
|
{
|
2006-01-23 02:31:55 +08:00
|
|
|
int col = 0;
|
|
|
|
|
|
|
|
int id = atoi(dbrow[col]); col++;
|
2008-07-16 16:35:59 +08:00
|
|
|
std::string name = dbrow[col]; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int function = atoi(dbrow[col]); col++;
|
|
|
|
int enabled = atoi(dbrow[col]); col++;
|
|
|
|
const char *linked_monitors = dbrow[col]; col++;
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
std::string protocol = dbrow[col]; col++;
|
|
|
|
std::string method = dbrow[col]; col++;
|
|
|
|
std::string host = dbrow[col]; col++;
|
|
|
|
std::string port = dbrow[col]; col++;
|
|
|
|
std::string path = dbrow[col]; col++;
|
|
|
|
std::string subpath = dbrow[col]; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
|
|
|
|
int width = atoi(dbrow[col]); col++;
|
|
|
|
int height = atoi(dbrow[col]); col++;
|
|
|
|
int palette = atoi(dbrow[col]); col++;
|
|
|
|
Orientation orientation = (Orientation)atoi(dbrow[col]); col++;
|
|
|
|
int brightness = atoi(dbrow[col]); col++;
|
|
|
|
int contrast = atoi(dbrow[col]); col++;
|
|
|
|
int hue = atoi(dbrow[col]); col++;
|
|
|
|
int colour = atoi(dbrow[col]); col++;
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
std::string event_prefix = dbrow[col]; col++;
|
|
|
|
std::string label_format = dbrow[col]; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
|
|
|
|
int label_x = atoi(dbrow[col]); col++;
|
|
|
|
int label_y = atoi(dbrow[col]); col++;
|
|
|
|
|
|
|
|
int image_buffer_count = atoi(dbrow[col]); col++;
|
|
|
|
int warmup_count = atoi(dbrow[col]); col++;
|
|
|
|
int pre_event_count = atoi(dbrow[col]); col++;
|
|
|
|
int post_event_count = atoi(dbrow[col]); col++;
|
2007-08-30 02:11:09 +08:00
|
|
|
int stream_replay_buffer = atoi(dbrow[col]); col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int alarm_frame_count = atoi(dbrow[col]); col++;
|
|
|
|
int section_length = atoi(dbrow[col]); col++;
|
|
|
|
int frame_skip = atoi(dbrow[col]); col++;
|
2006-03-24 02:21:26 +08:00
|
|
|
int capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
2006-03-31 20:49:05 +08:00
|
|
|
int alarm_capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int fps_report_interval = atoi(dbrow[col]); col++;
|
|
|
|
int ref_blend_perc = atoi(dbrow[col]); col++;
|
|
|
|
int track_motion = atoi(dbrow[col]); col++;
|
2005-10-18 05:55:02 +08:00
|
|
|
|
|
|
|
int cam_width = ((orientation==ROTATE_90||orientation==ROTATE_270)?height:width);
|
|
|
|
int cam_height = ((orientation==ROTATE_90||orientation==ROTATE_270)?width:height);
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
Camera *camera = 0;
|
|
|
|
if ( protocol == "http" )
|
|
|
|
{
|
|
|
|
camera = new RemoteCameraHttp(
|
|
|
|
id,
|
|
|
|
method,
|
|
|
|
host, // Host
|
|
|
|
port, // Port
|
|
|
|
path, // Path
|
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
|
|
|
);
|
|
|
|
}
|
2008-10-17 00:11:49 +08:00
|
|
|
#if HAVE_LIBAVFORMAT
|
2008-07-16 16:35:59 +08:00
|
|
|
else if ( protocol == "rtsp" )
|
|
|
|
{
|
|
|
|
camera = new RemoteCameraRtsp(
|
|
|
|
id,
|
|
|
|
method,
|
|
|
|
host, // Host
|
|
|
|
port, // Port
|
|
|
|
path, // Path
|
|
|
|
subpath,
|
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
|
|
|
);
|
|
|
|
}
|
2008-10-17 00:11:49 +08:00
|
|
|
#endif // HAVE_LIBAVFORMAT
|
2008-07-16 16:35:59 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Unexpected remote camera protocol '%s'", protocol.c_str() );
|
|
|
|
}
|
2005-10-18 05:55:02 +08:00
|
|
|
|
2003-10-08 22:49:26 +08:00
|
|
|
monitors[i] = new Monitor(
|
2006-01-23 02:31:55 +08:00
|
|
|
id,
|
2008-07-16 16:35:59 +08:00
|
|
|
name.c_str(),
|
2006-01-23 02:31:55 +08:00
|
|
|
function,
|
|
|
|
enabled,
|
|
|
|
linked_monitors,
|
2005-10-18 05:55:02 +08:00
|
|
|
camera,
|
2006-01-23 02:31:55 +08:00
|
|
|
orientation,
|
2008-07-16 16:35:59 +08:00
|
|
|
event_prefix.c_str(),
|
|
|
|
label_format.c_str(),
|
2006-01-23 02:31:55 +08:00
|
|
|
Coord( label_x, label_y ),
|
|
|
|
image_buffer_count,
|
|
|
|
warmup_count,
|
|
|
|
pre_event_count,
|
|
|
|
post_event_count,
|
2007-08-30 02:11:09 +08:00
|
|
|
stream_replay_buffer,
|
2006-01-23 02:31:55 +08:00
|
|
|
alarm_frame_count,
|
|
|
|
section_length,
|
|
|
|
frame_skip,
|
|
|
|
capture_delay,
|
2006-03-31 20:49:05 +08:00
|
|
|
alarm_capture_delay,
|
2006-01-23 02:31:55 +08:00
|
|
|
fps_report_interval,
|
|
|
|
ref_blend_perc,
|
|
|
|
track_motion,
|
2007-12-30 02:50:53 +08:00
|
|
|
RGB_WHITE,
|
2003-10-08 22:49:26 +08:00
|
|
|
purpose
|
|
|
|
);
|
2003-03-26 19:57:29 +08:00
|
|
|
Zone **zones = 0;
|
|
|
|
int n_zones = Zone::Load( monitors[i], zones );
|
|
|
|
monitors[i]->AddZones( n_zones, zones );
|
2008-07-16 16:35:59 +08:00
|
|
|
Debug( 1, "Loaded monitor %d(%s), %d zones", id, name.c_str(), n_zones );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
if ( mysql_errno( &dbconn ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
// Yadda yadda
|
|
|
|
mysql_free_result( result );
|
|
|
|
|
|
|
|
return( n_monitors );
|
|
|
|
}
|
|
|
|
|
2005-10-18 05:55:02 +08:00
|
|
|
int Monitor::LoadFileMonitors( const char *file, Monitor **&monitors, Purpose purpose )
|
|
|
|
{
|
|
|
|
static char sql[BUFSIZ];
|
|
|
|
if ( !file[0] )
|
|
|
|
{
|
2007-08-30 02:11:09 +08:00
|
|
|
strncpy( sql, "select Id, Name, Function+0, Enabled, LinkedMonitors, Path, Width, Height, Palette, Orientation+0, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion from Monitors where Function != 'None' and Type = 'File'", sizeof(sql) );
|
2005-10-18 05:55:02 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-08-30 02:11:09 +08:00
|
|
|
snprintf( sql, sizeof(sql), "select Id, Name, Function+0, Enabled, LinkedMonitors, Path, Width, Height, Palette, Orientation+0, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion from Monitors where Function != 'None' and Type = 'File' and Path = '%s'", file );
|
2005-10-18 05:55:02 +08:00
|
|
|
}
|
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2005-10-18 05:55:02 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
2005-10-18 05:55:02 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
int n_monitors = mysql_num_rows( result );
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got %d monitors", n_monitors );
|
2005-10-18 05:55:02 +08:00
|
|
|
delete[] monitors;
|
|
|
|
monitors = new Monitor *[n_monitors];
|
|
|
|
for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )
|
|
|
|
{
|
2006-01-23 02:31:55 +08:00
|
|
|
int col = 0;
|
|
|
|
|
|
|
|
int id = atoi(dbrow[col]); col++;
|
|
|
|
const char *name = dbrow[col]; col++;
|
|
|
|
int function = atoi(dbrow[col]); col++;
|
|
|
|
int enabled = atoi(dbrow[col]); col++;
|
|
|
|
const char *linked_monitors = dbrow[col]; col++;
|
|
|
|
|
|
|
|
const char *path = dbrow[col]; col++;
|
|
|
|
|
|
|
|
int width = atoi(dbrow[col]); col++;
|
|
|
|
int height = atoi(dbrow[col]); col++;
|
|
|
|
int palette = atoi(dbrow[col]); col++;
|
|
|
|
Orientation orientation = (Orientation)atoi(dbrow[col]); col++;
|
|
|
|
int brightness = atoi(dbrow[col]); col++;
|
|
|
|
int contrast = atoi(dbrow[col]); col++;
|
|
|
|
int hue = atoi(dbrow[col]); col++;
|
|
|
|
int colour = atoi(dbrow[col]); col++;
|
|
|
|
|
|
|
|
const char *event_prefix = dbrow[col]; col++;
|
|
|
|
const char *label_format = dbrow[col]; col++;
|
|
|
|
|
|
|
|
int label_x = atoi(dbrow[col]); col++;
|
|
|
|
int label_y = atoi(dbrow[col]); col++;
|
|
|
|
|
|
|
|
int image_buffer_count = atoi(dbrow[col]); col++;
|
|
|
|
int warmup_count = atoi(dbrow[col]); col++;
|
|
|
|
int pre_event_count = atoi(dbrow[col]); col++;
|
|
|
|
int post_event_count = atoi(dbrow[col]); col++;
|
2007-08-30 02:11:09 +08:00
|
|
|
int stream_replay_buffer = atoi(dbrow[col]); col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int alarm_frame_count = atoi(dbrow[col]); col++;
|
|
|
|
int section_length = atoi(dbrow[col]); col++;
|
|
|
|
int frame_skip = atoi(dbrow[col]); col++;
|
2006-03-24 02:21:26 +08:00
|
|
|
int capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
2006-03-31 20:49:05 +08:00
|
|
|
int alarm_capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int fps_report_interval = atoi(dbrow[col]); col++;
|
|
|
|
int ref_blend_perc = atoi(dbrow[col]); col++;
|
|
|
|
int track_motion = atoi(dbrow[col]); col++;
|
2005-10-18 05:55:02 +08:00
|
|
|
|
|
|
|
int cam_width = ((orientation==ROTATE_90||orientation==ROTATE_270)?height:width);
|
|
|
|
int cam_height = ((orientation==ROTATE_90||orientation==ROTATE_270)?width:height);
|
|
|
|
|
|
|
|
Camera *camera = new FileCamera(
|
2008-07-16 16:35:59 +08:00
|
|
|
id,
|
2006-01-23 02:31:55 +08:00
|
|
|
path, // File
|
2005-10-18 05:55:02 +08:00
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
|
|
|
);
|
|
|
|
|
|
|
|
monitors[i] = new Monitor(
|
2006-01-23 02:31:55 +08:00
|
|
|
id,
|
|
|
|
name,
|
|
|
|
function,
|
|
|
|
enabled,
|
|
|
|
linked_monitors,
|
2005-10-18 05:55:02 +08:00
|
|
|
camera,
|
2006-01-23 02:31:55 +08:00
|
|
|
orientation,
|
|
|
|
event_prefix,
|
|
|
|
label_format,
|
|
|
|
Coord( label_x, label_y ),
|
|
|
|
image_buffer_count,
|
|
|
|
warmup_count,
|
|
|
|
pre_event_count,
|
|
|
|
post_event_count,
|
2007-08-30 02:11:09 +08:00
|
|
|
stream_replay_buffer,
|
2006-01-23 02:31:55 +08:00
|
|
|
alarm_frame_count,
|
|
|
|
section_length,
|
|
|
|
frame_skip,
|
|
|
|
capture_delay,
|
2006-03-31 20:49:05 +08:00
|
|
|
alarm_capture_delay,
|
2006-01-23 02:31:55 +08:00
|
|
|
fps_report_interval,
|
|
|
|
ref_blend_perc,
|
|
|
|
track_motion,
|
2007-12-30 02:50:53 +08:00
|
|
|
RGB_WHITE,
|
2005-10-18 05:55:02 +08:00
|
|
|
purpose
|
|
|
|
);
|
|
|
|
Zone **zones = 0;
|
|
|
|
int n_zones = Zone::Load( monitors[i], zones );
|
|
|
|
monitors[i]->AddZones( n_zones, zones );
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Loaded monitor %d(%s), %d zones", id, name, n_zones );
|
2005-10-18 05:55:02 +08:00
|
|
|
}
|
|
|
|
if ( mysql_errno( &dbconn ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
|
2005-10-18 05:55:02 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
// Yadda yadda
|
|
|
|
mysql_free_result( result );
|
|
|
|
|
|
|
|
return( n_monitors );
|
|
|
|
}
|
|
|
|
|
2009-01-28 23:02:33 +08:00
|
|
|
#if HAVE_LIBAVFORMAT
|
2009-01-20 23:01:32 +08:00
|
|
|
int Monitor::LoadFfmpegMonitors( const char *file, Monitor **&monitors, Purpose purpose )
|
|
|
|
{
|
|
|
|
static char sql[BUFSIZ];
|
|
|
|
if ( !file[0] )
|
|
|
|
{
|
|
|
|
strncpy( sql, "select Id, Name, Function+0, Enabled, LinkedMonitors, Path, Width, Height, Palette, Orientation+0, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion from Monitors where Function != 'None' and Type = 'File'", sizeof(sql) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf( sql, sizeof(sql), "select Id, Name, Function+0, Enabled, LinkedMonitors, Path, Width, Height, Palette, Orientation+0, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion from Monitors where Function != 'None' and Type = 'File' and Path = '%s'", file );
|
|
|
|
}
|
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
int n_monitors = mysql_num_rows( result );
|
|
|
|
Debug( 1, "Got %d monitors", n_monitors );
|
|
|
|
delete[] monitors;
|
|
|
|
monitors = new Monitor *[n_monitors];
|
|
|
|
for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )
|
|
|
|
{
|
|
|
|
int col = 0;
|
|
|
|
|
|
|
|
int id = atoi(dbrow[col]); col++;
|
|
|
|
const char *name = dbrow[col]; col++;
|
|
|
|
int function = atoi(dbrow[col]); col++;
|
|
|
|
int enabled = atoi(dbrow[col]); col++;
|
|
|
|
const char *linked_monitors = dbrow[col]; col++;
|
|
|
|
|
|
|
|
const char *path = dbrow[col]; col++;
|
|
|
|
|
|
|
|
int width = atoi(dbrow[col]); col++;
|
|
|
|
int height = atoi(dbrow[col]); col++;
|
|
|
|
int palette = atoi(dbrow[col]); col++;
|
|
|
|
Orientation orientation = (Orientation)atoi(dbrow[col]); col++;
|
|
|
|
int brightness = atoi(dbrow[col]); col++;
|
|
|
|
int contrast = atoi(dbrow[col]); col++;
|
|
|
|
int hue = atoi(dbrow[col]); col++;
|
|
|
|
int colour = atoi(dbrow[col]); col++;
|
|
|
|
|
|
|
|
const char *event_prefix = dbrow[col]; col++;
|
|
|
|
const char *label_format = dbrow[col]; col++;
|
|
|
|
|
|
|
|
int label_x = atoi(dbrow[col]); col++;
|
|
|
|
int label_y = atoi(dbrow[col]); col++;
|
|
|
|
|
|
|
|
int image_buffer_count = atoi(dbrow[col]); col++;
|
|
|
|
int warmup_count = atoi(dbrow[col]); col++;
|
|
|
|
int pre_event_count = atoi(dbrow[col]); col++;
|
|
|
|
int post_event_count = atoi(dbrow[col]); col++;
|
|
|
|
int stream_replay_buffer = atoi(dbrow[col]); col++;
|
|
|
|
int alarm_frame_count = atoi(dbrow[col]); col++;
|
|
|
|
int section_length = atoi(dbrow[col]); col++;
|
|
|
|
int frame_skip = atoi(dbrow[col]); col++;
|
|
|
|
int capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
|
|
|
int alarm_capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
|
|
|
int fps_report_interval = atoi(dbrow[col]); col++;
|
|
|
|
int ref_blend_perc = atoi(dbrow[col]); col++;
|
|
|
|
int track_motion = atoi(dbrow[col]); col++;
|
|
|
|
|
|
|
|
int cam_width = ((orientation==ROTATE_90||orientation==ROTATE_270)?height:width);
|
|
|
|
int cam_height = ((orientation==ROTATE_90||orientation==ROTATE_270)?width:height);
|
|
|
|
|
|
|
|
Camera *camera = new FfmpegCamera(
|
|
|
|
id,
|
|
|
|
path, // File
|
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
|
|
|
);
|
|
|
|
|
|
|
|
monitors[i] = new Monitor(
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
function,
|
|
|
|
enabled,
|
|
|
|
linked_monitors,
|
|
|
|
camera,
|
|
|
|
orientation,
|
|
|
|
event_prefix,
|
|
|
|
label_format,
|
|
|
|
Coord( label_x, label_y ),
|
|
|
|
image_buffer_count,
|
|
|
|
warmup_count,
|
|
|
|
pre_event_count,
|
|
|
|
post_event_count,
|
|
|
|
stream_replay_buffer,
|
|
|
|
alarm_frame_count,
|
|
|
|
section_length,
|
|
|
|
frame_skip,
|
|
|
|
capture_delay,
|
|
|
|
alarm_capture_delay,
|
|
|
|
fps_report_interval,
|
|
|
|
ref_blend_perc,
|
|
|
|
track_motion,
|
|
|
|
RGB_WHITE,
|
|
|
|
purpose
|
|
|
|
);
|
|
|
|
Zone **zones = 0;
|
|
|
|
int n_zones = Zone::Load( monitors[i], zones );
|
|
|
|
monitors[i]->AddZones( n_zones, zones );
|
|
|
|
Debug( 1, "Loaded monitor %d(%s), %d zones", id, name, n_zones );
|
|
|
|
}
|
|
|
|
if ( mysql_errno( &dbconn ) )
|
|
|
|
{
|
|
|
|
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
|
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
// Yadda yadda
|
|
|
|
mysql_free_result( result );
|
|
|
|
|
|
|
|
return( n_monitors );
|
|
|
|
}
|
2009-01-28 23:02:33 +08:00
|
|
|
#endif // HAVE_LIBAVFORMAT
|
2009-01-20 23:01:32 +08:00
|
|
|
|
2003-09-23 17:52:45 +08:00
|
|
|
Monitor *Monitor::Load( int id, bool load_zones, Purpose purpose )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-07-04 04:39:47 +08:00
|
|
|
static char sql[BUFSIZ];
|
2008-07-16 16:35:59 +08:00
|
|
|
snprintf( sql, sizeof(sql), "select Id, Name, Type, Function+0, Enabled, LinkedMonitors, Device, Channel, Format, Protocol, Method, Host, Port, Path, SubPath, Width, Height, Palette, Orientation+0, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, TrackMotion, SignalCheckColour from Monitors where Id = %d", id );
|
2003-03-26 19:57:29 +08:00
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
int n_monitors = mysql_num_rows( result );
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got %d monitors", n_monitors );
|
2003-03-26 19:57:29 +08:00
|
|
|
Monitor *monitor = 0;
|
|
|
|
for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )
|
|
|
|
{
|
2006-01-23 02:31:55 +08:00
|
|
|
int col = 0;
|
|
|
|
|
|
|
|
int id = atoi(dbrow[col]); col++;
|
2008-07-16 16:35:59 +08:00
|
|
|
std::string name = dbrow[col]; col++;
|
|
|
|
std::string type = dbrow[col]; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int function = atoi(dbrow[col]); col++;
|
|
|
|
int enabled = atoi(dbrow[col]); col++;
|
2008-07-16 16:35:59 +08:00
|
|
|
std::string linked_monitors = dbrow[col]; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
std::string device = dbrow[col]; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int channel = atoi(dbrow[col]); col++;
|
|
|
|
int format = atoi(dbrow[col]); col++;
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
std::string protocol = dbrow[col]; col++;
|
|
|
|
std::string method = dbrow[col]; col++;
|
|
|
|
std::string host = dbrow[col]; col++;
|
|
|
|
std::string port = dbrow[col]; col++;
|
|
|
|
std::string path = dbrow[col]; col++;
|
|
|
|
std::string subpath = dbrow[col]; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
|
|
|
|
int width = atoi(dbrow[col]); col++;
|
|
|
|
int height = atoi(dbrow[col]); col++;
|
|
|
|
int palette = atoi(dbrow[col]); col++;
|
|
|
|
Orientation orientation = (Orientation)atoi(dbrow[col]); col++;
|
|
|
|
int brightness = atoi(dbrow[col]); col++;
|
|
|
|
int contrast = atoi(dbrow[col]); col++;
|
|
|
|
int hue = atoi(dbrow[col]); col++;
|
|
|
|
int colour = atoi(dbrow[col]); col++;
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
std::string event_prefix = dbrow[col]; col++;
|
|
|
|
std::string label_format = dbrow[col]; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
|
|
|
|
int label_x = atoi(dbrow[col]); col++;
|
|
|
|
int label_y = atoi(dbrow[col]); col++;
|
|
|
|
|
|
|
|
int image_buffer_count = atoi(dbrow[col]); col++;
|
|
|
|
int warmup_count = atoi(dbrow[col]); col++;
|
|
|
|
int pre_event_count = atoi(dbrow[col]); col++;
|
|
|
|
int post_event_count = atoi(dbrow[col]); col++;
|
2007-08-30 02:11:09 +08:00
|
|
|
int stream_replay_buffer = atoi(dbrow[col]); col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int alarm_frame_count = atoi(dbrow[col]); col++;
|
|
|
|
int section_length = atoi(dbrow[col]); col++;
|
|
|
|
int frame_skip = atoi(dbrow[col]); col++;
|
2006-03-24 02:21:26 +08:00
|
|
|
int capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
2006-03-31 20:49:05 +08:00
|
|
|
int alarm_capture_delay = (dbrow[col]&&atof(dbrow[col])>0.0)?int(DT_PREC_3/atof(dbrow[col])):0; col++;
|
2006-01-23 02:31:55 +08:00
|
|
|
int fps_report_interval = atoi(dbrow[col]); col++;
|
|
|
|
int ref_blend_perc = atoi(dbrow[col]); col++;
|
|
|
|
int track_motion = atoi(dbrow[col]); col++;
|
2007-09-05 23:05:28 +08:00
|
|
|
int signal_check_colour;
|
|
|
|
if ( dbrow[col][0] == '#' )
|
|
|
|
signal_check_colour = strtol(dbrow[col]+1,0,16);
|
|
|
|
else
|
|
|
|
signal_check_colour = strtol(dbrow[col],0,16);
|
2005-10-18 05:55:02 +08:00
|
|
|
|
|
|
|
int cam_width = ((orientation==ROTATE_90||orientation==ROTATE_270)?height:width);
|
|
|
|
int cam_height = ((orientation==ROTATE_90||orientation==ROTATE_270)?width:height);
|
|
|
|
|
|
|
|
Camera *camera = 0;
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( type == "Local" )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2005-10-18 05:55:02 +08:00
|
|
|
camera = new LocalCamera(
|
2008-07-16 16:35:59 +08:00
|
|
|
id,
|
|
|
|
device.c_str(),
|
|
|
|
channel,
|
|
|
|
format,
|
2008-07-21 23:20:52 +08:00
|
|
|
method,
|
2005-10-18 05:55:02 +08:00
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
2003-10-08 22:49:26 +08:00
|
|
|
);
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
else if ( type == "Remote" )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( protocol == "http" )
|
|
|
|
{
|
|
|
|
camera = new RemoteCameraHttp(
|
|
|
|
id,
|
|
|
|
method.c_str(),
|
|
|
|
host.c_str(),
|
|
|
|
port.c_str(),
|
|
|
|
path.c_str(),
|
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
|
|
|
);
|
|
|
|
}
|
2008-10-17 00:11:49 +08:00
|
|
|
#if HAVE_LIBAVFORMAT
|
2008-07-16 16:35:59 +08:00
|
|
|
else if ( protocol == "rtsp" )
|
|
|
|
{
|
|
|
|
camera = new RemoteCameraRtsp(
|
|
|
|
id,
|
|
|
|
method.c_str(),
|
|
|
|
host.c_str(),
|
|
|
|
port.c_str(),
|
|
|
|
path.c_str(),
|
|
|
|
subpath.c_str(),
|
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
|
|
|
);
|
|
|
|
}
|
2008-10-17 00:11:49 +08:00
|
|
|
#endif // HAVE_LIBAVFORMAT
|
2008-07-16 16:35:59 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Unexpected remote camera protocol '%s' for monitor %d", protocol.c_str(), id );
|
|
|
|
}
|
2005-10-18 05:55:02 +08:00
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
else if ( type == "File" )
|
2005-10-18 05:55:02 +08:00
|
|
|
{
|
|
|
|
camera = new FileCamera(
|
2008-07-16 16:35:59 +08:00
|
|
|
id,
|
|
|
|
path.c_str(),
|
2005-10-18 05:55:02 +08:00
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
2003-10-08 22:49:26 +08:00
|
|
|
);
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2009-01-28 23:02:33 +08:00
|
|
|
#if HAVE_LIBAVFORMAT
|
2009-01-20 23:01:32 +08:00
|
|
|
else if ( type == "Ffmpeg" )
|
|
|
|
{
|
|
|
|
camera = new FfmpegCamera(
|
|
|
|
id,
|
|
|
|
path.c_str(),
|
|
|
|
cam_width,
|
|
|
|
cam_height,
|
|
|
|
palette,
|
|
|
|
brightness,
|
|
|
|
contrast,
|
|
|
|
hue,
|
|
|
|
colour,
|
|
|
|
purpose==CAPTURE
|
|
|
|
);
|
|
|
|
}
|
2009-01-28 23:02:33 +08:00
|
|
|
#endif // HAVE_LIBAVFORMAT
|
2005-10-18 05:55:02 +08:00
|
|
|
else
|
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
Fatal( "Bogus monitor type '%s' for monitor %d", type.c_str(), id );
|
2005-10-18 05:55:02 +08:00
|
|
|
}
|
|
|
|
monitor = new Monitor(
|
2006-01-23 02:31:55 +08:00
|
|
|
id,
|
2008-07-16 16:35:59 +08:00
|
|
|
name.c_str(),
|
2006-01-23 02:31:55 +08:00
|
|
|
function,
|
|
|
|
enabled,
|
2008-07-16 16:35:59 +08:00
|
|
|
linked_monitors.c_str(),
|
2005-10-18 05:55:02 +08:00
|
|
|
camera,
|
|
|
|
orientation,
|
2008-07-16 16:35:59 +08:00
|
|
|
event_prefix.c_str(),
|
|
|
|
label_format.c_str(),
|
2006-01-23 02:31:55 +08:00
|
|
|
Coord( label_x, label_y ),
|
|
|
|
image_buffer_count,
|
|
|
|
warmup_count,
|
|
|
|
pre_event_count,
|
|
|
|
post_event_count,
|
2007-08-30 02:11:09 +08:00
|
|
|
stream_replay_buffer,
|
2006-01-23 02:31:55 +08:00
|
|
|
alarm_frame_count,
|
|
|
|
section_length,
|
|
|
|
frame_skip,
|
|
|
|
capture_delay,
|
2006-03-31 20:49:05 +08:00
|
|
|
alarm_capture_delay,
|
2006-01-23 02:31:55 +08:00
|
|
|
fps_report_interval,
|
|
|
|
ref_blend_perc,
|
|
|
|
track_motion,
|
2007-09-05 23:05:28 +08:00
|
|
|
signal_check_colour,
|
2005-10-18 05:55:02 +08:00
|
|
|
purpose
|
|
|
|
);
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
int n_zones = 0;
|
|
|
|
if ( load_zones )
|
|
|
|
{
|
|
|
|
Zone **zones = 0;
|
|
|
|
n_zones = Zone::Load( monitor, zones );
|
|
|
|
monitor->AddZones( n_zones, zones );
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
Debug( 1, "Loaded monitor %d(%s), %d zones", id, name.c_str(), n_zones );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
if ( mysql_errno( &dbconn ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
|
2003-03-26 19:57:29 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
// Yadda yadda
|
|
|
|
mysql_free_result( result );
|
|
|
|
|
|
|
|
return( monitor );
|
|
|
|
}
|
|
|
|
|
2009-01-28 17:48:06 +08:00
|
|
|
int Monitor::Capture()
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2009-01-28 17:48:06 +08:00
|
|
|
if ( camera->Capture( image ) == 0 )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( orientation != ROTATE_0 )
|
|
|
|
{
|
|
|
|
switch ( orientation )
|
|
|
|
{
|
2008-03-13 21:36:12 +08:00
|
|
|
case ROTATE_0 :
|
|
|
|
{
|
|
|
|
// No action required
|
|
|
|
break;
|
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
case ROTATE_90 :
|
|
|
|
case ROTATE_180 :
|
|
|
|
case ROTATE_270 :
|
|
|
|
{
|
|
|
|
image.Rotate( (orientation-1)*90 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FLIP_HORI :
|
|
|
|
case FLIP_VERT :
|
|
|
|
{
|
|
|
|
image.Flip( orientation==FLIP_HORI );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-12 23:14:24 +08:00
|
|
|
if ( image.Size() != camera->ImageSize() )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Captured image does not match expected size, check width, height and colour depth" );
|
2006-04-12 23:14:24 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
int index = image_count%image_buffer_count;
|
2008-07-21 23:20:52 +08:00
|
|
|
if ( (index == shared_data->last_read_index) && (function > MONITOR) )
|
2005-12-23 00:46:25 +08:00
|
|
|
{
|
2009-01-20 23:01:32 +08:00
|
|
|
Warning( "Buffer overrun at index %d, image %d, slow down capture, speed up analysis or increase ring buffer size", index, image_count );
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
|
2008-03-13 21:36:12 +08:00
|
|
|
gettimeofday( image_buffer[index].timestamp, NULL );
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( config.timestamp_on_capture )
|
|
|
|
{
|
2006-12-25 00:50:01 +08:00
|
|
|
TimestampImage( &image, image_buffer[index].timestamp );
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
image_buffer[index].image->CopyBuffer( image );
|
|
|
|
|
2006-01-15 06:47:02 +08:00
|
|
|
shared_data->signal = CheckSignal( &image );
|
2005-12-23 00:46:25 +08:00
|
|
|
shared_data->last_write_index = index;
|
2008-07-21 23:20:52 +08:00
|
|
|
shared_data->last_write_time = image_buffer[index].timestamp->tv_sec;
|
2005-12-23 00:46:25 +08:00
|
|
|
|
|
|
|
image_count++;
|
|
|
|
|
|
|
|
if ( image_count && !(image_count%fps_report_interval) )
|
|
|
|
{
|
|
|
|
time_t now = image_buffer[index].timestamp->tv_sec;
|
|
|
|
fps = double(fps_report_interval)/(now-last_fps_time);
|
2008-07-14 22:43:47 +08:00
|
|
|
//Info( "%d -> %d -> %d", fps_report_interval, now, last_fps_time );
|
|
|
|
//Info( "%d -> %d -> %lf -> %lf", now-last_fps_time, fps_report_interval/(now-last_fps_time), double(fps_report_interval)/(now-last_fps_time), fps );
|
|
|
|
Info( "%s: %d - Capturing at %.2lf fps", name, image_count, fps );
|
2005-12-23 00:46:25 +08:00
|
|
|
last_fps_time = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return( 0 );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2006-01-16 01:39:18 +08:00
|
|
|
shared_data->signal = false;
|
2005-12-23 00:46:25 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2006-12-25 00:50:01 +08:00
|
|
|
void Monitor::TimestampImage( Image *ts_image, const struct timeval *ts_time ) const
|
2005-12-23 00:46:25 +08:00
|
|
|
{
|
|
|
|
if ( label_format[0] )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2006-12-25 00:50:01 +08:00
|
|
|
// Expand the strftime macros first
|
|
|
|
char label_time_text[256];
|
|
|
|
strftime( label_time_text, sizeof(label_time_text), label_format, localtime( &ts_time->tv_sec ) );
|
2005-12-23 00:46:25 +08:00
|
|
|
|
2006-12-25 00:50:01 +08:00
|
|
|
char label_text[1024];
|
|
|
|
const char *s_ptr = label_time_text;
|
|
|
|
char *d_ptr = label_text;
|
|
|
|
while ( *s_ptr && ((d_ptr-label_text) < sizeof(label_text)) )
|
|
|
|
{
|
2007-09-06 00:31:42 +08:00
|
|
|
if ( *s_ptr == '%' )
|
2006-12-25 00:50:01 +08:00
|
|
|
{
|
|
|
|
bool found_macro = false;
|
|
|
|
switch ( *(s_ptr+1) )
|
|
|
|
{
|
|
|
|
case 'N' :
|
|
|
|
d_ptr += snprintf( d_ptr, sizeof(label_text)-(d_ptr-label_text), "%s", name );
|
|
|
|
found_macro = true;
|
|
|
|
break;
|
2007-09-06 00:31:42 +08:00
|
|
|
case 'Q' :
|
2006-12-25 00:50:01 +08:00
|
|
|
d_ptr += snprintf( d_ptr, sizeof(label_text)-(d_ptr-label_text), "%s", trigger_data->trigger_showtext );
|
|
|
|
found_macro = true;
|
|
|
|
break;
|
2007-09-18 23:07:19 +08:00
|
|
|
case 'f' :
|
2006-12-25 00:50:01 +08:00
|
|
|
d_ptr += snprintf( d_ptr, sizeof(label_text)-(d_ptr-label_text), "%02ld", ts_time->tv_usec/10000 );
|
|
|
|
found_macro = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( found_macro )
|
|
|
|
{
|
2007-09-06 00:31:42 +08:00
|
|
|
s_ptr += 2;
|
2006-12-25 00:50:01 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*d_ptr++ = *s_ptr++;
|
|
|
|
}
|
|
|
|
*d_ptr = '\0';
|
2005-12-23 00:46:25 +08:00
|
|
|
ts_image->Annotate( label_text, label_coord );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Monitor::closeEvent()
|
|
|
|
{
|
|
|
|
if ( event )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( function == RECORD || function == MOCORD )
|
|
|
|
{
|
2008-03-13 21:36:12 +08:00
|
|
|
gettimeofday( &(event->EndTime()), NULL );
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
|
|
|
delete event;
|
|
|
|
event = 0;
|
|
|
|
return( true );
|
2004-02-18 23:35:51 +08:00
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
return( false );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2008-10-06 03:07:43 +08:00
|
|
|
unsigned int Monitor::DetectMotion( const Image &comp_image, Event::StringSet &zoneSet )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
|
|
|
bool alarm = false;
|
|
|
|
unsigned int score = 0;
|
|
|
|
|
|
|
|
if ( n_zones <= 0 ) return( alarm );
|
|
|
|
|
2005-05-16 17:27:06 +08:00
|
|
|
if ( config.record_diag_images )
|
2004-01-28 01:03:45 +08:00
|
|
|
{
|
|
|
|
static char diag_path[PATH_MAX] = "";
|
|
|
|
if ( !diag_path[0] )
|
|
|
|
{
|
2005-05-16 17:27:06 +08:00
|
|
|
snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-r.jpg", config.dir_events, id );
|
2004-01-28 01:03:45 +08:00
|
|
|
}
|
|
|
|
ref_image.WriteJpeg( diag_path );
|
|
|
|
}
|
|
|
|
|
2004-01-15 05:26:47 +08:00
|
|
|
Image *delta_image = ref_image.Delta( comp_image );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2005-05-16 17:27:06 +08:00
|
|
|
if ( config.record_diag_images )
|
2004-01-28 01:03:45 +08:00
|
|
|
{
|
|
|
|
static char diag_path[PATH_MAX] = "";
|
|
|
|
if ( !diag_path[0] )
|
|
|
|
{
|
2005-05-16 17:27:06 +08:00
|
|
|
snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-d.jpg", config.dir_events, id );
|
2004-01-28 01:03:45 +08:00
|
|
|
}
|
|
|
|
delta_image->WriteJpeg( diag_path );
|
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
// Blank out all exclusion zones
|
|
|
|
for ( int n_zone = 0; n_zone < n_zones; n_zone++ )
|
|
|
|
{
|
|
|
|
Zone *zone = zones[n_zone];
|
|
|
|
zone->ClearAlarm();
|
|
|
|
if ( !zone->IsInactive() )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Blanking inactive zone %s", zone->Label() );
|
2005-11-30 01:51:06 +08:00
|
|
|
delta_image->Fill( RGB_BLACK, zone->GetPolygon() );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2003-04-15 19:54:08 +08:00
|
|
|
// Check preclusive zones first
|
2003-03-26 19:57:29 +08:00
|
|
|
for ( int n_zone = 0; n_zone < n_zones; n_zone++ )
|
|
|
|
{
|
|
|
|
Zone *zone = zones[n_zone];
|
2003-04-15 19:54:08 +08:00
|
|
|
if ( !zone->IsPreclusive() )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Checking preclusive zone %s", zone->Label() );
|
2003-03-26 19:57:29 +08:00
|
|
|
if ( zone->CheckAlarms( delta_image ) )
|
|
|
|
{
|
|
|
|
alarm = true;
|
|
|
|
score += zone->Score();
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Zone is alarmed, zone score = %d", zone->Score() );
|
2008-10-06 03:07:43 +08:00
|
|
|
zoneSet.insert( zone->Label() );
|
2005-11-20 20:17:45 +08:00
|
|
|
//zone->ResetStats();
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-24 18:43:29 +08:00
|
|
|
Coord alarm_centre;
|
|
|
|
int top_score = -1;
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
if ( alarm )
|
|
|
|
{
|
2003-04-13 20:20:00 +08:00
|
|
|
alarm = false;
|
|
|
|
score = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Find all alarm pixels in active zones
|
2003-03-26 19:57:29 +08:00
|
|
|
for ( int n_zone = 0; n_zone < n_zones; n_zone++ )
|
|
|
|
{
|
|
|
|
Zone *zone = zones[n_zone];
|
2003-04-13 20:20:00 +08:00
|
|
|
if ( !zone->IsActive() )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Checking active zone %s", zone->Label() );
|
2003-03-26 19:57:29 +08:00
|
|
|
if ( zone->CheckAlarms( delta_image ) )
|
|
|
|
{
|
|
|
|
alarm = true;
|
|
|
|
score += zone->Score();
|
|
|
|
zone->SetAlarm();
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Zone is alarmed, zone score = %d", zone->Score() );
|
2008-10-06 03:07:43 +08:00
|
|
|
zoneSet.insert( zone->Label() );
|
2005-05-16 17:27:06 +08:00
|
|
|
if ( config.opt_control && track_motion )
|
2005-02-24 18:43:29 +08:00
|
|
|
{
|
|
|
|
if ( (int)zone->Score() > top_score )
|
|
|
|
{
|
|
|
|
top_score = zone->Score();
|
|
|
|
alarm_centre = zone->GetAlarmCentre();
|
|
|
|
}
|
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
}
|
2003-04-13 20:20:00 +08:00
|
|
|
|
|
|
|
if ( alarm )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-04-13 20:20:00 +08:00
|
|
|
for ( int n_zone = 0; n_zone < n_zones; n_zone++ )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-04-13 20:20:00 +08:00
|
|
|
Zone *zone = zones[n_zone];
|
|
|
|
if ( !zone->IsInclusive() )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Checking inclusive zone %s", zone->Label() );
|
2003-04-13 20:20:00 +08:00
|
|
|
if ( zone->CheckAlarms( delta_image ) )
|
|
|
|
{
|
|
|
|
alarm = true;
|
|
|
|
score += zone->Score();
|
|
|
|
zone->SetAlarm();
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Zone is alarmed, zone score = %d", zone->Score() );
|
2008-10-06 03:07:43 +08:00
|
|
|
zoneSet.insert( zone->Label() );
|
2005-05-16 17:27:06 +08:00
|
|
|
if ( config.opt_control && track_motion )
|
2005-02-24 18:43:29 +08:00
|
|
|
{
|
|
|
|
if ( zone->Score() > top_score )
|
|
|
|
{
|
|
|
|
top_score = zone->Score();
|
|
|
|
alarm_centre = zone->GetAlarmCentre();
|
|
|
|
}
|
|
|
|
}
|
2003-04-13 20:20:00 +08:00
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2003-04-13 20:20:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-11-21 18:38:41 +08:00
|
|
|
// Find all alarm pixels in exclusive zones
|
2003-04-13 20:20:00 +08:00
|
|
|
for ( int n_zone = 0; n_zone < n_zones; n_zone++ )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-04-13 20:20:00 +08:00
|
|
|
Zone *zone = zones[n_zone];
|
|
|
|
if ( !zone->IsExclusive() )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Checking exclusive zone %s", zone->Label() );
|
2003-04-13 20:20:00 +08:00
|
|
|
if ( zone->CheckAlarms( delta_image ) )
|
|
|
|
{
|
|
|
|
alarm = true;
|
|
|
|
score += zone->Score();
|
|
|
|
zone->SetAlarm();
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Zone is alarmed, zone score = %d", zone->Score() );
|
2008-10-06 03:07:43 +08:00
|
|
|
zoneSet.insert( zone->Label() );
|
2003-04-13 20:20:00 +08:00
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( top_score > 0 )
|
2005-02-24 18:43:29 +08:00
|
|
|
{
|
|
|
|
shared_data->alarm_x = alarm_centre.X();
|
|
|
|
shared_data->alarm_y = alarm_centre.Y();
|
|
|
|
|
2008-07-14 22:43:47 +08:00
|
|
|
Info( "Got alarm centre at %d,%d, at count %d", shared_data->alarm_x, shared_data->alarm_y, image_count );
|
2005-02-24 18:43:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shared_data->alarm_x = shared_data->alarm_y = -1;
|
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
delete delta_image;
|
|
|
|
// This is a small and innocent hack to prevent scores of 0 being returned in alarm state
|
|
|
|
return( score?score:alarm );
|
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
|
|
|
|
bool Monitor::DumpSettings( char *output, bool verbose )
|
|
|
|
{
|
|
|
|
output[0] = 0;
|
|
|
|
|
|
|
|
sprintf( output+strlen(output), "Id : %d\n", id );
|
|
|
|
sprintf( output+strlen(output), "Name : %s\n", name );
|
2006-01-22 02:02:46 +08:00
|
|
|
sprintf( output+strlen(output), "Type : %s\n", camera->IsLocal()?"Local":(camera->IsRemote()?"Remote":"File") );
|
2005-12-23 00:46:25 +08:00
|
|
|
if ( camera->IsLocal() )
|
|
|
|
{
|
2008-07-21 23:20:52 +08:00
|
|
|
sprintf( output+strlen(output), "Device : %s\n", ((LocalCamera *)camera)->Device().c_str() );
|
2005-12-23 00:46:25 +08:00
|
|
|
sprintf( output+strlen(output), "Channel : %d\n", ((LocalCamera *)camera)->Channel() );
|
|
|
|
sprintf( output+strlen(output), "Format : %d\n", ((LocalCamera *)camera)->Format() );
|
|
|
|
}
|
2006-01-22 02:02:46 +08:00
|
|
|
else if ( camera->IsRemote() )
|
2005-12-23 00:46:25 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
sprintf( output+strlen(output), "Protocol : %s\n", ((RemoteCamera *)camera)->Protocol().c_str() );
|
|
|
|
sprintf( output+strlen(output), "Host : %s\n", ((RemoteCamera *)camera)->Host().c_str() );
|
|
|
|
sprintf( output+strlen(output), "Port : %s\n", ((RemoteCamera *)camera)->Port().c_str() );
|
|
|
|
sprintf( output+strlen(output), "Path : %s\n", ((RemoteCamera *)camera)->Path().c_str() );
|
2005-12-23 00:46:25 +08:00
|
|
|
}
|
2006-01-22 02:02:46 +08:00
|
|
|
else if ( camera->IsFile() )
|
|
|
|
{
|
|
|
|
sprintf( output+strlen(output), "Path : %s\n", ((FileCamera *)camera)->Path() );
|
|
|
|
}
|
2009-01-28 23:02:33 +08:00
|
|
|
#if HAVE_LIBAVFORMAT
|
2009-01-20 23:01:32 +08:00
|
|
|
else if ( camera->IsFfmpeg() )
|
|
|
|
{
|
|
|
|
sprintf( output+strlen(output), "Path : %s\n", ((FfmpegCamera *)camera)->Path().c_str() );
|
|
|
|
}
|
2009-01-28 23:02:33 +08:00
|
|
|
#endif // HAVE_LIBAVFORMAT
|
2005-12-23 00:46:25 +08:00
|
|
|
sprintf( output+strlen(output), "Width : %d\n", camera->Width() );
|
|
|
|
sprintf( output+strlen(output), "Height : %d\n", camera->Height() );
|
2008-07-21 23:20:52 +08:00
|
|
|
if ( camera->IsLocal() )
|
|
|
|
{
|
|
|
|
sprintf( output+strlen(output), "Palette : %d\n", ((LocalCamera *)camera)->Palette() );
|
|
|
|
}
|
2005-12-23 00:46:25 +08:00
|
|
|
sprintf( output+strlen(output), "Colours : %d\n", camera->Colours() );
|
|
|
|
sprintf( output+strlen(output), "Event Prefix : %s\n", event_prefix );
|
|
|
|
sprintf( output+strlen(output), "Label Format : %s\n", label_format );
|
|
|
|
sprintf( output+strlen(output), "Label Coord : %d,%d\n", label_coord.X(), label_coord.Y() );
|
|
|
|
sprintf( output+strlen(output), "Image Buffer Count : %d\n", image_buffer_count );
|
|
|
|
sprintf( output+strlen(output), "Warmup Count : %d\n", warmup_count );
|
|
|
|
sprintf( output+strlen(output), "Pre Event Count : %d\n", pre_event_count );
|
|
|
|
sprintf( output+strlen(output), "Post Event Count : %d\n", post_event_count );
|
2007-08-30 02:11:09 +08:00
|
|
|
sprintf( output+strlen(output), "Stream Replay Buffer : %d\n", stream_replay_buffer );
|
2005-12-23 00:46:25 +08:00
|
|
|
sprintf( output+strlen(output), "Alarm Frame Count : %d\n", alarm_frame_count );
|
|
|
|
sprintf( output+strlen(output), "Section Length : %d\n", section_length );
|
|
|
|
sprintf( output+strlen(output), "Maximum FPS : %.2f\n", capture_delay?DT_PREC_3/capture_delay:0.0 );
|
2006-03-31 20:49:05 +08:00
|
|
|
sprintf( output+strlen(output), "Alarm Maximum FPS : %.2f\n", alarm_capture_delay?DT_PREC_3/alarm_capture_delay:0.0 );
|
2005-12-23 00:46:25 +08:00
|
|
|
sprintf( output+strlen(output), "Reference Blend %%ge : %d\n", ref_blend_perc );
|
|
|
|
sprintf( output+strlen(output), "Track Motion : %d\n", track_motion );
|
|
|
|
sprintf( output+strlen(output), "Function: %d - %s\n", function,
|
|
|
|
function==NONE?"None":(
|
2006-01-23 03:38:41 +08:00
|
|
|
function==MONITOR?"Monitor Only":(
|
2005-12-23 00:46:25 +08:00
|
|
|
function==MODECT?"Motion Detection":(
|
|
|
|
function==RECORD?"Continuous Record":(
|
|
|
|
function==MOCORD?"Continuous Record with Motion Detection":(
|
|
|
|
function==NODECT?"Externally Triggered only, no Motion Detection":"Unknown"
|
|
|
|
))))));
|
|
|
|
sprintf( output+strlen(output), "Zones : %d\n", n_zones );
|
|
|
|
for ( int i = 0; i < n_zones; i++ )
|
|
|
|
{
|
|
|
|
zones[i]->DumpSettings( output+strlen(output), verbose );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
2006-01-23 02:31:55 +08:00
|
|
|
|
2007-08-30 02:11:09 +08:00
|
|
|
bool MonitorStream::checkSwapPath( const char *path, bool create_path )
|
|
|
|
{
|
|
|
|
uid_t uid = getuid();
|
|
|
|
gid_t gid = getgid();
|
|
|
|
|
|
|
|
struct stat stat_buf;
|
|
|
|
if ( stat( path, &stat_buf ) < 0 )
|
|
|
|
{
|
|
|
|
if ( create_path && errno == ENOENT )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 3, "Swap path '%s' missing, creating", path );
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( mkdir( path, 0755 ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't mkdir %s: %s", path, strerror(errno));
|
2007-08-30 02:11:09 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
if ( stat( path, &stat_buf ) < 0 )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't stat '%s': %s", path, strerror(errno) );
|
2007-08-30 02:11:09 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't stat '%s': %s", path, strerror(errno) );
|
2007-08-30 02:11:09 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !S_ISDIR(stat_buf.st_mode) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Swap image path '%s' is not a directory", path );
|
2007-08-30 02:11:09 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
mode_t mask = 0;
|
|
|
|
if ( uid == stat_buf.st_uid )
|
|
|
|
{
|
|
|
|
// If we are the owner
|
|
|
|
mask = 00700;
|
|
|
|
}
|
|
|
|
else if ( gid == stat_buf.st_gid )
|
|
|
|
{
|
|
|
|
// If we are in the owner group
|
|
|
|
mask = 00070;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We are neither the owner nor in the group
|
|
|
|
mask = 00007;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (stat_buf.st_mode & mask) != mask )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Insufficient permissions on swap image path '%s'", path );
|
2007-08-30 02:11:09 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MonitorStream::processCommand( const CmdMsg *msg )
|
|
|
|
{
|
2008-07-23 17:56:38 +08:00
|
|
|
Debug( 2, "Got message, type %d, msg %d", msg->msg_type, msg->msg_data[0] );
|
2007-08-30 02:11:09 +08:00
|
|
|
// Check for incoming command
|
|
|
|
switch( (MsgCommand)msg->msg_data[0] )
|
|
|
|
{
|
|
|
|
case CMD_PAUSE :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got PAUSE command" );
|
2007-08-30 02:11:09 +08:00
|
|
|
|
|
|
|
// Set paused flag
|
|
|
|
paused = true;
|
|
|
|
// Set delayed flag
|
|
|
|
delayed = true;
|
|
|
|
last_frame_sent = TV_2_FLOAT( now );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_PLAY :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got PLAY command" );
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( paused )
|
|
|
|
{
|
|
|
|
// Clear paused flag
|
|
|
|
paused = false;
|
|
|
|
// Set delayed_play flag
|
|
|
|
delayed = true;
|
|
|
|
}
|
|
|
|
replay_rate = ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-03 21:16:47 +08:00
|
|
|
case CMD_VARPLAY :
|
|
|
|
{
|
|
|
|
Debug( 1, "Got VARPLAY command" );
|
|
|
|
if ( paused )
|
|
|
|
{
|
|
|
|
// Clear paused flag
|
|
|
|
paused = false;
|
|
|
|
// Set delayed_play flag
|
|
|
|
delayed = true;
|
|
|
|
}
|
2008-12-06 07:56:15 +08:00
|
|
|
replay_rate = ntohs(((unsigned char)msg->msg_data[2]<<8)|(unsigned char)msg->msg_data[1])-32768;
|
2008-11-03 21:16:47 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-08-30 02:11:09 +08:00
|
|
|
case CMD_STOP :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got STOP command" );
|
2007-08-30 02:11:09 +08:00
|
|
|
|
|
|
|
// Clear paused flag
|
|
|
|
paused = false;
|
|
|
|
// Clear delayed_play flag
|
|
|
|
delayed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_FASTFWD :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got FAST FWD command" );
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( paused )
|
|
|
|
{
|
|
|
|
// Clear paused flag
|
|
|
|
paused = false;
|
|
|
|
// Set delayed_play flag
|
|
|
|
delayed = true;
|
|
|
|
}
|
|
|
|
// Set play rate
|
|
|
|
switch ( replay_rate )
|
|
|
|
{
|
|
|
|
case 2 * ZM_RATE_BASE :
|
|
|
|
replay_rate = 5 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
case 5 * ZM_RATE_BASE :
|
|
|
|
replay_rate = 10 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
case 10 * ZM_RATE_BASE :
|
|
|
|
replay_rate = 25 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
case 25 * ZM_RATE_BASE :
|
|
|
|
case 50 * ZM_RATE_BASE :
|
|
|
|
replay_rate = 50 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
replay_rate = 2 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_SLOWFWD :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got SLOW FWD command" );
|
2007-08-30 02:11:09 +08:00
|
|
|
// Set paused flag
|
|
|
|
paused = true;
|
|
|
|
// Set delayed flag
|
|
|
|
delayed = true;
|
|
|
|
// Set play rate
|
|
|
|
replay_rate = ZM_RATE_BASE;
|
|
|
|
// Set step
|
|
|
|
step = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_SLOWREV :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got SLOW REV command" );
|
2007-08-30 02:11:09 +08:00
|
|
|
// Set paused flag
|
|
|
|
paused = true;
|
|
|
|
// Set delayed flag
|
|
|
|
delayed = true;
|
|
|
|
// Set play rate
|
|
|
|
replay_rate = ZM_RATE_BASE;
|
|
|
|
// Set step
|
|
|
|
step = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_FASTREV :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got FAST REV command" );
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( paused )
|
|
|
|
{
|
|
|
|
// Clear paused flag
|
|
|
|
paused = false;
|
|
|
|
// Set delayed_play flag
|
|
|
|
delayed = true;
|
|
|
|
}
|
|
|
|
// Set play rate
|
|
|
|
switch ( replay_rate )
|
|
|
|
{
|
|
|
|
case -2 * ZM_RATE_BASE :
|
|
|
|
replay_rate = -5 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
case -5 * ZM_RATE_BASE :
|
|
|
|
replay_rate = -10 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
case -10 * ZM_RATE_BASE :
|
|
|
|
replay_rate = -25 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
case -25 * ZM_RATE_BASE :
|
|
|
|
case -50 * ZM_RATE_BASE :
|
|
|
|
replay_rate = -50 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
replay_rate = -2 * ZM_RATE_BASE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_ZOOMIN :
|
|
|
|
{
|
|
|
|
x = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
|
|
|
|
y = ((unsigned char)msg->msg_data[3]<<8)|(unsigned char)msg->msg_data[4];
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got ZOOM IN command, to %d,%d", x, y );
|
2007-08-30 02:11:09 +08:00
|
|
|
switch ( zoom )
|
|
|
|
{
|
|
|
|
case 100:
|
|
|
|
zoom = 150;
|
|
|
|
break;
|
|
|
|
case 150:
|
|
|
|
zoom = 200;
|
|
|
|
break;
|
|
|
|
case 200:
|
|
|
|
zoom = 300;
|
|
|
|
break;
|
|
|
|
case 300:
|
|
|
|
zoom = 400;
|
|
|
|
break;
|
|
|
|
case 400:
|
|
|
|
default :
|
|
|
|
zoom = 500;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_ZOOMOUT :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got ZOOM OUT command" );
|
2007-08-30 02:11:09 +08:00
|
|
|
switch ( zoom )
|
|
|
|
{
|
|
|
|
case 500:
|
|
|
|
zoom = 400;
|
|
|
|
break;
|
|
|
|
case 400:
|
|
|
|
zoom = 300;
|
|
|
|
break;
|
|
|
|
case 300:
|
|
|
|
zoom = 200;
|
|
|
|
break;
|
|
|
|
case 200:
|
|
|
|
zoom = 150;
|
|
|
|
break;
|
|
|
|
case 150:
|
|
|
|
default :
|
|
|
|
zoom = 100;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_PAN :
|
|
|
|
{
|
|
|
|
x = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
|
|
|
|
y = ((unsigned char)msg->msg_data[3]<<8)|(unsigned char)msg->msg_data[4];
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got PAN command, to %d,%d", x, y );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_SCALE :
|
|
|
|
{
|
|
|
|
scale = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got SCALE command, to %d", scale );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_QUERY :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Got QUERY command, sending STATUS" );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default :
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Got unexpected command %d", msg->msg_data[0] );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
2008-07-14 21:54:50 +08:00
|
|
|
int id;
|
2007-08-30 02:11:09 +08:00
|
|
|
double fps;
|
|
|
|
int state;
|
|
|
|
int buffer_level;
|
|
|
|
bool delayed;
|
|
|
|
bool paused;
|
|
|
|
int rate;
|
|
|
|
double delay;
|
|
|
|
int zoom;
|
2007-11-05 01:36:30 +08:00
|
|
|
bool enabled;
|
|
|
|
bool forced;
|
2007-08-30 02:11:09 +08:00
|
|
|
} status_data;
|
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
status_data.id = monitor->Id();
|
2007-08-30 02:11:09 +08:00
|
|
|
status_data.fps = monitor->GetFPS();
|
|
|
|
status_data.state = monitor->shared_data->state;
|
2008-10-30 07:08:21 +08:00
|
|
|
if ( playback_buffer > 0 )
|
|
|
|
status_data.buffer_level = (MOD_ADD( (temp_write_index-temp_read_index), 0, temp_image_buffer_count )*100)/temp_image_buffer_count;
|
|
|
|
else
|
|
|
|
status_data.buffer_level = 0;
|
2007-08-30 02:11:09 +08:00
|
|
|
status_data.delayed = delayed;
|
|
|
|
status_data.paused = paused;
|
|
|
|
status_data.rate = replay_rate;
|
|
|
|
status_data.delay = TV_2_FLOAT( now ) - TV_2_FLOAT( last_frame_timestamp );
|
|
|
|
status_data.zoom = zoom;
|
2007-11-05 01:36:30 +08:00
|
|
|
//status_data.enabled = monitor->shared_data->active;
|
|
|
|
status_data.enabled = monitor->trigger_data->trigger_state!=Monitor::TRIGGER_OFF;
|
|
|
|
status_data.forced = monitor->trigger_data->trigger_state==Monitor::TRIGGER_ON;
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 2, "L:%d, D:%d, P:%d, R:%d, d:%.3f, Z:%d, E:%d F:%d",
|
2007-08-30 02:11:09 +08:00
|
|
|
status_data.buffer_level,
|
|
|
|
status_data.delayed,
|
|
|
|
status_data.paused,
|
|
|
|
status_data.rate,
|
|
|
|
status_data.delay,
|
2007-11-05 01:36:30 +08:00
|
|
|
status_data.zoom,
|
|
|
|
status_data.enabled,
|
|
|
|
status_data.forced
|
2008-07-14 22:43:47 +08:00
|
|
|
);
|
2007-08-30 02:11:09 +08:00
|
|
|
|
|
|
|
DataMsg status_msg;
|
|
|
|
status_msg.msg_type = MSG_DATA_WATCH;
|
|
|
|
memcpy( &status_msg.msg_data, &status_data, sizeof(status_msg.msg_data) );
|
|
|
|
int nbytes = 0;
|
|
|
|
if ( (nbytes = sendto( sd, &status_msg, sizeof(status_msg), 0, (sockaddr *)&rem_addr, sizeof(rem_addr) )) < 0 )
|
|
|
|
{
|
|
|
|
//if ( errno != EAGAIN )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't sendto on sd %d: %s", sd, strerror(errno) );
|
2007-08-30 02:11:09 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFrameRate( monitor->GetFPS() );
|
|
|
|
}
|
|
|
|
|
2009-01-22 01:37:39 +08:00
|
|
|
bool MonitorStream::sendFrame( const char *filepath, struct timeval *timestamp )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
|
|
|
bool send_raw = ((scale>=ZM_SCALE_BASE)&&(zoom==ZM_SCALE_BASE));
|
|
|
|
|
|
|
|
if ( type != STREAM_JPEG )
|
|
|
|
send_raw = false;
|
|
|
|
if ( !config.timestamp_on_capture && timestamp )
|
|
|
|
send_raw = false;
|
|
|
|
|
|
|
|
if ( !send_raw )
|
|
|
|
{
|
|
|
|
Image temp_image( filepath );
|
|
|
|
|
2009-01-22 01:37:39 +08:00
|
|
|
return( sendFrame( &temp_image, timestamp ) );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int img_buffer_size = 0;
|
|
|
|
static unsigned char img_buffer[ZM_MAX_IMAGE_SIZE];
|
|
|
|
|
|
|
|
FILE *fdj = NULL;
|
|
|
|
if ( (fdj = fopen( filepath, "r" )) )
|
|
|
|
{
|
|
|
|
img_buffer_size = fread( img_buffer, 1, sizeof(img_buffer), fdj );
|
|
|
|
fclose( fdj );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't open %s: %s", filepath, strerror(errno) );
|
2009-01-22 01:37:39 +08:00
|
|
|
return( false );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( stdout, "--ZoneMinderFrame\r\n" );
|
|
|
|
fprintf( stdout, "Content-Length: %d\r\n", img_buffer_size );
|
|
|
|
fprintf( stdout, "Content-Type: image/jpeg\r\n\r\n" );
|
2009-01-22 01:37:39 +08:00
|
|
|
if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) < 0 )
|
|
|
|
{
|
|
|
|
Error( "Unable to send stream frame: %s", strerror(errno) );
|
|
|
|
return( false );
|
|
|
|
}
|
2007-08-30 02:11:09 +08:00
|
|
|
fprintf( stdout, "\r\n\r\n" );
|
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
last_frame_sent = TV_2_FLOAT( now );
|
2009-01-22 01:37:39 +08:00
|
|
|
|
|
|
|
return( true );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
2009-01-22 01:37:39 +08:00
|
|
|
return( false );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 01:37:39 +08:00
|
|
|
bool MonitorStream::sendFrame( Image *image, struct timeval *timestamp )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
|
|
|
if ( !config.timestamp_on_capture && timestamp )
|
|
|
|
{
|
|
|
|
monitor->TimestampImage( image, timestamp );
|
|
|
|
}
|
|
|
|
|
|
|
|
Image *send_image = prepareImage( image );
|
|
|
|
|
|
|
|
#if HAVE_LIBAVCODEC
|
|
|
|
if ( type == STREAM_MPEG )
|
|
|
|
{
|
|
|
|
if ( !vid_stream )
|
|
|
|
{
|
|
|
|
vid_stream = new VideoStream( "pipe:", format, bitrate, effective_fps, send_image->Colours(), send_image->Width(), send_image->Height() );
|
|
|
|
fprintf( stdout, "Content-type: %s\r\n\r\n", vid_stream->MimeType() );
|
|
|
|
vid_stream->OpenStream();
|
|
|
|
}
|
|
|
|
static struct timeval base_time;
|
|
|
|
struct DeltaTimeval delta_time;
|
|
|
|
if ( !frame_count )
|
|
|
|
base_time = *timestamp;
|
|
|
|
DELTA_TIMEVAL( delta_time, *timestamp, base_time, DT_PREC_3 );
|
2008-03-13 21:36:12 +08:00
|
|
|
/* double pts = */ vid_stream->EncodeFrame( send_image->Buffer(), send_image->Size(), config.mpeg_timed_frames, delta_time.delta );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // HAVE_LIBAVCODEC
|
|
|
|
{
|
|
|
|
static unsigned char temp_img_buffer[ZM_MAX_IMAGE_SIZE];
|
|
|
|
|
|
|
|
int img_buffer_size = 0;
|
|
|
|
unsigned char *img_buffer = temp_img_buffer;
|
|
|
|
|
|
|
|
fprintf( stdout, "--ZoneMinderFrame\r\n" );
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case STREAM_JPEG :
|
|
|
|
send_image->EncodeJpeg( img_buffer, &img_buffer_size );
|
|
|
|
fprintf( stdout, "Content-Type: image/jpeg\r\n" );
|
|
|
|
break;
|
|
|
|
case STREAM_RAW :
|
|
|
|
fprintf( stdout, "Content-Type: image/x-rgb\r\n" );
|
|
|
|
img_buffer = send_image->Buffer();
|
|
|
|
img_buffer_size = send_image->Size();
|
|
|
|
break;
|
|
|
|
case STREAM_ZIP :
|
|
|
|
fprintf( stdout, "Content-Type: image/x-rgbz\r\n" );
|
|
|
|
unsigned long zip_buffer_size;
|
|
|
|
send_image->Zip( img_buffer, &zip_buffer_size );
|
|
|
|
img_buffer_size = zip_buffer_size;
|
|
|
|
break;
|
2008-03-13 21:36:12 +08:00
|
|
|
default :
|
2008-07-14 22:43:47 +08:00
|
|
|
Fatal( "Unexpected frame type %d", type );
|
2008-03-13 21:36:12 +08:00
|
|
|
break;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
fprintf( stdout, "Content-Length: %d\r\n\r\n", img_buffer_size );
|
2009-01-22 01:37:39 +08:00
|
|
|
if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) < 0 )
|
|
|
|
{
|
|
|
|
Error( "Unable to send stream frame: %s", strerror(errno) );
|
|
|
|
return( false );
|
|
|
|
}
|
2007-08-30 02:11:09 +08:00
|
|
|
fprintf( stdout, "\r\n\r\n" );
|
|
|
|
fflush( stdout );
|
|
|
|
}
|
|
|
|
last_frame_sent = TV_2_FLOAT( now );
|
2009-01-22 01:37:39 +08:00
|
|
|
return( true );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MonitorStream::runStream()
|
|
|
|
{
|
2007-11-11 23:57:54 +08:00
|
|
|
if ( type == STREAM_SINGLE )
|
|
|
|
{
|
|
|
|
// Not yet migrated over to stream class
|
|
|
|
monitor->SingleImage( scale );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-30 02:11:09 +08:00
|
|
|
openComms();
|
|
|
|
|
|
|
|
checkInitialised();
|
|
|
|
|
|
|
|
updateFrameRate( monitor->GetFPS() );
|
|
|
|
|
|
|
|
if ( type == STREAM_JPEG )
|
|
|
|
fprintf( stdout, "Content-Type: multipart/x-mixed-replace;boundary=ZoneMinderFrame\r\n\r\n" );
|
|
|
|
|
|
|
|
int last_read_index = monitor->image_buffer_count;
|
|
|
|
|
|
|
|
time_t stream_start_time;
|
|
|
|
time( &stream_start_time );
|
|
|
|
|
|
|
|
frame_count = 0;
|
|
|
|
|
|
|
|
temp_image_buffer = 0;
|
2008-10-30 07:08:21 +08:00
|
|
|
temp_image_buffer_count = playback_buffer;
|
2007-08-30 02:11:09 +08:00
|
|
|
temp_read_index = temp_image_buffer_count;
|
|
|
|
temp_write_index = temp_image_buffer_count;
|
|
|
|
|
|
|
|
char swap_path[PATH_MAX] = "";
|
2008-10-30 07:08:21 +08:00
|
|
|
bool buffered_playback = false;
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2008-10-30 07:08:21 +08:00
|
|
|
if ( connkey && playback_buffer > 0 )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 2, "Checking swap image location" );
|
|
|
|
Debug( 3, "Checking swap image path" );
|
2007-08-30 02:11:09 +08:00
|
|
|
strncpy( swap_path, config.path_swap, sizeof(swap_path) );
|
|
|
|
if ( checkSwapPath( swap_path, false ) )
|
|
|
|
{
|
|
|
|
snprintf( &(swap_path[strlen(swap_path)]), sizeof(swap_path)-strlen(swap_path), "/zmswap-m%d", monitor->Id() );
|
|
|
|
if ( checkSwapPath( swap_path, true ) )
|
|
|
|
{
|
|
|
|
snprintf( &(swap_path[strlen(swap_path)]), sizeof(swap_path)-strlen(swap_path), "/zmswap-q%06d", connkey );
|
|
|
|
if ( checkSwapPath( swap_path, true ) )
|
|
|
|
{
|
2008-10-30 07:08:21 +08:00
|
|
|
buffered_playback = true;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-30 07:08:21 +08:00
|
|
|
if ( !buffered_playback )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Unable to validate swap image path, disabling buffered playback" );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 2, "Assigning temporary buffer" );
|
2007-08-30 02:11:09 +08:00
|
|
|
temp_image_buffer = new SwapImage[temp_image_buffer_count];
|
|
|
|
memset( temp_image_buffer, 0, sizeof(*temp_image_buffer)*temp_image_buffer_count );
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 2, "Assigned temporary buffer" );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( !zm_terminate )
|
|
|
|
{
|
|
|
|
bool got_command = false;
|
|
|
|
if ( feof( stdout ) || ferror( stdout ) )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-13 21:36:12 +08:00
|
|
|
gettimeofday( &now, NULL );
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2008-10-30 07:08:21 +08:00
|
|
|
if ( connkey )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
|
|
|
got_command = checkCommandQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool frame_sent = false;
|
2008-10-30 07:08:21 +08:00
|
|
|
if ( buffered_playback && delayed )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
|
|
|
if ( temp_read_index == temp_write_index )
|
|
|
|
{
|
|
|
|
// Go back to live viewing
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Exceeded temporary streaming buffer" );
|
2007-08-30 02:11:09 +08:00
|
|
|
// Clear paused flag
|
|
|
|
paused = false;
|
|
|
|
// Clear delayed_play flag
|
|
|
|
delayed = false;
|
|
|
|
replay_rate = ZM_RATE_BASE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( !paused )
|
|
|
|
{
|
|
|
|
int temp_index = MOD_ADD( temp_read_index, 0, temp_image_buffer_count );
|
2008-07-14 22:43:47 +08:00
|
|
|
//Debug( 3, "tri: %d, ti: %d", temp_read_index, temp_index );
|
2007-08-30 02:11:09 +08:00
|
|
|
SwapImage *swap_image = &temp_image_buffer[temp_index];
|
|
|
|
|
|
|
|
if ( !swap_image->valid )
|
|
|
|
{
|
|
|
|
paused = true;
|
|
|
|
delayed = true;
|
|
|
|
temp_read_index = MOD_ADD( temp_read_index, (replay_rate>=0?-1:1), temp_image_buffer_count );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
//Debug( 3, "siT: %f, lfT: %f", TV_2_FLOAT( swap_image->timestamp ), TV_2_FLOAT( last_frame_timestamp ) );
|
2007-08-30 02:11:09 +08:00
|
|
|
double expected_delta_time = ((TV_2_FLOAT( swap_image->timestamp ) - TV_2_FLOAT( last_frame_timestamp )) * ZM_RATE_BASE)/replay_rate;
|
|
|
|
double actual_delta_time = TV_2_FLOAT( now ) - last_frame_sent;
|
|
|
|
|
2008-07-14 22:43:47 +08:00
|
|
|
//Debug( 3, "eDT: %.3lf, aDT: %.3f, lFS:%.3f, NOW:%.3f", expected_delta_time, actual_delta_time, last_frame_sent, TV_2_FLOAT( now ) );
|
2007-08-30 02:11:09 +08:00
|
|
|
// If the next frame is due
|
|
|
|
if ( actual_delta_time > expected_delta_time )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
//Debug( 2, "eDT: %.3lf, aDT: %.3f", expected_delta_time, actual_delta_time );
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( temp_index%frame_mod == 0 )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 2, "Sending delayed frame %d", temp_index );
|
2007-08-30 02:11:09 +08:00
|
|
|
// Send the next frame
|
2009-01-22 01:37:39 +08:00
|
|
|
if ( !sendFrame( temp_image_buffer[temp_index].file_name, &temp_image_buffer[temp_index].timestamp ) )
|
|
|
|
zm_terminate = true;
|
2007-08-30 02:11:09 +08:00
|
|
|
memcpy( &last_frame_timestamp, &(swap_image->timestamp), sizeof(last_frame_timestamp) );
|
|
|
|
frame_sent = true;
|
|
|
|
}
|
|
|
|
temp_read_index = MOD_ADD( temp_read_index, (replay_rate>0?1:-1), temp_image_buffer_count );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( step != 0 )
|
|
|
|
{
|
|
|
|
temp_read_index = MOD_ADD( temp_read_index, (step>0?1:-1), temp_image_buffer_count );
|
|
|
|
|
|
|
|
SwapImage *swap_image = &temp_image_buffer[temp_read_index];
|
|
|
|
|
|
|
|
// Send the next frame
|
2009-01-22 01:37:39 +08:00
|
|
|
if ( !sendFrame( temp_image_buffer[temp_read_index].file_name, &temp_image_buffer[temp_read_index].timestamp ) )
|
|
|
|
zm_terminate = true;
|
2007-08-30 02:11:09 +08:00
|
|
|
memcpy( &last_frame_timestamp, &(swap_image->timestamp), sizeof(last_frame_timestamp) );
|
|
|
|
frame_sent = true;
|
|
|
|
step = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int temp_index = MOD_ADD( temp_read_index, 0, temp_image_buffer_count );
|
|
|
|
|
|
|
|
double actual_delta_time = TV_2_FLOAT( now ) - last_frame_sent;
|
|
|
|
if ( got_command || actual_delta_time > 5 )
|
|
|
|
{
|
|
|
|
// Send keepalive
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 2, "Sending keepalive frame %d", temp_index );
|
2007-08-30 02:11:09 +08:00
|
|
|
// Send the next frame
|
2009-01-22 01:37:39 +08:00
|
|
|
if ( !sendFrame( temp_image_buffer[temp_index].file_name, &temp_image_buffer[temp_index].timestamp ) )
|
|
|
|
zm_terminate = true;
|
2007-08-30 02:11:09 +08:00
|
|
|
frame_sent = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( temp_read_index == temp_write_index )
|
|
|
|
{
|
|
|
|
// Go back to live viewing
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Rewound over write index, resuming live play" );
|
2007-08-30 02:11:09 +08:00
|
|
|
// Clear paused flag
|
|
|
|
paused = false;
|
|
|
|
// Clear delayed_play flag
|
|
|
|
delayed = false;
|
|
|
|
replay_rate = ZM_RATE_BASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( last_read_index != monitor->shared_data->last_write_index )
|
|
|
|
{
|
|
|
|
int index = monitor->shared_data->last_write_index%monitor->image_buffer_count;
|
|
|
|
last_read_index = monitor->shared_data->last_write_index;
|
2008-07-14 22:43:47 +08:00
|
|
|
//Debug( 1, "%d: %x - %x", index, image_buffer[index].image, image_buffer[index].image->buffer );
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( (frame_mod == 1) || ((frame_count%frame_mod) == 0) )
|
|
|
|
{
|
|
|
|
if ( !paused && !delayed )
|
|
|
|
{
|
|
|
|
// Send the next frame
|
|
|
|
Monitor::Snapshot *snap = &monitor->image_buffer[index];
|
|
|
|
|
2009-01-22 01:37:39 +08:00
|
|
|
if ( !sendFrame( snap->image, snap->timestamp ) )
|
|
|
|
zm_terminate = true;
|
2007-08-30 02:11:09 +08:00
|
|
|
memcpy( &last_frame_timestamp, snap->timestamp, sizeof(last_frame_timestamp) );
|
|
|
|
frame_sent = true;
|
|
|
|
|
|
|
|
temp_read_index = temp_write_index;
|
|
|
|
}
|
|
|
|
}
|
2008-10-30 07:08:21 +08:00
|
|
|
if ( buffered_playback )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
|
|
|
if ( monitor->shared_data->valid )
|
|
|
|
{
|
|
|
|
if ( monitor->image_buffer[index].timestamp->tv_sec )
|
|
|
|
{
|
|
|
|
int temp_index = temp_write_index%temp_image_buffer_count;
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 2, "Storing frame %d", temp_index );
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( !temp_image_buffer[temp_index].valid )
|
|
|
|
{
|
|
|
|
snprintf( temp_image_buffer[temp_index].file_name, sizeof(temp_image_buffer[0].file_name), "%s/zmswap-i%05d.jpg", swap_path, temp_index );
|
|
|
|
temp_image_buffer[temp_index].valid = true;
|
|
|
|
}
|
|
|
|
memcpy( &(temp_image_buffer[temp_index].timestamp), monitor->image_buffer[index].timestamp, sizeof(temp_image_buffer[0].timestamp) );
|
|
|
|
monitor->image_buffer[index].image->WriteJpeg( temp_image_buffer[temp_index].file_name, config.jpeg_file_quality );
|
|
|
|
temp_write_index = MOD_ADD( temp_write_index, 1, temp_image_buffer_count );
|
|
|
|
if ( temp_write_index == temp_read_index )
|
|
|
|
{
|
|
|
|
// Go back to live viewing
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Exceeded temporary buffer, resuming live play" );
|
2007-08-30 02:11:09 +08:00
|
|
|
// Clear paused flag
|
|
|
|
paused = false;
|
|
|
|
// Clear delayed_play flag
|
|
|
|
delayed = false;
|
|
|
|
replay_rate = ZM_RATE_BASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Unable to store frame as timestamp invalid" );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Unable to store frame as shared memory invalid" );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
frame_count++;
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
usleep( (1000000 * ZM_RATE_BASE)/((base_fps?base_fps:1)*abs(replay_rate*2)) );
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( ttl )
|
|
|
|
{
|
|
|
|
if ( (now.tv_sec - stream_start_time) > ttl )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-30 07:08:21 +08:00
|
|
|
if ( buffered_playback )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
|
|
|
char swap_path[PATH_MAX] = "";
|
|
|
|
|
|
|
|
snprintf( swap_path, sizeof(swap_path), "%s/zmswap-m%d/zmswap-q%06d", config.path_swap, monitor->Id(), connkey );
|
2008-07-16 16:35:59 +08:00
|
|
|
Debug( 1, "Cleaning swap files from %s", swap_path );
|
2007-08-30 02:11:09 +08:00
|
|
|
struct stat stat_buf;
|
|
|
|
if ( stat( swap_path, &stat_buf ) < 0 )
|
|
|
|
{
|
|
|
|
if ( errno != ENOENT )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't stat '%s': %s", swap_path, strerror(errno) );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( !S_ISDIR(stat_buf.st_mode) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Swap image path '%s' is not a directory", swap_path );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char glob_pattern[PATH_MAX] = "";
|
|
|
|
|
|
|
|
snprintf( glob_pattern, sizeof(glob_pattern), "%s/*.*", swap_path );
|
|
|
|
glob_t pglob;
|
|
|
|
int glob_status = glob( glob_pattern, 0, 0, &pglob );
|
|
|
|
if ( glob_status != 0 )
|
|
|
|
{
|
|
|
|
if ( glob_status < 0 )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't glob '%s': %s", glob_pattern, strerror(errno) );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Debug( 1, "Can't glob '%s': %d", glob_pattern, glob_status );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < pglob.gl_pathc; i++ )
|
|
|
|
{
|
|
|
|
if ( unlink( pglob.gl_pathv[i] ) < 0 )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't unlink '%s': %s", pglob.gl_pathv[i], strerror(errno) );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
globfree( &pglob );
|
|
|
|
if ( rmdir( swap_path ) < 0 )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't rmdir '%s': %s", swap_path, strerror(errno) );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closeComms();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Monitor::SingleImage( int scale)
|
|
|
|
{
|
|
|
|
int img_buffer_size = 0;
|
|
|
|
static JOCTET img_buffer[ZM_MAX_IMAGE_SIZE];
|
|
|
|
Image scaled_image;
|
|
|
|
int index = shared_data->last_write_index%image_buffer_count;
|
|
|
|
Snapshot *snap = &image_buffer[index];
|
|
|
|
Image *snap_image = snap->image;
|
|
|
|
|
|
|
|
if ( scale != ZM_SCALE_BASE )
|
|
|
|
{
|
|
|
|
scaled_image.Assign( *snap_image );
|
|
|
|
scaled_image.Scale( scale );
|
|
|
|
snap_image = &scaled_image;
|
|
|
|
}
|
|
|
|
if ( !config.timestamp_on_capture )
|
|
|
|
{
|
|
|
|
TimestampImage( snap_image, snap->timestamp );
|
|
|
|
}
|
|
|
|
snap_image->EncodeJpeg( img_buffer, &img_buffer_size );
|
|
|
|
|
|
|
|
fprintf( stdout, "Content-Length: %d\r\n", img_buffer_size );
|
|
|
|
fprintf( stdout, "Content-Type: image/jpeg\r\n\r\n" );
|
|
|
|
fwrite( img_buffer, img_buffer_size, 1, stdout );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Monitor::SingleImageRaw( int scale)
|
|
|
|
{
|
|
|
|
Image scaled_image;
|
|
|
|
int index = shared_data->last_write_index%image_buffer_count;
|
|
|
|
Snapshot *snap = &image_buffer[index];
|
|
|
|
Image *snap_image = snap->image;
|
|
|
|
|
|
|
|
if ( scale != ZM_SCALE_BASE )
|
|
|
|
{
|
|
|
|
scaled_image.Assign( *snap_image );
|
|
|
|
scaled_image.Scale( scale );
|
|
|
|
snap_image = &scaled_image;
|
|
|
|
}
|
|
|
|
if ( !config.timestamp_on_capture )
|
|
|
|
{
|
|
|
|
TimestampImage( snap_image, snap->timestamp );
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( stdout, "Content-Length: %d\r\n", snap_image->Size() );
|
|
|
|
fprintf( stdout, "Content-Type: image/x-rgb\r\n\r\n" );
|
|
|
|
fwrite( snap_image->Buffer(), snap_image->Size(), 1, stdout );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Monitor::SingleImageZip( int scale)
|
|
|
|
{
|
|
|
|
unsigned long img_buffer_size = 0;
|
|
|
|
static Bytef img_buffer[ZM_MAX_IMAGE_SIZE];
|
|
|
|
Image scaled_image;
|
|
|
|
int index = shared_data->last_write_index%image_buffer_count;
|
|
|
|
Snapshot *snap = &image_buffer[index];
|
|
|
|
Image *snap_image = snap->image;
|
|
|
|
|
|
|
|
if ( scale != ZM_SCALE_BASE )
|
|
|
|
{
|
|
|
|
scaled_image.Assign( *snap_image );
|
|
|
|
scaled_image.Scale( scale );
|
|
|
|
snap_image = &scaled_image;
|
|
|
|
}
|
|
|
|
if ( !config.timestamp_on_capture )
|
|
|
|
{
|
|
|
|
TimestampImage( snap_image, snap->timestamp );
|
|
|
|
}
|
|
|
|
snap_image->Zip( img_buffer, &img_buffer_size );
|
|
|
|
|
2008-03-13 21:36:12 +08:00
|
|
|
fprintf( stdout, "Content-Length: %ld\r\n", img_buffer_size );
|
2007-08-30 02:11:09 +08:00
|
|
|
fprintf( stdout, "Content-Type: image/x-rgbz\r\n\r\n" );
|
|
|
|
fwrite( img_buffer, img_buffer_size, 1, stdout );
|
|
|
|
}
|