Added scale and rate to event streaming.

git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@654 e3e1d417-86f3-4887-817a-d78f3d33393f
This commit is contained in:
stan 2003-10-16 09:00:53 +00:00
parent 169972446d
commit 05683469a2
3 changed files with 52 additions and 19 deletions

View File

@ -150,7 +150,6 @@ bool Event::SendFrameImage( const Image *image, bool alarm_frame )
}
static int jpg_buffer_size = 0;
//static unsigned char jpg_buffer[monitor->CameraWidth()*monitor->CameraHeight()];
static unsigned char jpg_buffer[ZM_MAX_IMAGE_SIZE];
image->EncodeJpeg( jpg_buffer, &jpg_buffer_size );
@ -265,7 +264,7 @@ void Event::AddFrame( struct timeval timestamp, const Image *image, const Image
}
}
void Event::StreamEvent( const char *path, int event_id, int rate, FILE *fd )
void Event::StreamEvent( const char *path, int event_id, int rate, int scale, FILE *fd )
{
static char sql[BUFSIZ];
sprintf( sql, "select Id, EventId, ImagePath, Delta from Frames where EventId = %d order by Id", event_id );
@ -282,7 +281,8 @@ void Event::StreamEvent( const char *path, int event_id, int rate, FILE *fd )
exit( mysql_errno( &dbconn ) );
}
//setbuf( fd, 0 );
setbuf( fd, 0 );
fprintf( fd, "Server: ZoneMinder Stream Server\r\n" );
fprintf( fd, "Pragma: no-cache\r\n" );
fprintf( fd, "Cache-Control: no-cache\r\n" );
@ -291,36 +291,53 @@ void Event::StreamEvent( const char *path, int event_id, int rate, FILE *fd )
fprintf( fd, "--ZoneMinderFrame\n" );
int n_frames = mysql_num_rows( result );
Info(( "Got %d frames, at rate %d", n_frames, rate ));
Info(( "Got %d frames, at rate %d, scale %d", n_frames, rate, scale ));
FILE *fdj = NULL;
int n_bytes = 0;
static unsigned char buffer[400000];
static unsigned char buffer[ZM_MAX_IMAGE_SIZE];
double last_delta = 0;
struct timeval now, last_now;
struct DeltaTimeval delta_time;
gettimeofday( &now, &dummy_tz );
for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )
{
if ( rate )
{
if ( i )
{
int delay = 0;
gettimeofday( &now, &dummy_tz );
double frame_delta = atof(dbrow[3])-last_delta;
DELTA_TIMEVAL( delta_time, now, last_now, DT_PREC_6 );
int delay = (int)((DT_GRAN_1000000*frame_delta))-delta_time.delta;
if ( rate < 0 )
delay = (int)((1000000*(atof(dbrow[3])-last_delta))*abs(rate));
delay *= abs(rate);
else
delay = (int)((1000000*(atof(dbrow[3])-last_delta))/rate);
delay /= rate;
//Info(( "FD:%lf, DDT:%d, D:%d, N:%d.%d, LN:%d.%d", frame_delta, delta_time.delta, delay, now.tv_sec, now.tv_usec, last_now.tv_sec, last_now.tv_usec ));
if ( delay > 0 )
usleep( delay );
}
last_delta = atof(dbrow[3]);
gettimeofday( &last_now, &dummy_tz );
}
char filepath[PATH_MAX];
static char filepath[PATH_MAX];
sprintf( filepath, "%s/%s", path, dbrow[2] );
fprintf( fd, "Content-type: image/jpg\n\n" );
if ( scale == 1 )
{
if ( (fdj = fopen( filepath, "r" )) )
{
fprintf( fd, "Content-type: image/jpg\n\n" );
while ( (n_bytes = fread( buffer, 1, sizeof(buffer), fdj )) )
{
fwrite( buffer, 1, n_bytes, fd );
//fwrite( buffer, 1, n_bytes, fd );
write( fileno(fd), buffer, n_bytes );
}
fprintf( fd, "\n--ZoneMinderFrame\n" );
fclose( fdj );
}
else
@ -328,6 +345,19 @@ void Event::StreamEvent( const char *path, int event_id, int rate, FILE *fd )
Error(( "Can't open %s: %s", filepath, strerror(errno) ));
}
}
else
{
Image image( filepath );
image.Scale( scale );
image.EncodeJpeg( buffer, &n_bytes );
write( fileno(fd), buffer, n_bytes );
}
fprintf( fd, "\n--ZoneMinderFrame\n" );
fflush( fd );
}
if ( mysql_errno( &dbconn ) )
{
Error(( "Can't fetch row: %s", mysql_error( &dbconn ) ));

View File

@ -83,7 +83,7 @@ public:
void AddFrames( int n_frames, struct timeval **timestamps, const Image **images );
void AddFrame( struct timeval timestamp, const Image *image, const Image *alarm_frame=NULL, unsigned int score=0 );
static void StreamEvent( const char *path, int event_id, int rate=1, FILE *fd=stdout );
static void StreamEvent( const char *path, int event_id, int rate=1, int scale=1, FILE *fd=stdout );
};
#endif // ZM_EVENT_H

View File

@ -27,6 +27,7 @@ int main( int argc, const char *argv[] )
unsigned long idle = 5000;
unsigned long refresh = 50;
unsigned int rate = 1;
unsigned int scale = 1;
int event = 0;
char *path = ".";
unsigned int ttl = 0;
@ -55,6 +56,8 @@ int main( int argc, const char *argv[] )
char *value = strtok( NULL, "=" );
if ( !strcmp( name, "rate" ) )
rate = atoi( value );
else if ( !strcmp( name, "scale" ) )
scale = atoi( value );
else if ( !strcmp( name, "refresh" ) )
refresh = atol( value );
else if ( !strcmp( name, "idle" ) )
@ -87,7 +90,7 @@ int main( int argc, const char *argv[] )
}
else
{
Event::StreamEvent( path, event, rate, stdout );
Event::StreamEvent( path, event, rate, scale, stdout );
}
return( 0 );
}