static void * arv_fake_stream_thread (void *data) { ArvFakeStreamThreadData *thread_data = data; ArvBuffer *buffer; arv_log_stream_thread ("[FakeStream::thread] Start"); if (thread_data->callback != NULL) thread_data->callback (thread_data->user_data, ARV_STREAM_CALLBACK_TYPE_INIT, NULL); while (!thread_data->cancel) { arv_fake_camera_wait_for_next_frame (thread_data->camera); buffer = arv_stream_pop_input_buffer (thread_data->stream); if (buffer != NULL) { arv_fake_camera_fill_buffer (thread_data->camera, buffer, NULL); if (buffer->status == ARV_BUFFER_STATUS_SUCCESS) thread_data->n_completed_buffers++; else thread_data->n_failures++; if (thread_data->callback != NULL) thread_data->callback (thread_data->user_data, ARV_STREAM_CALLBACK_TYPE_BUFFER_DONE, buffer); arv_stream_push_output_buffer (thread_data->stream, buffer); } else thread_data->n_underruns++; } if (thread_data->callback != NULL) thread_data->callback (thread_data->user_data, ARV_STREAM_CALLBACK_TYPE_EXIT, NULL); arv_log_stream_thread ("[FakeStream::thread] Stop"); return NULL; }
static void _close_frame (ArvGvStreamThreadData *thread_data, ArvGvStreamFrameData *frame) { GTimeVal current_time; gint64 current_time_us; if (frame->buffer->status == ARV_BUFFER_STATUS_SUCCESS) thread_data->n_completed_buffers++; else if (frame->buffer->status != ARV_BUFFER_STATUS_ABORTED) thread_data->n_failures++; if (frame->buffer->status == ARV_BUFFER_STATUS_TIMEOUT) thread_data->n_timeouts++; if (frame->buffer->status == ARV_BUFFER_STATUS_ABORTED) thread_data->n_aborteds++; if (frame->buffer->status != ARV_BUFFER_STATUS_SUCCESS && frame->buffer->status != ARV_BUFFER_STATUS_ABORTED) thread_data->n_missing_packets += (int) frame->n_packets - (frame->last_valid_packet + 1); if (thread_data->callback != NULL) thread_data->callback (thread_data->user_data, ARV_STREAM_CALLBACK_TYPE_BUFFER_DONE, frame->buffer); g_get_current_time (¤t_time); current_time_us = current_time.tv_sec * 1000000 + current_time.tv_usec; if (thread_data->statistic_count > 5) { arv_statistic_fill (thread_data->statistic, 0, current_time_us - frame->first_packet_time_us, frame->buffer->frame_id); } else thread_data->statistic_count++; arv_stream_push_output_buffer (thread_data->stream, frame->buffer); arv_log_stream_thread ("[GvStream::_close_frame] Close frame %u", frame->frame_id); frame->buffer = NULL; frame->frame_id = 0; g_free (frame->packet_data); g_free (frame); }
static void * arv_uv_stream_thread (void *data) { ArvUvStreamThreadData *thread_data = data; ArvUvspPacket *packet; ArvBuffer *buffer = NULL; void *incoming_buffer; guint64 offset; size_t transferred; arv_log_stream_thread ("Start USB3Vision stream thread"); incoming_buffer = g_malloc (MAXIMUM_TRANSFER_SIZE); if (thread_data->callback != NULL) thread_data->callback (thread_data->user_data, ARV_STREAM_CALLBACK_TYPE_INIT, NULL); offset = 0; while (!thread_data->cancel) { size_t size; transferred = 0; if (buffer == NULL) size = thread_data->leader_size; else { if (offset < buffer->priv->size) size = MIN (thread_data->payload_size, buffer->priv->size - offset); else size = thread_data->trailer_size; } /* Avoid unnecessary memory copy by transferring data directly to the image buffer */ if (buffer != NULL && buffer->priv->status == ARV_BUFFER_STATUS_FILLING && offset + size <= buffer->priv->size) packet = buffer->priv->data + offset; else packet = incoming_buffer; arv_uv_device_bulk_transfer (thread_data->uv_device, 0x81 | LIBUSB_ENDPOINT_IN, packet, size, &transferred, 0, NULL); if (transferred > 0) { ArvUvspPacketType packet_type; arv_log_sp ("Received %d bytes", transferred); arv_uvsp_packet_debug (packet, ARV_DEBUG_LEVEL_LOG); packet_type = arv_uvsp_packet_get_packet_type (packet); switch (packet_type) { case ARV_UVSP_PACKET_TYPE_LEADER: if (buffer != NULL) { arv_debug_stream_thread ("New leader received while a buffer is still open"); buffer->priv->status = ARV_BUFFER_STATUS_MISSING_PACKETS; arv_stream_push_output_buffer (thread_data->stream, buffer); thread_data->n_failures++; buffer = NULL; } buffer = arv_stream_pop_input_buffer (thread_data->stream); if (buffer != NULL) { buffer->priv->status = ARV_BUFFER_STATUS_FILLING; buffer->priv->gvsp_payload_type = ARV_GVSP_PAYLOAD_TYPE_IMAGE; arv_uvsp_packet_get_region (packet, &buffer->priv->width, &buffer->priv->height, &buffer->priv->x_offset, &buffer->priv->y_offset); buffer->priv->frame_id = arv_uvsp_packet_get_frame_id (packet); buffer->priv->timestamp_ns = arv_uvsp_packet_get_timestamp (packet); offset = 0; } else thread_data->n_underruns++; break; case ARV_UVSP_PACKET_TYPE_TRAILER: if (buffer != NULL) { arv_log_stream_thread ("Received %" G_GUINT64_FORMAT " bytes - expected %" G_GUINT64_FORMAT, offset, buffer->priv->size); buffer->priv->status = ARV_BUFFER_STATUS_SUCCESS; arv_stream_push_output_buffer (thread_data->stream, buffer); thread_data->n_completed_buffers++; buffer = NULL; } break; case ARV_UVSP_PACKET_TYPE_DATA: if (buffer != NULL && buffer->priv->status == ARV_BUFFER_STATUS_FILLING) { if (offset + transferred <= buffer->priv->size) { if (packet == incoming_buffer) memcpy (((char *) buffer->priv->data) + offset, packet, transferred); offset += transferred; } else buffer->priv->status = ARV_BUFFER_STATUS_SIZE_MISMATCH; } break; default: arv_debug_stream_thread ("Unkown packet type"); break; } } } if (thread_data->callback != NULL) thread_data->callback (thread_data->user_data, ARV_STREAM_CALLBACK_TYPE_EXIT, NULL); g_free (incoming_buffer); arv_log_stream_thread ("Stop USB3Vision stream thread"); return NULL; }