1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-12 09:09:38 +01:00
libtgvoip/os/android/AudioInputAndroid.cpp

74 lines
2.0 KiB
C++
Raw Normal View History

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"
#include "JNIUtilities.h"
2017-02-02 17:24:40 +01:00
extern JavaVM* sharedJVM;
using namespace tgvoip;
using namespace tgvoip::audio;
2017-02-02 17:24:40 +01:00
jmethodID AudioInputAndroid::initMethod=NULL;
jmethodID AudioInputAndroid::releaseMethod=NULL;
jmethodID AudioInputAndroid::startMethod=NULL;
jmethodID AudioInputAndroid::stopMethod=NULL;
jmethodID AudioInputAndroid::getEnabledEffectsMaskMethod=NULL;
jclass AudioInputAndroid::jniClass=NULL;
AudioInputAndroid::AudioInputAndroid(){
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;
}
AudioInputAndroid::~AudioInputAndroid(){
2017-03-30 16:06:59 +02:00
{
MutexGuard guard(mutex);
jni::DoWithJNI([this](JNIEnv* env){
env->CallVoidMethod(javaObject, releaseMethod);
env->DeleteGlobalRef(javaObject);
javaObject=NULL;
});
2017-02-02 17:24:40 +01:00
}
}
void AudioInputAndroid::Start(){
MutexGuard guard(mutex);
jni::DoWithJNI([this](JNIEnv* env){
failed=!env->CallBooleanMethod(javaObject, startMethod);
});
2017-02-02 17:24:40 +01:00
running=true;
}
void AudioInputAndroid::Stop(){
MutexGuard guard(mutex);
2017-02-02 17:24:40 +01:00
running=false;
jni::DoWithJNI([this](JNIEnv* env){
env->CallVoidMethod(javaObject, stopMethod);
});
2017-02-02 17:24:40 +01: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);
}
unsigned int AudioInputAndroid::GetEnabledEffects(){
return enabledEffects;
}