Ensure PipeWireOut shuts down cleanly

This commit is contained in:
casey langen 2021-03-15 22:28:31 -07:00
parent dcb4e38921
commit a602b0efaf
2 changed files with 22 additions and 12 deletions

View File

@ -89,12 +89,16 @@ void PipeWireOut::OnStreamProcess(void* data) {
{
std::unique_lock<std::recursive_mutex> lock(self->mutex);
while (self->buffers.empty() && self->state == State::Playing) {
while (self->buffers.empty() && self->state != State::Shutdown) {
std::cerr << "[PipeWire] waiting for data...\n";
self->bufferCondition.wait(lock);
if (self->state == State::Shutdown) {
break;
}
}
if (self->state != State::Playing) {
//std::cerr << "[PipeWire] not playing, so buffers will not be filled.\n";
if (self->state == State::Shutdown) {
std::cerr << "[PipeWire] shutdown detected, so buffers will not be filled.\n";
return;
}
@ -186,24 +190,30 @@ IDevice* PipeWireOut::GetDefaultDevice() {
}
void PipeWireOut::StopPipeWire() {
std::unique_lock<std::recursive_mutex> lock(this->mutex);
std::cerr << "[PipeWire] shutdown started...\n";
{
std::unique_lock<std::recursive_mutex> lock(this->mutex);
this->state = State::Shutdown;
this->bufferCondition.notify_all();
}
if (this->pwThreadLoop) {
pw_thread_loop_stop(this->pwThreadLoop);
this->state = State::Stopped;
this->bufferCondition.notify_all();
if (this->pwStream) {
pw_stream_destroy(this->pwStream);
this->pwStream = nullptr;
}
pw_thread_loop_destroy(this->pwThreadLoop);
this->pwThreadLoop = nullptr;
}
if (this->pwStream) {
pw_stream_destroy(this->pwStream);
this->pwStream = nullptr;
}
this->initialized = false;
std::cerr << "[PipeWire] shutdown complete.\n";
}
bool PipeWireOut::StartPipeWire(IBuffer* buffer) {

View File

@ -102,7 +102,7 @@ class PipeWireOut : public IOutput {
};
enum class State {
Stopped, Paused, Playing
Stopped, Paused, Playing, Shutdown
};
std::deque<BufferContext*> buffers;