camera_test.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_type(device, MEDIA_ENT_F_IO_V4L);
  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 bridge_fd = -1;
  59. const struct media_v2_entity *bridge =
  60. mp_device_find_entity_type(device, MEDIA_ENT_F_VID_IF_BRIDGE);
  61. if (bridge) {
  62. video_entity_id = bridge->id;
  63. const struct media_v2_interface *iface =
  64. mp_device_find_entity_interface(device, bridge->id);
  65. char buf[256];
  66. if (!mp_find_device_path(iface->devnode, buf, 256)) {
  67. printf("Unable to find bridge device path\n");
  68. return 1;
  69. }
  70. bridge_fd = open(buf, O_RDWR);
  71. if (bridge_fd == -1) {
  72. printf("Unable to open bridge device\n");
  73. return 1;
  74. }
  75. }
  76. int subdev_fd = -1;
  77. const struct media_v2_entity *entity =
  78. mp_device_find_entity(device, subdev_name);
  79. if (!entity) {
  80. printf("Unable to find sub-device\n");
  81. return 1;
  82. }
  83. const struct media_v2_pad *source_pad =
  84. mp_device_get_pad_from_entity(device, entity->id);
  85. const struct media_v2_pad *sink_pad =
  86. mp_device_get_pad_from_entity(device, video_entity_id);
  87. // Disable other links
  88. const struct media_v2_entity *entities = mp_device_get_entities(device);
  89. for (int i = 0; i < mp_device_get_num_entities(device); ++i) {
  90. if (entities[i].id != video_entity_id &&
  91. entities[i].id != entity->id) {
  92. const struct media_v2_pad *pad =
  93. mp_device_get_pad_from_entity(device,
  94. entities[i].id);
  95. mp_device_setup_link(device, pad->id, sink_pad->id, false);
  96. }
  97. }
  98. // Then enable ours
  99. mp_device_setup_link(device, source_pad->id, sink_pad->id, true);
  100. const struct media_v2_interface *iface =
  101. mp_device_find_entity_interface(device, entity->id);
  102. char buf[256];
  103. if (!mp_find_device_path(iface->devnode, buf, 256)) {
  104. printf("Unable to find sub-device path\n");
  105. return 1;
  106. }
  107. subdev_fd = open(buf, O_RDWR);
  108. if (subdev_fd == -1) {
  109. printf("Unable to open sub-device\n");
  110. return 1;
  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, bridge_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",
  123. c->name,
  124. mp_control_id_to_str(c->id),
  125. mp_control_type_to_str(c->type),
  126. c->default_value);
  127. }
  128. double mode_list_begin = get_time();
  129. MPModeList *modes = mp_camera_list_available_modes(camera);
  130. double mode_list_end = get_time();
  131. printf("Available modes: (took %fms)\n",
  132. (mode_list_end - mode_list_begin) * 1000);
  133. for (MPModeList *list = modes; list; list = mp_camera_mode_list_next(list)) {
  134. MPMode *m = mp_camera_mode_list_get(list);
  135. printf(" %dx%d interval:%d/%d fmt:%s\n",
  136. m->width,
  137. m->height,
  138. m->frame_interval.numerator,
  139. m->frame_interval.denominator,
  140. mp_pixel_format_to_str(m->pixel_format));
  141. // Skip really slow framerates
  142. if (m->frame_interval.denominator < 15) {
  143. printf(" Skipping…\n");
  144. continue;
  145. }
  146. double start_capture = get_time();
  147. mp_camera_set_mode(camera, m);
  148. mp_camera_start_capture(camera);
  149. double last = get_time();
  150. printf(" Testing 10 captures, starting took %fms\n",
  151. (last - start_capture) * 1000);
  152. for (int i = 0; i < 10; ++i) {
  153. MPBuffer buffer;
  154. if (!mp_camera_capture_buffer(camera, &buffer)) {
  155. printf(" Failed to capture buffer\n");
  156. }
  157. size_t num_bytes = mp_pixel_format_width_to_bytes(
  158. m->pixel_format, m->width) *
  159. m->height;
  160. uint8_t *data = malloc(num_bytes);
  161. memcpy(data, buffer.data, num_bytes);
  162. printf(" first byte: %d.", data[0]);
  163. free(data);
  164. mp_camera_release_buffer(camera, buffer.index);
  165. double now = get_time();
  166. printf(" capture took %fms\n", (now - last) * 1000);
  167. last = now;
  168. }
  169. mp_camera_stop_capture(camera);
  170. }
  171. double cleanup_start = get_time();
  172. mp_camera_free(camera);
  173. close(video_fd);
  174. if (subdev_fd != -1)
  175. close(subdev_fd);
  176. if (bridge_fd != -1)
  177. close(bridge_fd);
  178. mp_device_close(device);
  179. double cleanup_end = get_time();
  180. printf("Cleanup took %fms\n", (cleanup_end - cleanup_start) * 1000);
  181. }