1
0
mirror of https://github.com/danog/php-libtgvoip.git synced 2024-12-02 17:28:12 +01:00
php-libtgvoip/audio/AudioOutputModule.cpp

123 lines
3.5 KiB
C++
Raw Normal View History

2017-07-04 15:50:02 +02:00
//
// 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 "AudioOutputModule.h"
#include <stdio.h>
#include "../libtgvoip/logging.h"
2017-07-13 18:03:33 +02:00
#include <unistd.h>
#include <phpcpp.h>
2017-07-04 15:50:02 +02:00
using namespace tgvoip;
using namespace tgvoip::audio;
2017-07-13 18:03:33 +02:00
AudioOutputModule::AudioOutputModule(std::string deviceID, VoIPController *controller)
2017-07-09 21:12:01 +02:00
{
2017-07-23 17:49:47 +02:00
wrapper = (VoIP *)(controller->implData);
wrapper->out = this;
wrapper->outputState = AUDIO_STATE_CREATED;
2017-07-04 15:50:02 +02:00
}
2017-07-09 21:12:01 +02:00
AudioOutputModule::~AudioOutputModule()
{
2017-07-23 17:49:47 +02:00
wrapper->outputState = AUDIO_STATE_NONE;
wrapper->out = NULL;
if (receiverThread)
{
LOGD("before join receiverThread");
join_thread(receiverThread);
}
2017-07-13 18:03:33 +02:00
}
2017-07-11 21:11:23 +02:00
2017-07-09 21:12:01 +02:00
void AudioOutputModule::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels)
{
2017-07-23 17:49:47 +02:00
outputSampleNumber = 960;
outputSampleRate = sampleRate;
outputBitsPerSample = bitsPerSample;
outputChannels = channels;
outputSamplePeriod = 1.0 / sampleRate * 1000000;
outputWritePeriod = 1.0 / sampleRate * outputSampleNumber * 1000000;
outputSamplePeriodSec = 1.0 / sampleRate;
outputWritePeriodSec = 1.0 / sampleRate * outputSampleNumber;
outputSamplesSize = outputSampleNumber * outputChannels * outputBitsPerSample / 8;
outputCSamplesSize = outputSampleNumber * outputChannels * outputBitsPerSample / 8 * sizeof(unsigned char);
wrapper->outputState = AUDIO_STATE_CONFIGURED;
2017-07-04 15:50:02 +02:00
}
2017-07-09 21:12:01 +02:00
void AudioOutputModule::Start()
{
2017-07-23 17:49:47 +02:00
if (wrapper->outputState == AUDIO_STATE_RUNNING)
return;
wrapper->outputState = AUDIO_STATE_RUNNING;
LOGE("STARTING RECEIVER THREAD");
start_thread(receiverThread, StartReceiverThread, this);
set_thread_priority(receiverThread, get_thread_max_priority());
set_thread_name(receiverThread, "voip-receiver");
2017-07-04 15:50:02 +02:00
}
2017-07-09 21:12:01 +02:00
void AudioOutputModule::Stop()
{
2017-07-23 17:49:47 +02:00
if (wrapper->outputState != AUDIO_STATE_RUNNING)
return;
wrapper->outputState = AUDIO_STATE_CONFIGURED;
2017-07-04 15:50:02 +02:00
}
2017-07-09 21:12:01 +02:00
bool AudioOutputModule::IsPlaying()
{
2017-07-23 17:49:47 +02:00
return wrapper->outputState == AUDIO_STATE_RUNNING;
2017-07-04 15:50:02 +02:00
}
2017-07-09 21:12:01 +02:00
float AudioOutputModule::GetLevel()
{
2017-07-23 17:49:47 +02:00
return outputLevel;
2017-07-04 15:50:02 +02:00
}
2017-07-20 16:24:17 +02:00
void *AudioOutputModule::StartReceiverThread(void *output)
{
2017-07-23 17:49:47 +02:00
((AudioOutputModule *)output)->RunReceiverThread();
return NULL;
2017-07-11 21:11:23 +02:00
}
2017-07-20 16:24:17 +02:00
void AudioOutputModule::RunReceiverThread()
{
2017-07-23 17:49:47 +02:00
unsigned char *data = (unsigned char *)malloc(outputCSamplesSize);
double time = VoIPController::GetCurrentTime();
double sleeptime;
while (wrapper->outputState == AUDIO_STATE_RUNNING)
{
lock_mutex(wrapper->outputMutex);
while (!wrapper->configuringOutput && wrapper->outputState == AUDIO_STATE_RUNNING)
{
if ((sleeptime = (outputWritePeriodSec - (VoIPController::GetCurrentTime() - time)) * 1000000.0) < 0)
{
LOGE("Receiver: I'm late!");
}
else
{
usleep(sleeptime);
}
time = VoIPController::GetCurrentTime();
InvokeCallback(data, outputCSamplesSize);
if (wrapper->outputFile != NULL)
{
if (fwrite(data, sizeof(unsigned char), outputSamplesSize, wrapper->outputFile) != outputCSamplesSize)
{
LOGE("COULD NOT WRITE DATA TO FILE");
}
}
}
unlock_mutex(wrapper->outputMutex);
}
free(data);
2017-07-04 15:50:02 +02:00
}
2017-07-09 21:12:01 +02:00
void AudioOutputModule::EnumerateDevices(std::vector<AudioOutputDevice> &devs)
{
2017-07-04 15:50:02 +02:00
}