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

Add mutexes to AudioBuffer

This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-07-01 22:04:19 +02:00
parent a83c35c95b
commit f5793b0db8
2 changed files with 34 additions and 6 deletions

View File

@ -13,29 +13,55 @@ AudioBuffer::~AudioBuffer()
qint64 AudioBuffer::readData(char *data, qint64 len) qint64 AudioBuffer::readData(char *data, qint64 len)
{ {
const qint64 total = qMin((qint64)buffer.size(), len); bufferMutex.lock();
memcpy(data, buffer.constData(), total); try {
buffer = buffer.mid(total); const qint64 total = qMin((qint64)buffer.size(), len);
memcpy(data, buffer.constData(), total);
buffer = buffer.mid(total);
}
catch (...)
{
bufferMutex.unlock();
return 0;
}
bufferMutex.unlock();
return total; return total;
} }
qint64 AudioBuffer::writeData(const char* data, qint64 len) qint64 AudioBuffer::writeData(const char* data, qint64 len)
{ {
buffer.append(data, len); bufferMutex.lock();
try {
buffer.append(data, len);
}
catch (...)
{
bufferMutex.unlock();
return 0;
}
bufferMutex.unlock();
return len; return len;
} }
qint64 AudioBuffer::bytesAvailable() const qint64 AudioBuffer::bytesAvailable() const
{ {
return buffer.size() + QIODevice::bytesAvailable(); bufferMutex.lock();
long long size = buffer.size() + QIODevice::bytesAvailable();
bufferMutex.unlock();
return size;
} }
qint64 AudioBuffer::bufferSize() const qint64 AudioBuffer::bufferSize() const
{ {
return buffer.size(); bufferMutex.lock();
long long size = buffer.size();
bufferMutex.unlock();
return size;
} }
void AudioBuffer::clear() void AudioBuffer::clear()
{ {
bufferMutex.lock();
buffer.clear(); buffer.clear();
bufferMutex.unlock();
} }

View File

@ -3,6 +3,7 @@
#include <QIODevice> #include <QIODevice>
#include <QByteArray> #include <QByteArray>
#include <QMutex>
class AudioBuffer : public QIODevice class AudioBuffer : public QIODevice
{ {
@ -19,6 +20,7 @@ public:
private: private:
QByteArray buffer; QByteArray buffer;
mutable QMutex bufferMutex;
}; };
#endif // AUDIOBUFFER_H #endif // AUDIOBUFFER_H