camera_test.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 != 2 && argc != 3) {
  21. printf("Usage: %s <media_device_name> [<sub_device_name>]\n",
  22. argv[0]);
  23. return 1;
  24. }
  25. char *video_name = argv[1];
  26. char *subdev_name = NULL;
  27. if (argc == 3) {
  28. subdev_name = argv[2];
  29. }
  30. double find_start = get_time();
  31. // First find the device
  32. MPDevice *device = mp_device_find(video_name);
  33. if (!device) {
  34. printf("Device not found\n");
  35. return 1;
  36. }
  37. double find_end = get_time();
  38. printf("Finding the device took %fms\n", (find_end - find_start) * 1000);
  39. int video_fd;
  40. uint32_t video_entity_id;
  41. {
  42. const struct media_v2_entity *entity =
  43. mp_device_find_entity(device, video_name);
  44. if (!entity) {
  45. printf("Unable to find video device interface\n");
  46. return 1;
  47. }
  48. video_entity_id = entity->id;
  49. const struct media_v2_interface *iface =
  50. mp_device_find_entity_interface(device, video_entity_id);
  51. char buf[256];
  52. if (!mp_find_device_path(iface->devnode, buf, 256)) {
  53. printf("Unable to find video device path\n");
  54. return 1;
  55. }
  56. video_fd = open(buf, O_RDWR);
  57. if (video_fd == -1) {
  58. printf("Unable to open video device\n");
  59. return 1;
  60. }
  61. }
  62. int subdev_fd = -1;
  63. if (subdev_name) {
  64. const struct media_v2_entity *entity =
  65. mp_device_find_entity(device, subdev_name);
  66. if (!entity) {
  67. printf("Unable to find sub-device\n");
  68. return 1;
  69. }
  70. const struct media_v2_pad *source_pad =
  71. mp_device_get_pad_from_entity(device, entity->id);
  72. const struct media_v2_pad *sink_pad =
  73. mp_device_get_pad_from_entity(device, video_entity_id);
  74. // Disable other links
  75. const struct media_v2_entity *entities =
  76. mp_device_get_entities(device);
  77. for (int i = 0; i < mp_device_get_num_entities(device); ++i) {
  78. if (entities[i].id != video_entity_id &&
  79. entities[i].id != entity->id) {
  80. const struct media_v2_pad *pad =
  81. mp_device_get_pad_from_entity(
  82. device, entities[i].id);
  83. mp_device_setup_link(
  84. device, pad->id, sink_pad->id, false);
  85. }
  86. }
  87. // Then enable ours
  88. mp_device_setup_link(device, source_pad->id, sink_pad->id, true);
  89. const struct media_v2_interface *iface =
  90. mp_device_find_entity_interface(device, entity->id);
  91. char buf[256];
  92. if (!mp_find_device_path(iface->devnode, buf, 256)) {
  93. printf("Unable to find sub-device path\n");
  94. return 1;
  95. }
  96. subdev_fd = open(buf, O_RDWR);
  97. if (subdev_fd == -1) {
  98. printf("Unable to open sub-device\n");
  99. return 1;
  100. }
  101. }
  102. double open_end = get_time();
  103. printf("Opening the device took %fms\n", (open_end - find_end) * 1000);
  104. MPCamera *camera = mp_camera_new(video_fd, subdev_fd);
  105. MPControlList *controls = mp_camera_list_controls(camera);
  106. double control_list_end = get_time();
  107. printf("Available controls: (took %fms)\n",
  108. (control_list_end - open_end) * 1000);
  109. for (MPControlList *list = controls; list;
  110. list = mp_control_list_next(list)) {
  111. MPControl *c = mp_control_list_get(list);
  112. printf(" %32s id:%s type:%s default:%d\n",
  113. c->name,
  114. mp_control_id_to_str(c->id),
  115. mp_control_type_to_str(c->type),
  116. c->default_value);
  117. }
  118. double mode_list_begin = get_time();
  119. MPModeList *modes = mp_camera_list_available_modes(camera);
  120. double mode_list_end = get_time();
  121. printf("Available modes: (took %fms)\n",
  122. (mode_list_end - mode_list_begin) * 1000);
  123. for (MPModeList *list = modes; list; list = mp_camera_mode_list_next(list)) {
  124. MPMode *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. }