[aravis] fake: fake gv camera stream mostly working.



commit 168ff59b32d3f6bcd84d83f21462d9869be1dbba
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Thu Jun 3 15:51:52 2010 +0200

    fake: fake gv camera stream mostly working.

 src/arvfakecamera.c   |   40 ++++++++++++++++-
 src/arvfakecamera.h   |    3 +
 src/arvfakegvcamera.c |  120 ++++++++++++++++++++++++++++++++-----------------
 src/arvgvsp.c         |   32 +++++++------
 4 files changed, 138 insertions(+), 57 deletions(-)
---
diff --git a/src/arvfakecamera.c b/src/arvfakecamera.c
index 1e919fa..241219b 100644
--- a/src/arvfakecamera.c
+++ b/src/arvfakecamera.c
@@ -118,6 +118,19 @@ _get_register (ArvFakeCamera *camera, guint32 address)
 	return *((guint32 *) ((void*) (camera->priv->memory + address)));
 }
 
+size_t
+arv_fake_camera_get_payload (ArvFakeCamera *camera)
+{
+	guint32 width, height;
+
+	g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), 0);
+
+	width = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_WIDTH);
+	height = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_HEIGHT);
+
+	return width * height;
+}
+
 void
 arv_fake_camera_wait_for_next_frame (ArvFakeCamera *camera)
 {
@@ -172,7 +185,7 @@ arv_fake_camera_fill_buffer (ArvFakeCamera *camera, ArvBuffer *buffer)
 	buffer->width = width;
 	buffer->height = height;
 	buffer->status = ARV_BUFFER_STATUS_SUCCESS;
-	buffer->timestamp_ns = time.tv_sec * 1000000000L + time.tv_nsec;
+	buffer->timestamp_ns = time.tv_sec * 1000000000LL + time.tv_nsec;
 	buffer->frame_id = camera->priv->frame_id++;
 	buffer->pixel_format = _get_register (camera, ARV_FAKE_CAMERA_REGISTER_PIXEL_FORMAT);
 
@@ -196,6 +209,31 @@ arv_fake_camera_set_inet_address (ArvFakeCamera *camera, GInetAddress *address)
 				      g_inet_address_get_native_size (address), (char *) bytes);
 }
 
