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 "AudioOutputAndroid.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "../../logging.h"
|
|
|
|
|
|
|
|
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 AudioOutputAndroid::initMethod=NULL;
|
|
|
|
jmethodID AudioOutputAndroid::releaseMethod=NULL;
|
|
|
|
jmethodID AudioOutputAndroid::startMethod=NULL;
|
|
|
|
jmethodID AudioOutputAndroid::stopMethod=NULL;
|
|
|
|
jclass AudioOutputAndroid::jniClass=NULL;
|
|
|
|
|
|
|
|
AudioOutputAndroid::AudioOutputAndroid(){
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
jmethodID ctor=env->GetMethodID(jniClass, "<init>", "(J)V");
|
|
|
|
jobject obj=env->NewObject(jniClass, ctor, (jlong)(intptr_t)this);
|
|
|
|
javaObject=env->NewGlobalRef(obj);
|
|
|
|
|
2018-07-17 18:48:21 +02:00
|
|
|
env->CallVoidMethod(javaObject, initMethod, 48000, 16, 1, 960*2);
|
|
|
|
|
2017-02-02 17:24:40 +01:00
|
|
|
if(didAttach){
|
|
|
|
sharedJVM->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
running=false;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
AudioOutputAndroid::~AudioOutputAndroid(){
|
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, releaseMethod);
|
|
|
|
env->DeleteGlobalRef(javaObject);
|
|
|
|
javaObject=NULL;
|
|
|
|
|
|
|
|
if(didAttach){
|
|
|
|
sharedJVM->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioOutputAndroid::Start(){
|
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, startMethod);
|
|
|
|
|
|
|
|
if(didAttach){
|
|
|
|
sharedJVM->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
running=true;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioOutputAndroid::Stop(){
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioOutputAndroid::HandleCallback(JNIEnv* env, jbyteArray buffer){
|
2017-02-02 17:24:40 +01:00
|
|
|
if(!running)
|
|
|
|
return;
|
|
|
|
unsigned char* buf=(unsigned char*) env->GetByteArrayElements(buffer, NULL);
|
|
|
|
size_t len=(size_t) env->GetArrayLength(buffer);
|
|
|
|
InvokeCallback(buf, len);
|
|
|
|
env->ReleaseByteArrayElements(buffer, (jbyte *) buf, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
bool AudioOutputAndroid::IsPlaying(){
|
2018-11-21 19:22:31 +01:00
|
|
|
return running;
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|