Audio input: removed mutex lock on write data. Audio output: removed mutex lock on read data

pull/27/head
f4exb 2016-12-25 20:04:19 +01:00
rodzic e05822ba02
commit 53f6413bd9
2 zmienionych plików z 189 dodań i 187 usunięć

Wyświetl plik

@ -1,183 +1,184 @@
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016 Edouard Griffiths, F4EXB // // Copyright (C) 2016 Edouard Griffiths, F4EXB //
// // // //
// This program is free software; you can redistribute it and/or modify // // This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by // // it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or // // the Free Software Foundation as version 3 of the License, or //
// // // //
// This program is distributed in the hope that it will be useful, // // This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of // // but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. // // GNU General Public License V3 for more details. //
// // // //
// You should have received a copy of the GNU General Public License // // You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. // // along with this program. If not, see <http://www.gnu.org/licenses/>. //
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
#include <string.h> #include <string.h>
#include <QAudioFormat> #include <QAudioFormat>
#include <QAudioDeviceInfo> #include <QAudioDeviceInfo>
#include <QAudioInput> #include <QAudioInput>
#include "audio/audioinput.h" #include "audio/audioinput.h"
#include "audio/audiofifo.h" #include "audio/audiofifo.h"
AudioInput::AudioInput() : AudioInput::AudioInput() :
m_mutex(), m_mutex(),
m_audioInput(0), m_audioInput(0),
m_audioUsageCount(0), m_audioUsageCount(0),
m_onExit(false), m_onExit(false),
m_audioFifos() m_audioFifos()
{ {
} }
AudioInput::~AudioInput() AudioInput::~AudioInput()
{ {
stop(); stop();
QMutexLocker mutexLocker(&m_mutex); QMutexLocker mutexLocker(&m_mutex);
for (AudioFifos::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it) for (AudioFifos::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it)
{ {
delete *it; delete *it;
} }
m_audioFifos.clear(); m_audioFifos.clear();
} }
bool AudioInput::start(int device, int rate) bool AudioInput::start(int device, int rate)
{ {
QMutexLocker mutexLocker(&m_mutex); QMutexLocker mutexLocker(&m_mutex);
if (m_audioUsageCount == 0) if (m_audioUsageCount == 0)
{ {
QAudioDeviceInfo devInfo; QAudioDeviceInfo devInfo;
if (device < 0) if (device < 0)
{ {
devInfo = QAudioDeviceInfo::defaultInputDevice(); devInfo = QAudioDeviceInfo::defaultInputDevice();
qWarning("AudioInput::start: using default device %s", qPrintable(devInfo.defaultInputDevice().deviceName())); qWarning("AudioInput::start: using default device %s", qPrintable(devInfo.defaultInputDevice().deviceName()));
} }
else else
{ {
QList<QAudioDeviceInfo> devicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); QList<QAudioDeviceInfo> devicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
if (device < devicesInfo.size()) if (device < devicesInfo.size())
{ {
devInfo = devicesInfo[device]; devInfo = devicesInfo[device];
qWarning("AudioInput::start: using audio device #%d: %s", device, qPrintable(devInfo.defaultInputDevice().deviceName())); qWarning("AudioInput::start: using audio device #%d: %s", device, qPrintable(devInfo.defaultInputDevice().deviceName()));
} }
else else
{ {
devInfo = QAudioDeviceInfo::defaultInputDevice(); devInfo = QAudioDeviceInfo::defaultInputDevice();
qWarning("AudioInput::start: audio device #%d does not exist. Using default device %s", device, qPrintable(devInfo.defaultInputDevice().deviceName())); qWarning("AudioInput::start: audio device #%d does not exist. Using default device %s", device, qPrintable(devInfo.defaultInputDevice().deviceName()));
} }
} }
//QAudioDeviceInfo devInfo(QAudioDeviceInfo::defaultOutputDevice()); //QAudioDeviceInfo devInfo(QAudioDeviceInfo::defaultOutputDevice());
m_audioFormat.setSampleRate(rate); m_audioFormat.setSampleRate(rate);
m_audioFormat.setChannelCount(2); m_audioFormat.setChannelCount(2);
m_audioFormat.setSampleSize(16); m_audioFormat.setSampleSize(16);
m_audioFormat.setCodec("audio/pcm"); m_audioFormat.setCodec("audio/pcm");
m_audioFormat.setByteOrder(QAudioFormat::LittleEndian); m_audioFormat.setByteOrder(QAudioFormat::LittleEndian);
m_audioFormat.setSampleType(QAudioFormat::SignedInt); m_audioFormat.setSampleType(QAudioFormat::SignedInt);
if (!devInfo.isFormatSupported(m_audioFormat)) if (!devInfo.isFormatSupported(m_audioFormat))
{ {
m_audioFormat = devInfo.nearestFormat(m_audioFormat); m_audioFormat = devInfo.nearestFormat(m_audioFormat);
qWarning("AudioInput::start: %d Hz S16_LE audio format not supported. New rate: %d", rate, m_audioFormat.sampleRate()); qWarning("AudioInput::start: %d Hz S16_LE audio format not supported. New rate: %d", rate, m_audioFormat.sampleRate());
} }
if (m_audioFormat.sampleSize() != 16) if (m_audioFormat.sampleSize() != 16)
{ {
qWarning("AudioInput::start: Audio device ( %s ) failed", qPrintable(devInfo.defaultInputDevice().deviceName())); qWarning("AudioInput::start: Audio device ( %s ) failed", qPrintable(devInfo.defaultInputDevice().deviceName()));
return false; return false;
} }
m_audioInput = new QAudioInput(devInfo, m_audioFormat); m_audioInput = new QAudioInput(devInfo, m_audioFormat);
QIODevice::open(QIODevice::ReadWrite); QIODevice::open(QIODevice::ReadWrite);
m_audioInput->start(this); m_audioInput->start(this);
if (m_audioInput->state() != QAudio::ActiveState) if (m_audioInput->state() != QAudio::ActiveState)
{ {
qWarning("AudioInput::start: cannot start"); qWarning("AudioInput::start: cannot start");
} }
} }
m_audioUsageCount++; m_audioUsageCount++;
return true; return true;
} }
void AudioInput::stop() void AudioInput::stop()
{ {
qDebug("AudioInput::stop"); qDebug("AudioInput::stop");
QMutexLocker mutexLocker(&m_mutex); QMutexLocker mutexLocker(&m_mutex);
if (m_audioUsageCount > 0) if (m_audioUsageCount > 0)
{ {
m_audioUsageCount--; m_audioUsageCount--;
if (m_audioUsageCount == 0) if (m_audioUsageCount == 0)
{ {
QIODevice::close(); QIODevice::close();
if (!m_onExit) { if (!m_onExit) {
delete m_audioInput; delete m_audioInput;
} }
} }
} }
} }
void AudioInput::addFifo(AudioFifo* audioFifo) void AudioInput::addFifo(AudioFifo* audioFifo)
{ {
QMutexLocker mutexLocker(&m_mutex); QMutexLocker mutexLocker(&m_mutex);
m_audioFifos.push_back(audioFifo); m_audioFifos.push_back(audioFifo);
} }
void AudioInput::removeFifo(AudioFifo* audioFifo) void AudioInput::removeFifo(AudioFifo* audioFifo)
{ {
QMutexLocker mutexLocker(&m_mutex); QMutexLocker mutexLocker(&m_mutex);
m_audioFifos.remove(audioFifo); m_audioFifos.remove(audioFifo);
} }
qint64 AudioInput::readData(char* data, qint64 maxLen) qint64 AudioInput::readData(char* data, qint64 maxLen)
{ {
Q_UNUSED(data); Q_UNUSED(data);
Q_UNUSED(maxLen); Q_UNUSED(maxLen);
return 0; return 0;
} }
qint64 AudioInput::writeData(const char *data, qint64 len) qint64 AudioInput::writeData(const char *data, qint64 len)
{ {
// @TODO: Study this mutex on OSX, for now deadlocks possible // Study this mutex on OSX, for now deadlocks possible
#ifndef __APPLE__ // Removed as it may indeed cause lockups and is in fact useless.
QMutexLocker mutexLocker(&m_mutex); //#ifndef __APPLE__
#endif // QMutexLocker mutexLocker(&m_mutex);
//#endif
if ((m_audioFormat.sampleSize() != 16)
|| (m_audioFormat.sampleType() != QAudioFormat::SignedInt) if ((m_audioFormat.sampleSize() != 16)
|| (m_audioFormat.byteOrder() != QAudioFormat::LittleEndian)) || (m_audioFormat.sampleType() != QAudioFormat::SignedInt)
{ || (m_audioFormat.byteOrder() != QAudioFormat::LittleEndian))
qCritical("AudioInput::writeData: invalid format not S16LE"); {
return 0; qCritical("AudioInput::writeData: invalid format not S16LE");
} return 0;
}
if (m_audioFormat.channelCount() != 2) {
qCritical("AudioInput::writeData: invalid format not stereo"); if (m_audioFormat.channelCount() != 2) {
return 0; qCritical("AudioInput::writeData: invalid format not stereo");
} return 0;
}
for (AudioFifos::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it)
{ for (AudioFifos::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it)
(*it)->write(reinterpret_cast<const quint8*>(data), len/4, 10); {
} (*it)->write(reinterpret_cast<const quint8*>(data), len/4, 10);
}
return len;
} return len;
}

Wyświetl plik

@ -158,10 +158,11 @@ qint64 AudioOutput::readData(char* data, qint64 maxLen)
{ {
//qDebug("AudioOutput::readData: %lld", maxLen); //qDebug("AudioOutput::readData: %lld", maxLen);
// @TODO: Study this mutex on OSX, for now deadlocks possible // Study this mutex on OSX, for now deadlocks possible
#ifndef __APPLE__ // Removed as it may indeed cause lockups and is in fact useless.
QMutexLocker mutexLocker(&m_mutex); //#ifndef __APPLE__
#endif // QMutexLocker mutexLocker(&m_mutex);
//#endif
unsigned int framesPerBuffer = maxLen / 4; unsigned int framesPerBuffer = maxLen / 4;