camera_test.c 7.3 KB

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