0

i have two class in c language (picture.c and decoder.c) and a Java class (VirtualACtivity.java). My project has got the next structure:

-android
---vlc
-----src
-------input
---------**picture.c**
-------misc
---------**decoder.c**
---vlc-android
-----src
-------org
--------videolan
---------vlc
----------gui
-----------video
--------------**VirtualActivity.java**

I have declared a native method in picture.c:

JNIEXPORT jintArray JNICALL
Java_org_videolan_vlc_gui_video_VirtualActivity_pasoArrays(JNIEnv * env, jobject jobj, jintArray array_color){
    int ancho = 480;
    int alto = 270;
    int size = ancho * alto;

     jintArray result;
     result = (*env)->NewIntArray(env, size);
     if (result == NULL) {
         return NULL; /* out of memory error thrown */
     }

     // move from the temp structure to the java structure
     // SetIntArrayRegion(env, array, start, length, values);
     (*env)->SetIntArrayRegion(env, result, 0, size, array_color);
     return result;
}

In VirtualActivity.java,

static {   
    try{
        Log.d(LOGTAG, "Cargando la librería native");
        System.loadLibrary("native");
        Log.d(LOGTAG, "Library loaded - native");
    }catch (Exception e){
        Log.d(LOGTAG, "Did not load library - native");
    }
}

private native int[] pasoArrays(int array[]);

libnative.so is created from picture.c.

I want to call JNIEXPORT jintArray JNICALL Java_org_videolan_vlc_gui_video_VirtualActivity_pasoArrays(JNIEnv * env, jobject jobj, jintArray array_color) method from a pure c method of the decoder.c. Then, my problem is that I don't know how initialize JNIEnv * env and jobject jobj and if it is possible.

I know that the native code is used to connect c and Java, but I need to call from a c class to a native method of other c class.


Ok,i have followed yours advices and i have written the next code:

In decoder.c:

   msg_Warn( p_dec, "Definición del array");
   int i;
       int size = 129600; 
   for ( i= 0; i< size ;i++){
    aux[i] = 2 ;
   }
   newFunction(aux) ;

In picture.c:

void newFunction(int color) {


    JavaVM* jvm;
    JNIEnv* env;
    JavaVMInitArgs args;
    JavaVMOption options[1];

    args.version = JNI_VERSION_1_6;
    args.nOptions = 1;
    options[0].optionString = "-Djava.class.path=/home/vmg/android/android/vlc-android/src/";
    args.options = options;
    args.ignoreUnrecognized = JNI_FALSE;

    JNI_CreateJavaVM(&jvm, (void **)&env, &args);

    jclass cls = (*env)->FindClass(env,"org.videolan.vlc.gui.video.VirtualActivity");
    jmethodID mid = (*env)->GetStaticMethodID(env, cls, "arrays", "([I)V");
    (*env)->CallStaticVoidMethod(env,cls, mid, color);

}

And in VirtualActivity.java:

public void arrays(int arr[]){
   int i;
   System.out.println("La longitud del array es" + arr.length );
   for (i = 0; i < arr.length; i++) {
   System.out.println("Este mensaje se está imprimiendo desde java" + ",   array = " + arr[i] + " para " + "  i = " + i );
   }
 }

But when i compile, i get the next error:

/home/vmg/android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ../vlc/android/src/.libs/libvlccore.a(picture.o): in function newFunction:../../src/misc/picture.c:646: error: undefined reference to 'JNI_CreateJavaVM'

In Android.mk:

include $(CLEAR_VARS)
LOCAL_MODULE    := native
LOCAL_SRC_FILES := ../../vlc/src/misc/picture.c  

ARCH=$(ANDROID_ABI)

LOCAL_C_INCLUDES := $(VLC_SRC_DIR)/include \
            $(VLC_SRC_DIR)/include/vlc \
                $(ANDROID_NDK)/platforms/android-9/arch-arm/usr/include \
                    $(ANDROID_NDK)/platforms/android-9/arch-arm/usr/include/android \
            /usr/lib/jvm/java-6-openjdk/include \
                    /usr/lib/jvm/java-6-openjdk/include/linux \


CPP_STATIC=$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++$(CXXSTL)/libs/$(ARCH)/libgnustl_static.a

