2018-09-01 00:59:09 +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 "AudioIOCallback.h"
|
|
|
|
#include "../VoIPController.h"
|
2020-01-22 12:43:51 +01:00
|
|
|
#include "../tools/logging.h"
|
2018-09-01 00:59:09 +02:00
|
|
|
|
|
|
|
using namespace tgvoip;
|
|
|
|
using namespace tgvoip::audio;
|
|
|
|
|
|
|
|
#pragma mark - IO
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
AudioIOCallback::AudioIOCallback()
|
|
|
|
{
|
2020-01-24 20:26:34 +01:00
|
|
|
input = std::make_shared<AudioInputCallback>();
|
|
|
|
output = std::make_shared<AudioOutputCallback>();
|
2018-09-01 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
AudioIOCallback::~AudioIOCallback()
|
|
|
|
{
|
2018-09-01 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-24 20:26:34 +01:00
|
|
|
std::shared_ptr<AudioInput> AudioIOCallback::GetInput()
|
2020-01-22 12:43:51 +01:00
|
|
|
{
|
2018-09-01 00:59:09 +02:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:26:34 +01:00
|
|
|
std::shared_ptr<AudioInput> AudioIOCallback::GetOutput()
|
2020-01-22 12:43:51 +01:00
|
|
|
{
|
2018-09-01 00:59:09 +02:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Input
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
AudioInputCallback::AudioInputCallback()
|
|
|
|
{
|
|
|
|
thread = new Thread(std::bind(&AudioInputCallback::RunThread, this));
|
2018-09-01 00:59:09 +02:00
|
|
|
thread->SetName("AudioInputCallback");
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
AudioInputCallback::~AudioInputCallback()
|
|
|
|
{
|
|
|
|
running = false;
|
2018-09-01 00:59:09 +02:00
|
|
|
thread->Join();
|
|
|
|
delete thread;
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void AudioInputCallback::Start()
|
|
|
|
{
|
|
|
|
if (!running)
|
|
|
|
{
|
|
|
|
running = true;
|
2018-09-01 00:59:09 +02:00
|
|
|
thread->Start();
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
recording = true;
|
2018-09-01 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void AudioInputCallback::Stop()
|
|
|
|
{
|
|
|
|
recording = false;
|
2018-09-01 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void AudioInputCallback::SetDataCallback(std::function<void(int16_t *, size_t)> c)
|
|
|
|
{
|
|
|
|
dataCallback = c;
|
2018-09-01 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void AudioInputCallback::RunThread()
|
|
|
|
{
|
2018-09-01 00:59:09 +02:00
|
|
|
int16_t buf[960];
|
2020-01-22 12:43:51 +01:00
|
|
|
while (running)
|
|
|
|
{
|
|
|
|
double t = VoIPController::GetCurrentTime();
|
2018-09-01 00:59:09 +02:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
dataCallback(buf, 960);
|
2020-01-22 12:43:51 +01:00
|
|
|
InvokeCallback(reinterpret_cast<unsigned char *>(buf), 960 * 2);
|
|
|
|
double sl = 0.02 - (VoIPController::GetCurrentTime() - t);
|
|
|
|
if (sl > 0)
|
2018-09-01 00:59:09 +02:00
|
|
|
Thread::Sleep(sl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Output
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
AudioOutputCallback::AudioOutputCallback()
|
|
|
|
{
|
|
|
|
thread = new Thread(std::bind(&AudioOutputCallback::RunThread, this));
|
2018-09-01 00:59:09 +02:00
|
|
|
thread->SetName("AudioOutputCallback");
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
AudioOutputCallback::~AudioOutputCallback()
|
|
|
|
{
|
|
|
|
running = false;
|
2018-09-01 00:59:09 +02:00
|
|
|
thread->Join();
|
|
|
|
delete thread;
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void AudioOutputCallback::Start()
|
|
|
|
{
|
|
|
|
if (!running)
|
|
|
|
{
|
|
|
|
running = true;
|
2018-09-01 00:59:09 +02:00
|
|
|
thread->Start();
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
playing = true;
|
2018-09-01 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void AudioOutputCallback::Stop()
|
|
|
|
{
|
|
|
|
playing = false;
|
2018-09-01 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
bool AudioOutputCallback::IsPlaying()
|
|
|
|
{
|
2018-09-01 00:59:09 +02:00
|
|
|
return playing;
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void AudioOutputCallback::SetDataCallback(std::function<void(int16_t *, size_t)> c)
|
|
|
|
{
|
|
|
|
dataCallback = c;
|
2018-09-01 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void AudioOutputCallback::RunThread()
|
|
|
|
{
|
2018-09-01 00:59:09 +02:00
|
|
|
int16_t buf[960];
|
2020-01-22 12:43:51 +01:00
|
|
|
while (running)
|
|
|
|
{
|
|
|
|
double t = VoIPController::GetCurrentTime();
|
|
|
|
InvokeCallback(reinterpret_cast<unsigned char *>(buf), 960 * 2);
|
2018-09-01 00:59:09 +02:00
|
|
|
dataCallback(buf, 960);
|
2020-01-22 12:43:51 +01:00
|
|
|
double sl = 0.02 - (VoIPController::GetCurrentTime() - t);
|
|
|
|
if (sl > 0)
|
2018-09-01 00:59:09 +02:00
|
|
|
Thread::Sleep(sl);
|
|
|
|
}
|
|
|
|
}
|