From 0da03ca5e8d466aed46bd986c4751447dacd7e1f Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sat, 20 May 2017 09:41:13 -0400 Subject: [PATCH 1/2] braces/whitespace --- src/zm_zone.cpp | 595 +++++++++++++++++++----------------------------- 1 file changed, 230 insertions(+), 365 deletions(-) diff --git a/src/zm_zone.cpp b/src/zm_zone.cpp index d2e36488f..eaaad56c5 100644 --- a/src/zm_zone.cpp +++ b/src/zm_zone.cpp @@ -23,8 +23,28 @@ #include "zm_image.h" #include "zm_monitor.h" -void Zone::Setup( Monitor *p_monitor, int p_id, const char *p_label, ZoneType p_type, const Polygon &p_polygon, const Rgb p_alarm_rgb, CheckMethod p_check_method, int p_min_pixel_threshold, int p_max_pixel_threshold, int p_min_alarm_pixels, int p_max_alarm_pixels, const Coord &p_filter_box, int p_min_filter_pixels, int p_max_filter_pixels, int p_min_blob_pixels, int p_max_blob_pixels, int p_min_blobs, int p_max_blobs, int p_overload_frames, int p_extend_alarm_frames ) -{ +void Zone::Setup( + Monitor *p_monitor, + int p_id, + const char *p_label, + ZoneType p_type, + const Polygon &p_polygon, + const Rgb p_alarm_rgb, + CheckMethod p_check_method, + int p_min_pixel_threshold, + int p_max_pixel_threshold, + int p_min_alarm_pixels, + int p_max_alarm_pixels, + const Coord &p_filter_box, + int p_min_filter_pixels, + int p_max_filter_pixels, + int p_min_blob_pixels, + int p_max_blob_pixels, + int p_min_blobs, + int p_max_blobs, + int p_overload_frames, + int p_extend_alarm_frames +) { monitor = p_monitor; id = p_id; @@ -46,7 +66,7 @@ void Zone::Setup( Monitor *p_monitor, int p_id, const char *p_label, ZoneType p_ min_blobs = p_min_blobs; max_blobs = p_max_blobs; overload_frames = p_overload_frames; - extend_alarm_frames = p_extend_alarm_frames; + extend_alarm_frames = p_extend_alarm_frames; Debug( 1, "Initialised zone %d/%s - %d - %dx%d - Rgb:%06x, CM:%d, MnAT:%d, MxAT:%d, MnAP:%d, MxAP:%d, FB:%dx%d, MnFP:%d, MxFP:%d, MnBS:%d, MxBS:%d, MnB:%d, MxB:%d, OF: %d, AF: %d", id, label, type, polygon.Width(), polygon.Height(), alarm_rgb, check_method, min_pixel_threshold, max_pixel_threshold, min_alarm_pixels, max_alarm_pixels, filter_box.X(), filter_box.Y(), min_filter_pixels, max_filter_pixels, min_blob_pixels, max_blob_pixels, min_blobs, max_blobs, overload_frames, extend_alarm_frames ); @@ -64,144 +84,113 @@ void Zone::Setup( Monitor *p_monitor, int p_id, const char *p_label, ZoneType p_ overload_count = 0; extend_alarm_count = 0; - pg_image = new Image( monitor->Width(), monitor->Height(), 1, ZM_SUBPIX_ORDER_NONE); + pg_image = new Image( monitor->Width(), monitor->Height(), 1, ZM_SUBPIX_ORDER_NONE ); pg_image->Clear(); pg_image->Fill( 0xff, polygon ); pg_image->Outline( 0xff, polygon ); ranges = new Range[monitor->Height()]; - for ( unsigned int y = 0; y < monitor->Height(); y++) - { + for ( unsigned int y = 0; y < monitor->Height(); y++ ) { ranges[y].lo_x = -1; ranges[y].hi_x = 0; ranges[y].off_x = 0; const uint8_t *ppoly = pg_image->Buffer( 0, y ); - for ( unsigned int x = 0; x < monitor->Width(); x++, ppoly++ ) - { - if ( *ppoly ) - { - if ( ranges[y].lo_x == -1 ) - { + for ( unsigned int x = 0; x < monitor->Width(); x++, ppoly++ ) { + if ( *ppoly ) { + if ( ranges[y].lo_x == -1 ) { ranges[y].lo_x = x; } - if ( (unsigned int)ranges[y].hi_x < x ) - { + if ( (unsigned int)ranges[y].hi_x < x ) { ranges[y].hi_x = x; } } } } - - if ( config.record_diag_images ) - { + + // FIXME: Is this not a problem? If you had two zones for a monitor.. then these would conflict. You would only get 1 dump file + if ( config.record_diag_images ) { static char diag_path[PATH_MAX] = ""; - if ( !diag_path[0] ) - { + if ( ! diag_path[0] ) { snprintf( diag_path, sizeof(diag_path), "%s/%s/diag-%d-poly.jpg", config.dir_events, monitor->Name(), id); } pg_image->WriteJpeg( diag_path ); } -} +} // end Zone::Setup -Zone::~Zone() -{ +Zone::~Zone() { delete[] label; delete image; delete pg_image; delete[] ranges; } -void Zone::RecordStats( const Event *event ) -{ - static char sql[ZM_SQL_MED_BUFSIZ]; +void Zone::RecordStats( const Event *event ) { + static char sql[ZM_SQL_MED_BUFSIZ]; snprintf( sql, sizeof(sql), "insert into Stats set MonitorId=%d, ZoneId=%d, EventId=%d, FrameId=%d, PixelDiff=%d, AlarmPixels=%d, FilterPixels=%d, BlobPixels=%d, Blobs=%d, MinBlobSize=%d, MaxBlobSize=%d, MinX=%d, MinY=%d, MaxX=%d, MaxY=%d, Score=%d", monitor->Id(), id, event->Id(), event->Frames()+1, pixel_diff, alarm_pixels, alarm_filter_pixels, alarm_blob_pixels, alarm_blobs, min_blob_size, max_blob_size, alarm_box.LoX(), alarm_box.LoY(), alarm_box.HiX(), alarm_box.HiY(), score ); - if ( mysql_query( &dbconn, sql ) ) - { + if ( mysql_query( &dbconn, sql ) ) { Error( "Can't insert event stats: %s", mysql_error( &dbconn ) ); exit( mysql_errno( &dbconn ) ); } -} +} // end void Zone::RecordStats( const Event *event ) - -//============================================================================= -bool Zone::CheckOverloadCount() -{ +bool Zone::CheckOverloadCount() { Info("Overloaded count: %d, Overloaded frames: %d", overload_count, overload_frames); - if ( overload_count ) - { - Info( "In overload mode, %d frames of %d remaining", overload_count, overload_frames ); - Debug( 4, "In overload mode, %d frames of %d remaining", overload_count, overload_frames ); - overload_count--; - return( false ); + if ( overload_count ) { + Info( "In overload mode, %d frames of %d remaining", overload_count, overload_frames ); + Debug( 4, "In overload mode, %d frames of %d remaining", overload_count, overload_frames ); + overload_count--; + return( false ); } return true; -} +} // end bool Zone::CheckOverloadCount() -void Zone::SetScore(unsigned int nScore) -{ +void Zone::SetScore(unsigned int nScore) { score = nScore; -} +} // end void Zone::SetScore(unsigned int nScore) - -void Zone::SetAlarmImage(const Image* srcImage) -{ +void Zone::SetAlarmImage(const Image* srcImage) { delete image; image = new Image(*srcImage); -} +} // end void Zone::SetAlarmImage( const Image* srcImage ) -int Zone::GetOverloadCount() -{ +int Zone::GetOverloadCount() { return overload_count; -} +} // end int Zone::GetOverloadCount() -void Zone::SetOverloadCount(int nOverCount) -{ +void Zone::SetOverloadCount(int nOverCount) { overload_count = nOverCount; -} +} // end void Zone::SetOverloadCount(int nOverCount ) -int Zone::GetOverloadFrames() -{ +int Zone::GetOverloadFrames() { return overload_frames; -} +} // end int Zone::GetOverloadFrames -int Zone::GetExtendAlarmCount() -{ +int Zone::GetExtendAlarmCount() { return extend_alarm_count; -} +} // end int Zone::GetExtendAlarmCount() -void Zone::SetExtendAlarmCount(int nExtendAlarmCount) -{ +void Zone::SetExtendAlarmCount(int nExtendAlarmCount) { extend_alarm_count = nExtendAlarmCount; -} +} // end void Zone::SetExtendAlarmCount( int nExtendAlarmCount ) -int Zone::GetExtendAlarmFrames() -{ +int Zone::GetExtendAlarmFrames() { return extend_alarm_frames; -} +} // end int Zone::GetExtendAlarmFrames() -bool Zone::CheckExtendAlarmCount() -{ - Info("ExtendAlarm count: %d, ExtendAlarm frames: %d", extend_alarm_count, extend_alarm_frames); - if ( extend_alarm_count ) - { - Debug( 3, "In extend mode, %d frames of %d remaining", extend_alarm_count, extend_alarm_frames ); - extend_alarm_count--; - return( true ); +bool Zone::CheckExtendAlarmCount() { + Info( "ExtendAlarm count: %d, ExtendAlarm frames: %d", extend_alarm_count, extend_alarm_frames ); + if ( extend_alarm_count ) { + Debug( 3, "In extend mode, %d frames of %d remaining", extend_alarm_count, extend_alarm_frames ); + extend_alarm_count--; + return( true ); } return false; -} +} // end bool Zone::CheckExtendAlarmCount - -//=========================================================================== - - - -bool Zone::CheckAlarms( const Image *delta_image ) -{ +bool Zone::CheckAlarms( const Image *delta_image ) { ResetStats(); - if ( overload_count ) - { + if ( overload_count ) { Info( "In overload mode, %d frames of %d remaining", overload_count, overload_frames ); Debug( 4, "In overload mode, %d frames of %d remaining", overload_count, overload_frames ); overload_count--; @@ -225,33 +214,29 @@ bool Zone::CheckAlarms( const Image *delta_image ) int alarm_mid_x = -1; int alarm_mid_y = -1; - + unsigned int lo_y = polygon.LoY(); unsigned int lo_x = polygon.LoX(); unsigned int hi_x = polygon.HiX(); unsigned int hi_y = polygon.HiY(); Debug( 4, "Checking alarms for zone %d/%s in lines %d -> %d", id, label, lo_y, hi_y ); - - - Debug( 5, "Checking for alarmed pixels" ); + /* if(config.cpu_extensions && sseversion >= 20) { - sse2_alarmedpixels(diff_image, pg_image, &alarm_pixels, &pixel_diff_count); - } else { - std_alarmedpixels(diff_image, pg_image, &alarm_pixels, &pixel_diff_count); - } */ + sse2_alarmedpixels(diff_image, pg_image, &alarm_pixels, &pixel_diff_count); + } else { + std_alarmedpixels(diff_image, pg_image, &alarm_pixels, &pixel_diff_count); + } */ std_alarmedpixels(diff_image, pg_image, &alarm_pixels, &pixel_diff_count); - - if ( config.record_diag_images ) - { + + if ( config.record_diag_images ) { static char diag_path[PATH_MAX] = ""; - if ( !diag_path[0] ) - { + if ( ! diag_path[0] ) { snprintf( diag_path, sizeof(diag_path), "%s/%s/diag-%d-%d.jpg", config.dir_events, monitor->Name(), id, 1 ); } diff_image->WriteJpeg( diag_path ); } - + if ( pixel_diff_count && alarm_pixels ) pixel_diff = pixel_diff_count/alarm_pixels; Debug( 5, "Got %d alarmed pixels, need %d -> %d, avg pixel diff %d", alarm_pixels, min_alarm_pixels, max_alarm_pixels, pixel_diff ); @@ -269,63 +254,52 @@ bool Zone::CheckAlarms( const Image *delta_image ) /* No alarmed pixels */ return (false); } - + score = (100*alarm_pixels)/polygon.Area(); - if(score < 1) + if ( score < 1 ) score = 1; /* Fix for score of 0 when frame meets thresholds but alarmed area is not big enough */ Debug( 5, "Current score is %d", score ); - - if ( check_method >= FILTERED_PIXELS ) - { + + if ( check_method >= FILTERED_PIXELS ) { int bx = filter_box.X(); int by = filter_box.Y(); int bx1 = bx-1; int by1 = by-1; Debug( 5, "Checking for filtered pixels" ); - if ( bx > 1 || by > 1 ) - { + if ( bx > 1 || by > 1 ) { // Now remove any pixels smaller than our filter size unsigned char *cpdiff; int ldx, hdx, ldy, hdy; bool block; - for ( unsigned int y = lo_y; y <= hi_y; y++ ) - { + for ( unsigned int y = lo_y; y <= hi_y; y++ ) { int lo_x = ranges[y].lo_x; int hi_x = ranges[y].hi_x; pdiff = (uint8_t*)diff_image->Buffer( lo_x, y ); - for ( int x = lo_x; x <= hi_x; x++, pdiff++ ) - { - if ( *pdiff == WHITE ) - { + for ( int x = lo_x; x <= hi_x; x++, pdiff++ ) { + if ( *pdiff == WHITE ) { // Check participation in an X block ldx = (x>=(lo_x+bx1))?-bx1:lo_x-x; hdx = (x<=(hi_x-bx1))?0:((hi_x-x)-bx1); ldy = (y>=(lo_y+by1))?-by1:lo_y-y; hdy = (y<=(hi_y-by1))?0:((hi_y-y)-by1); block = false; - for ( int dy = ldy; !block && dy <= hdy; dy++ ) - { - for ( int dx = ldx; !block && dx <= hdx; dx++ ) - { + for ( int dy = ldy; !block && dy <= hdy; dy++ ) { + for ( int dx = ldx; !block && dx <= hdx; dx++ ) { block = true; - for ( int dy2 = 0; block && dy2 < by; dy2++ ) - { - for ( int dx2 = 0; block && dx2 < bx; dx2++ ) - { + for ( int dy2 = 0; block && dy2 < by; dy2++ ) { + for ( int dx2 = 0; block && dx2 < bx; dx2++ ) { cpdiff = diff_buff + (((y+dy+dy2)*diff_width) + (x+dx+dx2)); - if ( !*cpdiff ) - { + if ( !*cpdiff ) { block = false; } } } } } - if ( !block ) - { + if ( !block ) { *pdiff = BLACK; continue; } @@ -333,24 +307,20 @@ bool Zone::CheckAlarms( const Image *delta_image ) } } } - } - else - { + } else { alarm_filter_pixels = alarm_pixels; } - - if ( config.record_diag_images ) - { + + if ( config.record_diag_images ) { static char diag_path[PATH_MAX] = ""; - if ( !diag_path[0] ) - { + if ( !diag_path[0] ) { snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-%d-%d.jpg", config.dir_events, monitor->Id(), id, 2 ); } diff_image->WriteJpeg( diag_path ); } - + Debug( 5, "Got %d filtered pixels, need %d -> %d", alarm_filter_pixels, min_filter_pixels, max_filter_pixels ); - + if( alarm_filter_pixels ) { if( min_filter_pixels && (alarm_filter_pixels < min_filter_pixels) ) { /* Not enough pixels alarmed */ @@ -364,14 +334,13 @@ bool Zone::CheckAlarms( const Image *delta_image ) /* No filtered pixels */ return (false); } - + score = (100*alarm_filter_pixels)/(polygon.Area()); - if(score < 1) + if ( score < 1 ) score = 1; /* Fix for score of 0 when frame meets thresholds but alarmed area is not big enough */ Debug( 5, "Current score is %d", score ); - if ( check_method >= BLOBS ) - { + if ( check_method >= BLOBS ) { Debug( 5, "Checking for blob pixels" ); typedef struct { unsigned char tag; int count; int lo_x; int hi_x; int lo_y; int hi_y; } BlobStats; BlobStats blob_stats[256]; @@ -380,44 +349,38 @@ bool Zone::CheckAlarms( const Image *delta_image ) uint8_t last_x, last_y; BlobStats *bsx, *bsy; BlobStats *bsm, *bss; - for ( unsigned int y = lo_y; y <= hi_y; y++ ) - { + for ( unsigned int y = lo_y; y <= hi_y; y++ ) { int lo_x = ranges[y].lo_x; int hi_x = ranges[y].hi_x; pdiff = (uint8_t*)diff_image->Buffer( lo_x, y ); - for ( int x = lo_x; x <= hi_x; x++, pdiff++ ) - { - if ( *pdiff == WHITE ) - { + for ( int x = lo_x; x <= hi_x; x++, pdiff++ ) { + if ( *pdiff == WHITE ) { Debug( 9, "Got white pixel at %d,%d (%p)", x, y, pdiff ); //last_x = (x>lo_x)?*(pdiff-1):0; //last_y = (y>lo_y&&x>=last_lo_x&&x<=last_hi_x)?*(pdiff-diff_width):0; - + last_x = 0; if(x > 0) { if((x-1) >= lo_x) { last_x = *(pdiff-1); } } - + last_y = 0; if(y > 0) { if((y-1) >= lo_y && ranges[(y-1)].lo_x <= x && ranges[(y-1)].hi_x >= x) { last_y = *(pdiff-diff_width); } } - - if ( last_x ) - { + + if ( last_x ) { Debug( 9, "Left neighbour is %d", last_x ); bsx = &blob_stats[last_x]; - if ( last_y ) - { + if ( last_y ) { Debug( 9, "Top neighbour is %d", last_y ); bsy = &blob_stats[last_y]; - if ( last_x == last_y ) - { + if ( last_x == last_y ) { Debug( 9, "Matching neighbours, setting to %d", last_x ); // Add to the blob from the x side (either side really) *pdiff = last_x; @@ -425,9 +388,7 @@ bool Zone::CheckAlarms( const Image *delta_image ) bsx->count++; if ( x > bsx->hi_x ) bsx->hi_x = x; if ( (int)y > bsx->hi_y ) bsx->hi_y = y; - } - else - { + } else { // Aggregate blobs bsm = bsx->count>=bsy->count?bsx:bsy; bss = bsm==bsx?bsy:bsx; @@ -437,19 +398,16 @@ bool Zone::CheckAlarms( const Image *delta_image ) Debug( 9, "Slave blob t:%d, c:%d, lx:%d, hx:%d, ly:%d, hy:%d", bss->tag, bss->count, bss->lo_x, bss->hi_x, bss->lo_y, bss->hi_y ); // Now change all those pixels to the other setting int changed = 0; - for ( int sy = bss->lo_y; sy <= bss->hi_y; sy++) - { + for ( int sy = bss->lo_y; sy <= bss->hi_y; sy++) { int lo_sx = bss->lo_x>=ranges[sy].lo_x?bss->lo_x:ranges[sy].lo_x; int hi_sx = bss->hi_x<=ranges[sy].hi_x?bss->hi_x:ranges[sy].hi_x; Debug( 9, "Changing %d, %d->%d", sy, lo_sx, hi_sx ); Debug( 9, "Range %d, %d->%d", sy, ranges[sy].lo_x, ranges[sy].hi_x ); spdiff = diff_buff + ((diff_width * sy) + lo_sx); - for ( int sx = lo_sx; sx <= hi_sx; sx++, spdiff++ ) - { + for ( int sx = lo_sx; sx <= hi_sx; sx++, spdiff++ ) { Debug( 9, "Pixel at %d,%d (%p) is %d", sx, sy, spdiff, *spdiff ); - if ( *spdiff == bss->tag ) - { + if ( *spdiff == bss->tag ) { Debug( 9, "Setting pixel" ); *spdiff = bsm->tag; changed++; @@ -458,8 +416,7 @@ bool Zone::CheckAlarms( const Image *delta_image ) } *pdiff = bsm->tag; alarm_blob_pixels++; - if ( !changed ) - { + if ( !changed ) { Info( "Master blob t:%d, c:%d, lx:%d, hx:%d, ly:%d, hy:%d", bsm->tag, bsm->count, bsm->lo_x, bsm->hi_x, bsm->lo_y, bsm->hi_y ); Info( "Slave blob t:%d, c:%d, lx:%d, hx:%d, ly:%d, hy:%d", bss->tag, bss->count, bss->lo_x, bss->hi_x, bss->lo_y, bss->hi_y ); Error( "No pixels changed, exiting" ); @@ -487,9 +444,7 @@ bool Zone::CheckAlarms( const Image *delta_image ) bss->hi_x = 0; bss->hi_y = 0; } - } - else - { + } else { Debug( 9, "Setting to left neighbour %d", last_x ); // Add to the blob from the x side *pdiff = last_x; @@ -498,14 +453,11 @@ bool Zone::CheckAlarms( const Image *delta_image ) if ( x > bsx->hi_x ) bsx->hi_x = x; if ( (int)y > bsx->hi_y ) bsx->hi_y = y; } - } - else - { - if ( last_y ) - { + } else { + if ( last_y ) { Debug( 9, "Top neighbour is %d", last_y ); Debug( 9, "Setting to top neighbour %d", last_y ); - + // Add to the blob from the y side BlobStats *bsy = &blob_stats[last_y]; @@ -514,28 +466,19 @@ bool Zone::CheckAlarms( const Image *delta_image ) bsy->count++; if ( x > bsy->hi_x ) bsy->hi_x = x; if ( (int)y > bsy->hi_y ) bsy->hi_y = y; - } - else - { + } else { // Create a new blob int i; - for ( i = (WHITE-1); i > 0; i-- ) - { + for ( i = (WHITE-1); i > 0; i-- ) { BlobStats *bs = &blob_stats[i]; // See if we can recycle one first, only if it's at least two rows up - if ( bs->count && bs->hi_y < (int)(y-1) ) - { - if ( (min_blob_pixels && bs->count < min_blob_pixels) || (max_blob_pixels && bs->count > max_blob_pixels) ) - { - if ( config.create_analysis_images || config.record_diag_images ) - { - for ( int sy = bs->lo_y; sy <= bs->hi_y; sy++ ) - { + if ( bs->count && bs->hi_y < (int)(y-1) ) { + if ( (min_blob_pixels && bs->count < min_blob_pixels) || (max_blob_pixels && bs->count > max_blob_pixels) ) { + if ( config.create_analysis_images || config.record_diag_images ) { + for ( int sy = bs->lo_y; sy <= bs->hi_y; sy++ ) { spdiff = diff_buff + ((diff_width * sy) + bs->lo_x); - for ( int sx = bs->lo_x; sx <= bs->hi_x; sx++, spdiff++ ) - { - if ( *spdiff == bs->tag ) - { + for ( int sx = bs->lo_x; sx <= bs->hi_x; sx++, spdiff++ ) { + if ( *spdiff == bs->tag ) { *spdiff = BLACK; } } @@ -543,7 +486,7 @@ bool Zone::CheckAlarms( const Image *delta_image ) } alarm_blobs--; alarm_blob_pixels -= bs->count; - + Debug( 6, "Eliminated blob %d, %d pixels (%d,%d - %d,%d), %d current blobs", i, bs->count, bs->lo_x, bs->lo_y, bs->hi_x, bs->hi_y, alarm_blobs ); bs->tag = 0; @@ -554,8 +497,7 @@ bool Zone::CheckAlarms( const Image *delta_image ) bs->hi_y = 0; } } - if ( !bs->count ) - { + if ( !bs->count ) { Debug( 9, "Creating new blob %d", i ); *pdiff = i; alarm_blob_pixels++; @@ -569,8 +511,7 @@ bool Zone::CheckAlarms( const Image *delta_image ) break; } } - if ( i == 0 ) - { + if ( i == 0 ) { Warning( "Max blob count reached. Unable to allocate new blobs so terminating. Zone settings may be too sensitive." ); x = hi_x+1; y = hi_y+1; @@ -580,40 +521,30 @@ bool Zone::CheckAlarms( const Image *delta_image ) } } } - if ( config.record_diag_images ) - { + if ( config.record_diag_images ) { static char diag_path[PATH_MAX] = ""; - if ( !diag_path[0] ) - { + if ( !diag_path[0] ) { snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-%d-%d.jpg", config.dir_events, monitor->Id(), id, 3 ); } diff_image->WriteJpeg( diag_path ); } - if ( !alarm_blobs ) - { + if ( !alarm_blobs ) { return( false ); } - + Debug( 5, "Got %d raw blob pixels, %d raw blobs, need %d -> %d, %d -> %d", alarm_blob_pixels, alarm_blobs, min_blob_pixels, max_blob_pixels, min_blobs, max_blobs ); // Now eliminate blobs under the threshold - for ( int i = 1; i < WHITE; i++ ) - { + for ( int i = 1; i < WHITE; i++ ) { BlobStats *bs = &blob_stats[i]; - if ( bs->count ) - { - if ( (min_blob_pixels && bs->count < min_blob_pixels) || (max_blob_pixels && bs->count > max_blob_pixels) ) - { - if ( config.create_analysis_images || config.record_diag_images ) - { - for ( int sy = bs->lo_y; sy <= bs->hi_y; sy++ ) - { + if ( bs->count ) { + if ( (min_blob_pixels && bs->count < min_blob_pixels) || (max_blob_pixels && bs->count > max_blob_pixels) ) { + if ( config.create_analysis_images || config.record_diag_images ) { + for ( int sy = bs->lo_y; sy <= bs->hi_y; sy++ ) { spdiff = diff_buff + ((diff_width * sy) + bs->lo_x); - for ( int sx = bs->lo_x; sx <= bs->hi_x; sx++, spdiff++ ) - { - if ( *spdiff == bs->tag ) - { + for ( int sx = bs->lo_x; sx <= bs->hi_x; sx++, spdiff++ ) { + if ( *spdiff == bs->tag ) { *spdiff = BLACK; } } @@ -621,7 +552,7 @@ bool Zone::CheckAlarms( const Image *delta_image ) } alarm_blobs--; alarm_blob_pixels -= bs->count; - + Debug( 6, "Eliminated blob %d, %d pixels (%d,%d - %d,%d), %d current blobs", i, bs->count, bs->lo_x, bs->lo_y, bs->hi_x, bs->hi_y, alarm_blobs ); bs->tag = 0; @@ -630,26 +561,22 @@ bool Zone::CheckAlarms( const Image *delta_image ) bs->lo_y = 0; bs->hi_x = 0; bs->hi_y = 0; - } - else - { + } else { Debug( 6, "Preserved blob %d, %d pixels (%d,%d - %d,%d), %d current blobs", i, bs->count, bs->lo_x, bs->lo_y, bs->hi_x, bs->hi_y, alarm_blobs ); if ( !min_blob_size || bs->count < min_blob_size ) min_blob_size = bs->count; if ( !max_blob_size || bs->count > max_blob_size ) max_blob_size = bs->count; } } } - if ( config.record_diag_images ) - { + if ( config.record_diag_images ) { static char diag_path[PATH_MAX] = ""; - if ( !diag_path[0] ) - { + if ( !diag_path[0] ) { snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-%d-%d.jpg", config.dir_events, monitor->Id(), id, 4 ); } diff_image->WriteJpeg( diag_path ); } Debug( 5, "Got %d blob pixels, %d blobs, need %d -> %d, %d -> %d", alarm_blob_pixels, alarm_blobs, min_blob_pixels, max_blob_pixels, min_blobs, max_blobs ); - + if( alarm_blobs ) { if( min_blobs && (alarm_blobs < min_blobs) ) { /* Not enough pixels alarmed */ @@ -663,9 +590,9 @@ bool Zone::CheckAlarms( const Image *delta_image ) /* No blobs */ return (false); } - + score = (100*alarm_blob_pixels)/(polygon.Area()); - if(score < 1) + if ( score < 1 ) score = 1; /* Fix for score of 0 when frame meets thresholds but alarmed area is not big enough */ Debug( 5, "Current score is %d", score ); @@ -673,25 +600,19 @@ bool Zone::CheckAlarms( const Image *delta_image ) alarm_hi_x = polygon.LoX()-1; alarm_lo_y = polygon.HiY()+1; alarm_hi_y = polygon.LoY()-1; - for ( int i = 1; i < WHITE; i++ ) - { + + for ( int i = 1; i < WHITE; i++ ) { BlobStats *bs = &blob_stats[i]; - if ( bs->count ) - { - if ( bs->count == max_blob_size ) - { - if ( config.weighted_alarm_centres ) - { + if ( bs->count ) { + if ( bs->count == max_blob_size ) { + if ( config.weighted_alarm_centres ) { unsigned long x_total = 0; unsigned long y_total = 0; - for ( int sy = bs->lo_y; sy <= bs->hi_y; sy++ ) - { + for ( int sy = bs->lo_y; sy <= bs->hi_y; sy++ ) { spdiff = diff_buff + ((diff_width * sy) + bs->lo_x); - for ( int sx = bs->lo_x; sx <= bs->hi_x; sx++, spdiff++ ) - { - if ( *spdiff == bs->tag ) - { + for ( int sx = bs->lo_x; sx <= bs->hi_x; sx++, spdiff++ ) { + if ( *spdiff == bs->tag ) { x_total += sx; y_total += sy; } @@ -699,9 +620,7 @@ bool Zone::CheckAlarms( const Image *delta_image ) } alarm_mid_x = int(round(x_total/bs->count)); alarm_mid_y = int(round(y_total/bs->count)); - } - else - { + } else { alarm_mid_x = int((bs->hi_x+bs->lo_x+1)/2); alarm_mid_y = int((bs->hi_y+bs->lo_y+1)/2); } @@ -711,115 +630,91 @@ bool Zone::CheckAlarms( const Image *delta_image ) if ( alarm_lo_y > bs->lo_y ) alarm_lo_y = bs->lo_y; if ( alarm_hi_x < bs->hi_x ) alarm_hi_x = bs->hi_x; if ( alarm_hi_y < bs->hi_y ) alarm_hi_y = bs->hi_y; - } - } - } - else - { + } // end if bs->count + } // end for i < WHITE + } else { alarm_mid_x = int((alarm_hi_x+alarm_lo_x+1)/2); alarm_mid_y = int((alarm_hi_y+alarm_lo_y+1)/2); } } - if ( type == INCLUSIVE ) - { + if ( type == INCLUSIVE ) { // score >>= 1; score /= 2; - } - else if ( type == EXCLUSIVE ) - { + } else if ( type == EXCLUSIVE ) { // score <<= 1; score *= 2; - } Debug( 5, "Adjusted score is %d", score ); // Now outline the changed region - if ( score ) - { + if ( score ) { alarm_box = Box( Coord( alarm_lo_x, alarm_lo_y ), Coord( alarm_hi_x, alarm_hi_y ) ); //if ( monitor->followMotion() ) - if ( true ) - { + if ( true ) { alarm_centre = Coord( alarm_mid_x, alarm_mid_y ); - } - else - { + } else { alarm_centre = alarm_box.Centre(); } - if ( (type < PRECLUSIVE) && check_method >= BLOBS && config.create_analysis_images ) - { + if ( (type < PRECLUSIVE) && check_method >= BLOBS && config.create_analysis_images ) { // First mask out anything we don't want - for ( unsigned int y = lo_y; y <= hi_y; y++ ) - { + for ( unsigned int y = lo_y; y <= hi_y; y++ ) { pdiff = diff_buff + ((diff_width * y) + lo_x); int lo_x2 = ranges[y].lo_x; int hi_x2 = ranges[y].hi_x; int lo_gap = lo_x2-lo_x; - if ( lo_gap > 0 ) - { - if ( lo_gap == 1 ) - { + if ( lo_gap > 0 ) { + if ( lo_gap == 1 ) { *pdiff++ = BLACK; - } - else - { + } else { memset( pdiff, BLACK, lo_gap ); pdiff += lo_gap; } } ppoly = pg_image->Buffer( lo_x2, y ); - for ( int x = lo_x2; x <= hi_x2; x++, pdiff++, ppoly++ ) - { - if ( !*ppoly ) - { + for ( int x = lo_x2; x <= hi_x2; x++, pdiff++, ppoly++ ) { + if ( !*ppoly ) { *pdiff = BLACK; } } int hi_gap = hi_x-hi_x2; - if ( hi_gap > 0 ) - { - if ( hi_gap == 1 ) - { + if ( hi_gap > 0 ) { + if ( hi_gap == 1 ) { *pdiff = BLACK; - } - else - { + } else { memset( pdiff, BLACK, hi_gap ); } } } - - if( monitor->Colours() == ZM_COLOUR_GRAY8 ) { + + if ( monitor->Colours() == ZM_COLOUR_GRAY8 ) { image = diff_image->HighlightEdges( alarm_rgb, ZM_COLOUR_RGB24, ZM_SUBPIX_ORDER_RGB, &polygon.Extent() ); } else { image = diff_image->HighlightEdges( alarm_rgb, monitor->Colours(), monitor->SubpixelOrder(), &polygon.Extent() ); } - + // Only need to delete this when 'image' becomes detached and points somewhere else delete diff_image; - } - else - { + } else { delete image; image = 0; } - Debug( 1, "%s: Pixel Diff: %d, Alarm Pixels: %d, Filter Pixels: %d, Blob Pixels: %d, Blobs: %d, Score: %d", Label(), pixel_diff, alarm_pixels, alarm_filter_pixels, alarm_blob_pixels, alarm_blobs, score ); + Debug( 1, "%s: Pixel Diff: %d, Alarm Pixels: %d, Filter Pixels: %d, Blob Pixels: %d, Blobs: %d, Score: %d", + Label(), pixel_diff, alarm_pixels, alarm_filter_pixels, alarm_blob_pixels, alarm_blobs, score ); } return( true ); } -bool Zone::ParsePolygonString( const char *poly_string, Polygon &polygon ) -{ +bool Zone::ParsePolygonString( const char *poly_string, Polygon &polygon ) { Debug( 3, "Parsing polygon string '%s'", poly_string ); char *str_ptr = new char[strlen(poly_string)+1]; @@ -830,27 +725,21 @@ bool Zone::ParsePolygonString( const char *poly_string, Polygon &polygon ) int n_coords = 0; int max_n_coords = strlen(str)/4; Coord *coords = new Coord[max_n_coords]; - while( true ) - { - if ( *str == '\0' ) - { + while( true ) { + if ( *str == '\0' ) { break; } ws = strchr( str, ' ' ); - if ( ws ) - { + if ( ws ) { *ws = '\0'; } char *cp = strchr( str, ',' ); - if ( !cp ) - { + if ( !cp ) { Error( "Bogus coordinate %s found in polygon string", str ); delete[] coords; delete[] str_ptr; return( false ); - } - else - { + } else { *cp = '\0'; char *xp = str; char *yp = cp+1; @@ -888,8 +777,7 @@ bool Zone::ParsePolygonString( const char *poly_string, Polygon &polygon ) return( true ); } -bool Zone::ParseZoneString( const char *zone_string, int &zone_id, int &colour, Polygon &polygon ) -{ +bool Zone::ParseZoneString( const char *zone_string, int &zone_id, int &colour, Polygon &polygon ) { Debug( 3, "Parsing zone string '%s'", zone_string ); char *str_ptr = new char[strlen(zone_string)+1]; @@ -897,14 +785,12 @@ bool Zone::ParseZoneString( const char *zone_string, int &zone_id, int &colour, strcpy( str, zone_string ); char *ws = strchr( str, ' ' ); - if ( !ws ) - { + if ( !ws ) { Debug( 3, "No initial whitespace found in zone string '%s', finishing", str ); } zone_id = strtol( str, 0, 10 ); Debug( 3, "Got zone %d from zone string", zone_id ); - if ( !ws ) - { + if ( !ws ) { delete str_ptr; return( true ); } @@ -913,14 +799,12 @@ bool Zone::ParseZoneString( const char *zone_string, int &zone_id, int &colour, str = ws+1; ws = strchr( str, ' ' ); - if ( !ws ) - { + if ( !ws ) { Debug( 3, "No secondary whitespace found in zone string '%s', finishing", zone_string ); } colour = strtol( str, 0, 16 ); Debug( 3, "Got colour %06x from zone string", colour ); - if ( !ws ) - { + if ( !ws ) { delete str_ptr; return( true ); } @@ -937,19 +821,16 @@ bool Zone::ParseZoneString( const char *zone_string, int &zone_id, int &colour, return( result ); } -int Zone::Load( Monitor *monitor, Zone **&zones ) -{ +int Zone::Load( Monitor *monitor, Zone **&zones ) { static char sql[ZM_SQL_MED_BUFSIZ]; snprintf( sql, sizeof(sql), "select Id,Name,Type+0,Units,Coords,AlarmRGB,CheckMethod+0,MinPixelThreshold,MaxPixelThreshold,MinAlarmPixels,MaxAlarmPixels,FilterX,FilterY,MinFilterPixels,MaxFilterPixels,MinBlobPixels,MaxBlobPixels,MinBlobs,MaxBlobs,OverloadFrames,ExtendAlarmFrames from Zones where MonitorId = %d order by Type, Id", monitor->Id() ); - if ( mysql_query( &dbconn, sql ) ) - { + 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 ) - { + if ( !result ) { Error( "Can't use query result: %s", mysql_error( &dbconn ) ); exit( mysql_errno( &dbconn ) ); } @@ -957,8 +838,7 @@ int Zone::Load( Monitor *monitor, Zone **&zones ) Debug( 1, "Got %d zones for monitor %s", n_zones, monitor->Name() ); delete[] zones; zones = new Zone *[n_zones]; - for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ ) - { + for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ ) { int col = 0; int Id = atoi(dbrow[col++]); @@ -982,7 +862,7 @@ int Zone::Load( Monitor *monitor, Zone **&zones ) int MaxBlobs = dbrow[col]?atoi(dbrow[col]):0; col++; int OverloadFrames = dbrow[col]?atoi(dbrow[col]):0; col++; int ExtendAlarmFrames = dbrow[col]?atoi(dbrow[col]):0; col++; - + /* HTML colour code is actually BGR in memory, we want RGB */ AlarmRGB = rgb_convert(AlarmRGB, ZM_SUBPIX_ORDER_BGR); @@ -995,14 +875,13 @@ int Zone::Load( Monitor *monitor, Zone **&zones ) } if ( polygon.LoX() < 0 || polygon.HiX() >= (int)monitor->Width() - || polygon.LoY() < 0 || polygon.HiY() >= (int)monitor->Height() ) { + || polygon.LoY() < 0 || polygon.HiY() >= (int)monitor->Height() ) { Error( "Zone %d/%s for monitor %s extends outside of image dimensions, (%d,%d), (%d,%d), ignoring", Id, Name, monitor->Name(), polygon.LoX(), polygon.LoY(), polygon.HiX(), polygon.HiY() ); n_zones -= 1; continue; } - if ( false && !strcmp( Units, "Percent" ) ) - { + if ( false && !strcmp( Units, "Percent" ) ) { MinAlarmPixels = (MinAlarmPixels*polygon.Area())/100; MaxAlarmPixels = (MaxAlarmPixels*polygon.Area())/100; MinFilterPixels = (MinFilterPixels*polygon.Area())/100; @@ -1011,53 +890,44 @@ int Zone::Load( Monitor *monitor, Zone **&zones ) MaxBlobPixels = (MaxBlobPixels*polygon.Area())/100; } - if ( atoi(dbrow[2]) == Zone::INACTIVE ) - { + if ( atoi(dbrow[2]) == Zone::INACTIVE ) { zones[i] = new Zone( monitor, Id, Name, polygon ); + } else if ( atoi(dbrow[2]) == Zone::PRIVACY ) { + zones[i] = new Zone( monitor, Id, Name, (Zone::ZoneType)Type, polygon ); } - else if ( atoi(dbrow[2]) == Zone::PRIVACY ) - { - zones[i] = new Zone( monitor, Id, Name, (Zone::ZoneType)Type, polygon ); - } - { - zones[i] = new Zone( monitor, Id, Name, (Zone::ZoneType)Type, polygon, AlarmRGB, (Zone::CheckMethod)CheckMethod, MinPixelThreshold, MaxPixelThreshold, MinAlarmPixels, MaxAlarmPixels, Coord( FilterX, FilterY ), MinFilterPixels, MaxFilterPixels, MinBlobPixels, MaxBlobPixels, MinBlobs, MaxBlobs, OverloadFrames, ExtendAlarmFrames ); - } + zones[i] = new Zone( monitor, Id, Name, (Zone::ZoneType)Type, polygon, AlarmRGB, (Zone::CheckMethod)CheckMethod, MinPixelThreshold, MaxPixelThreshold, MinAlarmPixels, MaxAlarmPixels, Coord( FilterX, FilterY ), MinFilterPixels, MaxFilterPixels, MinBlobPixels, MaxBlobPixels, MinBlobs, MaxBlobs, OverloadFrames, ExtendAlarmFrames ); } - if ( mysql_errno( &dbconn ) ) - { + if ( mysql_errno( &dbconn ) ) { Error( "Can't fetch row: %s", mysql_error( &dbconn ) ); exit( mysql_errno( &dbconn ) ); } - // Yadda yadda mysql_free_result( result ); return( n_zones ); } -bool Zone::DumpSettings( char *output, bool /*verbose*/ ) -{ +bool Zone::DumpSettings( char *output, bool /*verbose*/ ) { output[0] = 0; sprintf( output+strlen(output), " Id : %d\n", id ); sprintf( output+strlen(output), " Label : %s\n", label ); sprintf( output+strlen(output), " Type: %d - %s\n", type, - type==ACTIVE?"Active":( - type==INCLUSIVE?"Inclusive":( - type==EXCLUSIVE?"Exclusive":( - type==PRECLUSIVE?"Preclusive":( - type==INACTIVE?"Inactive":( - type==PRIVACY?"Privacy":"Unknown" - )))))); + type==ACTIVE?"Active":( + type==INCLUSIVE?"Inclusive":( + type==EXCLUSIVE?"Exclusive":( + type==PRECLUSIVE?"Preclusive":( + type==INACTIVE?"Inactive":( + type==PRIVACY?"Privacy":"Unknown" + )))))); sprintf( output+strlen(output), " Shape : %d points\n", polygon.getNumCoords() ); - for ( int i = 0; i < polygon.getNumCoords(); i++ ) - { + for ( int i = 0; i < polygon.getNumCoords(); i++ ) { sprintf( output+strlen(output), " %i: %d,%d\n", i, polygon.getCoord( i ).X(), polygon.getCoord( i ).Y() ); } sprintf( output+strlen(output), " Alarm RGB : %06x\n", alarm_rgb ); sprintf( output+strlen(output), " Check Method: %d - %s\n", check_method, - check_method==ALARMED_PIXELS?"Alarmed Pixels":( - check_method==FILTERED_PIXELS?"FilteredPixels":( - check_method==BLOBS?"Blobs":"Unknown" - ))); + check_method==ALARMED_PIXELS?"Alarmed Pixels":( + check_method==FILTERED_PIXELS?"FilteredPixels":( + check_method==BLOBS?"Blobs":"Unknown" + ))); sprintf( output+strlen(output), " Min Pixel Threshold : %d\n", min_pixel_threshold ); sprintf( output+strlen(output), " Max Pixel Threshold : %d\n", max_pixel_threshold ); sprintf( output+strlen(output), " Min Alarm Pixels : %d\n", min_alarm_pixels ); @@ -1082,38 +952,33 @@ void Zone::std_alarmedpixels(Image* pdiff_image, const Image* ppoly_image, unsig unsigned int hi_y; unsigned int lo_x; unsigned int hi_x; - + if(max_pixel_threshold) calc_max_pixel_threshold = max_pixel_threshold; - + lo_y = polygon.LoY(); hi_y = polygon.HiY(); - for ( unsigned int y = lo_y; y <= hi_y; y++ ) - { + for ( unsigned int y = lo_y; y <= hi_y; y++ ) { lo_x = ranges[y].lo_x; hi_x = ranges[y].hi_x; - + Debug( 7, "Checking line %d from %d -> %d", y, lo_x, hi_x ); pdiff = (uint8_t*)pdiff_image->Buffer( lo_x, y ); ppoly = ppoly_image->Buffer( lo_x, y ); - - for ( unsigned int x = lo_x; x <= hi_x; x++, pdiff++, ppoly++ ) - { - if ( *ppoly && (*pdiff > min_pixel_threshold) && (*pdiff <= calc_max_pixel_threshold) ) - { + + for ( unsigned int x = lo_x; x <= hi_x; x++, pdiff++, ppoly++ ) { + if ( *ppoly && (*pdiff > min_pixel_threshold) && (*pdiff <= calc_max_pixel_threshold) ) { pixelsalarmed++; pixelsdifference += *pdiff; *pdiff = WHITE; - } - else - { + } else { *pdiff = BLACK; } } } - + /* Store the results */ *pixel_count = pixelsalarmed; *pixel_sum = pixelsdifference; - Debug( 7, "STORED"); + Debug( 7, "STORED"); } From bb50784ed2a27dc39ca3b8d9697a6a0ee6767d3a Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sat, 20 May 2017 09:53:35 -0400 Subject: [PATCH 2/2] braces --- src/zm_remote_camera.cpp | 72 ++++++++++++++++----------------- src/zm_remote_camera_http.h | 3 +- src/zm_remote_camera_rtsp.cpp | 76 +++++++++++++---------------------- src/zm_remote_camera_rtsp.h | 19 ++++----- src/zm_stream.cpp | 7 +++- src/zm_videostore.cpp | 2 +- 6 files changed, 80 insertions(+), 99 deletions(-) diff --git a/src/zm_remote_camera.cpp b/src/zm_remote_camera.cpp index b85263798..99423fca1 100644 --- a/src/zm_remote_camera.cpp +++ b/src/zm_remote_camera.cpp @@ -44,59 +44,59 @@ RemoteCamera::RemoteCamera( path( p_path ), hp( 0 ) { - if ( path[0] != '/' ) - path = '/'+path; + if ( path[0] != '/' ) + path = '/'+path; } RemoteCamera::~RemoteCamera() { - if(hp != NULL) { - freeaddrinfo(hp); - hp = NULL; - } + if(hp != NULL) { + freeaddrinfo(hp); + hp = NULL; + } } void RemoteCamera::Initialise() { - if( protocol.empty() ) - Fatal( "No protocol specified for remote camera" ); + if( protocol.empty() ) + Fatal( "No protocol specified for remote camera" ); - if( host.empty() ) - Fatal( "No host specified for remote camera" ); + if( host.empty() ) + Fatal( "No host specified for remote camera" ); - if( port.empty() ) - Fatal( "No port specified for remote camera" ); + if( port.empty() ) + Fatal( "No port specified for remote camera" ); - //if( path.empty() ) - //Fatal( "No path specified for remote camera" ); + //if( path.empty() ) + //Fatal( "No path specified for remote camera" ); - // Cache as much as we can to speed things up - std::string::size_type authIndex = host.rfind( '@' ); + // Cache as much as we can to speed things up + std::string::size_type authIndex = host.rfind( '@' ); - if ( authIndex != std::string::npos ) - { - auth = host.substr( 0, authIndex ); - host.erase( 0, authIndex+1 ); - auth64 = base64Encode( auth ); + if ( authIndex != std::string::npos ) + { + auth = host.substr( 0, authIndex ); + host.erase( 0, authIndex+1 ); + auth64 = base64Encode( auth ); - authIndex = auth.rfind( ':' ); - username = auth.substr(0,authIndex); - password = auth.substr( authIndex+1, auth.length() ); + authIndex = auth.rfind( ':' ); + username = auth.substr(0,authIndex); + password = auth.substr( authIndex+1, auth.length() ); - } + } - mNeedAuth = false; - mAuthenticator = new zm::Authenticator(username,password); + mNeedAuth = false; + mAuthenticator = new zm::Authenticator(username,password); - struct addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; - int ret = getaddrinfo(host.c_str(), port.c_str(), &hints, &hp); - if ( ret != 0 ) - { - Fatal( "Can't getaddrinfo(%s port %s): %s", host.c_str(), port.c_str(), gai_strerror(ret) ); - } + int ret = getaddrinfo(host.c_str(), port.c_str(), &hints, &hp); + if ( ret != 0 ) + { + Fatal( "Can't getaddrinfo(%s port %s): %s", host.c_str(), port.c_str(), gai_strerror(ret) ); + } } diff --git a/src/zm_remote_camera_http.h b/src/zm_remote_camera_http.h index 395ae3975..fe5823397 100644 --- a/src/zm_remote_camera_http.h +++ b/src/zm_remote_camera_http.h @@ -30,8 +30,7 @@ // Class representing 'http' cameras, i.e. those which are // accessed over a network connection using http // -class RemoteCameraHttp : public RemoteCamera -{ +class RemoteCameraHttp : public RemoteCamera { protected: std::string request; struct timeval timeout; diff --git a/src/zm_remote_camera_rtsp.cpp b/src/zm_remote_camera_rtsp.cpp index 5a9028aac..9b0b6b41d 100644 --- a/src/zm_remote_camera_rtsp.cpp +++ b/src/zm_remote_camera_rtsp.cpp @@ -45,8 +45,7 @@ RemoteCameraRtsp::RemoteCameraRtsp( unsigned int p_monitor_id, const std::string else Fatal( "Unrecognised method '%s' when creating RTSP camera %d", p_method.c_str(), monitor_id ); - if ( capture ) - { + if ( capture ) { Initialise(); } @@ -76,36 +75,30 @@ RemoteCameraRtsp::RemoteCameraRtsp( unsigned int p_monitor_id, const std::string } else { Panic("Unexpected colours: %d",colours); } - -} +} // end RemoteCameraRtsp::RemoteCameraRtsp(...) -RemoteCameraRtsp::~RemoteCameraRtsp() -{ +RemoteCameraRtsp::~RemoteCameraRtsp() { av_frame_free( &mFrame ); av_frame_free( &mRawFrame ); #if HAVE_LIBSWSCALE - if ( mConvertContext ) - { + if ( mConvertContext ) { sws_freeContext( mConvertContext ); mConvertContext = NULL; } #endif - if ( mCodecContext ) - { + if ( mCodecContext ) { avcodec_close( mCodecContext ); mCodecContext = NULL; // Freed by avformat_free_context in the destructor of RtspThread class } - if ( capture ) - { + if ( capture ) { Terminate(); } } -void RemoteCameraRtsp::Initialise() -{ +void RemoteCameraRtsp::Initialise() { RemoteCamera::Initialise(); int max_size = width*height*colours; @@ -124,13 +117,11 @@ void RemoteCameraRtsp::Initialise() Connect(); } -void RemoteCameraRtsp::Terminate() -{ +void RemoteCameraRtsp::Terminate() { Disconnect(); } -int RemoteCameraRtsp::Connect() -{ +int RemoteCameraRtsp::Connect() { rtspThread = new RtspThread( monitor_id, method, protocol, host, port, path, auth, rtsp_describe ); rtspThread->start(); @@ -138,10 +129,8 @@ int RemoteCameraRtsp::Connect() return( 0 ); } -int RemoteCameraRtsp::Disconnect() -{ - if ( rtspThread ) - { +int RemoteCameraRtsp::Disconnect() { + if ( rtspThread ) { rtspThread->stop(); rtspThread->join(); delete rtspThread; @@ -150,11 +139,9 @@ int RemoteCameraRtsp::Disconnect() return( 0 ); } -int RemoteCameraRtsp::PrimeCapture() -{ +int RemoteCameraRtsp::PrimeCapture() { Debug( 2, "Waiting for sources" ); - for ( int i = 0; i < 100 && !rtspThread->hasSources(); i++ ) - { + for ( int i = 0; i < 100 && !rtspThread->hasSources(); i++ ) { usleep( 100000 ); } if ( !rtspThread->hasSources() ) @@ -241,7 +228,7 @@ int RemoteCameraRtsp::PrimeCapture() int pSize = avpicture_get_size( imagePixFormat, width, height ); #endif - if( (unsigned int)pSize != imagesize) { + if ( (unsigned int)pSize != imagesize ) { Fatal("Image size mismatch. Required: %d Available: %d",pSize,imagesize); } /* @@ -265,8 +252,7 @@ int RemoteCameraRtsp::PrimeCapture() int RemoteCameraRtsp::PreCapture() { if ( !rtspThread->isRunning() ) return( -1 ); - if ( !rtspThread->hasSources() ) - { + if ( !rtspThread->hasSources() ) { Error( "Cannot precapture, no RTP sources" ); return( -1 ); } @@ -303,25 +289,20 @@ int RemoteCameraRtsp::Capture( Image &image ) { int nalType = (buffer.head()[3] & 0x1f); // SPS The SPS NAL unit contains parameters that apply to a series of consecutive coded video pictures - if(nalType == 7) - { + if(nalType == 7) { lastSps = buffer; continue; - } + } else if(nalType == 8) { // PPS The PPS NAL unit contains parameters that apply to the decoding of one or more individual pictures inside a coded video sequence - else if(nalType == 8) - { lastPps = buffer; continue; - } + } else if(nalType == 5) { // IDR - else if(nalType == 5) - { buffer += lastSps; buffer += lastPps; } - } else { - Debug(3, "Not an h264 packet"); + } else { + Debug(3, "Not an h264 packet"); } av_init_packet( &packet ); @@ -382,7 +363,7 @@ int RemoteCameraRtsp::Capture( Image &image ) { } // end while true // can never get here. - return (0) ; + return (0); } //Function to handle capture and store @@ -392,7 +373,6 @@ int RemoteCameraRtsp::CaptureAndRecord(Image &image, timeval recording, char* ev uint8_t* directbuffer; int frameComplete = false; - while ( true ) { // WHY Are we clearing it? Might be something good in it. @@ -450,14 +430,12 @@ int RemoteCameraRtsp::CaptureAndRecord(Image &image, timeval recording, char* ev if(nalType == 7) { lastSps = buffer; continue; - } + } else if(nalType == 8) { // PPS - else if(nalType == 8) { lastPps = buffer; continue; - } + } else if(nalType == 5) { // IDR - else if(nalType == 5) { buffer += lastSps; buffer += lastPps; } @@ -525,14 +503,14 @@ int RemoteCameraRtsp::CaptureAndRecord(Image &image, timeval recording, char* ev #if HAVE_LIBSWSCALE // Why are we re-scaling after writing out the packet? if ( mConvertContext == NULL ) { - mConvertContext = sws_getContext( mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt, width, height, imagePixFormat, SWS_BICUBIC, NULL, NULL, NULL ); + mConvertContext = sws_getContext( mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt, width, height, imagePixFormat, SWS_BICUBIC, NULL, NULL, NULL ); - if ( mConvertContext == NULL ) - Fatal( "Unable to create conversion context"); + if ( mConvertContext == NULL ) + Fatal( "Unable to create conversion context"); } if ( sws_scale( mConvertContext, mRawFrame->data, mRawFrame->linesize, 0, mCodecContext->height, mFrame->data, mFrame->linesize ) < 0 ) - Fatal( "Unable to convert raw format %u to target format %u at frame %d", mCodecContext->pix_fmt, imagePixFormat, frameCount ); + Fatal( "Unable to convert raw format %u to target format %u at frame %d", mCodecContext->pix_fmt, imagePixFormat, frameCount ); #else // HAVE_LIBSWSCALE Fatal( "You must compile ffmpeg with the --enable-swscale option to use RTSP cameras" ); #endif // HAVE_LIBSWSCALE diff --git a/src/zm_remote_camera_rtsp.h b/src/zm_remote_camera_rtsp.h index 6080902b6..8ed8b713c 100644 --- a/src/zm_remote_camera_rtsp.h +++ b/src/zm_remote_camera_rtsp.h @@ -27,6 +27,7 @@ #include "zm_rtsp.h" #include "zm_ffmpeg.h" #include "zm_videostore.h" +#include "zm_packetqueue.h" // // Class representing 'rtsp' cameras, i.e. those which are @@ -52,16 +53,16 @@ protected: RtspThread *rtspThread; int frameCount; - + #if HAVE_LIBAVFORMAT - AVFormatContext *mFormatContext; - int mVideoStreamId; - int mAudioStreamId; - AVCodecContext *mCodecContext; - AVCodec *mCodec; - AVFrame *mRawFrame; - AVFrame *mFrame; - _AVPIXELFORMAT imagePixFormat; + AVFormatContext *mFormatContext; + int mVideoStreamId; + int mAudioStreamId; + AVCodecContext *mCodecContext; + AVCodec *mCodec; + AVFrame *mRawFrame; + AVFrame *mFrame; + _AVPIXELFORMAT imagePixFormat; #endif // HAVE_LIBAVFORMAT bool wasRecording; VideoStore *videoStore; diff --git a/src/zm_stream.cpp b/src/zm_stream.cpp index 01b9fa4ed..525d7b8ef 100644 --- a/src/zm_stream.cpp +++ b/src/zm_stream.cpp @@ -302,8 +302,7 @@ void StreamBase::openComms() { Error("Unable to open sock lock file %s: %s", sock_path_lock, strerror(errno) ); lock_fd = 0; - } - else if ( flock(lock_fd, LOCK_EX) != 0 ) + } else if ( flock(lock_fd, LOCK_EX) != 0 ) { Error("Unable to lock sock lock file %s: %s", sock_path_lock, strerror(errno) ); close(lock_fd); @@ -318,6 +317,8 @@ void StreamBase::openComms() if ( sd < 0 ) { Fatal( "Can't create socket: %s", strerror(errno) ); + } else { + Debug(3, "Have socket %d", sd ); } length = snprintf( loc_sock_path, sizeof(loc_sock_path), "%s/zms-%06ds.sock", config.path_socks, connkey ); @@ -332,6 +333,7 @@ void StreamBase::openComms() strncpy( loc_addr.sun_path, loc_sock_path, sizeof(loc_addr.sun_path) ); loc_addr.sun_family = AF_UNIX; + Debug(3, "Binding to %s", loc_sock_path ); if ( bind( sd, (struct sockaddr *)&loc_addr, strlen(loc_addr.sun_path)+sizeof(loc_addr.sun_family)+1 ) < 0 ) { Fatal( "Can't bind: %s", strerror(errno) ); @@ -341,6 +343,7 @@ void StreamBase::openComms() strncpy( rem_addr.sun_path, rem_sock_path, sizeof(rem_addr.sun_path) ); rem_addr.sun_family = AF_UNIX; } // end if connKey > 0 + Debug(3, "comms open" ); } void StreamBase::closeComms() diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index 82adbc35b..99b521c3c 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -549,7 +549,7 @@ void VideoStore::dumpPacket( AVPacket *pkt ){ int VideoStore::writeVideoFramePacket( AVPacket *ipkt ) { av_init_packet(&opkt); - int duration = 0; + int duration; //Scale the PTS of the outgoing packet to be the correct time base if (ipkt->pts != AV_NOPTS_VALUE) {