1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-04 02:27:46 +01:00
libtgvoip/os/android/AudioOutputAndroid.cpp
Grishka 5380aaba0d 2.2
- Refactored audio I/O to allow sharing a common context between input and output, for those OSes that require this
- Rewritten periodic operation handling to use a "run loop" thingy instead of an ugly loop formerly known as tick thread
- Fixed a bunch of compiler warnings (closes #13)
- Added automake so you no longer need to use the GYP file for standalone builds (closes #43)
2018-07-17 19:48:21 +03:00

108 lines
2.5 KiB
C++

//
// 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;
using namespace tgvoip;
using namespace tgvoip::audio;
jmethodID AudioOutputAndroid::initMethod=NULL;
jmethodID AudioOutputAndroid::releaseMethod=NULL;
jmethodID AudioOutputAndroid::startMethod=NULL;
jmethodID AudioOutputAndroid::stopMethod=NULL;
jclass AudioOutputAndroid::jniClass=NULL;
AudioOutputAndroid::AudioOutputAndroid(){
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);
env->CallVoidMethod(javaObject, initMethod, 48000, 16, 1, 960*2);
if(didAttach){
sharedJVM->DetachCurrentThread();
}
running=false;
}
AudioOutputAndroid::~AudioOutputAndroid(){
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();
}
}
void AudioOutputAndroid::Start(){
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;
}
void AudioOutputAndroid::Stop(){
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 AudioOutputAndroid::HandleCallback(JNIEnv* env, jbyteArray buffer){
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);
}
bool AudioOutputAndroid::IsPlaying(){
return false;
}