I am working in Windows and trying to access a USB webcam stream using libuvc.
The device is found, initialized, and starts streaming but the imshow() window does not open and hence I do not get stream. The newFrameCallback function isn't called (no output was printed on the console as shown in the image)
even though the device starts streaming.
I have used libuvc to access uvc class devices and supporting libraries like libusb and pthread.
What am I doing wrong?
void newFrameCallback(uvc_frame_t* frame, void* ptr){
uvc_frame_t* rgb;
uvc_error_t deviceStatus;
printf("Entering callback function on frame number %d\n", frameNum);
//allocates frame for converting to rgb
rgb = uvc_allocate_frame(frame->width * frame->height *3);
if(!rgb) {
printf("Abort: unable to allocate frame\n");
//uvc_free_frame(frame);
return;
}
Mat cvImg;
//converts to rgb
if(frame->frame_format == UVC_FRAME_FORMAT_MJPEG){
deviceStatus = uvc_any2rgb(frame, rgb);
if(deviceStatus) {
uvc_perror(deviceStatus, "uvc_mjpeg2rgb");
uvc_free_frame(rgb);
return;
}
cvImg = Mat(rgb->height, rgb->width, CV_8UC3, (uchar*)rgb->data);
cvtColor(cvImg, cvImg, COLOR_RGB2BGR);
}
else if(frame->frame_format == UVC_FRAME_FORMAT_UNCOMPRESSED){
printf("Copying uncompressed frame\n");
cvImg = Mat(frame->height, frame->width, CV_16SC1, (uchar*) frame->data);
}
else{
printf("Illegal frame format %d\n", frame->frame_format);
uvc_free_frame(rgb);
frameNum = frameNum + 1;
return;
}
//displays
imshow("UVC Image", cvImg);
waitKey(1);
frameNum = frameNum+1;
uvc_free_frame(rgb);
}
uvc_context_t* context;
uvc_device_t* device;
uvc_device_handle_t* deviceHandle;
uvc_stream_ctrl_t ctrl;
uvc_error_t status;
status = uvc_init(&context, NULL);
if(status < 0){
uvc_perror(status, "uvc_init");
return status;
}
printf("Trying to find device with pID %d\n", productID);
status = uvc_find_device(context, &device, 0, productID, NULL);
if(status < 0){
uvc_perror(status, "uvc_find_device");
return status;
}
puts("Device initialized and found");
status = uvc_open(device, &deviceHandle);
if(status < 0){
uvc_perror(status, "uvc_open");
return status;
}
status = uvc_get_stream_ctrl_format_size(deviceHandle, &ctrl,
UVC_FRAME_FORMAT_UNCOMPRESSED, width, height, 30);
void *frame_data;
if(status<0){
uvc_perror(status, "get_mode");
}
else {
status = uvc_start_streaming(deviceHandle, &ctrl, newFrameCallback,
(void*)12345,0);
if(status<0)uvc_perror(status, "start_streaming");
else{
printf("Streaming...\n");
uvc_set_ae_mode(deviceHandle, 1);
Sleep(200);
uvc_stop_streaming(deviceHandle);
printf("Done streaming.\n");
}
uvc_unref_device(device);
}
//disconnect from the device
uvc_exit(context);
return 0;****