From e70d037895975c1bf711f02b943116ad8d11a1cd Mon Sep 17 00:00:00 2001 From: SteveGilvarry Date: Sat, 27 Feb 2016 01:23:20 +1100 Subject: [PATCH 01/10] Start avpicture deprecation --- src/zm_ffmpeg_camera.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index 9e3a2960c..062d819d9 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -182,8 +182,8 @@ int FfmpegCamera::Capture( Image &image ) if ( frameComplete ) { Debug( 3, "Got frame %d", frameCount ); - - avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height); + av_image_fill_arrays(mFrame->data, mFrame->linesize, directbuffer, imagePixFormat, width, height, 1); + //avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height); #if HAVE_LIBSWSCALE if(mConvertContext == NULL) { From 2122d6e443bd468972f2def40b4b0450c57130b3 Mon Sep 17 00:00:00 2001 From: SteveGilvarry Date: Thu, 3 Mar 2016 01:03:55 +1100 Subject: [PATCH 02/10] Fix all the av_picture related deprecations --- src/zm_ffmpeg.cpp | 41 ++++++++++++++++++++++++++++------- src/zm_ffmpeg.h | 6 +++++ src/zm_ffmpeg_camera.cpp | 14 ++++++++++-- src/zm_local_camera.cpp | 39 +++++++++++++++++++++++++++++---- src/zm_mpeg.cpp | 28 ++++++++++++++++++++++-- src/zm_remote_camera_rtsp.cpp | 16 ++++++++++++-- 6 files changed, 126 insertions(+), 18 deletions(-) diff --git a/src/zm_ffmpeg.cpp b/src/zm_ffmpeg.cpp index 86817c223..1e54ad9e8 100644 --- a/src/zm_ffmpeg.cpp +++ b/src/zm_ffmpeg.cpp @@ -160,12 +160,21 @@ int SWScale::Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint #endif /* Check the buffer sizes */ - size_t insize = avpicture_get_size(in_pf, width, height); +#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; } - size_t outsize = avpicture_get_size(out_pf, width, height); +#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; @@ -179,13 +188,29 @@ 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 ) ) { - Error("Failed filling input frame with input buffer"); - return -7; +#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 ) ) { - Error("Failed filling output frame with output buffer"); - return -8; +#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; } /* Do the conversion */ diff --git a/src/zm_ffmpeg.h b/src/zm_ffmpeg.h index 031a10671..7806682c7 100644 --- a/src/zm_ffmpeg.h +++ b/src/zm_ffmpeg.h @@ -47,6 +47,12 @@ extern "C" { #else #include #endif + +#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0) +#include +#else +#include +#endif #elif HAVE_FFMPEG_AVUTIL_H #include #include diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index 062d819d9..cbb3e7eff 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -182,8 +182,13 @@ int FfmpegCamera::Capture( Image &image ) if ( frameComplete ) { Debug( 3, "Got frame %d", frameCount ); - av_image_fill_arrays(mFrame->data, mFrame->linesize, directbuffer, imagePixFormat, width, height, 1); - //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) { @@ -345,7 +350,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); } diff --git a/src/zm_local_camera.cpp b/src/zm_local_camera.cpp index 022f35dbe..e1dadeb74 100644 --- a/src/zm_local_camera.cpp +++ b/src/zm_local_camera.cpp @@ -626,7 +626,11 @@ LocalCamera::LocalCamera( int p_id, const std::string &p_device, int p_channel, if ( !tmpPicture ) Fatal( "Could not allocate temporary picture" ); - int pSize = avpicture_get_size( imagePixFormat, width, height ); +#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); } @@ -859,7 +863,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 } @@ -1017,7 +1032,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 @@ -2113,7 +2137,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 diff --git a/src/zm_mpeg.cpp b/src/zm_mpeg.cpp index 01f9006b8..77a80eb37 100644 --- a/src/zm_mpeg.cpp +++ b/src/zm_mpeg.cpp @@ -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 } } diff --git a/src/zm_remote_camera_rtsp.cpp b/src/zm_remote_camera_rtsp.cpp index a07d97a8e..9bbe458bf 100644 --- a/src/zm_remote_camera_rtsp.cpp +++ b/src/zm_remote_camera_rtsp.cpp @@ -214,7 +214,12 @@ int RemoteCameraRtsp::PrimeCapture() if(mRawFrame == NULL || mFrame == NULL) Fatal( "Unable to allocate frame(s)"); - int pSize = avpicture_get_size( imagePixFormat, width, height ); +#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); } @@ -330,7 +335,14 @@ int RemoteCameraRtsp::Capture( Image &image ) 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 + #if HAVE_LIBSWSCALE if(mConvertContext == NULL) { From 5f4441878e116baff4c0f23ba7145f40f461a3f6 Mon Sep 17 00:00:00 2001 From: SteveGilvarry Date: Thu, 3 Mar 2016 17:16:06 +1100 Subject: [PATCH 03/10] Coded_Frame deprecation all examples were no lomger setting AV_PKT_FLAG_KEY, is it on the packet already. --- src/zm_mpeg.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/zm_mpeg.cpp b/src/zm_mpeg.cpp index 77a80eb37..1cdf73057 100644 --- a/src/zm_mpeg.cpp +++ b/src/zm_mpeg.cpp @@ -702,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 ) { From 7058b0862acd7b6a5f1a415514fce8d5087fcfec Mon Sep 17 00:00:00 2001 From: abi Date: Sat, 16 Apr 2016 16:39:58 +0300 Subject: [PATCH 04/10] suppressed warnings of killall for FreeBSD --- scripts/zmdc.pl.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/zmdc.pl.in b/scripts/zmdc.pl.in index b068b9a49..36d5e922a 100644 --- a/scripts/zmdc.pl.in +++ b/scripts/zmdc.pl.in @@ -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 { From d9646d6ceb99d8f567619db426e2d0e63c9ed9ae Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 19 Apr 2016 15:19:37 -0400 Subject: [PATCH 05/10] Populate a Monitors hash with the Monitors data so we don't have to use in JOINs later --- scripts/zmaudit.pl.in | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index c95f65441..7f7fc4257 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -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} ) From 588dbc0c3c3a71dfea1653bddce0af3551560204 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 19 Apr 2016 15:22:46 -0400 Subject: [PATCH 06/10] Don't JOIN MOnitors to get event prefix. Use {->MonitorID} instead --- scripts/zmaudit.pl.in | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index 7f7fc4257..d3f5eadd2 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -471,16 +471,14 @@ MAIN: while( $loop ) { # New audit to close any events that were left open for longer than MIN_AGE seconds my $selectUnclosedEventsSql = - "SELECT E.Id, + "SELECT E.Id, 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, - M.EventPrefix as Prefix + max(F.Score) as MaxScore 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)" @@ -512,7 +510,7 @@ MAIN: while( $loop ) { $res = $updateUnclosedEventsSth->execute ( sprintf("%s%d%s", - $event->{Prefix}, + $Monitors{$event->{MonitorId}->{Prefix}, $event->{Id}, RECOVER_TAG ), From bb15915ce58681c3dcccf9c44f633379eb20d1f6 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 19 Apr 2016 15:23:36 -0400 Subject: [PATCH 07/10] Use EventPrefix, not Prefix --- scripts/zmaudit.pl.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index d3f5eadd2..b1e54cd17 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -510,7 +510,7 @@ MAIN: while( $loop ) { $res = $updateUnclosedEventsSth->execute ( sprintf("%s%d%s", - $Monitors{$event->{MonitorId}->{Prefix}, + $Monitors{$event->{MonitorId}->{EventPrefix}, $event->{Id}, RECOVER_TAG ), From d2870f06928b0975fca082c58649c5260ba45394 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 20 Apr 2016 09:44:11 -0400 Subject: [PATCH 08/10] move the mysql_free_result, looks like we can't free it until we free the row --- src/zm_db.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/zm_db.cpp b/src/zm_db.cpp index a342ca14b..bc15d9ec6 100644 --- a/src/zm_db.cpp +++ b/src/zm_db.cpp @@ -100,12 +100,13 @@ MYSQL_ROW zmDbFetchOne( const char *query ) { int n_rows = mysql_num_rows( result ); if ( n_rows != 1 ) { Error( "Bogus number of lines return from query, %d returned for query %s.", n_rows, query ); + mysql_free_result( result ); return NULL; } MYSQL_ROW dbrow = mysql_fetch_row( result ); - mysql_free_result( result ); if ( ! dbrow ) { + mysql_free_result( result ); Error("Error getting row from query %s. Error is %s", query, mysql_error( &dbconn ) ); return NULL; } From e52e88055367fa3eb60981f687f931207ef626eb Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 20 Apr 2016 10:00:13 -0400 Subject: [PATCH 09/10] rework open event check to not use group by --- scripts/zmaudit.pl.in | 76 ++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index b1e54cd17..a8cebde89 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -471,18 +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, E.MonitorId, - 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 - 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)" - ; + 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 = @@ -507,26 +519,32 @@ MAIN: while( $loop ) { aud_print( "Found open event '$event->{Id}'" ); if ( confirm( 'close', 'closing' ) ) { - $res = $updateUnclosedEventsSth->execute - ( - sprintf("%s%d%s", - $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}) - : 0 - , - $event->{MaxScore}, - RECOVER_TEXT, - $event->{Id} - ) or Fatal( "Can't execute: ".$updateUnclosedEventsSth->errstr() ); + $res = $selectFrameDataSth->execute( $event->{Id} ); + my $frame = $selectFrameDataSth->fetchrow_hashref(); + if ( $frame ) { + $res = $updateUnclosedEventsSth->execute + ( + sprintf("%s%d%s", + $Monitors{$event->{MonitorId}}->{EventPrefix}, + $event->{Id}, + RECOVER_TAG + ), + $frame->{EndTime}, + $frame->{EndTimeStamp} - $event->{TimeStamp}, + $frame->{Frames}, + $frame->{AlarmFrames}, + $frame->{TotScore}, + $frame->{AlarmFrames} + ? int($frame->{TotScore} / $frame->{AlarmFrames}) + : 0 + , + $frame->{MaxScore}, + RECOVER_TEXT, + $event->{Id} + ) or Fatal( "Can't execute: ".$updateUnclosedEventsSth->errstr() ); + } else { + Error("SHOULD DELETE"); + } # end if has frame data } } From 44f32f46b7620a63e2a43a9a41f4ae8e4bd2ea89 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 20 Apr 2016 11:56:58 -0400 Subject: [PATCH 10/10] deprecate zmdbFetchOne in favor of zmDbRow --- src/zm_config.cpp | 6 +++-- src/zm_db.cpp | 50 +++++++++++++++++++++++++++------------- src/zm_db.h | 17 +++++++++++++- src/zm_ffmpeg_camera.cpp | 4 ++-- src/zm_monitor.cpp | 8 +++---- src/zm_storage.cpp | 4 ++-- 6 files changed, 62 insertions(+), 27 deletions(-) diff --git a/src/zm_config.cpp b/src/zm_config.cpp index 5a7454a7b..1a5c65840 100644 --- a/src/zm_config.cpp +++ b/src/zm_config.cpp @@ -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 ); diff --git a/src/zm_db.cpp b/src/zm_db.cpp index bc15d9ec6..7d418abf9 100644 --- a/src/zm_db.cpp +++ b/src/zm_db.cpp @@ -95,20 +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 ); - if ( n_rows != 1 ) { - Error( "Bogus number of lines return from query, %d returned for query %s.", n_rows, query ); - mysql_free_result( result ); - return NULL; - } - - MYSQL_ROW dbrow = mysql_fetch_row( result ); - if ( ! dbrow ) { - mysql_free_result( result ); - Error("Error getting row from query %s. Error is %s", query, mysql_error( &dbconn ) ); - return NULL; - } - return dbrow; +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 ); + mysql_free_result( result_set ); + result_set = NULL; + return result_set; + } + + 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 ) ); + } else { + Debug(3, "Succes"); + } + return result_set; +} +zmDbRow::~zmDbRow() { + if ( result_set ) + mysql_free_result( result_set ); } diff --git a/src/zm_db.h b/src/zm_db.h index 6ec1b5e4e..50ae2974f 100644 --- a/src/zm_db.h +++ b/src/zm_db.h @@ -22,6 +22,21 @@ #include +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" */ diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index 7e1813896..88a2a43c5 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -594,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 } diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index b747934d2..b90820362 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -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 ) ); } diff --git a/src/zm_storage.cpp b/src/zm_storage.cpp index 14a6cfc01..ff2bce5a9 100644 --- a/src/zm_storage.cpp +++ b/src/zm_storage.cpp @@ -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;