Commit 9988ec08 authored by Peter Grasch's avatar Peter Grasch
Browse files

Adding locking to sound input client

BUG: 309443
parent 5c2a8553
......@@ -27,7 +27,7 @@
#include "soundinputbuffer.h"
SimonSoundInput::SimonSoundInput(QObject *parent) : QObject(parent),
m_input(SoundBackend::createObject()), m_buffer(0)
m_lock(QMutex::Recursive), m_input(SoundBackend::createObject()), m_buffer(0)
{
connect(m_input, SIGNAL(stateChanged(SimonSound::State)), this, SLOT(slotInputStateChanged(SimonSound::State)));
connect(m_input, SIGNAL(stateChanged(SimonSound::State)), this, SIGNAL(inputStateChanged(SimonSound::State)));
......@@ -47,6 +47,7 @@ qint64 SimonSoundInput::writeData(const char *toWrite, qint64 len)
void SimonSoundInput::processData(const QByteArray& data)
{
QMutexLocker l(&m_lock);
//length is in ms
qint64 length = SoundServer::getInstance()->byteSizeToLength(data.count(), m_device);
......@@ -91,6 +92,7 @@ bool SimonSoundInput::startRecording()
void SimonSoundInput::suspendInputClients()
{
QMutexLocker l(&m_lock);
QHashIterator<SoundInputClient*, qint64> j(m_activeInputClients);
while (j.hasNext()) {
j.next();
......@@ -101,6 +103,7 @@ void SimonSoundInput::suspendInputClients()
void SimonSoundInput::suspend(SoundInputClient* client)
{
QMutexLocker l(&m_lock);
client->suspend();
m_suspendedInputClients.insert(client, m_activeInputClients.value(client));
m_activeInputClients.remove(client);
......@@ -109,6 +112,7 @@ void SimonSoundInput::suspend(SoundInputClient* client)
void SimonSoundInput::resume(SoundInputClient* client)
{
QMutexLocker l(&m_lock);
m_activeInputClients.insert(client, m_suspendedInputClients.value(client));
m_suspendedInputClients.remove(client);
client->resume();
......@@ -117,12 +121,15 @@ void SimonSoundInput::resume(SoundInputClient* client)
void SimonSoundInput::registerInputClient(SoundInputClient* client)
{
QMutexLocker l(&m_lock);
m_activeInputClients.insert(client, 0);
}
bool SimonSoundInput::deRegisterInputClient(SoundInputClient* client)
{
QMutexLocker l(&m_lock);
kDebug() << "Deregistering input client";
if (m_activeInputClients.remove(client) == 0) {
......@@ -148,6 +155,7 @@ bool SimonSoundInput::deRegisterInputClient(SoundInputClient* client)
SoundClient::SoundClientPriority SimonSoundInput::getHighestPriority()
{
QMutexLocker l(&m_lock);
SoundClient::SoundClientPriority priority = SoundClient::Background;
QHashIterator<SoundInputClient*, qint64> j(m_activeInputClients);
while (j.hasNext()) {
......@@ -165,6 +173,7 @@ SoundClient::SoundClientPriority SimonSoundInput::getHighestPriority()
bool SimonSoundInput::activate(SoundClient::SoundClientPriority priority)
{
QMutexLocker l(&m_lock);
kDebug() << "Activating priority: " << priority;
bool activated = false;
QHashIterator<SoundInputClient*, qint64> j(m_activeInputClients);
......@@ -203,6 +212,7 @@ bool SimonSoundInput::activate(SoundClient::SoundClientPriority priority)
void SimonSoundInput::slotInputStateChanged(SimonSound::State state)
{
QMutexLocker l(&m_lock);
kDebug() << "Input state changed: " << state;
QList<SoundInputClient*> activeInputClientsKeys = m_activeInputClients.keys();
......@@ -245,11 +255,11 @@ bool SimonSoundInput::stopRecording()
return true;
m_input->stopRecording();
kDebug() << "Now stopping buffer";
killBuffer();
kDebug() << "Done";
return true;
}
......@@ -257,9 +267,9 @@ void SimonSoundInput::killBuffer()
{
if (m_buffer) {
m_buffer->stop();
m_buffer->wait();
// m_buffer->wait();
}
m_buffer = 0;
}
......@@ -269,5 +279,7 @@ SimonSoundInput::~SimonSoundInput()
stopRecording();
killBuffer();
if (m_buffer)
m_buffer->wait();
delete m_input;
}
......@@ -28,6 +28,7 @@ class QAudioInput;
#include <simonsound/soundbackendclient.h>
#include <QHash>
#include <QObject>
#include <QMutex>
class SoundInputClient;
class SoundBackend;
......@@ -42,6 +43,7 @@ class SimonSoundInput : public QObject, public SoundBackendClient
void inputStateChanged(SimonSound::State state);
private:
QMutex m_lock;
SimonSound::DeviceConfiguration m_device;
SoundBackend *m_input;
QHash<SoundInputClient*, qint64> m_activeInputClients;
......@@ -71,7 +73,7 @@ class SimonSoundInput : public QObject, public SoundBackendClient
bool activate(SoundClient::SoundClientPriority priority);
bool isActive() { return (m_activeInputClients.count() > 0); }
int bufferSize();
void processData(const QByteArray& data);
......
......@@ -75,7 +75,7 @@ QByteArray SimonSoundOutput::requestData(qint64 maxLen)
if (read <= 0) {
popWhenBufferEmpty = true;
} else popWhenBufferEmpty = false;
QByteArray output = QByteArray().append(toRead, read);
free(toRead);
return output;
......@@ -110,9 +110,9 @@ qint64 SimonSoundOutput::writeData(const char *toWrite, qint64 len)
bool SimonSoundOutput::registerOutputClient(SoundOutputClient* client)
{
kDebug() << "Registering output client";
bool newOut = false;
if (m_activeOutputClient != 0)
m_suspendedOutputClients.insert(0,m_activeOutputClient);
else
......@@ -120,10 +120,10 @@ bool SimonSoundOutput::registerOutputClient(SoundOutputClient* client)
newOut = true;
m_activeOutputClient = client;
if (newOut) //start playback
return m_output->startPlayback(this);
return true;
}
......@@ -131,7 +131,7 @@ bool SimonSoundOutput::registerOutputClient(SoundOutputClient* client)
bool SimonSoundOutput::deRegisterOutputClient(SoundOutputClient* client)
{
kWarning() << "Deregister output client";
if (client != m_activeOutputClient)
//wasn't active anyways
m_suspendedOutputClients.removeAll(client);
......@@ -170,7 +170,7 @@ bool SimonSoundOutput::preparePlayback(SimonSound::DeviceConfiguration& device)
SoundClient::SoundClientPriority SimonSoundOutput::getHighestPriority()
{
SoundClient::SoundClientPriority priority = SoundClient::Background;
if (m_activeOutputClient)
priority = m_activeOutputClient->priority();
......@@ -244,7 +244,7 @@ bool SimonSoundOutput::stopPlayback()
{
if (!haveSomethingToPlay()) {
emit playbackFinished();
SoundServer::getInstance()->closeOutput(this);
if (!haveSomethingToPlay()) {
//we got something while closing the output. This is properly locked in the sound server
......
......@@ -72,14 +72,14 @@ class SimonSoundOutput : public QObject, public SoundBackendClient
SoundClient::SoundClientPriority getHighestPriority();
bool activate(SoundClient::SoundClientPriority priority);
int bufferSize();
qint64 bufferTime();
QByteArray requestData(qint64 maxSize);
void startClientUpdate();
void completeClientUpdate();
void popClient();
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment