int main( int argc, char *argv[] ) { srand( getpid() * time( 0 ) ); int id = -1; static struct option long_options[] = { {"monitor", 1, 0, 'm'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; while (1) { int option_index = 0; int c = getopt_long (argc, argv, "m:h", long_options, &option_index); if (c == -1) { break; } switch (c) { case 'm': id = atoi(optarg); break; 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(); } if ( id < 0 ) { fprintf( stderr, "Bogus monitor %d\n", id ); Usage(); exit( 0 ); } char dbg_id_string[16]; snprintf( dbg_id_string, sizeof(dbg_id_string), "m%d", id ); zmDbgInit( "zmf", dbg_id_string, 0 ); zmLoadConfig(); Monitor *monitor = Monitor::Load( id, false, Monitor::QUERY ); if ( !monitor ) { fprintf( stderr, "Can't find monitor with id of %d\n", id ); exit( -1 ); } char capt_path[PATH_MAX]; char anal_path[PATH_MAX]; snprintf( capt_path, sizeof(capt_path), "%s/%d/%%s/%%0%dd-capture.jpg", config.dir_events, monitor->Id(), config.event_image_digits ); snprintf( anal_path, sizeof(anal_path), "%s/%d/%%s/%%0%dd-analyse.jpg", config.dir_events, monitor->Id(), config.event_image_digits ); zmSetDefaultTermHandler(); zmSetDefaultDieHandler(); sigset_t block_set; sigemptyset( &block_set ); int sd = OpenSocket( monitor->Id() ); FrameHeader frame_header = { 0, 0, false, 0 }; //unsigned char *image_data = 0; fd_set rfds; struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 0; while( 1 ) { struct timeval temp_timeout = timeout; FD_ZERO(&rfds); FD_SET(sd, &rfds); int n_found = select( sd+1, &rfds, NULL, NULL, &temp_timeout ); if( n_found == 0 ) { Debug( 1, "Select timed out" ); continue; } else if ( n_found < 0) { Error( "Select error: %s", strerror(errno) ); ReopenSocket( sd, monitor->Id() ); continue; } sigprocmask( SIG_BLOCK, &block_set, 0 ); int n_bytes = read( sd, &frame_header, sizeof(frame_header) ); if ( n_bytes != sizeof(frame_header) ) { if ( n_bytes < 0 ) { Error( "Can't read frame header: %s", strerror(errno) ); } else if ( n_bytes > 0 ) { Error( "Incomplete read of frame header, %d bytes only", n_bytes ); } else { Warning( "Socket closed at remote end" ); } ReopenSocket( sd, monitor->Id() ); continue; } Debug( 1, "Read frame header, expecting %ld bytes of image", frame_header.image_length ); static unsigned char image_data[ZM_MAX_IMAGE_SIZE]; n_bytes = read( sd, image_data, frame_header.image_length ); if ( n_bytes != (ssize_t)frame_header.image_length ) { if ( n_bytes < 0 ) { Error( "Can't read frame image data: %s", strerror(errno) ); } else if ( n_bytes > 0 ) { Error( "Incomplete read of frame image data, %d bytes only", n_bytes ); } else { Warning( "Socket closed at remote end" ); } ReopenSocket( sd, monitor->Id() ); continue; } static char subpath[PATH_MAX] = ""; if ( config.use_deep_storage ) { struct tm *time = localtime( &frame_header.event_time ); snprintf( subpath, sizeof(subpath), "%02d/%02d/%02d/%02d/%02d/%02d", time->tm_year-100, time->tm_mon+1, time->tm_mday, time->tm_hour, time->tm_min, time->tm_sec ); } else { snprintf( subpath, sizeof(subpath), "%ld", frame_header.event_id ); } static char path[PATH_MAX] = ""; snprintf( path, sizeof(path), frame_header.alarm_frame?anal_path:capt_path, subpath, frame_header.frame_id ); Debug( 1, "Got image, writing to %s", path ); FILE *fd = 0; if ( (fd = fopen( path, "w" )) < 0 ) { Error( "Can't fopen '%s': %s", path, strerror(errno) ); exit( -1 ); } if ( 0 == fwrite( image_data, frame_header.image_length, 1, fd ) ) { Error( "Can't fwrite image data: %s", strerror(errno) ); exit( -1 ); } fclose( fd ); sigprocmask( SIG_UNBLOCK, &block_set, 0 ); } }
int main( int argc, char *argv[] ) { srand( getpid() * time( 0 ) ); int id = -1; static struct option long_options[] = { {"monitor", 1, 0, 'm'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; while (1) { int option_index = 0; int c = getopt_long (argc, argv, "m:h", long_options, &option_index); if (c == -1) { break; } switch (c) { case 'm': id = atoi(optarg); break; 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(); } if ( id < 0 ) { fprintf( stderr, "Bogus monitor %d\n", id ); Usage(); exit( 0 ); } char dbg_id_string[16]; snprintf( dbg_id_string, sizeof(dbg_id_string), "m%d", id ); zmDbgInit( "zma", dbg_id_string, 0 ); zmLoadConfig(); Monitor *monitor = Monitor::Load( id, true, Monitor::ANALYSIS ); if ( monitor ) { Info( "In mode %d/%d, warming up", monitor->GetFunction(), monitor->Enabled() ); if ( config.opt_frame_server ) { Event::OpenFrameSocket( monitor->Id() ); } zmSetDefaultHupHandler(); zmSetDefaultTermHandler(); zmSetDefaultDieHandler(); sigset_t block_set; sigemptyset( &block_set ); while( !zm_terminate ) { // Process the next image sigprocmask( SIG_BLOCK, &block_set, 0 ); if ( !monitor->Analyse() ) { usleep( monitor->Active()?ZM_SAMPLE_RATE:ZM_SUSPENDED_RATE ); } if ( zm_reload ) { monitor->Reload(); zm_reload = false; } sigprocmask( SIG_UNBLOCK, &block_set, 0 ); } delete monitor; } else { fprintf( stderr, "Can't find monitor with id of %d\n", id ); } return( 0 ); }
int main( int argc, char *argv[] ) { zmDbgInit( "zmfix", "", -1 ); zmLoadConfig(); // Only do registered devices static char sql[BUFSIZ]; snprintf( sql, sizeof(sql), "select distinct Device from Monitors where not isnull(Device) and Type = 'Local'" ); 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 ) ); } for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ ) { fixDevice( dbrow[0] ); } if ( mysql_errno( &dbconn ) ) { Error( "Can't fetch row: %s", mysql_error( &dbconn ) ); exit( mysql_errno( &dbconn ) ); } // Yadda yadda mysql_free_result( result ); snprintf( sql, sizeof(sql), "select distinct ControlDevice from Monitors where not isnull(ControlDevice)" ); if ( mysql_query( &dbconn, sql ) ) { Error( "Can't run query: %s", mysql_error( &dbconn ) ); exit( mysql_errno( &dbconn ) ); } result = mysql_store_result( &dbconn ); if ( !result ) { Error( "Can't use query result: %s", mysql_error( &dbconn ) ); exit( mysql_errno( &dbconn ) ); } for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ ) { fixDevice( dbrow[0] ); } if ( mysql_errno( &dbconn ) ) { Error( "Can't fetch row: %s", mysql_error( &dbconn ) ); exit( mysql_errno( &dbconn ) ); } // Yadda yadda mysql_free_result( result ); if ( config.opt_x10 ) { if ( config.x10_device ) { fixDevice( config.x10_device ); } } return( 0 ); }
int main(int argc, char** argv) { // Set initial values to the default values int debug = ZMS_DEFAULT_DEBUG; int id = ZMS_DEFAULT_ID; int bitrate = ZMS_DEFAULT_BITRATE; int scale = ZMS_DEFAULT_SCALE; char mode[32]; sprintf(mode, "%s", ZMS_DEFAULT_MODE); char format[32]; sprintf(format, "%s", ZMS_DEFAULT_FORMAT); double maxfps = ZMS_DEFAULT_FPS; int buffer = ZMS_DEFAULT_BUFFER; // Parse command-line options int arg; while ((arg = getopt(argc, argv, OPTIONS)) != -1) { switch (arg) { case 'e': sprintf(mode, "%s", optarg); break; case 'o': sprintf(format, "%s", optarg); break; case 'u': buffer = atoi(optarg); break; case 'f': maxfps = atof(optarg); break; case 's': scale = atoi(optarg); break; case 'b': bitrate = atoi(optarg); break; case 'm': id = atoi(optarg); break; case 'd': debug = atoi(optarg); break; case 'i': case '?': printf("-e <mode> : Specify output mode: mpeg/jpg/zip/single/raw. Default = %s\n", ZMS_DEFAULT_MODE); printf("-o <format> : Specify output format. Default = %s\n", ZMS_DEFAULT_FORMAT); printf("-u <buffer size> : Specify buffer size in ms. Default = %d\n", ZMS_DEFAULT_BUFFER); printf("-f <maximum fps> : Specify maximum framerate. Default = %lf\n", ZMS_DEFAULT_FPS); printf("-s <scale> : Specify scale. Default = %d\n", ZMS_DEFAULT_SCALE); printf("-b <bitrate in bps> : Specify bitrate. Default = %d\n", ZMS_DEFAULT_BITRATE); printf("-m <monitor id> : Specify monitor id. Default = %d\n", ZMS_DEFAULT_ID); printf("-d <debug mode> : 0 = off, 1 = no streaming, 2 = with streaming. Default = 0\n"); printf("-i or -? : This information\n"); return EXIT_SUCCESS; } } // Set stream type StreamBase::StreamType streamtype; if (!strcasecmp("raw", mode)) streamtype = MonitorStream::STREAM_RAW; else if (!strcasecmp("mpeg", mode)) streamtype = MonitorStream::STREAM_MPEG; else if (!strcasecmp("jpg", mode)) streamtype = MonitorStream::STREAM_JPEG; else if (!strcasecmp("single", mode)) streamtype = MonitorStream::STREAM_SINGLE; else if (!strcasecmp("zip", mode)) streamtype = MonitorStream::STREAM_ZIP; else streamtype = MonitorStream::STREAM_MPEG; if (debug) { // Show stream parameters printf("Stream parameters:\n"); switch (streamtype) { case MonitorStream::STREAM_MPEG: printf("Output mode (-e) = %s\n", "mpeg"); printf("Output format (-o) = %s\n", format); break; default: printf("Output mode (-e) = %s\n", mode); } printf("Buffer size (-u) = %d ms\n", buffer); printf("Maximum FPS (-f) = %lf FPS\n", maxfps); printf("Scale (-s) = %d%%\n", scale); printf("Bitrate (-b) = %d bps\n", bitrate); printf("Monitor Id (-m) = %d\n", id); } if (debug) { // Set ZM debugger to print to stdout printf("Setting up ZoneMinder debugger to print to stdout..."); setenv("ZM_DBG_PRINT", "1", 1); printf("Done.\n"); } zmDbgInit("zmstreamer", "", 0); // Loading ZM configurations printf("Loading ZoneMinder configurations..."); zmLoadConfig(); printf("Done.\n"); // Setting stream parameters MonitorStream stream; stream.setStreamScale(scale); // default = 100 (scale) stream.setStreamReplayRate(100); // default = 100 (rate) stream.setStreamMaxFPS(maxfps); // default = 10 (maxfps) if (debug) stream.setStreamTTL(1); else stream.setStreamTTL(0); // default = 0 (ttl) stream.setStreamQueue(0); // default = 0 (connkey) stream.setStreamBuffer(buffer); // default = 0 (buffer) stream.setStreamStart(id); // default = 0 (monitor_id) stream.setStreamType(streamtype); if (streamtype == MonitorStream::STREAM_MPEG) { #if HAVE_LIBAVCODEC if (debug) printf("HAVE_LIBAVCODEC is set\n"); stream.setStreamFormat(format); // default = "" (format) stream.setStreamBitrate(bitrate); // default = 100000 (bitrate) #else fprintf(stderr, "MPEG streaming is disabled.\nYou should configure with the --with-ffmpeg option and rebuild to use this functionality.\n"); return EXIT_FAILURE; #endif } if (debug != 1) { if (debug) printf("Running stream..."); // Output headers fprintf(stdout, "Server: ZoneMinder Video Server/%s\r\n", ZM_VERSION); time_t now = time(0); char date_string[64]; strftime(date_string, sizeof (date_string) - 1, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&now)); fprintf(stdout, "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"); fprintf(stdout, "Last-Modified: %s\r\n", date_string); fprintf(stdout, "Cache-Control: no-store, no-cache, must-revalidate\r\n"); fprintf(stdout, "Cache-Control: post-check=0, pre-check=0\r\n"); fprintf(stdout, "Pragma: no-cache\r\n"); // Run stream stream.runStream(); } if (debug) printf("Done.\n"); return (EXIT_SUCCESS); }
int main( int argc, char *argv[] ) { srand( getpid() * time( 0 ) ); const char *device = ""; const char *protocol = ""; const char *host = ""; const char *port = ""; const char *path = ""; const char *file = ""; int monitor_id = -1; static struct option long_options[] = { {"device", 1, 0, 'd'}, {"protocol", 1, 0, 'r'}, /** 驱动协议 */ {"host", 1, 0, 'H'}, {"port", 1, 0, 'P'}, {"path", 1, 0, 'p'}, {"file", 1, 0, 'f'}, {"monitor", 1, 0, 'm'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; /** 进入死循环 */ while (1) { int option_index = 0; int c = getopt_long (argc, argv, "d:H:P:p:f:m:h", long_options, &option_index); if (c == -1) { break; } switch (c) { case 'd': device = optarg; break; case 'H': host = optarg; break; case 'P': port = optarg; break; case 'p': path = optarg; break; case 'f': file = optarg; break; case 'm': monitor_id = atoi(optarg); break; 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(); } int modes = ( device[0]?1:0 + host[0]?1:0 + file[0]?1:0 + (monitor_id>0?1:0) ); if ( modes > 1 ) { fprintf( stderr, "Only one of device, host/port/path, file or monitor id allowed\n" ); Usage(); exit( 0 ); } if ( modes < 1 ) { fprintf( stderr, "One of device, host/port/path, file or monitor id must be specified\n" ); Usage(); exit( 0 ); } char dbg_id_string[16]; if ( device[0] ) { const char *slash_ptr = strrchr( device, '/' ); snprintf( dbg_id_string, sizeof(dbg_id_string), "d%s", slash_ptr?slash_ptr+1:device ); } else if ( host[0] ) { snprintf( dbg_id_string, sizeof(dbg_id_string), "h%s", host ); } else if ( file[0] ) { const char *slash_ptr = strrchr( file, '/' ); snprintf( dbg_id_string, sizeof(dbg_id_string), "f%s", slash_ptr?slash_ptr+1:file ); } else { snprintf( dbg_id_string, sizeof(dbg_id_string), "m%d", monitor_id ); } zmDbgInit( "zmc", dbg_id_string, 0 ); /** 载入配置信息 */ zmLoadConfig(); /** Monitor 实例化类 */ Monitor **monitors = 0; int n_monitors = 0; if ( device[0] ) { /** 本机的摄像设备 */ n_monitors = Monitor::LoadLocalMonitors( device, monitors, Monitor::CAPTURE ); } else if ( host[0] ) { if ( !port ) port = "80"; /** 远程摄像设备,根据 protocol 选择 HTTP、RTSP */ n_monitors = Monitor::LoadRemoteMonitors( protocol, host, port, path, monitors, Monitor::CAPTURE ); } else if ( file[0] ) { /** 文件摄像 */ n_monitors = Monitor::LoadFileMonitors( file, monitors, Monitor::CAPTURE ); } else { Monitor *monitor = Monitor::Load( monitor_id, true, Monitor::CAPTURE ); if ( monitor ) { monitors = new Monitor *[1]; monitors[0] = monitor; n_monitors = 1; } } if ( !n_monitors ) { Error( "No monitors found" ); exit ( -1 ); } Info( "Starting Capture" ); zmSetDefaultTermHandler(); zmSetDefaultDieHandler(); sigset_t block_set; sigemptyset( &block_set ); sigaddset( &block_set, SIGUSR1 ); sigaddset( &block_set, SIGUSR2 ); if ( monitors[0]->PrimeCapture() < 0 ) { Error( "Failed to prime capture of initial monitor" ); exit( -1 ); } long *capture_delays = new long[n_monitors]; long *alarm_capture_delays = new long[n_monitors]; long *next_delays = new long[n_monitors]; struct timeval * last_capture_times = new struct timeval[n_monitors]; for ( int i = 0; i < n_monitors; i++ ) { last_capture_times[i].tv_sec = last_capture_times[i].tv_usec = 0; capture_delays[i] = monitors[i]->GetCaptureDelay(); alarm_capture_delays[i] = monitors[i]->GetAlarmCaptureDelay(); } int result = 0; struct timeval now; struct DeltaTimeval delta_time; while( !zm_terminate ) { sigprocmask( SIG_BLOCK, &block_set, 0 ); for ( int i = 0; i < n_monitors; i++ ) { long min_delay = MAXINT; gettimeofday( &now, NULL ); for ( int j = 0; j < n_monitors; j++ ) { if ( last_capture_times[j].tv_sec ) { DELTA_TIMEVAL( delta_time, now, last_capture_times[j], DT_PREC_3 ); if ( monitors[i]->GetState() == Monitor::ALARM ) next_delays[j] = alarm_capture_delays[j]-delta_time.delta; else next_delays[j] = capture_delays[j]-delta_time.delta; if ( next_delays[j] < 0 ) next_delays[j] = 0; } else { next_delays[j] = 0; } if ( next_delays[j] <= min_delay ) { min_delay = next_delays[j]; } } if ( next_delays[i] <= min_delay || next_delays[i] <= 0 ) { if ( monitors[i]->PreCapture() < 0 ) { Error( "Failed to pre-capture monitor %d (%d/%d)", monitors[i]->Id(), i, n_monitors ); zm_terminate = true; result = -1; break; } if ( monitors[i]->Capture() < 0 ) { Error( "Failed to capture image from monitor %d (%d/%d)", monitors[i]->Id(), i, n_monitors ); zm_terminate = true; result = -1; break; } if ( monitors[i]->PostCapture() < 0 ) { Error( "Failed to post-capture monitor %d (%d/%d)", monitors[i]->Id(), i, n_monitors ); zm_terminate = true; result = -1; break; } if ( next_delays[i] > 0 ) { gettimeofday( &now, NULL ); DELTA_TIMEVAL( delta_time, now, last_capture_times[i], DT_PREC_3 ); long sleep_time = next_delays[i]-delta_time.delta; if ( sleep_time > 0 ) { usleep( sleep_time*(DT_MAXGRAN/DT_PREC_3) ); } } gettimeofday( &(last_capture_times[i]), NULL ); } } sigprocmask( SIG_UNBLOCK, &block_set, 0 ); } for ( int i = 0; i < n_monitors; i++ ) { delete monitors[i]; } delete [] capture_delays; delete [] next_delays; delete [] last_capture_times; return( result ); }