2016-03-15 40 views
0

我的應用程序需要將java層中的audiorecord記錄的樣本發送給JNI。由於新線程中的Java環境變量導致的錯誤

但爲了避免內存操作(複製)多次,我在Native層的初始化過程中創建了緩衝區,並將其傳遞給java層,如下所示。

Application.c

#include <jni.h> 
#include <stdio.h> 
#include <string.h> 
#include <pthread.h> 

#include <android/log.h> 
#include "Application.h" 

JNIEnv *Native_env; 
jobject native_obj; 

jmethodID WriteID, ReadID; 
jclass JavaClass; 
jshortArray ShortArray; 

FILE *read_fptr; 
//jsize start = 0; 
//jsize leng = 0; 
jshort NativeArray[1920]; 

void Update_NativeJNVVariables(JNIEnv *env, jobject obj) 
{ 
    Native_env = (JNIEnv *)env; 
    native_obj = obj; 

    //ShortArray = (*Native_env)->NewShortArray(Native_env, 1920); 

    //leng = (*Native_env)->GetArrayLength(Native_env,ShortArray); 

    //__android_log_print(ANDROID_LOG_ERROR, "Update_NativeJNVVariables", "Length of array is %d", leng); 

    memset(NativeArray, 0x00, sizeof(NativeArray)); 

    //(*Native_env)->SetShortArrayRegion(Native_env,ShortArray, start, leng, NativeArray); 

    //jclass LocalClass; 
    //LocalClass = (*Native_env)->GetObjectClass(Native_env,native_obj); 
    //WriteClass = (*Native_env)->NewGlobalRef(Native_env,LocalClass); 

    JavaClass = (*Native_env)->FindClass(Native_env, "com/consilient/tech/uday/javaapi/MainActivity"); 

    //LocalClass = (*Native_env)->GetObjectClass(Native_env,native_obj); 
    //ReadClass = (*Native_env)->NewGlobalRef(Native_env,LocalClass); 

    if(JavaClass != NULL) 
    { 
     WriteID = (*Native_env)->GetMethodID(Native_env,JavaClass,"WriteAndroidPCM","([B)V"); 
    } 

    if(JavaClass != NULL) 
    { 
     //ReadID = (*Native_env)->GetMethodID(Native_env,JavaClass,"ReadAndroidPCM","([SI)V"); 
     ReadID = (*Native_env)->GetMethodID(Native_env,JavaClass,"ReadAndroidPCM","(I)V"); 
    } 

    read_fptr = fopen("/sdcard/outputs/read_file.pcm","wb"); 
    if(read_fptr == NULL) 
    { 
     __android_log_print(ANDROID_LOG_ERROR, "UPdateJavaEnv", "Read FIle Cannot Be opened"); 
    } 
} 

void ReadPCMSamples(short *Buffer, int no_of_samples) 
{ 
    // jboolean isCopy = JNI_TRUE; 
    // jshort *ArrayPointer = (*Native_env)->GetShortArrayElements(Native_env,ShortArray, &isCopy); 

    if(ReadID) 
     (*Native_env)->CallVoidMethod(Native_env,JavaClass,ReadID,no_of_samples); 

    //fwrite(ArrayPointer, sizeof(short), 160, read_fptr); 

    //(*Native_env)->ReleaseShortArrayElements(Native_env, ShortArray, ArrayPointer, 0); 

    //(*Native_env)->GetDirectBufferAddress(Native_env,native_obj); 

    //GetByteArray(Buffer, no_of_samples); 
} 

/* 
* Class:  com_consilient_tech_uday_javaapi_MainActivity 
* Method: StartNativeThread 
* Signature:()V 
*/ 
JNIEXPORT void JNICALL Java_com_consilient_tech_uday_javaapi_MainActivity_FillNativeBuffer 
    (JNIEnv *env, jobject obj, jshortArray Buffer, int no_of_samples) 
{ 
     jboolean isCopy = JNI_TRUE; 
     jshort *ArrayPointer = (*Native_env)->GetShortArrayElements(Native_env, Buffer, 0); 
/* 
     for (int i = 0; i < no_of_samples; i++) 
     { 
      NativeArray[i] = ArrayPointer[i]; 
     } 
*/ 
     (*Native_env)->ReleaseShortArrayElements(Native_env, Buffer, ArrayPointer, 0); 
    } 

int ifReceivedExit = 0; 

