UVCDevice.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. res = uvc_get_stream_ctrl_format_size(devh, &ctrl, frame_format, width, height, fps);
  55. if (res < 0)
  56. {
  57. uvc_perror(res, "get_mode");
  58. }
  59. /* Print out the result */
  60. uvc_print_stream_ctrl(&ctrl, stderr);
  61. res = uvc_start_streaming(devh, &ctrl, cb, &index, 0);
  62. if (res < 0)
  63. {
  64. uvc_perror(res, "start_streaming");
  65. }
  66. puts("Streaming...");
  67. /* enable auto exposure - see uvc_set_ae_mode documentation */
  68. puts("Enabling auto exposure ...");
  69. const uint8_t UVC_AUTO_EXPOSURE_MODE_AUTO = 2;
  70. res = uvc_set_ae_mode(devh, UVC_AUTO_EXPOSURE_MODE_AUTO);
  71. if (res == UVC_SUCCESS)
  72. {
  73. puts(" ... enabled auto exposure");
  74. }
  75. else if (res == UVC_ERROR_PIPE)
  76. {
  77. /* this error indicates that the camera does not support the full AE mode;
  78. * try again, using aperture priority mode (fixed aperture, variable exposure time) */
  79. puts(" ... full AE not supported, trying aperture priority mode");
  80. const uint8_t UVC_AUTO_EXPOSURE_MODE_APERTURE_PRIORITY = 8;
  81. res = uvc_set_ae_mode(devh, UVC_AUTO_EXPOSURE_MODE_APERTURE_PRIORITY);
  82. if (res < 0)
  83. {
  84. uvc_perror(res, " ... uvc_set_ae_mode failed to enable aperture priority mode");
  85. }
  86. else
  87. {
  88. puts(" ... enabled aperture priority auto exposure mode");
  89. }
  90. }
  91. else
  92. {
  93. uvc_perror(res, " ... uvc_set_ae_mode failed to enable auto exposure mode");
  94. }
  95. }
  96. void UVCDevice::stopStreaming()
  97. {
  98. uvc_stop_streaming(devh);
  99. puts("Done streaming.");
  100. }
  101. void UVCDevice::close()
  102. {
  103. uvc_unref_device(dev);
  104. }