Skip to content

Commit

Permalink
(PipeWire) reset playback buffer on stopping
Browse files Browse the repository at this point in the history
* Fix compile warnings
  • Loading branch information
viachaslavic committed Jan 21, 2025
1 parent ef4512d commit 7e8db19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
6 changes: 2 additions & 4 deletions audio/common/pipewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ void pipewire_core_deinit(pipewire_core_t *pw)
return pw_deinit();

if (pw->thread_loop)
{
pw_thread_loop_unlock(pw->thread_loop);
pw_thread_loop_stop(pw->thread_loop);
}

if (pw->registry)
pw_proxy_destroy((struct pw_proxy*)pw->registry);
Expand All @@ -218,7 +215,8 @@ void pipewire_core_deinit(pipewire_core_t *pw)
if (pw->ctx)
pw_context_destroy(pw->ctx);

pw_thread_loop_destroy(pw->thread_loop);
if (pw->thread_loop)
pw_thread_loop_destroy(pw->thread_loop);

if (pw->devicelist)
string_list_free(pw->devicelist);
Expand Down
24 changes: 17 additions & 7 deletions audio/drivers/pipewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ static void playback_process_cb(void *data)
if ((b = pw_stream_dequeue_buffer(audio->stream)) == NULL)
{
RARCH_WARN("[Audio] [PipeWire]: Out of buffers: %s\n", strerror(errno));
return;
return pw_thread_loop_signal(audio->pw->thread_loop, false);
}

buf = b->buffer;
if ((p = buf->datas[0].data) == NULL)
goto done;
return pw_thread_loop_signal(audio->pw->thread_loop, false);

/* calculate the total no of bytes to read data from buffer */
n_bytes = buf->datas[0].maxsize;
Expand Down Expand Up @@ -100,7 +100,6 @@ static void playback_process_cb(void *data)
buf->datas[0].chunk->stride = audio->frame_size;
buf->datas[0].chunk->size = n_bytes;

done:
pw_stream_queue_buffer(audio->stream, b);
pw_thread_loop_signal(audio->pw->thread_loop, false);
}
Expand Down Expand Up @@ -263,7 +262,7 @@ static ssize_t pipewire_write(void *data, const void *buf_, size_t len)

pw_thread_loop_lock(audio->pw->thread_loop);

while (len)
for (;;)
{
filled = spa_ringbuffer_get_write_index(&audio->ring, &idx);
avail = audio->highwater_mark - filled;
Expand All @@ -285,7 +284,10 @@ static ssize_t pipewire_write(void *data, const void *buf_, size_t len)

pw_thread_loop_wait(audio->pw->thread_loop);
if (pw_stream_get_state(audio->stream, &error) != PW_STREAM_STATE_STREAMING)
{
pw_thread_loop_unlock(audio->pw->thread_loop);
return -1;
}
}
else
break;
Expand Down Expand Up @@ -316,15 +318,21 @@ static bool pipewire_stop(void *data)
{
pipewire_audio_t *audio = (pipewire_audio_t*)data;
const char *error = NULL;
bool res = false;

if (!audio || !audio->pw)
return false;

if (pw_stream_get_state(audio->stream, &error) == PW_STREAM_STATE_STREAMING)
return pipewire_stream_set_active(audio->pw->thread_loop, audio->stream, false);
res = pipewire_stream_set_active(audio->pw->thread_loop, audio->stream, false);
else
/* For other states we assume that the stream is inactive */
res = true;

spa_ringbuffer_read_update(&audio->ring, 0);
spa_ringbuffer_write_update(&audio->ring, 0);

/* For other states we assume that the stream is inactive */
return true;
return res;
}

static bool pipewire_start(void *data, bool is_shutdown)
Expand Down Expand Up @@ -380,8 +388,10 @@ static void pipewire_free(void *data)

if (audio->stream)
{
pw_thread_loop_lock(audio->pw->thread_loop);
pw_stream_destroy(audio->stream);
audio->stream = NULL;
pw_thread_loop_unlock(audio->pw->thread_loop);
}
pipewire_core_deinit(audio->pw);
free(audio);
Expand Down
10 changes: 6 additions & 4 deletions audio/drivers_microphone/pipewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ static void capture_process_cb(void *data)
if (!(b = pw_stream_dequeue_buffer(mic->stream)))
{
RARCH_ERR("[Microphone] [PipeWire]: Out of buffers: %s\n", strerror(errno));
return;
return pw_thread_loop_signal(mic->pw->thread_loop, false);
}

buf = b->buffer;
if ((p = buf->datas[0].data) == NULL)
goto done;
return pw_thread_loop_signal(mic->pw->thread_loop, false);

offs = MIN(buf->datas[0].chunk->offset, buf->datas[0].maxsize);
n_bytes = MIN(buf->datas[0].chunk->size, buf->datas[0].maxsize - offs);
Expand All @@ -102,7 +102,6 @@ static void capture_process_cb(void *data)
idx += n_bytes;
spa_ringbuffer_write_update(&mic->ring, idx);

done:
pw_stream_queue_buffer(mic->stream, b);
pw_thread_loop_signal(mic->pw->thread_loop, false);
}
Expand Down Expand Up @@ -209,7 +208,7 @@ static int pipewire_microphone_read(void *driver_context, void *mic_context, voi

pw_thread_loop_lock(pw->thread_loop);

while (len)
for (;;)
{
/* get no of available bytes to read data from buffer */
readable = spa_ringbuffer_get_read_index(&mic->ring, &idx);
Expand All @@ -224,7 +223,10 @@ static int pipewire_microphone_read(void *driver_context, void *mic_context, voi

pw_thread_loop_wait(pw->thread_loop);
if (pw_stream_get_state(mic->stream, &error) != PW_STREAM_STATE_STREAMING)
{
pw_thread_loop_unlock(mic->pw->thread_loop);
return -1;
}
}
else
break;
Expand Down

0 comments on commit 7e8db19

Please sign in to comment.