background Thread for soundcleanup

This commit is contained in:
mrbesen 2021-12-18 15:34:27 +01:00
parent 5d2beeb643
commit f92d6c07d8
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
3 changed files with 68 additions and 27 deletions

View File

@ -5,6 +5,7 @@
#include <string>
#include <list>
#include <mutex>
#include <thread>
#include <vector>
#include "sounddevice.h"
@ -12,7 +13,7 @@
class Sound {
public:
static Sound& instance();
static void deinit();
static void deinitInstance();
void addPlayback(const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t endms = 0);
bool addDefaultDevice();
@ -25,8 +26,20 @@ private:
Sound();
~Sound();
ma_context context;
void deinit();
void init();
void backgroundThreadLoop();
void cleanAllDecoders();
void startBackgroundThread();
void stopBackgroundThread();
ma_context context;
bool threadShouldrun = false;
std::thread backgroundCleanup;
std::mutex devicesMutex;
std::vector<SoundDevice*> devices;
static Sound* inst;

View File

@ -44,7 +44,7 @@ MainWindow::~MainWindow() {
delete right;
delete play;
Sound::deinit();
Sound::deinitInstance();
}
void MainWindow::reloadConfig() {

View File

@ -54,7 +54,7 @@ Sound& Sound::instance() {
return *inst;
}
void Sound::deinit() {
void Sound::deinitInstance() {
if(inst) {
inst->stopAll();
delete inst;
@ -68,6 +68,7 @@ void Sound::addPlayback(const std::string& name, float volume, uint64_t beginms,
if(volume < 0.00001f)
volume = 0.00001f;
std::lock_guard<std::mutex> lock(devicesMutex);
for(SoundDevice* sd : devices) {
sd->addPlayback(name, volume, beginms, endms);
}
@ -76,6 +77,7 @@ void Sound::addPlayback(const std::string& name, float volume, uint64_t beginms,
bool Sound::addDefaultDevice() {
SoundDevice* sd = SoundDevice::createDevice(&context);
if(sd) {
std::lock_guard<std::mutex> lock(devicesMutex);
devices.push_back(sd);
}
return sd;
@ -84,12 +86,15 @@ bool Sound::addDefaultDevice() {
bool Sound::addDeviceWithName(const std::string& name) {
SoundDevice* sd = (name.empty() ? SoundDevice::createDevice(&context) : SoundDevice::createDevice(&context, name));
if(sd) {
std::lock_guard<std::mutex> lock(devicesMutex);
devices.push_back(sd);
}
return sd;
}
void Sound::stopAll() {
std::lock_guard<std::mutex> lock(devicesMutex);
for(SoundDevice* sd : devices) {
sd->stop();
}
@ -98,41 +103,64 @@ void Sound::stopAll() {
void Sound::reset() {
Log::info << "ResetAudio";
// deinit
stopAll();
for(SoundDevice* sd : devices) {
delete sd;
}
devices.clear();
ma_context_uninit(&context);
deinit();
init();
// reinit
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
Log::error << "Failed to initialize Sound context.";
throw new std::exception();
}
Log::info << "ResetAudio done";
}
Sound::Sound() {
init();
startBackgroundThread();
}
Sound::~Sound() {
deinit();
stopBackgroundThread();
}
void Sound::deinit() {
stopAll();
std::lock_guard<std::mutex> lock(devicesMutex);
for(SoundDevice* sd : devices) {
delete sd;
}
devices.clear();
ma_context_uninit(&context);
}
void Sound::init() {
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
Log::error << "Failed to initialize Sound context.";
throw new std::exception();
}
}
Sound::~Sound() {
stopAll();
for(SoundDevice* sd : devices) {
delete sd;
}
devices.clear();
ma_context_uninit(&context);
}
void Sound::backgroundThreadLoop() {
while(threadShouldrun) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
cleanAllDecoders();
}
}
void Sound::cleanAllDecoders() {
std::lock_guard<std::mutex> lock(devicesMutex);
for(SoundDevice* sd : devices) {
sd->cleanupDecoders();
}
}
void Sound::startBackgroundThread() {
threadShouldrun = true;
backgroundCleanup = std::thread(&Sound::backgroundThreadLoop, this);
}
void Sound::stopBackgroundThread() {
threadShouldrun = false;
if(backgroundCleanup.joinable()) {
backgroundCleanup.join();
}
}