LOCAL_CFLAGS := -std=gnu99
ifeq ($(ARCH), armeabi)
    LOCAL_CFLAGS += -DHAVE_ARMEABI
    # Needed by ARMv6 Thumb1 (the System Control coprocessor/CP15 is mandatory on ARMv6)
    # On newer ARM architectures we can use Thumb2
    LOCAL_ARM_MODE := arm
endif
ifeq ($(ARCH), armeabi-v7a)
    LOCAL_CFLAGS += -DHAVE_ARMEABI_V7A
endif
LOCAL_LDLIBS := -L$(VLC_CONTRIB)/lib \
    $(VLC_MODULES) \
    $(VLC_BUILD_DIR)/lib/.libs/libvlc.a \
    $(VLC_BUILD_DIR)/src/.libs/libvlccore.a \
    $(VLC_BUILD_DIR)/compat/.libs/libcompat.a \
        -L/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server \
    -ldl -lz -lm -llog \
    -ldvbpsi -lebml -lmatroska -ltag \
    -logg -lFLAC -ltheora \
    -lmpeg2 -ldca -la52 \
    -lavformat -lavcodec -lswscale -lavutil -lpostproc -lgsm -lopenjpeg \
    -lliveMedia -lUsageEnvironment -lBasicUsageEnvironment -lgroupsock \
    -lspeex -lspeexdsp \
    -lxml2 -lpng -lgnutls -lgcrypt -lgpg-error \
    -lfreetype -liconv -lass -lfribidi -lopus \
    -ljvm \
    $(CPP_STATIC) 
include $(BUILD_SHARED_LIBRARY)

In the folder /usr/lib/jvm/java-6-openjdk/include is the jni.h file, where is the declaration of :

#ifdef _JNI_IMPLEMENTATION_
#define _JNI_IMPORT_OR_EXPORT_ JNIEXPORT
#else
#define _JNI_IMPORT_OR_EXPORT_ JNIIMPORT
#endif
_JNI_IMPORT_OR_EXPORT_ jint JNICALL
JNI_GetDefaultJavaVMInitArgs(void *args);

_JNI_IMPORT_OR_EXPORT_ jint JNICALL
JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args);

_JNI_IMPORT_OR_EXPORT_ jint JNICALL
JNI_GetCreatedJavaVMs(JavaVM **, jsize, jsize *);

What am i doing wrong?

Thanks so much!

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
VirMarGu
  • 41
  • 8
  • Even if you call a JNI native method from some C code, this will not give your Java side access to this data. You probably need a "callback" from C to Java? Or please explain why you want to call pasoArray – Alex Cohn Jul 06 '13 at 06:03
  • Thanks for the reply! I have array in a c method and i need to pass that array to a java method to process it. The method of the c program isn't native code. – VirMarGu Jul 15 '13 at 16:55

2 Answers2

1

You should not call the native method from anywhere other than Java. Your goal can be achieved by wrapping all the functionality in another c function, and then call that c function from pasoArrays and from decoder.c. Something like this:

int[] newFunction(int[] color) {
// all the functionality goes here
}

JNIEXPORT jintArray JNICALL Java_org_videolan_vlc_gui_video_VirtualActivity_pasoArrays(JNIEnv * env, jobject jobj, jintArray array_color){
    return newFunction(color);
}

// from decoder.c
return newFunction(whatever);
Evgeny Tanhilevich
  • 1,119
  • 1
  • 8
  • 17
  • If i follow your advice, From decoder.c, i call to a newFunction(int[] color), but i have to pass the array to Java and if i call to JNIEXPORT jintArray JNICALL Java_org_videolan_vlc_gui_video_VirtualActivity_pasoArrays from newFunction, it will be as the beginning problem (calling from pure c code to native code). And if i put into newFunction the lines of code: Again the variables named JNIEnv * env and jobject jobj won't be – VirMarGu Jul 05 '13 at 17:00
  • Your comment is mostly incomprehensible, but you only need the JNIEnv and jobect in the JNI function. If you follow @Evgeny's suggestion you don't need them in the extracted C function. – user207421 Jul 06 '13 at 01:46
  • 1
    @VirMarGu, not sure what you are trying to achieve here. The `JNIEnv*` pointer should be valid within a thread, so you could remember the pointer and re-use it later. If you want to call Java from C, then this is possible, what you need is the [Invocation API](http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/invocation.html). With this approach performance overheads are likely to be huge, though. – Evgeny Tanhilevich Jul 06 '13 at 10:41
  • I'm sorry, in my last comment I wrote bad the jni method.I want to call from a c method of decoder.c to a java method of VirtualActivity.java and pass a array to the Java method. – VirMarGu Jul 08 '13 at 11:23
  • Please, i have got the problem yet. anybody can help me? Thanks! – VirMarGu Jul 15 '13 at 16:49
