123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #include "UVCDevice.h"
- #include <iostream>
- void UVCDevice::init(int index,uvc_context_t *ctx, uvc_device_t *dev)
- {
- uvc_error_t ret;
- uvc_device_descriptor_t *des;
- this->index = index;
- ret = uvc_get_device_descriptor(dev, &des);
- if (ret < 0)
- {
- uvc_perror(ret, "uvc_find_device");
- return;
- }
- ret = uvc_open(dev, &devh);
- if (ret < 0)
- {
- uvc_perror(ret, "uvc_open");
- return;
- }
- std::cout << "Device Find Handle is " << devh << std::endl;
- uvc_free_device_descriptor(des);
- // Print out a message containing all the information that libuvc knows about the device
- // uvc_print_diag(devh, stderr);
- const uvc_format_desc_t *format_desc = uvc_get_format_descs(devh);
- const uvc_frame_desc_t *frame_desc = format_desc->frame_descs;
- // set default frame_format
- switch (format_desc->bDescriptorSubtype)
- {
- case UVC_VS_FORMAT_MJPEG:
- frame_format = UVC_COLOR_FORMAT_MJPEG;
- break;
- case UVC_VS_FORMAT_FRAME_BASED:
- frame_format = UVC_FRAME_FORMAT_H264;
- break;
- default:
- frame_format = UVC_FRAME_FORMAT_YUYV;
- break;
- }
- }
- void UVCDevice::setStreamParameters(int width, int height, int fps, uvc_frame_format frame_format)
- {
- this->width = width;
- this->height = height;
- this->fps = fps;
- this->frame_format = frame_format;
- }
- void UVCDevice::setCallBack(uvc_frame_callback_t *cb)
- {
- this->cb = cb;
- }
- void UVCDevice::startStreaming()
- {
- uvc_error_t res;
- res = uvc_get_stream_ctrl_format_size(devh, &ctrl, frame_format, width, height, fps);
- if (res < 0)
- {
- uvc_perror(res, "get_mode");
- }
- /* Print out the result */
- uvc_print_stream_ctrl(&ctrl, stderr);
- res = uvc_start_streaming(devh, &ctrl, cb, &index, 0);
- if (res < 0)
- {
- uvc_perror(res, "start_streaming");
- }
- puts("Streaming...");
- /* enable auto exposure - see uvc_set_ae_mode documentation */
- puts("Enabling auto exposure ...");
- const uint8_t UVC_AUTO_EXPOSURE_MODE_AUTO = 2;
- res = uvc_set_ae_mode(devh, UVC_AUTO_EXPOSURE_MODE_AUTO);
- if (res == UVC_SUCCESS)
- {
- puts(" ... enabled auto exposure");
- }
- else if (res == UVC_ERROR_PIPE)
- {
- /* this error indicates that the camera does not support the full AE mode;
- * try again, using aperture priority mode (fixed aperture, variable exposure time) */
- puts(" ... full AE not supported, trying aperture priority mode");
- const uint8_t UVC_AUTO_EXPOSURE_MODE_APERTURE_PRIORITY = 8;
- res = uvc_set_ae_mode(devh, UVC_AUTO_EXPOSURE_MODE_APERTURE_PRIORITY);
- if (res < 0)
- {
- uvc_perror(res, " ... uvc_set_ae_mode failed to enable aperture priority mode");
- }
- else
- {
- puts(" ... enabled aperture priority auto exposure mode");
- }
- }
- else
- {
- uvc_perror(res, " ... uvc_set_ae_mode failed to enable auto exposure mode");
- }
- }
- void UVCDevice::stopStreaming()
- {
- uvc_stop_streaming(devh);
- puts("Done streaming.");
- }
- void UVCDevice::close()
- {
- uvc_unref_device(dev);
- }
|