ao_pipewire: wait for draining finishes before restart ao

When the stream is draining, setting stream to active has no effect.
This commit is contained in:
Misaki Kasumi 2024-05-06 20:25:53 +08:00 committed by sfan5
parent 3fc8929caf
commit 88f20a7011
1 changed files with 25 additions and 12 deletions

View File

@ -56,10 +56,11 @@ static uint64_t pw_stream_get_nsec(struct pw_stream *stream)
} }
#endif #endif
enum init_state { enum state {
INIT_STATE_NONE, STATE_UNINITIALIZED,
INIT_STATE_SUCCESS, STATE_READY,
INIT_STATE_ERROR, STATE_ERROR,
STATE_DRAINING,
}; };
enum { enum {
@ -73,7 +74,7 @@ struct priv {
struct pw_core *core; struct pw_core *core;
struct spa_hook stream_listener; struct spa_hook stream_listener;
struct spa_hook core_listener; struct spa_hook core_listener;
enum init_state init_state; enum state state;
bool muted; bool muted;
float volume; float volume;
@ -221,6 +222,7 @@ static void on_process(void *userdata)
if (eof) { if (eof) {
pw_stream_flush(p->stream, true); pw_stream_flush(p->stream, true);
ao_stop_streaming(ao); ao_stop_streaming(ao);
p->state = STATE_DRAINING;
} }
MP_TRACE(ao, "queued %d of %d samples\n", samples, nframes); MP_TRACE(ao, "queued %d of %d samples\n", samples, nframes);
@ -238,7 +240,7 @@ static void on_param_changed(void *userdata, uint32_t id, const struct spa_pod *
* As there is no proper callback for this we use the Latency param for this * As there is no proper callback for this we use the Latency param for this
*/ */
if (id == SPA_PARAM_Latency) { if (id == SPA_PARAM_Latency) {
p->init_state = INIT_STATE_SUCCESS; p->state = STATE_READY;
pw_thread_loop_signal(p->loop, false); pw_thread_loop_signal(p->loop, false);
} }
@ -273,7 +275,7 @@ static void on_state_changed(void *userdata, enum pw_stream_state old, enum pw_s
if (state == PW_STREAM_STATE_ERROR) { if (state == PW_STREAM_STATE_ERROR) {
MP_WARN(ao, "Stream in error state, trying to reload...\n"); MP_WARN(ao, "Stream in error state, trying to reload...\n");
p->init_state = INIT_STATE_ERROR; p->state = STATE_ERROR;
pw_thread_loop_signal(p->loop, false); pw_thread_loop_signal(p->loop, false);
ao_request_reload(ao); ao_request_reload(ao);
} }
@ -328,12 +330,22 @@ static void on_control_info(void *userdata, uint32_t id,
} }
} }
static void on_drained(void *userdata)
{
struct ao *ao = userdata;
struct priv *p = ao->priv;
p->state = STATE_READY;
pw_thread_loop_signal(p->loop, false);
}
static const struct pw_stream_events stream_events = { static const struct pw_stream_events stream_events = {
.version = PW_VERSION_STREAM_EVENTS, .version = PW_VERSION_STREAM_EVENTS,
.param_changed = on_param_changed, .param_changed = on_param_changed,
.process = on_process, .process = on_process,
.state_changed = on_state_changed, .state_changed = on_state_changed,
.control_info = on_control_info, .control_info = on_control_info,
.drained = on_drained,
}; };
static void uninit(struct ao *ao) static void uninit(struct ao *ao)
@ -559,7 +571,7 @@ error:
return -1; return -1;
} }
static void wait_for_init_done(struct ao *ao) static void wait_for_state_ready(struct ao *ao)
{ {
struct priv *p = ao->priv; struct priv *p = ao->priv;
struct timespec abstime; struct timespec abstime;
@ -571,7 +583,7 @@ static void wait_for_init_done(struct ao *ao)
return; return;
} }
while (p->init_state == INIT_STATE_NONE) { while (p->state != STATE_READY && p->state != STATE_ERROR) {
r = pw_thread_loop_timed_wait_full(p->loop, &abstime); r = pw_thread_loop_timed_wait_full(p->loop, &abstime);
if (r < 0) { if (r < 0) {
MP_WARN(ao, "Could not wait for initialization: %s\n", spa_strerror(r)); MP_WARN(ao, "Could not wait for initialization: %s\n", spa_strerror(r));
@ -679,11 +691,11 @@ static int init(struct ao *ao)
goto error; goto error;
} }
wait_for_init_done(ao); wait_for_state_ready(ao);
pw_thread_loop_unlock(p->loop); pw_thread_loop_unlock(p->loop);
if (p->init_state == INIT_STATE_ERROR) if (p->state == STATE_ERROR)
goto error; goto error;
return 0; return 0;
@ -708,6 +720,7 @@ static void start(struct ao *ao)
{ {
struct priv *p = ao->priv; struct priv *p = ao->priv;
pw_thread_loop_lock(p->loop); pw_thread_loop_lock(p->loop);
wait_for_state_ready(ao);
pw_stream_set_active(p->stream, true); pw_stream_set_active(p->stream, true);
pw_thread_loop_unlock(p->loop); pw_thread_loop_unlock(p->loop);
} }
@ -924,7 +937,7 @@ const struct ao_driver audio_out_pipewire = {
{ {
.loop = NULL, .loop = NULL,
.stream = NULL, .stream = NULL,
.init_state = INIT_STATE_NONE, .state = STATE_UNINITIALIZED,
.options.buffer_msec = 0, .options.buffer_msec = 0,
.options.volume_mode = VOLUME_MODE_CHANNEL, .options.volume_mode = VOLUME_MODE_CHANNEL,
}, },