0

Unfortunately, as you discovered yourself, Invocation API and the JNI_CreateJavaVM function aren't supported on Android.

Moreover, this is not an oversight or negligence from the side of Google developers. This is a direct consequence of the Android application model. In Android, the application is driven by the Java VM, not the other way around.

Nevertheless, you can call Java methods from your C code. An example of this technique you can find here: JNI - How to callback from C++ or C to Java?

More details may be found in the JNI documentation.


Update: here is a possible change for your picture.c:
#include <jni.h>
JavaVM* g_jvm = 0;
jclass g_cls;

JNIEXPORT jint JNICALL JNI_void picture_OnLoad(JavaVM *jvm) {, void *reserved) {

    g_jvm = jvm;
jclass cls = (*env)->FindClass(env, "org/videolan/vlc/gui/video/VirtualActivity");
g_cls = (jclass)(*env)->NewGlobalRef(env, cls);
}

void newFunction(int color) {
    JNIEnv* env;
    int bAttached = JNI_FALSE;

    if ((*g_jvm)->GetEnv(g_jvm, (void **)&env, JNI_VERSION_1_4) != JNI_OK) {
        *(g_jvm)->AttachCurrentThread(g_jvm, &env, 0);
        bAttached = JNI_TRUE;
    }

    jmethodID mid = (*env)->GetStaticMethodID(env, cls, "arrays", "([I)V");
    (*env)->CallStaticVoidMethod(env, cls, mid, color);

    if (bAttached) {
        *(g_jvm)->DetachCurrentThread(g_jvm);
    }
}

Note that you must fix your Android.mk file, so that the compiler looks for jni.h in $(ANDROID_NDK)/platforms/android-9/arch-arm/usr/include, so remove /usr/lib/jvm/java-6-openjdk/include and /usr/lib/jvm/java-6-openjdk/include/linux.

Also note, that you must change the definition of VirtualActivity.arrays() method:

static void arrays(int arr[]) {
   int i;
   System.out.println("La longitud del array es" + arr.length );
   for (i = 0; i < arr.length; i++) {
       System.out.println("Este mensaje se está imprimiendo desde java" + ",   array = " + arr[i] + " para " + "  i = " + i );
   }
}

It is only called from native, therefore you don't need it public. But you find it as StaticVoidMethod, threfore it should be declared static.

In libvlcjni.c

extern void picture_OnLoad(JavaVM *vm);

jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    // Keep a reference on the Java VM.
    myVm = vm;

