Use TryLocker instead of explicitly using tryLock on mutexes

This commit is contained in:
Roxanne Skelly 2019-05-09 15:21:43 -07:00
parent 3a4241b290
commit 7a4e3557b0
2 changed files with 6 additions and 4 deletions

View file

@ -10,6 +10,7 @@
//
#include "InboundAudioStream.h"
#include "TryLocker.h"
#include <glm/glm.hpp>
@ -247,7 +248,8 @@ int InboundAudioStream::lostAudioData(int numPackets) {
QByteArray decodedBuffer;
while (numPackets--) {
if (!_decoderMutex.tryLock()) {
MutexTryLocker lock(_decoderMutex);
if (!lock.isLocked()) {
// an incoming packet is being processed,
// and will likely be on the ring buffer shortly,
// so don't bother generating more data
@ -260,7 +262,6 @@ int InboundAudioStream::lostAudioData(int numPackets) {
decodedBuffer.resize(AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL * _numChannels);
memset(decodedBuffer.data(), 0, decodedBuffer.size());
}
_decoderMutex.unlock();
_ringBuffer.writeData(decodedBuffer.data(), decodedBuffer.size());
}
return 0;

View file

@ -11,6 +11,7 @@
#include "MixedProcessedAudioStream.h"
#include "AudioLogging.h"
#include "TryLocker.h"
MixedProcessedAudioStream::MixedProcessedAudioStream(int numFramesCapacity, int numStaticJitterFrames)
: InboundAudioStream(AudioConstants::STEREO, AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL,
@ -36,7 +37,8 @@ int MixedProcessedAudioStream::lostAudioData(int numPackets) {
QByteArray outputBuffer;
while (numPackets--) {
if (!_decoderMutex.tryLock()) {
MutexTryLocker lock(_decoderMutex);
if (!lock.isLocked()) {
// an incoming packet is being processed,
// and will likely be on the ring buffer shortly,
// so don't bother generating more data
@ -49,7 +51,6 @@ int MixedProcessedAudioStream::lostAudioData(int numPackets) {
decodedBuffer.resize(AudioConstants::NETWORK_FRAME_BYTES_STEREO);
memset(decodedBuffer.data(), 0, decodedBuffer.size());
}
_decoderMutex.unlock();
emit addedStereoSamples(decodedBuffer);
emit processSamples(decodedBuffer, outputBuffer);