camera_test.c 6.8 KB

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