void Start() 
{ 
    while(ifReceivedExit == 0) 
    { 
     ReadPCMSamples(NativeArray,160); 
    } 
} 

void Stop() 
{ 
    ifReceivedExit = 1; 
} 

pthread_t threadID; 

void StartThread() 
{ 
int res = 0; 
res = pthread_create(&threadID, NULL, Start, NULL); 
if (res != 0) 
    __android_log_print(ANDROID_LOG_ERROR, "Thread Creation", "Failed %s", strerror(res)); 
else 
    __android_log_print(ANDROID_LOG_ERROR, "Thread Creation", "Success"); // Thread ID %x res %d", threadID, res); 
} 

void StopThread() 
{ 
    Stop(); 
    if(JavaClass) 
     (*Native_env)->DeleteGlobalRef(Native_env,JavaClass); 

    if(read_fptr) 
     fclose(read_fptr); 
} 

jint JNI_OnLoad(JavaVM* vm, void* reserved) 
{ 
    return JNI_VERSION_1_6; 
} 

MainActivity.java

public class MainActivity extends AppCompatActivity 
{ 
    @Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    InitializeJNI(); 
} 

public void ReadAndroidPCM(int no_of_samples) 
{ 
    int read_samples = audioRecord.read(ReadBuffer, 0, no_of_samples); 

    FillNativeBuffer(ReadBuffer,no_of_samples); 

    //WriteRecordedDataToFile(ReadBuffer, read_samples, READ); 
} 

static 
{ 
    System.loadLibrary("VajraAPI"); 
} 

public native void InitializeJNI(); 
public native void StartNativeThread(); 
public native void StopNativeThread(); 
public native void FillNativeBuffer(short[] Buffer, int no_of_samples); 
} 

MainActivity.c

#include <jni.h> 

#include "com_consilient_tech_uday_javaapi_MainActivity.h" 
#include "Application.h" 

/* 
* Class:  com_consilient_tech_uday_javaapi_MainActivity 
* Method: InitializeJNI 
* Signature:()V 
*/ 
JNIEXPORT void JNICALL Java_com_consilient_tech_uday_javaapi_MainActivity_InitializeJNI 
    (JNIEnv *env, jobject obj) 
{ 
Update_NativeJNVVariables(env, obj); 
} 

/* 
* Class:  com_consilient_tech_uday_javaapi_MainActivity 
* Method: StartNativeThread 
* Signature:()V 
*/ 
JNIEXPORT void JNICALL Java_com_consilient_tech_uday_javaapi_MainActivity_StartNativeThread 
    (JNIEnv *env, jobject obj) 
{ 
StartThread(); 
} 

/* 
* Class:  com_consilient_tech_uday_javaapi_MainActivity 
* Method: StartNativeThread 
* Signature:()V 
*/ 
JNIEXPORT void JNICALL Java_com_consilient_tech_uday_javaapi_MainActivity_StopNativeThread 
    (JNIEnv *env, jobject obj) 
{ 
StopThread(); 
} 

/* 
* Class:  com_consilient_tech_uday_javaapi_MainActivity 
* Method: GetByteArrayFromNative 
* Signature: (SI)V 
*/ 
JNIEXPORT void JNICALL Java_com_consilient_tech_uday_javaapi_MainActivity_GetByteArrayFromNative 
    (JNIEnv *env, jobject obj, jint no_of_samples) 
{ 
//GetByteArray(no_of_samples); 
} 

當我嘗試去尋找錯誤的,它是在GetShortArrayRegion崩潰給分割故障

謝謝

+0

'Native_env'從哪裏來?你不應該緩存'JNIEnv'指針,以防你正在做什麼。 – Michael

+0

而你不應該嘗試將'ArrayPointer'傳遞給'ReadAndroidPCM'。 Java方法不知道'jshort *'是什麼。 – Michael

+0

有一個函數Update_JNI_Variables(),它是從Java層調用的,它用接收到的env和object變量更新Native_env變量和Native_obj。 – Uday

回答

0

感謝邁克爾提供的鏈接。

問題是Java本地環境變量的緩存(如您指定的那樣)。

環境不應該被緩存,而類可以通過使用NewGlobalRef方法成爲全局的,這將有助於在其他線程中使用類。方法ID也可以使我成爲全球。

最好在JNI_OnLoad中獲取類和方法ID,併爲其創建全局引用。

當我們創建一個新線程時,我們需要將新線程附加到Java VM。 並獲取JNI Env進行進一步處理。

相關問題