2017-02-02 17:24:40 +01:00
|
|
|
//
|
|
|
|
// libtgvoip is free and unencumbered public domain software.
|
|
|
|
// For more information, see http://unlicense.org or the UNLICENSE file
|
|
|
|
// you should have received with this source code distribution.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AudioInputAndroid.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "../../logging.h"
|
|
|
|
|
|
|
|
extern JavaVM* sharedJVM;
|
|
|
|
|
2017-03-30 16:06:59 +02:00
|
|
|
jmethodID CAudioInputAndroid::initMethod=NULL;
|
|
|
|
jmethodID CAudioInputAndroid::releaseMethod=NULL;
|
|
|
|
jmethodID CAudioInputAndroid::startMethod=NULL;
|
|
|
|
jmethodID CAudioInputAndroid::stopMethod=NULL;
|
|
|
|
jclass CAudioInputAndroid::jniClass=NULL;
|
2017-02-02 17:24:40 +01:00
|
|
|
|
|
|
|
CAudioInputAndroid::CAudioInputAndroid(){
|
|
|
|
JNIEnv* env=NULL;
|
|
|
|
bool didAttach=false;
|
|
|
|
sharedJVM->GetEnv((void**) &env, JNI_VERSION_1_6);
|
|
|
|
if(!env){
|
|
|
|
sharedJVM->AttachCurrentThread(&env, NULL);
|
|
|
|
didAttach=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
jmethodID ctor=env->GetMethodID(jniClass, "<init>", "(J)V");
|
|
|
|
jobject obj=env->NewObject(jniClass, ctor, (jlong)(intptr_t)this);
|
|
|
|
javaObject=env->NewGlobalRef(obj);
|
|
|
|
|
|
|
|
if(didAttach){
|
|
|
|
sharedJVM->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
running=false;
|
2017-03-30 16:06:59 +02:00
|
|
|
init_mutex(mutex);
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CAudioInputAndroid::~CAudioInputAndroid(){
|
2017-03-30 16:06:59 +02:00
|
|
|
{
|
|
|
|
CMutexGuard guard(mutex);
|
|
|
|
JNIEnv *env=NULL;
|
|
|
|
bool didAttach=false;
|
|
|
|
sharedJVM->GetEnv((void **) &env, JNI_VERSION_1_6);
|
|
|
|
if(!env){
|
|
|
|
sharedJVM->AttachCurrentThread(&env, NULL);
|
|
|
|
didAttach=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
env->CallVoidMethod(javaObject, releaseMethod);
|
|
|
|
env->DeleteGlobalRef(javaObject);
|
|
|
|
javaObject=NULL;
|
|
|
|
|
|
|
|
if(didAttach){
|
|
|
|
sharedJVM->DetachCurrentThread();
|
|
|
|
}
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
2017-03-30 16:06:59 +02:00
|
|
|
free_mutex(mutex);
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioInputAndroid::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels){
|
2017-03-30 16:06:59 +02:00
|
|
|
CMutexGuard guard(mutex);
|
2017-02-02 17:24:40 +01:00
|
|
|
JNIEnv* env=NULL;
|
|
|
|
bool didAttach=false;
|
|
|
|
sharedJVM->GetEnv((void**) &env, JNI_VERSION_1_6);
|
|
|
|
if(!env){
|
|
|
|
sharedJVM->AttachCurrentThread(&env, NULL);
|
|
|
|
didAttach=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
env->CallVoidMethod(javaObject, initMethod, sampleRate, bitsPerSample, channels, 960*2);
|
|
|
|
|
|
|
|
if(didAttach){
|
|
|
|
sharedJVM->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioInputAndroid::Start(){
|
2017-03-30 16:06:59 +02:00
|
|
|
CMutexGuard guard(mutex);
|
2017-02-02 17:24:40 +01:00
|
|
|
JNIEnv* env=NULL;
|
|
|
|
bool didAttach=false;
|
|
|
|
sharedJVM->GetEnv((void**) &env, JNI_VERSION_1_6);
|
|
|
|
if(!env){
|
|
|
|
sharedJVM->AttachCurrentThread(&env, NULL);
|
|
|
|
didAttach=true;
|
|
|
|
}
|
|
|
|
|
2017-03-30 16:06:59 +02:00
|
|
|
failed=!env->CallBooleanMethod(javaObject, startMethod);
|
2017-02-02 17:24:40 +01:00
|
|
|
|
|
|
|
if(didAttach){
|
|
|
|
sharedJVM->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
running=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioInputAndroid::Stop(){
|
2017-03-30 16:06:59 +02:00
|
|
|
CMutexGuard guard(mutex);
|
2017-02-02 17:24:40 +01:00
|
|
|
running=false;
|
|
|
|
JNIEnv* env=NULL;
|
|
|
|
bool didAttach=false;
|
|
|
|
sharedJVM->GetEnv((void**) &env, JNI_VERSION_1_6);
|
|
|
|
if(!env){
|
|
|
|
sharedJVM->AttachCurrentThread(&env, NULL);
|
|
|
|
didAttach=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
env->CallVoidMethod(javaObject, stopMethod);
|
|
|
|
|
|
|
|
if(didAttach){
|
|
|
|
sharedJVM->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioInputAndroid::HandleCallback(JNIEnv* env, jobject buffer){
|
|
|
|
if(!running)
|
|
|
|
return;
|
|
|
|
unsigned char* buf=(unsigned char*) env->GetDirectBufferAddress(buffer);
|
|
|
|
size_t len=(size_t) env->GetDirectBufferCapacity(buffer);
|
|
|
|
InvokeCallback(buf, len);
|
|
|
|
}
|