Skip to content

Commit

Permalink
Add locking to fix occasional crashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiw committed Dec 24, 2024
1 parent 5c48e78 commit 63af42a
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/audio/PulseAudioDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,11 @@ void PulseAudioDevice::StreamReadCallback_(pa_stream *s, size_t length, void *us
{
size_t len = length;
const void* data = nullptr;
pa_threaded_mainloop_lock(thisObj->mainloop_);
pa_stream_peek(s, &data, &len);
if (!data || len == 0)
{
pa_threaded_mainloop_unlock(thisObj->mainloop_);
break;
}

Expand All @@ -272,6 +274,7 @@ void PulseAudioDevice::StreamReadCallback_(pa_stream *s, size_t length, void *us
}

pa_stream_drop(s);
pa_threaded_mainloop_unlock(thisObj->mainloop_);

int timeToSleep = (double)samples / (double)thisObj->sampleRate_ * 1000;
std::this_thread::sleep_until(currentTime + std::chrono::milliseconds(timeToSleep));
Expand All @@ -281,7 +284,10 @@ void PulseAudioDevice::StreamReadCallback_(pa_stream *s, size_t length, void *us
// Default to a 20ms sleep time if we currently aren't able to RX samples
std::this_thread::sleep_until(currentTime + 20ms);
}

pa_threaded_mainloop_lock(thisObj->mainloop_);
readableSize = pa_stream_readable_size(s);
pa_threaded_mainloop_unlock(thisObj->mainloop_);
} while (thisObj->outputPendingThreadActive_);
});
}
Expand Down

0 comments on commit 63af42a

Please sign in to comment.