V4L2 Read Fails with EINVAL

I am working on a class library that uses V4L2 to capture video from available video inputs. I've written functions to list the available devices, and I've been able to write a function that opens the device and sets the format, but, when trying to read a frame from the V4L2 device, I get a EINVAL error. The V4L2 docs say that this is because the device does not support read caps, but I have already checked the device (in another function) with the QUERYCAPS call checking for the READWRITE flag to be set. So, according to the query, I can read, but when I try to read, it says it can't. Anyone know why this is? Please see my source code for the open function below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Device_Errors VideoInputDevice::Open(MediaFormat* format){
    Device_Errors retval = SUCCEEDED;
    try
    {
        string file("/dev/video");
        char index[3];
        sprintf(index,"%d", DeviceIndex, 10);
        file = file + index;
        VideoMediaFormat* vformat = (VideoMediaFormat*)format;
        VideoInputDeviceContext* context = new VideoInputDeviceContext();
        context->DeviceHandle = -1;
        context->Format = vformat;
        context->DeviceHandle = open(file.c_str(), O_RDWR);
        if(context->DeviceHandle == -1)
        {
            retval = INVALID_DEVICE;
        }
        else{
            v4l2_format rawfmt;
            memset(&rawfmt, 0, sizeof(v4l2_format));
            rawfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

            rawfmt.fmt.pix.width = vformat->Width;
            rawfmt.fmt.pix.height = vformat->Height;
            rawfmt.fmt.pix.pixelformat = (__u32)GetBPPFCC((VideoPixelFormat)vformat->PixelFormat);
            if(ioctl(context->DeviceHandle, VIDIOC_S_FMT, &rawfmt) == -1)
            {
                retval = INVALID_FORMAT;
            }
            else{
                context->Listener = Listener;
                DeviceContext = context;
                context->Stopped = false;
                context->ImageSize = rawfmt.fmt.pix.sizeimage;
                void* buffer = malloc(context->ImageSize);
                int r = read(context->DeviceHandle, buffer, context->ImageSize);
                free(buffer);
                r = pthread_create( &context->CaptureThread, NULL, &VideoInputDevice_Thread, (void*) DeviceContext);
            }
        }
        
    }
    catch(...){
        retval = UNEXPECTED;
    }
    return retval;
}
Topic archived. No new replies allowed.