javacpp-presets icon indicating copy to clipboard operation
javacpp-presets copied to clipboard

Parsing libv4l2.h and videodev2.h

Open mihetare opened this issue 6 years ago • 12 comments

Hi, I am trying to make a javacpp preset for V4L2. At the moment, I can compile everything successfully, though with a bit of modifications in the source files. In videodev2.h, I have to get manually rid of the attribute ((packed)) -keyword that is used in conjunction of some Structs in the file. Otherwise these cause NullPointerExceptions during the mvn install.

In addition, in libv4l2.h functions such as v4l2_open are declared as a variadic function and I have not yet found a way to deal with this properly. While the wrapping works, I seem to be losing functionality as the ... -part inf the function declaration is ignored.

The draft code can be found in https://github.com/Viritystila/V4L2JB

Best regards, Mikael Reponen

mihetare avatar Feb 05 '19 09:02 mihetare

Thanks! This guide should help you with these issues: https://github.com/bytedeco/javacpp/wiki/Mapping-Recipes Let me know if there is anything you're not able to figure out though.

saudet avatar Feb 05 '19 10:02 saudet

I have managed to map most of the problem points, but I have been unable to cleanly parse the structs with the __attribute__ ((packed)) attribute. I can set infoMap.put(new Info("__attribute__").skip()), which removes the NullPointerException but causes the structs, defined for example as

struct v4l2_rds_data { __u8 lsb; __u8 msb; __u8 block; } __attribute__ ((packed));

, to no be parsed. Similar method does not seem to affect the "((packed))" -part at all.

Concerning the varargs, I have tried such mappings as

        infoMap.put(new Info ("...").valueTypes("int").pointerTypes("IntPointer", "IntBuffer", "int[]", "Pointer"));   
        infoMap.put(new Info("...").valueTypes("@Cast(\"unsigned long int\") long"));

with no success. The C function is declared as

v4l2_ioctl(int fd, unsigned long int request, ...);

but the last vararg argument is still omitted from the Java method.

Thanks in advance!

mihetare avatar Feb 05 '19 21:02 mihetare

For the attribute, try instead infoMap.put(new Info("attribute").annotation())

saudet avatar Feb 05 '19 22:02 saudet

As for varargs, we can declare the actual parameters we need to use with Info.javaText(): https://github.com/bytedeco/javacpp/wiki/Mapping-Recipes#mapping-a-declaration-to-custom-code

saudet avatar Feb 06 '19 06:02 saudet

Thanks! I got the wrapper to work at least partially, I can read an image using v4l2_read, though not consistently. Also, I still need to manually remove the __attribute__((packed)) from the source file but that is ok as I need to change some other lines there too.

The c -function v4l2_mmap returns a mmap(), I suppose I have to modify the return value to BytePointer or similar in Java?

mihetare avatar Feb 07 '19 07:02 mihetare

You mean you need to call the system mmap() function? We could map it manually like I did here with poll() for libdc1394: https://github.com/bytedeco/javacpp-presets/blob/master/libdc1394/src/main/java/org/bytedeco/javacpp/presets/dc1394.java#L87

Though, it sounds like something that we could add to the systems presets: https://github.com/bytedeco/javacpp-presets/tree/master/systems

saudet avatar Feb 07 '19 08:02 saudet

The v4l2_mmap -function calls mmap() and returns a pointer to the mapped buffer.

One separate issue is that the C-function v4l2_ioctl(int fd, unsigned long int request, ...); accepts both Struct and int as the vargarg. While I got the Struct sorted with a pointer, I have not yet figured how to manage the int arg simultaneously.

mihetare avatar Feb 07 '19 09:02 mihetare

Java supports method overloading, that's what it's for. :)

saudet avatar Feb 07 '19 09:02 saudet

As for the issue with attribute declarations, I've just fixed this in commit https://github.com/bytedeco/javacpp/commit/e435a018957143bcbaef022212cae785249eb7e7.

Let me know if you encounter any other issues, and thanks for reporting!

saudet avatar Feb 19 '19 05:02 saudet

Thanks, I'll put this into use. So far I have gotten V4L2 input/output working using the read and write functions. The supposedly better method of streaming with memory mapping crashes the JVM, but am still looking into why it happens.

mihetare avatar Feb 19 '19 06:02 mihetare

@mihetare did you ever get your preset for V4L2 working?

barrypitman avatar Aug 25 '24 19:08 barrypitman

Sorry for the delay! I managed to get something working, but it would need a bit more work to make it good for general use. Please check it out in https://github.com/Viritystila/V4L2JB/tree/master

mihetare avatar Sep 06 '24 20:09 mihetare