camera_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "camera.h"
  2. #include "device.h"
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. double
  9. get_time()
  10. {
  11. struct timeval t;
  12. struct timezone tzp;
  13. gettimeofday(&t, &tzp);
  14. return t.tv_sec + t.tv_usec * 1e-6;
  15. }
  16. void
  17. on_capture(MPImage image, void *user_data)
  18. {
  19. size_t num_bytes =
  20. mp_pixel_format_width_to_bytes(image.pixel_format, image.width) *
  21. image.height;
  22. uint8_t *data = malloc(num_bytes);
  23. memcpy(data, image.data, num_bytes);
  24. printf(" first byte: %d.", data[0]);
  25. free(data);
  26. }
  27. int
  28. main(int argc, char *argv[])
  29. {
  30. if (argc != 2 && argc != 3) {
  31. printf("Usage: %s <media_device_name> [<sub_device_name>]\n",
  32. argv[0]);
  33. return 1;
  34. }
  35. char *video_name = argv[1];
  36. char *subdev_name = NULL;
  37. if (argc == 3) {
  38. subdev_name = argv[2];
  39. }
  40. double find_start = get_time();
  41. // First find the device
  42. MPDevice *device = mp_device_find(video_name);
  43. if (!device) {
  44. printf("Device not found\n");
  45. return 1;
  46. }
  47. double find_end = get_time();
  48. printf("Finding the device took %fms\n", (find_end - find_start) * 1000);
  49. int video_fd;
  50. uint32_t video_entity_id;
  51. {
  52. const struct media_v2_entity *entity =
  53. mp_device_find_entity(device, video_name);
  54. if (!entity) {
  55. printf("Unable to find video device interface\n");
  56. return 1;
  57. }
  58. video_entity_id = entity->id;
  59. const struct media_v2_interface *iface =
  60. mp_device_find_entity_interface(device, video_entity_id);
  61. char buf[256];
  62. if (!mp_find_device_path(iface->devnode, buf, 256)) {
  63. printf("Unable to find video device path\n");
  64. return 1;
  65. }
  66. video_fd = open(buf, O_RDWR);
  67. if (video_fd == -1) {
  68. printf("Unable to open video device\n");
  69. return 1;
  70. }
  71. }
  72. int subdev_fd = -1;
  73. if (subdev_name) {
  74. const struct media_v2_entity *entity =
  75. mp_device_find_entity(device, subdev_name);
  76. if (!entity) {
  77. printf("Unable to find sub-device\n");
  78. return 1;
  79. }
  80. const struct media_v2_pad *source_pad =
  81. mp_device_get_pad_from_entity(device, entity->id);
  82. const struct media_v2_pad *sink_pad =
  83. mp_device_get_pad_from_entity(device, video_entity_id);
  84. // Disable other links
  85. const struct media_v2_entity *entities =
  86. mp_device_get_entities(device);
  87. for (int i = 0; i < mp_device_get_num_entities(device); ++i) {
  88. if (entities[i].id != video_entity_id &&
  89. entities[i].id != entity->id) {
  90. const struct media_v2_pad *pad =
  91. mp_device_get_pad_from_entity(
  92. device, entities[i].id);
  93. mp_device_setup_link(device, pad->id, sink_pad->id,
  94. false);
  95. }
  96. }
  97. // Then enable ours
  98. mp_device_setup_link(device, source_pad->id, sink_pad->id, true);
  99. const struct media_v2_interface *iface =
  100. mp_device_find_entity_interface(device, entity->id);
  101. char buf[256];
  102. if (!mp_find_device_path(iface->devnode, buf, 256)) {
  103. printf("Unable to find sub-device path\n");
  104. return 1;
  105. }
  106. subdev_fd = open(buf, O_RDWR);
  107. if (subdev_fd == -1) {
  108. printf("Unable to open sub-device\n");
  109. return 1;
  110. }
  111. }
  112. double open_end = get_time();
  113. printf("Opening the device took %fms\n", (open_end - find_end) * 1000);
  114. MPCamera *camera = mp_camera_new(video_fd, subdev_fd);
  115. MPControlList *controls = mp_camera_list_controls(camera);
  116. double control_list_end = get_time();
  117. printf("Available controls: (took %fms)\n",
  118. (control_list_end - open_end) * 1000);
  119. for (MPControlList *list = controls; list;
  120. list = mp_control_list_next(list)) {
  121. MPControl *c = mp_control_list_get(list);
  122. printf(" %32s id:%s type:%s default:%d\n", c->name,
  123. mp_control_id_to_str(c->id), mp_control_type_to_str(c->type),
  124. c->default_value);
  125. }
  126. double mode_list_begin = get_time();
  127. MPCameraModeList *modes = mp_camera_list_available_modes(camera);
  128. double mode_list_end = get_time();
  129. printf("Available modes: (took %fms)\n",
  130. (mode_list_end - mode_list_begin) * 1000);
  131. for (MPCameraModeList *list = modes; list;
  132. list = mp_camera_mode_list_next(list)) {
  133. MPCameraMode *m = mp_camera_mode_list_get(list);
  134. printf(" %dx%d interval:%d/%d fmt:%s\n", m->width, m->height,
  135. m->frame_interval.numerator, m->frame_interval.denominator,
  136. mp_pixel_format_to_str(m->pixel_format));
  137. // Skip really slow framerates
  138. if (m->frame_interval.denominator < 15) {
  139. printf(" Skipping…\n");
  140. continue;
  141. }
  142. double start_capture = get_time();
  143. mp_camera_set_mode(camera, m);
  144. mp_camera_start_capture(camera);
  145. double last = get_time();
  146. printf(" Testing 10 captures, starting took %fms\n",
  147. (last - start_capture) * 1000);
  148. for (int i = 0; i < 10; ++i) {
  149. mp_camera_capture_image(camera, on_capture, NULL);
  150. double now = get_time();
  151. printf(" capture took %fms\n", (now - last) * 1000);
  152. last = now;
  153. }
  154. mp_camera_stop_capture(camera);
  155. }
  156. double cleanup_start = get_time();
  157. mp_camera_free(camera);
  158. close(video_fd);
  159. if (subdev_fd != -1)
  160. close(subdev_fd);
  161. mp_device_close(device);
  162. double cleanup_end = get_time();
  163. printf("Cleanup took %fms\n", (cleanup_end - cleanup_start) * 1000);
  164. }