2018-10-03 14:34:43 +00: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"
|
2019-02-06 18:22:38 +00:00
|
|
|
#include "JNIUtilities.h"
|
2018-10-03 14:34:43 +00:00
|
|
|
|
|
|
|
extern JavaVM* sharedJVM;
|
|
|
|
|
|
|
|
using namespace tgvoip;
|
|
|
|
using namespace tgvoip::audio;
|
|
|
|
|
|
|
|
jmethodID AudioInputAndroid::initMethod=NULL;
|
|
|
|
jmethodID AudioInputAndroid::releaseMethod=NULL;
|
|
|
|
jmethodID AudioInputAndroid::startMethod=NULL;
|
|
|
|
jmethodID AudioInputAndroid::stopMethod=NULL;
|
2019-02-06 18:22:38 +00:00
|
|
|
jmethodID AudioInputAndroid::getEnabledEffectsMaskMethod=NULL;
|
2018-10-03 14:34:43 +00:00
|
|
|
jclass AudioInputAndroid::jniClass=NULL;
|
|
|
|
|
|
|
|
AudioInputAndroid::AudioInputAndroid(){
|
2019-02-06 18:22:38 +00:00
|
|
|
jni::DoWithJNI([this](JNIEnv* env){
|
|
|
|
jmethodID ctor=env->GetMethodID(jniClass, "<init>", "(J)V");
|
|
|
|
jobject obj=env->NewObject(jniClass, ctor, (jlong)(intptr_t)this);
|
|
|
|
javaObject=env->NewGlobalRef(obj);
|
|
|
|
|
|
|
|
env->CallVoidMethod(javaObject, initMethod, 48000, 16, 1, 960*2);
|
|
|
|
enabledEffects=(unsigned int)env->CallIntMethod(javaObject, getEnabledEffectsMaskMethod);
|
|
|
|
});
|
2018-10-03 14:34:43 +00:00
|
|
|
running=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioInputAndroid::~AudioInputAndroid(){
|
|
|
|
{
|
|
|
|
MutexGuard guard(mutex);
|
2019-02-06 18:22:38 +00:00
|
|
|
jni::DoWithJNI([this](JNIEnv* env){
|
|
|
|
env->CallVoidMethod(javaObject, releaseMethod);
|
|
|
|
env->DeleteGlobalRef(javaObject);
|
|
|
|
javaObject=NULL;
|
|
|
|
});
|
2018-10-03 14:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioInputAndroid::Start(){
|
|
|
|
MutexGuard guard(mutex);
|
2019-02-06 18:22:38 +00:00
|
|
|
jni::DoWithJNI([this](JNIEnv* env){
|
|
|
|
failed=!env->CallBooleanMethod(javaObject, startMethod);
|
|
|
|
});
|
2018-10-03 14:34:43 +00:00
|
|
|
running=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioInputAndroid::Stop(){
|
|
|
|
MutexGuard guard(mutex);
|
|
|
|
running=false;
|
2019-02-06 18:22:38 +00:00
|
|
|
jni::DoWithJNI([this](JNIEnv* env){
|
|
|
|
env->CallVoidMethod(javaObject, stopMethod);
|
|
|
|
});
|
2018-10-03 14:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioInputAndroid::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);
|
2019-02-06 18:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int AudioInputAndroid::GetEnabledEffects(){
|
|
|
|
return enabledEffects;
|
|
|
|
}
|