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
1640 human readable free form text. The description is an empty string if none1640 human readable free form text. The description is an empty string if none
1641 was received.1641 was received.
1642 1642
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
1643``audio-device`` (RW)1650``audio-device`` (RW)
1644 Set the audio device. This directly reads/writes the ``--audio-device``1651 Set the audio device. This directly reads/writes the ``--audio-device``
1645 option, but on write accesses, the audio output will be scheduled for1652 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
10 10
11Where appropriate, they may also be placed in ~/.config/mpv/scripts/ from11Where appropriate, they may also be placed in ~/.config/mpv/scripts/ from
12where they will be automatically loaded when mpv starts.12where they will be automatically loaded when mpv starts.
13
14Some of these are just for testing mpv internals.
TOOLS/lua/audio-hotplug-test.lua+8−0
@@ -0,0 +1,8 @@
1local utils = require("mp.utils")
2
3mp.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
8end)
audio/out/ao.c+98−14
@@ -386,14 +386,25 @@ int ao_query_and_reset_events(struct ao *ao, int events) · 385 unmodified lines
386 int actual_events = 0;386 int actual_events = 0;
387 if (atomic_load(&ao->request_reload)) // don't need to reset it387 if (atomic_load(&ao->request_reload)) // don't need to reset it
388 actual_events |= AO_EVENT_RELOAD;388 actual_events |= AO_EVENT_RELOAD;
389 if (atomic_load(&ao->request_hotplug))
390 actual_events |= AO_EVENT_HOTPLUG;
389 return actual_events & events;391 return actual_events & events;
390}392}
391 393
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.
393void ao_request_reload(struct ao *ao)395void ao_request_reload(struct ao *ao)
394{396{
395 atomic_store(&ao->request_reload, true);397 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.
403void 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);
397}408}
398 409
399bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s,410bool 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
444 return ao->detected_device;455 return ao->detected_device;
445}456}
446 457
447struct ao_device_list *ao_get_device_list(struct mpv_global *global)458// ---
459
460struct 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
473struct 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
485static 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
495bool 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.
506struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp)
448{507{
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
450 MP_TARRAY_APPEND(list, list->devices, list->num_devices,515 MP_TARRAY_APPEND(list, list->devices, list->num_devices,
451 (struct ao_device_desc){"auto", "Autoselect device"});516 (struct ao_device_desc){"auto", "Autoselect device"});
517
452 for (int n = 0; audio_out_drivers[n]; n++) {518 for (int n = 0; audio_out_drivers[n]; n++) {
453 const struct ao_driver *d = audio_out_drivers[n];519 const struct ao_driver *d = audio_out_drivers[n];
454 if (d == &audio_out_null)520 if (d == &audio_out_null)
455 break; // don't add unsafe/special entries521 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);
457 if (!ao)525 if (!ao)
458 continue;526 continue;
459 int num = list->num_devices;527
460 if (d->list_devs)528 if (ao->driver->hotplug_init) {
461 d->list_devs(ao, list);529 if (!hp->ao && ao->driver->hotplug_init(ao) >= 0)
462 // Add at least a default entry530 hp->ao = ao; // keep this one
463 if (list->num_devices == num)531 if (hp->ao && hp->ao->driver == d)
464 ao_device_list_add(list, ao, &(struct ao_device_desc){"", "Default"});532 get_devices(hp->ao, list);
465 talloc_free(ao);533 } else {
534 get_devices(ao, list);
535 }
536 if (ao != hp->ao)
537 talloc_free(ao);
466 }538 }
539 hp->needs_update = false;
467 return list;540 return list;
468}541}
469 542
@@ -478,13 +551,24 @@ void ao_device_list_add(struct ao_device_list *list, struct ao *ao, · 8 unmodified lines
478 MP_TARRAY_APPEND(list, list->devices, list->num_devices, c);551 MP_TARRAY_APPEND(list, list->devices, list->num_devices, c);
479}552}
480 553
554void 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
481void ao_print_devices(struct mpv_global *global, struct mp_log *log)564void ao_print_devices(struct mpv_global *global, struct mp_log *log)
482{565{
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);
484 mp_info(log, "List of detected audio devices:\n");568 mp_info(log, "List of detected audio devices:\n");
485 for (int n = 0; n < list->num_devices; n++) {569 for (int n = 0; n < list->num_devices; n++) {
486 struct ao_device_desc *desc = &list->devices[n];570 struct ao_device_desc *desc = &list->devices[n];
487 mp_info(log, " '%s' (%s)\n", desc->name, desc->desc);571 mp_info(log, " '%s' (%s)\n", desc->name, desc->desc);
488 }572 }
489 talloc_free(list);573 ao_hotplug_destroy(hp);
490}574}
audio/out/ao.h+9−1
@@ -48,6 +48,7 @@ enum aocontrol { · 47 unmodified lines
48 48
49enum {49enum {
50 AO_EVENT_RELOAD = 1,50 AO_EVENT_RELOAD = 1,
51 AO_EVENT_HOTPLUG = 2,
51};52};
52 53
53typedef struct ao_control_vol {54typedef struct ao_control_vol {
@@ -92,8 +93,15 @@ void ao_drain(struct ao *ao); · 38 unmodified lines
92bool ao_eof_reached(struct ao *ao);93bool ao_eof_reached(struct ao *ao);
93int ao_query_and_reset_events(struct ao *ao, int events);94int ao_query_and_reset_events(struct ao *ao, int events);
94void ao_request_reload(struct ao *ao);95void ao_request_reload(struct ao *ao);
96void ao_hotplug_event(struct ao *ao);
97
98struct ao_hotplug;
99struct ao_hotplug *ao_hotplug_create(struct mpv_global *global,
100 struct input_ctx *input_ctx);
101void ao_hotplug_destroy(struct ao_hotplug *hp);
102bool ao_hotplug_check_update(struct ao_hotplug *hp);
103struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp);
95 104
96struct ao_device_list *ao_get_device_list(struct mpv_global *global);
97void ao_print_devices(struct mpv_global *global, struct mp_log *log);105void ao_print_devices(struct mpv_global *global, struct mp_log *log);
98 106
99#endif /* MPLAYER_AUDIO_OUT_H */107#endif /* MPLAYER_AUDIO_OUT_H */
audio/out/internal.h+13−10
@@ -59,7 +59,8 @@ struct ao { · 58 unmodified lines
59 // Used during init: if init fails, redirect to this ao59 // Used during init: if init fails, redirect to this ao
60 char *redirect;60 char *redirect;
61 61
62 atomic_bool request_reload;62 // Internal events (use ao_request_reload(), ao_hotplug_event())
63 atomic_bool request_reload, request_hotplug;
63 64
64 int buffer;65 int buffer;
65 double def_buffer;66 double def_buffer;
@@ -161,17 +162,19 @@ struct ao_driver { · 95 unmodified lines
161 // Return the list of devices currently available in the system. Use162 // Return the list of devices currently available in the system. Use
162 // ao_device_list_add() to add entries. The selected device will be set as163 // ao_device_list_add() to add entries. The selected device will be set as
163 // ao->device (using ao_device_desc.name).164 // ao->device (using ao_device_desc.name).
164 // Warning: the ao struct passed doesn't necessarily have ao_driver->init()165 // Warning: the ao struct passed is not initialized with ao_driver->init().
165 // called on it - in this case, ->uninit() won't be called either166 // Instead, hotplug_init/hotplug_uninit is called. If these
166 // after this function. The idea is that list_devs can be called167 // callbacks are not set, no driver initialization call is done
167 // both when no audio or when audio is active. the latter can168 // on the ao struct.
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.)
173 void (*list_devs)(struct ao *ao, struct ao_device_list *list);169 void (*list_devs)(struct ao *ao, struct ao_device_list *list);
174 170
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
175 // For option parsing (see vo.h)178 // For option parsing (see vo.h)
176 int priv_size;179 int priv_size;
177 const void *priv_defaults;180 const void *priv_defaults;
player/command.c+13−8
@@ -90,7 +90,7 @@ struct command_ctx { · 89 unmodified lines
90 int num_hooks;90 int num_hooks;
91 int64_t hook_seq; // for hook_handler.seq91 int64_t hook_seq; // for hook_handler.seq
92 92
93 struct ao_device_list *cached_ao_devices;93 struct ao_hotplug *hotplug;
94};94};
95 95
96struct overlay {96struct overlay {
@@ -1560,14 +1560,12 @@ static int mp_property_audio_devices(void *ctx, struct m_property *prop, · 1463 unmodified lines
1560{1560{
1561 struct MPContext *mpctx = ctx;1561 struct MPContext *mpctx = ctx;
1562 struct command_ctx *cmd = mpctx->command_ctx;1562 struct command_ctx *cmd = mpctx->command_ctx;
1563 if (!cmd->cached_ao_devices)1563 if (!cmd->hotplug)
1564 cmd->cached_ao_devices = ao_get_device_list(mpctx->global);1564 cmd->hotplug = ao_hotplug_create(mpctx->global, mpctx->input);
1565 if (!cmd->cached_ao_devices)
1566 return M_PROPERTY_ERROR;
1567 talloc_steal(cmd, cmd->cached_ao_devices);
1568 1565
1569 return m_property_read_list(action, arg, cmd->cached_ao_devices->num_devices,1566 struct ao_device_list *list = ao_hotplug_get_device_list(cmd->hotplug);
1570 get_device_entry, cmd->cached_ao_devices);1567 return m_property_read_list(action, arg, list->num_devices,
1568 get_device_entry, list);
1571}1569}
1572 1570
1573static int mp_property_ao(void *ctx, struct m_property *p, int action, void *arg)1571static 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
3535 "demuxer-cache-duration", "demuxer-cache-idle", "paused-for-cache"),3533 "demuxer-cache-duration", "demuxer-cache-idle", "paused-for-cache"),
3536 E(MP_EVENT_WIN_RESIZE, "window-scale"),3534 E(MP_EVENT_WIN_RESIZE, "window-scale"),
3537 E(MP_EVENT_WIN_STATE, "window-minimized", "display-names"),3535 E(MP_EVENT_WIN_STATE, "window-minimized", "display-names"),
3536 E(MP_EVENT_AUDIO_DEVICES, "audio-device-list"),
3538};3537};
3539#undef E3538#undef E
3540 3539
@@ -4819,6 +4818,7 @@ int run_command(MPContext *mpctx, mp_cmd_t *cmd) · 1278 unmodified lines
4819void command_uninit(struct MPContext *mpctx)4818void command_uninit(struct MPContext *mpctx)
4820{4819{
4821 overlay_uninit(mpctx);4820 overlay_uninit(mpctx);
4821 ao_hotplug_destroy(mpctx->command_ctx->hotplug);
4822 talloc_free(mpctx->command_ctx);4822 talloc_free(mpctx->command_ctx);
4823 mpctx->command_ctx = NULL;4823 mpctx->command_ctx = NULL;
4824}4824}
@@ -4868,6 +4868,11 @@ static void command_event(struct MPContext *mpctx, int event, void *arg) · 43 unmodified lines
4868 // Update chapters - does nothing if something else is visible.4868 // Update chapters - does nothing if something else is visible.
4869 set_osd_bar_chapters(mpctx, OSD_BAR_SEEK);4869 set_osd_bar_chapters(mpctx, OSD_BAR_SEEK);
4870 }4870 }
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");
4871}4876}
4872 4877
4873void mp_notify(struct MPContext *mpctx, int event, void *arg)4878void mp_notify(struct MPContext *mpctx, int event, void *arg)
player/command.h+1−0
@@ -47,6 +47,7 @@ enum { · 46 unmodified lines
47 MP_EVENT_CACHE_UPDATE,47 MP_EVENT_CACHE_UPDATE,
48 MP_EVENT_WIN_RESIZE,48 MP_EVENT_WIN_RESIZE,
49 MP_EVENT_WIN_STATE,49 MP_EVENT_WIN_STATE,
50 MP_EVENT_AUDIO_DEVICES,
50};51};
51 52
52bool mp_hook_test_completion(struct MPContext *mpctx, char *type);53bool 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