UVCDevice.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "UVCDevice.h"
  2. #include <iostream>
  3. void UVCDevice::init(int index, uvc_context_t *ctx, uvc_device_t *dev)
  4. {
  5. uvc_error_t ret;
  6. uvc_device_descriptor_t *des;
  7. this->index = index;
  8. ret = uvc_get_device_descriptor(dev, &des);
  9. if (ret < 0)
  10. {
  11. uvc_perror(ret, "uvc_find_device");
  12. return;
  13. }
  14. ret = uvc_open(dev, &devh);
  15. if (ret < 0)
  16. {
  17. uvc_perror(ret, "uvc_open");
  18. return;
  19. }
  20. std::cout << "Device Find Handle is " << devh << std::endl;
  21. uvc_free_device_descriptor(des);
  22. // Print out a message containing all the information that libuvc knows about the device
  23. // uvc_print_diag(devh, stderr);
  24. const uvc_format_desc_t *format_desc = uvc_get_format_descs(devh);
  25. const uvc_frame_desc_t *frame_desc = format_desc->frame_descs;
  26. // set default frame_format
  27. switch (format_desc->bDescriptorSubtype)
  28. {
  29. case UVC_VS_FORMAT_MJPEG:
  30. frame_format = UVC_COLOR_FORMAT_MJPEG;
  31. break;
  32. case UVC_VS_FORMAT_FRAME_BASED:
  33. frame_format = UVC_FRAME_FORMAT_H264;
  34. break;
  35. default:
  36. frame_format = UVC_FRAME_FORMAT_YUYV;
  37. break;
  38. }
  39. }
  40. void UVCDevice::setStreamParameters(int width, int height, int fps, uvc_frame_format frame_format)
  41. {
  42. this->width = width;
  43. this->height = height;
  44. this->fps = fps;
  45. this->frame_format = frame_format;
  46. }
  47. void UVCDevice::setCallBack(uvc_frame_callback_t *cb)
  48. {
  49. this->cb = cb;
  50. }
  51. void UVCDevice::startStreaming()
  52. {
  53. uvc_error_t res;
  54. uvc_device_t *dev_device = uvc_get_device(devh);
  55. res = uvc_get_stream_ctrl_format_size(devh, &ctrl, frame_format, width, height, fps);
  56. if (res < 0)
  57. {
  58. uvc_perror(res, "get_mode");
  59. }
  60. /* Print out the result */
  61. uvc_print_stream_ctrl(&ctrl, stderr);
  62. res = uvc_start_streaming(devh, &ctrl, cb, &index, 0);
  63. if (res < 0)
  64. {
  65. uvc_perror(res, "start_streaming");
  66. }
  67. puts("Streaming...");
  68. /* enable auto exposure - see uvc_set_ae_mode documentation */
  69. puts("Enabling auto exposure ...");
  70. const uint8_t UVC_AUTO_EXPOSURE_MODE_AUTO = 4;
  71. res = uvc_set_ae_mode(devh, UVC_AUTO_EXPOSURE_MODE_AUTO);
  72. if (res == UVC_SUCCESS)
  73. {
  74. // puts(" ... enabled auto exposure");
  75. //set exposure time
  76. uint32_t exposure_time = 250; // 1 second in microseconds
  77. res = uvc_set_exposure_abs(devh, exposure_time);
  78. // res = uvc_set_white_balance_component_auto(devh,0);
  79. if (res < 0)
  80. {
  81. uvc_perror(res, " ... uvc_set_exposure_abs failed");
  82. }
  83. else
  84. {
  85. puts(" ... exposure time set to 1 second");
  86. }
  87. }
  88. else if (res == UVC_ERROR_PIPE)
  89. {
  90. /* this error indicates that the camera does not support the full AE mode;
  91. * try again, using aperture priority mode (fixed aperture, variable exposure time) */
  92. puts(" ... full AE not supported, trying aperture priority mode");
  93. const uint8_t UVC_AUTO_EXPOSURE_MODE_APERTURE_PRIORITY = 8;
  94. res = uvc_set_ae_mode(devh, UVC_AUTO_EXPOSURE_MODE_APERTURE_PRIORITY);
  95. if (res < 0)
  96. {
  97. uvc_perror(res, " ... uvc_set_ae_mode failed to enable aperture priority mode");
  98. }
  99. else
  100. {
  101. puts(" ... enabled aperture priority auto exposure mode");
  102. }
  103. }
  104. else
  105. {
  106. uvc_perror(res, " ... uvc_set_ae_mode failed to enable auto exposure mode");
  107. }
  108. }
  109. void UVCDevice::stopStreaming()
  110. {
  111. uvc_stop_streaming(devh);
  112. puts("Done streaming.");
  113. }
  114. void UVCDevice::close()
  115. {
  116. uvc_unref_device(dev);
  117. }