Example #1
0
bool CvCaptureCAM_Aravis::grabFrame()
{
    // remove content of previous frame
    framebuffer = NULL;

    if(stream) {
        ArvBuffer *arv_buffer = NULL;
        int max_tries = 10;
        int tries = 0;
        for(; tries < max_tries; tries ++) {
            arv_buffer = arv_stream_timeout_pop_buffer (stream, 200000);
            if (arv_buffer != NULL && arv_buffer_get_status (arv_buffer) != ARV_BUFFER_STATUS_SUCCESS) {
                arv_stream_push_buffer (stream, arv_buffer);
            } else break;
        }
        if(arv_buffer != NULL && tries < max_tries) {
            size_t buffer_size;
            framebuffer = (void*)arv_buffer_get_data (arv_buffer, &buffer_size);

            // retieve image size properites
            arv_buffer_get_image_region (arv_buffer, &xoffset, &yoffset, &width, &height);

            // retieve image ID set by camera
            frameID = arv_buffer_get_frame_id(arv_buffer);

            arv_stream_push_buffer(stream, arv_buffer);
            return true;
        }
    }
    return false;
}
Example #2
0
static GstFlowReturn
gst_aravis_create (GstPushSrc * push_src, GstBuffer ** buffer)
{
    GstAravis *gst_aravis;
    ArvBuffer *arv_buffer;

    gst_aravis = GST_ARAVIS (push_src);

    do {
        arv_buffer = arv_stream_timeout_pop_buffer (gst_aravis->stream, gst_aravis->buffer_timeout_us);
        if (arv_buffer != NULL && arv_buffer->status != ARV_BUFFER_STATUS_SUCCESS)
            arv_stream_push_buffer (gst_aravis->stream, arv_buffer);
    } while (arv_buffer != NULL && arv_buffer->status != ARV_BUFFER_STATUS_SUCCESS);

    if (arv_buffer == NULL)
        return GST_FLOW_ERROR;

    *buffer = gst_buffer_new ();

    GST_BUFFER_DATA (*buffer) = arv_buffer->data;
    GST_BUFFER_MALLOCDATA (*buffer) = NULL;
    GST_BUFFER_SIZE (*buffer) = gst_aravis->payload;

    if (gst_aravis->timestamp_offset == 0) {
        gst_aravis->timestamp_offset = arv_buffer->timestamp_ns;
        gst_aravis->last_timestamp = arv_buffer->timestamp_ns;
    }

    GST_BUFFER_TIMESTAMP (*buffer) = arv_buffer->timestamp_ns - gst_aravis->timestamp_offset;
    GST_BUFFER_DURATION (*buffer) = arv_buffer->timestamp_ns - gst_aravis->last_timestamp;

    gst_aravis->last_timestamp = arv_buffer->timestamp_ns;

    arv_stream_push_buffer (gst_aravis->stream, arv_buffer);

    gst_buffer_set_caps (*buffer, gst_aravis->fixed_caps);

    return GST_FLOW_OK;
}
Example #3
0
bool CvCaptureCAM_Aravis::grabFrame()
{
    ArvBuffer *arv_buffer = NULL;
    int max_tries = 10;
    int tries = 0;
    for(; tries < max_tries; tries ++) {
        arv_buffer = arv_stream_timeout_pop_buffer (stream, 200000);
        if (arv_buffer != NULL && arv_buffer_get_status (arv_buffer) != ARV_BUFFER_STATUS_SUCCESS) {
            arv_stream_push_buffer (stream, arv_buffer);
        } else break;
    }

    if (tries == max_tries)
        return false;

    size_t buffer_size;
    framebuffer = (void*)arv_buffer_get_data (arv_buffer, &buffer_size);

    arv_buffer_get_image_region (arv_buffer, NULL, NULL, &width, &height);

    arv_stream_push_buffer(stream, arv_buffer);
    return true;
}
Example #4
0
int main(int argc, char *argv[])
{
    ArvDevice *device;
    ArvStream *stream;
    ArvCamera *camera;
    ArvGcFeatureNode *feature;
    guint64 n_completed_buffers;
    guint64 n_failures;
    guint64 n_underruns;
    GOptionContext *context;
    GError *error = NULL;
    void (*old_sigint_handler)(int);
    int i, payload;

    arv_g_thread_init (NULL);
    arv_g_type_init ();

    context = g_option_context_new (NULL);
    g_option_context_set_summary (context, "Test of heartbeat robustness while continuously changing a feature.");
    g_option_context_add_main_entries (context, arv_option_entries, NULL);

    if (!g_option_context_parse (context, &argc, &argv, &error)) {
	    g_option_context_free (context);
	    g_print ("Option parsing failed: %s\n", error->message);
	    g_error_free (error);
	    return EXIT_FAILURE;
    }

    g_option_context_free (context);

    arv_debug_enable (arv_option_debug_domains);

    camera = arv_camera_new (arv_option_camera_name);
    if (!ARV_IS_CAMERA (camera)) {
	    printf ("Device not found\n");
	    return EXIT_FAILURE;
    }

    device = arv_camera_get_device (camera);

    stream = arv_camera_create_stream (camera, NULL, NULL);
    if (!ARV_IS_STREAM (stream)) {
	    printf ("Invalid device\n");
    } else {
	    payload = arv_camera_get_payload (camera);

	    if (ARV_IS_GV_STREAM (stream)) {
		    g_object_set (stream,
				  //"socket-buffer", ARV_GV_STREAM_SOCKET_BUFFER_AUTO,
				  "socket-buffer", ARV_GV_STREAM_SOCKET_BUFFER_FIXED,
				  "socket-buffer-size", payload*6,
				  "packet-timeout", 1000 * 1000,
				  "frame-retention", 100 * 1000,
				  "packet-resend", ARV_GV_STREAM_PACKET_RESEND_ALWAYS,
				  NULL);
	    }

	    for (i = 0; i < 100; i++)
		    arv_stream_push_buffer(stream, arv_buffer_new(payload, NULL));

	    arv_camera_set_acquisition_mode(camera, ARV_ACQUISITION_MODE_CONTINUOUS);

	    feature = ARV_GC_FEATURE_NODE (arv_device_get_feature (device, arv_option_feature_name));

	    arv_camera_start_acquisition (camera);

	    old_sigint_handler = signal (SIGINT, set_cancel);

	    while (!cancel) {
		    ArvBuffer *buffer = arv_stream_timeout_pop_buffer(stream, 2000000);
		    if (buffer) {
			    usleep(10);
			    arv_stream_push_buffer (stream, buffer);
		    }

		    if (!(++i%5)) {
			    char *value;

			    if ((i/100) % 2 == 0)
				    value = g_strdup_printf ("%d", arv_option_min);
			    else
				    value = g_strdup_printf ("%d", arv_option_max);

			    fprintf (stderr, "Setting %s from %s to %s\n",
				     arv_option_feature_name,
				     arv_gc_feature_node_get_value_as_string (feature, NULL),
				     value);
			    arv_gc_feature_node_set_value_from_string (feature, value, NULL);

			    g_free (value);
		    }
	    }

	    signal (SIGINT, old_sigint_handler);

	    arv_stream_get_statistics (stream, &n_completed_buffers, &n_failures, &n_underruns);

	    printf ("\nCompleted buffers = %Lu\n", (unsigned long long) n_completed_buffers);
	    printf ("Failures          = %Lu\n", (unsigned long long) n_failures);
	    printf ("Underruns         = %Lu\n", (unsigned long long) n_underruns);

	    arv_camera_stop_acquisition (camera);
    }

    g_object_unref (camera);

    return 0;
}
Example #5
0
static GstFlowReturn
gst_aravis_create (GstPushSrc * push_src, GstBuffer ** buffer)
{
	GstAravis *gst_aravis;
	ArvBuffer *arv_buffer;
	int arv_row_stride;
	int width, height;
	char *buffer_data;
	size_t buffer_size;
	guint64 timestamp_ns;

	gst_aravis = GST_ARAVIS (push_src);

	do {
		arv_buffer = arv_stream_timeout_pop_buffer (gst_aravis->stream, gst_aravis->buffer_timeout_us);
		if (arv_buffer != NULL && arv_buffer_get_status (arv_buffer) != ARV_BUFFER_STATUS_SUCCESS)
			arv_stream_push_buffer (gst_aravis->stream, arv_buffer);
	} while (arv_buffer != NULL && arv_buffer_get_status (arv_buffer) != ARV_BUFFER_STATUS_SUCCESS);

	if (arv_buffer == NULL)
		return GST_FLOW_ERROR;

	*buffer = gst_buffer_new ();

	buffer_data = (char *) arv_buffer_get_data (arv_buffer, &buffer_size);
	arv_buffer_get_image_region (arv_buffer, NULL, NULL, &width, &height);
	arv_row_stride = width * ARV_PIXEL_FORMAT_BIT_PER_PIXEL (arv_buffer_get_image_pixel_format (arv_buffer)) / 8;
	timestamp_ns = arv_buffer_get_timestamp (arv_buffer);

	/* Gstreamer requires row stride to be a multiple of 4 */
	if ((arv_row_stride & 0x3) != 0) {
		int gst_row_stride;
		size_t size;
		void *data;
		int i;

		gst_row_stride = (arv_row_stride & ~(0x3)) + 4;

		size = height * gst_row_stride;
		data = g_malloc (size);

		for (i = 0; i < height; i++)
			memcpy (((char *) data) + i * gst_row_stride, buffer_data + i * arv_row_stride, arv_row_stride);

		GST_BUFFER_DATA (buffer) = data;
		GST_BUFFER_MALLOCDATA (buffer) = data;
		GST_BUFFER_SIZE (buffer) = size;
	} else {
		GST_BUFFER_DATA (*buffer) = buffer_data;
		GST_BUFFER_MALLOCDATA (*buffer) = NULL;
		GST_BUFFER_SIZE (*buffer) = buffer_size;
	}

	if (!gst_base_src_get_do_timestamp(GST_BASE_SRC(push_src))) {
		if (gst_aravis->timestamp_offset == 0) {
			gst_aravis->timestamp_offset = timestamp_ns;
			gst_aravis->last_timestamp = timestamp_ns;
		}

		GST_BUFFER_TIMESTAMP (*buffer) = timestamp_ns - gst_aravis->timestamp_offset;
		GST_BUFFER_DURATION (*buffer) = timestamp_ns - gst_aravis->last_timestamp;

		gst_aravis->last_timestamp = timestamp_ns;
	}

	arv_stream_push_buffer (gst_aravis->stream, arv_buffer);

	gst_buffer_set_caps (*buffer, gst_aravis->fixed_caps);

	return GST_FLOW_OK;
}
Example #6
0
static GstFlowReturn
gst_aravis_create (GstPushSrc * push_src, GstBuffer ** buffer)
{
	GstAravis *gst_aravis;
	ArvBuffer *arv_buffer;
	int arv_row_stride;

	gst_aravis = GST_ARAVIS (push_src);

	do {
		arv_buffer = arv_stream_timeout_pop_buffer (gst_aravis->stream, gst_aravis->buffer_timeout_us);
		if (arv_buffer != NULL && arv_buffer->status != ARV_BUFFER_STATUS_SUCCESS)
			arv_stream_push_buffer (gst_aravis->stream, arv_buffer);
	} while (arv_buffer != NULL && arv_buffer->status != ARV_BUFFER_STATUS_SUCCESS);

	if (arv_buffer == NULL)
		return GST_FLOW_ERROR;

	arv_row_stride = arv_buffer->width * ARV_PIXEL_FORMAT_BIT_PER_PIXEL (arv_buffer->pixel_format) / 8;

	/* Gstreamer requires row stride to be a multiple of 4 */
	if ((arv_row_stride & 0x3) != 0) {
		int gst_row_stride;
		size_t size;
		void *data;
		int i;

		gst_row_stride = (arv_row_stride & ~(0x3)) + 4;

		size = arv_buffer->height * gst_row_stride;
		data = g_malloc (size);

		for (i = 0; i < arv_buffer->height; i++)
			memcpy (((char *) data) + i * gst_row_stride, ((char *) arv_buffer->data) + i * arv_row_stride, arv_row_stride);

		*buffer = gst_buffer_new_wrapped (data, size);
	} else {
		*buffer = gst_buffer_new_wrapped_full (0,
			arv_buffer->data,
			arv_buffer->size,
			0,
			arv_buffer->size,
			NULL,
			NULL);
	}

	if (!gst_base_src_get_do_timestamp(GST_BASE_SRC(push_src))) {
		if (gst_aravis->timestamp_offset == 0) {
			gst_aravis->timestamp_offset = arv_buffer->timestamp_ns;
			gst_aravis->last_timestamp = arv_buffer->timestamp_ns;
		}

		GST_BUFFER_PTS (*buffer) = arv_buffer->timestamp_ns - gst_aravis->timestamp_offset;
		GST_BUFFER_DURATION (*buffer) = arv_buffer->timestamp_ns - gst_aravis->last_timestamp;

		gst_aravis->last_timestamp = arv_buffer->timestamp_ns;
	}

	arv_stream_push_buffer (gst_aravis->stream, arv_buffer);

	return GST_FLOW_OK;
}
int main (int argc, char **argv)
{
	
	ArvCamera * camera;
	ArvStream *stream;
	ArvBuffer *buffer;
	GOptionContext *context;
	GError *error = NULL;
	char memory_buffer[100000];
	int i;

	arv_g_thread_init (NULL);
	arv_g_type_init ();

	context = g_option_context_new (NULL);
	g_option_context_add_main_entries (context, arv_option_entries, NULL);

	if (!g_option_context_parse (context, &argc, &argv, &error)) {
		g_option_context_free (context);
		g_print ("Option parsing failed: %s\n", error->message);
		g_error_free (error);
		return EXIT_FAILURE;
	}

	g_option_context_free (context);
	if (arv_option_max_frames < 0)
		arv_option_max_errors_before_abort = -1;
	
	save_buffer_fn = GetSaveBufferFn(arv_option_save_type);

	arv_debug_enable (arv_option_debug_domains);

	if (arv_option_camera_name == NULL)
		g_print ("Looking for the first available camera\n");
	else
		g_print ("Looking for camera '%s'\n", arv_option_camera_name);

	camera = arv_camera_new (arv_option_camera_name);
	
	int errors = 0;
	if (camera == NULL)
	{
		g_print("No device found");
		return 1;
	}

	guint payload_size = arv_camera_get_payload(camera);
	g_print ("payload size  = %d (0x%x)\n", payload_size, payload_size);
	

	stream = arv_camera_create_stream (camera, NULL, NULL);
	if (arv_option_auto_buffer)
	{
		g_object_set (stream,"socket-buffer", ARV_GV_STREAM_SOCKET_BUFFER_AUTO,"socket-buffer-size", 0,NULL);
	}
	
	for (i = 0; i < 30; i++)
	{
		arv_stream_push_buffer (stream, arv_buffer_new (payload_size, NULL));
	}
	
	arv_camera_stop_acquisition(camera);

	// set the bit depth
	ArvDevice * device = arv_camera_get_device(camera);
	ArvGcNode * feature = arv_device_get_feature(device, "PixelFormat");
	char * pix_format = "Mono8";
	if (arv_option_pixel_format == 14)
		pix_format = "Mono14";
	arv_gc_feature_node_set_value_from_string(ARV_GC_FEATURE_NODE(feature), pix_format, NULL);
	if (arv_option_pixel_format == 14)
	{
		feature = arv_device_get_feature(device, "CMOSBitDepth");
		arv_gc_feature_node_set_value_from_string(ARV_GC_FEATURE_NODE(feature), "bit14bit", NULL);
	}


	signal (SIGINT, set_cancel);
	signal (SIGQUIT, set_cancel);

	int captured_frames = 0;
	guint64 timeout=1000000;
	#define _CAN_STOP (arv_option_max_frames > 0 && captured_frames >= arv_option_max_frames)
	arv_camera_start_acquisition(camera);
	do {
		g_usleep (100000);
			do  {
			buffer = arv_stream_timeout_pop_buffer (stream, timeout);
			if (buffer == NULL) break;
			ArvBufferStatus status = arv_buffer_get_status(buffer);
			fprintf(stderr, "Status is %d\n", status);
			if (status == ARV_BUFFER_STATUS_SUCCESS)
			{
		
				
				
				if (timeout > 100000) timeout -= 1000;
				errors = 0;
				if (save_buffer_fn != NULL)
				{
					struct timespec timestamp;
					clock_gettime(CLOCK_REALTIME, &timestamp);
					char filename[BUFSIZ];
					if (strcmp(arv_option_save_prefix,"") != 0)
					{
						sprintf(filename, "%s/%s%d.%s", arv_option_save_dir,arv_option_save_prefix, captured_frames, arv_option_save_type);
					}
					else
					{
						sprintf(filename, "%s/%d.%03ld.%s", arv_option_save_dir, (int)timestamp.tv_sec, (long)(timestamp.tv_nsec/1.0e6), arv_option_save_type);
					}
					if ((*save_buffer_fn)(buffer, filename) == false)
					{
						g_print("Couldn't save frame %d to %s\n", captured_frames, filename);
						set_cancel(SIGQUIT);
					}
					g_print("Saved frame %d to %s\n", captured_frames, filename);
					char latest[] = "latest.png";
					sprintf(latest, "latest.%s", arv_option_save_type);
					unlink(latest);
					symlink(filename, latest);
				}
				captured_frames++;
				g_usleep(arv_option_sample_period);
			}
			else 
			{
				if (timeout < 10000000) timeout+=1000;
				fprintf(stderr, "%d errors out of %d allowed\n", errors, arv_option_max_errors_before_abort);
				arv_camera_stop_acquisition(camera);
				if (++errors > arv_option_max_errors_before_abort && arv_option_max_errors_before_abort >= 0)
				{
					set_cancel(SIGQUIT);
				}
				else
				{
					arv_camera_start_acquisition(camera);
				}
			}
			arv_stream_push_buffer (stream, buffer);
			
			
			
		} while (!cancel && buffer != NULL && !_CAN_STOP);
	} while (!cancel && !_CAN_STOP);
	arv_camera_stop_acquisition(camera);


	guint64 n_processed_buffers; guint64 n_failures; guint64 n_underruns;
	arv_stream_get_statistics (stream, &n_processed_buffers, &n_failures, &n_underruns);
	g_print ("Processed buffers = %Lu\n", (unsigned long long) n_processed_buffers);
	g_print ("Failures          = %Lu\n", (unsigned long long) n_failures);
	g_print ("Underruns         = %Lu\n", (unsigned long long) n_underruns);


	g_object_unref (stream);
	g_object_unref (camera);
	
	
	return (errors > 0);
}
Example #8
0
    bool CameraGigeAravis::grabImage(Frame &newFrame){

        ArvBuffer *arv_buffer;
        //exp = arv_camera_get_exposure_time(camera);

        arv_buffer = arv_stream_timeout_pop_buffer(stream,2000000); //us
        char *buffer_data;
        size_t buffer_size;

        if(arv_buffer == NULL){

            throw runtime_error("arv_buffer is NULL");
            return false;

        }else{

            try{

                if(arv_buffer_get_status(arv_buffer) == ARV_BUFFER_STATUS_SUCCESS){

                    //BOOST_LOG_SEV(logger, normal) << "Success to grab a frame.";

                    buffer_data = (char *) arv_buffer_get_data (arv_buffer, &buffer_size);

                    //Timestamping.
                    //string acquisitionDate = TimeDate::localDateTime(microsec_clock::universal_time(),"%Y:%m:%d:%H:%M:%S");
                    //BOOST_LOG_SEV(logger, normal) << "Date : " << acquisitionDate;
                    boost::posix_time::ptime time = boost::posix_time::microsec_clock::universal_time();
                    string acquisitionDate = to_iso_extended_string(time);
                    //BOOST_LOG_SEV(logger, normal) << "Date : " << acqDateInMicrosec;

                    Mat image;
                    CamPixFmt imgDepth = MONO8;
                    int saturateVal = 0;

                    if(pixFormat == ARV_PIXEL_FORMAT_MONO_8){

                        //BOOST_LOG_SEV(logger, normal) << "Creating Mat 8 bits ...";
                        image = Mat(mHeight, mWidth, CV_8UC1, buffer_data);
                        imgDepth = MONO8;
                        saturateVal = 255;

                    }else if(pixFormat == ARV_PIXEL_FORMAT_MONO_12){

                        //BOOST_LOG_SEV(logger, normal) << "Creating Mat 16 bits ...";
                        image = Mat(mHeight, mWidth, CV_16UC1, buffer_data);
                        imgDepth = MONO12;
                        saturateVal = 4095;

                        //double t3 = (double)getTickCount();

                        if(shiftBitsImage){

                            //BOOST_LOG_SEV(logger, normal) << "Shifting bits ...";


                                unsigned short * p;

                                for(int i = 0; i < image.rows; i++){
                                    p = image.ptr<unsigned short>(i);
                                    for(int j = 0; j < image.cols; j++)
                                        p[j] = p[j] >> 4;
                                }

                            //BOOST_LOG_SEV(logger, normal) << "Bits shifted.";

                        }

                        //t3 = (((double)getTickCount() - t3)/getTickFrequency())*1000;
                        //cout << "> Time shift : " << t3 << endl;
                    }

                    //BOOST_LOG_SEV(logger, normal) << "Creating frame object ...";
                    newFrame = Frame(image, gain, exp, acquisitionDate);
                    //BOOST_LOG_SEV(logger, normal) << "Setting date of frame ...";
                    //newFrame.setAcqDateMicro(acqDateInMicrosec);
                    //BOOST_LOG_SEV(logger, normal) << "Setting fps of frame ...";
                    newFrame.mFps = fps;
                    newFrame.mFormat = imgDepth;
                    //BOOST_LOG_SEV(logger, normal) << "Setting saturated value of frame ...";
                    newFrame.mSaturatedValue = saturateVal;
                    newFrame.mFrameNumber = frameCounter;
                    frameCounter++;

                    //BOOST_LOG_SEV(logger, normal) << "Re-pushing arv buffer in stream ...";
                    arv_stream_push_buffer(stream, arv_buffer);

                    return true;

                }else{

                    switch(arv_buffer_get_status(arv_buffer)){