+guint32
+arv_fake_camera_get_acquisition_status (ArvFakeCamera *camera)
+{
+	g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), 0);
+
+	return _get_register (camera, ARV_FAKE_CAMERA_REGISTER_ACQUISITION);
+}
+
+GSocketAddress *
+arv_fake_camera_get_stream_address (ArvFakeCamera *camera)
+{
+	GSocketAddress *stream_socket_address;
+	GInetAddress *inet_address;
+
+	g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), NULL);
+
+	inet_address = g_inet_address_new_from_bytes (camera->priv->memory + ARV_GVBS_FIRST_STREAM_CHANNEL_IP_ADDRESS,
+						      G_SOCKET_FAMILY_IPV4);
+	stream_socket_address = g_inet_socket_address_new (inet_address,
+							   _get_register (camera, ARV_GVBS_FIRST_STREAM_CHANNEL_PORT));
+	g_object_unref (inet_address);
+
+	return stream_socket_address;
+}
+
 void
 arv_fake_camera_set_trigger_frequency (ArvFakeCamera *camera, double frequency)
 {
diff --git a/src/arvfakecamera.h b/src/arvfakecamera.h
index 00eed28..2ecff02 100644
--- a/src/arvfakecamera.h
+++ b/src/arvfakecamera.h
@@ -105,9 +105,12 @@ gboolean	arv_fake_camera_write_memory	(ArvFakeCamera *camera, guint32 address, g
 gboolean 	arv_fake_camera_read_register	(ArvFakeCamera *camera, guint32 address, guint32 *value);
 gboolean	arv_fake_camera_write_register 	(ArvFakeCamera *camera, guint32 address, guint32 value);
 
+size_t 		arv_fake_camera_get_payload 		(ArvFakeCamera *camera);
 void 		arv_fake_camera_wait_for_next_frame 	(ArvFakeCamera *camera);
 void		arv_fake_camera_fill_buffer		(ArvFakeCamera *camera, ArvBuffer *buffer);
 
+guint32 	arv_fake_camera_get_acquisition_status 	(ArvFakeCamera *camera);
+GSocketAddress *arv_fake_camera_get_stream_address 	(ArvFakeCamera *camera);
 void		arv_fake_camera_set_inet_address	(ArvFakeCamera *camera, GInetAddress *address);
 
 void 		arv_set_fake_camera_genicam_filename 	(const char *filename);
diff --git a/src/arvfakegvcamera.c b/src/arvfakegvcamera.c
index 4f061e4..1bc8941 100644
--- a/src/arvfakegvcamera.c
+++ b/src/arvfakegvcamera.c
@@ -30,70 +30,106 @@ void *
 arv_fake_gv_camera_thread (void *user_data)
 {
 	ArvFakeGvCamera *gv_camera = user_data;
-	ArvBuffer *image_buffer;
-	GSocketAddress *socket_address;
-	GInetAddress *inet_address;
+	ArvBuffer *image_buffer = NULL;
+	GError *error = NULL;
+	GSocketAddress *stream_address = NULL;
 	void *packet_buffer;
 	size_t packet_size;
-	size_t payload = 512*512;
+	size_t payload = 0;
 	guint32 frame_id = 0;
 	guint16 block_id;
 	ptrdiff_t offset;
 
-	image_buffer = arv_buffer_new (2048*2048, NULL);
-	packet_buffer = g_malloc (65536);
-	inet_address = g_inet_address_new_from_string ("127.0.0.1");
-	socket_address = g_inet_socket_address_new (inet_address, 58000);
-	g_object_unref (inet_address);
+	packet_buffer = g_malloc (ARV_FAKE_GV_CAMERA_BUFFER_SIZE);
 
 	do {
-		arv_fake_camera_wait_for_next_frame (gv_camera->camera);
-		arv_fake_camera_fill_buffer (gv_camera->camera, image_buffer);
+		if (arv_fake_camera_get_acquisition_status (gv_camera->camera) == 0) {
+			if (stream_address != NULL) {
+				g_object_unref (stream_address);
+				stream_address = NULL;
+				g_object_unref (image_buffer);
+				image_buffer = NULL;
+				arv_debug ("camera", "[FakeGvCamera::stream_thread] Stop stream");
+			}
+			g_usleep (100000);
+		} else {
+			if (stream_address == NULL) {
+				GInetAddress *inet_address;
+				char *inet_address_string;
+
+				stream_address = arv_fake_camera_get_stream_address (gv_camera->camera);
+				inet_address = g_inet_socket_address_get_address
+					(G_INET_SOCKET_ADDRESS (stream_address));
+				inet_address_string = g_inet_address_to_string (inet_address);
+				arv_debug ("camera", "[FakeGvCamera::stream_thread] Start stream to %s (%d)",
+					   inet_address_string,
+					   g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (stream_address)));
+				g_free (inet_address_string);
+
+				payload = arv_fake_camera_get_payload (gv_camera->camera);
+				image_buffer = arv_buffer_new (payload, NULL);
+			}
 
-		block_id = 0;
+			arv_fake_camera_wait_for_next_frame (gv_camera->camera);
+			arv_fake_camera_fill_buffer (gv_camera->camera, image_buffer);
+
+			block_id = 0;
+
+			packet_size = ARV_FAKE_GV_CAMERA_BUFFER_SIZE;
+			arv_gvsp_packet_new_data_leader (image_buffer->frame_id,
+							 block_id,
+							 image_buffer->timestamp_ns,
+							 image_buffer->pixel_format,
+							 image_buffer->width, image_buffer->height,
+							 image_buffer->x_offset, image_buffer->y_offset,
+							 packet_buffer, &packet_size);
+
+			g_socket_send_to (gv_camera->gvsp_socket, stream_address,
+					  packet_buffer, packet_size, NULL, &error);
+			if (error != NULL) {
+				arv_debug ("camera", "[ArvFakeGvCamera::stream_thread] Socket send error [%s]",
+					   error->message);
+				g_error_free (error);
+				error = NULL;
+			}
 
-		packet_size = 65536;
-		arv_gvsp_packet_new_data_leader (frame_id, block_id, image_buffer->timestamp_ns,
-						 image_buffer->pixel_format,
-						 image_buffer->width, image_buffer->height,
-						 image_buffer->x_offset, image_buffer->y_offset,
-						 packet_buffer, &packet_size);
+			block_id++;
+
+			offset = 0;
+			while (offset < payload) {
+				size_t data_size;
 
-		g_socket_send_to (gv_camera->gvsp_socket, socket_address, packet_buffer, packet_size, NULL, NULL);
+				data_size = MIN (1500, payload - offset);
 
-		block_id++;
+				packet_size = ARV_FAKE_GV_CAMERA_BUFFER_SIZE;
+				arv_gvsp_packet_new_data_block (image_buffer->frame_id, block_id,
+								data_size, image_buffer->data + offset,
+								packet_buffer, &packet_size);
 
-		offset = 0;
-		while (offset < payload) {
-			size_t data_size;
+				g_socket_send_to (gv_camera->gvsp_socket, stream_address,
+						  packet_buffer, packet_size, NULL, NULL);
 
-			data_size = MIN (1500, payload - offset);
+				offset += data_size;
+				block_id++;
+			}
 
-			packet_size = 65536;
-			arv_gvsp_packet_new_data_block (frame_id, block_id,
-							data_size, image_buffer->data + offset,
-							packet_buffer, &packet_size);
+			packet_size = ARV_FAKE_GV_CAMERA_BUFFER_SIZE;
+			arv_gvsp_packet_new_data_trailer (image_buffer->frame_id, block_id,
+							  packet_buffer, &packet_size);
 
-			g_socket_send_to (gv_camera->gvsp_socket, socket_address,
+			g_socket_send_to (gv_camera->gvsp_socket, stream_address,
 					  packet_buffer, packet_size, NULL, NULL);
 
-			offset += data_size;
-			block_id++;
+			frame_id++;
 		}
-
-		packet_size = 65536;
-		arv_gvsp_packet_new_data_trailer (frame_id, block_id,
-						  packet_buffer, &packet_size);
-
-		g_socket_send_to (gv_camera->gvsp_socket, socket_address, packet_buffer, packet_size, NULL, NULL);
-
-		frame_id++;
-
 	} while (!cancel);
 
-	g_object_unref (socket_address);
+	if (stream_address != NULL)
+		g_object_unref (stream_address);
+	if (image_buffer != NULL)
+		g_object_unref (image_buffer);
+
 	g_free (packet_buffer);
-	g_object_unref (image_buffer);
 
 	return NULL;
 }
diff --git a/src/arvgvsp.c b/src/arvgvsp.c
index 46f6fad..f9cf186 100644
--- a/src/arvgvsp.c
+++ b/src/arvgvsp.c
@@ -31,7 +31,8 @@
 #include <string.h>
 
 static ArvGvspPacket *
-arv_gvsp_packet_new (guint32 frame_id, guint16 block_id, size_t data_size, void *buffer, size_t *buffer_size)
+arv_gvsp_packet_new (ArvGvspPacketType packet_type,
+		     guint32 frame_id, guint16 block_id, size_t data_size, void *buffer, size_t *buffer_size)
 {
 	ArvGvspPacket *packet;
 	size_t packet_size;
@@ -48,9 +49,9 @@ arv_gvsp_packet_new (guint32 frame_id, guint16 block_id, size_t data_size, void
 	else
 		packet = g_malloc (packet_size);
 
-	packet->header.frame_id = frame_id;
-	packet->header.packet_type = ARV_GVSP_PACKET_TYPE_DATA_LEADER;
-	packet->header.block_id = block_id;
+	packet->header.frame_id = g_htonl (frame_id);
+	packet->header.packet_type = g_htons (packet_type);
+	packet->header.block_id = g_htons (block_id);
 
 	return packet;
 }
@@ -64,20 +65,21 @@ arv_gvsp_packet_new_data_leader	(guint32 frame_id, guint16 block_id,
 {
 	ArvGvspPacket *packet;
 
-	packet = arv_gvsp_packet_new (frame_id, block_id, sizeof (ArvGvspDataLeader), buffer, buffer_size);
+	packet = arv_gvsp_packet_new (ARV_GVSP_PACKET_TYPE_DATA_LEADER,
+				      frame_id, block_id, sizeof (ArvGvspDataLeader), buffer, buffer_size);
 
 	if (packet != NULL) {
 		ArvGvspDataLeader *leader;
 
 		leader = (ArvGvspDataLeader *) &packet->data;
 		leader->data0 = 0;
-		leader->timestamp_high = (guint64) (timestamp / 1000000000LL);
-		leader->timestamp_low  = (guint64) (timestamp % 1000000000LL);
-		leader->pixel_format = pixel_format;
-		leader->width = width;
-		leader->height = height;
-		leader->x_offset = x_offset;
-		leader->y_offset = y_offset;
+		leader->timestamp_high = g_htonl ((guint64) (timestamp / 1000000000LL));
+		leader->timestamp_low  = g_htonl ((guint64) (timestamp % 1000000000LL));
+		leader->pixel_format = g_htonl (pixel_format);
+		leader->width = g_htonl (width);
+		leader->height = g_htonl (height);
+		leader->x_offset = g_htonl (x_offset);
+		leader->y_offset = g_htonl (y_offset);
 	}
 
 	return packet;
@@ -89,7 +91,8 @@ arv_gvsp_packet_new_data_trailer (guint32 frame_id, guint16 block_id,
 {
 	ArvGvspPacket *packet;
 
-	packet = arv_gvsp_packet_new (frame_id, block_id, sizeof (ArvGvspDataTrailer), buffer, buffer_size);
+	packet = arv_gvsp_packet_new (ARV_GVSP_PACKET_TYPE_DATA_TRAILER,
+				      frame_id, block_id, sizeof (ArvGvspDataTrailer), buffer, buffer_size);
 
 	if (packet != NULL) {
 		ArvGvspDataTrailer *trailer;
@@ -109,7 +112,8 @@ arv_gvsp_packet_new_data_block (guint32 frame_id, guint16 block_id,
 {
 	ArvGvspPacket *packet;
 
-	packet = arv_gvsp_packet_new (frame_id, block_id, size, buffer, buffer_size);
+	packet = arv_gvsp_packet_new (ARV_GVSP_PACKET_TYPE_DATA_BLOCK,
+				      frame_id, block_id, size, buffer, buffer_size);
 
 	if (packet != NULL)
 		memcpy (&packet->data, data, size);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]