PageRenderTime 23ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/arvcameratest.c

https://gitlab.com/subin.plakkannickal/aravis
C | 420 lines | 351 code | 68 blank | 1 comment | 43 complexity | 73513f7baf32859361c37230484c8a4d MD5 | raw file
  1. #include <arv.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. static char *arv_option_camera_name = NULL;
  6. static char *arv_option_debug_domains = NULL;
  7. static gboolean arv_option_snaphot = FALSE;
  8. static char *arv_option_trigger = NULL;
  9. static double arv_option_software_trigger = -1;
  10. static double arv_option_frequency = -1.0;
  11. static int arv_option_width = -1;
  12. static int arv_option_height = -1;
  13. static int arv_option_horizontal_binning = -1;
  14. static int arv_option_vertical_binning = -1;
  15. static double arv_option_exposure_time_us = -1;
  16. static int arv_option_gain = -1;
  17. static gboolean arv_option_auto_socket_buffer = FALSE;
  18. static gboolean arv_option_no_packet_resend = FALSE;
  19. static unsigned int arv_option_packet_timeout = 20;
  20. static unsigned int arv_option_frame_retention = 100;
  21. static int arv_option_gv_stream_channel = -1;
  22. static int arv_option_gv_packet_delay = -1;
  23. static int arv_option_gv_packet_size = -1;
  24. static gboolean arv_option_realtime = FALSE;
  25. static gboolean arv_option_high_priority = FALSE;
  26. static gboolean arv_option_no_packet_socket = FALSE;
  27. static char *arv_option_chunks = NULL;
  28. static unsigned int arv_option_bandwidth_limit = -1;
  29. static const GOptionEntry arv_option_entries[] =
  30. {
  31. {
  32. "name", 'n', 0, G_OPTION_ARG_STRING,
  33. &arv_option_camera_name, "Camera name", NULL
  34. },
  35. {
  36. "snapshot", 's', 0, G_OPTION_ARG_NONE,
  37. &arv_option_snaphot, "Snapshot", NULL
  38. },
  39. {
  40. "frequency", 'f', 0, G_OPTION_ARG_DOUBLE,
  41. &arv_option_frequency, "Acquisition frequency", NULL
  42. },
  43. {
  44. "trigger", 't', 0, G_OPTION_ARG_STRING,
  45. &arv_option_trigger, "External trigger", NULL
  46. },
  47. {
  48. "software-trigger", 'o', 0, G_OPTION_ARG_DOUBLE,
  49. &arv_option_software_trigger, "Emit software trigger", NULL
  50. },
  51. {
  52. "width", 'w', 0, G_OPTION_ARG_INT,
  53. &arv_option_width, "Width", NULL
  54. },
  55. {
  56. "height", 'h', 0, G_OPTION_ARG_INT,
  57. &arv_option_height, "Height", NULL
  58. },
  59. {
  60. "h-binning", '\0', 0, G_OPTION_ARG_INT,
  61. &arv_option_horizontal_binning, "Horizontal binning", NULL
  62. },
  63. {
  64. "v-binning", '\0', 0, G_OPTION_ARG_INT,
  65. &arv_option_vertical_binning, "Vertical binning", NULL
  66. },
  67. {
  68. "exposure", 'e', 0, G_OPTION_ARG_DOUBLE,
  69. &arv_option_exposure_time_us, "Exposure time (µs)", NULL
  70. },
  71. {
  72. "gain", 'g', 0, G_OPTION_ARG_INT,
  73. &arv_option_gain, "Gain (dB)", NULL
  74. },
  75. {
  76. "auto", 'a', 0, G_OPTION_ARG_NONE,
  77. &arv_option_auto_socket_buffer, "Auto socket buffer size", NULL
  78. },
  79. {
  80. "no-packet-resend", 'r', 0, G_OPTION_ARG_NONE,
  81. &arv_option_no_packet_resend, "No packet resend", NULL
  82. },
  83. {
  84. "packet-timeout", 'p', 0, G_OPTION_ARG_INT,
  85. &arv_option_packet_timeout, "Packet timeout (ms)", NULL
  86. },
  87. {
  88. "frame-retention", 'm', 0, G_OPTION_ARG_INT,
  89. &arv_option_frame_retention, "Frame retention (ms)", NULL
  90. },
  91. {
  92. "gv-stream-channel", 'c', 0, G_OPTION_ARG_INT,
  93. &arv_option_gv_stream_channel, "GigEVision stream channel id", NULL
  94. },
  95. {
  96. "gv-packet-delay", 'y', 0, G_OPTION_ARG_INT,
  97. &arv_option_gv_packet_delay, "GigEVision packet delay (ns)", NULL
  98. },
  99. {
  100. "gv-packet-size", 'i', 0, G_OPTION_ARG_INT,
  101. &arv_option_gv_packet_size, "GigEVision packet size (bytes)", NULL
  102. },
  103. {
  104. "chunks", 'u', 0, G_OPTION_ARG_STRING,
  105. &arv_option_chunks, "Chunks", NULL
  106. },
  107. {
  108. "realtime", '\0', 0, G_OPTION_ARG_NONE,
  109. &arv_option_realtime, "Make stream thread realtime", NULL
  110. },
  111. {
  112. "high-priority", '\0', 0, G_OPTION_ARG_NONE,
  113. &arv_option_high_priority, "Make stream thread high priority", NULL
  114. },
  115. {
  116. "no-packet-socket", '\0', 0, G_OPTION_ARG_NONE,
  117. &arv_option_no_packet_socket, "Disable use of packet socket", NULL
  118. },
  119. {
  120. "debug", 'd', 0, G_OPTION_ARG_STRING,
  121. &arv_option_debug_domains, "Debug domains", NULL
  122. },
  123. {
  124. "bandwidth-limit", 'b', 0, G_OPTION_ARG_INT,
  125. &arv_option_bandwidth_limit, "Desired USB3 Vision device bandwidth limit", NULL
  126. },
  127. { NULL }
  128. };
  129. typedef struct {
  130. GMainLoop *main_loop;
  131. int buffer_count;
  132. ArvChunkParser *chunk_parser;
  133. char **chunks;
  134. } ApplicationData;
  135. static gboolean cancel = FALSE;
  136. static void
  137. set_cancel (int signal)
  138. {
  139. cancel = TRUE;
  140. }
  141. static void
  142. new_buffer_cb (ArvStream *stream, ApplicationData *data)
  143. {
  144. ArvBuffer *buffer;
  145. buffer = arv_stream_try_pop_buffer (stream);
  146. if (buffer != NULL) {
  147. if (arv_buffer_get_status (buffer) == ARV_BUFFER_STATUS_SUCCESS)
  148. data->buffer_count++;
  149. if (arv_buffer_get_payload_type (buffer) == ARV_BUFFER_PAYLOAD_TYPE_CHUNK_DATA &&
  150. data->chunks != NULL) {
  151. int i;
  152. for (i = 0; data->chunks[i] != NULL; i++)
  153. printf ("%s = %" G_GINT64_FORMAT "\n", data->chunks[i],
  154. arv_chunk_parser_get_integer_value (data->chunk_parser, buffer, data->chunks[i]));
  155. }
  156. /* Image processing here */
  157. arv_stream_push_buffer (stream, buffer);
  158. }
  159. }
  160. static void
  161. stream_cb (void *user_data, ArvStreamCallbackType type, ArvBuffer *buffer)
  162. {
  163. if (type == ARV_STREAM_CALLBACK_TYPE_INIT) {
  164. if (arv_option_realtime) {
  165. if (!arv_make_thread_realtime (10))
  166. printf ("Failed to make stream thread realtime\n");
  167. } else if (arv_option_high_priority) {
  168. if (!arv_make_thread_high_priority (-10))
  169. printf ("Failed to make stream thread high priority\n");
  170. }
  171. }
  172. }
  173. static gboolean
  174. periodic_task_cb (void *abstract_data)
  175. {
  176. ApplicationData *data = abstract_data;
  177. printf ("Frame rate = %d Hz\n", data->buffer_count);
  178. data->buffer_count = 0;
  179. if (cancel) {
  180. g_main_loop_quit (data->main_loop);
  181. return FALSE;
  182. }
  183. return TRUE;
  184. }
  185. static gboolean
  186. emit_software_trigger (void *abstract_data)
  187. {
  188. ArvCamera *camera = abstract_data;
  189. arv_camera_software_trigger (camera);
  190. return TRUE;
  191. }
  192. static void
  193. control_lost_cb (ArvGvDevice *gv_device)
  194. {
  195. printf ("Control lost\n");
  196. cancel = TRUE;
  197. }
  198. int
  199. main (int argc, char **argv)
  200. {
  201. ApplicationData data;
  202. ArvCamera *camera;
  203. ArvStream *stream;
  204. GOptionContext *context;
  205. GError *error = NULL;
  206. int i;
  207. data.buffer_count = 0;
  208. data.chunks = NULL;
  209. data.chunk_parser = NULL;
  210. arv_g_thread_init (NULL);
  211. arv_g_type_init ();
  212. context = g_option_context_new (NULL);
  213. g_option_context_add_main_entries (context, arv_option_entries, NULL);
  214. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  215. g_option_context_free (context);
  216. g_print ("Option parsing failed: %s\n", error->message);
  217. g_error_free (error);
  218. return EXIT_FAILURE;
  219. }
  220. g_option_context_free (context);
  221. arv_debug_enable (arv_option_debug_domains);
  222. if (arv_option_camera_name == NULL)
  223. g_print ("Looking for the first available camera\n");
  224. else
  225. g_print ("Looking for camera '%s'\n", arv_option_camera_name);
  226. camera = arv_camera_new (arv_option_camera_name);
  227. if (camera != NULL) {
  228. void (*old_sigint_handler)(int);
  229. gint payload;
  230. gint x, y, width, height;
  231. gint dx, dy;
  232. double exposure;
  233. guint64 n_completed_buffers;
  234. guint64 n_failures;
  235. guint64 n_underruns;
  236. int gain;
  237. guint software_trigger_source = 0;
  238. if (arv_option_chunks != NULL) {
  239. char *striped_chunks;
  240. striped_chunks = g_strdup (arv_option_chunks);
  241. arv_str_strip (striped_chunks, " ,:;", ',');
  242. data.chunks = g_strsplit_set (striped_chunks, ",", -1);
  243. g_free (striped_chunks);
  244. data.chunk_parser = arv_camera_create_chunk_parser (camera);
  245. for (i = 0; data.chunks[i] != NULL; i++) {
  246. char *chunk = g_strdup_printf ("Chunk%s", data.chunks[i]);
  247. g_free (data.chunks[i]);
  248. data.chunks[i] = chunk;
  249. }
  250. }
  251. arv_camera_set_chunks (camera, arv_option_chunks);
  252. arv_camera_set_region (camera, 0, 0, arv_option_width, arv_option_height);
  253. arv_camera_set_binning (camera, arv_option_horizontal_binning, arv_option_vertical_binning);
  254. arv_camera_set_exposure_time (camera, arv_option_exposure_time_us);
  255. arv_camera_set_gain (camera, arv_option_gain);
  256. if (arv_camera_is_uv_device(camera)) {
  257. arv_camera_uv_set_bandwidth (camera, arv_option_bandwidth_limit);
  258. }
  259. if (arv_camera_is_gv_device (camera)) {
  260. arv_camera_gv_select_stream_channel (camera, arv_option_gv_stream_channel);
  261. arv_camera_gv_set_packet_delay (camera, arv_option_gv_packet_delay);
  262. arv_camera_gv_set_packet_size (camera, arv_option_gv_packet_size);
  263. arv_camera_gv_set_stream_options (camera, arv_option_no_packet_socket ?
  264. ARV_GV_STREAM_OPTION_PACKET_SOCKET_DISABLED :
  265. ARV_GV_STREAM_OPTION_NONE);
  266. }
  267. arv_camera_get_region (camera, &x, &y, &width, &height);
  268. arv_camera_get_binning (camera, &dx, &dy);
  269. exposure = arv_camera_get_exposure_time (camera);
  270. payload = arv_camera_get_payload (camera);
  271. gain = arv_camera_get_gain (camera);
  272. printf ("vendor name = %s\n", arv_camera_get_vendor_name (camera));
  273. printf ("model name = %s\n", arv_camera_get_model_name (camera));
  274. printf ("device id = %s\n", arv_camera_get_device_id (camera));
  275. printf ("image width = %d\n", width);
  276. printf ("image height = %d\n", height);
  277. printf ("horizontal binning = %d\n", dx);
  278. printf ("vertical binning = %d\n", dy);
  279. printf ("payload = %d bytes\n", payload);
  280. printf ("exposure = %g µs\n", exposure);
  281. printf ("gain = %d dB\n", gain);
  282. if (arv_camera_is_gv_device (camera)) {
  283. printf ("gv n_stream channels = %d\n", arv_camera_gv_get_n_stream_channels (camera));
  284. printf ("gv current channel = %d\n", arv_camera_gv_get_current_stream_channel (camera));
  285. printf ("gv packet delay = %" G_GINT64_FORMAT " ns\n", arv_camera_gv_get_packet_delay (camera));
  286. printf ("gv packet size = %d bytes\n", arv_camera_gv_get_packet_size (camera));
  287. }
  288. if (arv_camera_is_uv_device (camera)) {
  289. guint min,max;
  290. arv_camera_uv_get_bandwidth_bounds (camera, &min, &max);
  291. printf ("uv bandwidth limit = %d [%d..%d]\n", arv_camera_uv_get_bandwidth (camera), min, max);
  292. }
  293. stream = arv_camera_create_stream (camera, stream_cb, NULL);
  294. if (stream != NULL) {
  295. if (ARV_IS_GV_STREAM (stream)) {
  296. if (arv_option_auto_socket_buffer)
  297. g_object_set (stream,
  298. "socket-buffer", ARV_GV_STREAM_SOCKET_BUFFER_AUTO,
  299. "socket-buffer-size", 0,
  300. NULL);
  301. if (arv_option_no_packet_resend)
  302. g_object_set (stream,
  303. "packet-resend", ARV_GV_STREAM_PACKET_RESEND_NEVER,
  304. NULL);
  305. g_object_set (stream,
  306. "packet-timeout", (unsigned) arv_option_packet_timeout * 1000,
  307. "frame-retention", (unsigned) arv_option_frame_retention * 1000,
  308. NULL);
  309. }
  310. for (i = 0; i < 50; i++)
  311. arv_stream_push_buffer (stream, arv_buffer_new (payload, NULL));
  312. arv_camera_set_acquisition_mode (camera, ARV_ACQUISITION_MODE_CONTINUOUS);
  313. if (arv_option_frequency > 0.0)
  314. arv_camera_set_frame_rate (camera, arv_option_frequency);
  315. if (arv_option_trigger != NULL)
  316. arv_camera_set_trigger (camera, arv_option_trigger);
  317. if (arv_option_software_trigger > 0.0) {
  318. arv_camera_set_trigger (camera, "Software");
  319. software_trigger_source = g_timeout_add ((double) (0.5 + 1000.0 /
  320. arv_option_software_trigger),
  321. emit_software_trigger, camera);
  322. }
  323. arv_camera_start_acquisition (camera);
  324. g_signal_connect (stream, "new-buffer", G_CALLBACK (new_buffer_cb), &data);
  325. arv_stream_set_emit_signals (stream, TRUE);
  326. g_signal_connect (arv_camera_get_device (camera), "control-lost",
  327. G_CALLBACK (control_lost_cb), NULL);
  328. g_timeout_add_seconds (1, periodic_task_cb, &data);
  329. data.main_loop = g_main_loop_new (NULL, FALSE);
  330. old_sigint_handler = signal (SIGINT, set_cancel);
  331. g_main_loop_run (data.main_loop);
  332. if (software_trigger_source > 0)
  333. g_source_remove (software_trigger_source);
  334. signal (SIGINT, old_sigint_handler);
  335. g_main_loop_unref (data.main_loop);
  336. arv_stream_get_statistics (stream, &n_completed_buffers, &n_failures, &n_underruns);
  337. printf ("Completed buffers = %Lu\n", (unsigned long long) n_completed_buffers);
  338. printf ("Failures = %Lu\n", (unsigned long long) n_failures);
  339. printf ("Underruns = %Lu\n", (unsigned long long) n_underruns);
  340. arv_camera_stop_acquisition (camera);
  341. arv_stream_set_emit_signals (stream, FALSE);
  342. g_object_unref (stream);
  343. } else
  344. printf ("Can't create stream thread (check if the device is not already used)\n");
  345. g_object_unref (camera);
  346. } else
  347. printf ("No camera found\n");
  348. if (data.chunks != NULL)
  349. g_strfreev (data.chunks);
  350. g_clear_object (&data.chunk_parser);
  351. return 0;
  352. }