2002-09-24 06:08:50 +08:00
|
|
|
//
|
2002-12-10 21:21:41 +08:00
|
|
|
// ZoneMinder Capture Daemon, $Date$, $Revision$
|
2005-02-24 22:40:14 +08:00
|
|
|
// Copyright (C) 2003, 2004, 2005 Philip Coombes
|
2002-09-24 06:08:50 +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.
|
|
|
|
//
|
|
|
|
|
2002-12-24 20:02:08 +08:00
|
|
|
#include <getopt.h>
|
2003-03-26 20:00:16 +08:00
|
|
|
#include <signal.h>
|
2003-03-27 06:12:25 +08:00
|
|
|
#include <values.h>
|
2003-03-26 20:00:16 +08:00
|
|
|
|
2002-09-16 17:19:24 +08:00
|
|
|
#include "zm.h"
|
2003-03-26 20:00:16 +08:00
|
|
|
#include "zm_db.h"
|
|
|
|
#include "zm_monitor.h"
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2003-03-26 20:00:16 +08:00
|
|
|
bool zmc_terminate = false;
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2004-01-15 05:26:47 +08:00
|
|
|
void zmc_term_handler( int /* signal */ )
|
2002-09-23 18:11:04 +08:00
|
|
|
{
|
|
|
|
Info(( "Got TERM signal, exiting" ));
|
2003-03-26 20:00:16 +08:00
|
|
|
zmc_terminate = true;
|
2002-09-23 18:11:04 +08:00
|
|
|
}
|
|
|
|
|
2002-12-24 20:02:08 +08:00
|
|
|
void Usage()
|
2002-09-16 17:19:24 +08:00
|
|
|
{
|
2004-02-16 03:17:38 +08:00
|
|
|
fprintf( stderr, "zmc -d <device_id> or -m <monitor_id>\n" );
|
|
|
|
|
2002-12-24 20:02:08 +08:00
|
|
|
fprintf( stderr, "Options:\n" );
|
2003-05-02 18:14:51 +08:00
|
|
|
fprintf( stderr, " -d, --device <device_id> : For local cameras, device to access 0=>/dev/video0 etc\n" );
|
|
|
|
fprintf( stderr, " -H <host> -P <port> -p <path> : For remote cameras\n" );
|
2004-02-16 03:17:38 +08:00
|
|
|
fprintf( stderr, " -m, --monitor <monitor_id> : For sources associated with a single monitor\n" );
|
2003-05-02 18:14:51 +08:00
|
|
|
fprintf( stderr, " -h, --help : This screen\n" );
|
2002-12-24 20:02:08 +08:00
|
|
|
exit( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
int device = -1;
|
2004-01-15 05:26:47 +08:00
|
|
|
const char *host = "";
|
|
|
|
const char *port = "";
|
|
|
|
const char *path = "";
|
2004-02-16 03:17:38 +08:00
|
|
|
int monitor_id = -1;
|
2002-12-24 20:02:08 +08:00
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"device", 1, 0, 'd'},
|
2003-03-26 20:00:16 +08:00
|
|
|
{"host", 1, 0, 'H'},
|
|
|
|
{"port", 1, 0, 'P'},
|
2004-02-16 03:17:38 +08:00
|
|
|
{"path", 1, 0, 'p'},
|
|
|
|
{"monitor", 1, 0, 'm'},
|
2002-12-24 20:02:08 +08:00
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int option_index = 0;
|
|
|
|
|
2004-02-16 03:17:38 +08:00
|
|
|
int c = getopt_long (argc, argv, "d:H:P:p:m:h", long_options, &option_index);
|
2002-12-24 20:02:08 +08:00
|
|
|
if (c == -1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
device = atoi(optarg);
|
|
|
|
break;
|
2003-03-26 20:00:16 +08:00
|
|
|
case 'H':
|
|
|
|
host = optarg;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
port = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
path = optarg;
|
|
|
|
break;
|
2004-02-16 03:17:38 +08:00
|
|
|
case 'm':
|
|
|
|
monitor_id = atoi(optarg);
|
|
|
|
break;
|
2002-12-24 20:02:08 +08:00
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
Usage();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//fprintf( stderr, "?? getopt returned character code 0%o ??\n", c );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Extraneous options, " );
|
|
|
|
while (optind < argc)
|
|
|
|
printf ("%s ", argv[optind++]);
|
|
|
|
printf ("\n");
|
|
|
|
Usage();
|
|
|
|
}
|
|
|
|
|
2004-02-16 03:17:38 +08:00
|
|
|
if (( device >= 0 && host[0] )
|
|
|
|
|| ( device >= 0 && monitor_id > 0 )
|
|
|
|
|| ( monitor_id > 0 && host[0] ))
|
2002-12-24 20:02:08 +08:00
|
|
|
{
|
2004-02-16 03:17:38 +08:00
|
|
|
fprintf( stderr, "Only one of device or host/port/path or monitor id allowed\n" );
|
2002-12-24 20:02:08 +08:00
|
|
|
Usage();
|
|
|
|
exit( 0 );
|
|
|
|
}
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2004-02-16 03:17:38 +08:00
|
|
|
if ( device < 0 && !host[0] && monitor_id <= 0 )
|
2003-03-26 20:00:16 +08:00
|
|
|
{
|
2004-02-16 03:17:38 +08:00
|
|
|
fprintf( stderr, "One of device or host/port/path or monitor id must be specified\n" );
|
2003-03-26 20:00:16 +08:00
|
|
|
Usage();
|
|
|
|
exit( 0 );
|
|
|
|
}
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2005-01-16 01:45:51 +08:00
|
|
|
char dbg_id_string[16];
|
2003-03-26 20:00:16 +08:00
|
|
|
if ( device >= 0 )
|
|
|
|
{
|
2005-01-16 01:45:51 +08:00
|
|
|
snprintf( dbg_id_string, sizeof(dbg_id_string), "d%d", device );
|
2003-03-26 20:00:16 +08:00
|
|
|
}
|
2004-02-16 03:17:38 +08:00
|
|
|
else if ( host[0] )
|
2002-09-16 17:19:24 +08:00
|
|
|
{
|
2005-01-16 01:45:51 +08:00
|
|
|
snprintf( dbg_id_string, sizeof(dbg_id_string), "h%s", host );
|
2002-09-16 17:19:24 +08:00
|
|
|
}
|
2004-02-16 03:17:38 +08:00
|
|
|
else
|
|
|
|
{
|
2005-01-16 01:45:51 +08:00
|
|
|
snprintf( dbg_id_string, sizeof(dbg_id_string), "m%d", monitor_id );
|
2004-02-16 03:17:38 +08:00
|
|
|
}
|
2003-03-26 20:00:16 +08:00
|
|
|
|
2005-01-16 01:45:51 +08:00
|
|
|
zmDbgInit( "zmc", dbg_id_string, 0 );
|
2003-03-26 20:00:16 +08:00
|
|
|
|
2004-12-29 00:46:48 +08:00
|
|
|
zmLoadConfig();
|
|
|
|
|
2003-03-26 20:00:16 +08:00
|
|
|
Monitor **monitors = 0;
|
|
|
|
int n_monitors = 0;
|
|
|
|
if ( device >= 0 )
|
2002-09-16 17:19:24 +08:00
|
|
|
{
|
2003-06-12 22:29:54 +08:00
|
|
|
n_monitors = Monitor::Load( device, monitors, Monitor::CAPTURE );
|
2002-09-16 17:19:24 +08:00
|
|
|
}
|
2004-02-16 03:17:38 +08:00
|
|
|
else if ( host[0] )
|
2002-09-16 17:19:24 +08:00
|
|
|
{
|
2003-03-26 20:00:16 +08:00
|
|
|
if ( !port )
|
|
|
|
port = "80";
|
2003-06-12 22:29:54 +08:00
|
|
|
n_monitors = Monitor::Load( host, port, path, monitors, Monitor::CAPTURE );
|
2002-09-16 17:19:24 +08:00
|
|
|
}
|
2004-02-16 03:17:38 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Monitor *monitor = Monitor::Load( monitor_id, true, Monitor::CAPTURE );
|
|
|
|
if ( monitor )
|
|
|
|
{
|
|
|
|
monitors = new Monitor *[1];
|
|
|
|
monitors[0] = monitor;
|
|
|
|
n_monitors = 1;
|
|
|
|
}
|
|
|
|
}
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2003-03-26 20:00:16 +08:00
|
|
|
if ( !n_monitors )
|
|
|
|
{
|
|
|
|
Error(( "No monitors found" ));
|
|
|
|
exit ( -1 );
|
|
|
|
}
|
2002-09-16 17:19:24 +08:00
|
|
|
|
|
|
|
Info(( "Starting Capture" ));
|
|
|
|
sigset_t block_set;
|
|
|
|
sigemptyset( &block_set );
|
|
|
|
struct sigaction action, old_action;
|
2002-12-24 20:02:08 +08:00
|
|
|
|
2003-03-26 20:00:16 +08:00
|
|
|
action.sa_handler = zmc_term_handler;
|
2002-09-23 18:11:04 +08:00
|
|
|
action.sa_mask = block_set;
|
|
|
|
action.sa_flags = 0;
|
|
|
|
sigaction( SIGTERM, &action, &old_action );
|
2002-12-24 20:02:08 +08:00
|
|
|
|
2003-04-04 17:55:23 +08:00
|
|
|
sigaddset( &block_set, SIGUSR1 );
|
|
|
|
sigaddset( &block_set, SIGUSR2 );
|
2003-03-26 20:00:16 +08:00
|
|
|
if ( device >= 0 && n_monitors == 1 )
|
2002-09-16 17:19:24 +08:00
|
|
|
{
|
|
|
|
monitors[0]->PreCapture();
|
|
|
|
}
|
2003-03-27 06:12:25 +08:00
|
|
|
|
2004-01-15 05:26:47 +08:00
|
|
|
long *capture_delays = new long[n_monitors];
|
|
|
|
long *next_delays = new long[n_monitors];
|
|
|
|
struct timeval * last_capture_times = new struct timeval[n_monitors];
|
2003-03-27 06:12:25 +08:00
|
|
|
for ( int i = 0; i < n_monitors; i++ )
|
|
|
|
{
|
|
|
|
last_capture_times[i].tv_sec = last_capture_times[i].tv_usec = 0;
|
2003-05-16 18:17:05 +08:00
|
|
|
capture_delays[i] = monitors[i]->GetCaptureDelay();
|
2003-03-27 06:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval now;
|
2003-04-07 18:56:38 +08:00
|
|
|
struct DeltaTimeval delta_time;
|
2003-03-26 20:00:16 +08:00
|
|
|
while( !zmc_terminate )
|
2002-09-16 17:19:24 +08:00
|
|
|
{
|
|
|
|
/* grab a new one */
|
|
|
|
sigprocmask( SIG_BLOCK, &block_set, 0 );
|
|
|
|
for ( int i = 0; i < n_monitors; i++ )
|
|
|
|
{
|
2003-03-27 06:12:25 +08:00
|
|
|
long min_delay = MAXINT;
|
2003-07-04 20:31:36 +08:00
|
|
|
if ( (bool)config.Item( ZM_NO_MAX_FPS_ON_ALARM ) && monitors[i]->GetState() == Monitor::ALARM )
|
2003-03-27 06:12:25 +08:00
|
|
|
{
|
2003-03-31 02:44:15 +08:00
|
|
|
next_delays[i] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gettimeofday( &now, &dummy_tz );
|
|
|
|
for ( int j = 0; j < n_monitors; j++ )
|
2003-03-27 06:12:25 +08:00
|
|
|
{
|
2003-03-31 02:44:15 +08:00
|
|
|
if ( last_capture_times[j].tv_sec )
|
|
|
|
{
|
2003-05-16 18:17:05 +08:00
|
|
|
DELTA_TIMEVAL( delta_time, now, last_capture_times[j], DT_PREC_3 );
|
|
|
|
next_delays[j] = capture_delays[j]-delta_time.delta;
|
2003-03-31 02:44:15 +08:00
|
|
|
if ( next_delays[j] < 0 )
|
|
|
|
{
|
|
|
|
next_delays[j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2003-03-27 06:12:25 +08:00
|
|
|
{
|
|
|
|
next_delays[j] = 0;
|
|
|
|
}
|
2003-03-31 02:44:15 +08:00
|
|
|
if ( next_delays[j] <= min_delay )
|
|
|
|
{
|
|
|
|
min_delay = next_delays[j];
|
|
|
|
}
|
2003-03-27 06:12:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( next_delays[i] <= min_delay || next_delays[i] <= 0 )
|
|
|
|
{
|
|
|
|
monitors[i]->PreCapture();
|
|
|
|
monitors[i]->PostCapture();
|
2003-04-07 18:56:38 +08:00
|
|
|
|
|
|
|
if ( next_delays[i] > 0 )
|
|
|
|
{
|
|
|
|
gettimeofday( &now, &dummy_tz );
|
2003-05-16 18:17:05 +08:00
|
|
|
DELTA_TIMEVAL( delta_time, now, last_capture_times[i], DT_PREC_3 );
|
|
|
|
long sleep_time = next_delays[i]-delta_time.delta;
|
2003-04-07 18:56:38 +08:00
|
|
|
if ( sleep_time > 0 )
|
|
|
|
{
|
2003-05-16 18:17:05 +08:00
|
|
|
usleep( sleep_time*(DT_MAXGRAN/DT_PREC_3) );
|
2003-04-07 18:56:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
gettimeofday( &(last_capture_times[i]), &dummy_tz );
|
2003-03-27 06:12:25 +08:00
|
|
|
}
|
2002-09-16 17:19:24 +08:00
|
|
|
}
|
|
|
|
sigprocmask( SIG_UNBLOCK, &block_set, 0 );
|
2003-03-26 20:00:16 +08:00
|
|
|
}
|
|
|
|
for ( int i = 0; i < n_monitors; i++ )
|
|
|
|
{
|
|
|
|
delete monitors[i];
|
2002-09-16 17:19:24 +08:00
|
|
|
}
|
2004-01-15 05:26:47 +08:00
|
|
|
delete [] capture_delays;
|
|
|
|
delete [] next_delays;
|
|
|
|
delete [] last_capture_times;
|
|
|
|
|
2002-10-28 22:33:57 +08:00
|
|
|
return( 0 );
|
2002-09-16 17:19:24 +08:00
|
|
|
}
|