AndroidAndroid知识Android开发

JAVA 和 native 共享内存

2017-06-02  本文已影响53人  NiceDream

java层

private ByteBuffer _recBuffer;
 
 try{
    _recBuffer = ByteBuffer.allocateDirect(204800); // Max 10 ms @ 48
    } catch(Exception e) {
      Log.d("UVCCamera","can not get _capBuffer");
    }

native 层

jclass cls = env->GetObjectClass(thiz);
    if(cls == NULL){
        LOGE("cls == NULL");
    } else {

    }
    jfieldID fidRecBuffer = env->GetFieldID( cls, "_recBuffer",  "Ljava/nio/ByteBuffer;");
    if(fidRecBuffer == NULL){
        LOGE("fidRecBuffer == NULL");
    } else {

    }
    jobject javaRecBufferLocal =  env->GetObjectField(thiz, fidRecBuffer);
    if(javaPlayBufferLocal == NULL){
        LOGE("javaPlayBufferLocal == NULL");
    } else {

    }
    _javaRecBuffer = env->NewGlobalRef(javaRecBufferLocal);
    if(_javaRecBuffer == NULL){
        LOGE("_javaRecBuffer == NULL");
    } else {
        _javaDirectRecBuffer =env->GetDirectBufferAddress(_javaRecBuffer);
    }



void* _javaDirectRecBuffer; // Direct buffer pointer to rec buffer
jobject _javaRecBuffer;

可以对_javaDirectRecBuffer进行操作,再java 层会同时更新数据

上一篇下一篇

猜你喜欢

热点阅读