1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Thread safe, encapsulated audio

This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2015-01-04 17:55:39 +01:00
parent 14ad87069e
commit 37ea7c359c
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
3 changed files with 56 additions and 86 deletions

View File

@ -20,12 +20,15 @@
#include <QDebug>
#include <QThread>
#include <QMutexLocker>
#include <cassert>
std::atomic<int> Audio::userCount{0};
Audio* Audio::instance{nullptr};
QThread* Audio::audioThread{nullptr};
QMutex* Audio::audioInLock{nullptr};
QMutex* Audio::audioOutLock{nullptr};
ALCdevice* Audio::alInDev{nullptr};
ALCdevice* Audio::alOutDev{nullptr};
ALCcontext* Audio::alContext{nullptr};
@ -39,6 +42,8 @@ Audio& Audio::getInstance()
audioThread = new QThread(instance);
audioThread->setObjectName("qTox Audio");
audioThread->start();
audioInLock = new QMutex(QMutex::Recursive);
audioOutLock = new QMutex(QMutex::Recursive);
instance->moveToThread(audioThread);
}
return *instance;
@ -46,18 +51,21 @@ Audio& Audio::getInstance()
void Audio::suscribeInput()
{
QMutexLocker lock(audioInLock);
if (!userCount++ && alInDev)
alcCaptureStart(alInDev);
}
void Audio::unsuscribeInput()
{
QMutexLocker lock(audioInLock);
if (!--userCount && alInDev)
alcCaptureStop(alInDev);
}
void Audio::openInput(const QString& inDevDescr)
{
QMutexLocker lock(audioInLock);
auto* tmp = alInDev;
alInDev = nullptr;
if (tmp)
@ -85,6 +93,7 @@ void Audio::openInput(const QString& inDevDescr)
void Audio::openOutput(const QString& outDevDescr)
{
QMutexLocker lock(audioOutLock);
auto* tmp = alOutDev;
alOutDev = nullptr;
if (outDevDescr.isEmpty())
@ -97,11 +106,8 @@ void Audio::openOutput(const QString& outDevDescr)
}
else
{
if (alContext)
{
alcMakeContextCurrent(nullptr);
if (alContext && alcMakeContextCurrent(nullptr) == ALC_TRUE)
alcDestroyContext(alContext);
}
if (tmp)
alcCloseDevice(tmp);
alContext=alcCreateContext(alOutDev,nullptr);
@ -122,6 +128,7 @@ void Audio::openOutput(const QString& outDevDescr)
void Audio::closeInput()
{
QMutexLocker lock(audioInLock);
if (alInDev)
alcCaptureCloseDevice(alInDev);
@ -130,11 +137,9 @@ void Audio::closeInput()
void Audio::closeOutput()
{
if (alContext)
{
alcMakeContextCurrent(nullptr);
QMutexLocker lock(audioOutLock);
if (alContext && alcMakeContextCurrent(nullptr) == ALC_TRUE)
alcDestroyContext(alContext);
}
if (alOutDev)
alcCloseDevice(alOutDev);
@ -142,6 +147,10 @@ void Audio::closeOutput()
void Audio::playMono16Sound(const QByteArray& data)
{
QMutexLocker lock(audioOutLock);
if (!alOutDev)
return;
ALuint buffer;
alGenBuffers(1, &buffer);
alBufferData(buffer, AL_FORMAT_MONO16, data.data(), data.size(), 44100);
@ -163,6 +172,8 @@ void Audio::playGroupAudio(int group, int peer, const int16_t* data,
{
assert(QThread::currentThread() == audioThread);
QMutexLocker lock(audioOutLock);
ToxGroupCall& call = Core::groupCalls[group];
if (!call.active || call.muteVol)
@ -178,6 +189,8 @@ void Audio::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, u
{
assert(channels == 1 || channels == 2);
QMutexLocker lock(audioOutLock);
ALuint bufid;
ALint processed = 0, queued = 16;
alGetSourcei(alSource, AL_BUFFERS_PROCESSED, &processed);
@ -210,3 +223,27 @@ void Audio::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, u
if (state != AL_PLAYING)
alSourcePlay(alSource);
}
bool Audio::isInputReady()
{
return (alInDev && userCount);
}
bool Audio::isOutputClosed()
{
return (alOutDev);
}
bool Audio::tryCaptureSamples(uint8_t* buf, int framesize)
{
QMutexLocker lock(audioInLock);
ALint samples=0;
alcGetIntegerv(Audio::alInDev, ALC_CAPTURE_SAMPLES, sizeof(samples), &samples);
if (samples < framesize)
return false;
memset(buf, 0, framesize * 2 * av_DefaultSettings.audio_channels); // Avoid uninitialized values (Valgrind)
alcCaptureSamples(Audio::alInDev, buf, framesize);
return true;
}

View File

@ -34,6 +34,7 @@ class QString;
class QByteArray;
class QTimer;
class QThread;
class QMutex;
struct Tox;
class Audio : QObject
@ -52,7 +53,11 @@ public:
static void closeInput(); ///< Close an input device, please don't use unless everyone's unsuscribed
static void closeOutput(); ///< Close an output device
static bool isInputReady(); ///< Returns true if the input device is open and suscribed to
static bool isOutputClosed(); ///< Returns true if the output device is open
static void playMono16Sound(const QByteArray& data); ///< Play a 44100Hz mono 16bit PCM sound
static bool tryCaptureSamples(uint8_t* buf, int framesize); ///< Does nothing and return false on failure
/// May be called from any thread, will always queue a call to playGroupAudio
/// The first and last argument are ignored, but allow direct compatibility with toxcore
@ -66,7 +71,6 @@ public slots:
public:
static QThread* audioThread;
static ALCdevice* alOutDev, *alInDev;
static ALCcontext* alContext;
static ALuint alMainSource;
@ -77,6 +81,8 @@ private:
private:
static Audio* instance;
static std::atomic<int> userCount;
static ALCdevice* alOutDev, *alInDev;
static QMutex* audioInLock, *audioOutLock;
};
#endif // AUDIO_H

View File

@ -241,7 +241,7 @@ void Core::sendCallAudio(int callId, ToxAv* toxav)
if (!calls[callId].active)
return;
if (calls[callId].muteMic || !Audio::alInDev)
if (calls[callId].muteMic || !Audio::isInputReady())
{
calls[callId].sendAudioTimer->start();
return;
@ -251,17 +251,7 @@ void Core::sendCallAudio(int callId, ToxAv* toxav)
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
uint8_t buf[bufsize], dest[bufsize];
bool frame = false;
ALint samples;
alcGetIntegerv(Audio::alInDev, ALC_CAPTURE_SAMPLES, sizeof(samples), &samples);
if (samples >= framesize)
{
memset(buf, 0, bufsize); // Avoid uninitialized values (Valgrind)
alcCaptureSamples(Audio::alInDev, buf, framesize);
frame = 1;
}
if (frame)
if (Audio::tryCaptureSamples(buf, framesize))
{
int r;
if ((r = toxav_prepare_audio_frame(toxav, callId, dest, framesize*2, (int16_t*)buf, framesize)) < 0)
@ -417,59 +407,6 @@ void Core::onAvRinging(void* _toxav, int32_t call_index, void* core)
}
}
//void Core::onAvStarting(void* _toxav, int32_t call_index, void* core)
//{
// ToxAv* toxav = static_cast<ToxAv*>(_toxav);
// int friendId = toxav_get_peer_id(toxav, call_index, 0);
// if (friendId < 0)
// {
// qWarning() << "Core: Received invalid AV starting";
// return;
// }
// ToxAvCSettings* transSettings = new ToxAvCSettings;
// int err = toxav_get_peer_csettings(toxav, call_index, 0, transSettings);
// if (err != ErrorNone)
// {
// qWarning() << "Core::onAvStarting: error getting call type";
// delete transSettings;
// return;
// }
// if (transSettings->call_type == TypeVideo)
// {
// qDebug() << QString("Core: AV starting from %1 with video").arg(friendId);
// prepareCall(friendId, call_index, toxav, true);
// emit static_cast<Core*>(core)->avStarting(friendId, call_index, true);
// }
// else
// {
// qDebug() << QString("Core: AV starting from %1 without video").arg(friendId);
// prepareCall(friendId, call_index, toxav, false);
// emit static_cast<Core*>(core)->avStarting(friendId, call_index, false);
// }
// delete transSettings;
//}
//void Core::onAvEnding(void* _toxav, int32_t call_index, void* core)
//{
// ToxAv* toxav = static_cast<ToxAv*>(_toxav);
// int friendId = toxav_get_peer_id(toxav, call_index, 0);
// if (friendId < 0)
// {
// qWarning() << "Core: Received invalid AV ending";
// return;
// }
// qDebug() << QString("Core: AV ending from %1").arg(friendId);
// cleanupCall(call_index);
// emit static_cast<Core*>(core)->avEnding(friendId, call_index);
//}
void Core::onAvRequestTimeout(void* _toxav, int32_t call_index, void* core)
{
ToxAv* toxav = static_cast<ToxAv*>(_toxav);
@ -668,7 +605,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav)
if (!groupCalls[groupId].active)
return;
if (groupCalls[groupId].muteMic || !Audio::alInDev)
if (groupCalls[groupId].muteMic || !Audio::isInputReady())
{
groupCalls[groupId].sendAudioTimer->start();
return;
@ -678,17 +615,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav)
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
uint8_t buf[bufsize];
bool frame = false;
ALint samples;
alcGetIntegerv(Audio::alInDev, ALC_CAPTURE_SAMPLES, sizeof(samples), &samples);
if (samples >= framesize)
{
memset(buf, 0, bufsize); // Avoid uninitialized values (Valgrind)
alcCaptureSamples(Audio::alInDev, buf, framesize);
frame = 1;
}
if (frame)
if (Audio::tryCaptureSamples(buf, framesize))
{
int r;
if ((r = toxav_group_send_audio(toxav_get_tox(toxav), groupId, (int16_t*)buf,