1
0
mirror of https://github.com/danog/php-libtgvoip.git synced 2024-11-26 11:54:44 +01:00

Started working on custom audio modules

This commit is contained in:
Daniil Gentili 2017-06-18 18:32:20 +01:00
parent 184512ce94
commit f5dd13a820
10 changed files with 333 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.so
*.o
*.swp

2
.gitmodules vendored
View File

@ -1,3 +1,3 @@
[submodule "libtgvoip"]
path = libtgvoip
url = https://github.com/grishka/libtgvoip
url = https://github.com/danog/libtgvoip

123
AudioInputPHP.cpp Normal file
View File

@ -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 <stdio.h>
#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, "<init>", "(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);
}

38
AudioInputPHP.h Normal file
View File

@ -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 &params);
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

125
AudioOutputPHP.cpp Normal file
View File

@ -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 <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;
}

38
AudioOutputPHP.h Normal file
View File

@ -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

View File

@ -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

View File

@ -10,6 +10,6 @@ You should have received a copy of the GNU General Public License along with php
If not, see <http://www.gnu.org/licenses/>.
*/
$VoIP = new \danog\MadelineProto\VoIP();
$VoIP = new \danog\MadelineProto\VoIP(function ($state) { var_dump($state); });
var_dump($VoIP);

@ -1 +1 @@
Subproject commit ba19486ef4ad391f42e2e2e04e0b8b9d59708831
Subproject commit 6344cae582c8217fc92fdf7c7505a697acf2756e

View File

@ -34,6 +34,7 @@ If not, see <http://www.gnu.org/licenses/>.
#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];
}
inst=new VoIPController();
inst->implData = static_cast<void*>(this);
inst->SetStateCallback([](tgvoip::VoIPController *controller, int state) {
@ -230,10 +229,8 @@ public:
}
void updateConnectionState(VoIPController* cntrlr, int state) {
if (setStateMethod) {
setStateMethod(state);
}
}
private:
VoIPController* inst;
@ -260,7 +257,7 @@ extern "C" {
// description of the class so that PHP knows which methods are accessible
Php::Class<VoIP> 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),