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:
parent
14ad87069e
commit
37ea7c359c
|
@ -20,12 +20,15 @@
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <QMutexLocker>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
std::atomic<int> Audio::userCount{0};
|
std::atomic<int> Audio::userCount{0};
|
||||||
Audio* Audio::instance{nullptr};
|
Audio* Audio::instance{nullptr};
|
||||||
QThread* Audio::audioThread{nullptr};
|
QThread* Audio::audioThread{nullptr};
|
||||||
|
QMutex* Audio::audioInLock{nullptr};
|
||||||
|
QMutex* Audio::audioOutLock{nullptr};
|
||||||
ALCdevice* Audio::alInDev{nullptr};
|
ALCdevice* Audio::alInDev{nullptr};
|
||||||
ALCdevice* Audio::alOutDev{nullptr};
|
ALCdevice* Audio::alOutDev{nullptr};
|
||||||
ALCcontext* Audio::alContext{nullptr};
|
ALCcontext* Audio::alContext{nullptr};
|
||||||
|
@ -39,6 +42,8 @@ Audio& Audio::getInstance()
|
||||||
audioThread = new QThread(instance);
|
audioThread = new QThread(instance);
|
||||||
audioThread->setObjectName("qTox Audio");
|
audioThread->setObjectName("qTox Audio");
|
||||||
audioThread->start();
|
audioThread->start();
|
||||||
|
audioInLock = new QMutex(QMutex::Recursive);
|
||||||
|
audioOutLock = new QMutex(QMutex::Recursive);
|
||||||
instance->moveToThread(audioThread);
|
instance->moveToThread(audioThread);
|
||||||
}
|
}
|
||||||
return *instance;
|
return *instance;
|
||||||
|
@ -46,18 +51,21 @@ Audio& Audio::getInstance()
|
||||||
|
|
||||||
void Audio::suscribeInput()
|
void Audio::suscribeInput()
|
||||||
{
|
{
|
||||||
|
QMutexLocker lock(audioInLock);
|
||||||
if (!userCount++ && alInDev)
|
if (!userCount++ && alInDev)
|
||||||
alcCaptureStart(alInDev);
|
alcCaptureStart(alInDev);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Audio::unsuscribeInput()
|
void Audio::unsuscribeInput()
|
||||||
{
|
{
|
||||||
|
QMutexLocker lock(audioInLock);
|
||||||
if (!--userCount && alInDev)
|
if (!--userCount && alInDev)
|
||||||
alcCaptureStop(alInDev);
|
alcCaptureStop(alInDev);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Audio::openInput(const QString& inDevDescr)
|
void Audio::openInput(const QString& inDevDescr)
|
||||||
{
|
{
|
||||||
|
QMutexLocker lock(audioInLock);
|
||||||
auto* tmp = alInDev;
|
auto* tmp = alInDev;
|
||||||
alInDev = nullptr;
|
alInDev = nullptr;
|
||||||
if (tmp)
|
if (tmp)
|
||||||
|
@ -85,6 +93,7 @@ void Audio::openInput(const QString& inDevDescr)
|
||||||
|
|
||||||
void Audio::openOutput(const QString& outDevDescr)
|
void Audio::openOutput(const QString& outDevDescr)
|
||||||
{
|
{
|
||||||
|
QMutexLocker lock(audioOutLock);
|
||||||
auto* tmp = alOutDev;
|
auto* tmp = alOutDev;
|
||||||
alOutDev = nullptr;
|
alOutDev = nullptr;
|
||||||
if (outDevDescr.isEmpty())
|
if (outDevDescr.isEmpty())
|
||||||
|
@ -97,11 +106,8 @@ void Audio::openOutput(const QString& outDevDescr)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (alContext)
|
if (alContext && alcMakeContextCurrent(nullptr) == ALC_TRUE)
|
||||||
{
|
|
||||||
alcMakeContextCurrent(nullptr);
|
|
||||||
alcDestroyContext(alContext);
|
alcDestroyContext(alContext);
|
||||||
}
|
|
||||||
if (tmp)
|
if (tmp)
|
||||||
alcCloseDevice(tmp);
|
alcCloseDevice(tmp);
|
||||||
alContext=alcCreateContext(alOutDev,nullptr);
|
alContext=alcCreateContext(alOutDev,nullptr);
|
||||||
|
@ -122,6 +128,7 @@ void Audio::openOutput(const QString& outDevDescr)
|
||||||
|
|
||||||
void Audio::closeInput()
|
void Audio::closeInput()
|
||||||
{
|
{
|
||||||
|
QMutexLocker lock(audioInLock);
|
||||||
if (alInDev)
|
if (alInDev)
|
||||||
alcCaptureCloseDevice(alInDev);
|
alcCaptureCloseDevice(alInDev);
|
||||||
|
|
||||||
|
@ -130,11 +137,9 @@ void Audio::closeInput()
|
||||||
|
|
||||||
void Audio::closeOutput()
|
void Audio::closeOutput()
|
||||||
{
|
{
|
||||||
if (alContext)
|
QMutexLocker lock(audioOutLock);
|
||||||
{
|
if (alContext && alcMakeContextCurrent(nullptr) == ALC_TRUE)
|
||||||
alcMakeContextCurrent(nullptr);
|
|
||||||
alcDestroyContext(alContext);
|
alcDestroyContext(alContext);
|
||||||
}
|
|
||||||
|
|
||||||
if (alOutDev)
|
if (alOutDev)
|
||||||
alcCloseDevice(alOutDev);
|
alcCloseDevice(alOutDev);
|
||||||
|
@ -142,6 +147,10 @@ void Audio::closeOutput()
|
||||||
|
|
||||||
void Audio::playMono16Sound(const QByteArray& data)
|
void Audio::playMono16Sound(const QByteArray& data)
|
||||||
{
|
{
|
||||||
|
QMutexLocker lock(audioOutLock);
|
||||||
|
if (!alOutDev)
|
||||||
|
return;
|
||||||
|
|
||||||
ALuint buffer;
|
ALuint buffer;
|
||||||
alGenBuffers(1, &buffer);
|
alGenBuffers(1, &buffer);
|
||||||
alBufferData(buffer, AL_FORMAT_MONO16, data.data(), data.size(), 44100);
|
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);
|
assert(QThread::currentThread() == audioThread);
|
||||||
|
|
||||||
|
QMutexLocker lock(audioOutLock);
|
||||||
|
|
||||||
ToxGroupCall& call = Core::groupCalls[group];
|
ToxGroupCall& call = Core::groupCalls[group];
|
||||||
|
|
||||||
if (!call.active || call.muteVol)
|
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);
|
assert(channels == 1 || channels == 2);
|
||||||
|
|
||||||
|
QMutexLocker lock(audioOutLock);
|
||||||
|
|
||||||
ALuint bufid;
|
ALuint bufid;
|
||||||
ALint processed = 0, queued = 16;
|
ALint processed = 0, queued = 16;
|
||||||
alGetSourcei(alSource, AL_BUFFERS_PROCESSED, &processed);
|
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)
|
if (state != AL_PLAYING)
|
||||||
alSourcePlay(alSource);
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ class QString;
|
||||||
class QByteArray;
|
class QByteArray;
|
||||||
class QTimer;
|
class QTimer;
|
||||||
class QThread;
|
class QThread;
|
||||||
|
class QMutex;
|
||||||
struct Tox;
|
struct Tox;
|
||||||
|
|
||||||
class Audio : QObject
|
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 closeInput(); ///< Close an input device, please don't use unless everyone's unsuscribed
|
||||||
static void closeOutput(); ///< Close an output device
|
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 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
|
/// 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
|
/// The first and last argument are ignored, but allow direct compatibility with toxcore
|
||||||
|
@ -66,7 +71,6 @@ public slots:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static QThread* audioThread;
|
static QThread* audioThread;
|
||||||
static ALCdevice* alOutDev, *alInDev;
|
|
||||||
static ALCcontext* alContext;
|
static ALCcontext* alContext;
|
||||||
static ALuint alMainSource;
|
static ALuint alMainSource;
|
||||||
|
|
||||||
|
@ -77,6 +81,8 @@ private:
|
||||||
private:
|
private:
|
||||||
static Audio* instance;
|
static Audio* instance;
|
||||||
static std::atomic<int> userCount;
|
static std::atomic<int> userCount;
|
||||||
|
static ALCdevice* alOutDev, *alInDev;
|
||||||
|
static QMutex* audioInLock, *audioOutLock;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // AUDIO_H
|
#endif // AUDIO_H
|
||||||
|
|
|
@ -241,7 +241,7 @@ void Core::sendCallAudio(int callId, ToxAv* toxav)
|
||||||
if (!calls[callId].active)
|
if (!calls[callId].active)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (calls[callId].muteMic || !Audio::alInDev)
|
if (calls[callId].muteMic || !Audio::isInputReady())
|
||||||
{
|
{
|
||||||
calls[callId].sendAudioTimer->start();
|
calls[callId].sendAudioTimer->start();
|
||||||
return;
|
return;
|
||||||
|
@ -251,17 +251,7 @@ void Core::sendCallAudio(int callId, ToxAv* toxav)
|
||||||
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
|
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
|
||||||
uint8_t buf[bufsize], dest[bufsize];
|
uint8_t buf[bufsize], dest[bufsize];
|
||||||
|
|
||||||
bool frame = false;
|
if (Audio::tryCaptureSamples(buf, framesize))
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
if ((r = toxav_prepare_audio_frame(toxav, callId, dest, framesize*2, (int16_t*)buf, framesize)) < 0)
|
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)
|
void Core::onAvRequestTimeout(void* _toxav, int32_t call_index, void* core)
|
||||||
{
|
{
|
||||||
ToxAv* toxav = static_cast<ToxAv*>(_toxav);
|
ToxAv* toxav = static_cast<ToxAv*>(_toxav);
|
||||||
|
@ -668,7 +605,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav)
|
||||||
if (!groupCalls[groupId].active)
|
if (!groupCalls[groupId].active)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (groupCalls[groupId].muteMic || !Audio::alInDev)
|
if (groupCalls[groupId].muteMic || !Audio::isInputReady())
|
||||||
{
|
{
|
||||||
groupCalls[groupId].sendAudioTimer->start();
|
groupCalls[groupId].sendAudioTimer->start();
|
||||||
return;
|
return;
|
||||||
|
@ -678,17 +615,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav)
|
||||||
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
|
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
|
||||||
uint8_t buf[bufsize];
|
uint8_t buf[bufsize];
|
||||||
|
|
||||||
bool frame = false;
|
if (Audio::tryCaptureSamples(buf, framesize))
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
if ((r = toxav_group_send_audio(toxav_get_tox(toxav), groupId, (int16_t*)buf,
|
if ((r = toxav_group_send_audio(toxav_get_tox(toxav), groupId, (int16_t*)buf,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user