由于新线程中的 Java 环境变量而出错
Error due to Java Environment Variable in new Thread
我的应用程序需要将音频记录在 java 层中记录的样本发送到 JNI。
但是为了避免多次内存操作(复制),我在 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 处崩溃并给出分段错误
谢谢
感谢 Michael 提供的 link。
问题是 Java 本机环境变量的缓存(如您指定的那样)。
环境不应被缓存,而 class 可以通过使用 NewGlobalRef 方法设为全局,这也有助于在其他线程中使用 class。方法 ID 也可以设为全局。
最好获取 Class 和 JNI_OnLoad 中的 MethodID 并为其创建全局引用。
当我们创建一个新线程时,我们需要将新线程附加到Java VM。
并获取JNI Env进行进一步处理。
我的应用程序需要将音频记录在 java 层中记录的样本发送到 JNI。
但是为了避免多次内存操作(复制),我在 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 处崩溃并给出分段错误
谢谢
感谢 Michael 提供的 link。
问题是 Java 本机环境变量的缓存(如您指定的那样)。
环境不应被缓存,而 class 可以通过使用 NewGlobalRef 方法设为全局,这也有助于在其他线程中使用 class。方法 ID 也可以设为全局。
最好获取 Class 和 JNI_OnLoad 中的 MethodID 并为其创建全局引用。
当我们创建一个新线程时,我们需要将新线程附加到Java VM。 并获取JNI Env进行进一步处理。