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

Finished refactoring

This commit is contained in:
Daniil Gentili 2017-07-10 00:06:50 +02:00
parent 35ada1b110
commit 3357ed8206
4 changed files with 111 additions and 48 deletions

View File

@ -14,13 +14,12 @@ using namespace tgvoip::audio;
AudioInputModule::AudioInputModule(std::string deviceID, void *controller)
{
wrapper = (VoIP *)((VoIPController *)controller)->implData;
wrapper->inputCreated = true;
wrapper->inputState = AUDIO_STATE_CREATED;
}
AudioInputModule::~AudioInputModule()
{
wrapper->inputConfigured = false;
wrapper->inputCreated = false;
wrapper->inputState = AUDIO_STATE_NONE;
}
void AudioInputModule::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels)
@ -31,28 +30,30 @@ void AudioInputModule::Configure(uint32_t sampleRate, uint32_t bitsPerSample, ui
wrapper->inputChannels = channels;
wrapper->inputSamplePeriod = 1 / sampleRate * 1000000;
wrapper->inputWritePeriod = 1 / sampleRate * wrapper->inputSampleNumber * 1000000;
wrapper->inputSampleSize = wrapper->inputSampleNumber * wrapper->inputChannels * wrapper->inputBitsPerSample / 8;
wrapper->inputSamplesSize = wrapper->inputSampleNumber * wrapper->inputChannels * wrapper->inputBitsPerSample / 8;
wrapper->inputConfigured = true;
wrapper->inputState = AUDIO_STATE_CONFIGURED;
}
void AudioInputModule::Start()
{
if (wrapper->inputRunning)
if (wrapper->inputState == AUDIO_STATE_RUNNING)
return;
wrapper->inputRunning = true;
wrapper->inputState = AUDIO_STATE_RUNNING;
}
void AudioInputModule::Stop()
{
wrapper->inputRunning = false;
if (wrapper->inputState != AUDIO_STATE_RUNNING)
return;
wrapper->inputState = AUDIO_STATE_CONFIGURED;
}
bool AudioInputModule::writeSamples(unsigned char *data)
{
if (wrapper->inputRunning)
if (wrapper->inputState == AUDIO_STATE_RUNNING)
{
LOGE("STARTED");
InvokeCallback(data, wrapper->inputSampleSize);
InvokeCallback(data, wrapper->inputSamplesSize);
return true;
}
else

View File

@ -14,12 +14,11 @@ using namespace tgvoip::audio;
AudioOutputModule::AudioOutputModule(std::string deviceID, void *controller)
{
wrapper = (VoIP *)((VoIPController *)controller)->implData;
wrapper->outputCreated = true;
wrapper->outputState = AUDIO_STATE_CREATED;
}
AudioOutputModule::~AudioOutputModule()
{
wrapper->outputCreated = false;
wrapper->outputConfigured = false;
wrapper->outputState = AUDIO_STATE_NONE;
}
void AudioOutputModule::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels)
@ -30,28 +29,28 @@ void AudioOutputModule::Configure(uint32_t sampleRate, uint32_t bitsPerSample, u
wrapper->outputChannels = channels;
wrapper->outputSamplePeriod = 1 / sampleRate * 1000000;
wrapper->outputWritePeriod = 1 / sampleRate * wrapper->outputSampleNumber * 1000000;
wrapper->outputSampleSize = wrapper->outputSampleNumber * wrapper->outputChannels * wrapper->outputBitsPerSample / 8;
wrapper->outputSamplesSize = wrapper->outputSampleNumber * wrapper->outputChannels * wrapper->outputBitsPerSample / 8;
wrapper->outputConfigured = true;
wrapper->outputState = AUDIO_STATE_CONFIGURED;
}
void AudioOutputModule::Start()
{
if (wrapper->outputRunning)
if (wrapper->outputState == AUDIO_STATE_RUNNING)
return;
wrapper->outputRunning = true;
wrapper->outputState = AUDIO_STATE_RUNNING;
}
void AudioOutputModule::Stop()
{
if (!wrapper->outputRunning)
if (wrapper->outputState != AUDIO_STATE_RUNNING)
return;
wrapper->outputRunning = false;
wrapper->outputState = AUDIO_STATE_CONFIGURED;
}
bool AudioOutputModule::IsPlaying()
{
return wrapper->outputRunning;
return wrapper->outputState == AUDIO_STATE_RUNNING;
}
float AudioOutputModule::GetLevel()
@ -61,8 +60,8 @@ float AudioOutputModule::GetLevel()
unsigned char *AudioOutputModule::readSamples()
{
unsigned char *buf = (unsigned char *)emalloc(wrapper->outputSampleSize);
InvokeCallback(buf, wrapper->outputSampleSize);
unsigned char *buf = (unsigned char *)emalloc(wrapper->outputSamplesSize);
InvokeCallback(buf, wrapper->outputSamplesSize);
return buf;
}

View File

