google code style, and when paused, don't die after 10 seconds

This commit is contained in:
Isaac Connor 2018-04-12 13:14:00 -04:00
parent 2af539e916
commit b974b4dcd1
3 changed files with 206 additions and 235 deletions

View File

@ -43,7 +43,7 @@
bool EventStream::loadInitialEventData( int monitor_id, time_t event_time ) {
static char sql[ZM_SQL_SML_BUFSIZ];
snprintf( sql, sizeof(sql), "select Id from Events where MonitorId = %d and unix_timestamp( EndTime ) > %ld order by Id asc limit 1", monitor_id, event_time );
snprintf(sql, sizeof(sql), "SELECT Id FROM Events WHERE MonitorId = %d AND unix_timestamp(EndTime) > %ld ORDER BY Id ASC LIMIT 1", monitor_id, event_time);
if ( mysql_query(&dbconn, sql) ) {
Error("Can't run query: %s", mysql_error(&dbconn));
@ -778,9 +778,8 @@ void EventStream::runStream() {
checkInitialised();
if ( type == STREAM_JPEG )
fprintf( stdout, "Content-Type: multipart/x-mixed-replace;boundary=ZoneMinderFrame\r\n\r\n" );
fputs("Content-Type: multipart/x-mixed-replace;boundary=ZoneMinderFrame\r\n\r\n", stdout);
if ( !event_data ) {
sendTextFrame("No event data found");

View File

@ -35,20 +35,20 @@ bool MonitorStream::checkSwapPath( const char *path, bool create_path ) {
Debug(3, "Swap path '%s' missing, creating", path);
if ( mkdir(path, 0755) ) {
Error("Can't mkdir %s: %s", path, strerror(errno));
return( false );
return false;
}
if ( stat(path, &stat_buf) < 0 ) {
Error("Can't stat '%s': %s", path, strerror(errno));
return( false );
return false;
}
} else {
Error("Can't stat '%s': %s", path, strerror(errno));
return( false );
return false;
}
}
if ( !S_ISDIR(stat_buf.st_mode) ) {
Error("Swap image path '%s' is not a directory", path);
return( false );
return false;
}
uid_t uid = getuid();
@ -68,9 +68,9 @@ bool MonitorStream::checkSwapPath( const char *path, bool create_path ) {
if ( (stat_buf.st_mode & mask) != mask ) {
Error("Insufficient permissions on swap image path '%s'", path);
return( false );
return false;
}
return( true );
return true;
} // end bool MonitorStream::checkSwapPath( const char *path, bool create_path )
void MonitorStream::processCommand(const CmdMsg *msg) {
@ -79,19 +79,14 @@ void MonitorStream::processCommand( const CmdMsg *msg ) {
switch( (MsgCommand)msg->msg_data[0] ) {
case CMD_PAUSE :
Debug(1, "Got PAUSE command");
// Set paused flag
paused = true;
// Set delayed flag
delayed = true;
last_frame_sent = TV_2_FLOAT(now);
break;
case CMD_PLAY :
Debug(1, "Got PLAY command");
if ( paused ) {
// Clear paused flag
paused = false;
// Set delayed_play flag
delayed = true;
}
replay_rate = ZM_RATE_BASE;
@ -99,27 +94,20 @@ void MonitorStream::processCommand( const CmdMsg *msg ) {
case CMD_VARPLAY :
Debug(1, "Got VARPLAY command");
if ( paused ) {
// Clear paused flag
paused = false;
// Set delayed_play flag
delayed = true;
}
replay_rate = ntohs(((unsigned char)msg->msg_data[2]<<8)|(unsigned char)msg->msg_data[1])-32768;
break;
case CMD_STOP :
Debug(1, "Got STOP command");
// Clear paused flag
paused = false;
// Clear delayed_play flag
delayed = false;
break;
case CMD_FASTFWD :
Debug(1, "Got FAST FWD command");
if ( paused ) {
// Clear paused flag
paused = false;
// Set delayed_play flag
delayed = true;
}
// Set play rate
@ -144,32 +132,22 @@ void MonitorStream::processCommand( const CmdMsg *msg ) {
break;
case CMD_SLOWFWD :
Debug( 1, "Got SLOW FWD command" );
// Set paused flag
paused = true;
// Set delayed flag
delayed = true;
// Set play rate
replay_rate = ZM_RATE_BASE;
// Set step
step = 1;
break;
case CMD_SLOWREV :
Debug( 1, "Got SLOW REV command" );
// Set paused flag
paused = true;
// Set delayed flag
delayed = true;
// Set play rate
replay_rate = ZM_RATE_BASE;
// Set step
step = -1;
break;
case CMD_FASTREV :
Debug( 1, "Got FAST REV command" );
if ( paused ) {
// Clear paused flag
paused = false;
// Set delayed_play flag
delayed = true;
}
// Set play rate
@ -307,7 +285,7 @@ void MonitorStream::processCommand( const CmdMsg *msg ) {
//exit( -1 );
}
}
Debug(2, "NUmber of bytes sent to (%s): (%d)", rem_addr.sun_path, nbytes );
Debug(2, "Number of bytes sent to (%s): (%d)", rem_addr.sun_path, nbytes);
// quit after sending a status, if this was a quit request
if ( (MsgCommand)msg->msg_data[0]==CMD_QUIT ) {
@ -330,7 +308,7 @@ bool MonitorStream::sendFrame( const char *filepath, struct timeval *timestamp )
if ( !send_raw ) {
Image temp_image(filepath);
return( sendFrame( &temp_image, timestamp ) );
return sendFrame(&temp_image, timestamp);
} else {
int img_buffer_size = 0;
static unsigned char img_buffer[ZM_MAX_IMAGE_SIZE];
@ -341,22 +319,21 @@ bool MonitorStream::sendFrame( const char *filepath, struct timeval *timestamp )
fclose(fdj);
} else {
Error("Can't open %s: %s", filepath, strerror(errno));
return( false );
return false;
}
// Calculate how long it takes to actually send the frame
struct timeval frameStartTime;
gettimeofday(&frameStartTime, NULL);
fprintf( stdout, "--ZoneMinderFrame\r\n" );
fputs("--ZoneMinderFrame\r\nContent-Type: image/jpeg\r\n\r\n", stdout );
fprintf(stdout, "Content-Length: %d\r\n", img_buffer_size);
fprintf( stdout, "Content-Type: image/jpeg\r\n\r\n" );
if ( fwrite(img_buffer, img_buffer_size, 1, stdout) != 1 ) {
if ( ! zm_terminate )
Error("Unable to send stream frame: %s", strerror(errno));
return( false );
return false;
}
fprintf( stdout, "\r\n\r\n" );
fputs("\r\n\r\n", stdout);
fflush(stdout);
struct timeval frameEndTime;
@ -370,10 +347,10 @@ bool MonitorStream::sendFrame( const char *filepath, struct timeval *timestamp )
last_frame_sent = TV_2_FLOAT(now);
return( true );
}
return( false );
return true;
}
return false;
} // end bool MonitorStream::sendFrame(const char *filepath, struct timeval *timestamp)
bool MonitorStream::sendFrame(Image *image, struct timeval *timestamp) {
Image *send_image = prepareImage(image);
@ -405,34 +382,36 @@ bool MonitorStream::sendFrame( Image *image, struct timeval *timestamp ) {
struct timeval frameStartTime;
gettimeofday(&frameStartTime, NULL);
fprintf( stdout, "--ZoneMinderFrame\r\n" );
fputs("--ZoneMinderFrame\r\n", stdout);
switch( type ) {
case STREAM_JPEG :
send_image->EncodeJpeg(img_buffer, &img_buffer_size);
fprintf( stdout, "Content-Type: image/jpeg\r\n" );
fputs("Content-Type: image/jpeg\r\n", stdout);
break;
case STREAM_RAW :
fprintf( stdout, "Content-Type: image/x-rgb\r\n" );
fputs("Content-Type: image/x-rgb\r\n", stdout);
img_buffer = (uint8_t*)send_image->Buffer();
img_buffer_size = send_image->Size();
break;
case STREAM_ZIP :
fprintf( stdout, "Content-Type: image/x-rgbz\r\n" );
fputs("Content-Type: image/x-rgbz\r\n",stdout);
unsigned long zip_buffer_size;
send_image->Zip(img_buffer, &zip_buffer_size);
img_buffer_size = zip_buffer_size;
break;
default :
Fatal( "Unexpected frame type %d", type );
break;
Error("Unexpected frame type %d", type);
return false;
}
fprintf(stdout, "Content-Length: %d\r\n\r\n", img_buffer_size);
if ( fwrite(img_buffer, img_buffer_size, 1, stdout) != 1 ) {
if ( !zm_terminate )
if ( !zm_terminate ){
// If the pipe was closed, we will get signalled SIGPIPE to exit, which will set zm_terminate
Error("Unable to send stream frame: %s", strerror(errno));
return( false );
}
fprintf( stdout, "\r\n\r\n" );
return false;
}
fputs("\r\n\r\n",stdout);
fflush( stdout );
struct timeval frameEndTime;
@ -465,7 +444,7 @@ void MonitorStream::runStream() {
updateFrameRate(monitor->GetFPS());
if ( type == STREAM_JPEG )
fprintf( stdout, "Content-Type: multipart/x-mixed-replace;boundary=ZoneMinderFrame\r\n\r\n" );
fputs("Content-Type: multipart/x-mixed-replace;boundary=ZoneMinderFrame\r\n\r\n", stdout);
// point to end which is theoretically not a valid value because all indexes are % image_buffer_count
unsigned int last_read_index = monitor->image_buffer_count;
@ -480,7 +459,7 @@ void MonitorStream::runStream() {
temp_read_index = temp_image_buffer_count;
temp_write_index = temp_image_buffer_count;
char *swap_path = 0;
std::string swap_path;
bool buffered_playback = false;
// 15 is the max length for the swap path suffix, /zmswap-whatever, assuming max 6 digits for monitor id
@ -491,28 +470,23 @@ void MonitorStream::runStream() {
int subfolder2_length = snprintf(NULL, 0, "/zmswap-q%06d", connkey) + 1;
int total_swap_path_length = swap_path_length + subfolder1_length + subfolder2_length;
if ( connkey && playback_buffer > 0 ) {
if ( connkey && ( playback_buffer > 0 ) ) {
if ( total_swap_path_length + max_swap_len_suffix > PATH_MAX ) {
Error("Swap Path is too long. %d > %d ", total_swap_path_length+max_swap_len_suffix, PATH_MAX);
} else {
swap_path = (char *)malloc( total_swap_path_length+max_swap_len_suffix );
strncpy( swap_path, staticConfig.PATH_SWAP.c_str(), swap_path_length );
swap_path = staticConfig.PATH_SWAP;
Debug( 3, "Checking swap path folder: %s", swap_path );
if ( checkSwapPath( swap_path, false ) ) {
// Append the subfolder name /zmswap-m{monitor-id} to the end of swap_path
int ndx = swap_path_length - 1; // Array index of the NULL terminator
snprintf( &(swap_path[ndx]), subfolder1_length, "/zmswap-m%d", monitor->Id() );
Debug( 3, "Checking swap path folder: %s", swap_path.c_str() );
if ( checkSwapPath(swap_path.c_str(), false) ) {
swap_path += stringtf("/zmswap-m%d", monitor->Id());
Debug( 4, "Checking swap path subfolder: %s", swap_path );
if ( checkSwapPath( swap_path, true ) ) {
// Append the subfolder name /zmswap-q{connection key} to the end of swap_path
ndx = swap_path_length+subfolder1_length - 2; // Array index of the NULL terminator
snprintf( &(swap_path[ndx]), subfolder2_length, "/zmswap-q%06d", connkey );
Debug(4, "Checking swap path subfolder: %s", swap_path.c_str());
if ( checkSwapPath(swap_path.c_str(), true) ) {
swap_path += stringtf("/zmswap-q%06d", connkey);
Debug( 4, "Checking swap path subfolder: %s", swap_path );
if ( checkSwapPath( swap_path, true ) ) {
Debug(4, "Checking swap path subfolder: %s", swap_path.c_str());
if ( checkSwapPath(swap_path.c_str(), true) ) {
buffered_playback = true;
}
}
@ -534,14 +508,14 @@ void MonitorStream::runStream() {
float max_secs_since_last_sent_frame = 10.0; //should be > keep alive amount (5 secs)
while ( !zm_terminate ) {
bool got_command = false;
if ( feof( stdout ) || ferror( stdout ) || !monitor->ShmValid() ) {
if ( feof(stdout) ) {
Debug(2,"feof stdout");
break;
} else if ( ferror(stdout) ) {
Debug(2,"ferror stdout");
break;
} else if ( !monitor->ShmValid() ) {
Debug(2,"monitor not valid.... maybe we should wait until it comes back.");
}
break;
}
@ -554,7 +528,6 @@ Debug(2, "Have checking command Queue for connkey: %d", connkey );
}
}
//bool frame_sent = false;
if ( buffered_playback && delayed ) {
if ( temp_read_index == temp_write_index ) {
// Go back to live viewing
@ -651,13 +624,13 @@ Debug(2, "Have checking command Queue for connkey: %d", connkey );
}
} // end if should send frame
if ( buffered_playback ) {
if ( buffered_playback && !paused ) {
if ( monitor->shared_data->valid ) {
if ( monitor->image_buffer[index].timestamp->tv_sec ) {
int temp_index = temp_write_index%temp_image_buffer_count;
Debug(2, "Storing frame %d", temp_index);
if ( !temp_image_buffer[temp_index].valid ) {
snprintf( temp_image_buffer[temp_index].file_name, sizeof(temp_image_buffer[0].file_name), "%s/zmswap-i%05d.jpg", swap_path, temp_index );
snprintf( temp_image_buffer[temp_index].file_name, sizeof(temp_image_buffer[0].file_name), "%s/zmswap-i%05d.jpg", swap_path.c_str(), temp_index );
temp_image_buffer[temp_index].valid = true;
}
memcpy( &(temp_image_buffer[temp_index].timestamp), monitor->image_buffer[index].timestamp, sizeof(temp_image_buffer[0].timestamp) );
@ -697,25 +670,25 @@ Debug(2, "Have checking command Queue for connkey: %d", connkey );
// If we didn't capture above, because frame_mod was bad? Then last_frame_sent will not have a value.
last_frame_sent = now.tv_sec;
Warning( "no last_frame_sent. Shouldn't happen. frame_mod was (%d) frame_count (%d) ", frame_mod, frame_count );
} else if ( (TV_2_FLOAT( now ) - last_frame_sent) > max_secs_since_last_sent_frame ) {
} else if ( (!paused) && ( (TV_2_FLOAT( now ) - last_frame_sent) > max_secs_since_last_sent_frame ) ) {
Error( "Terminating, last frame sent time %f secs more than maximum of %f", TV_2_FLOAT( now ) - last_frame_sent, max_secs_since_last_sent_frame );
break;
}
} // end while
if ( buffered_playback ) {
Debug( 1, "Cleaning swap files from %s", swap_path );
Debug(1, "Cleaning swap files from %s", swap_path.c_str());
struct stat stat_buf;
if ( stat( swap_path, &stat_buf ) < 0 ) {
if ( stat(swap_path.c_str(), &stat_buf) < 0 ) {
if ( errno != ENOENT ) {
Error( "Can't stat '%s': %s", swap_path, strerror(errno) );
Error("Can't stat '%s': %s", swap_path.c_str(), strerror(errno));
}
} else if ( !S_ISDIR(stat_buf.st_mode) ) {
Error( "Swap image path '%s' is not a directory", swap_path );
Error("Swap image path '%s' is not a directory", swap_path.c_str());
} else {
char glob_pattern[PATH_MAX] = "";
snprintf( glob_pattern, sizeof(glob_pattern), "%s/*.*", swap_path );
snprintf(glob_pattern, sizeof(glob_pattern), "%s/*.*", swap_path.c_str());
glob_t pglob;
int glob_status = glob(glob_pattern, 0, 0, &pglob);
if ( glob_status != 0 ) {
@ -732,15 +705,14 @@ Debug(2, "Have checking command Queue for connkey: %d", connkey );
}
}
globfree( &pglob );
if ( rmdir( swap_path ) < 0 ) {
Error( "Can't rmdir '%s': %s", swap_path, strerror(errno) );
if ( rmdir(swap_path.c_str()) < 0 ) {
Error( "Can't rmdir '%s': %s", swap_path.c_str(), strerror(errno) );
}
} // end if checking for swap_path
} // end if buffered_playback
if ( swap_path ) free( swap_path );
closeComms();
}
} // end MonitorStream::runStream
void MonitorStream::SingleImage( int scale ) {
int img_buffer_size = 0;

View File

@ -41,23 +41,23 @@ StreamBase::~StreamBase() {
bool StreamBase::loadMonitor(int monitor_id) {
if ( !(monitor = Monitor::Load(monitor_id, false, Monitor::QUERY)) ) {
Fatal( "Unable to load monitor id %d for streaming", monitor_id );
return( false );
Error("Unable to load monitor id %d for streaming", monitor_id);
return false;
}
if ( ! monitor->connect() ) {
Fatal( "Unable to connect to monitor id %d for streaming", monitor_id );
return( false );
Error("Unable to connect to monitor id %d for streaming", monitor_id);
return false;
}
return( true );
return true;
}
bool StreamBase::checkInitialised() {
if ( !monitor ) {
Fatal( "Cannot stream, not initialised" );
return( false );
return false;
}
return( true );
return true;
}
void StreamBase::updateFrameRate(double fps) {
@ -69,7 +69,7 @@ void StreamBase::updateFrameRate( double fps ) {
while( effective_fps > maxfps ) {
effective_fps /= 2.0;
frame_mod *= 2;
Debug( 3, "aEFPS:%.2f, aFM:%d", effective_fps, frame_mod );
Debug(3, "EffectiveFPS:%.2f, FrameMod:%d", effective_fps, frame_mod);
}
}
@ -80,7 +80,8 @@ bool StreamBase::checkCommandQueue() {
int nbytes = recvfrom(sd, &msg, sizeof(msg), MSG_DONTWAIT, 0, 0);
if ( nbytes < 0 ) {
if ( errno != EAGAIN ) {
Fatal( "recvfrom(), errno = %d, error = %s", errno, strerror(errno) );
Error("recvfrom(), errno = %d, error = %s", errno, strerror(errno));
return false;
}
}
//else if ( (nbytes != sizeof(msg)) )
@ -90,12 +91,12 @@ bool StreamBase::checkCommandQueue() {
else {
Debug(2, "Message length is (%d)", nbytes);
processCommand(&msg);
return( true );
return true;
}
} else {
Warning("No sd in checkCommandQueue, comms not open?");
}
return( false );
return false;
}
Image *StreamBase::prepareImage( Image *image ) {
@ -222,7 +223,7 @@ Image *StreamBase::prepareImage( Image *image ) {
last_x = x;
last_y = y;
return( image );
return image;
}
bool StreamBase::sendTextFrame( const char *frame_text ) {
@ -250,18 +251,17 @@ bool StreamBase::sendTextFrame( const char *frame_text ) {
image.EncodeJpeg(buffer, &n_bytes);
fprintf( stdout, "--ZoneMinderFrame\r\n" );
fputs("--ZoneMinderFrame\r\nContent-Type: image/jpeg\r\n\r\n", stdout);
fprintf(stdout, "Content-Length: %d\r\n", n_bytes);
fprintf( stdout, "Content-Type: image/jpeg\r\n\r\n" );
if ( fwrite(buffer, n_bytes, 1, stdout) != 1 ) {
Error("Unable to send stream text frame: %s", strerror(errno));
return( false );
return false;
}
fprintf( stdout, "\r\n\r\n" );
fputs("\r\n\r\n",stdout);
fflush(stdout);
}
last_frame_sent = TV_2_FLOAT(now);
return( true );
return true;
}
void StreamBase::openComms() {
@ -332,4 +332,4 @@ void StreamBase::closeComms() {
//unlink(sock_path_lock);
}
}
}
} // end void StreamBase::closeComms