History

audio: add device change notification for hotplugging

Not very important for the command line player; but GUI applications will want to know about this. This only adds the internal API; support for specific audio outputs comes later. This reuses the ao struct as context for the hotplug event listener, similar to how the "old" device listing API did. This is probably a bit unclean and confusing. One argument got reusing it is that otherwise rewriting parts of ao_pulse would be required (because the PulseAudio API requires so damn much boilerplate). Another is that --ao-defaults is applied to the hotplug dummy ao struct, which automatically applies such defaults even to the hotplug context. Notification works through the property observation mechanism in the client API. The notification chain is a bit complicated: the AO notifies the player, which in turn notifies the clients, which in turn will actually retrieve the device list. (It still has the advantage that it's slightly cleaner, since the AO stuff doesn't need to know about client API issues.) The weird handling of atomic flags in ao.c is because we still don't require real atomics from the compiler. Otherwise we'd just use atomic bitwise operations.

wm4 committed 11 years ago

Commit f061bef1 parent c152c598 files · +151 −33Browse files

Unified · Split

8 files
  1. DOCS/man/input.rst +7 −0
  2. TOOLS/lua/README.md +2 −0
  3. TOOLS/lua/audio-hotplug-test.lua +8 −0
  4. audio/out/ao.c +98 −14
  5. audio/out/ao.h +9 −1
  6. audio/out/internal.h +13 −10
  7. player/command.c +13 −8
  8. player/command.h +1 −0
