aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-09-20 11:01:34 +0200
committerdec05eba <dec05eba@protonmail.com>2022-09-20 11:01:34 +0200
commit2b7729efb989a8ad7c7c5fa6cd905e41facc0c26 (patch)
tree014984646f0356d624c75daa92299a51d2b11a41
parent919890b7b2eb16fc57439e6c9b8b28183febc467 (diff)
Fix build on ubuntu, use a mainloop with timeout instead of sleep
-rw-r--r--src/sound.cpp22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/sound.cpp b/src/sound.cpp
index 9ca1381..ab0450e 100644
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -154,6 +154,15 @@ fail:
return NULL;
}
+static void pa_sound_device_mainloop_timed(pa_handle *p, int64_t timeout_ms) {
+ const double start_time = clock_get_monotonic_seconds();
+ while((clock_get_monotonic_seconds() - start_time) * 1000.0 < timeout_ms) {
+ pa_mainloop_prepare(p->mainloop, 1 * 1000);
+ pa_mainloop_poll(p->mainloop);
+ pa_mainloop_dispatch(p->mainloop);
+ }
+}
+
// Returns a negative value on failure. Always blocks a time specified matching the sampling rate of the audio.
static int pa_sound_device_read(pa_handle *p, void *data, size_t length) {
assert(p);
@@ -173,28 +182,21 @@ static int pa_sound_device_read(pa_handle *p, void *data, size_t length) {
break;
retry = false;
-
- const double start_time = clock_get_monotonic_seconds();
- while((clock_get_monotonic_seconds() - start_time) * 1000.0 < timeout_ms) {
- pa_mainloop_prepare(p->mainloop, 1 * 1000);
- pa_mainloop_poll(p->mainloop);
- pa_mainloop_dispatch(p->mainloop);
- }
-
+ pa_sound_device_mainloop_timed(p, timeout_ms);
continue;
}
r = pa_stream_peek(p->stream, &p->read_data, &p->read_length);
if(r != 0) {
if(retry)
- usleep(timeout_ms * 1000);
+ pa_sound_device_mainloop_timed(p, timeout_ms);
return -1;
}
if(p->read_length < length || !p->read_data) {
pa_stream_drop(p->stream);
if(retry)
- usleep(timeout_ms * 1000);
+ pa_sound_device_mainloop_timed(p, timeout_ms);
return -1;
}