24 #include <fvcams/net.h> 25 #include <fvcams/cam_exceptions.h> 27 #include <core/exception.h> 28 #include <core/exceptions/software.h> 30 #include <fvutils/net/fuse_client.h> 31 #include <fvutils/net/fuse_message.h> 32 #include <fvutils/net/fuse_image_content.h> 33 #include <fvutils/net/fuse_imagelist_content.h> 34 #include <fvutils/system/camargp.h> 35 #include <fvutils/compression/jpeg_decompressor.h> 37 #include <netinet/in.h> 63 NetworkCamera::NetworkCamera(
const char *host,
unsigned short port,
bool jpeg)
69 __host = strdup(host);
77 __decompressor = NULL;
78 __decompressed_buffer = NULL;
82 __fuse_message = NULL;
83 __fuse_imageinfo = NULL;
85 __fusec =
new FuseClient(__host, __port,
this);
98 NetworkCamera::NetworkCamera(
const char *host,
unsigned short port,
const char *image_id,
101 if ( image_id == NULL ) {
104 if ( host == NULL ) {
107 __image_id = strdup(image_id);
108 __host = strdup(host);
115 __remote_version = 0;
116 __decompressor = NULL;
117 __decompressed_buffer = NULL;
121 __fuse_message = NULL;
122 __fuse_imageinfo = NULL;
124 __fusec =
new FuseClient(__host, __port,
this);
142 if ( cap->
has(
"image") ) {
143 __image_id = strdup(cap->
get(
"image").c_str());
147 if ( cap->
has(
"host") ) {
148 __host = strdup(cap->
get(
"host").c_str());
150 __host = strdup(
"localhost");
152 if ( cap->
has(
"port") ) {
153 int i = atoi(cap->
get(
"port").c_str());
154 if ( (i < 0) || (i >= 0xFFFF) ) {
157 __port = (
unsigned int)i;
162 __get_jpeg = ( cap->
has(
"jpeg") && (cap->
get(
"jpeg") ==
"true"));
167 __remote_version = 0;
168 __decompressor = NULL;
169 __decompressed_buffer = NULL;
173 __fuse_message = NULL;
174 __fuse_imageinfo = NULL;
176 __fusec =
new FuseClient(__host, __port,
this);
184 NetworkCamera::~NetworkCamera()
190 if ( __decompressed_buffer != NULL) free(__decompressed_buffer);
191 delete __decompressor;
196 NetworkCamera::open()
198 if ( __opened )
return;
202 __fusec->wait_greeting();
206 strncpy(imagedesc->
image_id, __image_id, IMAGE_ID_MAX_LENGTH);
209 if ( ! __fuse_imageinfo ) {
210 throw Exception(
"Could not receive image info. Image not available?");
219 NetworkCamera::start()
225 NetworkCamera::stop()
232 NetworkCamera::print_info()
238 NetworkCamera::capture()
243 if ( __fuse_image ) {
244 throw CaptureException(
"You must dispose the buffer before fetching a new image");
252 strncpy(irm->
image_id, __image_id, IMAGE_ID_MAX_LENGTH);
253 irm->
format = (__get_jpeg ? FUSE_IF_JPEG : FUSE_IF_RAW);
257 throw CaptureException(
"Capture failed, connection died while waiting for image");
259 if ( ! __fuse_image ) {
264 if ( (__fuse_image->pixel_width() != __last_width) ||
265 (__fuse_image->pixel_height() != __last_height) ) {
266 if (__decompressed_buffer != NULL ) {
267 free(__decompressed_buffer);
269 size_t buffer_size = colorspace_buffer_size(YUV422_PLANAR, __fuse_image->pixel_width(),
270 __fuse_image->pixel_height());
271 __decompressed_buffer = (
unsigned char *)malloc(buffer_size);
272 __decompressor->set_decompressed_buffer(__decompressed_buffer, buffer_size);
274 __decompressor->set_compressed_buffer(__fuse_image->buffer(), __fuse_image->buffer_size());
275 __decompressor->decompress();
281 NetworkCamera::buffer()
284 return __decompressed_buffer;
286 if ( __fuse_image ) {
287 return __fuse_image->buffer();
295 NetworkCamera::buffer_size()
298 return colorspace_buffer_size(YUV422_PLANAR, pixel_width(), pixel_height());
300 if (! __fuse_image) {
303 return colorspace_buffer_size((colorspace_t)__fuse_image->colorspace(),
304 __fuse_image->pixel_width(),
305 __fuse_image->pixel_height());
311 NetworkCamera::close()
317 if ( __fuse_imageinfo ) {
318 free(__fuse_imageinfo);
319 __fuse_imageinfo = NULL;
322 __fusec->disconnect();
330 NetworkCamera::dispose_buffer()
334 if ( __fuse_message ) {
335 __fuse_message->unref();
336 __fuse_message = NULL;
341 NetworkCamera::pixel_width()
343 if ( __fuse_imageinfo ) {
344 return ntohl(__fuse_imageinfo->width);
351 NetworkCamera::pixel_height()
353 if ( __fuse_imageinfo ) {
354 return ntohl(__fuse_imageinfo->height);
361 NetworkCamera::capture_time()
363 if ( __fuse_image ) {
364 return __fuse_image->capture_time();
371 NetworkCamera::flush()
373 if (! __connected)
return;
379 NetworkCamera::ready()
389 NetworkCamera::set_image_id(
const char *image_id)
391 __image_id = strdup(image_id);
394 strncpy(imagedesc->
image_id, __image_id, IMAGE_ID_MAX_LENGTH);
397 if ( ! __fuse_imageinfo ) {
398 throw Exception(
"Could not received image info. Image not available?");
404 NetworkCamera::set_image_number(
unsigned int n)
411 NetworkCamera::colorspace()
414 return YUV422_PLANAR;
416 if ( __fuse_imageinfo ) {
417 return (colorspace_t)ntohs(__fuse_imageinfo->colorspace);
428 std::vector<FUSE_imageinfo_t>&
429 NetworkCamera::image_list()
431 __image_list.clear();
437 __fusec->enqueue_and_wait(FUSE_MT_GET_IMAGE_LIST);
444 NetworkCamera::fuse_invalid_server_version(uint32_t local_version,
445 uint32_t remote_version)
throw()
447 __local_version = local_version;
448 __remote_version = remote_version;
453 NetworkCamera::fuse_connection_established() throw()
460 NetworkCamera::fuse_connection_died() throw()
474 if ( __fuse_image ) {
476 __fuse_message->ref();
480 __fuse_message = NULL;
485 case FUSE_MT_IMAGE_INFO:
489 __fuse_imageinfo = NULL;
493 case FUSE_MT_IMAGE_INFO_FAILED:
494 __fuse_imageinfo = NULL;
497 case FUSE_MT_GET_IMAGE_FAILED:
498 if ( __fuse_message ) {
499 __fuse_message->unref();
501 __fuse_message = NULL;
505 case FUSE_MT_IMAGE_LIST:
508 if (fuse_image_list ) {
509 while ( fuse_image_list->
has_next() ) {
517 __image_list.push_back(ii);
char image_id[IMAGE_ID_MAX_LENGTH]
image ID
Decompressor for JPEG images.
Fawkes library namespace.
Capturing a frame failed.
A class for handling time.
A NULL pointer was supplied where not allowed.
Image description message.
uint32_t width
width in pixels
uint32_t colorspace
color space
char image_id[IMAGE_ID_MAX_LENGTH]
image ID
bool has(std::string s) const
Check if an parameter was given.
Base class for exceptions in Fawkes.
bool has_next()
Check if another image info is available.
char image_id[IMAGE_ID_MAX_LENGTH]
image ID
uint32_t format
requested image format, see FUSE_image_format_t
uint32_t height
height in pixels
uint32_t buffer_size
size of following image buffer in bytes
Expected parameter is missing.
FUSE_imageinfo_t * next()
Get next image info.
std::string get(std::string s) const
Get the value of the given parameter.