TH_CLEAN_3.5: push() cleanup side of things, assure SDRThread::readStream() to actually check for full + make ThreadQueue notify even in case on not-successfull push(), make it spam notify_all() everytime

This commit is contained in:
vsonnier
2016-07-06 21:23:59 +02:00
parent b495b388c9
commit 21c8a81c32
13 changed files with 197 additions and 99 deletions
+38 -18
View File
@@ -8,6 +8,7 @@
#include <memory.h>
#include <mutex>
std::map<int, AudioThread *> AudioThread::deviceController;
std::map<int, int> AudioThread::deviceSampleRate;
std::map<int, std::thread *> AudioThread::deviceThread;
@@ -19,15 +20,11 @@ AudioThread::AudioThread() : IOThread(),
underflowCount.store(0);
active.store(false);
outputDevice.store(-1);
gain.store(1.0);
vBoundThreads = new std::vector<AudioThread *>;
boundThreads.store(vBoundThreads);
gain = 1.0;
}
AudioThread::~AudioThread() {
boundThreads.store(nullptr);
delete vBoundThreads;
}
std::recursive_mutex & AudioThread::getMutex()
@@ -39,8 +36,8 @@ void AudioThread::bindThread(AudioThread *other) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (std::find(boundThreads.load()->begin(), boundThreads.load()->end(), other) == boundThreads.load()->end()) {
boundThreads.load()->push_back(other);
if (std::find(boundThreads.begin(), boundThreads.end(), other) == boundThreads.end()) {
boundThreads.push_back(other);
}
}
@@ -49,9 +46,9 @@ void AudioThread::removeThread(AudioThread *other) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
std::vector<AudioThread *>::iterator i;
i = std::find(boundThreads.load()->begin(), boundThreads.load()->end(), other);
if (i != boundThreads.load()->end()) {
boundThreads.load()->erase(i);
i = std::find(boundThreads.begin(), boundThreads.end(), other);
if (i != boundThreads.end()) {
boundThreads.erase(i);
}
}
@@ -85,7 +82,7 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned
std::cout << "Audio buffer underflow.." << (src->underflowCount++) << std::endl;
}
if (src->boundThreads.load()->empty()) {
if (src->boundThreads.empty()) {
return 0;
}
@@ -93,9 +90,9 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned
double peak = 0.0;
//for all boundThreads
for (size_t j = 0; j < src->boundThreads.load()->size(); j++) {
for (size_t j = 0; j < src->boundThreads.size(); j++) {
AudioThread *srcmix = (*(src->boundThreads.load()))[j];
AudioThread *srcmix = src->boundThreads[j];
//lock every single boundThread srcmix in succession the time we process
//its audio samples.
@@ -276,6 +273,7 @@ void AudioThread::enumerateDevices(std::vector<RtAudio::DeviceInfo> &devs) {
}
void AudioThread::setDeviceSampleRate(int deviceId, int sampleRate) {
if (deviceController.find(deviceId) != deviceController.end()) {
AudioThreadCommand refreshDevice;
@@ -286,16 +284,17 @@ void AudioThread::setDeviceSampleRate(int deviceId, int sampleRate) {
}
void AudioThread::setSampleRate(int sampleRate) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (deviceController[outputDevice.load()] == this) {
deviceSampleRate[outputDevice.load()] = sampleRate;
dac.stopStream();
dac.closeStream();
std::lock_guard<std::recursive_mutex> lock(m_mutex);
for (size_t j = 0; j < boundThreads.load()->size(); j++) {
AudioThread *srcmix = (*(boundThreads.load()))[j];
for (size_t j = 0; j < boundThreads.size(); j++) {
AudioThread *srcmix = boundThreads[j];
srcmix->setSampleRate(sampleRate);
}
@@ -318,10 +317,15 @@ void AudioThread::setSampleRate(int sampleRate) {
}
int AudioThread::getSampleRate() {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
return this->sampleRate;
}
void AudioThread::setupDevice(int deviceId) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
parameters.deviceId = deviceId;
parameters.nChannels = 2;
parameters.firstChannel = 0;
@@ -373,6 +377,8 @@ void AudioThread::setupDevice(int deviceId) {
}
int AudioThread::getOutputDevice() {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (outputDevice == -1) {
return dac.getDefaultOutputDevice();
}
@@ -380,6 +386,9 @@ int AudioThread::getOutputDevice() {
}
void AudioThread::setInitOutputDevice(int deviceId, int sampleRate) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
outputDevice = deviceId;
if (sampleRate == -1) {
if (deviceSampleRate.find(parameters.deviceId) != deviceSampleRate.end()) {
@@ -415,6 +424,7 @@ void AudioThread::run() {
//Infinite loop, witing for commands or for termination
while (!stopping) {
AudioThreadCommand command;
cmdQueue.pop(command);
if (command.cmd == AudioThreadCommand::AUDIO_THREAD_CMD_SET_DEVICE) {
@@ -470,10 +480,14 @@ void AudioThread::terminate() {
}
bool AudioThread::isActive() {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
return active;
}
void AudioThread::setActive(bool state) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
AudioThreadInput *dummy;
if (state && !active && inputQueue) {
@@ -500,6 +514,9 @@ AudioThreadCommandQueue *AudioThread::getCommandQueue() {
}
void AudioThread::setGain(float gain_in) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (gain < 0.0) {
gain = 0.0;
}
@@ -510,5 +527,8 @@ void AudioThread::setGain(float gain_in) {
}
float AudioThread::getGain() {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
return gain;
}
+5 -4
View File
@@ -57,7 +57,7 @@ public:
std::atomic_bool initialized;
std::atomic_bool active;
std::atomic_int outputDevice;
std::atomic<float> gain;
float gain;
AudioThread();
~AudioThread();
@@ -88,7 +88,7 @@ private:
AudioThreadCommandQueue cmdQueue;
int sampleRate;
//The own m_mutex protecting this AudioThread
//The own m_mutex protecting this AudioThread, in particular boundThreads
std::recursive_mutex m_mutex;
public:
@@ -103,7 +103,8 @@ public:
static std::map<int,std::thread *> deviceThread;
static void deviceCleanup();
static void setDeviceSampleRate(int deviceId, int sampleRate);
std::atomic<std::vector<AudioThread *> *> boundThreads;
std::vector<AudioThread *> *vBoundThreads;
//protected by m_mutex
std::vector<AudioThread *> boundThreads;
};