AudioThread: re-apply consistent formating

This commit is contained in:
vsonnier 2018-01-22 07:10:45 +01:00
parent fc1c1c3b4d
commit 033bbd07df
2 changed files with 166 additions and 157 deletions

View File

@ -22,23 +22,23 @@ std::recursive_mutex AudioThread::m_device_mutex;
AudioThread::AudioThread() : IOThread(), nBufferFrames(1024), sampleRate(0), controllerThread(nullptr) {
audioQueuePtr = 0;
underflowCount = 0;
active.store(false);
outputDevice.store(-1);
audioQueuePtr = 0;
underflowCount = 0;
active.store(false);
outputDevice.store(-1);
gain = 1.0;
}
AudioThread::~AudioThread() {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (controllerThread != nullptr) {
if (controllerThread != nullptr) {
controllerThread->join();
delete controllerThread;
controllerThread->join();
delete controllerThread;
controllerThread = nullptr;
}
controllerThread = nullptr;
}
}
std::recursive_mutex & AudioThread::getMutex()
@ -48,14 +48,14 @@ std::recursive_mutex & AudioThread::getMutex()
void AudioThread::attachControllerThread(std::thread* controllerThread_in) {
//cleanup previous (should never happen)
if (controllerThread != nullptr) {
//cleanup previous (should never happen)
if (controllerThread != nullptr) {
controllerThread->join();
delete controllerThread;
}
controllerThread->join();
delete controllerThread;
}
controllerThread = controllerThread_in;
controllerThread = controllerThread_in;
}
void AudioThread::bindThread(AudioThread *other) {
@ -69,7 +69,7 @@ void AudioThread::bindThread(AudioThread *other) {
void AudioThread::removeThread(AudioThread *other) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
std::lock_guard<std::recursive_mutex> lock(m_mutex);
auto i = std::find(boundThreads.begin(), boundThreads.end(), other);
@ -80,15 +80,15 @@ void AudioThread::removeThread(AudioThread *other) {
void AudioThread::deviceCleanup() {
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
// only notify, let the thread die by itself.
for (auto i = deviceController.begin(); i != deviceController.end(); i++) {
i->second->terminate();
}
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
// only notify, let the thread die by itself.
for (auto i = deviceController.begin(); i != deviceController.end(); i++) {
i->second->terminate();
}
}
static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned int nBufferFrames, double /* streamTime */, RtAudioStreamStatus status,
void *userData) {
void *userData) {
float *out = (float*)outputBuffer;
@ -96,10 +96,10 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned
//actually active.
::memset(out, 0, nBufferFrames * 2 * sizeof(float));
//src in the controller thread:
AudioThread *src = (AudioThread *) userData;
//src in the controller thread:
AudioThread *src = (AudioThread *)userData;
//by construction, src is a controller thread, from deviceController:
//by construction, src is a controller thread, from deviceController:
std::lock_guard<std::recursive_mutex> lock(src->getMutex());
if (src->isTerminated()) {
@ -107,15 +107,15 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned
}
if (status) {
std::cout << "Audio buffer underflow.." << (src->underflowCount++) << std::endl << std::flush;
std::cout << "Audio buffer underflow.." << (src->underflowCount++) << std::endl << std::flush;
}
double peak = 0.0;
//Process the bound threads audio:
//Process the bound threads audio:
for (size_t j = 0; j < src->boundThreads.size(); j++) {
AudioThread *srcmix = src->boundThreads[j];
AudioThread *srcmix = src->boundThreads[j];
//lock every single boundThread srcmix in succession the time we process
//its audio samples.
@ -201,7 +201,8 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned
}
srcmix->audioQueuePtr++;
}
} else {
}
else {
for (int i = 0, iMax = srcmix->currentInput->channels * nBufferFrames; i < iMax; i++) {
if (srcmix->audioQueuePtr >= srcmix->currentInput->data.size()) {
@ -297,42 +298,42 @@ void AudioThread::enumerateDevices(std::vector<RtAudio::DeviceInfo> &devs) {
void AudioThread::setDeviceSampleRate(int deviceId, int sampleRate) {
AudioThread* matchingControllerThread = nullptr;
AudioThread* matchingControllerThread = nullptr;
//scope lock here to minimize the common unique static lock contention
{
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
//scope lock here to minimize the common unique static lock contention
{
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
if (deviceController.find(deviceId) != deviceController.end()) {
if (deviceController.find(deviceId) != deviceController.end()) {
matchingControllerThread = deviceController[deviceId];
}
}
matchingControllerThread = deviceController[deviceId];
}
}
//out-of-lock test
if (matchingControllerThread != nullptr) {
//out-of-lock test
if (matchingControllerThread != nullptr) {
AudioThreadCommand refreshDevice;
refreshDevice.cmd = AudioThreadCommand::AUDIO_THREAD_CMD_SET_SAMPLE_RATE;
refreshDevice.int_value = sampleRate;
//VSO : blocking push !
matchingControllerThread->getCommandQueue()->push(refreshDevice);
}
AudioThreadCommand refreshDevice;
refreshDevice.cmd = AudioThreadCommand::AUDIO_THREAD_CMD_SET_SAMPLE_RATE;
refreshDevice.int_value = sampleRate;
//VSO : blocking push !
matchingControllerThread->getCommandQueue()->push(refreshDevice);
}
}
void AudioThread::setSampleRate(int sampleRate) {
bool thisIsAController = false;
bool thisIsAController = false;
//scope lock here to minimize the common unique static lock contention
{
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
//scope lock here to minimize the common unique static lock contention
{
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
if (deviceController[outputDevice.load()] == this) {
thisIsAController = true;
deviceSampleRate[outputDevice.load()] = sampleRate;
}
}
if (deviceController[outputDevice.load()] == this) {
thisIsAController = true;
deviceSampleRate[outputDevice.load()] = sampleRate;
}
}
std::lock_guard<std::recursive_mutex> lock(m_mutex);
@ -341,13 +342,13 @@ void AudioThread::setSampleRate(int sampleRate) {
dac.stopStream();
dac.closeStream();
//Set bounded sample rate:
//Set bounded sample rate:
for (size_t j = 0; j < boundThreads.size(); j++) {
AudioThread *srcmix = boundThreads[j];
// the controller thread is part of the boundedThreads, so prevent infinite recursion:
if (srcmix != this) {
srcmix->setSampleRate(sampleRate);
}
// the controller thread is part of the boundedThreads, so prevent infinite recursion:
if (srcmix != this) {
srcmix->setSampleRate(sampleRate);
}
}
//make a local copy, snapshot of the list of demodulators
@ -359,7 +360,7 @@ void AudioThread::setSampleRate(int sampleRate) {
}
}
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &nBufferFrames, &audioCallback, (void *) this, &opts);
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &nBufferFrames, &audioCallback, (void *)this, &opts);
dac.startStream();
}
@ -374,8 +375,8 @@ int AudioThread::getSampleRate() {
void AudioThread::setupDevice(int deviceId) {
//global lock to setup the device...
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
//global lock to setup the device...
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
parameters.deviceId = deviceId;
parameters.nChannels = 2;
@ -385,10 +386,10 @@ void AudioThread::setupDevice(int deviceId) {
try {
if (deviceController.find(outputDevice.load()) != deviceController.end()) {
//'this' is not the controller, so remove it from the bounded list:
//beware, we must take the controller mutex, because the audio callback may use the list of bounded
//threads at that moment:
std::lock_guard<std::recursive_mutex> lock(deviceController[outputDevice.load()]->getMutex());
//'this' is not the controller, so remove it from the bounded list:
//beware, we must take the controller mutex, because the audio callback may use the list of bounded
//threads at that moment:
std::lock_guard<std::recursive_mutex> lock(deviceController[outputDevice.load()]->getMutex());
deviceController[outputDevice.load()]->removeThread(this);
}
@ -400,39 +401,43 @@ void AudioThread::setupDevice(int deviceId) {
if (deviceSampleRate.find(parameters.deviceId) != deviceSampleRate.end()) {
sampleRate = deviceSampleRate[parameters.deviceId];
} else {
std::cout << "Error, device sample rate wasn't initialized?" << std::endl;
return;
// sampleRate = AudioThread::getDefaultAudioSampleRate();
// deviceSampleRate[parameters.deviceId] = sampleRate;
}
else {
std::cout << "Error, device sample rate wasn't initialized?" << std::endl;
return;
// sampleRate = AudioThread::getDefaultAudioSampleRate();
// deviceSampleRate[parameters.deviceId] = sampleRate;
}
//Create a new controller:
//Create a new controller:
if (deviceController.find(parameters.deviceId) == deviceController.end()) {
//Create a new controller thread for parameters.deviceId:
//Create a new controller thread for parameters.deviceId:
deviceController[parameters.deviceId] = new AudioThread();
deviceController[parameters.deviceId]->setInitOutputDevice(parameters.deviceId, sampleRate);
deviceController[parameters.deviceId]->bindThread(this);
deviceController[parameters.deviceId]->attachControllerThread(new std::thread(&AudioThread::threadMain, deviceController[parameters.deviceId]));
deviceController[parameters.deviceId]->attachControllerThread(new std::thread(&AudioThread::threadMain, deviceController[parameters.deviceId]));
} else if (deviceController[parameters.deviceId] == this) {
}
else if (deviceController[parameters.deviceId] == this) {
//Attach callback
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &nBufferFrames, &audioCallback, (void *)this, &opts);
dac.startStream();
} else {
//we are a bound thread, add ourselves to the controller deviceController[parameters.deviceId].
//beware, we must take the controller mutex, because the audio callback may use the list of bounded
//threads at that moment:
std::lock_guard<std::recursive_mutex> lock(deviceController[parameters.deviceId]->getMutex());
//Attach callback
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &nBufferFrames, &audioCallback, (void *)this, &opts);
dac.startStream();
}
else {
//we are a bound thread, add ourselves to the controller deviceController[parameters.deviceId].
//beware, we must take the controller mutex, because the audio callback may use the list of bounded
//threads at that moment:
std::lock_guard<std::recursive_mutex> lock(deviceController[parameters.deviceId]->getMutex());
deviceController[parameters.deviceId]->bindThread(this);
}
active = true;
} catch (RtAudioError& e) {
}
catch (RtAudioError& e) {
e.printMessage();
return;
}
@ -453,15 +458,16 @@ int AudioThread::getOutputDevice() {
void AudioThread::setInitOutputDevice(int deviceId, int sampleRate) {
//global lock
//global lock
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
outputDevice = deviceId;
if (sampleRate == -1) {
if (deviceSampleRate.find(parameters.deviceId) != deviceSampleRate.end()) {
sampleRate = deviceSampleRate[deviceId];
sampleRate = deviceSampleRate[deviceId];
}
} else {
}
else {
deviceSampleRate[deviceId] = sampleRate;
}
this->sampleRate = sampleRate;
@ -470,12 +476,12 @@ void AudioThread::setInitOutputDevice(int deviceId, int sampleRate) {
void AudioThread::run() {
#ifdef __APPLE__
pthread_t tID = pthread_self(); // ID of this thread
int priority = sched_get_priority_max( SCHED_RR) - 1;
sched_param prio = {priority}; // scheduling priority of thread
int priority = sched_get_priority_max(SCHED_RR) - 1;
sched_param prio = { priority }; // scheduling priority of thread
pthread_setschedparam(tID, SCHED_RR, &prio);
#endif
// std::cout << "Audio thread initializing.." << std::endl;
// std::cout << "Audio thread initializing.." << std::endl;
if (dac.getDeviceCount() < 1) {
std::cout << "No audio devices found!" << std::endl;
@ -484,7 +490,7 @@ void AudioThread::run() {
setupDevice((outputDevice.load() == -1) ? (dac.getDefaultOutputDevice()) : outputDevice.load());
// std::cout << "Audio thread started." << std::endl;
// std::cout << "Audio thread started." << std::endl;
inputQueue = std::static_pointer_cast<AudioThreadInputQueue>(getInputQueue("AudioDataInput"));
@ -509,21 +515,22 @@ void AudioThread::run() {
inputQueue->flush();
}
//Nullify currentInput...
currentInput = nullptr;
//Nullify currentInput...
currentInput = nullptr;
//Stop : this affects the device list , so must be protected globally.
std::lock_guard<std::recursive_mutex> global_lock(m_device_mutex);
std::lock_guard<std::recursive_mutex> global_lock(m_device_mutex);
if (deviceController[parameters.deviceId] != this) {
//'this' is not the controller, so remove it from the bounded list:
//beware, we must take the controller mutex, because the audio callback may use the list of bounded
//threads at that moment:
std::lock_guard<std::recursive_mutex> lock(deviceController[parameters.deviceId]->getMutex());
//'this' is not the controller, so remove it from the bounded list:
//beware, we must take the controller mutex, because the audio callback may use the list of bounded
//threads at that moment:
std::lock_guard<std::recursive_mutex> lock(deviceController[parameters.deviceId]->getMutex());
deviceController[parameters.deviceId]->removeThread(this);
} else {
// 'this' is a controller thread:
}
else {
// 'this' is a controller thread:
try {
if (dac.isStreamOpen()) {
if (dac.isStreamRunning()) {
@ -531,12 +538,13 @@ void AudioThread::run() {
}
dac.closeStream();
}
} catch (RtAudioError& e) {
}
catch (RtAudioError& e) {
e.printMessage();
}
}
// std::cout << "Audio thread done." << std::endl;
// std::cout << "Audio thread done." << std::endl;
}
void AudioThread::terminate() {
@ -551,32 +559,33 @@ bool AudioThread::isActive() {
void AudioThread::setActive(bool state) {
AudioThread* matchingControllerThread = nullptr;
AudioThread* matchingControllerThread = nullptr;
std::lock_guard<std::recursive_mutex> lock(m_mutex);
std::lock_guard<std::recursive_mutex> lock(m_mutex);
//scope lock here to minimize the common unique static lock contention
{
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
//scope lock here to minimize the common unique static lock contention
{
std::lock_guard<std::recursive_mutex> lock(m_device_mutex);
if (deviceController.find(parameters.deviceId) != deviceController.end()) {
if (deviceController.find(parameters.deviceId) != deviceController.end()) {
matchingControllerThread = deviceController[parameters.deviceId];
}
}
matchingControllerThread = deviceController[parameters.deviceId];
}
}
if (matchingControllerThread == nullptr) {
return;
}
if (state && !active && inputQueue) {
matchingControllerThread->bindThread(this);
} else if (!state && active) {
matchingControllerThread->removeThread(this);
matchingControllerThread->bindThread(this);
}
else if (!state && active) {
matchingControllerThread->removeThread(this);
}
// Activity state changing, clear any inputs
if(inputQueue) {
if (inputQueue) {
inputQueue->flush();
}
active = state;

View File

@ -21,12 +21,12 @@ public:
int channels;
float peak;
int type;
bool is_squelch_active;
bool is_squelch_active;
std::vector<float> data;
AudioThreadInput() :
frequency(0), inputRate(0), sampleRate(0), channels(0), peak(0), type(0), is_squelch_active(false) {
frequency(0), inputRate(0), sampleRate(0), channels(0), peak(0), type(0), is_squelch_active(false) {
}
@ -65,7 +65,7 @@ public:
};
AudioThreadCommand() :
cmd(AUDIO_THREAD_CMD_NULL), int_value(0) {
cmd(AUDIO_THREAD_CMD_NULL), int_value(0) {
}
AudioThreadCommandEnum cmd;
@ -87,7 +87,7 @@ public:
static void enumerateDevices(std::vector<RtAudio::DeviceInfo> &devs);
void setInitOutputDevice(int deviceId, int sampleRate=-1);
void setInitOutputDevice(int deviceId, int sampleRate = -1);
int getOutputDevice();
int getSampleRate();
@ -110,8 +110,8 @@ public:
static void deviceCleanup();
static void setDeviceSampleRate(int deviceId, int sampleRate);
//
void attachControllerThread(std::thread* controllerThread);
//
void attachControllerThread(std::thread* controllerThread);
//fields below, only to be used by other AudioThreads !
size_t underflowCount;
@ -134,8 +134,8 @@ private:
AudioThreadCommandQueue cmdQueue;
int sampleRate;
//if != nullptr, it mean AudioThread is a controller thread.
std::thread* controllerThread = nullptr;
//if != nullptr, it mean AudioThread is a controller thread.
std::thread* controllerThread = nullptr;
//The own m_mutex protecting this AudioThread, in particular boundThreads
std::recursive_mutex m_mutex;
@ -146,8 +146,8 @@ private:
void bindThread(AudioThread *other);
void removeThread(AudioThread *other);
static std::map<int, AudioThread* > deviceController;
static std::map<int, AudioThread* > deviceController;
//The mutex protecting static deviceController, deviceThread and deviceSampleRate access.
static std::recursive_mutex m_device_mutex;
//The mutex protecting static deviceController, deviceThread and deviceSampleRate access.
static std::recursive_mutex m_device_mutex;
};