picture_OnLoad(vm);

    pthread_mutex_init(&vout_android_lock, NULL);

    LOGD("JNI interface loaded.");
    return JNI_VERSION_1_2;
}
Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Thanks for your reply! But my problem is that the call is realized from a method which isn't native code (JNI) and i don't know to initialize the env pointer. From native code(JNI) call and pass data to Java, i know to do it. My options are: 1- call from the c code (but it isn't native code JNI) to Java code or 2- call from the c code (but it isn't native code JNI) to native code which it will pass data to Java later. – VirMarGu Jul 17 '13 at 09:50
  • Yes, you can call a Java callback from C, following the technique I linked above. You can, and that's a recommended pattern, to separate the Java-related code from the algorithms, as you did with `picture.c`. See the [update](http://stackoverflow.com/a/17662119/192373) for some code. – Alex Cohn Jul 17 '13 at 10:13
  • Thanks so much, Alex Cohn! I've tested the code and after i've resolved some problems, i get the next error: error: ../vlc/android/src/.libs/libvlccore.a(picture.o): multiple definition of'JNI_OnLoad' ./obj/local/armeabi-v7a/objs-debug/vlcjni/libvlcjni.o: previous definition here // libvlcjni.so is other native library of the project. – VirMarGu Jul 17 '13 at 23:10
  • If i change the name of the method (maybe this is wrong), there isn't any problem in the compilation . But the application crashes when it executes the next line of code: if ((*g_jvm)->GetEnv(g_jvm, (void **)&env, JNI_VERSION_1_4) != JNI_OK) In decoder.c, i call to the newFunction(aux) method. I don't know where call to the JNI_OnLoad method, to so look for VirtualActivity class. Please i need help! Thanks! – VirMarGu Jul 17 '13 at 23:14
  • The easiest workaround is to change your `picture.c` (see the update), and add a call to your `picture_OnLoad()` to JNI_OnLoad function in libvlcjni.c. – Alex Cohn Jul 18 '13 at 00:19
  • Thanks so much for your help!I've changed the code which you've indicated me. But the application crashes...if the line of code:picture_OnLoad(vm);is commented, i.e it doesn't execute, then the application works fine, although c doesn't pass the array to java. The file named libvlcjni.c is into the folder vlc-android/jni, is there any condition more? – VirMarGu Jul 18 '13 at 15:31
  • What is the crash you get? – Alex Cohn Jul 18 '13 at 17:01
  • Thanks for try to help me! If i put the instruction:picture_OnLoad(vm); in the JNI_OnLOad, the application doesn't open and i get some similar errors to this error: 07-23 19:13:00.117: E/Ricky_time(518): index = -1 07-23 19:13:00.117: E/Ricky_time(518): -1 index Hour Diff = 0 Min Diff = 0 07-23 19:13:51.585: E/dalvikvm-heap(21533): Creating VM heap of size start:2097152 max:536870912 base:0x4000d000 07-23 19:13:52.671: E/ActivityManager(217): Btips test >>> restart package locked package name:com.lge.lge3dgameconverter ... – VirMarGu Jul 23 '13 at 17:19
  • So, the application starts if you comment out the call to picture_OnLoad()? Please compare the logs in both cases, and post the difference. Note that your Java code does prints to stdout, and not to log. Please use Android log everywhere. I am afraid that the app cannot start because there are too many calls to VirtualActivity.arrays(). – Alex Cohn Jul 24 '13 at 06:42
  • If the call to picture_OnLoad() in JNI_OnLoad() doesn't exit,the application installs in the mobile phone, but when I want to play a video, the application doesn't work fine. The error is: E/VLC/JNI/thumbnailer(6589): Could not find the video dimensions. E/dalvikvm(6589): Could not find class 'org.videolan.vlc.gui.video.VideoPlayerActivity$11', referenced from method org.videolan.vlc.gui.video.VideoPlayerActivity.onCreate E/Real3D(6589): setS3DType type = 129 E/VLC/VideoPlayerActivity(6589): Event not handled (0x200) E/VLC(6589): [0x68ab24]: main vout display Failed to set on top.. – VirMarGu Jul 24 '13 at 13:40
  • And the error continues:...E/InputDispatcher(231): channel '4088eaf8 org.videolan.vlc/org.videolan.vlc.gui.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8. E/InputDispatcher(231): channel '4088eaf8 org.videolan.vlc/org.videolan.vlc.gui.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed! – VirMarGu Jul 24 '13 at 13:41
  • But if that call is executed, then the application doesn't install in my mobile phone and the error is: E/StatusBarPolicy(300): Don't appear dataicon during the connection of wifi E/Ricky_time(567): index = -1 E/Ricky_time(567): -1 index Hour Diff = 0 Min Diff = 0 E/dalvikvm-heap(6944): Creating VM heap of size start:2097152 max:536870912 base:0x4000d000 E/dalvikvm-heap(6990): Creating VM heap of size start:2097152 max:536870912 base:0x4000d000 E/PackageChangeReceiver(5624): action/data of the intent is NULL or empty E/PackageChangeReceiver(5624): action/data of the intent is NULL or empty – VirMarGu Jul 24 '13 at 13:51