1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-26 20:24:38 +01:00
libtgvoip/audio/AudioIOCallback.cpp

146 lines
2.6 KiB
C++
Raw Permalink Normal View History

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