2
// ZoneMinder Stream Class Implementation, $Date: 2008-03-13 13:36:12 +0000 (Thu, 13 Mar 2008) $, $Revision: 2357 $
3
// Copyright (C) 2003, 2004, 2005, 2006 Philip Coombes
5
// This program is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU General Public License
7
// as published by the Free Software Foundation; either version 2
8
// of the License, or (at your option) any later version.
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
#include "zm_monitor.h"
26
#include "zm_stream.h"
28
StreamBase::~StreamBase()
33
bool StreamBase::loadMonitor( int monitor_id )
35
if ( !(monitor = Monitor::Load( monitor_id, false, Monitor::QUERY )) )
37
Fatal(( "Unable to load monitor id %d for streaming", monitor_id ));
43
bool StreamBase::checkInitialised()
47
Fatal(( "Cannot stream, not initialised" ));
53
void StreamBase::updateFrameRate( double fps )
55
base_fps = (int)(fps);
56
effective_fps = (base_fps*abs(replay_rate))/ZM_RATE_BASE;
58
Debug( 3, ( "FPS:%.2f, MXFPS:%d, BFPS:%d, EFPS:%d, FM:%d", fps, maxfps, base_fps, effective_fps, frame_mod ));
60
while( effective_fps > maxfps )
65
Debug( 3, ( "aEFPS:%d, aFM:%d", effective_fps, frame_mod ));
68
bool StreamBase::checkCommandQueue()
73
memset( &msg, 0, sizeof(msg) );
74
int nbytes = recvfrom( sd, &msg, sizeof(msg), MSG_DONTWAIT, 0, 0 );
77
if ( errno != EAGAIN )
79
Fatal(( "recvfrom(), errno = %d, error = %s", errno, strerror(errno) ));
82
//else if ( (nbytes != sizeof(msg)) )
84
//Error(( "Partial message received, expected %d bytes, got %d", sizeof(msg), nbytes ));
88
processCommand( &msg );
95
Image *StreamBase::prepareImage( Image *image )
97
static int last_scale = 0;
98
static int last_zoom = 0;
99
static int last_x = 0;
100
static int last_y = 0;
107
bool image_copied = false;
109
int mag = (scale * zoom) / ZM_SCALE_BASE;
110
int act_mag = mag > ZM_SCALE_BASE?ZM_SCALE_BASE:mag;
111
Debug( 3, ( "Scaling by %d, zooming by %d = magnifying by %d(%d)", scale, zoom, mag, act_mag ));
113
int last_mag = (last_scale * last_zoom) / ZM_SCALE_BASE;
114
int last_act_mag = last_mag > ZM_SCALE_BASE?ZM_SCALE_BASE:last_mag;
115
Debug( 3, ( "Last scaling by %d, zooming by %d = magnifying by %d(%d)", last_scale, last_zoom, last_mag, last_act_mag ));
117
int base_image_width = image->Width(), base_image_height = image->Height();
118
Debug( 3, ( "Base image width = %d, height = %d", base_image_width, base_image_height ));
120
int virt_image_width = (base_image_width * mag) / ZM_SCALE_BASE, virt_image_height = (base_image_height * mag) / ZM_SCALE_BASE;
121
Debug( 3, ( "Virtual image width = %d, height = %d", virt_image_width, virt_image_height ));
123
int last_virt_image_width = (base_image_width * last_mag) / ZM_SCALE_BASE, last_virt_image_height = (base_image_height * last_mag) / ZM_SCALE_BASE;
124
Debug( 3, ( "Last virtual image width = %d, height = %d", last_virt_image_width, last_virt_image_height ));
126
int act_image_width = (base_image_width * act_mag ) / ZM_SCALE_BASE, act_image_height = (base_image_height * act_mag ) / ZM_SCALE_BASE;
127
Debug( 3, ( "Actual image width = %d, height = %d", act_image_width, act_image_height ));
129
int last_act_image_width = (base_image_width * last_act_mag ) / ZM_SCALE_BASE, last_act_image_height = (base_image_height * last_act_mag ) / ZM_SCALE_BASE;
130
Debug( 3, ( "Last actual image width = %d, height = %d", last_act_image_width, last_act_image_height ));
132
int disp_image_width = (image->Width() * scale) / ZM_SCALE_BASE, disp_image_height = (image->Height() * scale) / ZM_SCALE_BASE;
133
Debug( 3, ( "Display image width = %d, height = %d", disp_image_width, disp_image_height ));
135
int last_disp_image_width = (image->Width() * last_scale) / ZM_SCALE_BASE, last_disp_image_height = (image->Height() * last_scale) / ZM_SCALE_BASE;
136
Debug( 3, ( "Last display image width = %d, height = %d", last_disp_image_width, last_disp_image_height ));
138
int send_image_width = (disp_image_width * act_mag ) / mag, send_image_height = (disp_image_height * act_mag ) / mag;
139
Debug( 3, ( "Send image width = %d, height = %d", send_image_width, send_image_height ));
141
int last_send_image_width = (last_disp_image_width * last_act_mag ) / last_mag, last_send_image_height = (last_disp_image_height * last_act_mag ) / last_mag;
142
Debug( 3, ( "Last send image width = %d, height = %d", last_send_image_width, last_send_image_height ));
144
if ( mag != ZM_SCALE_BASE )
146
Debug( 3, ( "Magnifying by %d", mag ));
147
if ( act_mag < ZM_SCALE_BASE )
151
static Image copy_image;
152
copy_image.Assign( *image );
160
Debug( 3, ( "Real image width = %d, height = %d", image->Width(), image->Height() ));
162
if ( disp_image_width < virt_image_width || disp_image_height < virt_image_height )
164
Debug( 3, ( "Got click at %d,%d", x, y ));
165
static Box last_crop;
167
if ( mag != last_mag || x != last_x || y != last_y )
172
if ( !(last_disp_image_width < last_virt_image_width || last_disp_image_height < last_virt_image_height) )
175
Debug( 3, ( "Recalculating crop" ));
176
// Recalculate crop parameters, as %ges
177
int click_x = (last_crop.LoX() * 100 ) / last_act_image_width; // Initial crop offset from last image
178
click_x += ( x * 100 ) / last_virt_image_width;
179
int click_y = (last_crop.LoY() * 100 ) / last_act_image_height; // Initial crop offset from last image
180
click_y += ( y * 100 ) / last_virt_image_height;
181
Debug( 3, ( "Got adjusted click at %d%%,%d%%", click_x, click_y ));
183
// Convert the click locations to the current image pixels
184
click_x = ( click_x * act_image_width ) / 100;
185
click_y = ( click_y * act_image_height ) / 100;
186
Debug( 3, ( "Got readjusted click at %d,%d", click_x, click_y ));
188
int lo_x = click_x - (send_image_width/2);
191
int hi_x = lo_x + (send_image_width-1);
192
if ( hi_x >= act_image_width )
194
hi_x = act_image_width - 1;
195
lo_x = hi_x - (send_image_width - 1);
198
int lo_y = click_y - (send_image_height/2);
201
int hi_y = lo_y + (send_image_height-1);
202
if ( hi_y >= act_image_height )
204
hi_y = act_image_height - 1;
205
lo_y = hi_y - (send_image_height - 1);
207
last_crop = Box( lo_x, lo_y, hi_x, hi_y );
209
Debug( 3, ( "Cropping to %d,%d -> %d,%d", last_crop.LoX(), last_crop.LoY(), last_crop.HiX(), last_crop.HiY() ));
212
static Image copy_image;
213
copy_image.Assign( *image );
217
image->Crop( last_crop );
227
void StreamBase::sendTextFrame( const char *frame_text )
229
Debug( 2, ( "Sending text frame '%s'", frame_text ));
231
Image image( monitor->Width(), monitor->Height(), monitor->Colours() );
232
image.Annotate( frame_text, image.centreCoord( frame_text ) );
236
image.Scale( scale );
239
if ( type == STREAM_MPEG )
243
vid_stream = new VideoStream( "pipe:", format, bitrate, effective_fps, image.Colours(), image.Width(), image.Height() );
244
fprintf( stdout, "Content-type: %s\r\n\r\n", vid_stream->MimeType() );
245
vid_stream->OpenStream();
247
/* double pts = */ vid_stream->EncodeFrame( image.Buffer(), image.Size() );
250
#endif // HAVE_LIBAVCODEC
252
static unsigned char buffer[ZM_MAX_IMAGE_SIZE];
255
image.EncodeJpeg( buffer, &n_bytes );
257
fprintf( stdout, "--ZoneMinderFrame\r\n" );
258
fprintf( stdout, "Content-Length: %d\r\n", n_bytes );
259
fprintf( stdout, "Content-Type: image/jpeg\r\n\r\n" );
260
fwrite( buffer, n_bytes, 1, stdout );
261
fprintf( stdout, "\r\n\r\n" );
264
last_frame_sent = TV_2_FLOAT( now );
267
void StreamBase::openComms()
271
sd = socket( AF_UNIX, SOCK_DGRAM, 0 );
274
Fatal(( "Can't create socket: %s", strerror(errno) ));
277
snprintf( loc_sock_path, sizeof(loc_sock_path), "%s/zms-%06ds.sock", config.path_socks, connkey );
278
unlink( loc_sock_path );
280
strncpy( loc_addr.sun_path, loc_sock_path, sizeof(loc_addr.sun_path) );
281
loc_addr.sun_family = AF_UNIX;
282
if ( bind( sd, (struct sockaddr *)&loc_addr, strlen(loc_addr.sun_path)+sizeof(loc_addr.sun_family)) < 0 )
284
Fatal(( "Can't bind: %s", strerror(errno) ));
287
snprintf( rem_sock_path, sizeof(rem_sock_path), "%s/zms-%06dw.sock", config.path_socks, connkey );
288
strncpy( rem_addr.sun_path, rem_sock_path, sizeof(rem_addr.sun_path) );
289
rem_addr.sun_family = AF_UNIX;
293
void StreamBase::closeComms()
302
if ( loc_sock_path[0] )
304
unlink( loc_sock_path );