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

No more callbacks

This commit is contained in:
Daniil Gentili 2017-07-09 21:12:01 +02:00
parent 018c58569c
commit c553699249
7 changed files with 126 additions and 182 deletions

View File

@ -4,7 +4,6 @@
// you should have received with this source code distribution.
//
#include "AudioInputModule.h"
#include <stdio.h>
#include "../libtgvoip/logging.h"
@ -12,45 +11,57 @@
using namespace tgvoip;
using namespace tgvoip::audio;
AudioInputModule::AudioInputModule(std::string deviceID, void *controller){
AudioInputModule::AudioInputModule(std::string deviceID, void *controller)
{
wrapper = (VoIP *)((VoIPController *)controller)->implData;
wrapper->inputCreated = true;
}
AudioInputModule::~AudioInputModule(){
AudioInputModule::~AudioInputModule()
{
wrapper->inputConfigured = false;
wrapper->inputCreated = false;
}
void AudioInputModule::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels)
{
wrapper->inputSampleNumber = 960;
wrapper->inputSampleRate = sampleRate;
wrapper->inputBitsPerSample = bitsPerSample;
wrapper->inputChannels = channels;
wrapper->inputSamplePeriod = 1 / sampleRate * 1000000;
wrapper->inputWritePeriod = 1 / sampleRate * wrapper->inputSampleNumber * 1000000;
wrapper->inputSampleSize = wrapper->inputSampleNumber * wrapper->inputChannels * wrapper->inputBitsPerSample / 8;
void AudioInputModule::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels) {
wrapper->configureAudioInput(sampleRate, bitsPerSample, channels);
wrapper->inputConfigured = true;
}
void AudioInputModule::Start(){
if(running)
void AudioInputModule::Start()
{
if (wrapper->inputRunning)
return;
running = true;
wrapper->startInput();
wrapper->inputRunning = true;
}
void AudioInputModule::Stop(){
wrapper->stopInput();
running = false;
void AudioInputModule::Stop()
{
wrapper->inputRunning = false;
}
bool AudioInputModule::writeFrames(unsigned char *data){
if (running) {
bool AudioInputModule::writeSamples(unsigned char *data)
{
if (wrapper->inputRunning)
{
LOGE("STARTED");
InvokeCallback(data, (size_t)960*2);
InvokeCallback(data, wrapper->inputSampleSize);
return true;
} else {
}
else
{
LOGE("NOT STARTED");
return false;
}/*
if (!running) {
return false;
}*/
}
}
void AudioInputModule::EnumerateDevices(std::vector<AudioInputDevice>& devs) {
void AudioInputModule::EnumerateDevices(std::vector<AudioInputDevice> &devs)
{
}

View File

@ -26,13 +26,11 @@ public:
virtual void Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels);
virtual void Start();
virtual void Stop();
bool writeFrames(unsigned char *data);
bool writeSamples(unsigned char *data);
static void EnumerateDevices(std::vector<AudioInputDevice>& devs);
private:
VoIP *wrapper;
bool running;
};
}}

View File

@ -4,7 +4,6 @@
// you should have received with this source code distribution.
//
#include "AudioOutputModule.h"
#include <stdio.h>
#include "../libtgvoip/logging.h"
@ -12,44 +11,61 @@
using namespace tgvoip;
using namespace tgvoip::audio;
AudioOutputModule::AudioOutputModule(std::string deviceID, void *controller){
AudioOutputModule::AudioOutputModule(std::string deviceID, void *controller)
{
wrapper = (VoIP *)((VoIPController *)controller)->implData;
wrapper->outputCreated = true;
}
AudioOutputModule::~AudioOutputModule(){
AudioOutputModule::~AudioOutputModule()
{
wrapper->outputCreated = false;
wrapper->outputConfigured = false;
}
void AudioOutputModule::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels)
{
wrapper->outputSampleNumber = 960;
wrapper->outputSampleRate = sampleRate;
wrapper->outputBitsPerSample = bitsPerSample;
wrapper->outputChannels = channels;
wrapper->outputSamplePeriod = 1 / sampleRate * 1000000;
wrapper->outputWritePeriod = 1 / sampleRate * wrapper->outputSampleNumber * 1000000;
wrapper->outputSampleSize = wrapper->outputSampleNumber * wrapper->outputChannels * wrapper->outputBitsPerSample / 8;
void AudioOutputModule::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels){
wrapper->configureAudioOutput(sampleRate, bitsPerSample, channels);
wrapper->outputConfigured = true;
}
void AudioOutputModule::Start(){
if(running)
void AudioOutputModule::Start()
{
if (wrapper->outputRunning)
return;
running = true;
wrapper->startOutput();
wrapper->outputRunning = true;
}
void AudioOutputModule::Stop(){
if(!running)
void AudioOutputModule::Stop()
{
if (!wrapper->outputRunning)
return;
wrapper->stopOutput();
running = false;
wrapper->outputRunning = false;
}
bool AudioOutputModule::IsPlaying(){
return running;
bool AudioOutputModule::IsPlaying()
{
return wrapper->outputRunning;
}
float AudioOutputModule::GetLevel(){
return wrapper->getOutputLevel();
float AudioOutputModule::GetLevel()
{
return wrapper->outputLevel;
}
unsigned char * AudioOutputModule::readFrames() {
unsigned char *buf = (unsigned char *) emalloc(960*2);
InvokeCallback(buf, 960*2);
unsigned char *AudioOutputModule::readSamples()
{
unsigned char *buf = (unsigned char *)emalloc(wrapper->outputSampleSize);
InvokeCallback(buf, wrapper->outputSampleSize);
return buf;
}
void AudioOutputModule::EnumerateDevices(std::vector<AudioOutputDevice>& devs) {
void AudioOutputModule::EnumerateDevices(std::vector<AudioOutputDevice> &devs)
{
}

View File

@ -23,12 +23,11 @@ public:
virtual void Stop();
virtual bool IsPlaying() override;
virtual float GetLevel() override;
unsigned char * readFrames();
unsigned char * readSamples();
static void EnumerateDevices(std::vector<AudioOutputDevice>& devs);
private:
VoIP *wrapper;
bool running;
};
}}

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(function ($state) { var_dump($state); });
$VoIP = new \danog\MadelineProto\VoIP();
var_dump($VoIP);

135
main.cpp
View File

@ -26,29 +26,20 @@ using namespace tgvoip;
using namespace tgvoip::audio;
using namespace std;
extern "C" {
void* test(void* wrapper){
emalloc(sizeof(Php::Value));
return NULL;
}
}
void VoIP::__construct()
{
pthread_t a;
pthread_create(&a, NULL, test, this);
//PHPthis = (Php::Object)this;
/*
/*
PHPthis = (Php::Object)this;
madeline = params[0];
current_call = params[1];*/
/*
inst = new VoIPController();
inst->implData = (void *)this;
inst->implData = (void *) this;
inst->SetStateCallback([](tgvoip::VoIPController *controller, int state) {
((VoIP *)controller->implData)->updateConnectionState(state);
((VoIP *)controller->implData)->state = state;
});
*/
}
void VoIP::start()
@ -62,7 +53,7 @@ void VoIP::connect()
}
void VoIP::setEncryptionKey(Php::Parameters &params)
{
char *key = (char *)emalloc(256);
char *key = (char *) emalloc(256);
memcpy(key, params[0], 256);
inst->SetEncryptionKey(key, (bool)params[1]);
efree(key);
@ -97,40 +88,35 @@ void VoIP::setRemoteEndpoints(Php::Parameters &params)
inst->SetRemoteEndpoints(eps, params[1]);
}
void VoIP::release()
void VoIP::__destruct()
{
delete inst;
}
void VoIP::__destruct()
Php::Value VoIP::writeSamples(Php::Parameters &params)
{
this->release();
}
Php::Value VoIP::writeFrames(Php::Parameters &params)
{
AudioInputModule *in = (AudioInputModule *)(intptr_t)inst;
unsigned char *data = (unsigned char *) emalloc(960*2);
memcpy(data, params[0], 960*2);
bool res = in->writeFrames(data);
bool res = ((AudioInputModule *)(intptr_t)inst)->writeSamples(data);
efree(data);
return res;
}
Php::Value VoIP::readFrames()
Php::Value VoIP::readSamples()
{
AudioOutputModule *out = (AudioOutputModule *)(intptr_t) inst;
const char *frames = (const char *) out->readFrames();
Php::Value returnframes = frames;
efree((void *) frames);
return returnframes;
const char *samples = (const char *) ((AudioOutputModule *)(intptr_t) inst)->readSamples();
Php::Value returnsamples = samples;
efree((void *) samples);
return returnsamples;
}
Php::Value VoIP::getDebugString()
{
char buf[10240];
char *buf = (char *) emalloc(10240);
inst->GetDebugString(buf, 10240);
return buf;
Php::Value returnvalue = buf;
efree(buf);
return returnvalue;
}
void VoIP::setNetworkType(Php::Parameters &params)
@ -202,7 +188,7 @@ Php::Value VoIP::getStats()
{
voip_stats_t _stats;
inst->GetStats(&_stats);
Php::Array stats;
Php::Value stats;
stats["bytesSentWifi"] = (int64_t)_stats.bytesSentWifi;
stats["bytesSentMobile"] = (int64_t)_stats.bytesSentMobile;
stats["bytesRecvdWifi"] = (int64_t)_stats.bytesRecvdWifi;
@ -220,74 +206,10 @@ Php::Value VoIP::getDebugLog()
return inst->GetDebugLog();
}
void VoIP::updateConnectionState(int state)
{
Php::Value a = "a";
delete a;
//setStateMethod(state);
void VoIP::setOutputLevel(Php::Parameters &params) {
outputLevel = (double) params[0];
}
void VoIP::startInput()
{
//PHPthis.call("startInput");
}
void VoIP::startOutput()
{
//PHPthis.call("startOutput");
}
void VoIP::stopInput()
{
//PHPthis.call("stopInput");
}
void VoIP::stopOutput()
{
//PHPthis.call("startInput");
}
void VoIP::configureAudioInput(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels) {
inputSampleRate = sampleRate;
inputBitsPerSample = bitsPerSample;
inputChannels = channels;
inputSamplePeriod = 1/sampleRate*1000000;
inputWritePeriod = 1/sampleRate*960*1000000;
configuredInput = true;
}
void VoIP::configureAudioOutput(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels) {
outputSampleRate = sampleRate;
outputBitsPerSample = bitsPerSample;
outputChannels = channels;
outputSamplePeriod = 1/sampleRate;
outputWritePeriod = 1/sampleRate*960*1000000;
configuredOutput = true;
}
float VoIP::getOutputLevel() {
//return (double)PHPthis.call("getOutputLevel");
return 0.0;
}
Php::Value VoIP::getCallConfig() {
Php::Value result;
if (configuredInput) {
result["input"]["sampleRate"] = inputSampleRate;
result["input"]["bitsPerSample"] = inputBitsPerSample;
result["input"]["channels"] = inputChannels;
result["input"]["samplePeriod"] = inputSamplePeriod;
result["input"]["writePeriod"] = inputWritePeriod;
}
if (configuredOutput) {
result["output"]["sampleRate"] = outputSampleRate;
result["output"]["bitsPerSample"] = outputBitsPerSample;
result["output"]["channels"] = outputChannels;
result["output"]["samplePeriod"] = outputSamplePeriod;
result["output"]["writePeriod"] = outputWritePeriod;
}
return result;
}
extern "C" {
/**
@ -317,12 +239,14 @@ PHPCPP_EXPORT void *get_module()
voip.method("getOutputLevel");
*/
voip.method<&VoIP::getCallConfig>("getCallConfig");
voip.method<&VoIP::__destruct>("__destruct", Php::Public | Php::Final);
voip.method<&VoIP::__construct>("__construct", Php::Public | Php::Final);
voip.method<&VoIP::setEncryptionKey>("setEncryptionKey", Php::Public | Php::Final, {
Php::ByVal("key", Php::Type::String), Php::ByVal("isOutgoing", Php::Type::Bool),
});
voip.method<&VoIP::setEncryptionKey>("setOutputLevel", Php::Public | Php::Final, {
Php::ByVal("level", Php::Type::Float),
});
voip.method<&VoIP::setNetworkType>("setNetworkType", Php::Public | Php::Final, {
Php::ByVal("type", Php::Type::Numeric),
});
@ -348,12 +272,11 @@ PHPCPP_EXPORT void *get_module()
voip.method<&VoIP::getVersion>("getVersion", Php::Public | Php::Final);
voip.method<&VoIP::getDebugString>("getDebugString", Php::Public | Php::Final);
voip.method<&VoIP::getStats>("getStats", Php::Public | Php::Final);
voip.method<&VoIP::release>("release", Php::Public | Php::Final);
voip.method<&VoIP::start>("start", Php::Public | Php::Final);
voip.method<&VoIP::connect>("connect", Php::Public | Php::Final);
voip.method<&VoIP::readFrames>("readFrames", Php::Public | Php::Final);
voip.method<&VoIP::writeFrames>("writeFrames", Php::Public | Php::Final, {Php::ByVal("frames", Php::Type::String)});
voip.method<&VoIP::readSamples>("readSamples", Php::Public | Php::Final);
voip.method<&VoIP::writeSamples>("writeSamples", Php::Public | Php::Final, {Php::ByVal("samples", Php::Type::String)});
voip.constant("STATE_WAIT_INIT", STATE_WAIT_INIT);
voip.constant("STATE_WAIT_INIT_ACK", STATE_WAIT_INIT_ACK);
@ -386,10 +309,6 @@ PHPCPP_EXPORT void *get_module()
voip.constant("PROXY_NONE", PROXY_NONE);
voip.constant("PROXY_SOCKS5", PROXY_SOCKS5);
voip.constant("FRAME_NUMBER", 960);
voip.constant("FRAME_SIZE", 960*2);
Php::Namespace danog("danog");
Php::Namespace MadelineProto("MadelineProto");

47
main.h
View File

@ -18,7 +18,6 @@ If not, see <http://www.gnu.org/licenses/>.
#include <ext/standard/info.h>
#include <phpcpp.h>
using namespace tgvoip;
using namespace tgvoip::audio;
@ -41,9 +40,9 @@ public:
void release();
Php::Value writeFrames(Php::Parameters &params);
Php::Value writeSamples(Php::Parameters &params);
Php::Value readFrames();
Php::Value readSamples();
Php::Value getDebugString();
@ -71,39 +70,41 @@ public:
void updateConnectionState(int state);
void startInput();
void startOutput();
void stopInput();
void stopOutput();
void configureAudioInput(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels);
void configureAudioOutput(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels);
float getOutputLevel();
Php::Value getCallConfig();
void setOutputLevel(Php::Parameters &params);
/*
Php::Value madeline;
Php::Value current_call;
*/
int inputBitsPerSample;
int outputBitsPerSample;
int inputSampleRate;
int outputSampleRate;
int inputChannels;
int outputChannels;
int inputSamplePeriod;
int outputSamplePeriod;
int inputWritePeriod;
int inputSampleNumber;
int inputSampleSize;
bool inputCreated = false;
bool inputConfigured = false;
bool inputRunning = false;
int outputBitsPerSample;
int outputSampleRate;
int outputChannels;
int outputSamplePeriod;
int outputWritePeriod;
int outputSampleNumber;
int outputSampleSize;
bool configuredInput = false;
bool configuredOutput = false;
bool outputCreated = false;
bool outputConfigured = false;
bool outputRunning = false;
float outputLevel = 0.0;
int state;
private:
VoIPController *inst;
};