Merge pull request #1427 from connortechnology/storageareas

Storageareas
This commit is contained in:
Isaac Connor 2016-04-20 11:58:55 -04:00
commit 0ae2b0a012
13 changed files with 255 additions and 91 deletions

View File

@ -155,10 +155,12 @@ MAIN: while( $loop ) {
Fatal("ZM_AUDIT_MIN_AGE is not set in config.");
}
my %Monitors;
my $db_monitors;
my $monitorSelectSql = "select Id from Monitors order by Id";
my $monitorSelectSql = "select * from Monitors order by Id";
my $monitorSelectSth = $dbh->prepare_cached( $monitorSelectSql )
or Fatal( "Can't prepare '$monitorSelectSql': ".$dbh->errstr() );
my $eventSelectSql = "SELECT Id, (unix_timestamp() - unix_timestamp(StartTime)) as Age
FROM Events WHERE MonitorId = ? ORDER BY Id";
my $eventSelectSth = $dbh->prepare_cached( $eventSelectSql )
@ -169,6 +171,8 @@ MAIN: while( $loop ) {
or Fatal( "Can't execute: ".$monitorSelectSth->errstr() );
while( my $monitor = $monitorSelectSth->fetchrow_hashref() )
{
$Monitors{$$monitor{Id}} = $monitor;
Debug( "Found database monitor '$monitor->{Id}'" );
my $db_events = $db_monitors->{$monitor->{Id}} = {};
my $res = $eventSelectSth->execute( $monitor->{Id} )
@ -467,20 +471,30 @@ MAIN: while( $loop ) {
# New audit to close any events that were left open for longer than MIN_AGE seconds
my $selectUnclosedEventsSql =
"SELECT E.Id,
max(F.TimeStamp) as EndTime,
unix_timestamp(max(F.TimeStamp)) - unix_timestamp(E.StartTime) as Length,
max(F.FrameId) as Frames,
#"SELECT E.Id, ANY_VALUE(E.MonitorId),
#
#max(F.TimeStamp) as EndTime,
#unix_timestamp(max(F.TimeStamp)) - unix_timestamp(E.StartTime) as Length,
#max(F.FrameId) as Frames,
#count(if(F.Score>0,1,NULL)) as AlarmFrames,
#sum(F.Score) as TotScore,
#max(F.Score) as MaxScore
#FROM Events as E
#INNER JOIN Frames as F on E.Id = F.EventId
#WHERE isnull(E.Frames) or isnull(E.EndTime)
#GROUP BY E.Id HAVING EndTime < (now() - interval ".$Config{ZM_AUDIT_MIN_AGE}." second)"
#;
"SELECT *, unix_timestamp(StartTime) AS TimeStamp FROM Events WHERE EndTime IS NULL AND StartTime < (now() - interval ".$Config{ZM_AUDIT_MIN_AGE}." second)";
my $selectFrameDataSql = "SELECT max(TimeStamp) as EndTime, unix_timestamp(max(TimeStamp)) AS EndTimeStamp, max(FrameId) as Frames,
count(if(F.Score>0,1,NULL)) as AlarmFrames,
sum(F.Score) as TotScore,
max(F.Score) as MaxScore,
M.EventPrefix as Prefix
FROM Events as E
LEFT JOIN Monitors as M on E.MonitorId = M.Id
INNER JOIN Frames as F on E.Id = F.EventId
WHERE isnull(E.Frames) or isnull(E.EndTime)
GROUP BY E.Id HAVING EndTime < (now() - interval ".$Config{ZM_AUDIT_MIN_AGE}." second)"
;
max(F.Score) as MaxScore
FROM Frames WHERE EventId=?";
my $selectFrameDataSth = $dbh->prepare_cached($selectFrameDataSql)
or Fatal( "Can't prepare '$selectFrameDataSql': ".$dbh->errstr() );
my $selectUnclosedEventsSth = $dbh->prepare_cached( $selectUnclosedEventsSql )
or Fatal( "Can't prepare '$selectUnclosedEventsSql': ".$dbh->errstr() );
my $updateUnclosedEventsSql =
@ -505,26 +519,32 @@ MAIN: while( $loop ) {
aud_print( "Found open event '$event->{Id}'" );
if ( confirm( 'close', 'closing' ) )
{
$res = $selectFrameDataSth->execute( $event->{Id} );
my $frame = $selectFrameDataSth->fetchrow_hashref();
if ( $frame ) {
$res = $updateUnclosedEventsSth->execute
(
sprintf("%s%d%s",
$event->{Prefix},
$Monitors{$event->{MonitorId}}->{EventPrefix},
$event->{Id},
RECOVER_TAG
),
$event->{EndTime},
$event->{Length},
$event->{Frames},
$event->{AlarmFrames},
$event->{TotScore},
$event->{AlarmFrames}
? int($event->{TotScore} / $event->{AlarmFrames})
$frame->{EndTime},
$frame->{EndTimeStamp} - $event->{TimeStamp},
$frame->{Frames},
$frame->{AlarmFrames},
$frame->{TotScore},
$frame->{AlarmFrames}
? int($frame->{TotScore} / $frame->{AlarmFrames})
: 0
,
$event->{MaxScore},
$frame->{MaxScore},
RECOVER_TEXT,
$event->{Id}
) or Fatal( "Can't execute: ".$updateUnclosedEventsSth->errstr() );
} else {
Error("SHOULD DELETE");
} # end if has frame data
}
}

View File

@ -862,7 +862,7 @@ sub killAll
my $killall;
if ( '@HOST_OS@' eq 'BSD' )
{
$killall = 'killall -';
$killall = 'killall -q -';
} elsif ( '@HOST_OS@' eq 'solaris' ) {
$killall = 'pkill -';
} else {

View File

@ -117,7 +117,8 @@ void zmLoadConfig()
Debug( 1, "Fetching ZM_SERVER_ID For Name = %s", staticConfig.SERVER_NAME.c_str() );
std::string sql = stringtf("SELECT Id FROM Servers WHERE Name='%s'", staticConfig.SERVER_NAME.c_str() );
if ( MYSQL_ROW dbrow = zmDbFetchOne( sql.c_str() ) ) {
zmDbRow dbrow;
if ( dbrow.fetch( sql.c_str() ) ) {
staticConfig.SERVER_ID = atoi(dbrow[0]);
} else {
Fatal("Can't get ServerId for Server %s", staticConfig.SERVER_NAME.c_str() );
@ -128,7 +129,8 @@ void zmLoadConfig()
Debug( 1, "Fetching ZM_SERVER_NAME For Id = %d", staticConfig.SERVER_ID );
std::string sql = stringtf("SELECT Name FROM Servers WHERE Id='%d'", staticConfig.SERVER_ID );
if ( MYSQL_ROW dbrow = zmDbFetchOne( sql.c_str() ) ) {
zmDbRow dbrow;
if ( dbrow.fetch( sql.c_str() ) ) {
staticConfig.SERVER_NAME = std::string(dbrow[0]);
} else {
Fatal("Can't get ServerName for Server ID %d", staticConfig.SERVER_ID );

View File

@ -95,19 +95,38 @@ MYSQL_RES * zmDbFetch( const char * query ) {
return result;
} // end MYSQL_RES * zmDbFetch( const char * query );
MYSQL_ROW zmDbFetchOne( const char *query ) {
MYSQL_RES *result = zmDbFetch( query );
int n_rows = mysql_num_rows( result );
zmDbRow *zmDbFetchOne( const char *query ) {
zmDbRow *row = new zmDbRow();
if ( row->fetch( query ) ) {
return row;
}
delete row;
return NULL;
}
MYSQL_RES *zmDbRow::fetch( const char *query ) {
result_set = zmDbFetch( query );
if ( ! result_set ) return result_set;
int n_rows = mysql_num_rows( result_set );
if ( n_rows != 1 ) {
Error( "Bogus number of lines return from query, %d returned for query %s.", n_rows, query );
return NULL;
mysql_free_result( result_set );
result_set = NULL;
return result_set;
}
MYSQL_ROW dbrow = mysql_fetch_row( result );
mysql_free_result( result );
if ( ! dbrow ) {
row = mysql_fetch_row( result_set );
if ( ! row ) {
mysql_free_result( result_set );
result_set = NULL;
Error("Error getting row from query %s. Error is %s", query, mysql_error( &dbconn ) );
return NULL;
} else {
Debug(3, "Succes");
}
return dbrow;
return result_set;
}
zmDbRow::~zmDbRow() {
if ( result_set )
mysql_free_result( result_set );
}

View File

@ -22,6 +22,21 @@
#include <mysql/mysql.h>
class zmDbRow {
private:
MYSQL_RES *result_set;
MYSQL_ROW row;
public:
zmDbRow() { result_set = NULL; row = NULL; };
MYSQL_RES *fetch( const char *query );
zmDbRow( MYSQL_RES *, MYSQL_ROW *row );
~zmDbRow();
char *operator[](unsigned int index) const {
return row[index];
}
};
#ifdef __cplusplus
extern "C" {
#endif
@ -33,7 +48,7 @@ void zmDbConnect();
void zmDbClose();
MYSQL_RES * zmDbFetch( const char *query );
MYSQL_ROW zmDbFetchOne( const char *query );
zmDbRow *zmDbFetchOne( const char *query );
#ifdef __cplusplus
} /* extern "C" */

View File

@ -167,12 +167,21 @@ int SWScale::Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint
#endif
/* Check the buffer sizes */
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
size_t insize = av_image_get_buffer_size(in_pf, width, height,1);
#else
size_t insize = avpicture_get_size(in_pf, width, height);
#endif
if(insize != in_buffer_size) {
Error("The input buffer size does not match the expected size for the input format. Required: %d Available: %d", insize, in_buffer_size);
return -4;
}
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
size_t outsize = av_image_get_buffer_size(out_pf, width, height,1);
#else
size_t outsize = avpicture_get_size(out_pf, width, height);
#endif
if(outsize < out_buffer_size) {
Error("The output buffer is undersized for the output format. Required: %d Available: %d", outsize, out_buffer_size);
return -5;
@ -186,11 +195,27 @@ int SWScale::Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint
}
/* Fill in the buffers */
if(!avpicture_fill( (AVPicture*)input_avframe, (uint8_t*)in_buffer, in_pf, width, height ) ) {
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
if(av_image_fill_arrays(input_avframe->data, input_avframe->linesize,
(uint8_t*)in_buffer, in_pf, width, height, 1) <= 0)
{
#else
if(avpicture_fill( (AVPicture*)input_avframe, (uint8_t*)in_buffer,
in_pf, width, height ) <= 0)
{
#endif
Error("Failed filling input frame with input buffer");
return -7;
}
if(!avpicture_fill( (AVPicture*)output_avframe, out_buffer, out_pf, width, height ) ) {
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
if(av_image_fill_arrays(output_avframe->data, output_avframe->linesize,
out_buffer, out_pf, width, height, 1) <= 0)
{
#else
if(avpicture_fill( (AVPicture*)output_avframe, out_buffer,
out_pf, width, height ) <= 0)
{
#endif
Error("Failed filling output frame with output buffer");
return -8;
}

View File

@ -48,6 +48,12 @@ extern "C" {
#else
#include <libavcodec/opt.h>
#endif
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
#include <libavutil/imgutils.h>
#else
#include <libavutil/avutil.h>
#endif
#elif HAVE_FFMPEG_AVUTIL_H
#include <ffmpeg/avutil.h>
#include <ffmpeg/base64.h>

View File

@ -193,7 +193,13 @@ int FfmpegCamera::Capture( Image &image )
if ( frameComplete ) {
Debug( 4, "Got frame %d", frameCount );
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(mFrame->data, mFrame->linesize,
directbuffer, imagePixFormat, width, height, 1);
#else
avpicture_fill( (AVPicture *)mFrame, directbuffer,
imagePixFormat, width, height);
#endif
#if HAVE_LIBSWSCALE
if(mConvertContext == NULL) {
@ -395,7 +401,12 @@ int FfmpegCamera::OpenFfmpeg() {
Debug ( 1, "Allocated frames" );
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
int pSize = av_image_get_buffer_size( imagePixFormat, width, height,1 );
#else
int pSize = avpicture_get_size( imagePixFormat, width, height );
#endif
if( (unsigned int)pSize != imagesize) {
Fatal("Image size mismatch. Required: %d Available: %d",pSize,imagesize);
}
@ -583,8 +594,8 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
//Keep the last keyframe so we can establish immediate video
if(packet.flags & AV_PKT_FLAG_KEY) {
Debug(4, "Have keyframe");
av_copy_packet(&lastKeyframePkt, &packet);
//Debug(4, "Have keyframe");
//av_copy_packet(&lastKeyframePkt, &packet);
//TODO I think we need to store the key frame location for seeking as part of the event
}

View File

@ -644,7 +644,11 @@ LocalCamera::LocalCamera(
if ( !tmpPicture )
Fatal( "Could not allocate temporary picture" );
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
int pSize = av_image_get_buffer_size( imagePixFormat, width, height,1 );
#else
int pSize = avpicture_get_size( imagePixFormat, width, height );
#endif
if( (unsigned int)pSize != imagesize) {
Fatal("Image size mismatch. Required: %d Available: %d",pSize,imagesize);
}
@ -877,7 +881,18 @@ void LocalCamera::Initialise()
#endif
if ( !capturePictures[i] )
Fatal( "Could not allocate picture" );
avpicture_fill( (AVPicture *)capturePictures[i], (uint8_t*)v4l2_data.buffers[i].start, capturePixFormat, v4l2_data.fmt.fmt.pix.width, v4l2_data.fmt.fmt.pix.height );
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(capturePictures[i]->data,
capturePictures[i]->linesize,
(uint8_t*)v4l2_data.buffers[i].start,capturePixFormat,
v4l2_data.fmt.fmt.pix.width,
v4l2_data.fmt.fmt.pix.height, 1);
#else
avpicture_fill( (AVPicture *)capturePictures[i],
(uint8_t*)v4l2_data.buffers[i].start, capturePixFormat,
v4l2_data.fmt.fmt.pix.width,
v4l2_data.fmt.fmt.pix.height );
#endif
#endif // HAVE_LIBSWSCALE
}
@ -1035,7 +1050,16 @@ void LocalCamera::Initialise()
#endif
if ( !capturePictures[i] )
Fatal( "Could not allocate picture" );
avpicture_fill( (AVPicture *)capturePictures[i], (unsigned char *)v4l1_data.bufptr+v4l1_data.frames.offsets[i], capturePixFormat, width, height );
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(capturePictures[i]->data,
capturePictures[i]->linesize,
(unsigned char *)v4l1_data.bufptr+v4l1_data.frames.offsets[i],
capturePixFormat, width, height, 1);
#else
avpicture_fill( (AVPicture *)capturePictures[i],
(unsigned char *)v4l1_data.bufptr+v4l1_data.frames.offsets[i],
capturePixFormat, width, height );
#endif
}
#endif // HAVE_LIBSWSCALE
@ -2131,7 +2155,14 @@ int LocalCamera::Capture( Image &image )
Debug( 9, "Calling sws_scale to perform the conversion" );
/* Use swscale to convert the image directly into the shared memory */
avpicture_fill( (AVPicture *)tmpPicture, directbuffer, imagePixFormat, width, height );
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(tmpPicture->data,
tmpPicture->linesize, directbuffer,
imagePixFormat, width, height, 1);
#else
avpicture_fill( (AVPicture *)tmpPicture, directbuffer,
imagePixFormat, width, height );
#endif
sws_scale( imgConversionContext, capturePictures[capture_frame]->data, capturePictures[capture_frame]->linesize, 0, height, tmpPicture->data, tmpPicture->linesize );
}
#endif

View File

@ -1119,8 +1119,8 @@ void Monitor::DumpZoneImage( const char *zone_string ) {
Debug(3, "Trying to load from event");
// Grab the most revent event image
std::string sql = stringtf( "SELECT MAX(Id) FROM Events WHERE MonitorId=%d AND Frames > 0", id );
MYSQL_ROW eventid_row = zmDbFetchOne(sql.c_str() );
if ( eventid_row ) {
zmDbRow eventid_row;
if ( eventid_row.fetch( sql.c_str() ) ) {
int event_id = atoi( eventid_row[0] );
Debug( 3, "Got event %d", event_id );
@ -2770,8 +2770,8 @@ int Monitor::LoadFfmpegMonitors( const char *file, Monitor **&monitors, Purpose
Monitor *Monitor::Load( unsigned int p_id, bool load_zones, Purpose purpose ) {
std::string sql = stringtf( "select Id, Name, ServerId, StorageId, Type, Function+0, Enabled, LinkedMonitors, Device, Channel, Format, V4LMultiBuffer, V4LCapturesPerFrame, Protocol, Method, Host, Port, Path, Options, User, Pass, Width, Height, Colours, Palette, Orientation+0, Deinterlacing, RTSPDescribe, SaveJPEGs, VideoWriter, EncoderParameters, RecordAudio, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, LabelSize, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MotionFrameSkip, AnalysisFPS, AnalysisUpdateDelay, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, AlarmRefBlendPerc, TrackMotion, SignalCheckColour, Exif from Monitors where Id = %d", p_id );
MYSQL_ROW dbrow = zmDbFetchOne( sql.c_str() );
if ( ! dbrow ) {
zmDbRow dbrow;
if ( ! dbrow.fetch( sql.c_str() ) ) {
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}

View File

@ -333,7 +333,13 @@ void VideoStream::OpenStream( )
Panic( "Could not allocate opicture" );
}
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
int size = av_image_get_buffer_size( c->pix_fmt, c->width,
c->height, 1 );
#else
int size = avpicture_get_size( c->pix_fmt, c->width, c->height );
#endif
uint8_t *opicture_buf = (uint8_t *)av_malloc( size );
if ( !opicture_buf )
{
@ -344,7 +350,13 @@ void VideoStream::OpenStream( )
#endif
Panic( "Could not allocate opicture_buf" );
}
avpicture_fill( (AVPicture *)opicture, opicture_buf, c->pix_fmt, c->width, c->height );
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(opicture->data, opicture->linesize,
opicture_buf, c->pix_fmt, c->width, c->height, 1);
#else
avpicture_fill( (AVPicture *)opicture, opicture_buf, c->pix_fmt,
c->width, c->height );
#endif
/* if the output format is not identical to the input format, then a temporary
picture is needed too. It is then converted to the required
@ -361,7 +373,12 @@ void VideoStream::OpenStream( )
{
Panic( "Could not allocate tmp_opicture" );
}
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
int size = av_image_get_buffer_size( pf, c->width,
c->height,1 );
#else
int size = avpicture_get_size( pf, c->width, c->height );
#endif
uint8_t *tmp_opicture_buf = (uint8_t *)av_malloc( size );
if ( !tmp_opicture_buf )
{
@ -372,7 +389,14 @@ void VideoStream::OpenStream( )
#endif
Panic( "Could not allocate tmp_opicture_buf" );
}
avpicture_fill( (AVPicture *)tmp_opicture, tmp_opicture_buf, pf, c->width, c->height );
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(tmp_opicture->data,
tmp_opicture->linesize, tmp_opicture_buf, pf,
c->width, c->height, 1);
#else
avpicture_fill( (AVPicture *)tmp_opicture,
tmp_opicture_buf, pf, c->width, c->height );
#endif
}
}
@ -678,14 +702,14 @@ double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size,
#endif
if ( got_packet )
{
if ( c->coded_frame->key_frame )
{
#if LIBAVCODEC_VERSION_CHECK(52, 30, 2, 30, 2)
pkt->flags |= AV_PKT_FLAG_KEY;
#else
pkt->flags |= PKT_FLAG_KEY;
#endif
}
// if ( c->coded_frame->key_frame )
// {
//#if LIBAVCODEC_VERSION_CHECK(52, 30, 2, 30, 2)
// pkt->flags |= AV_PKT_FLAG_KEY;
//#else
// pkt->flags |= PKT_FLAG_KEY;
//#endif
// }
if ( pkt->pts != (int64_t)AV_NOPTS_VALUE )
{

View File

@ -241,7 +241,12 @@ int RemoteCameraRtsp::PrimeCapture()
if(mRawFrame == NULL || mFrame == NULL)
Fatal( "Unable to allocate frame(s)");
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
int pSize = av_image_get_buffer_size( imagePixFormat, width, height, 1 );
#else
int pSize = avpicture_get_size( imagePixFormat, width, height );
#endif
if( (unsigned int)pSize != imagesize) {
Fatal("Image size mismatch. Required: %d Available: %d",pSize,imagesize);
}
@ -498,7 +503,13 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
Debug( 3, "Got frame %d", frameCount );
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height );
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(mFrame->data, mFrame->linesize,
directbuffer, imagePixFormat, width, height, 1);
#else
avpicture_fill( (AVPicture *)mFrame, directbuffer,
imagePixFormat, width, height);
#endif
//Video recording
if ( recording && !wasRecording ) {

View File

@ -50,8 +50,8 @@ Storage::Storage( unsigned int p_id ) {
char sql[ZM_SQL_SML_BUFSIZ];
snprintf( sql, sizeof(sql), "SELECT Id, Name, Path from Storage WHERE Id=%d", p_id );
Debug(1,"Loading Storage for %d using %s", p_id, sql );
MYSQL_ROW dbrow = zmDbFetchOne( sql );
if ( ! dbrow ) {
zmDbRow dbrow;
if ( ! dbrow.fetch( sql ) ) {
Error( "Unable to load storage area for id %d: %s", p_id, mysql_error( &dbconn ) );
} else {
unsigned int index = 0;