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

141 lines
2.5 KiB
C++
Raw 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()
{
input = new AudioInputCallback();
output = new AudioOutputCallback();
}
2020-01-22 12:43:51 +01:00
AudioIOCallback::~AudioIOCallback()
{
delete input;
delete output;
}
2020-01-22 12:43:51 +01:00
AudioInput *AudioIOCallback::GetInput()
{
return input;
}
2020-01-22 12:43:51 +01:00
AudioOutput *AudioIOCallback::GetOutput()
{
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();
InvokeCallback(reinterpret_cast<unsigned char *>(buf), 960 * 2);
dataCallback(buf, 960);
2020-01-22 12:43:51 +01:00
double sl = 0.02 - (VoIPController::GetCurrentTime() - t);
if (sl > 0)
Thread::Sleep(sl);
}
}