mirror of
https://github.com/danog/php-libtgvoip.git
synced 2024-12-11 16:59:46 +01:00
126 lines
2.8 KiB
C++
126 lines
2.8 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 "AudioOutputPHP.h"
|
||
|
#include <stdio.h>
|
||
|
#include "libtgvoip/logging.h"
|
||
|
|
||
|
extern JavaVM* sharedJVM;
|
||
|
|
||
|
using namespace tgvoip;
|
||
|
using namespace tgvoip::audio;
|
||
|
|
||
|
jmethodID AudioOutputPHP::initMethod=NULL;
|
||
|
jmethodID AudioOutputPHP::releaseMethod=NULL;
|
||
|
jmethodID AudioOutputPHP::startMethod=NULL;
|
||
|
jmethodID AudioOutputPHP::stopMethod=NULL;
|
||
|
jclass AudioOutputPHP::jniClass=NULL;
|
||
|
|
||
|
AudioOutputPHP::AudioOutputPHP(){
|
||
|
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);
|
||
|
|
||
|
if(didAttach){
|
||
|
sharedJVM->DetachCurrentThread();
|
||
|
}
|
||
|
running=false;
|
||
|
}
|
||
|
|
||
|
AudioOutputPHP::~AudioOutputPHP(){
|
||
|
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 AudioOutputPHP::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels){
|
||
|
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, initMethod, sampleRate, bitsPerSample, channels, 960*2);
|
||
|
|
||
|
if(didAttach){
|
||
|
sharedJVM->DetachCurrentThread();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void AudioOutputPHP::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 AudioOutputPHP::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 AudioOutputPHP::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 AudioOutputPHP::IsPlaying(){
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
float AudioOutputPHP::GetLevel(){
|
||
|
return 0;
|
||
|
}
|