2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Core Interfaces, $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
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ZM_EVENT_H
|
|
|
|
#define ZM_EVENT_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <time.h>
|
2004-03-03 00:32:49 +08:00
|
|
|
#include <sys/time.h>
|
2003-03-26 19:57:29 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <mysql/mysql.h>
|
|
|
|
|
2008-10-06 03:07:43 +08:00
|
|
|
#include <set>
|
|
|
|
#include <map>
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
#include "zm.h"
|
|
|
|
#include "zm_image.h"
|
2007-08-30 02:11:09 +08:00
|
|
|
#include "zm_stream.h"
|
2013-12-20 00:38:07 +08:00
|
|
|
#include "zm_video.h"
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2008-10-06 03:07:43 +08:00
|
|
|
class Zone;
|
2003-03-26 19:57:29 +08:00
|
|
|
class Monitor;
|
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
#define MAX_PRE_ALARM_FRAMES 16 // Maximum number of prealarm frames that can be stored
|
2004-09-25 20:37:45 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// Class describing events, i.e. captured periods of activity.
|
|
|
|
//
|
2016-06-22 01:48:32 +08:00
|
|
|
class Event {
|
2016-04-29 21:11:14 +08:00
|
|
|
friend class EventStream;
|
|
|
|
|
|
|
|
protected:
|
2016-06-22 01:48:32 +08:00
|
|
|
static bool initialised;
|
|
|
|
static char capture_file_format[PATH_MAX];
|
|
|
|
static char analyse_file_format[PATH_MAX];
|
|
|
|
static char general_file_format[PATH_MAX];
|
|
|
|
static char video_file_format[PATH_MAX];
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
protected:
|
2016-06-22 01:48:32 +08:00
|
|
|
static int sd;
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
public:
|
2016-06-22 01:48:32 +08:00
|
|
|
typedef std::set<std::string> StringSet;
|
|
|
|
typedef std::map<std::string,StringSet> StringSetMap;
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
protected:
|
2017-04-10 05:36:24 +08:00
|
|
|
typedef enum { NORMAL=0, BULK, ALARM } FrameType;
|
2017-04-13 21:47:19 +08:00
|
|
|
static const char * frame_type_names[3];
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
struct PreAlarmData {
|
|
|
|
Image *image;
|
|
|
|
struct timeval timestamp;
|
|
|
|
unsigned int score;
|
|
|
|
Image *alarm_frame;
|
|
|
|
};
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
static int pre_alarm_count;
|
|
|
|
static PreAlarmData pre_alarm_data[MAX_PRE_ALARM_FRAMES];
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
protected:
|
2016-06-22 01:48:32 +08:00
|
|
|
unsigned int id;
|
|
|
|
Monitor *monitor;
|
|
|
|
struct timeval start_time;
|
|
|
|
struct timeval end_time;
|
|
|
|
std::string cause;
|
|
|
|
StringSetMap noteSetMap;
|
|
|
|
bool videoEvent;
|
|
|
|
int frames;
|
|
|
|
int alarm_frames;
|
|
|
|
unsigned int tot_score;
|
|
|
|
unsigned int max_score;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
VideoWriter* videowriter;
|
|
|
|
FILE* timecodes_fd;
|
|
|
|
char video_name[PATH_MAX];
|
|
|
|
char video_file[PATH_MAX];
|
|
|
|
char timecodes_name[PATH_MAX];
|
|
|
|
char timecodes_file[PATH_MAX];
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
protected:
|
2016-06-22 01:48:32 +08:00
|
|
|
int last_db_frame;
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
protected:
|
2016-06-22 01:48:32 +08:00
|
|
|
static void Initialise() {
|
|
|
|
if ( initialised )
|
|
|
|
return;
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
snprintf( capture_file_format, sizeof(capture_file_format), "%%s/%%0%dd-capture.jpg", config.event_image_digits );
|
|
|
|
snprintf( analyse_file_format, sizeof(analyse_file_format), "%%s/%%0%dd-analyse.jpg", config.event_image_digits );
|
|
|
|
snprintf( general_file_format, sizeof(general_file_format), "%%s/%%0%dd-%%s", config.event_image_digits );
|
|
|
|
snprintf( video_file_format, sizeof(video_file_format), "%%s/%%s");
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
initialised = true;
|
|
|
|
}
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
void createNotes( std::string ¬es );
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
public:
|
2016-06-22 01:48:32 +08:00
|
|
|
static bool OpenFrameSocket( int );
|
|
|
|
static bool ValidateFrameSocket( int );
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
public:
|
2016-06-22 01:48:32 +08:00
|
|
|
Event( Monitor *p_monitor, struct timeval p_start_time, const std::string &p_cause, const StringSetMap &p_noteSetMap, bool p_videoEvent=false );
|
|
|
|
~Event();
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
int Id() const { return( id ); }
|
|
|
|
const std::string &Cause() { return( cause ); }
|
|
|
|
int Frames() const { return( frames ); }
|
|
|
|
int AlarmFrames() const { return( alarm_frames ); }
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
const struct timeval &StartTime() const { return( start_time ); }
|
|
|
|
const struct timeval &EndTime() const { return( end_time ); }
|
2017-04-13 01:36:49 +08:00
|
|
|
struct timeval &StartTime() { return( start_time ); }
|
2016-06-22 01:48:32 +08:00
|
|
|
struct timeval &EndTime() { return( end_time ); }
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
bool SendFrameImage( const Image *image, bool alarm_frame=false );
|
|
|
|
bool WriteFrameImage( Image *image, struct timeval timestamp, const char *event_file, bool alarm_frame=false );
|
|
|
|
bool WriteFrameVideo( const Image *image, const struct timeval timestamp, VideoWriter* videow );
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
void updateNotes( const StringSetMap &stringSetMap );
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
void AddFrames( int n_frames, Image **images, struct timeval **timestamps );
|
|
|
|
void AddFrame( Image *image, struct timeval timestamp, int score=0, Image *alarm_frame=NULL );
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
private:
|
2016-06-22 01:48:32 +08:00
|
|
|
void AddFramesInternal( int n_frames, int start_frame, Image **images, struct timeval **timestamps );
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
public:
|
2016-06-22 01:48:32 +08:00
|
|
|
static const char *getSubPath( struct tm *time ) {
|
|
|
|
static char subpath[PATH_MAX] = "";
|
|
|
|
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 );
|
|
|
|
return( subpath );
|
|
|
|
}
|
|
|
|
static const char *getSubPath( time_t *time ) {
|
|
|
|
return( Event::getSubPath( localtime( time ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
char* getEventFile(void) {
|
|
|
|
return video_file;
|
|
|
|
}
|
2016-04-29 21:11:14 +08:00
|
|
|
|
|
|
|
public:
|
2016-06-22 01:48:32 +08:00
|
|
|
static int PreAlarmCount() {
|
|
|
|
return( pre_alarm_count );
|
|
|
|
}
|
|
|
|
static void EmptyPreAlarmFrames() {
|
|
|
|
if ( pre_alarm_count > 0 ) {
|
|
|
|
for ( int i = 0; i < MAX_PRE_ALARM_FRAMES; i++ ) {
|
|
|
|
delete pre_alarm_data[i].image;
|
|
|
|
delete pre_alarm_data[i].alarm_frame;
|
|
|
|
}
|
|
|
|
memset( pre_alarm_data, 0, sizeof(pre_alarm_data) );
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2016-06-22 01:48:32 +08:00
|
|
|
pre_alarm_count = 0;
|
2007-09-05 00:08:55 +08:00
|
|
|
}
|
2016-06-22 01:48:32 +08:00
|
|
|
static void AddPreAlarmFrame( Image *image, struct timeval timestamp, int score=0, Image *alarm_frame=NULL ) {
|
|
|
|
pre_alarm_data[pre_alarm_count].image = new Image( *image );
|
|
|
|
pre_alarm_data[pre_alarm_count].timestamp = timestamp;
|
|
|
|
pre_alarm_data[pre_alarm_count].score = score;
|
|
|
|
if ( alarm_frame ) {
|
|
|
|
pre_alarm_data[pre_alarm_count].alarm_frame = new Image( *alarm_frame );
|
|
|
|
}
|
|
|
|
pre_alarm_count++;
|
2007-09-05 00:08:55 +08:00
|
|
|
}
|
2016-06-22 01:48:32 +08:00
|
|
|
void SavePreAlarmFrames() {
|
|
|
|
for ( int i = 0; i < pre_alarm_count; i++ ) {
|
|
|
|
AddFrame( pre_alarm_data[i].image, pre_alarm_data[i].timestamp, pre_alarm_data[i].score, pre_alarm_data[i].alarm_frame );
|
|
|
|
}
|
|
|
|
EmptyPreAlarmFrames();
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2007-08-30 02:11:09 +08:00
|
|
|
};
|
2004-09-25 20:37:45 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
class EventStream : public StreamBase {
|
2016-04-29 21:11:14 +08:00
|
|
|
public:
|
2007-08-30 02:11:09 +08:00
|
|
|
typedef enum { MODE_SINGLE, MODE_ALL, MODE_ALL_GAPLESS } StreamMode;
|
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
protected:
|
2008-07-24 23:38:21 +08:00
|
|
|
struct FrameData {
|
2016-04-29 21:11:14 +08:00
|
|
|
//unsigned long id;
|
|
|
|
time_t timestamp;
|
|
|
|
time_t offset;
|
|
|
|
double delta;
|
|
|
|
bool in_db;
|
2007-08-30 02:11:09 +08:00
|
|
|
};
|
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
struct EventData {
|
2016-04-29 21:11:14 +08:00
|
|
|
unsigned long event_id;
|
|
|
|
unsigned long monitor_id;
|
|
|
|
unsigned long storage_id;
|
|
|
|
unsigned long frame_count;
|
|
|
|
time_t start_time;
|
|
|
|
double duration;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
int n_frames;
|
|
|
|
FrameData *frames;
|
|
|
|
char video_file[PATH_MAX];
|
2007-08-30 02:11:09 +08:00
|
|
|
};
|
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
protected:
|
2007-08-30 02:11:09 +08:00
|
|
|
static const int STREAM_PAUSE_WAIT = 250000; // Microseconds
|
|
|
|
|
|
|
|
static const StreamMode DEFAULT_MODE = MODE_SINGLE;
|
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
protected:
|
2007-08-30 02:11:09 +08:00
|
|
|
StreamMode mode;
|
2008-04-21 21:52:05 +08:00
|
|
|
bool forceEventChange;
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
protected:
|
2007-08-30 02:11:09 +08:00
|
|
|
int curr_frame_id;
|
|
|
|
double curr_stream_time;
|
|
|
|
|
|
|
|
EventData *event_data;
|
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
protected:
|
2007-08-30 02:11:09 +08:00
|
|
|
bool loadEventData( int event_id );
|
2016-04-27 02:53:50 +08:00
|
|
|
bool loadInitialEventData( int init_event_id, unsigned int init_frame_id );
|
2007-08-30 02:11:09 +08:00
|
|
|
bool loadInitialEventData( int monitor_id, time_t event_time );
|
|
|
|
|
|
|
|
void checkEventLoaded();
|
|
|
|
void processCommand( const CmdMsg *msg );
|
2009-01-22 01:37:39 +08:00
|
|
|
bool sendFrame( int delta_us );
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
public:
|
2016-06-22 01:48:32 +08:00
|
|
|
EventStream() {
|
2016-04-29 21:11:14 +08:00
|
|
|
mode = DEFAULT_MODE;
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
forceEventChange = false;
|
2008-04-21 21:52:05 +08:00
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
curr_frame_id = 0;
|
|
|
|
curr_stream_time = 0.0;
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
event_data = 0;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
2016-06-22 01:48:32 +08:00
|
|
|
void setStreamStart( int init_event_id, unsigned int init_frame_id=0 ) {
|
2016-04-29 21:11:14 +08:00
|
|
|
loadInitialEventData( init_event_id, init_frame_id );
|
|
|
|
loadMonitor( event_data->monitor_id );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
2016-06-22 01:48:32 +08:00
|
|
|
void setStreamStart( int monitor_id, time_t event_time ) {
|
2016-04-29 21:11:14 +08:00
|
|
|
loadInitialEventData( monitor_id, event_time );
|
|
|
|
loadMonitor( monitor_id );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
2016-06-22 01:48:32 +08:00
|
|
|
void setStreamMode( StreamMode p_mode ) {
|
2016-04-29 21:11:14 +08:00
|
|
|
mode = p_mode;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
void runStream();
|
2016-04-29 21:11:14 +08:00
|
|
|
Image *getImage();
|
2003-03-26 19:57:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ZM_EVENT_H
|