From f5dd13a8208aa10665cd9ecf51661db95c336a34 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 18 Jun 2017 18:32:20 +0100 Subject: [PATCH] Started working on custom audio modules --- .gitignore | 1 + .gitmodules | 2 +- AudioInputPHP.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++++ AudioInputPHP.h | 38 ++++++++++++++ AudioOutputPHP.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++ AudioOutputPHP.h | 38 ++++++++++++++ Makefile | 2 +- example.php | 2 +- libtgvoip | 2 +- main.cpp | 11 ++-- 10 files changed, 333 insertions(+), 11 deletions(-) create mode 100644 AudioInputPHP.cpp create mode 100644 AudioInputPHP.h create mode 100644 AudioOutputPHP.cpp create mode 100644 AudioOutputPHP.h diff --git a/.gitignore b/.gitignore index cc78368..5b49304 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.so *.o +*.swp diff --git a/.gitmodules b/.gitmodules index a9bfed0..345da44 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "libtgvoip"] path = libtgvoip - url = https://github.com/grishka/libtgvoip + url = https://github.com/danog/libtgvoip diff --git a/AudioInputPHP.cpp b/AudioInputPHP.cpp new file mode 100644 index 0000000..f3497a0 --- /dev/null +++ b/AudioInputPHP.cpp @@ -0,0 +1,123 @@ +// +// 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 "AudioInputPHP.h" +#include +#include "libtgvoip/logging.h" + +extern JavaVM* sharedJVM; + +using namespace tgvoip; +using namespace tgvoip::audio; + +jmethodID AudioInputPHP::initMethod=NULL; +jmethodID AudioInputPHP::releaseMethod=NULL; +jmethodID AudioInputPHP::startMethod=NULL; +jmethodID AudioInputPHP::stopMethod=NULL; +jclass AudioInputPHP::jniClass=NULL; + +AudioInputPHP::AudioInputPHP(){ + 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, "", "(J)V"); + jobject obj=env->NewObject(jniClass, ctor, (jlong)(intptr_t)this); + javaObject=env->NewGlobalRef(obj); + + if(didAttach){ + sharedJVM->DetachCurrentThread(); + } + running=false; + init_mutex(mutex); +} + +AudioInputPHP::~AudioInputPHP(){ + { + MutexGuard guard(mutex); + 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(); + } + } + free_mutex(mutex); +} + +void AudioInputPHP::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels){ + MutexGuard guard(mutex); + 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 AudioInputPHP::Start(){ + MutexGuard guard(mutex); + JNIEnv* env=NULL; + bool didAttach=false; + sharedJVM->GetEnv((void**) &env, JNI_VERSION_1_6); + if(!env){ + sharedJVM->AttachCurrentThread(&env, NULL); + didAttach=true; + } + + failed=!env->CallBooleanMethod(javaObject, startMethod); + + if(didAttach){ + sharedJVM->DetachCurrentThread(); + } + running=true; +} + +void AudioInputPHP::Stop(){ + MutexGuard guard(mutex); + 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 AudioInputPHP::HandleCallback(JNIEnv* env, jobject buffer){ + if(!running) + return; + unsigned char* buf=(unsigned char*) env->GetDirectBufferAddress(buffer); + size_t len=(size_t) env->GetDirectBufferCapacity(buffer); + InvokeCallback(buf, len); +} diff --git a/AudioInputPHP.h b/AudioInputPHP.h new file mode 100644 index 0000000..ba0090a --- /dev/null +++ b/AudioInputPHP.h @@ -0,0 +1,38 @@ +// +// 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. +// + +#ifndef LIBTGVOIP_AUDIOINPUTPHP_H +#define LIBTGVOIP_AUDIOINPUTPHP_H + +#include "phpcpp.h" +#include "libtgvoip/audio/AudioInput.h" +#include "libtgvoip/threading.h" + +namespace tgvoip{ namespace audio{ +class AudioInputPHP : public AudioInput{ + +public: + AudioInputPHP(); + virtual ~AudioInputPHP(); + virtual void Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels); + virtual void Start(); + virtual void Stop(); + void HandleCallback(PHP::Parameters ¶ms); + static jmethodID initMethod; + static jmethodID releaseMethod; + static jmethodID startMethod; + static jmethodID stopMethod; + static jclass jniClass; + +private: + jobject javaObject; + bool running; + tgvoip_mutex_t mutex; + +}; +}} + +#endif //LIBTGVOIP_AUDIOINPUTPHP_H diff --git a/AudioOutputPHP.cpp b/AudioOutputPHP.cpp new file mode 100644 index 0000000..8588e37 --- /dev/null +++ b/AudioOutputPHP.cpp @@ -0,0 +1,125 @@ +// +// 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 +#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, "", "(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; +} diff --git a/AudioOutputPHP.h b/AudioOutputPHP.h new file mode 100644 index 0000000..8584b26 --- /dev/null +++ b/AudioOutputPHP.h @@ -0,0 +1,38 @@ +// +// 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. +// + +#ifndef LIBTGVOIP_AUDIOOUTPUTPHP_H +#define LIBTGVOIP_AUDIOOUTPUTPHP_H + +#include "libtgvoip/audio/AudioOutput.h" + +namespace tgvoip{ namespace audio{ +class AudioOutputPHP : public AudioOutput{ + +public: + + AudioOutputPHP(); + virtual ~AudioOutputPHP(); + virtual void Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels); + virtual void Start(); + virtual void Stop(); + virtual bool IsPlaying() override; + virtual float GetLevel() override; + void HandleCallback(JNIEnv* env, jbyteArray buffer); + static jmethodID initMethod; + static jmethodID releaseMethod; + static jmethodID startMethod; + static jmethodID stopMethod; + static jclass jniClass; + +private: + jobject javaObject; + bool running; + +}; +}} + +#endif //LIBTGVOIP_AUDIOOUTPUTPHP_H diff --git a/Makefile b/Makefile index 97d9239..2b6948c 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ include $(CLEAR_VARS) -COMPILER_FLAGS = -Wall -O3 -I/usr/include/opus -I/usr/include/openssl -Ilibtgvoip/webrtc_dsp -c -std=c++11 -fpic -DANDROID -finline-functions -ffast-math -Os -fno-strict-aliasing -DUSE_KISS_FFT -DFIXED_POINT -DTGVOIP_USE_CUSTOM_CRYPTO -o +COMPILER_FLAGS = -Wall -O3 -I/usr/include/opus -I/usr/include/openssl -Ilibtgvoip/webrtc_dsp -c -std=c++11 -fpic -DANDROID -finline-functions -ffast-math -Os -fno-strict-aliasing -DUSE_KISS_FFT -DFIXED_POINT -DTGVOIP_USE_CUSTOM_CRYPTO -DPHP_LIBTGVOIP -o #COMPILER_FLAGS = -Wall -c -O2 -std=c++11 -fpic -I/usr/include/opus -o LINKER_FLAGS = -shared LINKER_DEPENDENCIES = -lphpcpp -I/usr/include/opus diff --git a/example.php b/example.php index 9a996ce..2c2955e 100644 --- a/example.php +++ b/example.php @@ -10,6 +10,6 @@ You should have received a copy of the GNU General Public License along with php If not, see . */ -$VoIP = new \danog\MadelineProto\VoIP(); +$VoIP = new \danog\MadelineProto\VoIP(function ($state) { var_dump($state); }); var_dump($VoIP); diff --git a/libtgvoip b/libtgvoip index ba19486..6344cae 160000 --- a/libtgvoip +++ b/libtgvoip @@ -1 +1 @@ -Subproject commit ba19486ef4ad391f42e2e2e04e0b8b9d59708831 +Subproject commit 6344cae582c8217fc92fdf7c7505a697acf2756e diff --git a/main.cpp b/main.cpp index b27f79e..acb0ad4 100644 --- a/main.cpp +++ b/main.cpp @@ -34,6 +34,7 @@ If not, see . #include "libtgvoip/CongestionControl.cpp" #include "libtgvoip/VoIPServerConfig.cpp" #include "libtgvoip/NetworkSocket.cpp" +#include "libtgvoip/os/posix/NetworkSocketPosix.cpp" //#include "libtgvoip/os/android/AudioInputOpenSLES.cpp" //#include "libtgvoip/os/android/AudioOutputOpenSLES.cpp" @@ -69,9 +70,7 @@ public: impl_data_android_t* impl=(impl_data_android_t*) malloc(sizeof(impl_data_android_t)); impl->javaObject=env->NewGlobalRef(thiz); */ - if (params.size() == 1) { - setStateMethod = params[0]; - } + setStateMethod = params[0]; inst=new VoIPController(); inst->implData = static_cast(this); inst->SetStateCallback([](tgvoip::VoIPController *controller, int state) { @@ -230,9 +229,7 @@ public: } void updateConnectionState(VoIPController* cntrlr, int state) { - if (setStateMethod) { - setStateMethod(state); - } + setStateMethod(state); } private: @@ -260,7 +257,7 @@ extern "C" { // description of the class so that PHP knows which methods are accessible Php::Class voip("VoIP"); voip.method<&VoIP::__construct> ("__construct", { - Php::ByVal("setStateCallable", Php::Type::Callable, false), + Php::ByVal("setStateCallable", Php::Type::Callable) }); voip.method<&VoIP::setEncryptionKey> ("setEncryptionKey", { Php::ByVal("key", Php::Type::String),