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

use single mutex for audio resource locking

This commit is contained in:
Nils Fenner 2015-11-14 10:08:58 +01:00
parent 9627f45014
commit 137eca86d6
No known key found for this signature in database
GPG Key ID: 9591A163FF9BE04C
2 changed files with 19 additions and 18 deletions

View File

@ -56,8 +56,6 @@ Audio& Audio::getInstance()
Audio::Audio() Audio::Audio()
: audioThread(new QThread()) : audioThread(new QThread())
, audioInLock(QMutex::Recursive)
, audioOutLock(QMutex::Recursive)
, inputSubscriptions(0) , inputSubscriptions(0)
, outputSubscriptions(0) , outputSubscriptions(0)
, alOutDev(nullptr) , alOutDev(nullptr)
@ -101,7 +99,7 @@ Returns the current output volume, between 0 and 1
*/ */
qreal Audio::getOutputVolume() qreal Audio::getOutputVolume()
{ {
QMutexLocker locker(&audioOutLock); QMutexLocker locker(&mAudioLock);
return outputVolume; return outputVolume;
} }
@ -110,7 +108,8 @@ The volume must be between 0 and 1
*/ */
void Audio::setOutputVolume(qreal volume) void Audio::setOutputVolume(qreal volume)
{ {
QMutexLocker locker(&audioOutLock); QMutexLocker locker(&mAudioLock);
outputVolume = volume; outputVolume = volume;
alSourcef(alMainSource, AL_GAIN, outputVolume); alSourcef(alMainSource, AL_GAIN, outputVolume);
@ -130,7 +129,7 @@ The volume must be between 0 and 2
*/ */
void Audio::setInputVolume(qreal volume) void Audio::setInputVolume(qreal volume)
{ {
QMutexLocker locker(&audioInLock); QMutexLocker locker(&mAudioLock);
inputVolume = volume; inputVolume = volume;
} }
@ -141,7 +140,8 @@ If the input device is not open, it will be opened before capturing.
*/ */
void Audio::subscribeInput() void Audio::subscribeInput()
{ {
QMutexLocker locker(&audioInLock); QMutexLocker locker(&mAudioLock);
if (!alInDev) if (!alInDev)
initInput(Settings::getInstance().getInDev()); initInput(Settings::getInstance().getInDev());
@ -156,7 +156,7 @@ If the input device has no more subscriptions, it will be closed.
*/ */
void Audio::unsubscribeInput() void Audio::unsubscribeInput()
{ {
QMutexLocker locker(&audioInLock); QMutexLocker locker(&mAudioLock);
if (inputSubscriptions > 0) if (inputSubscriptions > 0)
{ {
@ -170,7 +170,7 @@ void Audio::unsubscribeInput()
void Audio::subscribeOutput() void Audio::subscribeOutput()
{ {
QMutexLocker locker(&audioOutLock); QMutexLocker locker(&mAudioLock);
internalSubscribeOutput(); internalSubscribeOutput();
} }
@ -185,6 +185,8 @@ void Audio::internalSubscribeOutput()
void Audio::unsubscribeOutput() void Audio::unsubscribeOutput()
{ {
QMutexLocker locker(&mAudioLock);
if (outputSubscriptions > 0) if (outputSubscriptions > 0)
{ {
outputSubscriptions--; outputSubscriptions--;
@ -331,7 +333,7 @@ Play a 44100Hz mono 16bit PCM sound
*/ */
void Audio::playMono16Sound(const QByteArray& data) void Audio::playMono16Sound(const QByteArray& data)
{ {
QMutexLocker lock(&audioOutLock); QMutexLocker locker(&mAudioLock);
internalSubscribeOutput(); internalSubscribeOutput();
@ -388,7 +390,7 @@ void Audio::playGroupAudio(int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate) unsigned samples, uint8_t channels, unsigned sample_rate)
{ {
assert(QThread::currentThread() == audioThread); assert(QThread::currentThread() == audioThread);
QMutexLocker lock(&audioOutLock); QMutexLocker locker(&mAudioLock);
if (!CoreAV::groupCalls.contains(group)) if (!CoreAV::groupCalls.contains(group))
return; return;
@ -418,7 +420,7 @@ void Audio::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, u
{ {
assert(channels == 1 || channels == 2); assert(channels == 1 || channels == 2);
QMutexLocker lock(&getInstance().audioOutLock); QMutexLocker locker(&getInstance().mAudioLock);
ALuint bufid; ALuint bufid;
ALint processed = 0, queued = 16; ALint processed = 0, queued = 16;
@ -510,7 +512,7 @@ Returns true if the input device is open and suscribed to
*/ */
bool Audio::isInputReady() bool Audio::isInputReady()
{ {
QMutexLocker locker(&audioInLock); QMutexLocker locker(&mAudioLock);
return alInDev && mInputInitialized; return alInDev && mInputInitialized;
} }
@ -519,7 +521,7 @@ Returns true if the output device is open
*/ */
bool Audio::isOutputReady() bool Audio::isOutputReady()
{ {
QMutexLocker locker(&audioOutLock); QMutexLocker locker(&mAudioLock);
return alOutDev && mOutputInitialized; return alOutDev && mOutputInitialized;
} }
@ -553,7 +555,7 @@ Does nothing and return false on failure
*/ */
bool Audio::tryCaptureSamples(int16_t* buf, int samples) bool Audio::tryCaptureSamples(int16_t* buf, int samples)
{ {
QMutexLocker lock(&audioInLock); QMutexLocker lock(&mAudioLock);
if (!(alInDev && mInputInitialized)) if (!(alInDev && mInputInitialized))
return false; return false;

View File

@ -62,13 +62,13 @@ public:
inline void reinitInput(const QString& inDevDesc) inline void reinitInput(const QString& inDevDesc)
{ {
QMutexLocker locker(&audioInLock); QMutexLocker locker(&mAudioLock);
cleanupInput(); cleanupInput();
initInput(inDevDesc); initInput(inDevDesc);
} }
inline bool reinitOutput(const QString& outDevDesc) inline bool reinitOutput(const QString& outDevDesc)
{ {
QMutexLocker locker(&audioOutLock); QMutexLocker locker(&mAudioLock);
cleanupOutput(); cleanupOutput();
return initOutput(outDevDesc); return initOutput(outDevDesc);
} }
@ -122,10 +122,9 @@ private:
private: private:
QThread* audioThread; QThread* audioThread;
QMutex audioInLock;
QMutex audioOutLock;
std::atomic<int> inputSubscriptions; std::atomic<int> inputSubscriptions;
std::atomic<int> outputSubscriptions; std::atomic<int> outputSubscriptions;
QMutex mAudioLock;
ALCdevice* alOutDev; ALCdevice* alOutDev;
ALCdevice* alInDev; ALCdevice* alInDev;
bool mInputInitialized; bool mInputInitialized;