diff --git a/audio/out/ao.c b/audio/out/ao.c index 4eb115194b..4dddd7dc2b 100644 --- a/audio/out/ao.c +++ b/audio/out/ao.c @@ -453,8 +453,9 @@ struct ao_hotplug { void *wakeup_ctx; // A single AO instance is used to listen to hotplug events. It wouldn't // make much sense to allow multiple AO drivers; all sane platforms have - // a single such audio API. - // This is _not_ the same AO instance as used for playing audio. + // a single audio API providing all events. + // This is _not_ necessarily the same AO instance as used for playing + // audio. struct ao *ao; // cached struct ao_device_list *list; @@ -494,7 +495,8 @@ bool ao_hotplug_check_update(struct ao_hotplug *hp) } // The return value is valid until the next call to this API. -struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp) +struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp, + struct ao *playback_ao) { if (hp->list && !hp->needs_update) return hp->list; @@ -506,6 +508,19 @@ struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp) MP_TARRAY_APPEND(list, list->devices, list->num_devices, (struct ao_device_desc){"auto", "Autoselect device"}); + // Try to use the same AO for hotplug handling as for playback. + // Different AOs may not agree and the playback one is the only one the + // user knows about and may even have configured explicitly. + if (!hp->ao && playback_ao && playback_ao->driver->hotplug_init) { + struct ao *ao = ao_alloc(true, hp->global, hp->wakeup_cb, hp->wakeup_ctx, + (char *)playback_ao->driver->name); + if (playback_ao->driver->hotplug_init(ao) >= 0) { + hp->ao = ao; + } else { + talloc_free(ao); + } + } + for (int n = 0; audio_out_drivers[n]; n++) { const struct ao_driver *d = audio_out_drivers[n]; if (d == &audio_out_null) @@ -569,10 +584,11 @@ static void dummy_wakeup(void *ctx) { } -void ao_print_devices(struct mpv_global *global, struct mp_log *log) +void ao_print_devices(struct mpv_global *global, struct mp_log *log, + struct ao *playback_ao) { struct ao_hotplug *hp = ao_hotplug_create(global, dummy_wakeup, NULL); - struct ao_device_list *list = ao_hotplug_get_device_list(hp); + struct ao_device_list *list = ao_hotplug_get_device_list(hp, playback_ao); mp_info(log, "List of detected audio devices:\n"); for (int n = 0; n < list->num_devices; n++) { struct ao_device_desc *desc = &list->devices[n]; diff --git a/audio/out/ao.h b/audio/out/ao.h index 7d111c46ff..1bca124599 100644 --- a/audio/out/ao.h +++ b/audio/out/ao.h @@ -126,8 +126,8 @@ struct ao_hotplug *ao_hotplug_create(struct mpv_global *global, void *wakeup_ctx); void ao_hotplug_destroy(struct ao_hotplug *hp); bool ao_hotplug_check_update(struct ao_hotplug *hp); -struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp); +struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp, struct ao *playback_ao); -void ao_print_devices(struct mpv_global *global, struct mp_log *log); +void ao_print_devices(struct mpv_global *global, struct mp_log *log, struct ao *playback_ao); #endif /* MPLAYER_AUDIO_OUT_H */ diff --git a/player/command.c b/player/command.c index 943f7e887d..2e6b9875d7 100644 --- a/player/command.c +++ b/player/command.c @@ -1702,7 +1702,7 @@ static int mp_property_audio_device(void *ctx, struct m_property *prop, if (mp_property_generic_option(mpctx, prop, M_PROPERTY_GET, &name) < 1) name = NULL; - struct ao_device_list *list = ao_hotplug_get_device_list(cmd->hotplug); + struct ao_device_list *list = ao_hotplug_get_device_list(cmd->hotplug, mpctx->ao); for (int n = 0; n < list->num_devices; n++) { struct ao_device_desc *dev = &list->devices[n]; if (dev->name && name && strcmp(dev->name, name) == 0) { @@ -1724,7 +1724,7 @@ static int mp_property_audio_devices(void *ctx, struct m_property *prop, struct command_ctx *cmd = mpctx->command_ctx; create_hotplug(mpctx); - struct ao_device_list *list = ao_hotplug_get_device_list(cmd->hotplug); + struct ao_device_list *list = ao_hotplug_get_device_list(cmd->hotplug, mpctx->ao); return m_property_read_list(action, arg, list->num_devices, get_device_entry, list); } diff --git a/player/main.c b/player/main.c index 7c1f5d8831..1c1cdf9f29 100644 --- a/player/main.c +++ b/player/main.c @@ -204,7 +204,7 @@ static bool handle_help_options(struct MPContext *mpctx) if (opts->ao_opts->audio_device && strcmp(opts->ao_opts->audio_device, "help") == 0) { - ao_print_devices(mpctx->global, log); + ao_print_devices(mpctx->global, log, mpctx->ao); return true; } if (opts->property_print_help) {