3

I'm trying to work with the openh264 native library from Java code using JNR. The function I'm calling is defined in openh264's C header file to return a simple struct by value:

typedef struct  _tagVersion {
    unsigned int uMajor;
    unsigned int uMinor;
    unsigned int uRevision;
    unsigned int uReserved;
} OpenH264Version;

OpenH264Version WelsGetCodecVersion (void);

I wrote the following Java counterparts:

public class OpenH264Version extends Struct {

    public OpenH264Version(Runtime runtime) {
        super(runtime);
    }

    public Unsigned32 uMajor = new Unsigned32();
    public Unsigned32 uMinor = new Unsigned32();
    public Unsigned32 uRevision = new Unsigned32();
    public Unsigned32 uReserved = new Unsigned32();
}

public interface OpenH264 {
    public OpenH264Version WelsGetCodecVersion();
}

and finally trying it:

OpenH264 openH264 = LibraryLoader.create(OpenH264.class)
                    .load("openh264");
OpenH264Version version = openH264.WelsGetCodecVersion();

However, the returned version has wrong values (and they are different on each invocation).
The lib was x64 running on Windows 10 x64.
I could load the same lib with JNI+JavaCpp and the return values were correct.
What am I missing?

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
zakgof
  • 213
  • 2
  • 8
  • 1
    I have asked similar question: https://stackoverflow.com/questions/56129984/how-to-return-by-value-from-native-function My current workaround is returning struct as "@Pinned @Out" parameter, but I am able to modify native code. – sgnsajgon May 16 '19 at 12:43
  • Out of curiosity, what would you need from JavaCPP to be able to keep using it instead of switching to JNR? – Samuel Audet Jun 17 '19 at 00:06
  • @SamuelAudet, JavaCpp automates much of the process, but still requires native compilation and bundling native libs. But we continue with JavaCpp because of this issue – zakgof Jun 19 '19 at 09:00
  • Looks like JNR does not support that (because JNI does not support that) as explained here: https://stackoverflow.com/a/56253872/2519698 – zakgof Jun 19 '19 at 14:25
  • @zakgof Correct me if I'm wrong, but it sounds like you're already bundling OpenH264. You could use JavaCPP to generate only the JNI code in C++, and then compile it as part of OpenH264. – Samuel Audet Jun 20 '19 at 06:07
  • 1
    @SamuelAudet, openh264 allows royalty-free usage when your app downloads original binaries on demand. – zakgof Jun 27 '19 at 07:27

0 Answers0