@ -95,8 +95,8 @@ void VoIP::__destruct()
Php::Value VoIP::writeSamples(Php::Parameters &params)
{
unsigned char *data = (unsigned char *) emalloc(inputSampleSize);
memcpy(data, params[0], inputSampleSize);
unsigned char *data = (unsigned char *) emalloc(inputSamplesSize);
memcpy(data, params[0], inputSamplesSize);
bool res = ((AudioInputModule *)(intptr_t)inst)->writeSamples(data);
efree(data);
return res;
@ -210,6 +210,54 @@ void VoIP::setOutputLevel(Php::Parameters &params) {
outputLevel = (double) params[0];
}
Php::Value VoIP::getState()
{
return state;
}
Php::Value VoIP::getOutputState()
{
return outputState;
}
Php::Value VoIP::getInputState()
{
return inputState;
}
Php::Value VoIP::getOutputParams()
{
Php::Value params;
params["bitsPerSample"] = outputBitsPerSample;
params["sampleRate"] = outputSampleRate;
params["channels"] = outputChannels;
params["samplePeriod"] = outputSamplePeriod;
params["writePeriod"] = outputWritePeriod;
params["sampleNumber"] = outputSampleNumber;
params["samplesSize"] = outputSamplesSize;
params["level"] = outputLevel;
return params;
}
Php::Value VoIP::getInputParams()
{
Php::Value params;
params["bitsPerSample"] = inputBitsPerSample;
params["sampleRate"] = inputSampleRate;
params["channels"] = inputChannels;
params["samplePeriod"] = inputSamplePeriod;
params["writePeriod"] = inputWritePeriod;
params["sampleNumber"] = inputSampleNumber;
params["samplesSize"] = inputSamplesSize;
return params;
}
extern "C" {
/**
@ -239,6 +287,13 @@ PHPCPP_EXPORT void *get_module()
voip.method("getOutputLevel");
*/
voip.method<&VoIP::getState>("getState", Php::Public | Php::Final);
voip.method<&VoIP::getOutputState>("getOutputState", Php::Public | Php::Final);
voip.method<&VoIP::getInputState>("getInputState", Php::Public | Php::Final);
voip.method<&VoIP::getOutputParams>("getOutputParams", Php::Public | Php::Final);
voip.method<&VoIP::getInputParams>("getInputParams", Php::Public | Php::Final);
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, {
@ -310,6 +365,11 @@ PHPCPP_EXPORT void *get_module()
voip.constant("PROXY_NONE", PROXY_NONE);
voip.constant("PROXY_SOCKS5", PROXY_SOCKS5);
voip.constant("AUDIO_STATE_NONE", AUDIO_STATE_NONE);
voip.constant("AUDIO_STATE_CREATED", AUDIO_STATE_CREATED);
voip.constant("AUDIO_STATE_CONFIGURED", AUDIO_STATE_CONFIGURED);
voip.constant("AUDIO_STATE_RUNNING", AUDIO_STATE_RUNNING);
Php::Namespace danog("danog");
Php::Namespace MadelineProto("MadelineProto");

51
main.h
View File

@ -18,12 +18,17 @@ If not, see <http://www.gnu.org/licenses/>.
#include <ext/standard/info.h>
#include <phpcpp.h>
#define AUDIO_STATE_NONE -1
#define AUDIO_STATE_CREATED 0
#define AUDIO_STATE_CONFIGURED 1
#define AUDIO_STATE_RUNNING 2
using namespace tgvoip;
using namespace tgvoip::audio;
class VoIP : public Php::Base {
public:
class VoIP : public Php::Base
{
public:
void __construct();
void __destruct();
@ -36,14 +41,12 @@ public:
void setRemoteEndpoints(Php::Parameters &params);
void release();
Php::Value writeSamples(Php::Parameters &params);
Php::Value readSamples();
Php::Value getDebugString();
void setNetworkType(Php::Parameters &params);
@ -67,45 +70,45 @@ public:
void setProxy(Php::Parameters &params);
Php::Value getDebugLog();
void updateConnectionState(int state);
void setOutputLevel(Php::Parameters &params);
Php::Value getState();
Php::Value getOutputState();
Php::Value getInputState();
Php::Value getOutputParams();
Php::Value getInputParams();
/*
Php::Value madeline;
Php::Value current_call;
*/
int inputBitsPerSample;
int inputSampleRate;
int inputChannels;
int inputSamplePeriod;
int inputWritePeriod;
int inputSampleNumber;
int inputSampleSize;
bool inputCreated = false;
bool inputConfigured = false;
bool inputRunning = false;
int inputSampleNumber;
int inputSamplesSize;
int inputState = AUDIO_STATE_NONE;
int outputBitsPerSample;
int outputSampleRate;
int outputChannels;
int outputSamplePeriod;
int outputWritePeriod;
int outputSampleNumber;
int outputSampleSize;
bool outputCreated = false;
bool outputConfigured = false;
bool outputRunning = false;
int outputSampleNumber;
int outputSamplesSize;
float outputLevel = 0.0;
int outputState = AUDIO_STATE_NONE;
int state = STATE_CREATED;
private:
private:
VoIPController *inst;
};