DOCS/man/input.rst+7−0
@@ -1640,6 +1640,13 @@ Property list · 1639 unmodified lines
16401640 human readable free form text. The description is an empty string if none
16411641 was received.
16421642
1643+ The special entry with the name set to ``auto`` selects the default audio
1644+ output driver and the default device.
1645+
1646+ The property can be watched with the property observation mechanism in
1647+ the client API and in Lua scripts. (Technically, change notification is
1648+ enabled the first time this property is read.)
1649+
16431650 ``audio-device`` (RW)
16441651 Set the audio device. This directly reads/writes the ``--audio-device``
16451652 option, but on write accesses, the audio output will be scheduled for
TOOLS/lua/README.md+2−0
@@ -10,3 +10,5 @@ to mpv's command line. · 9 unmodified lines
1010
1111 Where appropriate, they may also be placed in ~/.config/mpv/scripts/ from
1212 where they will be automatically loaded when mpv starts.
13+
14+Some of these are just for testing mpv internals.
TOOLS/lua/audio-hotplug-test.lua+8−0
@@ -0,0 +1,8 @@
1+local utils = require("mp.utils")
2+
3+mp.observe_property("audio-device-list", "native", function(name, val)
4+ print("Audio device list changed:")
5+ for index, e in ipairs(val) do
6+ print(" - '" .. e.name .. "' (" .. e.description .. ")")
7+ end
8+end)
audio/out/ao.c+98−14
@@ -386,14 +386,25 @@ int ao_query_and_reset_events(struct ao *ao, int events) · 385 unmodified lines
386386 int actual_events = 0;
387387 if (atomic_load(&ao->request_reload)) // don't need to reset it
388388 actual_events |= AO_EVENT_RELOAD;
389+ if (atomic_load(&ao->request_hotplug))
390+ actual_events |= AO_EVENT_HOTPLUG;
389391 return actual_events & events;
390392 }
391393
392−// Request that the player core destroys and recreates the AO.
394+// Request that the player core destroys and recreates the AO. Fully thread-safe.
393395 void ao_request_reload(struct ao *ao)
394396 {
395397 atomic_store(&ao->request_reload, true);
396− mp_input_wakeup(ao->input_ctx);
398+ if (ao->input_ctx)
399+ mp_input_wakeup(ao->input_ctx);
400+}
401+
402+// Notify the player that the device list changed. Fully thread-safe.
403+void ao_hotplug_event(struct ao *ao)
404+{
405+ atomic_store(&ao->request_hotplug, true);
406+ if (ao->input_ctx)
407+ mp_input_wakeup(ao->input_ctx);
397408 }
398409
399410 bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s,
@@ -444,26 +455,88 @@ const char *ao_get_detected_device(struct ao *ao) · 44 unmodified lines
444455 return ao->detected_device;
445456 }
446457
447−struct ao_device_list *ao_get_device_list(struct mpv_global *global)
458+// ---
459+
460+struct ao_hotplug {
461+ struct mpv_global *global;
462+ struct input_ctx *input_ctx;
463+ // A single AO instance is used to listen to hotplug events. It wouldn't
464+ // make much sense to allow multiple AO drivers; all sane platforms have
465+ // a single such audio API.
466+ // This is _not_ the same AO instance as used for playing audio.
467+ struct ao *ao;
468+ // cached
469+ struct ao_device_list *list;
470+ bool needs_update;
471+};
472+
473+struct ao_hotplug *ao_hotplug_create(struct mpv_global *global,
474+ struct input_ctx *input_ctx)
475+{
476+ struct ao_hotplug *hp = talloc_ptrtype(NULL, hp);
477+ *hp = (struct ao_hotplug){
478+ .global = global,
479+ .input_ctx = input_ctx,
480+ .needs_update = true,
481+ };
482+ return hp;
483+}
484+
485+static void get_devices(struct ao *ao, struct ao_device_list *list)
486+{
487+ int num = list->num_devices;
488+ if (ao->driver->list_devs)
489+ ao->driver->list_devs(ao, list);
490+ // Add at least a default entry
491+ if (list->num_devices == num)
492+ ao_device_list_add(list, ao, &(struct ao_device_desc){"", "Default"});
493+}
494+
495+bool ao_hotplug_check_update(struct ao_hotplug *hp)
496+{
497+ if (hp->ao && ao_query_and_reset_events(hp->ao, AO_EVENT_HOTPLUG)) {
498+ hp->needs_update = true;
499+ atomic_store(&hp->ao->request_hotplug, false);
500+ return true;
501+ }
502+ return false;
503+}
504+
505+// The return value is valid until the next call to this API.
506+struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp)
448507 {
449− struct ao_device_list *list = talloc_zero(NULL, struct ao_device_list);
508+ if (hp->list && !hp->needs_update)
509+ return hp->list;
510+
511+ talloc_free(hp->list);
512+ struct ao_device_list *list = talloc_zero(hp, struct ao_device_list);
513+ hp->list = list;
514+
450515 MP_TARRAY_APPEND(list, list->devices, list->num_devices,
451516 (struct ao_device_desc){"auto", "Autoselect device"});
517+
452518 for (int n = 0; audio_out_drivers[n]; n++) {
453519 const struct ao_driver *d = audio_out_drivers[n];
454520 if (d == &audio_out_null)
455521 break; // don't add unsafe/special entries
456− struct ao *ao = ao_alloc(true, global, NULL, (char *)d->name, NULL);
522+
523+ struct ao *ao = ao_alloc(true, hp->global, hp->input_ctx,
524+ (char *)d->name, NULL);
457525 if (!ao)
458526 continue;
459− int num = list->num_devices;
460− if (d->list_devs)
461− d->list_devs(ao, list);
462− // Add at least a default entry
463− if (list->num_devices == num)
464− ao_device_list_add(list, ao, &(struct ao_device_desc){"", "Default"});
465− talloc_free(ao);
527+
528+ if (ao->driver->hotplug_init) {
529+ if (!hp->ao && ao->driver->hotplug_init(ao) >= 0)
530+ hp->ao = ao; // keep this one
531+ if (hp->ao && hp->ao->driver == d)
532+ get_devices(hp->ao, list);
533+ } else {
534+ get_devices(ao, list);
535+ }
536+ if (ao != hp->ao)
537+ talloc_free(ao);
466538 }
539+ hp->needs_update = false;
467540 return list;
468541 }
469542
@@ -478,13 +551,24 @@ void ao_device_list_add(struct ao_device_list *list, struct ao *ao, · 8 unmodified lines
478551 MP_TARRAY_APPEND(list, list->devices, list->num_devices, c);
479552 }
480553
554+void ao_hotplug_destroy(struct ao_hotplug *hp)
555+{
556+ if (!hp)
557+ return;
558+ if (hp->ao && hp->ao->driver->hotplug_uninit)
559+ hp->ao->driver->hotplug_uninit(hp->ao);
560+ talloc_free(hp->ao);
561+ talloc_free(hp);
562+}
563+
481564 void ao_print_devices(struct mpv_global *global, struct mp_log *log)
482565 {
483− struct ao_device_list *list = ao_get_device_list(global);
566+ struct ao_hotplug *hp = ao_hotplug_create(global, NULL);
567+ struct ao_device_list *list = ao_hotplug_get_device_list(hp);
484568 mp_info(log, "List of detected audio devices:\n");
485569 for (int n = 0; n < list->num_devices; n++) {
486570 struct ao_device_desc *desc = &list->devices[n];
487571 mp_info(log, " '%s' (%s)\n", desc->name, desc->desc);
488572 }
489− talloc_free(list);
573+ ao_hotplug_destroy(hp);
490574 }
audio/out/ao.h+9−1
@@ -48,6 +48,7 @@ enum aocontrol { · 47 unmodified lines
4848
4949 enum {
5050 AO_EVENT_RELOAD = 1,
51+ AO_EVENT_HOTPLUG = 2,
5152 };
5253
5354 typedef struct ao_control_vol {
@@ -92,8 +93,15 @@ void ao_drain(struct ao *ao); · 38 unmodified lines
9293 bool ao_eof_reached(struct ao *ao);
9394 int ao_query_and_reset_events(struct ao *ao, int events);
9495 void ao_request_reload(struct ao *ao);
96+void ao_hotplug_event(struct ao *ao);
97+
98+struct ao_hotplug;
99+struct ao_hotplug *ao_hotplug_create(struct mpv_global *global,
100+ struct input_ctx *input_ctx);
101+void ao_hotplug_destroy(struct ao_hotplug *hp);
102+bool ao_hotplug_check_update(struct ao_hotplug *hp);
103+struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp);
95104
96−struct ao_device_list *ao_get_device_list(struct mpv_global *global);
97105 void ao_print_devices(struct mpv_global *global, struct mp_log *log);
98106
99107 #endif /* MPLAYER_AUDIO_OUT_H */
audio/out/internal.h+13−10
@@ -59,7 +59,8 @@ struct ao { · 58 unmodified lines
5959 // Used during init: if init fails, redirect to this ao
6060 char *redirect;
6161
62− atomic_bool request_reload;
62+ // Internal events (use ao_request_reload(), ao_hotplug_event())
63+ atomic_bool request_reload, request_hotplug;
6364
6465 int buffer;
6566 double def_buffer;
@@ -161,17 +162,19 @@ struct ao_driver { · 95 unmodified lines
161162 // Return the list of devices currently available in the system. Use
162163 // ao_device_list_add() to add entries. The selected device will be set as
163164 // ao->device (using ao_device_desc.name).
164− // Warning: the ao struct passed doesn't necessarily have ao_driver->init()
165− // called on it - in this case, ->uninit() won't be called either
166− // after this function. The idea is that list_devs can be called
167− // both when no audio or when audio is active. the latter can
168− // happen if the audio config change at runtime, and in this case
169− // we don't want to force a new connection to the audio server
170− // just to update the device list. For runtime updates, ->init()
171− // will have been called. In both cases, ao->priv is properly
172− // allocated. (Runtime updates are not used/supported yet.)
165+ // Warning: the ao struct passed is not initialized with ao_driver->init().
166+ // Instead, hotplug_init/hotplug_uninit is called. If these
167+ // callbacks are not set, no driver initialization call is done
168+ // on the ao struct.
173169 void (*list_devs)(struct ao *ao, struct ao_device_list *list);
174170
171+ // If set, these are called before/after ao_driver->list_devs is called.
172+ // It is also assumed that the driver can do hotplugging - which means
173+ // it is expected to call ao_hotplug_event(ao) whenever the system's
174+ // audio device list changes. The player will then call list_devs() again.
175+ int (*hotplug_init)(struct ao *ao);
176+ void (*hotplug_uninit)(struct ao *ao);
177+
175178 // For option parsing (see vo.h)
176179 int priv_size;
177180 const void *priv_defaults;
player/command.c+13−8
@@ -90,7 +90,7 @@ struct command_ctx { · 89 unmodified lines
9090 int num_hooks;
9191 int64_t hook_seq; // for hook_handler.seq
9292
93− struct ao_device_list *cached_ao_devices;
93+ struct ao_hotplug *hotplug;
9494 };
9595
9696 struct overlay {
@@ -1560,14 +1560,12 @@ static int mp_property_audio_devices(void *ctx, struct m_property *prop, · 1463 unmodified lines
15601560 {
15611561 struct MPContext *mpctx = ctx;
15621562 struct command_ctx *cmd = mpctx->command_ctx;
1563− if (!cmd->cached_ao_devices)
1564− cmd->cached_ao_devices = ao_get_device_list(mpctx->global);
1565− if (!cmd->cached_ao_devices)
1566− return M_PROPERTY_ERROR;
1567− talloc_steal(cmd, cmd->cached_ao_devices);
1563+ if (!cmd->hotplug)
1564+ cmd->hotplug = ao_hotplug_create(mpctx->global, mpctx->input);
15681565
1569− return m_property_read_list(action, arg, cmd->cached_ao_devices->num_devices,
1570− get_device_entry, cmd->cached_ao_devices);
1566+ struct ao_device_list *list = ao_hotplug_get_device_list(cmd->hotplug);
1567+ return m_property_read_list(action, arg, list->num_devices,
1568+ get_device_entry, list);
15711569 }
15721570
15731571 static int mp_property_ao(void *ctx, struct m_property *p, int action, void *arg)
@@ -3535,6 +3533,7 @@ static const char *const *const mp_event_property_change[] = { · 1961 unmodified lines
35353533 "demuxer-cache-duration", "demuxer-cache-idle", "paused-for-cache"),
35363534 E(MP_EVENT_WIN_RESIZE, "window-scale"),
35373535 E(MP_EVENT_WIN_STATE, "window-minimized", "display-names"),
3536+ E(MP_EVENT_AUDIO_DEVICES, "audio-device-list"),
35383537 };
35393538 #undef E
35403539
@@ -4819,6 +4818,7 @@ int run_command(MPContext *mpctx, mp_cmd_t *cmd) · 1278 unmodified lines
48194818 void command_uninit(struct MPContext *mpctx)
48204819 {
48214820 overlay_uninit(mpctx);
4821+ ao_hotplug_destroy(mpctx->command_ctx->hotplug);
48224822 talloc_free(mpctx->command_ctx);
48234823 mpctx->command_ctx = NULL;
48244824 }
@@ -4868,6 +4868,11 @@ static void command_event(struct MPContext *mpctx, int event, void *arg) · 43 unmodified lines
48684868 // Update chapters - does nothing if something else is visible.
48694869 set_osd_bar_chapters(mpctx, OSD_BAR_SEEK);
48704870 }
4871+
4872+ // This is a bit messy: ao_hotplug wakes up the player, and then we have
4873+ // to recheck the state. Then the client(s) will read the property.
4874+ if (ctx->hotplug && ao_hotplug_check_update(ctx->hotplug))
4875+ mp_notify_property(mpctx, "audio-device-list");
48714876 }
48724877
48734878 void mp_notify(struct MPContext *mpctx, int event, void *arg)
player/command.h+1−0
@@ -47,6 +47,7 @@ enum { · 46 unmodified lines
4747 MP_EVENT_CACHE_UPDATE,
4848 MP_EVENT_WIN_RESIZE,
4949 MP_EVENT_WIN_STATE,
50+ MP_EVENT_AUDIO_DEVICES,
5051 };
5152
5253 bool mp_hook_test_completion(struct MPContext *mpctx, char *type);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t