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>
|
2020-01-22 12:43:51 +01:00
|
|
|
#include "../../tools/logging.h"
|
2018-11-21 19:22:31 +01:00
|
|
|
#include "JNIUtilities.h"
|
2017-02-02 17:24:40 +01:00
|
|
|
|
|
|
|
extern JavaVM* sharedJVM;
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
using namespace tgvoip;
|
|
|
|
using namespace tgvoip::audio;
|
2017-02-02 17:24:40 +01:00
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
jmethodID AudioInputAndroid::initMethod=NULL;
|
|
|
|
jmethodID AudioInputAndroid::releaseMethod=NULL;
|
|
|
|
jmethodID AudioInputAndroid::startMethod=NULL;
|
|
|
|
jmethodID AudioInputAndroid::stopMethod=NULL;
|
2018-11-21 19:22:31 +01:00
|
|
|
jmethodID AudioInputAndroid::getEnabledEffectsMaskMethod=NULL;
|
2017-04-28 13:17:56 +02:00
|
|
|
jclass AudioInputAndroid::jniClass=NULL;
|
|
|
|
|
|
|
|
AudioInputAndroid::AudioInputAndroid(){
|
2018-11-21 19:22:31 +01: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);
|
|
|
|
});
|
2017-02-02 17:24:40 +01:00
|
|
|
running=false;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
AudioInputAndroid::~AudioInputAndroid(){
|
2017-03-30 16:06:59 +02:00
|
|
|
{
|
2017-04-28 13:17:56 +02:00
|
|
|
MutexGuard guard(mutex);
|
2018-11-21 19:22:31 +01:00
|
|
|
jni::DoWithJNI([this](JNIEnv* env){
|
|
|
|
env->CallVoidMethod(javaObject, releaseMethod);
|
|
|
|
env->DeleteGlobalRef(javaObject);
|
|
|
|
javaObject=NULL;
|
|
|
|
});
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioInputAndroid::Start(){
|
|
|
|
MutexGuard guard(mutex);
|
2018-11-21 19:22:31 +01:00
|
|
|
jni::DoWithJNI([this](JNIEnv* env){
|
|
|
|
failed=!env->CallBooleanMethod(javaObject, startMethod);
|
|
|
|
});
|
2017-02-02 17:24:40 +01:00
|
|
|
running=true;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioInputAndroid::Stop(){
|
|
|
|
MutexGuard guard(mutex);
|
2017-02-02 17:24:40 +01:00
|
|
|
running=false;
|
2018-11-21 19:22:31 +01:00
|
|
|
jni::DoWithJNI([this](JNIEnv* env){
|
|
|
|
env->CallVoidMethod(javaObject, stopMethod);
|
|
|
|
});
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioInputAndroid::HandleCallback(JNIEnv* env, jobject buffer){
|
2017-02-02 17:24:40 +01:00
|
|
|
if(!running)
|
|
|
|
return;
|
|
|
|
unsigned char* buf=(unsigned char*) env->GetDirectBufferAddress(buffer);
|
|
|
|
size_t len=(size_t) env->GetDirectBufferCapacity(buffer);
|
|
|
|
InvokeCallback(buf, len);
|
2018-11-21 19:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int AudioInputAndroid::GetEnabledEffects(){
|
|
|
|
return enabledEffects;
|
|
|
|
}
|