summaryrefslogtreecommitdiffstats
path: root/src/modules
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules')
l---------src/modules/Makefile1
-rw-r--r--src/modules/alsa-util.c433
-rw-r--r--src/modules/alsa-util.h45
-rw-r--r--src/modules/alsa/alsa-mixer.c4194
-rw-r--r--src/modules/alsa/alsa-mixer.h328
-rw-r--r--src/modules/alsa/alsa-sink.c2246
-rw-r--r--src/modules/alsa/alsa-sink.h36
-rw-r--r--src/modules/alsa/alsa-source.c2003
-rw-r--r--src/modules/alsa/alsa-source.h36
-rw-r--r--src/modules/alsa/alsa-util.c1401
-rw-r--r--src/modules/alsa/alsa-util.h143
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-aux.conf66
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-dock-mic.conf81
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-fm.conf66
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-front-mic.conf81
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-internal-mic.conf111
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-linein.conf93
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-mic-line.conf67
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-mic.conf104
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-mic.conf.common54
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-rear-mic.conf81
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-tvtuner.conf66
-rw-r--r--src/modules/alsa/mixer/paths/analog-input-video.conf65
-rw-r--r--src/modules/alsa/mixer/paths/analog-input.conf83
-rw-r--r--src/modules/alsa/mixer/paths/analog-input.conf.common290
-rw-r--r--src/modules/alsa/mixer/paths/analog-output-desktop-speaker.conf99
-rw-r--r--src/modules/alsa/mixer/paths/analog-output-headphones-2.conf87
-rw-r--r--src/modules/alsa/mixer/paths/analog-output-headphones.conf87
-rw-r--r--src/modules/alsa/mixer/paths/analog-output-lfe-on-mono.conf89
-rw-r--r--src/modules/alsa/mixer/paths/analog-output-mono.conf86
-rw-r--r--src/modules/alsa/mixer/paths/analog-output-speaker.conf99
-rw-r--r--src/modules/alsa/mixer/paths/analog-output.conf96
-rw-r--r--src/modules/alsa/mixer/paths/analog-output.conf.common147
-rw-r--r--src/modules/alsa/mixer/paths/iec958-stereo-output.conf19
-rw-r--r--src/modules/alsa/mixer/profile-sets/90-pulseaudio.rules40
-rw-r--r--src/modules/alsa/mixer/profile-sets/default.conf180
-rw-r--r--src/modules/alsa/mixer/profile-sets/maudio-fasttrack-pro.conf85
-rw-r--r--src/modules/alsa/mixer/profile-sets/native-instruments-audio4dj.conf91
-rw-r--r--src/modules/alsa/mixer/profile-sets/native-instruments-audio8dj.conf162
-rw-r--r--src/modules/alsa/mixer/profile-sets/native-instruments-korecontroller.conf85
-rw-r--r--src/modules/alsa/mixer/profile-sets/native-instruments-traktor-audio10.conf131
-rw-r--r--src/modules/alsa/mixer/profile-sets/native-instruments-traktor-audio6.conf92
-rw-r--r--src/modules/alsa/mixer/profile-sets/native-instruments-traktorkontrol-s4.conf81
-rw-r--r--src/modules/alsa/mixer/profile-sets/usb-headset.conf35
-rw-r--r--src/modules/alsa/mixer/samples/ATI IXP--Realtek ALC655 rev 0150
-rw-r--r--src/modules/alsa/mixer/samples/Brooktree Bt878--Bt87x24
-rw-r--r--src/modules/alsa/mixer/samples/Ensoniq AudioPCI--Cirrus Logic CS4297A rev 3135
-rw-r--r--src/modules/alsa/mixer/samples/HDA ATI HDMI--ATI R6xx HDMI4
-rw-r--r--src/modules/alsa/mixer/samples/HDA Intel--Analog Devices AD198162
-rw-r--r--src/modules/alsa/mixer/samples/HDA Intel--Realtek ALC889A113
-rw-r--r--src/modules/alsa/mixer/samples/Intel 82801CA-ICH3--Analog Devices AD1881A128
-rw-r--r--src/modules/alsa/mixer/samples/Logitech USB Speaker--USB Mixer27
-rw-r--r--src/modules/alsa/mixer/samples/USB Audio--USB Mixer37
-rw-r--r--src/modules/alsa/mixer/samples/USB Device 0x46d:0x9a4--USB Mixer5
-rw-r--r--src/modules/alsa/mixer/samples/VIA 8237--Analog Devices AD1888211
-rw-r--r--src/modules/alsa/mixer/samples/VIA 8237--C-Media Electronics CMI9761A+160
-rw-r--r--src/modules/alsa/module-alsa-card.c479
-rw-r--r--src/modules/alsa/module-alsa-sink.c136
-rw-r--r--src/modules/alsa/module-alsa-source.c143
-rw-r--r--src/modules/bluetooth/a2dp-codecs.h116
-rw-r--r--src/modules/bluetooth/bluetooth-util.c1662
-rw-r--r--src/modules/bluetooth/bluetooth-util.h142
-rw-r--r--src/modules/bluetooth/ipc.c133
-rw-r--r--src/modules/bluetooth/ipc.h360
-rw-r--r--src/modules/bluetooth/module-bluetooth-device.c3006
-rw-r--r--src/modules/bluetooth/module-bluetooth-discover.c220
-rw-r--r--src/modules/bluetooth/module-bluetooth-proximity.c487
-rw-r--r--src/modules/bluetooth/proximity-helper.c202
-rw-r--r--src/modules/bluetooth/rtp.h76
-rw-r--r--src/modules/bluetooth/sbc/sbc.c1234
-rw-r--r--src/modules/bluetooth/sbc/sbc.h113
-rw-r--r--src/modules/bluetooth/sbc/sbc_math.h61
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives.c554
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives.h80
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives_armv6.c299
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives_armv6.h52
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives_iwmmxt.c304
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives_iwmmxt.h42
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives_mmx.c375
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives_mmx.h41
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives_neon.c893
-rw-r--r--src/modules/bluetooth/sbc/sbc_primitives_neon.h41
-rw-r--r--src/modules/bluetooth/sbc/sbc_tables.h660
-rw-r--r--src/modules/dbus/iface-card-profile.c228
-rw-r--r--src/modules/dbus/iface-card-profile.h49
-rw-r--r--src/modules/dbus/iface-card.c561
-rw-r--r--src/modules/dbus/iface-card.h45
-rw-r--r--src/modules/dbus/iface-client.c461
-rw-r--r--src/modules/dbus/iface-client.h45
-rw-r--r--src/modules/dbus/iface-core.c2206
-rw-r--r--src/modules/dbus/iface-core.h52
-rw-r--r--src/modules/dbus/iface-device-port.c190
-rw-r--r--src/modules/dbus/iface-device-port.h50
-rw-r--r--src/modules/dbus/iface-device.c1315
-rw-r--r--src/modules/dbus/iface-device.h53
-rw-r--r--src/modules/dbus/iface-memstats.c230
-rw-r--r--src/modules/dbus/iface-memstats.h45
-rw-r--r--src/modules/dbus/iface-module.c336
-rw-r--r--src/modules/dbus/iface-module.h45
-rw-r--r--src/modules/dbus/iface-sample.c519
-rw-r--r--src/modules/dbus/iface-sample.h45
-rw-r--r--src/modules/dbus/iface-stream.c933
-rw-r--r--src/modules/dbus/iface-stream.h47
-rw-r--r--src/modules/dbus/module-dbus-protocol.c621
-rw-r--r--src/modules/echo-cancel/adrian-aec.c275
-rw-r--r--src/modules/echo-cancel/adrian-aec.h382
-rw-r--r--src/modules/echo-cancel/adrian-aec.orc8
-rw-r--r--src/modules/echo-cancel/adrian-license.txt17
-rw-r--r--src/modules/echo-cancel/adrian.c117
-rw-r--r--src/modules/echo-cancel/adrian.h31
-rw-r--r--src/modules/echo-cancel/echo-cancel.h90
-rw-r--r--src/modules/echo-cancel/module-echo-cancel.c1778
-rw-r--r--src/modules/echo-cancel/speex.c115
-rw-r--r--src/modules/gconf/gconf-helper.c133
-rw-r--r--src/modules/gconf/module-gconf.c402
-rw-r--r--src/modules/hal-util.c130
-rw-r--r--src/modules/hal-util.h30
-rw-r--r--src/modules/jack/module-jack-sink.c509
-rw-r--r--src/modules/jack/module-jack-source.c453
-rw-r--r--src/modules/jack/module-jackdbus-detect.c298
-rw-r--r--src/modules/ladspa.h603
-rw-r--r--src/modules/macosx/module-bonjour-publish.c513
-rw-r--r--src/modules/macosx/module-coreaudio-detect.c285
-rw-r--r--src/modules/macosx/module-coreaudio-device.c876
-rw-r--r--src/modules/module-alsa-sink.c503
-rw-r--r--src/modules/module-alsa-source.c491
-rw-r--r--src/modules/module-always-sink.c189
-rw-r--r--src/modules/module-augment-properties.c382
-rw-r--r--src/modules/module-card-restore.c368
-rw-r--r--src/modules/module-cli.c100
-rw-r--r--src/modules/module-combine-sink.c1414
-rw-r--r--src/modules/module-combine.c427
-rw-r--r--src/modules/module-console-kit.c365
-rw-r--r--src/modules/module-cork-music-on-phone.c237
-rw-r--r--src/modules/module-default-device-restore.c199
-rw-r--r--src/modules/module-defs.h.m412
-rw-r--r--src/modules/module-detect.c136
-rw-r--r--src/modules/module-device-manager.c1700
-rw-r--r--src/modules/module-device-restore.c962
-rw-r--r--src/modules/module-equalizer-sink.c2221
-rw-r--r--src/modules/module-esound-compat-spawnfd.c43
-rw-r--r--src/modules/module-esound-compat-spawnpid.c41
-rw-r--r--src/modules/module-esound-sink.c632
-rw-r--r--src/modules/module-filter-apply.c600
-rw-r--r--src/modules/module-filter-heuristics.c217
-rw-r--r--src/modules/module-hal-detect-compat.c84
-rw-r--r--src/modules/module-hal-detect.c860
-rw-r--r--src/modules/module-intended-roles.c464
-rw-r--r--src/modules/module-jack-sink.c404
-rw-r--r--src/modules/module-jack-source.c402
-rw-r--r--src/modules/module-ladspa-sink.c1024
-rw-r--r--src/modules/module-lirc.c166
-rw-r--r--src/modules/module-loopback.c857
-rw-r--r--src/modules/module-match.c211
-rw-r--r--src/modules/module-mmkbd-evdev.c183
-rw-r--r--src/modules/module-native-protocol-fd.c65
-rw-r--r--src/modules/module-null-sink.c344
-rw-r--r--src/modules/module-null-source.c288
-rw-r--r--src/modules/module-oss-mmap.c579
-rw-r--r--src/modules/module-oss.c520
-rw-r--r--src/modules/module-pipe-sink.c379
-rw-r--r--src/modules/module-pipe-source.c352
-rw-r--r--src/modules/module-position-event-sounds.c179
-rw-r--r--src/modules/module-protocol-stub.c549
-rw-r--r--src/modules/module-remap-sink.c498
-rw-r--r--src/modules/module-rescue-streams.c277
-rw-r--r--src/modules/module-rygel-media-server.c1125
-rw-r--r--src/modules/module-sine-source.c326
-rw-r--r--src/modules/module-sine.c171
-rw-r--r--src/modules/module-solaris.c1227
-rw-r--r--src/modules/module-stream-restore.c2286
-rw-r--r--src/modules/module-suspend-on-idle.c567
-rw-r--r--src/modules/module-switch-on-connect.c187
-rw-r--r--src/modules/module-tunnel.c2176
-rw-r--r--src/modules/module-udev-detect.c809
-rw-r--r--src/modules/module-virtual-sink.c669
-rw-r--r--src/modules/module-virtual-source.c746
-rw-r--r--src/modules/module-volume-restore.c355
-rw-r--r--src/modules/module-waveout.c620
-rw-r--r--src/modules/module-x11-publish.c191
-rw-r--r--src/modules/module-zeroconf-discover.c433
-rw-r--r--src/modules/module-zeroconf-publish.c781
-rw-r--r--src/modules/oss-util.c299
-rw-r--r--src/modules/oss/module-oss.c1570
-rw-r--r--src/modules/oss/oss-util.c429
-rw-r--r--src/modules/oss/oss-util.h (renamed from src/modules/oss-util.h)22
-rw-r--r--src/modules/raop/base64.c126
-rw-r--r--src/modules/raop/base64.h34
-rw-r--r--src/modules/raop/module-raop-discover.c392
-rw-r--r--src/modules/raop/module-raop-sink.c686
-rw-r--r--src/modules/raop/raop_client.c551
-rw-r--r--src/modules/raop/raop_client.h44
-rw-r--r--src/modules/reserve-monitor.c256
-rw-r--r--src/modules/reserve-monitor.h71
-rw-r--r--src/modules/reserve-wrap.c344
-rw-r--r--src/modules/reserve-wrap.h45
-rw-r--r--src/modules/reserve.c608
-rw-r--r--src/modules/reserve.h79
-rw-r--r--src/modules/rtp/Makefile13
-rw-r--r--src/modules/rtp/headerlist.c186
-rw-r--r--src/modules/rtp/headerlist.h46
-rw-r--r--src/modules/rtp/module-rtp-recv.c649
-rw-r--r--src/modules/rtp/module-rtp-send.c320
-rw-r--r--src/modules/rtp/rtp.c228
-rw-r--r--src/modules/rtp/rtp.h19
-rw-r--r--src/modules/rtp/rtsp_client.c530
-rw-r--r--src/modules/rtp/rtsp_client.h71
-rw-r--r--src/modules/rtp/sap.c131
-rw-r--r--src/modules/rtp/sap.h16
-rw-r--r--src/modules/rtp/sdp.c119
-rw-r--r--src/modules/rtp/sdp.h12
-rw-r--r--src/modules/udev-util.c301
-rw-r--r--src/modules/udev-util.h31
-rw-r--r--src/modules/x11/module-x11-bell.c (renamed from src/modules/module-x11-bell.c)136
-rw-r--r--src/modules/x11/module-x11-cork-request.c187
-rw-r--r--src/modules/x11/module-x11-publish.c246
-rw-r--r--src/modules/x11/module-x11-xsmp.c249
217 files changed, 75789 insertions, 7903 deletions
diff --git a/src/modules/Makefile b/src/modules/Makefile
deleted file mode 120000
index c110232d..00000000
--- a/src/modules/Makefile
+++ /dev/null
@@ -1 +0,0 @@
-../pulse/Makefile \ No newline at end of file
diff --git a/src/modules/alsa-util.c b/src/modules/alsa-util.c
deleted file mode 100644
index 04a2d849..00000000
--- a/src/modules/alsa-util.c
+++ /dev/null
@@ -1,433 +0,0 @@
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <sys/types.h>
-#include <asoundlib.h>
-
-#include <pulse/sample.h>
-#include <pulse/xmalloc.h>
-
-#include <pulsecore/log.h>
-
-#include "alsa-util.h"
-
-struct pa_alsa_fdlist {
- int num_fds;
- struct pollfd *fds;
- /* This is a temporary buffer used to avoid lots of mallocs */
- struct pollfd *work_fds;
-
- snd_pcm_t *pcm;
- snd_mixer_t *mixer;
-
- pa_mainloop_api *m;
- pa_defer_event *defer;
- pa_io_event **ios;
-
- int polled;
-
- void (*cb)(void *userdata);
- void *userdata;
-};
-
-static void io_cb(pa_mainloop_api*a, pa_io_event* e, PA_GCC_UNUSED int fd, pa_io_event_flags_t events, void *userdata) {
- struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
- int err, i;
- unsigned short revents;
-
- assert(a && fdl && (fdl->pcm || fdl->mixer) && fdl->fds && fdl->work_fds);
-
- if (fdl->polled)
- return;
-
- fdl->polled = 1;
-
- memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
-
- for (i = 0;i < fdl->num_fds;i++) {
- if (e == fdl->ios[i]) {
- if (events & PA_IO_EVENT_INPUT)
- fdl->work_fds[i].revents |= POLLIN;
- if (events & PA_IO_EVENT_OUTPUT)
- fdl->work_fds[i].revents |= POLLOUT;
- if (events & PA_IO_EVENT_ERROR)
- fdl->work_fds[i].revents |= POLLERR;
- if (events & PA_IO_EVENT_HANGUP)
- fdl->work_fds[i].revents |= POLLHUP;
- break;
- }
- }
-
- assert(i != fdl->num_fds);
-
- if (fdl->pcm)
- err = snd_pcm_poll_descriptors_revents(fdl->pcm, fdl->work_fds, fdl->num_fds, &revents);
- else
- err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents);
-
- if (err < 0) {
- pa_log_error(__FILE__": Unable to get poll revent: %s",
- snd_strerror(err));
- return;
- }
-
- if (revents) {
- if (fdl->pcm)
- fdl->cb(fdl->userdata);
- else
- snd_mixer_handle_events(fdl->mixer);
- }
-
- a->defer_enable(fdl->defer, 1);
-}
-
-static void defer_cb(pa_mainloop_api*a, PA_GCC_UNUSED pa_defer_event* e, void *userdata) {
- struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
- int num_fds, i, err;
- struct pollfd *temp;
-
- assert(a && fdl && (fdl->pcm || fdl->mixer));
-
- a->defer_enable(fdl->defer, 0);
-
- if (fdl->pcm)
- num_fds = snd_pcm_poll_descriptors_count(fdl->pcm);
- else
- num_fds = snd_mixer_poll_descriptors_count(fdl->mixer);
- assert(num_fds > 0);
-
- if (num_fds != fdl->num_fds) {
- if (fdl->fds)
- pa_xfree(fdl->fds);
- if (fdl->work_fds)
- pa_xfree(fdl->work_fds);
- fdl->fds = pa_xmalloc0(sizeof(struct pollfd) * num_fds);
- fdl->work_fds = pa_xmalloc(sizeof(struct pollfd) * num_fds);
- }
-
- memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
-
- if (fdl->pcm)
- err = snd_pcm_poll_descriptors(fdl->pcm, fdl->work_fds, num_fds);
- else
- err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds);
-
- if (err < 0) {
- pa_log_error(__FILE__": Unable to get poll descriptors: %s",
- snd_strerror(err));
- return;
- }
-
- fdl->polled = 0;
-
- if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
- return;
-
- if (fdl->ios) {
- for (i = 0;i < fdl->num_fds;i++)
- a->io_free(fdl->ios[i]);
- if (num_fds != fdl->num_fds) {
- pa_xfree(fdl->ios);
- fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
- assert(fdl->ios);
- }
- } else {
- fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
- assert(fdl->ios);
- }
-
- /* Swap pointers */
- temp = fdl->work_fds;
- fdl->work_fds = fdl->fds;
- fdl->fds = temp;
-
- fdl->num_fds = num_fds;
-
- for (i = 0;i < num_fds;i++) {
- fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
- ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
- ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
- io_cb, fdl);
- assert(fdl->ios[i]);
- }
-}
-
-struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
- struct pa_alsa_fdlist *fdl;
-
- fdl = pa_xmalloc(sizeof(struct pa_alsa_fdlist));
- assert(fdl);
-
- fdl->num_fds = 0;
- fdl->fds = NULL;
- fdl->work_fds = NULL;
-
- fdl->pcm = NULL;
- fdl->mixer = NULL;
-
- fdl->m = NULL;
- fdl->defer = NULL;
- fdl->ios = NULL;
-
- fdl->polled = 0;
-
- return fdl;
-}
-
-void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
- assert(fdl);
-
- if (fdl->defer) {
- assert(fdl->m);
- fdl->m->defer_free(fdl->defer);
- }
-
- if (fdl->ios) {
- int i;
- assert(fdl->m);
- for (i = 0;i < fdl->num_fds;i++)
- fdl->m->io_free(fdl->ios[0]);
- pa_xfree(fdl->ios);
- }
-
- if (fdl->fds)
- pa_xfree(fdl->fds);
- if (fdl->work_fds)
- pa_xfree(fdl->work_fds);
-
- pa_xfree(fdl);
-}
-
-int pa_alsa_fdlist_init_pcm(struct pa_alsa_fdlist *fdl, snd_pcm_t *pcm_handle, pa_mainloop_api* m, void (*cb)(void *userdata), void *userdata) {
- assert(fdl && pcm_handle && m && !fdl->m && cb);
-
- fdl->pcm = pcm_handle;
- fdl->m = m;
-
- fdl->defer = m->defer_new(m, defer_cb, fdl);
- assert(fdl->defer);
-
- fdl->cb = cb;
- fdl->userdata = userdata;
-
- return 0;
-}
-
-int pa_alsa_fdlist_init_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
- assert(fdl && mixer_handle && m && !fdl->m);
-
- fdl->mixer = mixer_handle;
- fdl->m = m;
-
- fdl->defer = m->defer_new(m, defer_cb, fdl);
- assert(fdl->defer);
-
- return 0;
-}
-
-static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
-
- static const snd_pcm_format_t format_trans[] = {
- [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
- [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
- [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
- [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
- [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
- [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
- [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
- };
-
- static const pa_sample_format_t try_order[] = {
- PA_SAMPLE_S16NE,
- PA_SAMPLE_S16RE,
- PA_SAMPLE_FLOAT32NE,
- PA_SAMPLE_FLOAT32RE,
- PA_SAMPLE_ULAW,
- PA_SAMPLE_ALAW,
- PA_SAMPLE_U8,
- PA_SAMPLE_INVALID
- };
-
- int i, ret;
-
- assert(pcm_handle);
- assert(f);
-
- if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
- return ret;
-
- if (*f == PA_SAMPLE_FLOAT32BE)
- *f = PA_SAMPLE_FLOAT32LE;
- else if (*f == PA_SAMPLE_FLOAT32LE)
- *f = PA_SAMPLE_FLOAT32BE;
- else if (*f == PA_SAMPLE_S16BE)
- *f = PA_SAMPLE_S16LE;
- else if (*f == PA_SAMPLE_S16LE)
- *f = PA_SAMPLE_S16BE;
- else
- goto try_auto;
-
- if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
- return ret;
-
-try_auto:
-
- for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
- *f = try_order[i];
-
- if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
- return ret;
- }
-
- return -1;
-}
-
-/* Set the hardware parameters of the given ALSA device. Returns the
- * selected fragment settings in *period and *period_size */
-int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size) {
- int ret = -1;
- snd_pcm_uframes_t buffer_size;
- unsigned int r = ss->rate;
- unsigned int c = ss->channels;
- pa_sample_format_t f = ss->format;
- snd_pcm_hw_params_t *hwparams;
-
- assert(pcm_handle);
- assert(ss);
- assert(periods);
- assert(period_size);
-
- buffer_size = *periods * *period_size;
-
- if ((ret = snd_pcm_hw_params_malloc(&hwparams)) < 0 ||
- (ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0 ||
- (ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0 ||
- (ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
- goto finish;
-
- if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
- goto finish;
-
- if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
- goto finish;
-
- if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
- goto finish;
-
- if ((*period_size > 0 && (ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL)) < 0) ||
- (*periods > 0 && (ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0))
- goto finish;
-
- if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
- goto finish;
-
- if (ss->rate != r) {
- pa_log_warn(__FILE__": device doesn't support %u Hz, changed to %u Hz.", ss->rate, r);
-
- /* If the sample rate deviates too much, we need to resample */
- if (r < ss->rate*.95 || r > ss->rate*1.05)
- ss->rate = r;
- }
-
- if (ss->channels != c) {
- pa_log_warn(__FILE__": device doesn't support %u channels, changed to %u.", ss->channels, c);
- ss->channels = c;
- }
-
- if (ss->format != f) {
- pa_log_warn(__FILE__": device doesn't support sample format %s, changed to %s.", pa_sample_format_to_string(ss->format), pa_sample_format_to_string(f));
- ss->format = f;
- }
-
- if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
- goto finish;
-
- if ((ret = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0 ||
- (ret = snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL)) < 0)
- goto finish;
-
- assert(buffer_size > 0);
- assert(*period_size > 0);
- *periods = buffer_size / *period_size;
- assert(*periods > 0);
-
- ret = 0;
-
-finish:
- if (hwparams)
- snd_pcm_hw_params_free(hwparams);
-
- return ret;
-}
-
-int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
- int err;
-
- assert(mixer && dev);
-
- if ((err = snd_mixer_attach(mixer, dev)) < 0) {
- pa_log_warn(__FILE__": Unable to attach to mixer %s: %s", dev, snd_strerror(err));
- return -1;
- }
-
- if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
- pa_log_warn(__FILE__": Unable to register mixer: %s", snd_strerror(err));
- return -1;
- }
-
- if ((err = snd_mixer_load(mixer)) < 0) {
- pa_log_warn(__FILE__": Unable to load mixer: %s", snd_strerror(err));
- return -1;
- }
-
- return 0;
-}
-
-snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback) {
- snd_mixer_elem_t *elem;
- snd_mixer_selem_id_t *sid = NULL;
- snd_mixer_selem_id_alloca(&sid);
-
- assert(mixer);
- assert(name);
-
- snd_mixer_selem_id_set_name(sid, name);
-
- if (!(elem = snd_mixer_find_selem(mixer, sid))) {
- pa_log_warn(__FILE__": Cannot find mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
-
- if (fallback) {
- snd_mixer_selem_id_set_name(sid, fallback);
-
- if (!(elem = snd_mixer_find_selem(mixer, sid)))
- pa_log_warn(__FILE__": Cannot find fallback mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
- }
- }
-
- if (elem)
- pa_log_info(__FILE__": Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
-
- return elem;
-}
diff --git a/src/modules/alsa-util.h b/src/modules/alsa-util.h
deleted file mode 100644
index 215844b4..00000000
--- a/src/modules/alsa-util.h
+++ /dev/null
@@ -1,45 +0,0 @@
-#ifndef fooalsautilhfoo
-#define fooalsautilhfoo
-
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#include <asoundlib.h>
-
-#include <pulse/sample.h>
-#include <pulse/mainloop-api.h>
-
-#include <pulse/channelmap.h>
-
-struct pa_alsa_fdlist;
-
-struct pa_alsa_fdlist *pa_alsa_fdlist_new(void);
-void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl);
-
-int pa_alsa_fdlist_init_pcm(struct pa_alsa_fdlist *fdl, snd_pcm_t *pcm_handle, pa_mainloop_api* m, void (*cb)(void *userdata), void *userdata);
-int pa_alsa_fdlist_init_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m);
-
-int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size);
-
-int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev);
-snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback);
-
-#endif
diff --git a/src/modules/alsa/alsa-mixer.c b/src/modules/alsa/alsa-mixer.c
new file mode 100644
index 00000000..348f037f
--- /dev/null
+++ b/src/modules/alsa/alsa-mixer.c
@@ -0,0 +1,4194 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2009 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <asoundlib.h>
+#include <math.h>
+
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+#endif
+
+#include <pulse/mainloop-api.h>
+#include <pulse/sample.h>
+#include <pulse/timeval.h>
+#include <pulse/util.h>
+#include <pulse/volume.h>
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+#include <pulse/utf8.h>
+
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/conf-parser.h>
+#include <pulsecore/strbuf.h>
+
+#include "alsa-mixer.h"
+#include "alsa-util.h"
+
+struct description_map {
+ const char *name;
+ const char *description;
+};
+
+static const char *lookup_description(const char *name, const struct description_map dm[], unsigned n) {
+ unsigned i;
+
+ for (i = 0; i < n; i++)
+ if (pa_streq(dm[i].name, name))
+ return _(dm[i].description);
+
+ return NULL;
+}
+
+struct pa_alsa_fdlist {
+ unsigned num_fds;
+ struct pollfd *fds;
+ /* This is a temporary buffer used to avoid lots of mallocs */
+ struct pollfd *work_fds;
+
+ snd_mixer_t *mixer;
+
+ pa_mainloop_api *m;
+ pa_defer_event *defer;
+ pa_io_event **ios;
+
+ pa_bool_t polled;
+
+ void (*cb)(void *userdata);
+ void *userdata;
+};
+
+static void io_cb(pa_mainloop_api *a, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata) {
+
+ struct pa_alsa_fdlist *fdl = userdata;
+ int err;
+ unsigned i;
+ unsigned short revents;
+
+ pa_assert(a);
+ pa_assert(fdl);
+ pa_assert(fdl->mixer);
+ pa_assert(fdl->fds);
+ pa_assert(fdl->work_fds);
+
+ if (fdl->polled)
+ return;
+
+ fdl->polled = TRUE;
+
+ memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
+
+ for (i = 0; i < fdl->num_fds; i++) {
+ if (e == fdl->ios[i]) {
+ if (events & PA_IO_EVENT_INPUT)
+ fdl->work_fds[i].revents |= POLLIN;
+ if (events & PA_IO_EVENT_OUTPUT)
+ fdl->work_fds[i].revents |= POLLOUT;
+ if (events & PA_IO_EVENT_ERROR)
+ fdl->work_fds[i].revents |= POLLERR;
+ if (events & PA_IO_EVENT_HANGUP)
+ fdl->work_fds[i].revents |= POLLHUP;
+ break;
+ }
+ }
+
+ pa_assert(i != fdl->num_fds);
+
+ if ((err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents)) < 0) {
+ pa_log_error("Unable to get poll revent: %s", pa_alsa_strerror(err));
+ return;
+ }
+
+ a->defer_enable(fdl->defer, 1);
+
+ if (revents)
+ snd_mixer_handle_events(fdl->mixer);
+}
+
+static void defer_cb(pa_mainloop_api *a, pa_defer_event *e, void *userdata) {
+ struct pa_alsa_fdlist *fdl = userdata;
+ unsigned num_fds, i;
+ int err, n;
+ struct pollfd *temp;
+
+ pa_assert(a);
+ pa_assert(fdl);
+ pa_assert(fdl->mixer);
+
+ a->defer_enable(fdl->defer, 0);
+
+ if ((n = snd_mixer_poll_descriptors_count(fdl->mixer)) < 0) {
+ pa_log("snd_mixer_poll_descriptors_count() failed: %s", pa_alsa_strerror(n));
+ return;
+ }
+ num_fds = (unsigned) n;
+
+ if (num_fds != fdl->num_fds) {
+ if (fdl->fds)
+ pa_xfree(fdl->fds);
+ if (fdl->work_fds)
+ pa_xfree(fdl->work_fds);
+ fdl->fds = pa_xnew0(struct pollfd, num_fds);
+ fdl->work_fds = pa_xnew(struct pollfd, num_fds);
+ }
+
+ memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
+
+ if ((err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds)) < 0) {
+ pa_log_error("Unable to get poll descriptors: %s", pa_alsa_strerror(err));
+ return;
+ }
+
+ fdl->polled = FALSE;
+
+ if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
+ return;
+
+ if (fdl->ios) {
+ for (i = 0; i < fdl->num_fds; i++)
+ a->io_free(fdl->ios[i]);
+
+ if (num_fds != fdl->num_fds) {
+ pa_xfree(fdl->ios);
+ fdl->ios = NULL;
+ }
+ }
+
+ if (!fdl->ios)
+ fdl->ios = pa_xnew(pa_io_event*, num_fds);
+
+ /* Swap pointers */
+ temp = fdl->work_fds;
+ fdl->work_fds = fdl->fds;
+ fdl->fds = temp;
+
+ fdl->num_fds = num_fds;
+
+ for (i = 0;i < num_fds;i++)
+ fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
+ ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
+ ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
+ io_cb, fdl);
+}
+
+struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
+ struct pa_alsa_fdlist *fdl;
+
+ fdl = pa_xnew0(struct pa_alsa_fdlist, 1);
+
+ return fdl;
+}
+
+void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
+ pa_assert(fdl);
+
+ if (fdl->defer) {
+ pa_assert(fdl->m);
+ fdl->m->defer_free(fdl->defer);
+ }
+
+ if (fdl->ios) {
+ unsigned i;
+ pa_assert(fdl->m);
+ for (i = 0; i < fdl->num_fds; i++)
+ fdl->m->io_free(fdl->ios[i]);
+ pa_xfree(fdl->ios);
+ }
+
+ if (fdl->fds)
+ pa_xfree(fdl->fds);
+ if (fdl->work_fds)
+ pa_xfree(fdl->work_fds);
+
+ pa_xfree(fdl);
+}
+
+int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api *m) {
+ pa_assert(fdl);
+ pa_assert(mixer_handle);
+ pa_assert(m);
+ pa_assert(!fdl->m);
+
+ fdl->mixer = mixer_handle;
+ fdl->m = m;
+ fdl->defer = m->defer_new(m, defer_cb, fdl);
+
+ return 0;
+}
+
+struct pa_alsa_mixer_pdata {
+ pa_rtpoll *rtpoll;
+ pa_rtpoll_item *poll_item;
+ snd_mixer_t *mixer;
+};
+
+
+struct pa_alsa_mixer_pdata *pa_alsa_mixer_pdata_new(void) {
+ struct pa_alsa_mixer_pdata *pd;
+
+ pd = pa_xnew0(struct pa_alsa_mixer_pdata, 1);
+
+ return pd;
+}
+
+void pa_alsa_mixer_pdata_free(struct pa_alsa_mixer_pdata *pd) {
+ pa_assert(pd);
+
+ if (pd->poll_item) {
+ pa_rtpoll_item_free(pd->poll_item);
+ }
+
+ pa_xfree(pd);
+}
+
+static int rtpoll_work_cb(pa_rtpoll_item *i) {
+ struct pa_alsa_mixer_pdata *pd;
+ struct pollfd *p;
+ unsigned n_fds;
+ unsigned short revents = 0;
+ int err;
+
+ pd = pa_rtpoll_item_get_userdata(i);
+ pa_assert_fp(pd);
+ pa_assert_fp(i == pd->poll_item);
+
+ p = pa_rtpoll_item_get_pollfd(i, &n_fds);
+
+ if ((err = snd_mixer_poll_descriptors_revents(pd->mixer, p, n_fds, &revents)) < 0) {
+ pa_log_error("Unable to get poll revent: %s", pa_alsa_strerror(err));
+ pa_rtpoll_item_free(i);
+ return -1;
+ }
+
+ if (revents) {
+ snd_mixer_handle_events(pd->mixer);
+ pa_rtpoll_item_free(i);
+ pa_alsa_set_mixer_rtpoll(pd, pd->mixer, pd->rtpoll);
+ }
+
+ return 0;
+}
+
+int pa_alsa_set_mixer_rtpoll(struct pa_alsa_mixer_pdata *pd, snd_mixer_t *mixer, pa_rtpoll *rtp) {
+ pa_rtpoll_item *i;
+ struct pollfd *p;
+ int err, n;
+
+ pa_assert(pd);
+ pa_assert(mixer);
+ pa_assert(rtp);
+
+ if ((n = snd_mixer_poll_descriptors_count(mixer)) < 0) {
+ pa_log("snd_mixer_poll_descriptors_count() failed: %s", pa_alsa_strerror(n));
+ return -1;
+ }
+
+ i = pa_rtpoll_item_new(rtp, PA_RTPOLL_LATE, (unsigned) n);
+
+ p = pa_rtpoll_item_get_pollfd(i, NULL);
+
+ memset(p, 0, sizeof(struct pollfd) * n);
+
+ if ((err = snd_mixer_poll_descriptors(mixer, p, (unsigned) n)) < 0) {
+ pa_log_error("Unable to get poll descriptors: %s", pa_alsa_strerror(err));
+ pa_rtpoll_item_free(i);
+ return -1;
+ }
+
+ pd->rtpoll = rtp;
+ pd->poll_item = i;
+ pd->mixer = mixer;
+
+ pa_rtpoll_item_set_userdata(i, pd);
+ pa_rtpoll_item_set_work_callback(i, rtpoll_work_cb);
+
+ return 0;
+}
+
+static int prepare_mixer(snd_mixer_t *mixer, const char *dev) {
+ int err;
+
+ pa_assert(mixer);
+ pa_assert(dev);
+
+ if ((err = snd_mixer_attach(mixer, dev)) < 0) {
+ pa_log_info("Unable to attach to mixer %s: %s", dev, pa_alsa_strerror(err));
+ return -1;
+ }
+
+ if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
+ pa_log_warn("Unable to register mixer: %s", pa_alsa_strerror(err));
+ return -1;
+ }
+
+ if ((err = snd_mixer_load(mixer)) < 0) {
+ pa_log_warn("Unable to load mixer: %s", pa_alsa_strerror(err));
+ return -1;
+ }
+
+ pa_log_info("Successfully attached to mixer '%s'", dev);
+ return 0;
+}
+
+snd_mixer_t *pa_alsa_open_mixer_for_pcm(snd_pcm_t *pcm, char **ctl_device) {
+ int err;
+ snd_mixer_t *m;
+ const char *dev;
+ snd_pcm_info_t* info;
+ snd_pcm_info_alloca(&info);
+
+ pa_assert(pcm);
+
+ if ((err = snd_mixer_open(&m, 0)) < 0) {
+ pa_log("Error opening mixer: %s", pa_alsa_strerror(err));
+ return NULL;
+ }
+
+ /* First, try by name */
+ if ((dev = snd_pcm_name(pcm)))
+ if (prepare_mixer(m, dev) >= 0) {
+ if (ctl_device)
+ *ctl_device = pa_xstrdup(dev);
+
+ return m;
+ }
+
+ /* Then, try by card index */
+ if (snd_pcm_info(pcm, info) >= 0) {
+ char *md;
+ int card_idx;
+
+ if ((card_idx = snd_pcm_info_get_card(info)) >= 0) {
+
+ md = pa_sprintf_malloc("hw:%i", card_idx);
+
+ if (!dev || !pa_streq(dev, md))
+ if (prepare_mixer(m, md) >= 0) {
+
+ if (ctl_device)
+ *ctl_device = md;
+ else
+ pa_xfree(md);
+
+ return m;
+ }
+
+ pa_xfree(md);
+ }
+ }
+
+ snd_mixer_close(m);
+ return NULL;
+}
+
+static const snd_mixer_selem_channel_id_t alsa_channel_ids[PA_CHANNEL_POSITION_MAX] = {
+ [PA_CHANNEL_POSITION_MONO] = SND_MIXER_SCHN_MONO, /* The ALSA name is just an alias! */
+
+ [PA_CHANNEL_POSITION_FRONT_CENTER] = SND_MIXER_SCHN_FRONT_CENTER,
+ [PA_CHANNEL_POSITION_FRONT_LEFT] = SND_MIXER_SCHN_FRONT_LEFT,
+ [PA_CHANNEL_POSITION_FRONT_RIGHT] = SND_MIXER_SCHN_FRONT_RIGHT,
+
+ [PA_CHANNEL_POSITION_REAR_CENTER] = SND_MIXER_SCHN_REAR_CENTER,
+ [PA_CHANNEL_POSITION_REAR_LEFT] = SND_MIXER_SCHN_REAR_LEFT,
+ [PA_CHANNEL_POSITION_REAR_RIGHT] = SND_MIXER_SCHN_REAR_RIGHT,
+
+ [PA_CHANNEL_POSITION_LFE] = SND_MIXER_SCHN_WOOFER,
+
+ [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
+
+ [PA_CHANNEL_POSITION_SIDE_LEFT] = SND_MIXER_SCHN_SIDE_LEFT,
+ [PA_CHANNEL_POSITION_SIDE_RIGHT] = SND_MIXER_SCHN_SIDE_RIGHT,
+
+ [PA_CHANNEL_POSITION_AUX0] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX1] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX2] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX3] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX4] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX5] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX6] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX7] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX8] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX9] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX10] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX11] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX12] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX13] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX14] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX15] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX16] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX17] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX18] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX19] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX20] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX21] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX22] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX23] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX24] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX25] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX26] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX27] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX28] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX29] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX30] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_AUX31] = SND_MIXER_SCHN_UNKNOWN,
+
+ [PA_CHANNEL_POSITION_TOP_CENTER] = SND_MIXER_SCHN_UNKNOWN,
+
+ [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SND_MIXER_SCHN_UNKNOWN,
+
+ [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SND_MIXER_SCHN_UNKNOWN,
+ [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SND_MIXER_SCHN_UNKNOWN
+};
+
+static void setting_free(pa_alsa_setting *s) {
+ pa_assert(s);
+
+ if (s->options)
+ pa_idxset_free(s->options, NULL, NULL);
+
+ pa_xfree(s->name);
+ pa_xfree(s->description);
+ pa_xfree(s);
+}
+
+static void option_free(pa_alsa_option *o) {
+ pa_assert(o);
+
+ pa_xfree(o->alsa_name);
+ pa_xfree(o->name);
+ pa_xfree(o->description);
+ pa_xfree(o);
+}
+
+static void decibel_fix_free(pa_alsa_decibel_fix *db_fix) {
+ pa_assert(db_fix);
+
+ pa_xfree(db_fix->name);
+ pa_xfree(db_fix->db_values);
+
+ pa_xfree(db_fix);
+}
+
+static void element_free(pa_alsa_element *e) {
+ pa_alsa_option *o;
+ pa_assert(e);
+
+ while ((o = e->options)) {
+ PA_LLIST_REMOVE(pa_alsa_option, e->options, o);
+ option_free(o);
+ }
+
+ if (e->db_fix)
+ decibel_fix_free(e->db_fix);
+
+ pa_xfree(e->alsa_name);
+ pa_xfree(e);
+}
+
+void pa_alsa_path_free(pa_alsa_path *p) {
+ pa_alsa_element *e;
+ pa_alsa_setting *s;
+
+ pa_assert(p);
+
+ while ((e = p->elements)) {
+ PA_LLIST_REMOVE(pa_alsa_element, p->elements, e);
+ element_free(e);
+ }
+
+ while ((s = p->settings)) {
+ PA_LLIST_REMOVE(pa_alsa_setting, p->settings, s);
+ setting_free(s);
+ }
+
+ pa_xfree(p->name);
+ pa_xfree(p->description);
+ pa_xfree(p);
+}
+
+void pa_alsa_path_set_free(pa_alsa_path_set *ps) {
+ pa_alsa_path *p;
+ pa_assert(ps);
+
+ while ((p = ps->paths)) {
+ PA_LLIST_REMOVE(pa_alsa_path, ps->paths, p);
+ pa_alsa_path_free(p);
+ }
+
+ pa_xfree(ps);
+}
+
+static long to_alsa_dB(pa_volume_t v) {
+ return (long) (pa_sw_volume_to_dB(v) * 100.0);
+}
+
+static pa_volume_t from_alsa_dB(long v) {
+ return pa_sw_volume_from_dB((double) v / 100.0);
+}
+
+static long to_alsa_volume(pa_volume_t v, long min, long max) {
+ long w;
+
+ w = (long) round(((double) v * (double) (max - min)) / PA_VOLUME_NORM) + min;
+ return PA_CLAMP_UNLIKELY(w, min, max);
+}
+
+static pa_volume_t from_alsa_volume(long v, long min, long max) {
+ return (pa_volume_t) round(((double) (v - min) * PA_VOLUME_NORM) / (double) (max - min));
+}
+
+#define SELEM_INIT(sid, name) \
+ do { \
+ snd_mixer_selem_id_alloca(&(sid)); \
+ snd_mixer_selem_id_set_name((sid), (name)); \
+ snd_mixer_selem_id_set_index((sid), 0); \
+ } while(FALSE)
+
+static int element_get_volume(pa_alsa_element *e, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v) {
+ snd_mixer_selem_id_t *sid;
+ snd_mixer_elem_t *me;
+ snd_mixer_selem_channel_id_t c;
+ pa_channel_position_mask_t mask = 0;
+ unsigned k;
+
+ pa_assert(m);
+ pa_assert(e);
+ pa_assert(cm);
+ pa_assert(v);
+
+ SELEM_INIT(sid, e->alsa_name);
+ if (!(me = snd_mixer_find_selem(m, sid))) {
+ pa_log_warn("Element %s seems to have disappeared.", e->alsa_name);
+ return -1;
+ }
+
+ pa_cvolume_mute(v, cm->channels);
+
+ /* We take the highest volume of all channels that match */
+
+ for (c = 0; c <= SND_MIXER_SCHN_LAST; c++) {
+ int r;
+ pa_volume_t f;
+
+ if (e->has_dB) {
+ long value = 0;
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT) {
+ if (snd_mixer_selem_has_playback_channel(me, c)) {
+ if (e->db_fix) {
+ if ((r = snd_mixer_selem_get_playback_volume(me, c, &value)) >= 0) {
+ /* If the channel volume is outside the limits set
+ * by the dB fix, we clamp the hw volume to be
+ * within the limits. */
+ if (value < e->db_fix->min_step) {
+ value = e->db_fix->min_step;
+ snd_mixer_selem_set_playback_volume(me, c, value);
+ pa_log_debug("Playback volume for element %s channel %i was below the dB fix limit. "
+ "Volume reset to %0.2f dB.", e->alsa_name, c,
+ e->db_fix->db_values[value - e->db_fix->min_step] / 100.0);
+ } else if (value > e->db_fix->max_step) {
+ value = e->db_fix->max_step;
+ snd_mixer_selem_set_playback_volume(me, c, value);
+ pa_log_debug("Playback volume for element %s channel %i was over the dB fix limit. "
+ "Volume reset to %0.2f dB.", e->alsa_name, c,
+ e->db_fix->db_values[value - e->db_fix->min_step] / 100.0);
+ }
+
+ /* Volume step -> dB value conversion. */
+ value = e->db_fix->db_values[value - e->db_fix->min_step];
+ }
+ } else
+ r = snd_mixer_selem_get_playback_dB(me, c, &value);
+ } else
+ r = -1;
+ } else {
+ if (snd_mixer_selem_has_capture_channel(me, c)) {
+ if (e->db_fix) {
+ if ((r = snd_mixer_selem_get_capture_volume(me, c, &value)) >= 0) {
+ /* If the channel volume is outside the limits set
+ * by the dB fix, we clamp the hw volume to be
+ * within the limits. */
+ if (value < e->db_fix->min_step) {
+ value = e->db_fix->min_step;
+ snd_mixer_selem_set_capture_volume(me, c, value);
+ pa_log_debug("Capture volume for element %s channel %i was below the dB fix limit. "
+ "Volume reset to %0.2f dB.", e->alsa_name, c,
+ e->db_fix->db_values[value - e->db_fix->min_step] / 100.0);
+ } else if (value > e->db_fix->max_step) {
+ value = e->db_fix->max_step;
+ snd_mixer_selem_set_capture_volume(me, c, value);
+ pa_log_debug("Capture volume for element %s channel %i was over the dB fix limit. "
+ "Volume reset to %0.2f dB.", e->alsa_name, c,
+ e->db_fix->db_values[value - e->db_fix->min_step] / 100.0);
+ }
+
+ /* Volume step -> dB value conversion. */
+ value = e->db_fix->db_values[value - e->db_fix->min_step];
+ }
+ } else
+ r = snd_mixer_selem_get_capture_dB(me, c, &value);
+ } else
+ r = -1;
+ }
+
+ if (r < 0)
+ continue;
+
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+ VALGRIND_MAKE_MEM_DEFINED(&value, sizeof(value));
+#endif
+
+ f = from_alsa_dB(value);
+
+ } else {
+ long value = 0;
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT) {
+ if (snd_mixer_selem_has_playback_channel(me, c))
+ r = snd_mixer_selem_get_playback_volume(me, c, &value);
+ else
+ r = -1;
+ } else {
+ if (snd_mixer_selem_has_capture_channel(me, c))
+ r = snd_mixer_selem_get_capture_volume(me, c, &value);
+ else
+ r = -1;
+ }
+
+ if (r < 0)
+ continue;
+
+ f = from_alsa_volume(value, e->min_volume, e->max_volume);
+ }
+
+ for (k = 0; k < cm->channels; k++)
+ if (e->masks[c][e->n_channels-1] & PA_CHANNEL_POSITION_MASK(cm->map[k]))
+ if (v->values[k] < f)
+ v->values[k] = f;
+
+ mask |= e->masks[c][e->n_channels-1];
+ }
+
+ for (k = 0; k < cm->channels; k++)
+ if (!(mask & PA_CHANNEL_POSITION_MASK(cm->map[k])))
+ v->values[k] = PA_VOLUME_NORM;
+
+ return 0;
+}
+
+int pa_alsa_path_get_volume(pa_alsa_path *p, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v) {
+ pa_alsa_element *e;
+
+ pa_assert(m);
+ pa_assert(p);
+ pa_assert(cm);
+ pa_assert(v);
+
+ if (!p->has_volume)
+ return -1;
+
+ pa_cvolume_reset(v, cm->channels);
+
+ PA_LLIST_FOREACH(e, p->elements) {
+ pa_cvolume ev;
+
+ if (e->volume_use != PA_ALSA_VOLUME_MERGE)
+ continue;
+
+ pa_assert(!p->has_dB || e->has_dB);
+
+ if (element_get_volume(e, m, cm, &ev) < 0)
+ return -1;
+
+ /* If we have no dB information all we can do is take the first element and leave */
+ if (!p->has_dB) {
+ *v = ev;
+ return 0;
+ }
+
+ pa_sw_cvolume_multiply(v, v, &ev);
+ }
+
+ return 0;
+}
+
+static int element_get_switch(pa_alsa_element *e, snd_mixer_t *m, pa_bool_t *b) {
+ snd_mixer_selem_id_t *sid;
+ snd_mixer_elem_t *me;
+ snd_mixer_selem_channel_id_t c;
+
+ pa_assert(m);
+ pa_assert(e);
+ pa_assert(b);
+
+ SELEM_INIT(sid, e->alsa_name);
+ if (!(me = snd_mixer_find_selem(m, sid))) {
+ pa_log_warn("Element %s seems to have disappeared.", e->alsa_name);
+ return -1;
+ }
+
+ /* We return muted if at least one channel is muted */
+
+ for (c = 0; c <= SND_MIXER_SCHN_LAST; c++) {
+ int r;
+ int value = 0;
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT) {
+ if (snd_mixer_selem_has_playback_channel(me, c))
+ r = snd_mixer_selem_get_playback_switch(me, c, &value);
+ else
+ r = -1;
+ } else {
+ if (snd_mixer_selem_has_capture_channel(me, c))
+ r = snd_mixer_selem_get_capture_switch(me, c, &value);
+ else
+ r = -1;
+ }
+
+ if (r < 0)
+ continue;
+
+ if (!value) {
+ *b = FALSE;
+ return 0;
+ }
+ }
+
+ *b = TRUE;
+ return 0;
+}
+
+int pa_alsa_path_get_mute(pa_alsa_path *p, snd_mixer_t *m, pa_bool_t *muted) {
+ pa_alsa_element *e;
+
+ pa_assert(m);
+ pa_assert(p);
+ pa_assert(muted);
+
+ if (!p->has_mute)
+ return -1;
+
+ PA_LLIST_FOREACH(e, p->elements) {
+ pa_bool_t b;
+
+ if (e->switch_use != PA_ALSA_SWITCH_MUTE)
+ continue;
+
+ if (element_get_switch(e, m, &b) < 0)
+ return -1;
+
+ if (!b) {
+ *muted = TRUE;
+ return 0;
+ }
+ }
+
+ *muted = FALSE;
+ return 0;
+}
+
+/* Finds the closest item in db_fix->db_values and returns the corresponding
+ * step. *db_value is replaced with the value from the db_values table.
+ * Rounding is done based on the rounding parameter: -1 means rounding down and
+ * +1 means rounding up. */
+static long decibel_fix_get_step(pa_alsa_decibel_fix *db_fix, long *db_value, int rounding) {
+ unsigned i = 0;
+ unsigned max_i = 0;
+
+ pa_assert(db_fix);
+ pa_assert(db_value);
+ pa_assert(rounding != 0);
+
+ max_i = db_fix->max_step - db_fix->min_step;
+
+ if (rounding > 0) {
+ for (i = 0; i < max_i; i++) {
+ if (db_fix->db_values[i] >= *db_value)
+ break;
+ }
+ } else {
+ for (i = 0; i < max_i; i++) {
+ if (db_fix->db_values[i + 1] > *db_value)
+ break;
+ }
+ }
+
+ *db_value = db_fix->db_values[i];
+
+ return i + db_fix->min_step;
+}
+
+/* Alsa lib documentation says for snd_mixer_selem_set_playback_dB() direction argument,
+ * that "-1 = accurate or first below, 0 = accurate, 1 = accurate or first above".
+ * But even with accurate nearest dB volume step is not selected, so that is why we need
+ * this function. Returns 0 and nearest selectable volume in *value_dB on success or
+ * negative error code if fails. */
+static int element_get_nearest_alsa_dB(snd_mixer_elem_t *me, snd_mixer_selem_channel_id_t c, pa_alsa_direction_t d, long *value_dB) {
+
+ long alsa_val;
+ long value_high;
+ long value_low;
+ int r = -1;
+
+ pa_assert(me);
+ pa_assert(value_dB);
+
+ if (d == PA_ALSA_DIRECTION_OUTPUT) {
+ if ((r = snd_mixer_selem_ask_playback_dB_vol(me, *value_dB, +1, &alsa_val)) >= 0)
+ r = snd_mixer_selem_ask_playback_vol_dB(me, alsa_val, &value_high);
+
+ if (r < 0)
+ return r;
+
+ if (value_high == *value_dB)
+ return r;
+
+ if ((r = snd_mixer_selem_ask_playback_dB_vol(me, *value_dB, -1, &alsa_val)) >= 0)
+ r = snd_mixer_selem_ask_playback_vol_dB(me, alsa_val, &value_low);
+ } else {
+ if ((r = snd_mixer_selem_ask_capture_dB_vol(me, *value_dB, +1, &alsa_val)) >= 0)
+ r = snd_mixer_selem_ask_capture_vol_dB(me, alsa_val, &value_high);
+
+ if (r < 0)
+ return r;
+
+ if (value_high == *value_dB)
+ return r;
+
+ if ((r = snd_mixer_selem_ask_capture_dB_vol(me, *value_dB, -1, &alsa_val)) >= 0)
+ r = snd_mixer_selem_ask_capture_vol_dB(me, alsa_val, &value_low);
+ }
+
+ if (r < 0)
+ return r;
+
+ if (labs(value_high - *value_dB) < labs(value_low - *value_dB))
+ *value_dB = value_high;
+ else
+ *value_dB = value_low;
+
+ return r;
+}
+
+static int element_set_volume(pa_alsa_element *e, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v, pa_bool_t sync_volume, pa_bool_t write_to_hw) {
+
+ snd_mixer_selem_id_t *sid;
+ pa_cvolume rv;
+ snd_mixer_elem_t *me;
+ snd_mixer_selem_channel_id_t c;
+ pa_channel_position_mask_t mask = 0;
+ unsigned k;
+
+ pa_assert(m);
+ pa_assert(e);
+ pa_assert(cm);
+ pa_assert(v);
+ pa_assert(pa_cvolume_compatible_with_channel_map(v, cm));
+
+ SELEM_INIT(sid, e->alsa_name);
+ if (!(me = snd_mixer_find_selem(m, sid))) {
+ pa_log_warn("Element %s seems to have disappeared.", e->alsa_name);
+ return -1;
+ }
+
+ pa_cvolume_mute(&rv, cm->channels);
+
+ for (c = 0; c <= SND_MIXER_SCHN_LAST; c++) {
+ int r;
+ pa_volume_t f = PA_VOLUME_MUTED;
+ pa_bool_t found = FALSE;
+
+ for (k = 0; k < cm->channels; k++)
+ if (e->masks[c][e->n_channels-1] & PA_CHANNEL_POSITION_MASK(cm->map[k])) {
+ found = TRUE;
+ if (v->values[k] > f)
+ f = v->values[k];
+ }
+
+ if (!found) {
+ /* Hmm, so this channel does not exist in the volume
+ * struct, so let's bind it to the overall max of the
+ * volume. */
+ f = pa_cvolume_max(v);
+ }
+
+ if (e->has_dB) {
+ long value = to_alsa_dB(f);
+ int rounding;
+
+ if (e->volume_limit >= 0 && value > (e->max_dB * 100))
+ value = e->max_dB * 100;
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT) {
+ /* If we call set_playback_volume() without checking first
+ * if the channel is available, ALSA behaves very
+ * strangely and doesn't fail the call */
+ if (snd_mixer_selem_has_playback_channel(me, c)) {
+ rounding = +1;
+ if (e->db_fix) {
+ if (write_to_hw)
+ r = snd_mixer_selem_set_playback_volume(me, c, decibel_fix_get_step(e->db_fix, &value, rounding));
+ else {
+ decibel_fix_get_step(e->db_fix, &value, rounding);
+ r = 0;
+ }
+
+ } else {
+ if (write_to_hw) {
+ if (sync_volume) {
+ if ((r = element_get_nearest_alsa_dB(me, c, PA_ALSA_DIRECTION_OUTPUT, &value)) >= 0)
+ r = snd_mixer_selem_set_playback_dB(me, c, value, 0);
+ } else {
+ if ((r = snd_mixer_selem_set_playback_dB(me, c, value, rounding)) >= 0)
+ r = snd_mixer_selem_get_playback_dB(me, c, &value);
+ }
+ } else {
+ long alsa_val;
+ if ((r = snd_mixer_selem_ask_playback_dB_vol(me, value, rounding, &alsa_val)) >= 0)
+ r = snd_mixer_selem_ask_playback_vol_dB(me, alsa_val, &value);
+ }
+ }
+ } else
+ r = -1;
+ } else {
+ if (snd_mixer_selem_has_capture_channel(me, c)) {
+ rounding = -1;
+ if (e->db_fix) {
+ if (write_to_hw)
+ r = snd_mixer_selem_set_capture_volume(me, c, decibel_fix_get_step(e->db_fix, &value, rounding));
+ else {
+ decibel_fix_get_step(e->db_fix, &value, rounding);
+ r = 0;
+ }
+
+ } else {
+ if (write_to_hw) {
+ if (sync_volume) {
+ if ((r = element_get_nearest_alsa_dB(me, c, PA_ALSA_DIRECTION_INPUT, &value)) >= 0)
+ r = snd_mixer_selem_set_capture_dB(me, c, value, 0);
+ } else {
+ if ((r = snd_mixer_selem_set_capture_dB(me, c, value, rounding)) >= 0)
+ r = snd_mixer_selem_get_capture_dB(me, c, &value);
+ }
+ } else {
+ long alsa_val;
+ if ((r = snd_mixer_selem_ask_capture_dB_vol(me, value, rounding, &alsa_val)) >= 0)
+ r = snd_mixer_selem_ask_capture_vol_dB(me, alsa_val, &value);
+ }
+ }
+ } else
+ r = -1;
+ }
+
+ if (r < 0)
+ continue;
+
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+ VALGRIND_MAKE_MEM_DEFINED(&value, sizeof(value));
+#endif
+
+ f = from_alsa_dB(value);
+
+ } else {
+ long value;
+
+ value = to_alsa_volume(f, e->min_volume, e->max_volume);
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT) {
+ if (snd_mixer_selem_has_playback_channel(me, c)) {
+ if ((r = snd_mixer_selem_set_playback_volume(me, c, value)) >= 0)
+ r = snd_mixer_selem_get_playback_volume(me, c, &value);
+ } else
+ r = -1;
+ } else {
+ if (snd_mixer_selem_has_capture_channel(me, c)) {
+ if ((r = snd_mixer_selem_set_capture_volume(me, c, value)) >= 0)
+ r = snd_mixer_selem_get_capture_volume(me, c, &value);
+ } else
+ r = -1;
+ }
+
+ if (r < 0)
+ continue;
+
+ f = from_alsa_volume(value, e->min_volume, e->max_volume);
+ }
+
+ for (k = 0; k < cm->channels; k++)
+ if (e->masks[c][e->n_channels-1] & PA_CHANNEL_POSITION_MASK(cm->map[k]))
+ if (rv.values[k] < f)
+ rv.values[k] = f;
+
+ mask |= e->masks[c][e->n_channels-1];
+ }
+
+ for (k = 0; k < cm->channels; k++)
+ if (!(mask & PA_CHANNEL_POSITION_MASK(cm->map[k])))
+ rv.values[k] = PA_VOLUME_NORM;
+
+ *v = rv;
+ return 0;
+}
+
+int pa_alsa_path_set_volume(pa_alsa_path *p, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v, pa_bool_t sync_volume, pa_bool_t write_to_hw) {
+
+ pa_alsa_element *e;
+ pa_cvolume rv;
+
+ pa_assert(m);
+ pa_assert(p);
+ pa_assert(cm);
+ pa_assert(v);
+ pa_assert(pa_cvolume_compatible_with_channel_map(v, cm));
+
+ if (!p->has_volume)
+ return -1;
+
+ rv = *v; /* Remaining adjustment */
+ pa_cvolume_reset(v, cm->channels); /* Adjustment done */
+
+ PA_LLIST_FOREACH(e, p->elements) {
+ pa_cvolume ev;
+
+ if (e->volume_use != PA_ALSA_VOLUME_MERGE)
+ continue;
+
+ pa_assert(!p->has_dB || e->has_dB);
+
+ ev = rv;
+ if (element_set_volume(e, m, cm, &ev, sync_volume, write_to_hw) < 0)
+ return -1;
+
+ if (!p->has_dB) {
+ *v = ev;
+ return 0;
+ }
+
+ pa_sw_cvolume_multiply(v, v, &ev);
+ pa_sw_cvolume_divide(&rv, &rv, &ev);
+ }
+
+ return 0;
+}
+
+static int element_set_switch(pa_alsa_element *e, snd_mixer_t *m, pa_bool_t b) {
+ snd_mixer_elem_t *me;
+ snd_mixer_selem_id_t *sid;
+ int r;
+
+ pa_assert(m);
+ pa_assert(e);
+
+ SELEM_INIT(sid, e->alsa_name);
+ if (!(me = snd_mixer_find_selem(m, sid))) {
+ pa_log_warn("Element %s seems to have disappeared.", e->alsa_name);
+ return -1;
+ }
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ r = snd_mixer_selem_set_playback_switch_all(me, b);
+ else
+ r = snd_mixer_selem_set_capture_switch_all(me, b);
+
+ if (r < 0)
+ pa_log_warn("Failed to set switch of %s: %s", e->alsa_name, pa_alsa_strerror(errno));
+
+ return r;
+}
+
+int pa_alsa_path_set_mute(pa_alsa_path *p, snd_mixer_t *m, pa_bool_t muted) {
+ pa_alsa_element *e;
+
+ pa_assert(m);
+ pa_assert(p);
+
+ if (!p->has_mute)
+ return -1;
+
+ PA_LLIST_FOREACH(e, p->elements) {
+
+ if (e->switch_use != PA_ALSA_SWITCH_MUTE)
+ continue;
+
+ if (element_set_switch(e, m, !muted) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+/* Depending on whether e->volume_use is _OFF, _ZERO or _CONSTANT, this
+ * function sets all channels of the volume element to e->min_volume, 0 dB or
+ * e->constant_volume. */
+static int element_set_constant_volume(pa_alsa_element *e, snd_mixer_t *m) {
+ snd_mixer_elem_t *me = NULL;
+ snd_mixer_selem_id_t *sid = NULL;
+ int r = 0;
+ long volume = -1;
+ pa_bool_t volume_set = FALSE;
+
+ pa_assert(m);
+ pa_assert(e);
+
+ SELEM_INIT(sid, e->alsa_name);
+ if (!(me = snd_mixer_find_selem(m, sid))) {
+ pa_log_warn("Element %s seems to have disappeared.", e->alsa_name);
+ return -1;
+ }
+
+ switch (e->volume_use) {
+ case PA_ALSA_VOLUME_OFF:
+ volume = e->min_volume;
+ volume_set = TRUE;
+ break;
+
+ case PA_ALSA_VOLUME_ZERO:
+ if (e->db_fix) {
+ long dB = 0;
+
+ volume = decibel_fix_get_step(e->db_fix, &dB, +1);
+ volume_set = TRUE;
+ }
+ break;
+
+ case PA_ALSA_VOLUME_CONSTANT:
+ volume = e->constant_volume;
+ volume_set = TRUE;
+ break;
+
+ default:
+ pa_assert_not_reached();
+ }
+
+ if (volume_set) {
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ r = snd_mixer_selem_set_playback_volume_all(me, volume);
+ else
+ r = snd_mixer_selem_set_capture_volume_all(me, volume);
+ } else {
+ pa_assert(e->volume_use == PA_ALSA_VOLUME_ZERO);
+ pa_assert(!e->db_fix);
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ r = snd_mixer_selem_set_playback_dB_all(me, 0, +1);
+ else
+ r = snd_mixer_selem_set_capture_dB_all(me, 0, +1);
+ }
+
+ if (r < 0)
+ pa_log_warn("Failed to set volume of %s: %s", e->alsa_name, pa_alsa_strerror(errno));
+
+ return r;
+}
+
+int pa_alsa_path_select(pa_alsa_path *p, snd_mixer_t *m) {
+ pa_alsa_element *e;
+ int r = 0;
+
+ pa_assert(m);
+ pa_assert(p);
+
+ pa_log_debug("Activating path %s", p->name);
+ pa_alsa_path_dump(p);
+
+ PA_LLIST_FOREACH(e, p->elements) {
+
+ switch (e->switch_use) {
+ case PA_ALSA_SWITCH_OFF:
+ r = element_set_switch(e, m, FALSE);
+ break;
+
+ case PA_ALSA_SWITCH_ON:
+ r = element_set_switch(e, m, TRUE);
+ break;
+
+ case PA_ALSA_SWITCH_MUTE:
+ case PA_ALSA_SWITCH_IGNORE:
+ case PA_ALSA_SWITCH_SELECT:
+ r = 0;
+ break;
+ }
+
+ if (r < 0)
+ return -1;
+
+ switch (e->volume_use) {
+ case PA_ALSA_VOLUME_OFF:
+ case PA_ALSA_VOLUME_ZERO:
+ case PA_ALSA_VOLUME_CONSTANT:
+ r = element_set_constant_volume(e, m);
+ break;
+
+ case PA_ALSA_VOLUME_MERGE:
+ case PA_ALSA_VOLUME_IGNORE:
+ r = 0;
+ break;
+ }
+
+ if (r < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+static int check_required(pa_alsa_element *e, snd_mixer_elem_t *me) {
+ pa_bool_t has_switch;
+ pa_bool_t has_enumeration;
+ pa_bool_t has_volume;
+
+ pa_assert(e);
+ pa_assert(me);
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT) {
+ has_switch =
+ snd_mixer_selem_has_playback_switch(me) ||
+ (e->direction_try_other && snd_mixer_selem_has_capture_switch(me));
+ } else {
+ has_switch =
+ snd_mixer_selem_has_capture_switch(me) ||
+ (e->direction_try_other && snd_mixer_selem_has_playback_switch(me));
+ }
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT) {
+ has_volume =
+ snd_mixer_selem_has_playback_volume(me) ||
+ (e->direction_try_other && snd_mixer_selem_has_capture_volume(me));
+ } else {
+ has_volume =
+ snd_mixer_selem_has_capture_volume(me) ||
+ (e->direction_try_other && snd_mixer_selem_has_playback_volume(me));
+ }
+
+ has_enumeration = snd_mixer_selem_is_enumerated(me);
+
+ if ((e->required == PA_ALSA_REQUIRED_SWITCH && !has_switch) ||
+ (e->required == PA_ALSA_REQUIRED_VOLUME && !has_volume) ||
+ (e->required == PA_ALSA_REQUIRED_ENUMERATION && !has_enumeration))
+ return -1;
+
+ if (e->required == PA_ALSA_REQUIRED_ANY && !(has_switch || has_volume || has_enumeration))
+ return -1;
+
+ if ((e->required_absent == PA_ALSA_REQUIRED_SWITCH && has_switch) ||
+ (e->required_absent == PA_ALSA_REQUIRED_VOLUME && has_volume) ||
+ (e->required_absent == PA_ALSA_REQUIRED_ENUMERATION && has_enumeration))
+ return -1;
+
+ if (e->required_absent == PA_ALSA_REQUIRED_ANY && (has_switch || has_volume || has_enumeration))
+ return -1;
+
+ if (e->required_any != PA_ALSA_REQUIRED_IGNORE) {
+ switch (e->required_any) {
+ case PA_ALSA_REQUIRED_VOLUME:
+ e->path->req_any_present |= (e->volume_use != PA_ALSA_VOLUME_IGNORE);
+ break;
+ case PA_ALSA_REQUIRED_SWITCH:
+ e->path->req_any_present |= (e->switch_use != PA_ALSA_SWITCH_IGNORE);
+ break;
+ case PA_ALSA_REQUIRED_ENUMERATION:
+ e->path->req_any_present |= (e->enumeration_use != PA_ALSA_ENUMERATION_IGNORE);
+ break;
+ case PA_ALSA_REQUIRED_ANY:
+ e->path->req_any_present |=
+ (e->volume_use != PA_ALSA_VOLUME_IGNORE) ||
+ (e->switch_use != PA_ALSA_SWITCH_IGNORE) ||
+ (e->enumeration_use != PA_ALSA_ENUMERATION_IGNORE);
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+ }
+
+ if (e->enumeration_use == PA_ALSA_ENUMERATION_SELECT) {
+ pa_alsa_option *o;
+ PA_LLIST_FOREACH(o, e->options) {
+ e->path->req_any_present |= (o->required_any != PA_ALSA_REQUIRED_IGNORE) &&
+ (o->alsa_idx >= 0);
+ if (o->required != PA_ALSA_REQUIRED_IGNORE && o->alsa_idx < 0)
+ return -1;
+ if (o->required_absent != PA_ALSA_REQUIRED_IGNORE && o->alsa_idx >= 0)
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int element_probe(pa_alsa_element *e, snd_mixer_t *m) {
+ snd_mixer_selem_id_t *sid;
+ snd_mixer_elem_t *me;
+
+ pa_assert(m);
+ pa_assert(e);
+ pa_assert(e->path);
+
+ SELEM_INIT(sid, e->alsa_name);
+
+ if (!(me = snd_mixer_find_selem(m, sid))) {
+
+ if (e->required != PA_ALSA_REQUIRED_IGNORE)
+ return -1;
+
+ e->switch_use = PA_ALSA_SWITCH_IGNORE;
+ e->volume_use = PA_ALSA_VOLUME_IGNORE;
+ e->enumeration_use = PA_ALSA_ENUMERATION_IGNORE;
+
+ return 0;
+ }
+
+ if (e->switch_use != PA_ALSA_SWITCH_IGNORE) {
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT) {
+
+ if (!snd_mixer_selem_has_playback_switch(me)) {
+ if (e->direction_try_other && snd_mixer_selem_has_capture_switch(me))
+ e->direction = PA_ALSA_DIRECTION_INPUT;
+ else
+ e->switch_use = PA_ALSA_SWITCH_IGNORE;
+ }
+
+ } else {
+
+ if (!snd_mixer_selem_has_capture_switch(me)) {
+ if (e->direction_try_other && snd_mixer_selem_has_playback_switch(me))
+ e->direction = PA_ALSA_DIRECTION_OUTPUT;
+ else
+ e->switch_use = PA_ALSA_SWITCH_IGNORE;
+ }
+ }
+
+ if (e->switch_use != PA_ALSA_SWITCH_IGNORE)
+ e->direction_try_other = FALSE;
+ }
+
+ if (e->volume_use != PA_ALSA_VOLUME_IGNORE) {
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT) {
+
+ if (!snd_mixer_selem_has_playback_volume(me)) {
+ if (e->direction_try_other && snd_mixer_selem_has_capture_volume(me))
+ e->direction = PA_ALSA_DIRECTION_INPUT;
+ else
+ e->volume_use = PA_ALSA_VOLUME_IGNORE;
+ }
+
+ } else {
+
+ if (!snd_mixer_selem_has_capture_volume(me)) {
+ if (e->direction_try_other && snd_mixer_selem_has_playback_volume(me))
+ e->direction = PA_ALSA_DIRECTION_OUTPUT;
+ else
+ e->volume_use = PA_ALSA_VOLUME_IGNORE;
+ }
+ }
+
+ if (e->volume_use != PA_ALSA_VOLUME_IGNORE) {
+ long min_dB = 0, max_dB = 0;
+ int r;
+
+ e->direction_try_other = FALSE;
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ r = snd_mixer_selem_get_playback_volume_range(me, &e->min_volume, &e->max_volume);
+ else
+ r = snd_mixer_selem_get_capture_volume_range(me, &e->min_volume, &e->max_volume);
+
+ if (r < 0) {
+ pa_log_warn("Failed to get volume range of %s: %s", e->alsa_name, pa_alsa_strerror(r));
+ return -1;
+ }
+
+ if (e->min_volume >= e->max_volume) {
+ pa_log_warn("Your kernel driver is broken: it reports a volume range from %li to %li which makes no sense.", e->min_volume, e->max_volume);
+ e->volume_use = PA_ALSA_VOLUME_IGNORE;
+
+ } else if (e->volume_use == PA_ALSA_VOLUME_CONSTANT &&
+ (e->min_volume > e->constant_volume || e->max_volume < e->constant_volume)) {
+ pa_log_warn("Constant volume %li configured for element %s, but the available range is from %li to %li.",
+ e->constant_volume, e->alsa_name, e->min_volume, e->max_volume);
+ e->volume_use = PA_ALSA_VOLUME_IGNORE;
+
+ } else {
+ pa_bool_t is_mono;
+ pa_channel_position_t p;
+
+ if (e->db_fix &&
+ ((e->min_volume > e->db_fix->min_step) ||
+ (e->max_volume < e->db_fix->max_step))) {
+ pa_log_warn("The step range of the decibel fix for element %s (%li-%li) doesn't fit to the "
+ "real hardware range (%li-%li). Disabling the decibel fix.", e->alsa_name,
+ e->db_fix->min_step, e->db_fix->max_step,
+ e->min_volume, e->max_volume);
+
+ decibel_fix_free(e->db_fix);
+ e->db_fix = NULL;
+ }
+
+ if (e->db_fix) {
+ e->has_dB = TRUE;
+ e->min_volume = e->db_fix->min_step;
+ e->max_volume = e->db_fix->max_step;
+ min_dB = e->db_fix->db_values[0];
+ max_dB = e->db_fix->db_values[e->db_fix->max_step - e->db_fix->min_step];
+ } else if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ e->has_dB = snd_mixer_selem_get_playback_dB_range(me, &min_dB, &max_dB) >= 0;
+ else
+ e->has_dB = snd_mixer_selem_get_capture_dB_range(me, &min_dB, &max_dB) >= 0;
+
+ /* Check that the kernel driver returns consistent limits with
+ * both _get_*_dB_range() and _ask_*_vol_dB(). */
+ if (e->has_dB && !e->db_fix) {
+ long min_dB_checked = 0;
+ long max_dB_checked = 0;
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ r = snd_mixer_selem_ask_playback_vol_dB(me, e->min_volume, &min_dB_checked);
+ else
+ r = snd_mixer_selem_ask_capture_vol_dB(me, e->min_volume, &min_dB_checked);
+
+ if (r < 0) {
+ pa_log_warn("Failed to query the dB value for %s at volume level %li", e->alsa_name, e->min_volume);
+ return -1;
+ }
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ r = snd_mixer_selem_ask_playback_vol_dB(me, e->max_volume, &max_dB_checked);
+ else
+ r = snd_mixer_selem_ask_capture_vol_dB(me, e->max_volume, &max_dB_checked);
+
+ if (r < 0) {
+ pa_log_warn("Failed to query the dB value for %s at volume level %li", e->alsa_name, e->max_volume);
+ return -1;
+ }
+
+ if (min_dB != min_dB_checked || max_dB != max_dB_checked) {
+ pa_log_warn("Your kernel driver is broken: the reported dB range for %s (from %0.2f dB to %0.2f dB) "
+ "doesn't match the dB values at minimum and maximum volume levels: %0.2f dB at level %li, "
+ "%0.2f dB at level %li.",
+ e->alsa_name,
+ min_dB / 100.0, max_dB / 100.0,
+ min_dB_checked / 100.0, e->min_volume, max_dB_checked / 100.0, e->max_volume);
+ return -1;
+ }
+ }
+
+ if (e->has_dB) {
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+ VALGRIND_MAKE_MEM_DEFINED(&min_dB, sizeof(min_dB));
+ VALGRIND_MAKE_MEM_DEFINED(&max_dB, sizeof(max_dB));
+#endif
+
+ e->min_dB = ((double) min_dB) / 100.0;
+ e->max_dB = ((double) max_dB) / 100.0;
+
+ if (min_dB >= max_dB) {
+ pa_assert(!e->db_fix);
+ pa_log_warn("Your kernel driver is broken: it reports a volume range from %0.2f dB to %0.2f dB which makes no sense.", e->min_dB, e->max_dB);
+ e->has_dB = FALSE;
+ }
+ }
+
+ if (e->volume_limit >= 0) {
+ if (e->volume_limit <= e->min_volume || e->volume_limit > e->max_volume)
+ pa_log_warn("Volume limit for element %s of path %s is invalid: %li isn't within the valid range "
+ "%li-%li. The volume limit is ignored.",
+ e->alsa_name, e->path->name, e->volume_limit, e->min_volume + 1, e->max_volume);
+
+ else {
+ e->max_volume = e->volume_limit;
+
+ if (e->has_dB) {
+ if (e->db_fix) {
+ e->db_fix->max_step = e->max_volume;
+ e->max_dB = ((double) e->db_fix->db_values[e->db_fix->max_step - e->db_fix->min_step]) / 100.0;
+
+ } else {
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ r = snd_mixer_selem_ask_playback_vol_dB(me, e->max_volume, &max_dB);
+ else
+ r = snd_mixer_selem_ask_capture_vol_dB(me, e->max_volume, &max_dB);
+
+ if (r < 0) {
+ pa_log_warn("Failed to get dB value of %s: %s", e->alsa_name, pa_alsa_strerror(r));
+ e->has_dB = FALSE;
+ } else
+ e->max_dB = ((double) max_dB) / 100.0;
+ }
+ }
+ }
+ }
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ is_mono = snd_mixer_selem_is_playback_mono(me) > 0;
+ else
+ is_mono = snd_mixer_selem_is_capture_mono(me) > 0;
+
+ if (is_mono) {
+ e->n_channels = 1;
+
+ if (!e->override_map) {
+ for (p = PA_CHANNEL_POSITION_FRONT_LEFT; p < PA_CHANNEL_POSITION_MAX; p++) {
+ if (alsa_channel_ids[p] == SND_MIXER_SCHN_UNKNOWN)
+ continue;
+
+ e->masks[alsa_channel_ids[p]][e->n_channels-1] = 0;
+ }
+
+ e->masks[SND_MIXER_SCHN_MONO][e->n_channels-1] = PA_CHANNEL_POSITION_MASK_ALL;
+ }
+
+ e->merged_mask = e->masks[SND_MIXER_SCHN_MONO][e->n_channels-1];
+ } else {
+ e->n_channels = 0;
+ for (p = PA_CHANNEL_POSITION_FRONT_LEFT; p < PA_CHANNEL_POSITION_MAX; p++) {
+
+ if (alsa_channel_ids[p] == SND_MIXER_SCHN_UNKNOWN)
+ continue;
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ e->n_channels += snd_mixer_selem_has_playback_channel(me, alsa_channel_ids[p]) > 0;
+ else
+ e->n_channels += snd_mixer_selem_has_capture_channel(me, alsa_channel_ids[p]) > 0;
+ }
+
+ if (e->n_channels <= 0) {
+ pa_log_warn("Volume element %s with no channels?", e->alsa_name);
+ return -1;
+ }
+
+ if (e->n_channels > 2) {
+ /* FIXME: In some places code like this is used:
+ *
+ * e->masks[alsa_channel_ids[p]][e->n_channels-1]
+ *
+ * The definition of e->masks is
+ *
+ * pa_channel_position_mask_t masks[SND_MIXER_SCHN_LAST][2];
+ *
+ * Since the array size is fixed at 2, we obviously
+ * don't support elements with more than two
+ * channels... */
+ pa_log_warn("Volume element %s has %u channels. That's too much! I can't handle that!", e->alsa_name, e->n_channels);
+ return -1;
+ }
+
+ if (!e->override_map) {
+ for (p = PA_CHANNEL_POSITION_FRONT_LEFT; p < PA_CHANNEL_POSITION_MAX; p++) {
+ pa_bool_t has_channel;
+
+ if (alsa_channel_ids[p] == SND_MIXER_SCHN_UNKNOWN)
+ continue;
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ has_channel = snd_mixer_selem_has_playback_channel(me, alsa_channel_ids[p]) > 0;
+ else
+ has_channel = snd_mixer_selem_has_capture_channel(me, alsa_channel_ids[p]) > 0;
+
+ e->masks[alsa_channel_ids[p]][e->n_channels-1] = has_channel ? PA_CHANNEL_POSITION_MASK(p) : 0;
+ }
+ }
+
+ e->merged_mask = 0;
+ for (p = PA_CHANNEL_POSITION_FRONT_LEFT; p < PA_CHANNEL_POSITION_MAX; p++) {
+ if (alsa_channel_ids[p] == SND_MIXER_SCHN_UNKNOWN)
+ continue;
+
+ e->merged_mask |= e->masks[alsa_channel_ids[p]][e->n_channels-1];
+ }
+ }
+ }
+ }
+
+ }
+
+ if (e->switch_use == PA_ALSA_SWITCH_SELECT) {
+ pa_alsa_option *o;
+
+ PA_LLIST_FOREACH(o, e->options)
+ o->alsa_idx = pa_streq(o->alsa_name, "on") ? 1 : 0;
+ } else if (e->enumeration_use == PA_ALSA_ENUMERATION_SELECT) {
+ int n;
+ pa_alsa_option *o;
+
+ if ((n = snd_mixer_selem_get_enum_items(me)) < 0) {
+ pa_log("snd_mixer_selem_get_enum_items() failed: %s", pa_alsa_strerror(n));
+ return -1;
+ }
+
+ PA_LLIST_FOREACH(o, e->options) {
+ int i;
+
+ for (i = 0; i < n; i++) {
+ char buf[128];
+
+ if (snd_mixer_selem_get_enum_item_name(me, i, sizeof(buf), buf) < 0)
+ continue;
+
+ if (!pa_streq(buf, o->alsa_name))
+ continue;
+
+ o->alsa_idx = i;
+ }
+ }
+ }
+
+ if (check_required(e, me) < 0)
+ return -1;
+
+ return 0;
+}
+
+static pa_alsa_element* element_get(pa_alsa_path *p, const char *section, pa_bool_t prefixed) {
+ pa_alsa_element *e;
+
+ pa_assert(p);
+ pa_assert(section);
+
+ if (prefixed) {
+ if (!pa_startswith(section, "Element "))
+ return NULL;
+
+ section += 8;
+ }
+
+ /* This is not an element section, but an enum section? */
+ if (strchr(section, ':'))
+ return NULL;
+
+ if (p->last_element && pa_streq(p->last_element->alsa_name, section))
+ return p->last_element;
+
+ PA_LLIST_FOREACH(e, p->elements)
+ if (pa_streq(e->alsa_name, section))
+ goto finish;
+
+ e = pa_xnew0(pa_alsa_element, 1);
+ e->path = p;
+ e->alsa_name = pa_xstrdup(section);
+ e->direction = p->direction;
+ e->volume_limit = -1;
+
+ PA_LLIST_INSERT_AFTER(pa_alsa_element, p->elements, p->last_element, e);
+
+finish:
+ p->last_element = e;
+ return e;
+}
+
+static pa_alsa_option* option_get(pa_alsa_path *p, const char *section) {
+ char *en;
+ const char *on;
+ pa_alsa_option *o;
+ pa_alsa_element *e;
+
+ if (!pa_startswith(section, "Option "))
+ return NULL;
+
+ section += 7;
+
+ /* This is not an enum section, but an element section? */
+ if (!(on = strchr(section, ':')))
+ return NULL;
+
+ en = pa_xstrndup(section, on - section);
+ on++;
+
+ if (p->last_option &&
+ pa_streq(p->last_option->element->alsa_name, en) &&
+ pa_streq(p->last_option->alsa_name, on)) {
+ pa_xfree(en);
+ return p->last_option;
+ }
+
+ pa_assert_se(e = element_get(p, en, FALSE));
+ pa_xfree(en);
+
+ PA_LLIST_FOREACH(o, e->options)
+ if (pa_streq(o->alsa_name, on))
+ goto finish;
+
+ o = pa_xnew0(pa_alsa_option, 1);
+ o->element = e;
+ o->alsa_name = pa_xstrdup(on);
+ o->alsa_idx = -1;
+
+ if (p->last_option && p->last_option->element == e)
+ PA_LLIST_INSERT_AFTER(pa_alsa_option, e->options, p->last_option, o);
+ else
+ PA_LLIST_PREPEND(pa_alsa_option, e->options, o);
+
+finish:
+ p->last_option = o;
+ return o;
+}
+
+static int element_parse_switch(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_element *e;
+
+ pa_assert(p);
+
+ if (!(e = element_get(p, section, TRUE))) {
+ pa_log("[%s:%u] Switch makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ if (pa_streq(rvalue, "ignore"))
+ e->switch_use = PA_ALSA_SWITCH_IGNORE;
+ else if (pa_streq(rvalue, "mute"))
+ e->switch_use = PA_ALSA_SWITCH_MUTE;
+ else if (pa_streq(rvalue, "off"))
+ e->switch_use = PA_ALSA_SWITCH_OFF;
+ else if (pa_streq(rvalue, "on"))
+ e->switch_use = PA_ALSA_SWITCH_ON;
+ else if (pa_streq(rvalue, "select"))
+ e->switch_use = PA_ALSA_SWITCH_SELECT;
+ else {
+ pa_log("[%s:%u] Switch invalid of '%s'", filename, line, section);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int element_parse_volume(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_element *e;
+
+ pa_assert(p);
+
+ if (!(e = element_get(p, section, TRUE))) {
+ pa_log("[%s:%u] Volume makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ if (pa_streq(rvalue, "ignore"))
+ e->volume_use = PA_ALSA_VOLUME_IGNORE;
+ else if (pa_streq(rvalue, "merge"))
+ e->volume_use = PA_ALSA_VOLUME_MERGE;
+ else if (pa_streq(rvalue, "off"))
+ e->volume_use = PA_ALSA_VOLUME_OFF;
+ else if (pa_streq(rvalue, "zero"))
+ e->volume_use = PA_ALSA_VOLUME_ZERO;
+ else {
+ uint32_t constant;
+
+ if (pa_atou(rvalue, &constant) >= 0) {
+ e->volume_use = PA_ALSA_VOLUME_CONSTANT;
+ e->constant_volume = constant;
+ } else {
+ pa_log("[%s:%u] Volume invalid of '%s'", filename, line, section);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int element_parse_enumeration(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_element *e;
+
+ pa_assert(p);
+
+ if (!(e = element_get(p, section, TRUE))) {
+ pa_log("[%s:%u] Enumeration makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ if (pa_streq(rvalue, "ignore"))
+ e->enumeration_use = PA_ALSA_ENUMERATION_IGNORE;
+ else if (pa_streq(rvalue, "select"))
+ e->enumeration_use = PA_ALSA_ENUMERATION_SELECT;
+ else {
+ pa_log("[%s:%u] Enumeration invalid of '%s'", filename, line, section);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int option_parse_priority(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_option *o;
+ uint32_t prio;
+
+ pa_assert(p);
+
+ if (!(o = option_get(p, section))) {
+ pa_log("[%s:%u] Priority makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ if (pa_atou(rvalue, &prio) < 0) {
+ pa_log("[%s:%u] Priority invalid of '%s'", filename, line, section);
+ return -1;
+ }
+
+ o->priority = prio;
+ return 0;
+}
+
+static int option_parse_name(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_option *o;
+
+ pa_assert(p);
+
+ if (!(o = option_get(p, section))) {
+ pa_log("[%s:%u] Name makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ pa_xfree(o->name);
+ o->name = pa_xstrdup(rvalue);
+
+ return 0;
+}
+
+static int element_parse_required(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_element *e;
+ pa_alsa_option *o;
+ pa_alsa_required_t req;
+
+ pa_assert(p);
+
+ e = element_get(p, section, TRUE);
+ o = option_get(p, section);
+ if (!e && !o) {
+ pa_log("[%s:%u] Required makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ if (pa_streq(rvalue, "ignore"))
+ req = PA_ALSA_REQUIRED_IGNORE;
+ else if (pa_streq(rvalue, "switch") && e)
+ req = PA_ALSA_REQUIRED_SWITCH;
+ else if (pa_streq(rvalue, "volume") && e)
+ req = PA_ALSA_REQUIRED_VOLUME;
+ else if (pa_streq(rvalue, "enumeration"))
+ req = PA_ALSA_REQUIRED_ENUMERATION;
+ else if (pa_streq(rvalue, "any"))
+ req = PA_ALSA_REQUIRED_ANY;
+ else {
+ pa_log("[%s:%u] Required invalid of '%s'", filename, line, section);
+ return -1;
+ }
+
+ if (pa_streq(lvalue, "required-absent")) {
+ if (e)
+ e->required_absent = req;
+ if (o)
+ o->required_absent = req;
+ }
+ else if (pa_streq(lvalue, "required-any")) {
+ if (e) {
+ e->required_any = req;
+ e->path->has_req_any = TRUE;
+ }
+ if (o) {
+ o->required_any = req;
+ o->element->path->has_req_any = TRUE;
+ }
+ }
+ else {
+ if (e)
+ e->required = req;
+ if (o)
+ o->required = req;
+ }
+
+ return 0;
+}
+
+static int element_parse_direction(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_element *e;
+
+ pa_assert(p);
+
+ if (!(e = element_get(p, section, TRUE))) {
+ pa_log("[%s:%u] Direction makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ if (pa_streq(rvalue, "playback"))
+ e->direction = PA_ALSA_DIRECTION_OUTPUT;
+ else if (pa_streq(rvalue, "capture"))
+ e->direction = PA_ALSA_DIRECTION_INPUT;
+ else {
+ pa_log("[%s:%u] Direction invalid of '%s'", filename, line, section);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int element_parse_direction_try_other(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_element *e;
+ int yes;
+
+ if (!(e = element_get(p, section, TRUE))) {
+ pa_log("[%s:%u] Direction makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ if ((yes = pa_parse_boolean(rvalue)) < 0) {
+ pa_log("[%s:%u] Direction invalid of '%s'", filename, line, section);
+ return -1;
+ }
+
+ e->direction_try_other = !!yes;
+ return 0;
+}
+
+static int element_parse_volume_limit(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_element *e;
+ long volume_limit;
+
+ if (!(e = element_get(p, section, TRUE))) {
+ pa_log("[%s:%u] volume-limit makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ if (pa_atol(rvalue, &volume_limit) < 0 || volume_limit < 0) {
+ pa_log("[%s:%u] Invalid value for volume-limit", filename, line);
+ return -1;
+ }
+
+ e->volume_limit = volume_limit;
+ return 0;
+}
+
+static pa_channel_position_mask_t parse_mask(const char *m) {
+ pa_channel_position_mask_t v;
+
+ if (pa_streq(m, "all-left"))
+ v = PA_CHANNEL_POSITION_MASK_LEFT;
+ else if (pa_streq(m, "all-right"))
+ v = PA_CHANNEL_POSITION_MASK_RIGHT;
+ else if (pa_streq(m, "all-center"))
+ v = PA_CHANNEL_POSITION_MASK_CENTER;
+ else if (pa_streq(m, "all-front"))
+ v = PA_CHANNEL_POSITION_MASK_FRONT;
+ else if (pa_streq(m, "all-rear"))
+ v = PA_CHANNEL_POSITION_MASK_REAR;
+ else if (pa_streq(m, "all-side"))
+ v = PA_CHANNEL_POSITION_MASK_SIDE_OR_TOP_CENTER;
+ else if (pa_streq(m, "all-top"))
+ v = PA_CHANNEL_POSITION_MASK_TOP;
+ else if (pa_streq(m, "all-no-lfe"))
+ v = PA_CHANNEL_POSITION_MASK_ALL ^ PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_LFE);
+ else if (pa_streq(m, "all"))
+ v = PA_CHANNEL_POSITION_MASK_ALL;
+ else {
+ pa_channel_position_t p;
+
+ if ((p = pa_channel_position_from_string(m)) == PA_CHANNEL_POSITION_INVALID)
+ return 0;
+
+ v = PA_CHANNEL_POSITION_MASK(p);
+ }
+
+ return v;
+}
+
+static int element_parse_override_map(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_path *p = userdata;
+ pa_alsa_element *e;
+ const char *state = NULL;
+ unsigned i = 0;
+ char *n;
+
+ if (!(e = element_get(p, section, TRUE))) {
+ pa_log("[%s:%u] Override map makes no sense in '%s'", filename, line, section);
+ return -1;
+ }
+
+ while ((n = pa_split(rvalue, ",", &state))) {
+ pa_channel_position_mask_t m;
+
+ if (!*n)
+ m = 0;
+ else {
+ if ((m = parse_mask(n)) == 0) {
+ pa_log("[%s:%u] Override map '%s' invalid in '%s'", filename, line, n, section);
+ pa_xfree(n);
+ return -1;
+ }
+ }
+
+ if (pa_streq(lvalue, "override-map.1"))
+ e->masks[i++][0] = m;
+ else
+ e->masks[i++][1] = m;
+
+ /* Later on we might add override-map.3 and so on here ... */
+
+ pa_xfree(n);
+ }
+
+ e->override_map = TRUE;
+
+ return 0;
+}
+
+static int element_set_option(pa_alsa_element *e, snd_mixer_t *m, int alsa_idx) {
+ snd_mixer_selem_id_t *sid;
+ snd_mixer_elem_t *me;
+ int r;
+
+ pa_assert(e);
+ pa_assert(m);
+
+ SELEM_INIT(sid, e->alsa_name);
+ if (!(me = snd_mixer_find_selem(m, sid))) {
+ pa_log_warn("Element %s seems to have disappeared.", e->alsa_name);
+ return -1;
+ }
+
+ if (e->switch_use == PA_ALSA_SWITCH_SELECT) {
+
+ if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+ r = snd_mixer_selem_set_playback_switch_all(me, alsa_idx);
+ else
+ r = snd_mixer_selem_set_capture_switch_all(me, alsa_idx);
+
+ if (r < 0)
+ pa_log_warn("Failed to set switch of %s: %s", e->alsa_name, pa_alsa_strerror(errno));
+
+ } else {
+ pa_assert(e->enumeration_use == PA_ALSA_ENUMERATION_SELECT);
+
+ if ((r = snd_mixer_selem_set_enum_item(me, 0, alsa_idx)) < 0)
+ pa_log_warn("Failed to set enumeration of %s: %s", e->alsa_name, pa_alsa_strerror(errno));
+ }
+
+ return r;
+}
+
+int pa_alsa_setting_select(pa_alsa_setting *s, snd_mixer_t *m) {
+ pa_alsa_option *o;
+ uint32_t idx;
+
+ pa_assert(s);
+ pa_assert(m);
+
+ PA_IDXSET_FOREACH(o, s->options, idx)
+ element_set_option(o->element, m, o->alsa_idx);
+
+ return 0;
+}
+
+static int option_verify(pa_alsa_option *o) {
+ static const struct description_map well_known_descriptions[] = {
+ { "input", N_("Input") },
+ { "input-docking", N_("Docking Station Input") },
+ { "input-docking-microphone", N_("Docking Station Microphone") },
+ { "input-docking-linein", N_("Docking Station Line-In") },
+ { "input-linein", N_("Line-In") },
+ { "input-microphone", N_("Microphone") },
+ { "input-microphone-front", N_("Front Microphone") },
+ { "input-microphone-rear", N_("Rear Microphone") },
+ { "input-microphone-external", N_("External Microphone") },
+ { "input-microphone-internal", N_("Internal Microphone") },
+ { "input-radio", N_("Radio") },
+ { "input-video", N_("Video") },
+ { "input-agc-on", N_("Automatic Gain Control") },
+ { "input-agc-off", N_("No Automatic Gain Control") },
+ { "input-boost-on", N_("Boost") },
+ { "input-boost-off", N_("No Boost") },
+ { "output-amplifier-on", N_("Amplifier") },
+ { "output-amplifier-off", N_("No Amplifier") },
+ { "output-bass-boost-on", N_("Bass Boost") },
+ { "output-bass-boost-off", N_("No Bass Boost") },
+ { "output-speaker", N_("Speaker") },
+ { "output-headphones", N_("Headphones") }
+ };
+
+ pa_assert(o);
+
+ if (!o->name) {
+ pa_log("No name set for option %s", o->alsa_name);
+ return -1;
+ }
+
+ if (o->element->enumeration_use != PA_ALSA_ENUMERATION_SELECT &&
+ o->element->switch_use != PA_ALSA_SWITCH_SELECT) {
+ pa_log("Element %s of option %s not set for select.", o->element->alsa_name, o->name);
+ return -1;
+ }
+
+ if (o->element->switch_use == PA_ALSA_SWITCH_SELECT &&
+ !pa_streq(o->alsa_name, "on") &&
+ !pa_streq(o->alsa_name, "off")) {
+ pa_log("Switch %s options need be named off or on ", o->element->alsa_name);
+ return -1;
+ }
+
+ if (!o->description)
+ o->description = pa_xstrdup(lookup_description(o->name,
+ well_known_descriptions,
+ PA_ELEMENTSOF(well_known_descriptions)));
+ if (!o->description)
+ o->description = pa_xstrdup(o->name);
+
+ return 0;
+}
+
+static int element_verify(pa_alsa_element *e) {
+ pa_alsa_option *o;
+
+ pa_assert(e);
+
+// pa_log_debug("Element %s, path %s: r=%d, r-any=%d, r-abs=%d", e->alsa_name, e->path->name, e->required, e->required_any, e->required_absent);
+ if ((e->required != PA_ALSA_REQUIRED_IGNORE && e->required == e->required_absent) ||
+ (e->required_any != PA_ALSA_REQUIRED_IGNORE && e->required_any == e->required_absent) ||
+ (e->required_absent == PA_ALSA_REQUIRED_ANY && e->required_any != PA_ALSA_REQUIRED_IGNORE) ||
+ (e->required_absent == PA_ALSA_REQUIRED_ANY && e->required != PA_ALSA_REQUIRED_IGNORE)) {
+ pa_log("Element %s cannot be required and absent at the same time.", e->alsa_name);
+ return -1;
+ }
+
+ if (e->switch_use == PA_ALSA_SWITCH_SELECT && e->enumeration_use == PA_ALSA_ENUMERATION_SELECT) {
+ pa_log("Element %s cannot set select for both switch and enumeration.", e->alsa_name);
+ return -1;
+ }
+
+ PA_LLIST_FOREACH(o, e->options)
+ if (option_verify(o) < 0)
+ return -1;
+
+ return 0;
+}
+
+static int path_verify(pa_alsa_path *p) {
+ static const struct description_map well_known_descriptions[] = {
+ { "analog-input", N_("Analog Input") },
+ { "analog-input-microphone", N_("Analog Microphone") },
+ { "analog-input-microphone-front", N_("Front Microphone") },
+ { "analog-input-microphone-rear", N_("Rear Microphone") },
+ { "analog-input-microphone-dock", N_("Docking Station Microphone") },
+ { "analog-input-microphone-internal", N_("Internal Microphone") },
+ { "analog-input-linein", N_("Analog Line-In") },
+ { "analog-input-radio", N_("Analog Radio") },
+ { "analog-input-video", N_("Analog Video") },
+ { "analog-output", N_("Analog Output") },
+ { "analog-output-headphones", N_("Analog Headphones") },
+ { "analog-output-lfe-on-mono", N_("Analog Output (LFE)") },
+ { "analog-output-mono", N_("Analog Mono Output") },
+ { "analog-output-speaker", N_("Analog Speakers") },
+ { "iec958-stereo-output", N_("Digital Output (IEC958)") },
+ { "iec958-passthrough-output", N_("Digital Passthrough (IEC958)") }
+ };
+
+ pa_alsa_element *e;
+
+ pa_assert(p);
+
+ PA_LLIST_FOREACH(e, p->elements)
+ if (element_verify(e) < 0)
+ return -1;
+
+ if (!p->description)
+ p->description = pa_xstrdup(lookup_description(p->name,
+ well_known_descriptions,
+ PA_ELEMENTSOF(well_known_descriptions)));
+
+ if (!p->description)
+ p->description = pa_xstrdup(p->name);
+
+ return 0;
+}
+
+pa_alsa_path* pa_alsa_path_new(const char *fname, pa_alsa_direction_t direction) {
+ pa_alsa_path *p;
+ char *fn;
+ int r;
+ const char *n;
+
+ pa_config_item items[] = {
+ /* [General] */
+ { "priority", pa_config_parse_unsigned, NULL, "General" },
+ { "description", pa_config_parse_string, NULL, "General" },
+ { "name", pa_config_parse_string, NULL, "General" },
+
+ /* [Option ...] */
+ { "priority", option_parse_priority, NULL, NULL },
+ { "name", option_parse_name, NULL, NULL },
+
+ /* [Element ...] */
+ { "switch", element_parse_switch, NULL, NULL },
+ { "volume", element_parse_volume, NULL, NULL },
+ { "enumeration", element_parse_enumeration, NULL, NULL },
+ { "override-map.1", element_parse_override_map, NULL, NULL },
+ { "override-map.2", element_parse_override_map, NULL, NULL },
+ /* ... later on we might add override-map.3 and so on here ... */
+ { "required", element_parse_required, NULL, NULL },
+ { "required-any", element_parse_required, NULL, NULL },
+ { "required-absent", element_parse_required, NULL, NULL },
+ { "direction", element_parse_direction, NULL, NULL },
+ { "direction-try-other", element_parse_direction_try_other, NULL, NULL },
+ { "volume-limit", element_parse_volume_limit, NULL, NULL },
+ { NULL, NULL, NULL, NULL }
+ };
+
+ pa_assert(fname);
+
+ p = pa_xnew0(pa_alsa_path, 1);
+ n = pa_path_get_filename(fname);
+ p->name = pa_xstrndup(n, strcspn(n, "."));
+ p->direction = direction;
+
+ items[0].data = &p->priority;
+ items[1].data = &p->description;
+ items[2].data = &p->name;
+
+ fn = pa_maybe_prefix_path(fname,
+ pa_run_from_build_tree() ? PA_BUILDDIR "/modules/alsa/mixer/paths/" :
+ PA_ALSA_PATHS_DIR);
+
+ r = pa_config_parse(fn, NULL, items, p);
+ pa_xfree(fn);
+
+ if (r < 0)
+ goto fail;
+
+ if (path_verify(p) < 0)
+ goto fail;
+
+ return p;
+
+fail:
+ pa_alsa_path_free(p);
+ return NULL;
+}
+
+pa_alsa_path *pa_alsa_path_synthesize(const char *element, pa_alsa_direction_t direction) {
+ pa_alsa_path *p;
+ pa_alsa_element *e;
+
+ pa_assert(element);
+
+ p = pa_xnew0(pa_alsa_path, 1);
+ p->name = pa_xstrdup(element);
+ p->direction = direction;
+
+ e = pa_xnew0(pa_alsa_element, 1);
+ e->path = p;
+ e->alsa_name = pa_xstrdup(element);
+ e->direction = direction;
+ e->volume_limit = -1;
+
+ e->switch_use = PA_ALSA_SWITCH_MUTE;
+ e->volume_use = PA_ALSA_VOLUME_MERGE;
+
+ PA_LLIST_PREPEND(pa_alsa_element, p->elements, e);
+ p->last_element = e;
+ return p;
+}
+
+static pa_bool_t element_drop_unsupported(pa_alsa_element *e) {
+ pa_alsa_option *o, *n;
+
+ pa_assert(e);
+
+ for (o = e->options; o; o = n) {
+ n = o->next;
+
+ if (o->alsa_idx < 0) {
+ PA_LLIST_REMOVE(pa_alsa_option, e->options, o);
+ option_free(o);
+ }
+ }
+
+ return
+ e->switch_use != PA_ALSA_SWITCH_IGNORE ||
+ e->volume_use != PA_ALSA_VOLUME_IGNORE ||
+ e->enumeration_use != PA_ALSA_ENUMERATION_IGNORE;
+}
+
+static void path_drop_unsupported(pa_alsa_path *p) {
+ pa_alsa_element *e, *n;
+
+ pa_assert(p);
+
+ for (e = p->elements; e; e = n) {
+ n = e->next;
+
+ if (!element_drop_unsupported(e)) {
+ PA_LLIST_REMOVE(pa_alsa_element, p->elements, e);
+ element_free(e);
+ }
+ }
+}
+
+static void path_make_options_unique(pa_alsa_path *p) {
+ pa_alsa_element *e;
+ pa_alsa_option *o, *u;
+
+ PA_LLIST_FOREACH(e, p->elements) {
+ PA_LLIST_FOREACH(o, e->options) {
+ unsigned i;
+ char *m;
+
+ for (u = o->next; u; u = u->next)
+ if (pa_streq(u->name, o->name))
+ break;
+
+ if (!u)
+ continue;
+
+ m = pa_xstrdup(o->name);
+
+ /* OK, this name is not unique, hence let's rename */
+ for (i = 1, u = o; u; u = u->next) {
+ char *nn, *nd;
+
+ if (!pa_streq(u->name, m))
+ continue;
+
+ nn = pa_sprintf_malloc("%s-%u", m, i);
+ pa_xfree(u->name);
+ u->name = nn;
+
+ nd = pa_sprintf_malloc("%s %u", u->description, i);
+ pa_xfree(u->description);
+ u->description = nd;
+
+ i++;
+ }
+
+ pa_xfree(m);
+ }
+ }
+}
+
+static pa_bool_t element_create_settings(pa_alsa_element *e, pa_alsa_setting *template) {
+ pa_alsa_option *o;
+
+ for (; e; e = e->next)
+ if (e->switch_use == PA_ALSA_SWITCH_SELECT ||
+ e->enumeration_use == PA_ALSA_ENUMERATION_SELECT)
+ break;
+
+ if (!e)
+ return FALSE;
+
+ for (o = e->options; o; o = o->next) {
+ pa_alsa_setting *s;
+
+ if (template) {
+ s = pa_xnewdup(pa_alsa_setting, template, 1);
+ s->options = pa_idxset_copy(template->options);
+ s->name = pa_sprintf_malloc(_("%s+%s"), template->name, o->name);
+ s->description =
+ (template->description[0] && o->description[0])
+ ? pa_sprintf_malloc(_("%s / %s"), template->description, o->description)
+ : (template->description[0]
+ ? pa_xstrdup(template->description)
+ : pa_xstrdup(o->description));
+
+ s->priority = PA_MAX(template->priority, o->priority);
+ } else {
+ s = pa_xnew0(pa_alsa_setting, 1);
+ s->options = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ s->name = pa_xstrdup(o->name);
+ s->description = pa_xstrdup(o->description);
+ s->priority = o->priority;
+ }
+
+ pa_idxset_put(s->options, o, NULL);
+
+ if (element_create_settings(e->next, s))
+ /* This is not a leaf, so let's get rid of it */
+ setting_free(s);
+ else {
+ /* This is a leaf, so let's add it */
+ PA_LLIST_INSERT_AFTER(pa_alsa_setting, e->path->settings, e->path->last_setting, s);
+
+ e->path->last_setting = s;
+ }
+ }
+
+ return TRUE;
+}
+
+static void path_create_settings(pa_alsa_path *p) {
+ pa_assert(p);
+
+ element_create_settings(p->elements, NULL);
+}
+
+int pa_alsa_path_probe(pa_alsa_path *p, snd_mixer_t *m, pa_bool_t ignore_dB) {
+ pa_alsa_element *e;
+ double min_dB[PA_CHANNEL_POSITION_MAX], max_dB[PA_CHANNEL_POSITION_MAX];
+ pa_channel_position_t t;
+ pa_channel_position_mask_t path_volume_channels = 0;
+
+ pa_assert(p);
+ pa_assert(m);
+
+ if (p->probed)
+ return 0;
+
+ pa_zero(min_dB);
+ pa_zero(max_dB);
+
+ pa_log_debug("Probing path '%s'", p->name);
+
+ PA_LLIST_FOREACH(e, p->elements) {
+ if (element_probe(e, m) < 0) {
+ p->supported = FALSE;
+ pa_log_debug("Probe of element '%s' failed.", e->alsa_name);
+ return -1;
+ }
+ pa_log_debug("Probe of element '%s' succeeded (volume=%d, switch=%d, enumeration=%d).", e->alsa_name, e->volume_use, e->switch_use, e->enumeration_use);
+
+ if (ignore_dB)
+ e->has_dB = FALSE;
+
+ if (e->volume_use == PA_ALSA_VOLUME_MERGE) {
+
+ if (!p->has_volume) {
+ p->min_volume = e->min_volume;
+ p->max_volume = e->max_volume;
+ }
+
+ if (e->has_dB) {
+ if (!p->has_volume) {
+ for (t = 0; t < PA_CHANNEL_POSITION_MAX; t++)
+ if (PA_CHANNEL_POSITION_MASK(t) & e->merged_mask) {
+ min_dB[t] = e->min_dB;
+ max_dB[t] = e->max_dB;
+ path_volume_channels |= PA_CHANNEL_POSITION_MASK(t);
+ }
+
+ p->has_dB = TRUE;
+ } else {
+
+ if (p->has_dB) {
+ for (t = 0; t < PA_CHANNEL_POSITION_MAX; t++)
+ if (PA_CHANNEL_POSITION_MASK(t) & e->merged_mask) {
+ min_dB[t] += e->min_dB;
+ max_dB[t] += e->max_dB;
+ path_volume_channels |= PA_CHANNEL_POSITION_MASK(t);
+ }
+ } else {
+ /* Hmm, there's another element before us
+ * which cannot do dB volumes, so we we need
+ * to 'neutralize' this slider */
+ e->volume_use = PA_ALSA_VOLUME_ZERO;
+ pa_log_info("Zeroing volume of '%s' on path '%s'", e->alsa_name, p->name);
+ }
+ }
+ } else if (p->has_volume) {
+ /* We can't use this volume, so let's ignore it */
+ e->volume_use = PA_ALSA_VOLUME_IGNORE;
+ pa_log_info("Ignoring volume of '%s' on path '%s' (missing dB info)", e->alsa_name, p->name);
+ }
+ p->has_volume = TRUE;
+ }
+
+ if (e->switch_use == PA_ALSA_SWITCH_MUTE)
+ p->has_mute = TRUE;
+ }
+
+ if (p->has_req_any && !p->req_any_present) {
+ p->supported = FALSE;
+ pa_log_debug("Skipping path '%s', none of required-any elements preset.", p->name);
+ return -1;
+ }
+
+ path_drop_unsupported(p);
+ path_make_options_unique(p);
+ path_create_settings(p);
+
+ p->supported = TRUE;
+ p->probed = TRUE;
+
+ p->min_dB = INFINITY;
+ p->max_dB = -INFINITY;
+
+ for (t = 0; t < PA_CHANNEL_POSITION_MAX; t++) {
+ if (path_volume_channels & PA_CHANNEL_POSITION_MASK(t)) {
+ if (p->min_dB > min_dB[t])
+ p->min_dB = min_dB[t];
+
+ if (p->max_dB < max_dB[t])
+ p->max_dB = max_dB[t];
+ }
+ }
+
+ return 0;
+}
+
+void pa_alsa_setting_dump(pa_alsa_setting *s) {
+ pa_assert(s);
+
+ pa_log_debug("Setting %s (%s) priority=%u",
+ s->name,
+ pa_strnull(s->description),
+ s->priority);
+}
+
+void pa_alsa_option_dump(pa_alsa_option *o) {
+ pa_assert(o);
+
+ pa_log_debug("Option %s (%s/%s) index=%i, priority=%u",
+ o->alsa_name,
+ pa_strnull(o->name),
+ pa_strnull(o->description),
+ o->alsa_idx,
+ o->priority);
+}
+
+void pa_alsa_element_dump(pa_alsa_element *e) {
+ pa_alsa_option *o;
+ pa_assert(e);
+
+ pa_log_debug("Element %s, direction=%i, switch=%i, volume=%i, volume_limit=%li, enumeration=%i, required=%i, required_any=%i, required_absent=%i, mask=0x%llx, n_channels=%u, override_map=%s",
+ e->alsa_name,
+ e->direction,
+ e->switch_use,
+ e->volume_use,
+ e->volume_limit,
+ e->enumeration_use,
+ e->required,
+ e->required_any,
+ e->required_absent,
+ (long long unsigned) e->merged_mask,
+ e->n_channels,
+ pa_yes_no(e->override_map));
+
+ PA_LLIST_FOREACH(o, e->options)
+ pa_alsa_option_dump(o);
+}
+
+void pa_alsa_path_dump(pa_alsa_path *p) {
+ pa_alsa_element *e;
+ pa_alsa_setting *s;
+ pa_assert(p);
+
+ pa_log_debug("Path %s (%s), direction=%i, priority=%u, probed=%s, supported=%s, has_mute=%s, has_volume=%s, "
+ "has_dB=%s, min_volume=%li, max_volume=%li, min_dB=%g, max_dB=%g",
+ p->name,
+ pa_strnull(p->description),
+ p->direction,
+ p->priority,
+ pa_yes_no(p->probed),
+ pa_yes_no(p->supported),
+ pa_yes_no(p->has_mute),
+ pa_yes_no(p->has_volume),
+ pa_yes_no(p->has_dB),
+ p->min_volume, p->max_volume,
+ p->min_dB, p->max_dB);
+
+ PA_LLIST_FOREACH(e, p->elements)
+ pa_alsa_element_dump(e);
+
+ PA_LLIST_FOREACH(s, p->settings)
+ pa_alsa_setting_dump(s);
+}
+
+static void element_set_callback(pa_alsa_element *e, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata) {
+ snd_mixer_selem_id_t *sid;
+ snd_mixer_elem_t *me;
+
+ pa_assert(e);
+ pa_assert(m);
+ pa_assert(cb);
+
+ SELEM_INIT(sid, e->alsa_name);
+ if (!(me = snd_mixer_find_selem(m, sid))) {
+ pa_log_warn("Element %s seems to have disappeared.", e->alsa_name);
+ return;
+ }
+
+ snd_mixer_elem_set_callback(me, cb);
+ snd_mixer_elem_set_callback_private(me, userdata);
+}
+
+void pa_alsa_path_set_callback(pa_alsa_path *p, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata) {
+ pa_alsa_element *e;
+
+ pa_assert(p);
+ pa_assert(m);
+ pa_assert(cb);
+
+ PA_LLIST_FOREACH(e, p->elements)
+ element_set_callback(e, m, cb, userdata);
+}
+
+void pa_alsa_path_set_set_callback(pa_alsa_path_set *ps, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata) {
+ pa_alsa_path *p;
+
+ pa_assert(ps);
+ pa_assert(m);
+ pa_assert(cb);
+
+ PA_LLIST_FOREACH(p, ps->paths)
+ pa_alsa_path_set_callback(p, m, cb, userdata);
+}
+
+pa_alsa_path_set *pa_alsa_path_set_new(pa_alsa_mapping *m, pa_alsa_direction_t direction) {
+ pa_alsa_path_set *ps;
+ char **pn = NULL, **en = NULL, **ie;
+ pa_alsa_decibel_fix *db_fix;
+ void *state;
+
+ pa_assert(m);
+ pa_assert(m->profile_set);
+ pa_assert(m->profile_set->decibel_fixes);
+ pa_assert(direction == PA_ALSA_DIRECTION_OUTPUT || direction == PA_ALSA_DIRECTION_INPUT);
+
+ if (m->direction != PA_ALSA_DIRECTION_ANY && m->direction != direction)
+ return NULL;
+
+ ps = pa_xnew0(pa_alsa_path_set, 1);
+ ps->direction = direction;
+
+ if (direction == PA_ALSA_DIRECTION_OUTPUT)
+ pn = m->output_path_names;
+ else if (direction == PA_ALSA_DIRECTION_INPUT)
+ pn = m->input_path_names;
+
+ if (pn) {
+ char **in;
+
+ for (in = pn; *in; in++) {
+ pa_alsa_path *p;
+ pa_bool_t duplicate = FALSE;
+ char **kn, *fn;
+
+ for (kn = pn; kn < in; kn++)
+ if (pa_streq(*kn, *in)) {
+ duplicate = TRUE;
+ break;
+ }
+
+ if (duplicate)
+ continue;
+
+ fn = pa_sprintf_malloc("%s.conf", *in);
+
+ if ((p = pa_alsa_path_new(fn, direction))) {
+ p->path_set = ps;
+ PA_LLIST_INSERT_AFTER(pa_alsa_path, ps->paths, ps->last_path, p);
+ ps->last_path = p;
+ }
+
+ pa_xfree(fn);
+ }
+
+ goto finish;
+ }
+
+ if (direction == PA_ALSA_DIRECTION_OUTPUT)
+ en = m->output_element;
+ else if (direction == PA_ALSA_DIRECTION_INPUT)
+ en = m->input_element;
+
+ if (!en) {
+ pa_alsa_path_set_free(ps);
+ return NULL;
+ }
+
+ for (ie = en; *ie; ie++) {
+ char **je;
+ pa_alsa_path *p;
+
+ p = pa_alsa_path_synthesize(*ie, direction);
+ p->path_set = ps;
+
+ /* Mark all other passed elements for require-absent */
+ for (je = en; *je; je++) {
+ pa_alsa_element *e;
+
+ if (je == ie)
+ continue;
+
+ e = pa_xnew0(pa_alsa_element, 1);
+ e->path = p;
+ e->alsa_name = pa_xstrdup(*je);
+ e->direction = direction;
+ e->required_absent = PA_ALSA_REQUIRED_ANY;
+ e->volume_limit = -1;
+
+ PA_LLIST_INSERT_AFTER(pa_alsa_element, p->elements, p->last_element, e);
+ p->last_element = e;
+ }
+
+ PA_LLIST_INSERT_AFTER(pa_alsa_path, ps->paths, ps->last_path, p);
+ ps->last_path = p;
+ }
+
+finish:
+ /* Assign decibel fixes to elements. */
+ PA_HASHMAP_FOREACH(db_fix, m->profile_set->decibel_fixes, state) {
+ pa_alsa_path *p;
+
+ PA_LLIST_FOREACH(p, ps->paths) {
+ pa_alsa_element *e;
+
+ PA_LLIST_FOREACH(e, p->elements) {
+ if (e->volume_use != PA_ALSA_VOLUME_IGNORE && pa_streq(db_fix->name, e->alsa_name)) {
+ /* The profile set that contains the dB fix may be freed
+ * before the element, so we have to copy the dB fix
+ * object. */
+ e->db_fix = pa_xnewdup(pa_alsa_decibel_fix, db_fix, 1);
+ e->db_fix->profile_set = NULL;
+ e->db_fix->name = pa_xstrdup(db_fix->name);
+ e->db_fix->db_values = pa_xmemdup(db_fix->db_values, (db_fix->max_step - db_fix->min_step + 1) * sizeof(long));
+ }
+ }
+ }
+ }
+
+ return ps;
+}
+
+void pa_alsa_path_set_dump(pa_alsa_path_set *ps) {
+ pa_alsa_path *p;
+ pa_assert(ps);
+
+ pa_log_debug("Path Set %p, direction=%i, probed=%s",
+ (void*) ps,
+ ps->direction,
+ pa_yes_no(ps->probed));
+
+ PA_LLIST_FOREACH(p, ps->paths)
+ pa_alsa_path_dump(p);
+}
+
+static void path_set_unify(pa_alsa_path_set *ps) {
+ pa_alsa_path *p;
+ pa_bool_t has_dB = TRUE, has_volume = TRUE, has_mute = TRUE;
+ pa_assert(ps);
+
+ /* We have issues dealing with paths that vary too wildly. That
+ * means for now we have to have all paths support volume/mute/dB
+ * or none. */
+
+ PA_LLIST_FOREACH(p, ps->paths) {
+ pa_assert(p->probed);
+
+ if (!p->has_volume)
+ has_volume = FALSE;
+ else if (!p->has_dB)
+ has_dB = FALSE;
+
+ if (!p->has_mute)
+ has_mute = FALSE;
+ }
+
+ if (!has_volume || !has_dB || !has_mute) {
+
+ if (!has_volume)
+ pa_log_debug("Some paths of the device lack hardware volume control, disabling hardware control altogether.");
+ else if (!has_dB)
+ pa_log_debug("Some paths of the device lack dB information, disabling dB logic altogether.");
+
+ if (!has_mute)
+ pa_log_debug("Some paths of the device lack hardware mute control, disabling hardware control altogether.");
+
+ PA_LLIST_FOREACH(p, ps->paths) {
+ if (!has_volume)
+ p->has_volume = FALSE;
+ else if (!has_dB)
+ p->has_dB = FALSE;
+
+ if (!has_mute)
+ p->has_mute = FALSE;
+ }
+ }
+}
+
+static void path_set_make_paths_unique(pa_alsa_path_set *ps) {
+ pa_alsa_path *p, *q;
+
+ PA_LLIST_FOREACH(p, ps->paths) {
+ unsigned i;
+ char *m;
+
+ for (q = p->next; q; q = q->next)
+ if (pa_streq(q->name, p->name))
+ break;
+
+ if (!q)
+ continue;
+
+ m = pa_xstrdup(p->name);
+
+ /* OK, this name is not unique, hence let's rename */
+ for (i = 1, q = p; q; q = q->next) {
+ char *nn, *nd;
+
+ if (!pa_streq(q->name, m))
+ continue;
+
+ nn = pa_sprintf_malloc("%s-%u", m, i);
+ pa_xfree(q->name);
+ q->name = nn;
+
+ nd = pa_sprintf_malloc("%s %u", q->description, i);
+ pa_xfree(q->description);
+ q->description = nd;
+
+ i++;
+ }
+
+ pa_xfree(m);
+ }
+}
+
+void pa_alsa_path_set_probe(pa_alsa_path_set *ps, snd_mixer_t *m, pa_bool_t ignore_dB) {
+ pa_alsa_path *p, *n;
+
+ pa_assert(ps);
+
+ if (ps->probed)
+ return;
+
+ for (p = ps->paths; p; p = n) {
+ n = p->next;
+
+ if (pa_alsa_path_probe(p, m, ignore_dB) < 0) {
+ PA_LLIST_REMOVE(pa_alsa_path, ps->paths, p);
+ pa_alsa_path_free(p);
+ }
+ }
+
+ path_set_unify(ps);
+ path_set_make_paths_unique(ps);
+ ps->probed = TRUE;
+}
+
+static void mapping_free(pa_alsa_mapping *m) {
+ pa_assert(m);
+
+ pa_xfree(m->name);
+ pa_xfree(m->description);
+
+ pa_xstrfreev(m->device_strings);
+ pa_xstrfreev(m->input_path_names);
+ pa_xstrfreev(m->output_path_names);
+ pa_xstrfreev(m->input_element);
+ pa_xstrfreev(m->output_element);
+
+ pa_assert(!m->input_pcm);
+ pa_assert(!m->output_pcm);
+
+ pa_xfree(m);
+}
+
+static void profile_free(pa_alsa_profile *p) {
+ pa_assert(p);
+
+ pa_xfree(p->name);
+ pa_xfree(p->description);
+
+ pa_xstrfreev(p->input_mapping_names);
+ pa_xstrfreev(p->output_mapping_names);
+
+ if (p->input_mappings)
+ pa_idxset_free(p->input_mappings, NULL, NULL);
+
+ if (p->output_mappings)
+ pa_idxset_free(p->output_mappings, NULL, NULL);
+
+ pa_xfree(p);
+}
+
+void pa_alsa_profile_set_free(pa_alsa_profile_set *ps) {
+ pa_assert(ps);
+
+ if (ps->profiles) {
+ pa_alsa_profile *p;
+
+ while ((p = pa_hashmap_steal_first(ps->profiles)))
+ profile_free(p);
+
+ pa_hashmap_free(ps->profiles, NULL, NULL);
+ }
+
+ if (ps->mappings) {
+ pa_alsa_mapping *m;
+
+ while ((m = pa_hashmap_steal_first(ps->mappings)))
+ mapping_free(m);
+
+ pa_hashmap_free(ps->mappings, NULL, NULL);
+ }
+
+ if (ps->decibel_fixes) {
+ pa_alsa_decibel_fix *db_fix;
+
+ while ((db_fix = pa_hashmap_steal_first(ps->decibel_fixes)))
+ decibel_fix_free(db_fix);
+
+ pa_hashmap_free(ps->decibel_fixes, NULL, NULL);
+ }
+
+ pa_xfree(ps);
+}
+
+static pa_alsa_mapping *mapping_get(pa_alsa_profile_set *ps, const char *name) {
+ pa_alsa_mapping *m;
+
+ if (!pa_startswith(name, "Mapping "))
+ return NULL;
+
+ name += 8;
+
+ if ((m = pa_hashmap_get(ps->mappings, name)))
+ return m;
+
+ m = pa_xnew0(pa_alsa_mapping, 1);
+ m->profile_set = ps;
+ m->name = pa_xstrdup(name);
+ pa_channel_map_init(&m->channel_map);
+
+ pa_hashmap_put(ps->mappings, m->name, m);
+
+ return m;
+}
+
+static pa_alsa_profile *profile_get(pa_alsa_profile_set *ps, const char *name) {
+ pa_alsa_profile *p;
+
+ if (!pa_startswith(name, "Profile "))
+ return NULL;
+
+ name += 8;
+
+ if ((p = pa_hashmap_get(ps->profiles, name)))
+ return p;
+
+ p = pa_xnew0(pa_alsa_profile, 1);
+ p->profile_set = ps;
+ p->name = pa_xstrdup(name);
+
+ pa_hashmap_put(ps->profiles, p->name, p);
+
+ return p;
+}
+
+static pa_alsa_decibel_fix *decibel_fix_get(pa_alsa_profile_set *ps, const char *name) {
+ pa_alsa_decibel_fix *db_fix;
+
+ if (!pa_startswith(name, "DecibelFix "))
+ return NULL;
+
+ name += 11;
+
+ if ((db_fix = pa_hashmap_get(ps->decibel_fixes, name)))
+ return db_fix;
+
+ db_fix = pa_xnew0(pa_alsa_decibel_fix, 1);
+ db_fix->profile_set = ps;
+ db_fix->name = pa_xstrdup(name);
+
+ pa_hashmap_put(ps->decibel_fixes, db_fix->name, db_fix);
+
+ return db_fix;
+}
+
+static int mapping_parse_device_strings(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_mapping *m;
+
+ pa_assert(ps);
+
+ if (!(m = mapping_get(ps, section))) {
+ pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section);
+ return -1;
+ }
+
+ pa_xstrfreev(m->device_strings);
+ if (!(m->device_strings = pa_split_spaces_strv(rvalue))) {
+ pa_log("[%s:%u] Device string list empty of '%s'", filename, line, section);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int mapping_parse_channel_map(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_mapping *m;
+
+ pa_assert(ps);
+
+ if (!(m = mapping_get(ps, section))) {
+ pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section);
+ return -1;
+ }
+
+ if (!(pa_channel_map_parse(&m->channel_map, rvalue))) {
+ pa_log("[%s:%u] Channel map invalid of '%s'", filename, line, section);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int mapping_parse_paths(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_mapping *m;
+
+ pa_assert(ps);
+
+ if (!(m = mapping_get(ps, section))) {
+ pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section);
+ return -1;
+ }
+
+ if (pa_streq(lvalue, "paths-input")) {
+ pa_xstrfreev(m->input_path_names);
+ m->input_path_names = pa_split_spaces_strv(rvalue);
+ } else {
+ pa_xstrfreev(m->output_path_names);
+ m->output_path_names = pa_split_spaces_strv(rvalue);
+ }
+
+ return 0;
+}
+
+static int mapping_parse_element(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_mapping *m;
+
+ pa_assert(ps);
+
+ if (!(m = mapping_get(ps, section))) {
+ pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section);
+ return -1;
+ }
+
+ if (pa_streq(lvalue, "element-input")) {
+ pa_xstrfreev(m->input_element);
+ m->input_element = pa_split_spaces_strv(rvalue);
+ } else {
+ pa_xstrfreev(m->output_element);
+ m->output_element = pa_split_spaces_strv(rvalue);
+ }
+
+ return 0;
+}
+
+static int mapping_parse_direction(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_mapping *m;
+
+ pa_assert(ps);
+
+ if (!(m = mapping_get(ps, section))) {
+ pa_log("[%s:%u] Section name %s invalid.", filename, line, section);
+ return -1;
+ }
+
+ if (pa_streq(rvalue, "input"))
+ m->direction = PA_ALSA_DIRECTION_INPUT;
+ else if (pa_streq(rvalue, "output"))
+ m->direction = PA_ALSA_DIRECTION_OUTPUT;
+ else if (pa_streq(rvalue, "any"))
+ m->direction = PA_ALSA_DIRECTION_ANY;
+ else {
+ pa_log("[%s:%u] Direction %s invalid.", filename, line, rvalue);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int mapping_parse_description(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_profile *p;
+ pa_alsa_mapping *m;
+
+ pa_assert(ps);
+
+ if ((m = mapping_get(ps, section))) {
+ pa_xfree(m->description);
+ m->description = pa_xstrdup(rvalue);
+ } else if ((p = profile_get(ps, section))) {
+ pa_xfree(p->description);
+ p->description = pa_xstrdup(rvalue);
+ } else {
+ pa_log("[%s:%u] Section name %s invalid.", filename, line, section);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int mapping_parse_priority(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_profile *p;
+ pa_alsa_mapping *m;
+ uint32_t prio;
+
+ pa_assert(ps);
+
+ if (pa_atou(rvalue, &prio) < 0) {
+ pa_log("[%s:%u] Priority invalid of '%s'", filename, line, section);
+ return -1;
+ }
+
+ if ((m = mapping_get(ps, section)))
+ m->priority = prio;
+ else if ((p = profile_get(ps, section)))
+ p->priority = prio;
+ else {
+ pa_log("[%s:%u] Section name %s invalid.", filename, line, section);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int profile_parse_mappings(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_profile *p;
+
+ pa_assert(ps);
+
+ if (!(p = profile_get(ps, section))) {
+ pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section);
+ return -1;
+ }
+
+ if (pa_streq(lvalue, "input-mappings")) {
+ pa_xstrfreev(p->input_mapping_names);
+ p->input_mapping_names = pa_split_spaces_strv(rvalue);
+ } else {
+ pa_xstrfreev(p->output_mapping_names);
+ p->output_mapping_names = pa_split_spaces_strv(rvalue);
+ }
+
+ return 0;
+}
+
+static int profile_parse_skip_probe(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_profile *p;
+ int b;
+
+ pa_assert(ps);
+
+ if (!(p = profile_get(ps, section))) {
+ pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section);
+ return -1;
+ }
+
+ if ((b = pa_parse_boolean(rvalue)) < 0) {
+ pa_log("[%s:%u] Skip probe invalid of '%s'", filename, line, section);
+ return -1;
+ }
+
+ p->supported = b;
+
+ return 0;
+}
+
+static int decibel_fix_parse_db_values(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ pa_alsa_profile_set *ps = userdata;
+ pa_alsa_decibel_fix *db_fix;
+ char **items;
+ char *item;
+ long *db_values;
+ unsigned n = 8; /* Current size of the db_values table. */
+ unsigned min_step = 0;
+ unsigned max_step = 0;
+ unsigned i = 0; /* Index to the items table. */
+ unsigned prev_step = 0;
+ double prev_db = 0;
+
+ pa_assert(filename);
+ pa_assert(section);
+ pa_assert(lvalue);
+ pa_assert(rvalue);
+ pa_assert(ps);
+
+ if (!(db_fix = decibel_fix_get(ps, section))) {
+ pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section);
+ return -1;
+ }
+
+ if (!(items = pa_split_spaces_strv(rvalue))) {
+ pa_log("[%s:%u] Value missing", pa_strnull(filename), line);
+ return -1;
+ }
+
+ db_values = pa_xnew(long, n);
+
+ while ((item = items[i++])) {
+ char *s = item; /* Step value string. */
+ char *d = item; /* dB value string. */
+ uint32_t step;
+ double db;
+
+ /* Move d forward until it points to a colon or to the end of the item. */
+ for (; *d && *d != ':'; ++d);
+
+ if (d == s) {
+ /* item started with colon. */
+ pa_log("[%s:%u] No step value found in %s", filename, line, item);
+ goto fail;
+ }
+
+ if (!*d || !*(d + 1)) {
+ /* No colon found, or it was the last character in item. */
+ pa_log("[%s:%u] No dB value found in %s", filename, line, item);
+ goto fail;
+ }
+
+ /* pa_atou() needs a null-terminating string. Let's replace the colon
+ * with a zero byte. */
+ *d++ = '\0';
+
+ if (pa_atou(s, &step) < 0) {
+ pa_log("[%s:%u] Invalid step value: %s", filename, line, s);
+ goto fail;
+ }
+
+ if (pa_atod(d, &db) < 0) {
+ pa_log("[%s:%u] Invalid dB value: %s", filename, line, d);
+ goto fail;
+ }
+
+ if (step <= prev_step && i != 1) {
+ pa_log("[%s:%u] Step value %u not greater than the previous value %u", filename, line, step, prev_step);
+ goto fail;
+ }
+
+ if (db < prev_db && i != 1) {
+ pa_log("[%s:%u] Decibel value %0.2f less than the previous value %0.2f", filename, line, db, prev_db);
+ goto fail;
+ }
+
+ if (i == 1) {
+ min_step = step;
+ db_values[0] = (long) (db * 100.0);
+ prev_step = step;
+ prev_db = db;
+ } else {
+ /* Interpolate linearly. */
+ double db_increment = (db - prev_db) / (step - prev_step);
+
+ for (; prev_step < step; ++prev_step, prev_db += db_increment) {
+
+ /* Reallocate the db_values table if it's about to overflow. */
+ if (prev_step + 1 - min_step == n) {
+ n *= 2;
+ db_values = pa_xrenew(long, db_values, n);
+ }
+
+ db_values[prev_step + 1 - min_step] = (long) ((prev_db + db_increment) * 100.0);
+ }
+ }
+
+ max_step = step;
+ }
+
+ db_fix->min_step = min_step;
+ db_fix->max_step = max_step;
+ pa_xfree(db_fix->db_values);
+ db_fix->db_values = db_values;
+
+ pa_xstrfreev(items);
+
+ return 0;
+
+fail:
+ pa_xstrfreev(items);
+ pa_xfree(db_values);
+
+ return -1;
+}
+
+static int mapping_verify(pa_alsa_mapping *m, const pa_channel_map *bonus) {
+
+ static const struct description_map well_known_descriptions[] = {
+ { "analog-mono", N_("Analog Mono") },
+ { "analog-stereo", N_("Analog Stereo") },
+ { "analog-surround-21", N_("Analog Surround 2.1") },
+ { "analog-surround-30", N_("Analog Surround 3.0") },
+ { "analog-surround-31", N_("Analog Surround 3.1") },
+ { "analog-surround-40", N_("Analog Surround 4.0") },
+ { "analog-surround-41", N_("Analog Surround 4.1") },
+ { "analog-surround-50", N_("Analog Surround 5.0") },
+ { "analog-surround-51", N_("Analog Surround 5.1") },
+ { "analog-surround-61", N_("Analog Surround 6.0") },
+ { "analog-surround-61", N_("Analog Surround 6.1") },
+ { "analog-surround-70", N_("Analog Surround 7.0") },
+ { "analog-surround-71", N_("Analog Surround 7.1") },
+ { "iec958-stereo", N_("Digital Stereo (IEC958)") },
+ { "iec958-passthrough", N_("Digital Passthrough (IEC958)") },
+ { "iec958-ac3-surround-40", N_("Digital Surround 4.0 (IEC958/AC3)") },
+ { "iec958-ac3-surround-51", N_("Digital Surround 5.1 (IEC958/AC3)") },
+ { "hdmi-stereo", N_("Digital Stereo (HDMI)") }
+ };
+
+ pa_assert(m);
+
+ if (!pa_channel_map_valid(&m->channel_map)) {
+ pa_log("Mapping %s is missing channel map.", m->name);
+ return -1;
+ }
+
+ if (!m->device_strings) {
+ pa_log("Mapping %s is missing device strings.", m->name);
+ return -1;
+ }
+
+ if ((m->input_path_names && m->input_element) ||
+ (m->output_path_names && m->output_element)) {
+ pa_log("Mapping %s must have either mixer path or mixer element, not both.", m->name);
+ return -1;
+ }
+
+ if (!m->description)
+ m->description = pa_xstrdup(lookup_description(m->name,
+ well_known_descriptions,
+ PA_ELEMENTSOF(well_known_descriptions)));
+
+ if (!m->description)
+ m->description = pa_xstrdup(m->name);
+
+ if (bonus) {
+ if (pa_channel_map_equal(&m->channel_map, bonus))
+ m->priority += 50;
+ else if (m->channel_map.channels == bonus->channels)
+ m->priority += 30;
+ }
+
+ return 0;
+}
+
+void pa_alsa_mapping_dump(pa_alsa_mapping *m) {
+ char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
+
+ pa_assert(m);
+
+ pa_log_debug("Mapping %s (%s), priority=%u, channel_map=%s, supported=%s, direction=%i",
+ m->name,
+ pa_strnull(m->description),
+ m->priority,
+ pa_channel_map_snprint(cm, sizeof(cm), &m->channel_map),
+ pa_yes_no(m->supported),
+ m->direction);
+}
+
+static void profile_set_add_auto_pair(
+ pa_alsa_profile_set *ps,
+ pa_alsa_mapping *m, /* output */
+ pa_alsa_mapping *n /* input */) {
+
+ char *name;
+ pa_alsa_profile *p;
+
+ pa_assert(ps);
+ pa_assert(m || n);
+
+ if (m && m->direction == PA_ALSA_DIRECTION_INPUT)
+ return;
+
+ if (n && n->direction == PA_ALSA_DIRECTION_OUTPUT)
+ return;
+
+ if (m && n)
+ name = pa_sprintf_malloc("output:%s+input:%s", m->name, n->name);
+ else if (m)
+ name = pa_sprintf_malloc("output:%s", m->name);
+ else
+ name = pa_sprintf_malloc("input:%s", n->name);
+
+ if (pa_hashmap_get(ps->profiles, name)) {
+ pa_xfree(name);
+ return;
+ }
+
+ p = pa_xnew0(pa_alsa_profile, 1);
+ p->profile_set = ps;
+ p->name = name;
+
+ if (m) {
+ p->output_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ pa_idxset_put(p->output_mappings, m, NULL);
+ p->priority += m->priority * 100;
+ }
+
+ if (n) {
+ p->input_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ pa_idxset_put(p->input_mappings, n, NULL);
+ p->priority += n->priority;
+ }
+
+ pa_hashmap_put(ps->profiles, p->name, p);
+}
+
+static void profile_set_add_auto(pa_alsa_profile_set *ps) {
+ pa_alsa_mapping *m, *n;
+ void *m_state, *n_state;
+
+ pa_assert(ps);
+
+ PA_HASHMAP_FOREACH(m, ps->mappings, m_state) {
+ profile_set_add_auto_pair(ps, m, NULL);
+
+ PA_HASHMAP_FOREACH(n, ps->mappings, n_state)
+ profile_set_add_auto_pair(ps, m, n);
+ }
+
+ PA_HASHMAP_FOREACH(n, ps->mappings, n_state)
+ profile_set_add_auto_pair(ps, NULL, n);
+}
+
+static int profile_verify(pa_alsa_profile *p) {
+
+ static const struct description_map well_known_descriptions[] = {
+ { "output:analog-mono+input:analog-mono", N_("Analog Mono Duplex") },
+ { "output:analog-stereo+input:analog-stereo", N_("Analog Stereo Duplex") },
+ { "output:iec958-stereo+input:iec958-stereo", N_("Digital Stereo Duplex (IEC958)") },
+ { "off", N_("Off") }
+ };
+
+ pa_assert(p);
+
+ /* Replace the output mapping names by the actual mappings */
+ if (p->output_mapping_names) {
+ char **name;
+
+ pa_assert(!p->output_mappings);
+ p->output_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ for (name = p->output_mapping_names; *name; name++) {
+ pa_alsa_mapping *m;
+ char **in;
+ pa_bool_t duplicate = FALSE;
+
+ for (in = name + 1; *in; in++)
+ if (pa_streq(*name, *in)) {
+ duplicate = TRUE;
+ break;
+ }
+
+ if (duplicate)
+ continue;
+
+ if (!(m = pa_hashmap_get(p->profile_set->mappings, *name)) || m->direction == PA_ALSA_DIRECTION_INPUT) {
+ pa_log("Profile '%s' refers to unexistant mapping '%s'.", p->name, *name);
+ return -1;
+ }
+
+ pa_idxset_put(p->output_mappings, m, NULL);
+
+ if (p->supported)
+ m->supported++;
+ }
+
+ pa_xstrfreev(p->output_mapping_names);
+ p->output_mapping_names = NULL;
+ }
+
+ /* Replace the input mapping names by the actual mappings */
+ if (p->input_mapping_names) {
+ char **name;
+
+ pa_assert(!p->input_mappings);
+ p->input_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ for (name = p->input_mapping_names; *name; name++) {
+ pa_alsa_mapping *m;
+ char **in;
+ pa_bool_t duplicate = FALSE;
+
+ for (in = name + 1; *in; in++)
+ if (pa_streq(*name, *in)) {
+ duplicate = TRUE;
+ break;
+ }
+
+ if (duplicate)
+ continue;
+
+ if (!(m = pa_hashmap_get(p->profile_set->mappings, *name)) || m->direction == PA_ALSA_DIRECTION_OUTPUT) {
+ pa_log("Profile '%s' refers to unexistant mapping '%s'.", p->name, *name);
+ return -1;
+ }
+
+ pa_idxset_put(p->input_mappings, m, NULL);
+
+ if (p->supported)
+ m->supported++;
+ }
+
+ pa_xstrfreev(p->input_mapping_names);
+ p->input_mapping_names = NULL;
+ }
+
+ if (!p->input_mappings && !p->output_mappings) {
+ pa_log("Profile '%s' lacks mappings.", p->name);
+ return -1;
+ }
+
+ if (!p->description)
+ p->description = pa_xstrdup(lookup_description(p->name,
+ well_known_descriptions,
+ PA_ELEMENTSOF(well_known_descriptions)));
+
+ if (!p->description) {
+ pa_strbuf *sb;
+ uint32_t idx;
+ pa_alsa_mapping *m;
+
+ sb = pa_strbuf_new();
+
+ if (p->output_mappings)
+ PA_IDXSET_FOREACH(m, p->output_mappings, idx) {
+ if (!pa_strbuf_isempty(sb))
+ pa_strbuf_puts(sb, " + ");
+
+ pa_strbuf_printf(sb, _("%s Output"), m->description);
+ }
+
+ if (p->input_mappings)
+ PA_IDXSET_FOREACH(m, p->input_mappings, idx) {
+ if (!pa_strbuf_isempty(sb))
+ pa_strbuf_puts(sb, " + ");
+
+ pa_strbuf_printf(sb, _("%s Input"), m->description);
+ }
+
+ p->description = pa_strbuf_tostring_free(sb);
+ }
+
+ return 0;
+}
+
+void pa_alsa_profile_dump(pa_alsa_profile *p) {
+ uint32_t idx;
+ pa_alsa_mapping *m;
+ pa_assert(p);
+
+ pa_log_debug("Profile %s (%s), priority=%u, supported=%s n_input_mappings=%u, n_output_mappings=%u",
+ p->name,
+ pa_strnull(p->description),
+ p->priority,
+ pa_yes_no(p->supported),
+ p->input_mappings ? pa_idxset_size(p->input_mappings) : 0,
+ p->output_mappings ? pa_idxset_size(p->output_mappings) : 0);
+
+ if (p->input_mappings)
+ PA_IDXSET_FOREACH(m, p->input_mappings, idx)
+ pa_log_debug("Input %s", m->name);
+
+ if (p->output_mappings)
+ PA_IDXSET_FOREACH(m, p->output_mappings, idx)
+ pa_log_debug("Output %s", m->name);
+}
+
+static int decibel_fix_verify(pa_alsa_decibel_fix *db_fix) {
+ pa_assert(db_fix);
+
+ /* Check that the dB mapping has been configured. Since "db-values" is
+ * currently the only option in the DecibelFix section, and decibel fix
+ * objects don't get created if a DecibelFix section is empty, this is
+ * actually a redundant check. Having this may prevent future bugs,
+ * however. */
+ if (!db_fix->db_values) {
+ pa_log("Decibel fix for element %s lacks the dB values.", db_fix->name);
+ return -1;
+ }
+
+ return 0;
+}
+
+void pa_alsa_decibel_fix_dump(pa_alsa_decibel_fix *db_fix) {
+ char *db_values = NULL;
+
+ pa_assert(db_fix);
+
+ if (db_fix->db_values) {
+ pa_strbuf *buf;
+ unsigned long i, nsteps;
+
+ pa_assert(db_fix->min_step <= db_fix->max_step);
+ nsteps = db_fix->max_step - db_fix->min_step + 1;
+
+ buf = pa_strbuf_new();
+ for (i = 0; i < nsteps; ++i)
+ pa_strbuf_printf(buf, "[%li]:%0.2f ", i + db_fix->min_step, db_fix->db_values[i] / 100.0);
+
+ db_values = pa_strbuf_tostring_free(buf);
+ }
+
+ pa_log_debug("Decibel fix %s, min_step=%li, max_step=%li, db_values=%s",
+ db_fix->name, db_fix->min_step, db_fix->max_step, pa_strnull(db_values));
+
+ pa_xfree(db_values);
+}
+
+pa_alsa_profile_set* pa_alsa_profile_set_new(const char *fname, const pa_channel_map *bonus) {
+ pa_alsa_profile_set *ps;
+ pa_alsa_profile *p;
+ pa_alsa_mapping *m;
+ pa_alsa_decibel_fix *db_fix;
+ char *fn;
+ int r;
+ void *state;
+
+ static pa_config_item items[] = {
+ /* [General] */
+ { "auto-profiles", pa_config_parse_bool, NULL, "General" },
+
+ /* [Mapping ...] */
+ { "device-strings", mapping_parse_device_strings, NULL, NULL },
+ { "channel-map", mapping_parse_channel_map, NULL, NULL },
+ { "paths-input", mapping_parse_paths, NULL, NULL },
+ { "paths-output", mapping_parse_paths, NULL, NULL },
+ { "element-input", mapping_parse_element, NULL, NULL },
+ { "element-output", mapping_parse_element, NULL, NULL },
+ { "direction", mapping_parse_direction, NULL, NULL },
+
+ /* Shared by [Mapping ...] and [Profile ...] */
+ { "description", mapping_parse_description, NULL, NULL },
+ { "priority", mapping_parse_priority, NULL, NULL },
+
+ /* [Profile ...] */
+ { "input-mappings", profile_parse_mappings, NULL, NULL },
+ { "output-mappings", profile_parse_mappings, NULL, NULL },
+ { "skip-probe", profile_parse_skip_probe, NULL, NULL },
+
+ /* [DecibelFix ...] */
+ { "db-values", decibel_fix_parse_db_values, NULL, NULL },
+ { NULL, NULL, NULL, NULL }
+ };
+
+ ps = pa_xnew0(pa_alsa_profile_set, 1);
+ ps->mappings = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ ps->profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ ps->decibel_fixes = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ items[0].data = &ps->auto_profiles;
+
+ if (!fname)
+ fname = "default.conf";
+
+ fn = pa_maybe_prefix_path(fname,
+ pa_run_from_build_tree() ? PA_BUILDDIR "/modules/alsa/mixer/profile-sets/" :
+ PA_ALSA_PROFILE_SETS_DIR);
+
+ r = pa_config_parse(fn, NULL, items, ps);
+ pa_xfree(fn);
+
+ if (r < 0)
+ goto fail;
+
+ PA_HASHMAP_FOREACH(m, ps->mappings, state)
+ if (mapping_verify(m, bonus) < 0)
+ goto fail;
+
+ if (ps->auto_profiles)
+ profile_set_add_auto(ps);
+
+ PA_HASHMAP_FOREACH(p, ps->profiles, state)
+ if (profile_verify(p) < 0)
+ goto fail;
+
+ PA_HASHMAP_FOREACH(db_fix, ps->decibel_fixes, state)
+ if (decibel_fix_verify(db_fix) < 0)
+ goto fail;
+
+ return ps;
+
+fail:
+ pa_alsa_profile_set_free(ps);
+ return NULL;
+}
+
+void pa_alsa_profile_set_probe(
+ pa_alsa_profile_set *ps,
+ const char *dev_id,
+ const pa_sample_spec *ss,
+ unsigned default_n_fragments,
+ unsigned default_fragment_size_msec) {
+
+ void *state;
+ pa_alsa_profile *p, *last = NULL;
+ pa_alsa_mapping *m;
+
+ pa_assert(ps);
+ pa_assert(dev_id);
+ pa_assert(ss);
+
+ if (ps->probed)
+ return;
+
+ PA_HASHMAP_FOREACH(p, ps->profiles, state) {
+ pa_sample_spec try_ss;
+ pa_channel_map try_map;
+ snd_pcm_uframes_t try_period_size, try_buffer_size;
+ uint32_t idx;
+
+ /* Is this already marked that it is supported? (i.e. from the config file) */
+ if (p->supported)
+ continue;
+
+ pa_log_debug("Looking at profile %s", p->name);
+
+ /* Close PCMs from the last iteration we don't need anymore */
+ if (last && last->output_mappings)
+ PA_IDXSET_FOREACH(m, last->output_mappings, idx) {
+
+ if (!m->output_pcm)
+ break;
+
+ if (last->supported)
+ m->supported++;
+
+ if (!p->output_mappings || !pa_idxset_get_by_data(p->output_mappings, m, NULL)) {
+ snd_pcm_close(m->output_pcm);
+ m->output_pcm = NULL;
+ }
+ }
+
+ if (last && last->input_mappings)
+ PA_IDXSET_FOREACH(m, last->input_mappings, idx) {
+
+ if (!m->input_pcm)
+ break;
+
+ if (last->supported)
+ m->supported++;
+
+ if (!p->input_mappings || !pa_idxset_get_by_data(p->input_mappings, m, NULL)) {
+ snd_pcm_close(m->input_pcm);
+ m->input_pcm = NULL;
+ }
+ }
+
+ p->supported = TRUE;
+
+ /* Check if we can open all new ones */
+ if (p->output_mappings)
+ PA_IDXSET_FOREACH(m, p->output_mappings, idx) {
+
+ if (m->output_pcm)
+ continue;
+
+ pa_log_debug("Checking for playback on %s (%s)", m->description, m->name);
+ try_map = m->channel_map;
+ try_ss = *ss;
+ try_ss.channels = try_map.channels;
+
+ try_period_size =
+ pa_usec_to_bytes(default_fragment_size_msec * PA_USEC_PER_MSEC, &try_ss) /
+ pa_frame_size(&try_ss);
+ try_buffer_size = default_n_fragments * try_period_size;
+
+ if (!(m ->output_pcm = pa_alsa_open_by_template(
+ m->device_strings,
+ dev_id,
+ NULL,
+ &try_ss, &try_map,
+ SND_PCM_STREAM_PLAYBACK,
+ &try_period_size, &try_buffer_size, 0, NULL, NULL,
+ TRUE))) {
+ p->supported = FALSE;
+ break;
+ }
+ }
+
+ if (p->input_mappings && p->supported)
+ PA_IDXSET_FOREACH(m, p->input_mappings, idx) {
+
+ if (m->input_pcm)
+ continue;
+
+ pa_log_debug("Checking for recording on %s (%s)", m->description, m->name);
+ try_map = m->channel_map;
+ try_ss = *ss;
+ try_ss.channels = try_map.channels;
+
+ try_period_size =
+ pa_usec_to_bytes(default_fragment_size_msec*PA_USEC_PER_MSEC, &try_ss) /
+ pa_frame_size(&try_ss);
+ try_buffer_size = default_n_fragments * try_period_size;
+
+ if (!(m ->input_pcm = pa_alsa_open_by_template(
+ m->device_strings,
+ dev_id,
+ NULL,
+ &try_ss, &try_map,
+ SND_PCM_STREAM_CAPTURE,
+ &try_period_size, &try_buffer_size, 0, NULL, NULL,
+ TRUE))) {
+ p->supported = FALSE;
+ break;
+ }
+ }
+
+ last = p;
+
+ if (p->supported)
+ pa_log_debug("Profile %s supported.", p->name);
+ }
+
+ /* Clean up */
+ if (last) {
+ uint32_t idx;
+
+ if (last->output_mappings)
+ PA_IDXSET_FOREACH(m, last->output_mappings, idx)
+ if (m->output_pcm) {
+
+ if (last->supported)
+ m->supported++;
+
+ snd_pcm_close(m->output_pcm);
+ m->output_pcm = NULL;
+ }
+
+ if (last->input_mappings)
+ PA_IDXSET_FOREACH(m, last->input_mappings, idx)
+ if (m->input_pcm) {
+
+ if (last->supported)
+ m->supported++;
+
+ snd_pcm_close(m->input_pcm);
+ m->input_pcm = NULL;
+ }
+ }
+
+ PA_HASHMAP_FOREACH(p, ps->profiles, state)
+ if (!p->supported) {
+ pa_hashmap_remove(ps->profiles, p->name);
+ profile_free(p);
+ }
+
+ PA_HASHMAP_FOREACH(m, ps->mappings, state)
+ if (m->supported <= 0) {
+ pa_hashmap_remove(ps->mappings, m->name);
+ mapping_free(m);
+ }
+
+ ps->probed = TRUE;
+}
+
+void pa_alsa_profile_set_dump(pa_alsa_profile_set *ps) {
+ pa_alsa_profile *p;
+ pa_alsa_mapping *m;
+ pa_alsa_decibel_fix *db_fix;
+ void *state;
+
+ pa_assert(ps);
+
+ pa_log_debug("Profile set %p, auto_profiles=%s, probed=%s, n_mappings=%u, n_profiles=%u, n_decibel_fixes=%u",
+ (void*)
+ ps,
+ pa_yes_no(ps->auto_profiles),
+ pa_yes_no(ps->probed),
+ pa_hashmap_size(ps->mappings),
+ pa_hashmap_size(ps->profiles),
+ pa_hashmap_size(ps->decibel_fixes));
+
+ PA_HASHMAP_FOREACH(m, ps->mappings, state)
+ pa_alsa_mapping_dump(m);
+
+ PA_HASHMAP_FOREACH(p, ps->profiles, state)
+ pa_alsa_profile_dump(p);
+
+ PA_HASHMAP_FOREACH(db_fix, ps->decibel_fixes, state)
+ pa_alsa_decibel_fix_dump(db_fix);
+}
+
+void pa_alsa_add_ports(pa_hashmap **p, pa_alsa_path_set *ps) {
+ pa_alsa_path *path;
+
+ pa_assert(p);
+ pa_assert(!*p);
+ pa_assert(ps);
+
+ /* if there is no path, we don't want a port list */
+ if (!ps->paths)
+ return;
+
+ if (!ps->paths->next){
+ pa_alsa_setting *s;
+
+ /* If there is only one path, but no or only one setting, then
+ * we want a port list either */
+ if (!ps->paths->settings || !ps->paths->settings->next)
+ return;
+
+ /* Ok, there is only one path, however with multiple settings,
+ * so let's create a port for each setting */
+ *p = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ PA_LLIST_FOREACH(s, ps->paths->settings) {
+ pa_device_port *port;
+ pa_alsa_port_data *data;
+
+ port = pa_device_port_new(s->name, s->description, sizeof(pa_alsa_port_data));
+ port->priority = s->priority;
+
+ data = PA_DEVICE_PORT_DATA(port);
+ data->path = ps->paths;
+ data->setting = s;
+
+ pa_hashmap_put(*p, port->name, port);
+ }
+
+ } else {
+
+ /* We have multiple paths, so let's create a port for each
+ * one, and each of each settings */
+ *p = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ PA_LLIST_FOREACH(path, ps->paths) {
+
+ if (!path->settings || !path->settings->next) {
+ pa_device_port *port;
+ pa_alsa_port_data *data;
+
+ /* If there is no or just one setting we only need a
+ * single entry */
+
+ port = pa_device_port_new(path->name, path->description, sizeof(pa_alsa_port_data));
+ port->priority = path->priority * 100;
+
+
+ data = PA_DEVICE_PORT_DATA(port);
+ data->path = path;
+ data->setting = path->settings;
+
+ pa_hashmap_put(*p, port->name, port);
+ } else {
+ pa_alsa_setting *s;
+
+ PA_LLIST_FOREACH(s, path->settings) {
+ pa_device_port *port;
+ pa_alsa_port_data *data;
+ char *n, *d;
+
+ n = pa_sprintf_malloc("%s;%s", path->name, s->name);
+
+ if (s->description[0])
+ d = pa_sprintf_malloc(_("%s / %s"), path->description, s->description);
+ else
+ d = pa_xstrdup(path->description);
+
+ port = pa_device_port_new(n, d, sizeof(pa_alsa_port_data));
+ port->priority = path->priority * 100 + s->priority;
+
+ pa_xfree(n);
+ pa_xfree(d);
+
+ data = PA_DEVICE_PORT_DATA(port);
+ data->path = path;
+ data->setting = s;
+
+ pa_hashmap_put(*p, port->name, port);
+ }
+ }
+ }
+ }
+
+ pa_log_debug("Added %u ports", pa_hashmap_size(*p));
+}
diff --git a/src/modules/alsa/alsa-mixer.h b/src/modules/alsa/alsa-mixer.h
new file mode 100644
index 00000000..d92d3e98
--- /dev/null
+++ b/src/modules/alsa/alsa-mixer.h
@@ -0,0 +1,328 @@
+#ifndef fooalsamixerhfoo
+#define fooalsamixerhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <asoundlib.h>
+
+#include <pulse/sample.h>
+#include <pulse/mainloop-api.h>
+#include <pulse/channelmap.h>
+#include <pulse/volume.h>
+
+#include <pulsecore/llist.h>
+#include <pulsecore/rtpoll.h>
+
+typedef struct pa_alsa_fdlist pa_alsa_fdlist;
+typedef struct pa_alsa_mixer_pdata pa_alsa_mixer_pdata;
+typedef struct pa_alsa_setting pa_alsa_setting;
+typedef struct pa_alsa_option pa_alsa_option;
+typedef struct pa_alsa_element pa_alsa_element;
+typedef struct pa_alsa_path pa_alsa_path;
+typedef struct pa_alsa_path_set pa_alsa_path_set;
+typedef struct pa_alsa_mapping pa_alsa_mapping;
+typedef struct pa_alsa_profile pa_alsa_profile;
+typedef struct pa_alsa_decibel_fix pa_alsa_decibel_fix;
+typedef struct pa_alsa_profile_set pa_alsa_profile_set;
+typedef struct pa_alsa_port_data pa_alsa_port_data;
+
+#include "alsa-util.h"
+
+typedef enum pa_alsa_switch_use {
+ PA_ALSA_SWITCH_IGNORE,
+ PA_ALSA_SWITCH_MUTE, /* make this switch follow mute status */
+ PA_ALSA_SWITCH_OFF, /* set this switch to 'off' unconditionally */
+ PA_ALSA_SWITCH_ON, /* set this switch to 'on' unconditionally */
+ PA_ALSA_SWITCH_SELECT /* allow the user to select switch status through a setting */
+} pa_alsa_switch_use_t;
+
+typedef enum pa_alsa_volume_use {
+ PA_ALSA_VOLUME_IGNORE,
+ PA_ALSA_VOLUME_MERGE, /* merge this volume slider into the global volume slider */
+ PA_ALSA_VOLUME_OFF, /* set this volume to minimal unconditionally */
+ PA_ALSA_VOLUME_ZERO, /* set this volume to 0dB unconditionally */
+ PA_ALSA_VOLUME_CONSTANT /* set this volume to a constant value unconditionally */
+} pa_alsa_volume_use_t;
+
+typedef enum pa_alsa_enumeration_use {
+ PA_ALSA_ENUMERATION_IGNORE,
+ PA_ALSA_ENUMERATION_SELECT
+} pa_alsa_enumeration_use_t;
+
+typedef enum pa_alsa_required {
+ PA_ALSA_REQUIRED_IGNORE,
+ PA_ALSA_REQUIRED_SWITCH,
+ PA_ALSA_REQUIRED_VOLUME,
+ PA_ALSA_REQUIRED_ENUMERATION,
+ PA_ALSA_REQUIRED_ANY
+} pa_alsa_required_t;
+
+typedef enum pa_alsa_direction {
+ PA_ALSA_DIRECTION_ANY,
+ PA_ALSA_DIRECTION_OUTPUT,
+ PA_ALSA_DIRECTION_INPUT
+} pa_alsa_direction_t;
+
+/* A setting combines a couple of options into a single entity that
+ * may be selected. Only one setting can be active at the same
+ * time. */
+struct pa_alsa_setting {
+ pa_alsa_path *path;
+ PA_LLIST_FIELDS(pa_alsa_setting);
+
+ pa_idxset *options;
+
+ char *name;
+ char *description;
+ unsigned priority;
+};
+
+/* An option belongs to an element and refers to one enumeration item
+ * of the element is an enumeration item, or a switch status if the
+ * element is a switch item. */
+struct pa_alsa_option {
+ pa_alsa_element *element;
+ PA_LLIST_FIELDS(pa_alsa_option);
+
+ char *alsa_name;
+ int alsa_idx;
+
+ char *name;
+ char *description;
+ unsigned priority;
+
+ pa_alsa_required_t required;
+ pa_alsa_required_t required_any;
+ pa_alsa_required_t required_absent;
+};
+
+/* An element wraps one specific ALSA element. A series of elements
+ * make up a path (see below). If the element is an enumeration or switch
+ * element it may include a list of options. */
+struct pa_alsa_element {
+ pa_alsa_path *path;
+ PA_LLIST_FIELDS(pa_alsa_element);
+
+ char *alsa_name;
+ pa_alsa_direction_t direction;
+
+ pa_alsa_switch_use_t switch_use;
+ pa_alsa_volume_use_t volume_use;
+ pa_alsa_enumeration_use_t enumeration_use;
+
+ pa_alsa_required_t required;
+ pa_alsa_required_t required_any;
+ pa_alsa_required_t required_absent;
+
+ long constant_volume;
+
+ pa_bool_t override_map:1;
+ pa_bool_t direction_try_other:1;
+
+ pa_bool_t has_dB:1;
+ long min_volume, max_volume;
+ long volume_limit; /* -1 for no configured limit */
+ double min_dB, max_dB;
+
+ pa_channel_position_mask_t masks[SND_MIXER_SCHN_LAST][2];
+ unsigned n_channels;
+
+ pa_channel_position_mask_t merged_mask;
+
+ PA_LLIST_HEAD(pa_alsa_option, options);
+
+ pa_alsa_decibel_fix *db_fix;
+};
+
+/* A path wraps a series of elements into a single entity which can be
+ * used to control it as if it had a single volume slider, a single
+ * mute switch and a single list of selectable options. */
+struct pa_alsa_path {
+ pa_alsa_path_set *path_set;
+ PA_LLIST_FIELDS(pa_alsa_path);
+
+ pa_alsa_direction_t direction;
+
+ char *name;
+ char *description;
+ unsigned priority;
+
+ pa_bool_t probed:1;
+ pa_bool_t supported:1;
+ pa_bool_t has_mute:1;
+ pa_bool_t has_volume:1;
+ pa_bool_t has_dB:1;
+ /* These two are used during probing only */
+ pa_bool_t has_req_any:1;
+ pa_bool_t req_any_present:1;
+
+ long min_volume, max_volume;
+ double min_dB, max_dB;
+
+ /* This is used during parsing only, as a shortcut so that we
+ * don't have to iterate the list all the time */
+ pa_alsa_element *last_element;
+ pa_alsa_option *last_option;
+ pa_alsa_setting *last_setting;
+
+ PA_LLIST_HEAD(pa_alsa_element, elements);
+ PA_LLIST_HEAD(pa_alsa_setting, settings);
+};
+
+/* A path set is simply a set of paths that are applicable to a
+ * device */
+struct pa_alsa_path_set {
+ PA_LLIST_HEAD(pa_alsa_path, paths);
+ pa_alsa_direction_t direction;
+ pa_bool_t probed:1;
+
+ /* This is used during parsing only, as a shortcut so that we
+ * don't have to iterate the list all the time */
+ pa_alsa_path *last_path;
+};
+
+int pa_alsa_setting_select(pa_alsa_setting *s, snd_mixer_t *m);
+void pa_alsa_setting_dump(pa_alsa_setting *s);
+
+void pa_alsa_option_dump(pa_alsa_option *o);
+
+void pa_alsa_element_dump(pa_alsa_element *e);
+
+pa_alsa_path *pa_alsa_path_new(const char *fname, pa_alsa_direction_t direction);
+pa_alsa_path *pa_alsa_path_synthesize(const char *element, pa_alsa_direction_t direction);
+int pa_alsa_path_probe(pa_alsa_path *p, snd_mixer_t *m, pa_bool_t ignore_dB);
+void pa_alsa_path_dump(pa_alsa_path *p);
+int pa_alsa_path_get_volume(pa_alsa_path *p, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v);
+int pa_alsa_path_get_mute(pa_alsa_path *path, snd_mixer_t *m, pa_bool_t *muted);
+int pa_alsa_path_set_volume(pa_alsa_path *path, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v, pa_bool_t sync_volume, pa_bool_t write_to_hw);
+int pa_alsa_path_set_mute(pa_alsa_path *path, snd_mixer_t *m, pa_bool_t muted);
+int pa_alsa_path_select(pa_alsa_path *p, snd_mixer_t *m);
+void pa_alsa_path_set_callback(pa_alsa_path *p, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata);
+void pa_alsa_path_free(pa_alsa_path *p);
+
+pa_alsa_path_set *pa_alsa_path_set_new(pa_alsa_mapping *m, pa_alsa_direction_t direction);
+void pa_alsa_path_set_probe(pa_alsa_path_set *s, snd_mixer_t *m, pa_bool_t ignore_dB);
+void pa_alsa_path_set_dump(pa_alsa_path_set *s);
+void pa_alsa_path_set_set_callback(pa_alsa_path_set *ps, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata);
+void pa_alsa_path_set_free(pa_alsa_path_set *s);
+
+struct pa_alsa_mapping {
+ pa_alsa_profile_set *profile_set;
+
+ char *name;
+ char *description;
+ unsigned priority;
+ pa_alsa_direction_t direction;
+
+ pa_channel_map channel_map;
+
+ char **device_strings;
+
+ char **input_path_names;
+ char **output_path_names;
+ char **input_element; /* list of fallbacks */
+ char **output_element;
+
+ unsigned supported;
+
+ /* Temporarily used during probing */
+ snd_pcm_t *input_pcm;
+ snd_pcm_t *output_pcm;
+
+ pa_sink *sink;
+ pa_source *source;
+};
+
+struct pa_alsa_profile {
+ pa_alsa_profile_set *profile_set;
+
+ char *name;
+ char *description;
+ unsigned priority;
+
+ pa_bool_t supported:1;
+
+ char **input_mapping_names;
+ char **output_mapping_names;
+
+ pa_idxset *input_mappings;
+ pa_idxset *output_mappings;
+};
+
+struct pa_alsa_decibel_fix {
+ pa_alsa_profile_set *profile_set;
+
+ char *name; /* Alsa volume element name. */
+ long min_step;
+ long max_step;
+
+ /* An array that maps alsa volume element steps to decibels. The steps can
+ * be used as indices to this array, after substracting min_step from the
+ * real value.
+ *
+ * The values are actually stored as integers representing millibels,
+ * because that's the format the alsa API uses. */
+ long *db_values;
+};
+
+struct pa_alsa_profile_set {
+ pa_hashmap *mappings;
+ pa_hashmap *profiles;
+ pa_hashmap *decibel_fixes;
+
+ pa_bool_t auto_profiles;
+ pa_bool_t probed:1;
+};
+
+void pa_alsa_mapping_dump(pa_alsa_mapping *m);
+void pa_alsa_profile_dump(pa_alsa_profile *p);
+void pa_alsa_decibel_fix_dump(pa_alsa_decibel_fix *db_fix);
+
+pa_alsa_profile_set* pa_alsa_profile_set_new(const char *fname, const pa_channel_map *bonus);
+void pa_alsa_profile_set_probe(pa_alsa_profile_set *ps, const char *dev_id, const pa_sample_spec *ss, unsigned default_n_fragments, unsigned default_fragment_size_msec);
+void pa_alsa_profile_set_free(pa_alsa_profile_set *s);
+void pa_alsa_profile_set_dump(pa_alsa_profile_set *s);
+
+snd_mixer_t *pa_alsa_open_mixer_for_pcm(snd_pcm_t *pcm, char **ctl_device);
+
+pa_alsa_fdlist *pa_alsa_fdlist_new(void);
+void pa_alsa_fdlist_free(pa_alsa_fdlist *fdl);
+int pa_alsa_fdlist_set_mixer(pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m);
+
+/* Alternative for handling alsa mixer events in io-thread. */
+
+pa_alsa_mixer_pdata *pa_alsa_mixer_pdata_new(void);
+void pa_alsa_mixer_pdata_free(pa_alsa_mixer_pdata *pd);
+int pa_alsa_set_mixer_rtpoll(struct pa_alsa_mixer_pdata *pd, snd_mixer_t *mixer, pa_rtpoll *rtp);
+
+/* Data structure for inclusion in pa_device_port for alsa
+ * sinks/sources. This contains nothing that needs to be freed
+ * individually */
+struct pa_alsa_port_data {
+ pa_alsa_path *path;
+ pa_alsa_setting *setting;
+};
+
+void pa_alsa_add_ports(pa_hashmap **p, pa_alsa_path_set *ps);
+
+#endif
diff --git a/src/modules/alsa/alsa-sink.c b/src/modules/alsa/alsa-sink.c
new file mode 100644
index 00000000..0164040d
--- /dev/null
+++ b/src/modules/alsa/alsa-sink.c
@@ -0,0 +1,2246 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2008 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+
+#include <asoundlib.h>
+
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+#endif
+
+#include <pulse/i18n.h>
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+#include <pulse/volume.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/module.h>
+#include <pulsecore/memchunk.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/core-rtclock.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/time-smoother.h>
+
+#include <modules/reserve-wrap.h>
+
+#include "alsa-util.h"
+#include "alsa-sink.h"
+
+/* #define DEBUG_TIMING */
+
+#define DEFAULT_DEVICE "default"
+
+#define DEFAULT_TSCHED_BUFFER_USEC (2*PA_USEC_PER_SEC) /* 2s -- Overall buffer size */
+#define DEFAULT_TSCHED_WATERMARK_USEC (20*PA_USEC_PER_MSEC) /* 20ms -- Fill up when only this much is left in the buffer */
+
+#define TSCHED_WATERMARK_INC_STEP_USEC (10*PA_USEC_PER_MSEC) /* 10ms -- On underrun, increase watermark by this */
+#define TSCHED_WATERMARK_DEC_STEP_USEC (5*PA_USEC_PER_MSEC) /* 5ms -- When everything's great, decrease watermark by this */
+#define TSCHED_WATERMARK_VERIFY_AFTER_USEC (20*PA_USEC_PER_SEC) /* 20s -- How long after a drop out recheck if things are good now */
+#define TSCHED_WATERMARK_INC_THRESHOLD_USEC (0*PA_USEC_PER_MSEC) /* 0ms -- If the buffer level ever below this theshold, increase the watermark */
+#define TSCHED_WATERMARK_DEC_THRESHOLD_USEC (100*PA_USEC_PER_MSEC) /* 100ms -- If the buffer level didn't drop below this theshold in the verification time, decrease the watermark */
+
+/* Note that TSCHED_WATERMARK_INC_THRESHOLD_USEC == 0 means tht we
+ * will increase the watermark only if we hit a real underrun. */
+
+#define TSCHED_MIN_SLEEP_USEC (10*PA_USEC_PER_MSEC) /* 10ms -- Sleep at least 10ms on each iteration */
+#define TSCHED_MIN_WAKEUP_USEC (4*PA_USEC_PER_MSEC) /* 4ms -- Wakeup at least this long before the buffer runs empty*/
+
+#define SMOOTHER_WINDOW_USEC (10*PA_USEC_PER_SEC) /* 10s -- smoother windows size */
+#define SMOOTHER_ADJUST_USEC (1*PA_USEC_PER_SEC) /* 1s -- smoother adjust time */
+
+#define SMOOTHER_MIN_INTERVAL (2*PA_USEC_PER_MSEC) /* 2ms -- min smoother update interval */
+#define SMOOTHER_MAX_INTERVAL (200*PA_USEC_PER_MSEC) /* 200ms -- max smoother update interval */
+
+#define VOLUME_ACCURACY (PA_VOLUME_NORM/100) /* don't require volume adjustments to be perfectly correct. don't necessarily extend granularity in software unless the differences get greater than this level */
+
+#define DEFAULT_REWIND_SAFEGUARD_BYTES (256U) /* 1.33ms @48kHz, we'll never rewind less than this */
+#define DEFAULT_REWIND_SAFEGUARD_USEC (1330) /* 1.33ms, depending on channels/rate/sample we may rewind more than 256 above */
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_sink *sink;
+
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+
+ snd_pcm_t *pcm_handle;
+
+ pa_alsa_fdlist *mixer_fdl;
+ pa_alsa_mixer_pdata *mixer_pd;
+ snd_mixer_t *mixer_handle;
+ pa_alsa_path_set *mixer_path_set;
+ pa_alsa_path *mixer_path;
+
+ pa_cvolume hardware_volume;
+
+ uint32_t old_rate;
+
+ size_t
+ frame_size,
+ fragment_size,
+ hwbuf_size,
+ tsched_watermark,
+ hwbuf_unused,
+ min_sleep,
+ min_wakeup,
+ watermark_inc_step,
+ watermark_dec_step,
+ watermark_inc_threshold,
+ watermark_dec_threshold,
+ rewind_safeguard;
+
+ pa_usec_t watermark_dec_not_before;
+
+ pa_memchunk memchunk;
+
+ char *device_name; /* name of the PCM device */
+ char *control_device; /* name of the control device */
+
+ pa_bool_t use_mmap:1, use_tsched:1;
+
+ pa_bool_t first, after_rewind;
+
+ pa_rtpoll_item *alsa_rtpoll_item;
+
+ snd_mixer_selem_channel_id_t mixer_map[SND_MIXER_SCHN_LAST];
+
+ pa_smoother *smoother;
+ uint64_t write_count;
+ uint64_t since_start;
+ pa_usec_t smoother_interval;
+ pa_usec_t last_smoother_update;
+
+ pa_reserve_wrapper *reserve;
+ pa_hook_slot *reserve_slot;
+ pa_reserve_monitor_wrapper *monitor;
+ pa_hook_slot *monitor_slot;
+};
+
+static void userdata_free(struct userdata *u);
+
+static pa_hook_result_t reserve_cb(pa_reserve_wrapper *r, void *forced, struct userdata *u) {
+ pa_assert(r);
+ pa_assert(u);
+
+ if (pa_sink_suspend(u->sink, TRUE, PA_SUSPEND_APPLICATION) < 0)
+ return PA_HOOK_CANCEL;
+
+ return PA_HOOK_OK;
+}
+
+static void reserve_done(struct userdata *u) {
+ pa_assert(u);
+
+ if (u->reserve_slot) {
+ pa_hook_slot_free(u->reserve_slot);
+ u->reserve_slot = NULL;
+ }
+
+ if (u->reserve) {
+ pa_reserve_wrapper_unref(u->reserve);
+ u->reserve = NULL;
+ }
+}
+
+static void reserve_update(struct userdata *u) {
+ const char *description;
+ pa_assert(u);
+
+ if (!u->sink || !u->reserve)
+ return;
+
+ if ((description = pa_proplist_gets(u->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ pa_reserve_wrapper_set_application_device_name(u->reserve, description);
+}
+
+static int reserve_init(struct userdata *u, const char *dname) {
+ char *rname;
+
+ pa_assert(u);
+ pa_assert(dname);
+
+ if (u->reserve)
+ return 0;
+
+ if (pa_in_system_mode())
+ return 0;
+
+ if (!(rname = pa_alsa_get_reserve_name(dname)))
+ return 0;
+
+ /* We are resuming, try to lock the device */
+ u->reserve = pa_reserve_wrapper_get(u->core, rname);
+ pa_xfree(rname);
+
+ if (!(u->reserve))
+ return -1;
+
+ reserve_update(u);
+
+ pa_assert(!u->reserve_slot);
+ u->reserve_slot = pa_hook_connect(pa_reserve_wrapper_hook(u->reserve), PA_HOOK_NORMAL, (pa_hook_cb_t) reserve_cb, u);
+
+ return 0;
+}
+
+static pa_hook_result_t monitor_cb(pa_reserve_monitor_wrapper *w, void* busy, struct userdata *u) {
+ pa_bool_t b;
+
+ pa_assert(w);
+ pa_assert(u);
+
+ b = PA_PTR_TO_UINT(busy) && !u->reserve;
+
+ pa_sink_suspend(u->sink, b, PA_SUSPEND_APPLICATION);
+ return PA_HOOK_OK;
+}
+
+static void monitor_done(struct userdata *u) {
+ pa_assert(u);
+
+ if (u->monitor_slot) {
+ pa_hook_slot_free(u->monitor_slot);
+ u->monitor_slot = NULL;
+ }
+
+ if (u->monitor) {
+ pa_reserve_monitor_wrapper_unref(u->monitor);
+ u->monitor = NULL;
+ }
+}
+
+static int reserve_monitor_init(struct userdata *u, const char *dname) {
+ char *rname;
+
+ pa_assert(u);
+ pa_assert(dname);
+
+ if (pa_in_system_mode())
+ return 0;
+
+ if (!(rname = pa_alsa_get_reserve_name(dname)))
+ return 0;
+
+ /* We are resuming, try to lock the device */
+ u->monitor = pa_reserve_monitor_wrapper_get(u->core, rname);
+ pa_xfree(rname);
+
+ if (!(u->monitor))
+ return -1;
+
+ pa_assert(!u->monitor_slot);
+ u->monitor_slot = pa_hook_connect(pa_reserve_monitor_wrapper_hook(u->monitor), PA_HOOK_NORMAL, (pa_hook_cb_t) monitor_cb, u);
+
+ return 0;
+}
+
+static void fix_min_sleep_wakeup(struct userdata *u) {
+ size_t max_use, max_use_2;
+
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ max_use = u->hwbuf_size - u->hwbuf_unused;
+ max_use_2 = pa_frame_align(max_use/2, &u->sink->sample_spec);
+
+ u->min_sleep = pa_usec_to_bytes(TSCHED_MIN_SLEEP_USEC, &u->sink->sample_spec);
+ u->min_sleep = PA_CLAMP(u->min_sleep, u->frame_size, max_use_2);
+
+ u->min_wakeup = pa_usec_to_bytes(TSCHED_MIN_WAKEUP_USEC, &u->sink->sample_spec);
+ u->min_wakeup = PA_CLAMP(u->min_wakeup, u->frame_size, max_use_2);
+}
+
+static void fix_tsched_watermark(struct userdata *u) {
+ size_t max_use;
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ max_use = u->hwbuf_size - u->hwbuf_unused;
+
+ if (u->tsched_watermark > max_use - u->min_sleep)
+ u->tsched_watermark = max_use - u->min_sleep;
+
+ if (u->tsched_watermark < u->min_wakeup)
+ u->tsched_watermark = u->min_wakeup;
+}
+
+static void increase_watermark(struct userdata *u) {
+ size_t old_watermark;
+ pa_usec_t old_min_latency, new_min_latency;
+
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ /* First, just try to increase the watermark */
+ old_watermark = u->tsched_watermark;
+ u->tsched_watermark = PA_MIN(u->tsched_watermark * 2, u->tsched_watermark + u->watermark_inc_step);
+ fix_tsched_watermark(u);
+
+ if (old_watermark != u->tsched_watermark) {
+ pa_log_info("Increasing wakeup watermark to %0.2f ms",
+ (double) pa_bytes_to_usec(u->tsched_watermark, &u->sink->sample_spec) / PA_USEC_PER_MSEC);
+ return;
+ }
+
+ /* Hmm, we cannot increase the watermark any further, hence let's raise the latency */
+ old_min_latency = u->sink->thread_info.min_latency;
+ new_min_latency = PA_MIN(old_min_latency * 2, old_min_latency + TSCHED_WATERMARK_INC_STEP_USEC);
+ new_min_latency = PA_MIN(new_min_latency, u->sink->thread_info.max_latency);
+
+ if (old_min_latency != new_min_latency) {
+ pa_log_info("Increasing minimal latency to %0.2f ms",
+ (double) new_min_latency / PA_USEC_PER_MSEC);
+
+ pa_sink_set_latency_range_within_thread(u->sink, new_min_latency, u->sink->thread_info.max_latency);
+ }
+
+ /* When we reach this we're officialy fucked! */
+}
+
+static void decrease_watermark(struct userdata *u) {
+ size_t old_watermark;
+ pa_usec_t now;
+
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ now = pa_rtclock_now();
+
+ if (u->watermark_dec_not_before <= 0)
+ goto restart;
+
+ if (u->watermark_dec_not_before > now)
+ return;
+
+ old_watermark = u->tsched_watermark;
+
+ if (u->tsched_watermark < u->watermark_dec_step)
+ u->tsched_watermark = u->tsched_watermark / 2;
+ else
+ u->tsched_watermark = PA_MAX(u->tsched_watermark / 2, u->tsched_watermark - u->watermark_dec_step);
+
+ fix_tsched_watermark(u);
+
+ if (old_watermark != u->tsched_watermark)
+ pa_log_info("Decreasing wakeup watermark to %0.2f ms",
+ (double) pa_bytes_to_usec(u->tsched_watermark, &u->sink->sample_spec) / PA_USEC_PER_MSEC);
+
+ /* We don't change the latency range*/
+
+restart:
+ u->watermark_dec_not_before = now + TSCHED_WATERMARK_VERIFY_AFTER_USEC;
+}
+
+static void hw_sleep_time(struct userdata *u, pa_usec_t *sleep_usec, pa_usec_t*process_usec) {
+ pa_usec_t usec, wm;
+
+ pa_assert(sleep_usec);
+ pa_assert(process_usec);
+
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ usec = pa_sink_get_requested_latency_within_thread(u->sink);
+
+ if (usec == (pa_usec_t) -1)
+ usec = pa_bytes_to_usec(u->hwbuf_size, &u->sink->sample_spec);
+
+ wm = pa_bytes_to_usec(u->tsched_watermark, &u->sink->sample_spec);
+
+ if (wm > usec)
+ wm = usec/2;
+
+ *sleep_usec = usec - wm;
+ *process_usec = wm;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Buffer time: %lu ms; Sleep time: %lu ms; Process time: %lu ms",
+ (unsigned long) (usec / PA_USEC_PER_MSEC),
+ (unsigned long) (*sleep_usec / PA_USEC_PER_MSEC),
+ (unsigned long) (*process_usec / PA_USEC_PER_MSEC));
+#endif
+}
+
+static int try_recover(struct userdata *u, const char *call, int err) {
+ pa_assert(u);
+ pa_assert(call);
+ pa_assert(err < 0);
+
+ pa_log_debug("%s: %s", call, pa_alsa_strerror(err));
+
+ pa_assert(err != -EAGAIN);
+
+ if (err == -EPIPE)
+ pa_log_debug("%s: Buffer underrun!", call);
+
+ if (err == -ESTRPIPE)
+ pa_log_debug("%s: System suspended!", call);
+
+ if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) < 0) {
+ pa_log("%s: %s", call, pa_alsa_strerror(err));
+ return -1;
+ }
+
+ u->first = TRUE;
+ u->since_start = 0;
+ return 0;
+}
+
+static size_t check_left_to_play(struct userdata *u, size_t n_bytes, pa_bool_t on_timeout) {
+ size_t left_to_play;
+ pa_bool_t underrun = FALSE;
+
+ /* We use <= instead of < for this check here because an underrun
+ * only happens after the last sample was processed, not already when
+ * it is removed from the buffer. This is particularly important
+ * when block transfer is used. */
+
+ if (n_bytes <= u->hwbuf_size)
+ left_to_play = u->hwbuf_size - n_bytes;
+ else {
+
+ /* We got a dropout. What a mess! */
+ left_to_play = 0;
+ underrun = TRUE;
+
+#ifdef DEBUG_TIMING
+ PA_DEBUG_TRAP;
+#endif
+
+ if (!u->first && !u->after_rewind)
+ if (pa_log_ratelimit(PA_LOG_INFO))
+ pa_log_info("Underrun!");
+ }
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("%0.2f ms left to play; inc threshold = %0.2f ms; dec threshold = %0.2f ms",
+ (double) pa_bytes_to_usec(left_to_play, &u->sink->sample_spec) / PA_USEC_PER_MSEC,
+ (double) pa_bytes_to_usec(u->watermark_inc_threshold, &u->sink->sample_spec) / PA_USEC_PER_MSEC,
+ (double) pa_bytes_to_usec(u->watermark_dec_threshold, &u->sink->sample_spec) / PA_USEC_PER_MSEC);
+#endif
+
+ if (u->use_tsched) {
+ pa_bool_t reset_not_before = TRUE;
+
+ if (!u->first && !u->after_rewind) {
+ if (underrun || left_to_play < u->watermark_inc_threshold)
+ increase_watermark(u);
+ else if (left_to_play > u->watermark_dec_threshold) {
+ reset_not_before = FALSE;
+
+ /* We decrease the watermark only if have actually
+ * been woken up by a timeout. If something else woke
+ * us up it's too easy to fulfill the deadlines... */
+
+ if (on_timeout)
+ decrease_watermark(u);
+ }
+ }
+
+ if (reset_not_before)
+ u->watermark_dec_not_before = 0;
+ }
+
+ return left_to_play;
+}
+
+static int mmap_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled, pa_bool_t on_timeout) {
+ pa_bool_t work_done = FALSE;
+ pa_usec_t max_sleep_usec = 0, process_usec = 0;
+ size_t left_to_play;
+ unsigned j = 0;
+
+ pa_assert(u);
+ pa_sink_assert_ref(u->sink);
+
+ if (u->use_tsched)
+ hw_sleep_time(u, &max_sleep_usec, &process_usec);
+
+ for (;;) {
+ snd_pcm_sframes_t n;
+ size_t n_bytes;
+ int r;
+ pa_bool_t after_avail = TRUE;
+
+ /* First we determine how many samples are missing to fill the
+ * buffer up to 100% */
+
+ if (PA_UNLIKELY((n = pa_alsa_safe_avail(u->pcm_handle, u->hwbuf_size, &u->sink->sample_spec)) < 0)) {
+
+ if ((r = try_recover(u, "snd_pcm_avail", (int) n)) == 0)
+ continue;
+
+ return r;
+ }
+
+ n_bytes = (size_t) n * u->frame_size;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("avail: %lu", (unsigned long) n_bytes);
+#endif
+
+ left_to_play = check_left_to_play(u, n_bytes, on_timeout);
+ on_timeout = FALSE;
+
+ if (u->use_tsched)
+
+ /* We won't fill up the playback buffer before at least
+ * half the sleep time is over because otherwise we might
+ * ask for more data from the clients then they expect. We
+ * need to guarantee that clients only have to keep around
+ * a single hw buffer length. */
+
+ if (!polled &&
+ pa_bytes_to_usec(left_to_play, &u->sink->sample_spec) > process_usec+max_sleep_usec/2) {
+#ifdef DEBUG_TIMING
+ pa_log_debug("Not filling up, because too early.");
+#endif
+ break;
+ }
+
+ if (PA_UNLIKELY(n_bytes <= u->hwbuf_unused)) {
+
+ if (polled)
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(u->pcm_handle);
+ pa_log(_("ALSA woke us up to write new data to the device, but there was actually nothing to write!\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers.\n"
+ "We were woken up with POLLOUT set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail."),
+ pa_strnull(dn));
+ pa_xfree(dn);
+ } PA_ONCE_END;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Not filling up, because not necessary.");
+#endif
+ break;
+ }
+
+
+ if (++j > 10) {
+#ifdef DEBUG_TIMING
+ pa_log_debug("Not filling up, because already too many iterations.");
+#endif
+
+ break;
+ }
+
+ n_bytes -= u->hwbuf_unused;
+ polled = FALSE;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Filling up");
+#endif
+
+ for (;;) {
+ pa_memchunk chunk;
+ void *p;
+ int err;
+ const snd_pcm_channel_area_t *areas;
+ snd_pcm_uframes_t offset, frames;
+ snd_pcm_sframes_t sframes;
+
+ frames = (snd_pcm_uframes_t) (n_bytes / u->frame_size);
+/* pa_log_debug("%lu frames to write", (unsigned long) frames); */
+
+ if (PA_UNLIKELY((err = pa_alsa_safe_mmap_begin(u->pcm_handle, &areas, &offset, &frames, u->hwbuf_size, &u->sink->sample_spec)) < 0)) {
+
+ if (!after_avail && err == -EAGAIN)
+ break;
+
+ if ((r = try_recover(u, "snd_pcm_mmap_begin", err)) == 0)
+ continue;
+
+ return r;
+ }
+
+ /* Make sure that if these memblocks need to be copied they will fit into one slot */
+ if (frames > pa_mempool_block_size_max(u->sink->core->mempool)/u->frame_size)
+ frames = pa_mempool_block_size_max(u->sink->core->mempool)/u->frame_size;
+
+ if (!after_avail && frames == 0)
+ break;
+
+ pa_assert(frames > 0);
+ after_avail = FALSE;
+
+ /* Check these are multiples of 8 bit */
+ pa_assert((areas[0].first & 7) == 0);
+ pa_assert((areas[0].step & 7)== 0);
+
+ /* We assume a single interleaved memory buffer */
+ pa_assert((areas[0].first >> 3) == 0);
+ pa_assert((areas[0].step >> 3) == u->frame_size);
+
+ p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
+
+ chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, TRUE);
+ chunk.length = pa_memblock_get_length(chunk.memblock);
+ chunk.index = 0;
+
+ pa_sink_render_into_full(u->sink, &chunk);
+ pa_memblock_unref_fixed(chunk.memblock);
+
+ if (PA_UNLIKELY((sframes = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0)) {
+
+ if (!after_avail && (int) sframes == -EAGAIN)
+ break;
+
+ if ((r = try_recover(u, "snd_pcm_mmap_commit", (int) sframes)) == 0)
+ continue;
+
+ return r;
+ }
+
+ work_done = TRUE;
+
+ u->write_count += frames * u->frame_size;
+ u->since_start += frames * u->frame_size;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Wrote %lu bytes (of possible %lu bytes)", (unsigned long) (frames * u->frame_size), (unsigned long) n_bytes);
+#endif
+
+ if ((size_t) frames * u->frame_size >= n_bytes)
+ break;
+
+ n_bytes -= (size_t) frames * u->frame_size;
+ }
+ }
+
+ if (u->use_tsched) {
+ *sleep_usec = pa_bytes_to_usec(left_to_play, &u->sink->sample_spec);
+
+ if (*sleep_usec > process_usec)
+ *sleep_usec -= process_usec;
+ else
+ *sleep_usec = 0;
+ } else
+ *sleep_usec = 0;
+
+ return work_done ? 1 : 0;
+}
+
+static int unix_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled, pa_bool_t on_timeout) {
+ pa_bool_t work_done = FALSE;
+ pa_usec_t max_sleep_usec = 0, process_usec = 0;
+ size_t left_to_play;
+ unsigned j = 0;
+
+ pa_assert(u);
+ pa_sink_assert_ref(u->sink);
+
+ if (u->use_tsched)
+ hw_sleep_time(u, &max_sleep_usec, &process_usec);
+
+ for (;;) {
+ snd_pcm_sframes_t n;
+ size_t n_bytes;
+ int r;
+ pa_bool_t after_avail = TRUE;
+
+ if (PA_UNLIKELY((n = pa_alsa_safe_avail(u->pcm_handle, u->hwbuf_size, &u->sink->sample_spec)) < 0)) {
+
+ if ((r = try_recover(u, "snd_pcm_avail", (int) n)) == 0)
+ continue;
+
+ return r;
+ }
+
+ n_bytes = (size_t) n * u->frame_size;
+ left_to_play = check_left_to_play(u, n_bytes, on_timeout);
+ on_timeout = FALSE;
+
+ if (u->use_tsched)
+
+ /* We won't fill up the playback buffer before at least
+ * half the sleep time is over because otherwise we might
+ * ask for more data from the clients then they expect. We
+ * need to guarantee that clients only have to keep around
+ * a single hw buffer length. */
+
+ if (!polled &&
+ pa_bytes_to_usec(left_to_play, &u->sink->sample_spec) > process_usec+max_sleep_usec/2)
+ break;
+
+ if (PA_UNLIKELY(n_bytes <= u->hwbuf_unused)) {
+
+ if (polled)
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(u->pcm_handle);
+ pa_log(_("ALSA woke us up to write new data to the device, but there was actually nothing to write!\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers.\n"
+ "We were woken up with POLLOUT set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail."),
+ pa_strnull(dn));
+ pa_xfree(dn);
+ } PA_ONCE_END;
+
+ break;
+ }
+
+ if (++j > 10) {
+#ifdef DEBUG_TIMING
+ pa_log_debug("Not filling up, because already too many iterations.");
+#endif
+
+ break;
+ }
+
+ n_bytes -= u->hwbuf_unused;
+ polled = FALSE;
+
+ for (;;) {
+ snd_pcm_sframes_t frames;
+ void *p;
+
+/* pa_log_debug("%lu frames to write", (unsigned long) frames); */
+
+ if (u->memchunk.length <= 0)
+ pa_sink_render(u->sink, n_bytes, &u->memchunk);
+
+ pa_assert(u->memchunk.length > 0);
+
+ frames = (snd_pcm_sframes_t) (u->memchunk.length / u->frame_size);
+
+ if (frames > (snd_pcm_sframes_t) (n_bytes/u->frame_size))
+ frames = (snd_pcm_sframes_t) (n_bytes/u->frame_size);
+
+ p = pa_memblock_acquire(u->memchunk.memblock);
+ frames = snd_pcm_writei(u->pcm_handle, (const uint8_t*) p + u->memchunk.index, (snd_pcm_uframes_t) frames);
+ pa_memblock_release(u->memchunk.memblock);
+
+ if (PA_UNLIKELY(frames < 0)) {
+
+ if (!after_avail && (int) frames == -EAGAIN)
+ break;
+
+ if ((r = try_recover(u, "snd_pcm_writei", (int) frames)) == 0)
+ continue;
+
+ return r;
+ }
+
+ if (!after_avail && frames == 0)
+ break;
+
+ pa_assert(frames > 0);
+ after_avail = FALSE;
+
+ u->memchunk.index += (size_t) frames * u->frame_size;
+ u->memchunk.length -= (size_t) frames * u->frame_size;
+
+ if (u->memchunk.length <= 0) {
+ pa_memblock_unref(u->memchunk.memblock);
+ pa_memchunk_reset(&u->memchunk);
+ }
+
+ work_done = TRUE;
+
+ u->write_count += frames * u->frame_size;
+ u->since_start += frames * u->frame_size;
+
+/* pa_log_debug("wrote %lu frames", (unsigned long) frames); */
+
+ if ((size_t) frames * u->frame_size >= n_bytes)
+ break;
+
+ n_bytes -= (size_t) frames * u->frame_size;
+ }
+ }
+
+ if (u->use_tsched) {
+ *sleep_usec = pa_bytes_to_usec(left_to_play, &u->sink->sample_spec);
+
+ if (*sleep_usec > process_usec)
+ *sleep_usec -= process_usec;
+ else
+ *sleep_usec = 0;
+ } else
+ *sleep_usec = 0;
+
+ return work_done ? 1 : 0;
+}
+
+static void update_smoother(struct userdata *u) {
+ snd_pcm_sframes_t delay = 0;
+ int64_t position;
+ int err;
+ pa_usec_t now1 = 0, now2;
+ snd_pcm_status_t *status;
+
+ snd_pcm_status_alloca(&status);
+
+ pa_assert(u);
+ pa_assert(u->pcm_handle);
+
+ /* Let's update the time smoother */
+
+ if (PA_UNLIKELY((err = pa_alsa_safe_delay(u->pcm_handle, &delay, u->hwbuf_size, &u->sink->sample_spec, FALSE)) < 0)) {
+ pa_log_warn("Failed to query DSP status data: %s", pa_alsa_strerror(err));
+ return;
+ }
+
+ if (PA_UNLIKELY((err = snd_pcm_status(u->pcm_handle, status)) < 0))
+ pa_log_warn("Failed to get timestamp: %s", pa_alsa_strerror(err));
+ else {
+ snd_htimestamp_t htstamp = { 0, 0 };
+ snd_pcm_status_get_htstamp(status, &htstamp);
+ now1 = pa_timespec_load(&htstamp);
+ }
+
+ /* Hmm, if the timestamp is 0, then it wasn't set and we take the current time */
+ if (now1 <= 0)
+ now1 = pa_rtclock_now();
+
+ /* check if the time since the last update is bigger than the interval */
+ if (u->last_smoother_update > 0)
+ if (u->last_smoother_update + u->smoother_interval > now1)
+ return;
+
+ position = (int64_t) u->write_count - ((int64_t) delay * (int64_t) u->frame_size);
+
+ if (PA_UNLIKELY(position < 0))
+ position = 0;
+
+ now2 = pa_bytes_to_usec((uint64_t) position, &u->sink->sample_spec);
+
+ pa_smoother_put(u->smoother, now1, now2);
+
+ u->last_smoother_update = now1;
+ /* exponentially increase the update interval up to the MAX limit */
+ u->smoother_interval = PA_MIN (u->smoother_interval * 2, SMOOTHER_MAX_INTERVAL);
+}
+
+static pa_usec_t sink_get_latency(struct userdata *u) {
+ pa_usec_t r;
+ int64_t delay;
+ pa_usec_t now1, now2;
+
+ pa_assert(u);
+
+ now1 = pa_rtclock_now();
+ now2 = pa_smoother_get(u->smoother, now1);
+
+ delay = (int64_t) pa_bytes_to_usec(u->write_count, &u->sink->sample_spec) - (int64_t) now2;
+
+ r = delay >= 0 ? (pa_usec_t) delay : 0;
+
+ if (u->memchunk.memblock)
+ r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
+
+ return r;
+}
+
+static int build_pollfd(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(u->pcm_handle);
+
+ if (u->alsa_rtpoll_item)
+ pa_rtpoll_item_free(u->alsa_rtpoll_item);
+
+ if (!(u->alsa_rtpoll_item = pa_alsa_build_pollfd(u->pcm_handle, u->rtpoll)))
+ return -1;
+
+ return 0;
+}
+
+/* Called from IO context */
+static int suspend(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(u->pcm_handle);
+
+ pa_smoother_pause(u->smoother, pa_rtclock_now());
+
+ /* Let's suspend -- we don't call snd_pcm_drain() here since that might
+ * take awfully long with our long buffer sizes today. */
+ snd_pcm_close(u->pcm_handle);
+ u->pcm_handle = NULL;
+
+ if (u->alsa_rtpoll_item) {
+ pa_rtpoll_item_free(u->alsa_rtpoll_item);
+ u->alsa_rtpoll_item = NULL;
+ }
+
+ /* We reset max_rewind/max_request here to make sure that while we
+ * are suspended the old max_request/max_rewind values set before
+ * the suspend can influence the per-stream buffer of newly
+ * created streams, without their requirements having any
+ * influence on them. */
+ pa_sink_set_max_rewind_within_thread(u->sink, 0);
+ pa_sink_set_max_request_within_thread(u->sink, 0);
+
+ pa_log_info("Device suspended...");
+
+ return 0;
+}
+
+/* Called from IO context */
+static int update_sw_params(struct userdata *u) {
+ snd_pcm_uframes_t avail_min;
+ int err;
+
+ pa_assert(u);
+
+ /* Use the full buffer if noone asked us for anything specific */
+ u->hwbuf_unused = 0;
+
+ if (u->use_tsched) {
+ pa_usec_t latency;
+
+ if ((latency = pa_sink_get_requested_latency_within_thread(u->sink)) != (pa_usec_t) -1) {
+ size_t b;
+
+ pa_log_debug("Latency set to %0.2fms", (double) latency / PA_USEC_PER_MSEC);
+
+ b = pa_usec_to_bytes(latency, &u->sink->sample_spec);
+
+ /* We need at least one sample in our buffer */
+
+ if (PA_UNLIKELY(b < u->frame_size))
+ b = u->frame_size;
+
+ u->hwbuf_unused = PA_LIKELY(b < u->hwbuf_size) ? (u->hwbuf_size - b) : 0;
+ }
+
+ fix_min_sleep_wakeup(u);
+ fix_tsched_watermark(u);
+ }
+
+ pa_log_debug("hwbuf_unused=%lu", (unsigned long) u->hwbuf_unused);
+
+ /* We need at last one frame in the used part of the buffer */
+ avail_min = (snd_pcm_uframes_t) u->hwbuf_unused / u->frame_size + 1;
+
+ if (u->use_tsched) {
+ pa_usec_t sleep_usec, process_usec;
+
+ hw_sleep_time(u, &sleep_usec, &process_usec);
+ avail_min += pa_usec_to_bytes(sleep_usec, &u->sink->sample_spec) / u->frame_size;
+ }
+
+ pa_log_debug("setting avail_min=%lu", (unsigned long) avail_min);
+
+ if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min, !u->use_tsched)) < 0) {
+ pa_log("Failed to set software parameters: %s", pa_alsa_strerror(err));
+ return err;
+ }
+
+ pa_sink_set_max_request_within_thread(u->sink, u->hwbuf_size - u->hwbuf_unused);
+ if (pa_alsa_pcm_is_hw(u->pcm_handle))
+ pa_sink_set_max_rewind_within_thread(u->sink, u->hwbuf_size);
+ else {
+ pa_log_info("Disabling rewind_within_thread for device %s", u->device_name);
+ pa_sink_set_max_rewind_within_thread(u->sink, 0);
+ }
+
+ return 0;
+}
+
+/* Called from IO context */
+static int unsuspend(struct userdata *u) {
+ pa_sample_spec ss;
+ int err;
+ pa_bool_t b, d;
+ snd_pcm_uframes_t period_size, buffer_size;
+
+ pa_assert(u);
+ pa_assert(!u->pcm_handle);
+
+ pa_log_info("Trying resume...");
+
+ if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_PLAYBACK,
+ SND_PCM_NONBLOCK|
+ SND_PCM_NO_AUTO_RESAMPLE|
+ SND_PCM_NO_AUTO_CHANNELS|
+ SND_PCM_NO_AUTO_FORMAT)) < 0) {
+ pa_log("Error opening PCM device %s: %s", u->device_name, pa_alsa_strerror(err));
+ goto fail;
+ }
+
+ ss = u->sink->sample_spec;
+ period_size = u->fragment_size / u->frame_size;
+ buffer_size = u->hwbuf_size / u->frame_size;
+ b = u->use_mmap;
+ d = u->use_tsched;
+
+ if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &period_size, &buffer_size, 0, &b, &d, TRUE)) < 0) {
+ pa_log("Failed to set hardware parameters: %s", pa_alsa_strerror(err));
+ goto fail;
+ }
+
+ if (b != u->use_mmap || d != u->use_tsched) {
+ pa_log_warn("Resume failed, couldn't get original access mode.");
+ goto fail;
+ }
+
+ if (!pa_sample_spec_equal(&ss, &u->sink->sample_spec)) {
+ pa_log_warn("Resume failed, couldn't restore original sample settings.");
+ goto fail;
+ }
+
+ if (period_size*u->frame_size != u->fragment_size ||
+ buffer_size*u->frame_size != u->hwbuf_size) {
+ pa_log_warn("Resume failed, couldn't restore original fragment settings. (Old: %lu/%lu, New %lu/%lu)",
+ (unsigned long) u->hwbuf_size, (unsigned long) u->fragment_size,
+ (unsigned long) (buffer_size*u->frame_size), (unsigned long) (period_size*u->frame_size));
+ goto fail;
+ }
+
+ if (update_sw_params(u) < 0)
+ goto fail;
+
+ if (build_pollfd(u) < 0)
+ goto fail;
+
+ u->write_count = 0;
+ pa_smoother_reset(u->smoother, pa_rtclock_now(), TRUE);
+ u->smoother_interval = SMOOTHER_MIN_INTERVAL;
+ u->last_smoother_update = 0;
+
+ u->first = TRUE;
+ u->since_start = 0;
+
+ pa_log_info("Resumed successfully...");
+
+ return 0;
+
+fail:
+ if (u->pcm_handle) {
+ snd_pcm_close(u->pcm_handle);
+ u->pcm_handle = NULL;
+ }
+
+ return -PA_ERR_IO;
+}
+
+/* Called from IO context */
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_FINISH_MOVE:
+ case PA_SINK_MESSAGE_ADD_INPUT: {
+ pa_sink_input *i = PA_SINK_INPUT(data);
+ int r = 0;
+
+ if (PA_LIKELY(!pa_sink_input_is_passthrough(i)))
+ break;
+
+ u->old_rate = u->sink->sample_spec.rate;
+
+ /* Passthrough format, see if we need to reset sink sample rate */
+ if (u->sink->sample_spec.rate == i->thread_info.sample_spec.rate)
+ break;
+
+ /* .. we do */
+ if ((r = suspend(u)) < 0)
+ return r;
+
+ u->sink->sample_spec.rate = i->thread_info.sample_spec.rate;
+
+ if ((r = unsuspend(u)) < 0)
+ return r;
+
+ break;
+ }
+
+ case PA_SINK_MESSAGE_START_MOVE:
+ case PA_SINK_MESSAGE_REMOVE_INPUT: {
+ pa_sink_input *i = PA_SINK_INPUT(data);
+ int r = 0;
+
+ if (PA_LIKELY(!pa_sink_input_is_passthrough(i)))
+ break;
+
+ /* Passthrough format, see if we need to reset sink sample rate */
+ if (u->sink->sample_spec.rate == u->old_rate)
+ break;
+
+ /* .. we do */
+ if ((r = suspend(u)) < 0)
+ return r;
+
+ u->sink->sample_spec.rate = u->old_rate;
+
+ if ((r = unsuspend(u)) < 0)
+ return r;
+
+ break;
+ }
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ pa_usec_t r = 0;
+
+ if (u->pcm_handle)
+ r = sink_get_latency(u);
+
+ *((pa_usec_t*) data) = r;
+
+ return 0;
+ }
+
+ case PA_SINK_MESSAGE_SET_STATE:
+
+ switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
+
+ case PA_SINK_SUSPENDED: {
+ int r;
+
+ pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
+
+ if ((r = suspend(u)) < 0)
+ return r;
+
+ break;
+ }
+
+ case PA_SINK_IDLE:
+ case PA_SINK_RUNNING: {
+ int r;
+
+ if (u->sink->thread_info.state == PA_SINK_INIT) {
+ if (build_pollfd(u) < 0)
+ return -PA_ERR_IO;
+ }
+
+ if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
+ if ((r = unsuspend(u)) < 0)
+ return r;
+ }
+
+ break;
+ }
+
+ case PA_SINK_UNLINKED:
+ case PA_SINK_INIT:
+ case PA_SINK_INVALID_STATE:
+ ;
+ }
+
+ break;
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from main context */
+static int sink_set_state_cb(pa_sink *s, pa_sink_state_t new_state) {
+ pa_sink_state_t old_state;
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ old_state = pa_sink_get_state(u->sink);
+
+ if (PA_SINK_IS_OPENED(old_state) && new_state == PA_SINK_SUSPENDED)
+ reserve_done(u);
+ else if (old_state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(new_state))
+ if (reserve_init(u, u->device_name) < 0)
+ return -PA_ERR_BUSY;
+
+ return 0;
+}
+
+static int ctl_mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
+ struct userdata *u = snd_mixer_elem_get_callback_private(elem);
+
+ pa_assert(u);
+ pa_assert(u->mixer_handle);
+
+ if (mask == SND_CTL_EVENT_MASK_REMOVE)
+ return 0;
+
+ if (u->sink->suspend_cause & PA_SUSPEND_SESSION)
+ return 0;
+
+ if (mask & SND_CTL_EVENT_MASK_VALUE) {
+ pa_sink_get_volume(u->sink, TRUE);
+ pa_sink_get_mute(u->sink, TRUE);
+ }
+
+ return 0;
+}
+
+static int io_mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
+ struct userdata *u = snd_mixer_elem_get_callback_private(elem);
+
+ pa_assert(u);
+ pa_assert(u->mixer_handle);
+
+ if (mask == SND_CTL_EVENT_MASK_REMOVE)
+ return 0;
+
+ if (u->sink->suspend_cause & PA_SUSPEND_SESSION)
+ return 0;
+
+ if (mask & SND_CTL_EVENT_MASK_VALUE)
+ pa_sink_update_volume_and_mute(u->sink);
+
+ return 0;
+}
+
+static void sink_get_volume_cb(pa_sink *s) {
+ struct userdata *u = s->userdata;
+ pa_cvolume r;
+ char vol_str_pcnt[PA_CVOLUME_SNPRINT_MAX];
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+
+ if (pa_alsa_path_get_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r) < 0)
+ return;
+
+ /* Shift down by the base volume, so that 0dB becomes maximum volume */
+ pa_sw_cvolume_multiply_scalar(&r, &r, s->base_volume);
+
+ pa_log_debug("Read hardware volume: %s", pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &r));
+
+ if (u->mixer_path->has_dB) {
+ char vol_str_db[PA_SW_CVOLUME_SNPRINT_DB_MAX];
+
+ pa_log_debug(" in dB: %s", pa_sw_cvolume_snprint_dB(vol_str_db, sizeof(vol_str_db), &r));
+ }
+
+ if (pa_cvolume_equal(&u->hardware_volume, &r))
+ return;
+
+ s->real_volume = u->hardware_volume = r;
+
+ /* Hmm, so the hardware volume changed, let's reset our software volume */
+ if (u->mixer_path->has_dB)
+ pa_sink_set_soft_volume(s, NULL);
+}
+
+static void sink_set_volume_cb(pa_sink *s) {
+ struct userdata *u = s->userdata;
+ pa_cvolume r;
+ char vol_str_pcnt[PA_CVOLUME_SNPRINT_MAX];
+ pa_bool_t sync_volume = !!(s->flags & PA_SINK_SYNC_VOLUME);
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+
+ /* Shift up by the base volume */
+ pa_sw_cvolume_divide_scalar(&r, &s->real_volume, s->base_volume);
+
+ if (pa_alsa_path_set_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r, sync_volume, !sync_volume) < 0)
+ return;
+
+ /* Shift down by the base volume, so that 0dB becomes maximum volume */
+ pa_sw_cvolume_multiply_scalar(&r, &r, s->base_volume);
+
+ u->hardware_volume = r;
+
+ if (u->mixer_path->has_dB) {
+ pa_cvolume new_soft_volume;
+ pa_bool_t accurate_enough;
+ char vol_str_db[PA_SW_CVOLUME_SNPRINT_DB_MAX];
+
+ /* Match exactly what the user requested by software */
+ pa_sw_cvolume_divide(&new_soft_volume, &s->real_volume, &u->hardware_volume);
+
+ /* If the adjustment to do in software is only minimal we
+ * can skip it. That saves us CPU at the expense of a bit of
+ * accuracy */
+ accurate_enough =
+ (pa_cvolume_min(&new_soft_volume) >= (PA_VOLUME_NORM - VOLUME_ACCURACY)) &&
+ (pa_cvolume_max(&new_soft_volume) <= (PA_VOLUME_NORM + VOLUME_ACCURACY));
+
+ pa_log_debug("Requested volume: %s", pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &s->real_volume));
+ pa_log_debug(" in dB: %s", pa_sw_cvolume_snprint_dB(vol_str_db, sizeof(vol_str_db), &s->real_volume));
+ pa_log_debug("Got hardware volume: %s", pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &u->hardware_volume));
+ pa_log_debug(" in dB: %s", pa_sw_cvolume_snprint_dB(vol_str_db, sizeof(vol_str_db), &u->hardware_volume));
+ pa_log_debug("Calculated software volume: %s (accurate-enough=%s)",
+ pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &new_soft_volume),
+ pa_yes_no(accurate_enough));
+ pa_log_debug(" in dB: %s", pa_sw_cvolume_snprint_dB(vol_str_db, sizeof(vol_str_db), &new_soft_volume));
+
+ if (!accurate_enough)
+ s->soft_volume = new_soft_volume;
+
+ } else {
+ pa_log_debug("Wrote hardware volume: %s", pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &r));
+
+ /* We can't match exactly what the user requested, hence let's
+ * at least tell the user about it */
+
+ s->real_volume = r;
+ }
+}
+
+static void sink_write_volume_cb(pa_sink *s) {
+ struct userdata *u = s->userdata;
+ pa_cvolume hw_vol = s->thread_info.current_hw_volume;
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+ pa_assert(s->flags & PA_SINK_SYNC_VOLUME);
+
+ /* Shift up by the base volume */
+ pa_sw_cvolume_divide_scalar(&hw_vol, &hw_vol, s->base_volume);
+
+ if (pa_alsa_path_set_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &hw_vol, TRUE, TRUE) < 0)
+ pa_log_error("Writing HW volume failed");
+ else {
+ pa_cvolume tmp_vol;
+ pa_bool_t accurate_enough;
+
+ /* Shift down by the base volume, so that 0dB becomes maximum volume */
+ pa_sw_cvolume_multiply_scalar(&hw_vol, &hw_vol, s->base_volume);
+
+ pa_sw_cvolume_divide(&tmp_vol, &hw_vol, &s->thread_info.current_hw_volume);
+ accurate_enough =
+ (pa_cvolume_min(&tmp_vol) >= (PA_VOLUME_NORM - VOLUME_ACCURACY)) &&
+ (pa_cvolume_max(&tmp_vol) <= (PA_VOLUME_NORM + VOLUME_ACCURACY));
+
+ if (!accurate_enough) {
+ union {
+ char db[2][PA_SW_CVOLUME_SNPRINT_DB_MAX];
+ char pcnt[2][PA_CVOLUME_SNPRINT_MAX];
+ } vol;
+
+ pa_log_debug("Written HW volume did not match with the request: %s (request) != %s",
+ pa_cvolume_snprint(vol.pcnt[0], sizeof(vol.pcnt[0]), &s->thread_info.current_hw_volume),
+ pa_cvolume_snprint(vol.pcnt[1], sizeof(vol.pcnt[1]), &hw_vol));
+ pa_log_debug(" in dB: %s (request) != %s",
+ pa_sw_cvolume_snprint_dB(vol.db[0], sizeof(vol.db[0]), &s->thread_info.current_hw_volume),
+ pa_sw_cvolume_snprint_dB(vol.db[1], sizeof(vol.db[1]), &hw_vol));
+ }
+ }
+}
+
+static void sink_get_mute_cb(pa_sink *s) {
+ struct userdata *u = s->userdata;
+ pa_bool_t b;
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+
+ if (pa_alsa_path_get_mute(u->mixer_path, u->mixer_handle, &b) < 0)
+ return;
+
+ s->muted = b;
+}
+
+static void sink_set_mute_cb(pa_sink *s) {
+ struct userdata *u = s->userdata;
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+
+ pa_alsa_path_set_mute(u->mixer_path, u->mixer_handle, s->muted);
+}
+
+static int sink_set_port_cb(pa_sink *s, pa_device_port *p) {
+ struct userdata *u = s->userdata;
+ pa_alsa_port_data *data;
+
+ pa_assert(u);
+ pa_assert(p);
+ pa_assert(u->mixer_handle);
+
+ data = PA_DEVICE_PORT_DATA(p);
+
+ pa_assert_se(u->mixer_path = data->path);
+ pa_alsa_path_select(u->mixer_path, u->mixer_handle);
+
+ if (u->mixer_path->has_volume && u->mixer_path->has_dB) {
+ s->base_volume = pa_sw_volume_from_dB(-u->mixer_path->max_dB);
+ s->n_volume_steps = PA_VOLUME_NORM+1;
+
+ pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(s->base_volume));
+ } else {
+ s->base_volume = PA_VOLUME_NORM;
+ s->n_volume_steps = u->mixer_path->max_volume - u->mixer_path->min_volume + 1;
+ }
+
+ if (data->setting)
+ pa_alsa_setting_select(data->setting, u->mixer_handle);
+
+ if (s->set_mute)
+ s->set_mute(s);
+ if (s->set_volume)
+ s->set_volume(s);
+
+ return 0;
+}
+
+static void sink_update_requested_latency_cb(pa_sink *s) {
+ struct userdata *u = s->userdata;
+ size_t before;
+ pa_assert(u);
+ pa_assert(u->use_tsched); /* only when timer scheduling is used
+ * we can dynamically adjust the
+ * latency */
+
+ if (!u->pcm_handle)
+ return;
+
+ before = u->hwbuf_unused;
+ update_sw_params(u);
+
+ /* Let's check whether we now use only a smaller part of the
+ buffer then before. If so, we need to make sure that subsequent
+ rewinds are relative to the new maximum fill level and not to the
+ current fill level. Thus, let's do a full rewind once, to clear
+ things up. */
+
+ if (u->hwbuf_unused > before) {
+ pa_log_debug("Requesting rewind due to latency change.");
+ pa_sink_request_rewind(s, (size_t) -1);
+ }
+}
+
+static int process_rewind(struct userdata *u) {
+ snd_pcm_sframes_t unused;
+ size_t rewind_nbytes, unused_nbytes, limit_nbytes;
+ pa_assert(u);
+
+ /* Figure out how much we shall rewind and reset the counter */
+ rewind_nbytes = u->sink->thread_info.rewind_nbytes;
+
+ pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
+
+ if (PA_UNLIKELY((unused = pa_alsa_safe_avail(u->pcm_handle, u->hwbuf_size, &u->sink->sample_spec)) < 0)) {
+ pa_log("snd_pcm_avail() failed: %s", pa_alsa_strerror((int) unused));
+ return -1;
+ }
+
+ unused_nbytes = (size_t) unused * u->frame_size;
+
+ /* make sure rewind doesn't go too far, can cause issues with DMAs */
+ unused_nbytes += u->rewind_safeguard;
+
+ if (u->hwbuf_size > unused_nbytes)
+ limit_nbytes = u->hwbuf_size - unused_nbytes;
+ else
+ limit_nbytes = 0;
+
+ if (rewind_nbytes > limit_nbytes)
+ rewind_nbytes = limit_nbytes;
+
+ if (rewind_nbytes > 0) {
+ snd_pcm_sframes_t in_frames, out_frames;
+
+ pa_log_debug("Limited to %lu bytes.", (unsigned long) rewind_nbytes);
+
+ in_frames = (snd_pcm_sframes_t) (rewind_nbytes / u->frame_size);
+ pa_log_debug("before: %lu", (unsigned long) in_frames);
+ if ((out_frames = snd_pcm_rewind(u->pcm_handle, (snd_pcm_uframes_t) in_frames)) < 0) {
+ pa_log("snd_pcm_rewind() failed: %s", pa_alsa_strerror((int) out_frames));
+ if (try_recover(u, "process_rewind", out_frames) < 0)
+ return -1;
+ out_frames = 0;
+ }
+
+ pa_log_debug("after: %lu", (unsigned long) out_frames);
+
+ rewind_nbytes = (size_t) out_frames * u->frame_size;
+
+ if (rewind_nbytes <= 0)
+ pa_log_info("Tried rewind, but was apparently not possible.");
+ else {
+ u->write_count -= rewind_nbytes;
+ pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
+ pa_sink_process_rewind(u->sink, rewind_nbytes);
+
+ u->after_rewind = TRUE;
+ return 0;
+ }
+ } else
+ pa_log_debug("Mhmm, actually there is nothing to rewind.");
+
+ pa_sink_process_rewind(u->sink, 0);
+ return 0;
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+ unsigned short revents = 0;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority);
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ for (;;) {
+ int ret;
+ pa_usec_t rtpoll_sleep = 0;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Loop");
+#endif
+
+ /* Render some data and write it to the dsp */
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+ int work_done;
+ pa_usec_t sleep_usec = 0;
+ pa_bool_t on_timeout = pa_rtpoll_timer_elapsed(u->rtpoll);
+
+ if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
+ if (process_rewind(u) < 0)
+ goto fail;
+
+ if (u->use_mmap)
+ work_done = mmap_write(u, &sleep_usec, revents & POLLOUT, on_timeout);
+ else
+ work_done = unix_write(u, &sleep_usec, revents & POLLOUT, on_timeout);
+
+ if (work_done < 0)
+ goto fail;
+
+/* pa_log_debug("work_done = %i", work_done); */
+
+ if (work_done) {
+
+ if (u->first) {
+ pa_log_info("Starting playback.");
+ snd_pcm_start(u->pcm_handle);
+
+ pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE);
+
+ u->first = FALSE;
+ }
+
+ update_smoother(u);
+ }
+
+ if (u->use_tsched) {
+ pa_usec_t cusec;
+
+ if (u->since_start <= u->hwbuf_size) {
+
+ /* USB devices on ALSA seem to hit a buffer
+ * underrun during the first iterations much
+ * quicker then we calculate here, probably due to
+ * the transport latency. To accommodate for that
+ * we artificially decrease the sleep time until
+ * we have filled the buffer at least once
+ * completely.*/
+
+ if (pa_log_ratelimit(PA_LOG_DEBUG))
+ pa_log_debug("Cutting sleep time for the initial iterations by half.");
+ sleep_usec /= 2;
+ }
+
+ /* OK, the playback buffer is now full, let's
+ * calculate when to wake up next */
+/* pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) sleep_usec / PA_USEC_PER_MSEC); */
+
+ /* Convert from the sound card time domain to the
+ * system time domain */
+ cusec = pa_smoother_translate(u->smoother, pa_rtclock_now(), sleep_usec);
+
+/* pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC); */
+
+ /* We don't trust the conversion, so we wake up whatever comes first */
+ rtpoll_sleep = PA_MIN(sleep_usec, cusec);
+ }
+
+ u->after_rewind = FALSE;
+
+ }
+
+ if (u->sink->flags & PA_SINK_SYNC_VOLUME) {
+ pa_usec_t volume_sleep;
+ pa_sink_volume_change_apply(u->sink, &volume_sleep);
+ if (volume_sleep > 0)
+ rtpoll_sleep = PA_MIN(volume_sleep, rtpoll_sleep);
+ }
+
+ if (rtpoll_sleep > 0)
+ pa_rtpoll_set_timer_relative(u->rtpoll, rtpoll_sleep);
+ else
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
+
+ /* Hmm, nothing to do. Let's sleep */
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (u->sink->flags & PA_SINK_SYNC_VOLUME)
+ pa_sink_volume_change_apply(u->sink, NULL);
+
+ if (ret == 0)
+ goto finish;
+
+ /* Tell ALSA about this and process its response */
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+ struct pollfd *pollfd;
+ int err;
+ unsigned n;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
+
+ if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
+ pa_log("snd_pcm_poll_descriptors_revents() failed: %s", pa_alsa_strerror(err));
+ goto fail;
+ }
+
+ if (revents & ~POLLOUT) {
+ if (pa_alsa_recover_from_poll(u->pcm_handle, revents) < 0)
+ goto fail;
+
+ u->first = TRUE;
+ u->since_start = 0;
+ } else if (revents && u->use_tsched && pa_log_ratelimit(PA_LOG_DEBUG))
+ pa_log_debug("Wakeup from ALSA!");
+
+ } else
+ revents = 0;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+static void set_sink_name(pa_sink_new_data *data, pa_modargs *ma, const char *device_id, const char *device_name, pa_alsa_mapping *mapping) {
+ const char *n;
+ char *t;
+
+ pa_assert(data);
+ pa_assert(ma);
+ pa_assert(device_name);
+
+ if ((n = pa_modargs_get_value(ma, "sink_name", NULL))) {
+ pa_sink_new_data_set_name(data, n);
+ data->namereg_fail = TRUE;
+ return;
+ }
+
+ if ((n = pa_modargs_get_value(ma, "name", NULL)))
+ data->namereg_fail = TRUE;
+ else {
+ n = device_id ? device_id : device_name;
+ data->namereg_fail = FALSE;
+ }
+
+ if (mapping)
+ t = pa_sprintf_malloc("alsa_output.%s.%s", n, mapping->name);
+ else
+ t = pa_sprintf_malloc("alsa_output.%s", n);
+
+ pa_sink_new_data_set_name(data, t);
+ pa_xfree(t);
+}
+
+static void find_mixer(struct userdata *u, pa_alsa_mapping *mapping, const char *element, pa_bool_t ignore_dB) {
+
+ if (!mapping && !element)
+ return;
+
+ if (!(u->mixer_handle = pa_alsa_open_mixer_for_pcm(u->pcm_handle, &u->control_device))) {
+ pa_log_info("Failed to find a working mixer device.");
+ return;
+ }
+
+ if (element) {
+
+ if (!(u->mixer_path = pa_alsa_path_synthesize(element, PA_ALSA_DIRECTION_OUTPUT)))
+ goto fail;
+
+ if (pa_alsa_path_probe(u->mixer_path, u->mixer_handle, ignore_dB) < 0)
+ goto fail;
+
+ pa_log_debug("Probed mixer path %s:", u->mixer_path->name);
+ pa_alsa_path_dump(u->mixer_path);
+ } else {
+
+ if (!(u->mixer_path_set = pa_alsa_path_set_new(mapping, PA_ALSA_DIRECTION_OUTPUT)))
+ goto fail;
+
+ pa_alsa_path_set_probe(u->mixer_path_set, u->mixer_handle, ignore_dB);
+
+ pa_log_debug("Probed mixer paths:");
+ pa_alsa_path_set_dump(u->mixer_path_set);
+ }
+
+ return;
+
+fail:
+
+ if (u->mixer_path_set) {
+ pa_alsa_path_set_free(u->mixer_path_set);
+ u->mixer_path_set = NULL;
+ } else if (u->mixer_path) {
+ pa_alsa_path_free(u->mixer_path);
+ u->mixer_path = NULL;
+ }
+
+ if (u->mixer_handle) {
+ snd_mixer_close(u->mixer_handle);
+ u->mixer_handle = NULL;
+ }
+}
+
+static int setup_mixer(struct userdata *u, pa_bool_t ignore_dB, pa_bool_t sync_volume) {
+ pa_assert(u);
+
+ if (!u->mixer_handle)
+ return 0;
+
+ if (u->sink->active_port) {
+ pa_alsa_port_data *data;
+
+ /* We have a list of supported paths, so let's activate the
+ * one that has been chosen as active */
+
+ data = PA_DEVICE_PORT_DATA(u->sink->active_port);
+ u->mixer_path = data->path;
+
+ pa_alsa_path_select(data->path, u->mixer_handle);
+
+ if (data->setting)
+ pa_alsa_setting_select(data->setting, u->mixer_handle);
+
+ } else {
+
+ if (!u->mixer_path && u->mixer_path_set)
+ u->mixer_path = u->mixer_path_set->paths;
+
+ if (u->mixer_path) {
+ /* Hmm, we have only a single path, then let's activate it */
+
+ pa_alsa_path_select(u->mixer_path, u->mixer_handle);
+
+ if (u->mixer_path->settings)
+ pa_alsa_setting_select(u->mixer_path->settings, u->mixer_handle);
+ } else
+ return 0;
+ }
+
+ if (!u->mixer_path->has_volume)
+ pa_log_info("Driver does not support hardware volume control, falling back to software volume control.");
+ else {
+
+ if (u->mixer_path->has_dB) {
+ pa_log_info("Hardware volume ranges from %0.2f dB to %0.2f dB.", u->mixer_path->min_dB, u->mixer_path->max_dB);
+
+ u->sink->base_volume = pa_sw_volume_from_dB(-u->mixer_path->max_dB);
+ u->sink->n_volume_steps = PA_VOLUME_NORM+1;
+
+ pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->sink->base_volume));
+
+ } else {
+ pa_log_info("Hardware volume ranges from %li to %li.", u->mixer_path->min_volume, u->mixer_path->max_volume);
+ u->sink->base_volume = PA_VOLUME_NORM;
+ u->sink->n_volume_steps = u->mixer_path->max_volume - u->mixer_path->min_volume + 1;
+ }
+
+ u->sink->get_volume = sink_get_volume_cb;
+ u->sink->set_volume = sink_set_volume_cb;
+ u->sink->write_volume = sink_write_volume_cb;
+
+ u->sink->flags |= PA_SINK_HW_VOLUME_CTRL;
+ if (u->mixer_path->has_dB) {
+ u->sink->flags |= PA_SINK_DECIBEL_VOLUME;
+ if (sync_volume) {
+ u->sink->flags |= PA_SINK_SYNC_VOLUME;
+ pa_log_info("Successfully enabled synchronous volume.");
+ }
+ }
+
+ pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->mixer_path->has_dB ? "supported" : "not supported");
+ }
+
+ if (!u->mixer_path->has_mute) {
+ pa_log_info("Driver does not support hardware mute control, falling back to software mute control.");
+ } else {
+ u->sink->get_mute = sink_get_mute_cb;
+ u->sink->set_mute = sink_set_mute_cb;
+ u->sink->flags |= PA_SINK_HW_MUTE_CTRL;
+ pa_log_info("Using hardware mute control.");
+ }
+
+ if (u->sink->flags & (PA_SINK_HW_VOLUME_CTRL|PA_SINK_HW_MUTE_CTRL)) {
+ int (*mixer_callback)(snd_mixer_elem_t *, unsigned int);
+ if (u->sink->flags & PA_SINK_SYNC_VOLUME) {
+ u->mixer_pd = pa_alsa_mixer_pdata_new();
+ mixer_callback = io_mixer_callback;
+
+ if (pa_alsa_set_mixer_rtpoll(u->mixer_pd, u->mixer_handle, u->rtpoll) < 0) {
+ pa_log("Failed to initialize file descriptor monitoring");
+ return -1;
+ }
+ } else {
+ u->mixer_fdl = pa_alsa_fdlist_new();
+ mixer_callback = ctl_mixer_callback;
+
+ if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, u->core->mainloop) < 0) {
+ pa_log("Failed to initialize file descriptor monitoring");
+ return -1;
+ }
+ }
+
+ if (u->mixer_path_set)
+ pa_alsa_path_set_set_callback(u->mixer_path_set, u->mixer_handle, mixer_callback, u);
+ else
+ pa_alsa_path_set_callback(u->mixer_path, u->mixer_handle, mixer_callback, u);
+ }
+
+ return 0;
+}
+
+pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, pa_alsa_mapping *mapping) {
+
+ struct userdata *u = NULL;
+ const char *dev_id = NULL;
+ pa_sample_spec ss, requested_ss;
+ pa_channel_map map;
+ uint32_t nfrags, frag_size, buffer_size, tsched_size, tsched_watermark, rewind_safeguard;
+ snd_pcm_uframes_t period_frames, buffer_frames, tsched_frames;
+ size_t frame_size;
+ pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d, ignore_dB = FALSE, namereg_fail = FALSE, sync_volume = FALSE;
+ pa_sink_new_data data;
+ pa_alsa_profile_set *profile_set = NULL;
+
+ pa_assert(m);
+ pa_assert(ma);
+
+ ss = m->core->default_sample_spec;
+ map = m->core->default_channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
+ pa_log("Failed to parse sample specification and channel map");
+ goto fail;
+ }
+
+ requested_ss = ss;
+ frame_size = pa_frame_size(&ss);
+
+ nfrags = m->core->default_n_fragments;
+ frag_size = (uint32_t) pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
+ if (frag_size <= 0)
+ frag_size = (uint32_t) frame_size;
+ tsched_size = (uint32_t) pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC, &ss);
+ tsched_watermark = (uint32_t) pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC, &ss);
+
+ if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 ||
+ pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0 ||
+ pa_modargs_get_value_u32(ma, "tsched_buffer_size", &tsched_size) < 0 ||
+ pa_modargs_get_value_u32(ma, "tsched_buffer_watermark", &tsched_watermark) < 0) {
+ pa_log("Failed to parse buffer metrics");
+ goto fail;
+ }
+
+ buffer_size = nfrags * frag_size;
+
+ period_frames = frag_size/frame_size;
+ buffer_frames = buffer_size/frame_size;
+ tsched_frames = tsched_size/frame_size;
+
+ if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
+ pa_log("Failed to parse mmap argument.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
+ pa_log("Failed to parse tsched argument.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
+ pa_log("Failed to parse ignore_dB argument.");
+ goto fail;
+ }
+
+ rewind_safeguard = PA_MAX(DEFAULT_REWIND_SAFEGUARD_BYTES, pa_usec_to_bytes(DEFAULT_REWIND_SAFEGUARD_USEC, &ss));
+ if (pa_modargs_get_value_u32(ma, "rewind_safeguard", &rewind_safeguard) < 0) {
+ pa_log("Failed to parse rewind_safeguard argument");
+ goto fail;
+ }
+
+ sync_volume = m->core->sync_volume;
+ if (pa_modargs_get_value_boolean(ma, "sync_volume", &sync_volume) < 0) {
+ pa_log("Failed to parse sync_volume argument.");
+ goto fail;
+ }
+
+ use_tsched = pa_alsa_may_tsched(use_tsched);
+
+ u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->use_mmap = use_mmap;
+ u->use_tsched = use_tsched;
+ u->first = TRUE;
+ u->rewind_safeguard = rewind_safeguard;
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ u->smoother = pa_smoother_new(
+ SMOOTHER_ADJUST_USEC,
+ SMOOTHER_WINDOW_USEC,
+ TRUE,
+ TRUE,
+ 5,
+ pa_rtclock_now(),
+ TRUE);
+ u->smoother_interval = SMOOTHER_MIN_INTERVAL;
+
+ dev_id = pa_modargs_get_value(
+ ma, "device_id",
+ pa_modargs_get_value(ma, "device", DEFAULT_DEVICE));
+
+ if (reserve_init(u, dev_id) < 0)
+ goto fail;
+
+ if (reserve_monitor_init(u, dev_id) < 0)
+ goto fail;
+
+ b = use_mmap;
+ d = use_tsched;
+
+ if (mapping) {
+
+ if (!(dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
+ pa_log("device_id= not set");
+ goto fail;
+ }
+
+ if (!(u->pcm_handle = pa_alsa_open_by_device_id_mapping(
+ dev_id,
+ &u->device_name,
+ &ss, &map,
+ SND_PCM_STREAM_PLAYBACK,
+ &period_frames, &buffer_frames, tsched_frames,
+ &b, &d, mapping)))
+ goto fail;
+
+ } else if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
+
+ if (!(profile_set = pa_alsa_profile_set_new(NULL, &map)))
+ goto fail;
+
+ if (!(u->pcm_handle = pa_alsa_open_by_device_id_auto(
+ dev_id,
+ &u->device_name,
+ &ss, &map,
+ SND_PCM_STREAM_PLAYBACK,
+ &period_frames, &buffer_frames, tsched_frames,
+ &b, &d, profile_set, &mapping)))
+ goto fail;
+
+ } else {
+
+ if (!(u->pcm_handle = pa_alsa_open_by_device_string(
+ pa_modargs_get_value(ma, "device", DEFAULT_DEVICE),
+ &u->device_name,
+ &ss, &map,
+ SND_PCM_STREAM_PLAYBACK,
+ &period_frames, &buffer_frames, tsched_frames,
+ &b, &d, FALSE)))
+ goto fail;
+ }
+
+ pa_assert(u->device_name);
+ pa_log_info("Successfully opened device %s.", u->device_name);
+
+ if (pa_alsa_pcm_is_modem(u->pcm_handle)) {
+ pa_log_notice("Device %s is modem, refusing further initialization.", u->device_name);
+ goto fail;
+ }
+
+ if (mapping)
+ pa_log_info("Selected mapping '%s' (%s).", mapping->description, mapping->name);
+
+ if (use_mmap && !b) {
+ pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
+ u->use_mmap = use_mmap = FALSE;
+ }
+
+ if (use_tsched && (!b || !d)) {
+ pa_log_info("Cannot enable timer-based scheduling, falling back to sound IRQ scheduling.");
+ u->use_tsched = use_tsched = FALSE;
+ }
+
+ if (u->use_mmap)
+ pa_log_info("Successfully enabled mmap() mode.");
+
+ if (u->use_tsched)
+ pa_log_info("Successfully enabled timer-based scheduling mode.");
+
+ /* ALSA might tweak the sample spec, so recalculate the frame size */
+ frame_size = pa_frame_size(&ss);
+
+ find_mixer(u, mapping, pa_modargs_get_value(ma, "control", NULL), ignore_dB);
+
+ pa_sink_new_data_init(&data);
+ data.driver = driver;
+ data.module = m;
+ data.card = card;
+ set_sink_name(&data, ma, dev_id, u->device_name, mapping);
+
+ /* We need to give pa_modargs_get_value_boolean() a pointer to a local
+ * variable instead of using &data.namereg_fail directly, because
+ * data.namereg_fail is a bitfield and taking the address of a bitfield
+ * variable is impossible. */
+ namereg_fail = data.namereg_fail;
+ if (pa_modargs_get_value_boolean(ma, "namereg_fail", &namereg_fail) < 0) {
+ pa_log("Failed to parse boolean argument namereg_fail.");
+ pa_sink_new_data_done(&data);
+ goto fail;
+ }
+ data.namereg_fail = namereg_fail;
+
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+
+ pa_alsa_init_proplist_pcm(m->core, data.proplist, u->pcm_handle);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (buffer_frames * frame_size));
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (period_frames * frame_size));
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap+timer" : (u->use_mmap ? "mmap" : "serial"));
+
+ if (mapping) {
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_NAME, mapping->name);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_DESCRIPTION, mapping->description);
+ }
+
+ pa_alsa_init_description(data.proplist);
+
+ if (u->control_device)
+ pa_alsa_init_proplist_ctl(data.proplist, u->control_device);
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&data);
+ goto fail;
+ }
+
+ if (u->mixer_path_set)
+ pa_alsa_add_ports(&data.ports, u->mixer_path_set);
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY|(u->use_tsched ? PA_SINK_DYNAMIC_LATENCY : 0));
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink object");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_u32(ma, "sync_volume_safety_margin",
+ &u->sink->thread_info.volume_change_safety_margin) < 0) {
+ pa_log("Failed to parse sync_volume_safety_margin parameter");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_s32(ma, "sync_volume_extra_delay",
+ &u->sink->thread_info.volume_change_extra_delay) < 0) {
+ pa_log("Failed to parse sync_volume_extra_delay parameter");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg;
+ if (u->use_tsched)
+ u->sink->update_requested_latency = sink_update_requested_latency_cb;
+ u->sink->set_state = sink_set_state_cb;
+ u->sink->set_port = sink_set_port_cb;
+ u->sink->userdata = u;
+
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+
+ u->frame_size = frame_size;
+ u->fragment_size = frag_size = (size_t) (period_frames * frame_size);
+ u->hwbuf_size = buffer_size = (size_t) (buffer_frames * frame_size);
+ pa_cvolume_mute(&u->hardware_volume, u->sink->sample_spec.channels);
+
+ pa_log_info("Using %0.1f fragments of size %lu bytes (%0.2fms), buffer size is %lu bytes (%0.2fms)",
+ (double) u->hwbuf_size / (double) u->fragment_size,
+ (long unsigned) u->fragment_size,
+ (double) pa_bytes_to_usec(u->fragment_size, &ss) / PA_USEC_PER_MSEC,
+ (long unsigned) u->hwbuf_size,
+ (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC);
+
+ pa_sink_set_max_request(u->sink, u->hwbuf_size);
+ if (pa_alsa_pcm_is_hw(u->pcm_handle))
+ pa_sink_set_max_rewind(u->sink, u->hwbuf_size);
+ else {
+ pa_log_info("Disabling rewind for device %s", u->device_name);
+ pa_sink_set_max_rewind(u->sink, 0);
+ }
+
+ if (u->use_tsched) {
+ u->tsched_watermark = pa_usec_to_bytes_round_up(pa_bytes_to_usec_round_up(tsched_watermark, &requested_ss), &u->sink->sample_spec);
+
+ u->watermark_inc_step = pa_usec_to_bytes(TSCHED_WATERMARK_INC_STEP_USEC, &u->sink->sample_spec);
+ u->watermark_dec_step = pa_usec_to_bytes(TSCHED_WATERMARK_DEC_STEP_USEC, &u->sink->sample_spec);
+
+ u->watermark_inc_threshold = pa_usec_to_bytes_round_up(TSCHED_WATERMARK_INC_THRESHOLD_USEC, &u->sink->sample_spec);
+ u->watermark_dec_threshold = pa_usec_to_bytes_round_up(TSCHED_WATERMARK_DEC_THRESHOLD_USEC, &u->sink->sample_spec);
+
+ fix_min_sleep_wakeup(u);
+ fix_tsched_watermark(u);
+
+ pa_sink_set_latency_range(u->sink,
+ 0,
+ pa_bytes_to_usec(u->hwbuf_size, &ss));
+
+ pa_log_info("Time scheduling watermark is %0.2fms",
+ (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC);
+ } else
+ pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(u->hwbuf_size, &ss));
+
+ reserve_update(u);
+
+ if (update_sw_params(u) < 0)
+ goto fail;
+
+ if (setup_mixer(u, ignore_dB, sync_volume) < 0)
+ goto fail;
+
+ pa_alsa_dump(PA_LOG_DEBUG, u->pcm_handle);
+
+ if (!(u->thread = pa_thread_new("alsa-sink", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ /* Get initial mixer settings */
+ if (data.volume_is_set) {
+ if (u->sink->set_volume)
+ u->sink->set_volume(u->sink);
+ } else {
+ if (u->sink->get_volume)
+ u->sink->get_volume(u->sink);
+ }
+
+ if (data.muted_is_set) {
+ if (u->sink->set_mute)
+ u->sink->set_mute(u->sink);
+ } else {
+ if (u->sink->get_mute)
+ u->sink->get_mute(u->sink);
+ }
+
+ pa_sink_put(u->sink);
+
+ if (profile_set)
+ pa_alsa_profile_set_free(profile_set);
+
+ return u->sink;
+
+fail:
+
+ if (u)
+ userdata_free(u);
+
+ if (profile_set)
+ pa_alsa_profile_set_free(profile_set);
+
+ return NULL;
+}
+
+static void userdata_free(struct userdata *u) {
+ pa_assert(u);
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->memchunk.memblock)
+ pa_memblock_unref(u->memchunk.memblock);
+
+ if (u->mixer_pd)
+ pa_alsa_mixer_pdata_free(u->mixer_pd);
+
+ if (u->alsa_rtpoll_item)
+ pa_rtpoll_item_free(u->alsa_rtpoll_item);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ if (u->pcm_handle) {
+ snd_pcm_drop(u->pcm_handle);
+ snd_pcm_close(u->pcm_handle);
+ }
+
+ if (u->mixer_fdl)
+ pa_alsa_fdlist_free(u->mixer_fdl);
+
+ if (u->mixer_path_set)
+ pa_alsa_path_set_free(u->mixer_path_set);
+ else if (u->mixer_path)
+ pa_alsa_path_free(u->mixer_path);
+
+ if (u->mixer_handle)
+ snd_mixer_close(u->mixer_handle);
+
+ if (u->smoother)
+ pa_smoother_free(u->smoother);
+
+ reserve_done(u);
+ monitor_done(u);
+
+ pa_xfree(u->device_name);
+ pa_xfree(u->control_device);
+ pa_xfree(u);
+}
+
+void pa_alsa_sink_free(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ userdata_free(u);
+}
diff --git a/src/modules/alsa/alsa-sink.h b/src/modules/alsa/alsa-sink.h
new file mode 100644
index 00000000..e640b624
--- /dev/null
+++ b/src/modules/alsa/alsa-sink.h
@@ -0,0 +1,36 @@
+#ifndef fooalsasinkhfoo
+#define fooalsasinkhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/sink.h>
+
+#include "alsa-util.h"
+
+pa_sink* pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, pa_alsa_mapping *mapping);
+
+void pa_alsa_sink_free(pa_sink *s);
+
+#endif
diff --git a/src/modules/alsa/alsa-source.c b/src/modules/alsa/alsa-source.c
new file mode 100644
index 00000000..f847b1ee
--- /dev/null
+++ b/src/modules/alsa/alsa-source.c
@@ -0,0 +1,2003 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2008 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+
+#include <asoundlib.h>
+
+#include <pulse/i18n.h>
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+#include <pulse/volume.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/module.h>
+#include <pulsecore/memchunk.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/core-rtclock.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/time-smoother.h>
+
+#include <modules/reserve-wrap.h>
+
+#include "alsa-util.h"
+#include "alsa-source.h"
+
+/* #define DEBUG_TIMING */
+
+#define DEFAULT_DEVICE "default"
+
+#define DEFAULT_TSCHED_BUFFER_USEC (2*PA_USEC_PER_SEC) /* 2s */
+#define DEFAULT_TSCHED_WATERMARK_USEC (20*PA_USEC_PER_MSEC) /* 20ms */
+
+#define TSCHED_WATERMARK_INC_STEP_USEC (10*PA_USEC_PER_MSEC) /* 10ms */
+#define TSCHED_WATERMARK_DEC_STEP_USEC (5*PA_USEC_PER_MSEC) /* 5ms */
+#define TSCHED_WATERMARK_VERIFY_AFTER_USEC (20*PA_USEC_PER_SEC) /* 20s */
+#define TSCHED_WATERMARK_INC_THRESHOLD_USEC (0*PA_USEC_PER_MSEC) /* 0ms */
+#define TSCHED_WATERMARK_DEC_THRESHOLD_USEC (100*PA_USEC_PER_MSEC) /* 100ms */
+#define TSCHED_WATERMARK_STEP_USEC (10*PA_USEC_PER_MSEC) /* 10ms */
+
+#define TSCHED_MIN_SLEEP_USEC (10*PA_USEC_PER_MSEC) /* 10ms */
+#define TSCHED_MIN_WAKEUP_USEC (4*PA_USEC_PER_MSEC) /* 4ms */
+
+#define SMOOTHER_WINDOW_USEC (10*PA_USEC_PER_SEC) /* 10s */
+#define SMOOTHER_ADJUST_USEC (1*PA_USEC_PER_SEC) /* 1s */
+
+#define SMOOTHER_MIN_INTERVAL (2*PA_USEC_PER_MSEC) /* 2ms */
+#define SMOOTHER_MAX_INTERVAL (200*PA_USEC_PER_MSEC) /* 200ms */
+
+#define VOLUME_ACCURACY (PA_VOLUME_NORM/100)
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_source *source;
+
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+
+ snd_pcm_t *pcm_handle;
+
+ pa_alsa_fdlist *mixer_fdl;
+ pa_alsa_mixer_pdata *mixer_pd;
+ snd_mixer_t *mixer_handle;
+ pa_alsa_path_set *mixer_path_set;
+ pa_alsa_path *mixer_path;
+
+ pa_cvolume hardware_volume;
+
+ size_t
+ frame_size,
+ fragment_size,
+ hwbuf_size,
+ tsched_watermark,
+ hwbuf_unused,
+ min_sleep,
+ min_wakeup,
+ watermark_inc_step,
+ watermark_dec_step,
+ watermark_inc_threshold,
+ watermark_dec_threshold;
+
+ pa_usec_t watermark_dec_not_before;
+
+ char *device_name; /* name of the PCM device */
+ char *control_device; /* name of the control device */
+
+ pa_bool_t use_mmap:1, use_tsched:1;
+
+ pa_bool_t first;
+
+ pa_rtpoll_item *alsa_rtpoll_item;
+
+ snd_mixer_selem_channel_id_t mixer_map[SND_MIXER_SCHN_LAST];
+
+ pa_smoother *smoother;
+ uint64_t read_count;
+ pa_usec_t smoother_interval;
+ pa_usec_t last_smoother_update;
+
+ pa_reserve_wrapper *reserve;
+ pa_hook_slot *reserve_slot;
+ pa_reserve_monitor_wrapper *monitor;
+ pa_hook_slot *monitor_slot;
+};
+
+static void userdata_free(struct userdata *u);
+
+static pa_hook_result_t reserve_cb(pa_reserve_wrapper *r, void *forced, struct userdata *u) {
+ pa_assert(r);
+ pa_assert(u);
+
+ if (pa_source_suspend(u->source, TRUE, PA_SUSPEND_APPLICATION) < 0)
+ return PA_HOOK_CANCEL;
+
+ return PA_HOOK_OK;
+}
+
+static void reserve_done(struct userdata *u) {
+ pa_assert(u);
+
+ if (u->reserve_slot) {
+ pa_hook_slot_free(u->reserve_slot);
+ u->reserve_slot = NULL;
+ }
+
+ if (u->reserve) {
+ pa_reserve_wrapper_unref(u->reserve);
+ u->reserve = NULL;
+ }
+}
+
+static void reserve_update(struct userdata *u) {
+ const char *description;
+ pa_assert(u);
+
+ if (!u->source || !u->reserve)
+ return;
+
+ if ((description = pa_proplist_gets(u->source->proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ pa_reserve_wrapper_set_application_device_name(u->reserve, description);
+}
+
+static int reserve_init(struct userdata *u, const char *dname) {
+ char *rname;
+
+ pa_assert(u);
+ pa_assert(dname);
+
+ if (u->reserve)
+ return 0;
+
+ if (pa_in_system_mode())
+ return 0;
+
+ if (!(rname = pa_alsa_get_reserve_name(dname)))
+ return 0;
+
+ /* We are resuming, try to lock the device */
+ u->reserve = pa_reserve_wrapper_get(u->core, rname);
+ pa_xfree(rname);
+
+ if (!(u->reserve))
+ return -1;
+
+ reserve_update(u);
+
+ pa_assert(!u->reserve_slot);
+ u->reserve_slot = pa_hook_connect(pa_reserve_wrapper_hook(u->reserve), PA_HOOK_NORMAL, (pa_hook_cb_t) reserve_cb, u);
+
+ return 0;
+}
+
+static pa_hook_result_t monitor_cb(pa_reserve_monitor_wrapper *w, void* busy, struct userdata *u) {
+ pa_bool_t b;
+
+ pa_assert(w);
+ pa_assert(u);
+
+ b = PA_PTR_TO_UINT(busy) && !u->reserve;
+
+ pa_source_suspend(u->source, b, PA_SUSPEND_APPLICATION);
+ return PA_HOOK_OK;
+}
+
+static void monitor_done(struct userdata *u) {
+ pa_assert(u);
+
+ if (u->monitor_slot) {
+ pa_hook_slot_free(u->monitor_slot);
+ u->monitor_slot = NULL;
+ }
+
+ if (u->monitor) {
+ pa_reserve_monitor_wrapper_unref(u->monitor);
+ u->monitor = NULL;
+ }
+}
+
+static int reserve_monitor_init(struct userdata *u, const char *dname) {
+ char *rname;
+
+ pa_assert(u);
+ pa_assert(dname);
+
+ if (pa_in_system_mode())
+ return 0;
+
+ if (!(rname = pa_alsa_get_reserve_name(dname)))
+ return 0;
+
+ /* We are resuming, try to lock the device */
+ u->monitor = pa_reserve_monitor_wrapper_get(u->core, rname);
+ pa_xfree(rname);
+
+ if (!(u->monitor))
+ return -1;
+
+ pa_assert(!u->monitor_slot);
+ u->monitor_slot = pa_hook_connect(pa_reserve_monitor_wrapper_hook(u->monitor), PA_HOOK_NORMAL, (pa_hook_cb_t) monitor_cb, u);
+
+ return 0;
+}
+
+static void fix_min_sleep_wakeup(struct userdata *u) {
+ size_t max_use, max_use_2;
+
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ max_use = u->hwbuf_size - u->hwbuf_unused;
+ max_use_2 = pa_frame_align(max_use/2, &u->source->sample_spec);
+
+ u->min_sleep = pa_usec_to_bytes(TSCHED_MIN_SLEEP_USEC, &u->source->sample_spec);
+ u->min_sleep = PA_CLAMP(u->min_sleep, u->frame_size, max_use_2);
+
+ u->min_wakeup = pa_usec_to_bytes(TSCHED_MIN_WAKEUP_USEC, &u->source->sample_spec);
+ u->min_wakeup = PA_CLAMP(u->min_wakeup, u->frame_size, max_use_2);
+}
+
+static void fix_tsched_watermark(struct userdata *u) {
+ size_t max_use;
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ max_use = u->hwbuf_size - u->hwbuf_unused;
+
+ if (u->tsched_watermark > max_use - u->min_sleep)
+ u->tsched_watermark = max_use - u->min_sleep;
+
+ if (u->tsched_watermark < u->min_wakeup)
+ u->tsched_watermark = u->min_wakeup;
+}
+
+static void increase_watermark(struct userdata *u) {
+ size_t old_watermark;
+ pa_usec_t old_min_latency, new_min_latency;
+
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ /* First, just try to increase the watermark */
+ old_watermark = u->tsched_watermark;
+ u->tsched_watermark = PA_MIN(u->tsched_watermark * 2, u->tsched_watermark + u->watermark_inc_step);
+ fix_tsched_watermark(u);
+
+ if (old_watermark != u->tsched_watermark) {
+ pa_log_info("Increasing wakeup watermark to %0.2f ms",
+ (double) pa_bytes_to_usec(u->tsched_watermark, &u->source->sample_spec) / PA_USEC_PER_MSEC);
+ return;
+ }
+
+ /* Hmm, we cannot increase the watermark any further, hence let's raise the latency */
+ old_min_latency = u->source->thread_info.min_latency;
+ new_min_latency = PA_MIN(old_min_latency * 2, old_min_latency + TSCHED_WATERMARK_INC_STEP_USEC);
+ new_min_latency = PA_MIN(new_min_latency, u->source->thread_info.max_latency);
+
+ if (old_min_latency != new_min_latency) {
+ pa_log_info("Increasing minimal latency to %0.2f ms",
+ (double) new_min_latency / PA_USEC_PER_MSEC);
+
+ pa_source_set_latency_range_within_thread(u->source, new_min_latency, u->source->thread_info.max_latency);
+ }
+
+ /* When we reach this we're officialy fucked! */
+}
+
+static void decrease_watermark(struct userdata *u) {
+ size_t old_watermark;
+ pa_usec_t now;
+
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ now = pa_rtclock_now();
+
+ if (u->watermark_dec_not_before <= 0)
+ goto restart;
+
+ if (u->watermark_dec_not_before > now)
+ return;
+
+ old_watermark = u->tsched_watermark;
+
+ if (u->tsched_watermark < u->watermark_dec_step)
+ u->tsched_watermark = u->tsched_watermark / 2;
+ else
+ u->tsched_watermark = PA_MAX(u->tsched_watermark / 2, u->tsched_watermark - u->watermark_dec_step);
+
+ fix_tsched_watermark(u);
+
+ if (old_watermark != u->tsched_watermark)
+ pa_log_info("Decreasing wakeup watermark to %0.2f ms",
+ (double) pa_bytes_to_usec(u->tsched_watermark, &u->source->sample_spec) / PA_USEC_PER_MSEC);
+
+ /* We don't change the latency range*/
+
+restart:
+ u->watermark_dec_not_before = now + TSCHED_WATERMARK_VERIFY_AFTER_USEC;
+}
+
+static void hw_sleep_time(struct userdata *u, pa_usec_t *sleep_usec, pa_usec_t*process_usec) {
+ pa_usec_t wm, usec;
+
+ pa_assert(sleep_usec);
+ pa_assert(process_usec);
+
+ pa_assert(u);
+ pa_assert(u->use_tsched);
+
+ usec = pa_source_get_requested_latency_within_thread(u->source);
+
+ if (usec == (pa_usec_t) -1)
+ usec = pa_bytes_to_usec(u->hwbuf_size, &u->source->sample_spec);
+
+ wm = pa_bytes_to_usec(u->tsched_watermark, &u->source->sample_spec);
+
+ if (wm > usec)
+ wm = usec/2;
+
+ *sleep_usec = usec - wm;
+ *process_usec = wm;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Buffer time: %lu ms; Sleep time: %lu ms; Process time: %lu ms",
+ (unsigned long) (usec / PA_USEC_PER_MSEC),
+ (unsigned long) (*sleep_usec / PA_USEC_PER_MSEC),
+ (unsigned long) (*process_usec / PA_USEC_PER_MSEC));
+#endif
+}
+
+static int try_recover(struct userdata *u, const char *call, int err) {
+ pa_assert(u);
+ pa_assert(call);
+ pa_assert(err < 0);
+
+ pa_log_debug("%s: %s", call, pa_alsa_strerror(err));
+
+ pa_assert(err != -EAGAIN);
+
+ if (err == -EPIPE)
+ pa_log_debug("%s: Buffer overrun!", call);
+
+ if (err == -ESTRPIPE)
+ pa_log_debug("%s: System suspended!", call);
+
+ if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) < 0) {
+ pa_log("%s: %s", call, pa_alsa_strerror(err));
+ return -1;
+ }
+
+ u->first = TRUE;
+ return 0;
+}
+
+static size_t check_left_to_record(struct userdata *u, size_t n_bytes, pa_bool_t on_timeout) {
+ size_t left_to_record;
+ size_t rec_space = u->hwbuf_size - u->hwbuf_unused;
+ pa_bool_t overrun = FALSE;
+
+ /* We use <= instead of < for this check here because an overrun
+ * only happens after the last sample was processed, not already when
+ * it is removed from the buffer. This is particularly important
+ * when block transfer is used. */
+
+ if (n_bytes <= rec_space)
+ left_to_record = rec_space - n_bytes;
+ else {
+
+ /* We got a dropout. What a mess! */
+ left_to_record = 0;
+ overrun = TRUE;
+
+#ifdef DEBUG_TIMING
+ PA_DEBUG_TRAP;
+#endif
+
+ if (pa_log_ratelimit(PA_LOG_INFO))
+ pa_log_info("Overrun!");
+ }
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("%0.2f ms left to record", (double) pa_bytes_to_usec(left_to_record, &u->source->sample_spec) / PA_USEC_PER_MSEC);
+#endif
+
+ if (u->use_tsched) {
+ pa_bool_t reset_not_before = TRUE;
+
+ if (overrun || left_to_record < u->watermark_inc_threshold)
+ increase_watermark(u);
+ else if (left_to_record > u->watermark_dec_threshold) {
+ reset_not_before = FALSE;
+
+ /* We decrease the watermark only if have actually
+ * been woken up by a timeout. If something else woke
+ * us up it's too easy to fulfill the deadlines... */
+
+ if (on_timeout)
+ decrease_watermark(u);
+ }
+
+ if (reset_not_before)
+ u->watermark_dec_not_before = 0;
+ }
+
+ return left_to_record;
+}
+
+static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled, pa_bool_t on_timeout) {
+ pa_bool_t work_done = FALSE;
+ pa_usec_t max_sleep_usec = 0, process_usec = 0;
+ size_t left_to_record;
+ unsigned j = 0;
+
+ pa_assert(u);
+ pa_source_assert_ref(u->source);
+
+ if (u->use_tsched)
+ hw_sleep_time(u, &max_sleep_usec, &process_usec);
+
+ for (;;) {
+ snd_pcm_sframes_t n;
+ size_t n_bytes;
+ int r;
+ pa_bool_t after_avail = TRUE;
+
+ if (PA_UNLIKELY((n = pa_alsa_safe_avail(u->pcm_handle, u->hwbuf_size, &u->source->sample_spec)) < 0)) {
+
+ if ((r = try_recover(u, "snd_pcm_avail", (int) n)) == 0)
+ continue;
+
+ return r;
+ }
+
+ n_bytes = (size_t) n * u->frame_size;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("avail: %lu", (unsigned long) n_bytes);
+#endif
+
+ left_to_record = check_left_to_record(u, n_bytes, on_timeout);
+ on_timeout = FALSE;
+
+ if (u->use_tsched)
+ if (!polled &&
+ pa_bytes_to_usec(left_to_record, &u->source->sample_spec) > process_usec+max_sleep_usec/2) {
+#ifdef DEBUG_TIMING
+ pa_log_debug("Not reading, because too early.");
+#endif
+ break;
+ }
+
+ if (PA_UNLIKELY(n_bytes <= 0)) {
+
+ if (polled)
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(u->pcm_handle);
+ pa_log(_("ALSA woke us up to read new data from the device, but there was actually nothing to read!\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers.\n"
+ "We were woken up with POLLIN set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail."),
+ pa_strnull(dn));
+ pa_xfree(dn);
+ } PA_ONCE_END;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Not reading, because not necessary.");
+#endif
+ break;
+ }
+
+
+ if (++j > 10) {
+#ifdef DEBUG_TIMING
+ pa_log_debug("Not filling up, because already too many iterations.");
+#endif
+
+ break;
+ }
+
+ polled = FALSE;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Reading");
+#endif
+
+ for (;;) {
+ pa_memchunk chunk;
+ void *p;
+ int err;
+ const snd_pcm_channel_area_t *areas;
+ snd_pcm_uframes_t offset, frames;
+ snd_pcm_sframes_t sframes;
+
+ frames = (snd_pcm_uframes_t) (n_bytes / u->frame_size);
+/* pa_log_debug("%lu frames to read", (unsigned long) frames); */
+
+ if (PA_UNLIKELY((err = pa_alsa_safe_mmap_begin(u->pcm_handle, &areas, &offset, &frames, u->hwbuf_size, &u->source->sample_spec)) < 0)) {
+
+ if (!after_avail && err == -EAGAIN)
+ break;
+
+ if ((r = try_recover(u, "snd_pcm_mmap_begin", err)) == 0)
+ continue;
+
+ return r;
+ }
+
+ /* Make sure that if these memblocks need to be copied they will fit into one slot */
+ if (frames > pa_mempool_block_size_max(u->source->core->mempool)/u->frame_size)
+ frames = pa_mempool_block_size_max(u->source->core->mempool)/u->frame_size;
+
+ if (!after_avail && frames == 0)
+ break;
+
+ pa_assert(frames > 0);
+ after_avail = FALSE;
+
+ /* Check these are multiples of 8 bit */
+ pa_assert((areas[0].first & 7) == 0);
+ pa_assert((areas[0].step & 7)== 0);
+
+ /* We assume a single interleaved memory buffer */
+ pa_assert((areas[0].first >> 3) == 0);
+ pa_assert((areas[0].step >> 3) == u->frame_size);
+
+ p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
+
+ chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, TRUE);
+ chunk.length = pa_memblock_get_length(chunk.memblock);
+ chunk.index = 0;
+
+ pa_source_post(u->source, &chunk);
+ pa_memblock_unref_fixed(chunk.memblock);
+
+ if (PA_UNLIKELY((sframes = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0)) {
+
+ if ((r = try_recover(u, "snd_pcm_mmap_commit", (int) sframes)) == 0)
+ continue;
+
+ return r;
+ }
+
+ work_done = TRUE;
+
+ u->read_count += frames * u->frame_size;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Read %lu bytes (of possible %lu bytes)", (unsigned long) (frames * u->frame_size), (unsigned long) n_bytes);
+#endif
+
+ if ((size_t) frames * u->frame_size >= n_bytes)
+ break;
+
+ n_bytes -= (size_t) frames * u->frame_size;
+ }
+ }
+
+ if (u->use_tsched) {
+ *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec);
+
+ if (*sleep_usec > process_usec)
+ *sleep_usec -= process_usec;
+ else
+ *sleep_usec = 0;
+ }
+
+ return work_done ? 1 : 0;
+}
+
+static int unix_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled, pa_bool_t on_timeout) {
+ int work_done = FALSE;
+ pa_usec_t max_sleep_usec = 0, process_usec = 0;
+ size_t left_to_record;
+ unsigned j = 0;
+
+ pa_assert(u);
+ pa_source_assert_ref(u->source);
+
+ if (u->use_tsched)
+ hw_sleep_time(u, &max_sleep_usec, &process_usec);
+
+ for (;;) {
+ snd_pcm_sframes_t n;
+ size_t n_bytes;
+ int r;
+ pa_bool_t after_avail = TRUE;
+
+ if (PA_UNLIKELY((n = pa_alsa_safe_avail(u->pcm_handle, u->hwbuf_size, &u->source->sample_spec)) < 0)) {
+
+ if ((r = try_recover(u, "snd_pcm_avail", (int) n)) == 0)
+ continue;
+
+ return r;
+ }
+
+ n_bytes = (size_t) n * u->frame_size;
+ left_to_record = check_left_to_record(u, n_bytes, on_timeout);
+ on_timeout = FALSE;
+
+ if (u->use_tsched)
+ if (!polled &&
+ pa_bytes_to_usec(left_to_record, &u->source->sample_spec) > process_usec+max_sleep_usec/2)
+ break;
+
+ if (PA_UNLIKELY(n_bytes <= 0)) {
+
+ if (polled)
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(u->pcm_handle);
+ pa_log(_("ALSA woke us up to read new data from the device, but there was actually nothing to read!\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers.\n"
+ "We were woken up with POLLIN set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail."),
+ pa_strnull(dn));
+ pa_xfree(dn);
+ } PA_ONCE_END;
+
+ break;
+ }
+
+ if (++j > 10) {
+#ifdef DEBUG_TIMING
+ pa_log_debug("Not filling up, because already too many iterations.");
+#endif
+
+ break;
+ }
+
+ polled = FALSE;
+
+ for (;;) {
+ void *p;
+ snd_pcm_sframes_t frames;
+ pa_memchunk chunk;
+
+ chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
+
+ frames = (snd_pcm_sframes_t) (pa_memblock_get_length(chunk.memblock) / u->frame_size);
+
+ if (frames > (snd_pcm_sframes_t) (n_bytes/u->frame_size))
+ frames = (snd_pcm_sframes_t) (n_bytes/u->frame_size);
+
+/* pa_log_debug("%lu frames to read", (unsigned long) n); */
+
+ p = pa_memblock_acquire(chunk.memblock);
+ frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) p, (snd_pcm_uframes_t) frames);
+ pa_memblock_release(chunk.memblock);
+
+ if (PA_UNLIKELY(frames < 0)) {
+ pa_memblock_unref(chunk.memblock);
+
+ if (!after_avail && (int) frames == -EAGAIN)
+ break;
+
+ if ((r = try_recover(u, "snd_pcm_readi", (int) frames)) == 0)
+ continue;
+
+ return r;
+ }
+
+ if (!after_avail && frames == 0) {
+ pa_memblock_unref(chunk.memblock);
+ break;
+ }
+
+ pa_assert(frames > 0);
+ after_avail = FALSE;
+
+ chunk.index = 0;
+ chunk.length = (size_t) frames * u->frame_size;
+
+ pa_source_post(u->source, &chunk);
+ pa_memblock_unref(chunk.memblock);
+
+ work_done = TRUE;
+
+ u->read_count += frames * u->frame_size;
+
+/* pa_log_debug("read %lu frames", (unsigned long) frames); */
+
+ if ((size_t) frames * u->frame_size >= n_bytes)
+ break;
+
+ n_bytes -= (size_t) frames * u->frame_size;
+ }
+ }
+
+ if (u->use_tsched) {
+ *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec);
+
+ if (*sleep_usec > process_usec)
+ *sleep_usec -= process_usec;
+ else
+ *sleep_usec = 0;
+ }
+
+ return work_done ? 1 : 0;
+}
+
+static void update_smoother(struct userdata *u) {
+ snd_pcm_sframes_t delay = 0;
+ uint64_t position;
+ int err;
+ pa_usec_t now1 = 0, now2;
+ snd_pcm_status_t *status;
+
+ snd_pcm_status_alloca(&status);
+
+ pa_assert(u);
+ pa_assert(u->pcm_handle);
+
+ /* Let's update the time smoother */
+
+ if (PA_UNLIKELY((err = pa_alsa_safe_delay(u->pcm_handle, &delay, u->hwbuf_size, &u->source->sample_spec, TRUE)) < 0)) {
+ pa_log_warn("Failed to get delay: %s", pa_alsa_strerror(err));
+ return;
+ }
+
+ if (PA_UNLIKELY((err = snd_pcm_status(u->pcm_handle, status)) < 0))
+ pa_log_warn("Failed to get timestamp: %s", pa_alsa_strerror(err));
+ else {
+ snd_htimestamp_t htstamp = { 0, 0 };
+ snd_pcm_status_get_htstamp(status, &htstamp);
+ now1 = pa_timespec_load(&htstamp);
+ }
+
+ /* Hmm, if the timestamp is 0, then it wasn't set and we take the current time */
+ if (now1 <= 0)
+ now1 = pa_rtclock_now();
+
+ /* check if the time since the last update is bigger than the interval */
+ if (u->last_smoother_update > 0)
+ if (u->last_smoother_update + u->smoother_interval > now1)
+ return;
+
+ position = u->read_count + ((uint64_t) delay * (uint64_t) u->frame_size);
+ now2 = pa_bytes_to_usec(position, &u->source->sample_spec);
+
+ pa_smoother_put(u->smoother, now1, now2);
+
+ u->last_smoother_update = now1;
+ /* exponentially increase the update interval up to the MAX limit */
+ u->smoother_interval = PA_MIN (u->smoother_interval * 2, SMOOTHER_MAX_INTERVAL);
+}
+
+static pa_usec_t source_get_latency(struct userdata *u) {
+ int64_t delay;
+ pa_usec_t now1, now2;
+
+ pa_assert(u);
+
+ now1 = pa_rtclock_now();
+ now2 = pa_smoother_get(u->smoother, now1);
+
+ delay = (int64_t) now2 - (int64_t) pa_bytes_to_usec(u->read_count, &u->source->sample_spec);
+
+ return delay >= 0 ? (pa_usec_t) delay : 0;
+}
+
+static int build_pollfd(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(u->pcm_handle);
+
+ if (u->alsa_rtpoll_item)
+ pa_rtpoll_item_free(u->alsa_rtpoll_item);
+
+ if (!(u->alsa_rtpoll_item = pa_alsa_build_pollfd(u->pcm_handle, u->rtpoll)))
+ return -1;
+
+ return 0;
+}
+
+/* Called from IO context */
+static int suspend(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(u->pcm_handle);
+
+ pa_smoother_pause(u->smoother, pa_rtclock_now());
+
+ /* Let's suspend */
+ snd_pcm_close(u->pcm_handle);
+ u->pcm_handle = NULL;
+
+ if (u->alsa_rtpoll_item) {
+ pa_rtpoll_item_free(u->alsa_rtpoll_item);
+ u->alsa_rtpoll_item = NULL;
+ }
+
+ pa_log_info("Device suspended...");
+
+ return 0;
+}
+
+/* Called from IO context */
+static int update_sw_params(struct userdata *u) {
+ snd_pcm_uframes_t avail_min;
+ int err;
+
+ pa_assert(u);
+
+ /* Use the full buffer if noone asked us for anything specific */
+ u->hwbuf_unused = 0;
+
+ if (u->use_tsched) {
+ pa_usec_t latency;
+
+ if ((latency = pa_source_get_requested_latency_within_thread(u->source)) != (pa_usec_t) -1) {
+ size_t b;
+
+ pa_log_debug("latency set to %0.2fms", (double) latency / PA_USEC_PER_MSEC);
+
+ b = pa_usec_to_bytes(latency, &u->source->sample_spec);
+
+ /* We need at least one sample in our buffer */
+
+ if (PA_UNLIKELY(b < u->frame_size))
+ b = u->frame_size;
+
+ u->hwbuf_unused = PA_LIKELY(b < u->hwbuf_size) ? (u->hwbuf_size - b) : 0;
+ }
+
+ fix_min_sleep_wakeup(u);
+ fix_tsched_watermark(u);
+ }
+
+ pa_log_debug("hwbuf_unused=%lu", (unsigned long) u->hwbuf_unused);
+
+ avail_min = 1;
+
+ if (u->use_tsched) {
+ pa_usec_t sleep_usec, process_usec;
+
+ hw_sleep_time(u, &sleep_usec, &process_usec);
+ avail_min += pa_usec_to_bytes(sleep_usec, &u->source->sample_spec) / u->frame_size;
+ }
+
+ pa_log_debug("setting avail_min=%lu", (unsigned long) avail_min);
+
+ if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min, !u->use_tsched)) < 0) {
+ pa_log("Failed to set software parameters: %s", pa_alsa_strerror(err));
+ return err;
+ }
+
+ return 0;
+}
+
+/* Called from IO context */
+static int unsuspend(struct userdata *u) {
+ pa_sample_spec ss;
+ int err;
+ pa_bool_t b, d;
+ snd_pcm_uframes_t period_size, buffer_size;
+
+ pa_assert(u);
+ pa_assert(!u->pcm_handle);
+
+ pa_log_info("Trying resume...");
+
+ if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_CAPTURE,
+ SND_PCM_NONBLOCK|
+ SND_PCM_NO_AUTO_RESAMPLE|
+ SND_PCM_NO_AUTO_CHANNELS|
+ SND_PCM_NO_AUTO_FORMAT)) < 0) {
+ pa_log("Error opening PCM device %s: %s", u->device_name, pa_alsa_strerror(err));
+ goto fail;
+ }
+
+ ss = u->source->sample_spec;
+ period_size = u->fragment_size / u->frame_size;
+ buffer_size = u->hwbuf_size / u->frame_size;
+ b = u->use_mmap;
+ d = u->use_tsched;
+
+ if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &period_size, &buffer_size, 0, &b, &d, TRUE)) < 0) {
+ pa_log("Failed to set hardware parameters: %s", pa_alsa_strerror(err));
+ goto fail;
+ }
+
+ if (b != u->use_mmap || d != u->use_tsched) {
+ pa_log_warn("Resume failed, couldn't get original access mode.");
+ goto fail;
+ }
+
+ if (!pa_sample_spec_equal(&ss, &u->source->sample_spec)) {
+ pa_log_warn("Resume failed, couldn't restore original sample settings.");
+ goto fail;
+ }
+
+ if (period_size*u->frame_size != u->fragment_size ||
+ buffer_size*u->frame_size != u->hwbuf_size) {
+ pa_log_warn("Resume failed, couldn't restore original fragment settings. (Old: %lu/%lu, New %lu/%lu)",
+ (unsigned long) u->hwbuf_size, (unsigned long) u->fragment_size,
+ (unsigned long) (buffer_size*u->frame_size), (unsigned long) (period_size*u->frame_size));
+ goto fail;
+ }
+
+ if (update_sw_params(u) < 0)
+ goto fail;
+
+ if (build_pollfd(u) < 0)
+ goto fail;
+
+ /* FIXME: We need to reload the volume somehow */
+
+ u->read_count = 0;
+ pa_smoother_reset(u->smoother, pa_rtclock_now(), TRUE);
+ u->smoother_interval = SMOOTHER_MIN_INTERVAL;
+ u->last_smoother_update = 0;
+
+ u->first = TRUE;
+
+ pa_log_info("Resumed successfully...");
+
+ return 0;
+
+fail:
+ if (u->pcm_handle) {
+ snd_pcm_close(u->pcm_handle);
+ u->pcm_handle = NULL;
+ }
+
+ return -PA_ERR_IO;
+}
+
+/* Called from IO context */
+static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE(o)->userdata;
+
+ switch (code) {
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ pa_usec_t r = 0;
+
+ if (u->pcm_handle)
+ r = source_get_latency(u);
+
+ *((pa_usec_t*) data) = r;
+
+ return 0;
+ }
+
+ case PA_SOURCE_MESSAGE_SET_STATE:
+
+ switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
+
+ case PA_SOURCE_SUSPENDED: {
+ int r;
+
+ pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
+
+ if ((r = suspend(u)) < 0)
+ return r;
+
+ break;
+ }
+
+ case PA_SOURCE_IDLE:
+ case PA_SOURCE_RUNNING: {
+ int r;
+
+ if (u->source->thread_info.state == PA_SOURCE_INIT) {
+ if (build_pollfd(u) < 0)
+ return -PA_ERR_IO;
+ }
+
+ if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
+ if ((r = unsuspend(u)) < 0)
+ return r;
+ }
+
+ break;
+ }
+
+ case PA_SOURCE_UNLINKED:
+ case PA_SOURCE_INIT:
+ case PA_SOURCE_INVALID_STATE:
+ ;
+ }
+
+ break;
+ }
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from main context */
+static int source_set_state_cb(pa_source *s, pa_source_state_t new_state) {
+ pa_source_state_t old_state;
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ old_state = pa_source_get_state(u->source);
+
+ if (PA_SOURCE_IS_OPENED(old_state) && new_state == PA_SOURCE_SUSPENDED)
+ reserve_done(u);
+ else if (old_state == PA_SOURCE_SUSPENDED && PA_SOURCE_IS_OPENED(new_state))
+ if (reserve_init(u, u->device_name) < 0)
+ return -PA_ERR_BUSY;
+
+ return 0;
+}
+
+static int ctl_mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
+ struct userdata *u = snd_mixer_elem_get_callback_private(elem);
+
+ pa_assert(u);
+ pa_assert(u->mixer_handle);
+
+ if (mask == SND_CTL_EVENT_MASK_REMOVE)
+ return 0;
+
+ if (u->source->suspend_cause & PA_SUSPEND_SESSION)
+ return 0;
+
+ if (mask & SND_CTL_EVENT_MASK_VALUE) {
+ pa_source_get_volume(u->source, TRUE);
+ pa_source_get_mute(u->source, TRUE);
+ }
+
+ return 0;
+}
+
+static int io_mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
+ struct userdata *u = snd_mixer_elem_get_callback_private(elem);
+
+ pa_assert(u);
+ pa_assert(u->mixer_handle);
+
+ if (mask == SND_CTL_EVENT_MASK_REMOVE)
+ return 0;
+
+ if (u->source->suspend_cause & PA_SUSPEND_SESSION)
+ return 0;
+
+ if (mask & SND_CTL_EVENT_MASK_VALUE)
+ pa_source_update_volume_and_mute(u->source);
+
+ return 0;
+}
+
+static void source_get_volume_cb(pa_source *s) {
+ struct userdata *u = s->userdata;
+ pa_cvolume r;
+ char vol_str_pcnt[PA_CVOLUME_SNPRINT_MAX];
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+
+ if (pa_alsa_path_get_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r) < 0)
+ return;
+
+ /* Shift down by the base volume, so that 0dB becomes maximum volume */
+ pa_sw_cvolume_multiply_scalar(&r, &r, s->base_volume);
+
+ pa_log_debug("Read hardware volume: %s", pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &r));
+
+ if (u->mixer_path->has_dB) {
+ char vol_str_db[PA_SW_CVOLUME_SNPRINT_DB_MAX];
+
+ pa_log_debug(" in dB: %s", pa_sw_cvolume_snprint_dB(vol_str_db, sizeof(vol_str_db), &r));
+ }
+
+ if (pa_cvolume_equal(&u->hardware_volume, &r))
+ return;
+
+ s->real_volume = u->hardware_volume = r;
+
+ /* Hmm, so the hardware volume changed, let's reset our software volume */
+ if (u->mixer_path->has_dB)
+ pa_source_set_soft_volume(s, NULL);
+}
+
+static void source_set_volume_cb(pa_source *s) {
+ struct userdata *u = s->userdata;
+ pa_cvolume r;
+ char vol_str_pcnt[PA_CVOLUME_SNPRINT_MAX];
+ pa_bool_t sync_volume = !!(s->flags & PA_SOURCE_SYNC_VOLUME);
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+
+ /* Shift up by the base volume */
+ pa_sw_cvolume_divide_scalar(&r, &s->real_volume, s->base_volume);
+
+ if (pa_alsa_path_set_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r, sync_volume, !sync_volume) < 0)
+ return;
+
+ /* Shift down by the base volume, so that 0dB becomes maximum volume */
+ pa_sw_cvolume_multiply_scalar(&r, &r, s->base_volume);
+
+ u->hardware_volume = r;
+
+ if (u->mixer_path->has_dB) {
+ pa_cvolume new_soft_volume;
+ pa_bool_t accurate_enough;
+ char vol_str_db[PA_SW_CVOLUME_SNPRINT_DB_MAX];
+
+ /* Match exactly what the user requested by software */
+ pa_sw_cvolume_divide(&new_soft_volume, &s->real_volume, &u->hardware_volume);
+
+ /* If the adjustment to do in software is only minimal we
+ * can skip it. That saves us CPU at the expense of a bit of
+ * accuracy */
+ accurate_enough =
+ (pa_cvolume_min(&new_soft_volume) >= (PA_VOLUME_NORM - VOLUME_ACCURACY)) &&
+ (pa_cvolume_max(&new_soft_volume) <= (PA_VOLUME_NORM + VOLUME_ACCURACY));
+
+ pa_log_debug("Requested volume: %s", pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &s->real_volume));
+ pa_log_debug(" in dB: %s", pa_sw_cvolume_snprint_dB(vol_str_db, sizeof(vol_str_db), &s->real_volume));
+ pa_log_debug("Got hardware volume: %s", pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &u->hardware_volume));
+ pa_log_debug(" in dB: %s", pa_sw_cvolume_snprint_dB(vol_str_db, sizeof(vol_str_db), &u->hardware_volume));
+ pa_log_debug("Calculated software volume: %s (accurate-enough=%s)",
+ pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &new_soft_volume),
+ pa_yes_no(accurate_enough));
+ pa_log_debug(" in dB: %s", pa_sw_cvolume_snprint_dB(vol_str_db, sizeof(vol_str_db), &new_soft_volume));
+
+ if (!accurate_enough)
+ s->soft_volume = new_soft_volume;
+
+ } else {
+ pa_log_debug("Wrote hardware volume: %s", pa_cvolume_snprint(vol_str_pcnt, sizeof(vol_str_pcnt), &r));
+
+ /* We can't match exactly what the user requested, hence let's
+ * at least tell the user about it */
+
+ s->real_volume = r;
+ }
+}
+
+static void source_write_volume_cb(pa_source *s) {
+ struct userdata *u = s->userdata;
+ pa_cvolume hw_vol = s->thread_info.current_hw_volume;
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+ pa_assert(s->flags & PA_SOURCE_SYNC_VOLUME);
+
+ /* Shift up by the base volume */
+ pa_sw_cvolume_divide_scalar(&hw_vol, &hw_vol, s->base_volume);
+
+ if (pa_alsa_path_set_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &hw_vol, TRUE, TRUE) < 0)
+ pa_log_error("Writing HW volume failed");
+ else {
+ pa_cvolume tmp_vol;
+ pa_bool_t accurate_enough;
+
+ /* Shift down by the base volume, so that 0dB becomes maximum volume */
+ pa_sw_cvolume_multiply_scalar(&hw_vol, &hw_vol, s->base_volume);
+
+ pa_sw_cvolume_divide(&tmp_vol, &hw_vol, &s->thread_info.current_hw_volume);
+ accurate_enough =
+ (pa_cvolume_min(&tmp_vol) >= (PA_VOLUME_NORM - VOLUME_ACCURACY)) &&
+ (pa_cvolume_max(&tmp_vol) <= (PA_VOLUME_NORM + VOLUME_ACCURACY));
+
+ if (!accurate_enough) {
+ union {
+ char db[2][PA_SW_CVOLUME_SNPRINT_DB_MAX];
+ char pcnt[2][PA_CVOLUME_SNPRINT_MAX];
+ } vol;
+
+ pa_log_debug("Written HW volume did not match with the request: %s (request) != %s",
+ pa_cvolume_snprint(vol.pcnt[0], sizeof(vol.pcnt[0]), &s->thread_info.current_hw_volume),
+ pa_cvolume_snprint(vol.pcnt[1], sizeof(vol.pcnt[1]), &hw_vol));
+ pa_log_debug(" in dB: %s (request) != %s",
+ pa_sw_cvolume_snprint_dB(vol.db[0], sizeof(vol.db[0]), &s->thread_info.current_hw_volume),
+ pa_sw_cvolume_snprint_dB(vol.db[1], sizeof(vol.db[1]), &hw_vol));
+ }
+ }
+}
+
+static void source_get_mute_cb(pa_source *s) {
+ struct userdata *u = s->userdata;
+ pa_bool_t b;
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+
+ if (pa_alsa_path_get_mute(u->mixer_path, u->mixer_handle, &b) < 0)
+ return;
+
+ s->muted = b;
+}
+
+static void source_set_mute_cb(pa_source *s) {
+ struct userdata *u = s->userdata;
+
+ pa_assert(u);
+ pa_assert(u->mixer_path);
+ pa_assert(u->mixer_handle);
+
+ pa_alsa_path_set_mute(u->mixer_path, u->mixer_handle, s->muted);
+}
+
+static int source_set_port_cb(pa_source *s, pa_device_port *p) {
+ struct userdata *u = s->userdata;
+ pa_alsa_port_data *data;
+
+ pa_assert(u);
+ pa_assert(p);
+ pa_assert(u->mixer_handle);
+
+ data = PA_DEVICE_PORT_DATA(p);
+
+ pa_assert_se(u->mixer_path = data->path);
+ pa_alsa_path_select(u->mixer_path, u->mixer_handle);
+
+ if (u->mixer_path->has_volume && u->mixer_path->has_dB) {
+ s->base_volume = pa_sw_volume_from_dB(-u->mixer_path->max_dB);
+ s->n_volume_steps = PA_VOLUME_NORM+1;
+
+ pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(s->base_volume));
+ } else {
+ s->base_volume = PA_VOLUME_NORM;
+ s->n_volume_steps = u->mixer_path->max_volume - u->mixer_path->min_volume + 1;
+ }
+
+ if (data->setting)
+ pa_alsa_setting_select(data->setting, u->mixer_handle);
+
+ if (s->set_mute)
+ s->set_mute(s);
+ if (s->set_volume)
+ s->set_volume(s);
+
+ return 0;
+}
+
+static void source_update_requested_latency_cb(pa_source *s) {
+ struct userdata *u = s->userdata;
+ pa_assert(u);
+ pa_assert(u->use_tsched); /* only when timer scheduling is used
+ * we can dynamically adjust the
+ * latency */
+
+ if (!u->pcm_handle)
+ return;
+
+ update_sw_params(u);
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+ unsigned short revents = 0;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority);
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ for (;;) {
+ int ret;
+ pa_usec_t rtpoll_sleep = 0;
+
+#ifdef DEBUG_TIMING
+ pa_log_debug("Loop");
+#endif
+
+ /* Read some data and pass it to the sources */
+ if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
+ int work_done;
+ pa_usec_t sleep_usec = 0;
+ pa_bool_t on_timeout = pa_rtpoll_timer_elapsed(u->rtpoll);
+
+ if (u->first) {
+ pa_log_info("Starting capture.");
+ snd_pcm_start(u->pcm_handle);
+
+ pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE);
+
+ u->first = FALSE;
+ }
+
+ if (u->use_mmap)
+ work_done = mmap_read(u, &sleep_usec, revents & POLLIN, on_timeout);
+ else
+ work_done = unix_read(u, &sleep_usec, revents & POLLIN, on_timeout);
+
+ if (work_done < 0)
+ goto fail;
+
+/* pa_log_debug("work_done = %i", work_done); */
+
+ if (work_done)
+ update_smoother(u);
+
+ if (u->use_tsched) {
+ pa_usec_t cusec;
+
+ /* OK, the capture buffer is now empty, let's
+ * calculate when to wake up next */
+
+/* pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) sleep_usec / PA_USEC_PER_MSEC); */
+
+ /* Convert from the sound card time domain to the
+ * system time domain */
+ cusec = pa_smoother_translate(u->smoother, pa_rtclock_now(), sleep_usec);
+
+/* pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC); */
+
+ /* We don't trust the conversion, so we wake up whatever comes first */
+ rtpoll_sleep = PA_MIN(sleep_usec, cusec);
+ }
+ }
+
+ if (u->source->flags & PA_SOURCE_SYNC_VOLUME) {
+ pa_usec_t volume_sleep;
+ pa_source_volume_change_apply(u->source, &volume_sleep);
+ if (volume_sleep > 0)
+ rtpoll_sleep = PA_MIN(volume_sleep, rtpoll_sleep);
+ }
+
+ if (rtpoll_sleep > 0)
+ pa_rtpoll_set_timer_relative(u->rtpoll, rtpoll_sleep);
+ else
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
+
+ /* Hmm, nothing to do. Let's sleep */
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (u->source->flags & PA_SOURCE_SYNC_VOLUME)
+ pa_source_volume_change_apply(u->source, NULL);
+
+ if (ret == 0)
+ goto finish;
+
+ /* Tell ALSA about this and process its response */
+ if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
+ struct pollfd *pollfd;
+ int err;
+ unsigned n;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
+
+ if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
+ pa_log("snd_pcm_poll_descriptors_revents() failed: %s", pa_alsa_strerror(err));
+ goto fail;
+ }
+
+ if (revents & ~POLLIN) {
+ if (pa_alsa_recover_from_poll(u->pcm_handle, revents) < 0)
+ goto fail;
+
+ u->first = TRUE;
+ } else if (revents && u->use_tsched && pa_log_ratelimit(PA_LOG_DEBUG))
+ pa_log_debug("Wakeup from ALSA!");
+
+ } else
+ revents = 0;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+static void set_source_name(pa_source_new_data *data, pa_modargs *ma, const char *device_id, const char *device_name, pa_alsa_mapping *mapping) {
+ const char *n;
+ char *t;
+
+ pa_assert(data);
+ pa_assert(ma);
+ pa_assert(device_name);
+
+ if ((n = pa_modargs_get_value(ma, "source_name", NULL))) {
+ pa_source_new_data_set_name(data, n);
+ data->namereg_fail = TRUE;
+ return;
+ }
+
+ if ((n = pa_modargs_get_value(ma, "name", NULL)))
+ data->namereg_fail = TRUE;
+ else {
+ n = device_id ? device_id : device_name;
+ data->namereg_fail = FALSE;
+ }
+
+ if (mapping)
+ t = pa_sprintf_malloc("alsa_input.%s.%s", n, mapping->name);
+ else
+ t = pa_sprintf_malloc("alsa_input.%s", n);
+
+ pa_source_new_data_set_name(data, t);
+ pa_xfree(t);
+}
+
+static void find_mixer(struct userdata *u, pa_alsa_mapping *mapping, const char *element, pa_bool_t ignore_dB) {
+
+ if (!mapping && !element)
+ return;
+
+ if (!(u->mixer_handle = pa_alsa_open_mixer_for_pcm(u->pcm_handle, &u->control_device))) {
+ pa_log_info("Failed to find a working mixer device.");
+ return;
+ }
+
+ if (element) {
+
+ if (!(u->mixer_path = pa_alsa_path_synthesize(element, PA_ALSA_DIRECTION_INPUT)))
+ goto fail;
+
+ if (pa_alsa_path_probe(u->mixer_path, u->mixer_handle, ignore_dB) < 0)
+ goto fail;
+
+ pa_log_debug("Probed mixer path %s:", u->mixer_path->name);
+ pa_alsa_path_dump(u->mixer_path);
+ } else {
+
+ if (!(u->mixer_path_set = pa_alsa_path_set_new(mapping, PA_ALSA_DIRECTION_INPUT)))
+ goto fail;
+
+ pa_alsa_path_set_probe(u->mixer_path_set, u->mixer_handle, ignore_dB);
+
+ pa_log_debug("Probed mixer paths:");
+ pa_alsa_path_set_dump(u->mixer_path_set);
+ }
+
+ return;
+
+fail:
+
+ if (u->mixer_path_set) {
+ pa_alsa_path_set_free(u->mixer_path_set);
+ u->mixer_path_set = NULL;
+ } else if (u->mixer_path) {
+ pa_alsa_path_free(u->mixer_path);
+ u->mixer_path = NULL;
+ }
+
+ if (u->mixer_handle) {
+ snd_mixer_close(u->mixer_handle);
+ u->mixer_handle = NULL;
+ }
+}
+
+static int setup_mixer(struct userdata *u, pa_bool_t ignore_dB, pa_bool_t sync_volume) {
+ pa_assert(u);
+
+ if (!u->mixer_handle)
+ return 0;
+
+ if (u->source->active_port) {
+ pa_alsa_port_data *data;
+
+ /* We have a list of supported paths, so let's activate the
+ * one that has been chosen as active */
+
+ data = PA_DEVICE_PORT_DATA(u->source->active_port);
+ u->mixer_path = data->path;
+
+ pa_alsa_path_select(data->path, u->mixer_handle);
+
+ if (data->setting)
+ pa_alsa_setting_select(data->setting, u->mixer_handle);
+
+ } else {
+
+ if (!u->mixer_path && u->mixer_path_set)
+ u->mixer_path = u->mixer_path_set->paths;
+
+ if (u->mixer_path) {
+ /* Hmm, we have only a single path, then let's activate it */
+
+ pa_alsa_path_select(u->mixer_path, u->mixer_handle);
+
+ if (u->mixer_path->settings)
+ pa_alsa_setting_select(u->mixer_path->settings, u->mixer_handle);
+ } else
+ return 0;
+ }
+
+ if (!u->mixer_path->has_volume)
+ pa_log_info("Driver does not support hardware volume control, falling back to software volume control.");
+ else {
+
+ if (u->mixer_path->has_dB) {
+ pa_log_info("Hardware volume ranges from %0.2f dB to %0.2f dB.", u->mixer_path->min_dB, u->mixer_path->max_dB);
+
+ u->source->base_volume = pa_sw_volume_from_dB(-u->mixer_path->max_dB);
+ u->source->n_volume_steps = PA_VOLUME_NORM+1;
+
+ pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->source->base_volume));
+
+ } else {
+ pa_log_info("Hardware volume ranges from %li to %li.", u->mixer_path->min_volume, u->mixer_path->max_volume);
+ u->source->base_volume = PA_VOLUME_NORM;
+ u->source->n_volume_steps = u->mixer_path->max_volume - u->mixer_path->min_volume + 1;
+ }
+
+ u->source->get_volume = source_get_volume_cb;
+ u->source->set_volume = source_set_volume_cb;
+ u->source->write_volume = source_write_volume_cb;
+
+ u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL;
+ if (u->mixer_path->has_dB) {
+ u->source->flags |= PA_SOURCE_DECIBEL_VOLUME;
+ if (sync_volume) {
+ u->source->flags |= PA_SOURCE_SYNC_VOLUME;
+ pa_log_info("Successfully enabled synchronous volume.");
+ }
+ }
+
+ pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->mixer_path->has_dB ? "supported" : "not supported");
+ }
+
+ if (!u->mixer_path->has_mute) {
+ pa_log_info("Driver does not support hardware mute control, falling back to software mute control.");
+ } else {
+ u->source->get_mute = source_get_mute_cb;
+ u->source->set_mute = source_set_mute_cb;
+ u->source->flags |= PA_SOURCE_HW_MUTE_CTRL;
+ pa_log_info("Using hardware mute control.");
+ }
+
+ if (u->source->flags & (PA_SOURCE_HW_VOLUME_CTRL|PA_SOURCE_HW_MUTE_CTRL)) {
+ int (*mixer_callback)(snd_mixer_elem_t *, unsigned int);
+ if (u->source->flags & PA_SOURCE_SYNC_VOLUME) {
+ u->mixer_pd = pa_alsa_mixer_pdata_new();
+ mixer_callback = io_mixer_callback;
+
+ if (pa_alsa_set_mixer_rtpoll(u->mixer_pd, u->mixer_handle, u->rtpoll) < 0) {
+ pa_log("Failed to initialize file descriptor monitoring");
+ return -1;
+ }
+ } else {
+ u->mixer_fdl = pa_alsa_fdlist_new();
+ mixer_callback = ctl_mixer_callback;
+
+ if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, u->core->mainloop) < 0) {
+ pa_log("Failed to initialize file descriptor monitoring");
+ return -1;
+ }
+ }
+
+ if (u->mixer_path_set)
+ pa_alsa_path_set_set_callback(u->mixer_path_set, u->mixer_handle, mixer_callback, u);
+ else
+ pa_alsa_path_set_callback(u->mixer_path, u->mixer_handle, mixer_callback, u);
+ }
+
+ return 0;
+}
+
+pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, pa_alsa_mapping *mapping) {
+
+ struct userdata *u = NULL;
+ const char *dev_id = NULL;
+ pa_sample_spec ss, requested_ss;
+ pa_channel_map map;
+ uint32_t nfrags, frag_size, buffer_size, tsched_size, tsched_watermark;
+ snd_pcm_uframes_t period_frames, buffer_frames, tsched_frames;
+ size_t frame_size;
+ pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d, ignore_dB = FALSE, namereg_fail = FALSE, sync_volume = FALSE;
+ pa_source_new_data data;
+ pa_alsa_profile_set *profile_set = NULL;
+
+ pa_assert(m);
+ pa_assert(ma);
+
+ ss = m->core->default_sample_spec;
+ map = m->core->default_channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
+ pa_log("Failed to parse sample specification and channel map");
+ goto fail;
+ }
+
+ requested_ss = ss;
+ frame_size = pa_frame_size(&ss);
+
+ nfrags = m->core->default_n_fragments;
+ frag_size = (uint32_t) pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
+ if (frag_size <= 0)
+ frag_size = (uint32_t) frame_size;
+ tsched_size = (uint32_t) pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC, &ss);
+ tsched_watermark = (uint32_t) pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC, &ss);
+
+ if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 ||
+ pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0 ||
+ pa_modargs_get_value_u32(ma, "tsched_buffer_size", &tsched_size) < 0 ||
+ pa_modargs_get_value_u32(ma, "tsched_buffer_watermark", &tsched_watermark) < 0) {
+ pa_log("Failed to parse buffer metrics");
+ goto fail;
+ }
+
+ buffer_size = nfrags * frag_size;
+
+ period_frames = frag_size/frame_size;
+ buffer_frames = buffer_size/frame_size;
+ tsched_frames = tsched_size/frame_size;
+
+ if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
+ pa_log("Failed to parse mmap argument.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
+ pa_log("Failed to parse tsched argument.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
+ pa_log("Failed to parse ignore_dB argument.");
+ goto fail;
+ }
+
+ sync_volume = m->core->sync_volume;
+ if (pa_modargs_get_value_boolean(ma, "sync_volume", &sync_volume) < 0) {
+ pa_log("Failed to parse sync_volume argument.");
+ goto fail;
+ }
+
+ use_tsched = pa_alsa_may_tsched(use_tsched);
+
+ u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->use_mmap = use_mmap;
+ u->use_tsched = use_tsched;
+ u->first = TRUE;
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ u->smoother = pa_smoother_new(
+ SMOOTHER_ADJUST_USEC,
+ SMOOTHER_WINDOW_USEC,
+ TRUE,
+ TRUE,
+ 5,
+ pa_rtclock_now(),
+ TRUE);
+ u->smoother_interval = SMOOTHER_MIN_INTERVAL;
+
+ dev_id = pa_modargs_get_value(
+ ma, "device_id",
+ pa_modargs_get_value(ma, "device", DEFAULT_DEVICE));
+
+ if (reserve_init(u, dev_id) < 0)
+ goto fail;
+
+ if (reserve_monitor_init(u, dev_id) < 0)
+ goto fail;
+
+ b = use_mmap;
+ d = use_tsched;
+
+ if (mapping) {
+
+ if (!(dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
+ pa_log("device_id= not set");
+ goto fail;
+ }
+
+ if (!(u->pcm_handle = pa_alsa_open_by_device_id_mapping(
+ dev_id,
+ &u->device_name,
+ &ss, &map,
+ SND_PCM_STREAM_CAPTURE,
+ &period_frames, &buffer_frames, tsched_frames,
+ &b, &d, mapping)))
+ goto fail;
+
+ } else if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
+
+ if (!(profile_set = pa_alsa_profile_set_new(NULL, &map)))
+ goto fail;
+
+ if (!(u->pcm_handle = pa_alsa_open_by_device_id_auto(
+ dev_id,
+ &u->device_name,
+ &ss, &map,
+ SND_PCM_STREAM_CAPTURE,
+ &period_frames, &buffer_frames, tsched_frames,
+ &b, &d, profile_set, &mapping)))
+ goto fail;
+
+ } else {
+
+ if (!(u->pcm_handle = pa_alsa_open_by_device_string(
+ pa_modargs_get_value(ma, "device", DEFAULT_DEVICE),
+ &u->device_name,
+ &ss, &map,
+ SND_PCM_STREAM_CAPTURE,
+ &period_frames, &buffer_frames, tsched_frames,
+ &b, &d, FALSE)))
+ goto fail;
+ }
+
+ pa_assert(u->device_name);
+ pa_log_info("Successfully opened device %s.", u->device_name);
+
+ if (pa_alsa_pcm_is_modem(u->pcm_handle)) {
+ pa_log_notice("Device %s is modem, refusing further initialization.", u->device_name);
+ goto fail;
+ }
+
+ if (mapping)
+ pa_log_info("Selected mapping '%s' (%s).", mapping->description, mapping->name);
+
+ if (use_mmap && !b) {
+ pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
+ u->use_mmap = use_mmap = FALSE;
+ }
+
+ if (use_tsched && (!b || !d)) {
+ pa_log_info("Cannot enable timer-based scheduling, falling back to sound IRQ scheduling.");
+ u->use_tsched = use_tsched = FALSE;
+ }
+
+ if (u->use_mmap)
+ pa_log_info("Successfully enabled mmap() mode.");
+
+ if (u->use_tsched)
+ pa_log_info("Successfully enabled timer-based scheduling mode.");
+
+ /* ALSA might tweak the sample spec, so recalculate the frame size */
+ frame_size = pa_frame_size(&ss);
+
+ find_mixer(u, mapping, pa_modargs_get_value(ma, "control", NULL), ignore_dB);
+
+ pa_source_new_data_init(&data);
+ data.driver = driver;
+ data.module = m;
+ data.card = card;
+ set_source_name(&data, ma, dev_id, u->device_name, mapping);
+
+ /* We need to give pa_modargs_get_value_boolean() a pointer to a local
+ * variable instead of using &data.namereg_fail directly, because
+ * data.namereg_fail is a bitfield and taking the address of a bitfield
+ * variable is impossible. */
+ namereg_fail = data.namereg_fail;
+ if (pa_modargs_get_value_boolean(ma, "namereg_fail", &namereg_fail) < 0) {
+ pa_log("Failed to parse boolean argument namereg_fail.");
+ pa_source_new_data_done(&data);
+ goto fail;
+ }
+ data.namereg_fail = namereg_fail;
+
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+
+ pa_alsa_init_proplist_pcm(m->core, data.proplist, u->pcm_handle);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (buffer_frames * frame_size));
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (period_frames * frame_size));
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap+timer" : (u->use_mmap ? "mmap" : "serial"));
+
+ if (mapping) {
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_NAME, mapping->name);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_DESCRIPTION, mapping->description);
+ }
+
+ pa_alsa_init_description(data.proplist);
+
+ if (u->control_device)
+ pa_alsa_init_proplist_ctl(data.proplist, u->control_device);
+
+ if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&data);
+ goto fail;
+ }
+
+ if (u->mixer_path_set)
+ pa_alsa_add_ports(&data.ports, u->mixer_path_set);
+
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY|(u->use_tsched ? PA_SOURCE_DYNAMIC_LATENCY : 0));
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
+ pa_log("Failed to create source object");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_u32(ma, "sync_volume_safety_margin",
+ &u->source->thread_info.volume_change_safety_margin) < 0) {
+ pa_log("Failed to parse sync_volume_safety_margin parameter");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_s32(ma, "sync_volume_extra_delay",
+ &u->source->thread_info.volume_change_extra_delay) < 0) {
+ pa_log("Failed to parse sync_volume_extra_delay parameter");
+ goto fail;
+ }
+
+ u->source->parent.process_msg = source_process_msg;
+ if (u->use_tsched)
+ u->source->update_requested_latency = source_update_requested_latency_cb;
+ u->source->set_state = source_set_state_cb;
+ u->source->set_port = source_set_port_cb;
+ u->source->userdata = u;
+
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+
+ u->frame_size = frame_size;
+ u->fragment_size = frag_size = (size_t) (period_frames * frame_size);
+ u->hwbuf_size = buffer_size = (size_t) (buffer_frames * frame_size);
+ pa_cvolume_mute(&u->hardware_volume, u->source->sample_spec.channels);
+
+ pa_log_info("Using %0.1f fragments of size %lu bytes (%0.2fms), buffer size is %lu bytes (%0.2fms)",
+ (double) u->hwbuf_size / (double) u->fragment_size,
+ (long unsigned) u->fragment_size,
+ (double) pa_bytes_to_usec(u->fragment_size, &ss) / PA_USEC_PER_MSEC,
+ (long unsigned) u->hwbuf_size,
+ (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC);
+
+ if (u->use_tsched) {
+ u->tsched_watermark = pa_usec_to_bytes_round_up(pa_bytes_to_usec_round_up(tsched_watermark, &requested_ss), &u->source->sample_spec);
+
+ u->watermark_inc_step = pa_usec_to_bytes(TSCHED_WATERMARK_INC_STEP_USEC, &u->source->sample_spec);
+ u->watermark_dec_step = pa_usec_to_bytes(TSCHED_WATERMARK_DEC_STEP_USEC, &u->source->sample_spec);
+
+ u->watermark_inc_threshold = pa_usec_to_bytes_round_up(TSCHED_WATERMARK_INC_THRESHOLD_USEC, &u->source->sample_spec);
+ u->watermark_dec_threshold = pa_usec_to_bytes_round_up(TSCHED_WATERMARK_DEC_THRESHOLD_USEC, &u->source->sample_spec);
+
+ fix_min_sleep_wakeup(u);
+ fix_tsched_watermark(u);
+
+ pa_source_set_latency_range(u->source,
+ 0,
+ pa_bytes_to_usec(u->hwbuf_size, &ss));
+
+ pa_log_info("Time scheduling watermark is %0.2fms",
+ (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC);
+ } else
+ pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(u->hwbuf_size, &ss));
+
+ reserve_update(u);
+
+ if (update_sw_params(u) < 0)
+ goto fail;
+
+ if (setup_mixer(u, ignore_dB, sync_volume) < 0)
+ goto fail;
+
+ pa_alsa_dump(PA_LOG_DEBUG, u->pcm_handle);
+
+ if (!(u->thread = pa_thread_new("alsa-source", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ /* Get initial mixer settings */
+ if (data.volume_is_set) {
+ if (u->source->set_volume)
+ u->source->set_volume(u->source);
+ } else {
+ if (u->source->get_volume)
+ u->source->get_volume(u->source);
+ }
+
+ if (data.muted_is_set) {
+ if (u->source->set_mute)
+ u->source->set_mute(u->source);
+ } else {
+ if (u->source->get_mute)
+ u->source->get_mute(u->source);
+ }
+
+ pa_source_put(u->source);
+
+ if (profile_set)
+ pa_alsa_profile_set_free(profile_set);
+
+ return u->source;
+
+fail:
+
+ if (u)
+ userdata_free(u);
+
+ if (profile_set)
+ pa_alsa_profile_set_free(profile_set);
+
+ return NULL;
+}
+
+static void userdata_free(struct userdata *u) {
+ pa_assert(u);
+
+ if (u->source)
+ pa_source_unlink(u->source);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->source)
+ pa_source_unref(u->source);
+
+ if (u->mixer_pd)
+ pa_alsa_mixer_pdata_free(u->mixer_pd);
+
+ if (u->alsa_rtpoll_item)
+ pa_rtpoll_item_free(u->alsa_rtpoll_item);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ if (u->pcm_handle) {
+ snd_pcm_drop(u->pcm_handle);
+ snd_pcm_close(u->pcm_handle);
+ }
+
+ if (u->mixer_fdl)
+ pa_alsa_fdlist_free(u->mixer_fdl);
+
+ if (u->mixer_path_set)
+ pa_alsa_path_set_free(u->mixer_path_set);
+ else if (u->mixer_path)
+ pa_alsa_path_free(u->mixer_path);
+
+ if (u->mixer_handle)
+ snd_mixer_close(u->mixer_handle);
+
+ if (u->smoother)
+ pa_smoother_free(u->smoother);
+
+ reserve_done(u);
+ monitor_done(u);
+
+ pa_xfree(u->device_name);
+ pa_xfree(u->control_device);
+ pa_xfree(u);
+}
+
+void pa_alsa_source_free(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ userdata_free(u);
+}
diff --git a/src/modules/alsa/alsa-source.h b/src/modules/alsa/alsa-source.h
new file mode 100644
index 00000000..5d9409e2
--- /dev/null
+++ b/src/modules/alsa/alsa-source.h
@@ -0,0 +1,36 @@
+#ifndef fooalsasourcehfoo
+#define fooalsasourcehfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/source.h>
+
+#include "alsa-util.h"
+
+pa_source* pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, pa_alsa_mapping *mapping);
+
+void pa_alsa_source_free(pa_source *s);
+
+#endif
diff --git a/src/modules/alsa/alsa-util.c b/src/modules/alsa/alsa-util.c
new file mode 100644
index 00000000..883c26f9
--- /dev/null
+++ b/src/modules/alsa/alsa-util.c
@@ -0,0 +1,1401 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2009 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <asoundlib.h>
+
+#include <pulse/sample.h>
+#include <pulse/xmalloc.h>
+#include <pulse/timeval.h>
+#include <pulse/util.h>
+#include <pulse/i18n.h>
+#include <pulse/utf8.h>
+
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/atomic.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/conf-parser.h>
+#include <pulsecore/core-rtclock.h>
+
+#include "alsa-util.h"
+#include "alsa-mixer.h"
+
+#ifdef HAVE_HAL
+#include "hal-util.h"
+#endif
+
+#ifdef HAVE_UDEV
+#include "udev-util.h"
+#endif
+
+static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
+
+ static const snd_pcm_format_t format_trans[] = {
+ [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
+ [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
+ [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
+ [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
+ [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
+ [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
+ [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
+ [PA_SAMPLE_S32LE] = SND_PCM_FORMAT_S32_LE,
+ [PA_SAMPLE_S32BE] = SND_PCM_FORMAT_S32_BE,
+ [PA_SAMPLE_S24LE] = SND_PCM_FORMAT_S24_3LE,
+ [PA_SAMPLE_S24BE] = SND_PCM_FORMAT_S24_3BE,
+ [PA_SAMPLE_S24_32LE] = SND_PCM_FORMAT_S24_LE,
+ [PA_SAMPLE_S24_32BE] = SND_PCM_FORMAT_S24_BE,
+ };
+
+ static const pa_sample_format_t try_order[] = {
+ PA_SAMPLE_FLOAT32NE,
+ PA_SAMPLE_FLOAT32RE,
+ PA_SAMPLE_S32NE,
+ PA_SAMPLE_S32RE,
+ PA_SAMPLE_S24_32NE,
+ PA_SAMPLE_S24_32RE,
+ PA_SAMPLE_S24NE,
+ PA_SAMPLE_S24RE,
+ PA_SAMPLE_S16NE,
+ PA_SAMPLE_S16RE,
+ PA_SAMPLE_ALAW,
+ PA_SAMPLE_ULAW,
+ PA_SAMPLE_U8
+ };
+
+ unsigned i;
+ int ret;
+
+ pa_assert(pcm_handle);
+ pa_assert(hwparams);
+ pa_assert(f);
+
+ if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
+ return ret;
+
+ pa_log_debug("snd_pcm_hw_params_set_format(%s) failed: %s",
+ snd_pcm_format_description(format_trans[*f]),
+ pa_alsa_strerror(ret));
+
+ if (*f == PA_SAMPLE_FLOAT32BE)
+ *f = PA_SAMPLE_FLOAT32LE;
+ else if (*f == PA_SAMPLE_FLOAT32LE)
+ *f = PA_SAMPLE_FLOAT32BE;
+ else if (*f == PA_SAMPLE_S24BE)
+ *f = PA_SAMPLE_S24LE;
+ else if (*f == PA_SAMPLE_S24LE)
+ *f = PA_SAMPLE_S24BE;
+ else if (*f == PA_SAMPLE_S24_32BE)
+ *f = PA_SAMPLE_S24_32LE;
+ else if (*f == PA_SAMPLE_S24_32LE)
+ *f = PA_SAMPLE_S24_32BE;
+ else if (*f == PA_SAMPLE_S16BE)
+ *f = PA_SAMPLE_S16LE;
+ else if (*f == PA_SAMPLE_S16LE)
+ *f = PA_SAMPLE_S16BE;
+ else if (*f == PA_SAMPLE_S32BE)
+ *f = PA_SAMPLE_S32LE;
+ else if (*f == PA_SAMPLE_S32LE)
+ *f = PA_SAMPLE_S32BE;
+ else
+ goto try_auto;
+
+ if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
+ return ret;
+
+ pa_log_debug("snd_pcm_hw_params_set_format(%s) failed: %s",
+ snd_pcm_format_description(format_trans[*f]),
+ pa_alsa_strerror(ret));
+
+try_auto:
+
+ for (i = 0; i < PA_ELEMENTSOF(try_order); i++) {
+ *f = try_order[i];
+
+ if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
+ return ret;
+
+ pa_log_debug("snd_pcm_hw_params_set_format(%s) failed: %s",
+ snd_pcm_format_description(format_trans[*f]),
+ pa_alsa_strerror(ret));
+ }
+
+ return -1;
+}
+
+static int set_period_size(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, snd_pcm_uframes_t size) {
+ snd_pcm_uframes_t s;
+ int d, ret;
+
+ pa_assert(pcm_handle);
+ pa_assert(hwparams);
+
+ s = size;
+ d = 0;
+ if (snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &s, &d) < 0) {
+ s = size;
+ d = -1;
+ if (snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &s, &d) < 0) {
+ s = size;
+ d = 1;
+ if ((ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &s, &d)) < 0) {
+ pa_log_info("snd_pcm_hw_params_set_period_size_near() failed: %s", pa_alsa_strerror(ret));
+ return ret;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static int set_buffer_size(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, snd_pcm_uframes_t size) {
+ int ret;
+
+ pa_assert(pcm_handle);
+ pa_assert(hwparams);
+
+ if ((ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &size)) < 0) {
+ pa_log_info("snd_pcm_hw_params_set_buffer_size_near() failed: %s", pa_alsa_strerror(ret));
+ return ret;
+ }
+
+ return 0;
+}
+
+/* Set the hardware parameters of the given ALSA device. Returns the
+ * selected fragment settings in *buffer_size and *period_size. If tsched mode can be enabled */
+int pa_alsa_set_hw_params(
+ snd_pcm_t *pcm_handle,
+ pa_sample_spec *ss,
+ snd_pcm_uframes_t *period_size,
+ snd_pcm_uframes_t *buffer_size,
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap,
+ pa_bool_t *use_tsched,
+ pa_bool_t require_exact_channel_number) {
+
+ int ret = -1;
+ snd_pcm_hw_params_t *hwparams, *hwparams_copy;
+ int dir;
+ snd_pcm_uframes_t _period_size = period_size ? *period_size : 0;
+ snd_pcm_uframes_t _buffer_size = buffer_size ? *buffer_size : 0;
+ pa_bool_t _use_mmap = use_mmap && *use_mmap;
+ pa_bool_t _use_tsched = use_tsched && *use_tsched;
+ pa_sample_spec _ss = *ss;
+
+ pa_assert(pcm_handle);
+ pa_assert(ss);
+
+ snd_pcm_hw_params_alloca(&hwparams);
+ snd_pcm_hw_params_alloca(&hwparams_copy);
+
+ if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) {
+ pa_log_debug("snd_pcm_hw_params_any() failed: %s", pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+ if ((ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0) {
+ pa_log_debug("snd_pcm_hw_params_set_rate_resample() failed: %s", pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+ if (_use_mmap) {
+
+ if (snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) {
+
+ /* mmap() didn't work, fall back to interleaved */
+
+ if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
+ pa_log_debug("snd_pcm_hw_params_set_access() failed: %s", pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+ _use_mmap = FALSE;
+ }
+
+ } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
+ pa_log_debug("snd_pcm_hw_params_set_access() failed: %s", pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+ if (!_use_mmap)
+ _use_tsched = FALSE;
+
+ if (!pa_alsa_pcm_is_hw(pcm_handle))
+ _use_tsched = FALSE;
+
+#if (SND_LIB_VERSION >= ((1<<16)|(0<<8)|24)) /* API additions in 1.0.24 */
+ if (_use_tsched) {
+
+ /* try to disable period wakeups if hardware can do so */
+ if (snd_pcm_hw_params_can_disable_period_wakeup(hwparams)) {
+
+ if (snd_pcm_hw_params_set_period_wakeup(pcm_handle, hwparams, FALSE) < 0)
+ /* don't bail, keep going with default mode with period wakeups */
+ pa_log_debug("snd_pcm_hw_params_set_period_wakeup() failed: %s", pa_alsa_strerror(ret));
+ else
+ pa_log_info("Trying to disable ALSA period wakeups, using timers only");
+ } else
+ pa_log_info("cannot disable ALSA period wakeups");
+ }
+#endif
+
+ if ((ret = set_format(pcm_handle, hwparams, &_ss.format)) < 0)
+ goto finish;
+
+ if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &_ss.rate, NULL)) < 0) {
+ pa_log_debug("snd_pcm_hw_params_set_rate_near() failed: %s", pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+ /* We ignore very small sampling rate deviations */
+ if (_ss.rate >= ss->rate*.95 && _ss.rate <= ss->rate*1.05)
+ _ss.rate = ss->rate;
+
+ if (require_exact_channel_number) {
+ if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, _ss.channels)) < 0) {
+ pa_log_debug("snd_pcm_hw_params_set_channels(%u) failed: %s", _ss.channels, pa_alsa_strerror(ret));
+ goto finish;
+ }
+ } else {
+ unsigned int c = _ss.channels;
+
+ if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0) {
+ pa_log_debug("snd_pcm_hw_params_set_channels_near(%u) failed: %s", _ss.channels, pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+ _ss.channels = c;
+ }
+
+ if (_use_tsched && tsched_size > 0) {
+ _buffer_size = (snd_pcm_uframes_t) (((uint64_t) tsched_size * _ss.rate) / ss->rate);
+ _period_size = _buffer_size;
+ } else {
+ _period_size = (snd_pcm_uframes_t) (((uint64_t) _period_size * _ss.rate) / ss->rate);
+ _buffer_size = (snd_pcm_uframes_t) (((uint64_t) _buffer_size * _ss.rate) / ss->rate);
+ }
+
+ if (_buffer_size > 0 || _period_size > 0) {
+ snd_pcm_uframes_t max_frames = 0;
+
+ if ((ret = snd_pcm_hw_params_get_buffer_size_max(hwparams, &max_frames)) < 0)
+ pa_log_warn("snd_pcm_hw_params_get_buffer_size_max() failed: %s", pa_alsa_strerror(ret));
+ else
+ pa_log_debug("Maximum hw buffer size is %lu ms", (long unsigned) (max_frames * PA_MSEC_PER_SEC / _ss.rate));
+
+ /* Some ALSA drivers really don't like if we set the buffer
+ * size first and the number of periods second. (which would
+ * make a lot more sense to me) So, try a few combinations
+ * before we give up. */
+
+ if (_buffer_size > 0 && _period_size > 0) {
+ snd_pcm_hw_params_copy(hwparams_copy, hwparams);
+
+ /* First try: set buffer size first, followed by period size */
+ if (set_buffer_size(pcm_handle, hwparams_copy, _buffer_size) >= 0 &&
+ set_period_size(pcm_handle, hwparams_copy, _period_size) >= 0 &&
+ snd_pcm_hw_params(pcm_handle, hwparams_copy) >= 0) {
+ pa_log_debug("Set buffer size first (to %lu samples), period size second (to %lu samples).", (unsigned long) _buffer_size, (unsigned long) _period_size);
+ goto success;
+ }
+
+ /* Second try: set period size first, followed by buffer size */
+ if (set_period_size(pcm_handle, hwparams_copy, _period_size) >= 0 &&
+ set_buffer_size(pcm_handle, hwparams_copy, _buffer_size) >= 0 &&
+ snd_pcm_hw_params(pcm_handle, hwparams_copy) >= 0) {
+ pa_log_debug("Set period size first (to %lu samples), buffer size second (to %lu samples).", (unsigned long) _period_size, (unsigned long) _buffer_size);
+ goto success;
+ }
+ }
+
+ if (_buffer_size > 0) {
+ snd_pcm_hw_params_copy(hwparams_copy, hwparams);
+
+ /* Third try: set only buffer size */
+ if (set_buffer_size(pcm_handle, hwparams_copy, _buffer_size) >= 0 &&
+ snd_pcm_hw_params(pcm_handle, hwparams_copy) >= 0) {
+ pa_log_debug("Set only buffer size (to %lu samples).", (unsigned long) _buffer_size);
+ goto success;
+ }
+ }
+
+ if (_period_size > 0) {
+ snd_pcm_hw_params_copy(hwparams_copy, hwparams);
+
+ /* Fourth try: set only period size */
+ if (set_period_size(pcm_handle, hwparams_copy, _period_size) >= 0 &&
+ snd_pcm_hw_params(pcm_handle, hwparams_copy) >= 0) {
+ pa_log_debug("Set only period size (to %lu samples).", (unsigned long) _period_size);
+ goto success;
+ }
+ }
+ }
+
+ pa_log_debug("Set neither period nor buffer size.");
+
+ /* Last chance, set nothing */
+ if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) {
+ pa_log_info("snd_pcm_hw_params failed: %s", pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+success:
+
+ if (ss->rate != _ss.rate)
+ pa_log_info("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle), ss->rate, _ss.rate);
+
+ if (ss->channels != _ss.channels)
+ pa_log_info("Device %s doesn't support %u channels, changed to %u.", snd_pcm_name(pcm_handle), ss->channels, _ss.channels);
+
+ if (ss->format != _ss.format)
+ pa_log_info("Device %s doesn't support sample format %s, changed to %s.", snd_pcm_name(pcm_handle), pa_sample_format_to_string(ss->format), pa_sample_format_to_string(_ss.format));
+
+ if ((ret = snd_pcm_prepare(pcm_handle)) < 0) {
+ pa_log_info("snd_pcm_prepare() failed: %s", pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+ if ((ret = snd_pcm_hw_params_current(pcm_handle, hwparams)) < 0) {
+ pa_log_info("snd_pcm_hw_params_current() failed: %s", pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+ if ((ret = snd_pcm_hw_params_get_period_size(hwparams, &_period_size, &dir)) < 0 ||
+ (ret = snd_pcm_hw_params_get_buffer_size(hwparams, &_buffer_size)) < 0) {
+ pa_log_info("snd_pcm_hw_params_get_{period|buffer}_size() failed: %s", pa_alsa_strerror(ret));
+ goto finish;
+ }
+
+#if (SND_LIB_VERSION >= ((1<<16)|(0<<8)|24)) /* API additions in 1.0.24 */
+ if (_use_tsched) {
+ unsigned int no_wakeup;
+ /* see if period wakeups were disabled */
+ snd_pcm_hw_params_get_period_wakeup(pcm_handle, hwparams, &no_wakeup);
+ if (no_wakeup == 0)
+ pa_log_info("ALSA period wakeups disabled");
+ else
+ pa_log_info("ALSA period wakeups were not disabled");
+ }
+#endif
+
+ ss->rate = _ss.rate;
+ ss->channels = _ss.channels;
+ ss->format = _ss.format;
+
+ pa_assert(_period_size > 0);
+ pa_assert(_buffer_size > 0);
+
+ if (buffer_size)
+ *buffer_size = _buffer_size;
+
+ if (period_size)
+ *period_size = _period_size;
+
+ if (use_mmap)
+ *use_mmap = _use_mmap;
+
+ if (use_tsched)
+ *use_tsched = _use_tsched;
+
+ ret = 0;
+
+finish:
+
+ return ret;
+}
+
+int pa_alsa_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t avail_min, pa_bool_t period_event) {
+ snd_pcm_sw_params_t *swparams;
+ snd_pcm_uframes_t boundary;
+ int err;
+
+ pa_assert(pcm);
+
+ snd_pcm_sw_params_alloca(&swparams);
+
+ if ((err = snd_pcm_sw_params_current(pcm, swparams) < 0)) {
+ pa_log_warn("Unable to determine current swparams: %s\n", pa_alsa_strerror(err));
+ return err;
+ }
+
+ if ((err = snd_pcm_sw_params_set_period_event(pcm, swparams, period_event)) < 0) {
+ pa_log_warn("Unable to disable period event: %s\n", pa_alsa_strerror(err));
+ return err;
+ }
+
+ if ((err = snd_pcm_sw_params_set_tstamp_mode(pcm, swparams, SND_PCM_TSTAMP_ENABLE)) < 0) {
+ pa_log_warn("Unable to enable time stamping: %s\n", pa_alsa_strerror(err));
+ return err;
+ }
+
+ if ((err = snd_pcm_sw_params_get_boundary(swparams, &boundary)) < 0) {
+ pa_log_warn("Unable to get boundary: %s\n", pa_alsa_strerror(err));
+ return err;
+ }
+
+ if ((err = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, boundary)) < 0) {
+ pa_log_warn("Unable to set stop threshold: %s\n", pa_alsa_strerror(err));
+ return err;
+ }
+
+ if ((err = snd_pcm_sw_params_set_start_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
+ pa_log_warn("Unable to set start threshold: %s\n", pa_alsa_strerror(err));
+ return err;
+ }
+
+ if ((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, avail_min)) < 0) {
+ pa_log_error("snd_pcm_sw_params_set_avail_min() failed: %s", pa_alsa_strerror(err));
+ return err;
+ }
+
+ if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) {
+ pa_log_warn("Unable to set sw params: %s\n", pa_alsa_strerror(err));
+ return err;
+ }
+
+ return 0;
+}
+
+snd_pcm_t *pa_alsa_open_by_device_id_auto(
+ const char *dev_id,
+ char **dev,
+ pa_sample_spec *ss,
+ pa_channel_map* map,
+ int mode,
+ snd_pcm_uframes_t *period_size,
+ snd_pcm_uframes_t *buffer_size,
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap,
+ pa_bool_t *use_tsched,
+ pa_alsa_profile_set *ps,
+ pa_alsa_mapping **mapping) {
+
+ char *d;
+ snd_pcm_t *pcm_handle;
+ void *state;
+ pa_alsa_mapping *m;
+
+ pa_assert(dev_id);
+ pa_assert(dev);
+ pa_assert(ss);
+ pa_assert(map);
+ pa_assert(ps);
+
+ /* First we try to find a device string with a superset of the
+ * requested channel map. We iterate through our device table from
+ * top to bottom and take the first that matches. If we didn't
+ * find a working device that way, we iterate backwards, and check
+ * all devices that do not provide a superset of the requested
+ * channel map.*/
+
+ PA_HASHMAP_FOREACH(m, ps->mappings, state) {
+ if (!pa_channel_map_superset(&m->channel_map, map))
+ continue;
+
+ pa_log_debug("Checking for superset %s (%s)", m->name, m->device_strings[0]);
+
+ pcm_handle = pa_alsa_open_by_device_id_mapping(
+ dev_id,
+ dev,
+ ss,
+ map,
+ mode,
+ period_size,
+ buffer_size,
+ tsched_size,
+ use_mmap,
+ use_tsched,
+ m);
+
+ if (pcm_handle) {
+ if (mapping)
+ *mapping = m;
+
+ return pcm_handle;
+ }
+ }
+
+ PA_HASHMAP_FOREACH_BACKWARDS(m, ps->mappings, state) {
+ if (pa_channel_map_superset(&m->channel_map, map))
+ continue;
+
+ pa_log_debug("Checking for subset %s (%s)", m->name, m->device_strings[0]);
+
+ pcm_handle = pa_alsa_open_by_device_id_mapping(
+ dev_id,
+ dev,
+ ss,
+ map,
+ mode,
+ period_size,
+ buffer_size,
+ tsched_size,
+ use_mmap,
+ use_tsched,
+ m);
+
+ if (pcm_handle) {
+ if (mapping)
+ *mapping = m;
+
+ return pcm_handle;
+ }
+ }
+
+ /* OK, we didn't find any good device, so let's try the raw hw: stuff */
+ d = pa_sprintf_malloc("hw:%s", dev_id);
+ pa_log_debug("Trying %s as last resort...", d);
+ pcm_handle = pa_alsa_open_by_device_string(
+ d,
+ dev,
+ ss,
+ map,
+ mode,
+ period_size,
+ buffer_size,
+ tsched_size,
+ use_mmap,
+ use_tsched,
+ FALSE);
+ pa_xfree(d);
+
+ if (pcm_handle && mapping)
+ *mapping = NULL;
+
+ return pcm_handle;
+}
+
+snd_pcm_t *pa_alsa_open_by_device_id_mapping(
+ const char *dev_id,
+ char **dev,
+ pa_sample_spec *ss,
+ pa_channel_map* map,
+ int mode,
+ snd_pcm_uframes_t *period_size,
+ snd_pcm_uframes_t *buffer_size,
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap,
+ pa_bool_t *use_tsched,
+ pa_alsa_mapping *m) {
+
+ snd_pcm_t *pcm_handle;
+ pa_sample_spec try_ss;
+ pa_channel_map try_map;
+
+ pa_assert(dev_id);
+ pa_assert(dev);
+ pa_assert(ss);
+ pa_assert(map);
+ pa_assert(m);
+
+ try_ss.channels = m->channel_map.channels;
+ try_ss.rate = ss->rate;
+ try_ss.format = ss->format;
+ try_map = m->channel_map;
+
+ pcm_handle = pa_alsa_open_by_template(
+ m->device_strings,
+ dev_id,
+ dev,
+ &try_ss,
+ &try_map,
+ mode,
+ period_size,
+ buffer_size,
+ tsched_size,
+ use_mmap,
+ use_tsched,
+ TRUE);
+
+ if (!pcm_handle)
+ return NULL;
+
+ *ss = try_ss;
+ *map = try_map;
+ pa_assert(map->channels == ss->channels);
+
+ return pcm_handle;
+}
+
+snd_pcm_t *pa_alsa_open_by_device_string(
+ const char *device,
+ char **dev,
+ pa_sample_spec *ss,
+ pa_channel_map* map,
+ int mode,
+ snd_pcm_uframes_t *period_size,
+ snd_pcm_uframes_t *buffer_size,
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap,
+ pa_bool_t *use_tsched,
+ pa_bool_t require_exact_channel_number) {
+
+ int err;
+ char *d;
+ snd_pcm_t *pcm_handle;
+ pa_bool_t reformat = FALSE;
+
+ pa_assert(device);
+ pa_assert(ss);
+ pa_assert(map);
+
+ d = pa_xstrdup(device);
+
+ for (;;) {
+ pa_log_debug("Trying %s %s SND_PCM_NO_AUTO_FORMAT ...", d, reformat ? "without" : "with");
+
+ if ((err = snd_pcm_open(&pcm_handle, d, mode,
+ SND_PCM_NONBLOCK|
+ SND_PCM_NO_AUTO_RESAMPLE|
+ SND_PCM_NO_AUTO_CHANNELS|
+ (reformat ? 0 : SND_PCM_NO_AUTO_FORMAT))) < 0) {
+ pa_log_info("Error opening PCM device %s: %s", d, pa_alsa_strerror(err));
+ goto fail;
+ }
+
+ pa_log_debug("Managed to open %s", d);
+
+ if ((err = pa_alsa_set_hw_params(
+ pcm_handle,
+ ss,
+ period_size,
+ buffer_size,
+ tsched_size,
+ use_mmap,
+ use_tsched,
+ require_exact_channel_number)) < 0) {
+
+ if (!reformat) {
+ reformat = TRUE;
+
+ snd_pcm_close(pcm_handle);
+ continue;
+ }
+
+ /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
+ if (!pa_startswith(d, "plug:") && !pa_startswith(d, "plughw:")) {
+ char *t;
+
+ t = pa_sprintf_malloc("plug:%s", d);
+ pa_xfree(d);
+ d = t;
+
+ reformat = FALSE;
+
+ snd_pcm_close(pcm_handle);
+ continue;
+ }
+
+ pa_log_info("Failed to set hardware parameters on %s: %s", d, pa_alsa_strerror(err));
+ snd_pcm_close(pcm_handle);
+
+ goto fail;
+ }
+
+ if (dev)
+ *dev = d;
+ else
+ pa_xfree(d);
+
+ if (ss->channels != map->channels)
+ pa_channel_map_init_extend(map, ss->channels, PA_CHANNEL_MAP_ALSA);
+
+ return pcm_handle;
+ }
+
+fail:
+ pa_xfree(d);
+
+ return NULL;
+}
+
+snd_pcm_t *pa_alsa_open_by_template(
+ char **template,
+ const char *dev_id,
+ char **dev,
+ pa_sample_spec *ss,
+ pa_channel_map* map,
+ int mode,
+ snd_pcm_uframes_t *period_size,
+ snd_pcm_uframes_t *buffer_size,
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap,
+ pa_bool_t *use_tsched,
+ pa_bool_t require_exact_channel_number) {
+
+ snd_pcm_t *pcm_handle;
+ char **i;
+
+ for (i = template; *i; i++) {
+ char *d;
+
+ d = pa_replace(*i, "%f", dev_id);
+
+ pcm_handle = pa_alsa_open_by_device_string(
+ d,
+ dev,
+ ss,
+ map,
+ mode,
+ period_size,
+ buffer_size,
+ tsched_size,
+ use_mmap,
+ use_tsched,
+ require_exact_channel_number);
+
+ pa_xfree(d);
+
+ if (pcm_handle)
+ return pcm_handle;
+ }
+
+ return NULL;
+}
+
+void pa_alsa_dump(pa_log_level_t level, snd_pcm_t *pcm) {
+ int err;
+ snd_output_t *out;
+
+ pa_assert(pcm);
+
+ pa_assert_se(snd_output_buffer_open(&out) == 0);
+
+ if ((err = snd_pcm_dump(pcm, out)) < 0)
+ pa_logl(level, "snd_pcm_dump(): %s", pa_alsa_strerror(err));
+ else {
+ char *s = NULL;
+ snd_output_buffer_string(out, &s);
+ pa_logl(level, "snd_pcm_dump():\n%s", pa_strnull(s));
+ }
+
+ pa_assert_se(snd_output_close(out) == 0);
+}
+
+void pa_alsa_dump_status(snd_pcm_t *pcm) {
+ int err;
+ snd_output_t *out;
+ snd_pcm_status_t *status;
+ char *s = NULL;
+
+ pa_assert(pcm);
+
+ snd_pcm_status_alloca(&status);
+
+ if ((err = snd_output_buffer_open(&out)) < 0) {
+ pa_log_debug("snd_output_buffer_open() failed: %s", pa_cstrerror(err));
+ return;
+ }
+
+ if ((err = snd_pcm_status(pcm, status)) < 0) {
+ pa_log_debug("snd_pcm_status() failed: %s", pa_cstrerror(err));
+ goto finish;
+ }
+
+ if ((err = snd_pcm_status_dump(status, out)) < 0) {
+ pa_log_debug("snd_pcm_dump(): %s", pa_alsa_strerror(err));
+ goto finish;
+ }
+
+ snd_output_buffer_string(out, &s);
+ pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
+
+finish:
+
+ snd_output_close(out);
+}
+
+static void alsa_error_handler(const char *file, int line, const char *function, int err, const char *fmt,...) {
+ va_list ap;
+ char *alsa_file;
+
+ alsa_file = pa_sprintf_malloc("(alsa-lib)%s", file);
+
+ va_start(ap, fmt);
+
+ pa_log_levelv_meta(PA_LOG_INFO, alsa_file, line, function, fmt, ap);
+
+ va_end(ap);
+
+ pa_xfree(alsa_file);
+}
+
+static pa_atomic_t n_error_handler_installed = PA_ATOMIC_INIT(0);
+
+void pa_alsa_refcnt_inc(void) {
+ /* This is not really thread safe, but we do our best */
+
+ if (pa_atomic_inc(&n_error_handler_installed) == 0)
+ snd_lib_error_set_handler(alsa_error_handler);
+}
+
+void pa_alsa_refcnt_dec(void) {
+ int r;
+
+ pa_assert_se((r = pa_atomic_dec(&n_error_handler_installed)) >= 1);
+
+ if (r == 1) {
+ snd_lib_error_set_handler(NULL);
+ snd_config_update_free_global();
+ }
+}
+
+pa_bool_t pa_alsa_init_description(pa_proplist *p) {
+ const char *d, *k;
+ pa_assert(p);
+
+ if (pa_device_init_description(p))
+ return TRUE;
+
+ if (!(d = pa_proplist_gets(p, "alsa.card_name")))
+ d = pa_proplist_gets(p, "alsa.name");
+
+ if (!d)
+ return FALSE;
+
+ k = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_DESCRIPTION);
+
+ if (d && k)
+ pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, _("%s %s"), d, k);
+ else if (d)
+ pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, d);
+
+ return FALSE;
+}
+
+void pa_alsa_init_proplist_card(pa_core *c, pa_proplist *p, int card) {
+ char *cn, *lcn, *dn;
+
+ pa_assert(p);
+ pa_assert(card >= 0);
+
+ pa_proplist_setf(p, "alsa.card", "%i", card);
+
+ if (snd_card_get_name(card, &cn) >= 0) {
+ pa_proplist_sets(p, "alsa.card_name", pa_strip(cn));
+ free(cn);
+ }
+
+ if (snd_card_get_longname(card, &lcn) >= 0) {
+ pa_proplist_sets(p, "alsa.long_card_name", pa_strip(lcn));
+ free(lcn);
+ }
+
+ if ((dn = pa_alsa_get_driver_name(card))) {
+ pa_proplist_sets(p, "alsa.driver_name", dn);
+ pa_xfree(dn);
+ }
+
+#ifdef HAVE_UDEV
+ pa_udev_get_info(card, p);
+#endif
+
+#ifdef HAVE_HAL
+ pa_hal_get_info(c, p, card);
+#endif
+}
+
+void pa_alsa_init_proplist_pcm_info(pa_core *c, pa_proplist *p, snd_pcm_info_t *pcm_info) {
+
+ static const char * const alsa_class_table[SND_PCM_CLASS_LAST+1] = {
+ [SND_PCM_CLASS_GENERIC] = "generic",
+ [SND_PCM_CLASS_MULTI] = "multi",
+ [SND_PCM_CLASS_MODEM] = "modem",
+ [SND_PCM_CLASS_DIGITIZER] = "digitizer"
+ };
+ static const char * const class_table[SND_PCM_CLASS_LAST+1] = {
+ [SND_PCM_CLASS_GENERIC] = "sound",
+ [SND_PCM_CLASS_MULTI] = NULL,
+ [SND_PCM_CLASS_MODEM] = "modem",
+ [SND_PCM_CLASS_DIGITIZER] = NULL
+ };
+ static const char * const alsa_subclass_table[SND_PCM_SUBCLASS_LAST+1] = {
+ [SND_PCM_SUBCLASS_GENERIC_MIX] = "generic-mix",
+ [SND_PCM_SUBCLASS_MULTI_MIX] = "multi-mix"
+ };
+
+ snd_pcm_class_t class;
+ snd_pcm_subclass_t subclass;
+ const char *n, *id, *sdn;
+ int card;
+
+ pa_assert(p);
+ pa_assert(pcm_info);
+
+ pa_proplist_sets(p, PA_PROP_DEVICE_API, "alsa");
+
+ if ((class = snd_pcm_info_get_class(pcm_info)) <= SND_PCM_CLASS_LAST) {
+ if (class_table[class])
+ pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, class_table[class]);
+ if (alsa_class_table[class])
+ pa_proplist_sets(p, "alsa.class", alsa_class_table[class]);
+ }
+
+ if ((subclass = snd_pcm_info_get_subclass(pcm_info)) <= SND_PCM_SUBCLASS_LAST)
+ if (alsa_subclass_table[subclass])
+ pa_proplist_sets(p, "alsa.subclass", alsa_subclass_table[subclass]);
+
+ if ((n = snd_pcm_info_get_name(pcm_info))) {
+ char *t = pa_xstrdup(n);
+ pa_proplist_sets(p, "alsa.name", pa_strip(t));
+ pa_xfree(t);
+ }
+
+ if ((id = snd_pcm_info_get_id(pcm_info)))
+ pa_proplist_sets(p, "alsa.id", id);
+
+ pa_proplist_setf(p, "alsa.subdevice", "%u", snd_pcm_info_get_subdevice(pcm_info));
+ if ((sdn = snd_pcm_info_get_subdevice_name(pcm_info)))
+ pa_proplist_sets(p, "alsa.subdevice_name", sdn);
+
+ pa_proplist_setf(p, "alsa.device", "%u", snd_pcm_info_get_device(pcm_info));
+
+ if ((card = snd_pcm_info_get_card(pcm_info)) >= 0)
+ pa_alsa_init_proplist_card(c, p, card);
+}
+
+void pa_alsa_init_proplist_pcm(pa_core *c, pa_proplist *p, snd_pcm_t *pcm) {
+ snd_pcm_hw_params_t *hwparams;
+ snd_pcm_info_t *info;
+ int bits, err;
+
+ snd_pcm_hw_params_alloca(&hwparams);
+ snd_pcm_info_alloca(&info);
+
+ if ((err = snd_pcm_hw_params_current(pcm, hwparams)) < 0)
+ pa_log_warn("Error fetching hardware parameter info: %s", pa_alsa_strerror(err));
+ else {
+
+ if ((bits = snd_pcm_hw_params_get_sbits(hwparams)) >= 0)
+ pa_proplist_setf(p, "alsa.resolution_bits", "%i", bits);
+ }
+
+ if ((err = snd_pcm_info(pcm, info)) < 0)
+ pa_log_warn("Error fetching PCM info: %s", pa_alsa_strerror(err));
+ else
+ pa_alsa_init_proplist_pcm_info(c, p, info);
+}
+
+void pa_alsa_init_proplist_ctl(pa_proplist *p, const char *name) {
+ int err;
+ snd_ctl_t *ctl;
+ snd_ctl_card_info_t *info;
+ const char *t;
+
+ pa_assert(p);
+
+ snd_ctl_card_info_alloca(&info);
+
+ if ((err = snd_ctl_open(&ctl, name, 0)) < 0) {
+ pa_log_warn("Error opening low-level control device '%s': %s", name, snd_strerror(err));
+ return;
+ }
+
+ if ((err = snd_ctl_card_info(ctl, info)) < 0) {
+ pa_log_warn("Control device %s card info: %s", name, snd_strerror(err));
+ snd_ctl_close(ctl);
+ return;
+ }
+
+ if ((t = snd_ctl_card_info_get_mixername(info)) && *t)
+ pa_proplist_sets(p, "alsa.mixer_name", t);
+
+ if ((t = snd_ctl_card_info_get_components(info)) && *t)
+ pa_proplist_sets(p, "alsa.components", t);
+
+ snd_ctl_close(ctl);
+}
+
+int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents) {
+ snd_pcm_state_t state;
+ int err;
+
+ pa_assert(pcm);
+
+ if (revents & POLLERR)
+ pa_log_debug("Got POLLERR from ALSA");
+ if (revents & POLLNVAL)
+ pa_log_warn("Got POLLNVAL from ALSA");
+ if (revents & POLLHUP)
+ pa_log_warn("Got POLLHUP from ALSA");
+ if (revents & POLLPRI)
+ pa_log_warn("Got POLLPRI from ALSA");
+ if (revents & POLLIN)
+ pa_log_debug("Got POLLIN from ALSA");
+ if (revents & POLLOUT)
+ pa_log_debug("Got POLLOUT from ALSA");
+
+ state = snd_pcm_state(pcm);
+ pa_log_debug("PCM state is %s", snd_pcm_state_name(state));
+
+ /* Try to recover from this error */
+
+ switch (state) {
+
+ case SND_PCM_STATE_XRUN:
+ if ((err = snd_pcm_recover(pcm, -EPIPE, 1)) != 0) {
+ pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", pa_alsa_strerror(err));
+ return -1;
+ }
+ break;
+
+ case SND_PCM_STATE_SUSPENDED:
+ if ((err = snd_pcm_recover(pcm, -ESTRPIPE, 1)) != 0) {
+ pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", pa_alsa_strerror(err));
+ return -1;
+ }
+ break;
+
+ default:
+
+ snd_pcm_drop(pcm);
+
+ if ((err = snd_pcm_prepare(pcm)) < 0) {
+ pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", pa_alsa_strerror(err));
+ return -1;
+ }
+ break;
+ }
+
+ return 0;
+}
+
+pa_rtpoll_item* pa_alsa_build_pollfd(snd_pcm_t *pcm, pa_rtpoll *rtpoll) {
+ int n, err;
+ struct pollfd *pollfd;
+ pa_rtpoll_item *item;
+
+ pa_assert(pcm);
+
+ if ((n = snd_pcm_poll_descriptors_count(pcm)) < 0) {
+ pa_log("snd_pcm_poll_descriptors_count() failed: %s", pa_alsa_strerror(n));
+ return NULL;
+ }
+
+ item = pa_rtpoll_item_new(rtpoll, PA_RTPOLL_NEVER, (unsigned) n);
+ pollfd = pa_rtpoll_item_get_pollfd(item, NULL);
+
+ if ((err = snd_pcm_poll_descriptors(pcm, pollfd, (unsigned) n)) < 0) {
+ pa_log("snd_pcm_poll_descriptors() failed: %s", pa_alsa_strerror(err));
+ pa_rtpoll_item_free(item);
+ return NULL;
+ }
+
+ return item;
+}
+
+snd_pcm_sframes_t pa_alsa_safe_avail(snd_pcm_t *pcm, size_t hwbuf_size, const pa_sample_spec *ss) {
+ snd_pcm_sframes_t n;
+ size_t k;
+
+ pa_assert(pcm);
+ pa_assert(hwbuf_size > 0);
+ pa_assert(ss);
+
+ /* Some ALSA driver expose weird bugs, let's inform the user about
+ * what is going on */
+
+ n = snd_pcm_avail(pcm);
+
+ if (n <= 0)
+ return n;
+
+ k = (size_t) n * pa_frame_size(ss);
+
+ if (PA_UNLIKELY(k >= hwbuf_size * 5 ||
+ k >= pa_bytes_per_second(ss)*10)) {
+
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
+ pa_log(_("snd_pcm_avail() returned a value that is exceptionally large: %lu bytes (%lu ms).\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
+ (unsigned long) k,
+ (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC),
+ pa_strnull(dn));
+ pa_xfree(dn);
+ pa_alsa_dump(PA_LOG_ERROR, pcm);
+ } PA_ONCE_END;
+
+ /* Mhmm, let's try not to fail completely */
+ n = (snd_pcm_sframes_t) (hwbuf_size / pa_frame_size(ss));
+ }
+
+ return n;
+}
+
+int pa_alsa_safe_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delay, size_t hwbuf_size, const pa_sample_spec *ss, pa_bool_t capture) {
+ ssize_t k;
+ size_t abs_k;
+ int r;
+ snd_pcm_sframes_t avail = 0;
+
+ pa_assert(pcm);
+ pa_assert(delay);
+ pa_assert(hwbuf_size > 0);
+ pa_assert(ss);
+
+ /* Some ALSA driver expose weird bugs, let's inform the user about
+ * what is going on. We're going to get both the avail and delay values so
+ * that we can compare and check them for capture */
+
+ if ((r = snd_pcm_avail_delay(pcm, &avail, delay)) < 0)
+ return r;
+
+ k = (ssize_t) *delay * (ssize_t) pa_frame_size(ss);
+
+ abs_k = k >= 0 ? (size_t) k : (size_t) -k;
+
+ if (PA_UNLIKELY(abs_k >= hwbuf_size * 5 ||
+ abs_k >= pa_bytes_per_second(ss)*10)) {
+
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
+ pa_log(_("snd_pcm_delay() returned a value that is exceptionally large: %li bytes (%s%lu ms).\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
+ (signed long) k,
+ k < 0 ? "-" : "",
+ (unsigned long) (pa_bytes_to_usec(abs_k, ss) / PA_USEC_PER_MSEC),
+ pa_strnull(dn));
+ pa_xfree(dn);
+ pa_alsa_dump(PA_LOG_ERROR, pcm);
+ } PA_ONCE_END;
+
+ /* Mhmm, let's try not to fail completely */
+ if (k < 0)
+ *delay = -(snd_pcm_sframes_t) (hwbuf_size / pa_frame_size(ss));
+ else
+ *delay = (snd_pcm_sframes_t) (hwbuf_size / pa_frame_size(ss));
+ }
+
+ if (capture) {
+ abs_k = (size_t) avail * pa_frame_size(ss);
+
+ if (PA_UNLIKELY(abs_k >= hwbuf_size * 5 ||
+ abs_k >= pa_bytes_per_second(ss)*10)) {
+
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
+ pa_log(_("snd_pcm_avail() returned a value that is exceptionally large: %lu bytes (%lu ms).\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
+ (unsigned long) k,
+ (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC),
+ pa_strnull(dn));
+ pa_xfree(dn);
+ pa_alsa_dump(PA_LOG_ERROR, pcm);
+ } PA_ONCE_END;
+
+ /* Mhmm, let's try not to fail completely */
+ avail = (snd_pcm_sframes_t) (hwbuf_size / pa_frame_size(ss));
+ }
+
+ if (PA_UNLIKELY(*delay < avail)) {
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
+ pa_log(_("snd_pcm_avail_delay() returned strange values: delay %lu is less than avail %lu.\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
+ (unsigned long) *delay,
+ (unsigned long) avail,
+ pa_strnull(dn));
+ pa_xfree(dn);
+ pa_alsa_dump(PA_LOG_ERROR, pcm);
+ } PA_ONCE_END;
+
+ /* try to fixup */
+ *delay = avail;
+ }
+ }
+
+ return 0;
+}
+
+int pa_alsa_safe_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames, size_t hwbuf_size, const pa_sample_spec *ss) {
+ int r;
+ snd_pcm_uframes_t before;
+ size_t k;
+
+ pa_assert(pcm);
+ pa_assert(areas);
+ pa_assert(offset);
+ pa_assert(frames);
+ pa_assert(hwbuf_size > 0);
+ pa_assert(ss);
+
+ before = *frames;
+
+ r = snd_pcm_mmap_begin(pcm, areas, offset, frames);
+
+ if (r < 0)
+ return r;
+
+ k = (size_t) *frames * pa_frame_size(ss);
+
+ if (PA_UNLIKELY(*frames > before ||
+ k >= hwbuf_size * 3 ||
+ k >= pa_bytes_per_second(ss)*10))
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
+ pa_log(_("snd_pcm_mmap_begin() returned a value that is exceptionally large: %lu bytes (%lu ms).\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
+ (unsigned long) k,
+ (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC),
+ pa_strnull(dn));
+ pa_xfree(dn);
+ pa_alsa_dump(PA_LOG_ERROR, pcm);
+ } PA_ONCE_END;
+
+ return r;
+}
+
+char *pa_alsa_get_driver_name(int card) {
+ char *t, *m, *n;
+
+ pa_assert(card >= 0);
+
+ t = pa_sprintf_malloc("/sys/class/sound/card%i/device/driver/module", card);
+ m = pa_readlink(t);
+ pa_xfree(t);
+
+ if (!m)
+ return NULL;
+
+ n = pa_xstrdup(pa_path_get_filename(m));
+ pa_xfree(m);
+
+ return n;
+}
+
+char *pa_alsa_get_driver_name_by_pcm(snd_pcm_t *pcm) {
+ int card;
+ snd_pcm_info_t* info;
+ snd_pcm_info_alloca(&info);
+
+ pa_assert(pcm);
+
+ if (snd_pcm_info(pcm, info) < 0)
+ return NULL;
+
+ if ((card = snd_pcm_info_get_card(info)) < 0)
+ return NULL;
+
+ return pa_alsa_get_driver_name(card);
+}
+
+char *pa_alsa_get_reserve_name(const char *device) {
+ const char *t;
+ int i;
+
+ pa_assert(device);
+
+ if ((t = strchr(device, ':')))
+ device = t+1;
+
+ if ((i = snd_card_get_index(device)) < 0) {
+ int32_t k;
+
+ if (pa_atoi(device, &k) < 0)
+ return NULL;
+
+ i = (int) k;
+ }
+
+ return pa_sprintf_malloc("Audio%i", i);
+}
+
+pa_bool_t pa_alsa_pcm_is_hw(snd_pcm_t *pcm) {
+ snd_pcm_info_t* info;
+ snd_pcm_info_alloca(&info);
+
+ pa_assert(pcm);
+
+ if (snd_pcm_info(pcm, info) < 0)
+ return FALSE;
+
+ return snd_pcm_info_get_card(info) >= 0;
+}
+
+pa_bool_t pa_alsa_pcm_is_modem(snd_pcm_t *pcm) {
+ snd_pcm_info_t* info;
+ snd_pcm_info_alloca(&info);
+
+ pa_assert(pcm);
+
+ if (snd_pcm_info(pcm, info) < 0)
+ return FALSE;
+
+ return snd_pcm_info_get_class(info) == SND_PCM_CLASS_MODEM;
+}
+
+PA_STATIC_TLS_DECLARE(cstrerror, pa_xfree);
+
+const char* pa_alsa_strerror(int errnum) {
+ const char *original = NULL;
+ char *translated, *t;
+ char errbuf[128];
+
+ if ((t = PA_STATIC_TLS_GET(cstrerror)))
+ pa_xfree(t);
+
+ original = snd_strerror(errnum);
+
+ if (!original) {
+ pa_snprintf(errbuf, sizeof(errbuf), "Unknown error %i", errnum);
+ original = errbuf;
+ }
+
+ if (!(translated = pa_locale_to_utf8(original))) {
+ pa_log_warn("Unable to convert error string to locale, filtering.");
+ translated = pa_utf8_filter(original);
+ }
+
+ PA_STATIC_TLS_SET(cstrerror, translated);
+
+ return translated;
+}
+
+pa_bool_t pa_alsa_may_tsched(pa_bool_t want) {
+
+ if (!want)
+ return FALSE;
+
+ if (!pa_rtclock_hrtimer()) {
+ /* We cannot depend on being woken up in time when the timers
+ are inaccurate, so let's fallback to classic IO based playback
+ then. */
+ pa_log_notice("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
+ return FALSE; }
+
+ if (pa_running_in_vm()) {
+ /* We cannot depend on being woken up when we ask for in a VM,
+ * so let's fallback to classic IO based playback then. */
+ pa_log_notice("Disabling timer-based scheduling because running inside a VM.");
+ return FALSE;
+ }
+
+ return TRUE;
+}
diff --git a/src/modules/alsa/alsa-util.h b/src/modules/alsa/alsa-util.h
new file mode 100644
index 00000000..ee5e781e
--- /dev/null
+++ b/src/modules/alsa/alsa-util.h
@@ -0,0 +1,143 @@
+#ifndef fooalsautilhfoo
+#define fooalsautilhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <asoundlib.h>
+
+#include <pulse/sample.h>
+#include <pulse/channelmap.h>
+#include <pulse/proplist.h>
+
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/core.h>
+#include <pulsecore/log.h>
+
+#include "alsa-mixer.h"
+
+int pa_alsa_set_hw_params(
+ snd_pcm_t *pcm_handle,
+ pa_sample_spec *ss, /* modified at return */
+ snd_pcm_uframes_t *period_size, /* modified at return */
+ snd_pcm_uframes_t *buffer_size, /* modified at return */
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap, /* modified at return */
+ pa_bool_t *use_tsched, /* modified at return */
+ pa_bool_t require_exact_channel_number);
+
+int pa_alsa_set_sw_params(
+ snd_pcm_t *pcm,
+ snd_pcm_uframes_t avail_min,
+ pa_bool_t period_event);
+
+/* Picks a working mapping from the profile set based on the specified ss/map */
+snd_pcm_t *pa_alsa_open_by_device_id_auto(
+ const char *dev_id,
+ char **dev, /* modified at return */
+ pa_sample_spec *ss, /* modified at return */
+ pa_channel_map* map, /* modified at return */
+ int mode,
+ snd_pcm_uframes_t *period_size, /* modified at return */
+ snd_pcm_uframes_t *buffer_size, /* modified at return */
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap, /* modified at return */
+ pa_bool_t *use_tsched, /* modified at return */
+ pa_alsa_profile_set *ps,
+ pa_alsa_mapping **mapping); /* modified at return */
+
+/* Uses the specified mapping */
+snd_pcm_t *pa_alsa_open_by_device_id_mapping(
+ const char *dev_id,
+ char **dev, /* modified at return */
+ pa_sample_spec *ss, /* modified at return */
+ pa_channel_map* map, /* modified at return */
+ int mode,
+ snd_pcm_uframes_t *period_size, /* modified at return */
+ snd_pcm_uframes_t *buffer_size, /* modified at return */
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap, /* modified at return */
+ pa_bool_t *use_tsched, /* modified at return */
+ pa_alsa_mapping *mapping);
+
+/* Opens the explicit ALSA device */
+snd_pcm_t *pa_alsa_open_by_device_string(
+ const char *dir,
+ char **dev, /* modified at return */
+ pa_sample_spec *ss, /* modified at return */
+ pa_channel_map* map, /* modified at return */
+ int mode,
+ snd_pcm_uframes_t *period_size, /* modified at return */
+ snd_pcm_uframes_t *buffer_size, /* modified at return */
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap, /* modified at return */
+ pa_bool_t *use_tsched, /* modified at return */
+ pa_bool_t require_exact_channel_number);
+
+/* Opens the explicit ALSA device with a fallback list */
+snd_pcm_t *pa_alsa_open_by_template(
+ char **template,
+ const char *dev_id,
+ char **dev, /* modified at return */
+ pa_sample_spec *ss, /* modified at return */
+ pa_channel_map* map, /* modified at return */
+ int mode,
+ snd_pcm_uframes_t *period_size, /* modified at return */
+ snd_pcm_uframes_t *buffer_size, /* modified at return */
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap, /* modified at return */
+ pa_bool_t *use_tsched, /* modified at return */
+ pa_bool_t require_exact_channel_number);
+
+void pa_alsa_dump(pa_log_level_t level, snd_pcm_t *pcm);
+void pa_alsa_dump_status(snd_pcm_t *pcm);
+
+void pa_alsa_refcnt_inc(void);
+void pa_alsa_refcnt_dec(void);
+
+void pa_alsa_init_proplist_pcm_info(pa_core *c, pa_proplist *p, snd_pcm_info_t *pcm_info);
+void pa_alsa_init_proplist_card(pa_core *c, pa_proplist *p, int card);
+void pa_alsa_init_proplist_pcm(pa_core *c, pa_proplist *p, snd_pcm_t *pcm);
+void pa_alsa_init_proplist_ctl(pa_proplist *p, const char *name);
+pa_bool_t pa_alsa_init_description(pa_proplist *p);
+
+int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents);
+
+pa_rtpoll_item* pa_alsa_build_pollfd(snd_pcm_t *pcm, pa_rtpoll *rtpoll);
+
+snd_pcm_sframes_t pa_alsa_safe_avail(snd_pcm_t *pcm, size_t hwbuf_size, const pa_sample_spec *ss);
+int pa_alsa_safe_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delay, size_t hwbuf_size, const pa_sample_spec *ss, pa_bool_t capture);
+int pa_alsa_safe_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames, size_t hwbuf_size, const pa_sample_spec *ss);
+
+char *pa_alsa_get_driver_name(int card);
+char *pa_alsa_get_driver_name_by_pcm(snd_pcm_t *pcm);
+
+char *pa_alsa_get_reserve_name(const char *device);
+
+pa_bool_t pa_alsa_pcm_is_hw(snd_pcm_t *pcm);
+pa_bool_t pa_alsa_pcm_is_modem(snd_pcm_t *pcm);
+
+const char* pa_alsa_strerror(int errnum);
+
+pa_bool_t pa_alsa_may_tsched(pa_bool_t want);
+
+#endif
diff --git a/src/modules/alsa/mixer/paths/analog-input-aux.conf b/src/modules/alsa/mixer/paths/analog-input-aux.conf
new file mode 100644
index 00000000..3a7cb7b2
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-aux.conf
@@ -0,0 +1,66 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where an 'Aux' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 90
+name = analog-input
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Line]
+switch = off
+volume = off
+
+[Element Aux]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Video]
+switch = off
+volume = off
+
+[Element Mic/Line]
+switch = off
+volume = off
+
+[Element TV Tuner]
+switch = off
+volume = off
+
+[Element FM]
+switch = off
+volume = off
+
+.include analog-input.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input-dock-mic.conf b/src/modules/alsa/mixer/paths/analog-input-dock-mic.conf
new file mode 100644
index 00000000..74826a96
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-dock-mic.conf
@@ -0,0 +1,81 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where a 'Dock Mic' or 'Dock Mic Boost' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 80
+name = analog-input-microphone-dock
+
+[Element Dock Mic Boost]
+required-any = any
+switch = select
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Option Dock Mic Boost:on]
+name = input-boost-on
+
+[Option Dock Mic Boost:off]
+name = input-boost-off
+
+[Element Dock Mic]
+required-any = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Input Source]
+enumeration = select
+
+[Option Input Source:Dock Mic]
+name = analog-input-microphone-dock
+required-any = any
+
+[Element Capture Source]
+enumeration = select
+
+[Option Capture Source:Dock Mic]
+name = analog-input-microphone-dock
+required-any = any
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Front Mic]
+switch = off
+volume = off
+
+[Element Rear Mic]
+switch = off
+volume = off
+
+.include analog-input-mic.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input-fm.conf b/src/modules/alsa/mixer/paths/analog-input-fm.conf
new file mode 100644
index 00000000..7f150e36
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-fm.conf
@@ -0,0 +1,66 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where an 'FM' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 70
+name = analog-input-radio
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Line]
+switch = off
+volume = off
+
+[Element Aux]
+switch = off
+volume = off
+
+[Element Video]
+switch = off
+volume = off
+
+[Element Mic/Line]
+switch = off
+volume = off
+
+[Element TV Tuner]
+switch = off
+volume = off
+
+[Element FM]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+.include analog-input.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input-front-mic.conf b/src/modules/alsa/mixer/paths/analog-input-front-mic.conf
new file mode 100644
index 00000000..6c58ece1
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-front-mic.conf
@@ -0,0 +1,81 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where a 'Front Mic' or 'Front Mic Boost' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 90
+name = analog-input-microphone-front
+
+[Element Front Mic Boost]
+required-any = any
+switch = select
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Option Front Mic Boost:on]
+name = input-boost-on
+
+[Option Front Mic Boost:off]
+name = input-boost-off
+
+[Element Front Mic]
+required-any = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Input Source]
+enumeration = select
+
+[Option Input Source:Front Mic]
+name = analog-input-microphone-front
+required-any = any
+
+[Element Capture Source]
+enumeration = select
+
+[Option Capture Source:Front Mic]
+name = analog-input-microphone-front
+required-any = any
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Rear Mic]
+switch = off
+volume = off
+
+[Element Dock Mic]
+switch = off
+volume = off
+
+.include analog-input-mic.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input-internal-mic.conf b/src/modules/alsa/mixer/paths/analog-input-internal-mic.conf
new file mode 100644
index 00000000..70a1cd12
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-internal-mic.conf
@@ -0,0 +1,111 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where a 'Internal Mic' or 'Internal Mic Boost' element exists
+; 'Int Mic' and 'Int Mic Boost' are for compatibility with kernels < 2.6.38
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 89
+name = analog-input-microphone-internal
+
+[Element Internal Mic Boost]
+required-any = any
+switch = select
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Option Internal Mic Boost:on]
+name = input-boost-on
+
+[Option Internal Mic Boost:off]
+name = input-boost-off
+
+[Element Int Mic Boost]
+required-any = any
+switch = select
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Option Int Mic Boost:on]
+name = input-boost-on
+
+[Option Int Mic Boost:off]
+name = input-boost-off
+
+
+[Element Internal Mic]
+required-any = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Int Mic]
+required-any = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Input Source]
+enumeration = select
+
+[Option Input Source:Internal Mic]
+name = analog-input-microphone-internal
+required-any = any
+
+[Option Input Source:Int Mic]
+name = analog-input-microphone-internal
+required-any = any
+
+[Element Capture Source]
+enumeration = select
+
+[Option Capture Source:Internal Mic]
+name = analog-input-microphone-internal
+required-any = any
+
+[Option Capture Source:Int Mic]
+name = analog-input-microphone-internal
+required-any = any
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Dock Mic]
+switch = off
+volume = off
+
+[Element Front Mic]
+switch = off
+volume = off
+
+[Element Rear Mic]
+switch = off
+volume = off
+
+.include analog-input-mic.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input-linein.conf b/src/modules/alsa/mixer/paths/analog-input-linein.conf
new file mode 100644
index 00000000..461cebdb
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-linein.conf
@@ -0,0 +1,93 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where a 'Line' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 90
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Line Boost]
+required-any = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Line]
+required-any = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Input Source]
+enumeration = select
+
+[Option Input Source:Line]
+name = analog-input-linein
+required-any = any
+
+[Element Capture Source]
+enumeration = select
+
+[Option Capture Source:Line]
+name = analog-input-linein
+required-any = any
+
+
+[Element Aux]
+switch = off
+volume = off
+
+[Element Video]
+switch = off
+volume = off
+
+[Element Mic/Line]
+switch = off
+volume = off
+
+[Element TV Tuner]
+switch = off
+volume = off
+
+[Element FM]
+switch = off
+volume = off
+
+[Element Mic Jack Mode]
+enumeration = select
+
+[Option Mic Jack Mode:Line In]
+priority = 19
+required-any = any
+name = input-linein
diff --git a/src/modules/alsa/mixer/paths/analog-input-mic-line.conf b/src/modules/alsa/mixer/paths/analog-input-mic-line.conf
new file mode 100644
index 00000000..fa680aab
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-mic-line.conf
@@ -0,0 +1,67 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where a 'Mic/Line' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 90
+name = analog-input
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Line]
+switch = off
+volume = off
+
+[Element Aux]
+switch = off
+volume = off
+
+[Element Video]
+switch = off
+volume = off
+
+[Element Mic/Line]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element TV Tuner]
+switch = off
+volume = off
+
+[Element FM]
+switch = off
+volume = off
+
+.include analog-input.conf.common
+.include analog-input-mic.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input-mic.conf b/src/modules/alsa/mixer/paths/analog-input-mic.conf
new file mode 100644
index 00000000..d88028bf
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-mic.conf
@@ -0,0 +1,104 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where a 'Mic' or 'Mic Boost' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 89
+name = analog-input-microphone
+
+[Element Mic Boost]
+required-any = any
+switch = select
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Option Mic Boost:on]
+name = input-boost-on
+
+[Option Mic Boost:off]
+name = input-boost-off
+
+[Element Mic]
+required-any = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Input Source]
+enumeration = select
+
+[Option Input Source:Mic]
+name = analog-input-microphone
+required-any = any
+
+[Element Capture Source]
+enumeration = select
+
+[Option Capture Source:Mic]
+name = analog-input-microphone
+required-any = any
+
+;;; Some AC'97s have "Mic Select" and "Mic Boost (+20dB)"
+
+[Element Mic Select]
+enumeration = select
+
+[Option Mic Select:Mic1]
+name = input-microphone
+priority = 20
+
+[Option Mic Select:Mic2]
+name = input-microphone
+priority = 19
+
+[Element Mic Boost (+20dB)]
+switch = select
+volume = merge
+
+[Option Mic Boost (+20dB):on]
+name = input-boost-on
+
+[Option Mic Boost (+20dB):off]
+name = input-boost-off
+
+[Element Front Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Rear Mic]
+switch = off
+volume = off
+
+[Element Dock Mic]
+switch = off
+volume = off
+
+.include analog-input-mic.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input-mic.conf.common b/src/modules/alsa/mixer/paths/analog-input-mic.conf.common
new file mode 100644
index 00000000..2e4f0d81
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-mic.conf.common
@@ -0,0 +1,54 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Common element for all microphone inputs
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[Element Line]
+switch = off
+volume = off
+
+[Element Line Boost]
+switch = off
+volume = off
+
+[Element Aux]
+switch = off
+volume = off
+
+[Element Video]
+switch = off
+volume = off
+
+[Element Mic/Line]
+switch = off
+volume = off
+
+[Element TV Tuner]
+switch = off
+volume = off
+
+[Element FM]
+switch = off
+volume = off
+
+[Element Mic Jack Mode]
+enumeration = select
+
+[Option Mic Jack Mode:Mic In]
+priority = 19
+name = input-microphone
diff --git a/src/modules/alsa/mixer/paths/analog-input-rear-mic.conf b/src/modules/alsa/mixer/paths/analog-input-rear-mic.conf
new file mode 100644
index 00000000..75ed61b0
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-rear-mic.conf
@@ -0,0 +1,81 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where a 'Rear Mic' or 'Rear Mic Boost' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 89
+name = analog-input-microphone-rear
+
+[Element Rear Mic Boost]
+required-any = any
+switch = select
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Option Rear Mic Boost:on]
+name = input-boost-on
+
+[Option Rear Mic Boost:off]
+name = input-boost-off
+
+[Element Rear Mic]
+required-any = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Input Source]
+enumeration = select
+
+[Option Input Source:Rear Mic]
+name = analog-input-microphone-rear
+required-any = any
+
+[Element Capture Source]
+enumeration = select
+
+[Option Capture Source:Rear Mic]
+name = analog-input-microphone-rear
+required-any = any
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Front Mic]
+switch = off
+volume = off
+
+[Element Dock Mic]
+switch = off
+volume = off
+
+.include analog-input-mic.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input-tvtuner.conf b/src/modules/alsa/mixer/paths/analog-input-tvtuner.conf
new file mode 100644
index 00000000..fae3ce83
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-tvtuner.conf
@@ -0,0 +1,66 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where a 'TV Tuner' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 70
+name = analog-input-video
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Line]
+switch = off
+volume = off
+
+[Element Aux]
+switch = off
+volume = off
+
+[Element Video]
+switch = off
+volume = off
+
+[Element Mic/Line]
+switch = off
+volume = off
+
+[Element TV Tuner]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element FM]
+switch = off
+volume = off
+
+.include analog-input.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input-video.conf b/src/modules/alsa/mixer/paths/analog-input-video.conf
new file mode 100644
index 00000000..19f18099
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input-video.conf
@@ -0,0 +1,65 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; For devices where a 'Video' element exists
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 70
+
+[Element Capture]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Mic]
+switch = off
+volume = off
+
+[Element Internal Mic]
+switch = off
+volume = off
+
+[Element Line]
+switch = off
+volume = off
+
+[Element Aux]
+switch = off
+volume = off
+
+[Element Video]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Mic/Line]
+switch = off
+volume = off
+
+[Element TV Tuner]
+switch = off
+volume = off
+
+[Element FM]
+switch = off
+volume = off
+
+.include analog-input.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input.conf b/src/modules/alsa/mixer/paths/analog-input.conf
new file mode 100644
index 00000000..b86c3564
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input.conf
@@ -0,0 +1,83 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; A fallback for devices that lack seperate Mic/Line/Aux/Video/TV
+; Tuner/FM elements
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 100
+
+[Element Capture]
+required = volume
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Mic]
+required-absent = any
+
+[Element Dock Mic]
+required-absent = any
+
+[Element Dock Mic Boost]
+required-absent = any
+
+[Element Front Mic]
+required-absent = any
+
+[Element Front Mic Boost]
+required-absent = any
+
+[Element Int Mic]
+required-absent = any
+
+[Element Int Mic Boost]
+required-absent = any
+
+[Element Internal Mic]
+required-absent = any
+
+[Element Internal Mic Boost]
+required-absent = any
+
+[Element Rear Mic]
+required-absent = any
+
+[Element Rear Mic Boost]
+required-absent = any
+
+[Element Line]
+required-absent = any
+
+[Element Aux]
+required-absent = any
+
+[Element Video]
+required-absent = any
+
+[Element Mic/Line]
+required-absent = any
+
+[Element TV Tuner]
+required-absent = any
+
+[Element FM]
+required-absent = any
+
+.include analog-input.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-input.conf.common b/src/modules/alsa/mixer/paths/analog-input.conf.common
new file mode 100644
index 00000000..94165776
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-input.conf.common
@@ -0,0 +1,290 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Mixer path for PulseAudio's ALSA backend, common elements for all
+; input paths. If multiple options by the same id are discovered they
+; will be suffixed with a number to distuingish them, in the same
+; order they appear here.
+;
+; Source selection should use the following names:
+;
+; input -- If we don't know the exact kind of input
+; input-microphone
+; input-microphone-internal
+; input-microphone-external
+; input-linein
+; input-video
+; input-radio
+; input-docking-microphone
+; input-docking-linein
+; input-docking
+;
+; We explicitly don't want to wrap the following sources:
+;
+; CD
+; Synth/MIDI
+; Phone
+; Mix
+; Digital/SPDIF
+; Master
+; PC Speaker
+;
+; See analog-output.conf.common for an explanation on the directives
+
+;;; 'Input Source Select'
+
+[Element Input Source Select]
+enumeration = select
+
+[Option Input Source Select:Input1]
+name = input
+priority = 10
+
+[Option Input Source Select:Input2]
+name = input
+priority = 5
+
+;;; 'Input Source'
+
+[Element Input Source]
+enumeration = select
+
+[Option Input Source:Digital Mic]
+name = input-microphone
+priority = 20
+
+[Option Input Source:Microphone]
+name = input-microphone
+priority = 20
+
+[Option Input Source:Front Microphone]
+name = input-microphone
+priority = 19
+
+[Option Input Source:Internal Mic 1]
+name = input-microphone
+priority = 19
+
+[Option Input Source:Line-In]
+name = input-linein
+priority = 18
+
+[Option Input Source:Line In]
+name = input-linein
+priority = 18
+
+[Option Input Source:Docking-Station]
+name = input-docking
+priority = 17
+
+[Option Input Source:AUX IN]
+name = input
+priority = 10
+
+;;; 'Capture Source'
+
+[Element Capture Source]
+enumeration = select
+
+[Option Capture Source:TV Tuner]
+name = input-video
+
+[Option Capture Source:FM]
+name = input-radio
+
+[Option Capture Source:Mic/Line]
+name = input
+
+[Option Capture Source:Line/Mic]
+name = input
+
+[Option Capture Source:Microphone]
+name = input-microphone
+
+[Option Capture Source:Int DMic]
+name = input-microphone-internal
+
+[Option Capture Source:iMic]
+name = input-microphone-internal
+
+[Option Capture Source:i-Mic]
+name = input-microphone-internal
+
+[Option Capture Source:Internal Microphone]
+name = input-microphone-internal
+
+[Option Capture Source:Front Microphone]
+name = input-microphone
+
+[Option Capture Source:Mic1]
+name = input-microphone
+
+[Option Capture Source:Mic2]
+name = input-microphone
+
+[Option Capture Source:D-Mic]
+name = input-microphone
+
+[Option Capture Source:IntMic]
+name = input-microphone-internal
+
+[Option Capture Source:ExtMic]
+name = input-microphone-external
+
+[Option Capture Source:Ext Mic]
+name = input-microphone-external
+
+[Option Capture Source:E-Mic]
+name = input-microphone-external
+
+[Option Capture Source:e-Mic]
+name = input-microphone-external
+
+[Option Capture Source:LineIn]
+name = input-linein
+
+[Option Capture Source:Analog]
+name = input
+
+[Option Capture Source:Line-In]
+name = input-linein
+
+[Option Capture Source:Line In]
+name = input-linein
+
+[Option Capture Source:Video]
+name = input-video
+
+[Option Capture Source:Aux]
+name = input
+
+[Option Capture Source:Aux0]
+name = input
+
+[Option Capture Source:Aux1]
+name = input
+
+[Option Capture Source:Aux2]
+name = input
+
+[Option Capture Source:Aux3]
+name = input
+
+[Option Capture Source:AUX IN]
+name = input
+
+[Option Capture Source:Aux In]
+name = input
+
+[Option Capture Source:AOUT]
+name = input
+
+[Option Capture Source:AUX]
+name = input
+
+[Option Capture Source:Cam Mic]
+name = input-microphone
+
+[Option Capture Source:Digital Mic]
+name = input-microphone
+
+[Option Capture Source:Digital Mic 1]
+name = input-microphone
+
+[Option Capture Source:Digital Mic 2]
+name = input-microphone
+
+[Option Capture Source:Analog Inputs]
+name = input
+
+[Option Capture Source:Unknown1]
+name = input
+
+[Option Capture Source:Unknown2]
+name = input
+
+[Option Capture Source:Docking-Station]
+name = input-docking
+
+;;; 'Mic Jack Mode'
+
+[Element Mic Jack Mode]
+enumeration = select
+
+[Option Mic Jack Mode:Mic In]
+name = input-microphone
+
+[Option Mic Jack Mode:Line In]
+name = input-linein
+
+;;; 'Digital Input Source'
+
+[Element Digital Input Source]
+enumeration = select
+
+[Option Digital Input Source:Analog Inputs]
+name = input
+
+[Option Digital Input Source:Digital Mic 1]
+name = input-microphone
+
+[Option Digital Input Source:Digital Mic 2]
+name = input-microphone
+
+;;; 'Analog Source'
+
+[Element Analog Source]
+enumeration = select
+
+[Option Analog Source:Mic]
+name = input-microphone
+
+[Option Analog Source:Line in]
+name = input-linein
+
+[Option Analog Source:Aux]
+name = input
+
+;;; 'Shared Mic/Line in'
+
+[Element Shared Mic/Line in]
+enumeration = select
+
+[Option Shared Mic/Line in:Mic in]
+name = input-microphone
+
+[Option Shared Mic/Line in:Line in]
+name = input-linein
+
+;;; Various Boosts
+
+[Element Capture Boost]
+switch = select
+
+[Option Capture Boost:on]
+name = input-boost-on
+
+[Option Capture Boost:off]
+name = input-boost-off
+
+[Element Auto Gain Control]
+switch = select
+
+[Option Auto Gain Control:on]
+name = input-agc-on
+
+[Option Auto Gain Control:off]
+name = input-agc-off
diff --git a/src/modules/alsa/mixer/paths/analog-output-desktop-speaker.conf b/src/modules/alsa/mixer/paths/analog-output-desktop-speaker.conf
new file mode 100644
index 00000000..dfdecf41
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-output-desktop-speaker.conf
@@ -0,0 +1,99 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Path for mixers that have a 'Desktop Speaker' control
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 101
+name = analog-output-speaker
+
+[Element Hardware Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master Mono]
+switch = off
+volume = off
+
+; This profile path is intended to control the desktop speaker, not
+; the headphones. But it should not hurt if we leave the headphone
+; jack enabled nonetheless.
+[Element Headphone]
+switch = mute
+volume = zero
+
+[Element Headphone2]
+switch = mute
+volume = zero
+
+[Element Speaker]
+switch = off
+volume = off
+
+[Element Desktop Speaker]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Front]
+switch = mute
+volume = merge
+override-map.1 = all-front
+override-map.2 = front-left,front-right
+
+[Element Rear]
+switch = mute
+volume = merge
+override-map.1 = all-rear
+override-map.2 = rear-left,rear-right
+
+[Element Surround]
+switch = mute
+volume = merge
+override-map.1 = all-rear
+override-map.2 = rear-left,rear-right
+
+[Element Side]
+switch = mute
+volume = merge
+override-map.1 = all-side
+override-map.2 = side-left,side-right
+
+[Element Center]
+switch = mute
+volume = merge
+override-map.1 = all-center
+override-map.2 = all-center,all-center
+
+[Element LFE]
+switch = mute
+volume = merge
+override-map.1 = lfe
+override-map.2 = lfe,lfe
+
+.include analog-output.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-output-headphones-2.conf b/src/modules/alsa/mixer/paths/analog-output-headphones-2.conf
new file mode 100644
index 00000000..e47543f5
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-output-headphones-2.conf
@@ -0,0 +1,87 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Path for mixers that have a 'Headphone2' control
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 89
+name = analog-output-headphones
+
+[Element Hardware Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master Mono]
+switch = off
+volume = off
+
+; This profile path is intended to control the second headphones, not
+; the first headphones. But it should not hurt if we leave the
+; headphone jack enabled nonetheless.
+[Element Headphone]
+switch = mute
+volume = zero
+
+[Element Headphone2]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Speaker]
+switch = off
+volume = off
+
+[Element Desktop Speaker]
+switch = off
+volume = off
+
+[Element Front]
+switch = off
+volume = off
+
+[Element Rear]
+switch = off
+volume = off
+
+[Element Surround]
+switch = off
+volume = off
+
+[Element Side]
+switch = off
+volume = off
+
+[Element Center]
+switch = off
+volume = off
+
+[Element LFE]
+switch = off
+volume = off
+
+.include analog-output.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-output-headphones.conf b/src/modules/alsa/mixer/paths/analog-output-headphones.conf
new file mode 100644
index 00000000..1d7bb0ba
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-output-headphones.conf
@@ -0,0 +1,87 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Path for mixers that have a 'Headphone' control
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 90
+name = analog-output-headphones
+
+[Element Hardware Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master Mono]
+switch = off
+volume = off
+
+[Element Headphone]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+; This profile path is intended to control the first headphones, not
+; the second headphones. But it should not hurt if we leave the second
+; headphone jack enabled nonetheless.
+[Element Headphone2]
+switch = mute
+volume = zero
+
+[Element Speaker]
+switch = off
+volume = off
+
+[Element Desktop Speaker]
+switch = off
+volume = off
+
+[Element Front]
+switch = off
+volume = off
+
+[Element Rear]
+switch = off
+volume = off
+
+[Element Surround]
+switch = off
+volume = off
+
+[Element Side]
+switch = off
+volume = off
+
+[Element Center]
+switch = off
+volume = off
+
+[Element LFE]
+switch = off
+volume = off
+
+.include analog-output.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-output-lfe-on-mono.conf b/src/modules/alsa/mixer/paths/analog-output-lfe-on-mono.conf
new file mode 100644
index 00000000..67ee32f7
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-output-lfe-on-mono.conf
@@ -0,0 +1,89 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Intended for usage in laptops that have a seperate LFE speaker
+; connected to the Master mono connector
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 40
+
+[Element Hardware Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master]
+switch = mute
+volume = merge
+override-map.1 = all-no-lfe
+override-map.2 = all-left,all-right
+
+[Element Master Mono]
+required = any
+switch = mute
+volume = merge
+override-map.1 = lfe
+override-map.2 = lfe,lfe
+
+; This profile path is intended to control the speaker, not the
+; headphones. But it should not hurt if we leave the headphone jack
+; enabled nonetheless.
+[Element Headphone]
+switch = mute
+volume = zero
+
+[Element Headphone2]
+switch = mute
+volume = zero
+
+[Element Speaker]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Desktop Speaker]
+switch = off
+volume = off
+
+[Element Front]
+switch = off
+volume = off
+
+[Element Rear]
+switch = off
+volume = off
+
+[Element Surround]
+switch = off
+volume = off
+
+[Element Side]
+switch = off
+volume = off
+
+[Element Center]
+switch = off
+volume = off
+
+[Element LFE]
+switch = off
+volume = off
+
+.include analog-output.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-output-mono.conf b/src/modules/alsa/mixer/paths/analog-output-mono.conf
new file mode 100644
index 00000000..13a2d6aa
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-output-mono.conf
@@ -0,0 +1,86 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Intended for usage on boards that have a seperate Mono output plug.
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 50
+
+[Element Hardware Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master]
+switch = off
+volume = off
+
+[Element Master Mono]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+; This profile path is intended to control the speaker, not the
+; headphones. But it should not hurt if we leave the headphone jack
+; enabled nonetheless.
+[Element Headphone]
+switch = mute
+volume = zero
+
+[Element Headphone2]
+switch = mute
+volume = zero
+
+[Element Speaker]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Desktop Speaker]
+switch = off
+volume = off
+
+[Element Front]
+switch = off
+volume = off
+
+[Element Rear]
+switch = off
+volume = off
+
+[Element Surround]
+switch = off
+volume = off
+
+[Element Side]
+switch = off
+volume = off
+
+[Element Center]
+switch = off
+volume = off
+
+[Element LFE]
+switch = off
+volume = off
+
+.include analog-output.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-output-speaker.conf b/src/modules/alsa/mixer/paths/analog-output-speaker.conf
new file mode 100644
index 00000000..c6916d6b
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-output-speaker.conf
@@ -0,0 +1,99 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Path for mixers that have a 'Speaker' control
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 100
+name = analog-output-speaker
+
+[Element Hardware Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master Mono]
+switch = off
+volume = off
+
+; This profile path is intended to control the speaker, not the
+; headphones. But it should not hurt if we leave the headphone jack
+; enabled nonetheless.
+[Element Headphone]
+switch = mute
+volume = zero
+
+[Element Headphone2]
+switch = mute
+volume = zero
+
+[Element Speaker]
+required = any
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Desktop Speaker]
+switch = off
+volume = off
+
+[Element Front]
+switch = mute
+volume = merge
+override-map.1 = all-front
+override-map.2 = front-left,front-right
+
+[Element Rear]
+switch = mute
+volume = merge
+override-map.1 = all-rear
+override-map.2 = rear-left,rear-right
+
+[Element Surround]
+switch = mute
+volume = merge
+override-map.1 = all-rear
+override-map.2 = rear-left,rear-right
+
+[Element Side]
+switch = mute
+volume = merge
+override-map.1 = all-side
+override-map.2 = side-left,side-right
+
+[Element Center]
+switch = mute
+volume = merge
+override-map.1 = all-center
+override-map.2 = all-center,all-center
+
+[Element LFE]
+switch = mute
+volume = merge
+override-map.1 = lfe
+override-map.2 = lfe,lfe
+
+.include analog-output.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-output.conf b/src/modules/alsa/mixer/paths/analog-output.conf
new file mode 100644
index 00000000..50fc88ea
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-output.conf
@@ -0,0 +1,96 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Intended for the 'default' output. Note that a-o-speaker.conf has a
+; higher priority than this
+;
+; See analog-output.conf.common for an explanation on the directives
+
+[General]
+priority = 99
+
+[Element Hardware Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element Master Mono]
+switch = off
+volume = off
+
+; This profile path is intended to control the default output, not the
+; headphones. But it should not hurt if we leave the headphone jack
+; enabled nonetheless.
+[Element Headphone]
+switch = mute
+volume = zero
+
+[Element Headphone2]
+switch = mute
+volume = zero
+
+[Element Speaker]
+switch = mute
+volume = off
+
+[Element Desktop Speaker]
+switch = mute
+volume = off
+
+[Element Front]
+switch = mute
+volume = merge
+override-map.1 = all-front
+override-map.2 = front-left,front-right
+
+[Element Rear]
+switch = mute
+volume = merge
+override-map.1 = all-rear
+override-map.2 = rear-left,rear-right
+
+[Element Surround]
+switch = mute
+volume = merge
+override-map.1 = all-rear
+override-map.2 = rear-left,rear-right
+
+[Element Side]
+switch = mute
+volume = merge
+override-map.1 = all-side
+override-map.2 = side-left,side-right
+
+[Element Center]
+switch = mute
+volume = merge
+override-map.1 = all-center
+override-map.2 = all-center,all-center
+
+[Element LFE]
+switch = mute
+volume = merge
+override-map.1 = lfe
+override-map.2 = lfe,lfe
+
+.include analog-output.conf.common
diff --git a/src/modules/alsa/mixer/paths/analog-output.conf.common b/src/modules/alsa/mixer/paths/analog-output.conf.common
new file mode 100644
index 00000000..ccaa494b
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/analog-output.conf.common
@@ -0,0 +1,147 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Common part of all paths
+
+; So here's generally how mixer paths are used by PA: PA goes through
+; a mixer path file from top to bottom and checks if a mixer element
+; described therein exists. If so it is added to the list of mixer
+; elements PA will control, keeping the order it read them in. If a
+; mixer element described here has set the required= or
+; required-absent= directives a path might not be accepted as valid
+; and is ignored in its entirety (see below). However usually if a
+; element listed here is missing this one element is ignored but not
+; the entire path.
+;
+; When a device shall be muted/unmuted *all* elements listed in a path
+; file with "switch = mute" will be toggled.
+;
+; When a device shall change its volume, PA will got through the list
+; of all elements with "volume = merge" and set the volume on the
+; first element. If that element does not support dB volumes, this is
+; where the story ends. If it does support dB volumes, PA divides the
+; requested volume by the volume that was set on this element, and
+; then go on to the next element with "volume = merge" and then set
+; that there, and so on. That way the first volume element in the
+; path will be the one that does the 'biggest' part of the overall
+; volume adjustment, with the remaining elements usually being set to
+; some value next to 0dB. This logic makes sure we get the full range
+; over all volume sliders and a very high granularity of volumes
+; already in hardware.
+;
+; All switches and enumerations set to "select" are exposed via the
+; "port" functionality of sinks/sources. Basically every possible
+; switch setting and every possible enumeration setting will be
+; combined and made into a "port". So make sure you don't list too
+; many switches/enums for exposing, because the number of ports might
+; rise exponentially.
+;
+; Only one path can be selected at a time. All paths that are valid
+; for an audio device will be exposed as "port" for the sink/source.
+
+
+; [General]
+; priority = ... # Priority for this path
+; description = ...
+;
+; [Option ...:...] # For each option of an enumeration or switch element
+; # that shall be exposed as a sink/source port. Needs to
+; # be named after the Element, followed by a colon, followed
+; # by the option name, resp. on/off if the element is a switch.
+; name = ... # Logical name to use in the path identifier
+; priority = ... # Priority if this is made into a device port
+; required = ignore | enumeration | any # In this element, this option must exist or the path will be invalid. ("any" is an alias for "enumeration".)
+; required-any = ignore | enumeration | any # In this element, either this or another option must exist (or an element)
+; required-absent = ignore | enumeration | any # In this element, this option must not exist or the path will be invalid
+;
+; [Element ...] # For each element that we shall control
+; required = ignore | switch | volume | enumeration | any # If set, require this element to be of this kind and available,
+; # otherwise don't consider this path valid for the card
+; required-any = ignore | switch | volume | enumeration | any # If set, at least one of the elements with required-any in this
+; # path must be present, otherwise this path is invalid for the card
+; required-absent = ignore | switch | volume # If set, require this element to not be of this kind and not
+; # available, otherwise don't consider this path valid for the card
+;
+; switch = ignore | mute | off | on | select # What to do with this switch: ignore it, make it follow mute status,
+; # always set it to off, always to on, or make it selectable as port.
+; # If set to 'select' you need to define an Option section for on
+; # and off
+; volume = ignore | merge | off | zero | <volume step> # What to do with this volume: ignore it, merge it into the device
+; # volume slider, always set it to the lowest value possible, or always
+; # set it to 0 dB (for whatever that means), or always set it to
+; # <volume step> (this only makes sense in path configurations where
+; # the exact hardware and driver are known beforehand).
+; volume-limit = <volume step> # Limit the maximum volume by disabling the volume steps above <volume step>.
+; enumeration = ignore | select # What to do with this enumeration, ignore it or make it selectable
+; # via device ports. If set to 'select' you need to define an Option section
+; # for each of the items you want to expose
+; direction = playback | capture # Is this relevant only for playback or capture? If not set this will implicitly be
+; # set the direction of the PCM device is opened as. Generally this doesn't need to be set
+; # unless you have a broken driver that has playback controls marked for capture or vice
+; # versa
+; direction-try-other = no | yes # If the element does not supported what is requested, try the other direction, too?
+;
+; override-map.1 = ... # Override the channel mask of the mixer control if the control only exposes a single channel
+; override-map.2 = ... # Override the channel masks of the mixer control if the control only exposes two channels
+; # Override maps should list for each element channel which high-level channels it controls via a
+; # channel mask. A channel mask may either be the name of a single channel, or the words "all-left",
+; # "all-right", "all-center", "all-front", "all-rear", and "all" to encode a specific subset of
+; # channels in a mask
+
+[Element PCM]
+switch = mute
+volume = merge
+override-map.1 = all
+override-map.2 = all-left,all-right
+
+[Element External Amplifier]
+switch = select
+
+[Option External Amplifier:on]
+name = output-amplifier-on
+priority = 10
+
+[Option External Amplifier:off]
+name = output-amplifier-off
+priority = 0
+
+[Element Bass Boost]
+switch = select
+
+[Option Bass Boost:on]
+name = output-bass-boost-on
+priority = 0
+
+[Option Bass Boost:off]
+name = output-bass-boost-off
+priority = 10
+
+;;; 'Analog Output'
+
+[Element Analog Output]
+enumeration = select
+
+[Option Analog Output:Speakers]
+name = output-speaker
+priority = 10
+
+[Option Analog Output:Headphones]
+name = output-headphones
+priority = 9
+
+[Option Analog Output:FP Headphones]
+name = output-headphones
+priority = 8
diff --git a/src/modules/alsa/mixer/paths/iec958-stereo-output.conf b/src/modules/alsa/mixer/paths/iec958-stereo-output.conf
new file mode 100644
index 00000000..8506a580
--- /dev/null
+++ b/src/modules/alsa/mixer/paths/iec958-stereo-output.conf
@@ -0,0 +1,19 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+
+[Element IEC958]
+switch = mute
diff --git a/src/modules/alsa/mixer/profile-sets/90-pulseaudio.rules b/src/modules/alsa/mixer/profile-sets/90-pulseaudio.rules
new file mode 100644
index 00000000..e1da3314
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/90-pulseaudio.rules
@@ -0,0 +1,40 @@
+# do not edit this file, it will be overwritten on update
+
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+SUBSYSTEM!="sound", GOTO="pulseaudio_end"
+ACTION!="change", GOTO="pulseaudio_end"
+KERNEL!="card*", GOTO="pulseaudio_end"
+
+# Some specific work arounds until we can handle heasets/handsets properly (i.e. "Speaker" only, no "master")
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="01ab", ENV{PULSE_PROFILE_SET}="usb-headset.conf"
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="0a0c", ENV{PULSE_PROFILE_SET}="usb-headset.conf"
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="1395", ATTRS{idProduct}=="0002", ENV{PULSE_PROFILE_SET}="usb-headset.conf"
+# UAC1.0 Sennheiser Dongle
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="1395", ATTRS{idProduct}=="3554", ENV{PULSE_PROFILE_SET}="usb-headset.conf"
+# BT Agile Handset
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="1885", ATTRS{idProduct}=="0501", ENV{PULSE_PROFILE_SET}="usb-headset.conf"
+
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="17cc", ATTRS{idProduct}=="1978", ENV{PULSE_PROFILE_SET}="native-instruments-audio8dj.conf"
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="17cc", ATTRS{idProduct}=="0839", ENV{PULSE_PROFILE_SET}="native-instruments-audio4dj.conf"
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="17cc", ATTRS{idProduct}=="baff", ENV{PULSE_PROFILE_SET}="native-instruments-traktorkontrol-s4.conf"
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="17cc", ATTRS{idProduct}=="4711", ENV{PULSE_PROFILE_SET}="native-instruments-korecontroller.conf"
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="17cc", ATTRS{idProduct}=="1011", ENV{PULSE_PROFILE_SET}="native-instruments-traktor-audio6.conf"
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="17cc", ATTRS{idProduct}=="1021", ENV{PULSE_PROFILE_SET}="native-instruments-traktor-audio10.conf"
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="0763", ATTRS{idProduct}=="2012", ENV{PULSE_PROFILE_SET}="maudio-fasttrack-pro.conf"
+
+LABEL="pulseaudio_end"
diff --git a/src/modules/alsa/mixer/profile-sets/default.conf b/src/modules/alsa/mixer/profile-sets/default.conf
new file mode 100644
index 00000000..283edfb3
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/default.conf
@@ -0,0 +1,180 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Default profile definitions for the ALSA backend of PulseAudio. This
+; is used as fallback for all cards that have no special mapping
+; assigned (and should be good enough for the vast majority of
+; cards). If you want to assign a different profile set than this one
+; to a device, either set the udev property PULSE_PROFILE_SET for the
+; card, or use the "profile_set" module argument when loading
+; module-alsa-card.
+;
+; So what is this about? Simply, what we do here is map ALSA devices
+; to how they are exposed in PA. We say which ALSA device string to
+; use to open a device, which channel mapping to use then, and which
+; mixer path to use. This is encoded in a 'mapping'. Multiple of these
+; mappings can be bound together in a 'profile' which is then directly
+; exposed in the UI as a card profile. Each mapping assigned to a
+; profile will result in one sink/source to be created if the profile
+; is selected for the card.
+;
+; Additionally, the path set configuration files can describe the
+; decibel values assigned to the steps of the volume elements. This
+; can be used to work around situations when the alsa driver doesn't
+; provide any decibel information, or when the information is
+; incorrect.
+
+
+; [General]
+; auto-profiles = no | yes # Instead of defining all profiles manually, autogenerate
+; # them by combining every input mapping with every output mapping.
+;
+; [Mapping id]
+; device-strings = ... # ALSA device string. %f will be replaced by the card identifier.
+; channel-map = ... # Channel mapping to use for this device
+; description = ...
+; paths-input = ... # A list of mixer paths to use. Every path in this list will be probed.
+; # If multiple are found to be working they will be available as device ports
+; paths-output = ...
+; element-input = ... # Instead of configuring a full mixer path simply configure a single
+; # mixer element for volume/mute handling
+; element-output = ...
+; priority = ...
+; direction = any | input | output # Only useful for?
+;
+; [Profile id]
+; input-mappings = ... # Lists mappings for sources on this profile, those mapping must be
+; # defined in this file too
+; output-mappings = ... # Lists mappings for sinks on this profile, those mappings must be
+; # defined in this file too
+; description = ...
+; priority = ... # Numeric value to deduce priority for this profile
+; skip-probe = no | yes # Skip probing for availability? If this is yes then this profile
+; # will be assumed as working without probing. Makes initialization
+; # a bit faster but only works if the card is really known well.
+;
+; [DecibelFix element] # Decibel fixes can be used to work around missing or incorrect dB
+; # information from alsa. A decibel fix is a table that maps volume steps
+; # to decibel values for one volume element. The "element" part in the
+; # section title is the name of the volume element.
+; #
+; # NOTE: This feature is meant just as a help for figuring out the correct
+; # decibel values. Pulseaudio is not the correct place to maintain the
+; # decibel mappings!
+; #
+; # If you need this feature, then you should make sure that when you have
+; # the correct values figured out, the alsa driver developers get informed
+; # too, so that they can fix the driver.
+;
+; db-values = ... # The option value consists of pairs of step numbers and decibel values.
+; # The pairs are separated with whitespace, and steps are separated from
+; # the corresponding decibel values with a colon. The values must be in an
+; # increasing order. Here's an example of a valid string:
+; #
+; # "0:-40.50 1:-38.70 3:-33.00 11:0"
+; #
+; # The lowest step imposes a lower limit for hardware volume and the
+; # highest step correspondingly imposes a higher limit. That means that
+; # that the mixer will never be set outside those values - the rest of the
+; # volume scale is done using software volume.
+; #
+; # As can be seen in the example, you don't need to specify a dB value for
+; # each step. The dB values for skipped steps will be linearly interpolated
+; # using the nearest steps that are given.
+
+[General]
+auto-profiles = yes
+
+[Mapping analog-mono]
+device-strings = hw:%f
+channel-map = mono
+paths-output = analog-output analog-output-speaker analog-output-desktop-speaker analog-output-headphones analog-output-headphones-2 analog-output-mono analog-output-lfe-on-mono
+paths-input = analog-input-front-mic analog-input-rear-mic analog-input-internal-mic analog-input-dock-mic analog-input analog-input-mic analog-input-linein analog-input-aux analog-input-video analog-input-tvtuner analog-input-fm analog-input-mic-line
+priority = 1
+
+[Mapping analog-stereo]
+device-strings = front:%f hw:%f
+channel-map = left,right
+paths-output = analog-output analog-output-speaker analog-output-desktop-speaker analog-output-headphones analog-output-headphones-2 analog-output-mono analog-output-lfe-on-mono
+paths-input = analog-input-front-mic analog-input-rear-mic analog-input-internal-mic analog-input-dock-mic analog-input analog-input-mic analog-input-linein analog-input-aux analog-input-video analog-input-tvtuner analog-input-fm analog-input-mic-line
+priority = 10
+
+[Mapping analog-surround-40]
+device-strings = surround40:%f
+channel-map = front-left,front-right,rear-left,rear-right
+paths-output = analog-output analog-output-speaker analog-output-desktop-speaker analog-output-lfe-on-mono
+priority = 7
+direction = output
+
+[Mapping analog-surround-41]
+device-strings = surround41:%f
+channel-map = front-left,front-right,rear-left,rear-right,lfe
+paths-output = analog-output analog-output-speaker analog-output-desktop-speaker analog-output-lfe-on-mono
+priority = 8
+direction = output
+
+[Mapping analog-surround-50]
+device-strings = surround50:%f
+channel-map = front-left,front-right,rear-left,rear-right,front-center
+paths-output = analog-output analog-output-speaker analog-output-desktop-speaker analog-output-lfe-on-mono
+priority = 7
+direction = output
+
+[Mapping analog-surround-51]
+device-strings = surround51:%f
+channel-map = front-left,front-right,rear-left,rear-right,front-center,lfe
+paths-output = analog-output analog-output-speaker analog-output-desktop-speaker analog-output-lfe-on-mono
+priority = 8
+direction = output
+
+[Mapping analog-surround-71]
+device-strings = surround71:%f
+channel-map = front-left,front-right,rear-left,rear-right,front-center,lfe,side-left,side-right
+description = Analog Surround 7.1
+paths-output = analog-output analog-output-speaker analog-output-desktop-speaker analog-output-lfe-on-mono
+priority = 7
+direction = output
+
+[Mapping iec958-stereo]
+device-strings = iec958:%f
+channel-map = left,right
+paths-input = iec958-stereo-input
+paths-output = iec958-stereo-output
+priority = 5
+
+[Mapping iec958-ac3-surround-40]
+device-strings = a52:%f
+channel-map = front-left,front-right,rear-left,rear-right
+priority = 2
+direction = output
+
+[Mapping iec958-ac3-surround-51]
+device-strings = a52:%f
+channel-map = front-left,front-right,rear-left,rear-right,front-center,lfe
+priority = 3
+direction = output
+
+[Mapping hdmi-stereo]
+device-strings = hdmi:%f
+channel-map = left,right
+priority = 4
+direction = output
+
+; An example for defining multiple-sink profiles
+#[Profile output:analog-stereo+output:iec958-stereo+input:analog-stereo]
+#description = Foobar
+#output-mappings = analog-stereo iec958-stereo
+#input-mappings = analog-stereo
diff --git a/src/modules/alsa/mixer/profile-sets/maudio-fasttrack-pro.conf b/src/modules/alsa/mixer/profile-sets/maudio-fasttrack-pro.conf
new file mode 100644
index 00000000..75f51121
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/maudio-fasttrack-pro.conf
@@ -0,0 +1,85 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; M-Audio FastTrack Pro
+;
+; This card has one duplex stereo channel called A and an additional
+; stereo output channel called B.
+;
+; We knowingly only define a subset of the theoretically possible
+; mapping combinations as profiles here.
+;
+; See default.conf for an explanation on the directives used here.
+
+[General]
+auto-profiles = no
+
+[Mapping analog-stereo-a-output]
+description = Analog Stereo Channel A
+device-strings = hw:%f,0,0
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-a-input]
+description = Analog Stereo Channel A
+device-strings = hw:%f,0,0
+channel-map = left,right
+direction = input
+
+[Mapping analog-stereo-b-output]
+description = Analog Stereo Channel B
+device-strings = hw:%f,1,0
+channel-map = left,right
+direction = output
+
+[Profile output:analog-stereo-all+input:analog-stereo-all]
+description = Analog Stereo Duplex Channel A, Analog Stereo output Channel B
+output-mappings = analog-stereo-a-output analog-stereo-b-output
+input-mappings = analog-stereo-a-input
+priority = 100
+skip-probe = yes
+
+[Profile output:analog-stereo-a-output+input:analog-stereo-a-input]
+description = Analog Stereo Duplex Channel A
+output-mappings = analog-stereo-a-output
+input-mappings = analog-stereo-a-input
+priority = 40
+skip-probe = yes
+
+[Profile output:analog-stereo-b+input:analog-stereo-b]
+description = Analog Stereo Output Channel B
+output-mappings = analog-stereo-b-output
+input-mappings =
+priority = 50
+skip-probe = yes
+
+[Profile output:analog-stereo-a]
+description = Analog Stereo Output Channel A
+output-mappings = analog-stereo-a-output
+priority = 5
+skip-probe = yes
+
+[Profile output:analog-stereo-b]
+description = Analog Stereo Output Channel B
+output-mappings = analog-stereo-b-output
+priority = 6
+skip-probe = yes
+
+[Profile input:analog-stereo-a]
+description = Analog Stereo Input Channel A
+input-mappings = analog-stereo-a-input
+priority = 2
+skip-probe = yes
diff --git a/src/modules/alsa/mixer/profile-sets/native-instruments-audio4dj.conf b/src/modules/alsa/mixer/profile-sets/native-instruments-audio4dj.conf
new file mode 100644
index 00000000..2b835308
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/native-instruments-audio4dj.conf
@@ -0,0 +1,91 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Native Instruments Audio 4 DJ
+;
+; This card has two stereo pairs of input and two stereo pairs of
+; output, named channels A and B. Channel B has an additional
+; Headphone connector.
+;
+; We knowingly only define a subset of the theoretically possible
+; mapping combinations as profiles here.
+;
+; See default.conf for an explanation on the directives used here.
+
+[General]
+auto-profiles = no
+
+[Mapping analog-stereo-a]
+description = Analog Stereo Channel A
+device-strings = hw:%f,0,0
+channel-map = left,right
+
+[Mapping analog-stereo-b-output]
+description = Analog Stereo Channel B (Headphones)
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-b-input]
+description = Analog Stereo Channel B
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = input
+
+[Profile output:analog-stereo-all+input:analog-stereo-all]
+description = Analog Stereo Duplex Channels A, B (Headphones)
+output-mappings = analog-stereo-a analog-stereo-b-output
+input-mappings = analog-stereo-a analog-stereo-b-input
+priority = 100
+skip-probe = yes
+
+[Profile output:analog-stereo-a+input:analog-stereo-a]
+description = Analog Stereo Duplex Channel A
+output-mappings = analog-stereo-a
+input-mappings = analog-stereo-a
+priority = 40
+skip-probe = yes
+
+[Profile output:analog-stereo-b+input:analog-stereo-b]
+description = Analog Stereo Duplex Channel B (Headphones)
+output-mappings = analog-stereo-b-output
+input-mappings = analog-stereo-b-input
+priority = 50
+skip-probe = yes
+
+[Profile output:analog-stereo-a]
+description = Analog Stereo Output Channel A
+output-mappings = analog-stereo-a
+priority = 5
+skip-probe = yes
+
+[Profile output:analog-stereo-b]
+description = Analog Stereo Output Channel B (Headphones)
+output-mappings = analog-stereo-b-output
+priority = 6
+skip-probe = yes
+
+[Profile input:analog-stereo-a]
+description = Analog Stereo Input Channel A
+input-mappings = analog-stereo-a
+priority = 2
+skip-probe = yes
+
+[Profile input:analog-stereo-b]
+description = Analog Stereo Input Channel B
+input-mappings = analog-stereo-b-input
+priority = 1
+skip-probe = yes
diff --git a/src/modules/alsa/mixer/profile-sets/native-instruments-audio8dj.conf b/src/modules/alsa/mixer/profile-sets/native-instruments-audio8dj.conf
new file mode 100644
index 00000000..3fe3cc56
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/native-instruments-audio8dj.conf
@@ -0,0 +1,162 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Native Instruments Audio 8 DJ
+;
+; This card has four stereo pairs of input and four stereo pairs of
+; output, named channels A to D. Channel C has an additional Mic/Line
+; connector, channel D an additional Headphone connector.
+;
+; We knowingly only define a subset of the theoretically possible
+; mapping combinations as profiles here.
+;
+; See default.conf for an explanation on the directives used here.
+
+[General]
+auto-profiles = no
+
+[Mapping analog-stereo-a]
+description = Analog Stereo Channel A
+device-strings = hw:%f,0,0
+channel-map = left,right
+
+[Mapping analog-stereo-b]
+description = Analog Stereo Channel B
+device-strings = hw:%f,0,1
+channel-map = left,right
+
+# Since we want to set a different description for channel C's/D's input
+# and output we define two seperate mappings for them
+[Mapping analog-stereo-c-output]
+description = Analog Stereo Channel C
+device-strings = hw:%f,0,2
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-c-input]
+description = Analog Stereo Channel C (Line/Mic)
+device-strings = hw:%f,0,2
+channel-map = left,right
+direction = input
+
+[Mapping analog-stereo-d-output]
+description = Analog Stereo Channel D (Headphones)
+device-strings = hw:%f,0,3
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-d-input]
+description = Analog Stereo Channel D
+device-strings = hw:%f,0,3
+channel-map = left,right
+direction = input
+
+[Profile output:analog-stereo-all+input:analog-stereo-all]
+description = Analog Stereo Duplex Channels A, B, C (Line/Mic), D (Headphones)
+output-mappings = analog-stereo-a analog-stereo-b analog-stereo-c-output analog-stereo-d-output
+input-mappings = analog-stereo-a analog-stereo-b analog-stereo-c-input analog-stereo-d-input
+priority = 100
+skip-probe = yes
+
+[Profile output:analog-stereo-d+input:analog-stereo-c]
+description = Analog Stereo Channel D (Headphones) Output, Channel C (Line/Mic) Input
+output-mappings = analog-stereo-d-output
+input-mappings = analog-stereo-c-input
+priority = 90
+skip-probe = yes
+
+[Profile output:analog-stereo-c-d+input:analog-stereo-c-d]
+description = Analog Stereo Duplex Channels C (Line/Mic), D (Line/Mic)
+output-mappings = analog-stereo-c-output analog-stereo-d-output
+input-mappings = analog-stereo-c-input analog-stereo-d-input
+priority = 80
+skip-probe = yes
+
+[Profile output:analog-stereo-a+input:analog-stereo-a]
+description = Analog Stereo Duplex Channel A
+output-mappings = analog-stereo-a
+input-mappings = analog-stereo-a
+priority = 50
+skip-probe = yes
+
+[Profile output:analog-stereo-b+input:analog-stereo-b]
+description = Analog Stereo Duplex Channel B
+output-mappings = analog-stereo-b
+input-mappings = analog-stereo-b
+priority = 40
+skip-probe = yes
+
+[Profile output:analog-stereo-c+input:analog-stereo-c]
+description = Analog Stereo Duplex Channel C (Line/Mic)
+output-mappings = analog-stereo-c-output
+input-mappings = analog-stereo-c-input
+priority = 60
+skip-probe = yes
+
+[Profile output:analog-stereo-d+input:analog-stereo-d]
+description = Analog Stereo Duplex Channel D (Headphones)
+output-mappings = analog-stereo-d-output
+input-mappings = analog-stereo-d-input
+priority = 70
+skip-probe = yes
+
+[Profile output:analog-stereo-a]
+description = Analog Stereo Output Channel A
+output-mappings = analog-stereo-a
+priority = 6
+skip-probe = yes
+
+[Profile output:analog-stereo-b]
+description = Analog Stereo Output Channel B
+output-mappings = analog-stereo-b
+priority = 5
+skip-probe = yes
+
+[Profile output:analog-stereo-c]
+description = Analog Stereo Output Channel C
+output-mappings = analog-stereo-c-output
+priority = 7
+skip-probe = yes
+
+[Profile output:analog-stereo-d]
+description = Analog Stereo Output Channel D (Headphones)
+output-mappings = analog-stereo-d-output
+priority = 8
+skip-probe = yes
+
+[Profile input:analog-stereo-a]
+description = Analog Stereo Input Channel A
+input-mappings = analog-stereo-a
+priority = 2
+skip-probe = yes
+
+[Profile input:analog-stereo-b]
+description = Analog Stereo Input Channel B
+input-mappings = analog-stereo-b
+priority = 1
+skip-probe = yes
+
+[Profile input:analog-stereo-c]
+description = Analog Stereo Input Channel C (Line/Mic)
+input-mappings = analog-stereo-c-input
+priority = 4
+skip-probe = yes
+
+[Profile input:analog-stereo-d]
+description = Analog Stereo Input Channel D
+input-mappings = analog-stereo-d-input
+priority = 3
+skip-probe = yes
diff --git a/src/modules/alsa/mixer/profile-sets/native-instruments-korecontroller.conf b/src/modules/alsa/mixer/profile-sets/native-instruments-korecontroller.conf
new file mode 100644
index 00000000..904357d0
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/native-instruments-korecontroller.conf
@@ -0,0 +1,85 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Native Instruments Kore Controller
+;
+; This card has one stereo pairs of input and two stereo pairs of
+; output, named "Master" and "Headphone". The master channel has
+; an additional Coax S/PDIF connector which is always on.
+;
+; We knowingly only define a subset of the theoretically possible
+; mapping combinations as profiles here.
+;
+; See default.conf for an explanation on the directives used here.
+
+[General]
+auto-profiles = no
+
+[Mapping analog-stereo-master-out]
+description = Analog Stereo Master Channel
+device-strings = hw:%f,0,0
+channel-map = left,right
+
+[Mapping analog-stereo-headphone-out]
+description = Analog Stereo Headphone Channel
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-input]
+description = Analog Stereo
+device-strings = hw:%f,0,0
+channel-map = left,right
+direction = input
+
+[Profile output:analog-stereo-all+input:analog-stereo-all]
+description = Analog Stereo Duplex Master Output, Headphones Output
+output-mappings = analog-stereo-master-out analog-stereo-headphone-out
+input-mappings = analog-stereo-input
+priority = 100
+skip-probe = yes
+
+[Profile output:analog-stereo-master+input:analog-stereo-input]
+description = Analog Stereo Duplex Master Output
+output-mappings = analog-stereo-master-out
+input-mappings = analog-stereo-input
+priority = 40
+skip-probe = yes
+
+[Profile output:analog-stereo-headphone-out+input:analog-stereo-input]
+description = Analog Stereo Headphones Output
+output-mappings = analog-stereo-headphone-out
+input-mappings = analog-stereo-input
+priority = 30
+skip-probe = yes
+
+[Profile output:analog-stereo-master]
+description = Analog Stereo Master Output
+output-mappings = analog-stereo-master-out
+priority = 3
+skip-probe = yes
+
+[Profile output:analog-stereo-headphone]
+description = Analog Stereo Headphones Output
+output-mappings = analog-stereo-headphone-out
+priority = 2
+skip-probe = yes
+
+[Profile input:analog-stereo-input]
+description = Analog Stereo Input
+input-mappings = analog-stereo-input
+priority = 1
+skip-probe = yes
diff --git a/src/modules/alsa/mixer/profile-sets/native-instruments-traktor-audio10.conf b/src/modules/alsa/mixer/profile-sets/native-instruments-traktor-audio10.conf
new file mode 100644
index 00000000..4deb65da
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/native-instruments-traktor-audio10.conf
@@ -0,0 +1,131 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Native Instruments Audio 10 DJ
+;
+; This card has five stereo pairs of input and five stereo pairs of
+; output
+;
+; We knowingly only define a subset of the theoretically possible
+; mapping combinations as profiles here.
+;
+; See default.conf for an explanation on the directives used here.
+
+[General]
+auto-profiles = no
+
+[Mapping analog-stereo-out-main]
+description = Analog Stereo Main
+device-strings = hw:%f,0,0
+channel-map = left,right
+
+[Mapping analog-stereo-out-a]
+description = Analog Stereo Channel A
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-out-b]
+description = Analog Stereo Channel B
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-out-c]
+description = Analog Stereo Channel C
+device-strings = hw:%f,0,2
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-out-d]
+description = Analog Stereo Channel D
+device-strings = hw:%f,0,3
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-in-main]
+description = Analog Stereo Main
+device-strings = hw:%f,0,0
+channel-map = left,right
+
+[Mapping analog-stereo-in-a]
+description = Analog Stereo Channel A
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = input
+
+[Mapping analog-stereo-in-b]
+description = Analog Stereo Channel B
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = input
+
+[Mapping analog-stereo-in-c]
+description = Analog Stereo Channel C
+device-strings = hw:%f,0,2
+channel-map = left,right
+direction = input
+
+[Mapping analog-stereo-in-d]
+description = Analog Stereo Channel D
+device-strings = hw:%f,0,3
+channel-map = left,right
+direction = input
+
+
+
+
+[Profile output:analog-stereo-all+input:analog-stereo-all]
+description = Analog Stereo Duplex Channels Main, A, B, C, D
+output-mappings = analog-stereo-out-main analog-stereo-out-a analog-stereo-out-b analog-stereo-out-c analog-stereo-out-d
+input-mappings = analog-stereo-in-main analog-stereo-in-a analog-stereo-in-b analog-stereo-in-c analog-stereo-in-d
+priority = 100
+skip-probe = yes
+
+[Profile output:analog-stereo-main+input:analog-stereo-main]
+description = Analog Stereo Duplex Main
+output-mappings = analog-stereo-out-main
+input-mappings = analog-stereo-in-main
+priority = 50
+skip-probe = yes
+
+[Profile output:analog-stereo-a+input:analog-stereo-a]
+description = Analog Stereo Duplex Channel A
+output-mappings = analog-stereo-out-a
+input-mappings = analog-stereo-in-a
+priority = 40
+skip-probe = yes
+
+[Profile output:analog-stereo-b+input:analog-stereo-b]
+description = Analog Stereo Duplex Channel B
+output-mappings = analog-stereo-out-b
+input-mappings = analog-stereo-in-b
+priority = 30
+skip-probe = yes
+
+[Profile output:analog-stereo-a+input:analog-stereo-c]
+description = Analog Stereo Duplex Channel C
+output-mappings = analog-stereo-out-c
+input-mappings = analog-stereo-in-c
+priority = 20
+skip-probe = yes
+
+[Profile output:analog-stereo-a+input:analog-stereo-d]
+description = Analog Stereo Duplex Channel D
+output-mappings = analog-stereo-out-d
+input-mappings = analog-stereo-in-d
+priority = 10
+skip-probe = yes
diff --git a/src/modules/alsa/mixer/profile-sets/native-instruments-traktor-audio6.conf b/src/modules/alsa/mixer/profile-sets/native-instruments-traktor-audio6.conf
new file mode 100644
index 00000000..48d9058b
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/native-instruments-traktor-audio6.conf
@@ -0,0 +1,92 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Native Instruments Audio 6 DJ
+;
+; This card has three stereo pairs of input and three stereo pairs of
+; output
+;
+; We knowingly only define a subset of the theoretically possible
+; mapping combinations as profiles here.
+;
+; See default.conf for an explanation on the directives used here.
+
+[General]
+auto-profiles = no
+
+[Mapping analog-stereo-out-main]
+description = Analog Stereo Main
+device-strings = hw:%f,0,0
+channel-map = left,right
+
+[Mapping analog-stereo-out-a]
+description = Analog Stereo Channel A
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-out-b]
+description = Analog Stereo Channel B
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-in-main]
+description = Analog Stereo Main
+device-strings = hw:%f,0,0
+channel-map = left,right
+
+[Mapping analog-stereo-in-a]
+description = Analog Stereo Channel A
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = input
+
+[Mapping analog-stereo-in-b]
+description = Analog Stereo Channel B
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = input
+
+
+
+[Profile output:analog-stereo-all+input:analog-stereo-all]
+description = Analog Stereo Duplex Channels A, B (Headphones)
+output-mappings = analog-stereo-out-main analog-stereo-out-a analog-stereo-out-b
+input-mappings = analog-stereo-in-main analog-stereo-in-a analog-stereo-in-b
+priority = 100
+skip-probe = yes
+
+[Profile output:analog-stereo-main+input:analog-stereo-main]
+description = Analog Stereo Duplex Channel Main
+output-mappings = analog-stereo-out-main
+input-mappings = analog-stereo-in-main
+priority = 50
+skip-probe = yes
+
+[Profile output:analog-stereo-a+input:analog-stereo-a]
+description = Analog Stereo Duplex Channel A
+output-mappings = analog-stereo-out-a
+input-mappings = analog-stereo-in-a
+priority = 40
+skip-probe = yes
+
+[Profile output:analog-stereo-b+input:analog-stereo-b]
+description = Analog Stereo Duplex Channel B
+output-mappings = analog-stereo-out-b
+input-mappings = analog-stereo-in-b
+priority = 30
+skip-probe = yes
diff --git a/src/modules/alsa/mixer/profile-sets/native-instruments-traktorkontrol-s4.conf b/src/modules/alsa/mixer/profile-sets/native-instruments-traktorkontrol-s4.conf
new file mode 100644
index 00000000..1da843a1
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/native-instruments-traktorkontrol-s4.conf
@@ -0,0 +1,81 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; Native Instruments Traktor Kontrol S4
+;
+; This controller has two stereo pairs of input (named "Channel C" and
+; "Channel D") and two stereo pairs of output, one "Main Out" and
+; "Headphone Out".
+;
+; See default.conf for an explanation on the directives used here.
+
+[General]
+auto-profiles = no
+
+[Mapping analog-stereo-output-main]
+description = Analog Stereo Main Out
+device-strings = hw:%f,0,0
+channel-map = left,right
+
+[Mapping analog-stereo-output-headphone]
+description = Analog Stereo Headphones Out
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = output
+
+[Mapping analog-stereo-c-input]
+description = Analog Stereo Channel C
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = input
+
+[Mapping analog-stereo-d-input]
+description = Analog Stereo Channel D
+device-strings = hw:%f,0,1
+channel-map = left,right
+direction = input
+
+[Profile output:analog-stereo-all+input:analog-stereo-all]
+description = Analog Stereo Duplex
+output-mappings = analog-stereo-output-main analog-stereo-output-headphone
+input-mappings = analog-stereo-c-input analog-stereo-d-input
+priority = 100
+skip-probe = yes
+
+[Profile output:analog-stereo-main]
+description = Analog Stereo Main Output
+output-mappings = analog-stereo-output-main
+priority = 4
+skip-probe = yes
+
+[Profile output:analog-stereo-headphone]
+description = Analog Stereo Output Headphones Out
+output-mappings = analog-stereo-output-headphone
+priority = 3
+skip-probe = yes
+
+[Profile input:analog-stereo-c]
+description = Analog Stereo Input Channel C
+input-mappings = analog-stereo-c-input
+priority = 2
+skip-probe = yes
+
+[Profile input:analog-stereo-d]
+description = Analog Stereo Input Channel D
+input-mappings = analog-stereo-d-input
+priority = 1
+skip-probe = yes
+
diff --git a/src/modules/alsa/mixer/profile-sets/usb-headset.conf b/src/modules/alsa/mixer/profile-sets/usb-headset.conf
new file mode 100644
index 00000000..adf78d17
--- /dev/null
+++ b/src/modules/alsa/mixer/profile-sets/usb-headset.conf
@@ -0,0 +1,35 @@
+# This file is part of PulseAudio.
+#
+# PulseAudio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of the
+# License, or (at your option) any later version.
+#
+# PulseAudio is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with PulseAudio; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+; This is a workaround - these usb headsets have one output volume control only, labeled "Speaker".
+; This causes the default profile set to not control the volume at all, which is a bug.
+
+[General]
+auto-profiles = yes
+
+[Mapping analog-mono]
+device-strings = hw:%f
+channel-map = mono
+paths-output = analog-output-speaker
+paths-input = analog-input analog-input-mic analog-input-linein analog-input-aux analog-input-video analog-input-tvtuner analog-input-fm analog-input-mic-line
+priority = 1
+
+[Mapping analog-stereo]
+device-strings = front:%f hw:%f
+channel-map = left,right
+paths-output = analog-output-speaker
+paths-input = analog-input analog-input-mic analog-input-linein analog-input-aux analog-input-video analog-input-tvtuner analog-input-fm analog-input-mic-line
+priority = 10
diff --git a/src/modules/alsa/mixer/samples/ATI IXP--Realtek ALC655 rev 0 b/src/modules/alsa/mixer/samples/ATI IXP--Realtek ALC655 rev 0
new file mode 100644
index 00000000..082c9a1b
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/ATI IXP--Realtek ALC655 rev 0
@@ -0,0 +1,150 @@
+Simple mixer control 'Master',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 29 [94%] [-3.00dB] [on]
+ Front Right: Playback 29 [94%] [-3.00dB] [on]
+Simple mixer control 'Master Mono',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'PCM',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 23 [74%] [0.00dB] [on]
+ Front Right: Playback 23 [74%] [0.00dB] [on]
+Simple mixer control 'Surround',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 0 [0%] [-46.50dB] [off]
+ Front Right: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'Surround Jack Mode',0
+ Capabilities: enum
+ Items: 'Shared' 'Independent'
+ Item0: 'Shared'
+Simple mixer control 'Center',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'LFE',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'Line',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'CD',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Mic',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Mono
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-34.50dB] [off]
+ Front Left: Capture [on]
+ Front Right: Capture [on]
+Simple mixer control 'Mic Boost (+20dB)',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'Mic Select',0
+ Capabilities: enum
+ Items: 'Mic1' 'Mic2'
+ Item0: 'Mic1'
+Simple mixer control 'Video',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Phone',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Mono
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono: Playback 31 [100%] [12.00dB] [off]
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'IEC958',0
+ Capabilities: pswitch pswitch-joined cswitch cswitch-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Mono: Playback [off] Capture [off]
+Simple mixer control 'IEC958 Playback AC97-SPSA',0
+ Capabilities: volume volume-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: 0 - 3
+ Mono: 0 [0%]
+Simple mixer control 'IEC958 Playback Source',0
+ Capabilities: enum
+ Items: 'PCM' 'Analog In' 'IEC958 In'
+ Item0: 'PCM'
+Simple mixer control 'PC Speaker',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 15
+ Mono: Playback 0 [0%] [-45.00dB] [on]
+Simple mixer control 'Aux',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [on] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [on] Capture [off]
+Simple mixer control 'Mono Output Select',0
+ Capabilities: enum
+ Items: 'Mix' 'Mic'
+ Item0: 'Mix'
+Simple mixer control 'Capture',0
+ Capabilities: cvolume cswitch cswitch-joined
+ Capture channels: Front Left - Front Right
+ Limits: Capture 0 - 15
+ Front Left: Capture 12 [80%] [18.00dB] [on]
+ Front Right: Capture 12 [80%] [18.00dB] [on]
+Simple mixer control 'Mix',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Mix Mono',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Channel Mode',0
+ Capabilities: enum
+ Items: '2ch' '4ch' '6ch'
+ Item0: '2ch'
+Simple mixer control 'Duplicate Front',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'External Amplifier',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [on]
diff --git a/src/modules/alsa/mixer/samples/Brooktree Bt878--Bt87x b/src/modules/alsa/mixer/samples/Brooktree Bt878--Bt87x
new file mode 100644
index 00000000..b8f61fab
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/Brooktree Bt878--Bt87x
@@ -0,0 +1,24 @@
+Simple mixer control 'FM',0
+ Capabilities: cswitch cswitch-joined cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Mono
+ Mono: Capture [off]
+Simple mixer control 'Mic/Line',0
+ Capabilities: cswitch cswitch-joined cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Mono
+ Mono: Capture [off]
+Simple mixer control 'Capture',0
+ Capabilities: cvolume cvolume-joined
+ Capture channels: Mono
+ Limits: Capture 0 - 15
+ Mono: Capture 13 [87%]
+Simple mixer control 'Capture Boost',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [on]
+Simple mixer control 'TV Tuner',0
+ Capabilities: cswitch cswitch-joined cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Mono
+ Mono: Capture [on]
diff --git a/src/modules/alsa/mixer/samples/Ensoniq AudioPCI--Cirrus Logic CS4297A rev 3 b/src/modules/alsa/mixer/samples/Ensoniq AudioPCI--Cirrus Logic CS4297A rev 3
new file mode 100644
index 00000000..a500a817
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/Ensoniq AudioPCI--Cirrus Logic CS4297A rev 3
@@ -0,0 +1,135 @@
+Simple mixer control 'Master',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 63
+ Mono:
+ Front Left: Playback 63 [100%] [0.00dB] [on]
+ Front Right: Playback 63 [100%] [0.00dB] [on]
+Simple mixer control 'Master Mono',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'Headphone',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 0 [0%] [-46.50dB] [off]
+ Front Right: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control '3D Control - Center',0
+ Capabilities: volume volume-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: 0 - 15
+ Mono: 0 [0%]
+Simple mixer control '3D Control - Depth',0
+ Capabilities: volume volume-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: 0 - 15
+ Mono: 0 [0%]
+Simple mixer control '3D Control - Switch',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'PCM',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 23 [74%] [0.00dB] [on]
+ Front Right: Playback 23 [74%] [0.00dB] [on]
+Simple mixer control 'Line',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [on]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [on]
+Simple mixer control 'CD',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Mic',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Mono
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono: Playback 23 [74%] [0.00dB] [on]
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Mic Boost (+20dB)',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'Mic Select',0
+ Capabilities: enum
+ Items: 'Mic1' 'Mic2'
+ Item0: 'Mic1'
+Simple mixer control 'Video',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Phone',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Mono
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-34.50dB] [off]
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'IEC958',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'PC Speaker',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 15
+ Mono: Playback 0 [0%] [-45.00dB] [off]
+Simple mixer control 'Aux',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Mono Output Select',0
+ Capabilities: enum
+ Items: 'Mix' 'Mic'
+ Item0: 'Mic'
+Simple mixer control 'Capture',0
+ Capabilities: cvolume cswitch cswitch-joined
+ Capture channels: Front Left - Front Right
+ Limits: Capture 0 - 15
+ Front Left: Capture 15 [100%] [22.50dB] [on]
+ Front Right: Capture 15 [100%] [22.50dB] [on]
+Simple mixer control 'Mix',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Mix Mono',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'External Amplifier',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
diff --git a/src/modules/alsa/mixer/samples/HDA ATI HDMI--ATI R6xx HDMI b/src/modules/alsa/mixer/samples/HDA ATI HDMI--ATI R6xx HDMI
new file mode 100644
index 00000000..244f24a8
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/HDA ATI HDMI--ATI R6xx HDMI
@@ -0,0 +1,4 @@
+Simple mixer control 'IEC958',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [on]
diff --git a/src/modules/alsa/mixer/samples/HDA Intel--Analog Devices AD1981 b/src/modules/alsa/mixer/samples/HDA Intel--Analog Devices AD1981
new file mode 100644
index 00000000..165522fa
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/HDA Intel--Analog Devices AD1981
@@ -0,0 +1,62 @@
+Simple mixer control 'Master',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 63
+ Mono:
+ Front Left: Playback 63 [100%] [3.00dB] [on]
+ Front Right: Playback 63 [100%] [3.00dB] [on]
+Simple mixer control 'PCM',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 23 [74%] [0.00dB] [on]
+ Front Right: Playback 23 [74%] [0.00dB] [on]
+Simple mixer control 'CD',0
+ Capabilities: pvolume pswitch cswitch cswitch-joined cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Capture [off]
+ Front Left: Playback 0 [0%] [-34.50dB] [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off]
+Simple mixer control 'Mic',0
+ Capabilities: pvolume pswitch cswitch cswitch-joined cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Capture [on]
+ Front Left: Playback 0 [0%] [-34.50dB] [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off]
+Simple mixer control 'Mic Boost',0
+ Capabilities: volume
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: 0 - 3
+ Front Left: 0 [0%]
+ Front Right: 0 [0%]
+Simple mixer control 'IEC958',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'IEC958 Default PCM',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'IEC958 Playback Source',0
+ Capabilities: enum
+ Items: 'PCM' 'ADC'
+ Item0: 'PCM'
+Simple mixer control 'Capture',0
+ Capabilities: cvolume cswitch
+ Capture channels: Front Left - Front Right
+ Limits: Capture 0 - 15
+ Front Left: Capture 0 [0%] [0.00dB] [on]
+ Front Right: Capture 0 [0%] [0.00dB] [on]
+Simple mixer control 'Mix',0
+ Capabilities: cswitch cswitch-joined cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Mono
+ Mono: Capture [off]
diff --git a/src/modules/alsa/mixer/samples/HDA Intel--Realtek ALC889A b/src/modules/alsa/mixer/samples/HDA Intel--Realtek ALC889A
new file mode 100644
index 00000000..28a2e73c
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/HDA Intel--Realtek ALC889A
@@ -0,0 +1,113 @@
+Simple mixer control 'Master',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 64
+ Mono: Playback 64 [100%] [0.00dB] [on]
+Simple mixer control 'Headphone',0
+ Capabilities: pswitch
+ Playback channels: Front Left - Front Right
+ Mono:
+ Front Left: Playback [on]
+ Front Right: Playback [on]
+Simple mixer control 'PCM',0
+ Capabilities: pvolume
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 255
+ Mono:
+ Front Left: Playback 255 [100%] [0.00dB]
+ Front Right: Playback 255 [100%] [0.00dB]
+Simple mixer control 'Front',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 64
+ Mono:
+ Front Left: Playback 44 [69%] [-20.00dB] [on]
+ Front Right: Playback 44 [69%] [-20.00dB] [on]
+Simple mixer control 'Front Mic',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 0 [0%] [-34.50dB] [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off]
+Simple mixer control 'Front Mic Boost',0
+ Capabilities: volume
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: 0 - 3
+ Front Left: 0 [0%]
+ Front Right: 0 [0%]
+Simple mixer control 'Surround',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 64
+ Mono:
+ Front Left: Playback 0 [0%] [-64.00dB] [on]
+ Front Right: Playback 0 [0%] [-64.00dB] [on]
+Simple mixer control 'Center',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 64
+ Mono: Playback 0 [0%] [-64.00dB] [on]
+Simple mixer control 'LFE',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 64
+ Mono: Playback 0 [0%] [-64.00dB] [on]
+Simple mixer control 'Side',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 64
+ Mono:
+ Front Left: Playback 0 [0%] [-64.00dB] [on]
+ Front Right: Playback 0 [0%] [-64.00dB] [on]
+Simple mixer control 'Line',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 0 [0%] [-34.50dB] [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off]
+Simple mixer control 'Mic',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 0 [0%] [-34.50dB] [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off]
+Simple mixer control 'Mic Boost',0
+ Capabilities: volume
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: 0 - 3
+ Front Left: 0 [0%]
+ Front Right: 0 [0%]
+Simple mixer control 'IEC958',0
+ Capabilities: pswitch pswitch-joined cswitch cswitch-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Mono: Playback [on] Capture [on]
+Simple mixer control 'IEC958 Default PCM',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [on]
+Simple mixer control 'Capture',0
+ Capabilities: cvolume cswitch
+ Capture channels: Front Left - Front Right
+ Limits: Capture 0 - 46
+ Front Left: Capture 23 [50%] [7.00dB] [on]
+ Front Right: Capture 23 [50%] [7.00dB] [on]
+Simple mixer control 'Capture',1
+ Capabilities: cvolume cswitch
+ Capture channels: Front Left - Front Right
+ Limits: Capture 0 - 46
+ Front Left: Capture 0 [0%] [-16.00dB] [off]
+ Front Right: Capture 0 [0%] [-16.00dB] [off]
+Simple mixer control 'Input Source',0
+ Capabilities: cenum
+ Items: 'Mic' 'Front Mic' 'Line'
+ Item0: 'Mic'
+Simple mixer control 'Input Source',1
+ Capabilities: cenum
+ Items: 'Mic' 'Front Mic' 'Line'
+ Item0: 'Mic'
diff --git a/src/modules/alsa/mixer/samples/Intel 82801CA-ICH3--Analog Devices AD1881A b/src/modules/alsa/mixer/samples/Intel 82801CA-ICH3--Analog Devices AD1881A
new file mode 100644
index 00000000..3ddd8af6
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/Intel 82801CA-ICH3--Analog Devices AD1881A
@@ -0,0 +1,128 @@
+Simple mixer control 'Master',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 63
+ Mono:
+ Front Left: Playback 44 [70%] [-28.50dB] [on]
+ Front Right: Playback 60 [95%] [-4.50dB] [on]
+Simple mixer control 'Master Mono',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 17 [55%] [-21.00dB] [on]
+Simple mixer control '3D Control - Center',0
+ Capabilities: volume volume-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: 0 - 15
+ Mono: 0 [0%]
+Simple mixer control '3D Control - Depth',0
+ Capabilities: volume volume-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: 0 - 15
+ Mono: 0 [0%]
+Simple mixer control '3D Control - Switch',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'PCM',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 9 [29%] [-21.00dB] [on]
+ Front Right: Playback 9 [29%] [-21.00dB] [on]
+Simple mixer control 'PCM Out Path & Mute',0
+ Capabilities: enum
+ Items: 'pre 3D' 'post 3D'
+ Item0: 'pre 3D'
+Simple mixer control 'Line',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'CD',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 9 [29%] [-21.00dB] [on] Capture [off]
+ Front Right: Playback 9 [29%] [-21.00dB] [on] Capture [off]
+Simple mixer control 'Mic',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Mono
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-34.50dB] [off]
+ Front Left: Capture [on]
+ Front Right: Capture [on]
+Simple mixer control 'Mic Boost (+20dB)',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'Mic Select',0
+ Capabilities: enum
+ Items: 'Mic1' 'Mic2'
+ Item0: 'Mic1'
+Simple mixer control 'Video',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Phone',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Mono
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-34.50dB] [off]
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'PC Speaker',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 15
+ Mono: Playback 8 [53%] [-21.00dB] [on]
+Simple mixer control 'Aux',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Mono Output Select',0
+ Capabilities: enum
+ Items: 'Mix' 'Mic'
+ Item0: 'Mix'
+Simple mixer control 'Capture',0
+ Capabilities: cvolume cswitch cswitch-joined
+ Capture channels: Front Left - Front Right
+ Limits: Capture 0 - 15
+ Front Left: Capture 13 [87%] [19.50dB] [on]
+ Front Right: Capture 13 [87%] [19.50dB] [on]
+Simple mixer control 'Mix',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Mix Mono',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'External Amplifier',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [on]
diff --git a/src/modules/alsa/mixer/samples/Logitech USB Speaker--USB Mixer b/src/modules/alsa/mixer/samples/Logitech USB Speaker--USB Mixer
new file mode 100644
index 00000000..38cf6778
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/Logitech USB Speaker--USB Mixer
@@ -0,0 +1,27 @@
+Simple mixer control 'Bass',0
+ Capabilities: volume volume-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: 0 - 48
+ Mono: 22 [46%]
+Simple mixer control 'Bass Boost',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'Treble',0
+ Capabilities: volume volume-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: 0 - 48
+ Mono: 25 [52%]
+Simple mixer control 'PCM',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 44
+ Mono:
+ Front Left: Playback 10 [23%] [-31.00dB] [on]
+ Front Right: Playback 10 [23%] [-31.00dB] [on]
+Simple mixer control 'Auto Gain Control',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
diff --git a/src/modules/alsa/mixer/samples/USB Audio--USB Mixer b/src/modules/alsa/mixer/samples/USB Audio--USB Mixer
new file mode 100644
index 00000000..9cb4fa7f
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/USB Audio--USB Mixer
@@ -0,0 +1,37 @@
+Simple mixer control 'Master',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 255
+ Mono: Playback 105 [41%] [-28.97dB] [on]
+Simple mixer control 'Line',0
+ Capabilities: pvolume cvolume pswitch pswitch-joined cswitch cswitch-joined
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 255 Capture 0 - 128
+ Front Left: Playback 191 [75%] [34.38dB] [off] Capture 0 [0%] [0.18dB] [off]
+ Front Right: Playback 191 [75%] [34.38dB] [off] Capture 0 [0%] [0.18dB] [off]
+Simple mixer control 'Mic',0
+ Capabilities: pvolume pvolume-joined cvolume cvolume-joined pswitch pswitch-joined cswitch cswitch-joined cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: Playback 0 - 255 Capture 0 - 128
+ Mono: Playback 191 [75%] [34.38dB] [off] Capture 0 [0%] [0.18dB] [on]
+Simple mixer control 'Mic Capture',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'IEC958 In',0
+ Capabilities: cswitch cswitch-joined
+ Capture channels: Mono
+ Mono: Capture [off]
+Simple mixer control 'Input 1',0
+ Capabilities: cswitch cswitch-joined cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Mono
+ Mono: Capture [off]
+Simple mixer control 'Input 2',0
+ Capabilities: cswitch cswitch-joined cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Mono
+ Mono: Capture [off]
diff --git a/src/modules/alsa/mixer/samples/USB Device 0x46d:0x9a4--USB Mixer b/src/modules/alsa/mixer/samples/USB Device 0x46d:0x9a4--USB Mixer
new file mode 100644
index 00000000..783f826f
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/USB Device 0x46d:0x9a4--USB Mixer
@@ -0,0 +1,5 @@
+Simple mixer control 'Mic',0
+ Capabilities: cvolume cvolume-joined cswitch cswitch-joined
+ Capture channels: Mono
+ Limits: Capture 0 - 3072
+ Mono: Capture 1536 [50%] [23.00dB] [on]
diff --git a/src/modules/alsa/mixer/samples/VIA 8237--Analog Devices AD1888 b/src/modules/alsa/mixer/samples/VIA 8237--Analog Devices AD1888
new file mode 100644
index 00000000..15e7b5a6
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/VIA 8237--Analog Devices AD1888
@@ -0,0 +1,211 @@
+Simple mixer control 'Master',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 31 [100%] [0.00dB] [on]
+ Front Right: Playback 31 [100%] [0.00dB] [on]
+Simple mixer control 'Master Mono',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'Master Surround',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 0 [0%] [-46.50dB] [off]
+ Front Right: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'Headphone Jack Sense',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'PCM',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 23 [74%] [0.00dB] [on]
+ Front Right: Playback 23 [74%] [0.00dB] [on]
+Simple mixer control 'Surround',0
+ Capabilities: pvolume pswitch
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 0 [0%] [-46.50dB] [off]
+ Front Right: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'Surround Jack Mode',0
+ Capabilities: enum
+ Items: 'Shared' 'Independent'
+ Item0: 'Shared'
+Simple mixer control 'Center',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 31 [100%] [0.00dB] [off]
+Simple mixer control 'LFE',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'Line',0
+ Capabilities: pvolume pswitch cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Line Jack Sense',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'CD',0
+ Capabilities: pvolume pswitch cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Mic',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Mono
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-34.50dB] [off]
+ Front Left: Capture [on]
+ Front Right: Capture [on]
+Simple mixer control 'Mic Boost (+20dB)',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'Mic Select',0
+ Capabilities: enum
+ Items: 'Mic1' 'Mic2'
+ Item0: 'Mic1'
+Simple mixer control 'Video',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Phone',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Mono
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-34.50dB] [off]
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'IEC958',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'IEC958 Output',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'IEC958 Playback AC97-SPSA',0
+ Capabilities: volume volume-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: 0 - 3
+ Mono: 3 [100%]
+Simple mixer control 'IEC958 Playback Source',0
+ Capabilities: enum
+ Items: 'AC-Link' 'A/D Converter'
+ Item0: 'AC-Link'
+Simple mixer control 'Aux',0
+ Capabilities: pvolume pswitch cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Capture',0
+ Capabilities: cvolume cswitch
+ Capture channels: Front Left - Front Right
+ Limits: Capture 0 - 15
+ Front Left: Capture 0 [0%] [0.00dB] [on]
+ Front Right: Capture 0 [0%] [0.00dB] [on]
+Simple mixer control 'Mix',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Mix Mono',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Channel Mode',0
+ Capabilities: enum
+ Items: '2ch' '4ch' '6ch'
+ Item0: '2ch'
+Simple mixer control 'Downmix',0
+ Capabilities: enum
+ Items: 'Off' '6 -> 4' '6 -> 2'
+ Item0: 'Off'
+Simple mixer control 'Exchange Front/Surround',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'External Amplifier',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [on]
+Simple mixer control 'High Pass Filter Enable',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'Input Source Select',0
+ Capabilities: enum
+ Items: 'Input1' 'Input2'
+ Item0: 'Input1'
+Simple mixer control 'Input Source Select',1
+ Capabilities: enum
+ Items: 'Input1' 'Input2'
+ Item0: 'Input1'
+Simple mixer control 'Spread Front to Surround and Center/LFE',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'VIA DXS',0
+ Capabilities: pvolume
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 31 [100%] [-48.00dB]
+ Front Right: Playback 31 [100%] [-48.00dB]
+Simple mixer control 'VIA DXS',1
+ Capabilities: pvolume
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 31 [100%] [-48.00dB]
+ Front Right: Playback 31 [100%] [-48.00dB]
+Simple mixer control 'VIA DXS',2
+ Capabilities: pvolume
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 31 [100%] [-48.00dB]
+ Front Right: Playback 31 [100%] [-48.00dB]
+Simple mixer control 'VIA DXS',3
+ Capabilities: pvolume
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 31 [100%] [-48.00dB]
+ Front Right: Playback 31 [100%] [-48.00dB]
+Simple mixer control 'V_REFOUT Enable',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [on]
diff --git a/src/modules/alsa/mixer/samples/VIA 8237--C-Media Electronics CMI9761A+ b/src/modules/alsa/mixer/samples/VIA 8237--C-Media Electronics CMI9761A+
new file mode 100644
index 00000000..d4f3db62
--- /dev/null
+++ b/src/modules/alsa/mixer/samples/VIA 8237--C-Media Electronics CMI9761A+
@@ -0,0 +1,160 @@
+Simple mixer control 'Master',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 0 [0%] [-46.50dB] [off]
+ Front Right: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'PCM',0
+ Capabilities: pvolume pswitch pswitch-joined
+ Playback channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Mono:
+ Front Left: Playback 31 [100%] [-48.00dB] [off]
+ Front Right: Playback 31 [100%] [-48.00dB] [off]
+Simple mixer control 'Surround',0
+ Capabilities: pswitch
+ Playback channels: Front Left - Front Right
+ Mono:
+ Front Left: Playback [off]
+ Front Right: Playback [off]
+Simple mixer control 'Surround Jack Mode',0
+ Capabilities: enum
+ Items: 'Shared' 'Independent'
+ Item0: 'Shared'
+Simple mixer control 'Center',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 31 [100%] [0.00dB] [off]
+Simple mixer control 'LFE',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 31
+ Mono: Playback 0 [0%] [-46.50dB] [off]
+Simple mixer control 'Line',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'CD',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Mic',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [on]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [on]
+Simple mixer control 'Mic Boost (+20dB)',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'Mic Select',0
+ Capabilities: enum
+ Items: 'Mic1' 'Mic2'
+ Item0: 'Mic1'
+Simple mixer control 'Video',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Phone',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'IEC958',0
+ Capabilities: pswitch pswitch-joined cswitch cswitch-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Mono: Playback [off] Capture [off]
+Simple mixer control 'IEC958 Capture Monitor',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'IEC958 Capture Valid',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'IEC958 Output',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [off]
+Simple mixer control 'IEC958 Playback AC97-SPSA',0
+ Capabilities: volume volume-joined
+ Playback channels: Mono
+ Capture channels: Mono
+ Limits: 0 - 3
+ Mono: 3 [100%]
+Simple mixer control 'IEC958 Playback Source',0
+ Capabilities: enum
+ Items: 'AC-Link' 'ADC' 'SPDIF-In'
+ Item0: 'AC-Link'
+Simple mixer control 'PC Speaker',0
+ Capabilities: pvolume pvolume-joined pswitch pswitch-joined
+ Playback channels: Mono
+ Limits: Playback 0 - 15
+ Mono: Playback 0 [0%] [-45.00dB] [off]
+Simple mixer control 'Aux',0
+ Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Playback channels: Front Left - Front Right
+ Capture channels: Front Left - Front Right
+ Limits: Playback 0 - 31
+ Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+ Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off]
+Simple mixer control 'Mono Output Select',0
+ Capabilities: enum
+ Items: 'Mix' 'Mic'
+ Item0: 'Mix'
+Simple mixer control 'Capture',0
+ Capabilities: cvolume cswitch cswitch-joined
+ Capture channels: Front Left - Front Right
+ Limits: Capture 0 - 15
+ Front Left: Capture 0 [0%] [0.00dB] [on]
+ Front Right: Capture 0 [0%] [0.00dB] [on]
+Simple mixer control 'Mix',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Mix Mono',0
+ Capabilities: cswitch cswitch-exclusive
+ Capture exclusive group: 0
+ Capture channels: Front Left - Front Right
+ Front Left: Capture [off]
+ Front Right: Capture [off]
+Simple mixer control 'Channel Mode',0
+ Capabilities: enum
+ Items: '2ch' '4ch' '6ch'
+ Item0: '2ch'
+Simple mixer control 'DAC Clock Source',0
+ Capabilities: enum
+ Items: 'AC-Link' 'SPDIF-In' 'Both'
+ Item0: 'AC-Link'
+Simple mixer control 'External Amplifier',0
+ Capabilities: pswitch pswitch-joined
+ Playback channels: Mono
+ Mono: Playback [on]
+Simple mixer control 'Input Source Select',0
+ Capabilities: enum
+ Items: 'Input1' 'Input2'
+ Item0: 'Input1'
+Simple mixer control 'Input Source Select',1
+ Capabilities: enum
+ Items: 'Input1' 'Input2'
+ Item0: 'Input1'
diff --git a/src/modules/alsa/module-alsa-card.c b/src/modules/alsa/module-alsa-card.c
new file mode 100644
index 00000000..e60aa5ef
--- /dev/null
+++ b/src/modules/alsa/module-alsa-card.c
@@ -0,0 +1,479 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/queue.h>
+
+#include <modules/reserve-wrap.h>
+
+#ifdef HAVE_UDEV
+#include <modules/udev-util.h>
+#endif
+
+#include "alsa-util.h"
+#include "alsa-sink.h"
+#include "alsa-source.h"
+#include "module-alsa-card-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("ALSA Card");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "name=<name for the card/sink/source, to be prefixed> "
+ "card_name=<name for the card> "
+ "card_properties=<properties for the card> "
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "source_name=<name for the source> "
+ "source_properties=<properties for the source> "
+ "namereg_fail=<pa_namereg_register() fail parameter value> "
+ "device_id=<ALSA card index> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "fragments=<number of fragments> "
+ "fragment_size=<fragment size> "
+ "mmap=<enable memory mapping?> "
+ "tsched=<enable system timer based scheduling mode?> "
+ "tsched_buffer_size=<buffer size when using timer based scheduling> "
+ "tsched_buffer_watermark=<lower fill watermark> "
+ "profile=<profile name> "
+ "ignore_dB=<ignore dB information from the device?> "
+ "sync_volume=<syncronize sw and hw voluchanges in IO-thread?> "
+ "profile_set=<profile set configuration file> ");
+
+static const char* const valid_modargs[] = {
+ "name",
+ "card_name",
+ "card_properties",
+ "sink_name",
+ "sink_properties",
+ "source_name",
+ "source_properties",
+ "namereg_fail",
+ "device_id",
+ "format",
+ "rate",
+ "fragments",
+ "fragment_size",
+ "mmap",
+ "tsched",
+ "tsched_buffer_size",
+ "tsched_buffer_watermark",
+ "profile",
+ "ignore_dB",
+ "sync_volume",
+ "profile_set",
+ NULL
+};
+
+#define DEFAULT_DEVICE_ID "0"
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+
+ char *device_id;
+
+ pa_card *card;
+
+ pa_modargs *modargs;
+
+ pa_alsa_profile_set *profile_set;
+};
+
+struct profile_data {
+ pa_alsa_profile *profile;
+};
+
+static void add_profiles(struct userdata *u, pa_hashmap *h) {
+ pa_alsa_profile *ap;
+ void *state;
+
+ pa_assert(u);
+ pa_assert(h);
+
+ PA_HASHMAP_FOREACH(ap, u->profile_set->profiles, state) {
+ struct profile_data *d;
+ pa_card_profile *cp;
+ pa_alsa_mapping *m;
+ uint32_t idx;
+
+ cp = pa_card_profile_new(ap->name, ap->description, sizeof(struct profile_data));
+ cp->priority = ap->priority;
+
+ if (ap->output_mappings) {
+ cp->n_sinks = pa_idxset_size(ap->output_mappings);
+
+ PA_IDXSET_FOREACH(m, ap->output_mappings, idx)
+ if (m->channel_map.channels > cp->max_sink_channels)
+ cp->max_sink_channels = m->channel_map.channels;
+ }
+
+ if (ap->input_mappings) {
+ cp->n_sources = pa_idxset_size(ap->input_mappings);
+
+ PA_IDXSET_FOREACH(m, ap->input_mappings, idx)
+ if (m->channel_map.channels > cp->max_source_channels)
+ cp->max_source_channels = m->channel_map.channels;
+ }
+
+ d = PA_CARD_PROFILE_DATA(cp);
+ d->profile = ap;
+
+ pa_hashmap_put(h, cp->name, cp);
+ }
+}
+
+static void add_disabled_profile(pa_hashmap *profiles) {
+ pa_card_profile *p;
+ struct profile_data *d;
+
+ p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
+
+ d = PA_CARD_PROFILE_DATA(p);
+ d->profile = NULL;
+
+ pa_hashmap_put(profiles, p->name, p);
+}
+
+static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
+ struct userdata *u;
+ struct profile_data *nd, *od;
+ uint32_t idx;
+ pa_alsa_mapping *am;
+ pa_queue *sink_inputs = NULL, *source_outputs = NULL;
+
+ pa_assert(c);
+ pa_assert(new_profile);
+ pa_assert_se(u = c->userdata);
+
+ nd = PA_CARD_PROFILE_DATA(new_profile);
+ od = PA_CARD_PROFILE_DATA(c->active_profile);
+
+ if (od->profile && od->profile->output_mappings)
+ PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) {
+ if (!am->sink)
+ continue;
+
+ if (nd->profile &&
+ nd->profile->output_mappings &&
+ pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL))
+ continue;
+
+ sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs);
+ pa_alsa_sink_free(am->sink);
+ am->sink = NULL;
+ }
+
+ if (od->profile && od->profile->input_mappings)
+ PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) {
+ if (!am->source)
+ continue;
+
+ if (nd->profile &&
+ nd->profile->input_mappings &&
+ pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL))
+ continue;
+
+ source_outputs = pa_source_move_all_start(am->source, source_outputs);
+ pa_alsa_source_free(am->source);
+ am->source = NULL;
+ }
+
+ if (nd->profile && nd->profile->output_mappings)
+ PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) {
+
+ if (!am->sink)
+ am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am);
+
+ if (sink_inputs && am->sink) {
+ pa_sink_move_all_finish(am->sink, sink_inputs, FALSE);
+ sink_inputs = NULL;
+ }
+ }
+
+ if (nd->profile && nd->profile->input_mappings)
+ PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) {
+
+ if (!am->source)
+ am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am);
+
+ if (source_outputs && am->source) {
+ pa_source_move_all_finish(am->source, source_outputs, FALSE);
+ source_outputs = NULL;
+ }
+ }
+
+ if (sink_inputs)
+ pa_sink_move_all_fail(sink_inputs);
+
+ if (source_outputs)
+ pa_source_move_all_fail(source_outputs);
+
+ return 0;
+}
+
+static void init_profile(struct userdata *u) {
+ uint32_t idx;
+ pa_alsa_mapping *am;
+ struct profile_data *d;
+
+ pa_assert(u);
+
+ d = PA_CARD_PROFILE_DATA(u->card->active_profile);
+
+ if (d->profile && d->profile->output_mappings)
+ PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx)
+ am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am);
+
+ if (d->profile && d->profile->input_mappings)
+ PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx)
+ am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am);
+}
+
+static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
+ char *t;
+ const char *n;
+
+ pa_assert(data);
+ pa_assert(ma);
+ pa_assert(device_id);
+
+ if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
+ pa_card_new_data_set_name(data, n);
+ data->namereg_fail = TRUE;
+ return;
+ }
+
+ if ((n = pa_modargs_get_value(ma, "name", NULL)))
+ data->namereg_fail = TRUE;
+ else {
+ n = device_id;
+ data->namereg_fail = FALSE;
+ }
+
+ t = pa_sprintf_malloc("alsa_card.%s", n);
+ pa_card_new_data_set_name(data, t);
+ pa_xfree(t);
+}
+
+int pa__init(pa_module *m) {
+ pa_card_new_data data;
+ pa_modargs *ma;
+ int alsa_card_index;
+ struct userdata *u;
+ pa_reserve_wrapper *reserve = NULL;
+ const char *description;
+ char *fn = NULL;
+ pa_bool_t namereg_fail = FALSE;
+
+ pa_alsa_refcnt_inc();
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
+ u->modargs = ma;
+
+ if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
+ pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(alsa_card_index));
+ goto fail;
+ }
+
+ if (!pa_in_system_mode()) {
+ char *rname;
+
+ if ((rname = pa_alsa_get_reserve_name(u->device_id))) {
+ reserve = pa_reserve_wrapper_get(m->core, rname);
+ pa_xfree(rname);
+
+ if (!reserve)
+ goto fail;
+ }
+ }
+
+#ifdef HAVE_UDEV
+ fn = pa_udev_get_property(alsa_card_index, "PULSE_PROFILE_SET");
+#endif
+
+ if (pa_modargs_get_value(ma, "profile_set", NULL)) {
+ pa_xfree(fn);
+ fn = pa_xstrdup(pa_modargs_get_value(ma, "profile_set", NULL));
+ }
+
+ u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map);
+ pa_xfree(fn);
+
+ if (!u->profile_set)
+ goto fail;
+
+ pa_alsa_profile_set_probe(u->profile_set, u->device_id, &m->core->default_sample_spec, m->core->default_n_fragments, m->core->default_fragment_size_msec);
+ pa_alsa_profile_set_dump(u->profile_set);
+
+ pa_card_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+
+ pa_alsa_init_proplist_card(m->core, data.proplist, alsa_card_index);
+
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
+ pa_alsa_init_description(data.proplist);
+ set_card_name(&data, ma, u->device_id);
+
+ /* We need to give pa_modargs_get_value_boolean() a pointer to a local
+ * variable instead of using &data.namereg_fail directly, because
+ * data.namereg_fail is a bitfield and taking the address of a bitfield
+ * variable is impossible. */
+ namereg_fail = data.namereg_fail;
+ if (pa_modargs_get_value_boolean(ma, "namereg_fail", &namereg_fail) < 0) {
+ pa_log("Failed to parse boolean argument namereg_fail.");
+ pa_card_new_data_done(&data);
+ goto fail;
+ }
+ data.namereg_fail = namereg_fail;
+
+ if (reserve)
+ if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ pa_reserve_wrapper_set_application_device_name(reserve, description);
+
+ data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ add_profiles(u, data.profiles);
+
+ if (pa_hashmap_isempty(data.profiles)) {
+ pa_log("Failed to find a working profile.");
+ pa_card_new_data_done(&data);
+ goto fail;
+ }
+
+ add_disabled_profile(data.profiles);
+
+ if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_card_new_data_done(&data);
+ goto fail;
+ }
+
+ u->card = pa_card_new(m->core, &data);
+ pa_card_new_data_done(&data);
+
+ if (!u->card)
+ goto fail;
+
+ u->card->userdata = u;
+ u->card->set_profile = card_set_profile;
+
+ init_profile(u);
+
+ if (reserve)
+ pa_reserve_wrapper_unref(reserve);
+
+ if (!pa_hashmap_isempty(u->profile_set->decibel_fixes))
+ pa_log_warn("Card %s uses decibel fixes (i.e. overrides the decibel information for some alsa volume elements). "
+ "Please note that this feature is meant just as a help for figuring out the correct decibel values. "
+ "Pulseaudio is not the correct place to maintain the decibel mappings! The fixed decibel values "
+ "should be sent to ALSA developers so that they can fix the driver. If it turns out that this feature "
+ "is abused (i.e. fixes are not pushed to ALSA), the decibel fix feature may be removed in some future "
+ "Pulseaudio version.", u->card->name);
+
+ return 0;
+
+fail:
+ if (reserve)
+ pa_reserve_wrapper_unref(reserve);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+ int n = 0;
+ uint32_t idx;
+ pa_sink *sink;
+ pa_source *source;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+ pa_assert(u->card);
+
+ PA_IDXSET_FOREACH(sink, u->card->sinks, idx)
+ n += pa_sink_linked_by(sink);
+
+ PA_IDXSET_FOREACH(source, u->card->sources, idx)
+ n += pa_source_linked_by(source);
+
+ return n;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ goto finish;
+
+ if (u->card && u->card->sinks) {
+ pa_sink *s;
+
+ while ((s = pa_idxset_steal_first(u->card->sinks, NULL)))
+ pa_alsa_sink_free(s);
+ }
+
+ if (u->card && u->card->sources) {
+ pa_source *s;
+
+ while ((s = pa_idxset_steal_first(u->card->sources, NULL)))
+ pa_alsa_source_free(s);
+ }
+
+ if (u->card)
+ pa_card_free(u->card);
+
+ if (u->modargs)
+ pa_modargs_free(u->modargs);
+
+ if (u->profile_set)
+ pa_alsa_profile_set_free(u->profile_set);
+
+ pa_xfree(u->device_id);
+ pa_xfree(u);
+
+finish:
+ pa_alsa_refcnt_dec();
+}
diff --git a/src/modules/alsa/module-alsa-sink.c b/src/modules/alsa/module-alsa-sink.c
new file mode 100644
index 00000000..6e64ab31
--- /dev/null
+++ b/src/modules/alsa/module-alsa-sink.c
@@ -0,0 +1,136 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2008 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/module.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/modargs.h>
+
+#include "alsa-util.h"
+#include "alsa-sink.h"
+#include "module-alsa-sink-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("ALSA Sink");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "name=<name of the sink, to be prefixed> "
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "namereg_fail=<pa_namereg_register() fail parameter value> "
+ "device=<ALSA device> "
+ "device_id=<ALSA card index> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "fragments=<number of fragments> "
+ "fragment_size=<fragment size> "
+ "mmap=<enable memory mapping?> "
+ "tsched=<enable system timer based scheduling mode?> "
+ "tsched_buffer_size=<buffer size when using timer based scheduling> "
+ "tsched_buffer_watermark=<lower fill watermark> "
+ "ignore_dB=<ignore dB information from the device?> "
+ "control=<name of mixer control> "
+ "rewind_safeguard=<number of bytes that cannot be rewound> "
+ "sync_volume=<syncronize sw and hw voluchanges in IO-thread?> "
+ "sync_volume_safety_margin=<usec adjustment depending on volume direction> "
+ "sync_volume_extra_delay=<usec adjustment to HW volume changes>");
+
+static const char* const valid_modargs[] = {
+ "name",
+ "sink_name",
+ "sink_properties",
+ "namereg_fail",
+ "device",
+ "device_id",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "fragments",
+ "fragment_size",
+ "mmap",
+ "tsched",
+ "tsched_buffer_size",
+ "tsched_buffer_watermark",
+ "ignore_dB",
+ "control",
+ "rewind_safeguard",
+ "sync_volume",
+ "sync_volume_safety_margin",
+ "sync_volume_extra_delay",
+ NULL
+};
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+
+ pa_assert(m);
+
+ pa_alsa_refcnt_inc();
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (!(m->userdata = pa_alsa_sink_new(m, ma, __FILE__, NULL, NULL)))
+ goto fail;
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ pa_sink *sink;
+
+ pa_assert(m);
+ pa_assert_se(sink = m->userdata);
+
+ return pa_sink_linked_by(sink);
+}
+
+void pa__done(pa_module*m) {
+ pa_sink *sink;
+
+ pa_assert(m);
+
+ if ((sink = m->userdata))
+ pa_alsa_sink_free(sink);
+
+ pa_alsa_refcnt_dec();
+}
diff --git a/src/modules/alsa/module-alsa-source.c b/src/modules/alsa/module-alsa-source.c
new file mode 100644
index 00000000..5ecd1e34
--- /dev/null
+++ b/src/modules/alsa/module-alsa-source.c
@@ -0,0 +1,143 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2008 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+
+#include <asoundlib.h>
+
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+#endif
+
+#include <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+
+#include "alsa-util.h"
+#include "alsa-source.h"
+#include "module-alsa-source-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("ALSA Source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "name=<name for the source, to be prefixed> "
+ "source_name=<name for the source> "
+ "source_properties=<properties for the source> "
+ "namereg_fail=<pa_namereg_register() fail parameter value> "
+ "device=<ALSA device> "
+ "device_id=<ALSA card index> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "fragments=<number of fragments> "
+ "fragment_size=<fragment size> "
+ "mmap=<enable memory mapping?> "
+ "tsched=<enable system timer based scheduling mode?> "
+ "tsched_buffer_size=<buffer size when using timer based scheduling> "
+ "tsched_buffer_watermark=<upper fill watermark> "
+ "ignore_dB=<ignore dB information from the device?> "
+ "control=<name of mixer control>"
+ "sync_volume=<syncronize sw and hw voluchanges in IO-thread?> "
+ "sync_volume_safety_margin=<usec adjustment depending on volume direction> "
+ "sync_volume_extra_delay=<usec adjustment to HW volume changes>");
+
+static const char* const valid_modargs[] = {
+ "name",
+ "source_name",
+ "source_properties",
+ "namereg_fail",
+ "device",
+ "device_id",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "fragments",
+ "fragment_size",
+ "mmap",
+ "tsched",
+ "tsched_buffer_size",
+ "tsched_buffer_watermark",
+ "ignore_dB",
+ "control",
+ "sync_volume",
+ "sync_volume_safety_margin",
+ "sync_volume_extra_delay",
+ NULL
+};
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+
+ pa_assert(m);
+
+ pa_alsa_refcnt_inc();
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (!(m->userdata = pa_alsa_source_new(m, ma, __FILE__, NULL, NULL)))
+ goto fail;
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ pa_source *source;
+
+ pa_assert(m);
+ pa_assert_se(source = m->userdata);
+
+ return pa_source_linked_by(source);
+}
+
+void pa__done(pa_module*m) {
+ pa_source *source;
+
+ pa_assert(m);
+
+ if ((source = m->userdata))
+ pa_alsa_source_free(source);
+
+ pa_alsa_refcnt_dec();
+}
diff --git a/src/modules/bluetooth/a2dp-codecs.h b/src/modules/bluetooth/a2dp-codecs.h
new file mode 100644
index 00000000..e44634ea
--- /dev/null
+++ b/src/modules/bluetooth/a2dp-codecs.h
@@ -0,0 +1,116 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2006-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#define A2DP_CODEC_SBC 0x00
+#define A2DP_CODEC_MPEG12 0x01
+#define A2DP_CODEC_MPEG24 0x02
+#define A2DP_CODEC_ATRAC 0x03
+
+#define SBC_SAMPLING_FREQ_16000 (1 << 3)
+#define SBC_SAMPLING_FREQ_32000 (1 << 2)
+#define SBC_SAMPLING_FREQ_44100 (1 << 1)
+#define SBC_SAMPLING_FREQ_48000 1
+
+#define SBC_CHANNEL_MODE_MONO (1 << 3)
+#define SBC_CHANNEL_MODE_DUAL_CHANNEL (1 << 2)
+#define SBC_CHANNEL_MODE_STEREO (1 << 1)
+#define SBC_CHANNEL_MODE_JOINT_STEREO 1
+
+#define SBC_BLOCK_LENGTH_4 (1 << 3)
+#define SBC_BLOCK_LENGTH_8 (1 << 2)
+#define SBC_BLOCK_LENGTH_12 (1 << 1)
+#define SBC_BLOCK_LENGTH_16 1
+
+#define SBC_SUBBANDS_4 (1 << 1)
+#define SBC_SUBBANDS_8 1
+
+#define SBC_ALLOCATION_SNR (1 << 1)
+#define SBC_ALLOCATION_LOUDNESS 1
+
+#define MPEG_CHANNEL_MODE_MONO (1 << 3)
+#define MPEG_CHANNEL_MODE_DUAL_CHANNEL (1 << 2)
+#define MPEG_CHANNEL_MODE_STEREO (1 << 1)
+#define MPEG_CHANNEL_MODE_JOINT_STEREO 1
+
+#define MPEG_LAYER_MP1 (1 << 2)
+#define MPEG_LAYER_MP2 (1 << 1)
+#define MPEG_LAYER_MP3 1
+
+#define MPEG_SAMPLING_FREQ_16000 (1 << 5)
+#define MPEG_SAMPLING_FREQ_22050 (1 << 4)
+#define MPEG_SAMPLING_FREQ_24000 (1 << 3)
+#define MPEG_SAMPLING_FREQ_32000 (1 << 2)
+#define MPEG_SAMPLING_FREQ_44100 (1 << 1)
+#define MPEG_SAMPLING_FREQ_48000 1
+
+#define MAX_BITPOOL 64
+#define MIN_BITPOOL 2
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+
+typedef struct {
+ uint8_t channel_mode:4;
+ uint8_t frequency:4;
+ uint8_t allocation_method:2;
+ uint8_t subbands:2;
+ uint8_t block_length:4;
+ uint8_t min_bitpool;
+ uint8_t max_bitpool;
+} __attribute__ ((packed)) a2dp_sbc_t;
+
+typedef struct {
+ uint8_t channel_mode:4;
+ uint8_t crc:1;
+ uint8_t layer:3;
+ uint8_t frequency:6;
+ uint8_t mpf:1;
+ uint8_t rfa:1;
+ uint16_t bitrate;
+} __attribute__ ((packed)) a2dp_mpeg_t;
+
+#elif __BYTE_ORDER == __BIG_ENDIAN
+
+typedef struct {
+ uint8_t frequency:4;
+ uint8_t channel_mode:4;
+ uint8_t block_length:4;
+ uint8_t subbands:2;
+ uint8_t allocation_method:2;
+ uint8_t min_bitpool;
+ uint8_t max_bitpool;
+} __attribute__ ((packed)) a2dp_sbc_t;
+
+typedef struct {
+ uint8_t layer:3;
+ uint8_t crc:1;
+ uint8_t channel_mode:4;
+ uint8_t rfa:1;
+ uint8_t mpf:1;
+ uint8_t frequency:6;
+ uint16_t bitrate;
+} __attribute__ ((packed)) a2dp_mpeg_t;
+
+#else
+#error "Unknown byte order"
+#endif
diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
new file mode 100644
index 00000000..b24fe7a3
--- /dev/null
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -0,0 +1,1662 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008-2009 Joao Paulo Rechi Vita
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/shared.h>
+#include <pulsecore/dbus-shared.h>
+
+#include "bluetooth-util.h"
+#include "ipc.h"
+#include "a2dp-codecs.h"
+
+#define HFP_AG_ENDPOINT "/MediaEndpoint/HFPAG"
+#define A2DP_SOURCE_ENDPOINT "/MediaEndpoint/A2DPSource"
+#define A2DP_SINK_ENDPOINT "/MediaEndpoint/A2DPSink"
+
+#define ENDPOINT_INTROSPECT_XML \
+ DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
+ "<node>" \
+ " <interface name=\"org.bluez.MediaEndpoint\">" \
+ " <method name=\"SetConfiguration\">" \
+ " <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
+ " <arg name=\"configuration\" direction=\"in\" type=\"ay\"/>" \
+ " </method>" \
+ " <method name=\"SelectConfiguration\">" \
+ " <arg name=\"capabilities\" direction=\"in\" type=\"ay\"/>" \
+ " <arg name=\"configuration\" direction=\"out\" type=\"ay\"/>" \
+ " </method>" \
+ " <method name=\"ClearConfiguration\">" \
+ " </method>" \
+ " <method name=\"Release\">" \
+ " </method>" \
+ " </interface>" \
+ " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
+ " <method name=\"Introspect\">" \
+ " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
+ " </method>" \
+ " </interface>" \
+ "</node>"
+
+struct pa_bluetooth_discovery {
+ PA_REFCNT_DECLARE;
+
+ pa_core *core;
+ pa_dbus_connection *connection;
+ PA_LLIST_HEAD(pa_dbus_pending, pending);
+ pa_hashmap *devices;
+ pa_hook hook;
+ pa_bool_t filter_added;
+};
+
+static void get_properties_reply(DBusPendingCall *pending, void *userdata);
+static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, DBusMessage *m, DBusPendingCallNotifyFunction func, void *call_data);
+
+static pa_bt_audio_state_t pa_bt_audio_state_from_string(const char* value) {
+ pa_assert(value);
+
+ if (pa_streq(value, "disconnected"))
+ return PA_BT_AUDIO_STATE_DISCONNECTED;
+ else if (pa_streq(value, "connecting"))
+ return PA_BT_AUDIO_STATE_CONNECTING;
+ else if (pa_streq(value, "connected"))
+ return PA_BT_AUDIO_STATE_CONNECTED;
+ else if (pa_streq(value, "playing"))
+ return PA_BT_AUDIO_STATE_PLAYING;
+
+ return PA_BT_AUDIO_STATE_INVALID;
+}
+
+static pa_bluetooth_uuid *uuid_new(const char *uuid) {
+ pa_bluetooth_uuid *u;
+
+ u = pa_xnew(pa_bluetooth_uuid, 1);
+ u->uuid = pa_xstrdup(uuid);
+ PA_LLIST_INIT(pa_bluetooth_uuid, u);
+
+ return u;
+}
+
+static void uuid_free(pa_bluetooth_uuid *u) {
+ pa_assert(u);
+
+ pa_xfree(u->uuid);
+ pa_xfree(u);
+}
+
+static pa_bluetooth_device* device_new(const char *path) {
+ pa_bluetooth_device *d;
+
+ d = pa_xnew(pa_bluetooth_device, 1);
+
+ d->dead = FALSE;
+
+ d->device_info_valid = 0;
+
+ d->name = NULL;
+ d->path = pa_xstrdup(path);
+ d->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ d->paired = -1;
+ d->alias = NULL;
+ d->device_connected = -1;
+ PA_LLIST_HEAD_INIT(pa_bluetooth_uuid, d->uuids);
+ d->address = NULL;
+ d->class = -1;
+ d->trusted = -1;
+
+ d->audio_state = PA_BT_AUDIO_STATE_INVALID;
+ d->audio_sink_state = PA_BT_AUDIO_STATE_INVALID;
+ d->audio_source_state = PA_BT_AUDIO_STATE_INVALID;
+ d->headset_state = PA_BT_AUDIO_STATE_INVALID;
+ d->hfgw_state = PA_BT_AUDIO_STATE_INVALID;
+
+ return d;
+}
+
+static void transport_free(pa_bluetooth_transport *t) {
+ pa_assert(t);
+
+ pa_xfree(t->path);
+ pa_xfree(t->config);
+ pa_xfree(t);
+}
+
+static void device_free(pa_bluetooth_device *d) {
+ pa_bluetooth_uuid *u;
+ pa_bluetooth_transport *t;
+
+ pa_assert(d);
+
+ while ((t = pa_hashmap_steal_first(d->transports)))
+ transport_free(t);
+
+ pa_hashmap_free(d->transports, NULL, NULL);
+
+ while ((u = d->uuids)) {
+ PA_LLIST_REMOVE(pa_bluetooth_uuid, d->uuids, u);
+ uuid_free(u);
+ }
+
+ pa_xfree(d->name);
+ pa_xfree(d->path);
+ pa_xfree(d->alias);
+ pa_xfree(d->address);
+ pa_xfree(d);
+}
+
+static pa_bool_t device_is_audio(pa_bluetooth_device *d) {
+ pa_assert(d);
+
+ return
+ d->device_info_valid && (d->hfgw_state != PA_BT_AUDIO_STATE_INVALID ||
+ (d->audio_state != PA_BT_AUDIO_STATE_INVALID &&
+ (d->audio_sink_state != PA_BT_AUDIO_STATE_INVALID ||
+ d->audio_source_state != PA_BT_AUDIO_STATE_INVALID ||
+ d->headset_state != PA_BT_AUDIO_STATE_INVALID)));
+}
+
+static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessageIter *i) {
+ const char *key;
+ DBusMessageIter variant_i;
+
+ pa_assert(y);
+ pa_assert(d);
+ pa_assert(i);
+
+ if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
+ pa_log("Property name not a string.");
+ return -1;
+ }
+
+ dbus_message_iter_get_basic(i, &key);
+
+ if (!dbus_message_iter_next(i)) {
+ pa_log("Property value missing");
+ return -1;
+ }
+
+ if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
+ pa_log("Property value not a variant.");
+ return -1;
+ }
+
+ dbus_message_iter_recurse(i, &variant_i);
+
+/* pa_log_debug("Parsing property org.bluez.Device.%s", key); */
+
+ switch (dbus_message_iter_get_arg_type(&variant_i)) {
+
+ case DBUS_TYPE_STRING: {
+
+ const char *value;
+ dbus_message_iter_get_basic(&variant_i, &value);
+
+ if (pa_streq(key, "Name")) {
+ pa_xfree(d->name);
+ d->name = pa_xstrdup(value);
+ } else if (pa_streq(key, "Alias")) {
+ pa_xfree(d->alias);
+ d->alias = pa_xstrdup(value);
+ } else if (pa_streq(key, "Address")) {
+ pa_xfree(d->address);
+ d->address = pa_xstrdup(value);
+ }
+
+/* pa_log_debug("Value %s", value); */
+
+ break;
+ }
+
+ case DBUS_TYPE_BOOLEAN: {
+
+ dbus_bool_t value;
+ dbus_message_iter_get_basic(&variant_i, &value);
+
+ if (pa_streq(key, "Paired"))
+ d->paired = !!value;
+ else if (pa_streq(key, "Connected"))
+ d->device_connected = !!value;
+ else if (pa_streq(key, "Trusted"))
+ d->trusted = !!value;
+
+/* pa_log_debug("Value %s", pa_yes_no(value)); */
+
+ break;
+ }
+
+ case DBUS_TYPE_UINT32: {
+
+ uint32_t value;
+ dbus_message_iter_get_basic(&variant_i, &value);
+
+ if (pa_streq(key, "Class"))
+ d->class = (int) value;
+
+/* pa_log_debug("Value %u", (unsigned) value); */
+
+ break;
+ }
+
+ case DBUS_TYPE_ARRAY: {
+
+ DBusMessageIter ai;
+ dbus_message_iter_recurse(&variant_i, &ai);
+
+ if (dbus_message_iter_get_arg_type(&ai) == DBUS_TYPE_STRING &&
+ pa_streq(key, "UUIDs")) {
+
+ while (dbus_message_iter_get_arg_type(&ai) != DBUS_TYPE_INVALID) {
+ pa_bluetooth_uuid *node;
+ const char *value;
+ DBusMessage *m;
+
+ dbus_message_iter_get_basic(&ai, &value);
+ node = uuid_new(value);
+ PA_LLIST_PREPEND(pa_bluetooth_uuid, d->uuids, node);
+
+ /* Vudentz said the interfaces are here when the UUIDs are announced */
+ if (strcasecmp(HSP_AG_UUID, value) == 0 || strcasecmp(HFP_AG_UUID, value) == 0) {
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.HandsfreeGateway", "GetProperties"));
+ send_and_add_to_pending(y, m, get_properties_reply, d);
+ } else if (strcasecmp(HSP_HS_UUID, value) == 0 || strcasecmp(HFP_HS_UUID, value) == 0) {
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.Headset", "GetProperties"));
+ send_and_add_to_pending(y, m, get_properties_reply, d);
+ } else if (strcasecmp(A2DP_SINK_UUID, value) == 0) {
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.AudioSink", "GetProperties"));
+ send_and_add_to_pending(y, m, get_properties_reply, d);
+ } else if (strcasecmp(A2DP_SOURCE_UUID, value) == 0) {
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.AudioSource", "GetProperties"));
+ send_and_add_to_pending(y, m, get_properties_reply, d);
+ }
+
+ /* this might eventually be racy if .Audio is not there yet, but the State change will come anyway later, so this call is for cold-detection mostly */
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.Audio", "GetProperties"));
+ send_and_add_to_pending(y, m, get_properties_reply, d);
+
+ if (!dbus_message_iter_next(&ai))
+ break;
+ }
+ }
+
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int parse_audio_property(pa_bluetooth_discovery *u, int *state, DBusMessageIter *i) {
+ const char *key;
+ DBusMessageIter variant_i;
+
+ pa_assert(u);
+ pa_assert(state);
+ pa_assert(i);
+
+ if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
+ pa_log("Property name not a string.");
+ return -1;
+ }
+
+ dbus_message_iter_get_basic(i, &key);
+
+ if (!dbus_message_iter_next(i)) {
+ pa_log("Property value missing");
+ return -1;
+ }
+
+ if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
+ pa_log("Property value not a variant.");
+ return -1;
+ }
+
+ dbus_message_iter_recurse(i, &variant_i);
+
+/* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|AudioSource|Headset}.%s", key); */
+
+ switch (dbus_message_iter_get_arg_type(&variant_i)) {
+
+ case DBUS_TYPE_STRING: {
+
+ const char *value;
+ dbus_message_iter_get_basic(&variant_i, &value);
+
+ if (pa_streq(key, "State")) {
+ *state = pa_bt_audio_state_from_string(value);
+ pa_log_debug("dbus: property 'State' changed to value '%s'", value);
+ }
+
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static void run_callback(pa_bluetooth_discovery *y, pa_bluetooth_device *d, pa_bool_t dead) {
+ pa_assert(y);
+ pa_assert(d);
+
+ if (!device_is_audio(d))
+ return;
+
+ d->dead = dead;
+ pa_hook_fire(&y->hook, d);
+}
+
+static void remove_all_devices(pa_bluetooth_discovery *y) {
+ pa_bluetooth_device *d;
+
+ pa_assert(y);
+
+ while ((d = pa_hashmap_steal_first(y->devices))) {
+ run_callback(y, d, TRUE);
+ device_free(d);
+ }
+}
+
+static pa_bluetooth_device *found_device(pa_bluetooth_discovery *y, const char* path) {
+ DBusMessage *m;
+ pa_bluetooth_device *d;
+
+ pa_assert(y);
+ pa_assert(path);
+
+ d = pa_hashmap_get(y->devices, path);
+ if (d)
+ return d;
+
+ d = device_new(path);
+
+ pa_hashmap_put(y->devices, d->path, d);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Device", "GetProperties"));
+ send_and_add_to_pending(y, m, get_properties_reply, d);
+
+ /* Before we read the other properties (Audio, AudioSink, AudioSource,
+ * Headset) we wait that the UUID is read */
+ return d;
+}
+
+static void get_properties_reply(DBusPendingCall *pending, void *userdata) {
+ DBusMessage *r;
+ DBusMessageIter arg_i, element_i;
+ pa_dbus_pending *p;
+ pa_bluetooth_device *d;
+ pa_bluetooth_discovery *y;
+ int valid;
+
+ pa_assert_se(p = userdata);
+ pa_assert_se(y = p->context_data);
+ pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+/* pa_log_debug("Got %s.GetProperties response for %s", */
+/* dbus_message_get_interface(p->message), */
+/* dbus_message_get_path(p->message)); */
+
+ /* We don't use p->call_data here right-away since the device
+ * might already be invalidated at this point */
+
+ if (!(d = pa_hashmap_get(y->devices, dbus_message_get_path(p->message))))
+ return;
+
+ pa_assert(p->call_data == d);
+
+ valid = dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR ? -1 : 1;
+
+ if (dbus_message_is_method_call(p->message, "org.bluez.Device", "GetProperties"))
+ d->device_info_valid = valid;
+
+ if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
+ pa_log_debug("Bluetooth daemon is apparently not available.");
+ remove_all_devices(y);
+ goto finish2;
+ }
+
+ if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+
+ if (!dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD))
+ pa_log("Error from GetProperties reply: %s", dbus_message_get_error_name(r));
+
+ goto finish;
+ }
+
+ if (!dbus_message_iter_init(r, &arg_i)) {
+ pa_log("GetProperties reply has no arguments.");
+ goto finish;
+ }
+
+ if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
+ pa_log("GetProperties argument is not an array.");
+ goto finish;
+ }
+
+ dbus_message_iter_recurse(&arg_i, &element_i);
+ while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
+
+ if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
+ DBusMessageIter dict_i;
+
+ dbus_message_iter_recurse(&element_i, &dict_i);
+
+ if (dbus_message_has_interface(p->message, "org.bluez.Device")) {
+ if (parse_device_property(y, d, &dict_i) < 0)
+ goto finish;
+
+ } else if (dbus_message_has_interface(p->message, "org.bluez.Audio")) {
+ if (parse_audio_property(y, &d->audio_state, &dict_i) < 0)
+ goto finish;
+
+ } else if (dbus_message_has_interface(p->message, "org.bluez.Headset")) {
+ if (parse_audio_property(y, &d->headset_state, &dict_i) < 0)
+ goto finish;
+
+ } else if (dbus_message_has_interface(p->message, "org.bluez.AudioSink")) {
+ if (parse_audio_property(y, &d->audio_sink_state, &dict_i) < 0)
+ goto finish;
+
+ } else if (dbus_message_has_interface(p->message, "org.bluez.AudioSource")) {
+ if (parse_audio_property(y, &d->audio_source_state, &dict_i) < 0)
+ goto finish;
+
+ } else if (dbus_message_has_interface(p->message, "org.bluez.HandsfreeGateway")) {
+ if (parse_audio_property(y, &d->hfgw_state, &arg_i) < 0)
+ goto finish;
+
+ }
+ }
+
+ if (!dbus_message_iter_next(&element_i))
+ break;
+ }
+
+finish:
+ run_callback(y, d, FALSE);
+
+finish2:
+ dbus_message_unref(r);
+
+ PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
+ pa_dbus_pending_free(p);
+}
+
+static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, DBusMessage *m, DBusPendingCallNotifyFunction func, void *call_data) {
+ pa_dbus_pending *p;
+ DBusPendingCall *call;
+
+ pa_assert(y);
+ pa_assert(m);
+
+ pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y->connection), m, &call, -1));
+
+ p = pa_dbus_pending_new(pa_dbus_connection_get(y->connection), m, call, y, call_data);
+ PA_LLIST_PREPEND(pa_dbus_pending, y->pending, p);
+ dbus_pending_call_set_notify(call, func, p, NULL);
+
+ return p;
+}
+
+#ifdef DBUS_TYPE_UNIX_FD
+static void register_endpoint_reply(DBusPendingCall *pending, void *userdata) {
+ DBusError e;
+ DBusMessage *r;
+ pa_dbus_pending *p;
+ pa_bluetooth_discovery *y;
+ char *endpoint;
+
+ pa_assert(pending);
+
+ dbus_error_init(&e);
+
+ pa_assert_se(p = userdata);
+ pa_assert_se(y = p->context_data);
+ pa_assert_se(endpoint = p->call_data);
+ pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+ if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
+ pa_log_debug("Bluetooth daemon is apparently not available.");
+ remove_all_devices(y);
+ goto finish;
+ }
+
+ if (dbus_message_is_error(r, PA_BLUETOOTH_ERROR_NOT_SUPPORTED)) {
+ pa_log_info("Couldn't register endpoint %s, because BlueZ is configured to disable the endpoint type.", endpoint);
+ goto finish;
+ }
+
+ if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+ pa_log("Error from RegisterEndpoint reply: %s", dbus_message_get_error_name(r));
+ goto finish;
+ }
+
+finish:
+ dbus_message_unref(r);
+
+ PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
+ pa_dbus_pending_free(p);
+
+ pa_xfree(endpoint);
+}
+#endif
+
+static void list_devices_reply(DBusPendingCall *pending, void *userdata) {
+ DBusError e;
+ DBusMessage *r;
+ char **paths = NULL;
+ int num = -1;
+ pa_dbus_pending *p;
+ pa_bluetooth_discovery *y;
+
+ pa_assert(pending);
+
+ dbus_error_init(&e);
+
+ pa_assert_se(p = userdata);
+ pa_assert_se(y = p->context_data);
+ pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+ if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
+ pa_log_debug("Bluetooth daemon is apparently not available.");
+ remove_all_devices(y);
+ goto finish;
+ }
+
+ if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+ pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r));
+ goto finish;
+ }
+
+ if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
+ pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e.message);
+ dbus_error_free(&e);
+ } else {
+ int i;
+
+ for (i = 0; i < num; ++i)
+ found_device(y, paths[i]);
+ }
+
+finish:
+ if (paths)
+ dbus_free_string_array(paths);
+
+ dbus_message_unref(r);
+
+ PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
+ pa_dbus_pending_free(p);
+}
+
+#ifdef DBUS_TYPE_UNIX_FD
+static void register_endpoint(pa_bluetooth_discovery *y, const char *path, const char *endpoint, const char *uuid) {
+ DBusMessage *m;
+ DBusMessageIter i, d;
+ uint8_t codec = 0;
+
+ pa_log_debug("Registering %s on adapter %s.", endpoint, path);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Media", "RegisterEndpoint"));
+
+ dbus_message_iter_init_append(m, &i);
+
+ dbus_message_iter_append_basic(&i, DBUS_TYPE_OBJECT_PATH, &endpoint);
+
+ dbus_message_iter_open_container(&i, DBUS_TYPE_ARRAY, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
+ &d);
+
+ pa_dbus_append_basic_variant_dict_entry(&d, "UUID", DBUS_TYPE_STRING, &uuid);
+
+ pa_dbus_append_basic_variant_dict_entry(&d, "Codec", DBUS_TYPE_BYTE, &codec);
+
+ if (pa_streq(uuid, HFP_AG_UUID)) {
+ uint8_t capability = 0;
+ pa_dbus_append_basic_array_variant_dict_entry(&d, "Capabilities", DBUS_TYPE_BYTE, &capability, 1);
+ } else {
+ a2dp_sbc_t capabilities;
+
+ capabilities.channel_mode = BT_A2DP_CHANNEL_MODE_MONO | BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL |
+ BT_A2DP_CHANNEL_MODE_STEREO | BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
+ capabilities.frequency = BT_SBC_SAMPLING_FREQ_16000 | BT_SBC_SAMPLING_FREQ_32000 |
+ BT_SBC_SAMPLING_FREQ_44100 | BT_SBC_SAMPLING_FREQ_48000;
+ capabilities.allocation_method = BT_A2DP_ALLOCATION_SNR | BT_A2DP_ALLOCATION_LOUDNESS;
+ capabilities.subbands = BT_A2DP_SUBBANDS_4 | BT_A2DP_SUBBANDS_8;
+ capabilities.block_length = BT_A2DP_BLOCK_LENGTH_4 | BT_A2DP_BLOCK_LENGTH_8 |
+ BT_A2DP_BLOCK_LENGTH_12 | BT_A2DP_BLOCK_LENGTH_16;
+ capabilities.min_bitpool = MIN_BITPOOL;
+ capabilities.max_bitpool = MAX_BITPOOL;
+
+ pa_dbus_append_basic_array_variant_dict_entry(&d, "Capabilities", DBUS_TYPE_BYTE, &capabilities, sizeof(capabilities));
+ }
+
+ dbus_message_iter_close_container(&i, &d);
+
+ send_and_add_to_pending(y, m, register_endpoint_reply, pa_xstrdup(endpoint));
+}
+#endif
+
+static void found_adapter(pa_bluetooth_discovery *y, const char *path) {
+ DBusMessage *m;
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "ListDevices"));
+ send_and_add_to_pending(y, m, list_devices_reply, NULL);
+
+#ifdef DBUS_TYPE_UNIX_FD
+ register_endpoint(y, path, HFP_AG_ENDPOINT, HFP_AG_UUID);
+ register_endpoint(y, path, A2DP_SOURCE_ENDPOINT, A2DP_SOURCE_UUID);
+ register_endpoint(y, path, A2DP_SINK_ENDPOINT, A2DP_SINK_UUID);
+#endif
+}
+
+static void list_adapters_reply(DBusPendingCall *pending, void *userdata) {
+ DBusError e;
+ DBusMessage *r;
+ char **paths = NULL;
+ int num = -1;
+ pa_dbus_pending *p;
+ pa_bluetooth_discovery *y;
+
+ pa_assert(pending);
+
+ dbus_error_init(&e);
+
+ pa_assert_se(p = userdata);
+ pa_assert_se(y = p->context_data);
+ pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+ if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
+ pa_log_debug("Bluetooth daemon is apparently not available.");
+ remove_all_devices(y);
+ goto finish;
+ }
+
+ if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+ pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r));
+ goto finish;
+ }
+
+ if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
+ pa_log("org.bluez.Manager.ListAdapters returned an error: %s", e.message);
+ dbus_error_free(&e);
+ } else {
+ int i;
+
+ for (i = 0; i < num; ++i)
+ found_adapter(y, paths[i]);
+ }
+
+finish:
+ if (paths)
+ dbus_free_string_array(paths);
+
+ dbus_message_unref(r);
+
+ PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
+ pa_dbus_pending_free(p);
+}
+
+static void list_adapters(pa_bluetooth_discovery *y) {
+ DBusMessage *m;
+ pa_assert(y);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
+ send_and_add_to_pending(y, m, list_adapters_reply, NULL);
+}
+
+int pa_bluetooth_transport_parse_property(pa_bluetooth_transport *t, DBusMessageIter *i)
+{
+ const char *key;
+ DBusMessageIter variant_i;
+
+ if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
+ pa_log("Property name not a string.");
+ return -1;
+ }
+
+ dbus_message_iter_get_basic(i, &key);
+
+ if (!dbus_message_iter_next(i)) {
+ pa_log("Property value missing");
+ return -1;
+ }
+
+ if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
+ pa_log("Property value not a variant.");
+ return -1;
+ }
+
+ dbus_message_iter_recurse(i, &variant_i);
+
+ switch (dbus_message_iter_get_arg_type(&variant_i)) {
+
+ case DBUS_TYPE_BOOLEAN: {
+
+ pa_bool_t *value;
+ dbus_message_iter_get_basic(&variant_i, &value);
+
+ if (pa_streq(key, "NREC"))
+ t->nrec = value;
+
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
+ DBusError err;
+ pa_bluetooth_discovery *y;
+
+ pa_assert(bus);
+ pa_assert(m);
+
+ pa_assert_se(y = userdata);
+
+ dbus_error_init(&err);
+
+ pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
+ dbus_message_get_interface(m),
+ dbus_message_get_path(m),
+ dbus_message_get_member(m));
+
+ if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceRemoved")) {
+ const char *path;
+ pa_bluetooth_device *d;
+
+ if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err.message);
+ goto fail;
+ }
+
+ pa_log_debug("Device %s removed", path);
+
+ if ((d = pa_hashmap_remove(y->devices, path))) {
+ run_callback(y, d, TRUE);
+ device_free(d);
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ } else if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceCreated")) {
+ const char *path;
+
+ if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err.message);
+ goto fail;
+ }
+
+ pa_log_debug("Device %s created", path);
+
+ found_device(y, path);
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ } else if (dbus_message_is_signal(m, "org.bluez.Manager", "AdapterAdded")) {
+ const char *path;
+
+ if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err.message);
+ goto fail;
+ }
+
+ pa_log_debug("Adapter %s created", path);
+
+ found_adapter(y, path);
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ } else if (dbus_message_is_signal(m, "org.bluez.Audio", "PropertyChanged") ||
+ dbus_message_is_signal(m, "org.bluez.Headset", "PropertyChanged") ||
+ dbus_message_is_signal(m, "org.bluez.AudioSink", "PropertyChanged") ||
+ dbus_message_is_signal(m, "org.bluez.AudioSource", "PropertyChanged") ||
+ dbus_message_is_signal(m, "org.bluez.HandsfreeGateway", "PropertyChanged") ||
+ dbus_message_is_signal(m, "org.bluez.Device", "PropertyChanged")) {
+
+ pa_bluetooth_device *d;
+
+ if ((d = pa_hashmap_get(y->devices, dbus_message_get_path(m)))) {
+ DBusMessageIter arg_i;
+
+ if (!dbus_message_iter_init(m, &arg_i)) {
+ pa_log("Failed to parse PropertyChanged: %s", err.message);
+ goto fail;
+ }
+
+ if (dbus_message_has_interface(m, "org.bluez.Device")) {
+ if (parse_device_property(y, d, &arg_i) < 0)
+ goto fail;
+
+ } else if (dbus_message_has_interface(m, "org.bluez.Audio")) {
+ if (parse_audio_property(y, &d->audio_state, &arg_i) < 0)
+ goto fail;
+
+ } else if (dbus_message_has_interface(m, "org.bluez.Headset")) {
+ if (parse_audio_property(y, &d->headset_state, &arg_i) < 0)
+ goto fail;
+
+ } else if (dbus_message_has_interface(m, "org.bluez.AudioSink")) {
+ if (parse_audio_property(y, &d->audio_sink_state, &arg_i) < 0)
+ goto fail;
+
+ } else if (dbus_message_has_interface(m, "org.bluez.AudioSource")) {
+ if (parse_audio_property(y, &d->audio_source_state, &arg_i) < 0)
+ goto fail;
+
+ } else if (dbus_message_has_interface(m, "org.bluez.HandsfreeGateway")) {
+ if (parse_audio_property(y, &d->hfgw_state, &arg_i) < 0)
+ goto fail;
+ }
+
+ run_callback(y, d, FALSE);
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ } else if (dbus_message_is_signal(m, "org.bluez.Device", "DisconnectRequested")) {
+ pa_bluetooth_device *d;
+
+ if ((d = pa_hashmap_get(y->devices, dbus_message_get_path(m)))) {
+ /* Device will disconnect in 2 sec */
+ d->audio_state = PA_BT_AUDIO_STATE_DISCONNECTED;
+ d->audio_sink_state = PA_BT_AUDIO_STATE_DISCONNECTED;
+ d->audio_source_state = PA_BT_AUDIO_STATE_DISCONNECTED;
+ d->headset_state = PA_BT_AUDIO_STATE_DISCONNECTED;
+ d->hfgw_state = PA_BT_AUDIO_STATE_DISCONNECTED;
+
+ run_callback(y, d, FALSE);
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ } else if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
+ const char *name, *old_owner, *new_owner;
+
+ if (!dbus_message_get_args(m, &err,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_STRING, &old_owner,
+ DBUS_TYPE_STRING, &new_owner,
+ DBUS_TYPE_INVALID)) {
+ pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
+ goto fail;
+ }
+
+ if (pa_streq(name, "org.bluez")) {
+ if (old_owner && *old_owner) {
+ pa_log_debug("Bluetooth daemon disappeared.");
+ remove_all_devices(y);
+ }
+
+ if (new_owner && *new_owner) {
+ pa_log_debug("Bluetooth daemon appeared.");
+ list_adapters(y);
+ }
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ } else if (dbus_message_is_signal(m, "org.bluez.MediaTransport", "PropertyChanged")) {
+ pa_bluetooth_device *d;
+ pa_bluetooth_transport *t;
+ void *state = NULL;
+ DBusMessageIter arg_i;
+
+ while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
+ if ((t = pa_hashmap_get(d->transports, dbus_message_get_path(m))))
+ break;
+
+ if (!t)
+ goto fail;
+
+ if (!dbus_message_iter_init(m, &arg_i)) {
+ pa_log("Failed to parse PropertyChanged: %s", err.message);
+ goto fail;
+ }
+
+ if (pa_bluetooth_transport_parse_property(t, &arg_i) < 0)
+ goto fail;
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ }
+
+fail:
+ dbus_error_free(&err);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+const pa_bluetooth_device* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery *y, const char* address) {
+ pa_bluetooth_device *d;
+ void *state = NULL;
+
+ pa_assert(y);
+ pa_assert(PA_REFCNT_VALUE(y) > 0);
+ pa_assert(address);
+
+ if (!pa_hook_is_firing(&y->hook))
+ pa_bluetooth_discovery_sync(y);
+
+ while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
+ if (pa_streq(d->address, address))
+ return device_is_audio(d) ? d : NULL;
+
+ return NULL;
+}
+
+const pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery *y, const char* path) {
+ pa_bluetooth_device *d;
+
+ pa_assert(y);
+ pa_assert(PA_REFCNT_VALUE(y) > 0);
+ pa_assert(path);
+
+ if (!pa_hook_is_firing(&y->hook))
+ pa_bluetooth_discovery_sync(y);
+
+ if ((d = pa_hashmap_get(y->devices, path)))
+ if (device_is_audio(d))
+ return d;
+
+ return NULL;
+}
+
+const pa_bluetooth_transport* pa_bluetooth_discovery_get_transport(pa_bluetooth_discovery *y, const char *path) {
+ pa_bluetooth_device *d;
+ pa_bluetooth_transport *t;
+ void *state = NULL;
+
+ pa_assert(y);
+ pa_assert(PA_REFCNT_VALUE(y) > 0);
+ pa_assert(path);
+
+ while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
+ if ((t = pa_hashmap_get(d->transports, path)))
+ return t;
+
+ return NULL;
+}
+
+const pa_bluetooth_transport* pa_bluetooth_device_get_transport(const pa_bluetooth_device *d, enum profile profile) {
+ pa_bluetooth_transport *t;
+ void *state = NULL;
+
+ pa_assert(d);
+
+ while ((t = pa_hashmap_iterate(d->transports, &state, NULL)))
+ if (t->profile == profile)
+ return t;
+
+ return NULL;
+}
+
+int pa_bluetooth_transport_acquire(const pa_bluetooth_transport *t, const char *accesstype, size_t *imtu, size_t *omtu) {
+ DBusMessage *m, *r;
+ DBusError err;
+ int ret;
+ uint16_t i, o;
+
+ pa_assert(t);
+ pa_assert(t->y);
+
+ dbus_error_init(&err);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", t->path, "org.bluez.MediaTransport", "Acquire"));
+ pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_STRING, &accesstype, DBUS_TYPE_INVALID));
+ r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->y->connection), m, -1, &err);
+
+ if (dbus_error_is_set(&err) || !r) {
+ pa_log("Failed to acquire transport fd: %s", err.message);
+ dbus_error_free(&err);
+ return -1;
+ }
+
+#ifdef DBUS_TYPE_UNIX_FD
+ if (!dbus_message_get_args(r, &err, DBUS_TYPE_UNIX_FD, &ret, DBUS_TYPE_UINT16, &i, DBUS_TYPE_UINT16, &o, DBUS_TYPE_INVALID)) {
+ pa_log("Failed to parse org.bluez.MediaTransport.Acquire(): %s", err.message);
+ ret = -1;
+ dbus_error_free(&err);
+ goto fail;
+ }
+#endif
+
+ if (imtu)
+ *imtu = i;
+
+ if (omtu)
+ *omtu = o;
+
+#ifdef DBUS_TYPE_UNIX_FD
+fail:
+#endif
+ dbus_message_unref(r);
+ return ret;
+}
+
+void pa_bluetooth_transport_release(const pa_bluetooth_transport *t, const char *accesstype) {
+ DBusMessage *m;
+ DBusError err;
+
+ pa_assert(t);
+ pa_assert(t->y);
+
+ dbus_error_init(&err);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", t->path, "org.bluez.MediaTransport", "Release"));
+ pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_STRING, &accesstype, DBUS_TYPE_INVALID));
+ dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->y->connection), m, -1, &err);
+
+ if (dbus_error_is_set(&err)) {
+ pa_log("Failed to release transport %s: %s", t->path, err.message);
+ dbus_error_free(&err);
+ } else
+ pa_log_info("Transport %s released", t->path);
+}
+
+static int setup_dbus(pa_bluetooth_discovery *y) {
+ DBusError err;
+
+ dbus_error_init(&err);
+
+ y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err);
+
+ if (dbus_error_is_set(&err) || !y->connection) {
+ pa_log("Failed to get D-Bus connection: %s", err.message);
+ dbus_error_free(&err);
+ return -1;
+ }
+
+ return 0;
+}
+
+#ifdef DBUS_TYPE_UNIX_FD
+static pa_bluetooth_transport *transport_new(pa_bluetooth_discovery *y, const char *path, enum profile p, const uint8_t *config, int size) {
+ pa_bluetooth_transport *t;
+
+ t = pa_xnew0(pa_bluetooth_transport, 1);
+ t->y = y;
+ t->path = pa_xstrdup(path);
+ t->profile = p;
+ t->config_size = size;
+
+ if (size > 0) {
+ t->config = pa_xnew(uint8_t, size);
+ memcpy(t->config, config, size);
+ }
+
+ return t;
+}
+
+static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
+ pa_bluetooth_discovery *y = userdata;
+ pa_bluetooth_device *d;
+ pa_bluetooth_transport *t;
+ const char *path, *dev_path = NULL, *uuid = NULL;
+ uint8_t *config = NULL;
+ int size = 0;
+ pa_bool_t nrec;
+ enum profile p;
+ DBusMessageIter args, props;
+ DBusMessage *r;
+
+ dbus_message_iter_init(m, &args);
+
+ dbus_message_iter_get_basic(&args, &path);
+ if (!dbus_message_iter_next(&args))
+ goto fail;
+
+ dbus_message_iter_recurse(&args, &props);
+ if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
+ goto fail;
+
+ /* Read transport properties */
+ while (dbus_message_iter_get_arg_type(&props) == DBUS_TYPE_DICT_ENTRY) {
+ const char *key;
+ DBusMessageIter value, entry;
+ int var;
+
+ dbus_message_iter_recurse(&props, &entry);
+ dbus_message_iter_get_basic(&entry, &key);
+
+ dbus_message_iter_next(&entry);
+ dbus_message_iter_recurse(&entry, &value);
+
+ var = dbus_message_iter_get_arg_type(&value);
+ if (strcasecmp(key, "UUID") == 0) {
+ if (var != DBUS_TYPE_STRING)
+ goto fail;
+ dbus_message_iter_get_basic(&value, &uuid);
+ } else if (strcasecmp(key, "Device") == 0) {
+ if (var != DBUS_TYPE_OBJECT_PATH)
+ goto fail;
+ dbus_message_iter_get_basic(&value, &dev_path);
+ } else if (strcasecmp(key, "NREC") == 0) {
+ if (var != DBUS_TYPE_BOOLEAN)
+ goto fail;
+ dbus_message_iter_get_basic(&value, &nrec);
+ } else if (strcasecmp(key, "Configuration") == 0) {
+ DBusMessageIter array;
+ if (var != DBUS_TYPE_ARRAY)
+ goto fail;
+ dbus_message_iter_recurse(&value, &array);
+ dbus_message_iter_get_fixed_array(&array, &config, &size);
+ }
+
+ dbus_message_iter_next(&props);
+ }
+
+ d = found_device(y, dev_path);
+ if (!d)
+ goto fail;
+
+ if (dbus_message_has_path(m, HFP_AG_ENDPOINT))
+ p = PROFILE_HSP;
+ else if (dbus_message_has_path(m, A2DP_SOURCE_ENDPOINT))
+ p = PROFILE_A2DP;
+ else
+ p = PROFILE_A2DP_SOURCE;
+
+ t = transport_new(y, path, p, config, size);
+ if (nrec)
+ t->nrec = nrec;
+ pa_hashmap_put(d->transports, t->path, t);
+
+ pa_log_debug("Transport %s profile %d available", t->path, t->profile);
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ return r;
+
+fail:
+ pa_log("org.bluez.MediaEndpoint.SetConfiguration: invalid arguments");
+ pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
+ "Unable to set configuration")));
+ return r;
+}
+
+static DBusMessage *endpoint_clear_configuration(DBusConnection *c, DBusMessage *m, void *userdata) {
+ pa_bluetooth_discovery *y = userdata;
+ pa_bluetooth_device *d;
+ pa_bluetooth_transport *t;
+ void *state = NULL;
+ DBusMessage *r;
+ DBusError e;
+ const char *path;
+
+ dbus_error_init(&e);
+
+ if (!dbus_message_get_args(m, &e, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ pa_log("org.bluez.MediaEndpoint.ClearConfiguration: %s", e.message);
+ dbus_error_free(&e);
+ goto fail;
+ }
+
+ while ((d = pa_hashmap_iterate(y->devices, &state, NULL))) {
+ if ((t = pa_hashmap_get(d->transports, path))) {
+ pa_log_debug("Clearing transport %s profile %d", t->path, t->profile);
+ pa_hashmap_remove(d->transports, t->path);
+ transport_free(t);
+ break;
+ }
+ }
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ return r;
+
+fail:
+ pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
+ "Unable to clear configuration")));
+ return r;
+}
+
+static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
+
+ switch (freq) {
+ case BT_SBC_SAMPLING_FREQ_16000:
+ case BT_SBC_SAMPLING_FREQ_32000:
+ return 53;
+
+ case BT_SBC_SAMPLING_FREQ_44100:
+
+ switch (mode) {
+ case BT_A2DP_CHANNEL_MODE_MONO:
+ case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ return 31;
+
+ case BT_A2DP_CHANNEL_MODE_STEREO:
+ case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ return 53;
+
+ default:
+ pa_log_warn("Invalid channel mode %u", mode);
+ return 53;
+ }
+
+ case BT_SBC_SAMPLING_FREQ_48000:
+
+ switch (mode) {
+ case BT_A2DP_CHANNEL_MODE_MONO:
+ case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ return 29;
+
+ case BT_A2DP_CHANNEL_MODE_STEREO:
+ case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ return 51;
+
+ default:
+ pa_log_warn("Invalid channel mode %u", mode);
+ return 51;
+ }
+
+ default:
+ pa_log_warn("Invalid sampling freq %u", freq);
+ return 53;
+ }
+}
+
+static DBusMessage *endpoint_select_configuration(DBusConnection *c, DBusMessage *m, void *userdata) {
+ pa_bluetooth_discovery *y = userdata;
+ a2dp_sbc_t *cap, config;
+ uint8_t *pconf = (uint8_t *) &config;
+ int i, size;
+ DBusMessage *r;
+ DBusError e;
+
+ static const struct {
+ uint32_t rate;
+ uint8_t cap;
+ } freq_table[] = {
+ { 16000U, BT_SBC_SAMPLING_FREQ_16000 },
+ { 32000U, BT_SBC_SAMPLING_FREQ_32000 },
+ { 44100U, BT_SBC_SAMPLING_FREQ_44100 },
+ { 48000U, BT_SBC_SAMPLING_FREQ_48000 }
+ };
+
+ dbus_error_init(&e);
+
+ if (!dbus_message_get_args(m, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &cap, &size, DBUS_TYPE_INVALID)) {
+ pa_log("org.bluez.MediaEndpoint.SelectConfiguration: %s", e.message);
+ dbus_error_free(&e);
+ goto fail;
+ }
+
+ if (dbus_message_has_path(m, HFP_AG_ENDPOINT))
+ goto done;
+
+ pa_assert(size == sizeof(config));
+
+ memset(&config, 0, sizeof(config));
+
+ /* Find the lowest freq that is at least as high as the requested
+ * sampling rate */
+ for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
+ if (freq_table[i].rate >= y->core->default_sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
+ config.frequency = freq_table[i].cap;
+ break;
+ }
+
+ if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
+ for (--i; i >= 0; i--) {
+ if (cap->frequency & freq_table[i].cap) {
+ config.frequency = freq_table[i].cap;
+ break;
+ }
+ }
+
+ if (i < 0) {
+ pa_log("Not suitable sample rate");
+ goto fail;
+ }
+ }
+
+ pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
+
+ if (y->core->default_sample_spec.channels <= 1) {
+ if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
+ }
+
+ if (y->core->default_sample_spec.channels >= 2) {
+ if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
+ else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
+ else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
+ else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
+ config.channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
+ } else {
+ pa_log("No supported channel modes");
+ goto fail;
+ }
+ }
+
+ if (cap->block_length & BT_A2DP_BLOCK_LENGTH_16)
+ config.block_length = BT_A2DP_BLOCK_LENGTH_16;
+ else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_12)
+ config.block_length = BT_A2DP_BLOCK_LENGTH_12;
+ else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_8)
+ config.block_length = BT_A2DP_BLOCK_LENGTH_8;
+ else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_4)
+ config.block_length = BT_A2DP_BLOCK_LENGTH_4;
+ else {
+ pa_log_error("No supported block lengths");
+ goto fail;
+ }
+
+ if (cap->subbands & BT_A2DP_SUBBANDS_8)
+ config.subbands = BT_A2DP_SUBBANDS_8;
+ else if (cap->subbands & BT_A2DP_SUBBANDS_4)
+ config.subbands = BT_A2DP_SUBBANDS_4;
+ else {
+ pa_log_error("No supported subbands");
+ goto fail;
+ }
+
+ if (cap->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS)
+ config.allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
+ else if (cap->allocation_method & BT_A2DP_ALLOCATION_SNR)
+ config.allocation_method = BT_A2DP_ALLOCATION_SNR;
+
+ config.min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
+ config.max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(config.frequency, config.channel_mode), cap->max_bitpool);
+
+done:
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ pa_assert_se(dbus_message_append_args(
+ r,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pconf, size,
+ DBUS_TYPE_INVALID));
+
+ return r;
+
+fail:
+ pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
+ "Unable to select configuration")));
+ return r;
+}
+
+static DBusHandlerResult endpoint_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
+ struct pa_bluetooth_discovery *y = userdata;
+ DBusMessage *r = NULL;
+ DBusError e;
+ const char *path;
+
+ pa_assert(y);
+
+ pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
+ dbus_message_get_interface(m),
+ dbus_message_get_path(m),
+ dbus_message_get_member(m));
+
+ path = dbus_message_get_path(m);
+ dbus_error_init(&e);
+
+ if (!pa_streq(path, A2DP_SOURCE_ENDPOINT) && !pa_streq(path, A2DP_SINK_ENDPOINT) && !pa_streq(path, HFP_AG_ENDPOINT))
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
+ const char *xml = ENDPOINT_INTROSPECT_XML;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ pa_assert_se(dbus_message_append_args(
+ r,
+ DBUS_TYPE_STRING, &xml,
+ DBUS_TYPE_INVALID));
+
+ } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "SetConfiguration")) {
+ r = endpoint_set_configuration(c, m, userdata);
+ } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "SelectConfiguration")) {
+ r = endpoint_select_configuration(c, m, userdata);
+ } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "ClearConfiguration"))
+ r = endpoint_clear_configuration(c, m, userdata);
+ else
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ if (r) {
+ pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
+ dbus_message_unref(r);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+#endif /* DBUS_TYPE_UNIX_FD */
+
+pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
+ DBusError err;
+ pa_bluetooth_discovery *y;
+#ifdef DBUS_TYPE_UNIX_FD
+ static const DBusObjectPathVTable vtable_endpoint = {
+ .message_function = endpoint_handler,
+ };
+#endif
+
+ pa_assert(c);
+
+ dbus_error_init(&err);
+
+ if ((y = pa_shared_get(c, "bluetooth-discovery")))
+ return pa_bluetooth_discovery_ref(y);
+
+ y = pa_xnew0(pa_bluetooth_discovery, 1);
+ PA_REFCNT_INIT(y);
+ y->core = c;
+ y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
+ pa_hook_init(&y->hook, y);
+ pa_shared_set(c, "bluetooth-discovery", y);
+
+ if (setup_dbus(y) < 0)
+ goto fail;
+
+ /* dynamic detection of bluetooth audio devices */
+ if (!dbus_connection_add_filter(pa_dbus_connection_get(y->connection), filter_cb, y, NULL)) {
+ pa_log_error("Failed to add filter function");
+ goto fail;
+ }
+ y->filter_added = TRUE;
+
+ if (pa_dbus_add_matches(
+ pa_dbus_connection_get(y->connection), &err,
+ "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
+ NULL) < 0) {
+ pa_log("Failed to add D-Bus matches: %s", err.message);
+ goto fail;
+ }
+
+#ifdef DBUS_TYPE_UNIX_FD
+ pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), HFP_AG_ENDPOINT, &vtable_endpoint, y));
+ pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT, &vtable_endpoint, y));
+ pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT, &vtable_endpoint, y));
+#endif
+
+ list_adapters(y);
+
+ return y;
+
+fail:
+
+ if (y)
+ pa_bluetooth_discovery_unref(y);
+
+ dbus_error_free(&err);
+
+ return NULL;
+}
+
+pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
+ pa_assert(y);
+ pa_assert(PA_REFCNT_VALUE(y) > 0);
+
+ PA_REFCNT_INC(y);
+
+ return y;
+}
+
+void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
+ pa_assert(y);
+ pa_assert(PA_REFCNT_VALUE(y) > 0);
+
+ if (PA_REFCNT_DEC(y) > 0)
+ return;
+
+ pa_dbus_free_pending_list(&y->pending);
+
+ if (y->devices) {
+ remove_all_devices(y);
+ pa_hashmap_free(y->devices, NULL, NULL);
+ }
+
+ if (y->connection) {
+#ifdef DBUS_TYPE_UNIX_FD
+ dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), HFP_AG_ENDPOINT);
+ dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT);
+ dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT);
+#endif
+ pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
+ "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
+ NULL);
+
+ if (y->filter_added)
+ dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
+
+ pa_dbus_connection_unref(y->connection);
+ }
+
+ pa_hook_done(&y->hook);
+
+ if (y->core)
+ pa_shared_remove(y->core, "bluetooth-discovery");
+
+ pa_xfree(y);
+}
+
+void pa_bluetooth_discovery_sync(pa_bluetooth_discovery *y) {
+ pa_assert(y);
+ pa_assert(PA_REFCNT_VALUE(y) > 0);
+
+ pa_dbus_sync_pending_list(&y->pending);
+}
+
+pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y) {
+ pa_assert(y);
+ pa_assert(PA_REFCNT_VALUE(y) > 0);
+
+ return &y->hook;
+}
+
+const char*pa_bluetooth_get_form_factor(uint32_t class) {
+ unsigned i;
+ const char *r;
+
+ static const char * const table[] = {
+ [1] = "headset",
+ [2] = "hands-free",
+ [4] = "microphone",
+ [5] = "speaker",
+ [6] = "headphone",
+ [7] = "portable",
+ [8] = "car",
+ [10] = "hifi"
+ };
+
+ if (((class >> 8) & 31) != 4)
+ return NULL;
+
+ if ((i = (class >> 2) & 63) > PA_ELEMENTSOF(table))
+ r = NULL;
+ else
+ r = table[i];
+
+ if (!r)
+ pa_log_debug("Unknown Bluetooth minor device class %u", i);
+
+ return r;
+}
+
+char *pa_bluetooth_cleanup_name(const char *name) {
+ char *t, *s, *d;
+ pa_bool_t space = FALSE;
+
+ pa_assert(name);
+
+ while ((*name >= 1 && *name <= 32) || *name >= 127)
+ name++;
+
+ t = pa_xstrdup(name);
+
+ for (s = d = t; *s; s++) {
+
+ if (*s <= 32 || *s >= 127 || *s == '_') {
+ space = TRUE;
+ continue;
+ }
+
+ if (space) {
+ *(d++) = ' ';
+ space = FALSE;
+ }
+
+ *(d++) = *s;
+ }
+
+ *d = 0;
+
+ return t;
+}
+
+pa_bool_t pa_bluetooth_uuid_has(pa_bluetooth_uuid *uuids, const char *uuid) {
+ pa_assert(uuid);
+
+ while (uuids) {
+ if (strcasecmp(uuids->uuid, uuid) == 0)
+ return TRUE;
+
+ uuids = uuids->next;
+ }
+
+ return FALSE;
+}
diff --git a/src/modules/bluetooth/bluetooth-util.h b/src/modules/bluetooth/bluetooth-util.h
new file mode 100644
index 00000000..248ca47d
--- /dev/null
+++ b/src/modules/bluetooth/bluetooth-util.h
@@ -0,0 +1,142 @@
+#ifndef foobluetoothutilhfoo
+#define foobluetoothutilhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008-2009 Joao Paulo Rechi Vita
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <dbus/dbus.h>
+
+#include <pulsecore/llist.h>
+#include <pulsecore/macro.h>
+
+#define PA_BLUETOOTH_ERROR_NOT_SUPPORTED "org.bluez.Error.NotSupported"
+
+/* UUID copied from bluez/audio/device.h */
+#define GENERIC_AUDIO_UUID "00001203-0000-1000-8000-00805f9b34fb"
+
+#define HSP_HS_UUID "00001108-0000-1000-8000-00805f9b34fb"
+#define HSP_AG_UUID "00001112-0000-1000-8000-00805f9b34fb"
+
+#define HFP_HS_UUID "0000111e-0000-1000-8000-00805f9b34fb"
+#define HFP_AG_UUID "0000111f-0000-1000-8000-00805f9b34fb"
+
+#define ADVANCED_AUDIO_UUID "0000110d-0000-1000-8000-00805f9b34fb"
+
+#define A2DP_SOURCE_UUID "0000110a-0000-1000-8000-00805f9b34fb"
+#define A2DP_SINK_UUID "0000110b-0000-1000-8000-00805f9b34fb"
+
+typedef struct pa_bluetooth_uuid pa_bluetooth_uuid;
+typedef struct pa_bluetooth_device pa_bluetooth_device;
+typedef struct pa_bluetooth_discovery pa_bluetooth_discovery;
+typedef struct pa_bluetooth_transport pa_bluetooth_transport;
+
+struct userdata;
+
+struct pa_bluetooth_uuid {
+ char *uuid;
+ PA_LLIST_FIELDS(pa_bluetooth_uuid);
+};
+
+enum profile {
+ PROFILE_A2DP,
+ PROFILE_A2DP_SOURCE,
+ PROFILE_HSP,
+ PROFILE_HFGW,
+ PROFILE_OFF
+};
+
+struct pa_bluetooth_transport {
+ pa_bluetooth_discovery *y;
+ char *path;
+ enum profile profile;
+ uint8_t codec;
+ uint8_t *config;
+ int config_size;
+ pa_bool_t nrec;
+};
+
+/* This enum is shared among Audio, Headset, AudioSink, and AudioSource, although not all values are acceptable in all profiles */
+typedef enum pa_bt_audio_state {
+ PA_BT_AUDIO_STATE_INVALID = -1,
+ PA_BT_AUDIO_STATE_DISCONNECTED,
+ PA_BT_AUDIO_STATE_CONNECTING,
+ PA_BT_AUDIO_STATE_CONNECTED,
+ PA_BT_AUDIO_STATE_PLAYING
+} pa_bt_audio_state_t;
+
+struct pa_bluetooth_device {
+ pa_bool_t dead;
+
+ int device_info_valid; /* 0: no results yet; 1: good results; -1: bad results ... */
+
+ /* Device information */
+ char *name;
+ char *path;
+ pa_hashmap *transports;
+ int paired;
+ char *alias;
+ int device_connected;
+ PA_LLIST_HEAD(pa_bluetooth_uuid, uuids);
+ char *address;
+ int class;
+ int trusted;
+
+ /* Audio state */
+ pa_bt_audio_state_t audio_state;
+
+ /* AudioSink state */
+ pa_bt_audio_state_t audio_sink_state;
+
+ /* AudioSource state */
+ pa_bt_audio_state_t audio_source_state;
+
+ /* Headset state */
+ pa_bt_audio_state_t headset_state;
+
+ /* HandsfreeGateway state */
+ pa_bt_audio_state_t hfgw_state;
+};
+
+pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *core);
+pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y);
+void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *d);
+
+void pa_bluetooth_discovery_sync(pa_bluetooth_discovery *d);
+
+const pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery *d, const char* path);
+const pa_bluetooth_device* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery *d, const char* address);
+
+const pa_bluetooth_transport* pa_bluetooth_discovery_get_transport(pa_bluetooth_discovery *y, const char *path);
+const pa_bluetooth_transport* pa_bluetooth_device_get_transport(const pa_bluetooth_device *d, enum profile profile);
+
+int pa_bluetooth_transport_acquire(const pa_bluetooth_transport *t, const char *accesstype, size_t *imtu, size_t *omtu);
+void pa_bluetooth_transport_release(const pa_bluetooth_transport *t, const char *accesstype);
+int pa_bluetooth_transport_parse_property(pa_bluetooth_transport *t, DBusMessageIter *i);
+
+pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *d);
+
+const char* pa_bluetooth_get_form_factor(uint32_t class);
+
+char *pa_bluetooth_cleanup_name(const char *name);
+
+pa_bool_t pa_bluetooth_uuid_has(pa_bluetooth_uuid *uuids, const char *uuid);
+
+#endif
diff --git a/src/modules/bluetooth/ipc.c b/src/modules/bluetooth/ipc.c
new file mode 100644
index 00000000..1bdad784
--- /dev/null
+++ b/src/modules/bluetooth/ipc.c
@@ -0,0 +1,133 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include "ipc.h"
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+/* This table contains the string representation for messages types */
+static const char *strtypes[] = {
+ "BT_REQUEST",
+ "BT_RESPONSE",
+ "BT_INDICATION",
+ "BT_ERROR",
+};
+
+/* This table contains the string representation for messages names */
+static const char *strnames[] = {
+ "BT_GET_CAPABILITIES",
+ "BT_OPEN",
+ "BT_SET_CONFIGURATION",
+ "BT_NEW_STREAM",
+ "BT_START_STREAM",
+ "BT_STOP_STREAM",
+ "BT_SUSPEND_STREAM",
+ "BT_RESUME_STREAM",
+ "BT_CONTROL",
+};
+
+int bt_audio_service_open(void)
+{
+ int sk;
+ int err;
+ struct sockaddr_un addr = {
+ AF_UNIX, BT_IPC_SOCKET_NAME
+ };
+
+ sk = socket(PF_LOCAL, SOCK_STREAM, 0);
+ if (sk < 0) {
+ err = errno;
+ fprintf(stderr, "%s: Cannot open socket: %s (%d)\n",
+ __FUNCTION__, strerror(err), err);
+ errno = err;
+ return -1;
+ }
+
+ if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ err = errno;
+ fprintf(stderr, "%s: connect() failed: %s (%d)\n",
+ __FUNCTION__, strerror(err), err);
+ close(sk);
+ errno = err;
+ return -1;
+ }
+
+ return sk;
+}
+
+int bt_audio_service_close(int sk)
+{
+ return close(sk);
+}
+
+int bt_audio_service_get_data_fd(int sk)
+{
+ char cmsg_b[CMSG_SPACE(sizeof(int))], m;
+ int err, ret;
+ struct iovec iov = { &m, sizeof(m) };
+ struct msghdr msgh;
+ struct cmsghdr *cmsg;
+
+ memset(&msgh, 0, sizeof(msgh));
+ msgh.msg_iov = &iov;
+ msgh.msg_iovlen = 1;
+ msgh.msg_control = &cmsg_b;
+ msgh.msg_controllen = CMSG_LEN(sizeof(int));
+
+ ret = recvmsg(sk, &msgh, 0);
+ if (ret < 0) {
+ err = errno;
+ fprintf(stderr, "%s: Unable to receive fd: %s (%d)\n",
+ __FUNCTION__, strerror(err), err);
+ errno = err;
+ return -1;
+ }
+
+ /* Receive auxiliary data in msgh */
+ for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
+ cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
+ if (cmsg->cmsg_level == SOL_SOCKET
+ && cmsg->cmsg_type == SCM_RIGHTS) {
+ memcpy(&ret, CMSG_DATA(cmsg), sizeof(int));
+ return ret;
+ }
+ }
+
+ errno = EINVAL;
+ return -1;
+}
+
+const char *bt_audio_strtype(uint8_t type)
+{
+ if (type >= ARRAY_SIZE(strtypes))
+ return NULL;
+
+ return strtypes[type];
+}
+
+const char *bt_audio_strname(uint8_t name)
+{
+ if (name >= ARRAY_SIZE(strnames))
+ return NULL;
+
+ return strnames[name];
+}
diff --git a/src/modules/bluetooth/ipc.h b/src/modules/bluetooth/ipc.h
new file mode 100644
index 00000000..d69b97e4
--- /dev/null
+++ b/src/modules/bluetooth/ipc.h
@@ -0,0 +1,360 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+/*
+ Message sequence chart of streaming sequence for A2DP transport
+
+ Audio daemon User
+ on snd_pcm_open
+ <--BT_GET_CAPABILITIES_REQ
+
+ BT_GET_CAPABILITIES_RSP-->
+
+ on snd_pcm_hw_params
+ <--BT_SETCONFIGURATION_REQ
+
+ BT_SET_CONFIGURATION_RSP-->
+
+ on snd_pcm_prepare
+ <--BT_START_STREAM_REQ
+
+ <Moves to streaming state>
+ BT_START_STREAM_RSP-->
+
+ BT_NEW_STREAM_IND -->
+
+ < streams data >
+ ..........
+
+ on snd_pcm_drop/snd_pcm_drain
+
+ <--BT_STOP_STREAM_REQ
+
+ <Moves to open state>
+ BT_STOP_STREAM_RSP-->
+
+ on IPC close or appl crash
+ <Moves to idle>
+
+ */
+
+#ifndef BT_AUDIOCLIENT_H
+#define BT_AUDIOCLIENT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <errno.h>
+
+#define BT_SUGGESTED_BUFFER_SIZE 512
+#define BT_IPC_SOCKET_NAME "\0/org/bluez/audio"
+
+/* Generic message header definition, except for RESPONSE messages */
+typedef struct {
+ uint8_t type;
+ uint8_t name;
+ uint16_t length;
+} __attribute__ ((packed)) bt_audio_msg_header_t;
+
+typedef struct {
+ bt_audio_msg_header_t h;
+ uint8_t posix_errno;
+} __attribute__ ((packed)) bt_audio_error_t;
+
+/* Message types */
+#define BT_REQUEST 0
+#define BT_RESPONSE 1
+#define BT_INDICATION 2
+#define BT_ERROR 3
+
+/* Messages names */
+#define BT_GET_CAPABILITIES 0
+#define BT_OPEN 1
+#define BT_SET_CONFIGURATION 2
+#define BT_NEW_STREAM 3
+#define BT_START_STREAM 4
+#define BT_STOP_STREAM 5
+#define BT_CLOSE 6
+#define BT_CONTROL 7
+#define BT_DELAY_REPORT 8
+
+#define BT_CAPABILITIES_TRANSPORT_A2DP 0
+#define BT_CAPABILITIES_TRANSPORT_SCO 1
+#define BT_CAPABILITIES_TRANSPORT_ANY 2
+
+#define BT_CAPABILITIES_ACCESS_MODE_READ 1
+#define BT_CAPABILITIES_ACCESS_MODE_WRITE 2
+#define BT_CAPABILITIES_ACCESS_MODE_READWRITE 3
+
+#define BT_FLAG_AUTOCONNECT 1
+
+struct bt_get_capabilities_req {
+ bt_audio_msg_header_t h;
+ char source[18]; /* Address of the local Device */
+ char destination[18];/* Address of the remote Device */
+ char object[128]; /* DBus object path */
+ uint8_t transport; /* Requested transport */
+ uint8_t flags; /* Requested flags */
+ uint8_t seid; /* Requested capability configuration */
+} __attribute__ ((packed));
+
+/**
+ * SBC Codec parameters as per A2DP profile 1.0 § 4.3
+ */
+
+/* A2DP seid are 6 bytes long so HSP/HFP are assigned to 7-8 bits */
+#define BT_A2DP_SEID_RANGE (1 << 6) - 1
+
+#define BT_A2DP_SBC_SOURCE 0x00
+#define BT_A2DP_SBC_SINK 0x01
+#define BT_A2DP_MPEG12_SOURCE 0x02
+#define BT_A2DP_MPEG12_SINK 0x03
+#define BT_A2DP_MPEG24_SOURCE 0x04
+#define BT_A2DP_MPEG24_SINK 0x05
+#define BT_A2DP_ATRAC_SOURCE 0x06
+#define BT_A2DP_ATRAC_SINK 0x07
+#define BT_A2DP_UNKNOWN_SOURCE 0x08
+#define BT_A2DP_UNKNOWN_SINK 0x09
+
+#define BT_SBC_SAMPLING_FREQ_16000 (1 << 3)
+#define BT_SBC_SAMPLING_FREQ_32000 (1 << 2)
+#define BT_SBC_SAMPLING_FREQ_44100 (1 << 1)
+#define BT_SBC_SAMPLING_FREQ_48000 1
+
+#define BT_A2DP_CHANNEL_MODE_MONO (1 << 3)
+#define BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL (1 << 2)
+#define BT_A2DP_CHANNEL_MODE_STEREO (1 << 1)
+#define BT_A2DP_CHANNEL_MODE_JOINT_STEREO 1
+
+#define BT_A2DP_BLOCK_LENGTH_4 (1 << 3)
+#define BT_A2DP_BLOCK_LENGTH_8 (1 << 2)
+#define BT_A2DP_BLOCK_LENGTH_12 (1 << 1)
+#define BT_A2DP_BLOCK_LENGTH_16 1
+
+#define BT_A2DP_SUBBANDS_4 (1 << 1)
+#define BT_A2DP_SUBBANDS_8 1
+
+#define BT_A2DP_ALLOCATION_SNR (1 << 1)
+#define BT_A2DP_ALLOCATION_LOUDNESS 1
+
+#define BT_MPEG_SAMPLING_FREQ_16000 (1 << 5)
+#define BT_MPEG_SAMPLING_FREQ_22050 (1 << 4)
+#define BT_MPEG_SAMPLING_FREQ_24000 (1 << 3)
+#define BT_MPEG_SAMPLING_FREQ_32000 (1 << 2)
+#define BT_MPEG_SAMPLING_FREQ_44100 (1 << 1)
+#define BT_MPEG_SAMPLING_FREQ_48000 1
+
+#define BT_MPEG_LAYER_1 (1 << 2)
+#define BT_MPEG_LAYER_2 (1 << 1)
+#define BT_MPEG_LAYER_3 1
+
+#define BT_HFP_CODEC_PCM 0x00
+
+#define BT_PCM_FLAG_NREC 0x01
+#define BT_PCM_FLAG_PCM_ROUTING 0x02
+
+#define BT_WRITE_LOCK (1 << 1)
+#define BT_READ_LOCK 1
+
+typedef struct {
+ uint8_t seid;
+ uint8_t transport;
+ uint8_t type;
+ uint8_t length;
+ uint8_t configured;
+ uint8_t lock;
+ uint8_t data[0];
+} __attribute__ ((packed)) codec_capabilities_t;
+
+typedef struct {
+ codec_capabilities_t capability;
+ uint8_t channel_mode;
+ uint8_t frequency;
+ uint8_t allocation_method;
+ uint8_t subbands;
+ uint8_t block_length;
+ uint8_t min_bitpool;
+ uint8_t max_bitpool;
+} __attribute__ ((packed)) sbc_capabilities_t;
+
+typedef struct {
+ codec_capabilities_t capability;
+ uint8_t channel_mode;
+ uint8_t crc;
+ uint8_t layer;
+ uint8_t frequency;
+ uint8_t mpf;
+ uint16_t bitrate;
+} __attribute__ ((packed)) mpeg_capabilities_t;
+
+typedef struct {
+ codec_capabilities_t capability;
+ uint8_t flags;
+ uint16_t sampling_rate;
+} __attribute__ ((packed)) pcm_capabilities_t;
+
+struct bt_get_capabilities_rsp {
+ bt_audio_msg_header_t h;
+ char source[18]; /* Address of the local Device */
+ char destination[18];/* Address of the remote Device */
+ char object[128]; /* DBus object path */
+ uint8_t data[0]; /* First codec_capabilities_t */
+} __attribute__ ((packed));
+
+struct bt_open_req {
+ bt_audio_msg_header_t h;
+ char source[18]; /* Address of the local Device */
+ char destination[18];/* Address of the remote Device */
+ char object[128]; /* DBus object path */
+ uint8_t seid; /* Requested capability configuration to lock */
+ uint8_t lock; /* Requested lock */
+} __attribute__ ((packed));
+
+struct bt_open_rsp {
+ bt_audio_msg_header_t h;
+ char source[18]; /* Address of the local Device */
+ char destination[18];/* Address of the remote Device */
+ char object[128]; /* DBus object path */
+} __attribute__ ((packed));
+
+struct bt_set_configuration_req {
+ bt_audio_msg_header_t h;
+ codec_capabilities_t codec; /* Requested codec */
+} __attribute__ ((packed));
+
+struct bt_set_configuration_rsp {
+ bt_audio_msg_header_t h;
+ uint16_t link_mtu; /* Max length that transport supports */
+} __attribute__ ((packed));
+
+#define BT_STREAM_ACCESS_READ 0
+#define BT_STREAM_ACCESS_WRITE 1
+#define BT_STREAM_ACCESS_READWRITE 2
+struct bt_start_stream_req {
+ bt_audio_msg_header_t h;
+} __attribute__ ((packed));
+
+struct bt_start_stream_rsp {
+ bt_audio_msg_header_t h;
+} __attribute__ ((packed));
+
+/* This message is followed by one byte of data containing the stream data fd
+ as ancilliary data */
+struct bt_new_stream_ind {
+ bt_audio_msg_header_t h;
+} __attribute__ ((packed));
+
+struct bt_stop_stream_req {
+ bt_audio_msg_header_t h;
+} __attribute__ ((packed));
+
+struct bt_stop_stream_rsp {
+ bt_audio_msg_header_t h;
+} __attribute__ ((packed));
+
+struct bt_close_req {
+ bt_audio_msg_header_t h;
+} __attribute__ ((packed));
+
+struct bt_close_rsp {
+ bt_audio_msg_header_t h;
+} __attribute__ ((packed));
+
+struct bt_suspend_stream_ind {
+ bt_audio_msg_header_t h;
+} __attribute__ ((packed));
+
+struct bt_resume_stream_ind {
+ bt_audio_msg_header_t h;
+} __attribute__ ((packed));
+
+#define BT_CONTROL_KEY_POWER 0x40
+#define BT_CONTROL_KEY_VOL_UP 0x41
+#define BT_CONTROL_KEY_VOL_DOWN 0x42
+#define BT_CONTROL_KEY_MUTE 0x43
+#define BT_CONTROL_KEY_PLAY 0x44
+#define BT_CONTROL_KEY_STOP 0x45
+#define BT_CONTROL_KEY_PAUSE 0x46
+#define BT_CONTROL_KEY_RECORD 0x47
+#define BT_CONTROL_KEY_REWIND 0x48
+#define BT_CONTROL_KEY_FAST_FORWARD 0x49
+#define BT_CONTROL_KEY_EJECT 0x4A
+#define BT_CONTROL_KEY_FORWARD 0x4B
+#define BT_CONTROL_KEY_BACKWARD 0x4C
+
+struct bt_control_req {
+ bt_audio_msg_header_t h;
+ uint8_t mode; /* Control Mode */
+ uint8_t key; /* Control Key */
+} __attribute__ ((packed));
+
+struct bt_control_rsp {
+ bt_audio_msg_header_t h;
+ uint8_t mode; /* Control Mode */
+ uint8_t key; /* Control Key */
+} __attribute__ ((packed));
+
+struct bt_control_ind {
+ bt_audio_msg_header_t h;
+ uint8_t mode; /* Control Mode */
+ uint8_t key; /* Control Key */
+} __attribute__ ((packed));
+
+struct bt_delay_report_req {
+ bt_audio_msg_header_t h;
+ uint16_t delay;
+} __attribute__ ((packed));
+
+struct bt_delay_report_ind {
+ bt_audio_msg_header_t h;
+ uint16_t delay;
+} __attribute__ ((packed));
+
+/* Function declaration */
+
+/* Opens a connection to the audio service: return a socket descriptor */
+int bt_audio_service_open(void);
+
+/* Closes a connection to the audio service */
+int bt_audio_service_close(int sk);
+
+/* Receives stream data file descriptor : must be called after a
+BT_STREAMFD_IND message is returned */
+int bt_audio_service_get_data_fd(int sk);
+
+/* Human readable message type string */
+const char *bt_audio_strtype(uint8_t type);
+
+/* Human readable message name string */
+const char *bt_audio_strname(uint8_t name);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BT_AUDIOCLIENT_H */
diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c
new file mode 100644
index 00000000..288ad2fd
--- /dev/null
+++ b/src/modules/bluetooth/module-bluetooth-device.c
@@ -0,0 +1,3006 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008-2009 Joao Paulo Rechi Vita
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+#include <errno.h>
+#include <linux/sockios.h>
+#include <arpa/inet.h>
+
+#include <pulse/i18n.h>
+#include <pulse/rtclock.h>
+#include <pulse/sample.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/core-rtclock.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/shared.h>
+#include <pulsecore/socket-util.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/poll.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/time-smoother.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/dbus-shared.h>
+
+#include "module-bluetooth-device-symdef.h"
+#include "ipc.h"
+#include "sbc.h"
+#include "a2dp-codecs.h"
+#include "rtp.h"
+#include "bluetooth-util.h"
+
+#define BITPOOL_DEC_LIMIT 32
+#define BITPOOL_DEC_STEP 5
+#define HSP_MAX_GAIN 15
+
+PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
+PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "name=<name for the card/sink/source, to be prefixed> "
+ "card_name=<name for the card> "
+ "card_properties=<properties for the card> "
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "source_name=<name for the source> "
+ "source_properties=<properties for the source> "
+ "address=<address of the device> "
+ "profile=<a2dp|hsp|hfgw> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "path=<device object path> "
+ "auto_connect=<automatically connect?> "
+ "sco_sink=<SCO over PCM sink name> "
+ "sco_source=<SCO over PCM source name>");
+
+/* TODO: not close fd when entering suspend mode in a2dp */
+
+static const char* const valid_modargs[] = {
+ "name",
+ "card_name",
+ "card_properties",
+ "sink_name",
+ "sink_properties",
+ "source_name",
+ "source_properties",
+ "address",
+ "profile",
+ "rate",
+ "channels",
+ "path",
+ "auto_connect",
+ "sco_sink",
+ "sco_source",
+ NULL
+};
+
+struct a2dp_info {
+ sbc_capabilities_t sbc_capabilities;
+ sbc_t sbc; /* Codec data */
+ pa_bool_t sbc_initialized; /* Keep track if the encoder is initialized */
+ size_t codesize, frame_length; /* SBC Codesize, frame_length. We simply cache those values here */
+
+ void* buffer; /* Codec transfer buffer */
+ size_t buffer_size; /* Size of the buffer */
+
+ uint16_t seq_num; /* Cumulative packet sequence */
+ uint8_t min_bitpool;
+ uint8_t max_bitpool;
+};
+
+struct hsp_info {
+ pcm_capabilities_t pcm_capabilities;
+ pa_sink *sco_sink;
+ void (*sco_sink_set_volume)(pa_sink *s);
+ pa_source *sco_source;
+ void (*sco_source_set_volume)(pa_source *s);
+ pa_hook_slot *sink_state_changed_slot;
+ pa_hook_slot *source_state_changed_slot;
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+
+ char *address;
+ char *path;
+ char *transport;
+ char *accesstype;
+
+ pa_bluetooth_discovery *discovery;
+ pa_bool_t auto_connect;
+
+ pa_dbus_connection *connection;
+
+ pa_card *card;
+ pa_sink *sink;
+ pa_source *source;
+
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+ pa_rtpoll_item *rtpoll_item;
+ pa_thread *thread;
+
+ uint64_t read_index, write_index;
+ pa_usec_t started_at;
+ pa_smoother *read_smoother;
+
+ pa_memchunk write_memchunk;
+
+ pa_sample_spec sample_spec, requested_sample_spec;
+
+ int service_fd;
+ int stream_fd;
+
+ size_t link_mtu;
+ size_t block_size;
+
+ struct a2dp_info a2dp;
+ struct hsp_info hsp;
+
+ enum profile profile;
+
+ pa_modargs *modargs;
+
+ int stream_write_type;
+ int service_write_type, service_read_type;
+
+ pa_bool_t filter_added;
+};
+
+#define FIXED_LATENCY_PLAYBACK_A2DP (25*PA_USEC_PER_MSEC)
+#define FIXED_LATENCY_RECORD_A2DP (25*PA_USEC_PER_MSEC)
+#define FIXED_LATENCY_PLAYBACK_HSP (125*PA_USEC_PER_MSEC)
+#define FIXED_LATENCY_RECORD_HSP (25*PA_USEC_PER_MSEC)
+
+#define MAX_PLAYBACK_CATCH_UP_USEC (100*PA_USEC_PER_MSEC)
+
+#define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source))
+
+static int init_bt(struct userdata *u);
+static int init_profile(struct userdata *u);
+
+static int service_send(struct userdata *u, const bt_audio_msg_header_t *msg) {
+ ssize_t r;
+
+ pa_assert(u);
+ pa_assert(msg);
+ pa_assert(msg->length > 0);
+
+ if (u->service_fd < 0) {
+ pa_log_warn("Service not connected");
+ return -1;
+ }
+
+ pa_log_debug("Sending %s -> %s",
+ pa_strnull(bt_audio_strtype(msg->type)),
+ pa_strnull(bt_audio_strname(msg->name)));
+
+ if ((r = pa_loop_write(u->service_fd, msg, msg->length, &u->service_write_type)) == (ssize_t) msg->length)
+ return 0;
+
+ if (r < 0)
+ pa_log_error("Error sending data to audio service: %s", pa_cstrerror(errno));
+ else
+ pa_log_error("Short write()");
+
+ return -1;
+}
+
+static int service_recv(struct userdata *u, bt_audio_msg_header_t *msg, size_t room) {
+ ssize_t r;
+
+ pa_assert(u);
+ pa_assert(u->service_fd >= 0);
+ pa_assert(msg);
+ pa_assert(room >= sizeof(*msg));
+
+ pa_log_debug("Trying to receive message from audio service...");
+
+ /* First, read the header */
+ if ((r = pa_loop_read(u->service_fd, msg, sizeof(*msg), &u->service_read_type)) != sizeof(*msg))
+ goto read_fail;
+
+ if (msg->length < sizeof(*msg)) {
+ pa_log_error("Invalid message size.");
+ return -1;
+ }
+
+ if (msg->length > room) {
+ pa_log_error("Not enough room.");
+ return -1;
+ }
+
+ /* Secondly, read the payload */
+ if (msg->length > sizeof(*msg)) {
+
+ size_t remains = msg->length - sizeof(*msg);
+
+ if ((r = pa_loop_read(u->service_fd,
+ (uint8_t*) msg + sizeof(*msg),
+ remains,
+ &u->service_read_type)) != (ssize_t) remains)
+ goto read_fail;
+ }
+
+ pa_log_debug("Received %s <- %s",
+ pa_strnull(bt_audio_strtype(msg->type)),
+ pa_strnull(bt_audio_strname(msg->name)));
+
+ return 0;
+
+read_fail:
+
+ if (r < 0)
+ pa_log_error("Error receiving data from audio service: %s", pa_cstrerror(errno));
+ else
+ pa_log_error("Short read()");
+
+ return -1;
+}
+
+static ssize_t service_expect(struct userdata*u, bt_audio_msg_header_t *rsp, size_t room, uint8_t expected_name, size_t expected_size) {
+ int r;
+
+ pa_assert(u);
+ pa_assert(u->service_fd >= 0);
+ pa_assert(rsp);
+
+ if ((r = service_recv(u, rsp, room)) < 0)
+ return r;
+
+ if ((rsp->type != BT_INDICATION && rsp->type != BT_RESPONSE) ||
+ rsp->name != expected_name ||
+ (expected_size > 0 && rsp->length != expected_size)) {
+
+ if (rsp->type == BT_ERROR && rsp->length == sizeof(bt_audio_error_t))
+ pa_log_error("Received error condition: %s", pa_cstrerror(((bt_audio_error_t*) rsp)->posix_errno));
+ else
+ pa_log_error("Bogus message %s received while %s was expected",
+ pa_strnull(bt_audio_strname(rsp->name)),
+ pa_strnull(bt_audio_strname(expected_name)));
+ return -1;
+ }
+
+ return 0;
+}
+
+/* Run from main thread */
+static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capabilities_rsp *rsp) {
+ uint16_t bytes_left;
+ const codec_capabilities_t *codec;
+
+ pa_assert(u);
+ pa_assert(rsp);
+
+ bytes_left = rsp->h.length - sizeof(*rsp);
+
+ if (bytes_left < sizeof(codec_capabilities_t)) {
+ pa_log_error("Packet too small to store codec information.");
+ return -1;
+ }
+
+ codec = (codec_capabilities_t *) rsp->data; /** ALIGNMENT? **/
+
+ pa_log_debug("Payload size is %lu %lu", (unsigned long) bytes_left, (unsigned long) sizeof(*codec));
+
+ if (((u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) && codec->transport != BT_CAPABILITIES_TRANSPORT_A2DP) ||
+ ((u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW) && codec->transport != BT_CAPABILITIES_TRANSPORT_SCO)) {
+ pa_log_error("Got capabilities for wrong codec.");
+ return -1;
+ }
+
+ if (u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW) {
+
+ if (bytes_left <= 0 || codec->length != sizeof(u->hsp.pcm_capabilities))
+ return -1;
+
+ pa_assert(codec->type == BT_HFP_CODEC_PCM);
+
+ if (codec->configured && seid == 0)
+ return codec->seid;
+
+ memcpy(&u->hsp.pcm_capabilities, codec, sizeof(u->hsp.pcm_capabilities));
+
+ } else if (u->profile == PROFILE_A2DP) {
+
+ while (bytes_left > 0) {
+ if ((codec->type == BT_A2DP_SBC_SINK) && !codec->lock)
+ break;
+
+ bytes_left -= codec->length;
+ codec = (const codec_capabilities_t*) ((const uint8_t*) codec + codec->length);
+ }
+
+ if (bytes_left <= 0 || codec->length != sizeof(u->a2dp.sbc_capabilities))
+ return -1;
+
+ pa_assert(codec->type == BT_A2DP_SBC_SINK);
+
+ if (codec->configured && seid == 0)
+ return codec->seid;
+
+ memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities));
+
+ } else if (u->profile == PROFILE_A2DP_SOURCE) {
+
+ while (bytes_left > 0) {
+ if ((codec->type == BT_A2DP_SBC_SOURCE) && !codec->lock)
+ break;
+
+ bytes_left -= codec->length;
+ codec = (const codec_capabilities_t*) ((const uint8_t*) codec + codec->length);
+ }
+
+ if (bytes_left <= 0 || codec->length != sizeof(u->a2dp.sbc_capabilities))
+ return -1;
+
+ pa_assert(codec->type == BT_A2DP_SBC_SOURCE);
+
+ if (codec->configured && seid == 0)
+ return codec->seid;
+
+ memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities));
+ }
+
+ return 0;
+}
+
+/* Run from main thread */
+static int get_caps(struct userdata *u, uint8_t seid) {
+ union {
+ struct bt_get_capabilities_req getcaps_req;
+ struct bt_get_capabilities_rsp getcaps_rsp;
+ bt_audio_error_t error;
+ uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
+ } msg;
+ int ret;
+
+ pa_assert(u);
+
+ memset(&msg, 0, sizeof(msg));
+ msg.getcaps_req.h.type = BT_REQUEST;
+ msg.getcaps_req.h.name = BT_GET_CAPABILITIES;
+ msg.getcaps_req.h.length = sizeof(msg.getcaps_req);
+ msg.getcaps_req.seid = seid;
+
+ pa_strlcpy(msg.getcaps_req.object, u->path, sizeof(msg.getcaps_req.object));
+ if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE)
+ msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_A2DP;
+ else {
+ pa_assert(u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW);
+ msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_SCO;
+ }
+ msg.getcaps_req.flags = u->auto_connect ? BT_FLAG_AUTOCONNECT : 0;
+
+ if (service_send(u, &msg.getcaps_req.h) < 0)
+ return -1;
+
+ if (service_expect(u, &msg.getcaps_rsp.h, sizeof(msg), BT_GET_CAPABILITIES, 0) < 0)
+ return -1;
+
+ ret = parse_caps(u, seid, &msg.getcaps_rsp);
+ if (ret <= 0)
+ return ret;
+
+ return get_caps(u, ret);
+}
+
+/* Run from main thread */
+static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
+
+ switch (freq) {
+ case BT_SBC_SAMPLING_FREQ_16000:
+ case BT_SBC_SAMPLING_FREQ_32000:
+ return 53;
+
+ case BT_SBC_SAMPLING_FREQ_44100:
+
+ switch (mode) {
+ case BT_A2DP_CHANNEL_MODE_MONO:
+ case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ return 31;
+
+ case BT_A2DP_CHANNEL_MODE_STEREO:
+ case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ return 53;
+
+ default:
+ pa_log_warn("Invalid channel mode %u", mode);
+ return 53;
+ }
+
+ case BT_SBC_SAMPLING_FREQ_48000:
+
+ switch (mode) {
+ case BT_A2DP_CHANNEL_MODE_MONO:
+ case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ return 29;
+
+ case BT_A2DP_CHANNEL_MODE_STEREO:
+ case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ return 51;
+
+ default:
+ pa_log_warn("Invalid channel mode %u", mode);
+ return 51;
+ }
+
+ default:
+ pa_log_warn("Invalid sampling freq %u", freq);
+ return 53;
+ }
+}
+
+/* Run from main thread */
+static int setup_a2dp(struct userdata *u) {
+ sbc_capabilities_t *cap;
+ int i;
+
+ static const struct {
+ uint32_t rate;
+ uint8_t cap;
+ } freq_table[] = {
+ { 16000U, BT_SBC_SAMPLING_FREQ_16000 },
+ { 32000U, BT_SBC_SAMPLING_FREQ_32000 },
+ { 44100U, BT_SBC_SAMPLING_FREQ_44100 },
+ { 48000U, BT_SBC_SAMPLING_FREQ_48000 }
+ };
+
+ pa_assert(u);
+ pa_assert(u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE);
+
+ cap = &u->a2dp.sbc_capabilities;
+
+ /* Find the lowest freq that is at least as high as the requested
+ * sampling rate */
+ for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
+ if (freq_table[i].rate >= u->sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
+ u->sample_spec.rate = freq_table[i].rate;
+ cap->frequency = freq_table[i].cap;
+ break;
+ }
+
+ if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
+ for (--i; i >= 0; i--) {
+ if (cap->frequency & freq_table[i].cap) {
+ u->sample_spec.rate = freq_table[i].rate;
+ cap->frequency = freq_table[i].cap;
+ break;
+ }
+ }
+
+ if (i < 0) {
+ pa_log("Not suitable sample rate");
+ return -1;
+ }
+ }
+
+ pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
+
+ if (cap->capability.configured)
+ return 0;
+
+ if (u->sample_spec.channels <= 1) {
+ if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
+ cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
+ u->sample_spec.channels = 1;
+ } else
+ u->sample_spec.channels = 2;
+ }
+
+ if (u->sample_spec.channels >= 2) {
+ u->sample_spec.channels = 2;
+
+ if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
+ cap->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
+ else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
+ cap->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
+ else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
+ cap->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
+ else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
+ cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
+ u->sample_spec.channels = 1;
+ } else {
+ pa_log("No supported channel modes");
+ return -1;
+ }
+ }
+
+ if (cap->block_length & BT_A2DP_BLOCK_LENGTH_16)
+ cap->block_length = BT_A2DP_BLOCK_LENGTH_16;
+ else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_12)
+ cap->block_length = BT_A2DP_BLOCK_LENGTH_12;
+ else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_8)
+ cap->block_length = BT_A2DP_BLOCK_LENGTH_8;
+ else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_4)
+ cap->block_length = BT_A2DP_BLOCK_LENGTH_4;
+ else {
+ pa_log_error("No supported block lengths");
+ return -1;
+ }
+
+ if (cap->subbands & BT_A2DP_SUBBANDS_8)
+ cap->subbands = BT_A2DP_SUBBANDS_8;
+ else if (cap->subbands & BT_A2DP_SUBBANDS_4)
+ cap->subbands = BT_A2DP_SUBBANDS_4;
+ else {
+ pa_log_error("No supported subbands");
+ return -1;
+ }
+
+ if (cap->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS)
+ cap->allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
+ else if (cap->allocation_method & BT_A2DP_ALLOCATION_SNR)
+ cap->allocation_method = BT_A2DP_ALLOCATION_SNR;
+
+ cap->min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
+ cap->max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(cap->frequency, cap->channel_mode), cap->max_bitpool);
+
+ return 0;
+}
+
+/* Run from main thread */
+static void setup_sbc(struct a2dp_info *a2dp, enum profile p) {
+ sbc_capabilities_t *active_capabilities;
+
+ pa_assert(a2dp);
+
+ active_capabilities = &a2dp->sbc_capabilities;
+
+ if (a2dp->sbc_initialized)
+ sbc_reinit(&a2dp->sbc, 0);
+ else
+ sbc_init(&a2dp->sbc, 0);
+ a2dp->sbc_initialized = TRUE;
+
+ switch (active_capabilities->frequency) {
+ case BT_SBC_SAMPLING_FREQ_16000:
+ a2dp->sbc.frequency = SBC_FREQ_16000;
+ break;
+ case BT_SBC_SAMPLING_FREQ_32000:
+ a2dp->sbc.frequency = SBC_FREQ_32000;
+ break;
+ case BT_SBC_SAMPLING_FREQ_44100:
+ a2dp->sbc.frequency = SBC_FREQ_44100;
+ break;
+ case BT_SBC_SAMPLING_FREQ_48000:
+ a2dp->sbc.frequency = SBC_FREQ_48000;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (active_capabilities->channel_mode) {
+ case BT_A2DP_CHANNEL_MODE_MONO:
+ a2dp->sbc.mode = SBC_MODE_MONO;
+ break;
+ case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ a2dp->sbc.mode = SBC_MODE_DUAL_CHANNEL;
+ break;
+ case BT_A2DP_CHANNEL_MODE_STEREO:
+ a2dp->sbc.mode = SBC_MODE_STEREO;
+ break;
+ case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ a2dp->sbc.mode = SBC_MODE_JOINT_STEREO;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (active_capabilities->allocation_method) {
+ case BT_A2DP_ALLOCATION_SNR:
+ a2dp->sbc.allocation = SBC_AM_SNR;
+ break;
+ case BT_A2DP_ALLOCATION_LOUDNESS:
+ a2dp->sbc.allocation = SBC_AM_LOUDNESS;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (active_capabilities->subbands) {
+ case BT_A2DP_SUBBANDS_4:
+ a2dp->sbc.subbands = SBC_SB_4;
+ break;
+ case BT_A2DP_SUBBANDS_8:
+ a2dp->sbc.subbands = SBC_SB_8;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (active_capabilities->block_length) {
+ case BT_A2DP_BLOCK_LENGTH_4:
+ a2dp->sbc.blocks = SBC_BLK_4;
+ break;
+ case BT_A2DP_BLOCK_LENGTH_8:
+ a2dp->sbc.blocks = SBC_BLK_8;
+ break;
+ case BT_A2DP_BLOCK_LENGTH_12:
+ a2dp->sbc.blocks = SBC_BLK_12;
+ break;
+ case BT_A2DP_BLOCK_LENGTH_16:
+ a2dp->sbc.blocks = SBC_BLK_16;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ a2dp->min_bitpool = active_capabilities->min_bitpool;
+ a2dp->max_bitpool = active_capabilities->max_bitpool;
+
+ /* Set minimum bitpool for source to get the maximum possible block_size */
+ a2dp->sbc.bitpool = p == PROFILE_A2DP ? a2dp->max_bitpool : a2dp->min_bitpool;
+ a2dp->codesize = sbc_get_codesize(&a2dp->sbc);
+ a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc);
+}
+
+/* Run from main thread */
+static int set_conf(struct userdata *u) {
+ union {
+ struct bt_open_req open_req;
+ struct bt_open_rsp open_rsp;
+ struct bt_set_configuration_req setconf_req;
+ struct bt_set_configuration_rsp setconf_rsp;
+ bt_audio_error_t error;
+ uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
+ } msg;
+
+ memset(&msg, 0, sizeof(msg));
+ msg.open_req.h.type = BT_REQUEST;
+ msg.open_req.h.name = BT_OPEN;
+ msg.open_req.h.length = sizeof(msg.open_req);
+
+ pa_strlcpy(msg.open_req.object, u->path, sizeof(msg.open_req.object));
+ msg.open_req.seid = (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) ? u->a2dp.sbc_capabilities.capability.seid : BT_A2DP_SEID_RANGE + 1;
+ msg.open_req.lock = (u->profile == PROFILE_A2DP) ? BT_WRITE_LOCK : BT_READ_LOCK | BT_WRITE_LOCK;
+
+ if (service_send(u, &msg.open_req.h) < 0)
+ return -1;
+
+ if (service_expect(u, &msg.open_rsp.h, sizeof(msg), BT_OPEN, sizeof(msg.open_rsp)) < 0)
+ return -1;
+
+ if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
+ u->sample_spec.format = PA_SAMPLE_S16LE;
+
+ if (setup_a2dp(u) < 0)
+ return -1;
+ } else {
+ pa_assert(u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW);
+
+ u->sample_spec.format = PA_SAMPLE_S16LE;
+ u->sample_spec.channels = 1;
+ u->sample_spec.rate = 8000;
+ }
+
+ memset(&msg, 0, sizeof(msg));
+ msg.setconf_req.h.type = BT_REQUEST;
+ msg.setconf_req.h.name = BT_SET_CONFIGURATION;
+ msg.setconf_req.h.length = sizeof(msg.setconf_req);
+
+ if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
+ memcpy(&msg.setconf_req.codec, &u->a2dp.sbc_capabilities, sizeof(u->a2dp.sbc_capabilities));
+ } else {
+ msg.setconf_req.codec.transport = BT_CAPABILITIES_TRANSPORT_SCO;
+ msg.setconf_req.codec.seid = BT_A2DP_SEID_RANGE + 1;
+ msg.setconf_req.codec.length = sizeof(pcm_capabilities_t);
+ }
+ msg.setconf_req.h.length += msg.setconf_req.codec.length - sizeof(msg.setconf_req.codec);
+
+ if (service_send(u, &msg.setconf_req.h) < 0)
+ return -1;
+
+ if (service_expect(u, &msg.setconf_rsp.h, sizeof(msg), BT_SET_CONFIGURATION, sizeof(msg.setconf_rsp)) < 0)
+ return -1;
+
+ u->link_mtu = msg.setconf_rsp.link_mtu;
+
+ /* setup SBC encoder now we agree on parameters */
+ if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
+ setup_sbc(&u->a2dp, u->profile);
+
+ u->block_size =
+ ((u->link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
+ / u->a2dp.frame_length
+ * u->a2dp.codesize);
+
+ pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
+ u->a2dp.sbc.allocation, u->a2dp.sbc.subbands, u->a2dp.sbc.blocks, u->a2dp.sbc.bitpool);
+ } else
+ u->block_size = u->link_mtu;
+
+ return 0;
+}
+
+/* from IO thread */
+static void a2dp_set_bitpool(struct userdata *u, uint8_t bitpool)
+{
+ struct a2dp_info *a2dp;
+
+ pa_assert(u);
+
+ a2dp = &u->a2dp;
+
+ if (a2dp->sbc.bitpool == bitpool)
+ return;
+
+ if (bitpool > a2dp->max_bitpool)
+ bitpool = a2dp->max_bitpool;
+ else if (bitpool < a2dp->min_bitpool)
+ bitpool = a2dp->min_bitpool;
+
+ a2dp->sbc.bitpool = bitpool;
+
+ a2dp->codesize = sbc_get_codesize(&a2dp->sbc);
+ a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc);
+
+ pa_log_debug("Bitpool has changed to %u", a2dp->sbc.bitpool);
+
+ u->block_size =
+ (u->link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
+ / a2dp->frame_length * a2dp->codesize;
+
+ pa_sink_set_max_request_within_thread(u->sink, u->block_size);
+ pa_sink_set_fixed_latency_within_thread(u->sink,
+ FIXED_LATENCY_PLAYBACK_A2DP + pa_bytes_to_usec(u->block_size, &u->sample_spec));
+}
+
+/* from IO thread, except in SCO over PCM */
+
+static int setup_stream(struct userdata *u) {
+ struct pollfd *pollfd;
+ int one;
+
+ pa_make_fd_nonblock(u->stream_fd);
+ pa_make_socket_low_delay(u->stream_fd);
+
+ one = 1;
+ if (setsockopt(u->stream_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one)) < 0)
+ pa_log_warn("Failed to enable SO_TIMESTAMP: %s", pa_cstrerror(errno));
+
+ pa_log_debug("Stream properly set up, we're ready to roll!");
+
+ if (u->profile == PROFILE_A2DP)
+ a2dp_set_bitpool(u, u->a2dp.max_bitpool);
+
+ u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->fd = u->stream_fd;
+ pollfd->events = pollfd->revents = 0;
+
+ u->read_index = u->write_index = 0;
+ u->started_at = 0;
+
+ if (u->source)
+ u->read_smoother = pa_smoother_new(
+ PA_USEC_PER_SEC,
+ PA_USEC_PER_SEC*2,
+ TRUE,
+ TRUE,
+ 10,
+ pa_rtclock_now(),
+ TRUE);
+
+ return 0;
+}
+
+static int start_stream_fd(struct userdata *u) {
+ union {
+ bt_audio_msg_header_t rsp;
+ struct bt_start_stream_req start_req;
+ struct bt_start_stream_rsp start_rsp;
+ struct bt_new_stream_ind streamfd_ind;
+ bt_audio_error_t error;
+ uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
+ } msg;
+
+ pa_assert(u);
+ pa_assert(u->rtpoll);
+ pa_assert(!u->rtpoll_item);
+ pa_assert(u->stream_fd < 0);
+
+ memset(msg.buf, 0, BT_SUGGESTED_BUFFER_SIZE);
+ msg.start_req.h.type = BT_REQUEST;
+ msg.start_req.h.name = BT_START_STREAM;
+ msg.start_req.h.length = sizeof(msg.start_req);
+
+ if (service_send(u, &msg.start_req.h) < 0)
+ return -1;
+
+ if (service_expect(u, &msg.rsp, sizeof(msg), BT_START_STREAM, sizeof(msg.start_rsp)) < 0)
+ return -1;
+
+ if (service_expect(u, &msg.rsp, sizeof(msg), BT_NEW_STREAM, sizeof(msg.streamfd_ind)) < 0)
+ return -1;
+
+ if ((u->stream_fd = bt_audio_service_get_data_fd(u->service_fd)) < 0) {
+ pa_log("Failed to get stream fd from audio service.");
+ return -1;
+ }
+
+ return setup_stream(u);
+}
+
+/* from IO thread */
+static int stop_stream_fd(struct userdata *u) {
+ union {
+ bt_audio_msg_header_t rsp;
+ struct bt_stop_stream_req start_req;
+ struct bt_stop_stream_rsp start_rsp;
+ bt_audio_error_t error;
+ uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
+ } msg;
+ int r = 0;
+
+ pa_assert(u);
+ pa_assert(u->rtpoll);
+
+ if (u->rtpoll_item) {
+ pa_rtpoll_item_free(u->rtpoll_item);
+ u->rtpoll_item = NULL;
+ }
+
+ if (u->stream_fd >= 0) {
+ memset(msg.buf, 0, BT_SUGGESTED_BUFFER_SIZE);
+ msg.start_req.h.type = BT_REQUEST;
+ msg.start_req.h.name = BT_STOP_STREAM;
+ msg.start_req.h.length = sizeof(msg.start_req);
+
+ if (service_send(u, &msg.start_req.h) < 0 ||
+ service_expect(u, &msg.rsp, sizeof(msg), BT_STOP_STREAM, sizeof(msg.start_rsp)) < 0)
+ r = -1;
+
+ pa_close(u->stream_fd);
+ u->stream_fd = -1;
+ }
+
+ if (u->read_smoother) {
+ pa_smoother_free(u->read_smoother);
+ u->read_smoother = NULL;
+ }
+
+ return r;
+}
+
+static void bt_transport_release(struct userdata *u) {
+ const char *accesstype = "rw";
+ const pa_bluetooth_transport *t;
+
+ /* Ignore if already released */
+ if (!u->accesstype)
+ return;
+
+ pa_log_debug("Releasing transport %s", u->transport);
+
+ t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
+ if (t)
+ pa_bluetooth_transport_release(t, accesstype);
+
+ pa_xfree(u->accesstype);
+ u->accesstype = NULL;
+
+ if (u->rtpoll_item) {
+ pa_rtpoll_item_free(u->rtpoll_item);
+ u->rtpoll_item = NULL;
+ }
+
+ if (u->stream_fd >= 0) {
+ pa_close(u->stream_fd);
+ u->stream_fd = -1;
+ }
+
+ if (u->read_smoother) {
+ pa_smoother_free(u->read_smoother);
+ u->read_smoother = NULL;
+ }
+}
+
+static int bt_transport_acquire(struct userdata *u, pa_bool_t start) {
+ const char *accesstype = "rw";
+ const pa_bluetooth_transport *t;
+
+ if (u->accesstype) {
+ if (start)
+ goto done;
+ return 0;
+ }
+
+ pa_log_debug("Acquiring transport %s", u->transport);
+
+ t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
+ if (!t) {
+ pa_log("Transport %s no longer available", u->transport);
+ pa_xfree(u->transport);
+ u->transport = NULL;
+ return -1;
+ }
+
+ /* FIXME: Handle in/out MTU properly when unix socket is not longer supported */
+ u->stream_fd = pa_bluetooth_transport_acquire(t, accesstype, NULL, &u->link_mtu);
+ if (u->stream_fd < 0)
+ return -1;
+
+ u->accesstype = pa_xstrdup(accesstype);
+ pa_log_info("Transport %s acquired: fd %d", u->transport, u->stream_fd);
+
+ if (!start)
+ return 0;
+
+done:
+ pa_log_info("Transport %s resuming", u->transport);
+ return setup_stream(u);
+}
+
+/* Run from IO thread */
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+ pa_bool_t failed = FALSE;
+ int r;
+
+ pa_assert(u->sink == PA_SINK(o));
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_SET_STATE:
+
+ switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
+
+ case PA_SINK_SUSPENDED:
+ pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
+
+ /* Stop the device if the source is suspended as well */
+ if (!u->source || u->source->state == PA_SOURCE_SUSPENDED) {
+ /* We deliberately ignore whether stopping
+ * actually worked. Since the stream_fd is
+ * closed it doesn't really matter */
+ if (u->transport)
+ bt_transport_release(u);
+ else
+ stop_stream_fd(u);
+ }
+
+ break;
+
+ case PA_SINK_IDLE:
+ case PA_SINK_RUNNING:
+ if (u->sink->thread_info.state != PA_SINK_SUSPENDED)
+ break;
+
+ /* Resume the device if the source was suspended as well */
+ if (!u->source || u->source->state == PA_SOURCE_SUSPENDED) {
+ if (u->transport) {
+ if (bt_transport_acquire(u, TRUE) < 0)
+ failed = TRUE;
+ } else if (start_stream_fd(u) < 0)
+ failed = TRUE;
+ }
+ break;
+
+ case PA_SINK_UNLINKED:
+ case PA_SINK_INIT:
+ case PA_SINK_INVALID_STATE:
+ ;
+ }
+ break;
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+
+ if (u->read_smoother) {
+ pa_usec_t wi, ri;
+
+ ri = pa_smoother_get(u->read_smoother, pa_rtclock_now());
+ wi = pa_bytes_to_usec(u->write_index + u->block_size, &u->sample_spec);
+
+ *((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
+ } else {
+ pa_usec_t ri, wi;
+
+ ri = pa_rtclock_now() - u->started_at;
+ wi = pa_bytes_to_usec(u->write_index, &u->sample_spec);
+
+ *((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
+ }
+
+ *((pa_usec_t*) data) += u->sink->thread_info.fixed_latency;
+ return 0;
+ }
+ }
+
+ r = pa_sink_process_msg(o, code, data, offset, chunk);
+
+ return (r < 0 || !failed) ? r : -1;
+}
+
+/* Run from IO thread */
+static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE(o)->userdata;
+ pa_bool_t failed = FALSE;
+ int r;
+
+ pa_assert(u->source == PA_SOURCE(o));
+
+ switch (code) {
+
+ case PA_SOURCE_MESSAGE_SET_STATE:
+
+ switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
+
+ case PA_SOURCE_SUSPENDED:
+ pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
+
+ /* Stop the device if the sink is suspended as well */
+ if (!u->sink || u->sink->state == PA_SINK_SUSPENDED) {
+ if (u->transport)
+ bt_transport_release(u);
+ else
+ stop_stream_fd(u);
+ }
+
+ if (u->read_smoother)
+ pa_smoother_pause(u->read_smoother, pa_rtclock_now());
+ break;
+
+ case PA_SOURCE_IDLE:
+ case PA_SOURCE_RUNNING:
+ if (u->source->thread_info.state != PA_SOURCE_SUSPENDED)
+ break;
+
+ /* Resume the device if the sink was suspended as well */
+ if (!u->sink || u->sink->thread_info.state == PA_SINK_SUSPENDED) {
+ if (u->transport) {
+ if (bt_transport_acquire(u, TRUE) < 0)
+ failed = TRUE;
+ } else if (start_stream_fd(u) < 0)
+ failed = TRUE;
+ }
+ /* We don't resume the smoother here. Instead we
+ * wait until the first packet arrives */
+ break;
+
+ case PA_SOURCE_UNLINKED:
+ case PA_SOURCE_INIT:
+ case PA_SOURCE_INVALID_STATE:
+ ;
+ }
+ break;
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ pa_usec_t wi, ri;
+
+ if (u->read_smoother) {
+ wi = pa_smoother_get(u->read_smoother, pa_rtclock_now());
+ ri = pa_bytes_to_usec(u->read_index, &u->sample_spec);
+
+ *((pa_usec_t*) data) = (wi > ri ? wi - ri : 0) + u->source->thread_info.fixed_latency;
+ } else
+ *((pa_usec_t*) data) = 0;
+
+ return 0;
+ }
+
+ }
+
+ r = pa_source_process_msg(o, code, data, offset, chunk);
+
+ return (r < 0 || !failed) ? r : -1;
+}
+
+/* Run from IO thread */
+static int hsp_process_render(struct userdata *u) {
+ int ret = 0;
+
+ pa_assert(u);
+ pa_assert(u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW);
+ pa_assert(u->sink);
+
+ /* First, render some data */
+ if (!u->write_memchunk.memblock)
+ pa_sink_render_full(u->sink, u->block_size, &u->write_memchunk);
+
+ pa_assert(u->write_memchunk.length == u->block_size);
+
+ for (;;) {
+ ssize_t l;
+ const void *p;
+
+ /* Now write that data to the socket. The socket is of type
+ * SEQPACKET, and we generated the data of the MTU size, so this
+ * should just work. */
+
+ p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index;
+ l = pa_write(u->stream_fd, p, u->write_memchunk.length, &u->stream_write_type);
+ pa_memblock_release(u->write_memchunk.memblock);
+
+ pa_assert(l != 0);
+
+ if (l < 0) {
+
+ if (errno == EINTR)
+ /* Retry right away if we got interrupted */
+ continue;
+
+ else if (errno == EAGAIN)
+ /* Hmm, apparently the socket was not writable, give up for now */
+ break;
+
+ pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno));
+ ret = -1;
+ break;
+ }
+
+ pa_assert((size_t) l <= u->write_memchunk.length);
+
+ if ((size_t) l != u->write_memchunk.length) {
+ pa_log_error("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
+ (unsigned long long) l,
+ (unsigned long long) u->write_memchunk.length);
+ ret = -1;
+ break;
+ }
+
+ u->write_index += (uint64_t) u->write_memchunk.length;
+ pa_memblock_unref(u->write_memchunk.memblock);
+ pa_memchunk_reset(&u->write_memchunk);
+
+ ret = 1;
+ break;
+ }
+
+ return ret;
+}
+
+/* Run from IO thread */
+static int hsp_process_push(struct userdata *u) {
+ int ret = 0;
+ pa_memchunk memchunk;
+
+ pa_assert(u);
+ pa_assert(u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW);
+ pa_assert(u->source);
+ pa_assert(u->read_smoother);
+
+ memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size);
+ memchunk.index = memchunk.length = 0;
+
+ for (;;) {
+ ssize_t l;
+ void *p;
+ struct msghdr m;
+ struct cmsghdr *cm;
+ uint8_t aux[1024];
+ struct iovec iov;
+ pa_bool_t found_tstamp = FALSE;
+ pa_usec_t tstamp;
+
+ memset(&m, 0, sizeof(m));
+ memset(&aux, 0, sizeof(aux));
+ memset(&iov, 0, sizeof(iov));
+
+ m.msg_iov = &iov;
+ m.msg_iovlen = 1;
+ m.msg_control = aux;
+ m.msg_controllen = sizeof(aux);
+
+ p = pa_memblock_acquire(memchunk.memblock);
+ iov.iov_base = p;
+ iov.iov_len = pa_memblock_get_length(memchunk.memblock);
+ l = recvmsg(u->stream_fd, &m, 0);
+ pa_memblock_release(memchunk.memblock);
+
+ if (l <= 0) {
+
+ if (l < 0 && errno == EINTR)
+ /* Retry right away if we got interrupted */
+ continue;
+
+ else if (l < 0 && errno == EAGAIN)
+ /* Hmm, apparently the socket was not readable, give up for now. */
+ break;
+
+ pa_log_error("Failed to read data from SCO socket: %s", l < 0 ? pa_cstrerror(errno) : "EOF");
+ ret = -1;
+ break;
+ }
+
+ pa_assert((size_t) l <= pa_memblock_get_length(memchunk.memblock));
+
+ memchunk.length = (size_t) l;
+ u->read_index += (uint64_t) l;
+
+ for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm))
+ if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SO_TIMESTAMP) {
+ struct timeval *tv = (struct timeval*) CMSG_DATA(cm);
+ pa_rtclock_from_wallclock(tv);
+ tstamp = pa_timeval_load(tv);
+ found_tstamp = TRUE;
+ break;
+ }
+
+ if (!found_tstamp) {
+ pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!");
+ tstamp = pa_rtclock_now();
+ }
+
+ pa_smoother_put(u->read_smoother, tstamp, pa_bytes_to_usec(u->read_index, &u->sample_spec));
+ pa_smoother_resume(u->read_smoother, tstamp, TRUE);
+
+ pa_source_post(u->source, &memchunk);
+
+ ret = 1;
+ break;
+ }
+
+ pa_memblock_unref(memchunk.memblock);
+
+ return ret;
+}
+
+/* Run from IO thread */
+static void a2dp_prepare_buffer(struct userdata *u) {
+ pa_assert(u);
+
+ if (u->a2dp.buffer_size >= u->link_mtu)
+ return;
+
+ u->a2dp.buffer_size = 2 * u->link_mtu;
+ pa_xfree(u->a2dp.buffer);
+ u->a2dp.buffer = pa_xmalloc(u->a2dp.buffer_size);
+}
+
+/* Run from IO thread */
+static int a2dp_process_render(struct userdata *u) {
+ struct a2dp_info *a2dp;
+ struct rtp_header *header;
+ struct rtp_payload *payload;
+ size_t nbytes;
+ void *d;
+ const void *p;
+ size_t to_write, to_encode;
+ unsigned frame_count;
+ int ret = 0;
+
+ pa_assert(u);
+ pa_assert(u->profile == PROFILE_A2DP);
+ pa_assert(u->sink);
+
+ /* First, render some data */
+ if (!u->write_memchunk.memblock)
+ pa_sink_render_full(u->sink, u->block_size, &u->write_memchunk);
+
+ pa_assert(u->write_memchunk.length == u->block_size);
+
+ a2dp_prepare_buffer(u);
+
+ a2dp = &u->a2dp;
+ header = a2dp->buffer;
+ payload = (struct rtp_payload*) ((uint8_t*) a2dp->buffer + sizeof(*header));
+
+ frame_count = 0;
+
+ /* Try to create a packet of the full MTU */
+
+ p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index;
+ to_encode = u->write_memchunk.length;
+
+ d = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload);
+ to_write = a2dp->buffer_size - sizeof(*header) - sizeof(*payload);
+
+ while (PA_LIKELY(to_encode > 0 && to_write > 0)) {
+ ssize_t written;
+ ssize_t encoded;
+
+ encoded = sbc_encode(&a2dp->sbc,
+ p, to_encode,
+ d, to_write,
+ &written);
+
+ if (PA_UNLIKELY(encoded <= 0)) {
+ pa_log_error("SBC encoding error (%li)", (long) encoded);
+ pa_memblock_release(u->write_memchunk.memblock);
+ return -1;
+ }
+
+/* pa_log_debug("SBC: encoded: %lu; written: %lu", (unsigned long) encoded, (unsigned long) written); */
+/* pa_log_debug("SBC: codesize: %lu; frame_length: %lu", (unsigned long) a2dp->codesize, (unsigned long) a2dp->frame_length); */
+
+ pa_assert_fp((size_t) encoded <= to_encode);
+ pa_assert_fp((size_t) encoded == a2dp->codesize);
+
+ pa_assert_fp((size_t) written <= to_write);
+ pa_assert_fp((size_t) written == a2dp->frame_length);
+
+ p = (const uint8_t*) p + encoded;
+ to_encode -= encoded;
+
+ d = (uint8_t*) d + written;
+ to_write -= written;
+
+ frame_count++;
+ }
+
+ pa_memblock_release(u->write_memchunk.memblock);
+
+ pa_assert(to_encode == 0);
+
+ PA_ONCE_BEGIN {
+ pa_log_debug("Using SBC encoder implementation: %s", pa_strnull(sbc_get_implementation_info(&a2dp->sbc)));
+ } PA_ONCE_END;
+
+ /* write it to the fifo */
+ memset(a2dp->buffer, 0, sizeof(*header) + sizeof(*payload));
+ header->v = 2;
+ header->pt = 1;
+ header->sequence_number = htons(a2dp->seq_num++);
+ header->timestamp = htonl(u->write_index / pa_frame_size(&u->sample_spec));
+ header->ssrc = htonl(1);
+ payload->frame_count = frame_count;
+
+ nbytes = (uint8_t*) d - (uint8_t*) a2dp->buffer;
+
+ for (;;) {
+ ssize_t l;
+
+ l = pa_write(u->stream_fd, a2dp->buffer, nbytes, &u->stream_write_type);
+
+ pa_assert(l != 0);
+
+ if (l < 0) {
+
+ if (errno == EINTR)
+ /* Retry right away if we got interrupted */
+ continue;
+
+ else if (errno == EAGAIN)
+ /* Hmm, apparently the socket was not writable, give up for now */
+ break;
+
+ pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno));
+ ret = -1;
+ break;
+ }
+
+ pa_assert((size_t) l <= nbytes);
+
+ if ((size_t) l != nbytes) {
+ pa_log_warn("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
+ (unsigned long long) l,
+ (unsigned long long) nbytes);
+ ret = -1;
+ break;
+ }
+
+ u->write_index += (uint64_t) u->write_memchunk.length;
+ pa_memblock_unref(u->write_memchunk.memblock);
+ pa_memchunk_reset(&u->write_memchunk);
+
+ ret = 1;
+
+ break;
+ }
+
+ return ret;
+}
+
+static int a2dp_process_push(struct userdata *u) {
+ int ret = 0;
+ pa_memchunk memchunk;
+
+ pa_assert(u);
+ pa_assert(u->profile == PROFILE_A2DP_SOURCE);
+ pa_assert(u->source);
+ pa_assert(u->read_smoother);
+
+ memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size);
+ memchunk.index = memchunk.length = 0;
+
+ for (;;) {
+ pa_bool_t found_tstamp = FALSE;
+ pa_usec_t tstamp;
+ struct a2dp_info *a2dp;
+ struct rtp_header *header;
+ struct rtp_payload *payload;
+ const void *p;
+ void *d;
+ ssize_t l;
+ size_t to_write, to_decode;
+ unsigned frame_count;
+
+ a2dp_prepare_buffer(u);
+
+ a2dp = &u->a2dp;
+ header = a2dp->buffer;
+ payload = (struct rtp_payload*) ((uint8_t*) a2dp->buffer + sizeof(*header));
+
+ l = pa_read(u->stream_fd, a2dp->buffer, a2dp->buffer_size, &u->stream_write_type);
+
+ if (l <= 0) {
+
+ if (l < 0 && errno == EINTR)
+ /* Retry right away if we got interrupted */
+ continue;
+
+ else if (l < 0 && errno == EAGAIN)
+ /* Hmm, apparently the socket was not readable, give up for now. */
+ break;
+
+ pa_log_error("Failed to read data from socket: %s", l < 0 ? pa_cstrerror(errno) : "EOF");
+ ret = -1;
+ break;
+ }
+
+ pa_assert((size_t) l <= a2dp->buffer_size);
+
+ u->read_index += (uint64_t) l;
+
+ /* TODO: get timestamp from rtp */
+ if (!found_tstamp) {
+ /* pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); */
+ tstamp = pa_rtclock_now();
+ }
+
+ pa_smoother_put(u->read_smoother, tstamp, pa_bytes_to_usec(u->read_index, &u->sample_spec));
+ pa_smoother_resume(u->read_smoother, tstamp, TRUE);
+
+ p = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload);
+ to_decode = l - sizeof(*header) - sizeof(*payload);
+
+ d = pa_memblock_acquire(memchunk.memblock);
+ to_write = memchunk.length = pa_memblock_get_length(memchunk.memblock);
+
+ while (PA_LIKELY(to_decode > 0)) {
+ size_t written;
+ ssize_t decoded;
+
+ decoded = sbc_decode(&a2dp->sbc,
+ p, to_decode,
+ d, to_write,
+ &written);
+
+ if (PA_UNLIKELY(decoded <= 0)) {
+ pa_log_error("SBC decoding error (%li)", (long) decoded);
+ pa_memblock_release(memchunk.memblock);
+ pa_memblock_unref(memchunk.memblock);
+ return -1;
+ }
+
+/* pa_log_debug("SBC: decoded: %lu; written: %lu", (unsigned long) decoded, (unsigned long) written); */
+/* pa_log_debug("SBC: frame_length: %lu; codesize: %lu", (unsigned long) a2dp->frame_length, (unsigned long) a2dp->codesize); */
+
+ /* Reset frame length, it can be changed due to bitpool change */
+ a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc);
+
+ pa_assert_fp((size_t) decoded <= to_decode);
+ pa_assert_fp((size_t) decoded == a2dp->frame_length);
+
+ pa_assert_fp((size_t) written == a2dp->codesize);
+
+ p = (const uint8_t*) p + decoded;
+ to_decode -= decoded;
+
+ d = (uint8_t*) d + written;
+ to_write -= written;
+
+ frame_count++;
+ }
+
+ memchunk.length -= to_write;
+
+ pa_memblock_release(memchunk.memblock);
+
+ pa_source_post(u->source, &memchunk);
+
+ ret = 1;
+ break;
+ }
+
+ pa_memblock_unref(memchunk.memblock);
+
+ return ret;
+}
+
+static void a2dp_reduce_bitpool(struct userdata *u)
+{
+ struct a2dp_info *a2dp;
+ uint8_t bitpool;
+
+ pa_assert(u);
+
+ a2dp = &u->a2dp;
+
+ /* Check if bitpool is already at its limit */
+ if (a2dp->sbc.bitpool <= BITPOOL_DEC_LIMIT)
+ return;
+
+ bitpool = a2dp->sbc.bitpool - BITPOOL_DEC_STEP;
+
+ if (bitpool < BITPOOL_DEC_LIMIT)
+ bitpool = BITPOOL_DEC_LIMIT;
+
+ a2dp_set_bitpool(u, bitpool);
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+ unsigned do_write = 0;
+ pa_bool_t writable = FALSE;
+
+ pa_assert(u);
+
+ pa_log_debug("IO Thread starting up");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority);
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ if (u->transport) {
+ if (bt_transport_acquire(u, TRUE) < 0)
+ goto fail;
+ } else if (start_stream_fd(u) < 0)
+ goto fail;
+
+ for (;;) {
+ struct pollfd *pollfd;
+ int ret;
+ pa_bool_t disable_timer = TRUE;
+
+ pollfd = u->rtpoll_item ? pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL) : NULL;
+
+ if (u->source && PA_SOURCE_IS_LINKED(u->source->thread_info.state)) {
+
+ /* We should send two blocks to the device before we expect
+ * a response. */
+
+ if (u->write_index == 0 && u->read_index <= 0)
+ do_write = 2;
+
+ if (pollfd && (pollfd->revents & POLLIN)) {
+ int n_read;
+
+ if (u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW)
+ n_read = hsp_process_push(u);
+ else
+ n_read = a2dp_process_push(u);
+
+ if (n_read < 0)
+ goto fail;
+
+ /* We just read something, so we are supposed to write something, too */
+ do_write += n_read;
+ }
+ }
+
+ if (u->sink && PA_SINK_IS_LINKED(u->sink->thread_info.state)) {
+
+ if (u->sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(u->sink, 0);
+
+ if (pollfd) {
+ if (pollfd->revents & POLLOUT)
+ writable = TRUE;
+
+ if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0 && writable) {
+ pa_usec_t time_passed;
+ pa_usec_t audio_sent;
+
+ /* Hmm, there is no input stream we could synchronize
+ * to. So let's do things by time */
+
+ time_passed = pa_rtclock_now() - u->started_at;
+ audio_sent = pa_bytes_to_usec(u->write_index, &u->sample_spec);
+
+ if (audio_sent <= time_passed) {
+ pa_usec_t audio_to_send = time_passed - audio_sent;
+
+ /* Never try to catch up for more than 100ms */
+ if (u->write_index > 0 && audio_to_send > MAX_PLAYBACK_CATCH_UP_USEC) {
+ pa_usec_t skip_usec;
+ uint64_t skip_bytes;
+
+ skip_usec = audio_to_send - MAX_PLAYBACK_CATCH_UP_USEC;
+ skip_bytes = pa_usec_to_bytes(skip_usec, &u->sample_spec);
+
+ if (skip_bytes > 0) {
+ pa_memchunk tmp;
+
+ pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream",
+ (unsigned long long) skip_usec,
+ (unsigned long long) skip_bytes);
+
+ pa_sink_render_full(u->sink, skip_bytes, &tmp);
+ pa_memblock_unref(tmp.memblock);
+ u->write_index += skip_bytes;
+
+ if (u->profile == PROFILE_A2DP)
+ a2dp_reduce_bitpool(u);
+ }
+ }
+
+ do_write = 1;
+ }
+ }
+
+ if (writable && do_write > 0) {
+ int n_written;
+
+ if (u->write_index <= 0)
+ u->started_at = pa_rtclock_now();
+
+ if (u->profile == PROFILE_A2DP) {
+ if ((n_written = a2dp_process_render(u)) < 0)
+ goto fail;
+ } else {
+ if ((n_written = hsp_process_render(u)) < 0)
+ goto fail;
+ }
+
+ if (n_written == 0)
+ pa_log("Broken kernel: we got EAGAIN on write() after POLLOUT!");
+
+ do_write -= n_written;
+ writable = FALSE;
+ }
+
+ if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0) {
+ pa_usec_t sleep_for;
+ pa_usec_t time_passed, next_write_at;
+
+ if (writable) {
+ /* Hmm, there is no input stream we could synchronize
+ * to. So let's estimate when we need to wake up the latest */
+ time_passed = pa_rtclock_now() - u->started_at;
+ next_write_at = pa_bytes_to_usec(u->write_index, &u->sample_spec);
+ sleep_for = time_passed < next_write_at ? next_write_at - time_passed : 0;
+ /* pa_log("Sleeping for %lu; time passed %lu, next write at %lu", (unsigned long) sleep_for, (unsigned long) time_passed, (unsigned long)next_write_at); */
+ } else
+ /* drop stream every 500 ms */
+ sleep_for = PA_USEC_PER_MSEC * 500;
+
+ pa_rtpoll_set_timer_relative(u->rtpoll, sleep_for);
+ disable_timer = FALSE;
+ }
+ }
+ }
+
+ if (disable_timer)
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
+
+ /* Hmm, nothing to do. Let's sleep */
+ if (pollfd)
+ pollfd->events = (short) (((u->sink && PA_SINK_IS_LINKED(u->sink->thread_info.state) && !writable) ? POLLOUT : 0) |
+ (u->source && PA_SOURCE_IS_LINKED(u->source->thread_info.state) ? POLLIN : 0));
+
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+
+ pollfd = u->rtpoll_item ? pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL) : NULL;
+
+ if (pollfd && (pollfd->revents & ~(POLLOUT|POLLIN))) {
+ pa_log_info("FD error: %s%s%s%s",
+ pollfd->revents & POLLERR ? "POLLERR " :"",
+ pollfd->revents & POLLHUP ? "POLLHUP " :"",
+ pollfd->revents & POLLPRI ? "POLLPRI " :"",
+ pollfd->revents & POLLNVAL ? "POLLNVAL " :"");
+ goto fail;
+ }
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
+ pa_log_debug("IO thread failed");
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("IO thread shutting down");
+}
+
+/* Run from main thread */
+static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
+ DBusError err;
+ struct userdata *u;
+
+ pa_assert(bus);
+ pa_assert(m);
+ pa_assert_se(u = userdata);
+
+ dbus_error_init(&err);
+
+ pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
+ dbus_message_get_interface(m),
+ dbus_message_get_path(m),
+ dbus_message_get_member(m));
+
+ if (!dbus_message_has_path(m, u->path) && !dbus_message_has_path(m, u->transport))
+ goto fail;
+
+ if (dbus_message_is_signal(m, "org.bluez.Headset", "SpeakerGainChanged") ||
+ dbus_message_is_signal(m, "org.bluez.Headset", "MicrophoneGainChanged")) {
+
+ dbus_uint16_t gain;
+ pa_cvolume v;
+
+ if (!dbus_message_get_args(m, &err, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID) || gain > HSP_MAX_GAIN) {
+ pa_log("Failed to parse org.bluez.Headset.{Speaker|Microphone}GainChanged: %s", err.message);
+ goto fail;
+ }
+
+ if (u->profile == PROFILE_HSP) {
+ if (u->sink && dbus_message_is_signal(m, "org.bluez.Headset", "SpeakerGainChanged")) {
+ pa_volume_t volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
+
+ /* increment volume by one to correct rounding errors */
+ if (volume < PA_VOLUME_NORM)
+ volume++;
+
+ pa_cvolume_set(&v, u->sample_spec.channels, volume);
+ pa_sink_volume_changed(u->sink, &v);
+
+ } else if (u->source && dbus_message_is_signal(m, "org.bluez.Headset", "MicrophoneGainChanged")) {
+ pa_volume_t volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
+
+ /* increment volume by one to correct rounding errors */
+ if (volume < PA_VOLUME_NORM)
+ volume++;
+
+ pa_cvolume_set(&v, u->sample_spec.channels, volume);
+ pa_source_volume_changed(u->source, &v);
+ }
+ }
+ } else if (dbus_message_is_signal(m, "org.bluez.MediaTransport", "PropertyChanged")) {
+ DBusMessageIter arg_i;
+ pa_bluetooth_transport *t;
+ pa_bool_t nrec;
+
+ t = (pa_bluetooth_transport *) pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
+ pa_assert(t);
+
+ if (!dbus_message_iter_init(m, &arg_i)) {
+ pa_log("Failed to parse PropertyChanged: %s", err.message);
+ goto fail;
+ }
+
+ nrec = t->nrec;
+
+ if (pa_bluetooth_transport_parse_property(t, &arg_i) < 0)
+ goto fail;
+
+ if (nrec != t->nrec) {
+ pa_log_debug("dbus: property 'NREC' changed to value '%s'", t->nrec ? "True" : "False");
+ pa_proplist_sets(u->source->proplist, "bluetooth.nrec", t->nrec ? "1" : "0");
+ }
+ }
+
+fail:
+ dbus_error_free(&err);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+/* Run from main thread */
+static void sink_set_volume_cb(pa_sink *s) {
+ DBusMessage *m;
+ dbus_uint16_t gain;
+ pa_volume_t volume;
+ struct userdata *u;
+ char *k;
+
+ pa_assert(s);
+ pa_assert(s->core);
+
+ k = pa_sprintf_malloc("bluetooth-device@%p", (void*) s);
+ u = pa_shared_get(s->core, k);
+ pa_xfree(k);
+
+ pa_assert(u);
+ pa_assert(u->sink == s);
+ pa_assert(u->profile == PROFILE_HSP);
+
+ gain = (pa_cvolume_max(&s->real_volume) * HSP_MAX_GAIN) / PA_VOLUME_NORM;
+
+ if (gain > HSP_MAX_GAIN)
+ gain = HSP_MAX_GAIN;
+
+ volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
+
+ /* increment volume by one to correct rounding errors */
+ if (volume < PA_VOLUME_NORM)
+ volume++;
+
+ pa_cvolume_set(&s->real_volume, u->sample_spec.channels, volume);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetSpeakerGain"));
+ pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID));
+ pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->connection), m, NULL));
+ dbus_message_unref(m);
+}
+
+/* Run from main thread */
+static void source_set_volume_cb(pa_source *s) {
+ DBusMessage *m;
+ dbus_uint16_t gain;
+ pa_volume_t volume;
+ struct userdata *u;
+ char *k;
+
+ pa_assert(s);
+ pa_assert(s->core);
+
+ k = pa_sprintf_malloc("bluetooth-device@%p", (void*) s);
+ u = pa_shared_get(s->core, k);
+ pa_xfree(k);
+
+ pa_assert(u);
+ pa_assert(u->source == s);
+ pa_assert(u->profile == PROFILE_HSP);
+
+ gain = (pa_cvolume_max(&s->real_volume) * HSP_MAX_GAIN) / PA_VOLUME_NORM;
+
+ if (gain > HSP_MAX_GAIN)
+ gain = HSP_MAX_GAIN;
+
+ volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
+
+ /* increment volume by one to correct rounding errors */
+ if (volume < PA_VOLUME_NORM)
+ volume++;
+
+ pa_cvolume_set(&s->real_volume, u->sample_spec.channels, volume);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetMicrophoneGain"));
+ pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID));
+ pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->connection), m, NULL));
+ dbus_message_unref(m);
+}
+
+/* Run from main thread */
+static char *get_name(const char *type, pa_modargs *ma, const char *device_id, pa_bool_t *namereg_fail) {
+ char *t;
+ const char *n;
+
+ pa_assert(type);
+ pa_assert(ma);
+ pa_assert(device_id);
+ pa_assert(namereg_fail);
+
+ t = pa_sprintf_malloc("%s_name", type);
+ n = pa_modargs_get_value(ma, t, NULL);
+ pa_xfree(t);
+
+ if (n) {
+ *namereg_fail = TRUE;
+ return pa_xstrdup(n);
+ }
+
+ if ((n = pa_modargs_get_value(ma, "name", NULL)))
+ *namereg_fail = TRUE;
+ else {
+ n = device_id;
+ *namereg_fail = FALSE;
+ }
+
+ return pa_sprintf_malloc("bluez_%s.%s", type, n);
+}
+
+static int sco_over_pcm_state_update(struct userdata *u, pa_bool_t changed) {
+ pa_assert(u);
+ pa_assert(USE_SCO_OVER_PCM(u));
+
+ if (PA_SINK_IS_OPENED(pa_sink_get_state(u->hsp.sco_sink)) ||
+ PA_SOURCE_IS_OPENED(pa_source_get_state(u->hsp.sco_source))) {
+
+ if (u->service_fd >= 0 && u->stream_fd >= 0)
+ return 0;
+
+ init_bt(u);
+
+ pa_log_debug("Resuming SCO over PCM");
+ if (init_profile(u) < 0) {
+ pa_log("Can't resume SCO over PCM");
+ return -1;
+ }
+
+ if (u->transport)
+ return bt_transport_acquire(u, TRUE);
+
+ return start_stream_fd(u);
+ }
+
+ if (changed) {
+ if (u->service_fd < 0 && u->stream_fd < 0)
+ return 0;
+
+ pa_log_debug("Closing SCO over PCM");
+
+ if (u->transport)
+ bt_transport_release(u);
+ else if (u->stream_fd >= 0)
+ stop_stream_fd(u);
+
+ if (u->service_fd >= 0) {
+ pa_close(u->service_fd);
+ u->service_fd = -1;
+ }
+ }
+
+ return 0;
+}
+
+static pa_hook_result_t sink_state_changed_cb(pa_core *c, pa_sink *s, struct userdata *u) {
+ pa_assert(c);
+ pa_sink_assert_ref(s);
+ pa_assert(u);
+
+ if (s != u->hsp.sco_sink)
+ return PA_HOOK_OK;
+
+ sco_over_pcm_state_update(u, TRUE);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_state_changed_cb(pa_core *c, pa_source *s, struct userdata *u) {
+ pa_assert(c);
+ pa_source_assert_ref(s);
+ pa_assert(u);
+
+ if (s != u->hsp.sco_source)
+ return PA_HOOK_OK;
+
+ sco_over_pcm_state_update(u, TRUE);
+
+ return PA_HOOK_OK;
+}
+
+/* Run from main thread */
+static int add_sink(struct userdata *u) {
+ char *k;
+
+ if (USE_SCO_OVER_PCM(u)) {
+ pa_proplist *p;
+
+ u->sink = u->hsp.sco_sink;
+ p = pa_proplist_new();
+ pa_proplist_sets(p, "bluetooth.protocol", "sco");
+ pa_proplist_update(u->sink->proplist, PA_UPDATE_MERGE, p);
+ pa_proplist_free(p);
+
+ if (!u->hsp.sink_state_changed_slot)
+ u->hsp.sink_state_changed_slot = pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_state_changed_cb, u);
+
+ } else {
+ pa_sink_new_data data;
+ pa_bool_t b;
+
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = u->module;
+ pa_sink_new_data_set_sample_spec(&data, &u->sample_spec);
+ pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "sco");
+ if (u->profile == PROFILE_HSP)
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
+ data.card = u->card;
+ data.name = get_name("sink", u->modargs, u->address, &b);
+ data.namereg_fail = b;
+
+ if (pa_modargs_get_proplist(u->modargs, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&data);
+ return -1;
+ }
+
+ u->sink = pa_sink_new(u->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY | (u->profile == PROFILE_HSP ? PA_SINK_HW_VOLUME_CTRL : 0));
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log_error("Failed to create sink");
+ return -1;
+ }
+
+ u->sink->userdata = u;
+ u->sink->parent.process_msg = sink_process_msg;
+
+ pa_sink_set_max_request(u->sink, u->block_size);
+ pa_sink_set_fixed_latency(u->sink,
+ (u->profile == PROFILE_A2DP ? FIXED_LATENCY_PLAYBACK_A2DP : FIXED_LATENCY_PLAYBACK_HSP) +
+ pa_bytes_to_usec(u->block_size, &u->sample_spec));
+ }
+
+ if (u->profile == PROFILE_HSP) {
+ u->sink->set_volume = sink_set_volume_cb;
+ u->sink->n_volume_steps = 16;
+
+ k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->sink);
+ pa_shared_set(u->core, k, u);
+ pa_xfree(k);
+ }
+
+ return 0;
+}
+
+/* Run from main thread */
+static int add_source(struct userdata *u) {
+ char *k;
+
+ if (USE_SCO_OVER_PCM(u)) {
+ u->source = u->hsp.sco_source;
+ pa_proplist_sets(u->source->proplist, "bluetooth.protocol", "hsp");
+
+ if (!u->hsp.source_state_changed_slot)
+ u->hsp.source_state_changed_slot = pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_state_changed_cb, u);
+
+ } else {
+ pa_source_new_data data;
+ pa_bool_t b;
+
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = u->module;
+ pa_source_new_data_set_sample_spec(&data, &u->sample_spec);
+ pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP_SOURCE ? "a2dp_source" : "hsp");
+ if ((u->profile == PROFILE_HSP) || (u->profile == PROFILE_HFGW))
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
+
+ data.card = u->card;
+ data.name = get_name("source", u->modargs, u->address, &b);
+ data.namereg_fail = b;
+
+ if (pa_modargs_get_proplist(u->modargs, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&data);
+ return -1;
+ }
+
+ u->source = pa_source_new(u->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY | (u->profile == PROFILE_HSP ? PA_SOURCE_HW_VOLUME_CTRL : 0));
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
+ pa_log_error("Failed to create source");
+ return -1;
+ }
+
+ u->source->userdata = u;
+ u->source->parent.process_msg = source_process_msg;
+
+ pa_source_set_fixed_latency(u->source,
+ (u->profile == PROFILE_A2DP_SOURCE ? FIXED_LATENCY_RECORD_A2DP : FIXED_LATENCY_RECORD_HSP) +
+ pa_bytes_to_usec(u->block_size, &u->sample_spec));
+ }
+
+ if ((u->profile == PROFILE_HSP) || (u->profile == PROFILE_HFGW)) {
+ if (u->transport) {
+ const pa_bluetooth_transport *t;
+ t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
+ pa_assert(t);
+ pa_proplist_sets(u->source->proplist, "bluetooth.nrec", t->nrec ? "1" : "0");
+ } else
+ pa_proplist_sets(u->source->proplist, "bluetooth.nrec", (u->hsp.pcm_capabilities.flags & BT_PCM_FLAG_NREC) ? "1" : "0");
+ }
+
+ if (u->profile == PROFILE_HSP) {
+ u->source->set_volume = source_set_volume_cb;
+ u->source->n_volume_steps = 16;
+
+ k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->source);
+ pa_shared_set(u->core, k, u);
+ pa_xfree(k);
+ }
+
+ return 0;
+}
+
+/* Run from main thread */
+static void shutdown_bt(struct userdata *u) {
+ pa_assert(u);
+
+ if (u->stream_fd >= 0) {
+ pa_close(u->stream_fd);
+ u->stream_fd = -1;
+
+ u->stream_write_type = 0;
+ }
+
+ if (u->service_fd >= 0) {
+ pa_close(u->service_fd);
+ u->service_fd = -1;
+ u->service_write_type = 0;
+ u->service_read_type = 0;
+ }
+
+ if (u->write_memchunk.memblock) {
+ pa_memblock_unref(u->write_memchunk.memblock);
+ pa_memchunk_reset(&u->write_memchunk);
+ }
+}
+
+static int bt_transport_config_a2dp(struct userdata *u) {
+ const pa_bluetooth_transport *t;
+ struct a2dp_info *a2dp = &u->a2dp;
+ a2dp_sbc_t *config;
+
+ t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
+ pa_assert(t);
+
+ config = (a2dp_sbc_t *) t->config;
+
+ u->sample_spec.format = PA_SAMPLE_S16LE;
+
+ if (a2dp->sbc_initialized)
+ sbc_reinit(&a2dp->sbc, 0);
+ else
+ sbc_init(&a2dp->sbc, 0);
+ a2dp->sbc_initialized = TRUE;
+
+ switch (config->frequency) {
+ case BT_SBC_SAMPLING_FREQ_16000:
+ a2dp->sbc.frequency = SBC_FREQ_16000;
+ u->sample_spec.rate = 16000U;
+ break;
+ case BT_SBC_SAMPLING_FREQ_32000:
+ a2dp->sbc.frequency = SBC_FREQ_32000;
+ u->sample_spec.rate = 32000U;
+ break;
+ case BT_SBC_SAMPLING_FREQ_44100:
+ a2dp->sbc.frequency = SBC_FREQ_44100;
+ u->sample_spec.rate = 44100U;
+ break;
+ case BT_SBC_SAMPLING_FREQ_48000:
+ a2dp->sbc.frequency = SBC_FREQ_48000;
+ u->sample_spec.rate = 48000U;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (config->channel_mode) {
+ case BT_A2DP_CHANNEL_MODE_MONO:
+ a2dp->sbc.mode = SBC_MODE_MONO;
+ u->sample_spec.channels = 1;
+ break;
+ case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ a2dp->sbc.mode = SBC_MODE_DUAL_CHANNEL;
+ u->sample_spec.channels = 2;
+ break;
+ case BT_A2DP_CHANNEL_MODE_STEREO:
+ a2dp->sbc.mode = SBC_MODE_STEREO;
+ u->sample_spec.channels = 2;
+ break;
+ case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ a2dp->sbc.mode = SBC_MODE_JOINT_STEREO;
+ u->sample_spec.channels = 2;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (config->allocation_method) {
+ case BT_A2DP_ALLOCATION_SNR:
+ a2dp->sbc.allocation = SBC_AM_SNR;
+ break;
+ case BT_A2DP_ALLOCATION_LOUDNESS:
+ a2dp->sbc.allocation = SBC_AM_LOUDNESS;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (config->subbands) {
+ case BT_A2DP_SUBBANDS_4:
+ a2dp->sbc.subbands = SBC_SB_4;
+ break;
+ case BT_A2DP_SUBBANDS_8:
+ a2dp->sbc.subbands = SBC_SB_8;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (config->block_length) {
+ case BT_A2DP_BLOCK_LENGTH_4:
+ a2dp->sbc.blocks = SBC_BLK_4;
+ break;
+ case BT_A2DP_BLOCK_LENGTH_8:
+ a2dp->sbc.blocks = SBC_BLK_8;
+ break;
+ case BT_A2DP_BLOCK_LENGTH_12:
+ a2dp->sbc.blocks = SBC_BLK_12;
+ break;
+ case BT_A2DP_BLOCK_LENGTH_16:
+ a2dp->sbc.blocks = SBC_BLK_16;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ a2dp->min_bitpool = config->min_bitpool;
+ a2dp->max_bitpool = config->max_bitpool;
+
+ /* Set minimum bitpool for source to get the maximum possible block_size */
+ a2dp->sbc.bitpool = u->profile == PROFILE_A2DP ? a2dp->max_bitpool : a2dp->min_bitpool;
+ a2dp->codesize = sbc_get_codesize(&a2dp->sbc);
+ a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc);
+
+ u->block_size =
+ ((u->link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
+ / a2dp->frame_length
+ * a2dp->codesize);
+
+ pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
+ a2dp->sbc.allocation, a2dp->sbc.subbands, a2dp->sbc.blocks, a2dp->sbc.bitpool);
+
+ return 0;
+}
+
+static int bt_transport_config(struct userdata *u) {
+ if (u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW) {
+ u->block_size = u->link_mtu;
+ u->sample_spec.format = PA_SAMPLE_S16LE;
+ u->sample_spec.channels = 1;
+ u->sample_spec.rate = 8000;
+ return 0;
+ }
+
+ return bt_transport_config_a2dp(u);
+}
+
+/* Run from main thread */
+static int bt_transport_open(struct userdata *u) {
+ if (bt_transport_acquire(u, FALSE) < 0)
+ return -1;
+
+ return bt_transport_config(u);
+}
+
+/* Run from main thread */
+static int init_bt(struct userdata *u) {
+ pa_assert(u);
+
+ shutdown_bt(u);
+
+ u->stream_write_type = 0;
+ u->service_write_type = 0;
+ u->service_read_type = 0;
+
+ if ((u->service_fd = bt_audio_service_open()) < 0) {
+ pa_log_warn("Bluetooth audio service not available");
+ return -1;
+ }
+
+ pa_log_debug("Connected to the bluetooth audio service");
+
+ return 0;
+}
+
+/* Run from main thread */
+static int setup_bt(struct userdata *u) {
+ const pa_bluetooth_device *d;
+ const pa_bluetooth_transport *t;
+
+ pa_assert(u);
+
+ if (!(d = pa_bluetooth_discovery_get_by_path(u->discovery, u->path))) {
+ pa_log_error("Failed to get device object.");
+ return -1;
+ }
+
+ /* release transport if exist */
+ if (u->transport) {
+ bt_transport_release(u);
+ pa_xfree(u->transport);
+ u->transport = NULL;
+ }
+
+ /* check if profile has a transport */
+ t = pa_bluetooth_device_get_transport(d, u->profile);
+ if (t) {
+ u->transport = pa_xstrdup(t->path);
+ return bt_transport_open(u);
+ }
+
+ if (get_caps(u, 0) < 0)
+ return -1;
+
+ pa_log_debug("Got device capabilities");
+
+ if (set_conf(u) < 0)
+ return -1;
+
+ pa_log_debug("Connection to the device configured");
+
+ if (USE_SCO_OVER_PCM(u)) {
+ pa_log_debug("Configured to use SCO over PCM");
+ return 0;
+ }
+
+ pa_log_debug("Got the stream socket");
+
+ return 0;
+}
+
+/* Run from main thread */
+static int init_profile(struct userdata *u) {
+ int r = 0;
+ pa_assert(u);
+ pa_assert(u->profile != PROFILE_OFF);
+
+ if (setup_bt(u) < 0)
+ return -1;
+
+ if (u->profile == PROFILE_A2DP ||
+ u->profile == PROFILE_HSP ||
+ u->profile == PROFILE_HFGW)
+ if (add_sink(u) < 0)
+ r = -1;
+
+ if (u->profile == PROFILE_HSP ||
+ u->profile == PROFILE_A2DP_SOURCE ||
+ u->profile == PROFILE_HFGW)
+ if (add_source(u) < 0)
+ r = -1;
+
+ return r;
+}
+
+/* Run from main thread */
+static void stop_thread(struct userdata *u) {
+ char *k;
+
+ pa_assert(u);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ u->thread = NULL;
+ }
+
+ if (u->rtpoll_item) {
+ pa_rtpoll_item_free(u->rtpoll_item);
+ u->rtpoll_item = NULL;
+ }
+
+ if (u->hsp.sink_state_changed_slot) {
+ pa_hook_slot_free(u->hsp.sink_state_changed_slot);
+ u->hsp.sink_state_changed_slot = NULL;
+ }
+
+ if (u->hsp.source_state_changed_slot) {
+ pa_hook_slot_free(u->hsp.source_state_changed_slot);
+ u->hsp.source_state_changed_slot = NULL;
+ }
+
+ if (u->sink) {
+ if (u->profile == PROFILE_HSP) {
+ k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->sink);
+ pa_shared_remove(u->core, k);
+ pa_xfree(k);
+ }
+
+ pa_sink_unref(u->sink);
+ u->sink = NULL;
+ }
+
+ if (u->source) {
+ if (u->profile == PROFILE_HSP) {
+ k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->source);
+ pa_shared_remove(u->core, k);
+ pa_xfree(k);
+ }
+
+ pa_source_unref(u->source);
+ u->source = NULL;
+ }
+
+ if (u->rtpoll) {
+ pa_thread_mq_done(&u->thread_mq);
+
+ pa_rtpoll_free(u->rtpoll);
+ u->rtpoll = NULL;
+ }
+
+ if (u->read_smoother) {
+ pa_smoother_free(u->read_smoother);
+ u->read_smoother = NULL;
+ }
+}
+
+/* Run from main thread */
+static int start_thread(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(!u->thread);
+ pa_assert(!u->rtpoll);
+ pa_assert(!u->rtpoll_item);
+
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
+
+ if (USE_SCO_OVER_PCM(u)) {
+ if (sco_over_pcm_state_update(u, FALSE) < 0) {
+ char *k;
+
+ if (u->sink) {
+ k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->sink);
+ pa_shared_remove(u->core, k);
+ pa_xfree(k);
+ u->sink = NULL;
+ }
+ if (u->source) {
+ k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->source);
+ pa_shared_remove(u->core, k);
+ pa_xfree(k);
+ u->source = NULL;
+ }
+ return -1;
+ }
+
+ pa_sink_ref(u->sink);
+ pa_source_ref(u->source);
+ /* FIXME: monitor stream_fd error */
+ return 0;
+ }
+
+ if (!(u->thread = pa_thread_new("bluetooth", thread_func, u))) {
+ pa_log_error("Failed to create IO thread");
+ stop_thread(u);
+ return -1;
+ }
+
+ if (u->sink) {
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+ pa_sink_put(u->sink);
+
+ if (u->sink->set_volume)
+ u->sink->set_volume(u->sink);
+ }
+
+ if (u->source) {
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+ pa_source_put(u->source);
+
+ if (u->source->set_volume)
+ u->source->set_volume(u->source);
+ }
+
+ return 0;
+}
+
+static void save_sco_volume_callbacks(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(USE_SCO_OVER_PCM(u));
+
+ u->hsp.sco_sink_set_volume = u->hsp.sco_sink->set_volume;
+ u->hsp.sco_source_set_volume = u->hsp.sco_source->set_volume;
+}
+
+static void restore_sco_volume_callbacks(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(USE_SCO_OVER_PCM(u));
+
+ u->hsp.sco_sink->set_volume = u->hsp.sco_sink_set_volume;
+ u->hsp.sco_source->set_volume = u->hsp.sco_source_set_volume;
+}
+
+/* Run from main thread */
+static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
+ struct userdata *u;
+ enum profile *d;
+ pa_queue *inputs = NULL, *outputs = NULL;
+ const pa_bluetooth_device *device;
+
+ pa_assert(c);
+ pa_assert(new_profile);
+ pa_assert_se(u = c->userdata);
+
+ d = PA_CARD_PROFILE_DATA(new_profile);
+
+ if (!(device = pa_bluetooth_discovery_get_by_path(u->discovery, u->path))) {
+ pa_log_error("Failed to get device object.");
+ return -PA_ERR_IO;
+ }
+
+ /* The state signal is sent by bluez, so it is racy to check
+ strictly for CONNECTED, we should also accept STREAMING state
+ as being good enough. However, if the profile is used
+ concurrently (which is unlikely), ipc will fail later on, and
+ module will be unloaded. */
+ if (device->headset_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HSP) {
+ pa_log_warn("HSP is not connected, refused to switch profile");
+ return -PA_ERR_IO;
+ }
+ else if (device->audio_sink_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_A2DP) {
+ pa_log_warn("A2DP is not connected, refused to switch profile");
+ return -PA_ERR_IO;
+ }
+ else if (device->hfgw_state <= PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HFGW) {
+ pa_log_warn("HandsfreeGateway is not connected, refused to switch profile");
+ return -PA_ERR_IO;
+ }
+
+ if (u->sink) {
+ inputs = pa_sink_move_all_start(u->sink, NULL);
+
+ if (!USE_SCO_OVER_PCM(u))
+ pa_sink_unlink(u->sink);
+ }
+
+ if (u->source) {
+ outputs = pa_source_move_all_start(u->source, NULL);
+
+ if (!USE_SCO_OVER_PCM(u))
+ pa_source_unlink(u->source);
+ }
+
+ stop_thread(u);
+ shutdown_bt(u);
+
+ if (USE_SCO_OVER_PCM(u))
+ restore_sco_volume_callbacks(u);
+
+ u->profile = *d;
+ u->sample_spec = u->requested_sample_spec;
+
+ if (USE_SCO_OVER_PCM(u))
+ save_sco_volume_callbacks(u);
+
+ init_bt(u);
+
+ if (u->profile != PROFILE_OFF)
+ init_profile(u);
+
+ if (u->sink || u->source)
+ start_thread(u);
+
+ if (inputs) {
+ if (u->sink)
+ pa_sink_move_all_finish(u->sink, inputs, FALSE);
+ else
+ pa_sink_move_all_fail(inputs);
+ }
+
+ if (outputs) {
+ if (u->source)
+ pa_source_move_all_finish(u->source, outputs, FALSE);
+ else
+ pa_source_move_all_fail(outputs);
+ }
+
+ return 0;
+}
+
+/* Run from main thread */
+static int add_card(struct userdata *u, const pa_bluetooth_device *device) {
+ pa_card_new_data data;
+ pa_bool_t b;
+ pa_card_profile *p;
+ enum profile *d;
+ const char *ff;
+ char *n;
+ const char *default_profile;
+
+ pa_assert(u);
+ pa_assert(device);
+
+ pa_card_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = u->module;
+
+ n = pa_bluetooth_cleanup_name(device->name);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, n);
+ pa_xfree(n);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, device->address);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "bluez");
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "sound");
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_BUS, "bluetooth");
+ if ((ff = pa_bluetooth_get_form_factor(device->class)))
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_FORM_FACTOR, ff);
+ pa_proplist_sets(data.proplist, "bluez.path", device->path);
+ pa_proplist_setf(data.proplist, "bluez.class", "0x%06x", (unsigned) device->class);
+ pa_proplist_sets(data.proplist, "bluez.name", device->name);
+ data.name = get_name("card", u->modargs, device->address, &b);
+ data.namereg_fail = b;
+
+ if (pa_modargs_get_proplist(u->modargs, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_card_new_data_done(&data);
+ return -1;
+ }
+
+ data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ /* we base hsp/a2dp availability on UUIDs.
+ Ideally, it would be based on "Connected" state, but
+ we can't afford to wait for this information when
+ we are loaded with profile="hsp", for instance */
+ if (pa_bluetooth_uuid_has(device->uuids, A2DP_SINK_UUID)) {
+ p = pa_card_profile_new("a2dp", _("High Fidelity Playback (A2DP)"), sizeof(enum profile));
+ p->priority = 10;
+ p->n_sinks = 1;
+ p->n_sources = 0;
+ p->max_sink_channels = 2;
+ p->max_source_channels = 0;
+
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_A2DP;
+
+ pa_hashmap_put(data.profiles, p->name, p);
+ }
+
+ if (pa_bluetooth_uuid_has(device->uuids, A2DP_SOURCE_UUID)) {
+ p = pa_card_profile_new("a2dp_source", _("High Fidelity Capture (A2DP)"), sizeof(enum profile));
+ p->priority = 10;
+ p->n_sinks = 0;
+ p->n_sources = 1;
+ p->max_sink_channels = 0;
+ p->max_source_channels = 2;
+
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_A2DP_SOURCE;
+
+ pa_hashmap_put(data.profiles, p->name, p);
+ }
+
+ if (pa_bluetooth_uuid_has(device->uuids, HSP_HS_UUID) ||
+ pa_bluetooth_uuid_has(device->uuids, HFP_HS_UUID)) {
+ p = pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile));
+ p->priority = 20;
+ p->n_sinks = 1;
+ p->n_sources = 1;
+ p->max_sink_channels = 1;
+ p->max_source_channels = 1;
+
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_HSP;
+
+ pa_hashmap_put(data.profiles, p->name, p);
+ }
+
+ if (pa_bluetooth_uuid_has(device->uuids, HFP_AG_UUID)) {
+ p = pa_card_profile_new("hfgw", _("Handsfree Gateway"), sizeof(enum profile));
+ p->priority = 20;
+ p->n_sinks = 1;
+ p->n_sources = 1;
+ p->max_sink_channels = 1;
+ p->max_source_channels = 1;
+
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_HFGW;
+
+ pa_hashmap_put(data.profiles, p->name, p);
+ }
+
+ pa_assert(!pa_hashmap_isempty(data.profiles));
+
+ p = pa_card_profile_new("off", _("Off"), sizeof(enum profile));
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_OFF;
+ pa_hashmap_put(data.profiles, p->name, p);
+
+ if ((default_profile = pa_modargs_get_value(u->modargs, "profile", NULL))) {
+ if (pa_hashmap_get(data.profiles, default_profile))
+ pa_card_new_data_set_profile(&data, default_profile);
+ else
+ pa_log_warn("Profile '%s' not valid or not supported by device.", default_profile);
+ }
+
+ u->card = pa_card_new(u->core, &data);
+ pa_card_new_data_done(&data);
+
+ if (!u->card) {
+ pa_log("Failed to allocate card.");
+ return -1;
+ }
+
+ u->card->userdata = u;
+ u->card->set_profile = card_set_profile;
+
+ d = PA_CARD_PROFILE_DATA(u->card->active_profile);
+
+ if ((device->headset_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HSP) ||
+ (device->audio_sink_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_A2DP) ||
+ (device->hfgw_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HFGW)) {
+ pa_log_warn("Default profile not connected, selecting off profile");
+ u->card->active_profile = pa_hashmap_get(u->card->profiles, "off");
+ u->card->save_profile = FALSE;
+ }
+
+ d = PA_CARD_PROFILE_DATA(u->card->active_profile);
+ u->profile = *d;
+
+ if (USE_SCO_OVER_PCM(u))
+ save_sco_volume_callbacks(u);
+
+ return 0;
+}
+
+/* Run from main thread */
+static const pa_bluetooth_device* find_device(struct userdata *u, const char *address, const char *path) {
+ const pa_bluetooth_device *d = NULL;
+
+ pa_assert(u);
+
+ if (!address && !path) {
+ pa_log_error("Failed to get device address/path from module arguments.");
+ return NULL;
+ }
+
+ if (path) {
+ if (!(d = pa_bluetooth_discovery_get_by_path(u->discovery, path))) {
+ pa_log_error("%s is not a valid BlueZ audio device.", path);
+ return NULL;
+ }
+
+ if (address && !(pa_streq(d->address, address))) {
+ pa_log_error("Passed path %s address %s != %s don't match.", path, d->address, address);
+ return NULL;
+ }
+
+ } else {
+ if (!(d = pa_bluetooth_discovery_get_by_address(u->discovery, address))) {
+ pa_log_error("%s is not known.", address);
+ return NULL;
+ }
+ }
+
+ if (d) {
+ u->address = pa_xstrdup(d->address);
+ u->path = pa_xstrdup(d->path);
+ }
+
+ return d;
+}
+
+/* Run from main thread */
+static int setup_dbus(struct userdata *u) {
+ DBusError err;
+
+ dbus_error_init(&err);
+
+ u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &err);
+
+ if (dbus_error_is_set(&err) || !u->connection) {
+ pa_log("Failed to get D-Bus connection: %s", err.message);
+ dbus_error_free(&err);
+ return -1;
+ }
+
+ return 0;
+}
+
+int pa__init(pa_module* m) {
+ pa_modargs *ma;
+ uint32_t channels;
+ struct userdata *u;
+ const char *address, *path;
+ DBusError err;
+ char *mike, *speaker, *transport;
+ const pa_bluetooth_device *device;
+
+ pa_assert(m);
+
+ dbus_error_init(&err);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log_error("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ u->core = m->core;
+ u->service_fd = -1;
+ u->stream_fd = -1;
+ u->sample_spec = m->core->default_sample_spec;
+ u->modargs = ma;
+
+ if (pa_modargs_get_value(ma, "sco_sink", NULL) &&
+ !(u->hsp.sco_sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sco_sink", NULL), PA_NAMEREG_SINK))) {
+ pa_log("SCO sink not found");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value(ma, "sco_source", NULL) &&
+ !(u->hsp.sco_source = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sco_source", NULL), PA_NAMEREG_SOURCE))) {
+ pa_log("SCO source not found");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_u32(ma, "rate", &u->sample_spec.rate) < 0 ||
+ u->sample_spec.rate <= 0 || u->sample_spec.rate > PA_RATE_MAX) {
+ pa_log_error("Failed to get rate from module arguments");
+ goto fail;
+ }
+
+ u->auto_connect = TRUE;
+ if (pa_modargs_get_value_boolean(ma, "auto_connect", &u->auto_connect)) {
+ pa_log("Failed to parse auto_connect= argument");
+ goto fail;
+ }
+
+ channels = u->sample_spec.channels;
+ if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
+ channels <= 0 || channels > PA_CHANNELS_MAX) {
+ pa_log_error("Failed to get channels from module arguments");
+ goto fail;
+ }
+ u->sample_spec.channels = (uint8_t) channels;
+ u->requested_sample_spec = u->sample_spec;
+
+ address = pa_modargs_get_value(ma, "address", NULL);
+ path = pa_modargs_get_value(ma, "path", NULL);
+
+ if (setup_dbus(u) < 0)
+ goto fail;
+
+ if (!(u->discovery = pa_bluetooth_discovery_get(m->core)))
+ goto fail;
+
+ if (!(device = find_device(u, address, path)))
+ goto fail;
+
+ /* Add the card structure. This will also initialize the default profile */
+ if (add_card(u, device) < 0)
+ goto fail;
+
+ if (!dbus_connection_add_filter(pa_dbus_connection_get(u->connection), filter_cb, u, NULL)) {
+ pa_log_error("Failed to add filter function");
+ goto fail;
+ }
+ u->filter_added = TRUE;
+
+ speaker = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='SpeakerGainChanged',path='%s'", u->path);
+ mike = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='MicrophoneGainChanged',path='%s'", u->path);
+ transport = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'");
+
+ if (pa_dbus_add_matches(
+ pa_dbus_connection_get(u->connection), &err,
+ speaker,
+ mike,
+ transport,
+ NULL) < 0) {
+
+ pa_xfree(speaker);
+ pa_xfree(mike);
+ pa_xfree(transport);
+
+ pa_log("Failed to add D-Bus matches: %s", err.message);
+ goto fail;
+ }
+
+ pa_xfree(speaker);
+ pa_xfree(mike);
+ pa_xfree(transport);
+
+ /* Connect to the BT service */
+ init_bt(u);
+
+ if (u->profile != PROFILE_OFF)
+ if (init_profile(u) < 0)
+ goto fail;
+
+ if (u->sink || u->source)
+ if (start_thread(u) < 0)
+ goto fail;
+
+ return 0;
+
+fail:
+
+ pa__done(m);
+
+ dbus_error_free(&err);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return
+ (u->sink ? pa_sink_linked_by(u->sink) : 0) +
+ (u->source ? pa_source_linked_by(u->source) : 0);
+}
+
+void pa__done(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink && !USE_SCO_OVER_PCM(u))
+ pa_sink_unlink(u->sink);
+
+ if (u->source && !USE_SCO_OVER_PCM(u))
+ pa_source_unlink(u->source);
+
+ stop_thread(u);
+
+ if (USE_SCO_OVER_PCM(u))
+ restore_sco_volume_callbacks(u);
+
+ if (u->connection) {
+
+ if (u->path) {
+ char *speaker, *mike;
+ speaker = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='SpeakerGainChanged',path='%s'", u->path);
+ mike = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='MicrophoneGainChanged',path='%s'", u->path);
+
+ pa_dbus_remove_matches(pa_dbus_connection_get(u->connection), speaker, mike, NULL);
+
+ pa_xfree(speaker);
+ pa_xfree(mike);
+ }
+
+ if (u->filter_added)
+ dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), filter_cb, u);
+
+ pa_dbus_connection_unref(u->connection);
+ }
+
+ if (u->card)
+ pa_card_free(u->card);
+
+ if (u->read_smoother)
+ pa_smoother_free(u->read_smoother);
+
+ shutdown_bt(u);
+
+ if (u->a2dp.buffer)
+ pa_xfree(u->a2dp.buffer);
+
+ sbc_finish(&u->a2dp.sbc);
+
+ if (u->modargs)
+ pa_modargs_free(u->modargs);
+
+ pa_xfree(u->address);
+ pa_xfree(u->path);
+
+ if (u->transport) {
+ bt_transport_release(u);
+ pa_xfree(u->transport);
+ }
+
+ if (u->discovery)
+ pa_bluetooth_discovery_unref(u->discovery);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/bluetooth/module-bluetooth-discover.c b/src/modules/bluetooth/module-bluetooth-discover.c
new file mode 100644
index 00000000..7b27f6bb
--- /dev/null
+++ b/src/modules/bluetooth/module-bluetooth-discover.c
@@ -0,0 +1,220 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008-2009 Joao Paulo Rechi Vita
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pulse/xmalloc.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-shared.h>
+
+#include "module-bluetooth-discover-symdef.h"
+#include "bluetooth-util.h"
+
+PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
+PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_USAGE("async=<Asynchronous initialization?> "
+ "sco_sink=<name of sink> "
+ "sco_source=<name of source> ");
+PA_MODULE_LOAD_ONCE(TRUE);
+
+static const char* const valid_modargs[] = {
+ "sco_sink",
+ "sco_source",
+ "async",
+ NULL
+};
+
+struct userdata {
+ pa_module *module;
+ pa_modargs *modargs;
+ pa_core *core;
+ pa_bluetooth_discovery *discovery;
+ pa_hook_slot *slot;
+ pa_hashmap *hashmap;
+};
+
+struct module_info {
+ char *path;
+ uint32_t module;
+};
+
+static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
+ struct module_info *mi;
+
+ pa_assert(u);
+ pa_assert(d);
+
+ mi = pa_hashmap_get(u->hashmap, d->path);
+
+ if (!d->dead && d->device_connected > 0 &&
+ (d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED ||
+ d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED ||
+ d->hfgw_state > PA_BT_AUDIO_STATE_CONNECTED)) {
+
+ if (!mi) {
+ pa_module *m = NULL;
+ char *args;
+
+ /* Oh, awesome, a new device has shown up and been connected! */
+
+ args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
+#if 0
+ /* This is in case we have to use hsp immediately, without waiting for .Audio.State = Connected */
+ if (d->headset_state >= PA_BT_AUDIO_STATE_CONNECTED && somecondition) {
+ char *tmp;
+ tmp = pa_sprintf_malloc("%s profile=\"hsp\"", args);
+ pa_xfree(args);
+ args = tmp;
+ }
+#endif
+
+ if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
+ pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
+ char *tmp;
+
+ tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
+ pa_modargs_get_value(u->modargs, "sco_sink", NULL),
+ pa_modargs_get_value(u->modargs, "sco_source", NULL));
+ pa_xfree(args);
+ args = tmp;
+ }
+
+ if (d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED)
+ args = pa_sprintf_malloc("%s profile=\"a2dp_source\" auto_connect=no", args);
+
+ if (d->hfgw_state > PA_BT_AUDIO_STATE_CONNECTED)
+ args = pa_sprintf_malloc("%s profile=\"hfgw\"", args);
+
+ pa_log_debug("Loading module-bluetooth-device %s", args);
+ m = pa_module_load(u->module->core, "module-bluetooth-device", args);
+ pa_xfree(args);
+
+ if (m) {
+ mi = pa_xnew(struct module_info, 1);
+ mi->module = m->index;
+ mi->path = pa_xstrdup(d->path);
+
+ pa_hashmap_put(u->hashmap, mi->path, mi);
+ } else
+ pa_log_debug("Failed to load module for device %s", d->path);
+ }
+
+ } else {
+
+ if (mi) {
+
+ /* Hmm, disconnection? Then let's unload our module */
+
+ pa_log_debug("Unloading module for %s", d->path);
+ pa_module_unload_request_by_index(u->core, mi->module, TRUE);
+
+ pa_hashmap_remove(u->hashmap, mi->path);
+ pa_xfree(mi->path);
+ pa_xfree(mi);
+ }
+ }
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module* m) {
+ struct userdata *u;
+ pa_modargs *ma = NULL;
+ pa_bool_t async = FALSE;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "async", &async) < 0) {
+ pa_log("Failed to parse async argument.");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ u->core = m->core;
+ u->modargs = ma;
+ ma = NULL;
+ u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ if (!(u->discovery = pa_bluetooth_discovery_get(u->core)))
+ goto fail;
+
+ u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery), PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
+
+ if (!async)
+ pa_bluetooth_discovery_sync(u->discovery);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module* m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->slot)
+ pa_hook_slot_free(u->slot);
+
+ if (u->discovery)
+ pa_bluetooth_discovery_unref(u->discovery);
+
+ if (u->hashmap) {
+ struct module_info *mi;
+
+ while ((mi = pa_hashmap_steal_first(u->hashmap))) {
+ pa_xfree(mi->path);
+ pa_xfree(mi);
+ }
+
+ pa_hashmap_free(u->hashmap, NULL, NULL);
+ }
+
+ if (u->modargs)
+ pa_modargs_free(u->modargs);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/bluetooth/module-bluetooth-proximity.c b/src/modules/bluetooth/module-bluetooth-proximity.c
new file mode 100644
index 00000000..8c3a5b9f
--- /dev/null
+++ b/src/modules/bluetooth/module-bluetooth-proximity.c
@@ -0,0 +1,487 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2005-2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <pulse/xmalloc.h>
+#include <pulsecore/module.h>
+#include <pulsecore/log.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/start-child.h>
+#include <pulsecore/dbus-shared.h>
+
+#include "module-bluetooth-proximity-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Bluetooth Proximity Volume Control");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(
+ "sink=<sink name> "
+ "hci=<hci device> "
+);
+
+#define DEFAULT_HCI "hci0"
+
+static const char* const valid_modargs[] = {
+ "sink",
+ "hci",
+ NULL,
+};
+
+struct bonding {
+ struct userdata *userdata;
+ char address[18];
+
+ pid_t pid;
+ int fd;
+
+ pa_io_event *io_event;
+
+ enum {
+ UNKNOWN,
+ FOUND,
+ NOT_FOUND
+ } state;
+};
+
+struct userdata {
+ pa_module *module;
+ pa_dbus_connection *dbus_connection;
+
+ char *sink_name;
+ char *hci, *hci_path;
+
+ pa_hashmap *bondings;
+
+ unsigned n_found;
+ unsigned n_unknown;
+
+ pa_bool_t muted:1;
+ pa_bool_t filter_added:1;
+};
+
+static void update_volume(struct userdata *u) {
+ pa_assert(u);
+
+ if (u->muted && u->n_found > 0) {
+ pa_sink *s;
+
+ u->muted = FALSE;
+
+ if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK))) {
+ pa_log_warn("Sink device '%s' not available for unmuting.", pa_strnull(u->sink_name));
+ return;
+ }
+
+ pa_log_info("Found %u BT devices, unmuting.", u->n_found);
+ pa_sink_set_mute(s, FALSE, FALSE);
+
+ } else if (!u->muted && (u->n_found+u->n_unknown) <= 0) {
+ pa_sink *s;
+
+ u->muted = TRUE;
+
+ if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK))) {
+ pa_log_warn("Sink device '%s' not available for muting.", pa_strnull(u->sink_name));
+ return;
+ }
+
+ pa_log_info("No BT devices found, muting.");
+ pa_sink_set_mute(s, TRUE, FALSE);
+
+ } else
+ pa_log_info("%u devices now active, %u with unknown state.", u->n_found, u->n_unknown);
+}
+
+static void bonding_free(struct bonding *b) {
+ pa_assert(b);
+
+ if (b->state == FOUND)
+ pa_assert_se(b->userdata->n_found-- >= 1);
+
+ if (b->state == UNKNOWN)
+ pa_assert_se(b->userdata->n_unknown-- >= 1);
+
+ if (b->pid != (pid_t) -1) {
+ kill(b->pid, SIGTERM);
+ waitpid(b->pid, NULL, 0);
+ }
+
+ if (b->fd >= 0)
+ pa_close(b->fd);
+
+ if (b->io_event)
+ b->userdata->module->core->mainloop->io_free(b->io_event);
+
+ pa_xfree(b);
+}
+
+static void io_event_cb(
+ pa_mainloop_api*a,
+ pa_io_event* e,
+ int fd,
+ pa_io_event_flags_t events,
+ void *userdata) {
+
+ struct bonding *b = userdata;
+ char x;
+ ssize_t r;
+
+ pa_assert(b);
+
+ if ((r = read(fd, &x, 1)) <= 0) {
+ pa_log_warn("Child watching '%s' died abnormally: %s", b->address, r == 0 ? "EOF" : pa_cstrerror(errno));
+
+ pa_assert_se(pa_hashmap_remove(b->userdata->bondings, b->address) == b);
+ bonding_free(b);
+ return;
+ }
+
+ pa_assert_se(r == 1);
+
+ if (b->state == UNKNOWN)
+ pa_assert_se(b->userdata->n_unknown-- >= 1);
+
+ if (x == '+') {
+ pa_assert(b->state == UNKNOWN || b->state == NOT_FOUND);
+
+ b->state = FOUND;
+ b->userdata->n_found++;
+
+ pa_log_info("Device '%s' is alive.", b->address);
+
+ } else {
+ pa_assert(x == '-');
+ pa_assert(b->state == UNKNOWN || b->state == FOUND);
+
+ if (b->state == FOUND)
+ b->userdata->n_found--;
+
+ b->state = NOT_FOUND;
+
+ pa_log_info("Device '%s' is dead.", b->address);
+ }
+
+ update_volume(b->userdata);
+}
+
+static struct bonding* bonding_new(struct userdata *u, const char *a) {
+ struct bonding *b = NULL;
+ DBusMessage *m = NULL, *r = NULL;
+ DBusError e;
+ const char *class;
+
+ pa_assert(u);
+ pa_assert(a);
+
+ pa_return_val_if_fail(strlen(a) == 17, NULL);
+ pa_return_val_if_fail(!pa_hashmap_get(u->bondings, a), NULL);
+
+ dbus_error_init(&e);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->hci_path, "org.bluez.Adapter", "GetRemoteMajorClass"));
+ pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_STRING, &a, DBUS_TYPE_INVALID));
+ r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->dbus_connection), m, -1, &e);
+
+ if (!r) {
+ pa_log("org.bluez.Adapter.GetRemoteMajorClass(%s) failed: %s", a, e.message);
+ goto fail;
+ }
+
+ if (!(dbus_message_get_args(r, &e, DBUS_TYPE_STRING, &class, DBUS_TYPE_INVALID))) {
+ pa_log("Malformed org.bluez.Adapter.GetRemoteMajorClass signal: %s", e.message);
+ goto fail;
+ }
+
+ if (strcmp(class, "phone")) {
+ pa_log_info("Found device '%s' of class '%s', ignoring.", a, class);
+ goto fail;
+ }
+
+ b = pa_xnew(struct bonding, 1);
+ b->userdata = u;
+ pa_strlcpy(b->address, a, sizeof(b->address));
+ b->pid = (pid_t) -1;
+ b->fd = -1;
+ b->io_event = NULL;
+ b->state = UNKNOWN;
+ u->n_unknown ++;
+
+ pa_log_info("Watching device '%s' of class '%s'.", b->address, class);
+
+ if ((b->fd = pa_start_child_for_read(PA_BT_PROXIMITY_HELPER, a, &b->pid)) < 0) {
+ pa_log("Failed to start helper tool.");
+ goto fail;
+ }
+
+ b->io_event = u->module->core->mainloop->io_new(
+ u->module->core->mainloop,
+ b->fd,
+ PA_IO_EVENT_INPUT,
+ io_event_cb,
+ b);
+
+ dbus_message_unref(m);
+ dbus_message_unref(r);
+
+ pa_hashmap_put(u->bondings, b->address, b);
+
+ return b;
+
+fail:
+ if (m)
+ dbus_message_unref(m);
+ if (r)
+ dbus_message_unref(r);
+
+ if (b)
+ bonding_free(b);
+
+ dbus_error_free(&e);
+ return NULL;
+}
+
+static void bonding_remove(struct userdata *u, const char *a) {
+ struct bonding *b;
+ pa_assert(u);
+
+ pa_return_if_fail((b = pa_hashmap_remove(u->bondings, a)));
+
+ pa_log_info("No longer watching device '%s'", b->address);
+ bonding_free(b);
+}
+
+static DBusHandlerResult filter_func(DBusConnection *connection, DBusMessage *m, void *userdata) {
+ struct userdata *u = userdata;
+ DBusError e;
+
+ dbus_error_init(&e);
+
+ if (dbus_message_is_signal(m, "org.bluez.Adapter", "BondingCreated")) {
+ const char *a;
+
+ if (!(dbus_message_get_args(m, &e, DBUS_TYPE_STRING, &a, DBUS_TYPE_INVALID))) {
+ pa_log("Malformed org.bluez.Adapter.BondingCreated signal: %s", e.message);
+ goto finish;
+ }
+
+ bonding_new(u, a);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ } else if (dbus_message_is_signal(m, "org.bluez.Adapter", "BondingRemoved")) {
+
+ const char *a;
+
+ if (!(dbus_message_get_args(m, &e, DBUS_TYPE_STRING, &a, DBUS_TYPE_INVALID))) {
+ pa_log("Malformed org.bluez.Adapter.BondingRemoved signal: %s", e.message);
+ goto finish;
+ }
+
+ bonding_remove(u, a);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ }
+
+finish:
+
+ dbus_error_free(&e);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static int add_matches(struct userdata *u, pa_bool_t add) {
+ char *filter1, *filter2;
+ DBusError e;
+ int r = -1;
+
+ pa_assert(u);
+ dbus_error_init(&e);
+
+ filter1 = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='BondingCreated',path='%s'", u->hci_path);
+ filter2 = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='BondingRemoved',path='%s'", u->hci_path);
+
+ if (add) {
+ dbus_bus_add_match(pa_dbus_connection_get(u->dbus_connection), filter1, &e);
+
+ if (dbus_error_is_set(&e)) {
+ pa_log("dbus_bus_add_match(%s) failed: %s", filter1, e.message);
+ goto finish;
+ }
+ } else
+ dbus_bus_remove_match(pa_dbus_connection_get(u->dbus_connection), filter1, &e);
+
+
+ if (add) {
+ dbus_bus_add_match(pa_dbus_connection_get(u->dbus_connection), filter2, &e);
+
+ if (dbus_error_is_set(&e)) {
+ pa_log("dbus_bus_add_match(%s) failed: %s", filter2, e.message);
+ dbus_bus_remove_match(pa_dbus_connection_get(u->dbus_connection), filter2, &e);
+ goto finish;
+ }
+ } else
+ dbus_bus_remove_match(pa_dbus_connection_get(u->dbus_connection), filter2, &e);
+
+ if (add) {
+ pa_assert_se(dbus_connection_add_filter(pa_dbus_connection_get(u->dbus_connection), filter_func, u, NULL));
+ u->filter_added = TRUE;
+ } else if (u->filter_added)
+ dbus_connection_remove_filter(pa_dbus_connection_get(u->dbus_connection), filter_func, u);
+
+ r = 0;
+
+finish:
+ pa_xfree(filter1);
+ pa_xfree(filter2);
+ dbus_error_free(&e);
+
+ return r;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+ DBusError e;
+ DBusMessage *msg = NULL, *r = NULL;
+ DBusMessageIter iter, sub;
+
+ pa_assert(m);
+ dbus_error_init(&e);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
+ u->hci = pa_xstrdup(pa_modargs_get_value(ma, "hci", DEFAULT_HCI));
+ u->hci_path = pa_sprintf_malloc("/org/bluez/%s", u->hci);
+ u->bondings = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ if (!(u->dbus_connection = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &e))) {
+ pa_log("Failed to get D-Bus connection: %s", e.message);
+ goto fail;
+ }
+
+ if (add_matches(u, TRUE) < 0)
+ goto fail;
+
+ pa_assert_se(msg = dbus_message_new_method_call("org.bluez", u->hci_path, "org.bluez.Adapter", "ListBondings"));
+
+ if (!(r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->dbus_connection), msg, -1, &e))) {
+ pa_log("org.bluez.Adapter.ListBondings failed: %s", e.message);
+ goto fail;
+ }
+
+ dbus_message_iter_init(r, &iter);
+
+ if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
+ pa_log("Malformed reply to org.bluez.Adapter.ListBondings.");
+ goto fail;
+ }
+
+ dbus_message_iter_recurse(&iter, &sub);
+
+ while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING) {
+ const char *a = NULL;
+
+ dbus_message_iter_get_basic(&sub, &a);
+ bonding_new(u, a);
+
+ dbus_message_iter_next(&sub);
+ }
+
+ dbus_message_unref(r);
+ dbus_message_unref(msg);
+
+ pa_modargs_free(ma);
+
+ if (pa_hashmap_size(u->bondings) == 0)
+ pa_log_warn("Warning: no phone device bonded.");
+
+ update_volume(u);
+
+ return 0;
+
+fail:
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ dbus_error_free(&e);
+
+ if (msg)
+ dbus_message_unref(msg);
+
+ if (r)
+ dbus_message_unref(r);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->bondings) {
+ struct bonding *b;
+
+ while ((b = pa_hashmap_steal_first(u->bondings)))
+ bonding_free(b);
+
+ pa_hashmap_free(u->bondings, NULL, NULL);
+ }
+
+ if (u->dbus_connection) {
+ add_matches(u, FALSE);
+ pa_dbus_connection_unref(u->dbus_connection);
+ }
+
+ pa_xfree(u->sink_name);
+ pa_xfree(u->hci_path);
+ pa_xfree(u->hci);
+ pa_xfree(u);
+}
diff --git a/src/modules/bluetooth/proximity-helper.c b/src/modules/bluetooth/proximity-helper.c
new file mode 100644
index 00000000..3767f01c
--- /dev/null
+++ b/src/modules/bluetooth/proximity-helper.c
@@ -0,0 +1,202 @@
+/*
+ * Small SUID helper that allows us to ping a BT device. Borrows
+ * heavily from bluez-utils' l2ping, which is licensed as GPL2+
+ * and comes with a copyright like this:
+ *
+ * Copyright (C) 2000-2001 Qualcomm Incorporated
+ * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
+ * Copyright (C) 2002-2007 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#undef NDEBUG
+
+#include <assert.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <sys/select.h>
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/hci.h>
+#include <bluetooth/hci_lib.h>
+#include <bluetooth/l2cap.h>
+
+#define PING_STRING "PulseAudio"
+#define IDENT 200
+#define TIMEOUT 4
+#define INTERVAL 2
+
+static void update_status(int found) {
+ static int status = -1;
+
+ if (!found && status != 0)
+ printf("-");
+ if (found && status <= 0)
+ printf("+");
+
+ fflush(stdout);
+ status = !!found;
+}
+
+int main(int argc, char *argv[]) {
+ struct sockaddr_l2 addr;
+ union {
+ l2cap_cmd_hdr hdr;
+ uint8_t buf[L2CAP_CMD_HDR_SIZE + sizeof(PING_STRING)];
+ } packet;
+ int fd = -1;
+ uint8_t id = IDENT;
+ int connected = 0;
+
+ assert(argc == 2);
+
+ for (;;) {
+ fd_set fds;
+ struct timeval end;
+ ssize_t r;
+
+ if (!connected) {
+
+ if (fd >= 0)
+ close(fd);
+
+ if ((fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP)) < 0) {
+ fprintf(stderr, "socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP) failed: %s", strerror(errno));
+ goto finish;
+ }
+
+ memset(&addr, 0, sizeof(addr));
+ addr.l2_family = AF_BLUETOOTH;
+ bacpy(&addr.l2_bdaddr, BDADDR_ANY);
+
+ if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ fprintf(stderr, "bind() failed: %s", strerror(errno));
+ goto finish;
+ }
+
+ memset(&addr, 0, sizeof(addr));
+ addr.l2_family = AF_BLUETOOTH;
+ str2ba(argv[1], &addr.l2_bdaddr);
+
+ if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+
+ if (errno == EHOSTDOWN || errno == ECONNRESET || errno == ETIMEDOUT) {
+ update_status(0);
+ sleep(INTERVAL);
+ continue;
+ }
+
+ fprintf(stderr, "connect() failed: %s", strerror(errno));
+ goto finish;
+ }
+
+ connected = 1;
+ }
+
+ assert(connected);
+
+ memset(&packet, 0, sizeof(packet));
+ strcpy((char*) packet.buf + L2CAP_CMD_HDR_SIZE, PING_STRING);
+ packet.hdr.ident = id;
+ packet.hdr.len = htobs(sizeof(PING_STRING));
+ packet.hdr.code = L2CAP_ECHO_REQ;
+
+ if ((r = send(fd, &packet, sizeof(packet), 0)) < 0) {
+
+ if (errno == EHOSTDOWN || errno == ECONNRESET || errno == ETIMEDOUT) {
+ update_status(0);
+ connected = 0;
+ sleep(INTERVAL);
+ continue;
+ }
+
+ fprintf(stderr, "send() failed: %s", strerror(errno));
+ goto finish;
+ }
+
+ assert(r == sizeof(packet));
+
+ gettimeofday(&end, NULL);
+ end.tv_sec += TIMEOUT;
+
+ for (;;) {
+ struct timeval now, delta;
+
+ gettimeofday(&now, NULL);
+
+ if (timercmp(&end, &now, <=)) {
+ update_status(0);
+ connected = 0;
+ sleep(INTERVAL);
+ break;
+ }
+
+ timersub(&end, &now, &delta);
+
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+
+ if (select(fd+1, &fds, NULL, NULL, &delta) < 0) {
+ fprintf(stderr, "select() failed: %s", strerror(errno));
+ goto finish;
+ }
+
+ if ((r = recv(fd, &packet, sizeof(packet), 0)) <= 0) {
+
+ if (errno == EHOSTDOWN || errno == ECONNRESET || errno == ETIMEDOUT) {
+ update_status(0);
+ connected = 0;
+ sleep(INTERVAL);
+ break;
+ }
+
+ fprintf(stderr, "send() failed: %s", r == 0 ? "EOF" : strerror(errno));
+ goto finish;
+ }
+
+ assert(r >= L2CAP_CMD_HDR_SIZE);
+
+ if (packet.hdr.ident != id)
+ continue;
+
+ if (packet.hdr.code == L2CAP_ECHO_RSP || packet.hdr.code == L2CAP_COMMAND_REJ) {
+
+ if (++id >= 0xFF)
+ id = IDENT;
+
+ update_status(1);
+ sleep(INTERVAL);
+ break;
+ }
+ }
+ }
+
+finish:
+
+ if (fd >= 0)
+ close(fd);
+
+ return 1;
+}
diff --git a/src/modules/bluetooth/rtp.h b/src/modules/bluetooth/rtp.h
new file mode 100644
index 00000000..45fddcf1
--- /dev/null
+++ b/src/modules/bluetooth/rtp.h
@@ -0,0 +1,76 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+
+struct rtp_header {
+ unsigned cc:4;
+ unsigned x:1;
+ unsigned p:1;
+ unsigned v:2;
+
+ unsigned pt:7;
+ unsigned m:1;
+
+ uint16_t sequence_number;
+ uint32_t timestamp;
+ uint32_t ssrc;
+ uint32_t csrc[0];
+} __attribute__ ((packed));
+
+struct rtp_payload {
+ unsigned frame_count:4;
+ unsigned rfa0:1;
+ unsigned is_last_fragment:1;
+ unsigned is_first_fragment:1;
+ unsigned is_fragmented:1;
+} __attribute__ ((packed));
+
+#elif __BYTE_ORDER == __BIG_ENDIAN
+
+struct rtp_header {
+ unsigned v:2;
+ unsigned p:1;
+ unsigned x:1;
+ unsigned cc:4;
+
+ unsigned m:1;
+ unsigned pt:7;
+
+ uint16_t sequence_number;
+ uint32_t timestamp;
+ uint32_t ssrc;
+ uint32_t csrc[0];
+} __attribute__ ((packed));
+
+struct rtp_payload {
+ unsigned is_fragmented:1;
+ unsigned is_first_fragment:1;
+ unsigned is_last_fragment:1;
+ unsigned rfa0:1;
+ unsigned frame_count:4;
+} __attribute__ ((packed));
+
+#else
+#error "Unknown byte order"
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc.c b/src/modules/bluetooth/sbc/sbc.c
new file mode 100644
index 00000000..77fcc5d1
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc.c
@@ -0,0 +1,1234 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2008 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+/* todo items:
+
+ use a log2 table for byte integer scale factors calculation (sum log2 results
+ for high and low bytes) fill bitpool by 16 bits instead of one at a time in
+ bits allocation/bitpool generation port to the dsp
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <limits.h>
+
+#include "sbc_math.h"
+#include "sbc_tables.h"
+
+#include "sbc.h"
+#include "sbc_primitives.h"
+
+#define SBC_SYNCWORD 0x9C
+
+/* This structure contains an unpacked SBC frame.
+ Yes, there is probably quite some unused space herein */
+struct sbc_frame {
+ uint8_t frequency;
+ uint8_t block_mode;
+ uint8_t blocks;
+ enum {
+ MONO = SBC_MODE_MONO,
+ DUAL_CHANNEL = SBC_MODE_DUAL_CHANNEL,
+ STEREO = SBC_MODE_STEREO,
+ JOINT_STEREO = SBC_MODE_JOINT_STEREO
+ } mode;
+ uint8_t channels;
+ enum {
+ LOUDNESS = SBC_AM_LOUDNESS,
+ SNR = SBC_AM_SNR
+ } allocation;
+ uint8_t subband_mode;
+ uint8_t subbands;
+ uint8_t bitpool;
+ uint16_t codesize;
+ uint8_t length;
+
+ /* bit number x set means joint stereo has been used in subband x */
+ uint8_t joint;
+
+ /* only the lower 4 bits of every element are to be used */
+ uint32_t SBC_ALIGNED scale_factor[2][8];
+
+ /* raw integer subband samples in the frame */
+ int32_t SBC_ALIGNED sb_sample_f[16][2][8];
+
+ /* modified subband samples */
+ int32_t SBC_ALIGNED sb_sample[16][2][8];
+
+ /* original pcm audio samples */
+ int16_t SBC_ALIGNED pcm_sample[2][16*8];
+};
+
+struct sbc_decoder_state {
+ int subbands;
+ int32_t V[2][170];
+ int offset[2][16];
+};
+
+/*
+ * Calculates the CRC-8 of the first len bits in data
+ */
+static const uint8_t crc_table[256] = {
+ 0x00, 0x1D, 0x3A, 0x27, 0x74, 0x69, 0x4E, 0x53,
+ 0xE8, 0xF5, 0xD2, 0xCF, 0x9C, 0x81, 0xA6, 0xBB,
+ 0xCD, 0xD0, 0xF7, 0xEA, 0xB9, 0xA4, 0x83, 0x9E,
+ 0x25, 0x38, 0x1F, 0x02, 0x51, 0x4C, 0x6B, 0x76,
+ 0x87, 0x9A, 0xBD, 0xA0, 0xF3, 0xEE, 0xC9, 0xD4,
+ 0x6F, 0x72, 0x55, 0x48, 0x1B, 0x06, 0x21, 0x3C,
+ 0x4A, 0x57, 0x70, 0x6D, 0x3E, 0x23, 0x04, 0x19,
+ 0xA2, 0xBF, 0x98, 0x85, 0xD6, 0xCB, 0xEC, 0xF1,
+ 0x13, 0x0E, 0x29, 0x34, 0x67, 0x7A, 0x5D, 0x40,
+ 0xFB, 0xE6, 0xC1, 0xDC, 0x8F, 0x92, 0xB5, 0xA8,
+ 0xDE, 0xC3, 0xE4, 0xF9, 0xAA, 0xB7, 0x90, 0x8D,
+ 0x36, 0x2B, 0x0C, 0x11, 0x42, 0x5F, 0x78, 0x65,
+ 0x94, 0x89, 0xAE, 0xB3, 0xE0, 0xFD, 0xDA, 0xC7,
+ 0x7C, 0x61, 0x46, 0x5B, 0x08, 0x15, 0x32, 0x2F,
+ 0x59, 0x44, 0x63, 0x7E, 0x2D, 0x30, 0x17, 0x0A,
+ 0xB1, 0xAC, 0x8B, 0x96, 0xC5, 0xD8, 0xFF, 0xE2,
+ 0x26, 0x3B, 0x1C, 0x01, 0x52, 0x4F, 0x68, 0x75,
+ 0xCE, 0xD3, 0xF4, 0xE9, 0xBA, 0xA7, 0x80, 0x9D,
+ 0xEB, 0xF6, 0xD1, 0xCC, 0x9F, 0x82, 0xA5, 0xB8,
+ 0x03, 0x1E, 0x39, 0x24, 0x77, 0x6A, 0x4D, 0x50,
+ 0xA1, 0xBC, 0x9B, 0x86, 0xD5, 0xC8, 0xEF, 0xF2,
+ 0x49, 0x54, 0x73, 0x6E, 0x3D, 0x20, 0x07, 0x1A,
+ 0x6C, 0x71, 0x56, 0x4B, 0x18, 0x05, 0x22, 0x3F,
+ 0x84, 0x99, 0xBE, 0xA3, 0xF0, 0xED, 0xCA, 0xD7,
+ 0x35, 0x28, 0x0F, 0x12, 0x41, 0x5C, 0x7B, 0x66,
+ 0xDD, 0xC0, 0xE7, 0xFA, 0xA9, 0xB4, 0x93, 0x8E,
+ 0xF8, 0xE5, 0xC2, 0xDF, 0x8C, 0x91, 0xB6, 0xAB,
+ 0x10, 0x0D, 0x2A, 0x37, 0x64, 0x79, 0x5E, 0x43,
+ 0xB2, 0xAF, 0x88, 0x95, 0xC6, 0xDB, 0xFC, 0xE1,
+ 0x5A, 0x47, 0x60, 0x7D, 0x2E, 0x33, 0x14, 0x09,
+ 0x7F, 0x62, 0x45, 0x58, 0x0B, 0x16, 0x31, 0x2C,
+ 0x97, 0x8A, 0xAD, 0xB0, 0xE3, 0xFE, 0xD9, 0xC4
+};
+
+static uint8_t sbc_crc8(const uint8_t *data, size_t len)
+{
+ uint8_t crc = 0x0f;
+ size_t i;
+ uint8_t octet;
+
+ for (i = 0; i < len / 8; i++)
+ crc = crc_table[crc ^ data[i]];
+
+ octet = data[i];
+ for (i = 0; i < len % 8; i++) {
+ char bit = ((octet ^ crc) & 0x80) >> 7;
+
+ crc = ((crc & 0x7f) << 1) ^ (bit ? 0x1d : 0);
+
+ octet = octet << 1;
+ }
+
+ return crc;
+}
+
+/*
+ * Code straight from the spec to calculate the bits array
+ * Takes a pointer to the frame in question, a pointer to the bits array and
+ * the sampling frequency (as 2 bit integer)
+ */
+static SBC_ALWAYS_INLINE void sbc_calculate_bits_internal(
+ const struct sbc_frame *frame, int (*bits)[8], int subbands)
+{
+ uint8_t sf = frame->frequency;
+
+ if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) {
+ int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
+ int ch, sb;
+
+ for (ch = 0; ch < frame->channels; ch++) {
+ max_bitneed = 0;
+ if (frame->allocation == SNR) {
+ for (sb = 0; sb < subbands; sb++) {
+ bitneed[ch][sb] = frame->scale_factor[ch][sb];
+ if (bitneed[ch][sb] > max_bitneed)
+ max_bitneed = bitneed[ch][sb];
+ }
+ } else {
+ for (sb = 0; sb < subbands; sb++) {
+ if (frame->scale_factor[ch][sb] == 0)
+ bitneed[ch][sb] = -5;
+ else {
+ if (subbands == 4)
+ loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
+ else
+ loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
+ if (loudness > 0)
+ bitneed[ch][sb] = loudness / 2;
+ else
+ bitneed[ch][sb] = loudness;
+ }
+ if (bitneed[ch][sb] > max_bitneed)
+ max_bitneed = bitneed[ch][sb];
+ }
+ }
+
+ bitcount = 0;
+ slicecount = 0;
+ bitslice = max_bitneed + 1;
+ do {
+ bitslice--;
+ bitcount += slicecount;
+ slicecount = 0;
+ for (sb = 0; sb < subbands; sb++) {
+ if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
+ slicecount++;
+ else if (bitneed[ch][sb] == bitslice + 1)
+ slicecount += 2;
+ }
+ } while (bitcount + slicecount < frame->bitpool);
+
+ if (bitcount + slicecount == frame->bitpool) {
+ bitcount += slicecount;
+ bitslice--;
+ }
+
+ for (sb = 0; sb < subbands; sb++) {
+ if (bitneed[ch][sb] < bitslice + 2)
+ bits[ch][sb] = 0;
+ else {
+ bits[ch][sb] = bitneed[ch][sb] - bitslice;
+ if (bits[ch][sb] > 16)
+ bits[ch][sb] = 16;
+ }
+ }
+
+ for (sb = 0; bitcount < frame->bitpool &&
+ sb < subbands; sb++) {
+ if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
+ bits[ch][sb]++;
+ bitcount++;
+ } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
+ bits[ch][sb] = 2;
+ bitcount += 2;
+ }
+ }
+
+ for (sb = 0; bitcount < frame->bitpool &&
+ sb < subbands; sb++) {
+ if (bits[ch][sb] < 16) {
+ bits[ch][sb]++;
+ bitcount++;
+ }
+ }
+
+ }
+
+ } else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) {
+ int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
+ int ch, sb;
+
+ max_bitneed = 0;
+ if (frame->allocation == SNR) {
+ for (ch = 0; ch < 2; ch++) {
+ for (sb = 0; sb < subbands; sb++) {
+ bitneed[ch][sb] = frame->scale_factor[ch][sb];
+ if (bitneed[ch][sb] > max_bitneed)
+ max_bitneed = bitneed[ch][sb];
+ }
+ }
+ } else {
+ for (ch = 0; ch < 2; ch++) {
+ for (sb = 0; sb < subbands; sb++) {
+ if (frame->scale_factor[ch][sb] == 0)
+ bitneed[ch][sb] = -5;
+ else {
+ if (subbands == 4)
+ loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
+ else
+ loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
+ if (loudness > 0)
+ bitneed[ch][sb] = loudness / 2;
+ else
+ bitneed[ch][sb] = loudness;
+ }
+ if (bitneed[ch][sb] > max_bitneed)
+ max_bitneed = bitneed[ch][sb];
+ }
+ }
+ }
+
+ bitcount = 0;
+ slicecount = 0;
+ bitslice = max_bitneed + 1;
+ do {
+ bitslice--;
+ bitcount += slicecount;
+ slicecount = 0;
+ for (ch = 0; ch < 2; ch++) {
+ for (sb = 0; sb < subbands; sb++) {
+ if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
+ slicecount++;
+ else if (bitneed[ch][sb] == bitslice + 1)
+ slicecount += 2;
+ }
+ }
+ } while (bitcount + slicecount < frame->bitpool);
+
+ if (bitcount + slicecount == frame->bitpool) {
+ bitcount += slicecount;
+ bitslice--;
+ }
+
+ for (ch = 0; ch < 2; ch++) {
+ for (sb = 0; sb < subbands; sb++) {
+ if (bitneed[ch][sb] < bitslice + 2) {
+ bits[ch][sb] = 0;
+ } else {
+ bits[ch][sb] = bitneed[ch][sb] - bitslice;
+ if (bits[ch][sb] > 16)
+ bits[ch][sb] = 16;
+ }
+ }
+ }
+
+ ch = 0;
+ sb = 0;
+ while (bitcount < frame->bitpool) {
+ if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
+ bits[ch][sb]++;
+ bitcount++;
+ } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
+ bits[ch][sb] = 2;
+ bitcount += 2;
+ }
+ if (ch == 1) {
+ ch = 0;
+ sb++;
+ if (sb >= subbands)
+ break;
+ } else
+ ch = 1;
+ }
+
+ ch = 0;
+ sb = 0;
+ while (bitcount < frame->bitpool) {
+ if (bits[ch][sb] < 16) {
+ bits[ch][sb]++;
+ bitcount++;
+ }
+ if (ch == 1) {
+ ch = 0;
+ sb++;
+ if (sb >= subbands)
+ break;
+ } else
+ ch = 1;
+ }
+
+ }
+
+}
+
+static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8])
+{
+ if (frame->subbands == 4)
+ sbc_calculate_bits_internal(frame, bits, 4);
+ else
+ sbc_calculate_bits_internal(frame, bits, 8);
+}
+
+/*
+ * Unpacks a SBC frame at the beginning of the stream in data,
+ * which has at most len bytes into frame.
+ * Returns the length in bytes of the packed frame, or a negative
+ * value on error. The error codes are:
+ *
+ * -1 Data stream too short
+ * -2 Sync byte incorrect
+ * -3 CRC8 incorrect
+ * -4 Bitpool value out of bounds
+ */
+static int sbc_unpack_frame(const uint8_t *data, struct sbc_frame *frame,
+ size_t len)
+{
+ unsigned int consumed;
+ /* Will copy the parts of the header that are relevant to crc
+ * calculation here */
+ uint8_t crc_header[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+ int crc_pos = 0;
+ int32_t temp;
+
+ int audio_sample;
+ int ch, sb, blk, bit; /* channel, subband, block and bit standard
+ counters */
+ int bits[2][8]; /* bits distribution */
+ uint32_t levels[2][8]; /* levels derived from that */
+
+ if (len < 4)
+ return -1;
+
+ if (data[0] != SBC_SYNCWORD)
+ return -2;
+
+ frame->frequency = (data[1] >> 6) & 0x03;
+
+ frame->block_mode = (data[1] >> 4) & 0x03;
+ switch (frame->block_mode) {
+ case SBC_BLK_4:
+ frame->blocks = 4;
+ break;
+ case SBC_BLK_8:
+ frame->blocks = 8;
+ break;
+ case SBC_BLK_12:
+ frame->blocks = 12;
+ break;
+ case SBC_BLK_16:
+ frame->blocks = 16;
+ break;
+ }
+
+ frame->mode = (data[1] >> 2) & 0x03;
+ switch (frame->mode) {
+ case MONO:
+ frame->channels = 1;
+ break;
+ case DUAL_CHANNEL: /* fall-through */
+ case STEREO:
+ case JOINT_STEREO:
+ frame->channels = 2;
+ break;
+ }
+
+ frame->allocation = (data[1] >> 1) & 0x01;
+
+ frame->subband_mode = (data[1] & 0x01);
+ frame->subbands = frame->subband_mode ? 8 : 4;
+
+ frame->bitpool = data[2];
+
+ if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
+ frame->bitpool > 16 * frame->subbands)
+ return -4;
+
+ if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
+ frame->bitpool > 32 * frame->subbands)
+ return -4;
+
+ /* data[3] is crc, we're checking it later */
+
+ consumed = 32;
+
+ crc_header[0] = data[1];
+ crc_header[1] = data[2];
+ crc_pos = 16;
+
+ if (frame->mode == JOINT_STEREO) {
+ if (len * 8 < consumed + frame->subbands)
+ return -1;
+
+ frame->joint = 0x00;
+ for (sb = 0; sb < frame->subbands - 1; sb++)
+ frame->joint |= ((data[4] >> (7 - sb)) & 0x01) << sb;
+ if (frame->subbands == 4)
+ crc_header[crc_pos / 8] = data[4] & 0xf0;
+ else
+ crc_header[crc_pos / 8] = data[4];
+
+ consumed += frame->subbands;
+ crc_pos += frame->subbands;
+ }
+
+ if (len * 8 < consumed + (4 * frame->subbands * frame->channels))
+ return -1;
+
+ for (ch = 0; ch < frame->channels; ch++) {
+ for (sb = 0; sb < frame->subbands; sb++) {
+ /* FIXME assert(consumed % 4 == 0); */
+ frame->scale_factor[ch][sb] =
+ (data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
+ crc_header[crc_pos >> 3] |=
+ frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
+
+ consumed += 4;
+ crc_pos += 4;
+ }
+ }
+
+ if (data[3] != sbc_crc8(crc_header, crc_pos))
+ return -3;
+
+ sbc_calculate_bits(frame, bits);
+
+ for (ch = 0; ch < frame->channels; ch++) {
+ for (sb = 0; sb < frame->subbands; sb++)
+ levels[ch][sb] = (1 << bits[ch][sb]) - 1;
+ }
+
+ for (blk = 0; blk < frame->blocks; blk++) {
+ for (ch = 0; ch < frame->channels; ch++) {
+ for (sb = 0; sb < frame->subbands; sb++) {
+ if (levels[ch][sb] > 0) {
+ audio_sample = 0;
+ for (bit = 0; bit < bits[ch][sb]; bit++) {
+ if (consumed > len * 8)
+ return -1;
+
+ if ((data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01)
+ audio_sample |= 1 << (bits[ch][sb] - bit - 1);
+
+ consumed++;
+ }
+
+ frame->sb_sample[blk][ch][sb] =
+ (((audio_sample << 1) | 1) << frame->scale_factor[ch][sb]) /
+ levels[ch][sb] - (1 << frame->scale_factor[ch][sb]);
+ } else
+ frame->sb_sample[blk][ch][sb] = 0;
+ }
+ }
+ }
+
+ if (frame->mode == JOINT_STEREO) {
+ for (blk = 0; blk < frame->blocks; blk++) {
+ for (sb = 0; sb < frame->subbands; sb++) {
+ if (frame->joint & (0x01 << sb)) {
+ temp = frame->sb_sample[blk][0][sb] +
+ frame->sb_sample[blk][1][sb];
+ frame->sb_sample[blk][1][sb] =
+ frame->sb_sample[blk][0][sb] -
+ frame->sb_sample[blk][1][sb];
+ frame->sb_sample[blk][0][sb] = temp;
+ }
+ }
+ }
+ }
+
+ if ((consumed & 0x7) != 0)
+ consumed += 8 - (consumed & 0x7);
+
+ return consumed >> 3;
+}
+
+static void sbc_decoder_init(struct sbc_decoder_state *state,
+ const struct sbc_frame *frame)
+{
+ int i, ch;
+
+ memset(state->V, 0, sizeof(state->V));
+ state->subbands = frame->subbands;
+
+ for (ch = 0; ch < 2; ch++)
+ for (i = 0; i < frame->subbands * 2; i++)
+ state->offset[ch][i] = (10 * i + 10);
+}
+
+static SBC_ALWAYS_INLINE int16_t sbc_clip16(int32_t s)
+{
+ if (s > 0x7FFF)
+ return 0x7FFF;
+ else if (s < -0x8000)
+ return -0x8000;
+ else
+ return s;
+}
+
+static inline void sbc_synthesize_four(struct sbc_decoder_state *state,
+ struct sbc_frame *frame, int ch, int blk)
+{
+ int i, k, idx;
+ int32_t *v = state->V[ch];
+ int *offset = state->offset[ch];
+
+ for (i = 0; i < 8; i++) {
+ /* Shifting */
+ offset[i]--;
+ if (offset[i] < 0) {
+ offset[i] = 79;
+ memcpy(v + 80, v, 9 * sizeof(*v));
+ }
+
+ /* Distribute the new matrix value to the shifted position */
+ v[offset[i]] = SCALE4_STAGED1(
+ MULA(synmatrix4[i][0], frame->sb_sample[blk][ch][0],
+ MULA(synmatrix4[i][1], frame->sb_sample[blk][ch][1],
+ MULA(synmatrix4[i][2], frame->sb_sample[blk][ch][2],
+ MUL (synmatrix4[i][3], frame->sb_sample[blk][ch][3])))));
+ }
+
+ /* Compute the samples */
+ for (idx = 0, i = 0; i < 4; i++, idx += 5) {
+ k = (i + 4) & 0xf;
+
+ /* Store in output, Q0 */
+ frame->pcm_sample[ch][blk * 4 + i] = sbc_clip16(SCALE4_STAGED1(
+ MULA(v[offset[i] + 0], sbc_proto_4_40m0[idx + 0],
+ MULA(v[offset[k] + 1], sbc_proto_4_40m1[idx + 0],
+ MULA(v[offset[i] + 2], sbc_proto_4_40m0[idx + 1],
+ MULA(v[offset[k] + 3], sbc_proto_4_40m1[idx + 1],
+ MULA(v[offset[i] + 4], sbc_proto_4_40m0[idx + 2],
+ MULA(v[offset[k] + 5], sbc_proto_4_40m1[idx + 2],
+ MULA(v[offset[i] + 6], sbc_proto_4_40m0[idx + 3],
+ MULA(v[offset[k] + 7], sbc_proto_4_40m1[idx + 3],
+ MULA(v[offset[i] + 8], sbc_proto_4_40m0[idx + 4],
+ MUL( v[offset[k] + 9], sbc_proto_4_40m1[idx + 4]))))))))))));
+ }
+}
+
+static inline void sbc_synthesize_eight(struct sbc_decoder_state *state,
+ struct sbc_frame *frame, int ch, int blk)
+{
+ int i, j, k, idx;
+ int *offset = state->offset[ch];
+
+ for (i = 0; i < 16; i++) {
+ /* Shifting */
+ offset[i]--;
+ if (offset[i] < 0) {
+ offset[i] = 159;
+ for (j = 0; j < 9; j++)
+ state->V[ch][j + 160] = state->V[ch][j];
+ }
+
+ /* Distribute the new matrix value to the shifted position */
+ state->V[ch][offset[i]] = SCALE8_STAGED1(
+ MULA(synmatrix8[i][0], frame->sb_sample[blk][ch][0],
+ MULA(synmatrix8[i][1], frame->sb_sample[blk][ch][1],
+ MULA(synmatrix8[i][2], frame->sb_sample[blk][ch][2],
+ MULA(synmatrix8[i][3], frame->sb_sample[blk][ch][3],
+ MULA(synmatrix8[i][4], frame->sb_sample[blk][ch][4],
+ MULA(synmatrix8[i][5], frame->sb_sample[blk][ch][5],
+ MULA(synmatrix8[i][6], frame->sb_sample[blk][ch][6],
+ MUL( synmatrix8[i][7], frame->sb_sample[blk][ch][7])))))))));
+ }
+
+ /* Compute the samples */
+ for (idx = 0, i = 0; i < 8; i++, idx += 5) {
+ k = (i + 8) & 0xf;
+
+ /* Store in output, Q0 */
+ frame->pcm_sample[ch][blk * 8 + i] = sbc_clip16(SCALE8_STAGED1(
+ MULA(state->V[ch][offset[i] + 0], sbc_proto_8_80m0[idx + 0],
+ MULA(state->V[ch][offset[k] + 1], sbc_proto_8_80m1[idx + 0],
+ MULA(state->V[ch][offset[i] + 2], sbc_proto_8_80m0[idx + 1],
+ MULA(state->V[ch][offset[k] + 3], sbc_proto_8_80m1[idx + 1],
+ MULA(state->V[ch][offset[i] + 4], sbc_proto_8_80m0[idx + 2],
+ MULA(state->V[ch][offset[k] + 5], sbc_proto_8_80m1[idx + 2],
+ MULA(state->V[ch][offset[i] + 6], sbc_proto_8_80m0[idx + 3],
+ MULA(state->V[ch][offset[k] + 7], sbc_proto_8_80m1[idx + 3],
+ MULA(state->V[ch][offset[i] + 8], sbc_proto_8_80m0[idx + 4],
+ MUL( state->V[ch][offset[k] + 9], sbc_proto_8_80m1[idx + 4]))))))))))));
+ }
+}
+
+static int sbc_synthesize_audio(struct sbc_decoder_state *state,
+ struct sbc_frame *frame)
+{
+ int ch, blk;
+
+ switch (frame->subbands) {
+ case 4:
+ for (ch = 0; ch < frame->channels; ch++) {
+ for (blk = 0; blk < frame->blocks; blk++)
+ sbc_synthesize_four(state, frame, ch, blk);
+ }
+ return frame->blocks * 4;
+
+ case 8:
+ for (ch = 0; ch < frame->channels; ch++) {
+ for (blk = 0; blk < frame->blocks; blk++)
+ sbc_synthesize_eight(state, frame, ch, blk);
+ }
+ return frame->blocks * 8;
+
+ default:
+ return -EIO;
+ }
+}
+
+static int sbc_analyze_audio(struct sbc_encoder_state *state,
+ struct sbc_frame *frame)
+{
+ int ch, blk;
+ int16_t *x;
+
+ switch (frame->subbands) {
+ case 4:
+ for (ch = 0; ch < frame->channels; ch++) {
+ x = &state->X[ch][state->position - 16 +
+ frame->blocks * 4];
+ for (blk = 0; blk < frame->blocks; blk += 4) {
+ state->sbc_analyze_4b_4s(
+ x,
+ frame->sb_sample_f[blk][ch],
+ frame->sb_sample_f[blk + 1][ch] -
+ frame->sb_sample_f[blk][ch]);
+ x -= 16;
+ }
+ }
+ return frame->blocks * 4;
+
+ case 8:
+ for (ch = 0; ch < frame->channels; ch++) {
+ x = &state->X[ch][state->position - 32 +
+ frame->blocks * 8];
+ for (blk = 0; blk < frame->blocks; blk += 4) {
+ state->sbc_analyze_4b_8s(
+ x,
+ frame->sb_sample_f[blk][ch],
+ frame->sb_sample_f[blk + 1][ch] -
+ frame->sb_sample_f[blk][ch]);
+ x -= 32;
+ }
+ }
+ return frame->blocks * 8;
+
+ default:
+ return -EIO;
+ }
+}
+
+/* Supplementary bitstream writing macros for 'sbc_pack_frame' */
+
+#define PUT_BITS(data_ptr, bits_cache, bits_count, v, n) \
+ do { \
+ bits_cache = (v) | (bits_cache << (n)); \
+ bits_count += (n); \
+ if (bits_count >= 16) { \
+ bits_count -= 8; \
+ *data_ptr++ = (uint8_t) \
+ (bits_cache >> bits_count); \
+ bits_count -= 8; \
+ *data_ptr++ = (uint8_t) \
+ (bits_cache >> bits_count); \
+ } \
+ } while (0)
+
+#define FLUSH_BITS(data_ptr, bits_cache, bits_count) \
+ do { \
+ while (bits_count >= 8) { \
+ bits_count -= 8; \
+ *data_ptr++ = (uint8_t) \
+ (bits_cache >> bits_count); \
+ } \
+ if (bits_count > 0) \
+ *data_ptr++ = (uint8_t) \
+ (bits_cache << (8 - bits_count)); \
+ } while (0)
+
+/*
+ * Packs the SBC frame from frame into the memory at data. At most len
+ * bytes will be used, should more memory be needed an appropriate
+ * error code will be returned. Returns the length of the packed frame
+ * on success or a negative value on error.
+ *
+ * The error codes are:
+ * -1 Not enough memory reserved
+ * -2 Unsupported sampling rate
+ * -3 Unsupported number of blocks
+ * -4 Unsupported number of subbands
+ * -5 Bitpool value out of bounds
+ * -99 not implemented
+ */
+
+static SBC_ALWAYS_INLINE ssize_t sbc_pack_frame_internal(uint8_t *data,
+ struct sbc_frame *frame, size_t len,
+ int frame_subbands, int frame_channels,
+ int joint)
+{
+ /* Bitstream writer starts from the fourth byte */
+ uint8_t *data_ptr = data + 4;
+ uint32_t bits_cache = 0;
+ uint32_t bits_count = 0;
+
+ /* Will copy the header parts for CRC-8 calculation here */
+ uint8_t crc_header[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+ int crc_pos = 0;
+
+ uint32_t audio_sample;
+
+ int ch, sb, blk; /* channel, subband, block and bit counters */
+ int bits[2][8]; /* bits distribution */
+ uint32_t levels[2][8]; /* levels are derived from that */
+ uint32_t sb_sample_delta[2][8];
+
+ data[0] = SBC_SYNCWORD;
+
+ data[1] = (frame->frequency & 0x03) << 6;
+
+ data[1] |= (frame->block_mode & 0x03) << 4;
+
+ data[1] |= (frame->mode & 0x03) << 2;
+
+ data[1] |= (frame->allocation & 0x01) << 1;
+
+ switch (frame_subbands) {
+ case 4:
+ /* Nothing to do */
+ break;
+ case 8:
+ data[1] |= 0x01;
+ break;
+ default:
+ return -4;
+ break;
+ }
+
+ data[2] = frame->bitpool;
+
+ if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
+ frame->bitpool > frame_subbands << 4)
+ return -5;
+
+ if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
+ frame->bitpool > frame_subbands << 5)
+ return -5;
+
+ /* Can't fill in crc yet */
+
+ crc_header[0] = data[1];
+ crc_header[1] = data[2];
+ crc_pos = 16;
+
+ if (frame->mode == JOINT_STEREO) {
+ PUT_BITS(data_ptr, bits_cache, bits_count,
+ joint, frame_subbands);
+ crc_header[crc_pos >> 3] = joint;
+ crc_pos += frame_subbands;
+ }
+
+ for (ch = 0; ch < frame_channels; ch++) {
+ for (sb = 0; sb < frame_subbands; sb++) {
+ PUT_BITS(data_ptr, bits_cache, bits_count,
+ frame->scale_factor[ch][sb] & 0x0F, 4);
+ crc_header[crc_pos >> 3] <<= 4;
+ crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F;
+ crc_pos += 4;
+ }
+ }
+
+ /* align the last crc byte */
+ if (crc_pos % 8)
+ crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
+
+ data[3] = sbc_crc8(crc_header, crc_pos);
+
+ sbc_calculate_bits(frame, bits);
+
+ for (ch = 0; ch < frame_channels; ch++) {
+ for (sb = 0; sb < frame_subbands; sb++) {
+ levels[ch][sb] = ((1 << bits[ch][sb]) - 1) <<
+ (32 - (frame->scale_factor[ch][sb] +
+ SCALE_OUT_BITS + 2));
+ sb_sample_delta[ch][sb] = (uint32_t) 1 <<
+ (frame->scale_factor[ch][sb] +
+ SCALE_OUT_BITS + 1);
+ }
+ }
+
+ for (blk = 0; blk < frame->blocks; blk++) {
+ for (ch = 0; ch < frame_channels; ch++) {
+ for (sb = 0; sb < frame_subbands; sb++) {
+
+ if (bits[ch][sb] == 0)
+ continue;
+
+ audio_sample = ((uint64_t) levels[ch][sb] *
+ (sb_sample_delta[ch][sb] +
+ frame->sb_sample_f[blk][ch][sb])) >> 32;
+
+ PUT_BITS(data_ptr, bits_cache, bits_count,
+ audio_sample, bits[ch][sb]);
+ }
+ }
+ }
+
+ FLUSH_BITS(data_ptr, bits_cache, bits_count);
+
+ return data_ptr - data;
+}
+
+static ssize_t sbc_pack_frame(uint8_t *data, struct sbc_frame *frame, size_t len,
+ int joint)
+{
+ if (frame->subbands == 4) {
+ if (frame->channels == 1)
+ return sbc_pack_frame_internal(
+ data, frame, len, 4, 1, joint);
+ else
+ return sbc_pack_frame_internal(
+ data, frame, len, 4, 2, joint);
+ } else {
+ if (frame->channels == 1)
+ return sbc_pack_frame_internal(
+ data, frame, len, 8, 1, joint);
+ else
+ return sbc_pack_frame_internal(
+ data, frame, len, 8, 2, joint);
+ }
+}
+
+static void sbc_encoder_init(struct sbc_encoder_state *state,
+ const struct sbc_frame *frame)
+{
+ memset(&state->X, 0, sizeof(state->X));
+ state->position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
+
+ sbc_init_primitives(state);
+}
+
+struct sbc_priv {
+ int init;
+ struct SBC_ALIGNED sbc_frame frame;
+ struct SBC_ALIGNED sbc_decoder_state dec_state;
+ struct SBC_ALIGNED sbc_encoder_state enc_state;
+};
+
+static void sbc_set_defaults(sbc_t *sbc, unsigned long flags)
+{
+ sbc->frequency = SBC_FREQ_44100;
+ sbc->mode = SBC_MODE_STEREO;
+ sbc->subbands = SBC_SB_8;
+ sbc->blocks = SBC_BLK_16;
+ sbc->bitpool = 32;
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+ sbc->endian = SBC_LE;
+#elif __BYTE_ORDER == __BIG_ENDIAN
+ sbc->endian = SBC_BE;
+#else
+#error "Unknown byte order"
+#endif
+}
+
+int sbc_init(sbc_t *sbc, unsigned long flags)
+{
+ if (!sbc)
+ return -EIO;
+
+ memset(sbc, 0, sizeof(sbc_t));
+
+ sbc->priv_alloc_base = malloc(sizeof(struct sbc_priv) + SBC_ALIGN_MASK);
+ if (!sbc->priv_alloc_base)
+ return -ENOMEM;
+
+ sbc->priv = (void *) (((uintptr_t) sbc->priv_alloc_base +
+ SBC_ALIGN_MASK) & ~((uintptr_t) SBC_ALIGN_MASK));
+
+ memset(sbc->priv, 0, sizeof(struct sbc_priv));
+
+ sbc_set_defaults(sbc, flags);
+
+ return 0;
+}
+
+ssize_t sbc_parse(sbc_t *sbc, const void *input, size_t input_len)
+{
+ return sbc_decode(sbc, input, input_len, NULL, 0, NULL);
+}
+
+ssize_t sbc_decode(sbc_t *sbc, const void *input, size_t input_len,
+ void *output, size_t output_len, size_t *written)
+{
+ struct sbc_priv *priv;
+ char *ptr;
+ int i, ch, framelen, samples;
+
+ if (!sbc || !input)
+ return -EIO;
+
+ priv = sbc->priv;
+
+ framelen = sbc_unpack_frame(input, &priv->frame, input_len);
+
+ if (!priv->init) {
+ sbc_decoder_init(&priv->dec_state, &priv->frame);
+ priv->init = 1;
+
+ sbc->frequency = priv->frame.frequency;
+ sbc->mode = priv->frame.mode;
+ sbc->subbands = priv->frame.subband_mode;
+ sbc->blocks = priv->frame.block_mode;
+ sbc->allocation = priv->frame.allocation;
+ sbc->bitpool = priv->frame.bitpool;
+
+ priv->frame.codesize = sbc_get_codesize(sbc);
+ priv->frame.length = framelen;
+ } else if (priv->frame.bitpool != sbc->bitpool) {
+ priv->frame.length = framelen;
+ sbc->bitpool = priv->frame.bitpool;
+ }
+
+ if (!output)
+ return framelen;
+
+ if (written)
+ *written = 0;
+
+ if (framelen <= 0)
+ return framelen;
+
+ samples = sbc_synthesize_audio(&priv->dec_state, &priv->frame);
+
+ ptr = output;
+
+ if (output_len < (size_t) (samples * priv->frame.channels * 2))
+ samples = output_len / (priv->frame.channels * 2);
+
+ for (i = 0; i < samples; i++) {
+ for (ch = 0; ch < priv->frame.channels; ch++) {
+ int16_t s;
+ s = priv->frame.pcm_sample[ch][i];
+
+ if (sbc->endian == SBC_BE) {
+ *ptr++ = (s & 0xff00) >> 8;
+ *ptr++ = (s & 0x00ff);
+ } else {
+ *ptr++ = (s & 0x00ff);
+ *ptr++ = (s & 0xff00) >> 8;
+ }
+ }
+ }
+
+ if (written)
+ *written = samples * priv->frame.channels * 2;
+
+ return framelen;
+}
+
+ssize_t sbc_encode(sbc_t *sbc, const void *input, size_t input_len,
+ void *output, size_t output_len, ssize_t *written)
+{
+ struct sbc_priv *priv;
+ int samples;
+ ssize_t framelen;
+ int (*sbc_enc_process_input)(int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels);
+
+ if (!sbc || !input)
+ return -EIO;
+
+ priv = sbc->priv;
+
+ if (written)
+ *written = 0;
+
+ if (!priv->init) {
+ priv->frame.frequency = sbc->frequency;
+ priv->frame.mode = sbc->mode;
+ priv->frame.channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
+ priv->frame.allocation = sbc->allocation;
+ priv->frame.subband_mode = sbc->subbands;
+ priv->frame.subbands = sbc->subbands ? 8 : 4;
+ priv->frame.block_mode = sbc->blocks;
+ priv->frame.blocks = 4 + (sbc->blocks * 4);
+ priv->frame.bitpool = sbc->bitpool;
+ priv->frame.codesize = sbc_get_codesize(sbc);
+ priv->frame.length = sbc_get_frame_length(sbc);
+
+ sbc_encoder_init(&priv->enc_state, &priv->frame);
+ priv->init = 1;
+ } else if (priv->frame.bitpool != sbc->bitpool) {
+ priv->frame.length = sbc_get_frame_length(sbc);
+ priv->frame.bitpool = sbc->bitpool;
+ }
+
+ /* input must be large enough to encode a complete frame */
+ if (input_len < priv->frame.codesize)
+ return 0;
+
+ /* output must be large enough to receive the encoded frame */
+ if (!output || output_len < priv->frame.length)
+ return -ENOSPC;
+
+ /* Select the needed input data processing function and call it */
+ if (priv->frame.subbands == 8) {
+ if (sbc->endian == SBC_BE)
+ sbc_enc_process_input =
+ priv->enc_state.sbc_enc_process_input_8s_be;
+ else
+ sbc_enc_process_input =
+ priv->enc_state.sbc_enc_process_input_8s_le;
+ } else {
+ if (sbc->endian == SBC_BE)
+ sbc_enc_process_input =
+ priv->enc_state.sbc_enc_process_input_4s_be;
+ else
+ sbc_enc_process_input =
+ priv->enc_state.sbc_enc_process_input_4s_le;
+ }
+
+ priv->enc_state.position = sbc_enc_process_input(
+ priv->enc_state.position, (const uint8_t *) input,
+ priv->enc_state.X, priv->frame.subbands * priv->frame.blocks,
+ priv->frame.channels);
+
+ samples = sbc_analyze_audio(&priv->enc_state, &priv->frame);
+
+ if (priv->frame.mode == JOINT_STEREO) {
+ int j = priv->enc_state.sbc_calc_scalefactors_j(
+ priv->frame.sb_sample_f, priv->frame.scale_factor,
+ priv->frame.blocks, priv->frame.subbands);
+ framelen = sbc_pack_frame(output, &priv->frame, output_len, j);
+ } else {
+ priv->enc_state.sbc_calc_scalefactors(
+ priv->frame.sb_sample_f, priv->frame.scale_factor,
+ priv->frame.blocks, priv->frame.channels,
+ priv->frame.subbands);
+ framelen = sbc_pack_frame(output, &priv->frame, output_len, 0);
+ }
+
+ if (written)
+ *written = framelen;
+
+ return samples * priv->frame.channels * 2;
+}
+
+void sbc_finish(sbc_t *sbc)
+{
+ if (!sbc)
+ return;
+
+ free(sbc->priv_alloc_base);
+
+ memset(sbc, 0, sizeof(sbc_t));
+}
+
+size_t sbc_get_frame_length(sbc_t *sbc)
+{
+ int ret;
+ uint8_t subbands, channels, blocks, joint, bitpool;
+ struct sbc_priv *priv;
+
+ priv = sbc->priv;
+ if (priv->init && priv->frame.bitpool == sbc->bitpool)
+ return priv->frame.length;
+
+ subbands = sbc->subbands ? 8 : 4;
+ blocks = 4 + (sbc->blocks * 4);
+ channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
+ joint = sbc->mode == SBC_MODE_JOINT_STEREO ? 1 : 0;
+ bitpool = sbc->bitpool;
+
+ ret = 4 + (4 * subbands * channels) / 8;
+ /* This term is not always evenly divide so we round it up */
+ if (channels == 1)
+ ret += ((blocks * channels * bitpool) + 7) / 8;
+ else
+ ret += (((joint ? subbands : 0) + blocks * bitpool) + 7) / 8;
+
+ return ret;
+}
+
+unsigned sbc_get_frame_duration(sbc_t *sbc)
+{
+ uint8_t subbands, blocks;
+ uint16_t frequency;
+ struct sbc_priv *priv;
+
+ priv = sbc->priv;
+ if (!priv->init) {
+ subbands = sbc->subbands ? 8 : 4;
+ blocks = 4 + (sbc->blocks * 4);
+ } else {
+ subbands = priv->frame.subbands;
+ blocks = priv->frame.blocks;
+ }
+
+ switch (sbc->frequency) {
+ case SBC_FREQ_16000:
+ frequency = 16000;
+ break;
+
+ case SBC_FREQ_32000:
+ frequency = 32000;
+ break;
+
+ case SBC_FREQ_44100:
+ frequency = 44100;
+ break;
+
+ case SBC_FREQ_48000:
+ frequency = 48000;
+ break;
+ default:
+ return 0;
+ }
+
+ return (1000000 * blocks * subbands) / frequency;
+}
+
+size_t sbc_get_codesize(sbc_t *sbc)
+{
+ uint16_t subbands, channels, blocks;
+ struct sbc_priv *priv;
+
+ priv = sbc->priv;
+ if (!priv->init) {
+ subbands = sbc->subbands ? 8 : 4;
+ blocks = 4 + (sbc->blocks * 4);
+ channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
+ } else {
+ subbands = priv->frame.subbands;
+ blocks = priv->frame.blocks;
+ channels = priv->frame.channels;
+ }
+
+ return subbands * blocks * channels * 2;
+}
+
+const char *sbc_get_implementation_info(sbc_t *sbc)
+{
+ struct sbc_priv *priv;
+
+ if (!sbc)
+ return NULL;
+
+ priv = sbc->priv;
+ if (!priv)
+ return NULL;
+
+ return priv->enc_state.implementation_info;
+}
+
+int sbc_reinit(sbc_t *sbc, unsigned long flags)
+{
+ struct sbc_priv *priv;
+
+ if (!sbc || !sbc->priv)
+ return -EIO;
+
+ priv = sbc->priv;
+
+ if (priv->init == 1)
+ memset(sbc->priv, 0, sizeof(struct sbc_priv));
+
+ sbc_set_defaults(sbc, flags);
+
+ return 0;
+}
diff --git a/src/modules/bluetooth/sbc/sbc.h b/src/modules/bluetooth/sbc/sbc.h
new file mode 100644
index 00000000..2f830ad5
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc.h
@@ -0,0 +1,113 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef __SBC_H
+#define __SBC_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+#include <sys/types.h>
+
+/* sampling frequency */
+#define SBC_FREQ_16000 0x00
+#define SBC_FREQ_32000 0x01
+#define SBC_FREQ_44100 0x02
+#define SBC_FREQ_48000 0x03
+
+/* blocks */
+#define SBC_BLK_4 0x00
+#define SBC_BLK_8 0x01
+#define SBC_BLK_12 0x02
+#define SBC_BLK_16 0x03
+
+/* channel mode */
+#define SBC_MODE_MONO 0x00
+#define SBC_MODE_DUAL_CHANNEL 0x01
+#define SBC_MODE_STEREO 0x02
+#define SBC_MODE_JOINT_STEREO 0x03
+
+/* allocation method */
+#define SBC_AM_LOUDNESS 0x00
+#define SBC_AM_SNR 0x01
+
+/* subbands */
+#define SBC_SB_4 0x00
+#define SBC_SB_8 0x01
+
+/* Data endianess */
+#define SBC_LE 0x00
+#define SBC_BE 0x01
+
+struct sbc_struct {
+ unsigned long flags;
+
+ uint8_t frequency;
+ uint8_t blocks;
+ uint8_t subbands;
+ uint8_t mode;
+ uint8_t allocation;
+ uint8_t bitpool;
+ uint8_t endian;
+
+ void *priv;
+ void *priv_alloc_base;
+};
+
+typedef struct sbc_struct sbc_t;
+
+int sbc_init(sbc_t *sbc, unsigned long flags);
+int sbc_reinit(sbc_t *sbc, unsigned long flags);
+
+ssize_t sbc_parse(sbc_t *sbc, const void *input, size_t input_len);
+
+/* Decodes ONE input block into ONE output block */
+ssize_t sbc_decode(sbc_t *sbc, const void *input, size_t input_len,
+ void *output, size_t output_len, size_t *written);
+
+/* Encodes ONE input block into ONE output block */
+ssize_t sbc_encode(sbc_t *sbc, const void *input, size_t input_len,
+ void *output, size_t output_len, ssize_t *written);
+
+/* Returns the output block size in bytes */
+size_t sbc_get_frame_length(sbc_t *sbc);
+
+/* Returns the time one input/output block takes to play in msec*/
+unsigned sbc_get_frame_duration(sbc_t *sbc);
+
+/* Returns the input block size in bytes */
+size_t sbc_get_codesize(sbc_t *sbc);
+
+const char *sbc_get_implementation_info(sbc_t *sbc);
+void sbc_finish(sbc_t *sbc);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SBC_H */
diff --git a/src/modules/bluetooth/sbc/sbc_math.h b/src/modules/bluetooth/sbc/sbc_math.h
new file mode 100644
index 00000000..5476860d
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_math.h
@@ -0,0 +1,61 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2008 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#define fabs(x) ((x) < 0 ? -(x) : (x))
+/* C does not provide an explicit arithmetic shift right but this will
+ always be correct and every compiler *should* generate optimal code */
+#define ASR(val, bits) ((-2 >> 1 == -1) ? \
+ ((int32_t)(val)) >> (bits) : ((int32_t) (val)) / (1 << (bits)))
+
+#define SCALE_SPROTO4_TBL 12
+#define SCALE_SPROTO8_TBL 14
+#define SCALE_NPROTO4_TBL 11
+#define SCALE_NPROTO8_TBL 11
+#define SCALE4_STAGED1_BITS 15
+#define SCALE4_STAGED2_BITS 16
+#define SCALE8_STAGED1_BITS 15
+#define SCALE8_STAGED2_BITS 16
+
+typedef int32_t sbc_fixed_t;
+
+#define SCALE4_STAGED1(src) ASR(src, SCALE4_STAGED1_BITS)
+#define SCALE4_STAGED2(src) ASR(src, SCALE4_STAGED2_BITS)
+#define SCALE8_STAGED1(src) ASR(src, SCALE8_STAGED1_BITS)
+#define SCALE8_STAGED2(src) ASR(src, SCALE8_STAGED2_BITS)
+
+#define SBC_FIXED_0(val) { val = 0; }
+#define MUL(a, b) ((a) * (b))
+#if defined(__arm__) && (!defined(__thumb__) || defined(__thumb2__))
+#define MULA(a, b, res) ({ \
+ int tmp = res; \
+ __asm__( \
+ "mla %0, %2, %3, %0" \
+ : "=&r" (tmp) \
+ : "0" (tmp), "r" (a), "r" (b)); \
+ tmp; })
+#else
+#define MULA(a, b, res) ((a) * (b) + (res))
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_primitives.c b/src/modules/bluetooth/sbc/sbc_primitives.c
new file mode 100644
index 00000000..ad780d08
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives.c
@@ -0,0 +1,554 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <stdint.h>
+#include <limits.h>
+#include <string.h>
+#include "sbc.h"
+#include "sbc_math.h"
+#include "sbc_tables.h"
+
+#include "sbc_primitives.h"
+#include "sbc_primitives_mmx.h"
+#include "sbc_primitives_iwmmxt.h"
+#include "sbc_primitives_neon.h"
+#include "sbc_primitives_armv6.h"
+
+/*
+ * A reference C code of analysis filter with SIMD-friendly tables
+ * reordering and code layout. This code can be used to develop platform
+ * specific SIMD optimizations. Also it may be used as some kind of test
+ * for compiler autovectorization capabilities (who knows, if the compiler
+ * is very good at this stuff, hand optimized assembly may be not strictly
+ * needed for some platform).
+ *
+ * Note: It is also possible to make a simple variant of analysis filter,
+ * which needs only a single constants table without taking care about
+ * even/odd cases. This simple variant of filter can be implemented without
+ * input data permutation. The only thing that would be lost is the
+ * possibility to use pairwise SIMD multiplications. But for some simple
+ * CPU cores without SIMD extensions it can be useful. If anybody is
+ * interested in implementing such variant of a filter, sourcecode from
+ * bluez versions 4.26/4.27 can be used as a reference and the history of
+ * the changes in git repository done around that time may be worth checking.
+ */
+
+static inline void sbc_analyze_four_simd(const int16_t *in, int32_t *out,
+ const FIXED_T *consts)
+{
+ FIXED_A t1[4];
+ FIXED_T t2[4];
+ int hop = 0;
+
+ /* rounding coefficient */
+ t1[0] = t1[1] = t1[2] = t1[3] =
+ (FIXED_A) 1 << (SBC_PROTO_FIXED4_SCALE - 1);
+
+ /* low pass polyphase filter */
+ for (hop = 0; hop < 40; hop += 8) {
+ t1[0] += (FIXED_A) in[hop] * consts[hop];
+ t1[0] += (FIXED_A) in[hop + 1] * consts[hop + 1];
+ t1[1] += (FIXED_A) in[hop + 2] * consts[hop + 2];
+ t1[1] += (FIXED_A) in[hop + 3] * consts[hop + 3];
+ t1[2] += (FIXED_A) in[hop + 4] * consts[hop + 4];
+ t1[2] += (FIXED_A) in[hop + 5] * consts[hop + 5];
+ t1[3] += (FIXED_A) in[hop + 6] * consts[hop + 6];
+ t1[3] += (FIXED_A) in[hop + 7] * consts[hop + 7];
+ }
+
+ /* scaling */
+ t2[0] = t1[0] >> SBC_PROTO_FIXED4_SCALE;
+ t2[1] = t1[1] >> SBC_PROTO_FIXED4_SCALE;
+ t2[2] = t1[2] >> SBC_PROTO_FIXED4_SCALE;
+ t2[3] = t1[3] >> SBC_PROTO_FIXED4_SCALE;
+
+ /* do the cos transform */
+ t1[0] = (FIXED_A) t2[0] * consts[40 + 0];
+ t1[0] += (FIXED_A) t2[1] * consts[40 + 1];
+ t1[1] = (FIXED_A) t2[0] * consts[40 + 2];
+ t1[1] += (FIXED_A) t2[1] * consts[40 + 3];
+ t1[2] = (FIXED_A) t2[0] * consts[40 + 4];
+ t1[2] += (FIXED_A) t2[1] * consts[40 + 5];
+ t1[3] = (FIXED_A) t2[0] * consts[40 + 6];
+ t1[3] += (FIXED_A) t2[1] * consts[40 + 7];
+
+ t1[0] += (FIXED_A) t2[2] * consts[40 + 8];
+ t1[0] += (FIXED_A) t2[3] * consts[40 + 9];
+ t1[1] += (FIXED_A) t2[2] * consts[40 + 10];
+ t1[1] += (FIXED_A) t2[3] * consts[40 + 11];
+ t1[2] += (FIXED_A) t2[2] * consts[40 + 12];
+ t1[2] += (FIXED_A) t2[3] * consts[40 + 13];
+ t1[3] += (FIXED_A) t2[2] * consts[40 + 14];
+ t1[3] += (FIXED_A) t2[3] * consts[40 + 15];
+
+ out[0] = t1[0] >>
+ (SBC_COS_TABLE_FIXED4_SCALE - SCALE_OUT_BITS);
+ out[1] = t1[1] >>
+ (SBC_COS_TABLE_FIXED4_SCALE - SCALE_OUT_BITS);
+ out[2] = t1[2] >>
+ (SBC_COS_TABLE_FIXED4_SCALE - SCALE_OUT_BITS);
+ out[3] = t1[3] >>
+ (SBC_COS_TABLE_FIXED4_SCALE - SCALE_OUT_BITS);
+}
+
+static inline void sbc_analyze_eight_simd(const int16_t *in, int32_t *out,
+ const FIXED_T *consts)
+{
+ FIXED_A t1[8];
+ FIXED_T t2[8];
+ int i, hop;
+
+ /* rounding coefficient */
+ t1[0] = t1[1] = t1[2] = t1[3] = t1[4] = t1[5] = t1[6] = t1[7] =
+ (FIXED_A) 1 << (SBC_PROTO_FIXED8_SCALE-1);
+
+ /* low pass polyphase filter */
+ for (hop = 0; hop < 80; hop += 16) {
+ t1[0] += (FIXED_A) in[hop] * consts[hop];
+ t1[0] += (FIXED_A) in[hop + 1] * consts[hop + 1];
+ t1[1] += (FIXED_A) in[hop + 2] * consts[hop + 2];
+ t1[1] += (FIXED_A) in[hop + 3] * consts[hop + 3];
+ t1[2] += (FIXED_A) in[hop + 4] * consts[hop + 4];
+ t1[2] += (FIXED_A) in[hop + 5] * consts[hop + 5];
+ t1[3] += (FIXED_A) in[hop + 6] * consts[hop + 6];
+ t1[3] += (FIXED_A) in[hop + 7] * consts[hop + 7];
+ t1[4] += (FIXED_A) in[hop + 8] * consts[hop + 8];
+ t1[4] += (FIXED_A) in[hop + 9] * consts[hop + 9];
+ t1[5] += (FIXED_A) in[hop + 10] * consts[hop + 10];
+ t1[5] += (FIXED_A) in[hop + 11] * consts[hop + 11];
+ t1[6] += (FIXED_A) in[hop + 12] * consts[hop + 12];
+ t1[6] += (FIXED_A) in[hop + 13] * consts[hop + 13];
+ t1[7] += (FIXED_A) in[hop + 14] * consts[hop + 14];
+ t1[7] += (FIXED_A) in[hop + 15] * consts[hop + 15];
+ }
+
+ /* scaling */
+ t2[0] = t1[0] >> SBC_PROTO_FIXED8_SCALE;
+ t2[1] = t1[1] >> SBC_PROTO_FIXED8_SCALE;
+ t2[2] = t1[2] >> SBC_PROTO_FIXED8_SCALE;
+ t2[3] = t1[3] >> SBC_PROTO_FIXED8_SCALE;
+ t2[4] = t1[4] >> SBC_PROTO_FIXED8_SCALE;
+ t2[5] = t1[5] >> SBC_PROTO_FIXED8_SCALE;
+ t2[6] = t1[6] >> SBC_PROTO_FIXED8_SCALE;
+ t2[7] = t1[7] >> SBC_PROTO_FIXED8_SCALE;
+
+
+ /* do the cos transform */
+ t1[0] = t1[1] = t1[2] = t1[3] = t1[4] = t1[5] = t1[6] = t1[7] = 0;
+
+ for (i = 0; i < 4; i++) {
+ t1[0] += (FIXED_A) t2[i * 2 + 0] * consts[80 + i * 16 + 0];
+ t1[0] += (FIXED_A) t2[i * 2 + 1] * consts[80 + i * 16 + 1];
+ t1[1] += (FIXED_A) t2[i * 2 + 0] * consts[80 + i * 16 + 2];
+ t1[1] += (FIXED_A) t2[i * 2 + 1] * consts[80 + i * 16 + 3];
+ t1[2] += (FIXED_A) t2[i * 2 + 0] * consts[80 + i * 16 + 4];
+ t1[2] += (FIXED_A) t2[i * 2 + 1] * consts[80 + i * 16 + 5];
+ t1[3] += (FIXED_A) t2[i * 2 + 0] * consts[80 + i * 16 + 6];
+ t1[3] += (FIXED_A) t2[i * 2 + 1] * consts[80 + i * 16 + 7];
+ t1[4] += (FIXED_A) t2[i * 2 + 0] * consts[80 + i * 16 + 8];
+ t1[4] += (FIXED_A) t2[i * 2 + 1] * consts[80 + i * 16 + 9];
+ t1[5] += (FIXED_A) t2[i * 2 + 0] * consts[80 + i * 16 + 10];
+ t1[5] += (FIXED_A) t2[i * 2 + 1] * consts[80 + i * 16 + 11];
+ t1[6] += (FIXED_A) t2[i * 2 + 0] * consts[80 + i * 16 + 12];
+ t1[6] += (FIXED_A) t2[i * 2 + 1] * consts[80 + i * 16 + 13];
+ t1[7] += (FIXED_A) t2[i * 2 + 0] * consts[80 + i * 16 + 14];
+ t1[7] += (FIXED_A) t2[i * 2 + 1] * consts[80 + i * 16 + 15];
+ }
+
+ for (i = 0; i < 8; i++)
+ out[i] = t1[i] >>
+ (SBC_COS_TABLE_FIXED8_SCALE - SCALE_OUT_BITS);
+}
+
+static inline void sbc_analyze_4b_4s_simd(int16_t *x,
+ int32_t *out, int out_stride)
+{
+ /* Analyze blocks */
+ sbc_analyze_four_simd(x + 12, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ sbc_analyze_four_simd(x + 8, out, analysis_consts_fixed4_simd_even);
+ out += out_stride;
+ sbc_analyze_four_simd(x + 4, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ sbc_analyze_four_simd(x + 0, out, analysis_consts_fixed4_simd_even);
+}
+
+static inline void sbc_analyze_4b_8s_simd(int16_t *x,
+ int32_t *out, int out_stride)
+{
+ /* Analyze blocks */
+ sbc_analyze_eight_simd(x + 24, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ sbc_analyze_eight_simd(x + 16, out, analysis_consts_fixed8_simd_even);
+ out += out_stride;
+ sbc_analyze_eight_simd(x + 8, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ sbc_analyze_eight_simd(x + 0, out, analysis_consts_fixed8_simd_even);
+}
+
+static inline int16_t unaligned16_be(const uint8_t *ptr)
+{
+ return (int16_t) ((ptr[0] << 8) | ptr[1]);
+}
+
+static inline int16_t unaligned16_le(const uint8_t *ptr)
+{
+ return (int16_t) (ptr[0] | (ptr[1] << 8));
+}
+
+/*
+ * Internal helper functions for input data processing. In order to get
+ * optimal performance, it is important to have "nsamples", "nchannels"
+ * and "big_endian" arguments used with this inline function as compile
+ * time constants.
+ */
+
+static SBC_ALWAYS_INLINE int sbc_encoder_process_input_s4_internal(
+ int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels, int big_endian)
+{
+ /* handle X buffer wraparound */
+ if (position < nsamples) {
+ if (nchannels > 0)
+ memcpy(&X[0][SBC_X_BUFFER_SIZE - 40], &X[0][position],
+ 36 * sizeof(int16_t));
+ if (nchannels > 1)
+ memcpy(&X[1][SBC_X_BUFFER_SIZE - 40], &X[1][position],
+ 36 * sizeof(int16_t));
+ position = SBC_X_BUFFER_SIZE - 40;
+ }
+
+ #define PCM(i) (big_endian ? \
+ unaligned16_be(pcm + (i) * 2) : unaligned16_le(pcm + (i) * 2))
+
+ /* copy/permutate audio samples */
+ while ((nsamples -= 8) >= 0) {
+ position -= 8;
+ if (nchannels > 0) {
+ int16_t *x = &X[0][position];
+ x[0] = PCM(0 + 7 * nchannels);
+ x[1] = PCM(0 + 3 * nchannels);
+ x[2] = PCM(0 + 6 * nchannels);
+ x[3] = PCM(0 + 4 * nchannels);
+ x[4] = PCM(0 + 0 * nchannels);
+ x[5] = PCM(0 + 2 * nchannels);
+ x[6] = PCM(0 + 1 * nchannels);
+ x[7] = PCM(0 + 5 * nchannels);
+ }
+ if (nchannels > 1) {
+ int16_t *x = &X[1][position];
+ x[0] = PCM(1 + 7 * nchannels);
+ x[1] = PCM(1 + 3 * nchannels);
+ x[2] = PCM(1 + 6 * nchannels);
+ x[3] = PCM(1 + 4 * nchannels);
+ x[4] = PCM(1 + 0 * nchannels);
+ x[5] = PCM(1 + 2 * nchannels);
+ x[6] = PCM(1 + 1 * nchannels);
+ x[7] = PCM(1 + 5 * nchannels);
+ }
+ pcm += 16 * nchannels;
+ }
+ #undef PCM
+
+ return position;
+}
+
+static SBC_ALWAYS_INLINE int sbc_encoder_process_input_s8_internal(
+ int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels, int big_endian)
+{
+ /* handle X buffer wraparound */
+ if (position < nsamples) {
+ if (nchannels > 0)
+ memcpy(&X[0][SBC_X_BUFFER_SIZE - 72], &X[0][position],
+ 72 * sizeof(int16_t));
+ if (nchannels > 1)
+ memcpy(&X[1][SBC_X_BUFFER_SIZE - 72], &X[1][position],
+ 72 * sizeof(int16_t));
+ position = SBC_X_BUFFER_SIZE - 72;
+ }
+
+ #define PCM(i) (big_endian ? \
+ unaligned16_be(pcm + (i) * 2) : unaligned16_le(pcm + (i) * 2))
+
+ /* copy/permutate audio samples */
+ while ((nsamples -= 16) >= 0) {
+ position -= 16;
+ if (nchannels > 0) {
+ int16_t *x = &X[0][position];
+ x[0] = PCM(0 + 15 * nchannels);
+ x[1] = PCM(0 + 7 * nchannels);
+ x[2] = PCM(0 + 14 * nchannels);
+ x[3] = PCM(0 + 8 * nchannels);
+ x[4] = PCM(0 + 13 * nchannels);
+ x[5] = PCM(0 + 9 * nchannels);
+ x[6] = PCM(0 + 12 * nchannels);
+ x[7] = PCM(0 + 10 * nchannels);
+ x[8] = PCM(0 + 11 * nchannels);
+ x[9] = PCM(0 + 3 * nchannels);
+ x[10] = PCM(0 + 6 * nchannels);
+ x[11] = PCM(0 + 0 * nchannels);
+ x[12] = PCM(0 + 5 * nchannels);
+ x[13] = PCM(0 + 1 * nchannels);
+ x[14] = PCM(0 + 4 * nchannels);
+ x[15] = PCM(0 + 2 * nchannels);
+ }
+ if (nchannels > 1) {
+ int16_t *x = &X[1][position];
+ x[0] = PCM(1 + 15 * nchannels);
+ x[1] = PCM(1 + 7 * nchannels);
+ x[2] = PCM(1 + 14 * nchannels);
+ x[3] = PCM(1 + 8 * nchannels);
+ x[4] = PCM(1 + 13 * nchannels);
+ x[5] = PCM(1 + 9 * nchannels);
+ x[6] = PCM(1 + 12 * nchannels);
+ x[7] = PCM(1 + 10 * nchannels);
+ x[8] = PCM(1 + 11 * nchannels);
+ x[9] = PCM(1 + 3 * nchannels);
+ x[10] = PCM(1 + 6 * nchannels);
+ x[11] = PCM(1 + 0 * nchannels);
+ x[12] = PCM(1 + 5 * nchannels);
+ x[13] = PCM(1 + 1 * nchannels);
+ x[14] = PCM(1 + 4 * nchannels);
+ x[15] = PCM(1 + 2 * nchannels);
+ }
+ pcm += 32 * nchannels;
+ }
+ #undef PCM
+
+ return position;
+}
+
+/*
+ * Input data processing functions. The data is endian converted if needed,
+ * channels are deintrleaved and audio samples are reordered for use in
+ * SIMD-friendly analysis filter function. The results are put into "X"
+ * array, getting appended to the previous data (or it is better to say
+ * prepended, as the buffer is filled from top to bottom). Old data is
+ * discarded when neededed, but availability of (10 * nrof_subbands)
+ * contiguous samples is always guaranteed for the input to the analysis
+ * filter. This is achieved by copying a sufficient part of old data
+ * to the top of the buffer on buffer wraparound.
+ */
+
+static int sbc_enc_process_input_4s_le(int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels)
+{
+ if (nchannels > 1)
+ return sbc_encoder_process_input_s4_internal(
+ position, pcm, X, nsamples, 2, 0);
+ else
+ return sbc_encoder_process_input_s4_internal(
+ position, pcm, X, nsamples, 1, 0);
+}
+
+static int sbc_enc_process_input_4s_be(int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels)
+{
+ if (nchannels > 1)
+ return sbc_encoder_process_input_s4_internal(
+ position, pcm, X, nsamples, 2, 1);
+ else
+ return sbc_encoder_process_input_s4_internal(
+ position, pcm, X, nsamples, 1, 1);
+}
+
+static int sbc_enc_process_input_8s_le(int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels)
+{
+ if (nchannels > 1)
+ return sbc_encoder_process_input_s8_internal(
+ position, pcm, X, nsamples, 2, 0);
+ else
+ return sbc_encoder_process_input_s8_internal(
+ position, pcm, X, nsamples, 1, 0);
+}
+
+static int sbc_enc_process_input_8s_be(int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels)
+{
+ if (nchannels > 1)
+ return sbc_encoder_process_input_s8_internal(
+ position, pcm, X, nsamples, 2, 1);
+ else
+ return sbc_encoder_process_input_s8_internal(
+ position, pcm, X, nsamples, 1, 1);
+}
+
+/* Supplementary function to count the number of leading zeros */
+
+static inline int sbc_clz(uint32_t x)
+{
+#ifdef __GNUC__
+ return __builtin_clz(x);
+#else
+ /* TODO: this should be replaced with something better if good
+ * performance is wanted when using compilers other than gcc */
+ int cnt = 0;
+ while (x) {
+ cnt++;
+ x >>= 1;
+ }
+ return 32 - cnt;
+#endif
+}
+
+static void sbc_calc_scalefactors(
+ int32_t sb_sample_f[16][2][8],
+ uint32_t scale_factor[2][8],
+ int blocks, int channels, int subbands)
+{
+ int ch, sb, blk;
+ for (ch = 0; ch < channels; ch++) {
+ for (sb = 0; sb < subbands; sb++) {
+ uint32_t x = 1 << SCALE_OUT_BITS;
+ for (blk = 0; blk < blocks; blk++) {
+ int32_t tmp = fabs(sb_sample_f[blk][ch][sb]);
+ if (tmp != 0)
+ x |= tmp - 1;
+ }
+ scale_factor[ch][sb] = (31 - SCALE_OUT_BITS) -
+ sbc_clz(x);
+ }
+ }
+}
+
+static int sbc_calc_scalefactors_j(
+ int32_t sb_sample_f[16][2][8],
+ uint32_t scale_factor[2][8],
+ int blocks, int subbands)
+{
+ int blk, joint = 0;
+ int32_t tmp0, tmp1;
+ uint32_t x, y;
+
+ /* last subband does not use joint stereo */
+ int sb = subbands - 1;
+ x = 1 << SCALE_OUT_BITS;
+ y = 1 << SCALE_OUT_BITS;
+ for (blk = 0; blk < blocks; blk++) {
+ tmp0 = fabs(sb_sample_f[blk][0][sb]);
+ tmp1 = fabs(sb_sample_f[blk][1][sb]);
+ if (tmp0 != 0)
+ x |= tmp0 - 1;
+ if (tmp1 != 0)
+ y |= tmp1 - 1;
+ }
+ scale_factor[0][sb] = (31 - SCALE_OUT_BITS) - sbc_clz(x);
+ scale_factor[1][sb] = (31 - SCALE_OUT_BITS) - sbc_clz(y);
+
+ /* the rest of subbands can use joint stereo */
+ while (--sb >= 0) {
+ int32_t sb_sample_j[16][2];
+ x = 1 << SCALE_OUT_BITS;
+ y = 1 << SCALE_OUT_BITS;
+ for (blk = 0; blk < blocks; blk++) {
+ tmp0 = sb_sample_f[blk][0][sb];
+ tmp1 = sb_sample_f[blk][1][sb];
+ sb_sample_j[blk][0] = ASR(tmp0, 1) + ASR(tmp1, 1);
+ sb_sample_j[blk][1] = ASR(tmp0, 1) - ASR(tmp1, 1);
+ tmp0 = fabs(tmp0);
+ tmp1 = fabs(tmp1);
+ if (tmp0 != 0)
+ x |= tmp0 - 1;
+ if (tmp1 != 0)
+ y |= tmp1 - 1;
+ }
+ scale_factor[0][sb] = (31 - SCALE_OUT_BITS) -
+ sbc_clz(x);
+ scale_factor[1][sb] = (31 - SCALE_OUT_BITS) -
+ sbc_clz(y);
+ x = 1 << SCALE_OUT_BITS;
+ y = 1 << SCALE_OUT_BITS;
+ for (blk = 0; blk < blocks; blk++) {
+ tmp0 = fabs(sb_sample_j[blk][0]);
+ tmp1 = fabs(sb_sample_j[blk][1]);
+ if (tmp0 != 0)
+ x |= tmp0 - 1;
+ if (tmp1 != 0)
+ y |= tmp1 - 1;
+ }
+ x = (31 - SCALE_OUT_BITS) - sbc_clz(x);
+ y = (31 - SCALE_OUT_BITS) - sbc_clz(y);
+
+ /* decide whether to use joint stereo for this subband */
+ if ((scale_factor[0][sb] + scale_factor[1][sb]) > x + y) {
+ joint |= 1 << (subbands - 1 - sb);
+ scale_factor[0][sb] = x;
+ scale_factor[1][sb] = y;
+ for (blk = 0; blk < blocks; blk++) {
+ sb_sample_f[blk][0][sb] = sb_sample_j[blk][0];
+ sb_sample_f[blk][1][sb] = sb_sample_j[blk][1];
+ }
+ }
+ }
+
+ /* bitmask with the information about subbands using joint stereo */
+ return joint;
+}
+
+/*
+ * Detect CPU features and setup function pointers
+ */
+void sbc_init_primitives(struct sbc_encoder_state *state)
+{
+ /* Default implementation for analyze functions */
+ state->sbc_analyze_4b_4s = sbc_analyze_4b_4s_simd;
+ state->sbc_analyze_4b_8s = sbc_analyze_4b_8s_simd;
+
+ /* Default implementation for input reordering / deinterleaving */
+ state->sbc_enc_process_input_4s_le = sbc_enc_process_input_4s_le;
+ state->sbc_enc_process_input_4s_be = sbc_enc_process_input_4s_be;
+ state->sbc_enc_process_input_8s_le = sbc_enc_process_input_8s_le;
+ state->sbc_enc_process_input_8s_be = sbc_enc_process_input_8s_be;
+
+ /* Default implementation for scale factors calculation */
+ state->sbc_calc_scalefactors = sbc_calc_scalefactors;
+ state->sbc_calc_scalefactors_j = sbc_calc_scalefactors_j;
+ state->implementation_info = "Generic C";
+
+ /* X86/AMD64 optimizations */
+#ifdef SBC_BUILD_WITH_MMX_SUPPORT
+ sbc_init_primitives_mmx(state);
+#endif
+
+ /* ARM optimizations */
+#ifdef SBC_BUILD_WITH_ARMV6_SUPPORT
+ sbc_init_primitives_armv6(state);
+#endif
+#ifdef SBC_BUILD_WITH_IWMMXT_SUPPORT
+ sbc_init_primitives_iwmmxt(state);
+#endif
+#ifdef SBC_BUILD_WITH_NEON_SUPPORT
+ sbc_init_primitives_neon(state);
+#endif
+}
diff --git a/src/modules/bluetooth/sbc/sbc_primitives.h b/src/modules/bluetooth/sbc/sbc_primitives.h
new file mode 100644
index 00000000..3fec8d5b
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives.h
@@ -0,0 +1,80 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef __SBC_PRIMITIVES_H
+#define __SBC_PRIMITIVES_H
+
+#define SCALE_OUT_BITS 15
+#define SBC_X_BUFFER_SIZE 328
+
+#ifdef __GNUC__
+#define SBC_ALWAYS_INLINE __attribute__((always_inline))
+#else
+#define SBC_ALWAYS_INLINE inline
+#endif
+
+struct sbc_encoder_state {
+ int position;
+ int16_t SBC_ALIGNED X[2][SBC_X_BUFFER_SIZE];
+ /* Polyphase analysis filter for 4 subbands configuration,
+ * it handles 4 blocks at once */
+ void (*sbc_analyze_4b_4s)(int16_t *x, int32_t *out, int out_stride);
+ /* Polyphase analysis filter for 8 subbands configuration,
+ * it handles 4 blocks at once */
+ void (*sbc_analyze_4b_8s)(int16_t *x, int32_t *out, int out_stride);
+ /* Process input data (deinterleave, endian conversion, reordering),
+ * depending on the number of subbands and input data byte order */
+ int (*sbc_enc_process_input_4s_le)(int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels);
+ int (*sbc_enc_process_input_4s_be)(int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels);
+ int (*sbc_enc_process_input_8s_le)(int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels);
+ int (*sbc_enc_process_input_8s_be)(int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels);
+ /* Scale factors calculation */
+ void (*sbc_calc_scalefactors)(int32_t sb_sample_f[16][2][8],
+ uint32_t scale_factor[2][8],
+ int blocks, int channels, int subbands);
+ /* Scale factors calculation with joint stereo support */
+ int (*sbc_calc_scalefactors_j)(int32_t sb_sample_f[16][2][8],
+ uint32_t scale_factor[2][8],
+ int blocks, int subbands);
+ const char *implementation_info;
+};
+
+/*
+ * Initialize pointers to the functions which are the basic "building bricks"
+ * of SBC codec. Best implementation is selected based on target CPU
+ * capabilities.
+ */
+void sbc_init_primitives(struct sbc_encoder_state *encoder_state);
+
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_primitives_armv6.c b/src/modules/bluetooth/sbc/sbc_primitives_armv6.c
new file mode 100644
index 00000000..95860980
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives_armv6.c
@@ -0,0 +1,299 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <stdint.h>
+#include <limits.h>
+#include "sbc.h"
+#include "sbc_math.h"
+#include "sbc_tables.h"
+
+#include "sbc_primitives_armv6.h"
+
+/*
+ * ARMv6 optimizations. The instructions are scheduled for ARM11 pipeline.
+ */
+
+#ifdef SBC_BUILD_WITH_ARMV6_SUPPORT
+
+static void __attribute__((naked)) sbc_analyze_four_armv6()
+{
+ /* r0 = in, r1 = out, r2 = consts */
+ asm volatile (
+ "push {r1, r4-r7, lr}\n"
+ "push {r8-r11}\n"
+ "ldrd r4, r5, [r0, #0]\n"
+ "ldrd r6, r7, [r2, #0]\n"
+ "ldrd r8, r9, [r0, #16]\n"
+ "ldrd r10, r11, [r2, #16]\n"
+ "mov r14, #0x8000\n"
+ "smlad r3, r4, r6, r14\n"
+ "smlad r12, r5, r7, r14\n"
+ "ldrd r4, r5, [r0, #32]\n"
+ "ldrd r6, r7, [r2, #32]\n"
+ "smlad r3, r8, r10, r3\n"
+ "smlad r12, r9, r11, r12\n"
+ "ldrd r8, r9, [r0, #48]\n"
+ "ldrd r10, r11, [r2, #48]\n"
+ "smlad r3, r4, r6, r3\n"
+ "smlad r12, r5, r7, r12\n"
+ "ldrd r4, r5, [r0, #64]\n"
+ "ldrd r6, r7, [r2, #64]\n"
+ "smlad r3, r8, r10, r3\n"
+ "smlad r12, r9, r11, r12\n"
+ "ldrd r8, r9, [r0, #8]\n"
+ "ldrd r10, r11, [r2, #8]\n"
+ "smlad r3, r4, r6, r3\n" /* t1[0] is done */
+ "smlad r12, r5, r7, r12\n" /* t1[1] is done */
+ "ldrd r4, r5, [r0, #24]\n"
+ "ldrd r6, r7, [r2, #24]\n"
+ "pkhtb r3, r12, r3, asr #16\n" /* combine t1[0] and t1[1] */
+ "smlad r12, r8, r10, r14\n"
+ "smlad r14, r9, r11, r14\n"
+ "ldrd r8, r9, [r0, #40]\n"
+ "ldrd r10, r11, [r2, #40]\n"
+ "smlad r12, r4, r6, r12\n"
+ "smlad r14, r5, r7, r14\n"
+ "ldrd r4, r5, [r0, #56]\n"
+ "ldrd r6, r7, [r2, #56]\n"
+ "smlad r12, r8, r10, r12\n"
+ "smlad r14, r9, r11, r14\n"
+ "ldrd r8, r9, [r0, #72]\n"
+ "ldrd r10, r11, [r2, #72]\n"
+ "smlad r12, r4, r6, r12\n"
+ "smlad r14, r5, r7, r14\n"
+ "ldrd r4, r5, [r2, #80]\n" /* start loading cos table */
+ "smlad r12, r8, r10, r12\n" /* t1[2] is done */
+ "smlad r14, r9, r11, r14\n" /* t1[3] is done */
+ "ldrd r6, r7, [r2, #88]\n"
+ "ldrd r8, r9, [r2, #96]\n"
+ "ldrd r10, r11, [r2, #104]\n" /* cos table fully loaded */
+ "pkhtb r12, r14, r12, asr #16\n" /* combine t1[2] and t1[3] */
+ "smuad r4, r3, r4\n"
+ "smuad r5, r3, r5\n"
+ "smlad r4, r12, r8, r4\n"
+ "smlad r5, r12, r9, r5\n"
+ "smuad r6, r3, r6\n"
+ "smuad r7, r3, r7\n"
+ "smlad r6, r12, r10, r6\n"
+ "smlad r7, r12, r11, r7\n"
+ "pop {r8-r11}\n"
+ "stmia r1, {r4, r5, r6, r7}\n"
+ "pop {r1, r4-r7, pc}\n"
+ );
+}
+
+#define sbc_analyze_four(in, out, consts) \
+ ((void (*)(int16_t *, int32_t *, const FIXED_T*)) \
+ sbc_analyze_four_armv6)((in), (out), (consts))
+
+static void __attribute__((naked)) sbc_analyze_eight_armv6()
+{
+ /* r0 = in, r1 = out, r2 = consts */
+ asm volatile (
+ "push {r1, r4-r7, lr}\n"
+ "push {r8-r11}\n"
+ "ldrd r4, r5, [r0, #24]\n"
+ "ldrd r6, r7, [r2, #24]\n"
+ "ldrd r8, r9, [r0, #56]\n"
+ "ldrd r10, r11, [r2, #56]\n"
+ "mov r14, #0x8000\n"
+ "smlad r3, r4, r6, r14\n"
+ "smlad r12, r5, r7, r14\n"
+ "ldrd r4, r5, [r0, #88]\n"
+ "ldrd r6, r7, [r2, #88]\n"
+ "smlad r3, r8, r10, r3\n"
+ "smlad r12, r9, r11, r12\n"
+ "ldrd r8, r9, [r0, #120]\n"
+ "ldrd r10, r11, [r2, #120]\n"
+ "smlad r3, r4, r6, r3\n"
+ "smlad r12, r5, r7, r12\n"
+ "ldrd r4, r5, [r0, #152]\n"
+ "ldrd r6, r7, [r2, #152]\n"
+ "smlad r3, r8, r10, r3\n"
+ "smlad r12, r9, r11, r12\n"
+ "ldrd r8, r9, [r0, #16]\n"
+ "ldrd r10, r11, [r2, #16]\n"
+ "smlad r3, r4, r6, r3\n" /* t1[6] is done */
+ "smlad r12, r5, r7, r12\n" /* t1[7] is done */
+ "ldrd r4, r5, [r0, #48]\n"
+ "ldrd r6, r7, [r2, #48]\n"
+ "pkhtb r3, r12, r3, asr #16\n" /* combine t1[6] and t1[7] */
+ "str r3, [sp, #-4]!\n" /* save to stack */
+ "smlad r3, r8, r10, r14\n"
+ "smlad r12, r9, r11, r14\n"
+ "ldrd r8, r9, [r0, #80]\n"
+ "ldrd r10, r11, [r2, #80]\n"
+ "smlad r3, r4, r6, r3\n"
+ "smlad r12, r5, r7, r12\n"
+ "ldrd r4, r5, [r0, #112]\n"
+ "ldrd r6, r7, [r2, #112]\n"
+ "smlad r3, r8, r10, r3\n"
+ "smlad r12, r9, r11, r12\n"
+ "ldrd r8, r9, [r0, #144]\n"
+ "ldrd r10, r11, [r2, #144]\n"
+ "smlad r3, r4, r6, r3\n"
+ "smlad r12, r5, r7, r12\n"
+ "ldrd r4, r5, [r0, #0]\n"
+ "ldrd r6, r7, [r2, #0]\n"
+ "smlad r3, r8, r10, r3\n" /* t1[4] is done */
+ "smlad r12, r9, r11, r12\n" /* t1[5] is done */
+ "ldrd r8, r9, [r0, #32]\n"
+ "ldrd r10, r11, [r2, #32]\n"
+ "pkhtb r3, r12, r3, asr #16\n" /* combine t1[4] and t1[5] */
+ "str r3, [sp, #-4]!\n" /* save to stack */
+ "smlad r3, r4, r6, r14\n"
+ "smlad r12, r5, r7, r14\n"
+ "ldrd r4, r5, [r0, #64]\n"
+ "ldrd r6, r7, [r2, #64]\n"
+ "smlad r3, r8, r10, r3\n"
+ "smlad r12, r9, r11, r12\n"
+ "ldrd r8, r9, [r0, #96]\n"
+ "ldrd r10, r11, [r2, #96]\n"
+ "smlad r3, r4, r6, r3\n"
+ "smlad r12, r5, r7, r12\n"
+ "ldrd r4, r5, [r0, #128]\n"
+ "ldrd r6, r7, [r2, #128]\n"
+ "smlad r3, r8, r10, r3\n"
+ "smlad r12, r9, r11, r12\n"
+ "ldrd r8, r9, [r0, #8]\n"
+ "ldrd r10, r11, [r2, #8]\n"
+ "smlad r3, r4, r6, r3\n" /* t1[0] is done */
+ "smlad r12, r5, r7, r12\n" /* t1[1] is done */
+ "ldrd r4, r5, [r0, #40]\n"
+ "ldrd r6, r7, [r2, #40]\n"
+ "pkhtb r3, r12, r3, asr #16\n" /* combine t1[0] and t1[1] */
+ "smlad r12, r8, r10, r14\n"
+ "smlad r14, r9, r11, r14\n"
+ "ldrd r8, r9, [r0, #72]\n"
+ "ldrd r10, r11, [r2, #72]\n"
+ "smlad r12, r4, r6, r12\n"
+ "smlad r14, r5, r7, r14\n"
+ "ldrd r4, r5, [r0, #104]\n"
+ "ldrd r6, r7, [r2, #104]\n"
+ "smlad r12, r8, r10, r12\n"
+ "smlad r14, r9, r11, r14\n"
+ "ldrd r8, r9, [r0, #136]\n"
+ "ldrd r10, r11, [r2, #136]!\n"
+ "smlad r12, r4, r6, r12\n"
+ "smlad r14, r5, r7, r14\n"
+ "ldrd r4, r5, [r2, #(160 - 136 + 0)]\n"
+ "smlad r12, r8, r10, r12\n" /* t1[2] is done */
+ "smlad r14, r9, r11, r14\n" /* t1[3] is done */
+ "ldrd r6, r7, [r2, #(160 - 136 + 8)]\n"
+ "smuad r4, r3, r4\n"
+ "smuad r5, r3, r5\n"
+ "pkhtb r12, r14, r12, asr #16\n" /* combine t1[2] and t1[3] */
+ /* r3 = t2[0:1] */
+ /* r12 = t2[2:3] */
+ "pop {r0, r14}\n" /* t2[4:5], t2[6:7] */
+ "ldrd r8, r9, [r2, #(160 - 136 + 32)]\n"
+ "smuad r6, r3, r6\n"
+ "smuad r7, r3, r7\n"
+ "ldrd r10, r11, [r2, #(160 - 136 + 40)]\n"
+ "smlad r4, r12, r8, r4\n"
+ "smlad r5, r12, r9, r5\n"
+ "ldrd r8, r9, [r2, #(160 - 136 + 64)]\n"
+ "smlad r6, r12, r10, r6\n"
+ "smlad r7, r12, r11, r7\n"
+ "ldrd r10, r11, [r2, #(160 - 136 + 72)]\n"
+ "smlad r4, r0, r8, r4\n"
+ "smlad r5, r0, r9, r5\n"
+ "ldrd r8, r9, [r2, #(160 - 136 + 96)]\n"
+ "smlad r6, r0, r10, r6\n"
+ "smlad r7, r0, r11, r7\n"
+ "ldrd r10, r11, [r2, #(160 - 136 + 104)]\n"
+ "smlad r4, r14, r8, r4\n"
+ "smlad r5, r14, r9, r5\n"
+ "ldrd r8, r9, [r2, #(160 - 136 + 16 + 0)]\n"
+ "smlad r6, r14, r10, r6\n"
+ "smlad r7, r14, r11, r7\n"
+ "ldrd r10, r11, [r2, #(160 - 136 + 16 + 8)]\n"
+ "stmia r1!, {r4, r5}\n"
+ "smuad r4, r3, r8\n"
+ "smuad r5, r3, r9\n"
+ "ldrd r8, r9, [r2, #(160 - 136 + 16 + 32)]\n"
+ "stmia r1!, {r6, r7}\n"
+ "smuad r6, r3, r10\n"
+ "smuad r7, r3, r11\n"
+ "ldrd r10, r11, [r2, #(160 - 136 + 16 + 40)]\n"
+ "smlad r4, r12, r8, r4\n"
+ "smlad r5, r12, r9, r5\n"
+ "ldrd r8, r9, [r2, #(160 - 136 + 16 + 64)]\n"
+ "smlad r6, r12, r10, r6\n"
+ "smlad r7, r12, r11, r7\n"
+ "ldrd r10, r11, [r2, #(160 - 136 + 16 + 72)]\n"
+ "smlad r4, r0, r8, r4\n"
+ "smlad r5, r0, r9, r5\n"
+ "ldrd r8, r9, [r2, #(160 - 136 + 16 + 96)]\n"
+ "smlad r6, r0, r10, r6\n"
+ "smlad r7, r0, r11, r7\n"
+ "ldrd r10, r11, [r2, #(160 - 136 + 16 + 104)]\n"
+ "smlad r4, r14, r8, r4\n"
+ "smlad r5, r14, r9, r5\n"
+ "smlad r6, r14, r10, r6\n"
+ "smlad r7, r14, r11, r7\n"
+ "pop {r8-r11}\n"
+ "stmia r1!, {r4, r5, r6, r7}\n"
+ "pop {r1, r4-r7, pc}\n"
+ );
+}
+
+#define sbc_analyze_eight(in, out, consts) \
+ ((void (*)(int16_t *, int32_t *, const FIXED_T*)) \
+ sbc_analyze_eight_armv6)((in), (out), (consts))
+
+static void sbc_analyze_4b_4s_armv6(int16_t *x, int32_t *out, int out_stride)
+{
+ /* Analyze blocks */
+ sbc_analyze_four(x + 12, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ sbc_analyze_four(x + 8, out, analysis_consts_fixed4_simd_even);
+ out += out_stride;
+ sbc_analyze_four(x + 4, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ sbc_analyze_four(x + 0, out, analysis_consts_fixed4_simd_even);
+}
+
+static void sbc_analyze_4b_8s_armv6(int16_t *x, int32_t *out, int out_stride)
+{
+ /* Analyze blocks */
+ sbc_analyze_eight(x + 24, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ sbc_analyze_eight(x + 16, out, analysis_consts_fixed8_simd_even);
+ out += out_stride;
+ sbc_analyze_eight(x + 8, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ sbc_analyze_eight(x + 0, out, analysis_consts_fixed8_simd_even);
+}
+
+void sbc_init_primitives_armv6(struct sbc_encoder_state *state)
+{
+ state->sbc_analyze_4b_4s = sbc_analyze_4b_4s_armv6;
+ state->sbc_analyze_4b_8s = sbc_analyze_4b_8s_armv6;
+ state->implementation_info = "ARMv6 SIMD";
+}
+
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_primitives_armv6.h b/src/modules/bluetooth/sbc/sbc_primitives_armv6.h
new file mode 100644
index 00000000..6a9efe50
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives_armv6.h
@@ -0,0 +1,52 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef __SBC_PRIMITIVES_ARMV6_H
+#define __SBC_PRIMITIVES_ARMV6_H
+
+#include "sbc_primitives.h"
+
+#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || \
+ defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || \
+ defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) || \
+ defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7__) || \
+ defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || \
+ defined(__ARM_ARCH_7M__)
+#define SBC_HAVE_ARMV6 1
+#endif
+
+#if !defined(SBC_HIGH_PRECISION) && (SCALE_OUT_BITS == 15) && \
+ defined(__GNUC__) && defined(SBC_HAVE_ARMV6) && \
+ defined(__ARM_EABI__) && !defined(__ARM_NEON__) && \
+ (!defined(__thumb__) || defined(__thumb2__))
+
+#define SBC_BUILD_WITH_ARMV6_SUPPORT
+
+void sbc_init_primitives_armv6(struct sbc_encoder_state *encoder_state);
+
+#endif
+
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_primitives_iwmmxt.c b/src/modules/bluetooth/sbc/sbc_primitives_iwmmxt.c
new file mode 100644
index 00000000..213967ef
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives_iwmmxt.c
@@ -0,0 +1,304 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2010 Keith Mok <ek9852@gmail.com>
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <stdint.h>
+#include <limits.h>
+#include "sbc.h"
+#include "sbc_math.h"
+#include "sbc_tables.h"
+
+#include "sbc_primitives_iwmmxt.h"
+
+/*
+ * IWMMXT optimizations
+ */
+
+#ifdef SBC_BUILD_WITH_IWMMXT_SUPPORT
+
+static inline void sbc_analyze_four_iwmmxt(const int16_t *in, int32_t *out,
+ const FIXED_T *consts)
+{
+ asm volatile (
+ "wldrd wr0, [%0]\n"
+ "tbcstw wr4, %2\n"
+ "wldrd wr2, [%1]\n"
+ "wldrd wr1, [%0, #8]\n"
+ "wldrd wr3, [%1, #8]\n"
+ "wmadds wr0, wr2, wr0\n"
+ " wldrd wr6, [%0, #16]\n"
+ "wmadds wr1, wr3, wr1\n"
+ " wldrd wr7, [%0, #24]\n"
+ "waddwss wr0, wr0, wr4\n"
+ " wldrd wr8, [%1, #16]\n"
+ "waddwss wr1, wr1, wr4\n"
+ " wldrd wr9, [%1, #24]\n"
+ " wmadds wr6, wr8, wr6\n"
+ " wldrd wr2, [%0, #32]\n"
+ " wmadds wr7, wr9, wr7\n"
+ " wldrd wr3, [%0, #40]\n"
+ " waddwss wr0, wr6, wr0\n"
+ " wldrd wr4, [%1, #32]\n"
+ " waddwss wr1, wr7, wr1\n"
+ " wldrd wr5, [%1, #40]\n"
+ " wmadds wr2, wr4, wr2\n"
+ "wldrd wr6, [%0, #48]\n"
+ " wmadds wr3, wr5, wr3\n"
+ "wldrd wr7, [%0, #56]\n"
+ " waddwss wr0, wr2, wr0\n"
+ "wldrd wr8, [%1, #48]\n"
+ " waddwss wr1, wr3, wr1\n"
+ "wldrd wr9, [%1, #56]\n"
+ "wmadds wr6, wr8, wr6\n"
+ " wldrd wr2, [%0, #64]\n"
+ "wmadds wr7, wr9, wr7\n"
+ " wldrd wr3, [%0, #72]\n"
+ "waddwss wr0, wr6, wr0\n"
+ " wldrd wr4, [%1, #64]\n"
+ "waddwss wr1, wr7, wr1\n"
+ " wldrd wr5, [%1, #72]\n"
+ " wmadds wr2, wr4, wr2\n"
+ "tmcr wcgr0, %4\n"
+ " wmadds wr3, wr5, wr3\n"
+ " waddwss wr0, wr2, wr0\n"
+ " waddwss wr1, wr3, wr1\n"
+ "\n"
+ "wsrawg wr0, wr0, wcgr0\n"
+ " wldrd wr4, [%1, #80]\n"
+ "wsrawg wr1, wr1, wcgr0\n"
+ " wldrd wr5, [%1, #88]\n"
+ "wpackwss wr0, wr0, wr0\n"
+ " wldrd wr6, [%1, #96]\n"
+ "wpackwss wr1, wr1, wr1\n"
+ "wmadds wr2, wr5, wr0\n"
+ " wldrd wr7, [%1, #104]\n"
+ "wmadds wr0, wr4, wr0\n"
+ "\n"
+ " wmadds wr3, wr7, wr1\n"
+ " wmadds wr1, wr6, wr1\n"
+ " waddwss wr2, wr3, wr2\n"
+ " waddwss wr0, wr1, wr0\n"
+ "\n"
+ "wstrd wr0, [%3]\n"
+ "wstrd wr2, [%3, #8]\n"
+ :
+ : "r" (in), "r" (consts),
+ "r" (1 << (SBC_PROTO_FIXED4_SCALE - 1)), "r" (out),
+ "r" (SBC_PROTO_FIXED4_SCALE)
+ : "wr0", "wr1", "wr2", "wr3", "wr4", "wr5", "wr6", "wr7",
+ "wr8", "wr9", "wcgr0", "memory");
+}
+
+static inline void sbc_analyze_eight_iwmmxt(const int16_t *in, int32_t *out,
+ const FIXED_T *consts)
+{
+ asm volatile (
+ "wldrd wr0, [%0]\n"
+ "tbcstw wr15, %2\n"
+ "wldrd wr1, [%0, #8]\n"
+ "wldrd wr2, [%0, #16]\n"
+ "wldrd wr3, [%0, #24]\n"
+ "wldrd wr4, [%1]\n"
+ "wldrd wr5, [%1, #8]\n"
+ "wldrd wr6, [%1, #16]\n"
+ "wldrd wr7, [%1, #24]\n"
+ "wmadds wr0, wr0, wr4\n"
+ " wldrd wr8, [%1, #32]\n"
+ "wmadds wr1, wr1, wr5\n"
+ " wldrd wr9, [%1, #40]\n"
+ "wmadds wr2, wr2, wr6\n"
+ " wldrd wr10, [%1, #48]\n"
+ "wmadds wr3, wr3, wr7\n"
+ " wldrd wr11, [%1, #56]\n"
+ "waddwss wr0, wr0, wr15\n"
+ " wldrd wr4, [%0, #32]\n"
+ "waddwss wr1, wr1, wr15\n"
+ " wldrd wr5, [%0, #40]\n"
+ "waddwss wr2, wr2, wr15\n"
+ " wldrd wr6, [%0, #48]\n"
+ "waddwss wr3, wr3, wr15\n"
+ " wldrd wr7, [%0, #56]\n"
+ " wmadds wr4, wr4, wr8\n"
+ " wldrd wr12, [%0, #64]\n"
+ " wmadds wr5, wr5, wr9\n"
+ " wldrd wr13, [%0, #72]\n"
+ " wmadds wr6, wr6, wr10\n"
+ " wldrd wr14, [%0, #80]\n"
+ " wmadds wr7, wr7, wr11\n"
+ " wldrd wr15, [%0, #88]\n"
+ " waddwss wr0, wr4, wr0\n"
+ " wldrd wr8, [%1, #64]\n"
+ " waddwss wr1, wr5, wr1\n"
+ " wldrd wr9, [%1, #72]\n"
+ " waddwss wr2, wr6, wr2\n"
+ " wldrd wr10, [%1, #80]\n"
+ " waddwss wr3, wr7, wr3\n"
+ " wldrd wr11, [%1, #88]\n"
+ " wmadds wr12, wr12, wr8\n"
+ "wldrd wr4, [%0, #96]\n"
+ " wmadds wr13, wr13, wr9\n"
+ "wldrd wr5, [%0, #104]\n"
+ " wmadds wr14, wr14, wr10\n"
+ "wldrd wr6, [%0, #112]\n"
+ " wmadds wr15, wr15, wr11\n"
+ "wldrd wr7, [%0, #120]\n"
+ " waddwss wr0, wr12, wr0\n"
+ "wldrd wr8, [%1, #96]\n"
+ " waddwss wr1, wr13, wr1\n"
+ "wldrd wr9, [%1, #104]\n"
+ " waddwss wr2, wr14, wr2\n"
+ "wldrd wr10, [%1, #112]\n"
+ " waddwss wr3, wr15, wr3\n"
+ "wldrd wr11, [%1, #120]\n"
+ "wmadds wr4, wr4, wr8\n"
+ " wldrd wr12, [%0, #128]\n"
+ "wmadds wr5, wr5, wr9\n"
+ " wldrd wr13, [%0, #136]\n"
+ "wmadds wr6, wr6, wr10\n"
+ " wldrd wr14, [%0, #144]\n"
+ "wmadds wr7, wr7, wr11\n"
+ " wldrd wr15, [%0, #152]\n"
+ "waddwss wr0, wr4, wr0\n"
+ " wldrd wr8, [%1, #128]\n"
+ "waddwss wr1, wr5, wr1\n"
+ " wldrd wr9, [%1, #136]\n"
+ "waddwss wr2, wr6, wr2\n"
+ " wldrd wr10, [%1, #144]\n"
+ " waddwss wr3, wr7, wr3\n"
+ " wldrd wr11, [%1, #152]\n"
+ " wmadds wr12, wr12, wr8\n"
+ "tmcr wcgr0, %4\n"
+ " wmadds wr13, wr13, wr9\n"
+ " wmadds wr14, wr14, wr10\n"
+ " wmadds wr15, wr15, wr11\n"
+ " waddwss wr0, wr12, wr0\n"
+ " waddwss wr1, wr13, wr1\n"
+ " waddwss wr2, wr14, wr2\n"
+ " waddwss wr3, wr15, wr3\n"
+ "\n"
+ "wsrawg wr0, wr0, wcgr0\n"
+ "wsrawg wr1, wr1, wcgr0\n"
+ "wsrawg wr2, wr2, wcgr0\n"
+ "wsrawg wr3, wr3, wcgr0\n"
+ "\n"
+ "wpackwss wr0, wr0, wr0\n"
+ "wpackwss wr1, wr1, wr1\n"
+ " wldrd wr4, [%1, #160]\n"
+ "wpackwss wr2, wr2, wr2\n"
+ " wldrd wr5, [%1, #168]\n"
+ "wpackwss wr3, wr3, wr3\n"
+ " wldrd wr6, [%1, #192]\n"
+ " wmadds wr4, wr4, wr0\n"
+ " wldrd wr7, [%1, #200]\n"
+ " wmadds wr5, wr5, wr0\n"
+ " wldrd wr8, [%1, #224]\n"
+ " wmadds wr6, wr6, wr1\n"
+ " wldrd wr9, [%1, #232]\n"
+ " wmadds wr7, wr7, wr1\n"
+ " waddwss wr4, wr6, wr4\n"
+ " waddwss wr5, wr7, wr5\n"
+ " wmadds wr8, wr8, wr2\n"
+ "wldrd wr6, [%1, #256]\n"
+ " wmadds wr9, wr9, wr2\n"
+ "wldrd wr7, [%1, #264]\n"
+ "waddwss wr4, wr8, wr4\n"
+ " waddwss wr5, wr9, wr5\n"
+ "wmadds wr6, wr6, wr3\n"
+ "wmadds wr7, wr7, wr3\n"
+ "waddwss wr4, wr6, wr4\n"
+ "waddwss wr5, wr7, wr5\n"
+ "\n"
+ "wstrd wr4, [%3]\n"
+ "wstrd wr5, [%3, #8]\n"
+ "\n"
+ "wldrd wr6, [%1, #176]\n"
+ "wldrd wr5, [%1, #184]\n"
+ "wmadds wr5, wr5, wr0\n"
+ "wldrd wr8, [%1, #208]\n"
+ "wmadds wr0, wr6, wr0\n"
+ "wldrd wr9, [%1, #216]\n"
+ "wmadds wr9, wr9, wr1\n"
+ "wldrd wr6, [%1, #240]\n"
+ "wmadds wr1, wr8, wr1\n"
+ "wldrd wr7, [%1, #248]\n"
+ "waddwss wr0, wr1, wr0\n"
+ "waddwss wr5, wr9, wr5\n"
+ "wmadds wr7, wr7, wr2\n"
+ "wldrd wr8, [%1, #272]\n"
+ "wmadds wr2, wr6, wr2\n"
+ "wldrd wr9, [%1, #280]\n"
+ "waddwss wr0, wr2, wr0\n"
+ "waddwss wr5, wr7, wr5\n"
+ "wmadds wr9, wr9, wr3\n"
+ "wmadds wr3, wr8, wr3\n"
+ "waddwss wr0, wr3, wr0\n"
+ "waddwss wr5, wr9, wr5\n"
+ "\n"
+ "wstrd wr0, [%3, #16]\n"
+ "wstrd wr5, [%3, #24]\n"
+ :
+ : "r" (in), "r" (consts),
+ "r" (1 << (SBC_PROTO_FIXED8_SCALE - 1)), "r" (out),
+ "r" (SBC_PROTO_FIXED8_SCALE)
+ : "wr0", "wr1", "wr2", "wr3", "wr4", "wr5", "wr6", "wr7",
+ "wr8", "wr9", "wr10", "wr11", "wr12", "wr13", "wr14", "wr15",
+ "wcgr0", "memory");
+}
+
+static inline void sbc_analyze_4b_4s_iwmmxt(int16_t *x, int32_t *out,
+ int out_stride)
+{
+ /* Analyze blocks */
+ sbc_analyze_four_iwmmxt(x + 12, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ sbc_analyze_four_iwmmxt(x + 8, out, analysis_consts_fixed4_simd_even);
+ out += out_stride;
+ sbc_analyze_four_iwmmxt(x + 4, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ sbc_analyze_four_iwmmxt(x + 0, out, analysis_consts_fixed4_simd_even);
+}
+
+static inline void sbc_analyze_4b_8s_iwmmxt(int16_t *x, int32_t *out,
+ int out_stride)
+{
+ /* Analyze blocks */
+ sbc_analyze_eight_iwmmxt(x + 24, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ sbc_analyze_eight_iwmmxt(x + 16, out, analysis_consts_fixed8_simd_even);
+ out += out_stride;
+ sbc_analyze_eight_iwmmxt(x + 8, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ sbc_analyze_eight_iwmmxt(x + 0, out, analysis_consts_fixed8_simd_even);
+}
+
+void sbc_init_primitives_iwmmxt(struct sbc_encoder_state *state)
+{
+ state->sbc_analyze_4b_4s = sbc_analyze_4b_4s_iwmmxt;
+ state->sbc_analyze_4b_8s = sbc_analyze_4b_8s_iwmmxt;
+ state->implementation_info = "IWMMXT";
+}
+
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_primitives_iwmmxt.h b/src/modules/bluetooth/sbc/sbc_primitives_iwmmxt.h
new file mode 100644
index 00000000..b535e686
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives_iwmmxt.h
@@ -0,0 +1,42 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2010 Keith Mok <ek9852@gmail.com>
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef __SBC_PRIMITIVES_IWMMXT_H
+#define __SBC_PRIMITIVES_IWMMXT_H
+
+#include "sbc_primitives.h"
+
+#if defined(__GNUC__) && defined(__IWMMXT__) && \
+ !defined(SBC_HIGH_PRECISION) && (SCALE_OUT_BITS == 15)
+
+#define SBC_BUILD_WITH_IWMMXT_SUPPORT
+
+void sbc_init_primitives_iwmmxt(struct sbc_encoder_state *encoder_state);
+
+#endif
+
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_primitives_mmx.c b/src/modules/bluetooth/sbc/sbc_primitives_mmx.c
new file mode 100644
index 00000000..7f2fbc37
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives_mmx.c
@@ -0,0 +1,375 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <stdint.h>
+#include <limits.h>
+#include "sbc.h"
+#include "sbc_math.h"
+#include "sbc_tables.h"
+
+#include "sbc_primitives_mmx.h"
+
+/*
+ * MMX optimizations
+ */
+
+#ifdef SBC_BUILD_WITH_MMX_SUPPORT
+
+static inline void sbc_analyze_four_mmx(const int16_t *in, int32_t *out,
+ const FIXED_T *consts)
+{
+ static const SBC_ALIGNED int32_t round_c[2] = {
+ 1 << (SBC_PROTO_FIXED4_SCALE - 1),
+ 1 << (SBC_PROTO_FIXED4_SCALE - 1),
+ };
+ asm volatile (
+ "movq (%0), %%mm0\n"
+ "movq 8(%0), %%mm1\n"
+ "pmaddwd (%1), %%mm0\n"
+ "pmaddwd 8(%1), %%mm1\n"
+ "paddd (%2), %%mm0\n"
+ "paddd (%2), %%mm1\n"
+ "\n"
+ "movq 16(%0), %%mm2\n"
+ "movq 24(%0), %%mm3\n"
+ "pmaddwd 16(%1), %%mm2\n"
+ "pmaddwd 24(%1), %%mm3\n"
+ "paddd %%mm2, %%mm0\n"
+ "paddd %%mm3, %%mm1\n"
+ "\n"
+ "movq 32(%0), %%mm2\n"
+ "movq 40(%0), %%mm3\n"
+ "pmaddwd 32(%1), %%mm2\n"
+ "pmaddwd 40(%1), %%mm3\n"
+ "paddd %%mm2, %%mm0\n"
+ "paddd %%mm3, %%mm1\n"
+ "\n"
+ "movq 48(%0), %%mm2\n"
+ "movq 56(%0), %%mm3\n"
+ "pmaddwd 48(%1), %%mm2\n"
+ "pmaddwd 56(%1), %%mm3\n"
+ "paddd %%mm2, %%mm0\n"
+ "paddd %%mm3, %%mm1\n"
+ "\n"
+ "movq 64(%0), %%mm2\n"
+ "movq 72(%0), %%mm3\n"
+ "pmaddwd 64(%1), %%mm2\n"
+ "pmaddwd 72(%1), %%mm3\n"
+ "paddd %%mm2, %%mm0\n"
+ "paddd %%mm3, %%mm1\n"
+ "\n"
+ "psrad %4, %%mm0\n"
+ "psrad %4, %%mm1\n"
+ "packssdw %%mm0, %%mm0\n"
+ "packssdw %%mm1, %%mm1\n"
+ "\n"
+ "movq %%mm0, %%mm2\n"
+ "pmaddwd 80(%1), %%mm0\n"
+ "pmaddwd 88(%1), %%mm2\n"
+ "\n"
+ "movq %%mm1, %%mm3\n"
+ "pmaddwd 96(%1), %%mm1\n"
+ "pmaddwd 104(%1), %%mm3\n"
+ "paddd %%mm1, %%mm0\n"
+ "paddd %%mm3, %%mm2\n"
+ "\n"
+ "movq %%mm0, (%3)\n"
+ "movq %%mm2, 8(%3)\n"
+ :
+ : "r" (in), "r" (consts), "r" (&round_c), "r" (out),
+ "i" (SBC_PROTO_FIXED4_SCALE)
+ : "cc", "memory");
+}
+
+static inline void sbc_analyze_eight_mmx(const int16_t *in, int32_t *out,
+ const FIXED_T *consts)
+{
+ static const SBC_ALIGNED int32_t round_c[2] = {
+ 1 << (SBC_PROTO_FIXED8_SCALE - 1),
+ 1 << (SBC_PROTO_FIXED8_SCALE - 1),
+ };
+ asm volatile (
+ "movq (%0), %%mm0\n"
+ "movq 8(%0), %%mm1\n"
+ "movq 16(%0), %%mm2\n"
+ "movq 24(%0), %%mm3\n"
+ "pmaddwd (%1), %%mm0\n"
+ "pmaddwd 8(%1), %%mm1\n"
+ "pmaddwd 16(%1), %%mm2\n"
+ "pmaddwd 24(%1), %%mm3\n"
+ "paddd (%2), %%mm0\n"
+ "paddd (%2), %%mm1\n"
+ "paddd (%2), %%mm2\n"
+ "paddd (%2), %%mm3\n"
+ "\n"
+ "movq 32(%0), %%mm4\n"
+ "movq 40(%0), %%mm5\n"
+ "movq 48(%0), %%mm6\n"
+ "movq 56(%0), %%mm7\n"
+ "pmaddwd 32(%1), %%mm4\n"
+ "pmaddwd 40(%1), %%mm5\n"
+ "pmaddwd 48(%1), %%mm6\n"
+ "pmaddwd 56(%1), %%mm7\n"
+ "paddd %%mm4, %%mm0\n"
+ "paddd %%mm5, %%mm1\n"
+ "paddd %%mm6, %%mm2\n"
+ "paddd %%mm7, %%mm3\n"
+ "\n"
+ "movq 64(%0), %%mm4\n"
+ "movq 72(%0), %%mm5\n"
+ "movq 80(%0), %%mm6\n"
+ "movq 88(%0), %%mm7\n"
+ "pmaddwd 64(%1), %%mm4\n"
+ "pmaddwd 72(%1), %%mm5\n"
+ "pmaddwd 80(%1), %%mm6\n"
+ "pmaddwd 88(%1), %%mm7\n"
+ "paddd %%mm4, %%mm0\n"
+ "paddd %%mm5, %%mm1\n"
+ "paddd %%mm6, %%mm2\n"
+ "paddd %%mm7, %%mm3\n"
+ "\n"
+ "movq 96(%0), %%mm4\n"
+ "movq 104(%0), %%mm5\n"
+ "movq 112(%0), %%mm6\n"
+ "movq 120(%0), %%mm7\n"
+ "pmaddwd 96(%1), %%mm4\n"
+ "pmaddwd 104(%1), %%mm5\n"
+ "pmaddwd 112(%1), %%mm6\n"
+ "pmaddwd 120(%1), %%mm7\n"
+ "paddd %%mm4, %%mm0\n"
+ "paddd %%mm5, %%mm1\n"
+ "paddd %%mm6, %%mm2\n"
+ "paddd %%mm7, %%mm3\n"
+ "\n"
+ "movq 128(%0), %%mm4\n"
+ "movq 136(%0), %%mm5\n"
+ "movq 144(%0), %%mm6\n"
+ "movq 152(%0), %%mm7\n"
+ "pmaddwd 128(%1), %%mm4\n"
+ "pmaddwd 136(%1), %%mm5\n"
+ "pmaddwd 144(%1), %%mm6\n"
+ "pmaddwd 152(%1), %%mm7\n"
+ "paddd %%mm4, %%mm0\n"
+ "paddd %%mm5, %%mm1\n"
+ "paddd %%mm6, %%mm2\n"
+ "paddd %%mm7, %%mm3\n"
+ "\n"
+ "psrad %4, %%mm0\n"
+ "psrad %4, %%mm1\n"
+ "psrad %4, %%mm2\n"
+ "psrad %4, %%mm3\n"
+ "\n"
+ "packssdw %%mm0, %%mm0\n"
+ "packssdw %%mm1, %%mm1\n"
+ "packssdw %%mm2, %%mm2\n"
+ "packssdw %%mm3, %%mm3\n"
+ "\n"
+ "movq %%mm0, %%mm4\n"
+ "movq %%mm0, %%mm5\n"
+ "pmaddwd 160(%1), %%mm4\n"
+ "pmaddwd 168(%1), %%mm5\n"
+ "\n"
+ "movq %%mm1, %%mm6\n"
+ "movq %%mm1, %%mm7\n"
+ "pmaddwd 192(%1), %%mm6\n"
+ "pmaddwd 200(%1), %%mm7\n"
+ "paddd %%mm6, %%mm4\n"
+ "paddd %%mm7, %%mm5\n"
+ "\n"
+ "movq %%mm2, %%mm6\n"
+ "movq %%mm2, %%mm7\n"
+ "pmaddwd 224(%1), %%mm6\n"
+ "pmaddwd 232(%1), %%mm7\n"
+ "paddd %%mm6, %%mm4\n"
+ "paddd %%mm7, %%mm5\n"
+ "\n"
+ "movq %%mm3, %%mm6\n"
+ "movq %%mm3, %%mm7\n"
+ "pmaddwd 256(%1), %%mm6\n"
+ "pmaddwd 264(%1), %%mm7\n"
+ "paddd %%mm6, %%mm4\n"
+ "paddd %%mm7, %%mm5\n"
+ "\n"
+ "movq %%mm4, (%3)\n"
+ "movq %%mm5, 8(%3)\n"
+ "\n"
+ "movq %%mm0, %%mm5\n"
+ "pmaddwd 176(%1), %%mm0\n"
+ "pmaddwd 184(%1), %%mm5\n"
+ "\n"
+ "movq %%mm1, %%mm7\n"
+ "pmaddwd 208(%1), %%mm1\n"
+ "pmaddwd 216(%1), %%mm7\n"
+ "paddd %%mm1, %%mm0\n"
+ "paddd %%mm7, %%mm5\n"
+ "\n"
+ "movq %%mm2, %%mm7\n"
+ "pmaddwd 240(%1), %%mm2\n"
+ "pmaddwd 248(%1), %%mm7\n"
+ "paddd %%mm2, %%mm0\n"
+ "paddd %%mm7, %%mm5\n"
+ "\n"
+ "movq %%mm3, %%mm7\n"
+ "pmaddwd 272(%1), %%mm3\n"
+ "pmaddwd 280(%1), %%mm7\n"
+ "paddd %%mm3, %%mm0\n"
+ "paddd %%mm7, %%mm5\n"
+ "\n"
+ "movq %%mm0, 16(%3)\n"
+ "movq %%mm5, 24(%3)\n"
+ :
+ : "r" (in), "r" (consts), "r" (&round_c), "r" (out),
+ "i" (SBC_PROTO_FIXED8_SCALE)
+ : "cc", "memory");
+}
+
+static inline void sbc_analyze_4b_4s_mmx(int16_t *x, int32_t *out,
+ int out_stride)
+{
+ /* Analyze blocks */
+ sbc_analyze_four_mmx(x + 12, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ sbc_analyze_four_mmx(x + 8, out, analysis_consts_fixed4_simd_even);
+ out += out_stride;
+ sbc_analyze_four_mmx(x + 4, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ sbc_analyze_four_mmx(x + 0, out, analysis_consts_fixed4_simd_even);
+
+ asm volatile ("emms\n");
+}
+
+static inline void sbc_analyze_4b_8s_mmx(int16_t *x, int32_t *out,
+ int out_stride)
+{
+ /* Analyze blocks */
+ sbc_analyze_eight_mmx(x + 24, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ sbc_analyze_eight_mmx(x + 16, out, analysis_consts_fixed8_simd_even);
+ out += out_stride;
+ sbc_analyze_eight_mmx(x + 8, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ sbc_analyze_eight_mmx(x + 0, out, analysis_consts_fixed8_simd_even);
+
+ asm volatile ("emms\n");
+}
+
+static void sbc_calc_scalefactors_mmx(
+ int32_t sb_sample_f[16][2][8],
+ uint32_t scale_factor[2][8],
+ int blocks, int channels, int subbands)
+{
+ static const SBC_ALIGNED int32_t consts[2] = {
+ 1 << SCALE_OUT_BITS,
+ 1 << SCALE_OUT_BITS,
+ };
+ int ch, sb;
+ intptr_t blk;
+ for (ch = 0; ch < channels; ch++) {
+ for (sb = 0; sb < subbands; sb += 2) {
+ blk = (blocks - 1) * (((char *) &sb_sample_f[1][0][0] -
+ (char *) &sb_sample_f[0][0][0]));
+ asm volatile (
+ "movq (%4), %%mm0\n"
+ "1:\n"
+ "movq (%1, %0), %%mm1\n"
+ "pxor %%mm2, %%mm2\n"
+ "pcmpgtd %%mm2, %%mm1\n"
+ "paddd (%1, %0), %%mm1\n"
+ "pcmpgtd %%mm1, %%mm2\n"
+ "pxor %%mm2, %%mm1\n"
+
+ "por %%mm1, %%mm0\n"
+
+ "sub %2, %0\n"
+ "jns 1b\n"
+
+ "movd %%mm0, %k0\n"
+ "psrlq $32, %%mm0\n"
+ "bsrl %k0, %k0\n"
+ "subl %5, %k0\n"
+ "movl %k0, (%3)\n"
+
+ "movd %%mm0, %k0\n"
+ "bsrl %k0, %k0\n"
+ "subl %5, %k0\n"
+ "movl %k0, 4(%3)\n"
+ : "+r" (blk)
+ : "r" (&sb_sample_f[0][ch][sb]),
+ "i" ((char *) &sb_sample_f[1][0][0] -
+ (char *) &sb_sample_f[0][0][0]),
+ "r" (&scale_factor[ch][sb]),
+ "r" (&consts),
+ "i" (SCALE_OUT_BITS)
+ : "cc", "memory");
+ }
+ }
+ asm volatile ("emms\n");
+}
+
+static int check_mmx_support(void)
+{
+#ifdef __amd64__
+ return 1; /* We assume that all 64-bit processors have MMX support */
+#else
+ int cpuid_feature_information;
+ asm volatile (
+ /* According to Intel manual, CPUID instruction is supported
+ * if the value of ID bit (bit 21) in EFLAGS can be modified */
+ "pushf\n"
+ "movl (%%esp), %0\n"
+ "xorl $0x200000, (%%esp)\n" /* try to modify ID bit */
+ "popf\n"
+ "pushf\n"
+ "xorl (%%esp), %0\n" /* check if ID bit changed */
+ "jz 1f\n"
+ "push %%eax\n"
+ "push %%ebx\n"
+ "push %%ecx\n"
+ "mov $1, %%eax\n"
+ "cpuid\n"
+ "pop %%ecx\n"
+ "pop %%ebx\n"
+ "pop %%eax\n"
+ "1:\n"
+ "popf\n"
+ : "=d" (cpuid_feature_information)
+ :
+ : "cc");
+ return cpuid_feature_information & (1 << 23);
+#endif
+}
+
+void sbc_init_primitives_mmx(struct sbc_encoder_state *state)
+{
+ if (check_mmx_support()) {
+ state->sbc_analyze_4b_4s = sbc_analyze_4b_4s_mmx;
+ state->sbc_analyze_4b_8s = sbc_analyze_4b_8s_mmx;
+ state->sbc_calc_scalefactors = sbc_calc_scalefactors_mmx;
+ state->implementation_info = "MMX";
+ }
+}
+
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_primitives_mmx.h b/src/modules/bluetooth/sbc/sbc_primitives_mmx.h
new file mode 100644
index 00000000..e0e728bc
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives_mmx.h
@@ -0,0 +1,41 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef __SBC_PRIMITIVES_MMX_H
+#define __SBC_PRIMITIVES_MMX_H
+
+#include "sbc_primitives.h"
+
+#if defined(__GNUC__) && (defined(__i386__) || defined(__amd64__)) && \
+ !defined(SBC_HIGH_PRECISION) && (SCALE_OUT_BITS == 15)
+
+#define SBC_BUILD_WITH_MMX_SUPPORT
+
+void sbc_init_primitives_mmx(struct sbc_encoder_state *encoder_state);
+
+#endif
+
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_primitives_neon.c b/src/modules/bluetooth/sbc/sbc_primitives_neon.c
new file mode 100644
index 00000000..0572158d
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives_neon.c
@@ -0,0 +1,893 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <stdint.h>
+#include <limits.h>
+#include "sbc.h"
+#include "sbc_math.h"
+#include "sbc_tables.h"
+
+#include "sbc_primitives_neon.h"
+
+/*
+ * ARM NEON optimizations
+ */
+
+#ifdef SBC_BUILD_WITH_NEON_SUPPORT
+
+static inline void _sbc_analyze_four_neon(const int16_t *in, int32_t *out,
+ const FIXED_T *consts)
+{
+ /* TODO: merge even and odd cases (or even merge all four calls to this
+ * function) in order to have only aligned reads from 'in' array
+ * and reduce number of load instructions */
+ asm volatile (
+ "vld1.16 {d4, d5}, [%0, :64]!\n"
+ "vld1.16 {d8, d9}, [%1, :128]!\n"
+
+ "vmull.s16 q0, d4, d8\n"
+ "vld1.16 {d6, d7}, [%0, :64]!\n"
+ "vmull.s16 q1, d5, d9\n"
+ "vld1.16 {d10, d11}, [%1, :128]!\n"
+
+ "vmlal.s16 q0, d6, d10\n"
+ "vld1.16 {d4, d5}, [%0, :64]!\n"
+ "vmlal.s16 q1, d7, d11\n"
+ "vld1.16 {d8, d9}, [%1, :128]!\n"
+
+ "vmlal.s16 q0, d4, d8\n"
+ "vld1.16 {d6, d7}, [%0, :64]!\n"
+ "vmlal.s16 q1, d5, d9\n"
+ "vld1.16 {d10, d11}, [%1, :128]!\n"
+
+ "vmlal.s16 q0, d6, d10\n"
+ "vld1.16 {d4, d5}, [%0, :64]!\n"
+ "vmlal.s16 q1, d7, d11\n"
+ "vld1.16 {d8, d9}, [%1, :128]!\n"
+
+ "vmlal.s16 q0, d4, d8\n"
+ "vmlal.s16 q1, d5, d9\n"
+
+ "vpadd.s32 d0, d0, d1\n"
+ "vpadd.s32 d1, d2, d3\n"
+
+ "vrshrn.s32 d0, q0, %3\n"
+
+ "vld1.16 {d2, d3, d4, d5}, [%1, :128]!\n"
+
+ "vdup.i32 d1, d0[1]\n" /* TODO: can be eliminated */
+ "vdup.i32 d0, d0[0]\n" /* TODO: can be eliminated */
+
+ "vmull.s16 q3, d2, d0\n"
+ "vmull.s16 q4, d3, d0\n"
+ "vmlal.s16 q3, d4, d1\n"
+ "vmlal.s16 q4, d5, d1\n"
+
+ "vpadd.s32 d0, d6, d7\n" /* TODO: can be eliminated */
+ "vpadd.s32 d1, d8, d9\n" /* TODO: can be eliminated */
+
+ "vst1.32 {d0, d1}, [%2, :128]\n"
+ : "+r" (in), "+r" (consts)
+ : "r" (out),
+ "i" (SBC_PROTO_FIXED4_SCALE)
+ : "memory",
+ "d0", "d1", "d2", "d3", "d4", "d5",
+ "d6", "d7", "d8", "d9", "d10", "d11");
+}
+
+static inline void _sbc_analyze_eight_neon(const int16_t *in, int32_t *out,
+ const FIXED_T *consts)
+{
+ /* TODO: merge even and odd cases (or even merge all four calls to this
+ * function) in order to have only aligned reads from 'in' array
+ * and reduce number of load instructions */
+ asm volatile (
+ "vld1.16 {d4, d5}, [%0, :64]!\n"
+ "vld1.16 {d8, d9}, [%1, :128]!\n"
+
+ "vmull.s16 q6, d4, d8\n"
+ "vld1.16 {d6, d7}, [%0, :64]!\n"
+ "vmull.s16 q7, d5, d9\n"
+ "vld1.16 {d10, d11}, [%1, :128]!\n"
+ "vmull.s16 q8, d6, d10\n"
+ "vld1.16 {d4, d5}, [%0, :64]!\n"
+ "vmull.s16 q9, d7, d11\n"
+ "vld1.16 {d8, d9}, [%1, :128]!\n"
+
+ "vmlal.s16 q6, d4, d8\n"
+ "vld1.16 {d6, d7}, [%0, :64]!\n"
+ "vmlal.s16 q7, d5, d9\n"
+ "vld1.16 {d10, d11}, [%1, :128]!\n"
+ "vmlal.s16 q8, d6, d10\n"
+ "vld1.16 {d4, d5}, [%0, :64]!\n"
+ "vmlal.s16 q9, d7, d11\n"
+ "vld1.16 {d8, d9}, [%1, :128]!\n"
+
+ "vmlal.s16 q6, d4, d8\n"
+ "vld1.16 {d6, d7}, [%0, :64]!\n"
+ "vmlal.s16 q7, d5, d9\n"
+ "vld1.16 {d10, d11}, [%1, :128]!\n"
+ "vmlal.s16 q8, d6, d10\n"
+ "vld1.16 {d4, d5}, [%0, :64]!\n"
+ "vmlal.s16 q9, d7, d11\n"
+ "vld1.16 {d8, d9}, [%1, :128]!\n"
+
+ "vmlal.s16 q6, d4, d8\n"
+ "vld1.16 {d6, d7}, [%0, :64]!\n"
+ "vmlal.s16 q7, d5, d9\n"
+ "vld1.16 {d10, d11}, [%1, :128]!\n"
+ "vmlal.s16 q8, d6, d10\n"
+ "vld1.16 {d4, d5}, [%0, :64]!\n"
+ "vmlal.s16 q9, d7, d11\n"
+ "vld1.16 {d8, d9}, [%1, :128]!\n"
+
+ "vmlal.s16 q6, d4, d8\n"
+ "vld1.16 {d6, d7}, [%0, :64]!\n"
+ "vmlal.s16 q7, d5, d9\n"
+ "vld1.16 {d10, d11}, [%1, :128]!\n"
+
+ "vmlal.s16 q8, d6, d10\n"
+ "vmlal.s16 q9, d7, d11\n"
+
+ "vpadd.s32 d0, d12, d13\n"
+ "vpadd.s32 d1, d14, d15\n"
+ "vpadd.s32 d2, d16, d17\n"
+ "vpadd.s32 d3, d18, d19\n"
+
+ "vrshr.s32 q0, q0, %3\n"
+ "vrshr.s32 q1, q1, %3\n"
+ "vmovn.s32 d0, q0\n"
+ "vmovn.s32 d1, q1\n"
+
+ "vdup.i32 d3, d1[1]\n" /* TODO: can be eliminated */
+ "vdup.i32 d2, d1[0]\n" /* TODO: can be eliminated */
+ "vdup.i32 d1, d0[1]\n" /* TODO: can be eliminated */
+ "vdup.i32 d0, d0[0]\n" /* TODO: can be eliminated */
+
+ "vld1.16 {d4, d5}, [%1, :128]!\n"
+ "vmull.s16 q6, d4, d0\n"
+ "vld1.16 {d6, d7}, [%1, :128]!\n"
+ "vmull.s16 q7, d5, d0\n"
+ "vmull.s16 q8, d6, d0\n"
+ "vmull.s16 q9, d7, d0\n"
+
+ "vld1.16 {d4, d5}, [%1, :128]!\n"
+ "vmlal.s16 q6, d4, d1\n"
+ "vld1.16 {d6, d7}, [%1, :128]!\n"
+ "vmlal.s16 q7, d5, d1\n"
+ "vmlal.s16 q8, d6, d1\n"
+ "vmlal.s16 q9, d7, d1\n"
+
+ "vld1.16 {d4, d5}, [%1, :128]!\n"
+ "vmlal.s16 q6, d4, d2\n"
+ "vld1.16 {d6, d7}, [%1, :128]!\n"
+ "vmlal.s16 q7, d5, d2\n"
+ "vmlal.s16 q8, d6, d2\n"
+ "vmlal.s16 q9, d7, d2\n"
+
+ "vld1.16 {d4, d5}, [%1, :128]!\n"
+ "vmlal.s16 q6, d4, d3\n"
+ "vld1.16 {d6, d7}, [%1, :128]!\n"
+ "vmlal.s16 q7, d5, d3\n"
+ "vmlal.s16 q8, d6, d3\n"
+ "vmlal.s16 q9, d7, d3\n"
+
+ "vpadd.s32 d0, d12, d13\n" /* TODO: can be eliminated */
+ "vpadd.s32 d1, d14, d15\n" /* TODO: can be eliminated */
+ "vpadd.s32 d2, d16, d17\n" /* TODO: can be eliminated */
+ "vpadd.s32 d3, d18, d19\n" /* TODO: can be eliminated */
+
+ "vst1.32 {d0, d1, d2, d3}, [%2, :128]\n"
+ : "+r" (in), "+r" (consts)
+ : "r" (out),
+ "i" (SBC_PROTO_FIXED8_SCALE)
+ : "memory",
+ "d0", "d1", "d2", "d3", "d4", "d5",
+ "d6", "d7", "d8", "d9", "d10", "d11",
+ "d12", "d13", "d14", "d15", "d16", "d17",
+ "d18", "d19");
+}
+
+static inline void sbc_analyze_4b_4s_neon(int16_t *x,
+ int32_t *out, int out_stride)
+{
+ /* Analyze blocks */
+ _sbc_analyze_four_neon(x + 12, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ _sbc_analyze_four_neon(x + 8, out, analysis_consts_fixed4_simd_even);
+ out += out_stride;
+ _sbc_analyze_four_neon(x + 4, out, analysis_consts_fixed4_simd_odd);
+ out += out_stride;
+ _sbc_analyze_four_neon(x + 0, out, analysis_consts_fixed4_simd_even);
+}
+
+static inline void sbc_analyze_4b_8s_neon(int16_t *x,
+ int32_t *out, int out_stride)
+{
+ /* Analyze blocks */
+ _sbc_analyze_eight_neon(x + 24, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ _sbc_analyze_eight_neon(x + 16, out, analysis_consts_fixed8_simd_even);
+ out += out_stride;
+ _sbc_analyze_eight_neon(x + 8, out, analysis_consts_fixed8_simd_odd);
+ out += out_stride;
+ _sbc_analyze_eight_neon(x + 0, out, analysis_consts_fixed8_simd_even);
+}
+
+static void sbc_calc_scalefactors_neon(
+ int32_t sb_sample_f[16][2][8],
+ uint32_t scale_factor[2][8],
+ int blocks, int channels, int subbands)
+{
+ int ch, sb;
+ for (ch = 0; ch < channels; ch++) {
+ for (sb = 0; sb < subbands; sb += 4) {
+ int blk = blocks;
+ int32_t *in = &sb_sample_f[0][ch][sb];
+ asm volatile (
+ "vmov.s32 q0, #0\n"
+ "vmov.s32 q1, %[c1]\n"
+ "vmov.s32 q14, #1\n"
+ "vmov.s32 q15, %[c2]\n"
+ "vadd.s32 q1, q1, q14\n"
+ "1:\n"
+ "vld1.32 {d16, d17}, [%[in], :128], %[inc]\n"
+ "vabs.s32 q8, q8\n"
+ "vld1.32 {d18, d19}, [%[in], :128], %[inc]\n"
+ "vabs.s32 q9, q9\n"
+ "vld1.32 {d20, d21}, [%[in], :128], %[inc]\n"
+ "vabs.s32 q10, q10\n"
+ "vld1.32 {d22, d23}, [%[in], :128], %[inc]\n"
+ "vabs.s32 q11, q11\n"
+ "vmax.s32 q0, q0, q8\n"
+ "vmax.s32 q1, q1, q9\n"
+ "vmax.s32 q0, q0, q10\n"
+ "vmax.s32 q1, q1, q11\n"
+ "subs %[blk], %[blk], #4\n"
+ "bgt 1b\n"
+ "vmax.s32 q0, q0, q1\n"
+ "vsub.s32 q0, q0, q14\n"
+ "vclz.s32 q0, q0\n"
+ "vsub.s32 q0, q15, q0\n"
+ "vst1.32 {d0, d1}, [%[out], :128]\n"
+ :
+ [blk] "+r" (blk),
+ [in] "+r" (in)
+ :
+ [inc] "r" ((char *) &sb_sample_f[1][0][0] -
+ (char *) &sb_sample_f[0][0][0]),
+ [out] "r" (&scale_factor[ch][sb]),
+ [c1] "i" (1 << SCALE_OUT_BITS),
+ [c2] "i" (31 - SCALE_OUT_BITS)
+ : "d0", "d1", "d2", "d3", "d16", "d17", "d18", "d19",
+ "d20", "d21", "d22", "d23", "d24", "d25", "d26",
+ "d27", "d28", "d29", "d30", "d31", "cc", "memory");
+ }
+ }
+}
+
+int sbc_calc_scalefactors_j_neon(
+ int32_t sb_sample_f[16][2][8],
+ uint32_t scale_factor[2][8],
+ int blocks, int subbands)
+{
+ static SBC_ALIGNED int32_t joint_bits_mask[8] = {
+ 8, 4, 2, 1, 128, 64, 32, 16
+ };
+ int joint, i;
+ int32_t *in0, *in1;
+ int32_t *in = &sb_sample_f[0][0][0];
+ uint32_t *out0, *out1;
+ uint32_t *out = &scale_factor[0][0];
+ int32_t *consts = joint_bits_mask;
+
+ i = subbands;
+
+ asm volatile (
+ /*
+ * constants: q13 = (31 - SCALE_OUT_BITS), q14 = 1
+ * input: q0 = ((1 << SCALE_OUT_BITS) + 1)
+ * %[in0] - samples for channel 0
+ * %[in1] - samples for shannel 1
+ * output: q0, q1 - scale factors without joint stereo
+ * q2, q3 - scale factors with joint stereo
+ * q15 - joint stereo selection mask
+ */
+ ".macro calc_scalefactors\n"
+ "vmov.s32 q1, q0\n"
+ "vmov.s32 q2, q0\n"
+ "vmov.s32 q3, q0\n"
+ "mov %[i], %[blocks]\n"
+ "1:\n"
+ "vld1.32 {d18, d19}, [%[in1], :128], %[inc]\n"
+ "vbic.s32 q11, q9, q14\n"
+ "vld1.32 {d16, d17}, [%[in0], :128], %[inc]\n"
+ "vhadd.s32 q10, q8, q11\n"
+ "vhsub.s32 q11, q8, q11\n"
+ "vabs.s32 q8, q8\n"
+ "vabs.s32 q9, q9\n"
+ "vabs.s32 q10, q10\n"
+ "vabs.s32 q11, q11\n"
+ "vmax.s32 q0, q0, q8\n"
+ "vmax.s32 q1, q1, q9\n"
+ "vmax.s32 q2, q2, q10\n"
+ "vmax.s32 q3, q3, q11\n"
+ "subs %[i], %[i], #1\n"
+ "bgt 1b\n"
+ "vsub.s32 q0, q0, q14\n"
+ "vsub.s32 q1, q1, q14\n"
+ "vsub.s32 q2, q2, q14\n"
+ "vsub.s32 q3, q3, q14\n"
+ "vclz.s32 q0, q0\n"
+ "vclz.s32 q1, q1\n"
+ "vclz.s32 q2, q2\n"
+ "vclz.s32 q3, q3\n"
+ "vsub.s32 q0, q13, q0\n"
+ "vsub.s32 q1, q13, q1\n"
+ "vsub.s32 q2, q13, q2\n"
+ "vsub.s32 q3, q13, q3\n"
+ ".endm\n"
+ /*
+ * constants: q14 = 1
+ * input: q15 - joint stereo selection mask
+ * %[in0] - value set by calc_scalefactors macro
+ * %[in1] - value set by calc_scalefactors macro
+ */
+ ".macro update_joint_stereo_samples\n"
+ "sub %[out1], %[in1], %[inc]\n"
+ "sub %[out0], %[in0], %[inc]\n"
+ "sub %[in1], %[in1], %[inc], asl #1\n"
+ "sub %[in0], %[in0], %[inc], asl #1\n"
+ "vld1.32 {d18, d19}, [%[in1], :128]\n"
+ "vbic.s32 q11, q9, q14\n"
+ "vld1.32 {d16, d17}, [%[in0], :128]\n"
+ "vld1.32 {d2, d3}, [%[out1], :128]\n"
+ "vbic.s32 q3, q1, q14\n"
+ "vld1.32 {d0, d1}, [%[out0], :128]\n"
+ "vhsub.s32 q10, q8, q11\n"
+ "vhadd.s32 q11, q8, q11\n"
+ "vhsub.s32 q2, q0, q3\n"
+ "vhadd.s32 q3, q0, q3\n"
+ "vbif.s32 q10, q9, q15\n"
+ "vbif.s32 d22, d16, d30\n"
+ "sub %[inc], %[zero], %[inc], asl #1\n"
+ "sub %[i], %[blocks], #2\n"
+ "2:\n"
+ "vbif.s32 d23, d17, d31\n"
+ "vst1.32 {d20, d21}, [%[in1], :128], %[inc]\n"
+ "vbif.s32 d4, d2, d30\n"
+ "vld1.32 {d18, d19}, [%[in1], :128]\n"
+ "vbif.s32 d5, d3, d31\n"
+ "vst1.32 {d22, d23}, [%[in0], :128], %[inc]\n"
+ "vbif.s32 d6, d0, d30\n"
+ "vld1.32 {d16, d17}, [%[in0], :128]\n"
+ "vbif.s32 d7, d1, d31\n"
+ "vst1.32 {d4, d5}, [%[out1], :128], %[inc]\n"
+ "vbic.s32 q11, q9, q14\n"
+ "vld1.32 {d2, d3}, [%[out1], :128]\n"
+ "vst1.32 {d6, d7}, [%[out0], :128], %[inc]\n"
+ "vbic.s32 q3, q1, q14\n"
+ "vld1.32 {d0, d1}, [%[out0], :128]\n"
+ "vhsub.s32 q10, q8, q11\n"
+ "vhadd.s32 q11, q8, q11\n"
+ "vhsub.s32 q2, q0, q3\n"
+ "vhadd.s32 q3, q0, q3\n"
+ "vbif.s32 q10, q9, q15\n"
+ "vbif.s32 d22, d16, d30\n"
+ "subs %[i], %[i], #2\n"
+ "bgt 2b\n"
+ "sub %[inc], %[zero], %[inc], asr #1\n"
+ "vbif.s32 d23, d17, d31\n"
+ "vst1.32 {d20, d21}, [%[in1], :128]\n"
+ "vbif.s32 q2, q1, q15\n"
+ "vst1.32 {d22, d23}, [%[in0], :128]\n"
+ "vbif.s32 q3, q0, q15\n"
+ "vst1.32 {d4, d5}, [%[out1], :128]\n"
+ "vst1.32 {d6, d7}, [%[out0], :128]\n"
+ ".endm\n"
+
+ "vmov.s32 q14, #1\n"
+ "vmov.s32 q13, %[c2]\n"
+
+ "cmp %[i], #4\n"
+ "bne 8f\n"
+
+ "4:\n" /* 4 subbands */
+ "add %[in0], %[in], #0\n"
+ "add %[in1], %[in], #32\n"
+ "add %[out0], %[out], #0\n"
+ "add %[out1], %[out], #32\n"
+ "vmov.s32 q0, %[c1]\n"
+ "vadd.s32 q0, q0, q14\n"
+
+ "calc_scalefactors\n"
+
+ /* check whether to use joint stereo for subbands 0, 1, 2 */
+ "vadd.s32 q15, q0, q1\n"
+ "vadd.s32 q9, q2, q3\n"
+ "vmov.s32 d31[1], %[zero]\n" /* last subband -> no joint */
+ "vld1.32 {d16, d17}, [%[consts], :128]!\n"
+ "vcgt.s32 q15, q15, q9\n"
+
+ /* calculate and save to memory 'joint' variable */
+ /* update and save scale factors to memory */
+ " vand.s32 q8, q8, q15\n"
+ "vbit.s32 q0, q2, q15\n"
+ " vpadd.s32 d16, d16, d17\n"
+ "vbit.s32 q1, q3, q15\n"
+ " vpadd.s32 d16, d16, d16\n"
+ "vst1.32 {d0, d1}, [%[out0], :128]\n"
+ "vst1.32 {d2, d3}, [%[out1], :128]\n"
+ " vst1.32 {d16[0]}, [%[joint]]\n"
+
+ "update_joint_stereo_samples\n"
+ "b 9f\n"
+
+ "8:\n" /* 8 subbands */
+ "add %[in0], %[in], #16\n\n"
+ "add %[in1], %[in], #48\n"
+ "add %[out0], %[out], #16\n\n"
+ "add %[out1], %[out], #48\n"
+ "vmov.s32 q0, %[c1]\n"
+ "vadd.s32 q0, q0, q14\n"
+
+ "calc_scalefactors\n"
+
+ /* check whether to use joint stereo for subbands 4, 5, 6 */
+ "vadd.s32 q15, q0, q1\n"
+ "vadd.s32 q9, q2, q3\n"
+ "vmov.s32 d31[1], %[zero]\n" /* last subband -> no joint */
+ "vld1.32 {d16, d17}, [%[consts], :128]!\n"
+ "vcgt.s32 q15, q15, q9\n"
+
+ /* calculate part of 'joint' variable and save it to d24 */
+ /* update and save scale factors to memory */
+ " vand.s32 q8, q8, q15\n"
+ "vbit.s32 q0, q2, q15\n"
+ " vpadd.s32 d16, d16, d17\n"
+ "vbit.s32 q1, q3, q15\n"
+ "vst1.32 {d0, d1}, [%[out0], :128]\n"
+ "vst1.32 {d2, d3}, [%[out1], :128]\n"
+ " vpadd.s32 d24, d16, d16\n"
+
+ "update_joint_stereo_samples\n"
+
+ "add %[in0], %[in], #0\n"
+ "add %[in1], %[in], #32\n"
+ "add %[out0], %[out], #0\n\n"
+ "add %[out1], %[out], #32\n"
+ "vmov.s32 q0, %[c1]\n"
+ "vadd.s32 q0, q0, q14\n"
+
+ "calc_scalefactors\n"
+
+ /* check whether to use joint stereo for subbands 0, 1, 2, 3 */
+ "vadd.s32 q15, q0, q1\n"
+ "vadd.s32 q9, q2, q3\n"
+ "vld1.32 {d16, d17}, [%[consts], :128]!\n"
+ "vcgt.s32 q15, q15, q9\n"
+
+ /* combine last part of 'joint' with d24 and save to memory */
+ /* update and save scale factors to memory */
+ " vand.s32 q8, q8, q15\n"
+ "vbit.s32 q0, q2, q15\n"
+ " vpadd.s32 d16, d16, d17\n"
+ "vbit.s32 q1, q3, q15\n"
+ " vpadd.s32 d16, d16, d16\n"
+ "vst1.32 {d0, d1}, [%[out0], :128]\n"
+ " vadd.s32 d16, d16, d24\n"
+ "vst1.32 {d2, d3}, [%[out1], :128]\n"
+ " vst1.32 {d16[0]}, [%[joint]]\n"
+
+ "update_joint_stereo_samples\n"
+ "9:\n"
+ ".purgem calc_scalefactors\n"
+ ".purgem update_joint_stereo_samples\n"
+ :
+ [i] "+&r" (i),
+ [in] "+&r" (in),
+ [in0] "=&r" (in0),
+ [in1] "=&r" (in1),
+ [out] "+&r" (out),
+ [out0] "=&r" (out0),
+ [out1] "=&r" (out1),
+ [consts] "+&r" (consts)
+ :
+ [inc] "r" ((char *) &sb_sample_f[1][0][0] -
+ (char *) &sb_sample_f[0][0][0]),
+ [blocks] "r" (blocks),
+ [joint] "r" (&joint),
+ [c1] "i" (1 << SCALE_OUT_BITS),
+ [c2] "i" (31 - SCALE_OUT_BITS),
+ [zero] "r" (0)
+ : "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
+ "d16", "d17", "d18", "d19", "d20", "d21", "d22",
+ "d23", "d24", "d25", "d26", "d27", "d28", "d29",
+ "d30", "d31", "cc", "memory");
+
+ return joint;
+}
+
+#define PERM_BE(a, b, c, d) { \
+ (a * 2) + 1, (a * 2) + 0, \
+ (b * 2) + 1, (b * 2) + 0, \
+ (c * 2) + 1, (c * 2) + 0, \
+ (d * 2) + 1, (d * 2) + 0 \
+ }
+#define PERM_LE(a, b, c, d) { \
+ (a * 2) + 0, (a * 2) + 1, \
+ (b * 2) + 0, (b * 2) + 1, \
+ (c * 2) + 0, (c * 2) + 1, \
+ (d * 2) + 0, (d * 2) + 1 \
+ }
+
+static SBC_ALWAYS_INLINE int sbc_enc_process_input_4s_neon_internal(
+ int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels, int big_endian)
+{
+ static SBC_ALIGNED uint8_t perm_be[2][8] = {
+ PERM_BE(7, 3, 6, 4),
+ PERM_BE(0, 2, 1, 5)
+ };
+ static SBC_ALIGNED uint8_t perm_le[2][8] = {
+ PERM_LE(7, 3, 6, 4),
+ PERM_LE(0, 2, 1, 5)
+ };
+ /* handle X buffer wraparound */
+ if (position < nsamples) {
+ int16_t *dst = &X[0][SBC_X_BUFFER_SIZE - 40];
+ int16_t *src = &X[0][position];
+ asm volatile (
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0}, [%[src], :64]!\n"
+ "vst1.16 {d0}, [%[dst], :64]!\n"
+ :
+ [dst] "+r" (dst),
+ [src] "+r" (src)
+ : : "memory", "d0", "d1", "d2", "d3");
+ if (nchannels > 1) {
+ dst = &X[1][SBC_X_BUFFER_SIZE - 40];
+ src = &X[1][position];
+ asm volatile (
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0}, [%[src], :64]!\n"
+ "vst1.16 {d0}, [%[dst], :64]!\n"
+ :
+ [dst] "+r" (dst),
+ [src] "+r" (src)
+ : : "memory", "d0", "d1", "d2", "d3");
+ }
+ position = SBC_X_BUFFER_SIZE - 40;
+ }
+
+ if ((nchannels > 1) && ((uintptr_t)pcm & 1)) {
+ /* poor 'pcm' alignment */
+ int16_t *x = &X[0][position];
+ int16_t *y = &X[1][position];
+ asm volatile (
+ "vld1.8 {d0, d1}, [%[perm], :128]\n"
+ "1:\n"
+ "sub %[x], %[x], #16\n"
+ "sub %[y], %[y], #16\n"
+ "sub %[position], %[position], #8\n"
+ "vld1.8 {d4, d5}, [%[pcm]]!\n"
+ "vuzp.16 d4, d5\n"
+ "vld1.8 {d20, d21}, [%[pcm]]!\n"
+ "vuzp.16 d20, d21\n"
+ "vswp d5, d20\n"
+ "vtbl.8 d16, {d4, d5}, d0\n"
+ "vtbl.8 d17, {d4, d5}, d1\n"
+ "vtbl.8 d18, {d20, d21}, d0\n"
+ "vtbl.8 d19, {d20, d21}, d1\n"
+ "vst1.16 {d16, d17}, [%[x], :128]\n"
+ "vst1.16 {d18, d19}, [%[y], :128]\n"
+ "subs %[nsamples], %[nsamples], #8\n"
+ "bgt 1b\n"
+ :
+ [x] "+r" (x),
+ [y] "+r" (y),
+ [pcm] "+r" (pcm),
+ [nsamples] "+r" (nsamples),
+ [position] "+r" (position)
+ :
+ [perm] "r" (big_endian ? perm_be : perm_le)
+ : "cc", "memory", "d0", "d1", "d2", "d3", "d4",
+ "d5", "d6", "d7", "d16", "d17", "d18", "d19",
+ "d20", "d21", "d22", "d23");
+ } else if (nchannels > 1) {
+ /* proper 'pcm' alignment */
+ int16_t *x = &X[0][position];
+ int16_t *y = &X[1][position];
+ asm volatile (
+ "vld1.8 {d0, d1}, [%[perm], :128]\n"
+ "1:\n"
+ "sub %[x], %[x], #16\n"
+ "sub %[y], %[y], #16\n"
+ "sub %[position], %[position], #8\n"
+ "vld2.16 {d4, d5}, [%[pcm]]!\n"
+ "vld2.16 {d20, d21}, [%[pcm]]!\n"
+ "vswp d5, d20\n"
+ "vtbl.8 d16, {d4, d5}, d0\n"
+ "vtbl.8 d17, {d4, d5}, d1\n"
+ "vtbl.8 d18, {d20, d21}, d0\n"
+ "vtbl.8 d19, {d20, d21}, d1\n"
+ "vst1.16 {d16, d17}, [%[x], :128]\n"
+ "vst1.16 {d18, d19}, [%[y], :128]\n"
+ "subs %[nsamples], %[nsamples], #8\n"
+ "bgt 1b\n"
+ :
+ [x] "+r" (x),
+ [y] "+r" (y),
+ [pcm] "+r" (pcm),
+ [nsamples] "+r" (nsamples),
+ [position] "+r" (position)
+ :
+ [perm] "r" (big_endian ? perm_be : perm_le)
+ : "cc", "memory", "d0", "d1", "d2", "d3", "d4",
+ "d5", "d6", "d7", "d16", "d17", "d18", "d19",
+ "d20", "d21", "d22", "d23");
+ } else {
+ int16_t *x = &X[0][position];
+ asm volatile (
+ "vld1.8 {d0, d1}, [%[perm], :128]\n"
+ "1:\n"
+ "sub %[x], %[x], #16\n"
+ "sub %[position], %[position], #8\n"
+ "vld1.8 {d4, d5}, [%[pcm]]!\n"
+ "vtbl.8 d16, {d4, d5}, d0\n"
+ "vtbl.8 d17, {d4, d5}, d1\n"
+ "vst1.16 {d16, d17}, [%[x], :128]\n"
+ "subs %[nsamples], %[nsamples], #8\n"
+ "bgt 1b\n"
+ :
+ [x] "+r" (x),
+ [pcm] "+r" (pcm),
+ [nsamples] "+r" (nsamples),
+ [position] "+r" (position)
+ :
+ [perm] "r" (big_endian ? perm_be : perm_le)
+ : "cc", "memory", "d0", "d1", "d2", "d3", "d4",
+ "d5", "d6", "d7", "d16", "d17", "d18", "d19");
+ }
+ return position;
+}
+
+static SBC_ALWAYS_INLINE int sbc_enc_process_input_8s_neon_internal(
+ int position,
+ const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels, int big_endian)
+{
+ static SBC_ALIGNED uint8_t perm_be[4][8] = {
+ PERM_BE(15, 7, 14, 8),
+ PERM_BE(13, 9, 12, 10),
+ PERM_BE(11, 3, 6, 0),
+ PERM_BE(5, 1, 4, 2)
+ };
+ static SBC_ALIGNED uint8_t perm_le[4][8] = {
+ PERM_LE(15, 7, 14, 8),
+ PERM_LE(13, 9, 12, 10),
+ PERM_LE(11, 3, 6, 0),
+ PERM_LE(5, 1, 4, 2)
+ };
+ /* handle X buffer wraparound */
+ if (position < nsamples) {
+ int16_t *dst = &X[0][SBC_X_BUFFER_SIZE - 72];
+ int16_t *src = &X[0][position];
+ asm volatile (
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1}, [%[dst], :128]!\n"
+ :
+ [dst] "+r" (dst),
+ [src] "+r" (src)
+ : : "memory", "d0", "d1", "d2", "d3");
+ if (nchannels > 1) {
+ dst = &X[1][SBC_X_BUFFER_SIZE - 72];
+ src = &X[1][position];
+ asm volatile (
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1, d2, d3}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1, d2, d3}, [%[dst], :128]!\n"
+ "vld1.16 {d0, d1}, [%[src], :128]!\n"
+ "vst1.16 {d0, d1}, [%[dst], :128]!\n"
+ :
+ [dst] "+r" (dst),
+ [src] "+r" (src)
+ : : "memory", "d0", "d1", "d2", "d3");
+ }
+ position = SBC_X_BUFFER_SIZE - 72;
+ }
+
+ if ((nchannels > 1) && ((uintptr_t)pcm & 1)) {
+ /* poor 'pcm' alignment */
+ int16_t *x = &X[0][position];
+ int16_t *y = &X[1][position];
+ asm volatile (
+ "vld1.8 {d0, d1, d2, d3}, [%[perm], :128]\n"
+ "1:\n"
+ "sub %[x], %[x], #32\n"
+ "sub %[y], %[y], #32\n"
+ "sub %[position], %[position], #16\n"
+ "vld1.8 {d4, d5, d6, d7}, [%[pcm]]!\n"
+ "vuzp.16 q2, q3\n"
+ "vld1.8 {d20, d21, d22, d23}, [%[pcm]]!\n"
+ "vuzp.16 q10, q11\n"
+ "vswp q3, q10\n"
+ "vtbl.8 d16, {d4, d5, d6, d7}, d0\n"
+ "vtbl.8 d17, {d4, d5, d6, d7}, d1\n"
+ "vtbl.8 d18, {d4, d5, d6, d7}, d2\n"
+ "vtbl.8 d19, {d4, d5, d6, d7}, d3\n"
+ "vst1.16 {d16, d17, d18, d19}, [%[x], :128]\n"
+ "vtbl.8 d16, {d20, d21, d22, d23}, d0\n"
+ "vtbl.8 d17, {d20, d21, d22, d23}, d1\n"
+ "vtbl.8 d18, {d20, d21, d22, d23}, d2\n"
+ "vtbl.8 d19, {d20, d21, d22, d23}, d3\n"
+ "vst1.16 {d16, d17, d18, d19}, [%[y], :128]\n"
+ "subs %[nsamples], %[nsamples], #16\n"
+ "bgt 1b\n"
+ :
+ [x] "+r" (x),
+ [y] "+r" (y),
+ [pcm] "+r" (pcm),
+ [nsamples] "+r" (nsamples),
+ [position] "+r" (position)
+ :
+ [perm] "r" (big_endian ? perm_be : perm_le)
+ : "cc", "memory", "d0", "d1", "d2", "d3", "d4",
+ "d5", "d6", "d7", "d16", "d17", "d18", "d19",
+ "d20", "d21", "d22", "d23");
+ } else if (nchannels > 1) {
+ /* proper 'pcm' alignment */
+ int16_t *x = &X[0][position];
+ int16_t *y = &X[1][position];
+ asm volatile (
+ "vld1.8 {d0, d1, d2, d3}, [%[perm], :128]\n"
+ "1:\n"
+ "sub %[x], %[x], #32\n"
+ "sub %[y], %[y], #32\n"
+ "sub %[position], %[position], #16\n"
+ "vld2.16 {d4, d5, d6, d7}, [%[pcm]]!\n"
+ "vld2.16 {d20, d21, d22, d23}, [%[pcm]]!\n"
+ "vswp q3, q10\n"
+ "vtbl.8 d16, {d4, d5, d6, d7}, d0\n"
+ "vtbl.8 d17, {d4, d5, d6, d7}, d1\n"
+ "vtbl.8 d18, {d4, d5, d6, d7}, d2\n"
+ "vtbl.8 d19, {d4, d5, d6, d7}, d3\n"
+ "vst1.16 {d16, d17, d18, d19}, [%[x], :128]\n"
+ "vtbl.8 d16, {d20, d21, d22, d23}, d0\n"
+ "vtbl.8 d17, {d20, d21, d22, d23}, d1\n"
+ "vtbl.8 d18, {d20, d21, d22, d23}, d2\n"
+ "vtbl.8 d19, {d20, d21, d22, d23}, d3\n"
+ "vst1.16 {d16, d17, d18, d19}, [%[y], :128]\n"
+ "subs %[nsamples], %[nsamples], #16\n"
+ "bgt 1b\n"
+ :
+ [x] "+r" (x),
+ [y] "+r" (y),
+ [pcm] "+r" (pcm),
+ [nsamples] "+r" (nsamples),
+ [position] "+r" (position)
+ :
+ [perm] "r" (big_endian ? perm_be : perm_le)
+ : "cc", "memory", "d0", "d1", "d2", "d3", "d4",
+ "d5", "d6", "d7", "d16", "d17", "d18", "d19",
+ "d20", "d21", "d22", "d23");
+ } else {
+ int16_t *x = &X[0][position];
+ asm volatile (
+ "vld1.8 {d0, d1, d2, d3}, [%[perm], :128]\n"
+ "1:\n"
+ "sub %[x], %[x], #32\n"
+ "sub %[position], %[position], #16\n"
+ "vld1.8 {d4, d5, d6, d7}, [%[pcm]]!\n"
+ "vtbl.8 d16, {d4, d5, d6, d7}, d0\n"
+ "vtbl.8 d17, {d4, d5, d6, d7}, d1\n"
+ "vtbl.8 d18, {d4, d5, d6, d7}, d2\n"
+ "vtbl.8 d19, {d4, d5, d6, d7}, d3\n"
+ "vst1.16 {d16, d17, d18, d19}, [%[x], :128]\n"
+ "subs %[nsamples], %[nsamples], #16\n"
+ "bgt 1b\n"
+ :
+ [x] "+r" (x),
+ [pcm] "+r" (pcm),
+ [nsamples] "+r" (nsamples),
+ [position] "+r" (position)
+ :
+ [perm] "r" (big_endian ? perm_be : perm_le)
+ : "cc", "memory", "d0", "d1", "d2", "d3", "d4",
+ "d5", "d6", "d7", "d16", "d17", "d18", "d19");
+ }
+ return position;
+}
+
+#undef PERM_BE
+#undef PERM_LE
+
+static int sbc_enc_process_input_4s_be_neon(int position, const uint8_t *pcm,
+ int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels)
+{
+ return sbc_enc_process_input_4s_neon_internal(
+ position, pcm, X, nsamples, nchannels, 1);
+}
+
+static int sbc_enc_process_input_4s_le_neon(int position, const uint8_t *pcm,
+ int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels)
+{
+ return sbc_enc_process_input_4s_neon_internal(
+ position, pcm, X, nsamples, nchannels, 0);
+}
+
+static int sbc_enc_process_input_8s_be_neon(int position, const uint8_t *pcm,
+ int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels)
+{
+ return sbc_enc_process_input_8s_neon_internal(
+ position, pcm, X, nsamples, nchannels, 1);
+}
+
+static int sbc_enc_process_input_8s_le_neon(int position, const uint8_t *pcm,
+ int16_t X[2][SBC_X_BUFFER_SIZE],
+ int nsamples, int nchannels)
+{
+ return sbc_enc_process_input_8s_neon_internal(
+ position, pcm, X, nsamples, nchannels, 0);
+}
+
+void sbc_init_primitives_neon(struct sbc_encoder_state *state)
+{
+ state->sbc_analyze_4b_4s = sbc_analyze_4b_4s_neon;
+ state->sbc_analyze_4b_8s = sbc_analyze_4b_8s_neon;
+ state->sbc_calc_scalefactors = sbc_calc_scalefactors_neon;
+ state->sbc_calc_scalefactors_j = sbc_calc_scalefactors_j_neon;
+ state->sbc_enc_process_input_4s_le = sbc_enc_process_input_4s_le_neon;
+ state->sbc_enc_process_input_4s_be = sbc_enc_process_input_4s_be_neon;
+ state->sbc_enc_process_input_8s_le = sbc_enc_process_input_8s_le_neon;
+ state->sbc_enc_process_input_8s_be = sbc_enc_process_input_8s_be_neon;
+ state->implementation_info = "NEON";
+}
+
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_primitives_neon.h b/src/modules/bluetooth/sbc/sbc_primitives_neon.h
new file mode 100644
index 00000000..ea3da06a
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_primitives_neon.h
@@ -0,0 +1,41 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef __SBC_PRIMITIVES_NEON_H
+#define __SBC_PRIMITIVES_NEON_H
+
+#include "sbc_primitives.h"
+
+#if defined(__GNUC__) && defined(__ARM_NEON__) && \
+ !defined(SBC_HIGH_PRECISION) && (SCALE_OUT_BITS == 15)
+
+#define SBC_BUILD_WITH_NEON_SUPPORT
+
+void sbc_init_primitives_neon(struct sbc_encoder_state *encoder_state);
+
+#endif
+
+#endif
diff --git a/src/modules/bluetooth/sbc/sbc_tables.h b/src/modules/bluetooth/sbc/sbc_tables.h
new file mode 100644
index 00000000..28c0d54b
--- /dev/null
+++ b/src/modules/bluetooth/sbc/sbc_tables.h
@@ -0,0 +1,660 @@
+/*
+ *
+ * Bluetooth low-complexity, subband codec (SBC) library
+ *
+ * Copyright (C) 2008-2010 Nokia Corporation
+ * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
+ * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+/* A2DP specification: Appendix B, page 69 */
+static const int sbc_offset4[4][4] = {
+ { -1, 0, 0, 0 },
+ { -2, 0, 0, 1 },
+ { -2, 0, 0, 1 },
+ { -2, 0, 0, 1 }
+};
+
+/* A2DP specification: Appendix B, page 69 */
+static const int sbc_offset8[4][8] = {
+ { -2, 0, 0, 0, 0, 0, 0, 1 },
+ { -3, 0, 0, 0, 0, 0, 1, 2 },
+ { -4, 0, 0, 0, 0, 0, 1, 2 },
+ { -4, 0, 0, 0, 0, 0, 1, 2 }
+};
+
+
+#define SS4(val) ASR(val, SCALE_SPROTO4_TBL)
+#define SS8(val) ASR(val, SCALE_SPROTO8_TBL)
+#define SN4(val) ASR(val, SCALE_NPROTO4_TBL)
+#define SN8(val) ASR(val, SCALE_NPROTO8_TBL)
+
+static const int32_t sbc_proto_4_40m0[] = {
+ SS4(0x00000000), SS4(0xffa6982f), SS4(0xfba93848), SS4(0x0456c7b8),
+ SS4(0x005967d1), SS4(0xfffb9ac7), SS4(0xff589157), SS4(0xf9c2a8d8),
+ SS4(0x027c1434), SS4(0x0019118b), SS4(0xfff3c74c), SS4(0xff137330),
+ SS4(0xf81b8d70), SS4(0x00ec1b8b), SS4(0xfff0b71a), SS4(0xffe99b00),
+ SS4(0xfef84470), SS4(0xf6fb4370), SS4(0xffcdc351), SS4(0xffe01dc7)
+};
+
+static const int32_t sbc_proto_4_40m1[] = {
+ SS4(0xffe090ce), SS4(0xff2c0475), SS4(0xf694f800), SS4(0xff2c0475),
+ SS4(0xffe090ce), SS4(0xffe01dc7), SS4(0xffcdc351), SS4(0xf6fb4370),
+ SS4(0xfef84470), SS4(0xffe99b00), SS4(0xfff0b71a), SS4(0x00ec1b8b),
+ SS4(0xf81b8d70), SS4(0xff137330), SS4(0xfff3c74c), SS4(0x0019118b),
+ SS4(0x027c1434), SS4(0xf9c2a8d8), SS4(0xff589157), SS4(0xfffb9ac7)
+};
+
+static const int32_t sbc_proto_8_80m0[] = {
+ SS8(0x00000000), SS8(0xfe8d1970), SS8(0xee979f00), SS8(0x11686100),
+ SS8(0x0172e690), SS8(0xfff5bd1a), SS8(0xfdf1c8d4), SS8(0xeac182c0),
+ SS8(0x0d9daee0), SS8(0x00e530da), SS8(0xffe9811d), SS8(0xfd52986c),
+ SS8(0xe7054ca0), SS8(0x0a00d410), SS8(0x006c1de4), SS8(0xffdba705),
+ SS8(0xfcbc98e8), SS8(0xe3889d20), SS8(0x06af2308), SS8(0x000bb7db),
+ SS8(0xffca00ed), SS8(0xfc3fbb68), SS8(0xe071bc00), SS8(0x03bf7948),
+ SS8(0xffc4e05c), SS8(0xffb54b3b), SS8(0xfbedadc0), SS8(0xdde26200),
+ SS8(0x0142291c), SS8(0xff960e94), SS8(0xff9f3e17), SS8(0xfbd8f358),
+ SS8(0xdbf79400), SS8(0xff405e01), SS8(0xff7d4914), SS8(0xff8b1a31),
+ SS8(0xfc1417b8), SS8(0xdac7bb40), SS8(0xfdbb828c), SS8(0xff762170)
+};
+
+static const int32_t sbc_proto_8_80m1[] = {
+ SS8(0xff7c272c), SS8(0xfcb02620), SS8(0xda612700), SS8(0xfcb02620),
+ SS8(0xff7c272c), SS8(0xff762170), SS8(0xfdbb828c), SS8(0xdac7bb40),
+ SS8(0xfc1417b8), SS8(0xff8b1a31), SS8(0xff7d4914), SS8(0xff405e01),
+ SS8(0xdbf79400), SS8(0xfbd8f358), SS8(0xff9f3e17), SS8(0xff960e94),
+ SS8(0x0142291c), SS8(0xdde26200), SS8(0xfbedadc0), SS8(0xffb54b3b),
+ SS8(0xffc4e05c), SS8(0x03bf7948), SS8(0xe071bc00), SS8(0xfc3fbb68),
+ SS8(0xffca00ed), SS8(0x000bb7db), SS8(0x06af2308), SS8(0xe3889d20),
+ SS8(0xfcbc98e8), SS8(0xffdba705), SS8(0x006c1de4), SS8(0x0a00d410),
+ SS8(0xe7054ca0), SS8(0xfd52986c), SS8(0xffe9811d), SS8(0x00e530da),
+ SS8(0x0d9daee0), SS8(0xeac182c0), SS8(0xfdf1c8d4), SS8(0xfff5bd1a)
+};
+
+static const int32_t synmatrix4[8][4] = {
+ { SN4(0x05a82798), SN4(0xfa57d868), SN4(0xfa57d868), SN4(0x05a82798) },
+ { SN4(0x030fbc54), SN4(0xf89be510), SN4(0x07641af0), SN4(0xfcf043ac) },
+ { SN4(0x00000000), SN4(0x00000000), SN4(0x00000000), SN4(0x00000000) },
+ { SN4(0xfcf043ac), SN4(0x07641af0), SN4(0xf89be510), SN4(0x030fbc54) },
+ { SN4(0xfa57d868), SN4(0x05a82798), SN4(0x05a82798), SN4(0xfa57d868) },
+ { SN4(0xf89be510), SN4(0xfcf043ac), SN4(0x030fbc54), SN4(0x07641af0) },
+ { SN4(0xf8000000), SN4(0xf8000000), SN4(0xf8000000), SN4(0xf8000000) },
+ { SN4(0xf89be510), SN4(0xfcf043ac), SN4(0x030fbc54), SN4(0x07641af0) }
+};
+
+static const int32_t synmatrix8[16][8] = {
+ { SN8(0x05a82798), SN8(0xfa57d868), SN8(0xfa57d868), SN8(0x05a82798),
+ SN8(0x05a82798), SN8(0xfa57d868), SN8(0xfa57d868), SN8(0x05a82798) },
+ { SN8(0x0471ced0), SN8(0xf8275a10), SN8(0x018f8b84), SN8(0x06a6d988),
+ SN8(0xf9592678), SN8(0xfe70747c), SN8(0x07d8a5f0), SN8(0xfb8e3130) },
+ { SN8(0x030fbc54), SN8(0xf89be510), SN8(0x07641af0), SN8(0xfcf043ac),
+ SN8(0xfcf043ac), SN8(0x07641af0), SN8(0xf89be510), SN8(0x030fbc54) },
+ { SN8(0x018f8b84), SN8(0xfb8e3130), SN8(0x06a6d988), SN8(0xf8275a10),
+ SN8(0x07d8a5f0), SN8(0xf9592678), SN8(0x0471ced0), SN8(0xfe70747c) },
+ { SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), SN8(0x00000000),
+ SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), SN8(0x00000000) },
+ { SN8(0xfe70747c), SN8(0x0471ced0), SN8(0xf9592678), SN8(0x07d8a5f0),
+ SN8(0xf8275a10), SN8(0x06a6d988), SN8(0xfb8e3130), SN8(0x018f8b84) },
+ { SN8(0xfcf043ac), SN8(0x07641af0), SN8(0xf89be510), SN8(0x030fbc54),
+ SN8(0x030fbc54), SN8(0xf89be510), SN8(0x07641af0), SN8(0xfcf043ac) },
+ { SN8(0xfb8e3130), SN8(0x07d8a5f0), SN8(0xfe70747c), SN8(0xf9592678),
+ SN8(0x06a6d988), SN8(0x018f8b84), SN8(0xf8275a10), SN8(0x0471ced0) },
+ { SN8(0xfa57d868), SN8(0x05a82798), SN8(0x05a82798), SN8(0xfa57d868),
+ SN8(0xfa57d868), SN8(0x05a82798), SN8(0x05a82798), SN8(0xfa57d868) },
+ { SN8(0xf9592678), SN8(0x018f8b84), SN8(0x07d8a5f0), SN8(0x0471ced0),
+ SN8(0xfb8e3130), SN8(0xf8275a10), SN8(0xfe70747c), SN8(0x06a6d988) },
+ { SN8(0xf89be510), SN8(0xfcf043ac), SN8(0x030fbc54), SN8(0x07641af0),
+ SN8(0x07641af0), SN8(0x030fbc54), SN8(0xfcf043ac), SN8(0xf89be510) },
+ { SN8(0xf8275a10), SN8(0xf9592678), SN8(0xfb8e3130), SN8(0xfe70747c),
+ SN8(0x018f8b84), SN8(0x0471ced0), SN8(0x06a6d988), SN8(0x07d8a5f0) },
+ { SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000),
+ SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000) },
+ { SN8(0xf8275a10), SN8(0xf9592678), SN8(0xfb8e3130), SN8(0xfe70747c),
+ SN8(0x018f8b84), SN8(0x0471ced0), SN8(0x06a6d988), SN8(0x07d8a5f0) },
+ { SN8(0xf89be510), SN8(0xfcf043ac), SN8(0x030fbc54), SN8(0x07641af0),
+ SN8(0x07641af0), SN8(0x030fbc54), SN8(0xfcf043ac), SN8(0xf89be510) },
+ { SN8(0xf9592678), SN8(0x018f8b84), SN8(0x07d8a5f0), SN8(0x0471ced0),
+ SN8(0xfb8e3130), SN8(0xf8275a10), SN8(0xfe70747c), SN8(0x06a6d988) }
+};
+
+/* Uncomment the following line to enable high precision build of SBC encoder */
+
+/* #define SBC_HIGH_PRECISION */
+
+#ifdef SBC_HIGH_PRECISION
+#define FIXED_A int64_t /* data type for fixed point accumulator */
+#define FIXED_T int32_t /* data type for fixed point constants */
+#define SBC_FIXED_EXTRA_BITS 16
+#else
+#define FIXED_A int32_t /* data type for fixed point accumulator */
+#define FIXED_T int16_t /* data type for fixed point constants */
+#define SBC_FIXED_EXTRA_BITS 0
+#endif
+
+/* A2DP specification: Section 12.8 Tables
+ *
+ * Original values are premultiplied by 2 for better precision (that is the
+ * maximum which is possible without overflows)
+ *
+ * Note: in each block of 8 numbers sign was changed for elements 2 and 7
+ * in order to compensate the same change applied to cos_table_fixed_4
+ */
+#define SBC_PROTO_FIXED4_SCALE \
+ ((sizeof(FIXED_T) * CHAR_BIT - 1) - SBC_FIXED_EXTRA_BITS + 1)
+#define F_PROTO4(x) (FIXED_A) ((x * 2) * \
+ ((FIXED_A) 1 << (sizeof(FIXED_T) * CHAR_BIT - 1)) + 0.5)
+#define F(x) F_PROTO4(x)
+static const FIXED_T _sbc_proto_fixed4[40] = {
+ F(0.00000000E+00), F(5.36548976E-04),
+ -F(1.49188357E-03), F(2.73370904E-03),
+ F(3.83720193E-03), F(3.89205149E-03),
+ F(1.86581691E-03), F(3.06012286E-03),
+
+ F(1.09137620E-02), F(2.04385087E-02),
+ -F(2.88757392E-02), F(3.21939290E-02),
+ F(2.58767811E-02), F(6.13245186E-03),
+ -F(2.88217274E-02), F(7.76463494E-02),
+
+ F(1.35593274E-01), F(1.94987841E-01),
+ -F(2.46636662E-01), F(2.81828203E-01),
+ F(2.94315332E-01), F(2.81828203E-01),
+ F(2.46636662E-01), -F(1.94987841E-01),
+
+ -F(1.35593274E-01), -F(7.76463494E-02),
+ F(2.88217274E-02), F(6.13245186E-03),
+ F(2.58767811E-02), F(3.21939290E-02),
+ F(2.88757392E-02), -F(2.04385087E-02),
+
+ -F(1.09137620E-02), -F(3.06012286E-03),
+ -F(1.86581691E-03), F(3.89205149E-03),
+ F(3.83720193E-03), F(2.73370904E-03),
+ F(1.49188357E-03), -F(5.36548976E-04),
+};
+#undef F
+
+/*
+ * To produce this cosine matrix in Octave:
+ *
+ * b = zeros(4, 8);
+ * for i = 0:3
+ * for j = 0:7 b(i+1, j+1) = cos((i + 0.5) * (j - 2) * (pi/4))
+ * endfor
+ * endfor;
+ * printf("%.10f, ", b');
+ *
+ * Note: in each block of 8 numbers sign was changed for elements 2 and 7
+ *
+ * Change of sign for element 2 allows to replace constant 1.0 (not
+ * representable in Q15 format) with -1.0 (fine with Q15).
+ * Changed sign for element 7 allows to have more similar constants
+ * and simplify subband filter function code.
+ */
+#define SBC_COS_TABLE_FIXED4_SCALE \
+ ((sizeof(FIXED_T) * CHAR_BIT - 1) + SBC_FIXED_EXTRA_BITS)
+#define F_COS4(x) (FIXED_A) ((x) * \
+ ((FIXED_A) 1 << (sizeof(FIXED_T) * CHAR_BIT - 1)) + 0.5)
+#define F(x) F_COS4(x)
+static const FIXED_T cos_table_fixed_4[32] = {
+ F(0.7071067812), F(0.9238795325), -F(1.0000000000), F(0.9238795325),
+ F(0.7071067812), F(0.3826834324), F(0.0000000000), F(0.3826834324),
+
+ -F(0.7071067812), F(0.3826834324), -F(1.0000000000), F(0.3826834324),
+ -F(0.7071067812), -F(0.9238795325), -F(0.0000000000), -F(0.9238795325),
+
+ -F(0.7071067812), -F(0.3826834324), -F(1.0000000000), -F(0.3826834324),
+ -F(0.7071067812), F(0.9238795325), F(0.0000000000), F(0.9238795325),
+
+ F(0.7071067812), -F(0.9238795325), -F(1.0000000000), -F(0.9238795325),
+ F(0.7071067812), -F(0.3826834324), -F(0.0000000000), -F(0.3826834324),
+};
+#undef F
+
+/* A2DP specification: Section 12.8 Tables
+ *
+ * Original values are premultiplied by 4 for better precision (that is the
+ * maximum which is possible without overflows)
+ *
+ * Note: in each block of 16 numbers sign was changed for elements 4, 13, 14, 15
+ * in order to compensate the same change applied to cos_table_fixed_8
+ */
+#define SBC_PROTO_FIXED8_SCALE \
+ ((sizeof(FIXED_T) * CHAR_BIT - 1) - SBC_FIXED_EXTRA_BITS + 1)
+#define F_PROTO8(x) (FIXED_A) ((x * 2) * \
+ ((FIXED_A) 1 << (sizeof(FIXED_T) * CHAR_BIT - 1)) + 0.5)
+#define F(x) F_PROTO8(x)
+static const FIXED_T _sbc_proto_fixed8[80] = {
+ F(0.00000000E+00), F(1.56575398E-04),
+ F(3.43256425E-04), F(5.54620202E-04),
+ -F(8.23919506E-04), F(1.13992507E-03),
+ F(1.47640169E-03), F(1.78371725E-03),
+ F(2.01182542E-03), F(2.10371989E-03),
+ F(1.99454554E-03), F(1.61656283E-03),
+ F(9.02154502E-04), F(1.78805361E-04),
+ F(1.64973098E-03), F(3.49717454E-03),
+
+ F(5.65949473E-03), F(8.02941163E-03),
+ F(1.04584443E-02), F(1.27472335E-02),
+ -F(1.46525263E-02), F(1.59045603E-02),
+ F(1.62208471E-02), F(1.53184106E-02),
+ F(1.29371806E-02), F(8.85757540E-03),
+ F(2.92408442E-03), -F(4.91578024E-03),
+ -F(1.46404076E-02), F(2.61098752E-02),
+ F(3.90751381E-02), F(5.31873032E-02),
+
+ F(6.79989431E-02), F(8.29847578E-02),
+ F(9.75753918E-02), F(1.11196689E-01),
+ -F(1.23264548E-01), F(1.33264415E-01),
+ F(1.40753505E-01), F(1.45389847E-01),
+ F(1.46955068E-01), F(1.45389847E-01),
+ F(1.40753505E-01), F(1.33264415E-01),
+ F(1.23264548E-01), -F(1.11196689E-01),
+ -F(9.75753918E-02), -F(8.29847578E-02),
+
+ -F(6.79989431E-02), -F(5.31873032E-02),
+ -F(3.90751381E-02), -F(2.61098752E-02),
+ F(1.46404076E-02), -F(4.91578024E-03),
+ F(2.92408442E-03), F(8.85757540E-03),
+ F(1.29371806E-02), F(1.53184106E-02),
+ F(1.62208471E-02), F(1.59045603E-02),
+ F(1.46525263E-02), -F(1.27472335E-02),
+ -F(1.04584443E-02), -F(8.02941163E-03),
+
+ -F(5.65949473E-03), -F(3.49717454E-03),
+ -F(1.64973098E-03), -F(1.78805361E-04),
+ -F(9.02154502E-04), F(1.61656283E-03),
+ F(1.99454554E-03), F(2.10371989E-03),
+ F(2.01182542E-03), F(1.78371725E-03),
+ F(1.47640169E-03), F(1.13992507E-03),
+ F(8.23919506E-04), -F(5.54620202E-04),
+ -F(3.43256425E-04), -F(1.56575398E-04),
+};
+#undef F
+
+/*
+ * To produce this cosine matrix in Octave:
+ *
+ * b = zeros(8, 16);
+ * for i = 0:7
+ * for j = 0:15 b(i+1, j+1) = cos((i + 0.5) * (j - 4) * (pi/8))
+ * endfor endfor;
+ * printf("%.10f, ", b');
+ *
+ * Note: in each block of 16 numbers sign was changed for elements 4, 13, 14, 15
+ *
+ * Change of sign for element 4 allows to replace constant 1.0 (not
+ * representable in Q15 format) with -1.0 (fine with Q15).
+ * Changed signs for elements 13, 14, 15 allow to have more similar constants
+ * and simplify subband filter function code.
+ */
+#define SBC_COS_TABLE_FIXED8_SCALE \
+ ((sizeof(FIXED_T) * CHAR_BIT - 1) + SBC_FIXED_EXTRA_BITS)
+#define F_COS8(x) (FIXED_A) ((x) * \
+ ((FIXED_A) 1 << (sizeof(FIXED_T) * CHAR_BIT - 1)) + 0.5)
+#define F(x) F_COS8(x)
+static const FIXED_T cos_table_fixed_8[128] = {
+ F(0.7071067812), F(0.8314696123), F(0.9238795325), F(0.9807852804),
+ -F(1.0000000000), F(0.9807852804), F(0.9238795325), F(0.8314696123),
+ F(0.7071067812), F(0.5555702330), F(0.3826834324), F(0.1950903220),
+ F(0.0000000000), F(0.1950903220), F(0.3826834324), F(0.5555702330),
+
+ -F(0.7071067812), -F(0.1950903220), F(0.3826834324), F(0.8314696123),
+ -F(1.0000000000), F(0.8314696123), F(0.3826834324), -F(0.1950903220),
+ -F(0.7071067812), -F(0.9807852804), -F(0.9238795325), -F(0.5555702330),
+ -F(0.0000000000), -F(0.5555702330), -F(0.9238795325), -F(0.9807852804),
+
+ -F(0.7071067812), -F(0.9807852804), -F(0.3826834324), F(0.5555702330),
+ -F(1.0000000000), F(0.5555702330), -F(0.3826834324), -F(0.9807852804),
+ -F(0.7071067812), F(0.1950903220), F(0.9238795325), F(0.8314696123),
+ F(0.0000000000), F(0.8314696123), F(0.9238795325), F(0.1950903220),
+
+ F(0.7071067812), -F(0.5555702330), -F(0.9238795325), F(0.1950903220),
+ -F(1.0000000000), F(0.1950903220), -F(0.9238795325), -F(0.5555702330),
+ F(0.7071067812), F(0.8314696123), -F(0.3826834324), -F(0.9807852804),
+ -F(0.0000000000), -F(0.9807852804), -F(0.3826834324), F(0.8314696123),
+
+ F(0.7071067812), F(0.5555702330), -F(0.9238795325), -F(0.1950903220),
+ -F(1.0000000000), -F(0.1950903220), -F(0.9238795325), F(0.5555702330),
+ F(0.7071067812), -F(0.8314696123), -F(0.3826834324), F(0.9807852804),
+ F(0.0000000000), F(0.9807852804), -F(0.3826834324), -F(0.8314696123),
+
+ -F(0.7071067812), F(0.9807852804), -F(0.3826834324), -F(0.5555702330),
+ -F(1.0000000000), -F(0.5555702330), -F(0.3826834324), F(0.9807852804),
+ -F(0.7071067812), -F(0.1950903220), F(0.9238795325), -F(0.8314696123),
+ -F(0.0000000000), -F(0.8314696123), F(0.9238795325), -F(0.1950903220),
+
+ -F(0.7071067812), F(0.1950903220), F(0.3826834324), -F(0.8314696123),
+ -F(1.0000000000), -F(0.8314696123), F(0.3826834324), F(0.1950903220),
+ -F(0.7071067812), F(0.9807852804), -F(0.9238795325), F(0.5555702330),
+ -F(0.0000000000), F(0.5555702330), -F(0.9238795325), F(0.9807852804),
+
+ F(0.7071067812), -F(0.8314696123), F(0.9238795325), -F(0.9807852804),
+ -F(1.0000000000), -F(0.9807852804), F(0.9238795325), -F(0.8314696123),
+ F(0.7071067812), -F(0.5555702330), F(0.3826834324), -F(0.1950903220),
+ -F(0.0000000000), -F(0.1950903220), F(0.3826834324), -F(0.5555702330),
+};
+#undef F
+
+/*
+ * Enforce 16 byte alignment for the data, which is supposed to be used
+ * with SIMD optimized code.
+ */
+
+#define SBC_ALIGN_BITS 4
+#define SBC_ALIGN_MASK ((1 << (SBC_ALIGN_BITS)) - 1)
+
+#ifdef __GNUC__
+#define SBC_ALIGNED __attribute__((aligned(1 << (SBC_ALIGN_BITS))))
+#else
+#define SBC_ALIGNED
+#endif
+
+/*
+ * Constant tables for the use in SIMD optimized analysis filters
+ * Each table consists of two parts:
+ * 1. reordered "proto" table
+ * 2. reordered "cos" table
+ *
+ * Due to non-symmetrical reordering, separate tables for "even"
+ * and "odd" cases are needed
+ */
+
+static const FIXED_T SBC_ALIGNED analysis_consts_fixed4_simd_even[40 + 16] = {
+#define C0 1.0932568993
+#define C1 1.3056875580
+#define C2 1.3056875580
+#define C3 1.6772280856
+
+#define F(x) F_PROTO4(x)
+ F(0.00000000E+00 * C0), F(3.83720193E-03 * C0),
+ F(5.36548976E-04 * C1), F(2.73370904E-03 * C1),
+ F(3.06012286E-03 * C2), F(3.89205149E-03 * C2),
+ F(0.00000000E+00 * C3), -F(1.49188357E-03 * C3),
+ F(1.09137620E-02 * C0), F(2.58767811E-02 * C0),
+ F(2.04385087E-02 * C1), F(3.21939290E-02 * C1),
+ F(7.76463494E-02 * C2), F(6.13245186E-03 * C2),
+ F(0.00000000E+00 * C3), -F(2.88757392E-02 * C3),
+ F(1.35593274E-01 * C0), F(2.94315332E-01 * C0),
+ F(1.94987841E-01 * C1), F(2.81828203E-01 * C1),
+ -F(1.94987841E-01 * C2), F(2.81828203E-01 * C2),
+ F(0.00000000E+00 * C3), -F(2.46636662E-01 * C3),
+ -F(1.35593274E-01 * C0), F(2.58767811E-02 * C0),
+ -F(7.76463494E-02 * C1), F(6.13245186E-03 * C1),
+ -F(2.04385087E-02 * C2), F(3.21939290E-02 * C2),
+ F(0.00000000E+00 * C3), F(2.88217274E-02 * C3),
+ -F(1.09137620E-02 * C0), F(3.83720193E-03 * C0),
+ -F(3.06012286E-03 * C1), F(3.89205149E-03 * C1),
+ -F(5.36548976E-04 * C2), F(2.73370904E-03 * C2),
+ F(0.00000000E+00 * C3), -F(1.86581691E-03 * C3),
+#undef F
+#define F(x) F_COS4(x)
+ F(0.7071067812 / C0), F(0.9238795325 / C1),
+ -F(0.7071067812 / C0), F(0.3826834324 / C1),
+ -F(0.7071067812 / C0), -F(0.3826834324 / C1),
+ F(0.7071067812 / C0), -F(0.9238795325 / C1),
+ F(0.3826834324 / C2), -F(1.0000000000 / C3),
+ -F(0.9238795325 / C2), -F(1.0000000000 / C3),
+ F(0.9238795325 / C2), -F(1.0000000000 / C3),
+ -F(0.3826834324 / C2), -F(1.0000000000 / C3),
+#undef F
+
+#undef C0
+#undef C1
+#undef C2
+#undef C3
+};
+
+static const FIXED_T SBC_ALIGNED analysis_consts_fixed4_simd_odd[40 + 16] = {
+#define C0 1.3056875580
+#define C1 1.6772280856
+#define C2 1.0932568993
+#define C3 1.3056875580
+
+#define F(x) F_PROTO4(x)
+ F(2.73370904E-03 * C0), F(5.36548976E-04 * C0),
+ -F(1.49188357E-03 * C1), F(0.00000000E+00 * C1),
+ F(3.83720193E-03 * C2), F(1.09137620E-02 * C2),
+ F(3.89205149E-03 * C3), F(3.06012286E-03 * C3),
+ F(3.21939290E-02 * C0), F(2.04385087E-02 * C0),
+ -F(2.88757392E-02 * C1), F(0.00000000E+00 * C1),
+ F(2.58767811E-02 * C2), F(1.35593274E-01 * C2),
+ F(6.13245186E-03 * C3), F(7.76463494E-02 * C3),
+ F(2.81828203E-01 * C0), F(1.94987841E-01 * C0),
+ -F(2.46636662E-01 * C1), F(0.00000000E+00 * C1),
+ F(2.94315332E-01 * C2), -F(1.35593274E-01 * C2),
+ F(2.81828203E-01 * C3), -F(1.94987841E-01 * C3),
+ F(6.13245186E-03 * C0), -F(7.76463494E-02 * C0),
+ F(2.88217274E-02 * C1), F(0.00000000E+00 * C1),
+ F(2.58767811E-02 * C2), -F(1.09137620E-02 * C2),
+ F(3.21939290E-02 * C3), -F(2.04385087E-02 * C3),
+ F(3.89205149E-03 * C0), -F(3.06012286E-03 * C0),
+ -F(1.86581691E-03 * C1), F(0.00000000E+00 * C1),
+ F(3.83720193E-03 * C2), F(0.00000000E+00 * C2),
+ F(2.73370904E-03 * C3), -F(5.36548976E-04 * C3),
+#undef F
+#define F(x) F_COS4(x)
+ F(0.9238795325 / C0), -F(1.0000000000 / C1),
+ F(0.3826834324 / C0), -F(1.0000000000 / C1),
+ -F(0.3826834324 / C0), -F(1.0000000000 / C1),
+ -F(0.9238795325 / C0), -F(1.0000000000 / C1),
+ F(0.7071067812 / C2), F(0.3826834324 / C3),
+ -F(0.7071067812 / C2), -F(0.9238795325 / C3),
+ -F(0.7071067812 / C2), F(0.9238795325 / C3),
+ F(0.7071067812 / C2), -F(0.3826834324 / C3),
+#undef F
+
+#undef C0
+#undef C1
+#undef C2
+#undef C3
+};
+
+static const FIXED_T SBC_ALIGNED analysis_consts_fixed8_simd_even[80 + 64] = {
+#define C0 2.7906148894
+#define C1 2.4270044280
+#define C2 2.8015616024
+#define C3 3.1710363741
+#define C4 2.5377944043
+#define C5 2.4270044280
+#define C6 2.8015616024
+#define C7 3.1710363741
+
+#define F(x) F_PROTO8(x)
+ F(0.00000000E+00 * C0), F(2.01182542E-03 * C0),
+ F(1.56575398E-04 * C1), F(1.78371725E-03 * C1),
+ F(3.43256425E-04 * C2), F(1.47640169E-03 * C2),
+ F(5.54620202E-04 * C3), F(1.13992507E-03 * C3),
+ -F(8.23919506E-04 * C4), F(0.00000000E+00 * C4),
+ F(2.10371989E-03 * C5), F(3.49717454E-03 * C5),
+ F(1.99454554E-03 * C6), F(1.64973098E-03 * C6),
+ F(1.61656283E-03 * C7), F(1.78805361E-04 * C7),
+ F(5.65949473E-03 * C0), F(1.29371806E-02 * C0),
+ F(8.02941163E-03 * C1), F(1.53184106E-02 * C1),
+ F(1.04584443E-02 * C2), F(1.62208471E-02 * C2),
+ F(1.27472335E-02 * C3), F(1.59045603E-02 * C3),
+ -F(1.46525263E-02 * C4), F(0.00000000E+00 * C4),
+ F(8.85757540E-03 * C5), F(5.31873032E-02 * C5),
+ F(2.92408442E-03 * C6), F(3.90751381E-02 * C6),
+ -F(4.91578024E-03 * C7), F(2.61098752E-02 * C7),
+ F(6.79989431E-02 * C0), F(1.46955068E-01 * C0),
+ F(8.29847578E-02 * C1), F(1.45389847E-01 * C1),
+ F(9.75753918E-02 * C2), F(1.40753505E-01 * C2),
+ F(1.11196689E-01 * C3), F(1.33264415E-01 * C3),
+ -F(1.23264548E-01 * C4), F(0.00000000E+00 * C4),
+ F(1.45389847E-01 * C5), -F(8.29847578E-02 * C5),
+ F(1.40753505E-01 * C6), -F(9.75753918E-02 * C6),
+ F(1.33264415E-01 * C7), -F(1.11196689E-01 * C7),
+ -F(6.79989431E-02 * C0), F(1.29371806E-02 * C0),
+ -F(5.31873032E-02 * C1), F(8.85757540E-03 * C1),
+ -F(3.90751381E-02 * C2), F(2.92408442E-03 * C2),
+ -F(2.61098752E-02 * C3), -F(4.91578024E-03 * C3),
+ F(1.46404076E-02 * C4), F(0.00000000E+00 * C4),
+ F(1.53184106E-02 * C5), -F(8.02941163E-03 * C5),
+ F(1.62208471E-02 * C6), -F(1.04584443E-02 * C6),
+ F(1.59045603E-02 * C7), -F(1.27472335E-02 * C7),
+ -F(5.65949473E-03 * C0), F(2.01182542E-03 * C0),
+ -F(3.49717454E-03 * C1), F(2.10371989E-03 * C1),
+ -F(1.64973098E-03 * C2), F(1.99454554E-03 * C2),
+ -F(1.78805361E-04 * C3), F(1.61656283E-03 * C3),
+ -F(9.02154502E-04 * C4), F(0.00000000E+00 * C4),
+ F(1.78371725E-03 * C5), -F(1.56575398E-04 * C5),
+ F(1.47640169E-03 * C6), -F(3.43256425E-04 * C6),
+ F(1.13992507E-03 * C7), -F(5.54620202E-04 * C7),
+#undef F
+#define F(x) F_COS8(x)
+ F(0.7071067812 / C0), F(0.8314696123 / C1),
+ -F(0.7071067812 / C0), -F(0.1950903220 / C1),
+ -F(0.7071067812 / C0), -F(0.9807852804 / C1),
+ F(0.7071067812 / C0), -F(0.5555702330 / C1),
+ F(0.7071067812 / C0), F(0.5555702330 / C1),
+ -F(0.7071067812 / C0), F(0.9807852804 / C1),
+ -F(0.7071067812 / C0), F(0.1950903220 / C1),
+ F(0.7071067812 / C0), -F(0.8314696123 / C1),
+ F(0.9238795325 / C2), F(0.9807852804 / C3),
+ F(0.3826834324 / C2), F(0.8314696123 / C3),
+ -F(0.3826834324 / C2), F(0.5555702330 / C3),
+ -F(0.9238795325 / C2), F(0.1950903220 / C3),
+ -F(0.9238795325 / C2), -F(0.1950903220 / C3),
+ -F(0.3826834324 / C2), -F(0.5555702330 / C3),
+ F(0.3826834324 / C2), -F(0.8314696123 / C3),
+ F(0.9238795325 / C2), -F(0.9807852804 / C3),
+ -F(1.0000000000 / C4), F(0.5555702330 / C5),
+ -F(1.0000000000 / C4), -F(0.9807852804 / C5),
+ -F(1.0000000000 / C4), F(0.1950903220 / C5),
+ -F(1.0000000000 / C4), F(0.8314696123 / C5),
+ -F(1.0000000000 / C4), -F(0.8314696123 / C5),
+ -F(1.0000000000 / C4), -F(0.1950903220 / C5),
+ -F(1.0000000000 / C4), F(0.9807852804 / C5),
+ -F(1.0000000000 / C4), -F(0.5555702330 / C5),
+ F(0.3826834324 / C6), F(0.1950903220 / C7),
+ -F(0.9238795325 / C6), -F(0.5555702330 / C7),
+ F(0.9238795325 / C6), F(0.8314696123 / C7),
+ -F(0.3826834324 / C6), -F(0.9807852804 / C7),
+ -F(0.3826834324 / C6), F(0.9807852804 / C7),
+ F(0.9238795325 / C6), -F(0.8314696123 / C7),
+ -F(0.9238795325 / C6), F(0.5555702330 / C7),
+ F(0.3826834324 / C6), -F(0.1950903220 / C7),
+#undef F
+
+#undef C0
+#undef C1
+#undef C2
+#undef C3
+#undef C4
+#undef C5
+#undef C6
+#undef C7
+};
+
+static const FIXED_T SBC_ALIGNED analysis_consts_fixed8_simd_odd[80 + 64] = {
+#define C0 2.5377944043
+#define C1 2.4270044280
+#define C2 2.8015616024
+#define C3 3.1710363741
+#define C4 2.7906148894
+#define C5 2.4270044280
+#define C6 2.8015616024
+#define C7 3.1710363741
+
+#define F(x) F_PROTO8(x)
+ F(0.00000000E+00 * C0), -F(8.23919506E-04 * C0),
+ F(1.56575398E-04 * C1), F(1.78371725E-03 * C1),
+ F(3.43256425E-04 * C2), F(1.47640169E-03 * C2),
+ F(5.54620202E-04 * C3), F(1.13992507E-03 * C3),
+ F(2.01182542E-03 * C4), F(5.65949473E-03 * C4),
+ F(2.10371989E-03 * C5), F(3.49717454E-03 * C5),
+ F(1.99454554E-03 * C6), F(1.64973098E-03 * C6),
+ F(1.61656283E-03 * C7), F(1.78805361E-04 * C7),
+ F(0.00000000E+00 * C0), -F(1.46525263E-02 * C0),
+ F(8.02941163E-03 * C1), F(1.53184106E-02 * C1),
+ F(1.04584443E-02 * C2), F(1.62208471E-02 * C2),
+ F(1.27472335E-02 * C3), F(1.59045603E-02 * C3),
+ F(1.29371806E-02 * C4), F(6.79989431E-02 * C4),
+ F(8.85757540E-03 * C5), F(5.31873032E-02 * C5),
+ F(2.92408442E-03 * C6), F(3.90751381E-02 * C6),
+ -F(4.91578024E-03 * C7), F(2.61098752E-02 * C7),
+ F(0.00000000E+00 * C0), -F(1.23264548E-01 * C0),
+ F(8.29847578E-02 * C1), F(1.45389847E-01 * C1),
+ F(9.75753918E-02 * C2), F(1.40753505E-01 * C2),
+ F(1.11196689E-01 * C3), F(1.33264415E-01 * C3),
+ F(1.46955068E-01 * C4), -F(6.79989431E-02 * C4),
+ F(1.45389847E-01 * C5), -F(8.29847578E-02 * C5),
+ F(1.40753505E-01 * C6), -F(9.75753918E-02 * C6),
+ F(1.33264415E-01 * C7), -F(1.11196689E-01 * C7),
+ F(0.00000000E+00 * C0), F(1.46404076E-02 * C0),
+ -F(5.31873032E-02 * C1), F(8.85757540E-03 * C1),
+ -F(3.90751381E-02 * C2), F(2.92408442E-03 * C2),
+ -F(2.61098752E-02 * C3), -F(4.91578024E-03 * C3),
+ F(1.29371806E-02 * C4), -F(5.65949473E-03 * C4),
+ F(1.53184106E-02 * C5), -F(8.02941163E-03 * C5),
+ F(1.62208471E-02 * C6), -F(1.04584443E-02 * C6),
+ F(1.59045603E-02 * C7), -F(1.27472335E-02 * C7),
+ F(0.00000000E+00 * C0), -F(9.02154502E-04 * C0),
+ -F(3.49717454E-03 * C1), F(2.10371989E-03 * C1),
+ -F(1.64973098E-03 * C2), F(1.99454554E-03 * C2),
+ -F(1.78805361E-04 * C3), F(1.61656283E-03 * C3),
+ F(2.01182542E-03 * C4), F(0.00000000E+00 * C4),
+ F(1.78371725E-03 * C5), -F(1.56575398E-04 * C5),
+ F(1.47640169E-03 * C6), -F(3.43256425E-04 * C6),
+ F(1.13992507E-03 * C7), -F(5.54620202E-04 * C7),
+#undef F
+#define F(x) F_COS8(x)
+ -F(1.0000000000 / C0), F(0.8314696123 / C1),
+ -F(1.0000000000 / C0), -F(0.1950903220 / C1),
+ -F(1.0000000000 / C0), -F(0.9807852804 / C1),
+ -F(1.0000000000 / C0), -F(0.5555702330 / C1),
+ -F(1.0000000000 / C0), F(0.5555702330 / C1),
+ -F(1.0000000000 / C0), F(0.9807852804 / C1),
+ -F(1.0000000000 / C0), F(0.1950903220 / C1),
+ -F(1.0000000000 / C0), -F(0.8314696123 / C1),
+ F(0.9238795325 / C2), F(0.9807852804 / C3),
+ F(0.3826834324 / C2), F(0.8314696123 / C3),
+ -F(0.3826834324 / C2), F(0.5555702330 / C3),
+ -F(0.9238795325 / C2), F(0.1950903220 / C3),
+ -F(0.9238795325 / C2), -F(0.1950903220 / C3),
+ -F(0.3826834324 / C2), -F(0.5555702330 / C3),
+ F(0.3826834324 / C2), -F(0.8314696123 / C3),
+ F(0.9238795325 / C2), -F(0.9807852804 / C3),
+ F(0.7071067812 / C4), F(0.5555702330 / C5),
+ -F(0.7071067812 / C4), -F(0.9807852804 / C5),
+ -F(0.7071067812 / C4), F(0.1950903220 / C5),
+ F(0.7071067812 / C4), F(0.8314696123 / C5),
+ F(0.7071067812 / C4), -F(0.8314696123 / C5),
+ -F(0.7071067812 / C4), -F(0.1950903220 / C5),
+ -F(0.7071067812 / C4), F(0.9807852804 / C5),
+ F(0.7071067812 / C4), -F(0.5555702330 / C5),
+ F(0.3826834324 / C6), F(0.1950903220 / C7),
+ -F(0.9238795325 / C6), -F(0.5555702330 / C7),
+ F(0.9238795325 / C6), F(0.8314696123 / C7),
+ -F(0.3826834324 / C6), -F(0.9807852804 / C7),
+ -F(0.3826834324 / C6), F(0.9807852804 / C7),
+ F(0.9238795325 / C6), -F(0.8314696123 / C7),
+ -F(0.9238795325 / C6), F(0.5555702330 / C7),
+ F(0.3826834324 / C6), -F(0.1950903220 / C7),
+#undef F
+
+#undef C0
+#undef C1
+#undef C2
+#undef C3
+#undef C4
+#undef C5
+#undef C6
+#undef C7
+};
diff --git a/src/modules/dbus/iface-card-profile.c b/src/modules/dbus/iface-card-profile.c
new file mode 100644
index 00000000..004e2e88
--- /dev/null
+++ b/src/modules/dbus/iface-card-profile.c
@@ -0,0 +1,228 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <dbus/dbus.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+
+#include "iface-card-profile.h"
+
+#define OBJECT_NAME "profile"
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_description(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_priority(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+struct pa_dbusiface_card_profile {
+ uint32_t index;
+ pa_card_profile *profile;
+ char *path;
+ pa_dbus_protocol *dbus_protocol;
+};
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INDEX,
+ PROPERTY_HANDLER_NAME,
+ PROPERTY_HANDLER_DESCRIPTION,
+ PROPERTY_HANDLER_SINKS,
+ PROPERTY_HANDLER_SOURCES,
+ PROPERTY_HANDLER_PRIORITY,
+ PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_get_index, .set_cb = NULL },
+ [PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_get_name, .set_cb = NULL },
+ [PROPERTY_HANDLER_DESCRIPTION] = { .property_name = "Description", .type = "s", .get_cb = handle_get_description, .set_cb = NULL },
+ [PROPERTY_HANDLER_SINKS] = { .property_name = "Sinks", .type = "u", .get_cb = handle_get_sinks, .set_cb = NULL },
+ [PROPERTY_HANDLER_SOURCES] = { .property_name = "Sources", .type = "u", .get_cb = handle_get_sources, .set_cb = NULL },
+ [PROPERTY_HANDLER_PRIORITY] = { .property_name = "Priority", .type = "u", .get_cb = handle_get_priority, .set_cb = NULL },
+};
+
+static pa_dbus_interface_info profile_interface_info = {
+ .name = PA_DBUSIFACE_CARD_PROFILE_INTERFACE,
+ .method_handlers = NULL,
+ .n_method_handlers = 0,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = NULL,
+ .n_signals = 0
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card_profile *p = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &p->index);
+}
+
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card_profile *p = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &p->profile->name);
+}
+
+static void handle_get_description(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card_profile *p = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &p->profile->description);
+}
+
+static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card_profile *p = userdata;
+ dbus_uint32_t sinks = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ sinks = p->profile->n_sinks;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sinks);
+}
+
+static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card_profile *p = userdata;
+ dbus_uint32_t sources = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ sources = p->profile->n_sources;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sources);
+}
+
+static void handle_get_priority(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card_profile *p = userdata;
+ dbus_uint32_t priority = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ priority = p->profile->priority;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &priority);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card_profile *p = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ dbus_uint32_t sinks = 0;
+ dbus_uint32_t sources = 0;
+ dbus_uint32_t priority = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ sinks = p->profile->n_sinks;
+ sources = p->profile->n_sources;
+ priority = p->profile->priority;
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &p->index);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &p->profile->name);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DESCRIPTION].property_name, DBUS_TYPE_STRING, &p->profile->description);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SINKS].property_name, DBUS_TYPE_UINT32, &sinks);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SOURCES].property_name, DBUS_TYPE_UINT32, &sources);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PRIORITY].property_name, DBUS_TYPE_UINT32, &priority);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+ dbus_message_unref(reply);
+}
+
+pa_dbusiface_card_profile *pa_dbusiface_card_profile_new(
+ pa_dbusiface_card *card,
+ pa_core *core,
+ pa_card_profile *profile,
+ uint32_t idx) {
+ pa_dbusiface_card_profile *p = NULL;
+
+ pa_assert(card);
+ pa_assert(core);
+ pa_assert(profile);
+
+ p = pa_xnew(pa_dbusiface_card_profile, 1);
+ p->index = idx;
+ p->profile = profile;
+ p->path = pa_sprintf_malloc("%s/%s%u", pa_dbusiface_card_get_path(card), OBJECT_NAME, idx);
+ p->dbus_protocol = pa_dbus_protocol_get(core);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(p->dbus_protocol, p->path, &profile_interface_info, p) >= 0);
+
+ return p;
+}
+
+void pa_dbusiface_card_profile_free(pa_dbusiface_card_profile *p) {
+ pa_assert(p);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(p->dbus_protocol, p->path, profile_interface_info.name) >= 0);
+
+ pa_dbus_protocol_unref(p->dbus_protocol);
+
+ pa_xfree(p->path);
+ pa_xfree(p);
+}
+
+const char *pa_dbusiface_card_profile_get_path(pa_dbusiface_card_profile *p) {
+ pa_assert(p);
+
+ return p->path;
+}
+
+const char *pa_dbusiface_card_profile_get_name(pa_dbusiface_card_profile *p) {
+ pa_assert(p);
+
+ return p->profile->name;
+}
diff --git a/src/modules/dbus/iface-card-profile.h b/src/modules/dbus/iface-card-profile.h
new file mode 100644
index 00000000..8ffb4b9c
--- /dev/null
+++ b/src/modules/dbus/iface-card-profile.h
@@ -0,0 +1,49 @@
+#ifndef foodbusifacecardprofilehfoo
+#define foodbusifacecardprofilehfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interface org.PulseAudio.Core1.CardProfile.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the CardProfile interface
+ * documentation.
+ */
+
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-card.h"
+
+#define PA_DBUSIFACE_CARD_PROFILE_INTERFACE PA_DBUS_CORE_INTERFACE ".CardProfile"
+
+typedef struct pa_dbusiface_card_profile pa_dbusiface_card_profile;
+
+pa_dbusiface_card_profile *pa_dbusiface_card_profile_new(
+ pa_dbusiface_card *card,
+ pa_core *core,
+ pa_card_profile *profile,
+ uint32_t idx);
+void pa_dbusiface_card_profile_free(pa_dbusiface_card_profile *p);
+
+const char *pa_dbusiface_card_profile_get_path(pa_dbusiface_card_profile *p);
+const char *pa_dbusiface_card_profile_get_name(pa_dbusiface_card_profile *p);
+
+#endif
diff --git a/src/modules/dbus/iface-card.c b/src/modules/dbus/iface-card.c
new file mode 100644
index 00000000..d99c8b95
--- /dev/null
+++ b/src/modules/dbus/iface-card.c
@@ -0,0 +1,561 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <dbus/dbus.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-card-profile.h"
+
+#include "iface-card.h"
+
+#define OBJECT_NAME "card"
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_profiles(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_active_profile(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_active_profile(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_profile_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+struct pa_dbusiface_card {
+ pa_dbusiface_core *core;
+
+ pa_card *card;
+ char *path;
+ pa_hashmap *profiles;
+ uint32_t next_profile_index;
+ pa_card_profile *active_profile;
+ pa_proplist *proplist;
+
+ pa_dbus_protocol *dbus_protocol;
+ pa_subscription *subscription;
+};
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INDEX,
+ PROPERTY_HANDLER_NAME,
+ PROPERTY_HANDLER_DRIVER,
+ PROPERTY_HANDLER_OWNER_MODULE,
+ PROPERTY_HANDLER_SINKS,
+ PROPERTY_HANDLER_SOURCES,
+ PROPERTY_HANDLER_PROFILES,
+ PROPERTY_HANDLER_ACTIVE_PROFILE,
+ PROPERTY_HANDLER_PROPERTY_LIST,
+ PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_get_index, .set_cb = NULL },
+ [PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_get_name, .set_cb = NULL },
+ [PROPERTY_HANDLER_DRIVER] = { .property_name = "Driver", .type = "s", .get_cb = handle_get_driver, .set_cb = NULL },
+ [PROPERTY_HANDLER_OWNER_MODULE] = { .property_name = "OwnerModule", .type = "o", .get_cb = handle_get_owner_module, .set_cb = NULL },
+ [PROPERTY_HANDLER_SINKS] = { .property_name = "Sinks", .type = "ao", .get_cb = handle_get_sinks, .set_cb = NULL },
+ [PROPERTY_HANDLER_SOURCES] = { .property_name = "Sources", .type = "ao", .get_cb = handle_get_sources, .set_cb = NULL },
+ [PROPERTY_HANDLER_PROFILES] = { .property_name = "Profiles", .type = "ao", .get_cb = handle_get_profiles, .set_cb = NULL },
+ [PROPERTY_HANDLER_ACTIVE_PROFILE] = { .property_name = "ActiveProfile", .type = "o", .get_cb = handle_get_active_profile, .set_cb = handle_set_active_profile },
+ [PROPERTY_HANDLER_PROPERTY_LIST] = { .property_name = "PropertyList", .type = "a{say}", .get_cb = handle_get_property_list, .set_cb = NULL }
+};
+
+enum method_handler_index {
+ METHOD_HANDLER_GET_PROFILE_BY_NAME,
+ METHOD_HANDLER_MAX
+};
+
+static pa_dbus_arg_info get_profile_by_name_args[] = { { "name", "s", "in" }, { "profile", "o", "out" } };
+
+static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
+ [METHOD_HANDLER_GET_PROFILE_BY_NAME] = {
+ .method_name = "GetProfileByName",
+ .arguments = get_profile_by_name_args,
+ .n_arguments = sizeof(get_profile_by_name_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_get_profile_by_name }
+};
+
+enum signal_index {
+ SIGNAL_ACTIVE_PROFILE_UPDATED,
+ SIGNAL_PROPERTY_LIST_UPDATED,
+ SIGNAL_MAX
+};
+
+static pa_dbus_arg_info active_profile_updated_args[] = { { "profile", "o", NULL } };
+static pa_dbus_arg_info property_list_updated_args[] = { { "property_list", "a{say}", NULL } };
+
+static pa_dbus_signal_info signals[SIGNAL_MAX] = {
+ [SIGNAL_ACTIVE_PROFILE_UPDATED] = { .name = "ActiveProfileUpdated", .arguments = active_profile_updated_args, .n_arguments = 1 },
+ [SIGNAL_PROPERTY_LIST_UPDATED] = { .name = "PropertyListUpdated", .arguments = property_list_updated_args, .n_arguments = 1 }
+};
+
+static pa_dbus_interface_info card_interface_info = {
+ .name = PA_DBUSIFACE_CARD_INTERFACE,
+ .method_handlers = method_handlers,
+ .n_method_handlers = METHOD_HANDLER_MAX,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = signals,
+ .n_signals = SIGNAL_MAX
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ dbus_uint32_t idx;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ idx = c->card->index;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &idx);
+}
+
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &c->card->name);
+}
+
+static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &c->card->driver);
+}
+
+static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ const char *owner_module;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ if (!c->card->module) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "Card %s doesn't have an owner module.", c->card->name);
+ return;
+ }
+
+ owner_module = pa_dbusiface_core_get_module_path(c->core, c->card->module);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &owner_module);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_sinks(pa_dbusiface_card *c, unsigned *n) {
+ const char **sinks = NULL;
+ unsigned i = 0;
+ uint32_t idx = 0;
+ pa_sink *sink = NULL;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_idxset_size(c->card->sinks);
+
+ if (*n == 0)
+ return NULL;
+
+ sinks = pa_xnew(const char *, *n);
+
+ PA_IDXSET_FOREACH(sink, c->card->sinks, idx) {
+ sinks[i] = pa_dbusiface_core_get_sink_path(c->core, sink);
+ ++i;
+ }
+
+ return sinks;
+}
+
+static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ const char **sinks;
+ unsigned n_sinks;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ sinks = get_sinks(c, &n_sinks);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, sinks, n_sinks);
+
+ pa_xfree(sinks);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_sources(pa_dbusiface_card *c, unsigned *n) {
+ const char **sources = NULL;
+ unsigned i = 0;
+ uint32_t idx = 0;
+ pa_source *source = NULL;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_idxset_size(c->card->sources);
+
+ if (*n == 0)
+ return NULL;
+
+ sources = pa_xnew(const char *, *n);
+
+ PA_IDXSET_FOREACH(source, c->card->sinks, idx) {
+ sources[i] = pa_dbusiface_core_get_source_path(c->core, source);
+ ++i;
+ }
+
+ return sources;
+}
+
+static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ const char **sources;
+ unsigned n_sources;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ sources = get_sources(c, &n_sources);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, sources, n_sources);
+
+ pa_xfree(sources);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_profiles(pa_dbusiface_card *c, unsigned *n) {
+ const char **profiles;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_card_profile *profile;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(c->profiles);
+
+ if (*n == 0)
+ return NULL;
+
+ profiles = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(profile, c->profiles, state)
+ profiles[i++] = pa_dbusiface_card_profile_get_path(profile);
+
+ return profiles;
+}
+
+static void handle_get_profiles(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ const char **profiles;
+ unsigned n_profiles;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ profiles = get_profiles(c, &n_profiles);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, profiles, n_profiles);
+
+ pa_xfree(profiles);
+}
+
+static void handle_get_active_profile(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ const char *active_profile;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ if (!c->active_profile) {
+ pa_assert(pa_hashmap_isempty(c->profiles));
+
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "The card %s has no profiles, and therefore there's no active profile either.", c->card->name);
+ return;
+ }
+
+ active_profile = pa_dbusiface_card_profile_get_path(pa_hashmap_get(c->profiles, c->active_profile->name));
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &active_profile);
+}
+
+static void handle_set_active_profile(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ const char *new_active_path;
+ pa_dbusiface_card_profile *new_active;
+ int r;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(c);
+
+ if (!c->active_profile) {
+ pa_assert(pa_hashmap_isempty(c->profiles));
+
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "The card %s has no profiles, and therefore there's no active profile either.",
+ c->card->name);
+ return;
+ }
+
+ dbus_message_iter_get_basic(iter, &new_active_path);
+
+ if (!(new_active = pa_hashmap_get(c->profiles, new_active_path))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such profile.", new_active_path);
+ return;
+ }
+
+ if ((r = pa_card_set_profile(c->card, pa_dbusiface_card_profile_get_name(new_active), TRUE)) < 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
+ "Internal error in PulseAudio: pa_card_set_profile() failed with error code %i.", r);
+ return;
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_dbus_send_proplist_variant_reply(conn, msg, c->proplist);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ dbus_uint32_t idx;
+ const char *owner_module = NULL;
+ const char **sinks = NULL;
+ unsigned n_sinks = 0;
+ const char **sources = NULL;
+ unsigned n_sources = 0;
+ const char **profiles = NULL;
+ unsigned n_profiles = 0;
+ const char *active_profile = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ idx = c->card->index;
+ if (c->card->module)
+ owner_module = pa_dbusiface_core_get_module_path(c->core, c->card->module);
+ sinks = get_sinks(c, &n_sinks);
+ sources = get_sources(c, &n_sources);
+ profiles = get_profiles(c, &n_profiles);
+ if (c->active_profile)
+ active_profile = pa_dbusiface_card_profile_get_path(pa_hashmap_get(c->profiles, c->active_profile->name));
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &idx);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &c->card->name);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DRIVER].property_name, DBUS_TYPE_STRING, &c->card->driver);
+
+ if (owner_module)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_OWNER_MODULE].property_name, DBUS_TYPE_OBJECT_PATH, &owner_module);
+
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SINKS].property_name, DBUS_TYPE_OBJECT_PATH, sinks, n_sinks);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SOURCES].property_name, DBUS_TYPE_OBJECT_PATH, sources, n_sources);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROFILES].property_name, DBUS_TYPE_OBJECT_PATH, profiles, n_profiles);
+
+ if (active_profile)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_ACTIVE_PROFILE].property_name, DBUS_TYPE_OBJECT_PATH, &active_profile);
+
+ pa_dbus_append_proplist_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROPERTY_LIST].property_name, c->proplist);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+
+ pa_xfree(sinks);
+ pa_xfree(sources);
+ pa_xfree(profiles);
+}
+
+static void handle_get_profile_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ const char *profile_name = NULL;
+ pa_dbusiface_card_profile *profile = NULL;
+ const char *profile_path = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &profile_name, DBUS_TYPE_INVALID));
+
+ if (!(profile = pa_hashmap_get(c->profiles, profile_name))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such profile on card %s.", profile_name, c->card->name);
+ return;
+ }
+
+ profile_path = pa_dbusiface_card_profile_get_path(profile);
+
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &profile_path);
+}
+
+static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ pa_dbusiface_card *c = userdata;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(core);
+ pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_CARD);
+ pa_assert(c);
+
+ /* We can't use idx != c->card->index, because the c->card pointer may
+ * be stale at this point. */
+ if (pa_idxset_get_by_index(core->cards, idx) != c->card)
+ return;
+
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
+ return;
+
+ if (c->active_profile != c->card->active_profile) {
+ const char *object_path;
+
+ c->active_profile = c->card->active_profile;
+ object_path = pa_dbusiface_card_profile_get_path(pa_hashmap_get(c->profiles, c->active_profile->name));
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(c->path,
+ PA_DBUSIFACE_CARD_INTERFACE,
+ signals[SIGNAL_ACTIVE_PROFILE_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+
+ if (!pa_proplist_equal(c->proplist, c->card->proplist)) {
+ DBusMessageIter msg_iter;
+
+ pa_proplist_update(c->proplist, PA_UPDATE_SET, c->card->proplist);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(c->path,
+ PA_DBUSIFACE_CARD_INTERFACE,
+ signals[SIGNAL_PROPERTY_LIST_UPDATED].name));
+ dbus_message_iter_init_append(signal_msg, &msg_iter);
+ pa_dbus_append_proplist(&msg_iter, c->proplist);
+
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+}
+
+pa_dbusiface_card *pa_dbusiface_card_new(pa_dbusiface_core *core, pa_card *card) {
+ pa_dbusiface_card *c = NULL;
+
+ pa_assert(core);
+ pa_assert(card);
+
+ c = pa_xnew0(pa_dbusiface_card, 1);
+ c->core = core;
+ c->card = card;
+ c->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, OBJECT_NAME, card->index);
+ c->profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ c->next_profile_index = 0;
+ c->active_profile = NULL;
+ c->proplist = pa_proplist_copy(card->proplist);
+ c->dbus_protocol = pa_dbus_protocol_get(card->core);
+ c->subscription = pa_subscription_new(card->core, PA_SUBSCRIPTION_MASK_CARD, subscription_cb, c);
+
+ if (card->profiles) {
+ pa_card_profile *profile;
+ void *state = NULL;
+
+ PA_HASHMAP_FOREACH(profile, card->profiles, state) {
+ pa_dbusiface_card_profile *p = pa_dbusiface_card_profile_new(c, card->core, profile, c->next_profile_index++);
+ pa_hashmap_put(c->profiles, pa_dbusiface_card_profile_get_name(p), p);
+ }
+ pa_assert_se(c->active_profile = card->active_profile);
+ }
+
+ pa_assert_se(pa_dbus_protocol_add_interface(c->dbus_protocol, c->path, &card_interface_info, c) >= 0);
+
+ return c;
+}
+
+static void profile_free_cb(void *p, void *userdata) {
+ pa_dbusiface_card_profile *profile = p;
+
+ pa_assert(profile);
+
+ pa_dbusiface_card_profile_free(profile);
+}
+
+void pa_dbusiface_card_free(pa_dbusiface_card *c) {
+ pa_assert(c);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(c->dbus_protocol, c->path, card_interface_info.name) >= 0);
+
+ pa_hashmap_free(c->profiles, profile_free_cb, NULL);
+ pa_proplist_free(c->proplist);
+ pa_dbus_protocol_unref(c->dbus_protocol);
+ pa_subscription_free(c->subscription);
+
+ pa_xfree(c->path);
+ pa_xfree(c);
+}
+
+const char *pa_dbusiface_card_get_path(pa_dbusiface_card *c) {
+ pa_assert(c);
+
+ return c->path;
+}
diff --git a/src/modules/dbus/iface-card.h b/src/modules/dbus/iface-card.h
new file mode 100644
index 00000000..e2c08a3b
--- /dev/null
+++ b/src/modules/dbus/iface-card.h
@@ -0,0 +1,45 @@
+#ifndef foodbusifacecardhfoo
+#define foodbusifacecardhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interface org.PulseAudio.Core1.Card.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the Card interface
+ * documentation.
+ */
+
+#include <pulsecore/card.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-core.h"
+
+#define PA_DBUSIFACE_CARD_INTERFACE PA_DBUS_CORE_INTERFACE ".Card"
+
+typedef struct pa_dbusiface_card pa_dbusiface_card;
+
+pa_dbusiface_card *pa_dbusiface_card_new(pa_dbusiface_core *core, pa_card *card);
+void pa_dbusiface_card_free(pa_dbusiface_card *c);
+
+const char *pa_dbusiface_card_get_path(pa_dbusiface_card *c);
+
+#endif
diff --git a/src/modules/dbus/iface-client.c b/src/modules/dbus/iface-client.c
new file mode 100644
index 00000000..e6675449
--- /dev/null
+++ b/src/modules/dbus/iface-client.c
@@ -0,0 +1,461 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+ Copyright 2009 Vincent Filali-Ansary <filali.v@azurdigitalnetworks.net>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <dbus/dbus.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-client.h"
+
+#define OBJECT_NAME "client"
+
+struct pa_dbusiface_client {
+ pa_dbusiface_core *core;
+
+ pa_client *client;
+ char *path;
+ pa_proplist *proplist;
+
+ pa_dbus_protocol *dbus_protocol;
+ pa_subscription *subscription;
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_playback_streams(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_record_streams(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_kill(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_update_properties(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_remove_properties(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INDEX,
+ PROPERTY_HANDLER_DRIVER,
+ PROPERTY_HANDLER_OWNER_MODULE,
+ PROPERTY_HANDLER_PLAYBACK_STREAMS,
+ PROPERTY_HANDLER_RECORD_STREAMS,
+ PROPERTY_HANDLER_PROPERTY_LIST,
+ PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_get_index, .set_cb = NULL },
+ [PROPERTY_HANDLER_DRIVER] = { .property_name = "Driver", .type = "s", .get_cb = handle_get_driver, .set_cb = NULL },
+ [PROPERTY_HANDLER_OWNER_MODULE] = { .property_name = "OwnerModule", .type = "o", .get_cb = handle_get_owner_module, .set_cb = NULL },
+ [PROPERTY_HANDLER_PLAYBACK_STREAMS] = { .property_name = "PlaybackStreams", .type = "ao", .get_cb = handle_get_playback_streams, .set_cb = NULL },
+ [PROPERTY_HANDLER_RECORD_STREAMS] = { .property_name = "RecordStreams", .type = "ao", .get_cb = handle_get_record_streams, .set_cb = NULL },
+ [PROPERTY_HANDLER_PROPERTY_LIST] = { .property_name = "PropertyList", .type = "a{say}", .get_cb = handle_get_property_list, .set_cb = NULL }
+};
+
+enum method_handler_index {
+ METHOD_HANDLER_KILL,
+ METHOD_HANDLER_UPDATE_PROPERTIES,
+ METHOD_HANDLER_REMOVE_PROPERTIES,
+ METHOD_HANDLER_MAX
+};
+
+static pa_dbus_arg_info update_properties_args[] = { { "property_list", "a{say}", "in" }, { "update_mode", "u", "in" } };
+static pa_dbus_arg_info remove_properties_args[] = { { "keys", "as", "in" } };
+
+static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
+ [METHOD_HANDLER_KILL] = {
+ .method_name = "Kill",
+ .arguments = NULL,
+ .n_arguments = 0,
+ .receive_cb = handle_kill },
+ [METHOD_HANDLER_UPDATE_PROPERTIES] = {
+ .method_name = "UpdateProperties",
+ .arguments = update_properties_args,
+ .n_arguments = sizeof(update_properties_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_update_properties },
+ [METHOD_HANDLER_REMOVE_PROPERTIES] = {
+ .method_name = "RemoveProperties",
+ .arguments = remove_properties_args,
+ .n_arguments = sizeof(remove_properties_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_remove_properties }
+};
+
+enum signal_index {
+ SIGNAL_PROPERTY_LIST_UPDATED,
+ SIGNAL_CLIENT_EVENT,
+ SIGNAL_MAX
+};
+
+static pa_dbus_arg_info property_list_updated_args[] = { { "property_list", "a{say}", NULL } };
+static pa_dbus_arg_info client_event_args[] = { { "name", "s", NULL },
+ { "property_list", "a{say}", NULL } };
+
+static pa_dbus_signal_info signals[SIGNAL_MAX] = {
+ [SIGNAL_PROPERTY_LIST_UPDATED] = { .name = "PropertyListUpdated", .arguments = property_list_updated_args, .n_arguments = 1 },
+ /* ClientEvent is sent from module-dbus-protocol.c. */
+ [SIGNAL_CLIENT_EVENT] = { .name = "ClientEvent", .arguments = client_event_args, .n_arguments = 1 }
+};
+
+static pa_dbus_interface_info client_interface_info = {
+ .name = PA_DBUSIFACE_CLIENT_INTERFACE,
+ .method_handlers = method_handlers,
+ .n_method_handlers = METHOD_HANDLER_MAX,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = signals,
+ .n_signals = SIGNAL_MAX
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+ dbus_uint32_t idx = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ idx = c->client->index;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &idx);
+}
+
+static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &c->client->driver);
+}
+
+static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+ const char *owner_module = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ if (!c->client->module) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "Client %d doesn't have an owner module.", c->client->index);
+ return;
+ }
+
+ owner_module = pa_dbusiface_core_get_module_path(c->core, c->client->module);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &owner_module);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_playback_streams(pa_dbusiface_client *c, unsigned *n) {
+ const char **playback_streams = NULL;
+ unsigned i = 0;
+ uint32_t idx = 0;
+ pa_sink_input *sink_input = NULL;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_idxset_size(c->client->sink_inputs);
+
+ if (*n == 0)
+ return NULL;
+
+ playback_streams = pa_xnew(const char *, *n);
+
+ PA_IDXSET_FOREACH(sink_input, c->client->sink_inputs, idx)
+ playback_streams[i++] = pa_dbusiface_core_get_playback_stream_path(c->core, sink_input);
+
+ return playback_streams;
+}
+
+static void handle_get_playback_streams(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+ const char **playback_streams = NULL;
+ unsigned n_playback_streams = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ playback_streams = get_playback_streams(c, &n_playback_streams);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, playback_streams, n_playback_streams);
+
+ pa_xfree(playback_streams);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_record_streams(pa_dbusiface_client *c, unsigned *n) {
+ const char **record_streams = NULL;
+ unsigned i = 0;
+ uint32_t idx = 0;
+ pa_source_output *source_output = NULL;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_idxset_size(c->client->source_outputs);
+
+ if (*n == 0)
+ return NULL;
+
+ record_streams = pa_xnew(const char *, *n);
+
+ PA_IDXSET_FOREACH(source_output, c->client->source_outputs, idx)
+ record_streams[i++] = pa_dbusiface_core_get_record_stream_path(c->core, source_output);
+
+ return record_streams;
+}
+
+static void handle_get_record_streams(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+ const char **record_streams = NULL;
+ unsigned n_record_streams = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ record_streams = get_record_streams(c, &n_record_streams);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, record_streams, n_record_streams);
+
+ pa_xfree(record_streams);
+}
+
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_dbus_send_proplist_variant_reply(conn, msg, c->client->proplist);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ dbus_uint32_t idx = 0;
+ const char *owner_module = NULL;
+ const char **playback_streams = NULL;
+ unsigned n_playback_streams = 0;
+ const char **record_streams = NULL;
+ unsigned n_record_streams = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ idx = c->client->index;
+ if (c->client->module)
+ owner_module = pa_dbusiface_core_get_module_path(c->core, c->client->module);
+ playback_streams = get_playback_streams(c, &n_playback_streams);
+ record_streams = get_record_streams(c, &n_record_streams);
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &idx);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DRIVER].property_name, DBUS_TYPE_STRING, &c->client->driver);
+
+ if (owner_module)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_OWNER_MODULE].property_name, DBUS_TYPE_OBJECT_PATH, &owner_module);
+
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PLAYBACK_STREAMS].property_name, DBUS_TYPE_OBJECT_PATH, playback_streams, n_playback_streams);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_RECORD_STREAMS].property_name, DBUS_TYPE_OBJECT_PATH, record_streams, n_record_streams);
+ pa_dbus_append_proplist_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROPERTY_LIST].property_name, c->client->proplist);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+
+ pa_xfree(playback_streams);
+ pa_xfree(record_streams);
+}
+
+static void handle_kill(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ dbus_connection_ref(conn);
+
+ pa_client_kill(c->client);
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ dbus_connection_unref(conn);
+}
+
+static void handle_update_properties(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+ DBusMessageIter msg_iter;
+ pa_proplist *property_list = NULL;
+ dbus_uint32_t update_mode = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ if (pa_dbus_protocol_get_client(c->dbus_protocol, conn) != c->client) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_ACCESS_DENIED, "Client tried to modify the property list of another client.");
+ return;
+ }
+
+ pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
+
+ if (!(property_list = pa_dbus_get_proplist_arg(conn, msg, &msg_iter)))
+ return;
+
+ dbus_message_iter_get_basic(&msg_iter, &update_mode);
+
+ if (!(update_mode == PA_UPDATE_SET || update_mode == PA_UPDATE_MERGE || update_mode == PA_UPDATE_REPLACE)) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid update mode: %u", update_mode);
+ goto finish;
+ }
+
+ pa_client_update_proplist(c->client, update_mode, property_list);
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+finish:
+ if (property_list)
+ pa_proplist_free(property_list);
+}
+
+static void handle_remove_properties(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+ char **keys = NULL;
+ int n_keys = 0;
+ pa_bool_t changed = FALSE;
+ int i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ if (pa_dbus_protocol_get_client(c->dbus_protocol, conn) != c->client) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_ACCESS_DENIED, "Client tried to modify the property list of another client.");
+ return;
+ }
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &keys, &n_keys, DBUS_TYPE_INVALID));
+
+ for (i = 0; i < n_keys; ++i)
+ changed |= pa_proplist_unset(c->client->proplist, keys[i]) >= 0;
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ if (changed)
+ pa_subscription_post(c->client->core, PA_SUBSCRIPTION_EVENT_CLIENT|PA_SUBSCRIPTION_EVENT_CHANGE, c->client->index);
+
+ dbus_free_string_array(keys);
+}
+
+static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ pa_dbusiface_client *c = userdata;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(core);
+ pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_CLIENT);
+ pa_assert(c);
+
+ /* We can't use idx != c->client->index, because the c->client pointer may
+ * be stale at this point. */
+ if (pa_idxset_get_by_index(core->clients, idx) != c->client)
+ return;
+
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
+ return;
+
+ if (!pa_proplist_equal(c->proplist, c->client->proplist)) {
+ DBusMessageIter msg_iter;
+
+ pa_proplist_update(c->proplist, PA_UPDATE_SET, c->client->proplist);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(c->path,
+ PA_DBUSIFACE_CLIENT_INTERFACE,
+ signals[SIGNAL_PROPERTY_LIST_UPDATED].name));
+ dbus_message_iter_init_append(signal_msg, &msg_iter);
+ pa_dbus_append_proplist(&msg_iter, c->proplist);
+
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+}
+
+pa_dbusiface_client *pa_dbusiface_client_new(pa_dbusiface_core *core, pa_client *client) {
+ pa_dbusiface_client *c = NULL;
+
+ pa_assert(core);
+ pa_assert(client);
+
+ c = pa_xnew(pa_dbusiface_client, 1);
+ c->core = core;
+ c->client = client;
+ c->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, OBJECT_NAME, client->index);
+ c->proplist = pa_proplist_copy(client->proplist);
+ c->dbus_protocol = pa_dbus_protocol_get(client->core);
+ c->subscription = pa_subscription_new(client->core, PA_SUBSCRIPTION_MASK_CLIENT, subscription_cb, c);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(c->dbus_protocol, c->path, &client_interface_info, c) >= 0);
+
+ return c;
+}
+
+void pa_dbusiface_client_free(pa_dbusiface_client *c) {
+ pa_assert(c);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(c->dbus_protocol, c->path, client_interface_info.name) >= 0);
+
+ pa_proplist_free(c->proplist);
+ pa_dbus_protocol_unref(c->dbus_protocol);
+ pa_subscription_free(c->subscription);
+
+ pa_xfree(c->path);
+ pa_xfree(c);
+}
+
+const char *pa_dbusiface_client_get_path(pa_dbusiface_client *c) {
+ pa_assert(c);
+
+ return c->path;
+}
diff --git a/src/modules/dbus/iface-client.h b/src/modules/dbus/iface-client.h
new file mode 100644
index 00000000..e8f151cd
--- /dev/null
+++ b/src/modules/dbus/iface-client.h
@@ -0,0 +1,45 @@
+#ifndef foodbusifaceclienthfoo
+#define foodbusifaceclienthfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interface org.PulseAudio.Core1.Client.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the Client interface
+ * documentation.
+ */
+
+#include <pulsecore/client.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-core.h"
+
+#define PA_DBUSIFACE_CLIENT_INTERFACE PA_DBUS_CORE_INTERFACE ".Client"
+
+typedef struct pa_dbusiface_client pa_dbusiface_client;
+
+pa_dbusiface_client *pa_dbusiface_client_new(pa_dbusiface_core *core, pa_client *client);
+void pa_dbusiface_client_free(pa_dbusiface_client *c);
+
+const char *pa_dbusiface_client_get_path(pa_dbusiface_client *c);
+
+#endif
diff --git a/src/modules/dbus/iface-core.c b/src/modules/dbus/iface-core.c
new file mode 100644
index 00000000..bb43df9b
--- /dev/null
+++ b/src/modules/dbus/iface-core.c
@@ -0,0 +1,2206 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <ctype.h>
+
+#include <dbus/dbus.h>
+
+#include <pulse/utf8.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/protocol-dbus.h>
+#include <pulsecore/socket-util.h>
+#include <pulsecore/strbuf.h>
+
+#include "iface-card.h"
+#include "iface-client.h"
+#include "iface-device.h"
+#include "iface-memstats.h"
+#include "iface-module.h"
+#include "iface-sample.h"
+#include "iface-stream.h"
+
+#include "iface-core.h"
+
+#define INTERFACE_REVISION 0
+
+static void handle_get_interface_revision(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_version(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_is_local(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_username(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_hostname(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_default_channels(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_default_channels(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_default_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_default_sample_format(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_default_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_default_sample_rate(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_cards(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_fallback_sink(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_fallback_sink(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_fallback_source(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_fallback_source(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_playback_streams(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_record_streams(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_samples(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_modules(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_clients(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_my_client(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_extensions(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_card_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sink_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_source_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sample_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_upload_sample(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_load_module(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_exit(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_listen_for_signal(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_stop_listening_for_signal(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+struct pa_dbusiface_core {
+ pa_core *core;
+ pa_subscription *subscription;
+
+ pa_dbus_protocol *dbus_protocol;
+
+ pa_hashmap *cards;
+ pa_hashmap *sinks_by_index;
+ pa_hashmap *sinks_by_path;
+ pa_hashmap *sources_by_index;
+ pa_hashmap *sources_by_path;
+ pa_hashmap *playback_streams;
+ pa_hashmap *record_streams;
+ pa_hashmap *samples;
+ pa_hashmap *modules;
+ pa_hashmap *clients;
+
+ pa_sink *fallback_sink;
+ pa_source *fallback_source;
+
+ pa_hook_slot *sink_put_slot;
+ pa_hook_slot *sink_unlink_slot;
+ pa_hook_slot *source_put_slot;
+ pa_hook_slot *source_unlink_slot;
+ pa_hook_slot *extension_registered_slot;
+ pa_hook_slot *extension_unregistered_slot;
+
+ pa_dbusiface_memstats *memstats;
+};
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INTERFACE_REVISION,
+ PROPERTY_HANDLER_NAME,
+ PROPERTY_HANDLER_VERSION,
+ PROPERTY_HANDLER_IS_LOCAL,
+ PROPERTY_HANDLER_USERNAME,
+ PROPERTY_HANDLER_HOSTNAME,
+ PROPERTY_HANDLER_DEFAULT_CHANNELS,
+ PROPERTY_HANDLER_DEFAULT_SAMPLE_FORMAT,
+ PROPERTY_HANDLER_DEFAULT_SAMPLE_RATE,
+ PROPERTY_HANDLER_CARDS,
+ PROPERTY_HANDLER_SINKS,
+ PROPERTY_HANDLER_FALLBACK_SINK,
+ PROPERTY_HANDLER_SOURCES,
+ PROPERTY_HANDLER_FALLBACK_SOURCE,
+ PROPERTY_HANDLER_PLAYBACK_STREAMS,
+ PROPERTY_HANDLER_RECORD_STREAMS,
+ PROPERTY_HANDLER_SAMPLES,
+ PROPERTY_HANDLER_MODULES,
+ PROPERTY_HANDLER_CLIENTS,
+ PROPERTY_HANDLER_MY_CLIENT,
+ PROPERTY_HANDLER_EXTENSIONS,
+ PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INTERFACE_REVISION] = { .property_name = "InterfaceRevision", .type = "u", .get_cb = handle_get_interface_revision, .set_cb = NULL },
+ [PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_get_name, .set_cb = NULL },
+ [PROPERTY_HANDLER_VERSION] = { .property_name = "Version", .type = "s", .get_cb = handle_get_version, .set_cb = NULL },
+ [PROPERTY_HANDLER_IS_LOCAL] = { .property_name = "IsLocal", .type = "b", .get_cb = handle_get_is_local, .set_cb = NULL },
+ [PROPERTY_HANDLER_USERNAME] = { .property_name = "Username", .type = "s", .get_cb = handle_get_username, .set_cb = NULL },
+ [PROPERTY_HANDLER_HOSTNAME] = { .property_name = "Hostname", .type = "s", .get_cb = handle_get_hostname, .set_cb = NULL },
+ [PROPERTY_HANDLER_DEFAULT_CHANNELS] = { .property_name = "DefaultChannels", .type = "au", .get_cb = handle_get_default_channels, .set_cb = handle_set_default_channels },
+ [PROPERTY_HANDLER_DEFAULT_SAMPLE_FORMAT] = { .property_name = "DefaultSampleFormat", .type = "u", .get_cb = handle_get_default_sample_format, .set_cb = handle_set_default_sample_format },
+ [PROPERTY_HANDLER_DEFAULT_SAMPLE_RATE] = { .property_name = "DefaultSampleRate", .type = "u", .get_cb = handle_get_default_sample_rate, .set_cb = handle_set_default_sample_rate },
+ [PROPERTY_HANDLER_CARDS] = { .property_name = "Cards", .type = "ao", .get_cb = handle_get_cards, .set_cb = NULL },
+ [PROPERTY_HANDLER_SINKS] = { .property_name = "Sinks", .type = "ao", .get_cb = handle_get_sinks, .set_cb = NULL },
+ [PROPERTY_HANDLER_FALLBACK_SINK] = { .property_name = "FallbackSink", .type = "o", .get_cb = handle_get_fallback_sink, .set_cb = handle_set_fallback_sink },
+ [PROPERTY_HANDLER_SOURCES] = { .property_name = "Sources", .type = "ao", .get_cb = handle_get_sources, .set_cb = NULL },
+ [PROPERTY_HANDLER_FALLBACK_SOURCE] = { .property_name = "FallbackSource", .type = "o", .get_cb = handle_get_fallback_source, .set_cb = handle_set_fallback_source },
+ [PROPERTY_HANDLER_PLAYBACK_STREAMS] = { .property_name = "PlaybackStreams", .type = "ao", .get_cb = handle_get_playback_streams, .set_cb = NULL },
+ [PROPERTY_HANDLER_RECORD_STREAMS] = { .property_name = "RecordStreams", .type = "ao", .get_cb = handle_get_record_streams, .set_cb = NULL },
+ [PROPERTY_HANDLER_SAMPLES] = { .property_name = "Samples", .type = "ao", .get_cb = handle_get_samples, .set_cb = NULL },
+ [PROPERTY_HANDLER_MODULES] = { .property_name = "Modules", .type = "ao", .get_cb = handle_get_modules, .set_cb = NULL },
+ [PROPERTY_HANDLER_CLIENTS] = { .property_name = "Clients", .type = "ao", .get_cb = handle_get_clients, .set_cb = NULL },
+ [PROPERTY_HANDLER_MY_CLIENT] = { .property_name = "MyClient", .type = "o", .get_cb = handle_get_my_client, .set_cb = NULL },
+ [PROPERTY_HANDLER_EXTENSIONS] = { .property_name = "Extensions", .type = "as", .get_cb = handle_get_extensions, .set_cb = NULL }
+};
+
+enum method_handler_index {
+ METHOD_HANDLER_GET_CARD_BY_NAME,
+ METHOD_HANDLER_GET_SINK_BY_NAME,
+ METHOD_HANDLER_GET_SOURCE_BY_NAME,
+ METHOD_HANDLER_GET_SAMPLE_BY_NAME,
+ METHOD_HANDLER_UPLOAD_SAMPLE,
+ METHOD_HANDLER_LOAD_MODULE,
+ METHOD_HANDLER_EXIT,
+ METHOD_HANDLER_LISTEN_FOR_SIGNAL,
+ METHOD_HANDLER_STOP_LISTENING_FOR_SIGNAL,
+ METHOD_HANDLER_MAX
+};
+
+static pa_dbus_arg_info get_card_by_name_args[] = { { "name", "s", "in" }, { "card", "o", "out" } };
+static pa_dbus_arg_info get_sink_by_name_args[] = { { "name", "s", "in" }, { "sink", "o", "out" } };
+static pa_dbus_arg_info get_source_by_name_args[] = { { "name", "s", "in" }, { "source", "o", "out" } };
+static pa_dbus_arg_info get_sample_by_name_args[] = { { "name", "s", "in" }, { "sample", "o", "out" } };
+static pa_dbus_arg_info upload_sample_args[] = { { "name", "s", "in" },
+ { "sample_format", "u", "in" },
+ { "sample_rate", "u", "in" },
+ { "channels", "au", "in" },
+ { "default_volume", "au", "in" },
+ { "property_list", "a{say}", "in" },
+ { "data", "ay", "in" },
+ { "sample", "o", "out" } };
+static pa_dbus_arg_info load_module_args[] = { { "name", "s", "in" }, { "arguments", "a{ss}", "in" }, { "module", "o", "out" } };
+static pa_dbus_arg_info listen_for_signal_args[] = { { "signal", "s", "in" }, { "objects", "ao", "in" } };
+static pa_dbus_arg_info stop_listening_for_signal_args[] = { { "signal", "s", "in" } };
+
+static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
+ [METHOD_HANDLER_GET_CARD_BY_NAME] = {
+ .method_name = "GetCardByName",
+ .arguments = get_card_by_name_args,
+ .n_arguments = sizeof(get_card_by_name_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_get_card_by_name },
+ [METHOD_HANDLER_GET_SINK_BY_NAME] = {
+ .method_name = "GetSinkByName",
+ .arguments = get_sink_by_name_args,
+ .n_arguments = sizeof(get_sink_by_name_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_get_sink_by_name },
+ [METHOD_HANDLER_GET_SOURCE_BY_NAME] = {
+ .method_name = "GetSourceByName",
+ .arguments = get_source_by_name_args,
+ .n_arguments = sizeof(get_source_by_name_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_get_source_by_name },
+ [METHOD_HANDLER_GET_SAMPLE_BY_NAME] = {
+ .method_name = "GetSampleByName",
+ .arguments = get_sample_by_name_args,
+ .n_arguments = sizeof(get_sample_by_name_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_get_sample_by_name },
+ [METHOD_HANDLER_UPLOAD_SAMPLE] = {
+ .method_name = "UploadSample",
+ .arguments = upload_sample_args,
+ .n_arguments = sizeof(upload_sample_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_upload_sample },
+ [METHOD_HANDLER_LOAD_MODULE] = {
+ .method_name = "LoadModule",
+ .arguments = load_module_args,
+ .n_arguments = sizeof(load_module_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_load_module },
+ [METHOD_HANDLER_EXIT] = {
+ .method_name = "Exit",
+ .arguments = NULL,
+ .n_arguments = 0,
+ .receive_cb = handle_exit },
+ [METHOD_HANDLER_LISTEN_FOR_SIGNAL] = {
+ .method_name = "ListenForSignal",
+ .arguments = listen_for_signal_args,
+ .n_arguments = sizeof(listen_for_signal_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_listen_for_signal },
+ [METHOD_HANDLER_STOP_LISTENING_FOR_SIGNAL] = {
+ .method_name = "StopListeningForSignal",
+ .arguments = stop_listening_for_signal_args,
+ .n_arguments = sizeof(stop_listening_for_signal_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_stop_listening_for_signal }
+};
+
+enum signal_index {
+ SIGNAL_NEW_CARD,
+ SIGNAL_CARD_REMOVED,
+ SIGNAL_NEW_SINK,
+ SIGNAL_SINK_REMOVED,
+ SIGNAL_FALLBACK_SINK_UPDATED,
+ SIGNAL_FALLBACK_SINK_UNSET,
+ SIGNAL_NEW_SOURCE,
+ SIGNAL_SOURCE_REMOVED,
+ SIGNAL_FALLBACK_SOURCE_UPDATED,
+ SIGNAL_FALLBACK_SOURCE_UNSET,
+ SIGNAL_NEW_PLAYBACK_STREAM,
+ SIGNAL_PLAYBACK_STREAM_REMOVED,
+ SIGNAL_NEW_RECORD_STREAM,
+ SIGNAL_RECORD_STREAM_REMOVED,
+ SIGNAL_NEW_SAMPLE,
+ SIGNAL_SAMPLE_REMOVED,
+ SIGNAL_NEW_MODULE,
+ SIGNAL_MODULE_REMOVED,
+ SIGNAL_NEW_CLIENT,
+ SIGNAL_CLIENT_REMOVED,
+ SIGNAL_NEW_EXTENSION,
+ SIGNAL_EXTENSION_REMOVED,
+ SIGNAL_MAX
+};
+
+static pa_dbus_arg_info new_card_args[] = { { "card", "o", NULL } };
+static pa_dbus_arg_info card_removed_args[] = { { "card", "o", NULL } };
+static pa_dbus_arg_info new_sink_args[] = { { "sink", "o", NULL } };
+static pa_dbus_arg_info sink_removed_args[] = { { "sink", "o", NULL } };
+static pa_dbus_arg_info fallback_sink_updated_args[] = { { "sink", "o", NULL } };
+static pa_dbus_arg_info new_source_args[] = { { "source", "o", NULL } };
+static pa_dbus_arg_info source_removed_args[] = { { "source", "o", NULL } };
+static pa_dbus_arg_info fallback_source_updated_args[] = { { "source", "o", NULL } };
+static pa_dbus_arg_info new_playback_stream_args[] = { { "playback_stream", "o", NULL } };
+static pa_dbus_arg_info playback_stream_removed_args[] = { { "playback_stream", "o", NULL } };
+static pa_dbus_arg_info new_record_stream_args[] = { { "record_stream", "o", NULL } };
+static pa_dbus_arg_info record_stream_removed_args[] = { { "record_stream", "o", NULL } };
+static pa_dbus_arg_info new_sample_args[] = { { "sample", "o", NULL } };
+static pa_dbus_arg_info sample_removed_args[] = { { "sample", "o", NULL } };
+static pa_dbus_arg_info new_module_args[] = { { "module", "o", NULL } };
+static pa_dbus_arg_info module_removed_args[] = { { "module", "o", NULL } };
+static pa_dbus_arg_info new_client_args[] = { { "client", "o", NULL } };
+static pa_dbus_arg_info client_removed_args[] = { { "client", "o", NULL } };
+static pa_dbus_arg_info new_extension_args[] = { { "extension", "s", NULL } };
+static pa_dbus_arg_info extension_removed_args[] = { { "extension", "s", NULL } };
+
+static pa_dbus_signal_info signals[SIGNAL_MAX] = {
+ [SIGNAL_NEW_CARD] = { .name = "NewCard", .arguments = new_card_args, .n_arguments = 1 },
+ [SIGNAL_CARD_REMOVED] = { .name = "CardRemoved", .arguments = card_removed_args, .n_arguments = 1 },
+ [SIGNAL_NEW_SINK] = { .name = "NewSink", .arguments = new_sink_args, .n_arguments = 1 },
+ [SIGNAL_SINK_REMOVED] = { .name = "SinkRemoved", .arguments = sink_removed_args, .n_arguments = 1 },
+ [SIGNAL_FALLBACK_SINK_UPDATED] = { .name = "FallbackSinkUpdated", .arguments = fallback_sink_updated_args, .n_arguments = 1 },
+ [SIGNAL_FALLBACK_SINK_UNSET] = { .name = "FallbackSinkUnset", .arguments = NULL, .n_arguments = 0 },
+ [SIGNAL_NEW_SOURCE] = { .name = "NewSource", .arguments = new_source_args, .n_arguments = 1 },
+ [SIGNAL_SOURCE_REMOVED] = { .name = "SourceRemoved", .arguments = source_removed_args, .n_arguments = 1 },
+ [SIGNAL_FALLBACK_SOURCE_UPDATED] = { .name = "FallbackSourceUpdated", .arguments = fallback_source_updated_args, .n_arguments = 1 },
+ [SIGNAL_FALLBACK_SOURCE_UNSET] = { .name = "FallbackSourceUnset", .arguments = NULL, .n_arguments = 0 },
+ [SIGNAL_NEW_PLAYBACK_STREAM] = { .name = "NewPlaybackStream", .arguments = new_playback_stream_args, .n_arguments = 1 },
+ [SIGNAL_PLAYBACK_STREAM_REMOVED] = { .name = "PlaybackStreamRemoved", .arguments = playback_stream_removed_args, .n_arguments = 1 },
+ [SIGNAL_NEW_RECORD_STREAM] = { .name = "NewRecordStream", .arguments = new_record_stream_args, .n_arguments = 1 },
+ [SIGNAL_RECORD_STREAM_REMOVED] = { .name = "RecordStreamRemoved", .arguments = record_stream_removed_args, .n_arguments = 1 },
+ [SIGNAL_NEW_SAMPLE] = { .name = "NewSample", .arguments = new_sample_args, .n_arguments = 1 },
+ [SIGNAL_SAMPLE_REMOVED] = { .name = "SampleRemoved", .arguments = sample_removed_args, .n_arguments = 1 },
+ [SIGNAL_NEW_MODULE] = { .name = "NewModule", .arguments = new_module_args, .n_arguments = 1 },
+ [SIGNAL_MODULE_REMOVED] = { .name = "ModuleRemoved", .arguments = module_removed_args, .n_arguments = 1 },
+ [SIGNAL_NEW_CLIENT] = { .name = "NewClient", .arguments = new_client_args, .n_arguments = 1 },
+ [SIGNAL_CLIENT_REMOVED] = { .name = "ClientRemoved", .arguments = client_removed_args, .n_arguments = 1 },
+ [SIGNAL_NEW_EXTENSION] = { .name = "NewExtension", .arguments = new_extension_args, .n_arguments = 1 },
+ [SIGNAL_EXTENSION_REMOVED] = { .name = "ExtensionRemoved", .arguments = extension_removed_args, .n_arguments = 1 }
+};
+
+static pa_dbus_interface_info core_interface_info = {
+ .name = PA_DBUS_CORE_INTERFACE,
+ .method_handlers = method_handlers,
+ .n_method_handlers = METHOD_HANDLER_MAX,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = signals,
+ .n_signals = SIGNAL_MAX
+};
+
+static void handle_get_interface_revision(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ dbus_uint32_t interface_revision = INTERFACE_REVISION;
+
+ pa_assert(conn);
+ pa_assert(msg);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &interface_revision);
+}
+
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ const char *server_name = PACKAGE_NAME;
+
+ pa_assert(conn);
+ pa_assert(msg);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &server_name);
+}
+
+static void handle_get_version(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ const char *version = PACKAGE_VERSION;
+
+ pa_assert(conn);
+ pa_assert(msg);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &version);
+}
+
+static dbus_bool_t get_is_local(DBusConnection *conn) {
+ int conn_fd;
+
+ pa_assert(conn);
+
+ if (!dbus_connection_get_socket(conn, &conn_fd))
+ return FALSE;
+
+ return pa_socket_is_local(conn_fd);
+}
+
+static void handle_get_is_local(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ dbus_bool_t is_local;
+
+ pa_assert(conn);
+ pa_assert(msg);
+
+ is_local = get_is_local(conn);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &is_local);
+}
+
+static void handle_get_username(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ char *username = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+
+ username = pa_get_user_name_malloc();
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &username);
+
+ pa_xfree(username);
+}
+
+static void handle_get_hostname(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ char *hostname = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+
+ hostname = pa_get_host_name_malloc();
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &hostname);
+
+ pa_xfree(hostname);
+}
+
+/* Caller frees the returned array. */
+static dbus_uint32_t *get_default_channels(pa_dbusiface_core *c, unsigned *n) {
+ dbus_uint32_t *default_channels = NULL;
+ unsigned i;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = c->core->default_channel_map.channels;
+ default_channels = pa_xnew(dbus_uint32_t, *n);
+
+ for (i = 0; i < *n; ++i)
+ default_channels[i] = c->core->default_channel_map.map[i];
+
+ return default_channels;
+}
+
+static void handle_get_default_channels(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ dbus_uint32_t *default_channels = NULL;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ default_channels = get_default_channels(c, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, default_channels, n);
+
+ pa_xfree(default_channels);
+}
+
+static void handle_set_default_channels(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ DBusMessageIter array_iter;
+ pa_channel_map new_channel_map;
+ const dbus_uint32_t *default_channels;
+ int n_channels;
+ unsigned i;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(c);
+
+ pa_channel_map_init(&new_channel_map);
+
+ dbus_message_iter_recurse(iter, &array_iter);
+ dbus_message_iter_get_fixed_array(&array_iter, &default_channels, &n_channels);
+
+ if (n_channels <= 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Empty channel array.");
+ return;
+ }
+
+ if (n_channels > (int) PA_CHANNELS_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
+ "Too many channels: %i. The maximum number of channels is %u.", n_channels, PA_CHANNELS_MAX);
+ return;
+ }
+
+ new_channel_map.channels = n_channels;
+
+ for (i = 0; i < new_channel_map.channels; ++i) {
+ if (default_channels[i] >= PA_CHANNEL_POSITION_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid channel position: %u.", default_channels[i]);
+ return;
+ }
+
+ new_channel_map.map[i] = default_channels[i];
+ }
+
+ c->core->default_channel_map = new_channel_map;
+ c->core->default_sample_spec.channels = n_channels;
+
+ pa_dbus_send_empty_reply(conn, msg);
+};
+
+static void handle_get_default_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ dbus_uint32_t default_sample_format;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ default_sample_format = c->core->default_sample_spec.format;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &default_sample_format);
+}
+
+static void handle_set_default_sample_format(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ dbus_uint32_t default_sample_format;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(c);
+
+ dbus_message_iter_get_basic(iter, &default_sample_format);
+
+ if (default_sample_format >= PA_SAMPLE_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample format.");
+ return;
+ }
+
+ c->core->default_sample_spec.format = default_sample_format;
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void handle_get_default_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ dbus_uint32_t default_sample_rate;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ default_sample_rate = c->core->default_sample_spec.rate;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &default_sample_rate);
+}
+
+static void handle_set_default_sample_rate(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ dbus_uint32_t default_sample_rate;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(c);
+
+ dbus_message_iter_get_basic(iter, &default_sample_rate);
+
+ if (default_sample_rate <= 0 || default_sample_rate > PA_RATE_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample rate.");
+ return;
+ }
+
+ c->core->default_sample_spec.rate = default_sample_rate;
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_cards(pa_dbusiface_core *c, unsigned *n) {
+ const char **cards;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_card *card;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(c->cards);
+
+ if (*n == 0)
+ return NULL;
+
+ cards = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(card, c->cards, state)
+ cards[i++] = pa_dbusiface_card_get_path(card);
+
+ return cards;
+}
+
+static void handle_get_cards(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char **cards;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ cards = get_cards(c, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, cards, n);
+
+ pa_xfree(cards);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_sinks(pa_dbusiface_core *c, unsigned *n) {
+ const char **sinks;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_device *sink;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(c->sinks_by_index);
+
+ if (*n == 0)
+ return NULL;
+
+ sinks = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(sink, c->sinks_by_index, state)
+ sinks[i++] = pa_dbusiface_device_get_path(sink);
+
+ return sinks;
+}
+
+static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char **sinks;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ sinks = get_sinks(c, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, sinks, n);
+
+ pa_xfree(sinks);
+}
+
+static void handle_get_fallback_sink(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ pa_dbusiface_device *fallback_sink;
+ const char *object_path;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ if (!c->fallback_sink) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "There are no sinks, and therefore no fallback sink either.");
+ return;
+ }
+
+ pa_assert_se((fallback_sink = pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(c->fallback_sink->index))));
+ object_path = pa_dbusiface_device_get_path(fallback_sink);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_set_fallback_sink(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ pa_dbusiface_device *fallback_sink;
+ const char *object_path;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(c);
+
+ if (!c->fallback_sink) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "There are no sinks, and therefore no fallback sink either.");
+ return;
+ }
+
+ dbus_message_iter_get_basic(iter, &object_path);
+
+ if (!(fallback_sink = pa_hashmap_get(c->sinks_by_path, object_path))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such sink.", object_path);
+ return;
+ }
+
+ pa_namereg_set_default_sink(c->core, pa_dbusiface_device_get_sink(fallback_sink));
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_sources(pa_dbusiface_core *c, unsigned *n) {
+ const char **sources;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_device *source;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(c->sources_by_index);
+
+ if (*n == 0)
+ return NULL;
+
+ sources = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(source, c->sources_by_index, state)
+ sources[i++] = pa_dbusiface_device_get_path(source);
+
+ return sources;
+}
+
+static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char **sources;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ sources = get_sources(c, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, sources, n);
+
+ pa_xfree(sources);
+}
+
+static void handle_get_fallback_source(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ pa_dbusiface_device *fallback_source;
+ const char *object_path;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ if (!c->fallback_source) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "There are no sources, and therefore no fallback source either.");
+ return;
+ }
+
+ pa_assert_se((fallback_source = pa_hashmap_get(c->sources_by_index, PA_UINT32_TO_PTR(c->fallback_source->index))));
+ object_path = pa_dbusiface_device_get_path(fallback_source);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_set_fallback_source(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ pa_dbusiface_device *fallback_source;
+ const char *object_path;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(c);
+
+ if (!c->fallback_source) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "There are no sources, and therefore no fallback source either.");
+ return;
+ }
+
+ dbus_message_iter_get_basic(iter, &object_path);
+
+ if (!(fallback_source = pa_hashmap_get(c->sources_by_path, object_path))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such source.", object_path);
+ return;
+ }
+
+ pa_namereg_set_default_source(c->core, pa_dbusiface_device_get_source(fallback_source));
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_playback_streams(pa_dbusiface_core *c, unsigned *n) {
+ const char **streams;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_stream *stream;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(c->playback_streams);
+
+ if (*n == 0)
+ return NULL;
+
+ streams = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(stream, c->playback_streams, state)
+ streams[i++] = pa_dbusiface_stream_get_path(stream);
+
+ return streams;
+}
+
+static void handle_get_playback_streams(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char **playback_streams;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ playback_streams = get_playback_streams(c, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, playback_streams, n);
+
+ pa_xfree(playback_streams);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_record_streams(pa_dbusiface_core *c, unsigned *n) {
+ const char **streams;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_stream *stream;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(c->record_streams);
+
+ if (*n == 0)
+ return NULL;
+
+ streams = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(stream, c->record_streams, state)
+ streams[i++] = pa_dbusiface_stream_get_path(stream);
+
+ return streams;
+}
+
+static void handle_get_record_streams(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char **record_streams;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ record_streams = get_record_streams(c, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, record_streams, n);
+
+ pa_xfree(record_streams);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_samples(pa_dbusiface_core *c, unsigned *n) {
+ const char **samples;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_sample *sample;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(c->samples);
+
+ if (*n == 0)
+ return NULL;
+
+ samples = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(sample, c->samples, state)
+ samples[i++] = pa_dbusiface_sample_get_path(sample);
+
+ return samples;
+}
+
+static void handle_get_samples(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char **samples;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ samples = get_samples(c, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, samples, n);
+
+ pa_xfree(samples);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_modules(pa_dbusiface_core *c, unsigned *n) {
+ const char **modules;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_module *module;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(c->modules);
+
+ if (*n == 0)
+ return NULL;
+
+ modules = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(module, c->modules, state)
+ modules[i++] = pa_dbusiface_module_get_path(module);
+
+ return modules;
+}
+
+static void handle_get_modules(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char **modules;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ modules = get_modules(c, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, modules, n);
+
+ pa_xfree(modules);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_clients(pa_dbusiface_core *c, unsigned *n) {
+ const char **clients;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_client *client;
+
+ pa_assert(c);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(c->clients);
+
+ if (*n == 0)
+ return NULL;
+
+ clients = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(client, c->clients, state)
+ clients[i++] = pa_dbusiface_client_get_path(client);
+
+ return clients;
+}
+
+static void handle_get_clients(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char **clients;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ clients = get_clients(c, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, clients, n);
+
+ pa_xfree(clients);
+}
+
+static const char *get_my_client(pa_dbusiface_core *c, DBusConnection *conn) {
+ pa_client *my_client;
+
+ pa_assert(c);
+ pa_assert(conn);
+
+ pa_assert_se((my_client = pa_dbus_protocol_get_client(c->dbus_protocol, conn)));
+
+ return pa_dbusiface_client_get_path(pa_hashmap_get(c->clients, PA_UINT32_TO_PTR(my_client->index)));
+}
+
+static void handle_get_my_client(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char *my_client;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ my_client = get_my_client(c, conn);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &my_client);
+}
+
+static void handle_get_extensions(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char **extensions;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ extensions = pa_dbus_protocol_get_extensions(c->dbus_protocol, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_STRING, extensions, n);
+
+ pa_xfree(extensions);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ dbus_uint32_t interface_revision;
+ const char *server_name;
+ const char *version;
+ dbus_bool_t is_local;
+ char *username;
+ char *hostname;
+ dbus_uint32_t *default_channels;
+ unsigned n_default_channels;
+ dbus_uint32_t default_sample_format;
+ dbus_uint32_t default_sample_rate;
+ const char **cards;
+ unsigned n_cards;
+ const char **sinks;
+ unsigned n_sinks;
+ const char *fallback_sink;
+ const char **sources;
+ unsigned n_sources;
+ const char *fallback_source;
+ const char **playback_streams;
+ unsigned n_playback_streams;
+ const char **record_streams;
+ unsigned n_record_streams;
+ const char **samples;
+ unsigned n_samples;
+ const char **modules;
+ unsigned n_modules;
+ const char **clients;
+ unsigned n_clients;
+ const char *my_client;
+ const char **extensions;
+ unsigned n_extensions;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ interface_revision = INTERFACE_REVISION;
+ server_name = PACKAGE_NAME;
+ version = PACKAGE_VERSION;
+ is_local = get_is_local(conn);
+ username = pa_get_user_name_malloc();
+ hostname = pa_get_host_name_malloc();
+ default_channels = get_default_channels(c, &n_default_channels);
+ default_sample_format = c->core->default_sample_spec.format;
+ default_sample_rate = c->core->default_sample_spec.rate;
+ cards = get_cards(c, &n_cards);
+ sinks = get_sinks(c, &n_sinks);
+ fallback_sink = c->fallback_sink
+ ? pa_dbusiface_device_get_path(pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(c->fallback_sink->index)))
+ : NULL;
+ sources = get_sources(c, &n_sources);
+ fallback_source = c->fallback_source
+ ? pa_dbusiface_device_get_path(pa_hashmap_get(c->sources_by_index,
+ PA_UINT32_TO_PTR(c->fallback_source->index)))
+ : NULL;
+ playback_streams = get_playback_streams(c, &n_playback_streams);
+ record_streams = get_record_streams(c, &n_record_streams);
+ samples = get_samples(c, &n_samples);
+ modules = get_modules(c, &n_modules);
+ clients = get_clients(c, &n_clients);
+ my_client = get_my_client(c, conn);
+ extensions = pa_dbus_protocol_get_extensions(c->dbus_protocol, &n_extensions);
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INTERFACE_REVISION].property_name, DBUS_TYPE_UINT32, &interface_revision);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &server_name);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_VERSION].property_name, DBUS_TYPE_STRING, &version);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_IS_LOCAL].property_name, DBUS_TYPE_BOOLEAN, &is_local);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_USERNAME].property_name, DBUS_TYPE_STRING, &username);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_HOSTNAME].property_name, DBUS_TYPE_STRING, &hostname);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEFAULT_CHANNELS].property_name, DBUS_TYPE_UINT32, default_channels, n_default_channels);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEFAULT_SAMPLE_FORMAT].property_name, DBUS_TYPE_UINT32, &default_sample_format);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEFAULT_SAMPLE_RATE].property_name, DBUS_TYPE_UINT32, &default_sample_rate);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CARDS].property_name, DBUS_TYPE_OBJECT_PATH, cards, n_cards);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SINKS].property_name, DBUS_TYPE_OBJECT_PATH, sinks, n_sinks);
+
+ if (fallback_sink)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_FALLBACK_SINK].property_name, DBUS_TYPE_OBJECT_PATH, &fallback_sink);
+
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SOURCES].property_name, DBUS_TYPE_OBJECT_PATH, sources, n_sources);
+
+ if (fallback_source)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_FALLBACK_SOURCE].property_name, DBUS_TYPE_OBJECT_PATH, &fallback_source);
+
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PLAYBACK_STREAMS].property_name, DBUS_TYPE_OBJECT_PATH, playback_streams, n_playback_streams);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_RECORD_STREAMS].property_name, DBUS_TYPE_OBJECT_PATH, record_streams, n_record_streams);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLES].property_name, DBUS_TYPE_OBJECT_PATH, samples, n_samples);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_MODULES].property_name, DBUS_TYPE_OBJECT_PATH, modules, n_modules);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CLIENTS].property_name, DBUS_TYPE_OBJECT_PATH, clients, n_clients);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_MY_CLIENT].property_name, DBUS_TYPE_OBJECT_PATH, &my_client);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_EXTENSIONS].property_name, DBUS_TYPE_STRING, extensions, n_extensions);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+
+ pa_xfree(username);
+ pa_xfree(hostname);
+ pa_xfree(default_channels);
+ pa_xfree(cards);
+ pa_xfree(sinks);
+ pa_xfree(sources);
+ pa_xfree(playback_streams);
+ pa_xfree(record_streams);
+ pa_xfree(samples);
+ pa_xfree(modules);
+ pa_xfree(clients);
+ pa_xfree(extensions);
+}
+
+static void handle_get_card_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ char *card_name;
+ pa_card *card;
+ pa_dbusiface_card *dbus_card;
+ const char *object_path;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &card_name, DBUS_TYPE_INVALID));
+
+ if (!(card = pa_namereg_get(c->core, card_name, PA_NAMEREG_CARD))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "No such card.");
+ return;
+ }
+
+ pa_assert_se((dbus_card = pa_hashmap_get(c->cards, PA_UINT32_TO_PTR(card->index))));
+
+ object_path = pa_dbusiface_card_get_path(dbus_card);
+
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_get_sink_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ char *sink_name;
+ pa_sink *sink;
+ pa_dbusiface_device *dbus_sink;
+ const char *object_path;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &sink_name, DBUS_TYPE_INVALID));
+
+ if (!(sink = pa_namereg_get(c->core, sink_name, PA_NAMEREG_SINK))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such sink.", sink_name);
+ return;
+ }
+
+ pa_assert_se((dbus_sink = pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(sink->index))));
+
+ object_path = pa_dbusiface_device_get_path(dbus_sink);
+
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_get_source_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ char *source_name;
+ pa_source *source;
+ pa_dbusiface_device *dbus_source;
+ const char *object_path;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &source_name, DBUS_TYPE_INVALID));
+
+ if (!(source = pa_namereg_get(c->core, source_name, PA_NAMEREG_SOURCE))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such source.", source_name);
+ return;
+ }
+
+ pa_assert_se((dbus_source = pa_hashmap_get(c->sources_by_index, PA_UINT32_TO_PTR(source->index))));
+
+ object_path = pa_dbusiface_device_get_path(dbus_source);
+
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_get_sample_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ char *sample_name;
+ pa_scache_entry *sample;
+ pa_dbusiface_sample *dbus_sample;
+ const char *object_path;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &sample_name, DBUS_TYPE_INVALID));
+
+ if (!(sample = pa_namereg_get(c->core, sample_name, PA_NAMEREG_SAMPLE))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "No such sample.");
+ return;
+ }
+
+ pa_assert_se((dbus_sample = pa_hashmap_get(c->samples, PA_UINT32_TO_PTR(sample->index))));
+
+ object_path = pa_dbusiface_sample_get_path(dbus_sample);
+
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_upload_sample(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ DBusMessageIter msg_iter;
+ DBusMessageIter array_iter;
+ const char *name;
+ dbus_uint32_t sample_format;
+ dbus_uint32_t sample_rate;
+ const dbus_uint32_t *channels;
+ int n_channels;
+ const dbus_uint32_t *default_volume;
+ int n_volume_entries;
+ pa_proplist *property_list;
+ const uint8_t *data;
+ int data_length;
+ int i;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_memchunk chunk;
+ uint32_t idx;
+ pa_dbusiface_sample *dbus_sample = NULL;
+ pa_scache_entry *sample = NULL;
+ const char *object_path;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ chunk.memblock = NULL;
+
+ pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &name);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &sample_format);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &sample_rate);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_recurse(&msg_iter, &array_iter);
+ dbus_message_iter_get_fixed_array(&array_iter, &channels, &n_channels);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_recurse(&msg_iter, &array_iter);
+ dbus_message_iter_get_fixed_array(&array_iter, &default_volume, &n_volume_entries);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ if (!(property_list = pa_dbus_get_proplist_arg(conn, msg, &msg_iter)))
+ return;
+
+ dbus_message_iter_recurse(&msg_iter, &array_iter);
+ dbus_message_iter_get_fixed_array(&array_iter, &data, &data_length);
+
+ if (sample_format >= PA_SAMPLE_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample format.");
+ goto finish;
+ }
+
+ if (sample_rate <= 0 || sample_rate > PA_RATE_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample rate.");
+ goto finish;
+ }
+
+ if (n_channels <= 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Empty channel map.");
+ goto finish;
+ }
+
+ if (n_channels > (int) PA_CHANNELS_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
+ "Too many channels: %i. The maximum is %u.", n_channels, PA_CHANNELS_MAX);
+ goto finish;
+ }
+
+ for (i = 0; i < n_channels; ++i) {
+ if (channels[i] >= PA_CHANNEL_POSITION_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid channel position.");
+ goto finish;
+ }
+ }
+
+ if (n_volume_entries != 0 && n_volume_entries != n_channels) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
+ "The channels and default_volume arguments have different number of elements (%i and %i, resp).",
+ n_channels, n_volume_entries);
+ goto finish;
+ }
+
+ for (i = 0; i < n_volume_entries; ++i) {
+ if (!PA_VOLUME_IS_VALID(default_volume[i])) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid volume: %u.", default_volume[i]);
+ goto finish;
+ }
+ }
+
+ if (data_length == 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Empty data.");
+ goto finish;
+ }
+
+ if (data_length > PA_SCACHE_ENTRY_SIZE_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
+ "Too big sample: %i bytes. The maximum sample length is %u bytes.",
+ data_length, PA_SCACHE_ENTRY_SIZE_MAX);
+ goto finish;
+ }
+
+ ss.format = sample_format;
+ ss.rate = sample_rate;
+ ss.channels = n_channels;
+
+ pa_assert(pa_sample_spec_valid(&ss));
+
+ if (!pa_frame_aligned(data_length, &ss)) {
+ char buf[PA_SAMPLE_SPEC_SNPRINT_MAX];
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
+ "The sample length (%i bytes) doesn't align with the sample format and channels (%s).",
+ data_length, pa_sample_spec_snprint(buf, sizeof(buf), &ss));
+ goto finish;
+ }
+
+ map.channels = n_channels;
+ for (i = 0; i < n_channels; ++i)
+ map.map[i] = channels[i];
+
+ chunk.memblock = pa_memblock_new(c->core->mempool, data_length);
+ chunk.index = 0;
+ chunk.length = data_length;
+
+ memcpy(pa_memblock_acquire(chunk.memblock), data, data_length);
+ pa_memblock_release(chunk.memblock);
+
+ if (pa_scache_add_item(c->core, name, &ss, &map, &chunk, property_list, &idx) < 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Adding the sample failed.");
+ goto finish;
+ }
+
+ sample = pa_idxset_get_by_index(c->core->scache, idx);
+
+ if (n_volume_entries > 0) {
+ sample->volume.channels = n_channels;
+ for (i = 0; i < n_volume_entries; ++i)
+ sample->volume.values[i] = default_volume[i];
+ sample->volume_is_set = TRUE;
+ } else {
+ sample->volume_is_set = FALSE;
+ }
+
+ dbus_sample = pa_dbusiface_sample_new(c, sample);
+ pa_hashmap_put(c->samples, PA_UINT32_TO_PTR(idx), dbus_sample);
+
+ object_path = pa_dbusiface_sample_get_path(dbus_sample);
+
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+
+finish:
+ if (property_list)
+ pa_proplist_free(property_list);
+
+ if (chunk.memblock)
+ pa_memblock_unref(chunk.memblock);
+}
+
+static pa_bool_t contains_space(const char *string) {
+ const char *p;
+
+ pa_assert(string);
+
+ for (p = string; *p; ++p) {
+ if (isspace(*p))
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static void handle_load_module(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ DBusMessageIter dict_entry_iter;
+ char *name = NULL;
+ const char *key = NULL;
+ const char *value = NULL;
+ char *escaped_value = NULL;
+ pa_strbuf *arg_buffer = NULL;
+ char *arg_string = NULL;
+ pa_module *module = NULL;
+ pa_dbusiface_module *dbus_module = NULL;
+ const char *object_path = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ if (c->core->disallow_module_loading) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_ACCESS_DENIED, "The server is configured to disallow module loading.");
+ return;
+ }
+
+ pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &name);
+
+ arg_buffer = pa_strbuf_new();
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_recurse(&msg_iter, &dict_iter);
+
+ while (dbus_message_iter_get_arg_type(&dict_iter) != DBUS_TYPE_INVALID) {
+ if (!pa_strbuf_isempty(arg_buffer))
+ pa_strbuf_putc(arg_buffer, ' ');
+
+ dbus_message_iter_recurse(&dict_iter, &dict_entry_iter);
+
+ dbus_message_iter_get_basic(&dict_entry_iter, &key);
+
+ if (strlen(key) <= 0 || !pa_ascii_valid(key) || contains_space(key)) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid module argument name: %s", key);
+ goto finish;
+ }
+
+ pa_assert_se(dbus_message_iter_next(&dict_entry_iter));
+ dbus_message_iter_get_basic(&dict_entry_iter, &value);
+
+ escaped_value = pa_escape(value, "\"");
+ pa_strbuf_printf(arg_buffer, "%s=\"%s\"", key, escaped_value);
+ pa_xfree(escaped_value);
+
+ dbus_message_iter_next(&dict_iter);
+ }
+
+ arg_string = pa_strbuf_tostring(arg_buffer);
+
+ if (!(module = pa_module_load(c->core, name, arg_string))) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Failed to load module.");
+ goto finish;
+ }
+
+ dbus_module = pa_dbusiface_module_new(module);
+ pa_hashmap_put(c->modules, PA_UINT32_TO_PTR(module->index), dbus_module);
+
+ object_path = pa_dbusiface_module_get_path(dbus_module);
+
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+
+finish:
+ if (arg_buffer)
+ pa_strbuf_free(arg_buffer);
+
+ pa_xfree(arg_string);
+}
+
+static void handle_exit(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ if (c->core->disallow_exit) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_ACCESS_DENIED, "The server is configured to disallow exiting.");
+ return;
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ pa_core_exit(c->core, FALSE, 0);
+}
+
+static void handle_listen_for_signal(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char *signal_str;
+ char **objects = NULL;
+ int n_objects;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL,
+ DBUS_TYPE_STRING, &signal_str,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &objects, &n_objects,
+ DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_add_signal_listener(c->dbus_protocol, conn, *signal_str ? signal_str : NULL, objects, n_objects);
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ dbus_free_string_array(objects);
+}
+
+static void handle_stop_listening_for_signal(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ const char *signal_str;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &signal_str, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_remove_signal_listener(c->dbus_protocol, conn, *signal_str ? signal_str : NULL);
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ pa_dbusiface_core *c = userdata;
+ pa_dbusiface_card *card_iface = NULL;
+ pa_dbusiface_device *device_iface = NULL;
+ pa_dbusiface_stream *stream_iface = NULL;
+ pa_dbusiface_sample *sample_iface = NULL;
+ pa_dbusiface_module *module_iface = NULL;
+ pa_dbusiface_client *client_iface = NULL;
+ DBusMessage *signal_msg = NULL;
+ const char *object_path = NULL;
+ pa_sink *new_fallback_sink = NULL;
+ pa_source *new_fallback_source = NULL;
+
+ pa_assert(c);
+
+ switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
+ case PA_SUBSCRIPTION_EVENT_SERVER:
+ new_fallback_sink = pa_namereg_get_default_sink(core);
+ new_fallback_source = pa_namereg_get_default_source(core);
+
+ if (c->fallback_sink != new_fallback_sink) {
+ if (c->fallback_sink)
+ pa_sink_unref(c->fallback_sink);
+ c->fallback_sink = new_fallback_sink ? pa_sink_ref(new_fallback_sink) : NULL;
+
+ if (c->fallback_sink) {
+ pa_assert_se(device_iface = pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(c->fallback_sink->index)));
+ object_path = pa_dbusiface_device_get_path(device_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_FALLBACK_SINK_UPDATED].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+
+ } else {
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_FALLBACK_SINK_UNSET].name)));
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+ }
+
+ if (c->fallback_source != new_fallback_source) {
+ if (c->fallback_source)
+ pa_source_unref(c->fallback_source);
+ c->fallback_source = new_fallback_source ? pa_source_ref(new_fallback_source) : NULL;
+
+ if (c->fallback_source) {
+ pa_assert_se(device_iface = pa_hashmap_get(c->sources_by_index, PA_UINT32_TO_PTR(c->fallback_source->index)));
+ object_path = pa_dbusiface_device_get_path(device_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_FALLBACK_SOURCE_UPDATED].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+
+ } else {
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_FALLBACK_SOURCE_UNSET].name)));
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+ }
+ break;
+
+ case PA_SUBSCRIPTION_EVENT_CARD:
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
+ if (!(card_iface = pa_hashmap_get(c->cards, PA_UINT32_TO_PTR(idx)))) {
+ pa_card *card = NULL;
+
+ if (!(card = pa_idxset_get_by_index(core->cards, idx)))
+ return; /* The card was removed immediately after creation. */
+
+ card_iface = pa_dbusiface_card_new(c, card);
+ pa_hashmap_put(c->cards, PA_UINT32_TO_PTR(idx), card_iface);
+ }
+
+ object_path = pa_dbusiface_card_get_path(card_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_NEW_CARD].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
+ if (!(card_iface = pa_hashmap_remove(c->cards, PA_UINT32_TO_PTR(idx))))
+ return;
+
+ object_path = pa_dbusiface_card_get_path(card_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_CARD_REMOVED].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbusiface_card_free(card_iface);
+ }
+ break;
+
+ case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
+ pa_sink_input *sink_input = NULL;
+
+ if (!(sink_input = pa_idxset_get_by_index(core->sink_inputs, idx)))
+ return; /* The sink input was removed immediately after creation. */
+
+ if (!(stream_iface = pa_hashmap_get(c->playback_streams, PA_UINT32_TO_PTR(idx)))) {
+ stream_iface = pa_dbusiface_stream_new_playback(c, sink_input);
+ pa_hashmap_put(c->playback_streams, PA_UINT32_TO_PTR(idx), stream_iface);
+ }
+
+ object_path = pa_dbusiface_stream_get_path(stream_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_NEW_PLAYBACK_STREAM].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
+ if (!(stream_iface = pa_hashmap_remove(c->playback_streams, PA_UINT32_TO_PTR(idx))))
+ return;
+
+ object_path = pa_dbusiface_stream_get_path(stream_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_PLAYBACK_STREAM_REMOVED].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbusiface_stream_free(stream_iface);
+ }
+ break;
+
+ case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
+ pa_source_output *source_output = NULL;
+
+ if (!(source_output = pa_idxset_get_by_index(core->source_outputs, idx)))
+ return; /* The source output was removed immediately after creation. */
+
+ if (!(stream_iface = pa_hashmap_get(c->record_streams, PA_UINT32_TO_PTR(idx)))) {
+ stream_iface = pa_dbusiface_stream_new_record(c, source_output);
+ pa_hashmap_put(c->record_streams, PA_UINT32_TO_PTR(idx), stream_iface);
+ }
+
+ object_path = pa_dbusiface_stream_get_path(stream_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_NEW_RECORD_STREAM].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
+ if (!(stream_iface = pa_hashmap_remove(c->record_streams, PA_UINT32_TO_PTR(idx))))
+ return;
+
+ object_path = pa_dbusiface_stream_get_path(stream_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_RECORD_STREAM_REMOVED].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbusiface_stream_free(stream_iface);
+ }
+ break;
+
+ case PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE:
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
+ pa_scache_entry *sample = NULL;
+
+ if (!(sample = pa_idxset_get_by_index(core->scache, idx)))
+ return; /* The sample was removed immediately after creation. */
+
+ if (!(sample_iface = pa_hashmap_get(c->samples, PA_UINT32_TO_PTR(idx)))) {
+ sample_iface = pa_dbusiface_sample_new(c, sample);
+ pa_hashmap_put(c->samples, PA_UINT32_TO_PTR(idx), sample_iface);
+ }
+
+ object_path = pa_dbusiface_sample_get_path(sample_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_NEW_SAMPLE].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
+ if (!(sample_iface = pa_hashmap_remove(c->samples, PA_UINT32_TO_PTR(idx))))
+ return;
+
+ object_path = pa_dbusiface_sample_get_path(sample_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_SAMPLE_REMOVED].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbusiface_sample_free(sample_iface);
+ }
+ break;
+
+ case PA_SUBSCRIPTION_EVENT_MODULE:
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
+ pa_module *module = NULL;
+
+ if (!(module = pa_idxset_get_by_index(core->modules, idx)))
+ return; /* The module was removed immediately after creation. */
+
+ if (!(module_iface = pa_hashmap_get(c->modules, PA_UINT32_TO_PTR(idx)))) {
+ module_iface = pa_dbusiface_module_new(module);
+ pa_hashmap_put(c->modules, PA_UINT32_TO_PTR(idx), module_iface);
+ }
+
+ object_path = pa_dbusiface_module_get_path(module_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_NEW_MODULE].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
+ if (!(module_iface = pa_hashmap_remove(c->modules, PA_UINT32_TO_PTR(idx))))
+ return;
+
+ object_path = pa_dbusiface_module_get_path(module_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_MODULE_REMOVED].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbusiface_module_free(module_iface);
+ }
+ break;
+
+ case PA_SUBSCRIPTION_EVENT_CLIENT:
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
+ pa_client *client = NULL;
+
+ if (!(client = pa_idxset_get_by_index(core->clients, idx)))
+ return; /* The client was removed immediately after creation. */
+
+ if (!(client_iface = pa_hashmap_get(c->clients, PA_UINT32_TO_PTR(idx)))) {
+ client_iface = pa_dbusiface_client_new(c, client);
+ pa_hashmap_put(c->clients, PA_UINT32_TO_PTR(idx), client_iface);
+ }
+
+ object_path = pa_dbusiface_client_get_path(client_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_NEW_CLIENT].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
+ if (!(client_iface = pa_hashmap_remove(c->clients, PA_UINT32_TO_PTR(idx))))
+ return;
+
+ object_path = pa_dbusiface_client_get_path(client_iface);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_CLIENT_REMOVED].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbusiface_client_free(client_iface);
+ }
+ break;
+ }
+
+ if (signal_msg) {
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ }
+}
+
+static pa_hook_result_t sink_put_cb(void *hook_data, void *call_data, void *slot_data) {
+ pa_dbusiface_core *c = slot_data;
+ pa_sink *s = call_data;
+ pa_dbusiface_device *d = NULL;
+ const char *object_path = NULL;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(c);
+ pa_assert(s);
+
+ d = pa_dbusiface_device_new_sink(c, s);
+ object_path = pa_dbusiface_device_get_path(d);
+
+ pa_assert_se(pa_hashmap_put(c->sinks_by_index, PA_UINT32_TO_PTR(s->index), d) >= 0);
+ pa_assert_se(pa_hashmap_put(c->sinks_by_path, object_path, d) >= 0);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_NEW_SINK].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_unlink_cb(void *hook_data, void *call_data, void *slot_data) {
+ pa_dbusiface_core *c = slot_data;
+ pa_sink *s = call_data;
+ pa_dbusiface_device *d = NULL;
+ const char *object_path = NULL;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(c);
+ pa_assert(s);
+
+ pa_assert_se(d = pa_hashmap_remove(c->sinks_by_index, PA_UINT32_TO_PTR(s->index)));
+ object_path = pa_dbusiface_device_get_path(d);
+ pa_assert_se(pa_hashmap_remove(c->sinks_by_path, object_path));
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_SINK_REMOVED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+
+ pa_dbusiface_device_free(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_put_cb(void *hook_data, void *call_data, void *slot_data) {
+ pa_dbusiface_core *c = slot_data;
+ pa_source *s = call_data;
+ pa_dbusiface_device *d = NULL;
+ const char *object_path = NULL;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(c);
+ pa_assert(s);
+
+ d = pa_dbusiface_device_new_source(c, s);
+ object_path = pa_dbusiface_device_get_path(d);
+
+ pa_assert_se(pa_hashmap_put(c->sources_by_index, PA_UINT32_TO_PTR(s->index), d) >= 0);
+ pa_assert_se(pa_hashmap_put(c->sources_by_path, object_path, d) >= 0);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_NEW_SOURCE].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_unlink_cb(void *hook_data, void *call_data, void *slot_data) {
+ pa_dbusiface_core *c = slot_data;
+ pa_source *s = call_data;
+ pa_dbusiface_device *d = NULL;
+ const char *object_path = NULL;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(c);
+ pa_assert(s);
+
+ pa_assert_se(d = pa_hashmap_remove(c->sources_by_index, PA_UINT32_TO_PTR(s->index)));
+ object_path = pa_dbusiface_device_get_path(d);
+ pa_assert_se(pa_hashmap_remove(c->sources_by_path, object_path));
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_SOURCE_REMOVED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+
+ pa_dbusiface_device_free(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t extension_registered_cb(void *hook_data, void *call_data, void *slot_data) {
+ pa_dbusiface_core *c = slot_data;
+ const char *ext_name = call_data;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(c);
+ pa_assert(ext_name);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_NEW_EXTENSION].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_STRING, &ext_name, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t extension_unregistered_cb(void *hook_data, void *call_data, void *slot_data) {
+ pa_dbusiface_core *c = slot_data;
+ const char *ext_name = call_data;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(c);
+ pa_assert(ext_name);
+
+ pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+ PA_DBUS_CORE_INTERFACE,
+ signals[SIGNAL_EXTENSION_REMOVED].name)));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_STRING, &ext_name, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+
+ return PA_HOOK_OK;
+}
+
+pa_dbusiface_core *pa_dbusiface_core_new(pa_core *core) {
+ pa_dbusiface_core *c;
+ pa_card *card;
+ pa_sink *sink;
+ pa_source *source;
+ pa_dbusiface_device *device;
+ pa_sink_input *sink_input;
+ pa_source_output *source_output;
+ pa_scache_entry *sample;
+ pa_module *module;
+ pa_client *client;
+ uint32_t idx;
+
+ pa_assert(core);
+
+ c = pa_xnew(pa_dbusiface_core, 1);
+ c->core = core;
+ c->subscription = pa_subscription_new(core, PA_SUBSCRIPTION_MASK_ALL, subscription_cb, c);
+ c->dbus_protocol = pa_dbus_protocol_get(core);
+ c->cards = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ c->sinks_by_index = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ c->sinks_by_path = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ c->sources_by_index = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ c->sources_by_path = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ c->playback_streams = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ c->record_streams = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ c->samples = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ c->modules = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ c->clients = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ c->fallback_sink = pa_namereg_get_default_sink(core);
+ c->fallback_source = pa_namereg_get_default_source(core);
+ c->sink_put_slot = pa_hook_connect(&core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_NORMAL, sink_put_cb, c);
+ c->sink_unlink_slot = pa_hook_connect(&core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_NORMAL, sink_unlink_cb, c);
+ c->source_put_slot = pa_hook_connect(&core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_NORMAL, source_put_cb, c);
+ c->source_unlink_slot = pa_hook_connect(&core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_NORMAL, source_unlink_cb, c);
+ c->extension_registered_slot = pa_dbus_protocol_hook_connect(c->dbus_protocol,
+ PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED,
+ PA_HOOK_NORMAL,
+ extension_registered_cb,
+ c);
+ c->extension_unregistered_slot = pa_dbus_protocol_hook_connect(c->dbus_protocol,
+ PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED,
+ PA_HOOK_NORMAL,
+ extension_unregistered_cb,
+ c);
+ c->memstats = pa_dbusiface_memstats_new(c, core);
+
+ if (c->fallback_sink)
+ pa_sink_ref(c->fallback_sink);
+ if (c->fallback_source)
+ pa_source_ref(c->fallback_source);
+
+ PA_IDXSET_FOREACH(card, core->cards, idx)
+ pa_hashmap_put(c->cards, PA_UINT32_TO_PTR(idx), pa_dbusiface_card_new(c, card));
+
+ PA_IDXSET_FOREACH(sink, core->sinks, idx) {
+ device = pa_dbusiface_device_new_sink(c, sink);
+ pa_hashmap_put(c->sinks_by_index, PA_UINT32_TO_PTR(idx), device);
+ pa_hashmap_put(c->sinks_by_path, pa_dbusiface_device_get_path(device), device);
+ }
+
+ PA_IDXSET_FOREACH(source, core->sources, idx) {
+ device = pa_dbusiface_device_new_source(c, source);
+ pa_hashmap_put(c->sources_by_index, PA_UINT32_TO_PTR(idx), device);
+ pa_hashmap_put(c->sources_by_path, pa_dbusiface_device_get_path(device), device);
+ }
+
+ PA_IDXSET_FOREACH(sink_input, core->sink_inputs, idx)
+ pa_hashmap_put(c->playback_streams, PA_UINT32_TO_PTR(idx), pa_dbusiface_stream_new_playback(c, sink_input));
+
+ PA_IDXSET_FOREACH(source_output, core->source_outputs, idx)
+ pa_hashmap_put(c->record_streams, PA_UINT32_TO_PTR(idx), pa_dbusiface_stream_new_record(c, source_output));
+
+ PA_IDXSET_FOREACH(sample, core->scache, idx)
+ pa_hashmap_put(c->samples, PA_UINT32_TO_PTR(idx), pa_dbusiface_sample_new(c, sample));
+
+ PA_IDXSET_FOREACH(module, core->modules, idx)
+ pa_hashmap_put(c->modules, PA_UINT32_TO_PTR(idx), pa_dbusiface_module_new(module));
+
+ PA_IDXSET_FOREACH(client, core->clients, idx)
+ pa_hashmap_put(c->clients, PA_UINT32_TO_PTR(idx), pa_dbusiface_client_new(c, client));
+
+ pa_assert_se(pa_dbus_protocol_add_interface(c->dbus_protocol, PA_DBUS_CORE_OBJECT_PATH, &core_interface_info, c) >= 0);
+
+ return c;
+}
+
+static void free_card_cb(void *p, void *userdata) {
+ pa_dbusiface_card *c = p;
+
+ pa_assert(c);
+
+ pa_dbusiface_card_free(c);
+}
+
+static void free_device_cb(void *p, void *userdata) {
+ pa_dbusiface_device *d = p;
+
+ pa_assert(d);
+
+ pa_dbusiface_device_free(d);
+}
+
+static void free_stream_cb(void *p, void *userdata) {
+ pa_dbusiface_stream *s = p;
+
+ pa_assert(s);
+
+ pa_dbusiface_stream_free(s);
+}
+
+static void free_sample_cb(void *p, void *userdata) {
+ pa_dbusiface_sample *s = p;
+
+ pa_assert(s);
+
+ pa_dbusiface_sample_free(s);
+}
+
+static void free_module_cb(void *p, void *userdata) {
+ pa_dbusiface_module *m = p;
+
+ pa_assert(m);
+
+ pa_dbusiface_module_free(m);
+}
+
+static void free_client_cb(void *p, void *userdata) {
+ pa_dbusiface_client *c = p;
+
+ pa_assert(c);
+
+ pa_dbusiface_client_free(c);
+}
+
+void pa_dbusiface_core_free(pa_dbusiface_core *c) {
+ pa_assert(c);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(c->dbus_protocol, PA_DBUS_CORE_OBJECT_PATH, core_interface_info.name) >= 0);
+
+ pa_subscription_free(c->subscription);
+ pa_hashmap_free(c->cards, free_card_cb, NULL);
+ pa_hashmap_free(c->sinks_by_index, free_device_cb, NULL);
+ pa_hashmap_free(c->sinks_by_path, NULL, NULL);
+ pa_hashmap_free(c->sources_by_index, free_device_cb, NULL);
+ pa_hashmap_free(c->sources_by_path, NULL, NULL);
+ pa_hashmap_free(c->playback_streams, free_stream_cb, NULL);
+ pa_hashmap_free(c->record_streams, free_stream_cb, NULL);
+ pa_hashmap_free(c->samples, free_sample_cb, NULL);
+ pa_hashmap_free(c->modules, free_module_cb, NULL);
+ pa_hashmap_free(c->clients, free_client_cb, NULL);
+ pa_hook_slot_free(c->sink_put_slot);
+ pa_hook_slot_free(c->sink_unlink_slot);
+ pa_hook_slot_free(c->source_put_slot);
+ pa_hook_slot_free(c->source_unlink_slot);
+ pa_hook_slot_free(c->extension_registered_slot);
+ pa_hook_slot_free(c->extension_unregistered_slot);
+ pa_dbusiface_memstats_free(c->memstats);
+
+ if (c->fallback_sink)
+ pa_sink_unref(c->fallback_sink);
+ if (c->fallback_source)
+ pa_source_unref(c->fallback_source);
+
+ pa_dbus_protocol_unref(c->dbus_protocol);
+
+ pa_xfree(c);
+}
+
+const char *pa_dbusiface_core_get_card_path(pa_dbusiface_core *c, const pa_card *card) {
+ pa_assert(c);
+ pa_assert(card);
+
+ return pa_dbusiface_card_get_path(pa_hashmap_get(c->cards, PA_UINT32_TO_PTR(card->index)));
+}
+
+const char *pa_dbusiface_core_get_sink_path(pa_dbusiface_core *c, const pa_sink *sink) {
+ pa_assert(c);
+ pa_assert(sink);
+
+ return pa_dbusiface_device_get_path(pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(sink->index)));
+}
+
+const char *pa_dbusiface_core_get_source_path(pa_dbusiface_core *c, const pa_source *source) {
+ pa_assert(c);
+ pa_assert(source);
+
+ return pa_dbusiface_device_get_path(pa_hashmap_get(c->sources_by_index, PA_UINT32_TO_PTR(source->index)));
+}
+
+const char *pa_dbusiface_core_get_playback_stream_path(pa_dbusiface_core *c, const pa_sink_input *sink_input) {
+ pa_assert(c);
+ pa_assert(sink_input);
+
+ return pa_dbusiface_stream_get_path(pa_hashmap_get(c->playback_streams, PA_UINT32_TO_PTR(sink_input->index)));
+}
+
+const char *pa_dbusiface_core_get_record_stream_path(pa_dbusiface_core *c, const pa_source_output *source_output) {
+ pa_assert(c);
+ pa_assert(source_output);
+
+ return pa_dbusiface_stream_get_path(pa_hashmap_get(c->record_streams, PA_UINT32_TO_PTR(source_output->index)));
+}
+
+const char *pa_dbusiface_core_get_module_path(pa_dbusiface_core *c, const pa_module *module) {
+ pa_assert(c);
+ pa_assert(module);
+
+ return pa_dbusiface_module_get_path(pa_hashmap_get(c->modules, PA_UINT32_TO_PTR(module->index)));
+}
+
+const char *pa_dbusiface_core_get_client_path(pa_dbusiface_core *c, const pa_client *client) {
+ pa_assert(c);
+ pa_assert(client);
+
+ return pa_dbusiface_client_get_path(pa_hashmap_get(c->clients, PA_UINT32_TO_PTR(client->index)));
+}
+
+pa_sink *pa_dbusiface_core_get_sink(pa_dbusiface_core *c, const char *object_path) {
+ pa_dbusiface_device *device = NULL;
+
+ pa_assert(c);
+ pa_assert(object_path);
+
+ device = pa_hashmap_get(c->sinks_by_path, object_path);
+
+ if (device)
+ return pa_dbusiface_device_get_sink(device);
+ else
+ return NULL;
+}
+
+pa_source *pa_dbusiface_core_get_source(pa_dbusiface_core *c, const char *object_path) {
+ pa_dbusiface_device *device = NULL;
+
+ pa_assert(c);
+ pa_assert(object_path);
+
+ device = pa_hashmap_get(c->sources_by_path, object_path);
+
+ if (device)
+ return pa_dbusiface_device_get_source(device);
+ else
+ return NULL;
+}
diff --git a/src/modules/dbus/iface-core.h b/src/modules/dbus/iface-core.h
new file mode 100644
index 00000000..900b6d1c
--- /dev/null
+++ b/src/modules/dbus/iface-core.h
@@ -0,0 +1,52 @@
+#ifndef foodbusifacecorehfoo
+#define foodbusifacecorehfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interface org.PulseAudio.Core1.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the Core interface
+ * documentation.
+ */
+
+#include <pulsecore/core.h>
+
+typedef struct pa_dbusiface_core pa_dbusiface_core;
+
+pa_dbusiface_core *pa_dbusiface_core_new(pa_core *core);
+void pa_dbusiface_core_free(pa_dbusiface_core *c);
+
+const char *pa_dbusiface_core_get_card_path(pa_dbusiface_core *c, const pa_card *card);
+const char *pa_dbusiface_core_get_sink_path(pa_dbusiface_core *c, const pa_sink *sink);
+const char *pa_dbusiface_core_get_source_path(pa_dbusiface_core *c, const pa_source *source);
+const char *pa_dbusiface_core_get_playback_stream_path(pa_dbusiface_core *c, const pa_sink_input *sink_input);
+const char *pa_dbusiface_core_get_record_stream_path(pa_dbusiface_core *c, const pa_source_output *source_output);
+const char *pa_dbusiface_core_get_module_path(pa_dbusiface_core *c, const pa_module *module);
+const char *pa_dbusiface_core_get_client_path(pa_dbusiface_core *c, const pa_client *client);
+
+/* Returns NULL if there's no sink with the given path. */
+pa_sink *pa_dbusiface_core_get_sink(pa_dbusiface_core *c, const char *object_path);
+
+/* Returns NULL if there's no source with the given path. */
+pa_source *pa_dbusiface_core_get_source(pa_dbusiface_core *c, const char *object_path);
+
+#endif
diff --git a/src/modules/dbus/iface-device-port.c b/src/modules/dbus/iface-device-port.c
new file mode 100644
index 00000000..d403b6a2
--- /dev/null
+++ b/src/modules/dbus/iface-device-port.c
@@ -0,0 +1,190 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <dbus/dbus.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+
+#include "iface-device-port.h"
+
+#define OBJECT_NAME "port"
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_description(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_priority(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+struct pa_dbusiface_device_port {
+ uint32_t index;
+ pa_device_port *port;
+ char *path;
+ pa_dbus_protocol *dbus_protocol;
+};
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INDEX,
+ PROPERTY_HANDLER_NAME,
+ PROPERTY_HANDLER_DESCRIPTION,
+ PROPERTY_HANDLER_PRIORITY,
+ PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_get_index, .set_cb = NULL },
+ [PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_get_name, .set_cb = NULL },
+ [PROPERTY_HANDLER_DESCRIPTION] = { .property_name = "Description", .type = "s", .get_cb = handle_get_description, .set_cb = NULL },
+ [PROPERTY_HANDLER_PRIORITY] = { .property_name = "Priority", .type = "u", .get_cb = handle_get_priority, .set_cb = NULL },
+};
+
+static pa_dbus_interface_info port_interface_info = {
+ .name = PA_DBUSIFACE_DEVICE_PORT_INTERFACE,
+ .method_handlers = NULL,
+ .n_method_handlers = 0,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = NULL,
+ .n_signals = 0
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device_port *p = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &p->index);
+}
+
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device_port *p = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &p->port->name);
+}
+
+static void handle_get_description(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device_port *p = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &p->port->description);
+}
+
+static void handle_get_priority(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device_port *p = userdata;
+ dbus_uint32_t priority = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ priority = p->port->priority;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &priority);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device_port *p = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ dbus_uint32_t priority = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(p);
+
+ priority = p->port->priority;
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &p->index);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &p->port->name);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DESCRIPTION].property_name, DBUS_TYPE_STRING, &p->port->description);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PRIORITY].property_name, DBUS_TYPE_UINT32, &priority);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+ dbus_message_unref(reply);
+}
+
+pa_dbusiface_device_port *pa_dbusiface_device_port_new(
+ pa_dbusiface_device *device,
+ pa_core *core,
+ pa_device_port *port,
+ uint32_t idx) {
+ pa_dbusiface_device_port *p = NULL;
+
+ pa_assert(device);
+ pa_assert(core);
+ pa_assert(port);
+
+ p = pa_xnew(pa_dbusiface_device_port, 1);
+ p->index = idx;
+ p->port = port;
+ p->path = pa_sprintf_malloc("%s/%s%u", pa_dbusiface_device_get_path(device), OBJECT_NAME, idx);
+ p->dbus_protocol = pa_dbus_protocol_get(core);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(p->dbus_protocol, p->path, &port_interface_info, p) >= 0);
+
+ return p;
+}
+
+void pa_dbusiface_device_port_free(pa_dbusiface_device_port *p) {
+ pa_assert(p);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(p->dbus_protocol, p->path, port_interface_info.name) >= 0);
+
+ pa_dbus_protocol_unref(p->dbus_protocol);
+
+ pa_xfree(p->path);
+ pa_xfree(p);
+}
+
+const char *pa_dbusiface_device_port_get_path(pa_dbusiface_device_port *p) {
+ pa_assert(p);
+
+ return p->path;
+}
+
+const char *pa_dbusiface_device_port_get_name(pa_dbusiface_device_port *p) {
+ pa_assert(p);
+
+ return p->port->name;
+}
diff --git a/src/modules/dbus/iface-device-port.h b/src/modules/dbus/iface-device-port.h
new file mode 100644
index 00000000..0461e2ff
--- /dev/null
+++ b/src/modules/dbus/iface-device-port.h
@@ -0,0 +1,50 @@
+#ifndef foodbusifacedeviceporthfoo
+#define foodbusifacedeviceporthfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interface org.PulseAudio.Core1.DevicePort.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the DevicePort interface
+ * documentation.
+ */
+
+#include <pulsecore/protocol-dbus.h>
+#include <pulsecore/sink.h>
+
+#include "iface-device.h"
+
+#define PA_DBUSIFACE_DEVICE_PORT_INTERFACE PA_DBUS_CORE_INTERFACE ".DevicePort"
+
+typedef struct pa_dbusiface_device_port pa_dbusiface_device_port;
+
+pa_dbusiface_device_port *pa_dbusiface_device_port_new(
+ pa_dbusiface_device *device,
+ pa_core *core,
+ pa_device_port *port,
+ uint32_t idx);
+void pa_dbusiface_device_port_free(pa_dbusiface_device_port *p);
+
+const char *pa_dbusiface_device_port_get_path(pa_dbusiface_device_port *p);
+const char *pa_dbusiface_device_port_get_name(pa_dbusiface_device_port *p);
+
+#endif
diff --git a/src/modules/dbus/iface-device.c b/src/modules/dbus/iface-device.c
new file mode 100644
index 00000000..652790f0
--- /dev/null
+++ b/src/modules/dbus/iface-device.c
@@ -0,0 +1,1315 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-device-port.h"
+
+#include "iface-device.h"
+
+#define SINK_OBJECT_NAME "sink"
+#define SOURCE_OBJECT_NAME "source"
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_card(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_channels(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_has_flat_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_has_convertible_to_decibel_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_base_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_volume_steps(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_mute(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_mute(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_has_hardware_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_has_hardware_mute(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_configured_latency(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_has_dynamic_latency(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_latency(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_is_hardware_device(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_is_network_device(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_state(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_ports(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_active_port(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_active_port(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_suspend(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_port_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_sink_get_monitor_source(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_sink_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_source_get_monitor_of_sink(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_source_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+enum device_type {
+ DEVICE_TYPE_SINK,
+ DEVICE_TYPE_SOURCE
+};
+
+struct pa_dbusiface_device {
+ pa_dbusiface_core *core;
+
+ union {
+ pa_sink *sink;
+ pa_source *source;
+ };
+ enum device_type type;
+ char *path;
+ pa_cvolume volume;
+ dbus_bool_t mute;
+ union {
+ pa_sink_state_t sink_state;
+ pa_source_state_t source_state;
+ };
+ pa_hashmap *ports;
+ uint32_t next_port_index;
+ pa_device_port *active_port;
+ pa_proplist *proplist;
+
+ pa_dbus_protocol *dbus_protocol;
+ pa_subscription *subscription;
+};
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INDEX,
+ PROPERTY_HANDLER_NAME,
+ PROPERTY_HANDLER_DRIVER,
+ PROPERTY_HANDLER_OWNER_MODULE,
+ PROPERTY_HANDLER_CARD,
+ PROPERTY_HANDLER_SAMPLE_FORMAT,
+ PROPERTY_HANDLER_SAMPLE_RATE,
+ PROPERTY_HANDLER_CHANNELS,
+ PROPERTY_HANDLER_VOLUME,
+ PROPERTY_HANDLER_HAS_FLAT_VOLUME,
+ PROPERTY_HANDLER_HAS_CONVERTIBLE_TO_DECIBEL_VOLUME,
+ PROPERTY_HANDLER_BASE_VOLUME,
+ PROPERTY_HANDLER_VOLUME_STEPS,
+ PROPERTY_HANDLER_MUTE,
+ PROPERTY_HANDLER_HAS_HARDWARE_VOLUME,
+ PROPERTY_HANDLER_HAS_HARDWARE_MUTE,
+ PROPERTY_HANDLER_CONFIGURED_LATENCY,
+ PROPERTY_HANDLER_HAS_DYNAMIC_LATENCY,
+ PROPERTY_HANDLER_LATENCY,
+ PROPERTY_HANDLER_IS_HARDWARE_DEVICE,
+ PROPERTY_HANDLER_IS_NETWORK_DEVICE,
+ PROPERTY_HANDLER_STATE,
+ PROPERTY_HANDLER_PORTS,
+ PROPERTY_HANDLER_ACTIVE_PORT,
+ PROPERTY_HANDLER_PROPERTY_LIST,
+ PROPERTY_HANDLER_MAX
+};
+
+enum sink_property_handler_index {
+ SINK_PROPERTY_HANDLER_MONITOR_SOURCE,
+ SINK_PROPERTY_HANDLER_MAX
+};
+
+enum source_property_handler_index {
+ SOURCE_PROPERTY_HANDLER_MONITOR_OF_SINK,
+ SOURCE_PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_get_index, .set_cb = NULL },
+ [PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_get_name, .set_cb = NULL },
+ [PROPERTY_HANDLER_DRIVER] = { .property_name = "Driver", .type = "s", .get_cb = handle_get_driver, .set_cb = NULL },
+ [PROPERTY_HANDLER_OWNER_MODULE] = { .property_name = "OwnerModule", .type = "o", .get_cb = handle_get_owner_module, .set_cb = NULL },
+ [PROPERTY_HANDLER_CARD] = { .property_name = "Card", .type = "o", .get_cb = handle_get_card, .set_cb = NULL },
+ [PROPERTY_HANDLER_SAMPLE_FORMAT] = { .property_name = "SampleFormat", .type = "u", .get_cb = handle_get_sample_format, .set_cb = NULL },
+ [PROPERTY_HANDLER_SAMPLE_RATE] = { .property_name = "SampleRate", .type = "u", .get_cb = handle_get_sample_rate, .set_cb = NULL },
+ [PROPERTY_HANDLER_CHANNELS] = { .property_name = "Channels", .type = "au", .get_cb = handle_get_channels, .set_cb = NULL },
+ [PROPERTY_HANDLER_VOLUME] = { .property_name = "Volume", .type = "au", .get_cb = handle_get_volume, .set_cb = handle_set_volume },
+ [PROPERTY_HANDLER_HAS_FLAT_VOLUME] = { .property_name = "HasFlatVolume", .type = "b", .get_cb = handle_get_has_flat_volume, .set_cb = NULL },
+ [PROPERTY_HANDLER_HAS_CONVERTIBLE_TO_DECIBEL_VOLUME] = { .property_name = "HasConvertibleToDecibelVolume", .type = "b", .get_cb = handle_get_has_convertible_to_decibel_volume, .set_cb = NULL },
+ [PROPERTY_HANDLER_BASE_VOLUME] = { .property_name = "BaseVolume", .type = "u", .get_cb = handle_get_base_volume, .set_cb = NULL },
+ [PROPERTY_HANDLER_VOLUME_STEPS] = { .property_name = "VolumeSteps", .type = "u", .get_cb = handle_get_volume_steps, .set_cb = NULL },
+ [PROPERTY_HANDLER_MUTE] = { .property_name = "Mute", .type = "b", .get_cb = handle_get_mute, .set_cb = handle_set_mute },
+ [PROPERTY_HANDLER_HAS_HARDWARE_VOLUME] = { .property_name = "HasHardwareVolume", .type = "b", .get_cb = handle_get_has_hardware_volume, .set_cb = NULL },
+ [PROPERTY_HANDLER_HAS_HARDWARE_MUTE] = { .property_name = "HasHardwareMute", .type = "b", .get_cb = handle_get_has_hardware_mute, .set_cb = NULL },
+ [PROPERTY_HANDLER_CONFIGURED_LATENCY] = { .property_name = "ConfiguredLatency", .type = "t", .get_cb = handle_get_configured_latency, .set_cb = NULL },
+ [PROPERTY_HANDLER_HAS_DYNAMIC_LATENCY] = { .property_name = "HasDynamicLatency", .type = "b", .get_cb = handle_get_has_dynamic_latency, .set_cb = NULL },
+ [PROPERTY_HANDLER_LATENCY] = { .property_name = "Latency", .type = "t", .get_cb = handle_get_latency, .set_cb = NULL },
+ [PROPERTY_HANDLER_IS_HARDWARE_DEVICE] = { .property_name = "IsHardwareDevice", .type = "b", .get_cb = handle_get_is_hardware_device, .set_cb = NULL },
+ [PROPERTY_HANDLER_IS_NETWORK_DEVICE] = { .property_name = "IsNetworkDevice", .type = "b", .get_cb = handle_get_is_network_device, .set_cb = NULL },
+ [PROPERTY_HANDLER_STATE] = { .property_name = "State", .type = "u", .get_cb = handle_get_state, .set_cb = NULL },
+ [PROPERTY_HANDLER_PORTS] = { .property_name = "Ports", .type = "ao", .get_cb = handle_get_ports, .set_cb = NULL },
+ [PROPERTY_HANDLER_ACTIVE_PORT] = { .property_name = "ActivePort", .type = "o", .get_cb = handle_get_active_port, .set_cb = handle_set_active_port },
+ [PROPERTY_HANDLER_PROPERTY_LIST] = { .property_name = "PropertyList", .type = "a{say}", .get_cb = handle_get_property_list, .set_cb = NULL }
+};
+
+static pa_dbus_property_handler sink_property_handlers[SINK_PROPERTY_HANDLER_MAX] = {
+ [SINK_PROPERTY_HANDLER_MONITOR_SOURCE] = { .property_name = "MonitorSource", .type = "o", .get_cb = handle_sink_get_monitor_source, .set_cb = NULL }
+};
+
+static pa_dbus_property_handler source_property_handlers[SOURCE_PROPERTY_HANDLER_MAX] = {
+ [SOURCE_PROPERTY_HANDLER_MONITOR_OF_SINK] = { .property_name = "MonitorOfSink", .type = "o", .get_cb = handle_source_get_monitor_of_sink, .set_cb = NULL }
+};
+
+enum method_handler_index {
+ METHOD_HANDLER_SUSPEND,
+ METHOD_HANDLER_GET_PORT_BY_NAME,
+ METHOD_HANDLER_MAX
+};
+
+static pa_dbus_arg_info suspend_args[] = { { "suspend", "b", "in" } };
+static pa_dbus_arg_info get_port_by_name_args[] = { { "name", "s", "in" }, { "port", "o", "out" } };
+
+static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
+ [METHOD_HANDLER_SUSPEND] = {
+ .method_name = "Suspend",
+ .arguments = suspend_args,
+ .n_arguments = sizeof(suspend_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_suspend },
+ [METHOD_HANDLER_GET_PORT_BY_NAME] = {
+ .method_name = "GetPortByName",
+ .arguments = get_port_by_name_args,
+ .n_arguments = sizeof(get_port_by_name_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_get_port_by_name }
+};
+
+enum signal_index {
+ SIGNAL_VOLUME_UPDATED,
+ SIGNAL_MUTE_UPDATED,
+ SIGNAL_STATE_UPDATED,
+ SIGNAL_ACTIVE_PORT_UPDATED,
+ SIGNAL_PROPERTY_LIST_UPDATED,
+ SIGNAL_MAX
+};
+
+static pa_dbus_arg_info volume_updated_args[] = { { "volume", "au", NULL } };
+static pa_dbus_arg_info mute_updated_args[] = { { "muted", "b", NULL } };
+static pa_dbus_arg_info state_updated_args[] = { { "state", "u", NULL } };
+static pa_dbus_arg_info active_port_updated_args[] = { { "port", "o", NULL } };
+static pa_dbus_arg_info property_list_updated_args[] = { { "property_list", "a{say}", NULL } };
+
+static pa_dbus_signal_info signals[SIGNAL_MAX] = {
+ [SIGNAL_VOLUME_UPDATED] = { .name = "VolumeUpdated", .arguments = volume_updated_args, .n_arguments = 1 },
+ [SIGNAL_MUTE_UPDATED] = { .name = "MuteUpdated", .arguments = mute_updated_args, .n_arguments = 1 },
+ [SIGNAL_STATE_UPDATED] = { .name = "StateUpdated", .arguments = state_updated_args, .n_arguments = 1 },
+ [SIGNAL_ACTIVE_PORT_UPDATED] = { .name = "ActivePortUpdated", .arguments = active_port_updated_args, .n_arguments = 1 },
+ [SIGNAL_PROPERTY_LIST_UPDATED] = { .name = "PropertyListUpdated", .arguments = property_list_updated_args, .n_arguments = 1 }
+};
+
+static pa_dbus_interface_info device_interface_info = {
+ .name = PA_DBUSIFACE_DEVICE_INTERFACE,
+ .method_handlers = method_handlers,
+ .n_method_handlers = METHOD_HANDLER_MAX,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = signals,
+ .n_signals = SIGNAL_MAX
+};
+
+static pa_dbus_interface_info sink_interface_info = {
+ .name = PA_DBUSIFACE_SINK_INTERFACE,
+ .method_handlers = NULL,
+ .n_method_handlers = 0,
+ .property_handlers = sink_property_handlers,
+ .n_property_handlers = SINK_PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_sink_get_all,
+ .signals = NULL,
+ .n_signals = 0
+};
+
+static pa_dbus_interface_info source_interface_info = {
+ .name = PA_DBUSIFACE_SOURCE_INTERFACE,
+ .method_handlers = NULL,
+ .n_method_handlers = 0,
+ .property_handlers = source_property_handlers,
+ .n_property_handlers = SOURCE_PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_source_get_all,
+ .signals = NULL,
+ .n_signals = 0
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_uint32_t idx = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ idx = (d->type == DEVICE_TYPE_SINK) ? d->sink->index : d->source->index;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &idx);
+}
+
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ const char *name = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ name = (d->type == DEVICE_TYPE_SINK) ? d->sink->name : d->source->name;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &name);
+}
+
+static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ const char *driver = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ driver = (d->type == DEVICE_TYPE_SINK) ? d->sink->driver : d->source->driver;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &driver);
+}
+
+static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ pa_module *owner_module = NULL;
+ const char *object_path = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ owner_module = (d->type == DEVICE_TYPE_SINK) ? d->sink->module : d->source->module;
+
+ if (!owner_module) {
+ if (d->type == DEVICE_TYPE_SINK)
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Sink %s doesn't have an owner module.", d->sink->name);
+ else
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Source %s doesn't have an owner module.", d->source->name);
+ return;
+ }
+
+ object_path = pa_dbusiface_core_get_module_path(d->core, owner_module);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_get_card(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ pa_card *card = NULL;
+ const char *object_path = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ card = (d->type == DEVICE_TYPE_SINK) ? d->sink->card : d->source->card;
+
+ if (!card) {
+ if (d->type == DEVICE_TYPE_SINK)
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Sink %s doesn't belong to any card.", d->sink->name);
+ else
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Source %s doesn't belong to any card.", d->source->name);
+ return;
+ }
+
+ object_path = pa_dbusiface_core_get_card_path(d->core, card);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_get_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_uint32_t sample_format = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ sample_format = (d->type == DEVICE_TYPE_SINK) ? d->sink->sample_spec.format : d->source->sample_spec.format;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sample_format);
+}
+
+static void handle_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_uint32_t sample_rate = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ sample_rate = (d->type == DEVICE_TYPE_SINK) ? d->sink->sample_spec.rate : d->source->sample_spec.rate;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sample_rate);
+}
+
+static void handle_get_channels(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ pa_channel_map *channel_map = NULL;
+ dbus_uint32_t channels[PA_CHANNELS_MAX];
+ unsigned i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ channel_map = (d->type == DEVICE_TYPE_SINK) ? &d->sink->channel_map : &d->source->channel_map;
+
+ for (i = 0; i < channel_map->channels; ++i)
+ channels[i] = channel_map->map[i];
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, channels, channel_map->channels);
+}
+
+static void handle_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_uint32_t volume[PA_CHANNELS_MAX];
+ unsigned i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ for (i = 0; i < d->volume.channels; ++i)
+ volume[i] = d->volume.values[i];
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, volume, d->volume.channels);
+}
+
+static void handle_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ DBusMessageIter array_iter;
+ int device_channels = 0;
+ dbus_uint32_t *volume = NULL;
+ int n_volume_entries = 0;
+ pa_cvolume new_vol;
+ int i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(d);
+
+ device_channels = (d->type == DEVICE_TYPE_SINK) ? d->sink->channel_map.channels : d->source->channel_map.channels;
+
+ dbus_message_iter_recurse(iter, &array_iter);
+ dbus_message_iter_get_fixed_array(&array_iter, &volume, &n_volume_entries);
+
+ if (n_volume_entries != device_channels && n_volume_entries != 1) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
+ "Expected %u volume entries, got %i.", device_channels, n_volume_entries);
+ return;
+ }
+
+ pa_cvolume_init(&new_vol);
+ new_vol.channels = n_volume_entries;
+
+ for (i = 0; i < n_volume_entries; ++i) {
+ if (!PA_VOLUME_IS_VALID(volume[i])) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Too large volume value: %u", volume[i]);
+ return;
+ }
+ new_vol.values[i] = volume[i];
+ }
+
+ if (d->type == DEVICE_TYPE_SINK)
+ pa_sink_set_volume(d->sink, &new_vol, TRUE, TRUE);
+ else
+ pa_source_set_volume(d->source, &new_vol, TRUE, TRUE);
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void handle_get_has_flat_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_bool_t has_flat_volume = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ has_flat_volume = (d->type == DEVICE_TYPE_SINK) ? (d->sink->flags & PA_SINK_FLAT_VOLUME) : FALSE;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &has_flat_volume);
+}
+
+static void handle_get_has_convertible_to_decibel_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_bool_t has_convertible_to_decibel_volume = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ has_convertible_to_decibel_volume = (d->type == DEVICE_TYPE_SINK)
+ ? (d->sink->flags & PA_SINK_DECIBEL_VOLUME)
+ : (d->source->flags & PA_SOURCE_DECIBEL_VOLUME);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &has_convertible_to_decibel_volume);
+}
+
+static void handle_get_base_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_uint32_t base_volume;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ base_volume = (d->type == DEVICE_TYPE_SINK) ? d->sink->base_volume : d->source->base_volume;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &base_volume);
+}
+
+static void handle_get_volume_steps(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_uint32_t volume_steps;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ volume_steps = (d->type == DEVICE_TYPE_SINK) ? d->sink->n_volume_steps : d->source->n_volume_steps;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &volume_steps);
+}
+
+static void handle_get_mute(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &d->mute);
+}
+
+static void handle_set_mute(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_bool_t mute = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(d);
+
+ dbus_message_iter_get_basic(iter, &mute);
+
+ if (d->type == DEVICE_TYPE_SINK)
+ pa_sink_set_mute(d->sink, mute, TRUE);
+ else
+ pa_source_set_mute(d->source, mute, TRUE);
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void handle_get_has_hardware_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_bool_t has_hardware_volume = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ has_hardware_volume = (d->type == DEVICE_TYPE_SINK)
+ ? (d->sink->flags & PA_SINK_HW_VOLUME_CTRL)
+ : (d->source->flags & PA_SOURCE_HW_VOLUME_CTRL);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &has_hardware_volume);
+}
+
+static void handle_get_has_hardware_mute(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_bool_t has_hardware_mute = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ has_hardware_mute = (d->type == DEVICE_TYPE_SINK)
+ ? (d->sink->flags & PA_SINK_HW_MUTE_CTRL)
+ : (d->source->flags & PA_SOURCE_HW_MUTE_CTRL);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &has_hardware_mute);
+}
+
+static void handle_get_configured_latency(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_uint64_t configured_latency = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ configured_latency = (d->type == DEVICE_TYPE_SINK)
+ ? pa_sink_get_requested_latency(d->sink)
+ : pa_source_get_requested_latency(d->source);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT64, &configured_latency);
+}
+
+static void handle_get_has_dynamic_latency(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_bool_t has_dynamic_latency = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ has_dynamic_latency = (d->type == DEVICE_TYPE_SINK)
+ ? (d->sink->flags & PA_SINK_DYNAMIC_LATENCY)
+ : (d->source->flags & PA_SOURCE_DYNAMIC_LATENCY);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &has_dynamic_latency);
+}
+
+static void handle_get_latency(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_uint64_t latency = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ if (d->type == DEVICE_TYPE_SINK && !(d->sink->flags & PA_SINK_LATENCY))
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Sink %s doesn't support latency querying.", d->sink->name);
+ else if (d->type == DEVICE_TYPE_SOURCE && !(d->source->flags & PA_SOURCE_LATENCY))
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Source %s doesn't support latency querying.", d->source->name);
+ return;
+
+ latency = (d->type == DEVICE_TYPE_SINK) ? pa_sink_get_latency(d->sink) : pa_source_get_latency(d->source);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT64, &latency);
+}
+
+static void handle_get_is_hardware_device(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_bool_t is_hardware_device = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ is_hardware_device = (d->type == DEVICE_TYPE_SINK)
+ ? (d->sink->flags & PA_SINK_HARDWARE)
+ : (d->source->flags & PA_SOURCE_HARDWARE);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &is_hardware_device);
+}
+
+static void handle_get_is_network_device(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_bool_t is_network_device = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ is_network_device = (d->type == DEVICE_TYPE_SINK)
+ ? (d->sink->flags & PA_SINK_NETWORK)
+ : (d->source->flags & PA_SOURCE_NETWORK);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &is_network_device);
+}
+
+static void handle_get_state(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_uint32_t state;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ state = (d->type == DEVICE_TYPE_SINK) ? d->sink_state : d->source_state;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &state);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_ports(pa_dbusiface_device *d, unsigned *n) {
+ const char **ports;
+ unsigned i = 0;
+ void *state = NULL;
+ pa_dbusiface_device_port *port = NULL;
+
+ pa_assert(d);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(d->ports);
+
+ if (*n == 0)
+ return NULL;
+
+ ports = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(port, d->ports, state)
+ ports[i++] = pa_dbusiface_device_port_get_path(port);
+
+ return ports;
+}
+
+static void handle_get_ports(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ const char **ports = NULL;
+ unsigned n_ports = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ ports = get_ports(d, &n_ports);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, ports, n_ports);
+
+ pa_xfree(ports);
+}
+
+static void handle_get_active_port(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ const char *active_port;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ if (!d->active_port) {
+ pa_assert(pa_hashmap_isempty(d->ports));
+
+ if (d->type == DEVICE_TYPE_SINK)
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "The sink %s has no ports, and therefore there's no active port either.", d->sink->name);
+ else
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "The source %s has no ports, and therefore there's no active port either.", d->source->name);
+ return;
+ }
+
+ active_port = pa_dbusiface_device_port_get_path(pa_hashmap_get(d->ports, d->active_port->name));
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &active_port);
+}
+
+static void handle_set_active_port(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ const char *new_active_path;
+ pa_dbusiface_device_port *new_active;
+ int r;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(d);
+
+ if (!d->active_port) {
+ pa_assert(pa_hashmap_isempty(d->ports));
+
+ if (d->type == DEVICE_TYPE_SINK)
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "The sink %s has no ports, and therefore there's no active port either.", d->sink->name);
+ else
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "The source %s has no ports, and therefore there's no active port either.", d->source->name);
+ return;
+ }
+
+ dbus_message_iter_get_basic(iter, &new_active_path);
+
+ if (!(new_active = pa_hashmap_get(d->ports, new_active_path))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "No such port: %s", new_active_path);
+ return;
+ }
+
+ if (d->type == DEVICE_TYPE_SINK) {
+ if ((r = pa_sink_set_port(d->sink, pa_dbusiface_device_port_get_name(new_active), TRUE)) < 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
+ "Internal error in PulseAudio: pa_sink_set_port() failed with error code %i.", r);
+ return;
+ }
+ } else {
+ if ((r = pa_source_set_port(d->source, pa_dbusiface_device_port_get_name(new_active), TRUE)) < 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
+ "Internal error in PulseAudio: pa_source_set_port() failed with error code %i.", r);
+ return;
+ }
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ pa_dbus_send_proplist_variant_reply(conn, msg, d->proplist);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ dbus_uint32_t idx = 0;
+ const char *name = NULL;
+ const char *driver = NULL;
+ pa_module *owner_module = NULL;
+ const char *owner_module_path = NULL;
+ pa_card *card = NULL;
+ const char *card_path = NULL;
+ dbus_uint32_t sample_format = 0;
+ dbus_uint32_t sample_rate = 0;
+ pa_channel_map *channel_map = NULL;
+ dbus_uint32_t channels[PA_CHANNELS_MAX];
+ dbus_uint32_t volume[PA_CHANNELS_MAX];
+ dbus_bool_t has_flat_volume = FALSE;
+ dbus_bool_t has_convertible_to_decibel_volume = FALSE;
+ dbus_uint32_t base_volume = 0;
+ dbus_uint32_t volume_steps = 0;
+ dbus_bool_t has_hardware_volume = FALSE;
+ dbus_bool_t has_hardware_mute = FALSE;
+ dbus_uint64_t configured_latency = 0;
+ dbus_bool_t has_dynamic_latency = FALSE;
+ dbus_uint64_t latency = 0;
+ dbus_bool_t is_hardware_device = FALSE;
+ dbus_bool_t is_network_device = FALSE;
+ dbus_uint32_t state = 0;
+ const char **ports = NULL;
+ unsigned n_ports = 0;
+ const char *active_port = NULL;
+ unsigned i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ if (d->type == DEVICE_TYPE_SINK) {
+ idx = d->sink->index;
+ name = d->sink->name;
+ driver = d->sink->driver;
+ owner_module = d->sink->module;
+ card = d->sink->card;
+ sample_format = d->sink->sample_spec.format;
+ sample_rate = d->sink->sample_spec.rate;
+ channel_map = &d->sink->channel_map;
+ has_flat_volume = d->sink->flags & PA_SINK_FLAT_VOLUME;
+ has_convertible_to_decibel_volume = d->sink->flags & PA_SINK_DECIBEL_VOLUME;
+ base_volume = d->sink->base_volume;
+ volume_steps = d->sink->n_volume_steps;
+ has_hardware_volume = d->sink->flags & PA_SINK_HW_VOLUME_CTRL;
+ has_hardware_mute = d->sink->flags & PA_SINK_HW_MUTE_CTRL;
+ configured_latency = pa_sink_get_requested_latency(d->sink);
+ has_dynamic_latency = d->sink->flags & PA_SINK_DYNAMIC_LATENCY;
+ latency = pa_sink_get_latency(d->sink);
+ is_hardware_device = d->sink->flags & PA_SINK_HARDWARE;
+ is_network_device = d->sink->flags & PA_SINK_NETWORK;
+ state = pa_sink_get_state(d->sink);
+ } else {
+ idx = d->source->index;
+ name = d->source->name;
+ driver = d->source->driver;
+ owner_module = d->source->module;
+ card = d->source->card;
+ sample_format = d->source->sample_spec.format;
+ sample_rate = d->source->sample_spec.rate;
+ channel_map = &d->source->channel_map;
+ has_flat_volume = FALSE;
+ has_convertible_to_decibel_volume = d->source->flags & PA_SOURCE_DECIBEL_VOLUME;
+ base_volume = d->source->base_volume;
+ volume_steps = d->source->n_volume_steps;
+ has_hardware_volume = d->source->flags & PA_SOURCE_HW_VOLUME_CTRL;
+ has_hardware_mute = d->source->flags & PA_SOURCE_HW_MUTE_CTRL;
+ configured_latency = pa_source_get_requested_latency(d->source);
+ has_dynamic_latency = d->source->flags & PA_SOURCE_DYNAMIC_LATENCY;
+ latency = pa_source_get_latency(d->source);
+ is_hardware_device = d->source->flags & PA_SOURCE_HARDWARE;
+ is_network_device = d->source->flags & PA_SOURCE_NETWORK;
+ state = pa_source_get_state(d->source);
+ }
+ if (owner_module)
+ owner_module_path = pa_dbusiface_core_get_module_path(d->core, owner_module);
+ if (card)
+ card_path = pa_dbusiface_core_get_card_path(d->core, card);
+ for (i = 0; i < channel_map->channels; ++i)
+ channels[i] = channel_map->map[i];
+ for (i = 0; i < d->volume.channels; ++i)
+ volume[i] = d->volume.values[i];
+ ports = get_ports(d, &n_ports);
+ if (d->active_port)
+ active_port = pa_dbusiface_device_port_get_path(pa_hashmap_get(d->ports, d->active_port->name));
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &idx);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &name);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DRIVER].property_name, DBUS_TYPE_STRING, &driver);
+
+ if (owner_module)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_OWNER_MODULE].property_name, DBUS_TYPE_OBJECT_PATH, &owner_module_path);
+
+ if (card)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CARD].property_name, DBUS_TYPE_OBJECT_PATH, &card_path);
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLE_FORMAT].property_name, DBUS_TYPE_UINT32, &sample_format);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLE_RATE].property_name, DBUS_TYPE_UINT32, &sample_rate);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CHANNELS].property_name, DBUS_TYPE_UINT32, channels, channel_map->channels);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_VOLUME].property_name, DBUS_TYPE_UINT32, volume, d->volume.channels);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_HAS_FLAT_VOLUME].property_name, DBUS_TYPE_BOOLEAN, &has_flat_volume);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_HAS_CONVERTIBLE_TO_DECIBEL_VOLUME].property_name, DBUS_TYPE_BOOLEAN, &has_convertible_to_decibel_volume);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_BASE_VOLUME].property_name, DBUS_TYPE_UINT32, &base_volume);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_VOLUME_STEPS].property_name, DBUS_TYPE_UINT32, &volume_steps);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_MUTE].property_name, DBUS_TYPE_BOOLEAN, &d->mute);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_HAS_HARDWARE_VOLUME].property_name, DBUS_TYPE_BOOLEAN, &has_hardware_volume);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_HAS_HARDWARE_MUTE].property_name, DBUS_TYPE_BOOLEAN, &has_hardware_mute);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CONFIGURED_LATENCY].property_name, DBUS_TYPE_UINT64, &configured_latency);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_HAS_DYNAMIC_LATENCY].property_name, DBUS_TYPE_BOOLEAN, &has_dynamic_latency);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_LATENCY].property_name, DBUS_TYPE_UINT64, &latency);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_IS_HARDWARE_DEVICE].property_name, DBUS_TYPE_BOOLEAN, &is_hardware_device);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_IS_NETWORK_DEVICE].property_name, DBUS_TYPE_BOOLEAN, &is_network_device);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_STATE].property_name, DBUS_TYPE_UINT32, &state);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PORTS].property_name, DBUS_TYPE_OBJECT_PATH, ports, n_ports);
+
+ if (active_port)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_ACTIVE_PORT].property_name, DBUS_TYPE_OBJECT_PATH, &active_port);
+
+ pa_dbus_append_proplist_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROPERTY_LIST].property_name, d->proplist);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+
+ pa_xfree(ports);
+}
+
+static void handle_suspend(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ dbus_bool_t suspend = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_BOOLEAN, &suspend, DBUS_TYPE_INVALID));
+
+ if ((d->type == DEVICE_TYPE_SINK) && (pa_sink_suspend(d->sink, suspend, PA_SUSPEND_USER) < 0)) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Internal error in PulseAudio: pa_sink_suspend() failed.");
+ return;
+ } else if ((d->type == DEVICE_TYPE_SOURCE) && (pa_source_suspend(d->source, suspend, PA_SUSPEND_USER) < 0)) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Internal error in PulseAudio: pa_source_suspend() failed.");
+ return;
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void handle_get_port_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ const char *port_name = NULL;
+ pa_dbusiface_device_port *port = NULL;
+ const char *port_path = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &port_name, DBUS_TYPE_INVALID));
+
+ if (!(port = pa_hashmap_get(d->ports, port_name))) {
+ if (d->type == DEVICE_TYPE_SINK)
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND,
+ "%s: No such port on sink %s.", port_name, d->sink->name);
+ else
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND,
+ "%s: No such port on source %s.", port_name, d->source->name);
+ return;
+ }
+
+ port_path = pa_dbusiface_device_port_get_path(port);
+
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &port_path);
+}
+
+static void handle_sink_get_monitor_source(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ const char *monitor_source = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+ pa_assert(d->type == DEVICE_TYPE_SINK);
+
+ monitor_source = pa_dbusiface_core_get_source_path(d->core, d->sink->monitor_source);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &monitor_source);
+}
+
+static void handle_sink_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ const char *monitor_source = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+ pa_assert(d->type == DEVICE_TYPE_SINK);
+
+ monitor_source = pa_dbusiface_core_get_source_path(d->core, d->sink->monitor_source);
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[SINK_PROPERTY_HANDLER_MONITOR_SOURCE].property_name, DBUS_TYPE_OBJECT_PATH, &monitor_source);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+}
+
+static void handle_source_get_monitor_of_sink(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ const char *monitor_of_sink = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+ pa_assert(d->type == DEVICE_TYPE_SOURCE);
+
+ if (!d->source->monitor_of) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "Source %s is not a monitor source.", d->source->name);
+ return;
+ }
+
+ monitor_of_sink = pa_dbusiface_core_get_sink_path(d->core, d->source->monitor_of);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &monitor_of_sink);
+}
+
+static void handle_source_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ const char *monitor_of_sink = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(d);
+ pa_assert(d->type == DEVICE_TYPE_SOURCE);
+
+ if (d->source->monitor_of)
+ monitor_of_sink = pa_dbusiface_core_get_sink_path(d->core, d->source->monitor_of);
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ if (monitor_of_sink)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[SOURCE_PROPERTY_HANDLER_MONITOR_OF_SINK].property_name, DBUS_TYPE_OBJECT_PATH, &monitor_of_sink);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+}
+
+static void subscription_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ pa_dbusiface_device *d = userdata;
+ DBusMessage *signal_msg = NULL;
+ const pa_cvolume *new_volume = NULL;
+ pa_bool_t new_mute = FALSE;
+ pa_sink_state_t new_sink_state = 0;
+ pa_source_state_t new_source_state = 0;
+ pa_device_port *new_active_port = NULL;
+ pa_proplist *new_proplist = NULL;
+ unsigned i = 0;
+
+ pa_assert(c);
+ pa_assert(d);
+
+ if ((d->type == DEVICE_TYPE_SINK && idx != d->sink->index) || (d->type == DEVICE_TYPE_SOURCE && idx != d->source->index))
+ return;
+
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
+ return;
+
+ pa_assert(((d->type == DEVICE_TYPE_SINK)
+ && ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK))
+ || ((d->type == DEVICE_TYPE_SOURCE)
+ && ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE)));
+
+ new_volume = (d->type == DEVICE_TYPE_SINK)
+ ? pa_sink_get_volume(d->sink, FALSE)
+ : pa_source_get_volume(d->source, FALSE);
+
+ if (!pa_cvolume_equal(&d->volume, new_volume)) {
+ dbus_uint32_t volume[PA_CHANNELS_MAX];
+ dbus_uint32_t *volume_ptr = volume;
+
+ d->volume = *new_volume;
+
+ for (i = 0; i < d->volume.channels; ++i)
+ volume[i] = d->volume.values[i];
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(d->path,
+ PA_DBUSIFACE_DEVICE_INTERFACE,
+ signals[SIGNAL_VOLUME_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &volume_ptr, d->volume.channels,
+ DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(d->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+
+ new_mute = (d->type == DEVICE_TYPE_SINK) ? pa_sink_get_mute(d->sink, FALSE) : pa_source_get_mute(d->source, FALSE);
+
+ if (d->mute != new_mute) {
+ d->mute = new_mute;
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(d->path,
+ PA_DBUSIFACE_DEVICE_INTERFACE,
+ signals[SIGNAL_MUTE_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_BOOLEAN, &d->mute, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(d->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+
+ if (d->type == DEVICE_TYPE_SINK)
+ new_sink_state = pa_sink_get_state(d->sink);
+ else
+ new_source_state = pa_source_get_state(d->source);
+
+ if ((d->type == DEVICE_TYPE_SINK && d->sink_state != new_sink_state)
+ || (d->type == DEVICE_TYPE_SOURCE && d->source_state != new_source_state)) {
+ dbus_uint32_t state = 0;
+
+ if (d->type == DEVICE_TYPE_SINK)
+ d->sink_state = new_sink_state;
+ else
+ d->source_state = new_source_state;
+
+ state = (d->type == DEVICE_TYPE_SINK) ? d->sink_state : d->source_state;
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(d->path,
+ PA_DBUSIFACE_DEVICE_INTERFACE,
+ signals[SIGNAL_STATE_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(d->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+
+ new_active_port = (d->type == DEVICE_TYPE_SINK) ? d->sink->active_port : d->source->active_port;
+
+ if (d->active_port != new_active_port) {
+ const char *object_path = NULL;
+
+ d->active_port = new_active_port;
+ object_path = pa_dbusiface_device_port_get_path(pa_hashmap_get(d->ports, d->active_port->name));
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(d->path,
+ PA_DBUSIFACE_DEVICE_INTERFACE,
+ signals[SIGNAL_ACTIVE_PORT_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(d->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+
+ new_proplist = (d->type == DEVICE_TYPE_SINK) ? d->sink->proplist : d->source->proplist;
+
+ if (!pa_proplist_equal(d->proplist, new_proplist)) {
+ DBusMessageIter msg_iter;
+
+ pa_proplist_update(d->proplist, PA_UPDATE_SET, new_proplist);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(d->path,
+ PA_DBUSIFACE_DEVICE_INTERFACE,
+ signals[SIGNAL_PROPERTY_LIST_UPDATED].name));
+ dbus_message_iter_init_append(signal_msg, &msg_iter);
+ pa_dbus_append_proplist(&msg_iter, d->proplist);
+
+ pa_dbus_protocol_send_signal(d->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+}
+
+pa_dbusiface_device *pa_dbusiface_device_new_sink(pa_dbusiface_core *core, pa_sink *sink) {
+ pa_dbusiface_device *d = NULL;
+
+ pa_assert(core);
+ pa_assert(sink);
+
+ d = pa_xnew0(pa_dbusiface_device, 1);
+ d->core = core;
+ d->sink = pa_sink_ref(sink);
+ d->type = DEVICE_TYPE_SINK;
+ d->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, SINK_OBJECT_NAME, sink->index);
+ d->volume = *pa_sink_get_volume(sink, FALSE);
+ d->mute = pa_sink_get_mute(sink, FALSE);
+ d->sink_state = pa_sink_get_state(sink);
+ d->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ d->next_port_index = 0;
+ d->active_port = NULL;
+ d->proplist = pa_proplist_copy(sink->proplist);
+ d->dbus_protocol = pa_dbus_protocol_get(sink->core);
+ d->subscription = pa_subscription_new(sink->core, PA_SUBSCRIPTION_MASK_SINK, subscription_cb, d);
+
+ if (sink->ports) {
+ pa_device_port *port;
+ void *state = NULL;
+
+ PA_HASHMAP_FOREACH(port, sink->ports, state) {
+ pa_dbusiface_device_port *p = pa_dbusiface_device_port_new(d, sink->core, port, d->next_port_index++);
+ pa_hashmap_put(d->ports, pa_dbusiface_device_port_get_name(p), p);
+ }
+ pa_assert_se(d->active_port = sink->active_port);
+ }
+
+ pa_assert_se(pa_dbus_protocol_add_interface(d->dbus_protocol, d->path, &device_interface_info, d) >= 0);
+ pa_assert_se(pa_dbus_protocol_add_interface(d->dbus_protocol, d->path, &sink_interface_info, d) >= 0);
+
+ return d;
+}
+
+pa_dbusiface_device *pa_dbusiface_device_new_source(pa_dbusiface_core *core, pa_source *source) {
+ pa_dbusiface_device *d = NULL;
+
+ pa_assert(core);
+ pa_assert(source);
+
+ d = pa_xnew0(pa_dbusiface_device, 1);
+ d->core = core;
+ d->source = pa_source_ref(source);
+ d->type = DEVICE_TYPE_SOURCE;
+ d->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, SOURCE_OBJECT_NAME, source->index);
+ d->volume = *pa_source_get_volume(source, FALSE);
+ d->mute = pa_source_get_mute(source, FALSE);
+ d->source_state = pa_source_get_state(source);
+ d->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ d->next_port_index = 0;
+ d->active_port = NULL;
+ d->proplist = pa_proplist_copy(source->proplist);
+ d->dbus_protocol = pa_dbus_protocol_get(source->core);
+ d->subscription = pa_subscription_new(source->core, PA_SUBSCRIPTION_MASK_SOURCE, subscription_cb, d);
+
+ if (source->ports) {
+ pa_device_port *port;
+ void *state = NULL;
+
+ PA_HASHMAP_FOREACH(port, source->ports, state) {
+ pa_dbusiface_device_port *p = pa_dbusiface_device_port_new(d, source->core, port, d->next_port_index++);
+ pa_hashmap_put(d->ports, pa_dbusiface_device_port_get_name(p), p);
+ }
+ pa_assert_se(d->active_port = source->active_port);
+ }
+
+ pa_assert_se(pa_dbus_protocol_add_interface(d->dbus_protocol, d->path, &device_interface_info, d) >= 0);
+ pa_assert_se(pa_dbus_protocol_add_interface(d->dbus_protocol, d->path, &source_interface_info, d) >= 0);
+
+ return d;
+}
+
+static void port_free_cb(void *p, void *userdata) {
+ pa_dbusiface_device_port *port = p;
+
+ pa_assert(port);
+
+ pa_dbusiface_device_port_free(port);
+}
+
+void pa_dbusiface_device_free(pa_dbusiface_device *d) {
+ pa_assert(d);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(d->dbus_protocol, d->path, device_interface_info.name) >= 0);
+
+ if (d->type == DEVICE_TYPE_SINK) {
+ pa_assert_se(pa_dbus_protocol_remove_interface(d->dbus_protocol, d->path, sink_interface_info.name) >= 0);
+ pa_sink_unref(d->sink);
+
+ } else {
+ pa_assert_se(pa_dbus_protocol_remove_interface(d->dbus_protocol, d->path, source_interface_info.name) >= 0);
+ pa_source_unref(d->source);
+ }
+ pa_hashmap_free(d->ports, port_free_cb, NULL);
+ pa_proplist_free(d->proplist);
+ pa_dbus_protocol_unref(d->dbus_protocol);
+ pa_subscription_free(d->subscription);
+
+ pa_xfree(d->path);
+ pa_xfree(d);
+}
+
+const char *pa_dbusiface_device_get_path(pa_dbusiface_device *d) {
+ pa_assert(d);
+
+ return d->path;
+}
+
+pa_sink *pa_dbusiface_device_get_sink(pa_dbusiface_device *d) {
+ pa_assert(d);
+ pa_assert(d->type == DEVICE_TYPE_SINK);
+
+ return d->sink;
+}
+
+pa_source *pa_dbusiface_device_get_source(pa_dbusiface_device *d) {
+ pa_assert(d);
+ pa_assert(d->type == DEVICE_TYPE_SOURCE);
+
+ return d->source;
+}
diff --git a/src/modules/dbus/iface-device.h b/src/modules/dbus/iface-device.h
new file mode 100644
index 00000000..62e05e9a
--- /dev/null
+++ b/src/modules/dbus/iface-device.h
@@ -0,0 +1,53 @@
+#ifndef foodbusifacedevicehfoo
+#define foodbusifacedevicehfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interfaces org.PulseAudio.Core1.Device,
+ * org.PulseAudio.Core1.Sink and org.PulseAudio.Core1.Source.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the interface
+ * documentation.
+ */
+
+#include <pulsecore/protocol-dbus.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/source.h>
+
+#include "iface-core.h"
+
+#define PA_DBUSIFACE_DEVICE_INTERFACE PA_DBUS_CORE_INTERFACE ".Device"
+#define PA_DBUSIFACE_SINK_INTERFACE PA_DBUS_CORE_INTERFACE ".Sink"
+#define PA_DBUSIFACE_SOURCE_INTERFACE PA_DBUS_CORE_INTERFACE ".Source"
+
+typedef struct pa_dbusiface_device pa_dbusiface_device;
+
+pa_dbusiface_device *pa_dbusiface_device_new_sink(pa_dbusiface_core *core, pa_sink *sink);
+pa_dbusiface_device *pa_dbusiface_device_new_source(pa_dbusiface_core *core, pa_source *source);
+void pa_dbusiface_device_free(pa_dbusiface_device *d);
+
+const char *pa_dbusiface_device_get_path(pa_dbusiface_device *d);
+
+pa_sink *pa_dbusiface_device_get_sink(pa_dbusiface_device *d);
+pa_source *pa_dbusiface_device_get_source(pa_dbusiface_device *d);
+
+#endif
diff --git a/src/modules/dbus/iface-memstats.c b/src/modules/dbus/iface-memstats.c
new file mode 100644
index 00000000..4cd692db
--- /dev/null
+++ b/src/modules/dbus/iface-memstats.c
@@ -0,0 +1,230 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <dbus/dbus.h>
+
+#include <pulsecore/core-scache.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-memstats.h"
+
+#define OBJECT_NAME "memstats"
+
+static void handle_get_current_memblocks(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_current_memblocks_size(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_accumulated_memblocks(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_accumulated_memblocks_size(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sample_cache_size(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+struct pa_dbusiface_memstats {
+ pa_core *core;
+ char *path;
+ pa_dbus_protocol *dbus_protocol;
+};
+
+enum property_handler_index {
+ PROPERTY_HANDLER_CURRENT_MEMBLOCKS,
+ PROPERTY_HANDLER_CURRENT_MEMBLOCKS_SIZE,
+ PROPERTY_HANDLER_ACCUMULATED_MEMBLOCKS,
+ PROPERTY_HANDLER_ACCUMULATED_MEMBLOCKS_SIZE,
+ PROPERTY_HANDLER_SAMPLE_CACHE_SIZE,
+ PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_CURRENT_MEMBLOCKS] = { .property_name = "CurrentMemblocks", .type = "u", .get_cb = handle_get_current_memblocks, .set_cb = NULL },
+ [PROPERTY_HANDLER_CURRENT_MEMBLOCKS_SIZE] = { .property_name = "CurrentMemblocksSize", .type = "u", .get_cb = handle_get_current_memblocks_size, .set_cb = NULL },
+ [PROPERTY_HANDLER_ACCUMULATED_MEMBLOCKS] = { .property_name = "AccumulatedMemblocks", .type = "u", .get_cb = handle_get_accumulated_memblocks, .set_cb = NULL },
+ [PROPERTY_HANDLER_ACCUMULATED_MEMBLOCKS_SIZE] = { .property_name = "AccumulatedMemblocksSize", .type = "u", .get_cb = handle_get_accumulated_memblocks_size, .set_cb = NULL },
+ [PROPERTY_HANDLER_SAMPLE_CACHE_SIZE] = { .property_name = "SampleCacheSize", .type = "u", .get_cb = handle_get_sample_cache_size, .set_cb = NULL }
+};
+
+static pa_dbus_interface_info memstats_interface_info = {
+ .name = PA_DBUSIFACE_MEMSTATS_INTERFACE,
+ .method_handlers = NULL,
+ .n_method_handlers = 0,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = NULL,
+ .n_signals = 0
+};
+
+static void handle_get_current_memblocks(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_memstats *m = userdata;
+ const pa_mempool_stat *stat;
+ dbus_uint32_t current_memblocks;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ stat = pa_mempool_get_stat(m->core->mempool);
+
+ current_memblocks = pa_atomic_load(&stat->n_allocated);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &current_memblocks);
+}
+
+static void handle_get_current_memblocks_size(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_memstats *m = userdata;
+ const pa_mempool_stat *stat;
+ dbus_uint32_t current_memblocks_size;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ stat = pa_mempool_get_stat(m->core->mempool);
+
+ current_memblocks_size = pa_atomic_load(&stat->allocated_size);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &current_memblocks_size);
+}
+
+static void handle_get_accumulated_memblocks(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_memstats *m = userdata;
+ const pa_mempool_stat *stat;
+ dbus_uint32_t accumulated_memblocks;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ stat = pa_mempool_get_stat(m->core->mempool);
+
+ accumulated_memblocks = pa_atomic_load(&stat->n_accumulated);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &accumulated_memblocks);
+}
+
+static void handle_get_accumulated_memblocks_size(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_memstats *m = userdata;
+ const pa_mempool_stat *stat;
+ dbus_uint32_t accumulated_memblocks_size;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ stat = pa_mempool_get_stat(m->core->mempool);
+
+ accumulated_memblocks_size = pa_atomic_load(&stat->accumulated_size);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &accumulated_memblocks_size);
+}
+
+static void handle_get_sample_cache_size(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_memstats *m = userdata;
+ dbus_uint32_t sample_cache_size;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ sample_cache_size = pa_scache_total_size(m->core);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sample_cache_size);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_memstats *m = userdata;
+ const pa_mempool_stat *stat;
+ dbus_uint32_t current_memblocks;
+ dbus_uint32_t current_memblocks_size;
+ dbus_uint32_t accumulated_memblocks;
+ dbus_uint32_t accumulated_memblocks_size;
+ dbus_uint32_t sample_cache_size;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ stat = pa_mempool_get_stat(m->core->mempool);
+
+ current_memblocks = pa_atomic_load(&stat->n_allocated);
+ current_memblocks_size = pa_atomic_load(&stat->allocated_size);
+ accumulated_memblocks = pa_atomic_load(&stat->n_accumulated);
+ accumulated_memblocks_size = pa_atomic_load(&stat->accumulated_size);
+ sample_cache_size = pa_scache_total_size(m->core);
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CURRENT_MEMBLOCKS].property_name, DBUS_TYPE_UINT32, &current_memblocks);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CURRENT_MEMBLOCKS_SIZE].property_name, DBUS_TYPE_UINT32, &current_memblocks_size);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_ACCUMULATED_MEMBLOCKS].property_name, DBUS_TYPE_UINT32, &accumulated_memblocks);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_ACCUMULATED_MEMBLOCKS_SIZE].property_name, DBUS_TYPE_UINT32, &accumulated_memblocks_size);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLE_CACHE_SIZE].property_name, DBUS_TYPE_UINT32, &sample_cache_size);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+}
+
+pa_dbusiface_memstats *pa_dbusiface_memstats_new(pa_dbusiface_core *dbus_core, pa_core *core) {
+ pa_dbusiface_memstats *m;
+
+ pa_assert(dbus_core);
+ pa_assert(core);
+
+ m = pa_xnew(pa_dbusiface_memstats, 1);
+ m->core = core;
+ m->path = pa_sprintf_malloc("%s/%s", PA_DBUS_CORE_OBJECT_PATH, OBJECT_NAME);
+ m->dbus_protocol = pa_dbus_protocol_get(core);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(m->dbus_protocol, m->path, &memstats_interface_info, m) >= 0);
+
+ return m;
+}
+
+void pa_dbusiface_memstats_free(pa_dbusiface_memstats *m) {
+ pa_assert(m);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(m->dbus_protocol, m->path, memstats_interface_info.name) >= 0);
+
+ pa_xfree(m->path);
+
+ pa_dbus_protocol_unref(m->dbus_protocol);
+
+ pa_xfree(m);
+}
+
+const char *pa_dbusiface_memstats_get_path(pa_dbusiface_memstats *m) {
+ pa_assert(m);
+
+ return m->path;
+}
diff --git a/src/modules/dbus/iface-memstats.h b/src/modules/dbus/iface-memstats.h
new file mode 100644
index 00000000..0820e8fe
--- /dev/null
+++ b/src/modules/dbus/iface-memstats.h
@@ -0,0 +1,45 @@
+#ifndef foodbusifacememstatshfoo
+#define foodbusifacememstatshfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interface org.PulseAudio.Core1.Memstats.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the Memstats interface
+ * documentation.
+ */
+
+#include <pulsecore/core.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-core.h"
+
+#define PA_DBUSIFACE_MEMSTATS_INTERFACE PA_DBUS_CORE_INTERFACE ".Memstats"
+
+typedef struct pa_dbusiface_memstats pa_dbusiface_memstats;
+
+pa_dbusiface_memstats *pa_dbusiface_memstats_new(pa_dbusiface_core *dbus_core, pa_core *core);
+void pa_dbusiface_memstats_free(pa_dbusiface_memstats *m);
+
+const char *pa_dbusiface_memstats_get_path(pa_dbusiface_memstats *m);
+
+#endif
diff --git a/src/modules/dbus/iface-module.c b/src/modules/dbus/iface-module.c
new file mode 100644
index 00000000..9973166c
--- /dev/null
+++ b/src/modules/dbus/iface-module.c
@@ -0,0 +1,336 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-module.h"
+
+#define OBJECT_NAME "module"
+
+struct pa_dbusiface_module {
+ pa_module *module;
+ char *path;
+ pa_proplist *proplist;
+
+ pa_dbus_protocol *dbus_protocol;
+ pa_subscription *subscription;
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_arguments(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_usage_counter(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_unload(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INDEX,
+ PROPERTY_HANDLER_NAME,
+ PROPERTY_HANDLER_ARGUMENTS,
+ PROPERTY_HANDLER_USAGE_COUNTER,
+ PROPERTY_HANDLER_PROPERTY_LIST,
+ PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_get_index, .set_cb = NULL },
+ [PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_get_name, .set_cb = NULL },
+ [PROPERTY_HANDLER_ARGUMENTS] = { .property_name = "Arguments", .type = "a{ss}", .get_cb = handle_get_arguments, .set_cb = NULL },
+ [PROPERTY_HANDLER_USAGE_COUNTER] = { .property_name = "UsageCounter", .type = "u", .get_cb = handle_get_usage_counter, .set_cb = NULL },
+ [PROPERTY_HANDLER_PROPERTY_LIST] = { .property_name = "PropertyList", .type = "a{say}", .get_cb = handle_get_property_list, .set_cb = NULL }
+};
+
+enum method_handler_index {
+ METHOD_HANDLER_UNLOAD,
+ METHOD_HANDLER_MAX
+};
+
+static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
+ [METHOD_HANDLER_UNLOAD] = {
+ .method_name = "Unload",
+ .arguments = NULL,
+ .n_arguments = 0,
+ .receive_cb = handle_unload }
+};
+
+enum signal_index {
+ SIGNAL_PROPERTY_LIST_UPDATED,
+ SIGNAL_MAX
+};
+
+static pa_dbus_arg_info property_list_updated_args[] = { { "property_list", "a{say}", NULL } };
+
+static pa_dbus_signal_info signals[SIGNAL_MAX] = {
+ [SIGNAL_PROPERTY_LIST_UPDATED] = { .name = "PropertyListUpdated", .arguments = property_list_updated_args, .n_arguments = 1 }
+};
+
+static pa_dbus_interface_info module_interface_info = {
+ .name = PA_DBUSIFACE_MODULE_INTERFACE,
+ .method_handlers = method_handlers,
+ .n_method_handlers = METHOD_HANDLER_MAX,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = signals,
+ .n_signals = SIGNAL_MAX
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_module *m = userdata;
+ dbus_uint32_t idx = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ idx = m->module->index;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &idx);
+}
+
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_module *m = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &m->module->name);
+}
+
+static void append_modargs_variant(DBusMessageIter *iter, pa_dbusiface_module *m) {
+ pa_modargs *ma = NULL;
+ DBusMessageIter variant_iter;
+ DBusMessageIter dict_iter;
+ DBusMessageIter dict_entry_iter;
+ void *state = NULL;
+ const char *key = NULL;
+ const char *value = NULL;
+
+ pa_assert(iter);
+ pa_assert(m);
+
+ pa_assert_se(ma = pa_modargs_new(m->module->argument, NULL));
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a{ss}", &variant_iter));
+ pa_assert_se(dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY, "{ss}", &dict_iter));
+
+ for (state = NULL, key = pa_modargs_iterate(ma, &state); key; key = pa_modargs_iterate(ma, &state)) {
+ pa_assert_se(value = pa_modargs_get_value(ma, key, NULL));
+
+ pa_assert_se(dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
+
+ pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &key));
+ pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &value));
+
+ pa_assert_se(dbus_message_iter_close_container(&dict_iter, &dict_entry_iter));
+ }
+
+ pa_assert_se(dbus_message_iter_close_container(&variant_iter, &dict_iter));
+ pa_assert_se(dbus_message_iter_close_container(iter, &variant_iter));
+
+ pa_modargs_free(ma);
+}
+
+static void handle_get_arguments(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_module *m = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ pa_assert_se(reply = dbus_message_new_method_return(msg));
+ dbus_message_iter_init_append(reply, &msg_iter);
+ append_modargs_variant(&msg_iter, m);
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+ dbus_message_unref(reply);
+}
+
+static void handle_get_usage_counter(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_module *m = userdata;
+ int real_counter_value = -1;
+ dbus_uint32_t usage_counter = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ if (!m->module->get_n_used || (real_counter_value = m->module->get_n_used(m->module)) < 0) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Module %u (%s) doesn't have a usage counter.", m->module->index, m->module->name);
+ return;
+ }
+
+ usage_counter = real_counter_value;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &usage_counter);
+}
+
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_module *m = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ pa_dbus_send_proplist_variant_reply(conn, msg, m->proplist);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_module *m = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ DBusMessageIter dict_entry_iter;
+ dbus_uint32_t idx = 0;
+ int real_counter_value = -1;
+ dbus_uint32_t usage_counter = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ idx = m->module->index;
+ if (m->module->get_n_used && (real_counter_value = m->module->get_n_used(m->module)) >= 0)
+ usage_counter = real_counter_value;
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &idx);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &m->module->name);
+
+ pa_assert_se(dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
+ pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &property_handlers[PROPERTY_HANDLER_ARGUMENTS].property_name));
+ append_modargs_variant(&dict_entry_iter, m);
+ pa_assert_se(dbus_message_iter_close_container(&dict_iter, &dict_entry_iter));
+
+ if (real_counter_value >= 0)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_ARGUMENTS].property_name, DBUS_TYPE_UINT32, &usage_counter);
+
+ pa_dbus_append_proplist_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROPERTY_LIST].property_name, m->proplist);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+}
+
+static void handle_unload(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_module *m = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(m);
+
+ if (m->module->core->disallow_module_loading) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_ACCESS_DENIED, "The server is configured to disallow module unloading.");
+ return;
+ }
+
+ pa_module_unload_request(m->module, FALSE);
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ pa_dbusiface_module *m = userdata;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(core);
+ pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_MODULE);
+ pa_assert(m);
+
+ /* We can't use idx != m->module->index, because the m->module pointer may
+ * be stale at this point. */
+ if (pa_idxset_get_by_index(core->modules, idx) != m->module)
+ return;
+
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
+ return;
+
+ if (!pa_proplist_equal(m->proplist, m->module->proplist)) {
+ DBusMessageIter msg_iter;
+
+ pa_proplist_update(m->proplist, PA_UPDATE_SET, m->module->proplist);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(m->path,
+ PA_DBUSIFACE_MODULE_INTERFACE,
+ signals[SIGNAL_PROPERTY_LIST_UPDATED].name));
+ dbus_message_iter_init_append(signal_msg, &msg_iter);
+ pa_dbus_append_proplist(&msg_iter, m->proplist);
+
+ pa_dbus_protocol_send_signal(m->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+}
+
+pa_dbusiface_module *pa_dbusiface_module_new(pa_module *module) {
+ pa_dbusiface_module *m;
+
+ pa_assert(module);
+
+ m = pa_xnew0(pa_dbusiface_module, 1);
+ m->module = module;
+ m->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, OBJECT_NAME, module->index);
+ m->proplist = pa_proplist_copy(module->proplist);
+ m->dbus_protocol = pa_dbus_protocol_get(module->core);
+ m->subscription = pa_subscription_new(module->core, PA_SUBSCRIPTION_MASK_MODULE, subscription_cb, m);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(m->dbus_protocol, m->path, &module_interface_info, m) >= 0);
+
+ return m;
+}
+
+void pa_dbusiface_module_free(pa_dbusiface_module *m) {
+ pa_assert(m);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(m->dbus_protocol, m->path, module_interface_info.name) >= 0);
+
+ pa_proplist_free(m->proplist);
+ pa_dbus_protocol_unref(m->dbus_protocol);
+ pa_subscription_free(m->subscription);
+
+ pa_xfree(m->path);
+ pa_xfree(m);
+}
+
+const char *pa_dbusiface_module_get_path(pa_dbusiface_module *m) {
+ pa_assert(m);
+
+ return m->path;
+}
diff --git a/src/modules/dbus/iface-module.h b/src/modules/dbus/iface-module.h
new file mode 100644
index 00000000..68ca1de5
--- /dev/null
+++ b/src/modules/dbus/iface-module.h
@@ -0,0 +1,45 @@
+#ifndef foodbusifacemodulehfoo
+#define foodbusifacemodulehfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interface org.PulseAudio.Core1.Module.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the Module interface
+ * documentation.
+ */
+
+#include <pulsecore/module.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-core.h"
+
+#define PA_DBUSIFACE_MODULE_INTERFACE PA_DBUS_CORE_INTERFACE ".Module"
+
+typedef struct pa_dbusiface_module pa_dbusiface_module;
+
+pa_dbusiface_module *pa_dbusiface_module_new(pa_module *module);
+void pa_dbusiface_module_free(pa_dbusiface_module *m);
+
+const char *pa_dbusiface_module_get_path(pa_dbusiface_module *m);
+
+#endif
diff --git a/src/modules/dbus/iface-sample.c b/src/modules/dbus/iface-sample.c
new file mode 100644
index 00000000..93d4fc8c
--- /dev/null
+++ b/src/modules/dbus/iface-sample.c
@@ -0,0 +1,519 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-sample.h"
+
+#define OBJECT_NAME "sample"
+
+struct pa_dbusiface_sample {
+ pa_dbusiface_core *core;
+
+ pa_scache_entry *sample;
+ char *path;
+ pa_proplist *proplist;
+
+ pa_dbus_protocol *dbus_protocol;
+ pa_subscription *subscription;
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_channels(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_default_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_duration(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_bytes(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_play(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_play_to_sink(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_remove(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INDEX,
+ PROPERTY_HANDLER_NAME,
+ PROPERTY_HANDLER_SAMPLE_FORMAT,
+ PROPERTY_HANDLER_SAMPLE_RATE,
+ PROPERTY_HANDLER_CHANNELS,
+ PROPERTY_HANDLER_DEFAULT_VOLUME,
+ PROPERTY_HANDLER_DURATION,
+ PROPERTY_HANDLER_BYTES,
+ PROPERTY_HANDLER_PROPERTY_LIST,
+ PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_get_index, .set_cb = NULL },
+ [PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_get_name, .set_cb = NULL },
+ [PROPERTY_HANDLER_SAMPLE_FORMAT] = { .property_name = "SampleFormat", .type = "u", .get_cb = handle_get_sample_format, .set_cb = NULL },
+ [PROPERTY_HANDLER_SAMPLE_RATE] = { .property_name = "SampleRate", .type = "u", .get_cb = handle_get_sample_rate, .set_cb = NULL },
+ [PROPERTY_HANDLER_CHANNELS] = { .property_name = "Channels", .type = "au", .get_cb = handle_get_channels, .set_cb = NULL },
+ [PROPERTY_HANDLER_DEFAULT_VOLUME] = { .property_name = "DefaultVolume", .type = "au", .get_cb = handle_get_default_volume, .set_cb = NULL },
+ [PROPERTY_HANDLER_DURATION] = { .property_name = "Duration", .type = "t", .get_cb = handle_get_duration, .set_cb = NULL },
+ [PROPERTY_HANDLER_BYTES] = { .property_name = "Bytes", .type = "u", .get_cb = handle_get_bytes, .set_cb = NULL },
+ [PROPERTY_HANDLER_PROPERTY_LIST] = { .property_name = "PropertyList", .type = "a{say}", .get_cb = handle_get_property_list, .set_cb = NULL }
+};
+
+enum method_handler_index {
+ METHOD_HANDLER_PLAY,
+ METHOD_HANDLER_PLAY_TO_SINK,
+ METHOD_HANDLER_REMOVE,
+ METHOD_HANDLER_MAX
+};
+
+static pa_dbus_arg_info play_args[] = { { "volume", "u", "in" }, { "property_list", "a{say}", "in" } };
+static pa_dbus_arg_info play_to_sink_args[] = { { "sink", "o", "in" },
+ { "volume", "u", "in" },
+ { "property_list", "a{say}", "in" } };
+
+static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
+ [METHOD_HANDLER_PLAY] = {
+ .method_name = "Play",
+ .arguments = play_args,
+ .n_arguments = sizeof(play_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_play },
+ [METHOD_HANDLER_PLAY_TO_SINK] = {
+ .method_name = "PlayToSink",
+ .arguments = play_to_sink_args,
+ .n_arguments = sizeof(play_to_sink_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_play_to_sink },
+ [METHOD_HANDLER_REMOVE] = {
+ .method_name = "Remove",
+ .arguments = NULL,
+ .n_arguments = 0,
+ .receive_cb = handle_remove }
+};
+
+enum signal_index {
+ SIGNAL_PROPERTY_LIST_UPDATED,
+ SIGNAL_MAX
+};
+
+static pa_dbus_arg_info property_list_updated_args[] = { { "property_list", "a{say}", NULL } };
+
+static pa_dbus_signal_info signals[SIGNAL_MAX] = {
+ [SIGNAL_PROPERTY_LIST_UPDATED] = { .name = "PropertyListUpdated", .arguments = property_list_updated_args, .n_arguments = 1 }
+};
+
+static pa_dbus_interface_info sample_interface_info = {
+ .name = PA_DBUSIFACE_SAMPLE_INTERFACE,
+ .method_handlers = method_handlers,
+ .n_method_handlers = METHOD_HANDLER_MAX,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = signals,
+ .n_signals = SIGNAL_MAX
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ dbus_uint32_t idx = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ idx = s->sample->index;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &idx);
+}
+
+static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &s->sample->name);
+}
+
+static void handle_get_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ dbus_uint32_t sample_format = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (!s->sample->memchunk.memblock) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Sample %s isn't loaded into memory yet, so its sample format is unknown.", s->sample->name);
+ return;
+ }
+
+ sample_format = s->sample->sample_spec.format;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sample_format);
+}
+
+static void handle_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ dbus_uint32_t sample_rate = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (!s->sample->memchunk.memblock) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Sample %s isn't loaded into memory yet, so its sample rate is unknown.", s->sample->name);
+ return;
+ }
+
+ sample_rate = s->sample->sample_spec.rate;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sample_rate);
+}
+
+static void handle_get_channels(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ dbus_uint32_t channels[PA_CHANNELS_MAX];
+ unsigned i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (!s->sample->memchunk.memblock) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Sample %s isn't loaded into memory yet, so its channel map is unknown.", s->sample->name);
+ return;
+ }
+
+ for (i = 0; i < s->sample->channel_map.channels; ++i)
+ channels[i] = s->sample->channel_map.map[i];
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, channels, s->sample->channel_map.channels);
+}
+
+static void handle_get_default_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ dbus_uint32_t default_volume[PA_CHANNELS_MAX];
+ unsigned i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (!s->sample->volume_is_set) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Sample %s doesn't have default volume stored.", s->sample->name);
+ return;
+ }
+
+ for (i = 0; i < s->sample->volume.channels; ++i)
+ default_volume[i] = s->sample->volume.values[i];
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, default_volume, s->sample->volume.channels);
+}
+
+static void handle_get_duration(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ dbus_uint64_t duration = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (!s->sample->memchunk.memblock) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Sample %s isn't loaded into memory yet, so its duration is unknown.", s->sample->name);
+ return;
+ }
+
+ duration = pa_bytes_to_usec(s->sample->memchunk.length, &s->sample->sample_spec);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT64, &duration);
+}
+
+static void handle_get_bytes(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ dbus_uint32_t bytes = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (!s->sample->memchunk.memblock) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
+ "Sample %s isn't loaded into memory yet, so its size is unknown.", s->sample->name);
+ return;
+ }
+
+ bytes = s->sample->memchunk.length;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &bytes);
+}
+
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ pa_dbus_send_proplist_variant_reply(conn, msg, s->proplist);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ dbus_uint32_t idx = 0;
+ dbus_uint32_t sample_format = 0;
+ dbus_uint32_t sample_rate = 0;
+ dbus_uint32_t channels[PA_CHANNELS_MAX];
+ dbus_uint32_t default_volume[PA_CHANNELS_MAX];
+ dbus_uint64_t duration = 0;
+ dbus_uint32_t bytes = 0;
+ unsigned i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ idx = s->sample->index;
+ if (s->sample->memchunk.memblock) {
+ sample_format = s->sample->sample_spec.format;
+ sample_rate = s->sample->sample_spec.rate;
+ for (i = 0; i < s->sample->channel_map.channels; ++i)
+ channels[i] = s->sample->channel_map.map[i];
+ duration = pa_bytes_to_usec(s->sample->memchunk.length, &s->sample->sample_spec);
+ bytes = s->sample->memchunk.length;
+ }
+ if (s->sample->volume_is_set) {
+ for (i = 0; i < s->sample->volume.channels; ++i)
+ default_volume[i] = s->sample->volume.values[i];
+ }
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &idx);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &s->sample->name);
+
+ if (s->sample->memchunk.memblock) {
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLE_FORMAT].property_name, DBUS_TYPE_UINT32, &sample_format);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLE_RATE].property_name, DBUS_TYPE_UINT32, &sample_rate);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CHANNELS].property_name, DBUS_TYPE_UINT32, channels, s->sample->channel_map.channels);
+ }
+
+ if (s->sample->volume_is_set)
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEFAULT_VOLUME].property_name, DBUS_TYPE_UINT32, default_volume, s->sample->volume.channels);
+
+ if (s->sample->memchunk.memblock) {
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DURATION].property_name, DBUS_TYPE_UINT64, &duration);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_BYTES].property_name, DBUS_TYPE_UINT32, &bytes);
+ }
+
+ pa_dbus_append_proplist_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROPERTY_LIST].property_name, s->proplist);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+ dbus_message_unref(reply);
+}
+
+static void handle_play(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ DBusMessageIter msg_iter;
+ dbus_uint32_t volume = 0;
+ pa_proplist *property_list = NULL;
+ pa_sink *sink = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &volume);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ if (!(property_list = pa_dbus_get_proplist_arg(conn, msg, &msg_iter)))
+ return;
+
+ if (!PA_VOLUME_IS_VALID(volume)) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid volume.");
+ goto finish;
+ }
+
+ if (!(sink = pa_namereg_get_default_sink(s->sample->core))) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
+ "Can't play sample %s, because there are no sinks available.", s->sample->name);
+ goto finish;
+ }
+
+ if (pa_scache_play_item(s->sample->core, s->sample->name, sink, volume, property_list, NULL) < 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Playing sample %s failed.", s->sample->name);
+ goto finish;
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+finish:
+ if (property_list)
+ pa_proplist_free(property_list);
+}
+
+static void handle_play_to_sink(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ DBusMessageIter msg_iter;
+ const char *sink_path = NULL;
+ dbus_uint32_t volume = 0;
+ pa_proplist *property_list = NULL;
+ pa_sink *sink = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &sink_path);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &volume);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ if (!(property_list = pa_dbus_get_proplist_arg(conn, msg, &msg_iter)))
+ return;
+
+ if (!(sink = pa_dbusiface_core_get_sink(s->core, sink_path))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such sink.", sink_path);
+ goto finish;
+ }
+
+ if (!PA_VOLUME_IS_VALID(volume)) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid volume.");
+ goto finish;
+ }
+
+ if (pa_scache_play_item(s->sample->core, s->sample->name, sink, volume, property_list, NULL) < 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Playing sample %s failed.", s->sample->name);
+ goto finish;
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+finish:
+ if (property_list)
+ pa_proplist_free(property_list);
+}
+
+static void handle_remove(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (pa_scache_remove_item(s->sample->core, s->sample->name) < 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Removing sample %s failed.", s->sample->name);
+ return;
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void subscription_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ pa_dbusiface_sample *s = userdata;
+ DBusMessage *signal_msg = NULL;
+
+ pa_assert(c);
+ pa_assert(s);
+
+ /* We can't use idx != s->sample->index, because the s->sample pointer may
+ * be stale at this point. */
+ if (pa_idxset_get_by_index(c->scache, idx) != s->sample)
+ return;
+
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
+ return;
+
+ if (!pa_proplist_equal(s->proplist, s->sample->proplist)) {
+ DBusMessageIter msg_iter;
+
+ pa_proplist_update(s->proplist, PA_UPDATE_SET, s->sample->proplist);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(s->path,
+ PA_DBUSIFACE_SAMPLE_INTERFACE,
+ signals[SIGNAL_PROPERTY_LIST_UPDATED].name));
+ dbus_message_iter_init_append(signal_msg, &msg_iter);
+ pa_dbus_append_proplist(&msg_iter, s->proplist);
+
+ pa_dbus_protocol_send_signal(s->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+}
+
+pa_dbusiface_sample *pa_dbusiface_sample_new(pa_dbusiface_core *core, pa_scache_entry *sample) {
+ pa_dbusiface_sample *s = NULL;
+
+ pa_assert(core);
+ pa_assert(sample);
+
+ s = pa_xnew0(pa_dbusiface_sample, 1);
+ s->core = core;
+ s->sample = sample;
+ s->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, OBJECT_NAME, sample->index);
+ s->proplist = pa_proplist_copy(sample->proplist);
+ s->dbus_protocol = pa_dbus_protocol_get(sample->core);
+ s->subscription = pa_subscription_new(sample->core, PA_SUBSCRIPTION_MASK_SAMPLE_CACHE, subscription_cb, s);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(s->dbus_protocol, s->path, &sample_interface_info, s) >= 0);
+
+ return s;
+}
+
+void pa_dbusiface_sample_free(pa_dbusiface_sample *s) {
+ pa_assert(s);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(s->dbus_protocol, s->path, sample_interface_info.name) >= 0);
+
+ pa_proplist_free(s->proplist);
+ pa_dbus_protocol_unref(s->dbus_protocol);
+ pa_subscription_free(s->subscription);
+
+ pa_xfree(s->path);
+ pa_xfree(s);
+}
+
+const char *pa_dbusiface_sample_get_path(pa_dbusiface_sample *s) {
+ pa_assert(s);
+
+ return s->path;
+}
diff --git a/src/modules/dbus/iface-sample.h b/src/modules/dbus/iface-sample.h
new file mode 100644
index 00000000..f1947ce8
--- /dev/null
+++ b/src/modules/dbus/iface-sample.h
@@ -0,0 +1,45 @@
+#ifndef foodbusifacesamplehfoo
+#define foodbusifacesamplehfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interface org.PulseAudio.Core1.Sample.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the Sample interface
+ * documentation.
+ */
+
+#include <pulsecore/core-scache.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-core.h"
+
+#define PA_DBUSIFACE_SAMPLE_INTERFACE PA_DBUS_CORE_INTERFACE ".Sample"
+
+typedef struct pa_dbusiface_sample pa_dbusiface_sample;
+
+pa_dbusiface_sample *pa_dbusiface_sample_new(pa_dbusiface_core *core, pa_scache_entry *sample);
+void pa_dbusiface_sample_free(pa_dbusiface_sample *c);
+
+const char *pa_dbusiface_sample_get_path(pa_dbusiface_sample *c);
+
+#endif
diff --git a/src/modules/dbus/iface-stream.c b/src/modules/dbus/iface-stream.c
new file mode 100644
index 00000000..d9f12374
--- /dev/null
+++ b/src/modules/dbus/iface-stream.c
@@ -0,0 +1,933 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+ Copyright 2009 Vincent Filali-Ansary <filali.v@azurdigitalnetworks.net>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-stream.h"
+
+#define PLAYBACK_OBJECT_NAME "playback_stream"
+#define RECORD_OBJECT_NAME "record_stream"
+
+enum stream_type {
+ STREAM_TYPE_PLAYBACK,
+ STREAM_TYPE_RECORD
+};
+
+struct pa_dbusiface_stream {
+ pa_dbusiface_core *core;
+
+ union {
+ pa_sink_input *sink_input;
+ pa_source_output *source_output;
+ };
+ enum stream_type type;
+ char *path;
+ union {
+ pa_sink *sink;
+ pa_source *source;
+ };
+ uint32_t sample_rate;
+ pa_cvolume volume;
+ dbus_bool_t mute;
+ pa_proplist *proplist;
+
+ pa_bool_t has_volume;
+
+ pa_dbus_protocol *dbus_protocol;
+ pa_subscription *subscription;
+ pa_hook_slot *send_event_slot;
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_client(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_device(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_channels(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_mute(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_set_mute(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_get_buffer_latency(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_device_latency(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_resample_method(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_move(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_kill(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INDEX,
+ PROPERTY_HANDLER_DRIVER,
+ PROPERTY_HANDLER_OWNER_MODULE,
+ PROPERTY_HANDLER_CLIENT,
+ PROPERTY_HANDLER_DEVICE,
+ PROPERTY_HANDLER_SAMPLE_FORMAT,
+ PROPERTY_HANDLER_SAMPLE_RATE,
+ PROPERTY_HANDLER_CHANNELS,
+ PROPERTY_HANDLER_VOLUME,
+ PROPERTY_HANDLER_MUTE,
+ PROPERTY_HANDLER_BUFFER_LATENCY,
+ PROPERTY_HANDLER_DEVICE_LATENCY,
+ PROPERTY_HANDLER_RESAMPLE_METHOD,
+ PROPERTY_HANDLER_PROPERTY_LIST,
+ PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_get_index, .set_cb = NULL },
+ [PROPERTY_HANDLER_DRIVER] = { .property_name = "Driver", .type = "s", .get_cb = handle_get_driver, .set_cb = NULL },
+ [PROPERTY_HANDLER_OWNER_MODULE] = { .property_name = "OwnerModule", .type = "o", .get_cb = handle_get_owner_module, .set_cb = NULL },
+ [PROPERTY_HANDLER_CLIENT] = { .property_name = "Client", .type = "o", .get_cb = handle_get_client, .set_cb = NULL },
+ [PROPERTY_HANDLER_DEVICE] = { .property_name = "Device", .type = "o", .get_cb = handle_get_device, .set_cb = NULL },
+ [PROPERTY_HANDLER_SAMPLE_FORMAT] = { .property_name = "SampleFormat", .type = "u", .get_cb = handle_get_sample_format, .set_cb = NULL },
+ [PROPERTY_HANDLER_SAMPLE_RATE] = { .property_name = "SampleRate", .type = "u", .get_cb = handle_get_sample_rate, .set_cb = NULL },
+ [PROPERTY_HANDLER_CHANNELS] = { .property_name = "Channels", .type = "au", .get_cb = handle_get_channels, .set_cb = NULL },
+ [PROPERTY_HANDLER_VOLUME] = { .property_name = "Volume", .type = "au", .get_cb = handle_get_volume, .set_cb = handle_set_volume },
+ [PROPERTY_HANDLER_MUTE] = { .property_name = "Mute", .type = "b", .get_cb = handle_get_mute, .set_cb = handle_set_mute },
+ [PROPERTY_HANDLER_BUFFER_LATENCY] = { .property_name = "BufferLatency", .type = "t", .get_cb = handle_get_buffer_latency, .set_cb = NULL },
+ [PROPERTY_HANDLER_DEVICE_LATENCY] = { .property_name = "DeviceLatency", .type = "t", .get_cb = handle_get_device_latency, .set_cb = NULL },
+ [PROPERTY_HANDLER_RESAMPLE_METHOD] = { .property_name = "ResampleMethod", .type = "s", .get_cb = handle_get_resample_method, .set_cb = NULL },
+ [PROPERTY_HANDLER_PROPERTY_LIST] = { .property_name = "PropertyList", .type = "a{say}", .get_cb = handle_get_property_list, .set_cb = NULL }
+};
+
+enum method_handler_index {
+ METHOD_HANDLER_MOVE,
+ METHOD_HANDLER_KILL,
+ METHOD_HANDLER_MAX
+};
+
+static pa_dbus_arg_info move_args[] = { { "device", "o", "in" } };
+
+static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
+ [METHOD_HANDLER_MOVE] = {
+ .method_name = "Move",
+ .arguments = move_args,
+ .n_arguments = sizeof(move_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_move },
+ [METHOD_HANDLER_KILL] = {
+ .method_name = "Kill",
+ .arguments = NULL,
+ .n_arguments = 0,
+ .receive_cb = handle_kill }
+};
+
+enum signal_index {
+ SIGNAL_DEVICE_UPDATED,
+ SIGNAL_SAMPLE_RATE_UPDATED,
+ SIGNAL_VOLUME_UPDATED,
+ SIGNAL_MUTE_UPDATED,
+ SIGNAL_PROPERTY_LIST_UPDATED,
+ SIGNAL_STREAM_EVENT,
+ SIGNAL_MAX
+};
+
+static pa_dbus_arg_info device_updated_args[] = { { "device", "o", NULL } };
+static pa_dbus_arg_info sample_rate_updated_args[] = { { "sample_rate", "u", NULL } };
+static pa_dbus_arg_info volume_updated_args[] = { { "volume", "au", NULL } };
+static pa_dbus_arg_info mute_updated_args[] = { { "muted", "b", NULL } };
+static pa_dbus_arg_info property_list_updated_args[] = { { "property_list", "a{say}", NULL } };
+static pa_dbus_arg_info stream_event_args[] = { { "name", "s", NULL }, { "property_list", "a{say}", NULL } };
+
+static pa_dbus_signal_info signals[SIGNAL_MAX] = {
+ [SIGNAL_DEVICE_UPDATED] = { .name = "DeviceUpdated", .arguments = device_updated_args, .n_arguments = 1 },
+ [SIGNAL_SAMPLE_RATE_UPDATED] = { .name = "SampleRateUpdated", .arguments = sample_rate_updated_args, .n_arguments = 1 },
+ [SIGNAL_VOLUME_UPDATED] = { .name = "VolumeUpdated", .arguments = volume_updated_args, .n_arguments = 1 },
+ [SIGNAL_MUTE_UPDATED] = { .name = "MuteUpdated", .arguments = mute_updated_args, .n_arguments = 1 },
+ [SIGNAL_PROPERTY_LIST_UPDATED] = { .name = "PropertyListUpdated", .arguments = property_list_updated_args, .n_arguments = 1 },
+ [SIGNAL_STREAM_EVENT] = { .name = "StreamEvent", .arguments = stream_event_args, .n_arguments = sizeof(stream_event_args) / sizeof(pa_dbus_arg_info) }
+};
+
+static pa_dbus_interface_info stream_interface_info = {
+ .name = PA_DBUSIFACE_STREAM_INTERFACE,
+ .method_handlers = method_handlers,
+ .n_method_handlers = METHOD_HANDLER_MAX,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = signals,
+ .n_signals = SIGNAL_MAX
+};
+
+static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ dbus_uint32_t idx;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ idx = (s->type == STREAM_TYPE_PLAYBACK) ? s->sink_input->index : s->source_output->index;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &idx);
+}
+
+/* The returned string has to be freed with pa_xfree() by the caller. */
+static char *stream_to_string(pa_dbusiface_stream *s) {
+ if (s->type == STREAM_TYPE_PLAYBACK)
+ return pa_sprintf_malloc("Playback stream %u", (unsigned) s->sink_input->index);
+ else
+ return pa_sprintf_malloc("Record stream %u", (unsigned) s->source_output->index);
+}
+
+static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ const char *driver = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ driver = (s->type == STREAM_TYPE_PLAYBACK) ? s->sink_input->driver : s->source_output->driver;
+
+ if (!driver) {
+ char *str = stream_to_string(s);
+
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "%s doesn't have a driver.", str);
+ pa_xfree(str);
+
+ return;
+ }
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &driver);
+}
+
+static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ pa_module *owner_module = NULL;
+ const char *object_path = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ owner_module = (s->type == STREAM_TYPE_PLAYBACK) ? s->sink_input->module : s->source_output->module;
+
+ if (!owner_module) {
+ char *str = stream_to_string(s);
+
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "%s doesn't have an owner module.", str);
+ pa_xfree(str);
+
+ return;
+ }
+
+ object_path = pa_dbusiface_core_get_module_path(s->core, owner_module);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_get_client(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ pa_client *client = NULL;
+ const char *object_path = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ client = (s->type == STREAM_TYPE_PLAYBACK) ? s->sink_input->client : s->source_output->client;
+
+ if (!client) {
+ char *str = stream_to_string(s);
+
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "%s isn't associated to any client.", str);
+ pa_xfree(str);
+
+ return;
+ }
+
+ object_path = pa_dbusiface_core_get_client_path(s->core, client);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
+}
+
+static void handle_get_device(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ const char *device = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (s->type == STREAM_TYPE_PLAYBACK)
+ device = pa_dbusiface_core_get_sink_path(s->core, s->sink);
+ else
+ device = pa_dbusiface_core_get_source_path(s->core, s->source);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &device);
+}
+
+static void handle_get_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ dbus_uint32_t sample_format = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ sample_format = (s->type == STREAM_TYPE_PLAYBACK)
+ ? s->sink_input->sample_spec.format
+ : s->source_output->sample_spec.format;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sample_format);
+}
+
+static void handle_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &s->sample_rate);
+}
+
+static void handle_get_channels(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ pa_channel_map *channel_map = NULL;
+ dbus_uint32_t channels[PA_CHANNELS_MAX];
+ unsigned i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ channel_map = (s->type == STREAM_TYPE_PLAYBACK) ? &s->sink_input->channel_map : &s->source_output->channel_map;
+
+ for (i = 0; i < channel_map->channels; ++i)
+ channels[i] = channel_map->map[i];
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, channels, channel_map->channels);
+}
+
+static void handle_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ dbus_uint32_t volume[PA_CHANNELS_MAX];
+ unsigned i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (!s->has_volume) {
+ char *str = stream_to_string(s);
+
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "%s doesn't have volume.", str);
+ pa_xfree(str);
+
+ return;
+ }
+
+ for (i = 0; i < s->volume.channels; ++i)
+ volume[i] = s->volume.values[i];
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, volume, s->volume.channels);
+}
+
+static void handle_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ pa_bool_t volume_writable = TRUE;
+ DBusMessageIter array_iter;
+ int stream_channels = 0;
+ dbus_uint32_t *volume = NULL;
+ int n_volume_entries = 0;
+ pa_cvolume new_vol;
+ int i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(s);
+
+ volume_writable = (s->type == STREAM_TYPE_PLAYBACK) ? s->sink_input->volume_writable : FALSE;
+
+ if (!s->has_volume || !volume_writable) {
+ char *str = stream_to_string(s);
+
+ if (!s->has_volume)
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "%s doesn't have volume.", str);
+ else if (!volume_writable)
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_ACCESS_DENIED, "%s has read-only volume.", str);
+ pa_xfree(str);
+
+ return;
+ }
+
+ stream_channels = s->sink_input->channel_map.channels;
+
+ dbus_message_iter_recurse(iter, &array_iter);
+ dbus_message_iter_get_fixed_array(&array_iter, &volume, &n_volume_entries);
+
+ if (n_volume_entries != stream_channels && n_volume_entries != 1) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
+ "Expected %u volume entries, got %u.", stream_channels, n_volume_entries);
+ return;
+ }
+
+ pa_cvolume_init(&new_vol);
+ new_vol.channels = n_volume_entries;
+
+ for (i = 0; i < n_volume_entries; ++i) {
+ if (!PA_VOLUME_IS_VALID(volume[i])) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid volume: %u", volume[i]);
+ return;
+ }
+ new_vol.values[i] = volume[i];
+ }
+
+ pa_sink_input_set_volume(s->sink_input, &new_vol, TRUE, TRUE);
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void handle_get_mute(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (s->type == STREAM_TYPE_RECORD) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "Record streams don't have mute.");
+ return;
+ }
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &s->mute);
+}
+
+static void handle_set_mute(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ dbus_bool_t mute = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(s);
+
+ dbus_message_iter_get_basic(iter, &mute);
+
+ if (s->type == STREAM_TYPE_RECORD) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "Record streams don't have mute.");
+ return;
+ }
+
+ pa_sink_input_set_mute(s->sink_input, mute, TRUE);
+
+ pa_dbus_send_empty_reply(conn, msg);
+};
+
+static void handle_get_buffer_latency(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ dbus_uint64_t buffer_latency = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (s->type == STREAM_TYPE_PLAYBACK)
+ buffer_latency = pa_sink_input_get_latency(s->sink_input, NULL);
+ else
+ buffer_latency = pa_source_output_get_latency(s->source_output, NULL);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT64, &buffer_latency);
+}
+
+static void handle_get_device_latency(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ dbus_uint64_t device_latency = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (s->type == STREAM_TYPE_PLAYBACK)
+ pa_sink_input_get_latency(s->sink_input, &device_latency);
+ else
+ pa_source_output_get_latency(s->source_output, &device_latency);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT64, &device_latency);
+}
+
+static void handle_get_resample_method(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ const char *resample_method = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (s->type == STREAM_TYPE_PLAYBACK)
+ resample_method = pa_resample_method_to_string(s->sink_input->actual_resample_method);
+ else
+ resample_method = pa_resample_method_to_string(s->source_output->actual_resample_method);
+
+ if (!resample_method)
+ resample_method = "";
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &resample_method);
+}
+
+static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ pa_dbus_send_proplist_variant_reply(conn, msg, s->proplist);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ dbus_uint32_t idx = 0;
+ const char *driver = NULL;
+ pa_module *owner_module = NULL;
+ const char *owner_module_path = NULL;
+ pa_client *client = NULL;
+ const char *client_path = NULL;
+ const char *device = NULL;
+ dbus_uint32_t sample_format = 0;
+ pa_channel_map *channel_map = NULL;
+ dbus_uint32_t channels[PA_CHANNELS_MAX];
+ dbus_uint32_t volume[PA_CHANNELS_MAX];
+ dbus_uint64_t buffer_latency = 0;
+ dbus_uint64_t device_latency = 0;
+ const char *resample_method = NULL;
+ unsigned i = 0;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (s->has_volume) {
+ for (i = 0; i < s->volume.channels; ++i)
+ volume[i] = s->volume.values[i];
+ }
+
+ if (s->type == STREAM_TYPE_PLAYBACK) {
+ idx = s->sink_input->index;
+ driver = s->sink_input->driver;
+ owner_module = s->sink_input->module;
+ client = s->sink_input->client;
+ device = pa_dbusiface_core_get_sink_path(s->core, s->sink);
+ sample_format = s->sink_input->sample_spec.format;
+ channel_map = &s->sink_input->channel_map;
+ buffer_latency = pa_sink_input_get_latency(s->sink_input, &device_latency);
+ resample_method = pa_resample_method_to_string(s->sink_input->actual_resample_method);
+ } else {
+ idx = s->source_output->index;
+ driver = s->source_output->driver;
+ owner_module = s->source_output->module;
+ client = s->source_output->client;
+ device = pa_dbusiface_core_get_source_path(s->core, s->source);
+ sample_format = s->source_output->sample_spec.format;
+ channel_map = &s->source_output->channel_map;
+ buffer_latency = pa_source_output_get_latency(s->source_output, &device_latency);
+ resample_method = pa_resample_method_to_string(s->source_output->actual_resample_method);
+ }
+ if (owner_module)
+ owner_module_path = pa_dbusiface_core_get_module_path(s->core, owner_module);
+ if (client)
+ client_path = pa_dbusiface_core_get_client_path(s->core, client);
+ for (i = 0; i < channel_map->channels; ++i)
+ channels[i] = channel_map->map[i];
+ if (!resample_method)
+ resample_method = "";
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &idx);
+
+ if (driver)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DRIVER].property_name, DBUS_TYPE_STRING, &driver);
+
+ if (owner_module)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_OWNER_MODULE].property_name, DBUS_TYPE_OBJECT_PATH, &owner_module_path);
+
+ if (client)
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CLIENT].property_name, DBUS_TYPE_OBJECT_PATH, &client_path);
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEVICE].property_name, DBUS_TYPE_OBJECT_PATH, &device);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLE_FORMAT].property_name, DBUS_TYPE_UINT32, &sample_format);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLE_RATE].property_name, DBUS_TYPE_UINT32, &s->sample_rate);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CHANNELS].property_name, DBUS_TYPE_UINT32, channels, channel_map->channels);
+
+ if (s->has_volume) {
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_VOLUME].property_name, DBUS_TYPE_UINT32, volume, s->volume.channels);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_MUTE].property_name, DBUS_TYPE_BOOLEAN, &s->mute);
+ }
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_BUFFER_LATENCY].property_name, DBUS_TYPE_UINT64, &buffer_latency);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEVICE_LATENCY].property_name, DBUS_TYPE_UINT64, &device_latency);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_RESAMPLE_METHOD].property_name, DBUS_TYPE_STRING, &resample_method);
+ pa_dbus_append_proplist_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROPERTY_LIST].property_name, s->proplist);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+ dbus_message_unref(reply);
+}
+
+static void handle_move(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ const char *device = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &device, DBUS_TYPE_INVALID));
+
+ if (s->type == STREAM_TYPE_PLAYBACK) {
+ pa_sink *sink = pa_dbusiface_core_get_sink(s->core, device);
+
+ if (!sink) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such sink.", device);
+ return;
+ }
+
+ if (pa_sink_input_move_to(s->sink_input, sink, TRUE) < 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
+ "Moving playback stream %u to sink %s failed.", s->sink_input->index, sink->name);
+ return;
+ }
+ } else {
+ pa_source *source = pa_dbusiface_core_get_source(s->core, device);
+
+ if (!source) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such source.", device);
+ return;
+ }
+
+ if (pa_source_output_move_to(s->source_output, source, TRUE) < 0) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
+ "Moving record stream %u to source %s failed.", s->source_output->index, source->name);
+ return;
+ }
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void handle_kill(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(s);
+
+ if (s->type == STREAM_TYPE_PLAYBACK)
+ pa_sink_input_kill(s->sink_input);
+ else
+ pa_source_output_kill(s->source_output);
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+static void subscription_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ pa_dbusiface_stream *s = userdata;
+ DBusMessage *signal_msg = NULL;
+ const char *new_device_path = NULL;
+ uint32_t new_sample_rate = 0;
+ pa_proplist *new_proplist = NULL;
+ unsigned i = 0;
+
+ pa_assert(c);
+ pa_assert(s);
+
+ if ((s->type == STREAM_TYPE_PLAYBACK && idx != s->sink_input->index)
+ || (s->type == STREAM_TYPE_RECORD && idx != s->source_output->index))
+ return;
+
+ if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
+ return;
+
+ pa_assert(((s->type == STREAM_TYPE_PLAYBACK)
+ && ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT))
+ || ((s->type == STREAM_TYPE_RECORD)
+ && ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT)));
+
+ if (s->type == STREAM_TYPE_PLAYBACK) {
+ pa_sink *new_sink = s->sink_input->sink;
+
+ if (s->sink != new_sink) {
+ pa_sink_unref(s->sink);
+ s->sink = pa_sink_ref(new_sink);
+
+ new_device_path = pa_dbusiface_core_get_sink_path(s->core, new_sink);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(s->path,
+ PA_DBUSIFACE_STREAM_INTERFACE,
+ signals[SIGNAL_DEVICE_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &new_device_path, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(s->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+ } else {
+ pa_source *new_source = s->source_output->source;
+
+ if (s->source != new_source) {
+ pa_source_unref(s->source);
+ s->source = pa_source_ref(new_source);
+
+ new_device_path = pa_dbusiface_core_get_source_path(s->core, new_source);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(s->path,
+ PA_DBUSIFACE_STREAM_INTERFACE,
+ signals[SIGNAL_DEVICE_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &new_device_path, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(s->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+ }
+
+ new_sample_rate = (s->type == STREAM_TYPE_PLAYBACK) ? s->sink_input->sample_spec.rate : s->source_output->sample_spec.rate;
+
+ if (s->sample_rate != new_sample_rate) {
+ s->sample_rate = new_sample_rate;
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(s->path,
+ PA_DBUSIFACE_STREAM_INTERFACE,
+ signals[SIGNAL_SAMPLE_RATE_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_UINT32, &s->sample_rate, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(s->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+
+ if (s->type == STREAM_TYPE_PLAYBACK) {
+ pa_bool_t new_mute = FALSE;
+
+ if (s->has_volume) {
+ pa_cvolume new_volume;
+
+ pa_sink_input_get_volume(s->sink_input, &new_volume, TRUE);
+
+ if (!pa_cvolume_equal(&s->volume, &new_volume)) {
+ dbus_uint32_t volume[PA_CHANNELS_MAX];
+ dbus_uint32_t *volume_ptr = volume;
+
+ s->volume = new_volume;
+
+ for (i = 0; i < s->volume.channels; ++i)
+ volume[i] = s->volume.values[i];
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(s->path,
+ PA_DBUSIFACE_STREAM_INTERFACE,
+ signals[SIGNAL_VOLUME_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &volume_ptr, s->volume.channels,
+ DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(s->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+ }
+
+ new_mute = pa_sink_input_get_mute(s->sink_input);
+
+ if (s->mute != new_mute) {
+ s->mute = new_mute;
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(s->path,
+ PA_DBUSIFACE_STREAM_INTERFACE,
+ signals[SIGNAL_MUTE_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_BOOLEAN, &s->mute, DBUS_TYPE_INVALID));
+
+ pa_dbus_protocol_send_signal(s->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+ }
+
+ new_proplist = (s->type == STREAM_TYPE_PLAYBACK) ? s->sink_input->proplist : s->source_output->proplist;
+
+ if (!pa_proplist_equal(s->proplist, new_proplist)) {
+ DBusMessageIter msg_iter;
+
+ pa_proplist_update(s->proplist, PA_UPDATE_SET, new_proplist);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(s->path,
+ PA_DBUSIFACE_STREAM_INTERFACE,
+ signals[SIGNAL_PROPERTY_LIST_UPDATED].name));
+ dbus_message_iter_init_append(signal_msg, &msg_iter);
+ pa_dbus_append_proplist(&msg_iter, s->proplist);
+
+ pa_dbus_protocol_send_signal(s->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+ signal_msg = NULL;
+ }
+}
+
+static pa_hook_result_t send_event_cb(void *hook_data, void *call_data, void *slot_data) {
+ pa_dbusiface_stream *s = slot_data;
+ DBusMessage *signal_msg = NULL;
+ DBusMessageIter msg_iter;
+ const char *name = NULL;
+ pa_proplist *property_list = NULL;
+
+ pa_assert(call_data);
+ pa_assert(s);
+
+ if (s->type == STREAM_TYPE_PLAYBACK) {
+ pa_sink_input_send_event_hook_data *data = call_data;
+
+ if (data->sink_input != s->sink_input)
+ return PA_HOOK_OK;
+
+ name = data->event;
+ property_list = data->data;
+ } else {
+ pa_source_output_send_event_hook_data *data = call_data;
+
+ if (data->source_output != s->source_output)
+ return PA_HOOK_OK;
+
+ name = data->event;
+ property_list = data->data;
+ }
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(s->path,
+ PA_DBUSIFACE_STREAM_INTERFACE,
+ signals[SIGNAL_STREAM_EVENT].name));
+ dbus_message_iter_init_append(signal_msg, &msg_iter);
+ pa_assert_se(dbus_message_iter_append_basic(&msg_iter, DBUS_TYPE_STRING, &name));
+ pa_dbus_append_proplist(&msg_iter, property_list);
+
+ pa_dbus_protocol_send_signal(s->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+
+ return PA_HOOK_OK;
+}
+
+pa_dbusiface_stream *pa_dbusiface_stream_new_playback(pa_dbusiface_core *core, pa_sink_input *sink_input) {
+ pa_dbusiface_stream *s;
+
+ pa_assert(core);
+ pa_assert(sink_input);
+
+ s = pa_xnew(pa_dbusiface_stream, 1);
+ s->core = core;
+ s->sink_input = pa_sink_input_ref(sink_input);
+ s->type = STREAM_TYPE_PLAYBACK;
+ s->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, PLAYBACK_OBJECT_NAME, sink_input->index);
+ s->sink = pa_sink_ref(sink_input->sink);
+ s->sample_rate = sink_input->sample_spec.rate;
+ s->has_volume = pa_sink_input_is_volume_readable(sink_input);
+
+ if (s->has_volume)
+ pa_sink_input_get_volume(sink_input, &s->volume, TRUE);
+ else
+ pa_cvolume_init(&s->volume);
+
+ s->mute = pa_sink_input_get_mute(sink_input);
+ s->proplist = pa_proplist_copy(sink_input->proplist);
+ s->dbus_protocol = pa_dbus_protocol_get(sink_input->core);
+ s->subscription = pa_subscription_new(sink_input->core, PA_SUBSCRIPTION_MASK_SINK_INPUT, subscription_cb, s);
+ s->send_event_slot = pa_hook_connect(&sink_input->core->hooks[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT],
+ PA_HOOK_NORMAL,
+ send_event_cb,
+ s);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(s->dbus_protocol, s->path, &stream_interface_info, s) >= 0);
+
+ return s;
+}
+
+pa_dbusiface_stream *pa_dbusiface_stream_new_record(pa_dbusiface_core *core, pa_source_output *source_output) {
+ pa_dbusiface_stream *s;
+
+ pa_assert(core);
+ pa_assert(source_output);
+
+ s = pa_xnew(pa_dbusiface_stream, 1);
+ s->core = core;
+ s->source_output = pa_source_output_ref(source_output);
+ s->type = STREAM_TYPE_RECORD;
+ s->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, RECORD_OBJECT_NAME, source_output->index);
+ s->source = pa_source_ref(source_output->source);
+ s->sample_rate = source_output->sample_spec.rate;
+ pa_cvolume_init(&s->volume);
+ s->mute = FALSE;
+ s->proplist = pa_proplist_copy(source_output->proplist);
+ s->has_volume = FALSE;
+ s->dbus_protocol = pa_dbus_protocol_get(source_output->core);
+ s->subscription = pa_subscription_new(source_output->core, PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, subscription_cb, s);
+ s->send_event_slot = pa_hook_connect(&source_output->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT],
+ PA_HOOK_NORMAL,
+ send_event_cb,
+ s);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(s->dbus_protocol, s->path, &stream_interface_info, s) >= 0);
+
+ return s;
+}
+
+void pa_dbusiface_stream_free(pa_dbusiface_stream *s) {
+ pa_assert(s);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(s->dbus_protocol, s->path, stream_interface_info.name) >= 0);
+
+ if (s->type == STREAM_TYPE_PLAYBACK) {
+ pa_sink_input_unref(s->sink_input);
+ pa_sink_unref(s->sink);
+ } else {
+ pa_source_output_unref(s->source_output);
+ pa_source_unref(s->source);
+ }
+
+ pa_proplist_free(s->proplist);
+ pa_dbus_protocol_unref(s->dbus_protocol);
+ pa_subscription_free(s->subscription);
+ pa_hook_slot_free(s->send_event_slot);
+
+ pa_xfree(s->path);
+ pa_xfree(s);
+}
+
+const char *pa_dbusiface_stream_get_path(pa_dbusiface_stream *s) {
+ pa_assert(s);
+
+ return s->path;
+}
diff --git a/src/modules/dbus/iface-stream.h b/src/modules/dbus/iface-stream.h
new file mode 100644
index 00000000..036b4e7e
--- /dev/null
+++ b/src/modules/dbus/iface-stream.h
@@ -0,0 +1,47 @@
+#ifndef foodbusifacestreamhfoo
+#define foodbusifacestreamhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* This object implements the D-Bus interface org.PulseAudio.Core1.Stream.
+ *
+ * See http://pulseaudio.org/wiki/DBusInterface for the Stream interface
+ * documentation.
+ */
+
+#include <pulsecore/protocol-dbus.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/source-output.h>
+
+#include "iface-core.h"
+
+#define PA_DBUSIFACE_STREAM_INTERFACE PA_DBUS_CORE_INTERFACE ".Stream"
+
+typedef struct pa_dbusiface_stream pa_dbusiface_stream;
+
+pa_dbusiface_stream *pa_dbusiface_stream_new_playback(pa_dbusiface_core *core, pa_sink_input *sink_input);
+pa_dbusiface_stream *pa_dbusiface_stream_new_record(pa_dbusiface_core *core, pa_source_output *source_output);
+void pa_dbusiface_stream_free(pa_dbusiface_stream *s);
+
+const char *pa_dbusiface_stream_get_path(pa_dbusiface_stream *s);
+
+#endif
diff --git a/src/modules/dbus/module-dbus-protocol.c b/src/modules/dbus/module-dbus-protocol.c
new file mode 100644
index 00000000..4969585f
--- /dev/null
+++ b/src/modules/dbus/module-dbus-protocol.c
@@ -0,0 +1,621 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Tanu Kaskinen
+ Copyright 2006 Lennart Poettering
+ Copyright 2006 Shams E. King
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <dbus/dbus.h>
+
+#include <pulse/mainloop-api.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/client.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/idxset.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/module.h>
+#include <pulsecore/protocol-dbus.h>
+
+#include "iface-client.h"
+#include "iface-core.h"
+
+#include "module-dbus-protocol-symdef.h"
+
+PA_MODULE_DESCRIPTION("D-Bus interface");
+PA_MODULE_USAGE(
+ "access=local|remote|local,remote "
+ "tcp_port=<port number> "
+ "tcp_listen=<hostname>");
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_AUTHOR("Tanu Kaskinen");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+
+enum server_type {
+ SERVER_TYPE_LOCAL,
+ SERVER_TYPE_TCP
+};
+
+struct server;
+struct connection;
+
+struct userdata {
+ pa_module *module;
+ pa_bool_t local_access;
+ pa_bool_t remote_access;
+ uint32_t tcp_port;
+ char *tcp_listen;
+
+ struct server *local_server;
+ struct server *tcp_server;
+
+ pa_idxset *connections;
+
+ pa_defer_event *cleanup_event;
+
+ pa_dbus_protocol *dbus_protocol;
+ pa_dbusiface_core *core_iface;
+};
+
+struct server {
+ struct userdata *userdata;
+ enum server_type type;
+ DBusServer *dbus_server;
+};
+
+struct connection {
+ struct server *server;
+ pa_dbus_wrap_connection *wrap_conn;
+ pa_client *client;
+};
+
+static const char* const valid_modargs[] = {
+ "access",
+ "tcp_port",
+ "tcp_listen",
+ NULL
+};
+
+static void connection_free(struct connection *c) {
+ pa_assert(c);
+
+ pa_assert_se(pa_dbus_protocol_unregister_connection(c->server->userdata->dbus_protocol, pa_dbus_wrap_connection_get(c->wrap_conn)) >= 0);
+
+ pa_client_free(c->client);
+ pa_dbus_wrap_connection_free(c->wrap_conn);
+ pa_xfree(c);
+}
+
+/* Called from pa_client_kill(). */
+static void client_kill_cb(pa_client *c) {
+ struct connection *conn;
+
+ pa_assert(c);
+ pa_assert(c->userdata);
+
+ conn = c->userdata;
+ connection_free(conn);
+ c->userdata = NULL;
+
+ pa_log_info("Connection killed.");
+}
+
+/* Called from pa_client_send_event(). */
+static void client_send_event_cb(pa_client *c, const char *name, pa_proplist *data) {
+ struct connection *conn = NULL;
+ DBusMessage *signal_msg = NULL;
+ DBusMessageIter msg_iter;
+
+ pa_assert(c);
+ pa_assert(name);
+ pa_assert(data);
+ pa_assert(c->userdata);
+
+ conn = c->userdata;
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(pa_dbusiface_core_get_client_path(conn->server->userdata->core_iface, c),
+ PA_DBUSIFACE_CLIENT_INTERFACE,
+ "ClientEvent"));
+ dbus_message_iter_init_append(signal_msg, &msg_iter);
+ pa_assert_se(dbus_message_iter_append_basic(&msg_iter, DBUS_TYPE_STRING, &name));
+ pa_dbus_append_proplist(&msg_iter, data);
+
+ pa_assert_se(dbus_connection_send(pa_dbus_wrap_connection_get(conn->wrap_conn), signal_msg, NULL));
+ dbus_message_unref(signal_msg);
+}
+
+/* Called by D-Bus at the authentication phase. */
+static dbus_bool_t user_check_cb(DBusConnection *connection, unsigned long uid, void *data) {
+ pa_log_debug("Allowing connection by user %lu.", uid);
+
+ return TRUE;
+}
+
+static DBusHandlerResult disconnection_filter_cb(DBusConnection *connection, DBusMessage *message, void *user_data) {
+ struct connection *c = user_data;
+
+ pa_assert(connection);
+ pa_assert(message);
+ pa_assert(c);
+
+ if (dbus_message_is_signal(message, "org.freedesktop.DBus.Local", "Disconnected")) {
+ /* The connection died. Now we want to free the connection object, but
+ * let's wait until this message is fully processed, in case someone
+ * else is interested in this signal too. */
+ c->server->userdata->module->core->mainloop->defer_enable(c->server->userdata->cleanup_event, 1);
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+/* Called by D-Bus when a new client connection is received. */
+static void connection_new_cb(DBusServer *dbus_server, DBusConnection *new_connection, void *data) {
+ struct server *s = data;
+ struct connection *c;
+ pa_client_new_data new_data;
+ pa_client *client;
+
+ pa_assert(new_connection);
+ pa_assert(s);
+
+ pa_client_new_data_init(&new_data);
+ new_data.module = s->userdata->module;
+ new_data.driver = __FILE__;
+ pa_proplist_sets(new_data.proplist, PA_PROP_APPLICATION_NAME, "D-Bus client");
+ client = pa_client_new(s->userdata->module->core, &new_data);
+ pa_client_new_data_done(&new_data);
+
+ if (!client) {
+ dbus_connection_close(new_connection);
+ return;
+ }
+
+ if (s->type == SERVER_TYPE_TCP || s->userdata->module->core->server_type == PA_SERVER_TYPE_SYSTEM) {
+ /* FIXME: Here we allow anyone from anywhere to access the server,
+ * anonymously. Access control should be configurable. */
+ dbus_connection_set_unix_user_function(new_connection, user_check_cb, NULL, NULL);
+ dbus_connection_set_allow_anonymous(new_connection, TRUE);
+ }
+
+ c = pa_xnew(struct connection, 1);
+ c->server = s;
+ c->wrap_conn = pa_dbus_wrap_connection_new_from_existing(s->userdata->module->core->mainloop, TRUE, new_connection);
+ c->client = client;
+
+ c->client->kill = client_kill_cb;
+ c->client->send_event = client_send_event_cb;
+ c->client->userdata = c;
+
+ pa_assert_se(dbus_connection_add_filter(new_connection, disconnection_filter_cb, c, NULL));
+
+ pa_idxset_put(s->userdata->connections, c, NULL);
+
+ pa_assert_se(pa_dbus_protocol_register_connection(s->userdata->dbus_protocol, new_connection, c->client) >= 0);
+}
+
+/* Called by PA mainloop when a D-Bus fd watch event needs handling. */
+static void io_event_cb(pa_mainloop_api *mainloop, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata) {
+ unsigned int flags = 0;
+ DBusWatch *watch = userdata;
+
+#if HAVE_DBUS_WATCH_GET_UNIX_FD
+ pa_assert(fd == dbus_watch_get_unix_fd(watch));
+#else
+ pa_assert(fd == dbus_watch_get_fd(watch));
+#endif
+
+ if (!dbus_watch_get_enabled(watch)) {
+ pa_log_warn("Asked to handle disabled watch: %p %i", (void*) watch, fd);
+ return;
+ }
+
+ if (events & PA_IO_EVENT_INPUT)
+ flags |= DBUS_WATCH_READABLE;
+ if (events & PA_IO_EVENT_OUTPUT)
+ flags |= DBUS_WATCH_WRITABLE;
+ if (events & PA_IO_EVENT_HANGUP)
+ flags |= DBUS_WATCH_HANGUP;
+ if (events & PA_IO_EVENT_ERROR)
+ flags |= DBUS_WATCH_ERROR;
+
+ dbus_watch_handle(watch, flags);
+}
+
+/* Called by PA mainloop when a D-Bus timer event needs handling. */
+static void time_event_cb(pa_mainloop_api *mainloop, pa_time_event* e, const struct timeval *tv, void *userdata) {
+ DBusTimeout *timeout = userdata;
+
+ if (dbus_timeout_get_enabled(timeout)) {
+ struct timeval next = *tv;
+ dbus_timeout_handle(timeout);
+
+ /* restart it for the next scheduled time */
+ pa_timeval_add(&next, (pa_usec_t) dbus_timeout_get_interval(timeout) * 1000);
+ mainloop->time_restart(e, &next);
+ }
+}
+
+/* Translates D-Bus fd watch event flags to PA IO event flags. */
+static pa_io_event_flags_t get_watch_flags(DBusWatch *watch) {
+ unsigned int flags;
+ pa_io_event_flags_t events = 0;
+
+ pa_assert(watch);
+
+ flags = dbus_watch_get_flags(watch);
+
+ /* no watch flags for disabled watches */
+ if (!dbus_watch_get_enabled(watch))
+ return PA_IO_EVENT_NULL;
+
+ if (flags & DBUS_WATCH_READABLE)
+ events |= PA_IO_EVENT_INPUT;
+ if (flags & DBUS_WATCH_WRITABLE)
+ events |= PA_IO_EVENT_OUTPUT;
+
+ return events | PA_IO_EVENT_HANGUP | PA_IO_EVENT_ERROR;
+}
+
+/* Called by D-Bus when a D-Bus fd watch event is added. */
+static dbus_bool_t watch_add_cb(DBusWatch *watch, void *data) {
+ struct server *s = data;
+ pa_mainloop_api *mainloop;
+ pa_io_event *ev;
+
+ pa_assert(watch);
+ pa_assert(s);
+
+ mainloop = s->userdata->module->core->mainloop;
+
+ ev = mainloop->io_new(
+ mainloop,
+#if HAVE_DBUS_WATCH_GET_UNIX_FD
+ dbus_watch_get_unix_fd(watch),
+#else
+ dbus_watch_get_fd(watch),
+#endif
+ get_watch_flags(watch), io_event_cb, watch);
+
+ dbus_watch_set_data(watch, ev, NULL);
+
+ return TRUE;
+}
+
+/* Called by D-Bus when a D-Bus fd watch event is removed. */
+static void watch_remove_cb(DBusWatch *watch, void *data) {
+ struct server *s = data;
+ pa_io_event *ev;
+
+ pa_assert(watch);
+ pa_assert(s);
+
+ if ((ev = dbus_watch_get_data(watch)))
+ s->userdata->module->core->mainloop->io_free(ev);
+}
+
+/* Called by D-Bus when a D-Bus fd watch event is toggled. */
+static void watch_toggled_cb(DBusWatch *watch, void *data) {
+ struct server *s = data;
+ pa_io_event *ev;
+
+ pa_assert(watch);
+ pa_assert(s);
+
+ pa_assert_se(ev = dbus_watch_get_data(watch));
+
+ /* get_watch_flags() checks if the watch is enabled */
+ s->userdata->module->core->mainloop->io_enable(ev, get_watch_flags(watch));
+}
+
+/* Called by D-Bus when a D-Bus timer event is added. */
+static dbus_bool_t timeout_add_cb(DBusTimeout *timeout, void *data) {
+ struct server *s = data;
+ pa_mainloop_api *mainloop;
+ pa_time_event *ev;
+ struct timeval tv;
+
+ pa_assert(timeout);
+ pa_assert(s);
+
+ if (!dbus_timeout_get_enabled(timeout))
+ return FALSE;
+
+ mainloop = s->userdata->module->core->mainloop;
+
+ pa_gettimeofday(&tv);
+ pa_timeval_add(&tv, (pa_usec_t) dbus_timeout_get_interval(timeout) * 1000);
+
+ ev = mainloop->time_new(mainloop, &tv, time_event_cb, timeout);
+
+ dbus_timeout_set_data(timeout, ev, NULL);
+
+ return TRUE;
+}
+
+/* Called by D-Bus when a D-Bus timer event is removed. */
+static void timeout_remove_cb(DBusTimeout *timeout, void *data) {
+ struct server *s = data;
+ pa_time_event *ev;
+
+ pa_assert(timeout);
+ pa_assert(s);
+
+ if ((ev = dbus_timeout_get_data(timeout)))
+ s->userdata->module->core->mainloop->time_free(ev);
+}
+
+/* Called by D-Bus when a D-Bus timer event is toggled. */
+static void timeout_toggled_cb(DBusTimeout *timeout, void *data) {
+ struct server *s = data;
+ pa_mainloop_api *mainloop;
+ pa_time_event *ev;
+
+ pa_assert(timeout);
+ pa_assert(s);
+
+ mainloop = s->userdata->module->core->mainloop;
+
+ pa_assert_se(ev = dbus_timeout_get_data(timeout));
+
+ if (dbus_timeout_get_enabled(timeout)) {
+ struct timeval tv;
+
+ pa_gettimeofday(&tv);
+ pa_timeval_add(&tv, (pa_usec_t) dbus_timeout_get_interval(timeout) * 1000);
+
+ mainloop->time_restart(ev, &tv);
+ } else
+ mainloop->time_restart(ev, NULL);
+}
+
+static void server_free(struct server *s) {
+ pa_assert(s);
+
+ if (s->dbus_server) {
+ dbus_server_disconnect(s->dbus_server);
+ dbus_server_unref(s->dbus_server);
+ }
+
+ pa_xfree(s);
+}
+
+static struct server *start_server(struct userdata *u, const char *address, enum server_type type) {
+ /* XXX: We assume that when we unref the DBusServer instance at module
+ * shutdown, nobody else holds any references to it. If we stop assuming
+ * that someday, dbus_server_set_new_connection_function,
+ * dbus_server_set_watch_functions and dbus_server_set_timeout_functions
+ * calls should probably register free callbacks, instead of providing NULL
+ * as they do now. */
+
+ struct server *s = NULL;
+ DBusError error;
+
+ pa_assert(u);
+ pa_assert(address);
+
+ dbus_error_init(&error);
+
+ s = pa_xnew0(struct server, 1);
+ s->userdata = u;
+ s->type = type;
+ s->dbus_server = dbus_server_listen(address, &error);
+
+ if (dbus_error_is_set(&error)) {
+ pa_log("dbus_server_listen() failed: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ dbus_server_set_new_connection_function(s->dbus_server, connection_new_cb, s, NULL);
+
+ if (!dbus_server_set_watch_functions(s->dbus_server, watch_add_cb, watch_remove_cb, watch_toggled_cb, s, NULL)) {
+ pa_log("dbus_server_set_watch_functions() ran out of memory.");
+ goto fail;
+ }
+
+ if (!dbus_server_set_timeout_functions(s->dbus_server, timeout_add_cb, timeout_remove_cb, timeout_toggled_cb, s, NULL)) {
+ pa_log("dbus_server_set_timeout_functions() ran out of memory.");
+ goto fail;
+ }
+
+ return s;
+
+fail:
+ if (s)
+ server_free(s);
+
+ dbus_error_free(&error);
+
+ return NULL;
+}
+
+static struct server *start_local_server(struct userdata *u) {
+ struct server *s = NULL;
+ char *address = NULL;
+
+ pa_assert(u);
+
+ address = pa_get_dbus_address_from_server_type(u->module->core->server_type);
+
+ s = start_server(u, address, SERVER_TYPE_LOCAL); /* May return NULL */
+
+ pa_xfree(address);
+
+ return s;
+}
+
+static struct server *start_tcp_server(struct userdata *u) {
+ struct server *s = NULL;
+ char *address = NULL;
+
+ pa_assert(u);
+
+ address = pa_sprintf_malloc("tcp:host=%s,port=%u", u->tcp_listen, u->tcp_port);
+
+ s = start_server(u, address, SERVER_TYPE_TCP); /* May return NULL */
+
+ pa_xfree(address);
+
+ return s;
+}
+
+static int get_access_arg(pa_modargs *ma, pa_bool_t *local_access, pa_bool_t *remote_access) {
+ const char *value = NULL;
+
+ pa_assert(ma);
+ pa_assert(local_access);
+ pa_assert(remote_access);
+
+ if (!(value = pa_modargs_get_value(ma, "access", NULL)))
+ return 0;
+
+ if (!strcmp(value, "local")) {
+ *local_access = TRUE;
+ *remote_access = FALSE;
+ } else if (!strcmp(value, "remote")) {
+ *local_access = FALSE;
+ *remote_access = TRUE;
+ } else if (!strcmp(value, "local,remote")) {
+ *local_access = TRUE;
+ *remote_access = TRUE;
+ } else
+ return -1;
+
+ return 0;
+}
+
+/* Frees dead client connections. */
+static void cleanup_cb(pa_mainloop_api *a, pa_defer_event *e, void *userdata) {
+ struct userdata *u = userdata;
+ struct connection *conn = NULL;
+ uint32_t idx;
+
+ PA_IDXSET_FOREACH(conn, u->connections, idx) {
+ if (!dbus_connection_get_is_connected(pa_dbus_wrap_connection_get(conn->wrap_conn))) {
+ pa_idxset_remove_by_data(u->connections, conn, NULL);
+ connection_free(conn);
+ }
+ }
+
+ u->module->core->mainloop->defer_enable(e, 0);
+}
+
+int pa__init(pa_module *m) {
+ struct userdata *u = NULL;
+ pa_modargs *ma = NULL;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ u->local_access = TRUE;
+ u->remote_access = FALSE;
+ u->tcp_port = PA_DBUS_DEFAULT_PORT;
+
+ if (get_access_arg(ma, &u->local_access, &u->remote_access) < 0) {
+ pa_log("Invalid access argument: '%s'", pa_modargs_get_value(ma, "access", NULL));
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_u32(ma, "tcp_port", &u->tcp_port) < 0 || u->tcp_port < 1 || u->tcp_port > 49150) {
+ pa_log("Invalid tcp_port argument: '%s'", pa_modargs_get_value(ma, "tcp_port", NULL));
+ goto fail;
+ }
+
+ u->tcp_listen = pa_xstrdup(pa_modargs_get_value(ma, "tcp_listen", "0.0.0.0"));
+
+ if (u->local_access && !(u->local_server = start_local_server(u))) {
+ pa_log("Starting the local D-Bus server failed.");
+ goto fail;
+ }
+
+ if (u->remote_access && !(u->tcp_server = start_tcp_server(u))) {
+ pa_log("Starting the D-Bus server for remote connections failed.");
+ goto fail;
+ }
+
+ u->connections = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ u->cleanup_event = m->core->mainloop->defer_new(m->core->mainloop, cleanup_cb, u);
+ m->core->mainloop->defer_enable(u->cleanup_event, 0);
+
+ u->dbus_protocol = pa_dbus_protocol_get(m->core);
+ u->core_iface = pa_dbusiface_core_new(m->core);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module *m) {
+ struct userdata *u;
+ struct connection *c;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->core_iface)
+ pa_dbusiface_core_free(u->core_iface);
+
+ while ((c = pa_idxset_steal_first(u->connections, NULL)))
+ connection_free(c);
+
+ pa_idxset_free(u->connections, NULL, NULL);
+
+ /* This must not be called before the connections are freed, because if
+ * there are any connections left, they will emit the
+ * org.freedesktop.DBus.Local.Disconnected signal, and
+ * disconnection_filter_cb() will be called. disconnection_filter_cb() then
+ * tries to enable the defer event, and if it's already freed, an assertion
+ * will be hit in mainloop.c. */
+ if (u->cleanup_event)
+ m->core->mainloop->defer_free(u->cleanup_event);
+
+ if (u->tcp_server)
+ server_free(u->tcp_server);
+
+ if (u->local_server)
+ server_free(u->local_server);
+
+ if (u->dbus_protocol)
+ pa_dbus_protocol_unref(u->dbus_protocol);
+
+ pa_xfree(u->tcp_listen);
+ pa_xfree(u);
+ m->userdata = NULL;
+}
diff --git a/src/modules/echo-cancel/adrian-aec.c b/src/modules/echo-cancel/adrian-aec.c
new file mode 100644
index 00000000..e969e8c5
--- /dev/null
+++ b/src/modules/echo-cancel/adrian-aec.c
@@ -0,0 +1,275 @@
+/* aec.cpp
+ *
+ * Copyright (C) DFS Deutsche Flugsicherung (2004, 2005).
+ * All Rights Reserved.
+ *
+ * Acoustic Echo Cancellation NLMS-pw algorithm
+ *
+ * Version 0.3 filter created with www.dsptutor.freeuk.com
+ * Version 0.3.1 Allow change of stability parameter delta
+ * Version 0.4 Leaky Normalized LMS - pre whitening algorithm
+ */
+
+#include <math.h>
+#include <string.h>
+#include <stdint.h>
+
+#include <pulse/xmalloc.h>
+
+#include "adrian-aec.h"
+
+#ifndef DISABLE_ORC
+#include "adrian-aec-orc-gen.h"
+#endif
+
+#ifdef __SSE__
+#include <xmmintrin.h>
+#endif
+
+/* Vector Dot Product */
+static REAL dotp(REAL a[], REAL b[])
+{
+ REAL sum0 = 0.0, sum1 = 0.0;
+ int j;
+
+ for (j = 0; j < NLMS_LEN; j += 2) {
+ // optimize: partial loop unrolling
+ sum0 += a[j] * b[j];
+ sum1 += a[j + 1] * b[j + 1];
+ }
+ return sum0 + sum1;
+}
+
+static REAL dotp_sse(REAL a[], REAL b[])
+{
+#ifdef __SSE__
+ /* This is taken from speex's inner product implementation */
+ int j;
+ REAL sum;
+ __m128 acc = _mm_setzero_ps();
+
+ for (j=0;j<NLMS_LEN;j+=8)
+ {
+ acc = _mm_add_ps(acc, _mm_mul_ps(_mm_load_ps(a+j), _mm_loadu_ps(b+j)));
+ acc = _mm_add_ps(acc, _mm_mul_ps(_mm_load_ps(a+j+4), _mm_loadu_ps(b+j+4)));
+ }
+ acc = _mm_add_ps(acc, _mm_movehl_ps(acc, acc));
+ acc = _mm_add_ss(acc, _mm_shuffle_ps(acc, acc, 0x55));
+ _mm_store_ss(&sum, acc);
+
+ return sum;
+#else
+ return dotp(a, b);
+#endif
+}
+
+
+AEC* AEC_init(int RATE, int have_vector)
+{
+ AEC *a = pa_xnew(AEC, 1);
+ a->hangover = 0;
+ memset(a->x, 0, sizeof(a->x));
+ memset(a->xf, 0, sizeof(a->xf));
+ memset(a->w_arr, 0, sizeof(a->w_arr));
+ a->j = NLMS_EXT;
+ a->delta = 0.0f;
+ AEC_setambient(a, NoiseFloor);
+ a->dfast = a->dslow = M75dB_PCM;
+ a->xfast = a->xslow = M80dB_PCM;
+ a->gain = 1.0f;
+ a->Fx = IIR1_init(2000.0f/RATE);
+ a->Fe = IIR1_init(2000.0f/RATE);
+ a->cutoff = FIR_HP_300Hz_init();
+ a->acMic = IIR_HP_init();
+ a->acSpk = IIR_HP_init();
+
+ a->aes_y2 = M0dB;
+
+ a->fdwdisplay = -1;
+ a->dumpcnt = 0;
+ memset(a->ws, 0, sizeof(a->ws));
+
+ if (have_vector) {
+ /* Get a 16-byte aligned location */
+ a->w = (REAL *) (((uintptr_t) a->w_arr) + (((uintptr_t) a->w_arr) % 16));
+ a->dotp = dotp_sse;
+ } else {
+ /* We don't care about alignment, just use the array as-is */
+ a->w = a->w_arr;
+ a->dotp = dotp;
+ }
+
+ return a;
+}
+
+// Adrian soft decision DTD
+// (Dual Average Near-End to Far-End signal Ratio DTD)
+// This algorithm uses exponential smoothing with differnt
+// ageing parameters to get fast and slow near-end and far-end
+// signal averages. The ratio of NFRs term
+// (dfast / xfast) / (dslow / xslow) is used to compute the stepsize
+// A ratio value of 2.5 is mapped to stepsize 0, a ratio of 0 is
+// mapped to 1.0 with a limited linear function.
+static float AEC_dtd(AEC *a, REAL d, REAL x)
+{
+ float ratio, stepsize;
+
+ // fast near-end and far-end average
+ a->dfast += ALPHAFAST * (fabsf(d) - a->dfast);
+ a->xfast += ALPHAFAST * (fabsf(x) - a->xfast);
+
+ // slow near-end and far-end average
+ a->dslow += ALPHASLOW * (fabsf(d) - a->dslow);
+ a->xslow += ALPHASLOW * (fabsf(x) - a->xslow);
+
+ if (a->xfast < M70dB_PCM) {
+ return 0.0; // no Spk signal
+ }
+
+ if (a->dfast < M70dB_PCM) {
+ return 0.0; // no Mic signal
+ }
+
+ // ratio of NFRs
+ ratio = (a->dfast * a->xslow) / (a->dslow * a->xfast);
+
+ // Linear interpolation with clamping at the limits
+ if (ratio < STEPX1)
+ stepsize = STEPY1;
+ else if (ratio > STEPX2)
+ stepsize = STEPY2;
+ else
+ stepsize = STEPY1 + (STEPY2 - STEPY1) * (ratio - STEPX1) / (STEPX2 - STEPX1);
+
+ return stepsize;
+}
+
+
+static void AEC_leaky(AEC *a)
+// The xfast signal is used to charge the hangover timer to Thold.
+// When hangover expires (no Spk signal for some time) the vector w
+// is erased. This is my implementation of Leaky NLMS.
+{
+ if (a->xfast >= M70dB_PCM) {
+ // vector w is valid for hangover Thold time
+ a->hangover = Thold;
+ } else {
+ if (a->hangover > 1) {
+ --(a->hangover);
+ } else if (1 == a->hangover) {
+ --(a->hangover);
+ // My Leaky NLMS is to erase vector w when hangover expires
+ memset(a->w, 0, sizeof(a->w));
+ }
+ }
+}
+
+
+#if 0
+void AEC::openwdisplay() {
+ // open TCP connection to program wdisplay.tcl
+ fdwdisplay = socket_async("127.0.0.1", 50999);
+};
+#endif
+
+
+static REAL AEC_nlms_pw(AEC *a, REAL d, REAL x_, float stepsize)
+{
+ REAL e;
+ REAL ef;
+ a->x[a->j] = x_;
+ a->xf[a->j] = IIR1_highpass(a->Fx, x_); // pre-whitening of x
+
+ // calculate error value
+ // (mic signal - estimated mic signal from spk signal)
+ e = d;
+ if (a->hangover > 0) {
+ e -= a->dotp(a->w, a->x + a->j);
+ }
+ ef = IIR1_highpass(a->Fe, e); // pre-whitening of e
+
+ // optimize: iterative dotp(xf, xf)
+ a->dotp_xf_xf += (a->xf[a->j] * a->xf[a->j] - a->xf[a->j + NLMS_LEN - 1] * a->xf[a->j + NLMS_LEN - 1]);
+
+ if (stepsize > 0.0) {
+ // calculate variable step size
+ REAL mikro_ef = stepsize * ef / a->dotp_xf_xf;
+
+#ifdef DISABLE_ORC
+ // update tap weights (filter learning)
+ int i;
+ for (i = 0; i < NLMS_LEN; i += 2) {
+ // optimize: partial loop unrolling
+ a->w[i] += mikro_ef * a->xf[i + a->j];
+ a->w[i + 1] += mikro_ef * a->xf[i + a->j + 1];
+ }
+#else
+ update_tap_weights(a->w, &a->xf[a->j], mikro_ef, NLMS_LEN);
+#endif
+ }
+
+ if (--(a->j) < 0) {
+ // optimize: decrease number of memory copies
+ a->j = NLMS_EXT;
+ memmove(a->x + a->j + 1, a->x, (NLMS_LEN - 1) * sizeof(REAL));
+ memmove(a->xf + a->j + 1, a->xf, (NLMS_LEN - 1) * sizeof(REAL));
+ }
+
+ // Saturation
+ if (e > MAXPCM) {
+ return MAXPCM;
+ } else if (e < -MAXPCM) {
+ return -MAXPCM;
+ } else {
+ return e;
+ }
+}
+
+
+int AEC_doAEC(AEC *a, int d_, int x_)
+{
+ REAL d = (REAL) d_;
+ REAL x = (REAL) x_;
+
+ // Mic Highpass Filter - to remove DC
+ d = IIR_HP_highpass(a->acMic, d);
+
+ // Mic Highpass Filter - cut-off below 300Hz
+ d = FIR_HP_300Hz_highpass(a->cutoff, d);
+
+ // Amplify, for e.g. Soundcards with -6dB max. volume
+ d *= a->gain;
+
+ // Spk Highpass Filter - to remove DC
+ x = IIR_HP_highpass(a->acSpk, x);
+
+ // Double Talk Detector
+ a->stepsize = AEC_dtd(a, d, x);
+
+ // Leaky (ageing of vector w)
+ AEC_leaky(a);
+
+ // Acoustic Echo Cancellation
+ d = AEC_nlms_pw(a, d, x, a->stepsize);
+
+#if 0
+ if (fdwdisplay >= 0) {
+ if (++dumpcnt >= (WIDEB*RATE/10)) {
+ // wdisplay creates 10 dumps per seconds = large CPU load!
+ dumpcnt = 0;
+ write(fdwdisplay, ws, DUMP_LEN*sizeof(float));
+ // we don't check return value. This is not production quality!!!
+ memset(ws, 0, sizeof(ws));
+ } else {
+ int i;
+ for (i = 0; i < DUMP_LEN; i += 2) {
+ // optimize: partial loop unrolling
+ ws[i] += w[i];
+ ws[i + 1] += w[i + 1];
+ }
+ }
+ }
+#endif
+
+ return (int) d;
+}
diff --git a/src/modules/echo-cancel/adrian-aec.h b/src/modules/echo-cancel/adrian-aec.h
new file mode 100644
index 00000000..d024b3c5
--- /dev/null
+++ b/src/modules/echo-cancel/adrian-aec.h
@@ -0,0 +1,382 @@
+/* aec.h
+ *
+ * Copyright (C) DFS Deutsche Flugsicherung (2004, 2005).
+ * All Rights Reserved.
+ * Author: Andre Adrian
+ *
+ * Acoustic Echo Cancellation Leaky NLMS-pw algorithm
+ *
+ * Version 0.3 filter created with www.dsptutor.freeuk.com
+ * Version 0.3.1 Allow change of stability parameter delta
+ * Version 0.4 Leaky Normalized LMS - pre whitening algorithm
+ */
+
+#ifndef _AEC_H /* include only once */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/gccmacro.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/macro.h>
+
+#define WIDEB 2
+
+// use double if your CPU does software-emulation of float
+#define REAL float
+
+/* dB Values */
+#define M0dB 1.0f
+#define M3dB 0.71f
+#define M6dB 0.50f
+#define M9dB 0.35f
+#define M12dB 0.25f
+#define M18dB 0.125f
+#define M24dB 0.063f
+
+/* dB values for 16bit PCM */
+/* MxdB_PCM = 32767 * 10 ^(x / 20) */
+#define M10dB_PCM 10362.0f
+#define M20dB_PCM 3277.0f
+#define M25dB_PCM 1843.0f
+#define M30dB_PCM 1026.0f
+#define M35dB_PCM 583.0f
+#define M40dB_PCM 328.0f
+#define M45dB_PCM 184.0f
+#define M50dB_PCM 104.0f
+#define M55dB_PCM 58.0f
+#define M60dB_PCM 33.0f
+#define M65dB_PCM 18.0f
+#define M70dB_PCM 10.0f
+#define M75dB_PCM 6.0f
+#define M80dB_PCM 3.0f
+#define M85dB_PCM 2.0f
+#define M90dB_PCM 1.0f
+
+#define MAXPCM 32767.0f
+
+/* Design constants (Change to fine tune the algorithms */
+
+/* The following values are for hardware AEC and studio quality
+ * microphone */
+
+/* NLMS filter length in taps (samples). A longer filter length gives
+ * better Echo Cancellation, but maybe slower convergence speed and
+ * needs more CPU power (Order of NLMS is linear) */
+#define NLMS_LEN (100*WIDEB*8)
+
+/* Vector w visualization length in taps (samples).
+ * Must match argv value for wdisplay.tcl */
+#define DUMP_LEN (40*WIDEB*8)
+
+/* minimum energy in xf. Range: M70dB_PCM to M50dB_PCM. Should be equal
+ * to microphone ambient Noise level */
+#define NoiseFloor M55dB_PCM
+
+/* Leaky hangover in taps.
+ */
+#define Thold (60 * WIDEB * 8)
+
+// Adrian soft decision DTD
+// left point. X is ratio, Y is stepsize
+#define STEPX1 1.0
+#define STEPY1 1.0
+// right point. STEPX2=2.0 is good double talk, 3.0 is good single talk.
+#define STEPX2 2.5
+#define STEPY2 0
+#define ALPHAFAST (1.0f / 100.0f)
+#define ALPHASLOW (1.0f / 20000.0f)
+
+
+
+/* Ageing multiplier for LMS memory vector w */
+#define Leaky 0.9999f
+
+/* Double Talk Detector Speaker/Microphone Threshold. Range <=1
+ * Large value (M0dB) is good for Single-Talk Echo cancellation,
+ * small value (M12dB) is good for Doulbe-Talk AEC */
+#define GeigelThreshold M6dB
+
+/* for Non Linear Processor. Range >0 to 1. Large value (M0dB) is good
+ * for Double-Talk, small value (M12dB) is good for Single-Talk */
+#define NLPAttenuation M12dB
+
+/* Below this line there are no more design constants */
+
+typedef struct IIR_HP IIR_HP;
+
+/* Exponential Smoothing or IIR Infinite Impulse Response Filter */
+struct IIR_HP {
+ REAL x;
+};
+
+static IIR_HP* IIR_HP_init(void) {
+ IIR_HP *i = pa_xnew(IIR_HP, 1);
+ i->x = 0.0f;
+ return i;
+ }
+
+static REAL IIR_HP_highpass(IIR_HP *i, REAL in) {
+ const REAL a0 = 0.01f; /* controls Transfer Frequency */
+ /* Highpass = Signal - Lowpass. Lowpass = Exponential Smoothing */
+ i->x += a0 * (in - i->x);
+ return in - i->x;
+ };
+
+typedef struct FIR_HP_300Hz FIR_HP_300Hz;
+
+#if WIDEB==1
+/* 17 taps FIR Finite Impulse Response filter
+ * Coefficients calculated with
+ * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html
+ */
+class FIR_HP_300Hz {
+ REAL z[18];
+
+public:
+ FIR_HP_300Hz() {
+ memset(this, 0, sizeof(FIR_HP_300Hz));
+ }
+
+ REAL highpass(REAL in) {
+ const REAL a[18] = {
+ // Kaiser Window FIR Filter, Filter type: High pass
+ // Passband: 300.0 - 4000.0 Hz, Order: 16
+ // Transition band: 75.0 Hz, Stopband attenuation: 10.0 dB
+ -0.034870606, -0.039650206, -0.044063766, -0.04800318,
+ -0.051370874, -0.054082647, -0.056070227, -0.057283327,
+ 0.8214126, -0.057283327, -0.056070227, -0.054082647,
+ -0.051370874, -0.04800318, -0.044063766, -0.039650206,
+ -0.034870606, 0.0
+ };
+ memmove(z + 1, z, 17 * sizeof(REAL));
+ z[0] = in;
+ REAL sum0 = 0.0, sum1 = 0.0;
+ int j;
+
+ for (j = 0; j < 18; j += 2) {
+ // optimize: partial loop unrolling
+ sum0 += a[j] * z[j];
+ sum1 += a[j + 1] * z[j + 1];
+ }
+ return sum0 + sum1;
+ }
+};
+
+#else
+
+/* 35 taps FIR Finite Impulse Response filter
+ * Passband 150Hz to 4kHz for 8kHz sample rate, 300Hz to 8kHz for 16kHz
+ * sample rate.
+ * Coefficients calculated with
+ * www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html
+ */
+struct FIR_HP_300Hz {
+ REAL z[36];
+};
+
+static FIR_HP_300Hz* FIR_HP_300Hz_init(void) {
+ FIR_HP_300Hz *ret = pa_xnew(FIR_HP_300Hz, 1);
+ memset(ret, 0, sizeof(FIR_HP_300Hz));
+ return ret;
+ }
+
+static REAL FIR_HP_300Hz_highpass(FIR_HP_300Hz *f, REAL in) {
+ REAL sum0 = 0.0, sum1 = 0.0;
+ int j;
+ const REAL a[36] = {
+ // Kaiser Window FIR Filter, Filter type: High pass
+ // Passband: 150.0 - 4000.0 Hz, Order: 34
+ // Transition band: 34.0 Hz, Stopband attenuation: 10.0 dB
+ -0.016165324, -0.017454365, -0.01871232, -0.019931411,
+ -0.021104068, -0.022222936, -0.02328091, -0.024271343,
+ -0.025187887, -0.02602462, -0.026776174, -0.027437767,
+ -0.028004972, -0.028474221, -0.028842418, -0.029107114,
+ -0.02926664, 0.8524841, -0.02926664, -0.029107114,
+ -0.028842418, -0.028474221, -0.028004972, -0.027437767,
+ -0.026776174, -0.02602462, -0.025187887, -0.024271343,
+ -0.02328091, -0.022222936, -0.021104068, -0.019931411,
+ -0.01871232, -0.017454365, -0.016165324, 0.0
+ };
+ memmove(f->z + 1, f->z, 35 * sizeof(REAL));
+ f->z[0] = in;
+
+ for (j = 0; j < 36; j += 2) {
+ // optimize: partial loop unrolling
+ sum0 += a[j] * f->z[j];
+ sum1 += a[j + 1] * f->z[j + 1];
+ }
+ return sum0 + sum1;
+ }
+#endif
+
+typedef struct IIR1 IIR1;
+
+/* Recursive single pole IIR Infinite Impulse response High-pass filter
+ *
+ * Reference: The Scientist and Engineer's Guide to Digital Processing
+ *
+ * output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1]
+ *
+ * X = exp(-2.0 * pi * Fc)
+ * A0 = (1 + X) / 2
+ * A1 = -(1 + X) / 2
+ * B1 = X
+ * Fc = cutoff freq / sample rate
+ */
+struct IIR1 {
+ REAL in0, out0;
+ REAL a0, a1, b1;
+};
+
+#if 0
+ IIR1() {
+ memset(this, 0, sizeof(IIR1));
+ }
+#endif
+
+static IIR1* IIR1_init(REAL Fc) {
+ IIR1 *i = pa_xnew(IIR1, 1);
+ i->b1 = expf(-2.0f * M_PI * Fc);
+ i->a0 = (1.0f + i->b1) / 2.0f;
+ i->a1 = -(i->a0);
+ i->in0 = 0.0f;
+ i->out0 = 0.0f;
+ return i;
+ }
+
+static REAL IIR1_highpass(IIR1 *i, REAL in) {
+ REAL out = i->a0 * in + i->a1 * i->in0 + i->b1 * i->out0;
+ i->in0 = in;
+ i->out0 = out;
+ return out;
+ }
+
+
+#if 0
+/* Recursive two pole IIR Infinite Impulse Response filter
+ * Coefficients calculated with
+ * http://www.dsptutor.freeuk.com/IIRFilterDesign/IIRFiltDes102.html
+ */
+class IIR2 {
+ REAL x[2], y[2];
+
+public:
+ IIR2() {
+ memset(this, 0, sizeof(IIR2));
+ }
+
+ REAL highpass(REAL in) {
+ // Butterworth IIR filter, Filter type: HP
+ // Passband: 2000 - 4000.0 Hz, Order: 2
+ const REAL a[] = { 0.29289323f, -0.58578646f, 0.29289323f };
+ const REAL b[] = { 1.3007072E-16f, 0.17157288f };
+ REAL out =
+ a[0] * in + a[1] * x[0] + a[2] * x[1] - b[0] * y[0] - b[1] * y[1];
+
+ x[1] = x[0];
+ x[0] = in;
+ y[1] = y[0];
+ y[0] = out;
+ return out;
+ }
+};
+#endif
+
+
+// Extention in taps to reduce mem copies
+#define NLMS_EXT (10*8)
+
+// block size in taps to optimize DTD calculation
+#define DTD_LEN 16
+
+typedef struct AEC AEC;
+
+struct AEC {
+ // Time domain Filters
+ IIR_HP *acMic, *acSpk; // DC-level remove Highpass)
+ FIR_HP_300Hz *cutoff; // 150Hz cut-off Highpass
+ REAL gain; // Mic signal amplify
+ IIR1 *Fx, *Fe; // pre-whitening Highpass for x, e
+
+ // Adrian soft decision DTD (Double Talk Detector)
+ REAL dfast, xfast;
+ REAL dslow, xslow;
+
+ // NLMS-pw
+ REAL x[NLMS_LEN + NLMS_EXT]; // tap delayed loudspeaker signal
+ REAL xf[NLMS_LEN + NLMS_EXT]; // pre-whitening tap delayed signal
+ REAL w_arr[NLMS_LEN + (16 / sizeof(REAL))]; // tap weights
+ REAL *w; // this will be a 16-byte aligned pointer into w_arr
+ int j; // optimize: less memory copies
+ double dotp_xf_xf; // double to avoid loss of precision
+ float delta; // noise floor to stabilize NLMS
+
+ // AES
+ float aes_y2; // not in use!
+
+ // w vector visualization
+ REAL ws[DUMP_LEN]; // tap weights sums
+ int fdwdisplay; // TCP file descriptor
+ int dumpcnt; // wdisplay output counter
+
+ // variables are public for visualization
+ int hangover;
+ float stepsize;
+
+ // vfuncs that are picked based on processor features available
+ REAL (*dotp) (REAL[], REAL[]);
+};
+
+/* Double-Talk Detector
+ *
+ * in d: microphone sample (PCM as REALing point value)
+ * in x: loudspeaker sample (PCM as REALing point value)
+ * return: from 0 for doubletalk to 1.0 for single talk
+ */
+static float AEC_dtd(AEC *a, REAL d, REAL x);
+
+static void AEC_leaky(AEC *a);
+
+/* Normalized Least Mean Square Algorithm pre-whitening (NLMS-pw)
+ * The LMS algorithm was developed by Bernard Widrow
+ * book: Haykin, Adaptive Filter Theory, 4. edition, Prentice Hall, 2002
+ *
+ * in d: microphone sample (16bit PCM value)
+ * in x_: loudspeaker sample (16bit PCM value)
+ * in stepsize: NLMS adaptation variable
+ * return: echo cancelled microphone sample
+ */
+static REAL AEC_nlms_pw(AEC *a, REAL d, REAL x_, float stepsize);
+
+ AEC* AEC_init(int RATE, int have_vector);
+
+/* Acoustic Echo Cancellation and Suppression of one sample
+ * in d: microphone signal with echo
+ * in x: loudspeaker signal
+ * return: echo cancelled microphone signal
+ */
+ int AEC_doAEC(AEC *a, int d_, int x_);
+
+PA_GCC_UNUSED static float AEC_getambient(AEC *a) {
+ return a->dfast;
+ };
+static void AEC_setambient(AEC *a, float Min_xf) {
+ a->dotp_xf_xf -= a->delta; // subtract old delta
+ a->delta = (NLMS_LEN-1) * Min_xf * Min_xf;
+ a->dotp_xf_xf += a->delta; // add new delta
+ };
+PA_GCC_UNUSED static void AEC_setgain(AEC *a, float gain_) {
+ a->gain = gain_;
+ };
+#if 0
+ void AEC_openwdisplay(AEC *a);
+#endif
+PA_GCC_UNUSED static void AEC_setaes(AEC *a, float aes_y2_) {
+ a->aes_y2 = aes_y2_;
+ };
+
+#define _AEC_H
+#endif
diff --git a/src/modules/echo-cancel/adrian-aec.orc b/src/modules/echo-cancel/adrian-aec.orc
new file mode 100644
index 00000000..80547723
--- /dev/null
+++ b/src/modules/echo-cancel/adrian-aec.orc
@@ -0,0 +1,8 @@
+.function update_tap_weights
+.dest 4 w float
+.source 4 xf float
+.floatparam 4 mikro_ef
+.temp 4 tmp float
+
+mulf tmp, mikro_ef, xf
+addf w, w, tmp
diff --git a/src/modules/echo-cancel/adrian-license.txt b/src/modules/echo-cancel/adrian-license.txt
new file mode 100644
index 00000000..7c06efd0
--- /dev/null
+++ b/src/modules/echo-cancel/adrian-license.txt
@@ -0,0 +1,17 @@
+ Copyright (C) DFS Deutsche Flugsicherung (2004). All Rights Reserved.
+
+ You are allowed to use this source code in any open source or closed
+ source software you want. You are allowed to use the algorithms for a
+ hardware solution. You are allowed to modify the source code.
+ You are not allowed to remove the name of the author from this memo or
+ from the source code files. You are not allowed to monopolize the
+ source code or the algorithms behind the source code as your
+ intellectual property. This source code is free of royalty and comes
+ with no warranty.
+
+--- The following does not apply to the PulseAudio module ---
+
+ Please see g711/gen-lic.txt for the ITU-T G.711 codec copyright.
+ Please see gsm/gen-lic.txt for the ITU-T GSM codec copyright.
+ Please see ilbc/COPYRIGHT and ilbc/NOTICE for the IETF iLBC codec
+ copyright.
diff --git a/src/modules/echo-cancel/adrian.c b/src/modules/echo-cancel/adrian.c
new file mode 100644
index 00000000..ab3858a4
--- /dev/null
+++ b/src/modules/echo-cancel/adrian.c
@@ -0,0 +1,117 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2010 Arun Raghavan <arun.raghavan@collabora.co.uk>
+
+ Contributor: Wim Taymans <wim.taymans@gmail.com>
+
+ The actual implementation is taken from the sources at
+ http://andreadrian.de/intercom/ - for the license, look for
+ adrian-license.txt in the same directory as this file.
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/modargs.h>
+
+#include "echo-cancel.h"
+
+/* should be between 10-20 ms */
+#define DEFAULT_FRAME_SIZE_MS 20
+
+static const char* const valid_modargs[] = {
+ "frame_size_ms",
+ NULL
+};
+
+static void pa_adrian_ec_fixate_spec(pa_sample_spec *source_ss, pa_channel_map *source_map,
+ pa_sample_spec *sink_ss, pa_channel_map *sink_map)
+{
+ source_ss->format = PA_SAMPLE_S16NE;
+ source_ss->channels = 1;
+ pa_channel_map_init_mono(source_map);
+
+ *sink_ss = *source_ss;
+ *sink_map = *source_map;
+}
+
+pa_bool_t pa_adrian_ec_init(pa_core *c, pa_echo_canceller *ec,
+ pa_sample_spec *source_ss, pa_channel_map *source_map,
+ pa_sample_spec *sink_ss, pa_channel_map *sink_map,
+ uint32_t *blocksize, const char *args)
+{
+ int framelen, rate, have_vector = 0;
+ uint32_t frame_size_ms;
+ pa_modargs *ma;
+
+ if (!(ma = pa_modargs_new(args, valid_modargs))) {
+ pa_log("Failed to parse submodule arguments.");
+ goto fail;
+ }
+
+ frame_size_ms = DEFAULT_FRAME_SIZE_MS;
+ if (pa_modargs_get_value_u32(ma, "frame_size_ms", &frame_size_ms) < 0 || frame_size_ms < 1 || frame_size_ms > 200) {
+ pa_log("Invalid frame_size_ms specification");
+ goto fail;
+ }
+
+ pa_adrian_ec_fixate_spec(source_ss, source_map, sink_ss, sink_map);
+
+ rate = source_ss->rate;
+ framelen = (rate * frame_size_ms) / 1000;
+
+ *blocksize = ec->params.priv.adrian.blocksize = framelen * pa_frame_size (source_ss);
+
+ pa_log_debug ("Using framelen %d, blocksize %u, channels %d, rate %d", framelen, ec->params.priv.adrian.blocksize, source_ss->channels, source_ss->rate);
+
+ /* For now we only support SSE */
+ if (c->cpu_info.cpu_type == PA_CPU_X86 && (c->cpu_info.flags.x86 & PA_CPU_X86_SSE))
+ have_vector = 1;
+
+ ec->params.priv.adrian.aec = AEC_init(rate, have_vector);
+ if (!ec->params.priv.adrian.aec)
+ goto fail;
+
+ pa_modargs_free(ma);
+ return TRUE;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+ return FALSE;
+}
+
+void pa_adrian_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out) {
+ unsigned int i;
+
+ for (i = 0; i < ec->params.priv.adrian.blocksize; i += 2) {
+ /* We know it's S16NE mono data */
+ int r = *(int16_t *)(rec + i);
+ int p = *(int16_t *)(play + i);
+ *(int16_t *)(out + i) = (int16_t) AEC_doAEC(ec->params.priv.adrian.aec, r, p);
+ }
+}
+
+void pa_adrian_ec_done(pa_echo_canceller *ec) {
+ pa_xfree(ec->params.priv.adrian.aec);
+ ec->params.priv.adrian.aec = NULL;
+}
diff --git a/src/modules/echo-cancel/adrian.h b/src/modules/echo-cancel/adrian.h
new file mode 100644
index 00000000..639fa9ec
--- /dev/null
+++ b/src/modules/echo-cancel/adrian.h
@@ -0,0 +1,31 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2010 Arun Raghavan <arun.raghavan@collabora.co.uk>
+
+ The actual implementation is taken from the sources at
+ http://andreadrian.de/intercom/ - for the license, look for
+ adrian-license.txt in the same directory as this file.
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* Forward declarations */
+
+typedef struct AEC AEC;
+
+AEC* AEC_init(int RATE, int have_vector);
+int AEC_doAEC(AEC *a, int d_, int x_);
diff --git a/src/modules/echo-cancel/echo-cancel.h b/src/modules/echo-cancel/echo-cancel.h
new file mode 100644
index 00000000..aa40adce
--- /dev/null
+++ b/src/modules/echo-cancel/echo-cancel.h
@@ -0,0 +1,90 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2010 Arun Raghavan <arun.raghavan@collabora.co.uk>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/sample.h>
+#include <pulse/channelmap.h>
+#include <pulsecore/core.h>
+#include <pulsecore/macro.h>
+
+#include <speex/speex_echo.h>
+#include <speex/speex_preprocess.h>
+#include "adrian.h"
+
+/* Common data structures */
+
+typedef struct pa_echo_canceller_params pa_echo_canceller_params;
+
+struct pa_echo_canceller_params {
+ union {
+ struct {
+ SpeexEchoState *state;
+ } speex;
+ struct {
+ uint32_t blocksize;
+ AEC *aec;
+ } adrian;
+ /* each canceller-specific structure goes here */
+ } priv;
+};
+
+typedef struct pa_echo_canceller pa_echo_canceller;
+
+struct pa_echo_canceller {
+ pa_bool_t (*init) (pa_core *c,
+ pa_echo_canceller *ec,
+ pa_sample_spec *source_ss,
+ pa_channel_map *source_map,
+ pa_sample_spec *sink_ss,
+ pa_channel_map *sink_map,
+ uint32_t *blocksize,
+ const char *args);
+ void (*run) (pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out);
+ void (*done) (pa_echo_canceller *ec);
+
+ pa_echo_canceller_params params;
+
+ pa_bool_t agc;
+ pa_bool_t denoise;
+ pa_bool_t echo_suppress;
+ int32_t echo_suppress_attenuation;
+ int32_t echo_suppress_attenuation_active;
+ SpeexPreprocessState *pp_state;
+};
+
+/* Speex canceller functions */
+pa_bool_t pa_speex_ec_init(pa_core *c, pa_echo_canceller *ec,
+ pa_sample_spec *source_ss, pa_channel_map *source_map,
+ pa_sample_spec *sink_ss, pa_channel_map *sink_map,
+ uint32_t *blocksize, const char *args);
+void pa_speex_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out);
+void pa_speex_ec_done(pa_echo_canceller *ec);
+
+/* Adrian Andre's echo canceller */
+pa_bool_t pa_adrian_ec_init(pa_core *c, pa_echo_canceller *ec,
+ pa_sample_spec *source_ss, pa_channel_map *source_map,
+ pa_sample_spec *sink_ss, pa_channel_map *sink_map,
+ uint32_t *blocksize, const char *args);
+void pa_adrian_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out);
+void pa_adrian_ec_done(pa_echo_canceller *ec);
diff --git a/src/modules/echo-cancel/module-echo-cancel.c b/src/modules/echo-cancel/module-echo-cancel.c
new file mode 100644
index 00000000..b84bf1db
--- /dev/null
+++ b/src/modules/echo-cancel/module-echo-cancel.c
@@ -0,0 +1,1778 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2010 Wim Taymans <wim.taymans@gmail.com>
+
+ Based on module-virtual-sink.c
+ module-virtual-source.c
+ module-loopback.c
+
+ Copyright 2010 Intel Corporation
+ Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+
+#include "echo-cancel.h"
+
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+#include <pulse/timeval.h>
+#include <pulse/rtclock.h>
+
+#include <pulsecore/atomic.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-rtclock.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/ltdl-helper.h>
+
+#include "module-echo-cancel-symdef.h"
+
+PA_MODULE_AUTHOR("Wim Taymans");
+PA_MODULE_DESCRIPTION("Echo Cancelation");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ _("source_name=<name for the source> "
+ "source_properties=<properties for the source> "
+ "source_master=<name of source to filter> "
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "sink_master=<name of sink to filter> "
+ "adjust_time=<how often to readjust rates in s> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "aec_method=<implementation to use> "
+ "aec_args=<parameters for the AEC engine> "
+ "agc=<perform automagic gain control?> "
+ "denoise=<apply denoising?> "
+ "echo_suppress=<perform residual echo suppression? (only with the speex canceller)> "
+ "echo_suppress_attenuation=<dB value of residual echo attenuation> "
+ "echo_suppress_attenuation_active=<dB value of residual echo attenuation when near end is active> "
+ "save_aec=<save AEC data in /tmp> "
+ "autoloaded=<set if this module is being loaded automatically> "
+ ));
+
+/* NOTE: Make sure the enum and ec_table are maintained in the correct order */
+typedef enum {
+ PA_ECHO_CANCELLER_INVALID = -1,
+ PA_ECHO_CANCELLER_SPEEX = 0,
+ PA_ECHO_CANCELLER_ADRIAN,
+} pa_echo_canceller_method_t;
+
+#define DEFAULT_ECHO_CANCELLER "speex"
+
+static const pa_echo_canceller ec_table[] = {
+ {
+ /* Speex */
+ .init = pa_speex_ec_init,
+ .run = pa_speex_ec_run,
+ .done = pa_speex_ec_done,
+ },
+ {
+ /* Adrian Andre's NLMS implementation */
+ .init = pa_adrian_ec_init,
+ .run = pa_adrian_ec_run,
+ .done = pa_adrian_ec_done,
+ },
+};
+
+#define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
+#define DEFAULT_AGC_ENABLED FALSE
+#define DEFAULT_DENOISE_ENABLED FALSE
+#define DEFAULT_ECHO_SUPPRESS_ENABLED FALSE
+#define DEFAULT_ECHO_SUPPRESS_ATTENUATION 0
+#define DEFAULT_SAVE_AEC 0
+#define DEFAULT_AUTOLOADED FALSE
+
+#define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
+
+/* This module creates a new (virtual) source and sink.
+ *
+ * The data sent to the new sink is kept in a memblockq before being
+ * forwarded to the real sink_master.
+ *
+ * Data read from source_master is matched against the saved sink data and
+ * echo canceled data is then pushed onto the new source.
+ *
+ * Both source and sink masters have their own threads to push/pull data
+ * respectively. We however perform all our actions in the source IO thread.
+ * To do this we send all played samples to the source IO thread where they
+ * are then pushed into the memblockq.
+ *
+ * Alignment is performed in two steps:
+ *
+ * 1) when something happens that requires quick adjustement of the alignment of
+ * capture and playback samples, we perform a resync. This adjusts the
+ * position in the playback memblock to the requested sample. Quick
+ * adjustements include moving the playback samples before the capture
+ * samples (because else the echo canceler does not work) or when the
+ * playback pointer drifts too far away.
+ *
+ * 2) periodically check the difference between capture and playback. we use a
+ * low and high watermark for adjusting the alignment. playback should always
+ * be before capture and the difference should not be bigger than one frame
+ * size. We would ideally like to resample the sink_input but most driver
+ * don't give enough accuracy to be able to do that right now.
+ */
+
+struct snapshot {
+ pa_usec_t sink_now;
+ pa_usec_t sink_latency;
+ size_t sink_delay;
+ int64_t send_counter;
+
+ pa_usec_t source_now;
+ pa_usec_t source_latency;
+ size_t source_delay;
+ int64_t recv_counter;
+ size_t rlen;
+ size_t plen;
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+
+ pa_bool_t autoloaded;
+ uint32_t save_aec;
+
+ pa_echo_canceller *ec;
+ uint32_t blocksize;
+
+ pa_bool_t need_realign;
+
+ /* to wakeup the source I/O thread */
+ pa_bool_t in_push;
+ pa_asyncmsgq *asyncmsgq;
+ pa_rtpoll_item *rtpoll_item_read, *rtpoll_item_write;
+
+ pa_source *source;
+ pa_bool_t source_auto_desc;
+ pa_source_output *source_output;
+ pa_memblockq *source_memblockq; /* echo canceler needs fixed sized chunks */
+ size_t source_skip;
+
+ pa_sink *sink;
+ pa_bool_t sink_auto_desc;
+ pa_sink_input *sink_input;
+ pa_memblockq *sink_memblockq;
+ int64_t send_counter; /* updated in sink IO thread */
+ int64_t recv_counter;
+ size_t sink_skip;
+
+ pa_atomic_t request_resync;
+
+ int active_mask;
+ pa_time_event *time_event;
+ pa_usec_t adjust_time;
+
+ FILE *captured_file;
+ FILE *played_file;
+ FILE *canceled_file;
+};
+
+static void source_output_snapshot_within_thread(struct userdata *u, struct snapshot *snapshot);
+
+static const char* const valid_modargs[] = {
+ "source_name",
+ "source_properties",
+ "source_master",
+ "sink_name",
+ "sink_properties",
+ "sink_master",
+ "adjust_time",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "aec_method",
+ "aec_args",
+ "agc",
+ "denoise",
+ "echo_suppress",
+ "echo_suppress_attenuation",
+ "echo_suppress_attenuation_active",
+ "save_aec",
+ "autoloaded",
+ NULL
+};
+
+enum {
+ SOURCE_OUTPUT_MESSAGE_POST = PA_SOURCE_OUTPUT_MESSAGE_MAX,
+ SOURCE_OUTPUT_MESSAGE_REWIND,
+ SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT,
+ SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
+};
+
+enum {
+ SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
+};
+
+static int64_t calc_diff(struct userdata *u, struct snapshot *snapshot) {
+ int64_t buffer, diff_time, buffer_latency;
+
+ /* get the number of samples between capture and playback */
+ if (snapshot->plen > snapshot->rlen)
+ buffer = snapshot->plen - snapshot->rlen;
+ else
+ buffer = 0;
+
+ buffer += snapshot->source_delay + snapshot->sink_delay;
+
+ /* add the amount of samples not yet transfered to the source context */
+ if (snapshot->recv_counter <= snapshot->send_counter)
+ buffer += (int64_t) (snapshot->send_counter - snapshot->recv_counter);
+ else
+ buffer += PA_CLIP_SUB(buffer, (int64_t) (snapshot->recv_counter - snapshot->send_counter));
+
+ /* convert to time */
+ buffer_latency = pa_bytes_to_usec(buffer, &u->source_output->sample_spec);
+
+ /* capture and playback samples are perfectly aligned when diff_time is 0 */
+ diff_time = (snapshot->sink_now + snapshot->sink_latency - buffer_latency) -
+ (snapshot->source_now - snapshot->source_latency);
+
+ pa_log_debug("diff %lld (%lld - %lld + %lld) %lld %lld %lld %lld", (long long) diff_time,
+ (long long) snapshot->sink_latency,
+ (long long) buffer_latency, (long long) snapshot->source_latency,
+ (long long) snapshot->source_delay, (long long) snapshot->sink_delay,
+ (long long) (snapshot->send_counter - snapshot->recv_counter),
+ (long long) (snapshot->sink_now - snapshot->source_now));
+
+ return diff_time;
+}
+
+/* Called from main context */
+static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
+ struct userdata *u = userdata;
+ uint32_t old_rate, base_rate, new_rate;
+ int64_t diff_time;
+ /*size_t fs*/
+ struct snapshot latency_snapshot;
+
+ pa_assert(u);
+ pa_assert(a);
+ pa_assert(u->time_event == e);
+ pa_assert_ctl_context();
+
+ if (u->active_mask != 3)
+ return;
+
+ /* update our snapshots */
+ pa_asyncmsgq_send(u->source_output->source->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT, &latency_snapshot, 0, NULL);
+ pa_asyncmsgq_send(u->sink_input->sink->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT, &latency_snapshot, 0, NULL);
+
+ /* calculate drift between capture and playback */
+ diff_time = calc_diff(u, &latency_snapshot);
+
+ /*fs = pa_frame_size(&u->source_output->sample_spec);*/
+ old_rate = u->sink_input->sample_spec.rate;
+ base_rate = u->source_output->sample_spec.rate;
+
+ if (diff_time < 0) {
+ /* recording before playback, we need to adjust quickly. The echo
+ * canceler does not work in this case. */
+ pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME,
+ NULL, diff_time, NULL, NULL);
+ /*new_rate = base_rate - ((pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
+ new_rate = base_rate;
+ }
+ else {
+ if (diff_time > 1000) {
+ /* diff too big, quickly adjust */
+ pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME,
+ NULL, diff_time, NULL, NULL);
+ }
+
+ /* recording behind playback, we need to slowly adjust the rate to match */
+ /*new_rate = base_rate + ((pa_usec_to_bytes(diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
+
+ /* assume equal samplerates for now */
+ new_rate = base_rate;
+ }
+
+ /* make sure we don't make too big adjustements because that sounds horrible */
+ if (new_rate > base_rate * 1.1 || new_rate < base_rate * 0.9)
+ new_rate = base_rate;
+
+ if (new_rate != old_rate) {
+ pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate, (unsigned long) new_rate);
+
+ pa_sink_input_set_rate(u->sink_input, new_rate);
+ }
+
+ pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
+}
+
+/* Called from source I/O thread context */
+static int source_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE(o)->userdata;
+
+ switch (code) {
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY:
+
+ /* The source is _put() before the source output is, so let's
+ * make sure we don't access it in that time. Also, the
+ * source output is first shut down, the source second. */
+ if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state)) {
+ *((pa_usec_t*) data) = 0;
+ return 0;
+ }
+
+ *((pa_usec_t*) data) =
+
+ /* Get the latency of the master source */
+ pa_source_get_latency_within_thread(u->source_output->source) +
+ /* Add the latency internal to our source output on top */
+ pa_bytes_to_usec(pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq), &u->source_output->source->sample_spec) +
+ /* and the buffering we do on the source */
+ pa_bytes_to_usec(u->blocksize, &u->source_output->source->sample_spec);
+
+ return 0;
+
+ }
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from sink I/O thread context */
+static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_GET_LATENCY:
+
+ /* The sink is _put() before the sink input is, so let's
+ * make sure we don't access it in that time. Also, the
+ * sink input is first shut down, the sink second. */
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
+ *((pa_usec_t*) data) = 0;
+ return 0;
+ }
+
+ *((pa_usec_t*) data) =
+
+ /* Get the latency of the master sink */
+ pa_sink_get_latency_within_thread(u->sink_input->sink) +
+
+ /* Add the latency internal to our sink input on top */
+ pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
+
+ return 0;
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+
+/* Called from main context */
+static int source_set_state_cb(pa_source *s, pa_source_state_t state) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(state) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
+ return 0;
+
+ pa_log_debug("Source state %d %d", state, u->active_mask);
+
+ if (state == PA_SOURCE_RUNNING) {
+ /* restart timer when both sink and source are active */
+ u->active_mask |= 1;
+ if (u->active_mask == 3)
+ pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
+
+ pa_atomic_store(&u->request_resync, 1);
+ pa_source_output_cork(u->source_output, FALSE);
+ } else if (state == PA_SOURCE_SUSPENDED) {
+ u->active_mask &= ~1;
+ pa_source_output_cork(u->source_output, TRUE);
+ }
+ return 0;
+}
+
+/* Called from main context */
+static int sink_set_state_cb(pa_sink *s, pa_sink_state_t state) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(state) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return 0;
+
+ pa_log_debug("Sink state %d %d", state, u->active_mask);
+
+ if (state == PA_SINK_RUNNING) {
+ /* restart timer when both sink and source are active */
+ u->active_mask |= 2;
+ if (u->active_mask == 3)
+ pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
+
+ pa_atomic_store(&u->request_resync, 1);
+ pa_sink_input_cork(u->sink_input, FALSE);
+ } else if (state == PA_SINK_SUSPENDED) {
+ u->active_mask &= ~2;
+ pa_sink_input_cork(u->sink_input, TRUE);
+ }
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void source_update_requested_latency_cb(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state))
+ return;
+
+ pa_log_debug("Source update requested latency");
+
+ /* Just hand this one over to the master source */
+ pa_source_output_set_requested_latency_within_thread(
+ u->source_output,
+ pa_source_get_requested_latency_within_thread(s));
+}
+
+/* Called from I/O thread context */
+static void sink_update_requested_latency_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ pa_log_debug("Sink update requested latency");
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_set_requested_latency_within_thread(
+ u->sink_input,
+ pa_sink_get_requested_latency_within_thread(s));
+}
+
+/* Called from I/O thread context */
+static void sink_request_rewind_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ pa_log_debug("Sink request rewind %lld", (long long) s->thread_info.rewind_nbytes);
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_request_rewind(u->sink_input,
+ s->thread_info.rewind_nbytes, TRUE, FALSE, FALSE);
+}
+
+/* Called from main context */
+static void source_set_volume_cb(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
+ return;
+
+ pa_source_output_set_volume(u->source_output, &s->real_volume, s->save_volume, TRUE);
+}
+
+/* Called from main context */
+static void sink_set_volume_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return;
+
+ pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, TRUE);
+}
+
+static void source_get_volume_cb(pa_source *s) {
+ struct userdata *u;
+ pa_cvolume v;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
+ return;
+
+ pa_source_output_get_volume(u->source_output, &v, TRUE);
+
+ if (pa_cvolume_equal(&s->real_volume, &v))
+ /* no change */
+ return;
+
+ s->real_volume = v;
+ pa_source_set_soft_volume(s, NULL);
+}
+
+/* Called from main context */
+static void source_set_mute_cb(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
+ return;
+
+ pa_source_output_set_mute(u->source_output, s->muted, s->save_muted);
+}
+
+/* Called from main context */
+static void sink_set_mute_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return;
+
+ pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
+}
+
+/* Called from main context */
+static void source_get_mute_cb(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
+ return;
+
+ pa_source_output_get_mute(u->source_output);
+}
+
+/* must be called from the input thread context */
+static void apply_diff_time(struct userdata *u, int64_t diff_time) {
+ int64_t diff;
+
+ if (diff_time < 0) {
+ diff = pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec);
+
+ if (diff > 0) {
+ /* add some extra safety samples to compensate for jitter in the
+ * timings */
+ diff += 10 * pa_frame_size (&u->source_output->sample_spec);
+
+ pa_log("Playback after capture (%lld), drop sink %lld", (long long) diff_time, (long long) diff);
+
+ u->sink_skip = diff;
+ u->source_skip = 0;
+ }
+ } else if (diff_time > 0) {
+ diff = pa_usec_to_bytes(diff_time, &u->source_output->sample_spec);
+
+ if (diff > 0) {
+ pa_log("playback too far ahead (%lld), drop source %lld", (long long) diff_time, (long long) diff);
+
+ u->source_skip = diff;
+ u->sink_skip = 0;
+ }
+ }
+}
+
+/* must be called from the input thread */
+static void do_resync(struct userdata *u) {
+ int64_t diff_time;
+ struct snapshot latency_snapshot;
+
+ pa_log("Doing resync");
+
+ /* update our snapshot */
+ source_output_snapshot_within_thread(u, &latency_snapshot);
+ pa_asyncmsgq_send(u->sink_input->sink->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT, &latency_snapshot, 0, NULL);
+
+ /* calculate drift between capture and playback */
+ diff_time = calc_diff(u, &latency_snapshot);
+
+ /* and adjust for the drift */
+ apply_diff_time(u, diff_time);
+}
+
+/* Called from input thread context */
+static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
+ struct userdata *u;
+ size_t rlen, plen;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output))) {
+ pa_log("push when no link?");
+ return;
+ }
+
+ /* handle queued messages */
+ u->in_push = TRUE;
+ while (pa_asyncmsgq_process_one(u->asyncmsgq) > 0)
+ ;
+ u->in_push = FALSE;
+
+ if (pa_atomic_cmpxchg (&u->request_resync, 1, 0)) {
+ do_resync(u);
+ }
+
+ pa_memblockq_push_align(u->source_memblockq, chunk);
+
+ rlen = pa_memblockq_get_length(u->source_memblockq);
+ plen = pa_memblockq_get_length(u->sink_memblockq);
+
+ while (rlen >= u->blocksize) {
+ pa_memchunk rchunk, pchunk;
+
+ /* take fixed block from recorded samples */
+ pa_memblockq_peek_fixed_size(u->source_memblockq, u->blocksize, &rchunk);
+
+ if (plen > u->blocksize && u->source_skip == 0) {
+ uint8_t *rdata, *pdata, *cdata;
+ pa_memchunk cchunk;
+
+ if (u->sink_skip) {
+ size_t to_skip;
+
+ if (u->sink_skip > plen)
+ to_skip = plen;
+ else
+ to_skip = u->sink_skip;
+
+ pa_memblockq_drop(u->sink_memblockq, to_skip);
+ plen -= to_skip;
+
+ u->sink_skip -= to_skip;
+ }
+
+ if (plen > u->blocksize && u->sink_skip == 0) {
+ /* take fixed block from played samples */
+ pa_memblockq_peek_fixed_size(u->sink_memblockq, u->blocksize, &pchunk);
+
+ rdata = pa_memblock_acquire(rchunk.memblock);
+ rdata += rchunk.index;
+ pdata = pa_memblock_acquire(pchunk.memblock);
+ pdata += pchunk.index;
+
+ cchunk.index = 0;
+ cchunk.length = u->blocksize;
+ cchunk.memblock = pa_memblock_new(u->source->core->mempool, cchunk.length);
+ cdata = pa_memblock_acquire(cchunk.memblock);
+
+ if (u->save_aec) {
+ if (u->captured_file)
+ fwrite(rdata, 1, u->blocksize, u->captured_file);
+ if (u->played_file)
+ fwrite(pdata, 1, u->blocksize, u->played_file);
+ }
+
+ /* perform echo cancelation */
+ u->ec->run(u->ec, rdata, pdata, cdata);
+
+ /* preprecessor is run after AEC. This is not a mistake! */
+ if (u->ec->pp_state)
+ speex_preprocess_run(u->ec->pp_state, (spx_int16_t *) cdata);
+
+ if (u->save_aec) {
+ if (u->canceled_file)
+ fwrite(cdata, 1, u->blocksize, u->canceled_file);
+ }
+
+ pa_memblock_release(cchunk.memblock);
+ pa_memblock_release(pchunk.memblock);
+ pa_memblock_release(rchunk.memblock);
+
+ /* drop consumed sink samples */
+ pa_memblockq_drop(u->sink_memblockq, u->blocksize);
+ pa_memblock_unref(pchunk.memblock);
+
+ pa_memblock_unref(rchunk.memblock);
+ /* the filtered samples now become the samples from our
+ * source */
+ rchunk = cchunk;
+
+ plen -= u->blocksize;
+ }
+ }
+
+ /* forward the (echo-canceled) data to the virtual source */
+ pa_source_post(u->source, &rchunk);
+ pa_memblock_unref(rchunk.memblock);
+
+ pa_memblockq_drop(u->source_memblockq, u->blocksize);
+ rlen -= u->blocksize;
+
+ if (u->source_skip) {
+ if (u->source_skip > u->blocksize) {
+ u->source_skip -= u->blocksize;
+ }
+ else {
+ u->sink_skip += (u->blocksize - u->source_skip);
+ u->source_skip = 0;
+ }
+ }
+ }
+}
+
+/* Called from I/O thread context */
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert(chunk);
+ pa_assert_se(u = i->userdata);
+
+ if (u->sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(u->sink, 0);
+
+ pa_sink_render_full(u->sink, nbytes, chunk);
+
+ if (i->thread_info.underrun_for > 0) {
+ pa_log_debug("Handling end of underrun.");
+ pa_atomic_store(&u->request_resync, 1);
+ }
+
+ /* let source thread handle the chunk. pass the sample count as well so that
+ * the source IO thread can update the right variables. */
+ pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_POST,
+ NULL, 0, chunk, NULL);
+ u->send_counter += chunk->length;
+
+ return 0;
+}
+
+/* Called from input thread context */
+static void source_output_process_rewind_cb(pa_source_output *o, size_t nbytes) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_source_process_rewind(u->source, nbytes);
+
+ /* go back on read side, we need to use older sink data for this */
+ pa_memblockq_rewind(u->sink_memblockq, nbytes);
+
+ /* manipulate write index */
+ pa_memblockq_seek(u->source_memblockq, -nbytes, PA_SEEK_RELATIVE, TRUE);
+
+ pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes,
+ (long long) pa_memblockq_get_length (u->source_memblockq));
+}
+
+/* Called from I/O thread context */
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_log_debug("Sink process rewind %lld", (long long) nbytes);
+
+ pa_sink_process_rewind(u->sink, nbytes);
+
+ pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_REWIND, NULL, (int64_t) nbytes, NULL, NULL);
+ u->send_counter -= nbytes;
+}
+
+static void source_output_snapshot_within_thread(struct userdata *u, struct snapshot *snapshot) {
+ size_t delay, rlen, plen;
+ pa_usec_t now, latency;
+
+ now = pa_rtclock_now();
+ latency = pa_source_get_latency_within_thread(u->source_output->source);
+ delay = pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq);
+
+ delay = (u->source_output->thread_info.resampler ? pa_resampler_request(u->source_output->thread_info.resampler, delay) : delay);
+ rlen = pa_memblockq_get_length(u->source_memblockq);
+ plen = pa_memblockq_get_length(u->sink_memblockq);
+
+ snapshot->source_now = now;
+ snapshot->source_latency = latency;
+ snapshot->source_delay = delay;
+ snapshot->recv_counter = u->recv_counter;
+ snapshot->rlen = rlen + u->sink_skip;
+ snapshot->plen = plen + u->source_skip;
+}
+
+
+/* Called from output thread context */
+static int source_output_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE_OUTPUT(obj)->userdata;
+
+ switch (code) {
+
+ case SOURCE_OUTPUT_MESSAGE_POST:
+
+ pa_source_output_assert_io_context(u->source_output);
+
+ if (PA_SOURCE_IS_OPENED(u->source_output->source->thread_info.state))
+ pa_memblockq_push_align(u->sink_memblockq, chunk);
+ else
+ pa_memblockq_flush_write(u->sink_memblockq, TRUE);
+
+ u->recv_counter += (int64_t) chunk->length;
+
+ return 0;
+
+ case SOURCE_OUTPUT_MESSAGE_REWIND:
+ pa_source_output_assert_io_context(u->source_output);
+
+ /* manipulate write index, never go past what we have */
+ if (PA_SOURCE_IS_OPENED(u->source_output->source->thread_info.state))
+ pa_memblockq_seek(u->sink_memblockq, -offset, PA_SEEK_RELATIVE, TRUE);
+ else
+ pa_memblockq_flush_write(u->sink_memblockq, TRUE);
+
+ pa_log_debug("Sink rewind (%lld)", (long long) offset);
+
+ u->recv_counter -= offset;
+
+ return 0;
+
+ case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT: {
+ struct snapshot *snapshot = (struct snapshot *) data;
+
+ source_output_snapshot_within_thread(u, snapshot);
+ return 0;
+ }
+
+ case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME:
+ apply_diff_time(u, offset);
+ return 0;
+
+ }
+
+ return pa_source_output_process_msg(obj, code, data, offset, chunk);
+}
+
+static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK_INPUT(obj)->userdata;
+
+ switch (code) {
+
+ case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT: {
+ size_t delay;
+ pa_usec_t now, latency;
+ struct snapshot *snapshot = (struct snapshot *) data;
+
+ pa_sink_input_assert_io_context(u->sink_input);
+
+ now = pa_rtclock_now();
+ latency = pa_sink_get_latency_within_thread(u->sink_input->sink);
+ delay = pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq);
+
+ delay = (u->sink_input->thread_info.resampler ? pa_resampler_request(u->sink_input->thread_info.resampler, delay) : delay);
+
+ snapshot->sink_now = now;
+ snapshot->sink_latency = latency;
+ snapshot->sink_delay = delay;
+ snapshot->send_counter = u->send_counter;
+ return 0;
+ }
+ }
+
+ return pa_sink_input_process_msg(obj, code, data, offset, chunk);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_log_debug("Sink input update max rewind %lld", (long long) nbytes);
+
+ pa_memblockq_set_maxrewind(u->sink_memblockq, nbytes);
+ pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
+}
+
+/* Called from I/O thread context */
+static void source_output_update_max_rewind_cb(pa_source_output *o, size_t nbytes) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_log_debug("Source output update max rewind %lld", (long long) nbytes);
+
+ pa_source_set_max_rewind_within_thread(u->source, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_log_debug("Sink input update max request %lld", (long long) nbytes);
+
+ pa_sink_set_max_request_within_thread(u->sink, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_requested_latency_cb(pa_sink_input *i) {
+ struct userdata *u;
+ pa_usec_t latency;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ latency = pa_sink_get_requested_latency_within_thread(i->sink);
+
+ pa_log_debug("Sink input update requested latency %lld", (long long) latency);
+}
+
+/* Called from I/O thread context */
+static void source_output_update_source_requested_latency_cb(pa_source_output *o) {
+ struct userdata *u;
+ pa_usec_t latency;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_se(u = o->userdata);
+
+ latency = pa_source_get_requested_latency_within_thread(o->source);
+
+ pa_log_debug("source output update requested latency %lld", (long long) latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_log_debug("Sink input update latency range %lld %lld",
+ (long long) i->sink->thread_info.min_latency,
+ (long long) i->sink->thread_info.max_latency);
+
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+}
+
+/* Called from I/O thread context */
+static void source_output_update_source_latency_range_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_log_debug("Source output update latency range %lld %lld",
+ (long long) o->source->thread_info.min_latency,
+ (long long) o->source->thread_info.max_latency);
+
+ pa_source_set_latency_range_within_thread(u->source, o->source->thread_info.min_latency, o->source->thread_info.max_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_log_debug("Sink input update fixed latency %lld",
+ (long long) i->sink->thread_info.fixed_latency);
+
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+}
+
+/* Called from I/O thread context */
+static void source_output_update_source_fixed_latency_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_log_debug("Source output update fixed latency %lld",
+ (long long) o->source->thread_info.fixed_latency);
+
+ pa_source_set_fixed_latency_within_thread(u->source, o->source->thread_info.fixed_latency);
+}
+
+/* Called from output thread context */
+static void source_output_attach_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_source_set_rtpoll(u->source, o->source->thread_info.rtpoll);
+ pa_source_set_latency_range_within_thread(u->source, o->source->thread_info.min_latency, o->source->thread_info.max_latency);
+ pa_source_set_fixed_latency_within_thread(u->source, o->source->thread_info.fixed_latency);
+ pa_source_set_max_rewind_within_thread(u->source, pa_source_output_get_max_rewind(o));
+
+ pa_log_debug("Source output %p attach", o);
+
+ pa_source_attach_within_thread(u->source);
+
+ u->rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
+ o->source->thread_info.rtpoll,
+ PA_RTPOLL_LATE,
+ u->asyncmsgq);
+}
+
+/* Called from I/O thread context */
+static void sink_input_attach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+
+ /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
+ * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+
+ /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
+ * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
+ * HERE. SEE (6) */
+ pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
+ pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
+
+ pa_log_debug("Sink input %p attach", i);
+
+ u->rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
+ i->sink->thread_info.rtpoll,
+ PA_RTPOLL_LATE,
+ u->asyncmsgq);
+
+ pa_sink_attach_within_thread(u->sink);
+}
+
+
+/* Called from output thread context */
+static void source_output_detach_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_source_detach_within_thread(u->source);
+ pa_source_set_rtpoll(u->source, NULL);
+
+ pa_log_debug("Source output %p detach", o);
+
+ if (u->rtpoll_item_read) {
+ pa_rtpoll_item_free(u->rtpoll_item_read);
+ u->rtpoll_item_read = NULL;
+ }
+}
+
+/* Called from I/O thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_detach_within_thread(u->sink);
+
+ pa_sink_set_rtpoll(u->sink, NULL);
+
+ pa_log_debug("Sink input %p detach", i);
+
+ if (u->rtpoll_item_write) {
+ pa_rtpoll_item_free(u->rtpoll_item_write);
+ u->rtpoll_item_write = NULL;
+ }
+}
+
+/* Called from output thread context */
+static void source_output_state_change_cb(pa_source_output *o, pa_source_output_state_t state) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_log_debug("Source output %p state %d", o, state);
+}
+
+/* Called from IO thread context */
+static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_log_debug("Sink input %p state %d", i, state);
+
+ /* If we are added for the first time, ask for a rewinding so that
+ * we are heard right-away. */
+ if (PA_SINK_INPUT_IS_LINKED(state) &&
+ i->thread_info.state == PA_SINK_INPUT_INIT) {
+ pa_log_debug("Requesting rewind due to state change.");
+ pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
+ }
+}
+
+/* Called from main thread */
+static void source_output_kill_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert_se(u = o->userdata);
+
+ /* The order here matters! We first kill the source output, followed
+ * by the source. That means the source callbacks must be protected
+ * against an unconnected source output! */
+ pa_source_output_unlink(u->source_output);
+ pa_source_unlink(u->source);
+
+ pa_source_output_unref(u->source_output);
+ u->source_output = NULL;
+
+ pa_source_unref(u->source);
+ u->source = NULL;
+
+ pa_log_debug("Source output kill %p", o);
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from main context */
+static void sink_input_kill_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* The order here matters! We first kill the sink input, followed
+ * by the sink. That means the sink callbacks must be protected
+ * against an unconnected sink input! */
+ pa_sink_input_unlink(u->sink_input);
+ pa_sink_unlink(u->sink);
+
+ pa_sink_input_unref(u->sink_input);
+ u->sink_input = NULL;
+
+ pa_sink_unref(u->sink);
+ u->sink = NULL;
+
+ pa_log_debug("Sink input kill %p", i);
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from main thread */
+static pa_bool_t source_output_may_move_to_cb(pa_source_output *o, pa_source *dest) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert_se(u = o->userdata);
+
+ return (u->source != dest) && (u->sink != dest->monitor_of);
+}
+
+/* Called from main context */
+static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ return u->sink != dest;
+}
+
+/* Called from main thread */
+static void source_output_moving_cb(pa_source_output *o, pa_source *dest) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert_se(u = o->userdata);
+
+ if (dest) {
+ pa_source_set_asyncmsgq(u->source, dest->asyncmsgq);
+ pa_source_update_flags(u->source, PA_SOURCE_LATENCY|PA_SOURCE_DYNAMIC_LATENCY, dest->flags);
+ } else
+ pa_source_set_asyncmsgq(u->source, NULL);
+
+ if (u->source_auto_desc && dest) {
+ const char *z;
+ pa_proplist *pl;
+
+ pl = pa_proplist_new();
+ z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Source %s on %s",
+ pa_proplist_gets(u->source->proplist, "device.echo-cancel.name"), z ? z : dest->name);
+
+ pa_source_update_proplist(u->source, PA_UPDATE_REPLACE, pl);
+ pa_proplist_free(pl);
+ }
+}
+
+/* Called from main context */
+static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ if (dest) {
+ pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
+ pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
+ } else
+ pa_sink_set_asyncmsgq(u->sink, NULL);
+
+ if (u->sink_auto_desc && dest) {
+ const char *z;
+ pa_proplist *pl;
+
+ pl = pa_proplist_new();
+ z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Sink %s on %s",
+ pa_proplist_gets(u->sink->proplist, "device.echo-cancel.name"), z ? z : dest->name);
+
+ pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
+ pa_proplist_free(pl);
+ }
+}
+
+/* Called from main context */
+static void sink_input_volume_changed_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_volume_changed(u->sink, &i->volume);
+}
+
+/* Called from main context */
+static void sink_input_mute_changed_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_mute_changed(u->sink, i->muted);
+}
+
+static pa_echo_canceller_method_t get_ec_method_from_string(const char *method) {
+ if (strcmp(method, "speex") == 0)
+ return PA_ECHO_CANCELLER_SPEEX;
+ else if (strcmp(method, "adrian") == 0)
+ return PA_ECHO_CANCELLER_ADRIAN;
+ else
+ return PA_ECHO_CANCELLER_INVALID;
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_sample_spec source_ss, sink_ss;
+ pa_channel_map source_map, sink_map;
+ pa_modargs *ma;
+ pa_source *source_master=NULL;
+ pa_sink *sink_master=NULL;
+ pa_source_output_new_data source_output_data;
+ pa_sink_input_new_data sink_input_data;
+ pa_source_new_data source_data;
+ pa_sink_new_data sink_data;
+ pa_memchunk silence;
+ pa_echo_canceller_method_t ec_method;
+ uint32_t adjust_time_sec;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (!(source_master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source_master", NULL), PA_NAMEREG_SOURCE))) {
+ pa_log("Master source not found");
+ goto fail;
+ }
+ pa_assert(source_master);
+
+ if (!(sink_master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink_master", NULL), PA_NAMEREG_SINK))) {
+ pa_log("Master sink not found");
+ goto fail;
+ }
+ pa_assert(sink_master);
+
+ source_ss = source_master->sample_spec;
+ source_map = source_master->channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &source_ss, &source_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+ pa_log("Invalid sample format specification or channel map");
+ goto fail;
+ }
+
+ sink_ss = sink_master->sample_spec;
+ sink_map = sink_master->channel_map;
+
+ u = pa_xnew0(struct userdata, 1);
+ if (!u) {
+ pa_log("Failed to alloc userdata");
+ goto fail;
+ }
+ u->core = m->core;
+ u->module = m;
+ m->userdata = u;
+
+ u->ec = pa_xnew0(pa_echo_canceller, 1);
+ if (!u->ec) {
+ pa_log("Failed to alloc echo canceller");
+ goto fail;
+ }
+
+ if ((ec_method = get_ec_method_from_string(pa_modargs_get_value(ma, "aec_method", DEFAULT_ECHO_CANCELLER))) < 0) {
+ pa_log("Invalid echo canceller implementation");
+ goto fail;
+ }
+
+ u->ec->init = ec_table[ec_method].init;
+ u->ec->run = ec_table[ec_method].run;
+ u->ec->done = ec_table[ec_method].done;
+
+ adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
+ if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
+ pa_log("Failed to parse adjust_time value");
+ goto fail;
+ }
+
+ if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
+ u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
+ else
+ u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
+
+ u->ec->agc = DEFAULT_AGC_ENABLED;
+ if (pa_modargs_get_value_boolean(ma, "agc", &u->ec->agc) < 0) {
+ pa_log("Failed to parse agc value");
+ goto fail;
+ }
+
+ u->ec->denoise = DEFAULT_DENOISE_ENABLED;
+ if (pa_modargs_get_value_boolean(ma, "denoise", &u->ec->denoise) < 0) {
+ pa_log("Failed to parse denoise value");
+ goto fail;
+ }
+
+ u->ec->echo_suppress = DEFAULT_ECHO_SUPPRESS_ENABLED;
+ if (pa_modargs_get_value_boolean(ma, "echo_suppress", &u->ec->echo_suppress) < 0) {
+ pa_log("Failed to parse echo_suppress value");
+ goto fail;
+ }
+ if (u->ec->echo_suppress && ec_method != PA_ECHO_CANCELLER_SPEEX) {
+ pa_log("Echo suppression is only useful with the speex canceller");
+ goto fail;
+ }
+
+ u->ec->echo_suppress_attenuation = DEFAULT_ECHO_SUPPRESS_ATTENUATION;
+ if (pa_modargs_get_value_s32(ma, "echo_suppress_attenuation", &u->ec->echo_suppress_attenuation) < 0) {
+ pa_log("Failed to parse echo_suppress_attenuation value");
+ goto fail;
+ }
+ if (u->ec->echo_suppress_attenuation > 0) {
+ pa_log("echo_suppress_attenuation should be a negative dB value");
+ goto fail;
+ }
+
+ u->ec->echo_suppress_attenuation_active = DEFAULT_ECHO_SUPPRESS_ATTENUATION;
+ if (pa_modargs_get_value_s32(ma, "echo_suppress_attenuation_active", &u->ec->echo_suppress_attenuation_active) < 0) {
+ pa_log("Failed to parse echo_supress_attenuation_active value");
+ goto fail;
+ }
+ if (u->ec->echo_suppress_attenuation_active > 0) {
+ pa_log("echo_suppress_attenuation_active should be a negative dB value");
+ goto fail;
+ }
+
+ u->save_aec = DEFAULT_SAVE_AEC;
+ if (pa_modargs_get_value_u32(ma, "save_aec", &u->save_aec) < 0) {
+ pa_log("Failed to parse save_aec value");
+ goto fail;
+ }
+
+ u->autoloaded = DEFAULT_AUTOLOADED;
+ if (pa_modargs_get_value_boolean(ma, "autoloaded", &u->autoloaded) < 0) {
+ pa_log("Failed to parse autoloaded value");
+ goto fail;
+ }
+
+ u->asyncmsgq = pa_asyncmsgq_new(0);
+ u->need_realign = TRUE;
+ if (u->ec->init) {
+ if (!u->ec->init(u->core, u->ec, &source_ss, &source_map, &sink_ss, &sink_map, &u->blocksize, pa_modargs_get_value(ma, "aec_args", NULL))) {
+ pa_log("Failed to init AEC engine");
+ goto fail;
+ }
+ }
+
+ if (u->ec->agc || u->ec->denoise || u->ec->echo_suppress) {
+ spx_int32_t tmp;
+
+ if (source_ss.channels != 1) {
+ pa_log("AGC, denoising and echo suppression only work with channels=1");
+ goto fail;
+ }
+
+ u->ec->pp_state = speex_preprocess_state_init(u->blocksize / pa_frame_size(&source_ss), source_ss.rate);
+
+ tmp = u->ec->agc;
+ speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_AGC, &tmp);
+ tmp = u->ec->denoise;
+ speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_DENOISE, &tmp);
+ if (u->ec->echo_suppress) {
+ if (u->ec->echo_suppress_attenuation)
+ speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS, &u->ec->echo_suppress_attenuation);
+ if (u->ec->echo_suppress_attenuation_active) {
+ speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS_ACTIVE,
+ &u->ec->echo_suppress_attenuation_active);
+ }
+ speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_ECHO_STATE, u->ec->params.priv.speex.state);
+ }
+ }
+
+ /* Create source */
+ pa_source_new_data_init(&source_data);
+ source_data.driver = __FILE__;
+ source_data.module = m;
+ if (!(source_data.name = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
+ source_data.name = pa_sprintf_malloc("%s.echo-cancel", source_master->name);
+ pa_source_new_data_set_sample_spec(&source_data, &source_ss);
+ pa_source_new_data_set_channel_map(&source_data, &source_map);
+ pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, source_master->name);
+ pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
+ if (!u->autoloaded)
+ pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
+ pa_proplist_sets(source_data.proplist, "device.echo-cancel.name", source_data.name);
+
+ if (pa_modargs_get_proplist(ma, "source_properties", source_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&source_data);
+ goto fail;
+ }
+
+ if ((u->source_auto_desc = !pa_proplist_contains(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
+ const char *z;
+
+ z = pa_proplist_gets(source_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Source %s on %s", source_data.name, z ? z : source_master->name);
+ }
+
+ u->source = pa_source_new(m->core, &source_data,
+ PA_SOURCE_HW_MUTE_CTRL|PA_SOURCE_HW_VOLUME_CTRL|PA_SOURCE_DECIBEL_VOLUME|
+ (source_master->flags & (PA_SOURCE_LATENCY|PA_SOURCE_DYNAMIC_LATENCY)));
+ pa_source_new_data_done(&source_data);
+
+ if (!u->source) {
+ pa_log("Failed to create source.");
+ goto fail;
+ }
+
+ u->source->parent.process_msg = source_process_msg_cb;
+ u->source->set_state = source_set_state_cb;
+ u->source->update_requested_latency = source_update_requested_latency_cb;
+ u->source->set_volume = source_set_volume_cb;
+ u->source->set_mute = source_set_mute_cb;
+ u->source->get_volume = source_get_volume_cb;
+ u->source->get_mute = source_get_mute_cb;
+ u->source->userdata = u;
+
+ pa_source_set_asyncmsgq(u->source, source_master->asyncmsgq);
+
+ /* Create sink */
+ pa_sink_new_data_init(&sink_data);
+ sink_data.driver = __FILE__;
+ sink_data.module = m;
+ if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
+ sink_data.name = pa_sprintf_malloc("%s.echo-cancel", sink_master->name);
+ pa_sink_new_data_set_sample_spec(&sink_data, &sink_ss);
+ pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, sink_master->name);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
+ if (!u->autoloaded)
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
+ pa_proplist_sets(sink_data.proplist, "device.echo-cancel.name", sink_data.name);
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&sink_data);
+ goto fail;
+ }
+
+ if ((u->sink_auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
+ const char *z;
+
+ z = pa_proplist_gets(sink_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Sink %s on %s", sink_data.name, z ? z : sink_master->name);
+ }
+
+ u->sink = pa_sink_new(m->core, &sink_data,
+ PA_SINK_HW_MUTE_CTRL|PA_SINK_HW_VOLUME_CTRL|PA_SINK_DECIBEL_VOLUME|
+ (sink_master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY)));
+ pa_sink_new_data_done(&sink_data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg_cb;
+ u->sink->set_state = sink_set_state_cb;
+ u->sink->update_requested_latency = sink_update_requested_latency_cb;
+ u->sink->request_rewind = sink_request_rewind_cb;
+ u->sink->set_volume = sink_set_volume_cb;
+ u->sink->set_mute = sink_set_mute_cb;
+ u->sink->userdata = u;
+
+ pa_sink_set_asyncmsgq(u->sink, sink_master->asyncmsgq);
+
+ /* Create source output */
+ pa_source_output_new_data_init(&source_output_data);
+ source_output_data.driver = __FILE__;
+ source_output_data.module = m;
+ pa_source_output_new_data_set_source(&source_output_data, source_master, FALSE);
+ source_output_data.destination_source = u->source;
+ /* FIXME
+ source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
+
+ pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_NAME, "Echo-Cancel Source Stream");
+ pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
+ pa_source_output_new_data_set_sample_spec(&source_output_data, &source_ss);
+ pa_source_output_new_data_set_channel_map(&source_output_data, &source_map);
+
+ pa_source_output_new(&u->source_output, m->core, &source_output_data);
+ pa_source_output_new_data_done(&source_output_data);
+
+ if (!u->source_output)
+ goto fail;
+
+ u->source_output->parent.process_msg = source_output_process_msg_cb;
+ u->source_output->push = source_output_push_cb;
+ u->source_output->process_rewind = source_output_process_rewind_cb;
+ u->source_output->update_max_rewind = source_output_update_max_rewind_cb;
+ u->source_output->update_source_requested_latency = source_output_update_source_requested_latency_cb;
+ u->source_output->update_source_latency_range = source_output_update_source_latency_range_cb;
+ u->source_output->update_source_fixed_latency = source_output_update_source_fixed_latency_cb;
+ u->source_output->kill = source_output_kill_cb;
+ u->source_output->attach = source_output_attach_cb;
+ u->source_output->detach = source_output_detach_cb;
+ u->source_output->state_change = source_output_state_change_cb;
+ u->source_output->may_move_to = source_output_may_move_to_cb;
+ u->source_output->moving = source_output_moving_cb;
+ u->source_output->userdata = u;
+
+ u->source->output_from_master = u->source_output;
+
+ /* Create sink input */
+ pa_sink_input_new_data_init(&sink_input_data);
+ sink_input_data.driver = __FILE__;
+ sink_input_data.module = m;
+ pa_sink_input_new_data_set_sink(&sink_input_data, sink_master, FALSE);
+ sink_input_data.origin_sink = u->sink;
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Echo-Cancel Sink Stream");
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
+ pa_sink_input_new_data_set_sample_spec(&sink_input_data, &sink_ss);
+ pa_sink_input_new_data_set_channel_map(&sink_input_data, &sink_map);
+ sink_input_data.flags = PA_SINK_INPUT_VARIABLE_RATE;
+
+ pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
+ pa_sink_input_new_data_done(&sink_input_data);
+
+ if (!u->sink_input)
+ goto fail;
+
+ u->sink_input->parent.process_msg = sink_input_process_msg_cb;
+ u->sink_input->pop = sink_input_pop_cb;
+ u->sink_input->process_rewind = sink_input_process_rewind_cb;
+ u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
+ u->sink_input->update_max_request = sink_input_update_max_request_cb;
+ u->sink_input->update_sink_requested_latency = sink_input_update_sink_requested_latency_cb;
+ u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
+ u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
+ u->sink_input->kill = sink_input_kill_cb;
+ u->sink_input->attach = sink_input_attach_cb;
+ u->sink_input->detach = sink_input_detach_cb;
+ u->sink_input->state_change = sink_input_state_change_cb;
+ u->sink_input->may_move_to = sink_input_may_move_to_cb;
+ u->sink_input->moving = sink_input_moving_cb;
+ u->sink_input->volume_changed = sink_input_volume_changed_cb;
+ u->sink_input->mute_changed = sink_input_mute_changed_cb;
+ u->sink_input->userdata = u;
+
+ u->sink->input_to_master = u->sink_input;
+
+ pa_sink_input_get_silence(u->sink_input, &silence);
+
+ u->source_memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0,
+ pa_frame_size(&source_ss), 1, 1, 0, &silence);
+ u->sink_memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0,
+ pa_frame_size(&sink_ss), 1, 1, 0, &silence);
+
+ pa_memblock_unref(silence.memblock);
+
+ if (!u->source_memblockq || !u->sink_memblockq) {
+ pa_log("Failed to create memblockq.");
+ goto fail;
+ }
+
+ /* our source and sink are not suspended when we create them */
+ u->active_mask = 3;
+
+ if (u->adjust_time > 0)
+ u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
+
+ if (u->save_aec) {
+ pa_log("Creating AEC files in /tmp");
+ u->captured_file = fopen("/tmp/aec_rec.sw", "wb");
+ if (u->captured_file == NULL)
+ perror ("fopen failed");
+ u->played_file = fopen("/tmp/aec_play.sw", "wb");
+ if (u->played_file == NULL)
+ perror ("fopen failed");
+ u->canceled_file = fopen("/tmp/aec_out.sw", "wb");
+ if (u->canceled_file == NULL)
+ perror ("fopen failed");
+ }
+
+ pa_sink_put(u->sink);
+ pa_source_put(u->source);
+
+ pa_sink_input_put(u->sink_input);
+ pa_source_output_put(u->source_output);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink) + pa_source_linked_by(u->source);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ /* See comments in source_output_kill_cb() above regarding
+ * destruction order! */
+
+ if (u->time_event)
+ u->core->mainloop->time_free(u->time_event);
+
+ if (u->source_output)
+ pa_source_output_unlink(u->source_output);
+ if (u->sink_input)
+ pa_sink_input_unlink(u->sink_input);
+
+ if (u->source)
+ pa_source_unlink(u->source);
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->source_output)
+ pa_source_output_unref(u->source_output);
+ if (u->sink_input)
+ pa_sink_input_unref(u->sink_input);
+
+ if (u->source)
+ pa_source_unref(u->source);
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->source_memblockq)
+ pa_memblockq_free(u->source_memblockq);
+ if (u->sink_memblockq)
+ pa_memblockq_free(u->sink_memblockq);
+
+ if (u->ec->pp_state)
+ speex_preprocess_state_destroy(u->ec->pp_state);
+
+ if (u->ec) {
+ if (u->ec->done)
+ u->ec->done(u->ec);
+
+ pa_xfree(u->ec);
+ }
+
+ if (u->asyncmsgq)
+ pa_asyncmsgq_unref(u->asyncmsgq);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/echo-cancel/speex.c b/src/modules/echo-cancel/speex.c
new file mode 100644
index 00000000..72c52680
--- /dev/null
+++ b/src/modules/echo-cancel/speex.c
@@ -0,0 +1,115 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2010 Wim Taymans <wim.taymans@gmail.com>
+
+ Contributor: Arun Raghavan <arun.raghavan@collabora.co.uk>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/modargs.h>
+#include "echo-cancel.h"
+
+/* should be between 10-20 ms */
+#define DEFAULT_FRAME_SIZE_MS 20
+/* should be between 100-500 ms */
+#define DEFAULT_FILTER_SIZE_MS 200
+
+static const char* const valid_modargs[] = {
+ "frame_size_ms",
+ "filter_size_ms",
+ NULL
+};
+
+static void pa_speex_ec_fixate_spec(pa_sample_spec *source_ss, pa_channel_map *source_map,
+ pa_sample_spec *sink_ss, pa_channel_map *sink_map)
+{
+ source_ss->format = PA_SAMPLE_S16NE;
+
+ *sink_ss = *source_ss;
+ *sink_map = *source_map;
+}
+
+pa_bool_t pa_speex_ec_init(pa_core *c, pa_echo_canceller *ec,
+ pa_sample_spec *source_ss, pa_channel_map *source_map,
+ pa_sample_spec *sink_ss, pa_channel_map *sink_map,
+ uint32_t *blocksize, const char *args)
+{
+ int framelen, y, rate;
+ uint32_t frame_size_ms, filter_size_ms;
+ pa_modargs *ma;
+
+ if (!(ma = pa_modargs_new(args, valid_modargs))) {
+ pa_log("Failed to parse submodule arguments.");
+ goto fail;
+ }
+
+ filter_size_ms = DEFAULT_FILTER_SIZE_MS;
+ if (pa_modargs_get_value_u32(ma, "filter_size_ms", &filter_size_ms) < 0 || filter_size_ms < 1 || filter_size_ms > 2000) {
+ pa_log("Invalid filter_size_ms specification");
+ goto fail;
+ }
+
+ frame_size_ms = DEFAULT_FRAME_SIZE_MS;
+ if (pa_modargs_get_value_u32(ma, "frame_size_ms", &frame_size_ms) < 0 || frame_size_ms < 1 || frame_size_ms > 200) {
+ pa_log("Invalid frame_size_ms specification");
+ goto fail;
+ }
+
+ pa_speex_ec_fixate_spec(source_ss, source_map, sink_ss, sink_map);
+
+ rate = source_ss->rate;
+ framelen = (rate * frame_size_ms) / 1000;
+ /* framelen should be a power of 2, round down to nearest power of two */
+ y = 1 << ((8 * sizeof (int)) - 2);
+ while (y > framelen)
+ y >>= 1;
+ framelen = y;
+
+ *blocksize = framelen * pa_frame_size (source_ss);
+
+ pa_log_debug ("Using framelen %d, blocksize %u, channels %d, rate %d", framelen, *blocksize, source_ss->channels, source_ss->rate);
+
+ ec->params.priv.speex.state = speex_echo_state_init_mc (framelen, (rate * filter_size_ms) / 1000, source_ss->channels, source_ss->channels);
+
+ if (!ec->params.priv.speex.state)
+ goto fail;
+
+ speex_echo_ctl(ec->params.priv.speex.state, SPEEX_ECHO_SET_SAMPLING_RATE, &rate);
+
+ pa_modargs_free(ma);
+ return TRUE;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+ return FALSE;
+}
+
+void pa_speex_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out) {
+ speex_echo_cancellation(ec->params.priv.speex.state, (const spx_int16_t *) rec, (const spx_int16_t *) play, (spx_int16_t *) out);
+}
+
+void pa_speex_ec_done(pa_echo_canceller *ec) {
+ if (ec->params.priv.speex.state)
+ speex_echo_state_destroy(ec->params.priv.speex.state);
+ ec->params.priv.speex.state = NULL;
+}
diff --git a/src/modules/gconf/gconf-helper.c b/src/modules/gconf/gconf-helper.c
new file mode 100644
index 00000000..fbd8cfd8
--- /dev/null
+++ b/src/modules/gconf/gconf-helper.c
@@ -0,0 +1,133 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <gconf/gconf-client.h>
+#include <glib.h>
+
+#include <pulsecore/core-util.h>
+
+#define PA_GCONF_ROOT "/system/pulseaudio"
+#define PA_GCONF_PATH_MODULES PA_GCONF_ROOT"/modules"
+
+static void handle_module(GConfClient *client, const char *name) {
+ gchar p[1024];
+ gboolean enabled, locked;
+ int i;
+
+ pa_snprintf(p, sizeof(p), PA_GCONF_PATH_MODULES"/%s/locked", name);
+ locked = gconf_client_get_bool(client, p, FALSE);
+
+ if (locked)
+ return;
+
+ pa_snprintf(p, sizeof(p), PA_GCONF_PATH_MODULES"/%s/enabled", name);
+ enabled = gconf_client_get_bool(client, p, FALSE);
+
+ printf("%c%s%c", enabled ? '+' : '-', name, 0);
+
+ if (enabled) {
+
+ for (i = 0; i < 10; i++) {
+ gchar *n, *a;
+
+ pa_snprintf(p, sizeof(p), PA_GCONF_PATH_MODULES"/%s/name%i", name, i);
+ if (!(n = gconf_client_get_string(client, p, NULL)) || !*n)
+ break;
+
+ pa_snprintf(p, sizeof(p), PA_GCONF_PATH_MODULES"/%s/args%i", name, i);
+ a = gconf_client_get_string(client, p, NULL);
+
+ printf("%s%c%s%c", n, 0, a ? a : "", 0);
+
+ g_free(n);
+ g_free(a);
+ }
+
+ printf("%c", 0);
+ }
+
+ fflush(stdout);
+}
+
+static void modules_callback(
+ GConfClient* client,
+ guint cnxn_id,
+ GConfEntry *entry,
+ gpointer user_data) {
+
+ const char *n;
+ char buf[128];
+
+ g_assert(strncmp(entry->key, PA_GCONF_PATH_MODULES"/", sizeof(PA_GCONF_PATH_MODULES)) == 0);
+
+ n = entry->key + sizeof(PA_GCONF_PATH_MODULES);
+
+ g_strlcpy(buf, n, sizeof(buf));
+ buf[strcspn(buf, "/")] = 0;
+
+ handle_module(client, buf);
+}
+
+int main(int argc, char *argv[]) {
+ GMainLoop *g;
+ GConfClient *client;
+ GSList *modules, *m;
+
+ g_type_init();
+
+ if (!(client = gconf_client_get_default()))
+ goto fail;
+
+ gconf_client_add_dir(client, PA_GCONF_ROOT, GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
+ gconf_client_notify_add(client, PA_GCONF_PATH_MODULES, modules_callback, NULL, NULL, NULL);
+
+ modules = gconf_client_all_dirs(client, PA_GCONF_PATH_MODULES, NULL);
+
+ for (m = modules; m; m = m->next) {
+ char *e = strrchr(m->data, '/');
+ handle_module(client, e ? e+1 : m->data);
+ }
+
+ g_slist_free(modules);
+
+ /* Signal the parent that we are now initialized */
+ printf("!");
+ fflush(stdout);
+
+ g = g_main_loop_new(NULL, FALSE);
+ g_main_loop_run(g);
+ g_main_loop_unref(g);
+
+ g_object_unref(G_OBJECT(client));
+
+ return 0;
+
+fail:
+ return 1;
+}
diff --git a/src/modules/gconf/module-gconf.c b/src/modules/gconf/module-gconf.c
new file mode 100644
index 00000000..3bad9113
--- /dev/null
+++ b/src/modules/gconf/module-gconf.c
@@ -0,0 +1,402 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <pulse/xmalloc.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulse/mainloop-api.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/start-child.h>
+
+#include "module-gconf-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("GConf Adapter");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+#define MAX_MODULES 10
+#define BUF_MAX 2048
+
+struct module_item {
+ char *name;
+ char *args;
+ uint32_t index;
+};
+
+struct module_info {
+ char *name;
+
+ struct module_item items[MAX_MODULES];
+ unsigned n_items;
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+
+ pa_hashmap *module_infos;
+
+ pid_t pid;
+
+ int fd;
+ int fd_type;
+ pa_io_event *io_event;
+
+ char buf[BUF_MAX];
+ size_t buf_fill;
+};
+
+static int fill_buf(struct userdata *u) {
+ ssize_t r;
+ pa_assert(u);
+
+ if (u->buf_fill >= BUF_MAX) {
+ pa_log("read buffer overflow");
+ return -1;
+ }
+
+ if ((r = pa_read(u->fd, u->buf + u->buf_fill, BUF_MAX - u->buf_fill, &u->fd_type)) <= 0)
+ return -1;
+
+ u->buf_fill += (size_t) r;
+ return 0;
+}
+
+static int read_byte(struct userdata *u) {
+ int ret;
+ pa_assert(u);
+
+ if (u->buf_fill < 1)
+ if (fill_buf(u) < 0)
+ return -1;
+
+ ret = u->buf[0];
+ pa_assert(u->buf_fill > 0);
+ u->buf_fill--;
+ memmove(u->buf, u->buf+1, u->buf_fill);
+ return ret;
+}
+
+static char *read_string(struct userdata *u) {
+ pa_assert(u);
+
+ for (;;) {
+ char *e;
+
+ if ((e = memchr(u->buf, 0, u->buf_fill))) {
+ char *ret = pa_xstrdup(u->buf);
+ u->buf_fill -= (size_t) (e - u->buf +1);
+ memmove(u->buf, e+1, u->buf_fill);
+ return ret;
+ }
+
+ if (fill_buf(u) < 0)
+ return NULL;
+ }
+}
+
+static void unload_one_module(struct userdata *u, struct module_info*m, unsigned i) {
+ pa_assert(u);
+ pa_assert(m);
+ pa_assert(i < m->n_items);
+
+ if (m->items[i].index == PA_INVALID_INDEX)
+ return;
+
+ pa_log_debug("Unloading module #%i", m->items[i].index);
+ pa_module_unload_by_index(u->core, m->items[i].index, TRUE);
+ m->items[i].index = PA_INVALID_INDEX;
+ pa_xfree(m->items[i].name);
+ pa_xfree(m->items[i].args);
+ m->items[i].name = m->items[i].args = NULL;
+}
+
+static void unload_all_modules(struct userdata *u, struct module_info*m) {
+ unsigned i;
+
+ pa_assert(u);
+ pa_assert(m);
+
+ for (i = 0; i < m->n_items; i++)
+ unload_one_module(u, m, i);
+
+ m->n_items = 0;
+}
+
+static void load_module(
+ struct userdata *u,
+ struct module_info *m,
+ unsigned i,
+ const char *name,
+ const char *args,
+ pa_bool_t is_new) {
+
+ pa_module *mod;
+
+ pa_assert(u);
+ pa_assert(m);
+ pa_assert(name);
+ pa_assert(args);
+
+ if (!is_new) {
+ if (m->items[i].index != PA_INVALID_INDEX &&
+ strcmp(m->items[i].name, name) == 0 &&
+ strcmp(m->items[i].args, args) == 0)
+ return;
+
+ unload_one_module(u, m, i);
+ }
+
+ pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name, args);
+
+ m->items[i].name = pa_xstrdup(name);
+ m->items[i].args = pa_xstrdup(args);
+ m->items[i].index = PA_INVALID_INDEX;
+
+ if (!(mod = pa_module_load(u->core, name, args))) {
+ pa_log("pa_module_load() failed");
+ return;
+ }
+
+ m->items[i].index = mod->index;
+}
+
+static void module_info_free(void *p, void *userdata) {
+ struct module_info *m = p;
+ struct userdata *u = userdata;
+
+ pa_assert(m);
+ pa_assert(u);
+
+ unload_all_modules(u, m);
+ pa_xfree(m->name);
+ pa_xfree(m);
+}
+
+static int handle_event(struct userdata *u) {
+ int opcode;
+ int ret = 0;
+
+ do {
+ if ((opcode = read_byte(u)) < 0){
+ if (errno == EINTR || errno == EAGAIN)
+ break;
+ goto fail;
+ }
+
+ switch (opcode) {
+ case '!':
+ /* The helper tool is now initialized */
+ ret = 1;
+ break;
+
+ case '+': {
+ char *name;
+ struct module_info *m;
+ unsigned i, j;
+
+ if (!(name = read_string(u)))
+ goto fail;
+
+ if (!(m = pa_hashmap_get(u->module_infos, name))) {
+ m = pa_xnew(struct module_info, 1);
+ m->name = name;
+ m->n_items = 0;
+ pa_hashmap_put(u->module_infos, m->name, m);
+ } else
+ pa_xfree(name);
+
+ i = 0;
+ while (i < MAX_MODULES) {
+ char *module, *args;
+
+ if (!(module = read_string(u))) {
+ if (i > m->n_items) m->n_items = i;
+ goto fail;
+ }
+
+ if (!*module) {
+ pa_xfree(module);
+ break;
+ }
+
+ if (!(args = read_string(u))) {
+ pa_xfree(module);
+
+ if (i > m->n_items) m->n_items = i;
+ goto fail;
+ }
+
+ load_module(u, m, i, module, args, i >= m->n_items);
+
+ i++;
+
+ pa_xfree(module);
+ pa_xfree(args);
+ }
+
+ /* Unload all removed modules */
+ for (j = i; j < m->n_items; j++)
+ unload_one_module(u, m, j);
+
+ m->n_items = i;
+
+ break;
+ }
+
+ case '-': {
+ char *name;
+ struct module_info *m;
+
+ if (!(name = read_string(u)))
+ goto fail;
+
+ if ((m = pa_hashmap_get(u->module_infos, name))) {
+ pa_hashmap_remove(u->module_infos, name);
+ module_info_free(m, u);
+ }
+
+ pa_xfree(name);
+
+ break;
+ }
+ }
+ } while (u->buf_fill > 0 && ret == 0);
+
+ return ret;
+
+fail:
+ pa_log("Unable to read or parse data from client.");
+ return -1;
+}
+
+static void io_event_cb(
+ pa_mainloop_api*a,
+ pa_io_event* e,
+ int fd,
+ pa_io_event_flags_t events,
+ void *userdata) {
+
+ struct userdata *u = userdata;
+
+ if (handle_event(u) < 0) {
+
+ if (u->io_event) {
+ u->core->mainloop->io_free(u->io_event);
+ u->io_event = NULL;
+ }
+
+ pa_module_unload_request(u->module, TRUE);
+ }
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ int r;
+
+ u = pa_xnew(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ m->userdata = u;
+ u->module_infos = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ u->pid = (pid_t) -1;
+ u->fd = -1;
+ u->fd_type = 0;
+ u->io_event = NULL;
+ u->buf_fill = 0;
+
+ if ((u->fd = pa_start_child_for_read(
+#if defined(__linux__) && !defined(__OPTIMIZE__)
+ pa_run_from_build_tree() ? PA_BUILDDIR "/gconf-helper" :
+#endif
+ PA_GCONF_HELPER, NULL, &u->pid)) < 0)
+ goto fail;
+
+ u->io_event = m->core->mainloop->io_new(
+ m->core->mainloop,
+ u->fd,
+ PA_IO_EVENT_INPUT,
+ io_event_cb,
+ u);
+
+ do {
+ if ((r = handle_event(u)) < 0)
+ goto fail;
+
+ /* Read until the client signalled us that it is ready with
+ * initialization */
+ } while (r != 1);
+
+ return 0;
+
+fail:
+ pa__done(m);
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->pid != (pid_t) -1) {
+ kill(u->pid, SIGTERM);
+
+ for (;;) {
+ if (waitpid(u->pid, NULL, 0) >= 0)
+ break;
+
+ if (errno != EINTR) {
+ pa_log("waitpid() failed: %s", pa_cstrerror(errno));
+ break;
+ }
+ }
+ }
+
+ if (u->io_event)
+ m->core->mainloop->io_free(u->io_event);
+
+ if (u->fd >= 0)
+ pa_close(u->fd);
+
+ if (u->module_infos)
+ pa_hashmap_free(u->module_infos, module_info_free, u);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/hal-util.c b/src/modules/hal-util.c
new file mode 100644
index 00000000..2d59f51d
--- /dev/null
+++ b/src/modules/hal-util.c
@@ -0,0 +1,130 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/log.h>
+#include <pulsecore/dbus-shared.h>
+
+#include <hal/libhal.h>
+
+#include "hal-util.h"
+
+int pa_hal_get_info(pa_core *core, pa_proplist *p, int card) {
+ pa_dbus_connection *c = NULL;
+ LibHalContext *hal = NULL;
+ DBusError error;
+ int r = -1;
+ char **udis = NULL, *t;
+ int n, i;
+
+ pa_assert(core);
+ pa_assert(p);
+ pa_assert(card >= 0);
+
+ dbus_error_init(&error);
+
+ if (!(c = pa_dbus_bus_get(core, DBUS_BUS_SYSTEM, &error)) || dbus_error_is_set(&error)) {
+ pa_log_error("Unable to contact DBUS system bus: %s: %s", error.name, error.message);
+ goto finish;
+ }
+
+
+ if (!(hal = libhal_ctx_new())) {
+ pa_log_error("libhal_ctx_new() finished");
+ goto finish;
+ }
+
+ if (!libhal_ctx_set_dbus_connection(hal, pa_dbus_connection_get(c))) {
+ pa_log_error("Error establishing DBUS connection: %s: %s", error.name, error.message);
+ goto finish;
+ }
+
+ if (!libhal_ctx_init(hal, &error)) {
+ pa_log_error("Couldn't connect to hald: %s: %s", error.name, error.message);
+ goto finish;
+ }
+
+ if (!(udis = libhal_find_device_by_capability(hal, "sound", &n, &error))) {
+ pa_log_error("Couldn't find devices: %s: %s", error.name, error.message);
+ goto finish;
+ }
+
+ for (i = 0; i < n; i++) {
+ dbus_int32_t this_card;
+
+ this_card = libhal_device_get_property_int(hal, udis[i], "sound.card", &error);
+ if (dbus_error_is_set(&error)) {
+ dbus_error_free(&error);
+ continue;
+ }
+
+ if (this_card == card)
+ break;
+
+ }
+
+ if (i >= n)
+ goto finish;
+
+ pa_proplist_sets(p, "hal.udi", udis[i]);
+
+ /* The data HAL stores in info.product is not actually a product
+ * string but simply the ALSA card name. We will hence not write
+ * it to PA_PROP_DEVICE_PRODUCT_NAME */
+ t = libhal_device_get_property_string(hal, udis[i], "info.product", &error);
+ if (dbus_error_is_set(&error))
+ dbus_error_free(&error);
+ if (t) {
+ pa_proplist_sets(p, "hal.product", t);
+ libhal_free_string(t);
+ }
+
+ t = libhal_device_get_property_string(hal, udis[i], "sound.card_id", &error);
+ if (dbus_error_is_set(&error))
+ dbus_error_free(&error);
+ if (t) {
+ pa_proplist_sets(p, "hal.card_id", t);
+ libhal_free_string(t);
+ }
+
+ r = 0;
+
+finish:
+
+ if (udis)
+ libhal_free_string_array(udis);
+
+ dbus_error_free(&error);
+
+ if (hal) {
+ libhal_ctx_shutdown(hal, &error);
+ libhal_ctx_free(hal);
+ dbus_error_free(&error);
+ }
+
+ if (c)
+ pa_dbus_connection_unref(c);
+
+ return r;
+}
diff --git a/src/modules/hal-util.h b/src/modules/hal-util.h
new file mode 100644
index 00000000..19e41d77
--- /dev/null
+++ b/src/modules/hal-util.h
@@ -0,0 +1,30 @@
+#ifndef foohalutilhfoo
+#define foohalutilhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+
+#include <pulsecore/core.h>
+
+int pa_hal_get_info(pa_core *core, pa_proplist *p, int card);
+
+#endif
diff --git a/src/modules/jack/module-jack-sink.c b/src/modules/jack/module-jack-sink.c
new file mode 100644
index 00000000..35b0385d
--- /dev/null
+++ b/src/modules/jack/module-jack-sink.c
@@ -0,0 +1,509 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <jack/jack.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/sink.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/sample-util.h>
+
+#include "module-jack-sink-symdef.h"
+
+/* General overview:
+ *
+ * Because JACK has a very unflexible event loop management which
+ * doesn't allow us to add our own event sources to the event thread
+ * we cannot use the JACK real-time thread for dispatching our PA
+ * work. Instead, we run an additional RT thread which does most of
+ * the PA handling, and have the JACK RT thread request data from it
+ * via pa_asyncmsgq. The cost is an additional context switch which
+ * should hopefully not be that expensive if RT scheduling is
+ * enabled. A better fix would only be possible with additional event
+ * source support in JACK.
+ */
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("JACK Sink");
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_USAGE(
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the card> "
+ "server_name=<jack server name> "
+ "client_name=<jack client name> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "connect=<connect ports?>");
+
+#define DEFAULT_SINK_NAME "jack_out"
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_sink *sink;
+
+ unsigned channels;
+
+ jack_port_t* port[PA_CHANNELS_MAX];
+ jack_client_t *client;
+
+ void *buffer[PA_CHANNELS_MAX];
+
+ pa_thread_mq thread_mq;
+ pa_asyncmsgq *jack_msgq;
+ pa_rtpoll *rtpoll;
+ pa_rtpoll_item *rtpoll_item;
+
+ pa_thread *thread;
+
+ jack_nframes_t frames_in_buffer;
+ jack_nframes_t saved_frame_time;
+ pa_bool_t saved_frame_time_valid;
+};
+
+static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
+ "server_name",
+ "client_name",
+ "channels",
+ "channel_map",
+ "connect",
+ NULL
+};
+
+enum {
+ SINK_MESSAGE_RENDER = PA_SINK_MESSAGE_MAX,
+ SINK_MESSAGE_BUFFER_SIZE,
+ SINK_MESSAGE_ON_SHUTDOWN
+};
+
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *memchunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case SINK_MESSAGE_RENDER:
+
+ /* Handle the request from the JACK thread */
+
+ if (u->sink->thread_info.state == PA_SINK_RUNNING) {
+ pa_memchunk chunk;
+ size_t nbytes;
+ void *p;
+
+ pa_assert(offset > 0);
+ nbytes = (size_t) offset * pa_frame_size(&u->sink->sample_spec);
+
+ pa_sink_render_full(u->sink, nbytes, &chunk);
+
+ p = (uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index;
+ pa_deinterleave(p, u->buffer, u->channels, sizeof(float), (unsigned) offset);
+ pa_memblock_release(chunk.memblock);
+
+ pa_memblock_unref(chunk.memblock);
+ } else {
+ unsigned c;
+ pa_sample_spec ss;
+
+ /* Humm, we're not RUNNING, hence let's write some silence */
+ /* This can happen if we're paused, or during shutdown (when we're unlinked but jack is still running). */
+
+ ss = u->sink->sample_spec;
+ ss.channels = 1;
+
+ for (c = 0; c < u->channels; c++)
+ pa_silence_memory(u->buffer[c], (size_t) offset * pa_sample_size(&ss), &ss);
+ }
+
+ u->frames_in_buffer = (jack_nframes_t) offset;
+ u->saved_frame_time = * (jack_nframes_t*) data;
+ u->saved_frame_time_valid = TRUE;
+
+ return 0;
+
+ case SINK_MESSAGE_BUFFER_SIZE:
+ pa_sink_set_max_request_within_thread(u->sink, (size_t) offset * pa_frame_size(&u->sink->sample_spec));
+ return 0;
+
+ case SINK_MESSAGE_ON_SHUTDOWN:
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ return 0;
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ jack_nframes_t l, ft, d;
+ size_t n;
+
+ /* This is the "worst-case" latency */
+ l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer;
+
+ if (u->saved_frame_time_valid) {
+ /* Adjust the worst case latency by the time that
+ * passed since we last handed data to JACK */
+
+ ft = jack_frame_time(u->client);
+ d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0;
+ l = l > d ? l - d : 0;
+ }
+
+ /* Convert it to usec */
+ n = l * pa_frame_size(&u->sink->sample_spec);
+ *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
+
+ return 0;
+ }
+
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, memchunk);
+}
+
+/* JACK Callback: This is called when JACK needs some data */
+static int jack_process(jack_nframes_t nframes, void *arg) {
+ struct userdata *u = arg;
+ unsigned c;
+ jack_nframes_t frame_time;
+ pa_assert(u);
+
+ /* We just forward the request to our other RT thread */
+
+ for (c = 0; c < u->channels; c++)
+ pa_assert_se(u->buffer[c] = jack_port_get_buffer(u->port[c], nframes));
+
+ frame_time = jack_frame_time(u->client);
+
+ pa_assert_se(pa_asyncmsgq_send(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_RENDER, &frame_time, nframes, NULL) == 0);
+ return 0;
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority);
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ for (;;) {
+ int ret;
+
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
+ if (u->sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(u->sink, 0);
+
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+/* JACK Callback: This is called when JACK triggers an error */
+static void jack_error_func(const char*t) {
+ char *s;
+
+ s = pa_xstrndup(t, strcspn(t, "\n\r"));
+ pa_log_warn("JACK error >%s<", s);
+ pa_xfree(s);
+}
+
+/* JACK Callback: This is called when JACK is set up */
+static void jack_init(void *arg) {
+ struct userdata *u = arg;
+
+ pa_log_info("JACK thread starting up.");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority+4);
+}
+
+/* JACK Callback: This is called when JACK kicks us */
+static void jack_shutdown(void* arg) {
+ struct userdata *u = arg;
+
+ pa_log_info("JACK thread shutting down.");
+ pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_ON_SHUTDOWN, NULL, 0, NULL, NULL);
+}
+
+/* JACK Callback: This is called when JACK changes the buffer size */
+static int jack_buffer_size(jack_nframes_t nframes, void *arg) {
+ struct userdata *u = arg;
+
+ pa_log_info("JACK buffer size changed.");
+ pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_BUFFER_SIZE, NULL, nframes, NULL, NULL);
+ return 0;
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u = NULL;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_modargs *ma = NULL;
+ jack_status_t status;
+ const char *server_name, *client_name;
+ uint32_t channels = 0;
+ pa_bool_t do_connect = TRUE;
+ unsigned i;
+ const char **ports = NULL, **p;
+ pa_sink_new_data data;
+
+ pa_assert(m);
+
+ jack_set_error_function(jack_error_func);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) {
+ pa_log("Failed to parse connect= argument.");
+ goto fail;
+ }
+
+ server_name = pa_modargs_get_value(ma, "server_name", NULL);
+ client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio JACK Sink");
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->saved_frame_time_valid = FALSE;
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ /* The queue linking the JACK thread and our RT thread */
+ u->jack_msgq = pa_asyncmsgq_new(0);
+
+ /* The msgq from the JACK RT thread should have an even higher
+ * priority than the normal message queues, to match the guarantee
+ * all other drivers make: supplying the audio device with data is
+ * the top priority -- and as long as that is possible we don't do
+ * anything else */
+ u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->jack_msgq);
+
+ if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
+ pa_log("jack_client_open() failed.");
+ goto fail;
+ }
+
+ ports = jack_get_ports(u->client, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsInput);
+
+ channels = 0;
+ if (ports)
+ for (p = ports; *p; p++)
+ channels++;
+
+ if (!channels)
+ channels = m->core->default_sample_spec.channels;
+
+ if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
+ channels <= 0 ||
+ channels > PA_CHANNELS_MAX) {
+ pa_log("Failed to parse channels= argument.");
+ goto fail;
+ }
+
+ if (channels == m->core->default_channel_map.channels)
+ map = m->core->default_channel_map;
+ else
+ pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA);
+
+ if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) {
+ pa_log("Failed to parse channel_map= argument.");
+ goto fail;
+ }
+
+ pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client));
+
+ u->channels = ss.channels = (uint8_t) channels;
+ ss.rate = jack_get_sample_rate(u->client);
+ ss.format = PA_SAMPLE_FLOAT32NE;
+
+ pa_assert(pa_sample_spec_valid(&ss));
+
+ for (i = 0; i < ss.channels; i++) {
+ if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) {
+ pa_log("jack_port_register() failed.");
+ goto fail;
+ }
+ }
+
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "jack");
+ if (server_name)
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server_name);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Jack sink (%s)", jack_get_client_name(u->client));
+ pa_proplist_sets(data.proplist, "jack.client_name", jack_get_client_name(u->client));
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&data);
+ goto fail;
+ }
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg;
+ u->sink->userdata = u;
+
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+ pa_sink_set_max_request(u->sink, jack_get_buffer_size(u->client) * pa_frame_size(&u->sink->sample_spec));
+
+ jack_set_process_callback(u->client, jack_process, u);
+ jack_on_shutdown(u->client, jack_shutdown, u);
+ jack_set_thread_init_callback(u->client, jack_init, u);
+ jack_set_buffer_size_callback(u->client, jack_buffer_size, u);
+
+ if (!(u->thread = pa_thread_new("jack-sink", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ if (jack_activate(u->client)) {
+ pa_log("jack_activate() failed");
+ goto fail;
+ }
+
+ if (do_connect) {
+ for (i = 0, p = ports; i < ss.channels; i++, p++) {
+
+ if (!p || !*p) {
+ pa_log("Not enough physical output ports, leaving unconnected.");
+ break;
+ }
+
+ pa_log_info("Connecting %s to %s", jack_port_name(u->port[i]), *p);
+
+ if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) {
+ pa_log("Failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
+ break;
+ }
+ }
+ }
+
+ pa_sink_put(u->sink);
+
+ if (ports)
+ jack_free(ports);
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ if (ports)
+ jack_free(ports);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->client)
+ jack_client_close(u->client);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+
+ if (u->jack_msgq)
+ pa_asyncmsgq_unref(u->jack_msgq);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/jack/module-jack-source.c b/src/modules/jack/module-jack-source.c
new file mode 100644
index 00000000..13109f3e
--- /dev/null
+++ b/src/modules/jack/module-jack-source.c
@@ -0,0 +1,453 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <jack/jack.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/source.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/sample-util.h>
+
+#include "module-jack-source-symdef.h"
+
+/* See module-jack-sink for a few comments how this module basically
+ * works */
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("JACK Source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(
+ "source_name=<name for the source> "
+ "source_properties=<properties for the source> "
+ "server_name=<jack server name> "
+ "client_name=<jack client name> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "connect=<connect ports?>");
+
+#define DEFAULT_SOURCE_NAME "jack_in"
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_source *source;
+
+ unsigned channels;
+
+ jack_port_t* port[PA_CHANNELS_MAX];
+ jack_client_t *client;
+
+ pa_thread_mq thread_mq;
+ pa_asyncmsgq *jack_msgq;
+ pa_rtpoll *rtpoll;
+ pa_rtpoll_item *rtpoll_item;
+
+ pa_thread *thread;
+
+ jack_nframes_t saved_frame_time;
+ pa_bool_t saved_frame_time_valid;
+};
+
+static const char* const valid_modargs[] = {
+ "source_name",
+ "source_properties",
+ "server_name",
+ "client_name",
+ "channels",
+ "channel_map",
+ "connect",
+ NULL
+};
+
+enum {
+ SOURCE_MESSAGE_POST = PA_SOURCE_MESSAGE_MAX,
+ SOURCE_MESSAGE_ON_SHUTDOWN
+};
+
+static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE(o)->userdata;
+
+ switch (code) {
+
+ case SOURCE_MESSAGE_POST:
+
+ /* Handle the new block from the JACK thread */
+ pa_assert(chunk);
+ pa_assert(chunk->length > 0);
+
+ if (u->source->thread_info.state == PA_SOURCE_RUNNING)
+ pa_source_post(u->source, chunk);
+
+ u->saved_frame_time = (jack_nframes_t) offset;
+ u->saved_frame_time_valid = TRUE;
+
+ return 0;
+
+ case SOURCE_MESSAGE_ON_SHUTDOWN:
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ return 0;
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ jack_nframes_t l, ft, d;
+ size_t n;
+
+ /* This is the "worst-case" latency */
+ l = jack_port_get_total_latency(u->client, u->port[0]);
+
+ if (u->saved_frame_time_valid) {
+ /* Adjust the worst case latency by the time that
+ * passed since we last handed data to JACK */
+
+ ft = jack_frame_time(u->client);
+ d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0;
+ l += d;
+ }
+
+ /* Convert it to usec */
+ n = l * pa_frame_size(&u->source->sample_spec);
+ *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
+
+ return 0;
+ }
+ }
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
+}
+
+static int jack_process(jack_nframes_t nframes, void *arg) {
+ unsigned c;
+ struct userdata *u = arg;
+ const void *buffer[PA_CHANNELS_MAX];
+ void *p;
+ jack_nframes_t frame_time;
+ pa_memchunk chunk;
+
+ pa_assert(u);
+
+ for (c = 0; c < u->channels; c++)
+ pa_assert_se(buffer[c] = jack_port_get_buffer(u->port[c], nframes));
+
+ /* We interleave the data and pass it on to the other RT thread */
+
+ pa_memchunk_reset(&chunk);
+ chunk.length = nframes * pa_frame_size(&u->source->sample_spec);
+ chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
+ p = pa_memblock_acquire(chunk.memblock);
+ pa_interleave(buffer, u->channels, p, sizeof(float), nframes);
+ pa_memblock_release(chunk.memblock);
+
+ frame_time = jack_frame_time(u->client);
+
+ pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_POST, NULL, frame_time, &chunk, NULL);
+
+ pa_memblock_unref(chunk.memblock);
+
+ return 0;
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority);
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ for (;;) {
+ int ret;
+
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+static void jack_error_func(const char*t) {
+ char *s;
+
+ s = pa_xstrndup(t, strcspn(t, "\n\r"));
+ pa_log_warn("JACK error >%s<", s);
+ pa_xfree(s);
+}
+
+static void jack_init(void *arg) {
+ struct userdata *u = arg;
+
+ pa_log_info("JACK thread starting up.");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority+4);
+}
+
+static void jack_shutdown(void* arg) {
+ struct userdata *u = arg;
+
+ pa_log_info("JACK thread shutting down..");
+ pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_ON_SHUTDOWN, NULL, 0, NULL, NULL);
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u = NULL;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_modargs *ma = NULL;
+ jack_status_t status;
+ const char *server_name, *client_name;
+ uint32_t channels = 0;
+ pa_bool_t do_connect = TRUE;
+ unsigned i;
+ const char **ports = NULL, **p;
+ pa_source_new_data data;
+
+ pa_assert(m);
+
+ jack_set_error_function(jack_error_func);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) {
+ pa_log("Failed to parse connect= argument.");
+ goto fail;
+ }
+
+ server_name = pa_modargs_get_value(ma, "server_name", NULL);
+ client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio JACK Source");
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->saved_frame_time_valid = FALSE;
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ u->jack_msgq = pa_asyncmsgq_new(0);
+ u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->jack_msgq);
+
+ if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
+ pa_log("jack_client_open() failed.");
+ goto fail;
+ }
+
+ ports = jack_get_ports(u->client, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsOutput);
+
+ channels = 0;
+ if (ports)
+ for (p = ports; *p; p++)
+ channels++;
+
+ if (!channels)
+ channels = m->core->default_sample_spec.channels;
+
+ if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
+ channels <= 0 ||
+ channels >= PA_CHANNELS_MAX) {
+ pa_log("failed to parse channels= argument.");
+ goto fail;
+ }
+
+ if (channels == m->core->default_channel_map.channels)
+ map = m->core->default_channel_map;
+ else
+ pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA);
+
+ if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) {
+ pa_log("failed to parse channel_map= argument.");
+ goto fail;
+ }
+
+ pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client));
+
+ u->channels = ss.channels = (uint8_t) channels;
+ ss.rate = jack_get_sample_rate(u->client);
+ ss.format = PA_SAMPLE_FLOAT32NE;
+
+ pa_assert(pa_sample_spec_valid(&ss));
+
+ for (i = 0; i < ss.channels; i++) {
+ if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0))) {
+ pa_log("jack_port_register() failed.");
+ goto fail;
+ }
+ }
+
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "jack");
+ if (server_name)
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server_name);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Jack source (%s)", jack_get_client_name(u->client));
+ pa_proplist_sets(data.proplist, "jack.client_name", jack_get_client_name(u->client));
+
+ if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&data);
+ goto fail;
+ }
+
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
+ pa_log("Failed to create source.");
+ goto fail;
+ }
+
+ u->source->parent.process_msg = source_process_msg;
+ u->source->userdata = u;
+
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+
+ jack_set_process_callback(u->client, jack_process, u);
+ jack_on_shutdown(u->client, jack_shutdown, u);
+ jack_set_thread_init_callback(u->client, jack_init, u);
+
+ if (!(u->thread = pa_thread_new("jack-source", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ if (jack_activate(u->client)) {
+ pa_log("jack_activate() failed");
+ goto fail;
+ }
+
+ if (do_connect) {
+ for (i = 0, p = ports; i < ss.channels; i++, p++) {
+
+ if (!p || !*p) {
+ pa_log("Not enough physical output ports, leaving unconnected.");
+ break;
+ }
+
+ pa_log_info("Connecting %s to %s", jack_port_name(u->port[i]), *p);
+
+ if (jack_connect(u->client, *p, jack_port_name(u->port[i]))) {
+ pa_log("Failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
+ break;
+ }
+ }
+
+ }
+
+ pa_source_put(u->source);
+
+ if (ports)
+ jack_free(ports);
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ if (ports)
+ jack_free(ports);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_source_linked_by(u->source);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->source)
+ pa_source_unlink(u->source);
+
+ if (u->client)
+ jack_client_close(u->client);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->source)
+ pa_source_unref(u->source);
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+
+ if (u->jack_msgq)
+ pa_asyncmsgq_unref(u->jack_msgq);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/jack/module-jackdbus-detect.c b/src/modules/jack/module-jackdbus-detect.c
new file mode 100644
index 00000000..864f96b1
--- /dev/null
+++ b/src/modules/jack/module-jackdbus-detect.c
@@ -0,0 +1,298 @@
+/***
+ This file is part of PulseAudio.
+
+ Written by David Henningsson <david.henningsson@canonical.com>
+ Copyright 2010 Canonical Ltd.
+
+ Some code taken from other parts of PulseAudio, these are
+ Copyright 2006-2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/log.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-shared.h>
+
+#include "module-jackdbus-detect-symdef.h"
+
+PA_MODULE_AUTHOR("David Henningsson");
+PA_MODULE_DESCRIPTION("Adds JACK sink/source ports when JACK is started");
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_USAGE("connect=<connect ports?>");
+
+#define JACK_SERVICE_NAME "org.jackaudio.service"
+#define JACK_INTERFACE_NAME "org.jackaudio.JackControl"
+#define JACK_INTERFACE_PATH "/org/jackaudio/Controller"
+
+#define SERVICE_FILTER \
+ "type='signal'," \
+ "sender='" DBUS_SERVICE_DBUS "'," \
+ "interface='" DBUS_INTERFACE_DBUS "'," \
+ "member='NameOwnerChanged'," \
+ "arg0='" JACK_SERVICE_NAME "'"
+
+#define RUNNING_FILTER(_a) \
+ "type='signal'," \
+ "sender='" JACK_SERVICE_NAME "'," \
+ "interface='" JACK_INTERFACE_NAME "'," \
+ "member='" _a "'"
+
+static const char* const valid_modargs[] = {
+ "connect",
+ NULL
+};
+
+#define JACK_SS_SINK 0
+#define JACK_SS_SOURCE 1
+#define JACK_SS_COUNT 2
+
+static const char* const modnames[JACK_SS_COUNT] = {
+ "module-jack-sink",
+ "module-jack-source"
+};
+
+
+struct userdata {
+ pa_module *module;
+ pa_core *core;
+ pa_dbus_connection *connection;
+ pa_bool_t filter_added, match_added;
+ pa_bool_t is_service_started;
+ pa_bool_t autoconnect_ports;
+ /* Using index here protects us from module unloading without us knowing */
+ int jack_module_index[JACK_SS_COUNT];
+};
+
+
+static void ensure_ports_stopped(struct userdata* u) {
+ int i;
+ pa_assert(u);
+
+ for (i = 0; i < JACK_SS_COUNT; i++)
+ if (u->jack_module_index[i]) {
+ pa_module_unload_request_by_index(u->core, u->jack_module_index[i], TRUE);
+ u->jack_module_index[i] = 0;
+ pa_log_info("Stopped %s.", modnames[i]);
+ }
+}
+
+static void ensure_ports_started(struct userdata* u) {
+ int i;
+ pa_assert(u);
+
+ for (i = 0; i < JACK_SS_COUNT; i++)
+ if (!u->jack_module_index[i]) {
+ char* args;
+ pa_module* m;
+ args = pa_sprintf_malloc("connect=%s", pa_yes_no(u->autoconnect_ports));
+ m = pa_module_load(u->core, modnames[i], args);
+ pa_xfree(args);
+
+ if (m) {
+ pa_log_info("Successfully started %s.", modnames[i]);
+ u->jack_module_index[i] = m->index;
+ }
+ else
+ pa_log_info("Failed to start %s.", modnames[i]);
+ }
+}
+
+
+static pa_bool_t check_service_started(struct userdata* u) {
+ DBusError error;
+ DBusMessage *m = NULL, *reply = NULL;
+ pa_bool_t new_status = FALSE;
+ dbus_bool_t call_result;
+ pa_assert(u);
+
+ dbus_error_init(&error);
+
+ /* Just a safety check; it isn't such a big deal if the name disappears just after the call. */
+ if (!dbus_bus_name_has_owner(pa_dbus_connection_get(u->connection),
+ JACK_SERVICE_NAME, &error)) {
+ pa_log_debug("jackdbus isn't running.");
+ goto finish;
+ }
+
+ if (!(m = dbus_message_new_method_call(JACK_SERVICE_NAME, JACK_INTERFACE_PATH, JACK_INTERFACE_NAME, "IsStarted"))) {
+ pa_log("Failed to allocate IsStarted() method call.");
+ goto finish;
+ }
+
+ if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
+ pa_log("IsStarted() call failed: %s: %s", error.name, error.message);
+ goto finish;
+ }
+
+ if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &call_result, DBUS_TYPE_INVALID)) {
+ pa_log("IsStarted() call return failed: %s: %s", error.name, error.message);
+ goto finish;
+ }
+
+ new_status = call_result;
+
+finish:
+ if (m)
+ dbus_message_unref(m);
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+ if (new_status)
+ ensure_ports_started(u);
+ else
+ ensure_ports_stopped(u);
+ u->is_service_started = new_status;
+ return new_status;
+}
+
+static DBusHandlerResult dbus_filter_handler(DBusConnection *c, DBusMessage *s, void *userdata) {
+ struct userdata *u = NULL;
+ DBusError error;
+
+ pa_assert(userdata);
+ u = ((pa_module*) userdata)->userdata;
+ pa_assert(u);
+
+ dbus_error_init(&error);
+
+ if (dbus_message_is_signal(s, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
+ const char *name, *old, *new;
+ if (!dbus_message_get_args(s, &error,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_STRING, &old,
+ DBUS_TYPE_STRING, &new,
+ DBUS_TYPE_INVALID))
+ goto finish;
+ if (strcmp(name, JACK_SERVICE_NAME))
+ goto finish;
+
+ ensure_ports_stopped(u);
+ check_service_started(u);
+ }
+
+ else if (dbus_message_is_signal(s, JACK_INTERFACE_NAME, "ServerStarted")) {
+ ensure_ports_stopped(u);
+ check_service_started(u);
+ }
+
+ else if (dbus_message_is_signal(s, JACK_INTERFACE_NAME, "ServerStopped")) {
+ ensure_ports_stopped(u);
+ }
+
+finish:
+ dbus_error_free(&error);
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+int pa__init(pa_module *m) {
+ DBusError error;
+ pa_dbus_connection *connection = NULL;
+ struct userdata *u = NULL;
+ pa_modargs *ma;
+
+ pa_assert(m);
+
+ dbus_error_init(&error);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->autoconnect_ports = TRUE;
+
+ if (pa_modargs_get_value_boolean(ma, "connect", &u->autoconnect_ports) < 0) {
+ pa_log("Failed to parse connect= argument.");
+ goto fail;
+ }
+
+ if (!(connection = pa_dbus_bus_get(m->core, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
+
+ if (connection)
+ pa_dbus_connection_unref(connection);
+
+ pa_log_error("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);
+ goto fail;
+ }
+ u->connection = connection;
+
+ if (!dbus_connection_add_filter(pa_dbus_connection_get(connection), dbus_filter_handler, m, NULL)) {
+ pa_log_error("Unable to add D-Bus filter");
+ goto fail;
+ }
+ u->filter_added = 1;
+
+ if (pa_dbus_add_matches(
+ pa_dbus_connection_get(connection), &error, SERVICE_FILTER,
+ RUNNING_FILTER("ServerStarted"), RUNNING_FILTER("ServerStopped"), NULL) < 0) {
+ pa_log_error("Unable to subscribe to signals: %s: %s", error.name, error.message);
+ goto fail;
+ }
+ u->match_added = 1;
+
+ check_service_started(u);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ dbus_error_free(&error);
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ ensure_ports_stopped(u);
+
+ if (u->match_added) {
+ pa_dbus_remove_matches(
+ pa_dbus_connection_get(u->connection), SERVICE_FILTER,
+ RUNNING_FILTER("ServerStarted"), RUNNING_FILTER("ServerStopped"), NULL);
+ }
+
+ if (u->filter_added) {
+ dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), dbus_filter_handler, m);
+ }
+
+ if (u->connection) {
+ pa_dbus_connection_unref(u->connection);
+ }
+
+ pa_xfree(u);
+}
diff --git a/src/modules/ladspa.h b/src/modules/ladspa.h
new file mode 100644
index 00000000..b1a9c4e5
--- /dev/null
+++ b/src/modules/ladspa.h
@@ -0,0 +1,603 @@
+/* ladspa.h
+
+ Linux Audio Developer's Simple Plugin API Version 1.1[LGPL].
+ Copyright (C) 2000-2002 Richard W.E. Furse, Paul Barton-Davis,
+ Stefan Westerfeld.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License
+ as published by the Free Software Foundation; either version 2.1 of
+ the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA. */
+
+#ifndef LADSPA_INCLUDED
+#define LADSPA_INCLUDED
+
+#define LADSPA_VERSION "1.1"
+#define LADSPA_VERSION_MAJOR 1
+#define LADSPA_VERSION_MINOR 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*****************************************************************************/
+
+/* Overview:
+
+ There is a large number of synthesis packages in use or development
+ on the Linux platform at this time. This API (`The Linux Audio
+ Developer's Simple Plugin API') attempts to give programmers the
+ ability to write simple `plugin' audio processors in C/C++ and link
+ them dynamically (`plug') into a range of these packages (`hosts').
+ It should be possible for any host and any plugin to communicate
+ completely through this interface.
+
+ This API is deliberately short and simple. To achieve compatibility
+ with a range of promising Linux sound synthesis packages it
+ attempts to find the `greatest common divisor' in their logical
+ behaviour. Having said this, certain limiting decisions are
+ implicit, notably the use of a fixed type (LADSPA_Data) for all
+ data transfer and absence of a parameterised `initialisation'
+ phase. See below for the LADSPA_Data typedef.
+
+ Plugins are expected to distinguish between control and audio
+ data. Plugins have `ports' that are inputs or outputs for audio or
+ control data and each plugin is `run' for a `block' corresponding
+ to a short time interval measured in samples. Audio data is
+ communicated using arrays of LADSPA_Data, allowing a block of audio
+ to be processed by the plugin in a single pass. Control data is
+ communicated using single LADSPA_Data values. Control data has a
+ single value at the start of a call to the `run()' or `run_adding()'
+ function, and may be considered to remain this value for its
+ duration. The plugin may assume that all its input and output ports
+ have been connected to the relevant data location (see the
+ `connect_port()' function below) before it is asked to run.
+
+ Plugins will reside in shared object files suitable for dynamic
+ linking by dlopen() and family. The file will provide a number of
+ `plugin types' that can be used to instantiate actual plugins
+ (sometimes known as `plugin instances') that can be connected
+ together to perform tasks.
+
+ This API contains very limited error-handling. */
+
+/*****************************************************************************/
+
+/* Fundamental data type passed in and out of plugin. This data type
+ is used to communicate audio samples and control values. It is
+ assumed that the plugin will work sensibly given any numeric input
+ value although it may have a preferred range (see hints below).
+
+ For audio it is generally assumed that 1.0f is the `0dB' reference
+ amplitude and is a `normal' signal level. */
+
+typedef float LADSPA_Data;
+
+/*****************************************************************************/
+
+/* Special Plugin Properties:
+
+ Optional features of the plugin type are encapsulated in the
+ LADSPA_Properties type. This is assembled by ORing individual
+ properties together. */
+
+typedef int LADSPA_Properties;
+
+/* Property LADSPA_PROPERTY_REALTIME indicates that the plugin has a
+ real-time dependency (e.g. listens to a MIDI device) and so its
+ output must not be cached or subject to significant latency. */
+#define LADSPA_PROPERTY_REALTIME 0x1
+
+/* Property LADSPA_PROPERTY_INPLACE_BROKEN indicates that the plugin
+ may cease to work correctly if the host elects to use the same data
+ location for both input and output (see connect_port()). This
+ should be avoided as enabling this flag makes it impossible for
+ hosts to use the plugin to process audio `in-place.' */
+#define LADSPA_PROPERTY_INPLACE_BROKEN 0x2
+
+/* Property LADSPA_PROPERTY_HARD_RT_CAPABLE indicates that the plugin
+ is capable of running not only in a conventional host but also in a
+ `hard real-time' environment. To qualify for this the plugin must
+ satisfy all of the following:
+
+ (1) The plugin must not use malloc(), free() or other heap memory
+ management within its run() or run_adding() functions. All new
+ memory used in run() must be managed via the stack. These
+ restrictions only apply to the run() function.
+
+ (2) The plugin will not attempt to make use of any library
+ functions with the exceptions of functions in the ANSI standard C
+ and C maths libraries, which the host is expected to provide.
+
+ (3) The plugin will not access files, devices, pipes, sockets, IPC
+ or any other mechanism that might result in process or thread
+ blocking.
+
+ (4) The plugin will take an amount of time to execute a run() or
+ run_adding() call approximately of form (A+B*SampleCount) where A
+ and B depend on the machine and host in use. This amount of time
+ may not depend on input signals or plugin state. The host is left
+ the responsibility to perform timings to estimate upper bounds for
+ A and B. */
+#define LADSPA_PROPERTY_HARD_RT_CAPABLE 0x4
+
+#define LADSPA_IS_REALTIME(x) ((x) & LADSPA_PROPERTY_REALTIME)
+#define LADSPA_IS_INPLACE_BROKEN(x) ((x) & LADSPA_PROPERTY_INPLACE_BROKEN)
+#define LADSPA_IS_HARD_RT_CAPABLE(x) ((x) & LADSPA_PROPERTY_HARD_RT_CAPABLE)
+
+/*****************************************************************************/
+
+/* Plugin Ports:
+
+ Plugins have `ports' that are inputs or outputs for audio or
+ data. Ports can communicate arrays of LADSPA_Data (for audio
+ inputs/outputs) or single LADSPA_Data values (for control
+ input/outputs). This information is encapsulated in the
+ LADSPA_PortDescriptor type which is assembled by ORing individual
+ properties together.
+
+ Note that a port must be an input or an output port but not both
+ and that a port must be a control or audio port but not both. */
+
+typedef int LADSPA_PortDescriptor;
+
+/* Property LADSPA_PORT_INPUT indicates that the port is an input. */
+#define LADSPA_PORT_INPUT 0x1
+
+/* Property LADSPA_PORT_OUTPUT indicates that the port is an output. */
+#define LADSPA_PORT_OUTPUT 0x2
+
+/* Property LADSPA_PORT_CONTROL indicates that the port is a control
+ port. */
+#define LADSPA_PORT_CONTROL 0x4
+
+/* Property LADSPA_PORT_AUDIO indicates that the port is a audio
+ port. */
+#define LADSPA_PORT_AUDIO 0x8
+
+#define LADSPA_IS_PORT_INPUT(x) ((x) & LADSPA_PORT_INPUT)
+#define LADSPA_IS_PORT_OUTPUT(x) ((x) & LADSPA_PORT_OUTPUT)
+#define LADSPA_IS_PORT_CONTROL(x) ((x) & LADSPA_PORT_CONTROL)
+#define LADSPA_IS_PORT_AUDIO(x) ((x) & LADSPA_PORT_AUDIO)
+
+/*****************************************************************************/
+
+/* Plugin Port Range Hints:
+
+ The host may wish to provide a representation of data entering or
+ leaving a plugin (e.g. to generate a GUI automatically). To make
+ this more meaningful, the plugin should provide `hints' to the host
+ describing the usual values taken by the data.
+
+ Note that these are only hints. The host may ignore them and the
+ plugin must not assume that data supplied to it is meaningful. If
+ the plugin receives invalid input data it is expected to continue
+ to run without failure and, where possible, produce a sensible
+ output (e.g. a high-pass filter given a negative cutoff frequency
+ might switch to an all-pass mode).
+
+ Hints are meaningful for all input and output ports but hints for
+ input control ports are expected to be particularly useful.
+
+ More hint information is encapsulated in the
+ LADSPA_PortRangeHintDescriptor type which is assembled by ORing
+ individual hint types together. Hints may require further
+ LowerBound and UpperBound information.
+
+ All the hint information for a particular port is aggregated in the
+ LADSPA_PortRangeHint structure. */
+
+typedef int LADSPA_PortRangeHintDescriptor;
+
+/* Hint LADSPA_HINT_BOUNDED_BELOW indicates that the LowerBound field
+ of the LADSPA_PortRangeHint should be considered meaningful. The
+ value in this field should be considered the (inclusive) lower
+ bound of the valid range. If LADSPA_HINT_SAMPLE_RATE is also
+ specified then the value of LowerBound should be multiplied by the
+ sample rate. */
+#define LADSPA_HINT_BOUNDED_BELOW 0x1
+
+/* Hint LADSPA_HINT_BOUNDED_ABOVE indicates that the UpperBound field
+ of the LADSPA_PortRangeHint should be considered meaningful. The
+ value in this field should be considered the (inclusive) upper
+ bound of the valid range. If LADSPA_HINT_SAMPLE_RATE is also
+ specified then the value of UpperBound should be multiplied by the
+ sample rate. */
+#define LADSPA_HINT_BOUNDED_ABOVE 0x2
+
+/* Hint LADSPA_HINT_TOGGLED indicates that the data item should be
+ considered a Boolean toggle. Data less than or equal to zero should
+ be considered `off' or `false,' and data above zero should be
+ considered `on' or `true.' LADSPA_HINT_TOGGLED may not be used in
+ conjunction with any other hint except LADSPA_HINT_DEFAULT_0 or
+ LADSPA_HINT_DEFAULT_1. */
+#define LADSPA_HINT_TOGGLED 0x4
+
+/* Hint LADSPA_HINT_SAMPLE_RATE indicates that any bounds specified
+ should be interpreted as multiples of the sample rate. For
+ instance, a frequency range from 0Hz to the Nyquist frequency (half
+ the sample rate) could be requested by this hint in conjunction
+ with LowerBound = 0 and UpperBound = 0.5. Hosts that support bounds
+ at all must support this hint to retain meaning. */
+#define LADSPA_HINT_SAMPLE_RATE 0x8
+
+/* Hint LADSPA_HINT_LOGARITHMIC indicates that it is likely that the
+ user will find it more intuitive to view values using a logarithmic
+ scale. This is particularly useful for frequencies and gains. */
+#define LADSPA_HINT_LOGARITHMIC 0x10
+
+/* Hint LADSPA_HINT_INTEGER indicates that a user interface would
+ probably wish to provide a stepped control taking only integer
+ values. Any bounds set should be slightly wider than the actual
+ integer range required to avoid floating point rounding errors. For
+ instance, the integer set {0,1,2,3} might be described as [-0.1,
+ 3.1]. */
+#define LADSPA_HINT_INTEGER 0x20
+
+/* The various LADSPA_HINT_HAS_DEFAULT_* hints indicate a `normal'
+ value for the port that is sensible as a default. For instance,
+ this value is suitable for use as an initial value in a user
+ interface or as a value the host might assign to a control port
+ when the user has not provided one. Defaults are encoded using a
+ mask so only one default may be specified for a port. Some of the
+ hints make use of lower and upper bounds, in which case the
+ relevant bound or bounds must be available and
+ LADSPA_HINT_SAMPLE_RATE must be applied as usual. The resulting
+ default must be rounded if LADSPA_HINT_INTEGER is present. Default
+ values were introduced in LADSPA v1.1. */
+#define LADSPA_HINT_DEFAULT_MASK 0x3C0
+
+/* This default values indicates that no default is provided. */
+#define LADSPA_HINT_DEFAULT_NONE 0x0
+
+/* This default hint indicates that the suggested lower bound for the
+ port should be used. */
+#define LADSPA_HINT_DEFAULT_MINIMUM 0x40
+
+/* This default hint indicates that a low value between the suggested
+ lower and upper bounds should be chosen. For ports with
+ LADSPA_HINT_LOGARITHMIC, this should be exp(log(lower) * 0.75 +
+ log(upper) * 0.25). Otherwise, this should be (lower * 0.75 + upper
+ * 0.25). */
+#define LADSPA_HINT_DEFAULT_LOW 0x80
+
+/* This default hint indicates that a middle value between the
+ suggested lower and upper bounds should be chosen. For ports with
+ LADSPA_HINT_LOGARITHMIC, this should be exp(log(lower) * 0.5 +
+ log(upper) * 0.5). Otherwise, this should be (lower * 0.5 + upper *
+ 0.5). */
+#define LADSPA_HINT_DEFAULT_MIDDLE 0xC0
+
+/* This default hint indicates that a high value between the suggested
+ lower and upper bounds should be chosen. For ports with
+ LADSPA_HINT_LOGARITHMIC, this should be exp(log(lower) * 0.25 +
+ log(upper) * 0.75). Otherwise, this should be (lower * 0.25 + upper
+ * 0.75). */
+#define LADSPA_HINT_DEFAULT_HIGH 0x100
+
+/* This default hint indicates that the suggested upper bound for the
+ port should be used. */
+#define LADSPA_HINT_DEFAULT_MAXIMUM 0x140
+
+/* This default hint indicates that the number 0 should be used. Note
+ that this default may be used in conjunction with
+ LADSPA_HINT_TOGGLED. */
+#define LADSPA_HINT_DEFAULT_0 0x200
+
+/* This default hint indicates that the number 1 should be used. Note
+ that this default may be used in conjunction with
+ LADSPA_HINT_TOGGLED. */
+#define LADSPA_HINT_DEFAULT_1 0x240
+
+/* This default hint indicates that the number 100 should be used. */
+#define LADSPA_HINT_DEFAULT_100 0x280
+
+/* This default hint indicates that the Hz frequency of `concert A'
+ should be used. This will be 440 unless the host uses an unusual
+ tuning convention, in which case it may be within a few Hz. */
+#define LADSPA_HINT_DEFAULT_440 0x2C0
+
+#define LADSPA_IS_HINT_BOUNDED_BELOW(x) ((x) & LADSPA_HINT_BOUNDED_BELOW)
+#define LADSPA_IS_HINT_BOUNDED_ABOVE(x) ((x) & LADSPA_HINT_BOUNDED_ABOVE)
+#define LADSPA_IS_HINT_TOGGLED(x) ((x) & LADSPA_HINT_TOGGLED)
+#define LADSPA_IS_HINT_SAMPLE_RATE(x) ((x) & LADSPA_HINT_SAMPLE_RATE)
+#define LADSPA_IS_HINT_LOGARITHMIC(x) ((x) & LADSPA_HINT_LOGARITHMIC)
+#define LADSPA_IS_HINT_INTEGER(x) ((x) & LADSPA_HINT_INTEGER)
+
+#define LADSPA_IS_HINT_HAS_DEFAULT(x) ((x) & LADSPA_HINT_DEFAULT_MASK)
+#define LADSPA_IS_HINT_DEFAULT_MINIMUM(x) (((x) & LADSPA_HINT_DEFAULT_MASK) \
+ == LADSPA_HINT_DEFAULT_MINIMUM)
+#define LADSPA_IS_HINT_DEFAULT_LOW(x) (((x) & LADSPA_HINT_DEFAULT_MASK) \
+ == LADSPA_HINT_DEFAULT_LOW)
+#define LADSPA_IS_HINT_DEFAULT_MIDDLE(x) (((x) & LADSPA_HINT_DEFAULT_MASK) \
+ == LADSPA_HINT_DEFAULT_MIDDLE)
+#define LADSPA_IS_HINT_DEFAULT_HIGH(x) (((x) & LADSPA_HINT_DEFAULT_MASK) \
+ == LADSPA_HINT_DEFAULT_HIGH)
+#define LADSPA_IS_HINT_DEFAULT_MAXIMUM(x) (((x) & LADSPA_HINT_DEFAULT_MASK) \
+ == LADSPA_HINT_DEFAULT_MAXIMUM)
+#define LADSPA_IS_HINT_DEFAULT_0(x) (((x) & LADSPA_HINT_DEFAULT_MASK) \
+ == LADSPA_HINT_DEFAULT_0)
+#define LADSPA_IS_HINT_DEFAULT_1(x) (((x) & LADSPA_HINT_DEFAULT_MASK) \
+ == LADSPA_HINT_DEFAULT_1)
+#define LADSPA_IS_HINT_DEFAULT_100(x) (((x) & LADSPA_HINT_DEFAULT_MASK) \
+ == LADSPA_HINT_DEFAULT_100)
+#define LADSPA_IS_HINT_DEFAULT_440(x) (((x) & LADSPA_HINT_DEFAULT_MASK) \
+ == LADSPA_HINT_DEFAULT_440)
+
+typedef struct _LADSPA_PortRangeHint {
+
+ /* Hints about the port. */
+ LADSPA_PortRangeHintDescriptor HintDescriptor;
+
+ /* Meaningful when hint LADSPA_HINT_BOUNDED_BELOW is active. When
+ LADSPA_HINT_SAMPLE_RATE is also active then this value should be
+ multiplied by the relevant sample rate. */
+ LADSPA_Data LowerBound;
+
+ /* Meaningful when hint LADSPA_HINT_BOUNDED_ABOVE is active. When
+ LADSPA_HINT_SAMPLE_RATE is also active then this value should be
+ multiplied by the relevant sample rate. */
+ LADSPA_Data UpperBound;
+
+} LADSPA_PortRangeHint;
+
+/*****************************************************************************/
+
+/* Plugin Handles:
+
+ This plugin handle indicates a particular instance of the plugin
+ concerned. It is valid to compare this to NULL (0 for C++) but
+ otherwise the host should not attempt to interpret it. The plugin
+ may use it to reference internal instance data. */
+
+typedef void * LADSPA_Handle;
+
+/*****************************************************************************/
+
+/* Descriptor for a Type of Plugin:
+
+ This structure is used to describe a plugin type. It provides a
+ number of functions to examine the type, instantiate it, link it to
+ buffers and workspaces and to run it. */
+
+typedef struct _LADSPA_Descriptor {
+
+ /* This numeric identifier indicates the plugin type
+ uniquely. Plugin programmers may reserve ranges of IDs from a
+ central body to avoid clashes. Hosts may assume that IDs are
+ below 0x1000000. */
+ unsigned long UniqueID;
+
+ /* This identifier can be used as a unique, case-sensitive
+ identifier for the plugin type within the plugin file. Plugin
+ types should be identified by file and label rather than by index
+ or plugin name, which may be changed in new plugin
+ versions. Labels must not contain white-space characters. */
+ const char * Label;
+
+ /* This indicates a number of properties of the plugin. */
+ LADSPA_Properties Properties;
+
+ /* This member points to the null-terminated name of the plugin
+ (e.g. "Sine Oscillator"). */
+ const char * Name;
+
+ /* This member points to the null-terminated string indicating the
+ maker of the plugin. This can be an empty string but not NULL. */
+ const char * Maker;
+
+ /* This member points to the null-terminated string indicating any
+ copyright applying to the plugin. If no Copyright applies the
+ string "None" should be used. */
+ const char * Copyright;
+
+ /* This indicates the number of ports (input AND output) present on
+ the plugin. */
+ unsigned long PortCount;
+
+ /* This member indicates an array of port descriptors. Valid indices
+ vary from 0 to PortCount-1. */
+ const LADSPA_PortDescriptor * PortDescriptors;
+
+ /* This member indicates an array of null-terminated strings
+ describing ports (e.g. "Frequency (Hz)"). Valid indices vary from
+ 0 to PortCount-1. */
+ const char * const * PortNames;
+
+ /* This member indicates an array of range hints for each port (see
+ above). Valid indices vary from 0 to PortCount-1. */
+ const LADSPA_PortRangeHint * PortRangeHints;
+
+ /* This may be used by the plugin developer to pass any custom
+ implementation data into an instantiate call. It must not be used
+ or interpreted by the host. It is expected that most plugin
+ writers will not use this facility as LADSPA_Handle should be
+ used to hold instance data. */
+ void * ImplementationData;
+
+ /* This member is a function pointer that instantiates a plugin. A
+ handle is returned indicating the new plugin instance. The
+ instantiation function accepts a sample rate as a parameter. The
+ plugin descriptor from which this instantiate function was found
+ must also be passed. This function must return NULL if
+ instantiation fails.
+
+ Note that instance initialisation should generally occur in
+ activate() rather than here. */
+ LADSPA_Handle (*instantiate)(const struct _LADSPA_Descriptor * Descriptor,
+ unsigned long SampleRate);
+
+ /* This member is a function pointer that connects a port on an
+ instantiated plugin to a memory location at which a block of data
+ for the port will be read/written. The data location is expected
+ to be an array of LADSPA_Data for audio ports or a single
+ LADSPA_Data value for control ports. Memory issues will be
+ managed by the host. The plugin must read/write the data at these
+ locations every time run() or run_adding() is called and the data
+ present at the time of this connection call should not be
+ considered meaningful.
+
+ connect_port() may be called more than once for a plugin instance
+ to allow the host to change the buffers that the plugin is
+ reading or writing. These calls may be made before or after
+ activate() or deactivate() calls.
+
+ connect_port() must be called at least once for each port before
+ run() or run_adding() is called. When working with blocks of
+ LADSPA_Data the plugin should pay careful attention to the block
+ size passed to the run function as the block allocated may only
+ just be large enough to contain the block of samples.
+
+ Plugin writers should be aware that the host may elect to use the
+ same buffer for more than one port and even use the same buffer
+ for both input and output (see LADSPA_PROPERTY_INPLACE_BROKEN).
+ However, overlapped buffers or use of a single buffer for both
+ audio and control data may result in unexpected behaviour. */
+ void (*connect_port)(LADSPA_Handle Instance,
+ unsigned long Port,
+ LADSPA_Data * DataLocation);
+
+ /* This member is a function pointer that initialises a plugin
+ instance and activates it for use. This is separated from
+ instantiate() to aid real-time support and so that hosts can
+ reinitialise a plugin instance by calling deactivate() and then
+ activate(). In this case the plugin instance must reset all state
+ information dependent on the history of the plugin instance
+ except for any data locations provided by connect_port() and any
+ gain set by set_run_adding_gain(). If there is nothing for
+ activate() to do then the plugin writer may provide a NULL rather
+ than an empty function.
+
+ When present, hosts must call this function once before run() (or
+ run_adding()) is called for the first time. This call should be
+ made as close to the run() call as possible and indicates to
+ real-time plugins that they are now live. Plugins should not rely
+ on a prompt call to run() after activate(). activate() may not be
+ called again unless deactivate() is called first. Note that
+ connect_port() may be called before or after a call to
+ activate(). */
+ void (*activate)(LADSPA_Handle Instance);
+
+ /* This method is a function pointer that runs an instance of a
+ plugin for a block. Two parameters are required: the first is a
+ handle to the particular instance to be run and the second
+ indicates the block size (in samples) for which the plugin
+ instance may run.
+
+ Note that if an activate() function exists then it must be called
+ before run() or run_adding(). If deactivate() is called for a
+ plugin instance then the plugin instance may not be reused until
+ activate() has been called again.
+
+ If the plugin has the property LADSPA_PROPERTY_HARD_RT_CAPABLE
+ then there are various things that the plugin should not do
+ within the run() or run_adding() functions (see above). */
+ void (*run)(LADSPA_Handle Instance,
+ unsigned long SampleCount);
+
+ /* This method is a function pointer that runs an instance of a
+ plugin for a block. This has identical behaviour to run() except
+ in the way data is output from the plugin. When run() is used,
+ values are written directly to the memory areas associated with
+ the output ports. However when run_adding() is called, values
+ must be added to the values already present in the memory
+ areas. Furthermore, output values written must be scaled by the
+ current gain set by set_run_adding_gain() (see below) before
+ addition.
+
+ run_adding() is optional. When it is not provided by a plugin,
+ this function pointer must be set to NULL. When it is provided,
+ the function set_run_adding_gain() must be provided also. */
+ void (*run_adding)(LADSPA_Handle Instance,
+ unsigned long SampleCount);
+
+ /* This method is a function pointer that sets the output gain for
+ use when run_adding() is called (see above). If this function is
+ never called the gain is assumed to default to 1. Gain
+ information should be retained when activate() or deactivate()
+ are called.
+
+ This function should be provided by the plugin if and only if the
+ run_adding() function is provided. When it is absent this
+ function pointer must be set to NULL. */
+ void (*set_run_adding_gain)(LADSPA_Handle Instance,
+ LADSPA_Data Gain);
+
+ /* This is the counterpart to activate() (see above). If there is
+ nothing for deactivate() to do then the plugin writer may provide
+ a NULL rather than an empty function.
+
+ Hosts must deactivate all activated units after they have been
+ run() (or run_adding()) for the last time. This call should be
+ made as close to the last run() call as possible and indicates to
+ real-time plugins that they are no longer live. Plugins should
+ not rely on prompt deactivation. Note that connect_port() may be
+ called before or after a call to deactivate().
+
+ Deactivation is not similar to pausing as the plugin instance
+ will be reinitialised when activate() is called to reuse it. */
+ void (*deactivate)(LADSPA_Handle Instance);
+
+ /* Once an instance of a plugin has been finished with it can be
+ deleted using the following function. The instance handle passed
+ ceases to be valid after this call.
+
+ If activate() was called for a plugin instance then a
+ corresponding call to deactivate() must be made before cleanup()
+ is called. */
+ void (*cleanup)(LADSPA_Handle Instance);
+
+} LADSPA_Descriptor;
+
+/**********************************************************************/
+
+/* Accessing a Plugin: */
+
+/* The exact mechanism by which plugins are loaded is host-dependent,
+ however all most hosts will need to know is the name of shared
+ object file containing the plugin types. To allow multiple hosts to
+ share plugin types, hosts may wish to check for environment
+ variable LADSPA_PATH. If present, this should contain a
+ colon-separated path indicating directories that should be searched
+ (in order) when loading plugin types.
+
+ A plugin programmer must include a function called
+ "ladspa_descriptor" with the following function prototype within
+ the shared object file. This function will have C-style linkage (if
+ you are using C++ this is taken care of by the `extern "C"' clause
+ at the top of the file).
+
+ A host will find the plugin shared object file by one means or
+ another, find the ladspa_descriptor() function, call it, and
+ proceed from there.
+
+ Plugin types are accessed by index (not ID) using values from 0
+ upwards. Out of range indexes must result in this function
+ returning NULL, so the plugin count can be determined by checking
+ for the least index that results in NULL being returned. */
+
+const LADSPA_Descriptor * ladspa_descriptor(unsigned long Index);
+
+/* Datatype corresponding to the ladspa_descriptor() function. */
+typedef const LADSPA_Descriptor *
+(*LADSPA_Descriptor_Function)(unsigned long Index);
+
+/**********************************************************************/
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LADSPA_INCLUDED */
+
+/* EOF */
diff --git a/src/modules/macosx/module-bonjour-publish.c b/src/modules/macosx/module-bonjour-publish.c
new file mode 100644
index 00000000..667b6b73
--- /dev/null
+++ b/src/modules/macosx/module-bonjour-publish.c
@@ -0,0 +1,513 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Daniel Mack
+ based on module-zeroconf-publish.c
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <dns_sd.h>
+
+#include <CoreFoundation/CoreFoundation.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/util.h>
+
+#include <pulsecore/parseaddr.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/source.h>
+#include <pulsecore/native-common.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/dynarray.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/protocol-native.h>
+
+#include "module-bonjour-publish-symdef.h"
+
+PA_MODULE_AUTHOR("Daniel Mack");
+PA_MODULE_DESCRIPTION("Mac OS X Bonjour Service Publisher");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+#define SERVICE_TYPE_SINK "_pulse-sink._tcp"
+#define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
+#define SERVICE_TYPE_SERVER "_pulse-server._tcp"
+
+static const char* const valid_modargs[] = {
+ NULL
+};
+
+enum service_subtype {
+ SUBTYPE_HARDWARE,
+ SUBTYPE_VIRTUAL,
+ SUBTYPE_MONITOR
+};
+
+struct service {
+ struct userdata *userdata;
+ DNSServiceRef service;
+ DNSRecordRef rec, rec2;
+ char *service_name;
+ pa_object *device;
+ enum service_subtype subtype;
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+
+ pa_hashmap *services;
+ char *service_name;
+
+ pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot;
+
+ pa_native_protocol *native;
+ DNSServiceRef main_service;
+};
+
+static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_channel_map *ret_map, const char **ret_name, pa_proplist **ret_proplist, enum service_subtype *ret_subtype) {
+ pa_assert(s);
+ pa_assert(ret_ss);
+ pa_assert(ret_proplist);
+ pa_assert(ret_subtype);
+
+ if (pa_sink_isinstance(s->device)) {
+ pa_sink *sink = PA_SINK(s->device);
+
+ *ret_ss = sink->sample_spec;
+ *ret_map = sink->channel_map;
+ *ret_name = sink->name;
+ *ret_proplist = sink->proplist;
+ *ret_subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
+
+ } else if (pa_source_isinstance(s->device)) {
+ pa_source *source = PA_SOURCE(s->device);
+
+ *ret_ss = source->sample_spec;
+ *ret_map = source->channel_map;
+ *ret_name = source->name;
+ *ret_proplist = source->proplist;
+ *ret_subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
+
+ } else
+ pa_assert_not_reached();
+}
+
+static void txt_record_server_data(pa_core *c, TXTRecordRef *txt) {
+ char s[128];
+ char *t;
+
+ pa_assert(c);
+
+ TXTRecordSetValue(txt, "server-version", strlen(PACKAGE_NAME" "PACKAGE_VERSION), PACKAGE_NAME" "PACKAGE_VERSION);
+
+ t = pa_get_user_name_malloc();
+ TXTRecordSetValue(txt, "user-name", strlen(t), t);
+ pa_xfree(t);
+
+ t = pa_machine_id();
+ TXTRecordSetValue(txt, "machine-id", strlen(t), t);
+ pa_xfree(t);
+
+ t = pa_uname_string();
+ TXTRecordSetValue(txt, "uname", strlen(t), t);
+ pa_xfree(t);
+
+ t = pa_get_fqdn(s, sizeof(s));
+ TXTRecordSetValue(txt, "fqdn", strlen(t), t);
+
+ snprintf(s, sizeof(s), "0x%08x", c->cookie);
+ TXTRecordSetValue(txt, "cookie", strlen(s), s);
+}
+
+static void service_free(struct service *s);
+
+static void dns_service_register_reply(DNSServiceRef sdRef,
+ DNSServiceFlags flags,
+ DNSServiceErrorType errorCode,
+ const char *name,
+ const char *regtype,
+ const char *domain,
+ void *context) {
+ struct service *s = context;
+
+ pa_assert(s);
+
+ switch (errorCode) {
+ case kDNSServiceErr_NameConflict:
+ pa_log("DNS service reported kDNSServiceErr_NameConflict\n");
+ service_free(s);
+ break;
+
+ case kDNSServiceErr_NoError:
+ default:
+ break;
+ }
+}
+
+static uint16_t compute_port(struct userdata *u) {
+ pa_strlist *i;
+
+ pa_assert(u);
+
+ for (i = pa_native_protocol_servers(u->native); i; i = pa_strlist_next(i)) {
+ pa_parsed_address a;
+
+ if (pa_parse_address(pa_strlist_data(i), &a) >= 0 &&
+ (a.type == PA_PARSED_ADDRESS_TCP4 ||
+ a.type == PA_PARSED_ADDRESS_TCP6 ||
+ a.type == PA_PARSED_ADDRESS_TCP_AUTO) &&
+ a.port > 0) {
+
+ pa_xfree(a.path_or_host);
+ return a.port;
+ }
+
+ pa_xfree(a.path_or_host);
+ }
+
+ return PA_NATIVE_DEFAULT_PORT;
+}
+
+static int publish_service(struct service *s) {
+ int r = -1;
+ TXTRecordRef txt;
+ DNSServiceErrorType err;
+ const char *name = NULL, *t;
+ pa_proplist *proplist = NULL;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ char cm[PA_CHANNEL_MAP_SNPRINT_MAX], tmp[64];
+ enum service_subtype subtype;
+
+ const char * const subtype_text[] = {
+ [SUBTYPE_HARDWARE] = "hardware",
+ [SUBTYPE_VIRTUAL] = "virtual",
+ [SUBTYPE_MONITOR] = "monitor"
+ };
+
+ pa_assert(s);
+
+ if (s->service) {
+ DNSServiceRefDeallocate(s->service);
+ s->service = NULL;
+ }
+
+ TXTRecordCreate(&txt, 0, NULL);
+
+ txt_record_server_data(s->userdata->core, &txt);
+
+ get_service_data(s, &ss, &map, &name, &proplist, &subtype);
+ TXTRecordSetValue(&txt, "device", strlen(name), name);
+
+ snprintf(tmp, sizeof(tmp), "%u", ss.rate);
+ TXTRecordSetValue(&txt, "rate", strlen(tmp), tmp);
+
+ snprintf(tmp, sizeof(tmp), "%u", ss.channels);
+ TXTRecordSetValue(&txt, "channels", strlen(tmp), tmp);
+
+ t = pa_sample_format_to_string(ss.format);
+ TXTRecordSetValue(&txt, "format", strlen(t), t);
+
+ t = pa_channel_map_snprint(cm, sizeof(cm), &map);
+ TXTRecordSetValue(&txt, "channel_map", strlen(t), t);
+
+ t = subtype_text[subtype];
+ TXTRecordSetValue(&txt, "subtype", strlen(t), t);
+
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ TXTRecordSetValue(&txt, "description", strlen(t), t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_ICON_NAME)))
+ TXTRecordSetValue(&txt, "icon-name", strlen(t), t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_VENDOR_NAME)))
+ TXTRecordSetValue(&txt, "vendor-name", strlen(t), t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_PRODUCT_NAME)))
+ TXTRecordSetValue(&txt, "product-name", strlen(t), t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_CLASS)))
+ TXTRecordSetValue(&txt, "class", strlen(t), t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_FORM_FACTOR)))
+ TXTRecordSetValue(&txt, "form-factor", strlen(t), t);
+
+ err = DNSServiceRegister(&s->service,
+ 0, /* flags */
+ kDNSServiceInterfaceIndexAny,
+ s->service_name,
+ pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
+ NULL, /* domain */
+ NULL, /* host */
+ compute_port(s->userdata),
+ TXTRecordGetLength(&txt),
+ TXTRecordGetBytesPtr(&txt),
+ dns_service_register_reply, s);
+
+ if (err != kDNSServiceErr_NoError) {
+ pa_log("DNSServiceRegister() returned err %d", err);
+ goto finish;
+ }
+
+ pa_log_debug("Successfully registered Bonjour services for >%s<.", s->service_name);
+ return 0;
+
+finish:
+
+ /* Remove this service */
+ if (r < 0)
+ service_free(s);
+
+ TXTRecordDeallocate(&txt);
+
+ return r;
+}
+
+static struct service *get_service(struct userdata *u, pa_object *device) {
+ struct service *s;
+ char *hn, *un;
+ const char *n;
+
+ pa_assert(u);
+ pa_object_assert_ref(device);
+
+ if ((s = pa_hashmap_get(u->services, device)))
+ return s;
+
+ s = pa_xnew0(struct service, 1);
+ s->userdata = u;
+ s->device = device;
+
+ if (pa_sink_isinstance(device)) {
+ if (!(n = pa_proplist_gets(PA_SINK(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ n = PA_SINK(device)->name;
+ } else {
+ if (!(n = pa_proplist_gets(PA_SOURCE(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ n = PA_SOURCE(device)->name;
+ }
+
+ hn = pa_get_host_name_malloc();
+ un = pa_get_user_name_malloc();
+
+ s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", un, hn, n), kDNSServiceMaxDomainName-1);
+
+ pa_xfree(un);
+ pa_xfree(hn);
+
+ pa_hashmap_put(u->services, s->device, s);
+
+ return s;
+}
+
+static void service_free(struct service *s) {
+ pa_assert(s);
+
+ pa_hashmap_remove(s->userdata->services, s->device);
+
+ if (s->service)
+ DNSServiceRefDeallocate(s->service);
+
+ pa_xfree(s->service_name);
+ pa_xfree(s);
+}
+
+static pa_bool_t shall_ignore(pa_object *o) {
+ pa_object_assert_ref(o);
+
+ if (pa_sink_isinstance(o))
+ return !!(PA_SINK(o)->flags & PA_SINK_NETWORK);
+
+ if (pa_source_isinstance(o))
+ return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK);
+
+ pa_assert_not_reached();
+}
+
+static pa_hook_result_t device_new_or_changed_cb(pa_core *c, pa_object *o, struct userdata *u) {
+ pa_assert(c);
+ pa_object_assert_ref(o);
+
+ if (!shall_ignore(o))
+ publish_service(get_service(u, o));
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
+ struct service *s;
+
+ pa_assert(c);
+ pa_object_assert_ref(o);
+
+ if ((s = pa_hashmap_get(u->services, o)))
+ service_free(s);
+
+ return PA_HOOK_OK;
+}
+
+static int publish_main_service(struct userdata *u) {
+ DNSServiceErrorType err;
+ TXTRecordRef txt;
+
+ pa_assert(u);
+
+ if (u->main_service) {
+ DNSServiceRefDeallocate(u->main_service);
+ u->main_service = NULL;
+ }
+
+ TXTRecordCreate(&txt, 0, NULL);
+ txt_record_server_data(u->core, &txt);
+
+ err = DNSServiceRegister(&u->main_service,
+ 0, /* flags */
+ kDNSServiceInterfaceIndexAny,
+ u->service_name,
+ SERVICE_TYPE_SERVER,
+ NULL, /* domain */
+ NULL, /* host */
+ compute_port(u),
+ TXTRecordGetLength(&txt),
+ TXTRecordGetBytesPtr(&txt),
+ NULL, NULL);
+
+ if (err != kDNSServiceErr_NoError) {
+ pa_log("%s(): DNSServiceRegister() returned err %d", __func__, err);
+ return err;
+ }
+
+ TXTRecordDeallocate(&txt);
+
+ return 0;
+}
+
+static int publish_all_services(struct userdata *u) {
+ pa_sink *sink;
+ pa_source *source;
+ uint32_t idx;
+
+ pa_assert(u);
+
+ pa_log_debug("Publishing services in Bonjour");
+
+ for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
+ if (!shall_ignore(PA_OBJECT(sink)))
+ publish_service(get_service(u, PA_OBJECT(sink)));
+
+ for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
+ if (!shall_ignore(PA_OBJECT(source)))
+ publish_service(get_service(u, PA_OBJECT(source)));
+
+ return publish_main_service(u);
+}
+
+static void unpublish_all_services(struct userdata *u) {
+ void *state = NULL;
+ struct service *s;
+
+ pa_assert(u);
+
+ pa_log_debug("Unpublishing services in Bonjour");
+
+ while ((s = pa_hashmap_iterate(u->services, &state, NULL)))
+ service_free(s);
+
+ if (u->main_service)
+ DNSServiceRefDeallocate(u->main_service);
+}
+
+int pa__init(pa_module*m) {
+
+ struct userdata *u;
+ pa_modargs *ma = NULL;
+ char *hn, *un;
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->native = pa_native_protocol_get(u->core);
+
+ u->services = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->sink_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
+ u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->source_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
+
+ un = pa_get_user_name_malloc();
+ hn = pa_get_host_name_malloc();
+ u->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s", un, hn), kDNSServiceMaxDomainName-1);
+ pa_xfree(un);
+ pa_xfree(hn);
+
+ publish_all_services(u);
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata*u;
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ unpublish_all_services(u);
+
+ if (u->services)
+ pa_hashmap_free(u->services, NULL, NULL);
+
+ if (u->sink_new_slot)
+ pa_hook_slot_free(u->sink_new_slot);
+ if (u->source_new_slot)
+ pa_hook_slot_free(u->source_new_slot);
+ if (u->sink_changed_slot)
+ pa_hook_slot_free(u->sink_changed_slot);
+ if (u->source_changed_slot)
+ pa_hook_slot_free(u->source_changed_slot);
+ if (u->sink_unlink_slot)
+ pa_hook_slot_free(u->sink_unlink_slot);
+ if (u->source_unlink_slot)
+ pa_hook_slot_free(u->source_unlink_slot);
+
+ if (u->native)
+ pa_native_protocol_unref(u->native);
+
+ pa_xfree(u->service_name);
+ pa_xfree(u);
+}
diff --git a/src/modules/macosx/module-coreaudio-detect.c b/src/modules/macosx/module-coreaudio-detect.c
new file mode 100644
index 00000000..f4f2ee28
--- /dev/null
+++ b/src/modules/macosx/module-coreaudio-detect.c
@@ -0,0 +1,285 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009,2010 Daniel Mack <daniel@caiaq.de>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/llist.h>
+
+#include <CoreAudio/CoreAudio.h>
+
+#include "module-coreaudio-detect-symdef.h"
+
+#define DEVICE_MODULE_NAME "module-coreaudio-device"
+
+PA_MODULE_AUTHOR("Daniel Mack");
+PA_MODULE_DESCRIPTION("CoreAudio device detection");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE("ioproc_frames=<passed on to module-coreaudio-device> ");
+
+static const char* const valid_modargs[] = {
+ "ioproc_frames",
+ NULL
+};
+
+typedef struct ca_device ca_device;
+
+struct ca_device {
+ AudioObjectID id;
+ unsigned int module_index;
+ PA_LLIST_FIELDS(ca_device);
+};
+
+struct userdata {
+ int detect_fds[2];
+ pa_io_event *detect_io;
+ unsigned int ioproc_frames;
+ PA_LLIST_HEAD(ca_device, devices);
+};
+
+static int ca_device_added(struct pa_module *m, AudioObjectID id) {
+ AudioObjectPropertyAddress property_address;
+ OSStatus err;
+ pa_module *mod;
+ struct userdata *u;
+ struct ca_device *dev;
+ char *args, tmp[64];
+ UInt32 size;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ /* To prevent generating a black hole that will suck us in,
+ don't create sources/sinks for PulseAudio virtual devices */
+
+ property_address.mSelector = kAudioDevicePropertyDeviceManufacturer;
+ property_address.mScope = kAudioObjectPropertyScopeGlobal;
+ property_address.mElement = kAudioObjectPropertyElementMaster;
+
+ size = sizeof(tmp);
+ err = AudioObjectGetPropertyData(id, &property_address, 0, NULL, &size, tmp);
+
+ if (!err && strcmp(tmp, "pulseaudio.org") == 0)
+ return 0;
+
+ if (u->ioproc_frames)
+ args = pa_sprintf_malloc("object_id=%d ioproc_frames=%d", (int) id, u->ioproc_frames);
+ else
+ args = pa_sprintf_malloc("object_id=%d", (int) id);
+
+ pa_log_debug("Loading %s with arguments '%s'", DEVICE_MODULE_NAME, args);
+ mod = pa_module_load(m->core, DEVICE_MODULE_NAME, args);
+ pa_xfree(args);
+
+ if (!mod) {
+ pa_log_info("Failed to load module %s with arguments '%s'", DEVICE_MODULE_NAME, args);
+ return -1;
+ }
+
+ dev = pa_xnew0(ca_device, 1);
+ dev->module_index = mod->index;
+ dev->id = id;
+
+ PA_LLIST_INIT(ca_device, dev);
+ PA_LLIST_PREPEND(ca_device, u->devices, dev);
+
+ return 0;
+}
+
+static int ca_update_device_list(struct pa_module *m) {
+ AudioObjectPropertyAddress property_address;
+ OSStatus err;
+ UInt32 i, size, num_devices;
+ AudioDeviceID *device_id;
+ struct ca_device *dev;
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ property_address.mSelector = kAudioHardwarePropertyDevices;
+ property_address.mScope = kAudioObjectPropertyScopeGlobal;
+ property_address.mElement = kAudioObjectPropertyElementMaster;
+
+ /* get the number of currently available audio devices */
+ err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &property_address, 0, NULL, &size);
+ if (err) {
+ pa_log("Unable to get data size for kAudioHardwarePropertyDevices.");
+ return -1;
+ }
+
+ num_devices = size / sizeof(AudioDeviceID);
+ device_id = pa_xnew(AudioDeviceID, num_devices);
+
+ err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property_address, 0, NULL, &size, device_id);
+ if (err) {
+ pa_log("Unable to get kAudioHardwarePropertyDevices.");
+ pa_xfree(device_id);
+ return -1;
+ }
+
+ /* scan for devices which are reported but not in our cached list */
+ for (i = 0; i < num_devices; i++) {
+ bool found = FALSE;
+
+ PA_LLIST_FOREACH(dev, u->devices)
+ if (dev->id == device_id[i]) {
+ found = TRUE;
+ break;
+ }
+
+ if (!found)
+ ca_device_added(m, device_id[i]);
+ }
+
+ /* scan for devices which are in our cached list but are not reported */
+scan_removed:
+
+ PA_LLIST_FOREACH(dev, u->devices) {
+ bool found = FALSE;
+
+ for (i = 0; i < num_devices; i++)
+ if (dev->id == device_id[i]) {
+ found = TRUE;
+ break;
+ }
+
+ if (!found) {
+ pa_log_debug("object id %d has been removed (module index %d) %p", (unsigned int) dev->id, dev->module_index, dev);
+ pa_module_unload_request_by_index(m->core, dev->module_index, TRUE);
+ PA_LLIST_REMOVE(ca_device, u->devices, dev);
+ pa_xfree(dev);
+ /* the current list item pointer is not valid anymore, so start over. */
+ goto scan_removed;
+ }
+ }
+
+ pa_xfree(device_id);
+ return 0;
+}
+
+static OSStatus property_listener_proc(AudioObjectID objectID, UInt32 numberAddresses,
+ const AudioObjectPropertyAddress inAddresses[],
+ void *clientData)
+{
+ struct userdata *u = clientData;
+ char dummy = 1;
+
+ pa_assert(u);
+
+ /* dispatch module load/unload operations in main thread */
+ write(u->detect_fds[1], &dummy, 1);
+
+ return 0;
+}
+
+static void detect_handle(pa_mainloop_api *a, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata) {
+ pa_module *m = userdata;
+ char dummy;
+
+ pa_assert(m);
+
+ read(fd, &dummy, 1);
+ ca_update_device_list(m);
+}
+
+int pa__init(pa_module *m) {
+ struct userdata *u = pa_xnew0(struct userdata, 1);
+ AudioObjectPropertyAddress property_address;
+ pa_modargs *ma;
+
+ pa_assert(m);
+
+ m->userdata = u;
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ pa_modargs_get_value_u32(ma, "ioproc_frames", &u->ioproc_frames);
+
+ property_address.mSelector = kAudioHardwarePropertyDevices;
+ property_address.mScope = kAudioObjectPropertyScopeGlobal;
+ property_address.mElement = kAudioObjectPropertyElementMaster;
+
+ if (AudioObjectAddPropertyListener(kAudioObjectSystemObject, &property_address, property_listener_proc, u)) {
+ pa_log("AudioObjectAddPropertyListener() failed.");
+ goto fail;
+ }
+
+ if (ca_update_device_list(m))
+ goto fail;
+
+ pa_assert_se(pipe(u->detect_fds) == 0);
+ pa_assert_se(u->detect_io = m->core->mainloop->io_new(m->core->mainloop, u->detect_fds[0], PA_IO_EVENT_INPUT, detect_handle, m));
+
+ return 0;
+
+fail:
+ pa_xfree(u);
+ return -1;
+}
+
+void pa__done(pa_module *m) {
+ struct userdata *u;
+ struct ca_device *dev;
+ AudioObjectPropertyAddress property_address;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ dev = u->devices;
+
+ property_address.mSelector = kAudioHardwarePropertyDevices;
+ property_address.mScope = kAudioObjectPropertyScopeGlobal;
+ property_address.mElement = kAudioObjectPropertyElementMaster;
+
+ AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &property_address, property_listener_proc, u);
+
+ while (dev) {
+ struct ca_device *next = dev->next;
+
+ pa_module_unload_request_by_index(m->core, dev->module_index, TRUE);
+ pa_xfree(dev);
+
+ dev = next;
+ }
+
+ if (u->detect_fds[0] >= 0)
+ close(u->detect_fds[0]);
+
+ if (u->detect_fds[1] >= 0)
+ close(u->detect_fds[1]);
+
+ if (u->detect_io)
+ m->core->mainloop->io_free(u->detect_io);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/macosx/module-coreaudio-device.c b/src/modules/macosx/module-coreaudio-device.c
new file mode 100644
index 00000000..d2762819
--- /dev/null
+++ b/src/modules/macosx/module-coreaudio-device.c
@@ -0,0 +1,876 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009,2010 Daniel Mack <daniel@caiaq.de>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* TODO:
+ - implement hardware volume controls
+ - handle audio device stream format changes (will require changes to the core)
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/sink.h>
+#include <pulsecore/source.h>
+#include <pulsecore/module.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/llist.h>
+#include <pulsecore/card.h>
+#include <pulsecore/strbuf.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+
+#include <CoreAudio/CoreAudio.h>
+#include <CoreAudio/CoreAudioTypes.h>
+#include <CoreAudio/AudioHardware.h>
+
+#include "module-coreaudio-device-symdef.h"
+
+#define DEFAULT_FRAMES_PER_IOPROC 512
+
+PA_MODULE_AUTHOR("Daniel Mack");
+PA_MODULE_DESCRIPTION("CoreAudio device");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE("object_id=<the CoreAudio device id> "
+ "ioproc_frames=<audio frames per IOProc call> ");
+
+static const char* const valid_modargs[] = {
+ "object_id",
+ "ioproc_frames",
+ NULL
+};
+
+enum {
+ CA_MESSAGE_RENDER = PA_SINK_MESSAGE_MAX,
+};
+
+typedef struct coreaudio_sink coreaudio_sink;
+typedef struct coreaudio_source coreaudio_source;
+
+struct userdata {
+ AudioObjectID object_id;
+ AudioDeviceIOProcID proc_id;
+
+ pa_thread_mq thread_mq;
+ pa_asyncmsgq *async_msgq;
+
+ pa_rtpoll *rtpoll;
+ pa_thread *thread;
+
+ pa_module *module;
+ pa_card *card;
+ pa_bool_t running;
+
+ char *device_name, *vendor_name;
+
+ const AudioBufferList *render_input_data;
+ AudioBufferList *render_output_data;
+
+ AudioStreamBasicDescription stream_description;
+
+ PA_LLIST_HEAD(coreaudio_sink, sinks);
+ PA_LLIST_HEAD(coreaudio_source, sources);
+};
+
+struct coreaudio_sink {
+ pa_sink *pa_sink;
+ struct userdata *userdata;
+
+ char *name;
+ unsigned int channel_idx;
+ pa_bool_t active;
+
+ pa_channel_map map;
+ pa_sample_spec ss;
+
+ PA_LLIST_FIELDS(coreaudio_sink);
+};
+
+struct coreaudio_source {
+ pa_source *pa_source;
+ struct userdata *userdata;
+
+ char *name;
+ unsigned int channel_idx;
+ pa_bool_t active;
+
+ pa_channel_map map;
+ pa_sample_spec ss;
+
+ PA_LLIST_FIELDS(coreaudio_source);
+};
+
+static OSStatus io_render_proc (AudioDeviceID device,
+ const AudioTimeStamp *now,
+ const AudioBufferList *inputData,
+ const AudioTimeStamp *inputTime,
+ AudioBufferList *outputData,
+ const AudioTimeStamp *outputTime,
+ void *clientData)
+{
+ struct userdata *u = clientData;
+
+ pa_assert(u);
+ pa_assert(device == u->object_id);
+
+ u->render_input_data = inputData;
+ u->render_output_data = outputData;
+
+ if (u->sinks)
+ pa_assert_se(pa_asyncmsgq_send(u->async_msgq, PA_MSGOBJECT(u->sinks->pa_sink),
+ CA_MESSAGE_RENDER, NULL, 0, NULL) == 0);
+
+ if (u->sources)
+ pa_assert_se(pa_asyncmsgq_send(u->async_msgq, PA_MSGOBJECT(u->sources->pa_source),
+ CA_MESSAGE_RENDER, NULL, 0, NULL) == 0);
+
+ return 0;
+}
+
+static OSStatus ca_stream_format_changed(AudioObjectID objectID,
+ UInt32 numberAddresses,
+ const AudioObjectPropertyAddress addresses[],
+ void *clientData)
+{
+ struct userdata *u = clientData;
+ UInt32 i;
+
+ pa_assert(u);
+
+ /* REVISIT: PA can't currently handle external format change requests.
+ * Hence, we set the original format back in this callback to avoid horrible audio artefacts.
+ * The device settings will appear to be 'locked' for any application as long as the PA daemon is running.
+ * Once we're able to propagate such events up in the core, this needs to be changed. */
+
+ for (i = 0; i < numberAddresses; i++)
+ AudioObjectSetPropertyData(objectID, addresses + i, 0, NULL, sizeof(u->stream_description), &u->stream_description);
+
+ return 0;
+}
+
+static pa_usec_t get_latency_us(pa_object *o) {
+ struct userdata *u;
+ pa_sample_spec *ss;
+ bool is_source;
+ UInt32 v, total = 0;
+ UInt32 err, size = sizeof(v);
+ AudioObjectPropertyAddress property_address;
+ AudioObjectID stream_id;
+
+ if (pa_sink_isinstance(o)) {
+ coreaudio_sink *sink = PA_SINK(o)->userdata;
+
+ u = sink->userdata;
+ ss = &sink->ss;
+ is_source = FALSE;
+ } else if (pa_source_isinstance(o)) {
+ coreaudio_source *source = PA_SOURCE(o)->userdata;
+
+ u = source->userdata;
+ ss = &source->ss;
+ is_source = TRUE;
+ } else
+ pa_assert_not_reached();
+
+ pa_assert(u);
+
+ property_address.mScope = kAudioObjectPropertyScopeGlobal;
+ property_address.mElement = kAudioObjectPropertyElementMaster;
+
+ /* get the device latency */
+ property_address.mSelector = kAudioDevicePropertyLatency;
+ size = sizeof(total);
+ AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &total);
+ total += v;
+
+ /* the the IOProc buffer size */
+ property_address.mSelector = kAudioDevicePropertyBufferFrameSize;
+ size = sizeof(v);
+ AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &v);
+ total += v;
+
+ /* IOProc safety offset - this value is the same for both directions, hence we divide it by 2 */
+ property_address.mSelector = kAudioDevicePropertySafetyOffset;
+ size = sizeof(v);
+ AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &v);
+ total += v / 2;
+
+ /* get the stream latency.
+ * FIXME: this assumes the stream latency is the same for all streams */
+ property_address.mSelector = kAudioDevicePropertyStreams;
+ size = sizeof(stream_id);
+ err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &stream_id);
+ if (!err) {
+ property_address.mSelector = kAudioStreamPropertyLatency;
+ size = sizeof(v);
+ err = AudioObjectGetPropertyData(stream_id, &property_address, 0, NULL, &size, &v);
+ if (!err)
+ total += v;
+ }
+
+ return pa_bytes_to_usec(total * pa_frame_size(ss), ss);
+}
+
+static void ca_device_check_device_state(struct userdata *u) {
+ coreaudio_sink *ca_sink;
+ coreaudio_source *ca_source;
+ pa_bool_t active = FALSE;
+
+ pa_assert(u);
+
+ for (ca_sink = u->sinks; ca_sink; ca_sink = ca_sink->next)
+ if (ca_sink->active)
+ active = TRUE;
+
+ for (ca_source = u->sources; ca_source; ca_source = ca_source->next)
+ if (ca_source->active)
+ active = TRUE;
+
+ if (active && !u->running)
+ AudioDeviceStart(u->object_id, u->proc_id);
+ else if (!active && u->running)
+ AudioDeviceStop(u->object_id, u->proc_id);
+
+ u->running = active;
+}
+
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ coreaudio_sink *sink = PA_SINK(o)->userdata;
+ struct userdata *u = sink->userdata;
+ unsigned int i;
+ pa_memchunk audio_chunk;
+
+ switch (code) {
+ case CA_MESSAGE_RENDER: {
+ /* audio out */
+ for (i = 0; i < u->render_output_data->mNumberBuffers; i++) {
+ AudioBuffer *buf = u->render_output_data->mBuffers + i;
+
+ pa_assert(sink);
+
+ if (PA_SINK_IS_OPENED(sink->pa_sink->thread_info.state)) {
+ if (sink->pa_sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(sink->pa_sink, 0);
+
+ audio_chunk.memblock = pa_memblock_new_fixed(u->module->core->mempool, buf->mData, buf->mDataByteSize, FALSE);
+ audio_chunk.length = buf->mDataByteSize;
+ audio_chunk.index = 0;
+
+ pa_sink_render_into_full(sink->pa_sink, &audio_chunk);
+ pa_memblock_unref_fixed(audio_chunk.memblock);
+ }
+
+ sink = sink->next;
+ }
+
+ return 0;
+ }
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ *((pa_usec_t *) data) = get_latency_us(PA_OBJECT(o));
+ return 0;
+ }
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ coreaudio_source *source = PA_SOURCE(o)->userdata;
+ struct userdata *u = source->userdata;
+ unsigned int i;
+ pa_memchunk audio_chunk;
+
+ switch (code) {
+ case CA_MESSAGE_RENDER: {
+ /* audio in */
+ for (i = 0; i < u->render_input_data->mNumberBuffers; i++) {
+ const AudioBuffer *buf = u->render_input_data->mBuffers + i;
+
+ pa_assert(source);
+
+ if (PA_SOURCE_IS_OPENED(source->pa_source->thread_info.state)) {
+ audio_chunk.memblock = pa_memblock_new_fixed(u->module->core->mempool, buf->mData, buf->mDataByteSize, TRUE);
+ audio_chunk.length = buf->mDataByteSize;
+ audio_chunk.index = 0;
+
+ pa_source_post(source->pa_source, &audio_chunk);
+ pa_memblock_unref_fixed(audio_chunk.memblock);
+ }
+
+ source = source->next;
+ }
+
+ return 0;
+ }
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ *((pa_usec_t *) data) = get_latency_us(PA_OBJECT(o));
+ return 0;
+ }
+ }
+
+ return pa_source_process_msg(o, code, data, offset, chunk);;
+}
+
+static int ca_sink_set_state(pa_sink *s, pa_sink_state_t state)
+{
+ coreaudio_sink *sink = s->userdata;
+
+ switch (state) {
+ case PA_SINK_SUSPENDED:
+ case PA_SINK_IDLE:
+ sink->active = FALSE;
+ break;
+
+ case PA_SINK_RUNNING:
+ sink->active = TRUE;
+ break;
+
+ case PA_SINK_UNLINKED:
+ case PA_SINK_INIT:
+ case PA_SINK_INVALID_STATE:
+ ;
+ }
+
+ ca_device_check_device_state(sink->userdata);
+
+ return 0;
+}
+
+static int ca_device_create_sink(pa_module *m, AudioBuffer *buf, int channel_idx) {
+ OSStatus err;
+ UInt32 size;
+ struct userdata *u = m->userdata;
+ pa_sink_new_data new_data;
+ pa_sink_flags_t flags = PA_SINK_LATENCY | PA_SINK_HARDWARE;
+ coreaudio_sink *ca_sink;
+ pa_sink *sink;
+ unsigned int i;
+ char tmp[255];
+ pa_strbuf *strbuf;
+ AudioObjectPropertyAddress property_address;
+
+ ca_sink = pa_xnew0(coreaudio_sink, 1);
+ ca_sink->map.channels = buf->mNumberChannels;
+ ca_sink->ss.channels = buf->mNumberChannels;
+ ca_sink->channel_idx = channel_idx;
+
+ /* build a name for this stream */
+ strbuf = pa_strbuf_new();
+
+ for (i = 0; i < buf->mNumberChannels; i++) {
+ property_address.mSelector = kAudioObjectPropertyElementName;
+ property_address.mScope = kAudioDevicePropertyScopeOutput;
+ property_address.mElement = channel_idx + i + 1;
+ size = sizeof(tmp);
+ err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, tmp);
+ if (err || !strlen(tmp))
+ snprintf(tmp, sizeof(tmp), "Channel %d", (int) property_address.mElement);
+
+ if (i > 0)
+ pa_strbuf_puts(strbuf, ", ");
+
+ pa_strbuf_puts(strbuf, tmp);
+ }
+
+ ca_sink->name = pa_strbuf_tostring_free(strbuf);
+
+ pa_log_debug("Stream name is >%s<", ca_sink->name);
+
+ /* default to mono streams */
+ for (i = 0; i < ca_sink->map.channels; i++)
+ ca_sink->map.map[i] = PA_CHANNEL_POSITION_MONO;
+
+ if (buf->mNumberChannels == 2) {
+ ca_sink->map.map[0] = PA_CHANNEL_POSITION_LEFT;
+ ca_sink->map.map[1] = PA_CHANNEL_POSITION_RIGHT;
+ }
+
+ ca_sink->ss.rate = u->stream_description.mSampleRate;
+ ca_sink->ss.format = PA_SAMPLE_FLOAT32LE;
+
+ pa_sink_new_data_init(&new_data);
+ new_data.card = u->card;
+ new_data.driver = __FILE__;
+ new_data.module = u->module;
+ new_data.namereg_fail = FALSE;
+ pa_sink_new_data_set_name(&new_data, ca_sink->name);
+ pa_sink_new_data_set_channel_map(&new_data, &ca_sink->map);
+ pa_sink_new_data_set_sample_spec(&new_data, &ca_sink->ss);
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_PRODUCT_NAME, u->device_name);
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, u->device_name);
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, "mmap");
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_CLASS, "sound");
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_API, "CoreAudio");
+ pa_proplist_setf(new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) buf->mDataByteSize);
+
+ if (u->vendor_name)
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_VENDOR_NAME, u->vendor_name);
+
+ sink = pa_sink_new(m->core, &new_data, flags);
+ pa_sink_new_data_done(&new_data);
+
+ if (!sink) {
+ pa_log("unable to create sink.");
+ return -1;
+ }
+
+ sink->parent.process_msg = sink_process_msg;
+ sink->userdata = ca_sink;
+ sink->set_state = ca_sink_set_state;
+
+ pa_sink_set_asyncmsgq(sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(sink, u->rtpoll);
+
+ ca_sink->pa_sink = sink;
+ ca_sink->userdata = u;
+
+ PA_LLIST_PREPEND(coreaudio_sink, u->sinks, ca_sink);
+
+ return 0;
+}
+
+static int ca_source_set_state(pa_source *s, pa_source_state_t state)
+{
+ coreaudio_source *source = s->userdata;
+
+ switch (state) {
+ case PA_SOURCE_SUSPENDED:
+ case PA_SOURCE_IDLE:
+ source->active = FALSE;
+ break;
+
+ case PA_SOURCE_RUNNING:
+ source->active = TRUE;
+ break;
+
+ case PA_SOURCE_UNLINKED:
+ case PA_SOURCE_INIT:
+ case PA_SOURCE_INVALID_STATE:
+ ;
+ }
+
+ ca_device_check_device_state(source->userdata);
+
+ return 0;
+}
+
+static int ca_device_create_source(pa_module *m, AudioBuffer *buf, int channel_idx) {
+ OSStatus err;
+ UInt32 size;
+ struct userdata *u = m->userdata;
+ pa_source_new_data new_data;
+ pa_source_flags_t flags = PA_SOURCE_LATENCY | PA_SOURCE_HARDWARE;
+ coreaudio_source *ca_source;
+ pa_source *source;
+ unsigned int i;
+ char tmp[255];
+ pa_strbuf *strbuf;
+ AudioObjectPropertyAddress property_address;
+
+ ca_source = pa_xnew0(coreaudio_source, 1);
+ ca_source->map.channels = buf->mNumberChannels;
+ ca_source->ss.channels = buf->mNumberChannels;
+ ca_source->channel_idx = channel_idx;
+
+ /* build a name for this stream */
+ strbuf = pa_strbuf_new();
+
+ for (i = 0; i < buf->mNumberChannels; i++) {
+ property_address.mSelector = kAudioObjectPropertyElementName;
+ property_address.mScope = kAudioDevicePropertyScopeInput;
+ property_address.mElement = channel_idx + i + 1;
+ size = sizeof(tmp);
+ err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, tmp);
+ if (err || !strlen(tmp))
+ snprintf(tmp, sizeof(tmp), "Channel %d", (int) property_address.mElement);
+
+ if (i > 0)
+ pa_strbuf_puts(strbuf, ", ");
+
+ pa_strbuf_puts(strbuf, tmp);
+ }
+
+ ca_source->name = pa_strbuf_tostring_free(strbuf);
+
+ pa_log_debug("Stream name is >%s<", ca_source->name);
+
+ /* default to mono streams */
+ for (i = 0; i < ca_source->map.channels; i++)
+ ca_source->map.map[i] = PA_CHANNEL_POSITION_MONO;
+
+ if (buf->mNumberChannels == 2) {
+ ca_source->map.map[0] = PA_CHANNEL_POSITION_LEFT;
+ ca_source->map.map[1] = PA_CHANNEL_POSITION_RIGHT;
+ }
+
+ ca_source->ss.rate = u->stream_description.mSampleRate;
+ ca_source->ss.format = PA_SAMPLE_FLOAT32LE;
+
+ pa_source_new_data_init(&new_data);
+ new_data.card = u->card;
+ new_data.driver = __FILE__;
+ new_data.module = u->module;
+ new_data.namereg_fail = FALSE;
+ pa_source_new_data_set_name(&new_data, ca_source->name);
+ pa_source_new_data_set_channel_map(&new_data, &ca_source->map);
+ pa_source_new_data_set_sample_spec(&new_data, &ca_source->ss);
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_PRODUCT_NAME, u->device_name);
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, u->device_name);
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, "mmap");
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_CLASS, "sound");
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_API, "CoreAudio");
+ pa_proplist_setf(new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) buf->mDataByteSize);
+
+ if (u->vendor_name)
+ pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_VENDOR_NAME, u->vendor_name);
+
+ source = pa_source_new(m->core, &new_data, flags);
+ pa_source_new_data_done(&new_data);
+
+ if (!source) {
+ pa_log("unable to create source.");
+ return -1;
+ }
+
+ source->parent.process_msg = source_process_msg;
+ source->userdata = ca_source;
+ source->set_state = ca_source_set_state;
+
+ pa_source_set_asyncmsgq(source, u->thread_mq.inq);
+ pa_source_set_rtpoll(source, u->rtpoll);
+
+ ca_source->pa_source = source;
+ ca_source->userdata = u;
+
+ PA_LLIST_PREPEND(coreaudio_source, u->sources, ca_source);
+
+ return 0;
+}
+
+static int ca_device_create_streams(pa_module *m, bool direction_in) {
+ OSStatus err;
+ UInt32 size, i, channel_idx;
+ struct userdata *u = m->userdata;
+ AudioBufferList *buffer_list;
+ AudioObjectPropertyAddress property_address;
+
+ property_address.mScope = direction_in ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
+ property_address.mElement = kAudioObjectPropertyElementMaster;
+
+ /* get current stream format */
+ size = sizeof(AudioStreamBasicDescription);
+ property_address.mSelector = kAudioDevicePropertyStreamFormat;
+ err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &u->stream_description);
+ if (err) {
+ /* no appropriate streams found - silently bail. */
+ return -1;
+ }
+
+ if (u->stream_description.mFormatID != kAudioFormatLinearPCM) {
+ pa_log("Unsupported audio format '%c%c%c%c'",
+ (char) (u->stream_description.mFormatID >> 24),
+ (char) (u->stream_description.mFormatID >> 16) & 0xff,
+ (char) (u->stream_description.mFormatID >> 8) & 0xff,
+ (char) (u->stream_description.mFormatID & 0xff));
+ return -1;
+ }
+
+ /* get stream configuration */
+ size = 0;
+ property_address.mSelector = kAudioDevicePropertyStreamConfiguration;
+ err = AudioObjectGetPropertyDataSize(u->object_id, &property_address, 0, NULL, &size);
+ if (err) {
+ pa_log("Failed to get kAudioDevicePropertyStreamConfiguration (%s).", direction_in ? "input" : "output");
+ return -1;
+ }
+
+ if (!size)
+ return 0;
+
+ buffer_list = (AudioBufferList *) pa_xmalloc(size);
+ err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, buffer_list);
+
+ if (!err) {
+ pa_log_debug("Sample rate: %f", u->stream_description.mSampleRate);
+ pa_log_debug("%d bytes per packet", (unsigned int) u->stream_description.mBytesPerPacket);
+ pa_log_debug("%d frames per packet", (unsigned int) u->stream_description.mFramesPerPacket);
+ pa_log_debug("%d bytes per frame", (unsigned int) u->stream_description.mBytesPerFrame);
+ pa_log_debug("%d channels per frame", (unsigned int) u->stream_description.mChannelsPerFrame);
+ pa_log_debug("%d bits per channel", (unsigned int) u->stream_description.mBitsPerChannel);
+
+ for (channel_idx = 0, i = 0; i < buffer_list->mNumberBuffers; i++) {
+ AudioBuffer *buf = buffer_list->mBuffers + i;
+
+ if (direction_in)
+ ca_device_create_source(m, buf, channel_idx);
+ else
+ ca_device_create_sink(m, buf, channel_idx);
+
+ channel_idx += buf->mNumberChannels;
+ }
+ }
+
+ pa_xfree(buffer_list);
+ return 0;
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+ pa_assert(u->module);
+ pa_assert(u->module->core);
+
+ pa_log_debug("Thread starting up");
+
+ if (u->module->core->realtime_scheduling)
+ pa_make_realtime(u->module->core->realtime_priority);
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ for (;;) {
+ int ret;
+
+ ret = pa_rtpoll_run(u->rtpoll, TRUE);
+
+ if (ret < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->module->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+int pa__init(pa_module *m) {
+ OSStatus err;
+ UInt32 size, frames;
+ struct userdata *u = NULL;
+ pa_modargs *ma = NULL;
+ char tmp[64];
+ pa_card_new_data card_new_data;
+ coreaudio_sink *ca_sink;
+ coreaudio_source *ca_source;
+ AudioObjectPropertyAddress property_address;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ m->userdata = u;
+
+ if (pa_modargs_get_value_u32(ma, "object_id", (unsigned int *) &u->object_id) != 0) {
+ pa_log("Failed to parse object_id argument.");
+ goto fail;
+ }
+
+ property_address.mScope = kAudioObjectPropertyScopeGlobal;
+ property_address.mElement = kAudioObjectPropertyElementMaster;
+
+ /* get device product name */
+ property_address.mSelector = kAudioDevicePropertyDeviceName;
+ size = sizeof(tmp);
+ err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, tmp);
+ if (err) {
+ pa_log("Failed to get kAudioDevicePropertyDeviceName (err = %08x).", (int) err);
+ goto fail;
+ }
+
+ u->device_name = pa_xstrdup(tmp);
+
+ pa_card_new_data_init(&card_new_data);
+ pa_proplist_sets(card_new_data.proplist, PA_PROP_DEVICE_STRING, tmp);
+ card_new_data.driver = __FILE__;
+ pa_card_new_data_set_name(&card_new_data, tmp);
+ pa_log_info("Initializing module for CoreAudio device '%s' (id %d)", tmp, (unsigned int) u->object_id);
+
+ /* get device vendor name (may fail) */
+ property_address.mSelector = kAudioDevicePropertyDeviceManufacturer;
+ size = sizeof(tmp);
+ err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, tmp);
+ if (!err)
+ u->vendor_name = pa_xstrdup(tmp);
+
+ /* create the card object */
+ u->card = pa_card_new(m->core, &card_new_data);
+ if (!u->card) {
+ pa_log("Unable to create card.\n");
+ goto fail;
+ }
+
+ pa_card_new_data_done(&card_new_data);
+ u->card->userdata = u;
+
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+ u->async_msgq = pa_asyncmsgq_new(0);
+ pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->async_msgq);
+
+ PA_LLIST_HEAD_INIT(coreaudio_sink, u->sinks);
+
+ /* create sinks */
+ ca_device_create_streams(m, FALSE);
+
+ /* create sources */
+ ca_device_create_streams(m, TRUE);
+
+ /* create the message thread */
+ if (!(u->thread = pa_thread_new(u->device_name, thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ /* register notification callback for stream format changes */
+ property_address.mSelector = kAudioDevicePropertyStreamFormat;
+ property_address.mScope = kAudioObjectPropertyScopeGlobal;
+ property_address.mElement = kAudioObjectPropertyElementMaster;
+
+ AudioObjectAddPropertyListener(u->object_id, &property_address, ca_stream_format_changed, u);
+
+ /* set number of frames in IOProc */
+ frames = DEFAULT_FRAMES_PER_IOPROC;
+ pa_modargs_get_value_u32(ma, "ioproc_frames", (unsigned int *) &frames);
+
+ property_address.mSelector = kAudioDevicePropertyBufferFrameSize;
+ AudioObjectSetPropertyData(u->object_id, &property_address, 0, NULL, sizeof(frames), &frames);
+ pa_log_debug("%u frames per IOProc\n", (unsigned int) frames);
+
+ /* create one ioproc for both directions */
+ err = AudioDeviceCreateIOProcID(u->object_id, io_render_proc, u, &u->proc_id);
+ if (err) {
+ pa_log("AudioDeviceCreateIOProcID() failed (err = %08x\n).", (int) err);
+ goto fail;
+ }
+
+ for (ca_sink = u->sinks; ca_sink; ca_sink = ca_sink->next)
+ pa_sink_put(ca_sink->pa_sink);
+
+ for (ca_source = u->sources; ca_source; ca_source = ca_source->next)
+ pa_source_put(ca_source->pa_source);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (u)
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module *m) {
+ struct userdata *u;
+ coreaudio_sink *ca_sink;
+ coreaudio_source *ca_source;
+ AudioObjectPropertyAddress property_address;
+
+ pa_assert(m);
+
+ u = m->userdata;
+ pa_assert(u);
+
+ /* unlink sinks */
+ for (ca_sink = u->sinks; ca_sink; ca_sink = ca_sink->next)
+ if (ca_sink->pa_sink)
+ pa_sink_unlink(ca_sink->pa_sink);
+
+ /* unlink sources */
+ for (ca_source = u->sources; ca_source; ca_source = ca_source->next)
+ if (ca_source->pa_source)
+ pa_source_unlink(ca_source->pa_source);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ pa_thread_mq_done(&u->thread_mq);
+ pa_asyncmsgq_unref(u->async_msgq);
+ }
+
+ /* free sinks */
+ for (ca_sink = u->sinks; ca_sink;) {
+ coreaudio_sink *next = ca_sink->next;
+
+ if (ca_sink->pa_sink)
+ pa_sink_unref(ca_sink->pa_sink);
+
+ pa_xfree(ca_sink->name);
+ pa_xfree(ca_sink);
+ ca_sink = next;
+ }
+
+ /* free sources */
+ for (ca_source = u->sources; ca_source;) {
+ coreaudio_source *next = ca_source->next;
+
+ if (ca_source->pa_source)
+ pa_source_unref(ca_source->pa_source);
+
+ pa_xfree(ca_source->name);
+ pa_xfree(ca_source);
+ ca_source = next;
+ }
+
+ if (u->proc_id) {
+ AudioDeviceStop(u->object_id, u->proc_id);
+ AudioDeviceDestroyIOProcID(u->object_id, u->proc_id);
+ }
+
+ property_address.mSelector = kAudioDevicePropertyStreamFormat;
+ property_address.mScope = kAudioObjectPropertyScopeGlobal;
+ property_address.mElement = kAudioObjectPropertyElementMaster;
+
+ AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &property_address, ca_stream_format_changed, u);
+
+ pa_xfree(u->device_name);
+ pa_xfree(u->vendor_name);
+ pa_rtpoll_free(u->rtpoll);
+ pa_card_free(u->card);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-alsa-sink.c b/src/modules/module-alsa-sink.c
deleted file mode 100644
index d5abdc28..00000000
--- a/src/modules/module-alsa-sink.c
+++ /dev/null
@@ -1,503 +0,0 @@
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <assert.h>
-#include <stdio.h>
-
-#ifdef HAVE_SYS_POLL_H
-#include <sys/poll.h>
-#else
-#include "poll.h"
-#endif
-
-#include <asoundlib.h>
-
-#include <pulse/xmalloc.h>
-
-#include <pulsecore/core.h>
-#include <pulsecore/module.h>
-#include <pulsecore/memchunk.h>
-#include <pulsecore/sink.h>
-#include <pulsecore/modargs.h>
-#include <pulsecore/core-util.h>
-#include <pulsecore/sample-util.h>
-#include <pulsecore/log.h>
-
-#include "alsa-util.h"
-#include "module-alsa-sink-symdef.h"
-
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("ALSA Sink")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE(
- "sink_name=<name for the sink> "
- "device=<ALSA device> "
- "format=<sample format> "
- "channels=<number of channels> "
- "rate=<sample rate> "
- "fragments=<number of fragments> "
- "fragment_size=<fragment size> "
- "channel_map=<channel map>")
-
-struct userdata {
- snd_pcm_t *pcm_handle;
- snd_mixer_t *mixer_handle;
- snd_mixer_elem_t *mixer_elem;
- pa_sink *sink;
- struct pa_alsa_fdlist *pcm_fdl;
- struct pa_alsa_fdlist *mixer_fdl;
- long hw_volume_max, hw_volume_min;
-
- size_t frame_size, fragment_size;
- pa_memchunk memchunk, silence;
- pa_module *module;
-};
-
-static const char* const valid_modargs[] = {
- "device",
- "sink_name",
- "format",
- "channels",
- "rate",
- "fragments",
- "fragment_size",
- "channel_map",
- NULL
-};
-
-#define DEFAULT_SINK_NAME "alsa_output"
-#define DEFAULT_DEVICE "default"
-
-static void update_usage(struct userdata *u) {
- pa_module_set_used(u->module,
- (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
- (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0));
-}
-
-static void xrun_recovery(struct userdata *u) {
- assert(u);
-
- pa_log(__FILE__": *** ALSA-XRUN (playback) ***");
-
- if (snd_pcm_prepare(u->pcm_handle) < 0)
- pa_log(__FILE__": snd_pcm_prepare() failed");
-}
-
-static void do_write(struct userdata *u) {
- assert(u);
-
- update_usage(u);
-
- for (;;) {
- pa_memchunk *memchunk = NULL;
- snd_pcm_sframes_t frames;
-
- if (u->memchunk.memblock)
- memchunk = &u->memchunk;
- else {
- if (pa_sink_render(u->sink, u->fragment_size, &u->memchunk) < 0)
- memchunk = &u->silence;
- else
- memchunk = &u->memchunk;
- }
-
- assert(memchunk->memblock && memchunk->memblock->data && memchunk->length && memchunk->memblock->length && (memchunk->length % u->frame_size) == 0);
-
- if ((frames = snd_pcm_writei(u->pcm_handle, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length / u->frame_size)) < 0) {
- if (frames == -EAGAIN)
- return;
-
- if (frames == -EPIPE) {
- xrun_recovery(u);
- continue;
- }
-
- pa_log(__FILE__": snd_pcm_writei() failed: %s", snd_strerror(frames));
- return;
- }
-
- if (memchunk == &u->memchunk) {
- size_t l = frames * u->frame_size;
- memchunk->index += l;
- memchunk->length -= l;
-
- if (memchunk->length == 0) {
- pa_memblock_unref(memchunk->memblock);
- memchunk->memblock = NULL;
- memchunk->index = memchunk->length = 0;
- }
- }
-
- break;
- }
-}
-
-static void fdl_callback(void *userdata) {
- struct userdata *u = userdata;
- assert(u);
-
- if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
- xrun_recovery(u);
-
- do_write(u);
-}
-
-static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
- struct userdata *u = snd_mixer_elem_get_callback_private(elem);
-
- assert(u && u->mixer_handle);
-
- if (mask == SND_CTL_EVENT_MASK_REMOVE)
- return 0;
-
- if (mask & SND_CTL_EVENT_MASK_VALUE) {
- if (u->sink->get_hw_volume)
- u->sink->get_hw_volume(u->sink);
- if (u->sink->get_hw_mute)
- u->sink->get_hw_mute(u->sink);
- pa_subscription_post(u->sink->core,
- PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
- u->sink->index);
- }
-
- return 0;
-}
-
-static pa_usec_t sink_get_latency_cb(pa_sink *s) {
- pa_usec_t r = 0;
- struct userdata *u = s->userdata;
- snd_pcm_sframes_t frames;
- int err;
-
- assert(s && u && u->sink);
-
- if ((err = snd_pcm_delay(u->pcm_handle, &frames)) < 0) {
- pa_log(__FILE__": failed to get delay: %s", snd_strerror(err));
- s->get_latency = NULL;
- return 0;
- }
-
- if (frames < 0)
- frames = 0;
-
- r += pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
-
- if (u->memchunk.memblock)
- r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
-
- return r;
-}
-
-static int sink_get_hw_volume_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- long vol;
- int err;
- int i;
-
- assert(u && u->mixer_elem);
-
- for (i = 0;i < s->hw_volume.channels;i++) {
- assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
-
- err = snd_mixer_selem_get_playback_volume(u->mixer_elem, i, &vol);
- if (err < 0)
- goto fail;
- s->hw_volume.values[i] =
- (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
- }
-
- return 0;
-
-fail:
- pa_log_error(__FILE__": Unable to read volume: %s", snd_strerror(err));
- s->get_hw_volume = NULL;
- s->set_hw_volume = NULL;
- return -1;
-}
-
-static int sink_set_hw_volume_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- int err;
- int i;
- pa_volume_t vol;
-
- assert(u && u->mixer_elem);
-
- for (i = 0;i < s->hw_volume.channels;i++) {
- assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
-
- vol = s->hw_volume.values[i];
-
- if (vol > PA_VOLUME_NORM)
- vol = PA_VOLUME_NORM;
-
- vol = (vol * (u->hw_volume_max - u->hw_volume_min)) /
- PA_VOLUME_NORM + u->hw_volume_min;
-
- err = snd_mixer_selem_set_playback_volume(u->mixer_elem, i, vol);
- if (err < 0)
- goto fail;
- }
-
- return 0;
-
-fail:
- pa_log_error(__FILE__": Unable to set volume: %s", snd_strerror(err));
- s->get_hw_volume = NULL;
- s->set_hw_volume = NULL;
- return -1;
-}
-
-static int sink_get_hw_mute_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- int err, sw;
-
- assert(u && u->mixer_elem);
-
- err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw);
- if (err) {
- pa_log_error(__FILE__": Unable to get switch: %s", snd_strerror(err));
- s->get_hw_mute = NULL;
- s->set_hw_mute = NULL;
- return -1;
- }
-
- s->hw_muted = !sw;
-
- return 0;
-}
-
-static int sink_set_hw_mute_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- int err;
-
- assert(u && u->mixer_elem);
-
- err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->hw_muted);
- if (err) {
- pa_log_error(__FILE__": Unable to set switch: %s", snd_strerror(err));
- s->get_hw_mute = NULL;
- s->set_hw_mute = NULL;
- return -1;
- }
-
- return 0;
-}
-
-int pa__init(pa_core *c, pa_module*m) {
- pa_modargs *ma = NULL;
- int ret = -1;
- struct userdata *u = NULL;
- const char *dev;
- pa_sample_spec ss;
- pa_channel_map map;
- uint32_t periods, fragsize;
- snd_pcm_uframes_t period_size;
- size_t frame_size;
- snd_pcm_info_t *pcm_info = NULL;
- int err;
-
- if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
- goto fail;
- }
-
- ss = c->default_sample_spec;
- if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
- pa_log(__FILE__": failed to parse sample specification and channel map");
- goto fail;
- }
-
- frame_size = pa_frame_size(&ss);
-
- /* Fix latency to 100ms */
- periods = 8;
- fragsize = pa_bytes_per_second(&ss)/128;
-
- if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
- pa_log(__FILE__": failed to parse buffer metrics");
- goto fail;
- }
- period_size = fragsize/frame_size;
-
- u = pa_xnew0(struct userdata, 1);
- m->userdata = u;
- u->module = m;
-
- snd_config_update_free_global();
- if ((err = snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
- pa_log(__FILE__": Error opening PCM device %s: %s", dev, snd_strerror(err));
- goto fail;
- }
-
- if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
- (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
- pa_log(__FILE__": Error fetching PCM info: %s", snd_strerror(err));
- goto fail;
- }
-
- if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
- pa_log(__FILE__": Failed to set hardware parameters: %s", snd_strerror(err));
- goto fail;
- }
-
- if (ss.channels != map.channels)
- /* Seems ALSA didn't like the channel number, so let's fix the channel map */
- pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
-
- if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
- pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err));
- goto fail;
- }
-
- if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
- !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "PCM", "Master"))) {
- snd_mixer_close(u->mixer_handle);
- u->mixer_handle = NULL;
- }
-
- if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": Failed to create sink object");
- goto fail;
- }
-
- u->sink->is_hardware = 1;
- u->sink->get_latency = sink_get_latency_cb;
- if (u->mixer_handle) {
- assert(u->mixer_elem);
- if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
- int i;
-
- for (i = 0;i < ss.channels;i++) {
- if (!snd_mixer_selem_has_playback_channel(u->mixer_elem, i))
- break;
- }
-
- if (i == ss.channels) {
- u->sink->get_hw_volume = sink_get_hw_volume_cb;
- u->sink->set_hw_volume = sink_set_hw_volume_cb;
- snd_mixer_selem_get_playback_volume_range(
- u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
- }
- }
- if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
- u->sink->get_hw_mute = sink_get_hw_mute_cb;
- u->sink->set_hw_mute = sink_set_hw_mute_cb;
- }
- }
- u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev, snd_pcm_info_get_name(pcm_info));
-
- u->pcm_fdl = pa_alsa_fdlist_new();
- assert(u->pcm_fdl);
- if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
- pa_log(__FILE__": failed to initialise file descriptor monitoring");
- goto fail;
- }
-
- if (u->mixer_handle) {
- u->mixer_fdl = pa_alsa_fdlist_new();
- assert(u->mixer_fdl);
- if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
- pa_log(__FILE__": failed to initialise file descriptor monitoring");
- goto fail;
- }
- snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
- snd_mixer_elem_set_callback_private(u->mixer_elem, u);
- } else
- u->mixer_fdl = NULL;
-
- u->frame_size = frame_size;
- u->fragment_size = period_size * frame_size;
-
- pa_log_info(__FILE__": using %u fragments of size %lu bytes.", periods, (long unsigned)u->fragment_size);
-
- u->silence.memblock = pa_memblock_new(u->silence.length = u->fragment_size, c->memblock_stat);
- assert(u->silence.memblock);
- pa_silence_memblock(u->silence.memblock, &ss);
- u->silence.index = 0;
-
- u->memchunk.memblock = NULL;
- u->memchunk.index = u->memchunk.length = 0;
-
- ret = 0;
-
- /* Get initial mixer settings */
- if (u->sink->get_hw_volume)
- u->sink->get_hw_volume(u->sink);
- if (u->sink->get_hw_mute)
- u->sink->get_hw_mute(u->sink);
-
-finish:
- if (ma)
- pa_modargs_free(ma);
-
- if (pcm_info)
- snd_pcm_info_free(pcm_info);
-
- return ret;
-
-fail:
-
- if (u)
- pa__done(c, m);
-
- goto finish;
-}
-
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata *u;
- assert(c && m);
-
- if (!(u = m->userdata))
- return;
-
- if (u->sink) {
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- }
-
- if (u->pcm_fdl)
- pa_alsa_fdlist_free(u->pcm_fdl);
- if (u->mixer_fdl)
- pa_alsa_fdlist_free(u->mixer_fdl);
-
- if (u->mixer_handle)
- snd_mixer_close(u->mixer_handle);
-
- if (u->pcm_handle) {
- snd_pcm_drop(u->pcm_handle);
- snd_pcm_close(u->pcm_handle);
- }
-
- if (u->memchunk.memblock)
- pa_memblock_unref(u->memchunk.memblock);
- if (u->silence.memblock)
- pa_memblock_unref(u->silence.memblock);
-
- pa_xfree(u);
-}
-
diff --git a/src/modules/module-alsa-source.c b/src/modules/module-alsa-source.c
deleted file mode 100644
index ca4ac9d0..00000000
--- a/src/modules/module-alsa-source.c
+++ /dev/null
@@ -1,491 +0,0 @@
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <assert.h>
-#include <stdio.h>
-
-#ifdef HAVE_SYS_POLL_H
-#include <sys/poll.h>
-#else
-#include "poll.h"
-#endif
-
-#include <asoundlib.h>
-
-#include <pulse/xmalloc.h>
-
-#include <pulsecore/core-error.h>
-#include <pulsecore/core.h>
-#include <pulsecore/module.h>
-#include <pulsecore/memchunk.h>
-#include <pulsecore/sink.h>
-#include <pulsecore/modargs.h>
-#include <pulsecore/core-util.h>
-#include <pulsecore/sample-util.h>
-#include <pulsecore/log.h>
-
-#include "alsa-util.h"
-#include "module-alsa-source-symdef.h"
-
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("ALSA Source")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE(
- "source_name=<name for the source> "
- "device=<ALSA device> "
- "format=<sample format> "
- "channels=<number of channels> "
- "rate=<sample rate> "
- "fragments=<number of fragments> "
- "fragment_size=<fragment size> "
- "channel_map=<channel map>")
-
-struct userdata {
- snd_pcm_t *pcm_handle;
- snd_mixer_t *mixer_handle;
- snd_mixer_elem_t *mixer_elem;
- pa_source *source;
- struct pa_alsa_fdlist *pcm_fdl;
- struct pa_alsa_fdlist *mixer_fdl;
- long hw_volume_max, hw_volume_min;
-
- size_t frame_size, fragment_size;
- pa_memchunk memchunk;
- pa_module *module;
-};
-
-static const char* const valid_modargs[] = {
- "device",
- "source_name",
- "channels",
- "rate",
- "format",
- "fragments",
- "fragment_size",
- "channel_map",
- NULL
-};
-
-#define DEFAULT_SOURCE_NAME "alsa_input"
-#define DEFAULT_DEVICE "default"
-
-static void update_usage(struct userdata *u) {
- pa_module_set_used(u->module,
- (u->source ? pa_idxset_size(u->source->outputs) : 0));
-}
-
-static void xrun_recovery(struct userdata *u) {
- assert(u);
-
- pa_log(__FILE__": *** ALSA-XRUN (capture) ***");
-
- if (snd_pcm_prepare(u->pcm_handle) < 0)
- pa_log(__FILE__": snd_pcm_prepare() failed");
-}
-
-static void do_read(struct userdata *u) {
- assert(u);
-
- update_usage(u);
-
- for (;;) {
- pa_memchunk post_memchunk;
- snd_pcm_sframes_t frames;
- size_t l;
-
- if (!u->memchunk.memblock) {
- u->memchunk.memblock = pa_memblock_new(u->memchunk.length = u->fragment_size, u->source->core->memblock_stat);
- u->memchunk.index = 0;
- }
-
- assert(u->memchunk.memblock);
- assert(u->memchunk.length);
- assert(u->memchunk.memblock->data);
- assert(u->memchunk.memblock->length);
- assert(u->memchunk.length % u->frame_size == 0);
-
- if ((frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size)) < 0) {
- if (frames == -EAGAIN)
- return;
-
- if (frames == -EPIPE) {
- xrun_recovery(u);
- continue;
- }
-
- pa_log(__FILE__": snd_pcm_readi() failed: %s", pa_cstrerror(-frames));
- return;
- }
-
- l = frames * u->frame_size;
-
- post_memchunk = u->memchunk;
- post_memchunk.length = l;
-
- pa_source_post(u->source, &post_memchunk);
-
- u->memchunk.index += l;
- u->memchunk.length -= l;
-
- if (u->memchunk.length == 0) {
- pa_memblock_unref(u->memchunk.memblock);
- u->memchunk.memblock = NULL;
- u->memchunk.index = u->memchunk.length = 0;
- }
-
- break;
- }
-}
-
-static void fdl_callback(void *userdata) {
- struct userdata *u = userdata;
- assert(u);
-
- if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
- xrun_recovery(u);
-
- do_read(u);
-}
-
-static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
- struct userdata *u = snd_mixer_elem_get_callback_private(elem);
-
- assert(u && u->mixer_handle);
-
- if (mask == SND_CTL_EVENT_MASK_REMOVE)
- return 0;
-
- if (mask & SND_CTL_EVENT_MASK_VALUE) {
- if (u->source->get_hw_volume)
- u->source->get_hw_volume(u->source);
- if (u->source->get_hw_mute)
- u->source->get_hw_mute(u->source);
- pa_subscription_post(u->source->core,
- PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
- u->source->index);
- }
-
- return 0;
-}
-
-static pa_usec_t source_get_latency_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- snd_pcm_sframes_t frames;
- assert(s && u && u->source);
-
- if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
- pa_log(__FILE__": failed to get delay");
- s->get_latency = NULL;
- return 0;
- }
-
- return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
-}
-
-static int source_get_hw_volume_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- long vol;
- int err;
- int i;
-
- assert(u && u->mixer_elem);
-
- for (i = 0;i < s->hw_volume.channels;i++) {
- assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
-
- err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol);
- if (err < 0)
- goto fail;
- s->hw_volume.values[i] =
- (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
- }
-
- return 0;
-
-fail:
- pa_log_error(__FILE__": Unable to read volume: %s", snd_strerror(err));
- s->get_hw_volume = NULL;
- s->set_hw_volume = NULL;
- return -1;
-}
-
-static int source_set_hw_volume_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- int err;
- pa_volume_t vol;
- int i;
-
- assert(u && u->mixer_elem);
-
- for (i = 0;i < s->hw_volume.channels;i++) {
- assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
-
- vol = s->hw_volume.values[i];
-
- if (vol > PA_VOLUME_NORM)
- vol = PA_VOLUME_NORM;
-
- vol = vol * (u->hw_volume_max - u->hw_volume_min) /
- PA_VOLUME_NORM + u->hw_volume_min;
- err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, vol);
- if (err < 0)
- goto fail;
- }
-
- return 0;
-
-fail:
- pa_log_error(__FILE__": Unable to set volume: %s", snd_strerror(err));
- s->get_hw_volume = NULL;
- s->set_hw_volume = NULL;
- return -1;
-}
-
-static int source_get_hw_mute_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- int err, sw;
-
- assert(u && u->mixer_elem);
-
- err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw);
- if (err) {
- pa_log_error(__FILE__": Unable to get switch: %s", snd_strerror(err));
- s->get_hw_mute = NULL;
- s->set_hw_mute = NULL;
- return -1;
- }
-
- s->hw_muted = !sw;
-
- return 0;
-}
-
-static int source_set_hw_mute_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- int err;
-
- assert(u && u->mixer_elem);
-
- err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->hw_muted);
- if (err) {
- pa_log_error(__FILE__": Unable to set switch: %s", snd_strerror(err));
- s->get_hw_mute = NULL;
- s->set_hw_mute = NULL;
- return -1;
- }
-
- return 0;
-}
-
-int pa__init(pa_core *c, pa_module*m) {
- pa_modargs *ma = NULL;
- int ret = -1;
- struct userdata *u = NULL;
- const char *dev;
- pa_sample_spec ss;
- pa_channel_map map;
- unsigned periods, fragsize;
- snd_pcm_uframes_t period_size;
- size_t frame_size;
- snd_pcm_info_t *pcm_info = NULL;
- int err;
-
- if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
- goto fail;
- }
-
- ss = c->default_sample_spec;
- if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
- pa_log(__FILE__": failed to parse sample specification");
- goto fail;
- }
-
- frame_size = pa_frame_size(&ss);
-
- /* Fix latency to 100ms */
- periods = 12;
- fragsize = pa_bytes_per_second(&ss)/128;
-
- if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
- pa_log(__FILE__": failed to parse buffer metrics");
- goto fail;
- }
- period_size = fragsize/frame_size;
-
- u = pa_xnew0(struct userdata, 1);
- m->userdata = u;
- u->module = m;
-
- snd_config_update_free_global();
- if ((err = snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
- pa_log(__FILE__": Error opening PCM device %s: %s", dev, snd_strerror(err));
- goto fail;
- }
-
- if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
- (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
- pa_log(__FILE__": Error fetching PCM info: %s", snd_strerror(err));
- goto fail;
- }
-
- if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
- pa_log(__FILE__": Failed to set hardware parameters: %s", snd_strerror(err));
- goto fail;
- }
-
- if (ss.channels != map.channels)
- /* Seems ALSA didn't like the channel number, so let's fix the channel map */
- pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
-
- if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
- pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err));
- goto fail;
- }
-
- if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
- !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture", "Mic"))) {
- snd_mixer_close(u->mixer_handle);
- u->mixer_handle = NULL;
- }
-
- if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": Failed to create source object");
- goto fail;
- }
-
- u->source->is_hardware = 1;
- u->source->userdata = u;
- u->source->get_latency = source_get_latency_cb;
- if (u->mixer_handle) {
- assert(u->mixer_elem);
- if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
- int i;
-
- for (i = 0;i < ss.channels;i++) {
- if (!snd_mixer_selem_has_capture_channel(u->mixer_elem, i))
- break;
- }
-
- if (i == ss.channels) {
- u->source->get_hw_volume = source_get_hw_volume_cb;
- u->source->set_hw_volume = source_set_hw_volume_cb;
- snd_mixer_selem_get_capture_volume_range(
- u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
- }
- }
- if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
- u->source->get_hw_mute = source_get_hw_mute_cb;
- u->source->set_hw_mute = source_set_hw_mute_cb;
- }
- }
- pa_source_set_owner(u->source, m);
- u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev, snd_pcm_info_get_name(pcm_info));
-
- u->pcm_fdl = pa_alsa_fdlist_new();
- assert(u->pcm_fdl);
- if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
- pa_log(__FILE__": failed to initialise file descriptor monitoring");
- goto fail;
- }
-
- if (u->mixer_handle) {
- u->mixer_fdl = pa_alsa_fdlist_new();
- assert(u->mixer_fdl);
- if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
- pa_log(__FILE__": failed to initialise file descriptor monitoring");
- goto fail;
- }
- snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
- snd_mixer_elem_set_callback_private(u->mixer_elem, u);
- } else
- u->mixer_fdl = NULL;
-
- u->frame_size = frame_size;
- u->fragment_size = period_size * frame_size;
-
- pa_log_info(__FILE__": using %u fragments of size %lu bytes.", periods, (long unsigned) u->fragment_size);
-
- u->memchunk.memblock = NULL;
- u->memchunk.index = u->memchunk.length = 0;
-
- snd_pcm_start(u->pcm_handle);
-
- ret = 0;
-
- /* Get initial mixer settings */
- if (u->source->get_hw_volume)
- u->source->get_hw_volume(u->source);
- if (u->source->get_hw_mute)
- u->source->get_hw_mute(u->source);
-
-finish:
- if (ma)
- pa_modargs_free(ma);
-
- if (pcm_info)
- snd_pcm_info_free(pcm_info);
-
- return ret;
-
-fail:
-
- if (u)
- pa__done(c, m);
-
- goto finish;
-}
-
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata *u;
- assert(c && m);
-
- if (!(u = m->userdata))
- return;
-
- if (u->source) {
- pa_source_disconnect(u->source);
- pa_source_unref(u->source);
- }
-
- if (u->pcm_fdl)
- pa_alsa_fdlist_free(u->pcm_fdl);
- if (u->mixer_fdl)
- pa_alsa_fdlist_free(u->mixer_fdl);
-
- if (u->mixer_handle)
- snd_mixer_close(u->mixer_handle);
-
- if (u->pcm_handle) {
- snd_pcm_drop(u->pcm_handle);
- snd_pcm_close(u->pcm_handle);
- }
-
- if (u->memchunk.memblock)
- pa_memblock_unref(u->memchunk.memblock);
-
- pa_xfree(u);
-}
-
diff --git a/src/modules/module-always-sink.c b/src/modules/module-always-sink.c
new file mode 100644
index 00000000..4c871da4
--- /dev/null
+++ b/src/modules/module-always-sink.c
@@ -0,0 +1,189 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-util.h>
+
+#include "module-always-sink-symdef.h"
+
+PA_MODULE_AUTHOR("Colin Guthrie");
+PA_MODULE_DESCRIPTION(_("Always keeps at least one sink loaded even if it's a null one"));
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(
+ "sink_name=<name of sink>");
+
+#define DEFAULT_SINK_NAME "auto_null"
+
+static const char* const valid_modargs[] = {
+ "sink_name",
+ NULL,
+};
+
+struct userdata {
+ pa_hook_slot *put_slot, *unlink_slot;
+ uint32_t null_module;
+ pa_bool_t ignore;
+ char *sink_name;
+};
+
+static void load_null_sink_if_needed(pa_core *c, pa_sink *sink, struct userdata* u) {
+ pa_sink *target;
+ uint32_t idx;
+ char *t;
+ pa_module *m;
+
+ pa_assert(c);
+ pa_assert(u);
+ pa_assert(u->null_module == PA_INVALID_INDEX);
+
+ /* Loop through all sinks and check to see if we have *any*
+ * sinks. Ignore the sink passed in (if it's not null) */
+ for (target = pa_idxset_first(c->sinks, &idx); target; target = pa_idxset_next(c->sinks, &idx))
+ if (!sink || target != sink)
+ break;
+
+ if (target)
+ return;
+
+ pa_log_debug("Autoloading null-sink as no other sinks detected.");
+
+ u->ignore = TRUE;
+
+ t = pa_sprintf_malloc("sink_name=%s sink_properties='device.description=\"%s\"'", u->sink_name,
+ _("Dummy Output"));
+ m = pa_module_load(c, "module-null-sink", t);
+ u->null_module = m ? m->index : PA_INVALID_INDEX;
+ pa_xfree(t);
+
+ u->ignore = FALSE;
+
+ if (!m)
+ pa_log_warn("Unable to load module-null-sink");
+}
+
+static pa_hook_result_t put_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(c);
+ pa_assert(sink);
+ pa_assert(u);
+
+ /* This is us detecting ourselves on load... just ignore this. */
+ if (u->ignore)
+ return PA_HOOK_OK;
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ /* Auto-loaded null-sink not active, so ignoring newly detected sink. */
+ if (u->null_module == PA_INVALID_INDEX)
+ return PA_HOOK_OK;
+
+ /* This is us detecting ourselves on load in a different way... just ignore this too. */
+ if (sink->module && sink->module->index == u->null_module)
+ return PA_HOOK_OK;
+
+ pa_log_info("A new sink has been discovered. Unloading null-sink.");
+
+ pa_module_unload_request_by_index(c, u->null_module, TRUE);
+ u->null_module = PA_INVALID_INDEX;
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t unlink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(c);
+ pa_assert(sink);
+ pa_assert(u);
+
+ /* First check to see if it's our own null-sink that's been removed... */
+ if (u->null_module != PA_INVALID_INDEX && sink->module && sink->module->index == u->null_module) {
+ pa_log_debug("Autoloaded null-sink removed");
+ u->null_module = PA_INVALID_INDEX;
+ return PA_HOOK_OK;
+ }
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ load_null_sink_if_needed(c, sink, u);
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ return -1;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+ u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ u->put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) put_hook_callback, u);
+ u->unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_EARLY, (pa_hook_cb_t) unlink_hook_callback, u);
+ u->null_module = PA_INVALID_INDEX;
+ u->ignore = FALSE;
+
+ pa_modargs_free(ma);
+
+ load_null_sink_if_needed(m->core, NULL, u);
+
+ return 0;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->put_slot)
+ pa_hook_slot_free(u->put_slot);
+ if (u->unlink_slot)
+ pa_hook_slot_free(u->unlink_slot);
+ if (u->null_module != PA_INVALID_INDEX && m->core->state != PA_CORE_SHUTDOWN)
+ pa_module_unload_request_by_index(m->core, u->null_module, TRUE);
+
+ pa_xfree(u->sink_name);
+ pa_xfree(u);
+}
diff --git a/src/modules/module-augment-properties.c b/src/modules/module-augment-properties.c
new file mode 100644
index 00000000..05f6f0a4
--- /dev/null
+++ b/src/modules/module-augment-properties.c
@@ -0,0 +1,382 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/stat.h>
+#include <dirent.h>
+#include <time.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/client.h>
+#include <pulsecore/conf-parser.h>
+
+#include "module-augment-properties-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Augment the property sets of streams with additional static information");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+#define STAT_INTERVAL 30
+#define MAX_CACHE_SIZE 50
+
+static const char* const valid_modargs[] = {
+ NULL
+};
+
+struct rule {
+ time_t timestamp;
+ pa_bool_t good;
+ time_t mtime;
+ char *process_name;
+ char *application_name;
+ char *icon_name;
+ char *role;
+ pa_proplist *proplist;
+};
+
+struct userdata {
+ pa_hashmap *cache;
+ pa_hook_slot *client_new_slot, *client_proplist_changed_slot;
+};
+
+static void rule_free(struct rule *r) {
+ pa_assert(r);
+
+ pa_xfree(r->process_name);
+ pa_xfree(r->application_name);
+ pa_xfree(r->icon_name);
+ pa_xfree(r->role);
+ if (r->proplist)
+ pa_proplist_free(r->proplist);
+ pa_xfree(r);
+}
+
+static int parse_properties(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ struct rule *r = userdata;
+ pa_proplist *n;
+
+ if (!(n = pa_proplist_from_string(rvalue)))
+ return -1;
+
+ if (r->proplist) {
+ pa_proplist_update(r->proplist, PA_UPDATE_MERGE, n);
+ pa_proplist_free(n);
+ } else
+ r->proplist = n;
+
+ return 0;
+}
+
+static int parse_categories(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ struct rule *r = userdata;
+ const char *state = NULL;
+ char *c;
+
+ while ((c = pa_split(rvalue, ";", &state))) {
+
+ if (pa_streq(c, "Game")) {
+ pa_xfree(r->role);
+ r->role = pa_xstrdup("game");
+ } else if (pa_streq(c, "Telephony")) {
+ pa_xfree(r->role);
+ r->role = pa_xstrdup("phone");
+ }
+
+ pa_xfree(c);
+ }
+
+ return 0;
+}
+
+static int check_type(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ return pa_streq(rvalue, "Application") ? 0 : -1;
+}
+
+static int catch_all(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ return 0;
+}
+
+static void update_rule(struct rule *r) {
+ char *fn;
+ struct stat st;
+ static pa_config_item table[] = {
+ { "Name", pa_config_parse_string, NULL, "Desktop Entry" },
+ { "Icon", pa_config_parse_string, NULL, "Desktop Entry" },
+ { "Type", check_type, NULL, "Desktop Entry" },
+ { "X-PulseAudio-Properties", parse_properties, NULL, "Desktop Entry" },
+ { "Categories", parse_categories, NULL, "Desktop Entry" },
+ { NULL, catch_all, NULL, NULL },
+ { NULL, NULL, NULL, NULL },
+ };
+ pa_bool_t found = FALSE;
+
+ pa_assert(r);
+ fn = pa_sprintf_malloc(DESKTOPFILEDIR PA_PATH_SEP "%s.desktop", r->process_name);
+
+ if (stat(fn, &st) == 0)
+ found = TRUE;
+ else {
+#ifdef DT_DIR
+ DIR *desktopfiles_dir;
+ struct dirent *dir;
+
+ /* Let's try a more aggressive search, but only one level */
+ if ((desktopfiles_dir = opendir(DESKTOPFILEDIR))) {
+ while ((dir = readdir(desktopfiles_dir))) {
+ if (dir->d_type != DT_DIR
+ || strcmp(dir->d_name, ".") == 0
+ || strcmp(dir->d_name, "..") == 0)
+ continue;
+
+ pa_xfree(fn);
+ fn = pa_sprintf_malloc(DESKTOPFILEDIR PA_PATH_SEP "%s" PA_PATH_SEP "%s.desktop", dir->d_name, r->process_name);
+
+ if (stat(fn, &st) == 0) {
+ found = TRUE;
+ break;
+ }
+ }
+ closedir(desktopfiles_dir);
+ }
+#endif
+ }
+ if (!found) {
+ r->good = FALSE;
+ pa_xfree(fn);
+ return;
+ }
+
+ if (r->good) {
+ if (st.st_mtime == r->mtime) {
+ /* Theoretically the filename could have changed, but if so
+ having the same mtime is very unlikely so not worth tracking it in r */
+ pa_xfree(fn);
+ return;
+ }
+ pa_log_debug("Found %s (which has been updated since we last checked).", fn);
+ } else
+ pa_log_debug("Found %s.", fn);
+
+ r->good = TRUE;
+ r->mtime = st.st_mtime;
+ pa_xfree(r->application_name);
+ pa_xfree(r->icon_name);
+ pa_xfree(r->role);
+ r->application_name = r->icon_name = r->role = NULL;
+ if (r->proplist)
+ pa_proplist_clear(r->proplist);
+
+ table[0].data = &r->application_name;
+ table[1].data = &r->icon_name;
+
+ if (pa_config_parse(fn, NULL, table, r) < 0)
+ pa_log_warn("Failed to parse .desktop file %s.", fn);
+
+ pa_xfree(fn);
+}
+
+static void apply_rule(struct rule *r, pa_proplist *p) {
+ pa_assert(r);
+ pa_assert(p);
+
+ if (!r->good)
+ return;
+
+ if (r->proplist)
+ pa_proplist_update(p, PA_UPDATE_MERGE, r->proplist);
+
+ if (r->icon_name)
+ if (!pa_proplist_contains(p, PA_PROP_APPLICATION_ICON_NAME))
+ pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, r->icon_name);
+
+ if (r->application_name) {
+ const char *t;
+
+ t = pa_proplist_gets(p, PA_PROP_APPLICATION_NAME);
+
+ if (!t || pa_streq(t, r->process_name))
+ pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, r->application_name);
+ }
+
+ if (r->role)
+ if (!pa_proplist_contains(p, PA_PROP_MEDIA_ROLE))
+ pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, r->role);
+}
+
+static void make_room(pa_hashmap *cache) {
+ pa_assert(cache);
+
+ while (pa_hashmap_size(cache) >= MAX_CACHE_SIZE) {
+ struct rule *r;
+
+ pa_assert_se(r = pa_hashmap_steal_first(cache));
+ rule_free(r);
+ }
+}
+
+static pa_hook_result_t process(struct userdata *u, pa_proplist *p) {
+ struct rule *r;
+ time_t now;
+ const char *pn;
+
+ pa_assert(u);
+ pa_assert(p);
+
+ if (!(pn = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
+ return PA_HOOK_OK;
+
+ if (*pn == '.' || strchr(pn, '/'))
+ return PA_HOOK_OK;
+
+ time(&now);
+
+ pa_log_debug("Looking for .desktop file for %s", pn);
+
+ if ((r = pa_hashmap_get(u->cache, pn))) {
+ if (now-r->timestamp > STAT_INTERVAL) {
+ r->timestamp = now;
+ update_rule(r);
+ }
+ } else {
+ make_room(u->cache);
+
+ r = pa_xnew0(struct rule, 1);
+ r->process_name = pa_xstrdup(pn);
+ r->timestamp = now;
+ pa_hashmap_put(u->cache, r->process_name, r);
+ update_rule(r);
+ }
+
+ apply_rule(r, p);
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t client_new_cb(pa_core *core, pa_client_new_data *data, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_assert(data);
+ pa_assert(u);
+
+ return process(u, data->proplist);
+}
+
+static pa_hook_result_t client_proplist_changed_cb(pa_core *core, pa_client *client, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_assert(client);
+ pa_assert(u);
+
+ return process(u, client->proplist);
+}
+
+int pa__init(pa_module *m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+
+ u->cache = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ u->client_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CLIENT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) client_new_cb, u);
+ u->client_proplist_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CLIENT_PROPLIST_CHANGED], PA_HOOK_EARLY, (pa_hook_cb_t) client_proplist_changed_cb, u);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module *m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->client_new_slot)
+ pa_hook_slot_free(u->client_new_slot);
+ if (u->client_proplist_changed_slot)
+ pa_hook_slot_free(u->client_proplist_changed_slot);
+
+ if (u->cache) {
+ struct rule *r;
+
+ while ((r = pa_hashmap_steal_first(u->cache)))
+ rule_free(r);
+
+ pa_hashmap_free(u->cache, NULL, NULL);
+ }
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-card-restore.c b/src/modules/module-card-restore.c
new file mode 100644
index 00000000..fc5df5f6
--- /dev/null
+++ b/src/modules/module-card-restore.c
@@ -0,0 +1,368 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006-2008 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pulse/gccmacro.h>
+#include <pulse/xmalloc.h>
+#include <pulse/timeval.h>
+#include <pulse/rtclock.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-subscribe.h>
+#include <pulsecore/card.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/database.h>
+#include <pulsecore/tagstruct.h>
+
+#include "module-card-restore-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Automatically restore profile of cards");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
+
+static const char* const valid_modargs[] = {
+ NULL
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_subscription *subscription;
+ pa_hook_slot *card_new_hook_slot;
+ pa_time_event *save_time_event;
+ pa_database *database;
+};
+
+#define ENTRY_VERSION 1
+
+struct entry {
+ uint8_t version;
+ char *profile;
+};
+
+static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(a);
+ pa_assert(e);
+ pa_assert(u);
+
+ pa_assert(e == u->save_time_event);
+ u->core->mainloop->time_free(u->save_time_event);
+ u->save_time_event = NULL;
+
+ pa_database_sync(u->database);
+ pa_log_info("Synced.");
+}
+
+static void trigger_save(struct userdata *u) {
+ if (u->save_time_event)
+ return;
+
+ u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
+}
+
+static struct entry* entry_new(void) {
+ struct entry *r = pa_xnew0(struct entry, 1);
+ r->version = ENTRY_VERSION;
+ return r;
+}
+
+static void entry_free(struct entry* e) {
+ pa_assert(e);
+
+ pa_xfree(e->profile);
+ pa_xfree(e);
+}
+
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) {
+ pa_tagstruct *t;
+ pa_datum key, data;
+ pa_bool_t r;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu8(t, e->version);
+ pa_tagstruct_puts(t, e->profile);
+
+ key.data = (char *) name;
+ key.size = strlen(name);
+
+ data.data = (void*)pa_tagstruct_data(t, &data.size);
+
+ r = (pa_database_set(u->database, &key, &data, TRUE) == 0);
+
+ pa_tagstruct_free(t);
+
+ return r;
+}
+
+#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
+
+#define LEGACY_ENTRY_VERSION 1
+static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) {
+ struct legacy_entry {
+ uint8_t version;
+ char profile[PA_NAME_MAX];
+ } PA_GCC_PACKED ;
+ struct legacy_entry *le;
+ struct entry *e;
+
+ pa_assert(u);
+ pa_assert(data);
+
+ if (data->size != sizeof(struct legacy_entry)) {
+ pa_log_debug("Size does not match.");
+ return NULL;
+ }
+
+ le = (struct legacy_entry*)data->data;
+
+ if (le->version != LEGACY_ENTRY_VERSION) {
+ pa_log_debug("Version mismatch.");
+ return NULL;
+ }
+
+ if (!memchr(le->profile, 0, sizeof(le->profile))) {
+ pa_log_warn("Profile has missing NUL byte.");
+ return NULL;
+ }
+
+ e = entry_new();
+ e->profile = pa_xstrdup(le->profile);
+ return e;
+}
+#endif
+
+static struct entry* entry_read(struct userdata *u, const char *name) {
+ pa_datum key, data;
+ struct entry *e = NULL;
+ pa_tagstruct *t = NULL;
+ const char* profile;
+
+ pa_assert(u);
+ pa_assert(name);
+
+ key.data = (char*) name;
+ key.size = strlen(name);
+
+ pa_zero(data);
+
+ if (!pa_database_get(u->database, &key, &data))
+ goto fail;
+
+ t = pa_tagstruct_new(data.data, data.size);
+ e = entry_new();
+
+ if (pa_tagstruct_getu8(t, &e->version) < 0 ||
+ e->version > ENTRY_VERSION ||
+ pa_tagstruct_gets(t, &profile) < 0) {
+
+ goto fail;
+ }
+
+ e->profile = pa_xstrdup(profile);
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ pa_tagstruct_free(t);
+ pa_datum_free(&data);
+
+ return e;
+
+fail:
+
+ pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
+
+ if (e)
+ entry_free(e);
+ if (t)
+ pa_tagstruct_free(t);
+
+#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
+ pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name);
+ if ((e = legacy_entry_read(u, &data))) {
+ pa_log_debug("Success. Saving new format for key: %s", name);
+ if (entry_write(u, name, e))
+ trigger_save(u);
+ pa_datum_free(&data);
+ return e;
+ } else
+ pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name);
+#endif
+
+ pa_datum_free(&data);
+ return NULL;
+}
+
+static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ struct userdata *u = userdata;
+ struct entry *entry, *old;
+ pa_card *card;
+
+ pa_assert(c);
+ pa_assert(u);
+
+ if (t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW) &&
+ t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE))
+ return;
+
+ entry = entry_new();
+
+ if (!(card = pa_idxset_get_by_index(c->cards, idx)))
+ return;
+
+ if (!card->save_profile)
+ return;
+
+ entry->profile = pa_xstrdup(card->active_profile ? card->active_profile->name : "");
+
+ if ((old = entry_read(u, card->name))) {
+
+ if (pa_streq(old->profile, entry->profile)) {
+ entry_free(old);
+ entry_free(entry);
+ return;
+ }
+
+ entry_free(old);
+ }
+
+ pa_log_info("Storing profile for card %s.", card->name);
+
+ if (entry_write(u, card->name, entry))
+ trigger_save(u);
+
+ entry_free(entry);
+}
+
+static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, struct userdata *u) {
+ struct entry *e;
+
+ pa_assert(new_data);
+
+ if ((e = entry_read(u, new_data->name)) && e->profile[0]) {
+
+ if (!new_data->active_profile) {
+ pa_log_info("Restoring profile for card %s.", new_data->name);
+ pa_card_new_data_set_profile(new_data, e->profile);
+ new_data->save_profile = TRUE;
+ } else
+ pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name);
+
+ entry_free(e);
+ }
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+ char *fname;
+ pa_card *card;
+ uint32_t idx;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+
+ u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_CARD, subscribe_callback, u);
+
+ u->card_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) card_new_hook_callback, u);
+
+ if (!(fname = pa_state_path("card-database", TRUE)))
+ goto fail;
+
+ if (!(u->database = pa_database_open(fname, TRUE))) {
+ pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
+ pa_xfree(fname);
+ goto fail;
+ }
+
+ pa_log_info("Successfully opened database file '%s'.", fname);
+ pa_xfree(fname);
+
+ for (card = pa_idxset_first(m->core->cards, &idx); card; card = pa_idxset_next(m->core->cards, &idx))
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW, card->index, u);
+
+ pa_modargs_free(ma);
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->subscription)
+ pa_subscription_free(u->subscription);
+
+ if (u->card_new_hook_slot)
+ pa_hook_slot_free(u->card_new_hook_slot);
+
+ if (u->save_time_event)
+ u->core->mainloop->time_free(u->save_time_event);
+
+ if (u->database)
+ pa_database_close(u->database);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-cli.c b/src/modules/module-cli.c
index 2eef5c46..2a1d1751 100644
--- a/src/modules/module-cli.c
+++ b/src/modules/module-cli.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,8 +24,9 @@
#endif
#include <stdio.h>
-#include <assert.h>
#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
#include <pulsecore/module.h>
#include <pulsecore/iochannel.h>
@@ -33,13 +34,16 @@
#include <pulsecore/sioman.h>
#include <pulsecore/log.h>
#include <pulsecore/modargs.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/core-util.h>
#include "module-cli-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Command line interface")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("exit_on_eof=<exit daemon after EOF?>")
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Command line interface");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE("exit_on_eof=<exit daemon after EOF?>");
static const char* const valid_modargs[] = {
"exit_on_eof",
@@ -48,61 +52,78 @@ static const char* const valid_modargs[] = {
static void eof_and_unload_cb(pa_cli*c, void *userdata) {
pa_module *m = userdata;
-
- assert(c);
- assert(m);
- pa_module_unload_request(m);
+ pa_assert(c);
+ pa_assert(m);
+
+ pa_module_unload_request(m, TRUE);
}
static void eof_and_exit_cb(pa_cli*c, void *userdata) {
pa_module *m = userdata;
- assert(c);
- assert(m);
+ pa_assert(c);
+ pa_assert(m);
- m->core->mainloop->quit(m->core->mainloop, 0);
+ pa_core_exit(m->core, FALSE, 0);
}
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
pa_iochannel *io;
pa_modargs *ma;
- int exit_on_eof = 0;
-
- assert(c);
- assert(m);
+ pa_bool_t exit_on_eof = FALSE;
+#ifndef OS_IS_WIN32
+ int fd;
+#endif
- if (c->running_as_daemon) {
- pa_log_info(__FILE__": Running as daemon, refusing to load this module.");
+ pa_assert(m);
+
+ if (m->core->running_as_daemon) {
+ pa_log_info("Running as daemon, refusing to load this module.");
return 0;
}
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
+ pa_log("failed to parse module arguments.");
goto fail;
}
-
+
if (pa_modargs_get_value_boolean(ma, "exit_on_eof", &exit_on_eof) < 0) {
- pa_log(__FILE__": exit_on_eof= expects boolean argument.");
+ pa_log("exit_on_eof= expects boolean argument.");
goto fail;
}
if (pa_stdio_acquire() < 0) {
- pa_log(__FILE__": STDIN/STDUSE already in use.");
+ pa_log("STDIN/STDOUT already in use.");
goto fail;
}
- io = pa_iochannel_new(c->mainloop, STDIN_FILENO, STDOUT_FILENO);
- assert(io);
- pa_iochannel_set_noclose(io, 1);
-
- m->userdata = pa_cli_new(c, io, m);
- assert(m->userdata);
+ /* We try to open the controlling tty anew here. This has the
+ * benefit of giving us a new fd that doesn't share the O_NDELAY
+ * flag with fds 0, 1, or 2. Since pa_iochannel_xxx needs O_NDELAY
+ * on its fd using those fds directly could set O_NDELAY which
+ * fprintf() doesn't really like, resulting in truncated output
+ * of log messages, particularly because if stdout and stderr are
+ * dup'ed they share the same O_NDELAY, too. */
+
+#ifndef OS_IS_WIN32
+ if ((fd = pa_open_cloexec("/dev/tty", O_RDWR|O_NONBLOCK, 0)) >= 0) {
+ io = pa_iochannel_new(m->core->mainloop, fd, fd);
+ pa_log_debug("Managed to open /dev/tty.");
+ }
+ else
+#endif
+ {
+ io = pa_iochannel_new(m->core->mainloop, STDIN_FILENO, STDOUT_FILENO);
+ pa_iochannel_set_noclose(io, TRUE);
+ pa_log_debug("Failed to open /dev/tty, using stdin/stdout fds instead.");
+ }
+ m->userdata = pa_cli_new(m->core, io, m);
pa_cli_set_eof_callback(m->userdata, exit_on_eof ? eof_and_exit_cb : eof_and_unload_cb, m);
pa_modargs_free(ma);
-
+
return 0;
fail:
@@ -113,11 +134,10 @@ fail:
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
- assert(c);
- assert(m);
+void pa__done(pa_module*m) {
+ pa_assert(m);
- if (c->running_as_daemon == 0) {
+ if (m->userdata) {
pa_cli_free(m->userdata);
pa_stdio_release();
}
diff --git a/src/modules/module-combine-sink.c b/src/modules/module-combine-sink.c
new file mode 100644
index 00000000..5d29af4b
--- /dev/null
+++ b/src/modules/module-combine-sink.c
@@ -0,0 +1,1414 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2008 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/macro.h>
+#include <pulsecore/module.h>
+#include <pulsecore/llist.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/memblockq.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-rtclock.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/time-smoother.h>
+#include <pulsecore/strlist.h>
+
+#include "module-combine-sink-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Combine multiple sinks to one");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "slaves=<slave sinks> "
+ "adjust_time=<how often to readjust rates in s> "
+ "resample_method=<method> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map>");
+
+#define DEFAULT_SINK_NAME "combined"
+
+#define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
+
+#define DEFAULT_ADJUST_TIME_USEC (10*PA_USEC_PER_SEC)
+
+#define BLOCK_USEC (PA_USEC_PER_MSEC * 200)
+
+static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
+ "slaves",
+ "adjust_time",
+ "resample_method",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ NULL
+};
+
+struct output {
+ struct userdata *userdata;
+
+ pa_sink *sink;
+ pa_sink_input *sink_input;
+ pa_bool_t ignore_state_change;
+
+ pa_asyncmsgq *inq, /* Message queue from the sink thread to this sink input */
+ *outq; /* Message queue from this sink input to the sink thread */
+ pa_rtpoll_item *inq_rtpoll_item_read, *inq_rtpoll_item_write;
+ pa_rtpoll_item *outq_rtpoll_item_read, *outq_rtpoll_item_write;
+
+ pa_memblockq *memblockq;
+
+ /* For communication of the stream latencies to the main thread */
+ pa_usec_t total_latency;
+
+ /* For coomunication of the stream parameters to the sink thread */
+ pa_atomic_t max_request;
+ pa_atomic_t requested_latency;
+
+ PA_LLIST_FIELDS(struct output);
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_sink *sink;
+
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+
+ pa_time_event *time_event;
+ pa_usec_t adjust_time;
+
+ pa_bool_t automatic;
+ pa_bool_t auto_desc;
+
+ pa_strlist *unlinked_slaves;
+
+ pa_hook_slot *sink_put_slot, *sink_unlink_slot, *sink_state_changed_slot;
+
+ pa_resample_method_t resample_method;
+
+ pa_usec_t block_usec;
+
+ pa_idxset* outputs; /* managed in main context */
+
+ struct {
+ PA_LLIST_HEAD(struct output, active_outputs); /* managed in IO thread context */
+ pa_atomic_t running; /* we cache that value here, so that every thread can query it cheaply */
+ pa_usec_t timestamp;
+ pa_bool_t in_null_mode;
+ pa_smoother *smoother;
+ uint64_t counter;
+ } thread_info;
+};
+
+enum {
+ SINK_MESSAGE_ADD_OUTPUT = PA_SINK_MESSAGE_MAX,
+ SINK_MESSAGE_REMOVE_OUTPUT,
+ SINK_MESSAGE_NEED,
+ SINK_MESSAGE_UPDATE_LATENCY,
+ SINK_MESSAGE_UPDATE_MAX_REQUEST,
+ SINK_MESSAGE_UPDATE_REQUESTED_LATENCY
+};
+
+enum {
+ SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX,
+};
+
+static void output_disable(struct output *o);
+static void output_enable(struct output *o);
+static void output_free(struct output *o);
+static int output_create_sink_input(struct output *o);
+
+static void adjust_rates(struct userdata *u) {
+ struct output *o;
+ pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency, avg_total_latency = 0;
+ uint32_t base_rate;
+ uint32_t idx;
+ unsigned n = 0;
+
+ pa_assert(u);
+ pa_sink_assert_ref(u->sink);
+
+ if (pa_idxset_size(u->outputs) <= 0)
+ return;
+
+ if (!PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)))
+ return;
+
+ PA_IDXSET_FOREACH(o, u->outputs, idx) {
+ pa_usec_t sink_latency;
+
+ if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
+ continue;
+
+ o->total_latency = pa_sink_input_get_latency(o->sink_input, &sink_latency);
+ o->total_latency += sink_latency;
+
+ if (sink_latency > max_sink_latency)
+ max_sink_latency = sink_latency;
+
+ if (min_total_latency == (pa_usec_t) -1 || o->total_latency < min_total_latency)
+ min_total_latency = o->total_latency;
+
+ avg_total_latency += o->total_latency;
+ n++;
+
+ pa_log_debug("[%s] total=%0.2fms sink=%0.2fms ", o->sink->name, (double) o->total_latency / PA_USEC_PER_MSEC, (double) sink_latency / PA_USEC_PER_MSEC);
+
+ if (o->total_latency > 10*PA_USEC_PER_SEC)
+ pa_log_warn("[%s] Total latency of output is very high (%0.2fms), most likely the audio timing in one of your drivers is broken.", o->sink->name, (double) o->total_latency / PA_USEC_PER_MSEC);
+ }
+
+ if (min_total_latency == (pa_usec_t) -1)
+ return;
+
+ avg_total_latency /= n;
+
+ target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
+
+ pa_log_info("[%s] avg total latency is %0.2f msec.", u->sink->name, (double) avg_total_latency / PA_USEC_PER_MSEC);
+ pa_log_info("[%s] target latency is %0.2f msec.", u->sink->name, (double) target_latency / PA_USEC_PER_MSEC);
+
+ base_rate = u->sink->sample_spec.rate;
+
+ PA_IDXSET_FOREACH(o, u->outputs, idx) {
+ uint32_t new_rate = base_rate;
+ uint32_t current_rate = o->sink_input->sample_spec.rate;
+
+ if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
+ continue;
+
+ if (o->total_latency != target_latency)
+ new_rate += (uint32_t) (((double) o->total_latency - (double) target_latency) / (double) u->adjust_time * (double) new_rate);
+
+ if (new_rate < (uint32_t) (base_rate*0.8) || new_rate > (uint32_t) (base_rate*1.25)) {
+ pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->sink->name, base_rate, new_rate);
+ new_rate = base_rate;
+ } else {
+ if (base_rate < new_rate + 20 && new_rate < base_rate + 20)
+ new_rate = base_rate;
+ /* Do the adjustment in small steps; 2‰ can be considered inaudible */
+ if (new_rate < (uint32_t) (current_rate*0.998) || new_rate > (uint32_t) (current_rate*1.002)) {
+ pa_log_info("[%s] new rate of %u Hz not within 2‰ of %u Hz, forcing smaller adjustment", o->sink_input->sink->name, new_rate, current_rate);
+ new_rate = PA_CLAMP(new_rate, (uint32_t) (current_rate*0.998), (uint32_t) (current_rate*1.002));
+ }
+ pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.2f msec.", o->sink_input->sink->name, new_rate, (double) new_rate / base_rate, (double) o->total_latency / PA_USEC_PER_MSEC);
+ }
+ pa_sink_input_set_rate(o->sink_input, new_rate);
+ }
+
+ pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_UPDATE_LATENCY, NULL, (int64_t) avg_total_latency, NULL);
+}
+
+static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+ pa_assert(a);
+ pa_assert(u->time_event == e);
+
+ adjust_rates(u);
+
+ pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time);
+}
+
+static void process_render_null(struct userdata *u, pa_usec_t now) {
+ size_t ate = 0;
+ pa_assert(u);
+
+ /* If we are not running, we cannot produce any data */
+ if (!pa_atomic_load(&u->thread_info.running))
+ return;
+
+ if (u->thread_info.in_null_mode)
+ u->thread_info.timestamp = now;
+
+ while (u->thread_info.timestamp < now + u->block_usec) {
+ pa_memchunk chunk;
+
+ pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
+ pa_memblock_unref(chunk.memblock);
+
+ u->thread_info.counter += chunk.length;
+
+/* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
+ u->thread_info.timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
+
+ ate += chunk.length;
+
+ if (ate >= u->sink->thread_info.max_request)
+ break;
+ }
+
+/* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
+
+ pa_smoother_put(u->thread_info.smoother, now,
+ pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec) - (u->thread_info.timestamp - now));
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority+1);
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ u->thread_info.timestamp = pa_rtclock_now();
+ u->thread_info.in_null_mode = FALSE;
+
+ for (;;) {
+ int ret;
+
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
+ if (u->sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(u->sink, 0);
+
+ /* If no outputs are connected, render some data and drop it immediately. */
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && !u->thread_info.active_outputs) {
+ pa_usec_t now;
+
+ now = pa_rtclock_now();
+
+ if (!u->thread_info.in_null_mode || u->thread_info.timestamp <= now)
+ process_render_null(u, now);
+
+ pa_rtpoll_set_timer_absolute(u->rtpoll, u->thread_info.timestamp);
+ u->thread_info.in_null_mode = TRUE;
+ } else {
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
+ u->thread_info.in_null_mode = FALSE;
+ }
+
+ /* Hmm, nothing to do. Let's sleep */
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) {
+ pa_log_info("pa_rtpoll_run() = %i", ret);
+ goto fail;
+ }
+
+ if (ret == 0)
+ goto finish;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+/* Called from I/O thread context */
+static void render_memblock(struct userdata *u, struct output *o, size_t length) {
+ pa_assert(u);
+ pa_assert(o);
+
+ /* We are run by the sink thread, on behalf of an output (o). The
+ * output is waiting for us, hence it is safe to access its
+ * mainblockq and asyncmsgq directly. */
+
+ /* If we are not running, we cannot produce any data */
+ if (!pa_atomic_load(&u->thread_info.running))
+ return;
+
+ /* Maybe there's some data in the requesting output's queue
+ * now? */
+ while (pa_asyncmsgq_process_one(o->inq) > 0)
+ ;
+
+ /* Ok, now let's prepare some data if we really have to */
+ while (!pa_memblockq_is_readable(o->memblockq)) {
+ struct output *j;
+ pa_memchunk chunk;
+
+ /* Render data! */
+ pa_sink_render(u->sink, length, &chunk);
+
+ u->thread_info.counter += chunk.length;
+
+ /* OK, let's send this data to the other threads */
+ PA_LLIST_FOREACH(j, u->thread_info.active_outputs) {
+ if (j == o)
+ continue;
+
+ pa_asyncmsgq_post(j->inq, PA_MSGOBJECT(j->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, &chunk, NULL);
+ }
+
+ /* And place it directly into the requesting output's queue */
+ pa_memblockq_push_align(o->memblockq, &chunk);
+ pa_memblock_unref(chunk.memblock);
+ }
+}
+
+/* Called from I/O thread context */
+static void request_memblock(struct output *o, size_t length) {
+ pa_assert(o);
+ pa_sink_input_assert_ref(o->sink_input);
+ pa_sink_assert_ref(o->userdata->sink);
+
+ /* If another thread already prepared some data we received
+ * the data over the asyncmsgq, hence let's first process
+ * it. */
+ while (pa_asyncmsgq_process_one(o->inq) > 0)
+ ;
+
+ /* Check whether we're now readable */
+ if (pa_memblockq_is_readable(o->memblockq))
+ return;
+
+ /* OK, we need to prepare new data, but only if the sink is actually running */
+ if (pa_atomic_load(&o->userdata->thread_info.running))
+ pa_asyncmsgq_send(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_NEED, o, (int64_t) length, NULL);
+}
+
+/* Called from I/O thread context */
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
+ struct output *o;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(o = i->userdata);
+
+ /* If necessary, get some new data */
+ request_memblock(o, nbytes);
+
+ /* pa_log("%s q size is %u + %u (%u/%u)", */
+ /* i->sink->name, */
+ /* pa_memblockq_get_nblocks(o->memblockq), */
+ /* pa_memblockq_get_nblocks(i->thread_info.render_memblockq), */
+ /* pa_memblockq_get_maxrewind(o->memblockq), */
+ /* pa_memblockq_get_maxrewind(i->thread_info.render_memblockq)); */
+
+ if (pa_memblockq_peek(o->memblockq, chunk) < 0)
+ return -1;
+
+ pa_memblockq_drop(o->memblockq, chunk->length);
+
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct output *o;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(o = i->userdata);
+
+ pa_memblockq_rewind(o->memblockq, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct output *o;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(o = i->userdata);
+
+ pa_memblockq_set_maxrewind(o->memblockq, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
+ struct output *o;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(o = i->userdata);
+
+ if (pa_atomic_load(&o->max_request) == (int) nbytes)
+ return;
+
+ pa_atomic_store(&o->max_request, (int) nbytes);
+ pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
+}
+
+/* Called from thread context */
+static void sink_input_update_sink_requested_latency_cb(pa_sink_input *i) {
+ struct output *o;
+ pa_usec_t c;
+
+ pa_assert(i);
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(o = i->userdata);
+
+ c = pa_sink_get_requested_latency_within_thread(i->sink);
+
+ if (c == (pa_usec_t) -1)
+ c = i->sink->thread_info.max_latency;
+
+ if (pa_atomic_load(&o->requested_latency) == (int) c)
+ return;
+
+ pa_atomic_store(&o->requested_latency, (int) c);
+ pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
+}
+
+/* Called from I/O thread context */
+static void sink_input_attach_cb(pa_sink_input *i) {
+ struct output *o;
+ pa_usec_t c;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(o = i->userdata);
+
+ /* Set up the queue from the sink thread to us */
+ pa_assert(!o->inq_rtpoll_item_read && !o->outq_rtpoll_item_write);
+
+ o->inq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
+ i->sink->thread_info.rtpoll,
+ PA_RTPOLL_LATE, /* This one is not that important, since we check for data in _peek() anyway. */
+ o->inq);
+
+ o->outq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
+ i->sink->thread_info.rtpoll,
+ PA_RTPOLL_EARLY,
+ o->outq);
+
+ pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
+
+ pa_atomic_store(&o->max_request, (int) pa_sink_input_get_max_request(i));
+
+ c = pa_sink_get_requested_latency_within_thread(i->sink);
+ pa_atomic_store(&o->requested_latency, (int) (c == (pa_usec_t) -1 ? 0 : c));
+
+ pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
+ pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
+}
+
+/* Called from I/O thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+ struct output *o;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(o = i->userdata);
+
+ if (o->inq_rtpoll_item_read) {
+ pa_rtpoll_item_free(o->inq_rtpoll_item_read);
+ o->inq_rtpoll_item_read = NULL;
+ }
+
+ if (o->outq_rtpoll_item_write) {
+ pa_rtpoll_item_free(o->outq_rtpoll_item_write);
+ o->outq_rtpoll_item_write = NULL;
+ }
+}
+
+/* Called from main context */
+static void sink_input_kill_cb(pa_sink_input *i) {
+ struct output *o;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(o = i->userdata);
+
+ pa_module_unload_request(o->userdata->module, TRUE);
+ output_free(o);
+}
+
+/* Called from thread context */
+static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct output *o = PA_SINK_INPUT(obj)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
+ pa_usec_t *r = data;
+
+ *r = pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &o->sink_input->sample_spec);
+
+ /* Fall through, the default handler will add in the extra
+ * latency added by the resampler */
+ break;
+ }
+
+ case SINK_INPUT_MESSAGE_POST:
+
+ if (PA_SINK_IS_OPENED(o->sink_input->sink->thread_info.state))
+ pa_memblockq_push_align(o->memblockq, chunk);
+ else
+ pa_memblockq_flush_write(o->memblockq, TRUE);
+
+ return 0;
+ }
+
+ return pa_sink_input_process_msg(obj, code, data, offset, chunk);
+}
+
+/* Called from main context */
+static void suspend(struct userdata *u) {
+ struct output *o;
+ uint32_t idx;
+
+ pa_assert(u);
+
+ /* Let's suspend by unlinking all streams */
+ PA_IDXSET_FOREACH(o, u->outputs, idx)
+ output_disable(o);
+
+ pa_log_info("Device suspended...");
+}
+
+/* Called from main context */
+static void unsuspend(struct userdata *u) {
+ struct output *o;
+ uint32_t idx;
+
+ pa_assert(u);
+
+ /* Let's resume */
+ PA_IDXSET_FOREACH(o, u->outputs, idx)
+ output_enable(o);
+
+ pa_log_info("Resumed successfully...");
+}
+
+/* Called from main context */
+static int sink_set_state(pa_sink *sink, pa_sink_state_t state) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(sink);
+ pa_assert_se(u = sink->userdata);
+
+ /* Please note that in contrast to the ALSA modules we call
+ * suspend/unsuspend from main context here! */
+
+ switch (state) {
+ case PA_SINK_SUSPENDED:
+ pa_assert(PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)));
+
+ suspend(u);
+ break;
+
+ case PA_SINK_IDLE:
+ case PA_SINK_RUNNING:
+
+ if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED)
+ unsuspend(u);
+
+ break;
+
+ case PA_SINK_UNLINKED:
+ case PA_SINK_INIT:
+ case PA_SINK_INVALID_STATE:
+ ;
+ }
+
+ return 0;
+}
+
+/* Called from IO context */
+static void update_max_request(struct userdata *u) {
+ size_t max_request = 0;
+ struct output *o;
+
+ pa_assert(u);
+ pa_sink_assert_io_context(u->sink);
+
+ /* Collects the max_request values of all streams and sets the
+ * largest one locally */
+
+ PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
+ size_t mr = (size_t) pa_atomic_load(&o->max_request);
+
+ if (mr > max_request)
+ max_request = mr;
+ }
+
+ if (max_request <= 0)
+ max_request = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
+
+ pa_sink_set_max_request_within_thread(u->sink, max_request);
+}
+
+/* Called from IO context */
+static void update_fixed_latency(struct userdata *u) {
+ pa_usec_t fixed_latency = 0;
+ struct output *o;
+
+ pa_assert(u);
+ pa_sink_assert_io_context(u->sink);
+
+ /* Collects the requested_latency values of all streams and sets
+ * the largest one as fixed_latency locally */
+
+ PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
+ pa_usec_t rl = (size_t) pa_atomic_load(&o->requested_latency);
+
+ if (rl > fixed_latency)
+ fixed_latency = rl;
+ }
+
+ if (fixed_latency <= 0)
+ fixed_latency = u->block_usec;
+
+ pa_sink_set_fixed_latency_within_thread(u->sink, fixed_latency);
+}
+
+/* Called from thread context of the io thread */
+static void output_add_within_thread(struct output *o) {
+ pa_assert(o);
+ pa_sink_assert_io_context(o->sink);
+
+ PA_LLIST_PREPEND(struct output, o->userdata->thread_info.active_outputs, o);
+
+ pa_assert(!o->outq_rtpoll_item_read && !o->inq_rtpoll_item_write);
+
+ o->outq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
+ o->userdata->rtpoll,
+ PA_RTPOLL_EARLY-1, /* This item is very important */
+ o->outq);
+ o->inq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
+ o->userdata->rtpoll,
+ PA_RTPOLL_EARLY,
+ o->inq);
+}
+
+/* Called from thread context of the io thread */
+static void output_remove_within_thread(struct output *o) {
+ pa_assert(o);
+ pa_sink_assert_io_context(o->sink);
+
+ PA_LLIST_REMOVE(struct output, o->userdata->thread_info.active_outputs, o);
+
+ if (o->outq_rtpoll_item_read) {
+ pa_rtpoll_item_free(o->outq_rtpoll_item_read);
+ o->outq_rtpoll_item_read = NULL;
+ }
+
+ if (o->inq_rtpoll_item_write) {
+ pa_rtpoll_item_free(o->inq_rtpoll_item_write);
+ o->inq_rtpoll_item_write = NULL;
+ }
+}
+
+/* Called from thread context of the io thread */
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_SET_STATE: {
+ pa_bool_t running = (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING);
+
+ pa_atomic_store(&u->thread_info.running, running);
+
+ if (running)
+ pa_smoother_resume(u->thread_info.smoother, pa_rtclock_now(), TRUE);
+ else
+ pa_smoother_pause(u->thread_info.smoother, pa_rtclock_now());
+
+ break;
+ }
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ pa_usec_t x, y, c, *delay = data;
+
+ x = pa_rtclock_now();
+ y = pa_smoother_get(u->thread_info.smoother, x);
+
+ c = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
+
+ if (y < c)
+ *delay = c - y;
+ else
+ *delay = 0;
+
+ return 0;
+ }
+
+ case SINK_MESSAGE_ADD_OUTPUT:
+ output_add_within_thread(data);
+ update_max_request(u);
+ update_fixed_latency(u);
+ return 0;
+
+ case SINK_MESSAGE_REMOVE_OUTPUT:
+ output_remove_within_thread(data);
+ update_max_request(u);
+ update_fixed_latency(u);
+ return 0;
+
+ case SINK_MESSAGE_NEED:
+ render_memblock(u, (struct output*) data, (size_t) offset);
+ return 0;
+
+ case SINK_MESSAGE_UPDATE_LATENCY: {
+ pa_usec_t x, y, latency = (pa_usec_t) offset;
+
+ x = pa_rtclock_now();
+ y = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
+
+ if (y > latency)
+ y -= latency;
+ else
+ y = 0;
+
+ pa_smoother_put(u->thread_info.smoother, x, y);
+ return 0;
+ }
+
+ case SINK_MESSAGE_UPDATE_MAX_REQUEST:
+ update_max_request(u);
+ break;
+
+ case SINK_MESSAGE_UPDATE_REQUESTED_LATENCY:
+ update_fixed_latency(u);
+ break;
+}
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+static void update_description(struct userdata *u) {
+ pa_bool_t first = TRUE;
+ char *t;
+ struct output *o;
+ uint32_t idx;
+
+ pa_assert(u);
+
+ if (!u->auto_desc)
+ return;
+
+ if (pa_idxset_isempty(u->outputs)) {
+ pa_sink_set_description(u->sink, "Simultaneous output");
+ return;
+ }
+
+ t = pa_xstrdup("Simultaneous output to");
+
+ PA_IDXSET_FOREACH(o, u->outputs, idx) {
+ char *e;
+
+ if (first) {
+ e = pa_sprintf_malloc("%s %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+ first = FALSE;
+ } else
+ e = pa_sprintf_malloc("%s, %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+
+ pa_xfree(t);
+ t = e;
+ }
+
+ pa_sink_set_description(u->sink, t);
+ pa_xfree(t);
+}
+
+static int output_create_sink_input(struct output *o) {
+ pa_sink_input_new_data data;
+
+ pa_assert(o);
+
+ if (o->sink_input)
+ return 0;
+
+ pa_sink_input_new_data_init(&data);
+ pa_sink_input_new_data_set_sink(&data, o->sink, FALSE);
+ data.driver = __FILE__;
+ pa_proplist_setf(data.proplist, PA_PROP_MEDIA_NAME, "Simultaneous output on %s", pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+ pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "filter");
+ pa_sink_input_new_data_set_sample_spec(&data, &o->userdata->sink->sample_spec);
+ pa_sink_input_new_data_set_channel_map(&data, &o->userdata->sink->channel_map);
+ data.module = o->userdata->module;
+ data.resample_method = o->userdata->resample_method;
+ data.flags = PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE|PA_SINK_INPUT_NO_CREATE_ON_SUSPEND;
+
+ pa_sink_input_new(&o->sink_input, o->userdata->core, &data);
+
+ pa_sink_input_new_data_done(&data);
+
+ if (!o->sink_input)
+ return -1;
+
+ o->sink_input->parent.process_msg = sink_input_process_msg;
+ o->sink_input->pop = sink_input_pop_cb;
+ o->sink_input->process_rewind = sink_input_process_rewind_cb;
+ o->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
+ o->sink_input->update_max_request = sink_input_update_max_request_cb;
+ o->sink_input->update_sink_requested_latency = sink_input_update_sink_requested_latency_cb;
+ o->sink_input->attach = sink_input_attach_cb;
+ o->sink_input->detach = sink_input_detach_cb;
+ o->sink_input->kill = sink_input_kill_cb;
+ o->sink_input->userdata = o;
+
+ pa_sink_input_set_requested_latency(o->sink_input, BLOCK_USEC);
+
+ return 0;
+}
+
+/* Called from main context */
+static struct output *output_new(struct userdata *u, pa_sink *sink) {
+ struct output *o;
+
+ pa_assert(u);
+ pa_assert(sink);
+ pa_assert(u->sink);
+
+ o = pa_xnew0(struct output, 1);
+ o->userdata = u;
+ o->inq = pa_asyncmsgq_new(0);
+ o->outq = pa_asyncmsgq_new(0);
+ o->sink = sink;
+ o->memblockq = pa_memblockq_new(
+ 0,
+ MEMBLOCKQ_MAXLENGTH,
+ MEMBLOCKQ_MAXLENGTH,
+ pa_frame_size(&u->sink->sample_spec),
+ 1,
+ 0,
+ 0,
+ &u->sink->silence);
+
+ pa_assert_se(pa_idxset_put(u->outputs, o, NULL) == 0);
+ update_description(u);
+
+ return o;
+}
+
+/* Called from main context */
+static void output_free(struct output *o) {
+ pa_assert(o);
+
+ output_disable(o);
+
+ pa_assert_se(pa_idxset_remove_by_data(o->userdata->outputs, o, NULL));
+ update_description(o->userdata);
+
+ if (o->inq_rtpoll_item_read)
+ pa_rtpoll_item_free(o->inq_rtpoll_item_read);
+ if (o->inq_rtpoll_item_write)
+ pa_rtpoll_item_free(o->inq_rtpoll_item_write);
+
+ if (o->outq_rtpoll_item_read)
+ pa_rtpoll_item_free(o->outq_rtpoll_item_read);
+ if (o->outq_rtpoll_item_write)
+ pa_rtpoll_item_free(o->outq_rtpoll_item_write);
+
+ if (o->inq)
+ pa_asyncmsgq_unref(o->inq);
+
+ if (o->outq)
+ pa_asyncmsgq_unref(o->outq);
+
+ if (o->memblockq)
+ pa_memblockq_free(o->memblockq);
+
+ pa_xfree(o);
+}
+
+/* Called from main context */
+static void output_enable(struct output *o) {
+ pa_assert(o);
+
+ if (o->sink_input)
+ return;
+
+ /* This might cause the sink to be resumed. The state change hook
+ * of the sink might hence be called from here, which might then
+ * cause us to be called in a loop. Make sure that state changes
+ * for this output don't cause this loop by setting a flag here */
+ o->ignore_state_change = TRUE;
+
+ if (output_create_sink_input(o) >= 0) {
+
+ if (pa_sink_get_state(o->sink) != PA_SINK_INIT) {
+
+ /* First we register the output. That means that the sink
+ * will start to pass data to this output. */
+ pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
+
+ /* Then we enable the sink input. That means that the sink
+ * is now asked for new data. */
+ pa_sink_input_put(o->sink_input);
+
+ } else
+ /* Hmm the sink is not yet started, do things right here */
+ output_add_within_thread(o);
+ }
+
+ o->ignore_state_change = FALSE;
+}
+
+/* Called from main context */
+static void output_disable(struct output *o) {
+ pa_assert(o);
+
+ if (!o->sink_input)
+ return;
+
+ /* First we disable the sink input. That means that the sink is
+ * not asked for new data anymore */
+ pa_sink_input_unlink(o->sink_input);
+
+ /* Then we unregister the output. That means that the sink doesn't
+ * pass any further data to this output */
+ pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
+
+ /* Now dellocate the stream */
+ pa_sink_input_unref(o->sink_input);
+ o->sink_input = NULL;
+
+ /* Finally, drop all queued data */
+ pa_memblockq_flush_write(o->memblockq, TRUE);
+ pa_asyncmsgq_flush(o->inq, FALSE);
+ pa_asyncmsgq_flush(o->outq, FALSE);
+}
+
+/* Called from main context */
+static void output_verify(struct output *o) {
+ pa_assert(o);
+
+ if (PA_SINK_IS_OPENED(pa_sink_get_state(o->userdata->sink)))
+ output_enable(o);
+ else
+ output_disable(o);
+}
+
+/* Called from main context */
+static pa_bool_t is_suitable_sink(struct userdata *u, pa_sink *s) {
+ const char *t;
+
+ pa_sink_assert_ref(s);
+
+ if (s == u->sink)
+ return FALSE;
+
+ if (!(s->flags & PA_SINK_HARDWARE))
+ return FALSE;
+
+ if (!(s->flags & PA_SINK_LATENCY))
+ return FALSE;
+
+ if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_CLASS)))
+ if (!pa_streq(t, "sound"))
+ return FALSE;
+
+ return TRUE;
+}
+
+/* Called from main context */
+static pa_hook_result_t sink_put_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
+ struct output *o;
+
+ pa_core_assert_ref(c);
+ pa_sink_assert_ref(s);
+ pa_assert(u);
+
+ if (u->automatic) {
+ if (!is_suitable_sink(u, s))
+ return PA_HOOK_OK;
+ } else {
+ /* Check if the sink is a previously unlinked slave (non-automatic mode) */
+ pa_strlist *l = u->unlinked_slaves;
+
+ while (l && !pa_streq(pa_strlist_data(l), s->name))
+ l = pa_strlist_next(l);
+
+ if (!l)
+ return PA_HOOK_OK;
+
+ u->unlinked_slaves = pa_strlist_remove(u->unlinked_slaves, s->name);
+ }
+
+ pa_log_info("Configuring new sink: %s", s->name);
+ if (!(o = output_new(u, s))) {
+ pa_log("Failed to create sink input on sink '%s'.", s->name);
+ return PA_HOOK_OK;
+ }
+
+ output_verify(o);
+
+ return PA_HOOK_OK;
+}
+
+/* Called from main context */
+static struct output* find_output(struct userdata *u, pa_sink *s) {
+ struct output *o;
+ uint32_t idx;
+
+ pa_assert(u);
+ pa_assert(s);
+
+ if (u->sink == s)
+ return NULL;
+
+ PA_IDXSET_FOREACH(o, u->outputs, idx)
+ if (o->sink == s)
+ return o;
+
+ return NULL;
+}
+
+/* Called from main context */
+static pa_hook_result_t sink_unlink_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
+ struct output *o;
+
+ pa_assert(c);
+ pa_sink_assert_ref(s);
+ pa_assert(u);
+
+ if (!(o = find_output(u, s)))
+ return PA_HOOK_OK;
+
+ pa_log_info("Unconfiguring sink: %s", s->name);
+
+ if (!u->automatic)
+ u->unlinked_slaves = pa_strlist_prepend(u->unlinked_slaves, s->name);
+
+ output_free(o);
+
+ return PA_HOOK_OK;
+}
+
+/* Called from main context */
+static pa_hook_result_t sink_state_changed_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
+ struct output *o;
+
+ if (!(o = find_output(u, s)))
+ return PA_HOOK_OK;
+
+ /* This state change might be triggered because we are creating a
+ * stream here, in that case we don't want to create it a second
+ * time here and enter a loop */
+ if (o->ignore_state_change)
+ return PA_HOOK_OK;
+
+ output_verify(o);
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_modargs *ma = NULL;
+ const char *slaves, *rm;
+ int resample_method = PA_RESAMPLER_TRIVIAL;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ struct output *o;
+ uint32_t idx;
+ pa_sink_new_data data;
+ uint32_t adjust_time_sec;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("failed to parse module arguments");
+ goto fail;
+ }
+
+ if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
+ if ((resample_method = pa_parse_resample_method(rm)) < 0) {
+ pa_log("invalid resample method '%s'", rm);
+ goto fail;
+ }
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+ u->resample_method = resample_method;
+ u->outputs = pa_idxset_new(NULL, NULL);
+ u->thread_info.smoother = pa_smoother_new(
+ PA_USEC_PER_SEC,
+ PA_USEC_PER_SEC*2,
+ TRUE,
+ TRUE,
+ 10,
+ pa_rtclock_now(),
+ TRUE);
+
+ adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
+ if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
+ pa_log("Failed to parse adjust_time value");
+ goto fail;
+ }
+
+ if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
+ u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
+ else
+ u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
+
+ slaves = pa_modargs_get_value(ma, "slaves", NULL);
+ u->automatic = !slaves;
+
+ ss = m->core->default_sample_spec;
+ map = m->core->default_channel_map;
+
+ /* Check the specified slave sinks for sample_spec and channel_map to use for the combined sink */
+ if (!u->automatic) {
+ const char*split_state = NULL;
+ char *n = NULL;
+ pa_sample_spec slaves_spec;
+ pa_channel_map slaves_map;
+ pa_bool_t is_first_slave = TRUE;
+
+ pa_sample_spec_init(&slaves_spec);
+
+ while ((n = pa_split(slaves, ",", &split_state))) {
+ pa_sink *slave_sink;
+
+ if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK))) {
+ pa_log("Invalid slave sink '%s'", n);
+ pa_xfree(n);
+ goto fail;
+ }
+
+ pa_xfree(n);
+
+ if (is_first_slave) {
+ slaves_spec = slave_sink->sample_spec;
+ slaves_map = slave_sink->channel_map;
+ is_first_slave = FALSE;
+ } else {
+ if (slaves_spec.format != slave_sink->sample_spec.format)
+ slaves_spec.format = PA_SAMPLE_INVALID;
+
+ if (slaves_spec.rate < slave_sink->sample_spec.rate)
+ slaves_spec.rate = slave_sink->sample_spec.rate;
+
+ if (!pa_channel_map_equal(&slaves_map, &slave_sink->channel_map))
+ slaves_spec.channels = 0;
+ }
+ }
+
+ if (!is_first_slave) {
+ if (slaves_spec.format != PA_SAMPLE_INVALID)
+ ss.format = slaves_spec.format;
+
+ ss.rate = slaves_spec.rate;
+
+ if (slaves_spec.channels > 0) {
+ map = slaves_map;
+ ss.channels = slaves_map.channels;
+ }
+ }
+ }
+
+ if ((pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0)) {
+ pa_log("Invalid sample specification.");
+ goto fail;
+ }
+
+ pa_sink_new_data_init(&data);
+ data.namereg_fail = FALSE;
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "filter");
+
+ if (slaves)
+ pa_proplist_sets(data.proplist, "combine.slaves", slaves);
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&data);
+ goto fail;
+ }
+
+ /* Check proplist for a description & fill in a default value if not */
+ u->auto_desc = FALSE;
+ if (NULL == pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)) {
+ u->auto_desc = TRUE;
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Simultaneous Output");
+ }
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg;
+ u->sink->set_state = sink_set_state;
+ u->sink->userdata = u;
+
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+
+ u->block_usec = BLOCK_USEC;
+ pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec));
+
+ if (!u->automatic) {
+ const char*split_state;
+ char *n = NULL;
+ pa_assert(slaves);
+
+ /* The slaves have been specified manually */
+
+ split_state = NULL;
+ while ((n = pa_split(slaves, ",", &split_state))) {
+ pa_sink *slave_sink;
+
+ if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK)) || slave_sink == u->sink) {
+ pa_log("Invalid slave sink '%s'", n);
+ pa_xfree(n);
+ goto fail;
+ }
+
+ pa_xfree(n);
+
+ if (!output_new(u, slave_sink)) {
+ pa_log("Failed to create slave sink input on sink '%s'.", slave_sink->name);
+ goto fail;
+ }
+ }
+
+ if (pa_idxset_size(u->outputs) <= 1)
+ pa_log_warn("No slave sinks specified.");
+
+ u->sink_put_slot = NULL;
+
+ } else {
+ pa_sink *s;
+
+ /* We're in automatic mode, we add every sink that matches our needs */
+
+ PA_IDXSET_FOREACH(s, m->core->sinks, idx) {
+
+ if (!is_suitable_sink(u, s))
+ continue;
+
+ if (!output_new(u, s)) {
+ pa_log("Failed to create sink input on sink '%s'.", s->name);
+ goto fail;
+ }
+ }
+ }
+
+ u->sink_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_put_hook_cb, u);
+ u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_EARLY, (pa_hook_cb_t) sink_unlink_hook_cb, u);
+ u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_state_changed_hook_cb, u);
+
+ if (!(u->thread = pa_thread_new("combine", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ /* Activate the sink and the sink inputs */
+ pa_sink_put(u->sink);
+
+ PA_IDXSET_FOREACH(o, u->outputs, idx)
+ output_verify(o);
+
+ if (u->adjust_time > 0)
+ u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+ struct output *o;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ pa_strlist_free(u->unlinked_slaves);
+
+ if (u->sink_put_slot)
+ pa_hook_slot_free(u->sink_put_slot);
+
+ if (u->sink_unlink_slot)
+ pa_hook_slot_free(u->sink_unlink_slot);
+
+ if (u->sink_state_changed_slot)
+ pa_hook_slot_free(u->sink_state_changed_slot);
+
+ if (u->outputs) {
+ while ((o = pa_idxset_first(u->outputs, NULL)))
+ output_free(o);
+
+ pa_idxset_free(u->outputs, NULL, NULL);
+ }
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ if (u->time_event)
+ u->core->mainloop->time_free(u->time_event);
+
+ if (u->thread_info.smoother)
+ pa_smoother_free(u->thread_info.smoother);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-combine.c b/src/modules/module-combine.c
index 4e3dd555..251df494 100644
--- a/src/modules/module-combine.c
+++ b/src/modules/module-combine.c
@@ -1,18 +1,19 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2009 Lennart Poettering
+ Copyright 2011 Colin Guthrie
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -23,421 +24,49 @@
#include <config.h>
#endif
-#include <assert.h>
-#include <stdio.h>
-
-#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
#include <pulsecore/module.h>
-#include <pulsecore/llist.h>
-#include <pulsecore/sink.h>
-#include <pulsecore/sink-input.h>
-#include <pulsecore/memblockq.h>
#include <pulsecore/log.h>
-#include <pulsecore/core-util.h>
-#include <pulsecore/modargs.h>
-#include <pulsecore/namereg.h>
#include "module-combine-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE(
- "sink_name=<name for the sink> "
- "master=<master sink> "
- "slaves=<slave sinks> "
- "adjust_time=<seconds> "
- "resample_method=<method> "
- "format=<sample format> "
- "channels=<number of channels> "
- "rate=<sample rate> "
- "channel_map=<channel map> ")
-
-#define DEFAULT_SINK_NAME "combined"
-#define MEMBLOCKQ_MAXLENGTH (1024*170)
-#define RENDER_SIZE (1024*10)
-
-#define DEFAULT_ADJUST_TIME 20
-
-static const char* const valid_modargs[] = {
- "sink_name",
- "master",
- "slaves",
- "adjust_time",
- "resample_method",
- "format",
- "channels",
- "rate",
- "channel_map",
- NULL
-};
-
-struct output {
- struct userdata *userdata;
- pa_sink_input *sink_input;
- size_t counter;
- pa_memblockq *memblockq;
- pa_usec_t total_latency;
- PA_LLIST_FIELDS(struct output);
-};
+PA_MODULE_AUTHOR("Colin Guthrie");
+PA_MODULE_DESCRIPTION("Compatibility module (module-combine rename)");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_DEPRECATED("Please use module-combine-sink instead of module-combine!");
struct userdata {
- pa_module *module;
- pa_core *core;
- pa_sink *sink;
- unsigned n_outputs;
- struct output *master;
- pa_time_event *time_event;
- uint32_t adjust_time;
-
- PA_LLIST_HEAD(struct output, outputs);
+ uint32_t module_index;
};
-static void output_free(struct output *o);
-static void clear_up(struct userdata *u);
-
-static void update_usage(struct userdata *u) {
- pa_module_set_used(u->module,
- (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
- (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0));
-}
-
-
-static void adjust_rates(struct userdata *u) {
- struct output *o;
- pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency;
- uint32_t base_rate;
- assert(u && u->sink);
-
- for (o = u->outputs; o; o = o->next) {
- uint32_t sink_latency = o->sink_input->sink ? pa_sink_get_latency(o->sink_input->sink) : 0;
-
- o->total_latency = sink_latency + pa_sink_input_get_latency(o->sink_input);
-
- if (sink_latency > max_sink_latency)
- max_sink_latency = sink_latency;
-
- if (o->total_latency < min_total_latency)
- min_total_latency = o->total_latency;
- }
-
- assert(min_total_latency != (pa_usec_t) -1);
-
- target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
-
- pa_log_info(__FILE__": [%s] target latency is %0.0f usec.", u->sink->name, (float) target_latency);
-
- base_rate = u->sink->sample_spec.rate;
-
- for (o = u->outputs; o; o = o->next) {
- uint32_t r = base_rate;
-
- if (o->total_latency < target_latency)
- r -= (uint32_t) (((((double) target_latency - o->total_latency))/u->adjust_time)*r/ 1000000);
- else if (o->total_latency > target_latency)
- r += (uint32_t) (((((double) o->total_latency - target_latency))/u->adjust_time)*r/ 1000000);
-
- if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1))
- pa_log_warn(__FILE__": [%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->name, base_rate, r);
- else {
- pa_log_info(__FILE__": [%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", o->sink_input->name, r, (double) r / base_rate, (float) o->total_latency);
- pa_sink_input_set_rate(o->sink_input, r);
- }
- }
-}
-
-static void request_memblock(struct userdata *u) {
- pa_memchunk chunk;
- struct output *o;
- assert(u && u->sink);
-
- update_usage(u);
-
- if (pa_sink_render(u->sink, RENDER_SIZE, &chunk) < 0)
- return;
-
- for (o = u->outputs; o; o = o->next)
- pa_memblockq_push_align(o->memblockq, &chunk);
-
- pa_memblock_unref(chunk.memblock);
-}
-
-static void time_callback(pa_mainloop_api*a, pa_time_event* e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
- struct userdata *u = userdata;
- struct timeval n;
- assert(u && a && u->time_event == e);
-
- adjust_rates(u);
-
- pa_gettimeofday(&n);
- n.tv_sec += u->adjust_time;
- u->sink->core->mainloop->time_restart(e, &n);
-}
-
-static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
- struct output *o = i->userdata;
- assert(i && o && o->sink_input && chunk);
-
- if (pa_memblockq_peek(o->memblockq, chunk) >= 0)
- return 0;
-
- /* Try harder */
- request_memblock(o->userdata);
-
- return pa_memblockq_peek(o->memblockq, chunk);
-}
-
-static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
- struct output *o = i->userdata;
- assert(i && o && o->sink_input && chunk && length);
-
- pa_memblockq_drop(o->memblockq, chunk, length);
- o->counter += length;
-}
-
-static void sink_input_kill_cb(pa_sink_input *i) {
- struct output *o = i->userdata;
- assert(i && o && o->sink_input);
- pa_module_unload_request(o->userdata->module);
- clear_up(o->userdata);
-}
-
-static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
- struct output *o = i->userdata;
- assert(i && o && o->sink_input);
-
- return pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &i->sample_spec);
-}
-
-static pa_usec_t sink_get_latency_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- assert(s && u && u->sink && u->master);
-
- return
- pa_sink_input_get_latency(u->master->sink_input) +
- pa_sink_get_latency(u->master->sink_input->sink);
-}
-
-static struct output *output_new(struct userdata *u, pa_sink *sink, int resample_method) {
- struct output *o = NULL;
- char t[256];
- assert(u && sink && u->sink);
-
- o = pa_xmalloc(sizeof(struct output));
- o->userdata = u;
-
- o->counter = 0;
- o->memblockq = pa_memblockq_new(
- 0,
- MEMBLOCKQ_MAXLENGTH,
- MEMBLOCKQ_MAXLENGTH,
- pa_frame_size(&u->sink->sample_spec),
- 1,
- 0,
- NULL,
- sink->core->memblock_stat);
-
- snprintf(t, sizeof(t), "%s: output #%u", u->sink->name, u->n_outputs+1);
- if (!(o->sink_input = pa_sink_input_new(sink, __FILE__, t, &u->sink->sample_spec, &u->sink->channel_map, NULL, 1, resample_method)))
- goto fail;
-
- o->sink_input->get_latency = sink_input_get_latency_cb;
- o->sink_input->peek = sink_input_peek_cb;
- o->sink_input->drop = sink_input_drop_cb;
- o->sink_input->kill = sink_input_kill_cb;
- o->sink_input->userdata = o;
- o->sink_input->owner = u->module;
-
- PA_LLIST_PREPEND(struct output, u->outputs, o);
- u->n_outputs++;
- return o;
-
-fail:
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_module *module;
- if (o) {
- if (o->sink_input) {
- pa_sink_input_disconnect(o->sink_input);
- pa_sink_input_unref(o->sink_input);
- }
+ pa_assert(m);
+ pa_assert_se(m->userdata = u = pa_xnew0(struct userdata, 1));
- if (o->memblockq)
- pa_memblockq_free(o->memblockq);
-
- pa_xfree(o);
- }
+ pa_log_warn("We will now load module-combine-sink. Please make sure to remove module-combine from your configuration.");
- return NULL;
-}
+ module = pa_module_load(m->core, "module-combine-sink", m->argument);
+ u->module_index = module ? module->index : PA_INVALID_INDEX;
-static void output_free(struct output *o) {
- assert(o);
- PA_LLIST_REMOVE(struct output, o->userdata->outputs, o);
- o->userdata->n_outputs--;
- pa_memblockq_free(o->memblockq);
- pa_sink_input_disconnect(o->sink_input);
- pa_sink_input_unref(o->sink_input);
- pa_xfree(o);
+ return module ? 0 : -1;
}
-static void clear_up(struct userdata *u) {
- struct output *o;
- assert(u);
-
- if (u->time_event) {
- u->core->mainloop->time_free(u->time_event);
- u->time_event = NULL;
- }
-
- while ((o = u->outputs))
- output_free(o);
-
- u->master = NULL;
-
- if (u->sink) {
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- u->sink = NULL;
- }
-}
-int pa__init(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata *u;
- pa_modargs *ma = NULL;
- const char *master_name, *slaves, *rm;
- pa_sink *master_sink;
- char *n = NULL;
- const char*split_state;
- struct timeval tv;
- int resample_method = -1;
- pa_sample_spec ss;
- pa_channel_map map;
-
- assert(c && m);
-
- if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
- goto fail;
- }
-
- if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
- if ((resample_method = pa_parse_resample_method(rm)) < 0) {
- pa_log(__FILE__": invalid resample method '%s'", rm);
- goto fail;
- }
- }
-
- u = pa_xnew(struct userdata, 1);
- m->userdata = u;
- u->sink = NULL;
- u->n_outputs = 0;
- u->master = NULL;
- u->module = m;
- u->core = c;
- u->time_event = NULL;
- u->adjust_time = DEFAULT_ADJUST_TIME;
- PA_LLIST_HEAD_INIT(struct output, u->outputs);
-
- if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
- pa_log(__FILE__": failed to parse adjust_time value");
- goto fail;
- }
-
- if (!(master_name = pa_modargs_get_value(ma, "master", NULL)) || !(slaves = pa_modargs_get_value(ma, "slaves", NULL))) {
- pa_log(__FILE__": no master or slave sinks specified");
- goto fail;
- }
-
- if (!(master_sink = pa_namereg_get(c, master_name, PA_NAMEREG_SINK, 1))) {
- pa_log(__FILE__": invalid master sink '%s'", master_name);
- goto fail;
- }
-
- ss = master_sink->sample_spec;
- if ((pa_modargs_get_sample_spec(ma, &ss) < 0)) {
- pa_log(__FILE__": invalid sample specification.");
- goto fail;
- }
-
- if (ss.channels == master_sink->sample_spec.channels)
- map = master_sink->channel_map;
- else
- pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_DEFAULT);
-
- if ((pa_modargs_get_channel_map(ma, &map) < 0)) {
- pa_log(__FILE__": invalid channel map.");
- goto fail;
- }
-
- if (ss.channels != map.channels) {
- pa_log(__FILE__": channel map and sample specification don't match.");
- goto fail;
- }
-
- if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": failed to create sink");
- goto fail;
- }
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Combined sink");
- u->sink->get_latency = sink_get_latency_cb;
- u->sink->userdata = u;
-
- if (!(u->master = output_new(u, master_sink, resample_method))) {
- pa_log(__FILE__": failed to create master sink input on sink '%s'.", u->sink->name);
- goto fail;
- }
-
- split_state = NULL;
- while ((n = pa_split(slaves, ",", &split_state))) {
- pa_sink *slave_sink;
-
- if (!(slave_sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
- pa_log(__FILE__": invalid slave sink '%s'", n);
- goto fail;
- }
+ pa_assert(m);
+ pa_assert(m->userdata);
- pa_xfree(n);
+ u = m->userdata;
- if (!output_new(u, slave_sink, resample_method)) {
- pa_log(__FILE__": failed to create slave sink input on sink '%s'.", slave_sink->name);
- goto fail;
- }
- }
-
- if (u->n_outputs <= 1)
- pa_log_warn(__FILE__": WARNING: no slave sinks specified.");
+ if (u && PA_INVALID_INDEX != u->module_index)
+ pa_module_unload_by_index(m->core, u->module_index, TRUE);
- if (u->adjust_time > 0) {
- pa_gettimeofday(&tv);
- tv.tv_sec += u->adjust_time;
- u->time_event = c->mainloop->time_new(c->mainloop, &tv, time_callback, u);
- }
-
- pa_modargs_free(ma);
- return 0;
-
-fail:
- pa_xfree(n);
-
- if (ma)
- pa_modargs_free(ma);
-
- pa__done(c, m);
- return -1;
-}
-
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata *u;
- assert(c && m);
-
- if (!(u = m->userdata))
- return;
-
- clear_up(u);
pa_xfree(u);
}
-
-
diff --git a/src/modules/module-console-kit.c b/src/modules/module-console-kit.c
new file mode 100644
index 00000000..4c5857cf
--- /dev/null
+++ b/src/modules/module-console-kit.c
@@ -0,0 +1,365 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/log.h>
+#include <pulsecore/hashmap.h>
+#include <pulsecore/idxset.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/dbus-shared.h>
+
+#include "module-console-kit-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Create a client for each ConsoleKit session of this user");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+static const char* const valid_modargs[] = {
+ NULL
+};
+
+struct session {
+ char *id;
+ pa_client *client;
+};
+
+struct userdata {
+ pa_module *module;
+ pa_core *core;
+ pa_dbus_connection *connection;
+ pa_hashmap *sessions;
+ pa_bool_t filter_added;
+};
+
+static void add_session(struct userdata *u, const char *id) {
+ DBusError error;
+ DBusMessage *m = NULL, *reply = NULL;
+ uint32_t uid;
+ struct session *session;
+ pa_client_new_data data;
+
+ dbus_error_init(&error);
+
+ if (pa_hashmap_get(u->sessions, id)) {
+ pa_log_warn("Duplicate session %s, ignoring.", id);
+ return;
+ }
+
+ if (!(m = dbus_message_new_method_call("org.freedesktop.ConsoleKit", id, "org.freedesktop.ConsoleKit.Session", "GetUnixUser"))) {
+ pa_log("Failed to allocate GetUnixUser() method call.");
+ goto fail;
+ }
+
+ if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
+ pa_log("GetUnixUser() call failed: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ /* CK 0.3 this changed from int32 to uint32 */
+ if (!dbus_message_get_args(reply, &error, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID)) {
+ dbus_error_free(&error);
+
+ if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
+ pa_log("Failed to parse GetUnixUser() result: %s: %s", error.name, error.message);
+ goto fail;
+ }
+ }
+
+ /* We only care about our own sessions */
+ if ((uid_t) uid != getuid())
+ goto fail;
+
+ session = pa_xnew(struct session, 1);
+ session->id = pa_xstrdup(id);
+
+ pa_client_new_data_init(&data);
+ data.module = u->module;
+ data.driver = __FILE__;
+ pa_proplist_setf(data.proplist, PA_PROP_APPLICATION_NAME, "ConsoleKit Session %s", id);
+ pa_proplist_sets(data.proplist, "console-kit.session", id);
+ session->client = pa_client_new(u->core, &data);
+ pa_client_new_data_done(&data);
+
+ if (!session->client) {
+ pa_xfree(session->id);
+ pa_xfree(session);
+ goto fail;
+ }
+
+ pa_hashmap_put(u->sessions, session->id, session);
+
+ pa_log_debug("Added new session %s", id);
+
+fail:
+
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+}
+
+static void free_session(struct session *session) {
+ pa_assert(session);
+
+ pa_log_debug("Removing session %s", session->id);
+
+ pa_client_free(session->client);
+ pa_xfree(session->id);
+ pa_xfree(session);
+}
+
+static void remove_session(struct userdata *u, const char *id) {
+ struct session *session;
+
+ if (!(session = pa_hashmap_remove(u->sessions, id)))
+ return;
+
+ free_session(session);
+}
+
+static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, void *userdata) {
+ struct userdata *u = userdata;
+ DBusError error;
+ const char *path;
+
+ pa_assert(bus);
+ pa_assert(message);
+ pa_assert(u);
+
+ dbus_error_init(&error);
+
+ pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
+ dbus_message_get_interface(message),
+ dbus_message_get_path(message),
+ dbus_message_get_member(message));
+
+ if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionAdded")) {
+
+ /* CK API changed to match spec in 0.3 */
+ if (!dbus_message_get_args(message, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ dbus_error_free(&error);
+
+ if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID)) {
+ pa_log_error("Failed to parse SessionAdded message: %s: %s", error.name, error.message);
+ goto finish;
+ }
+ }
+
+ add_session(u, path);
+
+ } else if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionRemoved")) {
+
+ /* CK API changed to match spec in 0.3 */
+ if (!dbus_message_get_args(message, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ dbus_error_free(&error);
+
+ if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID)) {
+ pa_log_error("Failed to parse SessionRemoved message: %s: %s", error.name, error.message);
+ goto finish;
+ }
+ }
+
+ remove_session(u, path);
+ }
+
+finish:
+ dbus_error_free(&error);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static int get_session_list(struct userdata *u) {
+ DBusError error;
+ DBusMessage *m = NULL, *reply = NULL;
+ uint32_t uid;
+ DBusMessageIter iter, sub;
+ int ret = -1;
+
+ pa_assert(u);
+
+ dbus_error_init(&error);
+
+ if (!(m = dbus_message_new_method_call("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", "GetSessionsForUnixUser"))) {
+ pa_log("Failed to allocate GetSessionsForUnixUser() method call.");
+ goto fail;
+ }
+
+ uid = (uint32_t) getuid();
+ if (!(dbus_message_append_args(m, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID))) {
+ pa_log("Failed to append arguments to GetSessionsForUnixUser() method call.");
+ goto fail;
+ }
+
+ if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
+ pa_log("GetSessionsForUnixUser() call failed: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ dbus_message_iter_init(reply, &iter);
+
+ if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
+ dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_OBJECT_PATH) {
+ pa_log("Failed to parse GetSessionsForUnixUser() result.");
+ goto fail;
+ }
+
+ dbus_message_iter_recurse(&iter, &sub);
+
+ for (;;) {
+ int at;
+ const char *id;
+
+ if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
+ break;
+
+ assert(at == DBUS_TYPE_OBJECT_PATH);
+ dbus_message_iter_get_basic(&sub, &id);
+
+ add_session(u, id);
+
+ dbus_message_iter_next(&sub);
+ }
+
+ ret = 0;
+
+fail:
+
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ return ret;
+}
+
+int pa__init(pa_module*m) {
+ DBusError error;
+ pa_dbus_connection *connection;
+ struct userdata *u = NULL;
+ pa_modargs *ma;
+
+ pa_assert(m);
+
+ dbus_error_init(&error);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (!(connection = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &error)) || dbus_error_is_set(&error)) {
+
+ if (connection)
+ pa_dbus_connection_unref(connection);
+
+ pa_log_error("Unable to contact D-Bus system bus: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->connection = connection;
+ u->sessions = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ if (!dbus_connection_add_filter(pa_dbus_connection_get(connection), filter_cb, u, NULL)) {
+ pa_log_error("Failed to add filter function");
+ goto fail;
+ }
+
+ u->filter_added = TRUE;
+
+ if (pa_dbus_add_matches(
+ pa_dbus_connection_get(connection), &error,
+ "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionAdded'",
+ "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionRemoved'", NULL) < 0) {
+ pa_log_error("Unable to subscribe to ConsoleKit signals: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ if (get_session_list(u) < 0)
+ goto fail;
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ dbus_error_free(&error);
+ pa__done(m);
+
+ return -1;
+}
+
+
+void pa__done(pa_module *m) {
+ struct userdata *u;
+ struct session *session;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sessions) {
+ while ((session = pa_hashmap_steal_first(u->sessions)))
+ free_session(session);
+
+ pa_hashmap_free(u->sessions, NULL, NULL);
+ }
+
+ if (u->connection) {
+ pa_dbus_remove_matches(
+ pa_dbus_connection_get(u->connection),
+ "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionAdded'",
+ "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionRemoved'", NULL);
+
+ if (u->filter_added)
+ dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), filter_cb, u);
+
+ pa_dbus_connection_unref(u->connection);
+ }
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-cork-music-on-phone.c b/src/modules/module-cork-music-on-phone.c
new file mode 100644
index 00000000..4c9c1788
--- /dev/null
+++ b/src/modules/module-cork-music-on-phone.c
@@ -0,0 +1,237 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/macro.h>
+#include <pulsecore/hashmap.h>
+#include <pulsecore/hook-list.h>
+#include <pulsecore/core.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/modargs.h>
+
+#include "module-cork-music-on-phone-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Mute or cork music while a phone stream exists");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+static const char* const valid_modargs[] = {
+ NULL
+};
+
+struct userdata {
+ pa_core *core;
+ pa_hashmap *cork_state;
+ pa_hook_slot
+ *sink_input_put_slot,
+ *sink_input_unlink_slot,
+ *sink_input_move_start_slot,
+ *sink_input_move_finish_slot;
+};
+
+static pa_bool_t shall_cork(pa_sink *s, pa_sink_input *ignore) {
+ pa_sink_input *j;
+ uint32_t idx;
+ pa_sink_assert_ref(s);
+
+ for (j = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); j; j = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
+ const char *role;
+
+ if (j == ignore)
+ continue;
+
+ if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
+ continue;
+
+ if (pa_streq(role, "phone")) {
+ pa_log_debug("Found a phone stream that will trigger the auto-cork.");
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+static void apply_cork(struct userdata *u, pa_sink *s, pa_sink_input *ignore, pa_bool_t cork) {
+ pa_sink_input *j;
+ uint32_t idx;
+
+ pa_assert(u);
+ pa_sink_assert_ref(s);
+
+ for (j = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); j; j = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
+ pa_bool_t corked, muted, corked_here;
+ const char *role;
+
+ if (j == ignore)
+ continue;
+
+ if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
+ continue;
+
+ if (!pa_streq(role, "video") &&
+ !pa_streq(role, "music"))
+ continue;
+
+ corked = (pa_sink_input_get_state(j) == PA_SINK_INPUT_CORKED);
+ muted = pa_sink_input_get_mute(j);
+ corked_here = !!pa_hashmap_get(u->cork_state, j);
+
+ if (cork && !corked && !muted) {
+ pa_log_debug("Found a music/video stream that should be corked/muted.");
+ if (!corked_here)
+ pa_hashmap_put(u->cork_state, j, PA_INT_TO_PTR(1));
+ pa_sink_input_set_mute(j, TRUE, FALSE);
+ pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_CORK, NULL);
+ } else if (!cork) {
+ pa_hashmap_remove(u->cork_state, j);
+
+ if (corked_here && (corked || muted)) {
+ pa_log_debug("Found a music/video stream that should be uncorked/unmuted.");
+ if (muted)
+ pa_sink_input_set_mute(j, FALSE, FALSE);
+ if (corked)
+ pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_UNCORK, NULL);
+ }
+ }
+ }
+}
+
+static pa_hook_result_t process(struct userdata *u, pa_sink_input *i, pa_bool_t create) {
+ pa_bool_t cork = FALSE;
+ const char *role;
+
+ pa_assert(u);
+ pa_sink_input_assert_ref(i);
+
+ if (!create)
+ pa_hashmap_remove(u->cork_state, i);
+
+ if (!(role = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_ROLE)))
+ return PA_HOOK_OK;
+
+ if (!pa_streq(role, "phone") &&
+ !pa_streq(role, "music") &&
+ !pa_streq(role, "video"))
+ return PA_HOOK_OK;
+
+ if (!i->sink)
+ return PA_HOOK_OK;
+
+ cork = shall_cork(i->sink, create ? NULL : i);
+ apply_cork(u, i->sink, create ? NULL : i, cork);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_sink_input_assert_ref(i);
+
+ return process(u, i, TRUE);
+}
+
+static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_sink_input_assert_ref(i);
+
+ return process(u, i, FALSE);
+}
+
+static pa_hook_result_t sink_input_move_start_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_sink_input_assert_ref(i);
+
+ return process(u, i, FALSE);
+}
+
+static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_sink_input_assert_ref(i);
+
+ return process(u, i, TRUE);
+}
+
+int pa__init(pa_module *m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+
+ u->core = m->core;
+ u->cork_state = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ u->sink_input_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_put_cb, u);
+ u->sink_input_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_unlink_cb, u);
+ u->sink_input_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_move_start_cb, u);
+ u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_move_finish_cb, u);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+
+
+}
+
+void pa__done(pa_module *m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink_input_put_slot)
+ pa_hook_slot_free(u->sink_input_put_slot);
+ if (u->sink_input_unlink_slot)
+ pa_hook_slot_free(u->sink_input_unlink_slot);
+ if (u->sink_input_move_start_slot)
+ pa_hook_slot_free(u->sink_input_move_start_slot);
+ if (u->sink_input_move_finish_slot)
+ pa_hook_slot_free(u->sink_input_move_finish_slot);
+
+ if (u->cork_state)
+ pa_hashmap_free(u->cork_state, NULL, NULL);
+
+ pa_xfree(u);
+
+}
diff --git a/src/modules/module-default-device-restore.c b/src/modules/module-default-device-restore.c
new file mode 100644
index 00000000..f28bddb7
--- /dev/null
+++ b/src/modules/module-default-device-restore.c
@@ -0,0 +1,199 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006-2008 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/module.h>
+#include <pulsecore/log.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/core-error.h>
+
+#include "module-default-device-restore-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Automatically restore the default sink and source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+#define SAVE_INTERVAL (5 * PA_USEC_PER_SEC)
+
+struct userdata {
+ pa_core *core;
+ pa_subscription *subscription;
+ pa_time_event *time_event;
+ char *sink_filename, *source_filename;
+ pa_bool_t modified;
+};
+
+static void load(struct userdata *u) {
+ FILE *f;
+
+ /* We never overwrite manually configured settings */
+
+ if (u->core->default_sink)
+ pa_log_info("Manually configured default sink, not overwriting.");
+ else if ((f = pa_fopen_cloexec(u->sink_filename, "r"))) {
+ char ln[256] = "";
+ pa_sink *s;
+
+ if (fgets(ln, sizeof(ln)-1, f))
+ pa_strip_nl(ln);
+ fclose(f);
+
+ if (!ln[0])
+ pa_log_info("No previous default sink setting, ignoring.");
+ else if ((s = pa_namereg_get(u->core, ln, PA_NAMEREG_SINK))) {
+ pa_namereg_set_default_sink(u->core, s);
+ pa_log_info("Restored default sink '%s'.", ln);
+ } else
+ pa_log_info("Saved default sink '%s' not existant, not restoring default sink setting.", ln);
+
+ } else if (errno != ENOENT)
+ pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
+
+ if (u->core->default_source)
+ pa_log_info("Manually configured default source, not overwriting.");
+ else if ((f = pa_fopen_cloexec(u->source_filename, "r"))) {
+ char ln[256] = "";
+ pa_source *s;
+
+ if (fgets(ln, sizeof(ln)-1, f))
+ pa_strip_nl(ln);
+ fclose(f);
+
+ if (!ln[0])
+ pa_log_info("No previous default source setting, ignoring.");
+ else if ((s = pa_namereg_get(u->core, ln, PA_NAMEREG_SOURCE))) {
+ pa_namereg_set_default_source(u->core, s);
+ pa_log_info("Restored default source '%s'.", ln);
+ } else
+ pa_log_info("Saved default source '%s' not existant, not restoring default source setting.", ln);
+
+ } else if (errno != ENOENT)
+ pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
+}
+
+static void save(struct userdata *u) {
+ FILE *f;
+
+ if (!u->modified)
+ return;
+
+ if (u->sink_filename) {
+ if ((f = pa_fopen_cloexec(u->sink_filename, "w"))) {
+ pa_sink *s = pa_namereg_get_default_sink(u->core);
+ fprintf(f, "%s\n", s ? s->name : "");
+ fclose(f);
+ } else
+ pa_log("Failed to save default sink: %s", pa_cstrerror(errno));
+ }
+
+ if (u->source_filename) {
+ if ((f = pa_fopen_cloexec(u->source_filename, "w"))) {
+ pa_source *s = pa_namereg_get_default_source(u->core);
+ fprintf(f, "%s\n", s ? s->name : "");
+ fclose(f);
+ } else
+ pa_log("Failed to save default source: %s", pa_cstrerror(errno));
+ }
+
+ u->modified = FALSE;
+}
+
+static void time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+ save(u);
+
+ if (u->time_event) {
+ u->core->mainloop->time_free(u->time_event);
+ u->time_event = NULL;
+ }
+}
+
+static void subscribe_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+
+ u->modified = TRUE;
+
+ if (!u->time_event)
+ u->time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, time_cb, u);
+}
+
+int pa__init(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+
+ if (!(u->sink_filename = pa_state_path("default-sink", TRUE)))
+ goto fail;
+
+ if (!(u->source_filename = pa_state_path("default-source", TRUE)))
+ goto fail;
+
+ load(u);
+
+ u->subscription = pa_subscription_new(u->core, PA_SUBSCRIPTION_MASK_SERVER, subscribe_cb, u);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ save(u);
+
+ if (u->subscription)
+ pa_subscription_free(u->subscription);
+
+ if (u->time_event)
+ m->core->mainloop->time_free(u->time_event);
+
+ pa_xfree(u->sink_filename);
+ pa_xfree(u->source_filename);
+ pa_xfree(u);
+}
diff --git a/src/modules/module-defs.h.m4 b/src/modules/module-defs.h.m4
index c961412d..b6a60b6a 100644
--- a/src/modules/module-defs.h.m4
+++ b/src/modules/module-defs.h.m4
@@ -1,4 +1,3 @@
-dnl $Id$
changecom(`/*', `*/')dnl
define(`module_name', patsubst(patsubst(patsubst(fname, `-symdef.h$'), `^.*/'), `[^0-9a-zA-Z]', `_'))dnl
define(`c_symbol', patsubst(module_name, `[^0-9a-zA-Z]', `_'))dnl
@@ -10,6 +9,7 @@ define(`gen_symbol', `#define $1 'module_name`_LTX_$1')dnl
#include <pulsecore/core.h>
#include <pulsecore/module.h>
+#include <pulsecore/macro.h>
gen_symbol(pa__init)
gen_symbol(pa__done)
@@ -17,13 +17,19 @@ gen_symbol(pa__get_author)
gen_symbol(pa__get_description)
gen_symbol(pa__get_usage)
gen_symbol(pa__get_version)
+gen_symbol(pa__get_deprecated)
+gen_symbol(pa__load_once)
+gen_symbol(pa__get_n_used)
-int pa__init(struct pa_core *c, struct pa_module*m);
-void pa__done(struct pa_core *c, struct pa_module*m);
+int pa__init(pa_module*m);
+void pa__done(pa_module*m);
+int pa__get_n_used(pa_module*m);
const char* pa__get_author(void);
const char* pa__get_description(void);
const char* pa__get_usage(void);
const char* pa__get_version(void);
+const char* pa__get_deprecated(void);
+pa_bool_t pa__load_once(void);
#endif
diff --git a/src/modules/module-detect.c b/src/modules/module-detect.c
index ebafa10d..bb4994c6 100644
--- a/src/modules/module-detect.c
+++ b/src/modules/module-detect.c
@@ -1,18 +1,20 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+ Copyright 2006 Diego Pettenò
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,7 +26,6 @@
#endif
#include <stdio.h>
-#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
@@ -33,20 +34,26 @@
#include <sys/types.h>
#include <sys/stat.h>
-#include <pulse/xmalloc.h>
-
#include <pulsecore/core-error.h>
#include <pulsecore/module.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
#include <pulsecore/core-util.h>
+#include <pulsecore/macro.h>
#include "module-detect-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("just-one=<boolean>")
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE("just-one=<boolean>");
+PA_MODULE_DEPRECATED("Please use module-udev-detect instead of module-detect!");
+
+static const char* const valid_modargs[] = {
+ "just-one",
+ NULL
+};
#ifdef HAVE_ALSA
@@ -54,11 +61,11 @@ static int detect_alsa(pa_core *c, int just_one) {
FILE *f;
int n = 0, n_sink = 0, n_source = 0;
- if (!(f = fopen("/proc/asound/devices", "r"))) {
+ if (!(f = pa_fopen_cloexec("/proc/asound/devices", "r"))) {
if (errno != ENOENT)
- pa_log_error(__FILE__": open(\"/proc/asound/devices\") failed: %s", pa_cstrerror(errno));
-
+ pa_log_error("open(\"/proc/asound/devices\") failed: %s", pa_cstrerror(errno));
+
return -1;
}
@@ -66,7 +73,7 @@ static int detect_alsa(pa_core *c, int just_one) {
char line[64], args[64];
unsigned device, subdevice;
int is_sink;
-
+
if (!fgets(line, sizeof(line), f))
break;
@@ -81,7 +88,7 @@ static int detect_alsa(pa_core *c, int just_one) {
if (just_one && is_sink && n_sink >= 1)
continue;
-
+
if (just_one && !is_sink && n_source >= 1)
continue;
@@ -92,7 +99,7 @@ static int detect_alsa(pa_core *c, int just_one) {
if (subdevice != 0)
continue;
- snprintf(args, sizeof(args), "device=hw:%u", device);
+ pa_snprintf(args, sizeof(args), "device_id=%u", device);
if (!pa_module_load(c, is_sink ? "module-alsa-sink" : "module-alsa-source", args))
continue;
@@ -105,22 +112,22 @@ static int detect_alsa(pa_core *c, int just_one) {
}
fclose(f);
-
+
return n;
}
#endif
-#ifdef HAVE_OSS
+#ifdef HAVE_OSS_OUTPUT
static int detect_oss(pa_core *c, int just_one) {
FILE *f;
int n = 0, b = 0;
-
- if (!(f = fopen("/dev/sndstat", "r")) &&
- !(f = fopen("/proc/sndstat", "r")) &&
- !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
+
+ if (!(f = pa_fopen_cloexec("/dev/sndstat", "r")) &&
+ !(f = pa_fopen_cloexec("/proc/sndstat", "r")) &&
+ !(f = pa_fopen_cloexec("/proc/asound/oss/sndstat", "r"))) {
if (errno != ENOENT)
- pa_log_error(__FILE__": failed to open OSS sndstat device: %s", pa_cstrerror(errno));
+ pa_log_error("failed to open OSS sndstat device: %s", pa_cstrerror(errno));
return -1;
}
@@ -128,36 +135,36 @@ static int detect_oss(pa_core *c, int just_one) {
while (!feof(f)) {
char line[64], args[64];
unsigned device;
-
+
if (!fgets(line, sizeof(line), f))
break;
line[strcspn(line, "\r\n")] = 0;
if (!b) {
- b = strcmp(line, "Audio devices:") == 0 || strcmp(line, "Installed devices:") == 0;
+ b = strcmp(line, "Audio devices:") == 0 || strcmp(line, "Installed devices:") == 0;
continue;
}
if (line[0] == 0)
break;
-
+
if (sscanf(line, "%u: ", &device) == 1) {
if (device == 0)
- snprintf(args, sizeof(args), "device=/dev/dsp");
+ pa_snprintf(args, sizeof(args), "device=/dev/dsp");
else
- snprintf(args, sizeof(args), "device=/dev/dsp%u", device);
-
+ pa_snprintf(args, sizeof(args), "device=/dev/dsp%u", device);
+
if (!pa_module_load(c, "module-oss", args))
continue;
-
- } else if (sscanf(line, "pcm%u: ", &device) == 1) {
+
+ } else if (sscanf(line, "pcm%u: ", &device) == 1) {
/* FreeBSD support, the devices are named /dev/dsp0.0, dsp0.1 and so on */
- snprintf(args, sizeof(args), "device=/dev/dsp%u.0", device);
-
+ pa_snprintf(args, sizeof(args), "device=/dev/dsp%u.0", device);
+
if (!pa_module_load(c, "module-oss", args))
continue;
- }
+ }
n++;
@@ -182,14 +189,14 @@ static int detect_solaris(pa_core *c, int just_one) {
if (stat(dev, &s) < 0) {
if (errno != ENOENT)
- pa_log_error(__FILE__": failed to open device %s: %s", dev, pa_cstrerror(errno));
+ pa_log_error("failed to open device %s: %s", dev, pa_cstrerror(errno));
return -1;
}
if (!S_ISCHR(s.st_mode))
return 0;
- snprintf(args, sizeof(args), "device=%s", dev);
+ pa_snprintf(args, sizeof(args), "device=%s", dev);
if (!pa_module_load(c, "module-solaris", args))
return 0;
@@ -211,49 +218,44 @@ static int detect_waveout(pa_core *c, int just_one) {
}
#endif
-int pa__init(pa_core *c, pa_module*m) {
- int just_one = 0, n = 0;
+int pa__init(pa_module*m) {
+ pa_bool_t just_one = FALSE;
+ int n = 0;
pa_modargs *ma;
- static const char* const valid_modargs[] = {
- "just-one",
- NULL
- };
-
- assert(c);
- assert(m);
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": Failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto fail;
}
-
+
if (pa_modargs_get_value_boolean(ma, "just-one", &just_one) < 0) {
- pa_log(__FILE__": just_one= expects a boolean argument.");
+ pa_log("just_one= expects a boolean argument.");
goto fail;
}
-#if HAVE_ALSA
- if ((n = detect_alsa(c, just_one)) <= 0)
+#ifdef HAVE_ALSA
+ if ((n = detect_alsa(m->core, just_one)) <= 0)
#endif
-#if HAVE_OSS
- if ((n = detect_oss(c, just_one)) <= 0)
+#ifdef HAVE_OSS_OUTPUT
+ if ((n = detect_oss(m->core, just_one)) <= 0)
#endif
-#if HAVE_SOLARIS
- if ((n = detect_solaris(c, just_one)) <= 0)
+#ifdef HAVE_SOLARIS
+ if ((n = detect_solaris(m->core, just_one)) <= 0)
#endif
-#if OS_IS_WIN32
- if ((n = detect_waveout(c, just_one)) <= 0)
+#ifdef OS_IS_WIN32
+ if ((n = detect_waveout(m->core, just_one)) <= 0)
#endif
{
- pa_log_warn(__FILE__": failed to detect any sound hardware.");
+ pa_log_warn("failed to detect any sound hardware.");
goto fail;
}
- pa_log_info(__FILE__": loaded %i modules.", n);
-
+ pa_log_info("loaded %i modules.", n);
+
/* We were successful and can unload ourselves now. */
- pa_module_unload_request(m);
+ pa_module_unload_request(m, TRUE);
pa_modargs_free(ma);
@@ -262,12 +264,6 @@ int pa__init(pa_core *c, pa_module*m) {
fail:
if (ma)
pa_modargs_free(ma);
-
- return -1;
-}
-
-void pa__done(PA_GCC_UNUSED pa_core *c, PA_GCC_UNUSED pa_module*m) {
- /* NOP */
+ return -1;
}
-
diff --git a/src/modules/module-device-manager.c b/src/modules/module-device-manager.c
new file mode 100644
index 00000000..67baef31
--- /dev/null
+++ b/src/modules/module-device-manager.c
@@ -0,0 +1,1700 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006-2008 Lennart Poettering
+ Copyright 2009 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pulse/gccmacro.h>
+#include <pulse/xmalloc.h>
+#include <pulse/timeval.h>
+#include <pulse/rtclock.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-subscribe.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/source-output.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/protocol-native.h>
+#include <pulsecore/pstream.h>
+#include <pulsecore/pstream-util.h>
+#include <pulsecore/database.h>
+#include <pulsecore/tagstruct.h>
+
+#include "module-device-manager-symdef.h"
+
+PA_MODULE_AUTHOR("Colin Guthrie");
+PA_MODULE_DESCRIPTION("Keep track of devices (and their descriptions) both past and present and prioritise by role");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(
+ "do_routing=<Automatically route streams based on a priority list (unique per-role)?> "
+ "on_hotplug=<When new device becomes available, recheck streams?> "
+ "on_rescue=<When device becomes unavailable, recheck streams?>");
+
+#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
+#define DUMP_DATABASE
+
+static const char* const valid_modargs[] = {
+ "do_routing",
+ "on_hotplug",
+ "on_rescue",
+ NULL
+};
+
+#define NUM_ROLES 9
+enum {
+ ROLE_NONE,
+ ROLE_VIDEO,
+ ROLE_MUSIC,
+ ROLE_GAME,
+ ROLE_EVENT,
+ ROLE_PHONE,
+ ROLE_ANIMATION,
+ ROLE_PRODUCTION,
+ ROLE_A11Y,
+ ROLE_MAX
+};
+
+typedef uint32_t role_indexes_t[NUM_ROLES];
+
+static const char* role_names[NUM_ROLES] = {
+ "none",
+ "video",
+ "music",
+ "game",
+ "event",
+ "phone",
+ "animation",
+ "production",
+ "a11y",
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_subscription *subscription;
+ pa_hook_slot
+ *sink_new_hook_slot,
+ *source_new_hook_slot,
+ *sink_input_new_hook_slot,
+ *source_output_new_hook_slot,
+ *sink_put_hook_slot,
+ *source_put_hook_slot,
+ *sink_unlink_hook_slot,
+ *source_unlink_hook_slot,
+ *connection_unlink_hook_slot;
+ pa_time_event *save_time_event;
+ pa_database *database;
+
+ pa_native_protocol *protocol;
+ pa_idxset *subscribed;
+
+ pa_bool_t on_hotplug;
+ pa_bool_t on_rescue;
+ pa_bool_t do_routing;
+
+ role_indexes_t preferred_sinks;
+ role_indexes_t preferred_sources;
+};
+
+#define ENTRY_VERSION 1
+
+struct entry {
+ uint8_t version;
+ char *description;
+ pa_bool_t user_set_description;
+ char *icon;
+ role_indexes_t priority;
+};
+
+enum {
+ SUBCOMMAND_TEST,
+ SUBCOMMAND_READ,
+ SUBCOMMAND_RENAME,
+ SUBCOMMAND_DELETE,
+ SUBCOMMAND_ROLE_DEVICE_PRIORITY_ROUTING,
+ SUBCOMMAND_REORDER,
+ SUBCOMMAND_SUBSCRIBE,
+ SUBCOMMAND_EVENT
+};
+
+
+/* Forward declarations */
+#ifdef DUMP_DATABASE
+static void dump_database(struct userdata *);
+#endif
+static void notify_subscribers(struct userdata *);
+
+
+static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(a);
+ pa_assert(e);
+ pa_assert(u);
+
+ pa_assert(e == u->save_time_event);
+ u->core->mainloop->time_free(u->save_time_event);
+ u->save_time_event = NULL;
+
+ pa_database_sync(u->database);
+ pa_log_info("Synced.");
+
+#ifdef DUMP_DATABASE
+ dump_database(u);
+#endif
+}
+
+static void trigger_save(struct userdata *u) {
+
+ pa_assert(u);
+
+ notify_subscribers(u);
+
+ if (u->save_time_event)
+ return;
+
+ u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
+}
+
+static struct entry* entry_new(void) {
+ struct entry *r = pa_xnew0(struct entry, 1);
+ r->version = ENTRY_VERSION;
+ return r;
+}
+
+static void entry_free(struct entry* e) {
+ pa_assert(e);
+
+ pa_xfree(e->description);
+ pa_xfree(e->icon);
+}
+
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) {
+ pa_tagstruct *t;
+ pa_datum key, data;
+ pa_bool_t r;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu8(t, e->version);
+ pa_tagstruct_puts(t, e->description);
+ pa_tagstruct_put_boolean(t, e->user_set_description);
+ pa_tagstruct_puts(t, e->icon);
+ for (uint8_t i=0; i<ROLE_MAX; ++i)
+ pa_tagstruct_putu32(t, e->priority[i]);
+
+ key.data = (char *) name;
+ key.size = strlen(name);
+
+ data.data = (void*)pa_tagstruct_data(t, &data.size);
+
+ r = (pa_database_set(u->database, &key, &data, TRUE) == 0);
+
+ pa_tagstruct_free(t);
+
+ return r;
+}
+
+#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
+
+#define LEGACY_ENTRY_VERSION 1
+static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) {
+ struct legacy_entry {
+ uint8_t version;
+ char description[PA_NAME_MAX];
+ pa_bool_t user_set_description;
+ char icon[PA_NAME_MAX];
+ role_indexes_t priority;
+ } PA_GCC_PACKED;
+ struct legacy_entry *le;
+ struct entry *e;
+
+ pa_assert(u);
+ pa_assert(data);
+
+ if (data->size != sizeof(struct legacy_entry)) {
+ pa_log_debug("Size does not match.");
+ return NULL;
+ }
+
+ le = (struct legacy_entry*)data->data;
+
+ if (le->version != LEGACY_ENTRY_VERSION) {
+ pa_log_debug("Version mismatch.");
+ return NULL;
+ }
+
+ if (!memchr(le->description, 0, sizeof(le->description))) {
+ pa_log_warn("Description has missing NUL byte.");
+ return NULL;
+ }
+
+ if (!memchr(le->icon, 0, sizeof(le->icon))) {
+ pa_log_warn("Icon has missing NUL byte.");
+ return NULL;
+ }
+
+ e = entry_new();
+ e->description = pa_xstrdup(le->description);
+ e->icon = pa_xstrdup(le->icon);
+ return e;
+}
+#endif
+
+static struct entry* entry_read(struct userdata *u, const char *name) {
+ pa_datum key, data;
+ struct entry *e = NULL;
+ pa_tagstruct *t = NULL;
+ const char *description, *icon;
+
+ pa_assert(u);
+ pa_assert(name);
+
+ key.data = (char*) name;
+ key.size = strlen(name);
+
+ pa_zero(data);
+
+ if (!pa_database_get(u->database, &key, &data))
+ goto fail;
+
+ t = pa_tagstruct_new(data.data, data.size);
+ e = entry_new();
+
+ if (pa_tagstruct_getu8(t, &e->version) < 0 ||
+ e->version > ENTRY_VERSION ||
+ pa_tagstruct_gets(t, &description) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->user_set_description) < 0 ||
+ pa_tagstruct_gets(t, &icon) < 0) {
+
+ goto fail;
+ }
+
+ e->description = pa_xstrdup(description);
+ e->icon = pa_xstrdup(icon);
+
+ for (uint8_t i=0; i<ROLE_MAX; ++i) {
+ if (pa_tagstruct_getu32(t, &e->priority[i]) < 0)
+ goto fail;
+ }
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ pa_tagstruct_free(t);
+ pa_datum_free(&data);
+
+ return e;
+
+fail:
+ pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
+
+ if (e)
+ entry_free(e);
+ if (t)
+ pa_tagstruct_free(t);
+
+#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
+ pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name);
+ if ((e = legacy_entry_read(u, &data))) {
+ pa_log_debug("Success. Saving new format for key: %s", name);
+ if (entry_write(u, name, e))
+ trigger_save(u);
+ pa_datum_free(&data);
+ return e;
+ } else
+ pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name);
+#endif
+
+ pa_datum_free(&data);
+ return NULL;
+}
+
+#ifdef DUMP_DATABASE
+static void dump_database_helper(struct userdata *u, uint32_t role_index, const char* human, pa_bool_t sink_mode) {
+ pa_assert(u);
+ pa_assert(human);
+
+ if (sink_mode) {
+ pa_sink *s;
+ if (PA_INVALID_INDEX != u->preferred_sinks[role_index] && (s = pa_idxset_get_by_index(u->core->sinks, u->preferred_sinks[role_index])))
+ pa_log_debug(" %s %s (%s)", human, pa_strnull(pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION)), s->name);
+ else
+ pa_log_debug(" %s No sink specified", human);
+ } else {
+ pa_source *s;
+ if (PA_INVALID_INDEX != u->preferred_sources[role_index] && (s = pa_idxset_get_by_index(u->core->sources, u->preferred_sources[role_index])))
+ pa_log_debug(" %s %s (%s)", human, pa_strnull(pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION)), s->name);
+ else
+ pa_log_debug(" %s No source specified", human);
+ }
+}
+
+static void dump_database(struct userdata *u) {
+ pa_datum key;
+ pa_bool_t done;
+
+ pa_assert(u);
+
+ done = !pa_database_first(u->database, &key, NULL);
+
+ pa_log_debug("Dumping database");
+ while (!done) {
+ char *name;
+ struct entry *e;
+ pa_datum next_key;
+
+ done = !pa_database_next(u->database, &key, &next_key, NULL);
+
+ name = pa_xstrndup(key.data, key.size);
+
+ if ((e = entry_read(u, name))) {
+ pa_log_debug(" Got entry: %s", name);
+ pa_log_debug(" Description: %s", e->description);
+ pa_log_debug(" Priorities: None: %3u, Video: %3u, Music: %3u, Game: %3u, Event: %3u",
+ e->priority[ROLE_NONE], e->priority[ROLE_VIDEO], e->priority[ROLE_MUSIC], e->priority[ROLE_GAME], e->priority[ROLE_EVENT]);
+ pa_log_debug(" Phone: %3u, Anim: %3u, Prodtn: %3u, A11y: %3u",
+ e->priority[ROLE_PHONE], e->priority[ROLE_ANIMATION], e->priority[ROLE_PRODUCTION], e->priority[ROLE_A11Y]);
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ pa_datum_free(&key);
+ key = next_key;
+ }
+
+ if (u->do_routing) {
+ pa_log_debug(" Highest priority devices per-role:");
+
+ pa_log_debug(" Sinks:");
+ for (uint32_t role = ROLE_NONE; role < NUM_ROLES; ++role) {
+ char name[13];
+ uint32_t len = PA_MIN(12u, strlen(role_names[role]));
+ strncpy(name, role_names[role], len);
+ for (int i = len+1; i < 12; ++i) name[i] = ' ';
+ name[len] = ':'; name[0] -= 32; name[12] = '\0';
+ dump_database_helper(u, role, name, TRUE);
+ }
+
+ pa_log_debug(" Sources:");
+ for (uint32_t role = ROLE_NONE; role < NUM_ROLES; ++role) {
+ char name[13];
+ uint32_t len = PA_MIN(12u, strlen(role_names[role]));
+ strncpy(name, role_names[role], len);
+ for (int i = len+1; i < 12; ++i) name[i] = ' ';
+ name[len] = ':'; name[0] -= 32; name[12] = '\0';
+ dump_database_helper(u, role, name, FALSE);
+ }
+ }
+
+ pa_log_debug("Completed database dump");
+}
+#endif
+
+static void notify_subscribers(struct userdata *u) {
+
+ pa_native_connection *c;
+ uint32_t idx;
+
+ pa_assert(u);
+
+ for (c = pa_idxset_first(u->subscribed, &idx); c; c = pa_idxset_next(u->subscribed, &idx)) {
+ pa_tagstruct *t;
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(t, PA_COMMAND_EXTENSION);
+ pa_tagstruct_putu32(t, 0);
+ pa_tagstruct_putu32(t, u->module->index);
+ pa_tagstruct_puts(t, u->module->name);
+ pa_tagstruct_putu32(t, SUBCOMMAND_EVENT);
+
+ pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), t);
+ }
+}
+
+static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) {
+
+ pa_assert(a);
+ pa_assert(b);
+
+ if (!pa_streq(a->description, b->description)
+ || a->user_set_description != b->user_set_description
+ || !pa_streq(a->icon, b->icon))
+ return FALSE;
+
+ for (int i=0; i < NUM_ROLES; ++i)
+ if (a->priority[i] != b->priority[i])
+ return FALSE;
+
+ return TRUE;
+}
+
+static char *get_name(const char *key, const char *prefix) {
+ char *t;
+
+ if (strncmp(key, prefix, strlen(prefix)))
+ return NULL;
+
+ t = pa_xstrdup(key + strlen(prefix));
+ return t;
+}
+
+static inline struct entry *load_or_initialize_entry(struct userdata *u, struct entry *entry, const char *name, const char *prefix) {
+ struct entry *old;
+
+ pa_assert(u);
+ pa_assert(entry);
+ pa_assert(name);
+ pa_assert(prefix);
+
+ if ((old = entry_read(u, name))) {
+ *entry = *old;
+ entry->description = pa_xstrdup(old->description);
+ entry->icon = pa_xstrdup(old->icon);
+ } else {
+ /* This is a new device, so make sure we write it's priority list correctly */
+ role_indexes_t max_priority;
+ pa_datum key;
+ pa_bool_t done;
+
+ pa_zero(max_priority);
+ done = !pa_database_first(u->database, &key, NULL);
+
+ /* Find all existing devices with the same prefix so we calculate the current max priority for each role */
+ while (!done) {
+ pa_datum next_key;
+
+ done = !pa_database_next(u->database, &key, &next_key, NULL);
+
+ if (key.size > strlen(prefix) && strncmp(key.data, prefix, strlen(prefix)) == 0) {
+ char *name2;
+ struct entry *e;
+
+ name2 = pa_xstrndup(key.data, key.size);
+
+ if ((e = entry_read(u, name2))) {
+ for (uint32_t i = 0; i < NUM_ROLES; ++i) {
+ max_priority[i] = PA_MAX(max_priority[i], e->priority[i]);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name2);
+ }
+ pa_datum_free(&key);
+ key = next_key;
+ }
+
+ /* Actually initialise our entry now we've calculated it */
+ for (uint32_t i = 0; i < NUM_ROLES; ++i) {
+ entry->priority[i] = max_priority[i] + 1;
+ }
+ entry->user_set_description = FALSE;
+ }
+
+ return old;
+}
+
+static uint32_t get_role_index(const char* role) {
+ pa_assert(role);
+
+ for (uint32_t i = ROLE_NONE; i < NUM_ROLES; ++i)
+ if (strcmp(role, role_names[i]) == 0)
+ return i;
+
+ return PA_INVALID_INDEX;
+}
+
+static void update_highest_priority_device_indexes(struct userdata *u, const char *prefix, void *ignore_device) {
+ role_indexes_t *indexes, highest_priority_available;
+ pa_datum key;
+ pa_bool_t done, sink_mode;
+
+ pa_assert(u);
+ pa_assert(prefix);
+
+ sink_mode = (strcmp(prefix, "sink:") == 0);
+
+ if (sink_mode)
+ indexes = &u->preferred_sinks;
+ else
+ indexes = &u->preferred_sources;
+
+ for (uint32_t i = 0; i < NUM_ROLES; ++i) {
+ (*indexes)[i] = PA_INVALID_INDEX;
+ }
+ pa_zero(highest_priority_available);
+
+ done = !pa_database_first(u->database, &key, NULL);
+
+ /* Find all existing devices with the same prefix so we find the highest priority device for each role */
+ while (!done) {
+ pa_datum next_key;
+
+ done = !pa_database_next(u->database, &key, &next_key, NULL);
+
+ if (key.size > strlen(prefix) && strncmp(key.data, prefix, strlen(prefix)) == 0) {
+ char *name, *device_name;
+ struct entry *e;
+
+ name = pa_xstrndup(key.data, key.size);
+ device_name = get_name(name, prefix);
+
+ if ((e = entry_read(u, name))) {
+ for (uint32_t i = 0; i < NUM_ROLES; ++i) {
+ if (!highest_priority_available[i] || e->priority[i] < highest_priority_available[i]) {
+ /* We've found a device with a higher priority than that we've currently got,
+ so see if it is currently available or not and update our list */
+ uint32_t idx;
+ pa_bool_t found = FALSE;
+
+ if (sink_mode) {
+ pa_sink *sink;
+
+ PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
+ if ((pa_sink*) ignore_device == sink)
+ continue;
+ if (strcmp(sink->name, device_name) == 0) {
+ found = TRUE;
+ idx = sink->index; /* Is this needed? */
+ break;
+ }
+ }
+ } else {
+ pa_source *source;
+
+ PA_IDXSET_FOREACH(source, u->core->sources, idx) {
+ if ((pa_source*) ignore_device == source)
+ continue;
+ if (strcmp(source->name, device_name) == 0) {
+ found = TRUE;
+ idx = source->index; /* Is this needed? */
+ break;
+ }
+ }
+ }
+ if (found) {
+ highest_priority_available[i] = e->priority[i];
+ (*indexes)[i] = idx;
+ }
+
+ }
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+ pa_xfree(device_name);
+ }
+
+ pa_datum_free(&key);
+ key = next_key;
+ }
+}
+
+
+static void route_sink_input(struct userdata *u, pa_sink_input *si) {
+ const char *role;
+ uint32_t role_index, device_index;
+ pa_sink *sink;
+
+ pa_assert(u);
+ pa_assert(u->do_routing);
+
+ if (si->save_sink)
+ return;
+
+ /* Skip this if it is already in the process of being moved anyway */
+ if (!si->sink)
+ return;
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
+ return;
+
+ if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
+ role_index = get_role_index("none");
+ else
+ role_index = get_role_index(role);
+
+ if (PA_INVALID_INDEX == role_index)
+ return;
+
+ device_index = u->preferred_sinks[role_index];
+ if (PA_INVALID_INDEX == device_index)
+ return;
+
+ if (!(sink = pa_idxset_get_by_index(u->core->sinks, device_index)))
+ return;
+
+ if (si->sink != sink)
+ pa_sink_input_move_to(si, sink, FALSE);
+}
+
+static pa_hook_result_t route_sink_inputs(struct userdata *u, pa_sink *ignore_sink) {
+ pa_sink_input *si;
+ uint32_t idx;
+
+ pa_assert(u);
+
+ if (!u->do_routing)
+ return PA_HOOK_OK;
+
+ update_highest_priority_device_indexes(u, "sink:", ignore_sink);
+
+ PA_IDXSET_FOREACH(si, u->core->sink_inputs, idx) {
+ route_sink_input(u, si);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static void route_source_output(struct userdata *u, pa_source_output *so) {
+ const char *role;
+ uint32_t role_index, device_index;
+ pa_source *source;
+
+ pa_assert(u);
+ pa_assert(u->do_routing);
+
+ if (so->save_source)
+ return;
+
+ if (so->direct_on_input)
+ return;
+
+ /* Skip this if it is already in the process of being moved anyway */
+ if (!so->source)
+ return;
+
+ /* It might happen that a stream and a source are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
+ return;
+
+ if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
+ role_index = get_role_index("none");
+ else
+ role_index = get_role_index(role);
+
+ if (PA_INVALID_INDEX == role_index)
+ return;
+
+ device_index = u->preferred_sources[role_index];
+ if (PA_INVALID_INDEX == device_index)
+ return;
+
+ if (!(source = pa_idxset_get_by_index(u->core->sources, device_index)))
+ return;
+
+ if (so->source != source)
+ pa_source_output_move_to(so, source, FALSE);
+}
+
+static pa_hook_result_t route_source_outputs(struct userdata *u, pa_source* ignore_source) {
+ pa_source_output *so;
+ uint32_t idx;
+
+ pa_assert(u);
+
+ if (!u->do_routing)
+ return PA_HOOK_OK;
+
+ update_highest_priority_device_indexes(u, "source:", ignore_source);
+
+ PA_IDXSET_FOREACH(so, u->core->source_outputs, idx) {
+ route_source_output(u, so);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ struct userdata *u = userdata;
+ struct entry *entry, *old = NULL;
+ char *name = NULL;
+
+ pa_assert(c);
+ pa_assert(u);
+
+ if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
+ t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
+ t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
+ t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE) &&
+
+ /*t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&*/
+ t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE) &&
+ /*t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW) &&*/
+ t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
+ return;
+
+ entry = entry_new();
+
+ if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
+ pa_sink_input *si;
+
+ if (!u->do_routing)
+ return;
+ if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
+ return;
+
+ /* The role may change mid-stream, so we reroute */
+ route_sink_input(u, si);
+
+ return;
+ } else if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT) {
+ pa_source_output *so;
+
+ if (!u->do_routing)
+ return;
+ if (!(so = pa_idxset_get_by_index(c->source_outputs, idx)))
+ return;
+
+ /* The role may change mid-stream, so we reroute */
+ route_source_output(u, so);
+
+ return;
+ } else if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
+ pa_sink *sink;
+
+ if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
+ return;
+
+ name = pa_sprintf_malloc("sink:%s", sink->name);
+
+ old = load_or_initialize_entry(u, entry, name, "sink:");
+
+ if (!entry->user_set_description) {
+ pa_xfree(entry->description);
+ entry->description = pa_xstrdup(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION));
+ } else if (!pa_streq(entry->description, pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION))) {
+ /* Warning: If two modules fight over the description, this could cause an infinite loop.
+ by changing the description here, we retrigger this subscription callback. The only thing stopping us from
+ looping is the fact that the string comparison will fail on the second iteration. If another module tries to manage
+ the description, this will fail... */
+ pa_sink_set_description(sink, entry->description);
+ }
+
+ pa_xfree(entry->icon);
+ entry->icon = pa_xstrdup(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_ICON_NAME));
+
+ } else if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE) {
+ pa_source *source;
+
+ pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
+
+ if (!(source = pa_idxset_get_by_index(c->sources, idx)))
+ return;
+
+ if (source->monitor_of)
+ return;
+
+ name = pa_sprintf_malloc("source:%s", source->name);
+
+ old = load_or_initialize_entry(u, entry, name, "source:");
+
+ if (!entry->user_set_description) {
+ pa_xfree(entry->description);
+ entry->description = pa_xstrdup(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION));
+ } else if (!pa_streq(entry->description, pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION))) {
+ /* Warning: If two modules fight over the description, this could cause an infinite loop.
+ by changing the description here, we retrigger this subscription callback. The only thing stopping us from
+ looping is the fact that the string comparison will fail on the second iteration. If another module tries to manage
+ the description, this will fail... */
+ pa_source_set_description(source, entry->description);
+ }
+
+ pa_xfree(entry->icon);
+ entry->icon = pa_xstrdup(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_ICON_NAME));
+ }
+
+ pa_assert(name);
+
+ if (old) {
+
+ if (entries_equal(old, entry)) {
+ entry_free(old);
+ entry_free(entry);
+ pa_xfree(name);
+
+ return;
+ }
+
+ entry_free(old);
+ }
+
+ pa_log_info("Storing device %s.", name);
+
+ if (entry_write(u, name, entry))
+ trigger_save(u);
+ else
+ pa_log_warn("Could not save device");;
+
+ entry_free(entry);
+ pa_xfree(name);
+}
+
+static pa_hook_result_t sink_new_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
+ char *name;
+ struct entry *e;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+
+ name = pa_sprintf_malloc("sink:%s", new_data->name);
+
+ if ((e = entry_read(u, name))) {
+ if (e->user_set_description && strncmp(e->description, pa_proplist_gets(new_data->proplist, PA_PROP_DEVICE_DESCRIPTION), sizeof(e->description)) != 0) {
+ pa_log_info("Restoring description for sink %s.", new_data->name);
+ pa_proplist_sets(new_data->proplist, PA_PROP_DEVICE_DESCRIPTION, e->description);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_new_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
+ char *name;
+ struct entry *e;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+
+ name = pa_sprintf_malloc("source:%s", new_data->name);
+
+ if ((e = entry_read(u, name))) {
+ if (e->user_set_description && strncmp(e->description, pa_proplist_gets(new_data->proplist, PA_PROP_DEVICE_DESCRIPTION), sizeof(e->description)) != 0) {
+ /* NB, We cannot detect if we are a monitor here... this could mess things up a bit... */
+ pa_log_info("Restoring description for source %s.", new_data->name);
+ pa_proplist_sets(new_data->proplist, PA_PROP_DEVICE_DESCRIPTION, e->description);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
+ const char *role;
+ uint32_t role_index;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+
+ if (!u->do_routing)
+ return PA_HOOK_OK;
+
+ if (new_data->sink)
+ pa_log_debug("Not restoring device for stream because already set.");
+ else {
+ if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
+ role_index = get_role_index("none");
+ else
+ role_index = get_role_index(role);
+
+ if (PA_INVALID_INDEX != role_index) {
+ uint32_t device_index;
+
+ device_index = u->preferred_sinks[role_index];
+ if (PA_INVALID_INDEX != device_index) {
+ pa_sink *sink;
+
+ if ((sink = pa_idxset_get_by_index(u->core->sinks, device_index))) {
+ if (!pa_sink_input_new_data_set_sink(new_data, sink, FALSE))
+ pa_log_debug("Not restoring device for stream because no supported format was found");
+ }
+ }
+ }
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
+ const char *role;
+ uint32_t role_index;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+
+ if (!u->do_routing)
+ return PA_HOOK_OK;
+
+ if (new_data->direct_on_input)
+ return PA_HOOK_OK;
+
+ if (new_data->source)
+ pa_log_debug("Not restoring device for stream because already set.");
+ else {
+ if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
+ role_index = get_role_index("none");
+ else
+ role_index = get_role_index(role);
+
+ if (PA_INVALID_INDEX != role_index) {
+ uint32_t device_index;
+
+ device_index = u->preferred_sources[role_index];
+ if (PA_INVALID_INDEX != device_index) {
+ pa_source *source;
+
+ if ((source = pa_idxset_get_by_index(u->core->sources, device_index)))
+ if (!pa_source_output_new_data_set_source(new_data, source, FALSE))
+ pa_log_debug("Not restoring device for stream because no supported format was found");
+ }
+ }
+ }
+
+ return PA_HOOK_OK;
+}
+
+
+static pa_hook_result_t sink_put_hook_callback(pa_core *c, PA_GCC_UNUSED pa_sink *sink, struct userdata *u) {
+ pa_assert(c);
+ pa_assert(u);
+ pa_assert(u->core == c);
+ pa_assert(u->on_hotplug);
+
+ notify_subscribers(u);
+
+ return route_sink_inputs(u, NULL);
+}
+
+static pa_hook_result_t source_put_hook_callback(pa_core *c, PA_GCC_UNUSED pa_source *source, struct userdata *u) {
+ pa_assert(c);
+ pa_assert(u);
+ pa_assert(u->core == c);
+ pa_assert(u->on_hotplug);
+
+ notify_subscribers(u);
+
+ return route_source_outputs(u, NULL);
+}
+
+static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
+ pa_assert(c);
+ pa_assert(sink);
+ pa_assert(u);
+ pa_assert(u->core == c);
+ pa_assert(u->on_rescue);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ notify_subscribers(u);
+
+ return route_sink_inputs(u, sink);
+}
+
+static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
+ pa_assert(c);
+ pa_assert(source);
+ pa_assert(u);
+ pa_assert(u->core == c);
+ pa_assert(u->on_rescue);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ notify_subscribers(u);
+
+ return route_source_outputs(u, source);
+}
+
+
+static void apply_entry(struct userdata *u, const char *name, struct entry *e) {
+ uint32_t idx;
+ char *n;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ if (!e->user_set_description)
+ return;
+
+ if ((n = get_name(name, "sink:"))) {
+ pa_sink *s;
+ PA_IDXSET_FOREACH(s, u->core->sinks, idx) {
+ if (!pa_streq(s->name, n)) {
+ continue;
+ }
+
+ pa_log_info("Setting description for sink %s to '%s'", s->name, e->description);
+ pa_sink_set_description(s, e->description);
+ }
+ pa_xfree(n);
+ }
+ else if ((n = get_name(name, "source:"))) {
+ pa_source *s;
+ PA_IDXSET_FOREACH(s, u->core->sources, idx) {
+ if (!pa_streq(s->name, n)) {
+ continue;
+ }
+
+ if (s->monitor_of) {
+ pa_log_warn("Cowardly refusing to set the description for monitor source %s.", s->name);
+ continue;
+ }
+
+ pa_log_info("Setting description for source %s to '%s'", s->name, e->description);
+ pa_source_set_description(s, e->description);
+ }
+ pa_xfree(n);
+ }
+}
+
+
+#define EXT_VERSION 1
+
+static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connection *c, uint32_t tag, pa_tagstruct *t) {
+ struct userdata *u;
+ uint32_t command;
+ pa_tagstruct *reply = NULL;
+
+ pa_assert(p);
+ pa_assert(m);
+ pa_assert(c);
+ pa_assert(t);
+
+ u = m->userdata;
+
+ if (pa_tagstruct_getu32(t, &command) < 0)
+ goto fail;
+
+ reply = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
+ pa_tagstruct_putu32(reply, tag);
+
+ switch (command) {
+ case SUBCOMMAND_TEST: {
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ pa_tagstruct_putu32(reply, EXT_VERSION);
+ break;
+ }
+
+ case SUBCOMMAND_READ: {
+ pa_datum key;
+ pa_bool_t done;
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ done = !pa_database_first(u->database, &key, NULL);
+
+ while (!done) {
+ pa_datum next_key;
+ struct entry *e;
+ char *name;
+
+ done = !pa_database_next(u->database, &key, &next_key, NULL);
+
+ name = pa_xstrndup(key.data, key.size);
+ pa_datum_free(&key);
+
+ if ((e = entry_read(u, name))) {
+ uint32_t idx;
+ char *device_name;
+ uint32_t found_index = PA_INVALID_INDEX;
+
+ if ((device_name = get_name(name, "sink:"))) {
+ pa_sink* s;
+ PA_IDXSET_FOREACH(s, u->core->sinks, idx) {
+ if (strcmp(s->name, device_name) == 0) {
+ found_index = s->index;
+ break;
+ }
+ }
+ pa_xfree(device_name);
+ } else if ((device_name = get_name(name, "source:"))) {
+ pa_source* s;
+ PA_IDXSET_FOREACH(s, u->core->sources, idx) {
+ if (strcmp(s->name, device_name) == 0) {
+ found_index = s->index;
+ break;
+ }
+ }
+ pa_xfree(device_name);
+ }
+
+ pa_tagstruct_puts(reply, name);
+ pa_tagstruct_puts(reply, e->description);
+ pa_tagstruct_puts(reply, e->icon);
+ pa_tagstruct_putu32(reply, found_index);
+ pa_tagstruct_putu32(reply, NUM_ROLES);
+
+ for (uint32_t i = ROLE_NONE; i < NUM_ROLES; ++i) {
+ pa_tagstruct_puts(reply, role_names[i]);
+ pa_tagstruct_putu32(reply, e->priority[i]);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ key = next_key;
+ }
+
+ break;
+ }
+
+ case SUBCOMMAND_RENAME: {
+
+ struct entry *e;
+ const char *device, *description;
+
+ if (pa_tagstruct_gets(t, &device) < 0 ||
+ pa_tagstruct_gets(t, &description) < 0)
+ goto fail;
+
+ if (!device || !*device || !description || !*description)
+ goto fail;
+
+ if ((e = entry_read(u, device))) {
+ pa_xfree(e->description);
+ e->description = pa_xstrdup(description);
+ e->user_set_description = TRUE;
+
+ if (entry_write(u, (char *)device, e)) {
+ apply_entry(u, device, e);
+
+ trigger_save(u);
+ }
+ else
+ pa_log_warn("Could not save device");
+
+ entry_free(e);
+ }
+ else
+ pa_log_warn("Could not rename device %s, no entry in database", device);
+
+ break;
+ }
+
+ case SUBCOMMAND_DELETE:
+
+ while (!pa_tagstruct_eof(t)) {
+ const char *name;
+ pa_datum key;
+
+ if (pa_tagstruct_gets(t, &name) < 0)
+ goto fail;
+
+ key.data = (char*) name;
+ key.size = strlen(name);
+
+ /** @todo: Reindex the priorities */
+ pa_database_unset(u->database, &key);
+ }
+
+ trigger_save(u);
+
+ break;
+
+ case SUBCOMMAND_ROLE_DEVICE_PRIORITY_ROUTING: {
+
+ pa_bool_t enable;
+
+ if (pa_tagstruct_get_boolean(t, &enable) < 0)
+ goto fail;
+
+ if ((u->do_routing = enable)) {
+ /* Update our caches */
+ update_highest_priority_device_indexes(u, "sink:", NULL);
+ update_highest_priority_device_indexes(u, "source:", NULL);
+ }
+
+ break;
+ }
+
+ case SUBCOMMAND_REORDER: {
+
+ const char *role;
+ struct entry *e;
+ uint32_t role_index, n_devices;
+ pa_datum key;
+ pa_bool_t done, sink_mode = TRUE;
+ struct device_t { uint32_t prio; char *device; };
+ struct device_t *device;
+ struct device_t **devices;
+ uint32_t i, idx, offset;
+ pa_hashmap *h;
+ /*void *state;*/
+ pa_bool_t first;
+
+ if (pa_tagstruct_gets(t, &role) < 0 ||
+ pa_tagstruct_getu32(t, &n_devices) < 0 ||
+ n_devices < 1)
+ goto fail;
+
+ if (PA_INVALID_INDEX == (role_index = get_role_index(role)))
+ goto fail;
+
+ /* Cycle through the devices given and make sure they exist */
+ h = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ first = TRUE;
+ idx = 0;
+ for (i = 0; i < n_devices; ++i) {
+ const char *s;
+ if (pa_tagstruct_gets(t, &s) < 0) {
+ while ((device = pa_hashmap_steal_first(h))) {
+ pa_xfree(device->device);
+ pa_xfree(device);
+ }
+
+ pa_hashmap_free(h, NULL, NULL);
+ pa_log_error("Protocol error on reorder");
+ goto fail;
+ }
+
+ /* Ensure this is a valid entry */
+ if (!(e = entry_read(u, s))) {
+ while ((device = pa_hashmap_steal_first(h))) {
+ pa_xfree(device->device);
+ pa_xfree(device);
+ }
+
+ pa_hashmap_free(h, NULL, NULL);
+ pa_log_error("Client specified an unknown device in it's reorder list.");
+ goto fail;
+ }
+ entry_free(e);
+
+ if (first) {
+ first = FALSE;
+ sink_mode = (0 == strncmp("sink:", s, 5));
+ } else if ((sink_mode && 0 != strncmp("sink:", s, 5)) || (!sink_mode && 0 != strncmp("source:", s, 7))) {
+ while ((device = pa_hashmap_steal_first(h))) {
+ pa_xfree(device->device);
+ pa_xfree(device);
+ }
+
+ pa_hashmap_free(h, NULL, NULL);
+ pa_log_error("Attempted to reorder mixed devices (sinks and sources)");
+ goto fail;
+ }
+
+ /* Add the device to our hashmap. If it's alredy in it, free it now and carry on */
+ device = pa_xnew(struct device_t, 1);
+ device->device = pa_xstrdup(s);
+ if (pa_hashmap_put(h, device->device, device) == 0) {
+ device->prio = idx;
+ idx++;
+ } else {
+ pa_xfree(device->device);
+ pa_xfree(device);
+ }
+ }
+
+ /*pa_log_debug("Hashmap contents (received from client)");
+ PA_HASHMAP_FOREACH(device, h, state) {
+ pa_log_debug(" - %s (%d)", device->device, device->prio);
+ }*/
+
+ /* Now cycle through our list and add all the devices.
+ This has the effect of addign in any in our DB,
+ not specified in the device list (and thus will be
+ tacked on at the end) */
+ offset = idx;
+ done = !pa_database_first(u->database, &key, NULL);
+
+ while (!done && idx < 256) {
+ pa_datum next_key;
+
+ done = !pa_database_next(u->database, &key, &next_key, NULL);
+
+ device = pa_xnew(struct device_t, 1);
+ device->device = pa_xstrndup(key.data, key.size);
+ if ((sink_mode && 0 == strncmp("sink:", device->device, 5))
+ || (!sink_mode && 0 == strncmp("source:", device->device, 7))) {
+
+ /* Add the device to our hashmap. If it's alredy in it, free it now and carry on */
+ if (pa_hashmap_put(h, device->device, device) == 0
+ && (e = entry_read(u, device->device))) {
+ /* We add offset on to the existing priorirty so that when we order, the
+ existing entries are always lower priority than the new ones. */
+ device->prio = (offset + e->priority[role_index]);
+ pa_xfree(e);
+ }
+ else {
+ pa_xfree(device->device);
+ pa_xfree(device);
+ }
+ } else {
+ pa_xfree(device->device);
+ pa_xfree(device);
+ }
+
+ pa_datum_free(&key);
+
+ key = next_key;
+ }
+
+ /*pa_log_debug("Hashmap contents (combined with database)");
+ PA_HASHMAP_FOREACH(device, h, state) {
+ pa_log_debug(" - %s (%d)", device->device, device->prio);
+ }*/
+
+ /* Now we put all the entries in a simple list for sorting it. */
+ n_devices = pa_hashmap_size(h);
+ devices = pa_xnew(struct device_t *, n_devices);
+ idx = 0;
+ while ((device = pa_hashmap_steal_first(h))) {
+ devices[idx++] = device;
+ }
+ pa_hashmap_free(h, NULL, NULL);
+
+ /* Simple bubble sort */
+ for (i = 0; i < n_devices; ++i) {
+ for (uint32_t j = i; j < n_devices; ++j) {
+ if (devices[i]->prio > devices[j]->prio) {
+ struct device_t *tmp;
+ tmp = devices[i];
+ devices[i] = devices[j];
+ devices[j] = tmp;
+ }
+ }
+ }
+
+ /*pa_log_debug("Sorted device list");
+ for (i = 0; i < n_devices; ++i) {
+ pa_log_debug(" - %s (%d)", devices[i]->device, devices[i]->prio);
+ }*/
+
+ /* Go through in order and write the new entry and cleanup our own list */
+ idx = 1;
+ first = TRUE;
+ for (i = 0; i < n_devices; ++i) {
+ if ((e = entry_read(u, devices[i]->device))) {
+ if (e->priority[role_index] == idx)
+ idx++;
+ else {
+ e->priority[role_index] = idx;
+
+ if (entry_write(u, (char *) devices[i]->device, e)) {
+ first = FALSE;
+ idx++;
+ }
+ }
+
+ pa_xfree(e);
+ }
+ pa_xfree(devices[i]->device);
+ pa_xfree(devices[i]);
+ }
+
+ if (!first) {
+ trigger_save(u);
+
+ if (sink_mode)
+ route_sink_inputs(u, NULL);
+ else
+ route_source_outputs(u, NULL);
+ }
+
+ break;
+ }
+
+ case SUBCOMMAND_SUBSCRIBE: {
+
+ pa_bool_t enabled;
+
+ if (pa_tagstruct_get_boolean(t, &enabled) < 0 ||
+ !pa_tagstruct_eof(t))
+ goto fail;
+
+ if (enabled)
+ pa_idxset_put(u->subscribed, c, NULL);
+ else
+ pa_idxset_remove_by_data(u->subscribed, c, NULL);
+
+ break;
+ }
+
+ default:
+ goto fail;
+ }
+
+ pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), reply);
+ return 0;
+
+ fail:
+
+ if (reply)
+ pa_tagstruct_free(reply);
+
+ return -1;
+}
+
+static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_native_connection *c, struct userdata *u) {
+ pa_assert(p);
+ pa_assert(c);
+ pa_assert(u);
+
+ pa_idxset_remove_by_data(u->subscribed, c, NULL);
+ return PA_HOOK_OK;
+}
+
+struct prioritised_indexes {
+ uint32_t index;
+ int32_t priority;
+};
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+ char *fname;
+ pa_sink *sink;
+ pa_source *source;
+ uint32_t idx;
+ pa_bool_t do_routing = FALSE, on_hotplug = TRUE, on_rescue = TRUE;
+ uint32_t total_devices;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "do_routing", &do_routing) < 0 ||
+ pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
+ pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
+ pa_log("on_hotplug= and on_rescue= expect boolean arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->do_routing = do_routing;
+ u->on_hotplug = on_hotplug;
+ u->on_rescue = on_rescue;
+ u->subscribed = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ u->protocol = pa_native_protocol_get(m->core);
+ pa_native_protocol_install_ext(u->protocol, m, extension_cb);
+
+ u->connection_unlink_hook_slot = pa_hook_connect(&pa_native_protocol_hooks(u->protocol)[PA_NATIVE_HOOK_CONNECTION_UNLINK], PA_HOOK_NORMAL, (pa_hook_cb_t) connection_unlink_hook_cb, u);
+
+ u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE|PA_SUBSCRIPTION_MASK_SINK_INPUT|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, subscribe_callback, u);
+
+ /* Used to handle device description management */
+ u->sink_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) sink_new_hook_callback, u);
+ u->source_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_new_hook_callback, u);
+
+ /* The following slots are used to deal with routing */
+ /* A little bit later than module-stream-restore, but before module-intended-roles */
+ u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY+5, (pa_hook_cb_t) sink_input_new_hook_callback, u);
+ u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY+5, (pa_hook_cb_t) source_output_new_hook_callback, u);
+
+ if (on_hotplug) {
+ /* A little bit later than module-stream-restore, but before module-intended-roles */
+ u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+5, (pa_hook_cb_t) sink_put_hook_callback, u);
+ u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+5, (pa_hook_cb_t) source_put_hook_callback, u);
+ }
+
+ if (on_rescue) {
+ /* A little bit later than module-stream-restore, a little bit earlier than module-intended-roles, module-rescue-streams, ... */
+ u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+5, (pa_hook_cb_t) sink_unlink_hook_callback, u);
+ u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+5, (pa_hook_cb_t) source_unlink_hook_callback, u);
+ }
+
+ if (!(fname = pa_state_path("device-manager", TRUE)))
+ goto fail;
+
+ if (!(u->database = pa_database_open(fname, TRUE))) {
+ pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
+ pa_xfree(fname);
+ goto fail;
+ }
+
+ pa_log_info("Successfully opened database file '%s'.", fname);
+ pa_xfree(fname);
+
+ /* Attempt to inject the devices into the list in priority order */
+ total_devices = PA_MAX(pa_idxset_size(m->core->sinks), pa_idxset_size(m->core->sources));
+ if (total_devices > 0 && total_devices < 128) {
+ uint32_t i;
+ struct prioritised_indexes p_i[128];
+
+ /* We cycle over all the available sinks so that they are added to our database if they are not in it yet */
+ i = 0;
+ PA_IDXSET_FOREACH(sink, m->core->sinks, idx) {
+ pa_log_debug("Found sink index %u", sink->index);
+ p_i[i ].index = sink->index;
+ p_i[i++].priority = sink->priority;
+ }
+ /* Bubble sort it (only really useful for first time creation) */
+ if (i > 1)
+ for (uint32_t j = 0; j < i; ++j)
+ for (uint32_t k = 0; k < i; ++k)
+ if (p_i[j].priority > p_i[k].priority) {
+ struct prioritised_indexes tmp_pi = p_i[k];
+ p_i[k] = p_i[j];
+ p_i[j] = tmp_pi;
+ }
+ /* Register it */
+ for (uint32_t j = 0; j < i; ++j)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, p_i[j].index, u);
+
+
+ /* We cycle over all the available sources so that they are added to our database if they are not in it yet */
+ i = 0;
+ PA_IDXSET_FOREACH(source, m->core->sources, idx) {
+ p_i[i ].index = source->index;
+ p_i[i++].priority = source->priority;
+ }
+ /* Bubble sort it (only really useful for first time creation) */
+ if (i > 1)
+ for (uint32_t j = 0; j < i; ++j)
+ for (uint32_t k = 0; k < i; ++k)
+ if (p_i[j].priority > p_i[k].priority) {
+ struct prioritised_indexes tmp_pi = p_i[k];
+ p_i[k] = p_i[j];
+ p_i[j] = tmp_pi;
+ }
+ /* Register it */
+ for (uint32_t j = 0; j < i; ++j)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, p_i[j].index, u);
+ }
+ else if (total_devices > 0) {
+ /* This user has a *lot* of devices... */
+ PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
+
+ PA_IDXSET_FOREACH(source, m->core->sources, idx)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
+ }
+
+ /* Perform the routing (if it's enabled) which will update our priority list cache too */
+ for (uint32_t i = 0; i < NUM_ROLES; ++i) {
+ u->preferred_sinks[i] = u->preferred_sources[i] = PA_INVALID_INDEX;
+ }
+
+ route_sink_inputs(u, NULL);
+ route_source_outputs(u, NULL);
+
+#ifdef DUMP_DATABASE
+ dump_database(u);
+#endif
+
+ pa_modargs_free(ma);
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->subscription)
+ pa_subscription_free(u->subscription);
+
+ if (u->sink_new_hook_slot)
+ pa_hook_slot_free(u->sink_new_hook_slot);
+ if (u->source_new_hook_slot)
+ pa_hook_slot_free(u->source_new_hook_slot);
+
+ if (u->sink_input_new_hook_slot)
+ pa_hook_slot_free(u->sink_input_new_hook_slot);
+ if (u->source_output_new_hook_slot)
+ pa_hook_slot_free(u->source_output_new_hook_slot);
+
+ if (u->sink_put_hook_slot)
+ pa_hook_slot_free(u->sink_put_hook_slot);
+ if (u->source_put_hook_slot)
+ pa_hook_slot_free(u->source_put_hook_slot);
+
+ if (u->sink_unlink_hook_slot)
+ pa_hook_slot_free(u->sink_unlink_hook_slot);
+ if (u->source_unlink_hook_slot)
+ pa_hook_slot_free(u->source_unlink_hook_slot);
+
+ if (u->connection_unlink_hook_slot)
+ pa_hook_slot_free(u->connection_unlink_hook_slot);
+
+ if (u->save_time_event)
+ u->core->mainloop->time_free(u->save_time_event);
+
+ if (u->database)
+ pa_database_close(u->database);
+
+ if (u->protocol) {
+ pa_native_protocol_remove_ext(u->protocol, m);
+ pa_native_protocol_unref(u->protocol);
+ }
+
+ if (u->subscribed)
+ pa_idxset_free(u->subscribed, NULL, NULL);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-device-restore.c b/src/modules/module-device-restore.c
new file mode 100644
index 00000000..7d94ffa4
--- /dev/null
+++ b/src/modules/module-device-restore.c
@@ -0,0 +1,962 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006-2008 Lennart Poettering
+ Copyright 2011 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pulse/gccmacro.h>
+#include <pulse/xmalloc.h>
+#include <pulse/volume.h>
+#include <pulse/timeval.h>
+#include <pulse/rtclock.h>
+#include <pulse/format.h>
+#include <pulse/internal.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-subscribe.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/source.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/protocol-native.h>
+#include <pulsecore/pstream.h>
+#include <pulsecore/pstream-util.h>
+#include <pulsecore/database.h>
+#include <pulsecore/tagstruct.h>
+
+#include "module-device-restore-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Automatically restore the volume/mute state of devices");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(
+ "restore_port=<Save/restore port?> "
+ "restore_volume=<Save/restore volumes?> "
+ "restore_muted=<Save/restore muted states?>");
+
+#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
+
+static const char* const valid_modargs[] = {
+ "restore_volume",
+ "restore_muted",
+ "restore_port",
+ NULL
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_subscription *subscription;
+ pa_hook_slot
+ *sink_new_hook_slot,
+ *sink_fixate_hook_slot,
+ *source_new_hook_slot,
+ *source_fixate_hook_slot,
+ *connection_unlink_hook_slot;
+ pa_time_event *save_time_event;
+ pa_database *database;
+
+ pa_native_protocol *protocol;
+ pa_idxset *subscribed;
+
+ pa_bool_t restore_volume:1;
+ pa_bool_t restore_muted:1;
+ pa_bool_t restore_port:1;
+};
+
+/* Protocol extention commands */
+enum {
+ SUBCOMMAND_TEST,
+ SUBCOMMAND_SUBSCRIBE,
+ SUBCOMMAND_EVENT,
+ SUBCOMMAND_READ_SINK_FORMATS_ALL,
+ SUBCOMMAND_READ_SINK_FORMATS,
+ SUBCOMMAND_SAVE_SINK_FORMATS
+};
+
+
+#define ENTRY_VERSION 1
+
+struct entry {
+ uint8_t version;
+ pa_bool_t muted_valid, volume_valid, port_valid;
+ pa_bool_t muted;
+ pa_channel_map channel_map;
+ pa_cvolume volume;
+ char *port;
+ pa_idxset *formats;
+};
+
+static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(a);
+ pa_assert(e);
+ pa_assert(u);
+
+ pa_assert(e == u->save_time_event);
+ u->core->mainloop->time_free(u->save_time_event);
+ u->save_time_event = NULL;
+
+ pa_database_sync(u->database);
+ pa_log_info("Synced.");
+}
+
+static void trigger_save(struct userdata *u) {
+ if (u->save_time_event)
+ return;
+
+ u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
+}
+
+static struct entry* entry_new(pa_bool_t add_pcm_format) {
+ struct entry *r = pa_xnew0(struct entry, 1);
+ r->version = ENTRY_VERSION;
+ r->formats = pa_idxset_new(NULL, NULL);
+ if (add_pcm_format) {
+ pa_format_info *f = pa_format_info_new();
+ f->encoding = PA_ENCODING_PCM;
+ pa_idxset_put(r->formats, f, NULL);
+ }
+ return r;
+}
+
+static void entry_free(struct entry* e) {
+ pa_assert(e);
+
+ pa_idxset_free(e->formats, (pa_free2_cb_t) pa_format_info_free2, NULL);
+ pa_xfree(e->port);
+ pa_xfree(e);
+}
+
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) {
+ pa_tagstruct *t;
+ pa_datum key, data;
+ pa_bool_t r;
+ uint32_t i;
+ pa_format_info *f;
+ uint8_t n_formats;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ n_formats = pa_idxset_size(e->formats);
+ pa_assert(n_formats > 0);
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu8(t, e->version);
+ pa_tagstruct_put_boolean(t, e->volume_valid);
+ pa_tagstruct_put_channel_map(t, &e->channel_map);
+ pa_tagstruct_put_cvolume(t, &e->volume);
+ pa_tagstruct_put_boolean(t, e->muted_valid);
+ pa_tagstruct_put_boolean(t, e->muted);
+ pa_tagstruct_put_boolean(t, e->port_valid);
+ pa_tagstruct_puts(t, e->port);
+ pa_tagstruct_putu8(t, n_formats);
+
+ PA_IDXSET_FOREACH(f, e->formats, i) {
+ pa_tagstruct_put_format_info(t, f);
+ }
+
+ key.data = (char *) name;
+ key.size = strlen(name);
+
+ data.data = (void*)pa_tagstruct_data(t, &data.size);
+
+ r = (pa_database_set(u->database, &key, &data, TRUE) == 0);
+
+ pa_tagstruct_free(t);
+
+ return r;
+}
+
+#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
+
+#define LEGACY_ENTRY_VERSION 2
+static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) {
+ struct legacy_entry {
+ uint8_t version;
+ pa_bool_t muted_valid:1, volume_valid:1, port_valid:1;
+ pa_bool_t muted:1;
+ pa_channel_map channel_map;
+ pa_cvolume volume;
+ char port[PA_NAME_MAX];
+ } PA_GCC_PACKED;
+ struct legacy_entry *le;
+ struct entry *e;
+
+ pa_assert(u);
+ pa_assert(data);
+
+ if (data->size != sizeof(struct legacy_entry)) {
+ pa_log_debug("Size does not match.");
+ return NULL;
+ }
+
+ le = (struct legacy_entry*)data->data;
+
+ if (le->version != LEGACY_ENTRY_VERSION) {
+ pa_log_debug("Version mismatch.");
+ return NULL;
+ }
+
+ if (!memchr(le->port, 0, sizeof(le->port))) {
+ pa_log_warn("Port has missing NUL byte.");
+ return NULL;
+ }
+
+ if (le->volume_valid && !pa_channel_map_valid(&le->channel_map)) {
+ pa_log_warn("Invalid channel map.");
+ return NULL;
+ }
+
+ if (le->volume_valid && (!pa_cvolume_valid(&le->volume) || !pa_cvolume_compatible_with_channel_map(&le->volume, &le->channel_map))) {
+ pa_log_warn("Volume and channel map don't match.");
+ return NULL;
+ }
+
+ e = entry_new(TRUE);
+ e->muted_valid = le->muted_valid;
+ e->volume_valid = le->volume_valid;
+ e->port_valid = le->port_valid;
+ e->muted = le->muted;
+ e->channel_map = le->channel_map;
+ e->volume = le->volume;
+ e->port = pa_xstrdup(le->port);
+ return e;
+}
+#endif
+
+static struct entry* entry_read(struct userdata *u, const char *name) {
+ pa_datum key, data;
+ struct entry *e = NULL;
+ pa_tagstruct *t = NULL;
+ const char* port;
+ uint8_t i, n_formats;
+
+ pa_assert(u);
+ pa_assert(name);
+
+ key.data = (char*) name;
+ key.size = strlen(name);
+
+ pa_zero(data);
+
+ if (!pa_database_get(u->database, &key, &data))
+ goto fail;
+
+ t = pa_tagstruct_new(data.data, data.size);
+ e = entry_new(FALSE);
+
+ if (pa_tagstruct_getu8(t, &e->version) < 0 ||
+ e->version > ENTRY_VERSION ||
+ pa_tagstruct_get_boolean(t, &e->volume_valid) < 0 ||
+ pa_tagstruct_get_channel_map(t, &e->channel_map) < 0 ||
+ pa_tagstruct_get_cvolume(t, &e->volume) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->muted_valid) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->muted) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->port_valid) < 0 ||
+ pa_tagstruct_gets(t, &port) < 0 ||
+ pa_tagstruct_getu8(t, &n_formats) < 0 || n_formats < 1) {
+
+ goto fail;
+ }
+
+ e->port = pa_xstrdup(port);
+
+ for (i = 0; i < n_formats; ++i) {
+ pa_format_info *f = pa_format_info_new();
+ if (pa_tagstruct_get_format_info(t, f) < 0) {
+ pa_format_info_free(f);
+ goto fail;
+ }
+ pa_idxset_put(e->formats, f, NULL);
+ }
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ if (e->volume_valid && !pa_channel_map_valid(&e->channel_map)) {
+ pa_log_warn("Invalid channel map stored in database for device %s", name);
+ goto fail;
+ }
+
+ if (e->volume_valid && (!pa_cvolume_valid(&e->volume) || !pa_cvolume_compatible_with_channel_map(&e->volume, &e->channel_map))) {
+ pa_log_warn("Volume and channel map don't match in database entry for device %s", name);
+ goto fail;
+ }
+
+ pa_tagstruct_free(t);
+ pa_datum_free(&data);
+
+ return e;
+
+fail:
+
+ pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
+
+ if (e)
+ entry_free(e);
+ if (t)
+ pa_tagstruct_free(t);
+
+#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
+ pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name);
+ if ((e = legacy_entry_read(u, &data))) {
+ pa_log_debug("Success. Saving new format for key: %s", name);
+ if (entry_write(u, name, e))
+ trigger_save(u);
+ pa_datum_free(&data);
+ return e;
+ } else
+ pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name);
+#endif
+
+ pa_datum_free(&data);
+ return NULL;
+}
+
+static struct entry* entry_copy(const struct entry *e) {
+ struct entry* r;
+ uint32_t idx;
+ pa_format_info *f;
+
+ pa_assert(e);
+ r = entry_new(FALSE);
+ r->version = e->version;
+ r->muted_valid = e->muted_valid;
+ r->volume_valid = e->volume_valid;
+ r->port_valid = e->port_valid;
+ r->muted = e->muted;
+ r->channel_map = e->channel_map;
+ r->volume = e->volume;
+ r->port = pa_xstrdup(e->port);
+
+ PA_IDXSET_FOREACH(f, e->formats, idx) {
+ pa_idxset_put(r->formats, pa_format_info_copy(f), NULL);
+ }
+ return r;
+}
+
+static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) {
+ pa_cvolume t;
+
+ if (a->port_valid != b->port_valid ||
+ (a->port_valid && !pa_streq(a->port, b->port)))
+ return FALSE;
+
+ if (a->muted_valid != b->muted_valid ||
+ (a->muted_valid && (a->muted != b->muted)))
+ return FALSE;
+
+ t = b->volume;
+ if (a->volume_valid != b->volume_valid ||
+ (a->volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->volume)))
+ return FALSE;
+
+ if (pa_idxset_size(a->formats) != pa_idxset_size(b->formats))
+ return FALSE;
+
+ /** TODO: Compare a bit better */
+
+ return TRUE;
+}
+
+static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ struct userdata *u = userdata;
+ struct entry *entry, *old;
+ char *name;
+
+ pa_assert(c);
+ pa_assert(u);
+
+ if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
+ t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
+ t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
+ t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
+ return;
+
+ if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
+ pa_sink *sink;
+
+ if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
+ return;
+
+ name = pa_sprintf_malloc("sink:%s", sink->name);
+
+ if ((old = entry_read(u, name)))
+ entry = entry_copy(old);
+ else
+ entry = entry_new(TRUE);
+
+ if (sink->save_volume) {
+ entry->channel_map = sink->channel_map;
+ entry->volume = *pa_sink_get_volume(sink, FALSE);
+ entry->volume_valid = TRUE;
+ }
+
+ if (sink->save_muted) {
+ entry->muted = pa_sink_get_mute(sink, FALSE);
+ entry->muted_valid = TRUE;
+ }
+
+ if (sink->save_port) {
+ pa_xfree(entry->port);
+ entry->port = pa_xstrdup(sink->active_port ? sink->active_port->name : "");
+ entry->port_valid = TRUE;
+ }
+
+ } else {
+ pa_source *source;
+
+ pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
+
+ if (!(source = pa_idxset_get_by_index(c->sources, idx)))
+ return;
+
+ name = pa_sprintf_malloc("source:%s", source->name);
+
+ if ((old = entry_read(u, name)))
+ entry = entry_copy(old);
+ else
+ entry = entry_new(TRUE);
+
+ if (source->save_volume) {
+ entry->channel_map = source->channel_map;
+ entry->volume = *pa_source_get_volume(source, FALSE);
+ entry->volume_valid = TRUE;
+ }
+
+ if (source->save_muted) {
+ entry->muted = pa_source_get_mute(source, FALSE);
+ entry->muted_valid = TRUE;
+ }
+
+ if (source->save_port) {
+ pa_xfree(entry->port);
+ entry->port = pa_xstrdup(source->active_port ? source->active_port->name : "");
+ entry->port_valid = TRUE;
+ }
+ }
+
+ pa_assert(entry);
+
+ if (old) {
+
+ if (entries_equal(old, entry)) {
+ entry_free(old);
+ entry_free(entry);
+ pa_xfree(name);
+ return;
+ }
+
+ entry_free(old);
+ }
+
+ pa_log_info("Storing volume/mute/port for device %s.", name);
+
+ if (entry_write(u, name, entry))
+ trigger_save(u);
+
+ entry_free(entry);
+ pa_xfree(name);
+}
+
+static pa_hook_result_t sink_new_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
+ char *name;
+ struct entry *e;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+ pa_assert(u->restore_port);
+
+ name = pa_sprintf_malloc("sink:%s", new_data->name);
+
+ if ((e = entry_read(u, name))) {
+
+ if (e->port_valid) {
+ if (!new_data->active_port) {
+ pa_log_info("Restoring port for sink %s.", name);
+ pa_sink_new_data_set_port(new_data, e->port);
+ new_data->save_port = TRUE;
+ } else
+ pa_log_debug("Not restoring port for sink %s, because already set.", name);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
+ char *name;
+ struct entry *e;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+ pa_assert(u->restore_volume || u->restore_muted);
+
+ name = pa_sprintf_malloc("sink:%s", new_data->name);
+
+ if ((e = entry_read(u, name))) {
+
+ if (u->restore_volume && e->volume_valid) {
+
+ if (!new_data->volume_is_set) {
+ pa_cvolume v;
+
+ pa_log_info("Restoring volume for sink %s.", new_data->name);
+
+ v = e->volume;
+ pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map);
+ pa_sink_new_data_set_volume(new_data, &v);
+
+ new_data->save_volume = TRUE;
+ } else
+ pa_log_debug("Not restoring volume for sink %s, because already set.", new_data->name);
+ }
+
+ if (u->restore_muted && e->muted_valid) {
+
+ if (!new_data->muted_is_set) {
+ pa_log_info("Restoring mute state for sink %s.", new_data->name);
+ pa_sink_new_data_set_muted(new_data, e->muted);
+ new_data->save_muted = TRUE;
+ } else
+ pa_log_debug("Not restoring mute state for sink %s, because already set.", new_data->name);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_new_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
+ char *name;
+ struct entry *e;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+ pa_assert(u->restore_port);
+
+ name = pa_sprintf_malloc("source:%s", new_data->name);
+
+ if ((e = entry_read(u, name))) {
+
+ if (e->port_valid) {
+ if (!new_data->active_port) {
+ pa_log_info("Restoring port for source %s.", name);
+ pa_source_new_data_set_port(new_data, e->port);
+ new_data->save_port = TRUE;
+ } else
+ pa_log_debug("Not restoring port for source %s, because already set.", name);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
+ char *name;
+ struct entry *e;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+ pa_assert(u->restore_volume || u->restore_muted);
+
+ name = pa_sprintf_malloc("source:%s", new_data->name);
+
+ if ((e = entry_read(u, name))) {
+
+ if (u->restore_volume && e->volume_valid) {
+
+ if (!new_data->volume_is_set) {
+ pa_cvolume v;
+
+ pa_log_info("Restoring volume for source %s.", new_data->name);
+
+ v = e->volume;
+ pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map);
+ pa_source_new_data_set_volume(new_data, &v);
+
+ new_data->save_volume = TRUE;
+ } else
+ pa_log_debug("Not restoring volume for source %s, because already set.", new_data->name);
+ }
+
+ if (u->restore_muted && e->muted_valid) {
+
+ if (!new_data->muted_is_set) {
+ pa_log_info("Restoring mute state for source %s.", new_data->name);
+ pa_source_new_data_set_muted(new_data, e->muted);
+ new_data->save_muted = TRUE;
+ } else
+ pa_log_debug("Not restoring mute state for source %s, because already set.", new_data->name);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ return PA_HOOK_OK;
+}
+
+#define EXT_VERSION 1
+
+static void read_sink_format_reply(struct userdata *u, pa_tagstruct *reply, pa_sink *sink) {
+ struct entry *e;
+ char *name;
+
+ pa_assert(u);
+ pa_assert(reply);
+ pa_assert(sink);
+
+ pa_tagstruct_putu32(reply, sink->index);
+
+ /* Read or create an entry */
+ name = pa_sprintf_malloc("sink:%s", sink->name);
+ if (!(e = entry_read(u, name))) {
+ /* Fake a reply with PCM encoding supported */
+ pa_format_info *f = pa_format_info_new();
+
+ pa_tagstruct_putu8(reply, 1);
+ f->encoding = PA_ENCODING_PCM;
+ pa_tagstruct_put_format_info(reply, f);
+
+ pa_format_info_free(f);
+ } else {
+ uint32_t idx;
+ pa_format_info *f;
+
+ /* Write all the formats from the entry to the reply */
+ pa_tagstruct_putu8(reply, pa_idxset_size(e->formats));
+ PA_IDXSET_FOREACH(f, e->formats, idx) {
+ pa_tagstruct_put_format_info(reply, f);
+ }
+ }
+ pa_xfree(name);
+}
+
+static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connection *c, uint32_t tag, pa_tagstruct *t) {
+ struct userdata *u;
+ uint32_t command;
+ pa_tagstruct *reply = NULL;
+
+ pa_assert(p);
+ pa_assert(m);
+ pa_assert(c);
+ pa_assert(t);
+
+ u = m->userdata;
+
+ if (pa_tagstruct_getu32(t, &command) < 0)
+ goto fail;
+
+ reply = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
+ pa_tagstruct_putu32(reply, tag);
+
+ switch (command) {
+ case SUBCOMMAND_TEST: {
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ pa_tagstruct_putu32(reply, EXT_VERSION);
+ break;
+ }
+
+ case SUBCOMMAND_SUBSCRIBE: {
+
+ pa_bool_t enabled;
+
+ if (pa_tagstruct_get_boolean(t, &enabled) < 0 ||
+ !pa_tagstruct_eof(t))
+ goto fail;
+
+ if (enabled)
+ pa_idxset_put(u->subscribed, c, NULL);
+ else
+ pa_idxset_remove_by_data(u->subscribed, c, NULL);
+
+ break;
+ }
+
+ case SUBCOMMAND_READ_SINK_FORMATS_ALL: {
+ pa_sink *sink;
+ uint32_t idx;
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
+ read_sink_format_reply(u, reply, sink);
+ }
+
+ break;
+ }
+ case SUBCOMMAND_READ_SINK_FORMATS: {
+ uint32_t sink_index;
+ pa_sink *sink;
+
+ pa_assert(reply);
+
+ /* Get the sink index and the number of formats from the tagstruct */
+ if (pa_tagstruct_getu32(t, &sink_index) < 0)
+ goto fail;
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ /* Now find our sink */
+ if (!(sink = pa_idxset_get_by_index(u->core->sinks, sink_index)))
+ goto fail;
+
+ read_sink_format_reply(u, reply, sink);
+
+ break;
+ }
+
+ case SUBCOMMAND_SAVE_SINK_FORMATS: {
+
+ struct entry *e;
+ uint32_t sink_index;
+ char *name;
+ pa_sink *sink;
+ uint8_t i, n_formats;
+
+ /* Get the sink index and the number of formats from the tagstruct */
+ if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
+ pa_tagstruct_getu8(t, &n_formats) < 0 || n_formats < 1) {
+
+ goto fail;
+ }
+
+ /* Now find our sink */
+ if (!(sink = pa_idxset_get_by_index(u->core->sinks, sink_index)))
+ goto fail;
+
+ /* Read or create an entry */
+ name = pa_sprintf_malloc("sink:%s", sink->name);
+ if (!(e = entry_read(u, name)))
+ e = entry_new(FALSE);
+
+ /* Read all the formats from our tagstruct */
+ for (i = 0; i < n_formats; ++i) {
+ pa_format_info *f = pa_format_info_new();
+ if (pa_tagstruct_get_format_info(t, f) < 0) {
+ pa_format_info_free(f);
+ pa_xfree(name);
+ goto fail;
+ }
+ pa_idxset_put(e->formats, f, NULL);
+ }
+
+ if (!pa_tagstruct_eof(t)) {
+ entry_free(e);
+ pa_xfree(name);
+ goto fail;
+ }
+
+ if (entry_write(u, name, e))
+ trigger_save(u);
+ else
+ pa_log_warn("Could not save format info for sink %s", sink->name);
+
+ pa_xfree(name);
+ entry_free(e);
+
+ break;
+ }
+
+ default:
+ goto fail;
+ }
+
+ pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), reply);
+ return 0;
+
+fail:
+
+ if (reply)
+ pa_tagstruct_free(reply);
+
+ return -1;
+}
+
+static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_native_connection *c, struct userdata *u) {
+ pa_assert(p);
+ pa_assert(c);
+ pa_assert(u);
+
+ pa_idxset_remove_by_data(u->subscribed, c, NULL);
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+ char *fname;
+ pa_sink *sink;
+ pa_source *source;
+ uint32_t idx;
+ pa_bool_t restore_volume = TRUE, restore_muted = TRUE, restore_port = TRUE;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
+ pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0 ||
+ pa_modargs_get_value_boolean(ma, "restore_port", &restore_port) < 0) {
+ pa_log("restore_port=, restore_volume= and restore_muted= expect boolean arguments");
+ goto fail;
+ }
+
+ if (!restore_muted && !restore_volume && !restore_port)
+ pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring port enabled!");
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->restore_volume = restore_volume;
+ u->restore_muted = restore_muted;
+ u->restore_port = restore_port;
+
+ u->subscribed = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ u->protocol = pa_native_protocol_get(m->core);
+ pa_native_protocol_install_ext(u->protocol, m, extension_cb);
+
+ u->connection_unlink_hook_slot = pa_hook_connect(&pa_native_protocol_hooks(u->protocol)[PA_NATIVE_HOOK_CONNECTION_UNLINK], PA_HOOK_NORMAL, (pa_hook_cb_t) connection_unlink_hook_cb, u);
+
+ u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u);
+
+ if (restore_port) {
+ u->sink_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) sink_new_hook_callback, u);
+ u->source_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_new_hook_callback, u);
+ }
+
+ if (restore_muted || restore_volume) {
+ u->sink_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_fixate_hook_callback, u);
+ u->source_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) source_fixate_hook_callback, u);
+ }
+
+ if (!(fname = pa_state_path("device-volumes", TRUE)))
+ goto fail;
+
+ if (!(u->database = pa_database_open(fname, TRUE))) {
+ pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
+ pa_xfree(fname);
+ goto fail;
+ }
+
+ pa_log_info("Successfully opened database file '%s'.", fname);
+ pa_xfree(fname);
+
+ for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
+
+ for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
+
+ pa_modargs_free(ma);
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->subscription)
+ pa_subscription_free(u->subscription);
+
+ if (u->sink_fixate_hook_slot)
+ pa_hook_slot_free(u->sink_fixate_hook_slot);
+ if (u->source_fixate_hook_slot)
+ pa_hook_slot_free(u->source_fixate_hook_slot);
+ if (u->sink_new_hook_slot)
+ pa_hook_slot_free(u->sink_new_hook_slot);
+ if (u->source_new_hook_slot)
+ pa_hook_slot_free(u->source_new_hook_slot);
+
+ if (u->connection_unlink_hook_slot)
+ pa_hook_slot_free(u->connection_unlink_hook_slot);
+
+ if (u->save_time_event)
+ u->core->mainloop->time_free(u->save_time_event);
+
+ if (u->database)
+ pa_database_close(u->database);
+
+ if (u->protocol) {
+ pa_native_protocol_remove_ext(u->protocol, m);
+ pa_native_protocol_unref(u->protocol);
+ }
+
+ if (u->subscribed)
+ pa_idxset_free(u->subscribed, NULL, NULL);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-equalizer-sink.c b/src/modules/module-equalizer-sink.c
new file mode 100644
index 00000000..e7d8790a
--- /dev/null
+++ b/src/modules/module-equalizer-sink.c
@@ -0,0 +1,2221 @@
+/***
+ This file is part of PulseAudio.
+
+ This module is based off Lennart Poettering's LADSPA sink and swaps out
+ LADSPA functionality for a dbus-aware STFT OLA based digital equalizer.
+ All new work is published under Pulseaudio's original license.
+
+ Copyright 2009 Jason Newton <nevion@gmail.com>
+
+ Original Author:
+ Copyright 2004-2008 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <float.h>
+#include <math.h>
+#include <string.h>
+#include <stdint.h>
+
+//#undef __SSE2__
+#ifdef __SSE2__
+#include <xmmintrin.h>
+#include <emmintrin.h>
+#endif
+
+#include <fftw3.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+#include <pulse/timeval.h>
+
+#include <pulsecore/core-rtclock.h>
+#include <pulsecore/aupdate.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/shared.h>
+#include <pulsecore/idxset.h>
+#include <pulsecore/strlist.h>
+#include <pulsecore/database.h>
+#include <pulsecore/protocol-dbus.h>
+#include <pulsecore/dbus-util.h>
+
+#include "module-equalizer-sink-symdef.h"
+
+PA_MODULE_AUTHOR("Jason Newton");
+PA_MODULE_DESCRIPTION(_("General Purpose Equalizer"));
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ _("sink_name=<name of the sink> "
+ "sink_properties=<properties for the sink> "
+ "sink_master=<sink to connect to> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "autoloaded=<set if this module is being loaded automatically> "
+ ));
+
+#define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
+#define DEFAULT_AUTOLOADED FALSE
+
+struct userdata {
+ pa_module *module;
+ pa_sink *sink;
+ pa_sink_input *sink_input;
+ pa_bool_t autoloaded;
+
+ size_t channels;
+ size_t fft_size;//length (res) of fft
+ size_t window_size;/*
+ *sliding window size
+ *effectively chooses R
+ */
+ size_t R;/* the hop size between overlapping windows
+ * the latency of the filter, calculated from window_size
+ * based on constraints of COLA and window function
+ */
+ //for twiddling with pulseaudio
+ size_t overlap_size;//window_size-R
+ size_t samples_gathered;
+ size_t input_buffer_max;
+ //message
+ float *W;//windowing function (time domain)
+ float *work_buffer, **input, **overlap_accum;
+ fftwf_complex *output_window;
+ fftwf_plan forward_plan, inverse_plan;
+ //size_t samplings;
+
+ float **Xs;
+ float ***Hs;//thread updatable copies of the freq response filters (magintude based)
+ pa_aupdate **a_H;
+ pa_memblockq *input_q;
+ char *output_buffer;
+ size_t output_buffer_length;
+ size_t output_buffer_max_length;
+ pa_memblockq *output_q;
+ pa_bool_t first_iteration;
+
+ pa_dbus_protocol *dbus_protocol;
+ char *dbus_path;
+
+ pa_database *database;
+ char **base_profiles;
+};
+
+static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
+ "sink_master",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "autoloaded",
+ NULL
+};
+
+#define v_size 4
+#define SINKLIST "equalized_sinklist"
+#define EQDB "equalizer_db"
+#define EQ_STATE_DB "equalizer-state"
+#define FILTER_SIZE(u) ((u)->fft_size / 2 + 1)
+#define CHANNEL_PROFILE_SIZE(u) (FILTER_SIZE(u) + 1)
+#define FILTER_STATE_SIZE(u) (CHANNEL_PROFILE_SIZE(u) * (u)->channels)
+
+static void dbus_init(struct userdata *u);
+static void dbus_done(struct userdata *u);
+
+static void hanning_window(float *W, size_t window_size){
+ /* h=.5*(1-cos(2*pi*j/(window_size+1)), COLA for R=(M+1)/2 */
+ for (size_t i = 0; i < window_size; ++i)
+ W[i] = (float).5 * (1 - cos(2*M_PI*i / (window_size+1)));
+}
+
+static void fix_filter(float *H, size_t fft_size){
+ /* divide out the fft gain */
+ for (size_t i = 0; i < fft_size / 2 + 1; ++i)
+ H[i] /= fft_size;
+}
+
+static void interpolate(float *signal, size_t length, uint32_t *xs, float *ys, size_t n_points){
+ /* Note that xs must be monotonically increasing! */
+ float x_range_lower, x_range_upper, c0;
+
+ pa_assert(n_points >= 2);
+ pa_assert(xs[0] == 0);
+ pa_assert(xs[n_points - 1] == length - 1);
+
+ for (size_t x = 0, x_range_lower_i = 0; x < length-1; ++x) {
+ pa_assert(x_range_lower_i < n_points-1);
+
+ x_range_lower = (float) xs[x_range_lower_i];
+ x_range_upper = (float) xs[x_range_lower_i+1];
+
+ pa_assert_se(x_range_lower < x_range_upper);
+ pa_assert_se(x >= x_range_lower);
+ pa_assert_se(x <= x_range_upper);
+
+ /* bilinear-interpolation of coefficients specified */
+ c0 = (x-x_range_lower) / (x_range_upper-x_range_lower);
+ pa_assert(c0 >= 0 && c0 <= 1.0);
+
+ signal[x] = ((1.0f - c0) * ys[x_range_lower_i] + c0 * ys[x_range_lower_i + 1]);
+ while(x >= xs[x_range_lower_i + 1])
+ x_range_lower_i++;
+ }
+
+ signal[length-1] = ys[n_points-1];
+}
+
+static pa_bool_t is_monotonic(const uint32_t *xs, size_t length) {
+ pa_assert(xs);
+
+ if (length < 2)
+ return TRUE;
+
+ for(size_t i = 1; i < length; ++i)
+ if (xs[i] <= xs[i-1])
+ return FALSE;
+
+ return TRUE;
+}
+
+/* ensures memory allocated is a multiple of v_size and aligned */
+static void * alloc(size_t x, size_t s){
+ size_t f;
+ float *t;
+
+ f = PA_ROUND_UP(x*s, sizeof(float)*v_size);
+ pa_assert_se(t = fftwf_malloc(f));
+ pa_memzero(t, f);
+
+ return t;
+}
+
+static void alloc_input_buffers(struct userdata *u, size_t min_buffer_length){
+ if (min_buffer_length <= u->input_buffer_max)
+ return;
+
+ pa_assert(min_buffer_length >= u->window_size);
+ for (size_t c = 0; c < u->channels; ++c) {
+ float *tmp = alloc(min_buffer_length, sizeof(float));
+ if (u->input[c]) {
+ if (!u->first_iteration)
+ memcpy(tmp, u->input[c], u->overlap_size * sizeof(float));
+ free(u->input[c]);
+ }
+ u->input[c] = tmp;
+ }
+ u->input_buffer_max = min_buffer_length;
+}
+
+/* Called from I/O thread context */
+static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ //size_t fs=pa_frame_size(&u->sink->sample_spec);
+
+ /* The sink is _put() before the sink input is, so let's
+ * make sure we don't access it in that time. Also, the
+ * sink input is first shut down, the sink second. */
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
+ *((pa_usec_t*) data) = 0;
+ return 0;
+ }
+
+ *((pa_usec_t*) data) =
+ /* Get the latency of the master sink */
+ pa_sink_get_latency_within_thread(u->sink_input->sink) +
+
+ /* Add the latency internal to our sink input on top */
+ pa_bytes_to_usec(pa_memblockq_get_length(u->output_q) +
+ pa_memblockq_get_length(u->input_q), &u->sink_input->sink->sample_spec) +
+ pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
+ // pa_bytes_to_usec(u->samples_gathered * fs, &u->sink->sample_spec);
+ //+ pa_bytes_to_usec(u->latency * fs, ss)
+ return 0;
+ }
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+
+/* Called from main context */
+static int sink_set_state_cb(pa_sink *s, pa_sink_state_t state) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(state) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return 0;
+
+ pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void sink_request_rewind_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes+pa_memblockq_get_length(u->input_q), TRUE, FALSE, FALSE);
+}
+
+/* Called from I/O thread context */
+static void sink_update_requested_latency_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_set_requested_latency_within_thread(
+ u->sink_input,
+ pa_sink_get_requested_latency_within_thread(s));
+}
+
+/* Called from main context */
+static void sink_set_volume_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return;
+
+ pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, TRUE);
+}
+
+/* Called from main context */
+static void sink_set_mute_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return;
+
+ pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
+}
+
+#if 1
+//reference implementation
+static void dsp_logic(
+ float * restrict dst,//used as a temp array too, needs to be fft_length!
+ float * restrict src,/*input data w/ overlap at start,
+ *automatically cycled in routine
+ */
+ float * restrict overlap,
+ const float X,//multipliar
+ const float * restrict H,//The freq. magnitude scalers filter
+ const float * restrict W,//The windowing function
+ fftwf_complex * restrict output_window,//The transformed window'd src
+ struct userdata *u){
+
+ //use a linear-phase sliding STFT and overlap-add method (for each channel)
+ //window the data
+ for(size_t j = 0; j < u->window_size; ++j){
+ dst[j] = X * W[j] * src[j];
+ }
+ //zero padd the the remaining fft window
+ memset(dst + u->window_size, 0, (u->fft_size - u->window_size) * sizeof(float));
+ //Processing is done here!
+ //do fft
+ fftwf_execute_dft_r2c(u->forward_plan, dst, output_window);
+ //perform filtering
+ for(size_t j = 0; j < FILTER_SIZE(u); ++j){
+ u->output_window[j][0] *= H[j];
+ u->output_window[j][1] *= H[j];
+ }
+ //inverse fft
+ fftwf_execute_dft_c2r(u->inverse_plan, output_window, dst);
+ ////debug: tests overlaping add
+ ////and negates ALL PREVIOUS processing
+ ////yields a perfect reconstruction if COLA is held
+ //for(size_t j = 0; j < u->window_size; ++j){
+ // u->work_buffer[j] = u->W[j] * u->input[c][j];
+ //}
+
+ //overlap add and preserve overlap component from this window (linear phase)
+ for(size_t j = 0; j < u->overlap_size; ++j){
+ u->work_buffer[j] += overlap[j];
+ overlap[j] = dst[u->R + j];
+ }
+ ////debug: tests if basic buffering works
+ ////shouldn't modify the signal AT ALL (beyond roundoff)
+ //for(size_t j = 0; j < u->window_size;++j){
+ // u->work_buffer[j] = u->input[c][j];
+ //}
+
+ //preseve the needed input for the next window's overlap
+ memmove(src, src + u->R,
+ (u->samples_gathered - u->R) * sizeof(float)
+ );
+}
+#else
+typedef float v4sf __attribute__ ((__aligned__(v_size * sizeof(float))));
+typedef union float_vector {
+ float f[v_size];
+ v4sf v;
+ __m128 m;
+} float_vector_t;
+
+//regardless of sse enabled, the loops in here assume
+//16 byte aligned addresses and memory allocations divisible by v_size
+static void dsp_logic(
+ float * restrict dst,//used as a temp array too, needs to be fft_length!
+ float * restrict src,/*input data w/ overlap at start,
+ *automatically cycled in routine
+ */
+ float * restrict overlap,//The size of the overlap
+ const float X,//multipliar
+ const float * restrict H,//The freq. magnitude scalers filter
+ const float * restrict W,//The windowing function
+ fftwf_complex * restrict output_window,//The transformed window'd src
+ struct userdata *u){//Collection of constants
+ const size_t overlap_size = PA_ROUND_UP(u->overlap_size, v_size);
+ float_vector_t x;
+ x.f[0] = x.f[1] = x.f[2] = x.f[3] = X;
+
+ //assert(u->samples_gathered >= u->R);
+ //use a linear-phase sliding STFT and overlap-add method
+ for(size_t j = 0; j < u->window_size; j += v_size){
+ //dst[j] = W[j] * src[j];
+ float_vector_t *d = (float_vector_t*) (dst + j);
+ float_vector_t *w = (float_vector_t*) (W + j);
+ float_vector_t *s = (float_vector_t*) (src + j);
+//#if __SSE2__
+ d->m = _mm_mul_ps(x.m, _mm_mul_ps(w->m, s->m));
+// d->v = x->v * w->v * s->v;
+//#endif
+ }
+ //zero padd the the remaining fft window
+ memset(dst + u->window_size, 0, (u->fft_size - u->window_size) * sizeof(float));
+
+ //Processing is done here!
+ //do fft
+ fftwf_execute_dft_r2c(u->forward_plan, dst, output_window);
+ //perform filtering - purely magnitude based
+ for(size_t j = 0; j < FILTER_SIZE; j += v_size / 2){
+ //output_window[j][0]*=H[j];
+ //output_window[j][1]*=H[j];
+ float_vector_t *d = (float_vector_t*)( ((float *) output_window) + 2 * j);
+ float_vector_t h;
+ h.f[0] = h.f[1] = H[j];
+ h.f[2] = h.f[3] = H[j + 1];
+//#if __SSE2__
+ d->m = _mm_mul_ps(d->m, h.m);
+//#else
+// d->v = d->v * h.v;
+//#endif
+ }
+
+ //inverse fft
+ fftwf_execute_dft_c2r(u->inverse_plan, output_window, dst);
+
+ ////debug: tests overlaping add
+ ////and negates ALL PREVIOUS processing
+ ////yields a perfect reconstruction if COLA is held
+ //for(size_t j = 0; j < u->window_size; ++j){
+ // dst[j] = W[j] * src[j];
+ //}
+
+ //overlap add and preserve overlap component from this window (linear phase)
+ for(size_t j = 0; j < overlap_size; j += v_size){
+ //dst[j]+=overlap[j];
+ //overlap[j]+=dst[j+R];
+ float_vector_t *d = (float_vector_t*)(dst + j);
+ float_vector_t *o = (float_vector_t*)(overlap + j);
+//#if __SSE2__
+ d->m = _mm_add_ps(d->m, o->m);
+ o->m = ((float_vector_t*)(dst + u->R + j))->m;
+//#else
+// d->v = d->v + o->v;
+// o->v = ((float_vector_t*)(dst + u->R + j))->v;
+//#endif
+ }
+ //memcpy(overlap, dst+u->R, u->overlap_size * sizeof(float)); //overlap preserve (debug)
+ //zero out the bit beyond the real overlap so we don't add garbage next iteration
+ memset(overlap + u->overlap_size, 0, overlap_size - u->overlap_size);
+
+ ////debug: tests if basic buffering works
+ ////shouldn't modify the signal AT ALL (beyond roundoff)
+ //for(size_t j = 0; j < u->window_size; ++j){
+ // dst[j] = src[j];
+ //}
+
+ //preseve the needed input for the next window's overlap
+ memmove(src, src + u->R,
+ (u->samples_gathered - u->R) * sizeof(float)
+ );
+}
+#endif
+
+static void flatten_to_memblockq(struct userdata *u){
+ size_t mbs = pa_mempool_block_size_max(u->sink->core->mempool);
+ pa_memchunk tchunk;
+ char *dst;
+ size_t i = 0;
+ while(i < u->output_buffer_length){
+ tchunk.index = 0;
+ tchunk.length = PA_MIN((u->output_buffer_length - i), mbs);
+ tchunk.memblock = pa_memblock_new(u->sink->core->mempool, tchunk.length);
+ //pa_log_debug("pushing %ld into the q", tchunk.length);
+ dst = pa_memblock_acquire(tchunk.memblock);
+ memcpy(dst, u->output_buffer + i, tchunk.length);
+ pa_memblock_release(tchunk.memblock);
+ pa_memblockq_push(u->output_q, &tchunk);
+ pa_memblock_unref(tchunk.memblock);
+ i += tchunk.length;
+ }
+}
+
+static void process_samples(struct userdata *u){
+ size_t fs = pa_frame_size(&(u->sink->sample_spec));
+ unsigned a_i;
+ float *H, X;
+ size_t iterations, offset;
+ pa_assert(u->samples_gathered >= u->window_size);
+ iterations = (u->samples_gathered - u->overlap_size) / u->R;
+ //make sure there is enough buffer memory allocated
+ if(iterations * u->R * fs > u->output_buffer_max_length){
+ u->output_buffer_max_length = iterations * u->R * fs;
+ pa_xfree(u->output_buffer);
+ u->output_buffer = pa_xmalloc(u->output_buffer_max_length);
+ }
+ u->output_buffer_length = iterations * u->R * fs;
+
+ for(size_t iter = 0; iter < iterations; ++iter){
+ offset = iter * u->R * fs;
+ for(size_t c = 0;c < u->channels; c++) {
+ a_i = pa_aupdate_read_begin(u->a_H[c]);
+ X = u->Xs[c][a_i];
+ H = u->Hs[c][a_i];
+ dsp_logic(
+ u->work_buffer,
+ u->input[c],
+ u->overlap_accum[c],
+ X,
+ H,
+ u->W,
+ u->output_window,
+ u
+ );
+ pa_aupdate_read_end(u->a_H[c]);
+ if(u->first_iteration){
+ /* The windowing function will make the audio ramped in, as a cheap fix we can
+ * undo the windowing (for non-zero window values)
+ */
+ for(size_t i = 0; i < u->overlap_size; ++i){
+ u->work_buffer[i] = u->W[i] <= FLT_EPSILON ? u->work_buffer[i] : u->work_buffer[i] / u->W[i];
+ }
+ }
+ pa_sample_clamp(PA_SAMPLE_FLOAT32NE, (uint8_t *) (((float *)u->output_buffer) + c) + offset, fs, u->work_buffer, sizeof(float), u->R);
+ }
+ if(u->first_iteration){
+ u->first_iteration = FALSE;
+ }
+ u->samples_gathered -= u->R;
+ }
+ flatten_to_memblockq(u);
+}
+
+static void input_buffer(struct userdata *u, pa_memchunk *in){
+ size_t fs = pa_frame_size(&(u->sink->sample_spec));
+ size_t samples = in->length/fs;
+ float *src = (float*) ((uint8_t*) pa_memblock_acquire(in->memblock) + in->index);
+ pa_assert(u->samples_gathered + samples <= u->input_buffer_max);
+ for(size_t c = 0; c < u->channels; c++) {
+ //buffer with an offset after the overlap from previous
+ //iterations
+ pa_assert_se(
+ u->input[c] + u->samples_gathered + samples <= u->input[c] + u->input_buffer_max
+ );
+ pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input[c] + u->samples_gathered, sizeof(float), src + c, fs, samples);
+ }
+ u->samples_gathered += samples;
+ pa_memblock_release(in->memblock);
+}
+
+/* Called from I/O thread context */
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
+ struct userdata *u;
+ size_t fs, target_samples;
+ size_t mbs;
+ //struct timeval start, end;
+ pa_memchunk tchunk;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+ pa_assert(chunk);
+ pa_assert(u->sink);
+
+ /* FIXME: Please clean this up. I see more commented code lines
+ * than uncommented code lines. I am sorry, but I am too dumb to
+ * understand this. */
+
+ fs = pa_frame_size(&(u->sink->sample_spec));
+ mbs = pa_mempool_block_size_max(u->sink->core->mempool);
+ if(pa_memblockq_get_length(u->output_q) > 0){
+ //pa_log_debug("qsize is %ld", pa_memblockq_get_length(u->output_q));
+ goto END;
+ }
+ //nbytes = PA_MIN(nbytes, pa_mempool_block_size_max(u->sink->core->mempool));
+ target_samples = PA_ROUND_UP(nbytes / fs, u->R);
+ ////pa_log_debug("vanilla mbs = %ld",mbs);
+ //mbs = PA_ROUND_DOWN(mbs / fs, u->R);
+ //mbs = PA_MAX(mbs, u->R);
+ //target_samples = PA_MAX(target_samples, mbs);
+ //pa_log_debug("target samples: %ld", target_samples);
+ if(u->first_iteration){
+ //allocate request_size
+ target_samples = PA_MAX(target_samples, u->window_size);
+ }else{
+ //allocate request_size + overlap
+ target_samples += u->overlap_size;
+ }
+ alloc_input_buffers(u, target_samples);
+ //pa_log_debug("post target samples: %ld", target_samples);
+ chunk->memblock = NULL;
+
+ /* Hmm, process any rewind request that might be queued up */
+ pa_sink_process_rewind(u->sink, 0);
+
+ //pa_log_debug("start output-buffered %ld, input-buffered %ld, requested %ld",buffered_samples,u->samples_gathered,samples_requested);
+ //pa_rtclock_get(&start);
+ do{
+ size_t input_remaining = target_samples - u->samples_gathered;
+ // pa_log_debug("input remaining %ld samples", input_remaining);
+ pa_assert(input_remaining > 0);
+ while (pa_memblockq_peek(u->input_q, &tchunk) < 0) {
+ //pa_sink_render(u->sink, input_remaining * fs, &tchunk);
+ pa_sink_render_full(u->sink, PA_MIN(input_remaining * fs, mbs), &tchunk);
+ pa_memblockq_push(u->input_q, &tchunk);
+ pa_memblock_unref(tchunk.memblock);
+ }
+ pa_assert(tchunk.memblock);
+
+ tchunk.length = PA_MIN(input_remaining * fs, tchunk.length);
+
+ pa_memblockq_drop(u->input_q, tchunk.length);
+ //pa_log_debug("asked for %ld input samples, got %ld samples",input_remaining,buffer->length/fs);
+ /* copy new input */
+ //pa_rtclock_get(start);
+ // pa_log_debug("buffering %ld bytes", tchunk.length);
+ input_buffer(u, &tchunk);
+ //pa_rtclock_get(&end);
+ //pa_log_debug("Took %0.5f seconds to setup", pa_timeval_diff(end, start) / (double) PA_USEC_PER_SEC);
+ pa_memblock_unref(tchunk.memblock);
+ } while(u->samples_gathered < target_samples);
+
+ //pa_rtclock_get(&end);
+ //pa_log_debug("Took %0.6f seconds to get data", (double) pa_timeval_diff(&end, &start) / PA_USEC_PER_SEC);
+
+ pa_assert(u->fft_size >= u->window_size);
+ pa_assert(u->R < u->window_size);
+ //pa_rtclock_get(&start);
+ /* process a block */
+ process_samples(u);
+ //pa_rtclock_get(&end);
+ //pa_log_debug("Took %0.6f seconds to process", (double) pa_timeval_diff(&end, &start) / PA_USEC_PER_SEC);
+END:
+ pa_assert_se(pa_memblockq_peek(u->output_q, chunk) >= 0);
+ pa_assert(chunk->memblock);
+ pa_memblockq_drop(u->output_q, chunk->length);
+
+ /** FIXME: Uh? you need to unref the chunk here! */
+
+ //pa_log_debug("gave %ld", chunk->length/fs);
+ //pa_log_debug("end pop");
+ return 0;
+}
+
+/* Called from main context */
+static void sink_input_volume_changed_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_volume_changed(u->sink, &i->volume);
+}
+
+/* Called from main context */
+static void sink_input_mute_changed_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_mute_changed(u->sink, i->muted);
+}
+
+static void reset_filter(struct userdata *u){
+ size_t fs = pa_frame_size(&u->sink->sample_spec);
+ size_t max_request;
+
+ u->samples_gathered = 0;
+
+ for(size_t i = 0; i < u->channels; ++i)
+ pa_memzero(u->overlap_accum[i], u->overlap_size * sizeof(float));
+
+ u->first_iteration = TRUE;
+ //set buffer size to max request, no overlap copy
+ max_request = PA_ROUND_UP(pa_sink_input_get_max_request(u->sink_input) / fs , u->R);
+ max_request = PA_MAX(max_request, u->window_size);
+ pa_sink_set_max_request_within_thread(u->sink, max_request * fs);
+}
+
+/* Called from I/O thread context */
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+ size_t amount = 0;
+
+ pa_log_debug("Rewind callback!");
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ if (u->sink->thread_info.rewind_nbytes > 0) {
+ size_t max_rewrite;
+
+ //max_rewrite = nbytes;
+ max_rewrite = nbytes + pa_memblockq_get_length(u->input_q);
+ //PA_MIN(pa_memblockq_get_length(u->input_q), nbytes);
+ amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
+ u->sink->thread_info.rewind_nbytes = 0;
+
+ if (amount > 0) {
+ //invalidate the output q
+ pa_memblockq_seek(u->input_q, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE);
+ pa_log("Resetting filter");
+ //reset_filter(u); //this is the "proper" thing to do...
+ }
+ }
+
+ pa_sink_process_rewind(u->sink, amount);
+ pa_memblockq_rewind(u->input_q, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_memblockq_set_maxrewind(u->input_q, nbytes);
+ pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+ size_t fs;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ fs = pa_frame_size(&u->sink_input->sample_spec);
+ pa_sink_set_max_request_within_thread(u->sink, PA_ROUND_UP(nbytes / fs, u->R) * fs);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_detach_within_thread(u->sink);
+
+ pa_sink_set_rtpoll(u->sink, NULL);
+}
+
+/* Called from I/O thread context */
+static void sink_input_attach_cb(pa_sink_input *i) {
+ struct userdata *u;
+ size_t fs, max_request;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+
+ fs = pa_frame_size(&u->sink_input->sample_spec);
+ /* set buffer size to max request, no overlap copy */
+ max_request = PA_ROUND_UP(pa_sink_input_get_max_request(u->sink_input) / fs, u->R);
+ max_request = PA_MAX(max_request, u->window_size);
+
+ pa_sink_set_max_request_within_thread(u->sink, max_request * fs);
+ pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
+
+ pa_sink_attach_within_thread(u->sink);
+}
+
+/* Called from main context */
+static void sink_input_kill_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* The order here matters! We first kill the sink input, followed
+ * by the sink. That means the sink callbacks must be protected
+ * against an unconnected sink input! */
+ pa_sink_input_unlink(u->sink_input);
+ pa_sink_unlink(u->sink);
+
+ pa_sink_input_unref(u->sink_input);
+ u->sink_input = NULL;
+
+ pa_sink_unref(u->sink);
+ u->sink = NULL;
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from IO thread context */
+static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* If we are added for the first time, ask for a rewinding so that
+ * we are heard right-away. */
+ if (PA_SINK_INPUT_IS_LINKED(state) &&
+ i->thread_info.state == PA_SINK_INPUT_INIT) {
+ pa_log_debug("Requesting rewind due to state change.");
+ pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
+ }
+}
+
+static void pack(char **strs, size_t len, char **packed, size_t *length){
+ size_t t_len = 0;
+ size_t headers = (1+len) * sizeof(uint16_t);
+ char *p;
+ for(size_t i = 0; i < len; ++i){
+ t_len += strlen(strs[i]);
+ }
+ *length = headers + t_len;
+ p = *packed = pa_xmalloc0(*length);
+ *((uint16_t *) p) = (uint16_t) len;
+ p += sizeof(uint16_t);
+ for(size_t i = 0; i < len; ++i){
+ uint16_t l = strlen(strs[i]);
+ *((uint16_t *) p) = (uint16_t) l;
+ p += sizeof(uint16_t);
+ memcpy(p, strs[i], l);
+ p += l;
+ }
+}
+static void unpack(char *str, size_t length, char ***strs, size_t *len){
+ char *p = str;
+ *len = *((uint16_t *) p);
+ p += sizeof(uint16_t);
+ *strs = pa_xnew(char *, *len);
+
+ for(size_t i = 0; i < *len; ++i){
+ size_t l = *((uint16_t *) p);
+ p += sizeof(uint16_t);
+ (*strs)[i] = pa_xnew(char, l + 1);
+ memcpy((*strs)[i], p, l);
+ (*strs)[i][l] = '\0';
+ p += l;
+ }
+}
+static void save_profile(struct userdata *u, size_t channel, char *name){
+ unsigned a_i;
+ const size_t profile_size = CHANNEL_PROFILE_SIZE(u) * sizeof(float);
+ float *H_n, *profile;
+ const float *H;
+ pa_datum key, data;
+ profile = pa_xnew0(float, profile_size);
+ a_i = pa_aupdate_read_begin(u->a_H[channel]);
+ profile[0] = u->Xs[a_i][channel];
+ H = u->Hs[channel][a_i];
+ H_n = profile + 1;
+ for(size_t i = 0 ; i <= FILTER_SIZE(u); ++i){
+ H_n[i] = H[i] * u->fft_size;
+ //H_n[i] = H[i];
+ }
+ pa_aupdate_read_end(u->a_H[channel]);
+ key.data=name;
+ key.size = strlen(key.data);
+ data.data = profile;
+ data.size = profile_size;
+ pa_database_set(u->database, &key, &data, TRUE);
+ pa_database_sync(u->database);
+ if(u->base_profiles[channel]){
+ pa_xfree(u->base_profiles[channel]);
+ }
+ u->base_profiles[channel] = pa_xstrdup(name);
+}
+
+static void save_state(struct userdata *u){
+ unsigned a_i;
+ const size_t filter_state_size = FILTER_STATE_SIZE(u) * sizeof(float);
+ float *H_n, *state;
+ float *H;
+ pa_datum key, data;
+ pa_database *database;
+ char *dbname;
+ char *packed;
+ size_t packed_length;
+
+ pack(u->base_profiles, u->channels, &packed, &packed_length);
+ state = (float *) pa_xmalloc0(filter_state_size + packed_length);
+ memcpy(state + FILTER_STATE_SIZE(u), packed, packed_length);
+ pa_xfree(packed);
+
+ for(size_t c = 0; c < u->channels; ++c){
+ a_i = pa_aupdate_read_begin(u->a_H[c]);
+ state[c * CHANNEL_PROFILE_SIZE(u)] = u->Xs[c][a_i];
+ H = u->Hs[c][a_i];
+ H_n = &state[c * CHANNEL_PROFILE_SIZE(u) + 1];
+ memcpy(H_n, H, FILTER_SIZE(u) * sizeof(float));
+ pa_aupdate_read_end(u->a_H[c]);
+ }
+
+ key.data = u->sink->name;
+ key.size = strlen(key.data);
+ data.data = state;
+ data.size = filter_state_size + packed_length;
+ //thread safety for 0.9.17?
+ pa_assert_se(dbname = pa_state_path(EQ_STATE_DB, FALSE));
+ pa_assert_se(database = pa_database_open(dbname, TRUE));
+ pa_xfree(dbname);
+
+ pa_database_set(database, &key, &data, TRUE);
+ pa_database_sync(database);
+ pa_database_close(database);
+ pa_xfree(state);
+}
+
+static void remove_profile(pa_core *c, char *name){
+ pa_datum key;
+ pa_database *database;
+ key.data = name;
+ key.size = strlen(key.data);
+ pa_assert_se(database = pa_shared_get(c, EQDB));
+ pa_database_unset(database, &key);
+ pa_database_sync(database);
+}
+
+static const char* load_profile(struct userdata *u, size_t channel, char *name){
+ unsigned a_i;
+ pa_datum key, value;
+ const size_t profile_size = CHANNEL_PROFILE_SIZE(u) * sizeof(float);
+ key.data = name;
+ key.size = strlen(key.data);
+ if(pa_database_get(u->database, &key, &value) != NULL){
+ if(value.size == profile_size){
+ float *profile = (float *) value.data;
+ a_i = pa_aupdate_write_begin(u->a_H[channel]);
+ u->Xs[channel][a_i] = profile[0];
+ memcpy(u->Hs[channel][a_i], profile + 1, FILTER_SIZE(u) * sizeof(float));
+ fix_filter(u->Hs[channel][a_i], u->fft_size);
+ pa_aupdate_write_end(u->a_H[channel]);
+ pa_xfree(u->base_profiles[channel]);
+ u->base_profiles[channel] = pa_xstrdup(name);
+ }else{
+ return "incompatible size";
+ }
+ pa_datum_free(&value);
+ }else{
+ return "profile doesn't exist";
+ }
+ return NULL;
+}
+
+static void load_state(struct userdata *u){
+ unsigned a_i;
+ float *H;
+ pa_datum key, value;
+ pa_database *database;
+ char *dbname;
+ pa_assert_se(dbname = pa_state_path(EQ_STATE_DB, FALSE));
+ database = pa_database_open(dbname, FALSE);
+ pa_xfree(dbname);
+ if(!database){
+ pa_log("No resume state");
+ return;
+ }
+
+ key.data = u->sink->name;
+ key.size = strlen(key.data);
+
+ if(pa_database_get(database, &key, &value) != NULL){
+ if(value.size > FILTER_STATE_SIZE(u) * sizeof(float) + sizeof(uint16_t)){
+ float *state = (float *) value.data;
+ size_t n_profs;
+ char **names;
+ for(size_t c = 0; c < u->channels; ++c){
+ a_i = pa_aupdate_write_begin(u->a_H[c]);
+ H = state + c * CHANNEL_PROFILE_SIZE(u) + 1;
+ u->Xs[c][a_i] = state[c * CHANNEL_PROFILE_SIZE(u)];
+ memcpy(u->Hs[c][a_i], H, FILTER_SIZE(u) * sizeof(float));
+ pa_aupdate_write_end(u->a_H[c]);
+ }
+ unpack(((char *)value.data) + FILTER_STATE_SIZE(u) * sizeof(float), value.size - FILTER_STATE_SIZE(u) * sizeof(float), &names, &n_profs);
+ n_profs = PA_MIN(n_profs, u->channels);
+ for(size_t c = 0; c < n_profs; ++c){
+ pa_xfree(u->base_profiles[c]);
+ u->base_profiles[c] = names[c];
+ }
+ pa_xfree(names);
+ }
+ pa_datum_free(&value);
+ }else{
+ pa_log("resume state exists but is wrong size!");
+ }
+ pa_database_close(database);
+}
+
+/* Called from main context */
+static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ return u->sink != dest;
+}
+
+/* Called from main context */
+static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ if (dest) {
+ pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
+ pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
+ } else
+ pa_sink_set_asyncmsgq(u->sink, NULL);
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_modargs *ma;
+ const char *z;
+ pa_sink *master;
+ pa_sink_input_new_data sink_input_data;
+ pa_sink_new_data sink_data;
+ size_t fs, i;
+ unsigned c;
+ float *H;
+ unsigned a_i;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink_master", NULL), PA_NAMEREG_SINK))) {
+ pa_log("Master sink not found");
+ goto fail;
+ }
+
+ ss = master->sample_spec;
+ ss.format = PA_SAMPLE_FLOAT32;
+ map = master->channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+ pa_log("Invalid sample format specification or channel map");
+ goto fail;
+ }
+
+ fs = pa_frame_size(&ss);
+
+ u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ m->userdata = u;
+
+ u->channels = ss.channels;
+ u->fft_size = pow(2, ceil(log(ss.rate) / log(2)));//probably unstable near corner cases of powers of 2
+ pa_log_debug("fft size: %ld", u->fft_size);
+ u->window_size = 15999;
+ if (u->window_size % 2 == 0)
+ u->window_size--;
+ u->R = (u->window_size + 1) / 2;
+ u->overlap_size = u->window_size - u->R;
+ u->samples_gathered = 0;
+ u->input_buffer_max = 0;
+
+ u->a_H = pa_xnew0(pa_aupdate *, u->channels);
+ u->Xs = pa_xnew0(float *, u->channels);
+ u->Hs = pa_xnew0(float **, u->channels);
+
+ for (c = 0; c < u->channels; ++c) {
+ u->Xs[c] = pa_xnew0(float, 2);
+ u->Hs[c] = pa_xnew0(float *, 2);
+ for (i = 0; i < 2; ++i)
+ u->Hs[c][i] = alloc(FILTER_SIZE(u), sizeof(float));
+ }
+
+ u->W = alloc(u->window_size, sizeof(float));
+ u->work_buffer = alloc(u->fft_size, sizeof(float));
+ u->input = pa_xnew0(float *, u->channels);
+ u->overlap_accum = pa_xnew0(float *, u->channels);
+ for (c = 0; c < u->channels; ++c) {
+ u->a_H[c] = pa_aupdate_new();
+ u->input[c] = NULL;
+ u->overlap_accum[c] = alloc(u->overlap_size, sizeof(float));
+ }
+ u->output_window = alloc(FILTER_SIZE(u), sizeof(fftwf_complex));
+ u->forward_plan = fftwf_plan_dft_r2c_1d(u->fft_size, u->work_buffer, u->output_window, FFTW_ESTIMATE);
+ u->inverse_plan = fftwf_plan_dft_c2r_1d(u->fft_size, u->output_window, u->work_buffer, FFTW_ESTIMATE);
+
+ hanning_window(u->W, u->window_size);
+ u->first_iteration = TRUE;
+
+ u->base_profiles = pa_xnew0(char *, u->channels);
+ for (c = 0; c < u->channels; ++c)
+ u->base_profiles[c] = pa_xstrdup("default");
+
+ /* Create sink */
+ pa_sink_new_data_init(&sink_data);
+ sink_data.driver = __FILE__;
+ sink_data.module = m;
+ if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
+ sink_data.name = pa_sprintf_malloc("%s.equalizer", master->name);
+ pa_sink_new_data_set_sample_spec(&sink_data, &ss);
+ pa_sink_new_data_set_channel_map(&sink_data, &map);
+
+ z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "FFT based equalizer on %s", z ? z : master->name);
+
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&sink_data);
+ goto fail;
+ }
+
+ u->autoloaded = DEFAULT_AUTOLOADED;
+ if (pa_modargs_get_value_boolean(ma, "autoloaded", &u->autoloaded) < 0) {
+ pa_log("Failed to parse autoloaded value");
+ goto fail;
+ }
+
+ u->sink = pa_sink_new(m->core, &sink_data,
+ PA_SINK_HW_MUTE_CTRL|PA_SINK_HW_VOLUME_CTRL|PA_SINK_DECIBEL_VOLUME|
+ (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY)));
+ pa_sink_new_data_done(&sink_data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg_cb;
+ u->sink->set_state = sink_set_state_cb;
+ u->sink->update_requested_latency = sink_update_requested_latency_cb;
+ u->sink->request_rewind = sink_request_rewind_cb;
+ u->sink->set_volume = sink_set_volume_cb;
+ u->sink->set_mute = sink_set_mute_cb;
+ u->sink->userdata = u;
+
+ u->input_q = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, fs, 1, 1, 0, &u->sink->silence);
+ u->output_q = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, fs, 1, 1, 0, NULL);
+ u->output_buffer = NULL;
+ u->output_buffer_length = 0;
+ u->output_buffer_max_length = 0;
+
+ pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
+ //pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(u->R*fs, &ss));
+
+ /* Create sink input */
+ pa_sink_input_new_data_init(&sink_input_data);
+ sink_input_data.driver = __FILE__;
+ sink_input_data.module = m;
+ pa_sink_input_new_data_set_sink(&sink_input_data, master, FALSE);
+ sink_input_data.origin_sink = u->sink;
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Equalized Stream");
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
+ pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
+ pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
+
+ pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
+ pa_sink_input_new_data_done(&sink_input_data);
+
+ if (!u->sink_input)
+ goto fail;
+
+ u->sink_input->pop = sink_input_pop_cb;
+ u->sink_input->process_rewind = sink_input_process_rewind_cb;
+ u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
+ u->sink_input->update_max_request = sink_input_update_max_request_cb;
+ u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
+ u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
+ u->sink_input->kill = sink_input_kill_cb;
+ u->sink_input->attach = sink_input_attach_cb;
+ u->sink_input->detach = sink_input_detach_cb;
+ u->sink_input->state_change = sink_input_state_change_cb;
+ u->sink_input->may_move_to = sink_input_may_move_to_cb;
+ u->sink_input->moving = sink_input_moving_cb;
+ u->sink_input->volume_changed = sink_input_volume_changed_cb;
+ u->sink_input->mute_changed = sink_input_mute_changed_cb;
+ u->sink_input->userdata = u;
+
+ u->sink->input_to_master = u->sink_input;
+
+ dbus_init(u);
+
+ /* default filter to these */
+ for (c = 0; c< u->channels; ++c) {
+ a_i = pa_aupdate_write_begin(u->a_H[c]);
+ H = u->Hs[c][a_i];
+ u->Xs[c][a_i] = 1.0f;
+
+ for(i = 0; i < FILTER_SIZE(u); ++i)
+ H[i] = 1.0 / sqrtf(2.0f);
+
+ fix_filter(H, u->fft_size);
+ pa_aupdate_write_end(u->a_H[c]);
+ }
+
+ /* load old parameters */
+ load_state(u);
+
+ pa_sink_put(u->sink);
+ pa_sink_input_put(u->sink_input);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+ unsigned c;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ save_state(u);
+
+ dbus_done(u);
+
+ for(c = 0; c < u->channels; ++c)
+ pa_xfree(u->base_profiles[c]);
+ pa_xfree(u->base_profiles);
+
+ /* See comments in sink_input_kill_cb() above regarding
+ * destruction order! */
+
+ if (u->sink_input)
+ pa_sink_input_unlink(u->sink_input);
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->sink_input)
+ pa_sink_input_unref(u->sink_input);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ pa_xfree(u->output_buffer);
+ pa_memblockq_free(u->output_q);
+ pa_memblockq_free(u->input_q);
+
+ fftwf_destroy_plan(u->inverse_plan);
+ fftwf_destroy_plan(u->forward_plan);
+ pa_xfree(u->output_window);
+ for (c = 0; c < u->channels; ++c) {
+ pa_aupdate_free(u->a_H[c]);
+ pa_xfree(u->overlap_accum[c]);
+ pa_xfree(u->input[c]);
+ }
+ pa_xfree(u->a_H);
+ pa_xfree(u->overlap_accum);
+ pa_xfree(u->input);
+ pa_xfree(u->work_buffer);
+ pa_xfree(u->W);
+ for (c = 0; c < u->channels; ++c) {
+ pa_xfree(u->Xs[c]);
+ for (size_t i = 0; i < 2; ++i)
+ pa_xfree(u->Hs[c][i]);
+ pa_xfree(u->Hs[c]);
+ }
+ pa_xfree(u->Xs);
+ pa_xfree(u->Hs);
+
+ pa_xfree(u);
+}
+
+/*
+ * DBus Routines and Callbacks
+ */
+#define EXTNAME "org.PulseAudio.Ext.Equalizing1"
+#define MANAGER_PATH "/org/pulseaudio/equalizing1"
+#define MANAGER_IFACE EXTNAME ".Manager"
+#define EQUALIZER_IFACE EXTNAME ".Equalizer"
+static void manager_get_revision(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void manager_get_sinks(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void manager_get_profiles(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void manager_get_all(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void manager_handle_remove_profile(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_get_revision(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_get_filter_rate(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_get_n_coefs(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_get_n_channels(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_get_all(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_handle_seed_filter(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_handle_get_filter_points(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_handle_get_filter(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_handle_set_filter(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_handle_save_profile(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_handle_load_profile(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_handle_save_state(DBusConnection *conn, DBusMessage *msg, void *_u);
+static void equalizer_handle_get_profile_name(DBusConnection *conn, DBusMessage *msg, void *_u);
+enum manager_method_index {
+ MANAGER_METHOD_REMOVE_PROFILE,
+ MANAGER_METHOD_MAX
+};
+
+pa_dbus_arg_info remove_profile_args[]={
+ {"name", "s","in"},
+};
+
+static pa_dbus_method_handler manager_methods[MANAGER_METHOD_MAX]={
+ [MANAGER_METHOD_REMOVE_PROFILE]{
+ .method_name="RemoveProfile",
+ .arguments=remove_profile_args,
+ .n_arguments=sizeof(remove_profile_args)/sizeof(pa_dbus_arg_info),
+ .receive_cb=manager_handle_remove_profile}
+};
+
+enum manager_handler_index {
+ MANAGER_HANDLER_REVISION,
+ MANAGER_HANDLER_EQUALIZED_SINKS,
+ MANAGER_HANDLER_PROFILES,
+ MANAGER_HANDLER_MAX
+};
+
+static pa_dbus_property_handler manager_handlers[MANAGER_HANDLER_MAX]={
+ [MANAGER_HANDLER_REVISION]={.property_name="InterfaceRevision",.type="u",.get_cb=manager_get_revision,.set_cb=NULL},
+ [MANAGER_HANDLER_EQUALIZED_SINKS]={.property_name="EqualizedSinks",.type="ao",.get_cb=manager_get_sinks,.set_cb=NULL},
+ [MANAGER_HANDLER_PROFILES]={.property_name="Profiles",.type="as",.get_cb=manager_get_profiles,.set_cb=NULL}
+};
+
+pa_dbus_arg_info sink_args[]={
+ {"sink", "o", NULL}
+};
+
+enum manager_signal_index{
+ MANAGER_SIGNAL_SINK_ADDED,
+ MANAGER_SIGNAL_SINK_REMOVED,
+ MANAGER_SIGNAL_PROFILES_CHANGED,
+ MANAGER_SIGNAL_MAX
+};
+
+static pa_dbus_signal_info manager_signals[MANAGER_SIGNAL_MAX]={
+ [MANAGER_SIGNAL_SINK_ADDED]={.name="SinkAdded", .arguments=sink_args, .n_arguments=sizeof(sink_args)/sizeof(pa_dbus_arg_info)},
+ [MANAGER_SIGNAL_SINK_REMOVED]={.name="SinkRemoved", .arguments=sink_args, .n_arguments=sizeof(sink_args)/sizeof(pa_dbus_arg_info)},
+ [MANAGER_SIGNAL_PROFILES_CHANGED]={.name="ProfilesChanged", .arguments=NULL, .n_arguments=0}
+};
+
+static pa_dbus_interface_info manager_info={
+ .name=MANAGER_IFACE,
+ .method_handlers=manager_methods,
+ .n_method_handlers=MANAGER_METHOD_MAX,
+ .property_handlers=manager_handlers,
+ .n_property_handlers=MANAGER_HANDLER_MAX,
+ .get_all_properties_cb=manager_get_all,
+ .signals=manager_signals,
+ .n_signals=MANAGER_SIGNAL_MAX
+};
+
+enum equalizer_method_index {
+ EQUALIZER_METHOD_FILTER_POINTS,
+ EQUALIZER_METHOD_SEED_FILTER,
+ EQUALIZER_METHOD_SAVE_PROFILE,
+ EQUALIZER_METHOD_LOAD_PROFILE,
+ EQUALIZER_METHOD_SET_FILTER,
+ EQUALIZER_METHOD_GET_FILTER,
+ EQUALIZER_METHOD_SAVE_STATE,
+ EQUALIZER_METHOD_GET_PROFILE_NAME,
+ EQUALIZER_METHOD_MAX
+};
+
+enum equalizer_handler_index {
+ EQUALIZER_HANDLER_REVISION,
+ EQUALIZER_HANDLER_SAMPLERATE,
+ EQUALIZER_HANDLER_FILTERSAMPLERATE,
+ EQUALIZER_HANDLER_N_COEFS,
+ EQUALIZER_HANDLER_N_CHANNELS,
+ EQUALIZER_HANDLER_MAX
+};
+
+pa_dbus_arg_info filter_points_args[]={
+ {"channel", "u","in"},
+ {"xs", "au","in"},
+ {"ys", "ad","out"},
+ {"preamp", "d","out"}
+};
+pa_dbus_arg_info seed_filter_args[]={
+ {"channel", "u","in"},
+ {"xs", "au","in"},
+ {"ys", "ad","in"},
+ {"preamp", "d","in"}
+};
+
+pa_dbus_arg_info set_filter_args[]={
+ {"channel", "u","in"},
+ {"ys", "ad","in"},
+ {"preamp", "d","in"}
+};
+pa_dbus_arg_info get_filter_args[]={
+ {"channel", "u","in"},
+ {"ys", "ad","out"},
+ {"preamp", "d","out"}
+};
+
+pa_dbus_arg_info save_profile_args[]={
+ {"channel", "u","in"},
+ {"name", "s","in"}
+};
+pa_dbus_arg_info load_profile_args[]={
+ {"channel", "u","in"},
+ {"name", "s","in"}
+};
+pa_dbus_arg_info base_profile_name_args[]={
+ {"channel", "u","in"},
+ {"name", "s","out"}
+};
+
+static pa_dbus_method_handler equalizer_methods[EQUALIZER_METHOD_MAX]={
+ [EQUALIZER_METHOD_SEED_FILTER]{
+ .method_name="SeedFilter",
+ .arguments=seed_filter_args,
+ .n_arguments=sizeof(seed_filter_args)/sizeof(pa_dbus_arg_info),
+ .receive_cb=equalizer_handle_seed_filter},
+ [EQUALIZER_METHOD_FILTER_POINTS]{
+ .method_name="FilterAtPoints",
+ .arguments=filter_points_args,
+ .n_arguments=sizeof(filter_points_args)/sizeof(pa_dbus_arg_info),
+ .receive_cb=equalizer_handle_get_filter_points},
+ [EQUALIZER_METHOD_SET_FILTER]{
+ .method_name="SetFilter",
+ .arguments=set_filter_args,
+ .n_arguments=sizeof(set_filter_args)/sizeof(pa_dbus_arg_info),
+ .receive_cb=equalizer_handle_set_filter},
+ [EQUALIZER_METHOD_GET_FILTER]{
+ .method_name="GetFilter",
+ .arguments=get_filter_args,
+ .n_arguments=sizeof(get_filter_args)/sizeof(pa_dbus_arg_info),
+ .receive_cb=equalizer_handle_get_filter},
+ [EQUALIZER_METHOD_SAVE_PROFILE]{
+ .method_name="SaveProfile",
+ .arguments=save_profile_args,
+ .n_arguments=sizeof(save_profile_args)/sizeof(pa_dbus_arg_info),
+ .receive_cb=equalizer_handle_save_profile},
+ [EQUALIZER_METHOD_LOAD_PROFILE]{
+ .method_name="LoadProfile",
+ .arguments=load_profile_args,
+ .n_arguments=sizeof(load_profile_args)/sizeof(pa_dbus_arg_info),
+ .receive_cb=equalizer_handle_load_profile},
+ [EQUALIZER_METHOD_SAVE_STATE]{
+ .method_name="SaveState",
+ .arguments=NULL,
+ .n_arguments=0,
+ .receive_cb=equalizer_handle_save_state},
+ [EQUALIZER_METHOD_GET_PROFILE_NAME]{
+ .method_name="BaseProfile",
+ .arguments=base_profile_name_args,
+ .n_arguments=sizeof(base_profile_name_args)/sizeof(pa_dbus_arg_info),
+ .receive_cb=equalizer_handle_get_profile_name}
+};
+
+static pa_dbus_property_handler equalizer_handlers[EQUALIZER_HANDLER_MAX]={
+ [EQUALIZER_HANDLER_REVISION]={.property_name="InterfaceRevision",.type="u",.get_cb=equalizer_get_revision,.set_cb=NULL},
+ [EQUALIZER_HANDLER_SAMPLERATE]{.property_name="SampleRate",.type="u",.get_cb=equalizer_get_sample_rate,.set_cb=NULL},
+ [EQUALIZER_HANDLER_FILTERSAMPLERATE]{.property_name="FilterSampleRate",.type="u",.get_cb=equalizer_get_filter_rate,.set_cb=NULL},
+ [EQUALIZER_HANDLER_N_COEFS]{.property_name="NFilterCoefficients",.type="u",.get_cb=equalizer_get_n_coefs,.set_cb=NULL},
+ [EQUALIZER_HANDLER_N_CHANNELS]{.property_name="NChannels",.type="u",.get_cb=equalizer_get_n_channels,.set_cb=NULL},
+};
+
+enum equalizer_signal_index{
+ EQUALIZER_SIGNAL_FILTER_CHANGED,
+ EQUALIZER_SIGNAL_SINK_RECONFIGURED,
+ EQUALIZER_SIGNAL_MAX
+};
+
+static pa_dbus_signal_info equalizer_signals[EQUALIZER_SIGNAL_MAX]={
+ [EQUALIZER_SIGNAL_FILTER_CHANGED]={.name="FilterChanged", .arguments=NULL, .n_arguments=0},
+ [EQUALIZER_SIGNAL_SINK_RECONFIGURED]={.name="SinkReconfigured", .arguments=NULL, .n_arguments=0},
+};
+
+static pa_dbus_interface_info equalizer_info={
+ .name=EQUALIZER_IFACE,
+ .method_handlers=equalizer_methods,
+ .n_method_handlers=EQUALIZER_METHOD_MAX,
+ .property_handlers=equalizer_handlers,
+ .n_property_handlers=EQUALIZER_HANDLER_MAX,
+ .get_all_properties_cb=equalizer_get_all,
+ .signals=equalizer_signals,
+ .n_signals=EQUALIZER_SIGNAL_MAX
+};
+
+void dbus_init(struct userdata *u){
+ uint32_t dummy;
+ DBusMessage *signal = NULL;
+ pa_idxset *sink_list = NULL;
+ u->dbus_protocol=pa_dbus_protocol_get(u->sink->core);
+ u->dbus_path=pa_sprintf_malloc("/org/pulseaudio/core1/sink%d", u->sink->index);
+
+ pa_dbus_protocol_add_interface(u->dbus_protocol, u->dbus_path, &equalizer_info, u);
+ sink_list = pa_shared_get(u->sink->core, SINKLIST);
+ u->database = pa_shared_get(u->sink->core, EQDB);
+ if(sink_list == NULL){
+ char *dbname;
+ sink_list=pa_idxset_new(&pa_idxset_trivial_hash_func, &pa_idxset_trivial_compare_func);
+ pa_shared_set(u->sink->core, SINKLIST, sink_list);
+ pa_assert_se(dbname = pa_state_path("equalizer-presets", FALSE));
+ pa_assert_se(u->database = pa_database_open(dbname, TRUE));
+ pa_xfree(dbname);
+ pa_shared_set(u->sink->core, EQDB, u->database);
+ pa_dbus_protocol_add_interface(u->dbus_protocol, MANAGER_PATH, &manager_info, u->sink->core);
+ pa_dbus_protocol_register_extension(u->dbus_protocol, EXTNAME);
+ }
+ pa_idxset_put(sink_list, u, &dummy);
+
+ pa_assert_se((signal = dbus_message_new_signal(MANAGER_PATH, MANAGER_IFACE, manager_signals[MANAGER_SIGNAL_SINK_ADDED].name)));
+ dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &u->dbus_path, DBUS_TYPE_INVALID);
+ pa_dbus_protocol_send_signal(u->dbus_protocol, signal);
+ dbus_message_unref(signal);
+}
+
+void dbus_done(struct userdata *u){
+ pa_idxset *sink_list;
+ uint32_t dummy;
+
+ DBusMessage *signal = NULL;
+ pa_assert_se((signal = dbus_message_new_signal(MANAGER_PATH, MANAGER_IFACE, manager_signals[MANAGER_SIGNAL_SINK_REMOVED].name)));
+ dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &u->dbus_path, DBUS_TYPE_INVALID);
+ pa_dbus_protocol_send_signal(u->dbus_protocol, signal);
+ dbus_message_unref(signal);
+
+ pa_assert_se(sink_list=pa_shared_get(u->sink->core,SINKLIST));
+ pa_idxset_remove_by_data(sink_list,u,&dummy);
+ if(pa_idxset_size(sink_list)==0){
+ pa_dbus_protocol_unregister_extension(u->dbus_protocol, EXTNAME);
+ pa_dbus_protocol_remove_interface(u->dbus_protocol, MANAGER_PATH, manager_info.name);
+ pa_shared_remove(u->sink->core, EQDB);
+ pa_database_close(u->database);
+ pa_shared_remove(u->sink->core, SINKLIST);
+ pa_xfree(sink_list);
+ }
+ pa_dbus_protocol_remove_interface(u->dbus_protocol, u->dbus_path, equalizer_info.name);
+ pa_xfree(u->dbus_path);
+ pa_dbus_protocol_unref(u->dbus_protocol);
+}
+
+void manager_handle_remove_profile(DBusConnection *conn, DBusMessage *msg, void *_u) {
+ DBusError error;
+ pa_core *c = (pa_core *)_u;
+ DBusMessage *signal = NULL;
+ pa_dbus_protocol *dbus_protocol;
+ char *name;
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(c);
+ dbus_error_init(&error);
+ if(!dbus_message_get_args(msg, &error,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INVALID)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "%s", error.message);
+ dbus_error_free(&error);
+ return;
+ }
+ remove_profile(c,name);
+ pa_dbus_send_empty_reply(conn, msg);
+
+ pa_assert_se((signal = dbus_message_new_signal(MANAGER_PATH, MANAGER_IFACE, manager_signals[MANAGER_SIGNAL_PROFILES_CHANGED].name)));
+ dbus_protocol = pa_dbus_protocol_get(c);
+ pa_dbus_protocol_send_signal(dbus_protocol, signal);
+ pa_dbus_protocol_unref(dbus_protocol);
+ dbus_message_unref(signal);
+}
+
+void manager_get_revision(DBusConnection *conn, DBusMessage *msg, void *_u){
+ uint32_t rev=1;
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_UINT32, &rev);
+}
+
+static void get_sinks(pa_core *u, char ***names, unsigned *n_sinks){
+ void *iter = NULL;
+ struct userdata *sink_u = NULL;
+ uint32_t dummy;
+ pa_idxset *sink_list;
+ pa_assert(u);
+ pa_assert(names);
+ pa_assert(n_sinks);
+
+ pa_assert_se(sink_list = pa_shared_get(u, SINKLIST));
+ *n_sinks = (unsigned) pa_idxset_size(sink_list);
+ *names = *n_sinks > 0 ? pa_xnew0(char *,*n_sinks) : NULL;
+ for(uint32_t i = 0; i < *n_sinks; ++i){
+ sink_u = (struct userdata *) pa_idxset_iterate(sink_list, &iter, &dummy);
+ (*names)[i] = pa_xstrdup(sink_u->dbus_path);
+ }
+}
+
+void manager_get_sinks(DBusConnection *conn, DBusMessage *msg, void *_u){
+ unsigned n;
+ char **names = NULL;
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(_u);
+
+ get_sinks((pa_core *) _u, &names, &n);
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, names, n);
+ for(unsigned i = 0; i < n; ++i){
+ pa_xfree(names[i]);
+ }
+ pa_xfree(names);
+}
+
+static void get_profiles(pa_core *c, char ***names, unsigned *n){
+ char *name;
+ pa_database *database;
+ pa_datum key, next_key;
+ pa_strlist *head=NULL, *iter;
+ pa_bool_t done;
+ pa_assert_se(database = pa_shared_get(c, EQDB));
+
+ pa_assert(c);
+ pa_assert(names);
+ pa_assert(n);
+ done = !pa_database_first(database, &key, NULL);
+ *n = 0;
+ while(!done){
+ done = !pa_database_next(database, &key, &next_key, NULL);
+ name=pa_xmalloc(key.size + 1);
+ memcpy(name, key.data, key.size);
+ name[key.size] = '\0';
+ pa_datum_free(&key);
+ head = pa_strlist_prepend(head, name);
+ pa_xfree(name);
+ key = next_key;
+ (*n)++;
+ }
+ (*names) = *n > 0 ? pa_xnew0(char *, *n) : NULL;
+ iter=head;
+ for(unsigned i = 0; i < *n; ++i){
+ (*names)[*n - 1 - i] = pa_xstrdup(pa_strlist_data(iter));
+ iter = pa_strlist_next(iter);
+ }
+ pa_strlist_free(head);
+}
+
+void manager_get_profiles(DBusConnection *conn, DBusMessage *msg, void *_u){
+ char **names;
+ unsigned n;
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(_u);
+
+ get_profiles((pa_core *)_u, &names, &n);
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_STRING, names, n);
+ for(unsigned i = 0; i < n; ++i){
+ pa_xfree(names[i]);
+ }
+ pa_xfree(names);
+}
+
+void manager_get_all(DBusConnection *conn, DBusMessage *msg, void *_u){
+ pa_core *c;
+ char **names = NULL;
+ unsigned n;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter, dict_iter;
+ uint32_t rev;
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert_se(c = _u);
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ rev = 1;
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, manager_handlers[MANAGER_HANDLER_REVISION].property_name, DBUS_TYPE_UINT32, &rev);
+
+ get_sinks(c, &names, &n);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter,manager_handlers[MANAGER_HANDLER_EQUALIZED_SINKS].property_name, DBUS_TYPE_OBJECT_PATH, names, n);
+ for(unsigned i = 0; i < n; ++i){
+ pa_xfree(names[i]);
+ }
+ pa_xfree(names);
+
+ get_profiles(c, &names, &n);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, manager_handlers[MANAGER_HANDLER_PROFILES].property_name, DBUS_TYPE_STRING, names, n);
+ for(unsigned i = 0; i < n; ++i){
+ pa_xfree(names[i]);
+ }
+ pa_xfree(names);
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+ dbus_message_unref(reply);
+}
+
+void equalizer_handle_seed_filter(DBusConnection *conn, DBusMessage *msg, void *_u) {
+ struct userdata *u = _u;
+ DBusError error;
+ DBusMessage *signal = NULL;
+ float *ys;
+ uint32_t *xs, channel, r_channel;
+ double *_ys, preamp;
+ unsigned x_npoints, y_npoints, a_i;
+ float *H;
+ pa_bool_t points_good = TRUE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+
+ dbus_error_init(&error);
+
+ if(!dbus_message_get_args(msg, &error,
+ DBUS_TYPE_UINT32, &channel,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &xs, &x_npoints,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, &_ys, &y_npoints,
+ DBUS_TYPE_DOUBLE, &preamp,
+ DBUS_TYPE_INVALID)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "%s", error.message);
+ dbus_error_free(&error);
+ return;
+ }
+ if(channel > u->channels){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "invalid channel: %d", channel);
+ dbus_error_free(&error);
+ return;
+ }
+ for(size_t i = 0; i < x_npoints; ++i){
+ if(xs[i] >= FILTER_SIZE(u)){
+ points_good = FALSE;
+ break;
+ }
+ }
+ if(!is_monotonic(xs, x_npoints) || !points_good){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "xs must be monotonic and 0<=x<=%ld", u->fft_size / 2);
+ dbus_error_free(&error);
+ return;
+ }else if(x_npoints != y_npoints || x_npoints < 2 || x_npoints > FILTER_SIZE(u)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "xs and ys must be the same length and 2<=l<=%ld!", FILTER_SIZE(u));
+ dbus_error_free(&error);
+ return;
+ }else if(xs[0] != 0 || xs[x_npoints - 1] != u->fft_size / 2){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "xs[0] must be 0 and xs[-1]=fft_size/2");
+ dbus_error_free(&error);
+ return;
+ }
+
+ ys = pa_xmalloc(x_npoints * sizeof(float));
+ for(uint32_t i = 0; i < x_npoints; ++i){
+ ys[i] = (float) _ys[i];
+ }
+ r_channel = channel == u->channels ? 0 : channel;
+ a_i = pa_aupdate_write_begin(u->a_H[r_channel]);
+ H = u->Hs[r_channel][a_i];
+ u->Xs[r_channel][a_i] = preamp;
+ interpolate(H, FILTER_SIZE(u), xs, ys, x_npoints);
+ fix_filter(H, u->fft_size);
+ if(channel == u->channels){
+ for(size_t c = 1; c < u->channels; ++c){
+ unsigned b_i = pa_aupdate_write_begin(u->a_H[c]);
+ float *H_p = u->Hs[c][b_i];
+ u->Xs[c][b_i] = preamp;
+ memcpy(H_p, H, FILTER_SIZE(u) * sizeof(float));
+ pa_aupdate_write_end(u->a_H[c]);
+ }
+ }
+ pa_aupdate_write_end(u->a_H[r_channel]);
+ pa_xfree(ys);
+
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ pa_assert_se((signal = dbus_message_new_signal(u->dbus_path, EQUALIZER_IFACE, equalizer_signals[EQUALIZER_SIGNAL_FILTER_CHANGED].name)));
+ pa_dbus_protocol_send_signal(u->dbus_protocol, signal);
+ dbus_message_unref(signal);
+}
+
+void equalizer_handle_get_filter_points(DBusConnection *conn, DBusMessage *msg, void *_u) {
+ struct userdata *u = (struct userdata *) _u;
+ uint32_t *xs, channel, r_channel;
+ double *ys, preamp;
+ unsigned x_npoints, a_i;
+ float *H;
+ pa_bool_t points_good=TRUE;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusError error;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+
+ dbus_error_init(&error);
+ if(!dbus_message_get_args(msg, &error,
+ DBUS_TYPE_UINT32, &channel,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &xs, &x_npoints,
+ DBUS_TYPE_INVALID)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "%s", error.message);
+ dbus_error_free(&error);
+ return;
+ }
+ if(channel > u->channels){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "invalid channel: %d", channel);
+ dbus_error_free(&error);
+ return;
+ }
+
+ for(size_t i = 0; i < x_npoints; ++i){
+ if(xs[i] >= FILTER_SIZE(u)){
+ points_good=FALSE;
+ break;
+ }
+ }
+
+ if(x_npoints > FILTER_SIZE(u) || !points_good){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "xs indices/length must be <= %ld!", FILTER_SIZE(u));
+ dbus_error_free(&error);
+ return;
+ }
+
+ r_channel = channel == u->channels ? 0 : channel;
+ ys = pa_xmalloc(x_npoints * sizeof(double));
+ a_i = pa_aupdate_read_begin(u->a_H[r_channel]);
+ H = u->Hs[r_channel][a_i];
+ preamp = u->Xs[r_channel][a_i];
+ for(uint32_t i = 0; i < x_npoints; ++i){
+ ys[i] = H[xs[i]] * u->fft_size;
+ }
+ pa_aupdate_read_end(u->a_H[r_channel]);
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+ dbus_message_iter_init_append(reply, &msg_iter);
+
+ pa_dbus_append_basic_array(&msg_iter, DBUS_TYPE_DOUBLE, ys, x_npoints);
+ pa_dbus_append_basic_variant(&msg_iter, DBUS_TYPE_DOUBLE, &preamp);
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+ dbus_message_unref(reply);
+ pa_xfree(ys);
+}
+
+static void get_filter(struct userdata *u, size_t channel, double **H_, double *preamp){
+ float *H;
+ unsigned a_i;
+ size_t r_channel = channel == u->channels ? 0 : channel;
+ *H_ = pa_xnew0(double, FILTER_SIZE(u));
+ a_i = pa_aupdate_read_begin(u->a_H[r_channel]);
+ H = u->Hs[r_channel][a_i];
+ for(size_t i = 0;i < FILTER_SIZE(u); ++i){
+ (*H_)[i] = H[i] * u->fft_size;
+ }
+ *preamp = u->Xs[r_channel][a_i];
+
+ pa_aupdate_read_end(u->a_H[r_channel]);
+}
+
+void equalizer_handle_get_filter(DBusConnection *conn, DBusMessage *msg, void *_u){
+ struct userdata *u;
+ unsigned n_coefs;
+ uint32_t channel;
+ double *H_, preamp;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusError error;
+ pa_assert_se(u = (struct userdata *) _u);
+ pa_assert(conn);
+ pa_assert(msg);
+
+ dbus_error_init(&error);
+ if(!dbus_message_get_args(msg, &error,
+ DBUS_TYPE_UINT32, &channel,
+ DBUS_TYPE_INVALID)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "%s", error.message);
+ dbus_error_free(&error);
+ return;
+ }
+ if(channel > u->channels){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "invalid channel: %d", channel);
+ dbus_error_free(&error);
+ return;
+ }
+
+ n_coefs = CHANNEL_PROFILE_SIZE(u);
+ pa_assert(conn);
+ pa_assert(msg);
+ get_filter(u, channel, &H_, &preamp);
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+ dbus_message_iter_init_append(reply, &msg_iter);
+
+ pa_dbus_append_basic_array(&msg_iter, DBUS_TYPE_DOUBLE, H_, n_coefs);
+ pa_dbus_append_basic_variant(&msg_iter, DBUS_TYPE_DOUBLE, &preamp);
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+ dbus_message_unref(reply);
+ pa_xfree(H_);
+}
+
+static void set_filter(struct userdata *u, size_t channel, double *H_, double preamp){
+ unsigned a_i;
+ size_t r_channel = channel == u->channels ? 0 : channel;
+ float *H;
+ //all channels
+ a_i = pa_aupdate_write_begin(u->a_H[r_channel]);
+ u->Xs[r_channel][a_i] = (float) preamp;
+ H = u->Hs[r_channel][a_i];
+ for(size_t i = 0; i < FILTER_SIZE(u); ++i){
+ H[i] = (float) H_[i];
+ }
+ fix_filter(H, u->fft_size);
+ if(channel == u->channels){
+ for(size_t c = 1; c < u->channels; ++c){
+ unsigned b_i = pa_aupdate_write_begin(u->a_H[c]);
+ u->Xs[c][b_i] = u->Xs[r_channel][a_i];
+ memcpy(u->Hs[c][b_i], u->Hs[r_channel][a_i], FILTER_SIZE(u) * sizeof(float));
+ pa_aupdate_write_end(u->a_H[c]);
+ }
+ }
+ pa_aupdate_write_end(u->a_H[r_channel]);
+}
+
+void equalizer_handle_set_filter(DBusConnection *conn, DBusMessage *msg, void *_u){
+ struct userdata *u;
+ double *H, preamp;
+ uint32_t channel;
+ unsigned _n_coefs;
+ DBusMessage *signal = NULL;
+ DBusError error;
+ pa_assert_se(u = (struct userdata *) _u);
+ pa_assert(conn);
+ pa_assert(msg);
+
+ dbus_error_init(&error);
+ if(!dbus_message_get_args(msg, &error,
+ DBUS_TYPE_UINT32, &channel,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, &H, &_n_coefs,
+ DBUS_TYPE_DOUBLE, &preamp,
+ DBUS_TYPE_INVALID)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "%s", error.message);
+ dbus_error_free(&error);
+ return;
+ }
+ if(channel > u->channels){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "invalid channel: %d", channel);
+ dbus_error_free(&error);
+ return;
+ }
+ if(_n_coefs != FILTER_SIZE(u)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "This filter takes exactly %ld coefficients, you gave %d", FILTER_SIZE(u), _n_coefs);
+ return;
+ }
+ set_filter(u, channel, H, preamp);
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ pa_assert_se((signal = dbus_message_new_signal(u->dbus_path, EQUALIZER_IFACE, equalizer_signals[EQUALIZER_SIGNAL_FILTER_CHANGED].name)));
+ pa_dbus_protocol_send_signal(u->dbus_protocol, signal);
+ dbus_message_unref(signal);
+}
+
+void equalizer_handle_save_profile(DBusConnection *conn, DBusMessage *msg, void *_u) {
+ struct userdata *u = (struct userdata *) _u;
+ char *name;
+ uint32_t channel, r_channel;
+ DBusMessage *signal = NULL;
+ DBusError error;
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+ dbus_error_init(&error);
+
+ if(!dbus_message_get_args(msg, &error,
+ DBUS_TYPE_UINT32, &channel,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INVALID)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "%s", error.message);
+ dbus_error_free(&error);
+ return;
+ }
+ if(channel > u->channels){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "invalid channel: %d", channel);
+ dbus_error_free(&error);
+ return;
+ }
+ r_channel = channel == u->channels ? 0 : channel;
+ save_profile(u, r_channel, name);
+ pa_dbus_send_empty_reply(conn, msg);
+
+ pa_assert_se((signal = dbus_message_new_signal(MANAGER_PATH, MANAGER_IFACE, manager_signals[MANAGER_SIGNAL_PROFILES_CHANGED].name)));
+ pa_dbus_protocol_send_signal(u->dbus_protocol, signal);
+ dbus_message_unref(signal);
+}
+
+void equalizer_handle_load_profile(DBusConnection *conn, DBusMessage *msg, void *_u) {
+ struct userdata *u = (struct userdata *) _u;
+ char *name;
+ DBusError error;
+ uint32_t channel, r_channel;
+ const char *err_msg = NULL;
+ DBusMessage *signal = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+ dbus_error_init(&error);
+
+ if(!dbus_message_get_args(msg, &error,
+ DBUS_TYPE_UINT32, &channel,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INVALID)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "%s", error.message);
+ dbus_error_free(&error);
+ return;
+ }
+ if(channel > u->channels){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "invalid channel: %d", channel);
+ dbus_error_free(&error);
+ return;
+ }
+ r_channel = channel == u->channels ? 0 : channel;
+
+ err_msg = load_profile(u, r_channel, name);
+ if(err_msg != NULL){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "error loading profile %s: %s", name, err_msg);
+ dbus_error_free(&error);
+ return;
+ }
+ if(channel == u->channels){
+ for(uint32_t c = 1; c < u->channels; ++c){
+ load_profile(u, c, name);
+ }
+ }
+ pa_dbus_send_empty_reply(conn, msg);
+
+ pa_assert_se((signal = dbus_message_new_signal(u->dbus_path, EQUALIZER_IFACE, equalizer_signals[EQUALIZER_SIGNAL_FILTER_CHANGED].name)));
+ pa_dbus_protocol_send_signal(u->dbus_protocol, signal);
+ dbus_message_unref(signal);
+}
+
+void equalizer_handle_save_state(DBusConnection *conn, DBusMessage *msg, void *_u) {
+ struct userdata *u = (struct userdata *) _u;
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+
+ save_state(u);
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+void equalizer_handle_get_profile_name(DBusConnection *conn, DBusMessage *msg, void *_u){
+ struct userdata *u = (struct userdata *) _u;
+ DBusError error;
+ uint32_t channel, r_channel;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+ dbus_error_init(&error);
+
+ if(!dbus_message_get_args(msg, &error,
+ DBUS_TYPE_UINT32, &channel,
+ DBUS_TYPE_INVALID)){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "%s", error.message);
+ dbus_error_free(&error);
+ return;
+ }
+ if(channel > u->channels){
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "invalid channel: %d", channel);
+ dbus_error_free(&error);
+ return;
+ }
+ r_channel = channel == u->channels ? 0 : channel;
+ pa_assert(u->base_profiles[r_channel]);
+ pa_dbus_send_basic_value_reply(conn,msg, DBUS_TYPE_STRING, &u->base_profiles[r_channel]);
+}
+
+void equalizer_get_revision(DBusConnection *conn, DBusMessage *msg, void *_u){
+ uint32_t rev=1;
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_UINT32, &rev);
+}
+
+void equalizer_get_n_channels(DBusConnection *conn, DBusMessage *msg, void *_u){
+ struct userdata *u;
+ uint32_t channels;
+ pa_assert_se(u = (struct userdata *) _u);
+ pa_assert(conn);
+ pa_assert(msg);
+
+ channels = (uint32_t) u->channels;
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &channels);
+}
+
+void equalizer_get_n_coefs(DBusConnection *conn, DBusMessage *msg, void *_u){
+ struct userdata *u;
+ uint32_t n_coefs;
+ pa_assert_se(u = (struct userdata *) _u);
+ pa_assert(conn);
+ pa_assert(msg);
+
+ n_coefs = (uint32_t) CHANNEL_PROFILE_SIZE(u);
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &n_coefs);
+}
+
+void equalizer_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *_u){
+ struct userdata *u;
+ uint32_t rate;
+ pa_assert_se(u = (struct userdata *) _u);
+ pa_assert(conn);
+ pa_assert(msg);
+
+ rate = (uint32_t) u->sink->sample_spec.rate;
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &rate);
+}
+
+void equalizer_get_filter_rate(DBusConnection *conn, DBusMessage *msg, void *_u){
+ struct userdata *u;
+ uint32_t fft_size;
+ pa_assert_se(u = (struct userdata *) _u);
+ pa_assert(conn);
+ pa_assert(msg);
+
+ fft_size = (uint32_t) u->fft_size;
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &fft_size);
+}
+
+void equalizer_get_all(DBusConnection *conn, DBusMessage *msg, void *_u){
+ struct userdata *u;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter, dict_iter;
+ uint32_t rev, n_coefs, rate, fft_size, channels;
+
+ pa_assert_se(u = _u);
+ pa_assert(msg);
+
+ rev = 1;
+ n_coefs = (uint32_t) CHANNEL_PROFILE_SIZE(u);
+ rate = (uint32_t) u->sink->sample_spec.rate;
+ fft_size = (uint32_t) u->fft_size;
+ channels = (uint32_t) u->channels;
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, equalizer_handlers[EQUALIZER_HANDLER_REVISION].property_name, DBUS_TYPE_UINT32, &rev);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, equalizer_handlers[EQUALIZER_HANDLER_SAMPLERATE].property_name, DBUS_TYPE_UINT32, &rate);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, equalizer_handlers[EQUALIZER_HANDLER_FILTERSAMPLERATE].property_name, DBUS_TYPE_UINT32, &fft_size);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, equalizer_handlers[EQUALIZER_HANDLER_N_COEFS].property_name, DBUS_TYPE_UINT32, &n_coefs);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, equalizer_handlers[EQUALIZER_HANDLER_N_CHANNELS].property_name, DBUS_TYPE_UINT32, &channels);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+ dbus_message_unref(reply);
+}
diff --git a/src/modules/module-esound-compat-spawnfd.c b/src/modules/module-esound-compat-spawnfd.c
index 54e090e4..617d5a14 100644
--- a/src/modules/module-esound-compat-spawnfd.c
+++ b/src/modules/module-esound-compat-spawnfd.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,8 +24,6 @@
#endif
#include <unistd.h>
-#include <assert.h>
-#include <string.h>
#include <errno.h>
#include <pulsecore/core-error.h>
@@ -36,35 +34,38 @@
#include "module-esound-compat-spawnfd-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("ESOUND compatibility module: -spawnfd emulation")
-PA_MODULE_USAGE("fd=<file descriptor>")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("ESOUND compatibility module: -spawnfd emulation");
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_USAGE("fd=<file descriptor>");
static const char* const valid_modargs[] = {
"fd",
NULL,
};
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
int ret = -1, fd = -1;
char x = 1;
- assert(c && m);
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs)) ||
pa_modargs_get_value_s32(ma, "fd", &fd) < 0 ||
fd < 0) {
- pa_log(__FILE__": Failed to parse module arguments");
+
+ pa_log("Failed to parse module arguments");
goto finish;
}
if (pa_loop_write(fd, &x, sizeof(x), NULL) != sizeof(x))
- pa_log(__FILE__": WARNING: write(%u, 1, 1) failed: %s", fd, pa_cstrerror(errno));
+ pa_log_warn("write(%u, 1, 1) failed: %s", fd, pa_cstrerror(errno));
- close(fd);
+ pa_assert_se(pa_close(fd) == 0);
- pa_module_unload_request(m);
+ pa_module_unload_request(m, TRUE);
ret = 0;
@@ -74,9 +75,3 @@ finish:
return ret;
}
-
-void pa__done(pa_core *c, pa_module*m) {
- assert(c && m);
-}
-
-
diff --git a/src/modules/module-esound-compat-spawnpid.c b/src/modules/module-esound-compat-spawnpid.c
index 3108baf7..94ebdaad 100644
--- a/src/modules/module-esound-compat-spawnpid.c
+++ b/src/modules/module-esound-compat-spawnpid.c
@@ -1,17 +1,19 @@
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -23,46 +25,45 @@
#endif
#include <unistd.h>
-#include <assert.h>
-#include <string.h>
#include <errno.h>
#include <signal.h>
#include <pulsecore/core-error.h>
#include <pulsecore/module.h>
-#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
#include "module-esound-compat-spawnpid-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("ESOUND compatibility module: -spawnpid emulation")
-PA_MODULE_USAGE("pid=<process id>")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("ESOUND compatibility module: -spawnpid emulation");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE("pid=<process id>");
static const char* const valid_modargs[] = {
"pid",
NULL,
};
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
int ret = -1;
uint32_t pid = 0;
- assert(c && m);
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs)) ||
pa_modargs_get_value_u32(ma, "pid", &pid) < 0 ||
!pid) {
- pa_log(__FILE__": Failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto finish;
}
- if (kill(pid, SIGUSR1) < 0)
- pa_log(__FILE__": WARNING: kill(%u) failed: %s", pid, pa_cstrerror(errno));
+ if (kill((pid_t) pid, SIGUSR1) < 0)
+ pa_log_warn("kill(%u) failed: %s", pid, pa_cstrerror(errno));
- pa_module_unload_request(m);
+ pa_module_unload_request(m, TRUE);
ret = 0;
@@ -72,9 +73,3 @@ finish:
return ret;
}
-
-void pa__done(pa_core *c, pa_module*m) {
- assert(c && m);
-}
-
-
diff --git a/src/modules/module-esound-sink.c b/src/modules/module-esound-sink.c
index 86ffaf78..d79054f8 100644
--- a/src/modules/module-esound-sink.c
+++ b/src/modules/module-esound-sink.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,17 +24,32 @@
#endif
#include <stdlib.h>
-#include <sys/stat.h>
#include <stdio.h>
-#include <assert.h>
#include <errno.h>
#include <string.h>
-#include <fcntl.h>
#include <unistd.h>
-#include <limits.h>
+#ifdef HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+
+#ifdef HAVE_NETINET_TCP_H
+#include <netinet/tcp.h>
+#endif
+
+#ifdef HAVE_SYS_IOCTL_H
+#include <sys/ioctl.h>
+#endif
+
+#ifdef HAVE_LINUX_SOCKIOS_H
+#include <linux/sockios.h>
+#endif
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
+#include <pulsecore/socket.h>
#include <pulsecore/core-error.h>
#include <pulsecore/iochannel.h>
#include <pulsecore/sink.h>
@@ -45,168 +60,380 @@
#include <pulsecore/socket-client.h>
#include <pulsecore/esound.h>
#include <pulsecore/authkey.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/time-smoother.h>
+#include <pulsecore/socket-util.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/poll.h>
#include "module-esound-sink-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("ESOUND Sink")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("sink_name=<name for the sink> server=<address> cookie=<filename> format=<sample format> channels=<number of channels> rate=<sample rate>")
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("ESOUND Sink");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "server=<address> cookie=<filename> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels>");
-#define DEFAULT_SINK_NAME "esound_output"
+#define DEFAULT_SINK_NAME "esound_out"
struct userdata {
pa_core *core;
-
+ pa_module *module;
pa_sink *sink;
- pa_iochannel *io;
- pa_socket_client *client;
- pa_defer_event *defer_event;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+ pa_rtpoll_item *rtpoll_item;
+ pa_thread *thread;
pa_memchunk memchunk;
- pa_module *module;
void *write_data;
size_t write_length, write_index;
-
+
void *read_data;
size_t read_length, read_index;
- enum { STATE_AUTH, STATE_LATENCY, STATE_RUNNING, STATE_DEAD } state;
+ enum {
+ STATE_AUTH,
+ STATE_LATENCY,
+ STATE_PREPARE,
+ STATE_RUNNING,
+ STATE_DEAD
+ } state;
pa_usec_t latency;
esd_format_t format;
int32_t rate;
+
+ pa_smoother *smoother;
+ int fd;
+
+ int64_t offset;
+
+ pa_iochannel *io;
+ pa_socket_client *client;
+
+ size_t block_size;
};
static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
"server",
"cookie",
- "rate",
"format",
+ "rate",
"channels",
- "sink_name",
NULL
};
-static void cancel(struct userdata *u) {
- assert(u);
+enum {
+ SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX
+};
+
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
- u->state = STATE_DEAD;
+ switch (code) {
- if (u->io) {
- pa_iochannel_free(u->io);
- u->io = NULL;
- }
+ case PA_SINK_MESSAGE_SET_STATE:
- if (u->defer_event) {
- u->core->mainloop->defer_free(u->defer_event);
- u->defer_event = NULL;
- }
+ switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
- if (u->sink) {
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- u->sink = NULL;
+ case PA_SINK_SUSPENDED:
+ pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
+
+ pa_smoother_pause(u->smoother, pa_rtclock_now());
+ break;
+
+ case PA_SINK_IDLE:
+ case PA_SINK_RUNNING:
+
+ if (u->sink->thread_info.state == PA_SINK_SUSPENDED)
+ pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE);
+
+ break;
+
+ case PA_SINK_UNLINKED:
+ case PA_SINK_INIT:
+ case PA_SINK_INVALID_STATE:
+ ;
+ }
+
+ break;
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ pa_usec_t w, r;
+
+ r = pa_smoother_get(u->smoother, pa_rtclock_now());
+ w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec);
+
+ *((pa_usec_t*) data) = w > r ? w - r : 0;
+ return 0;
+ }
+
+ case SINK_MESSAGE_PASS_SOCKET: {
+ struct pollfd *pollfd;
+
+ pa_assert(!u->rtpoll_item);
+
+ u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->fd = u->fd;
+ pollfd->events = pollfd->revents = 0;
+
+ return 0;
+ }
}
- if (u->module) {
- pa_module_unload_request(u->module);
- u->module = NULL;
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+ int write_type = 0;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ pa_smoother_set_time_offset(u->smoother, pa_rtclock_now());
+
+ for (;;) {
+ int ret;
+
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
+ if (u->sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(u->sink, 0);
+
+ if (u->rtpoll_item) {
+ struct pollfd *pollfd;
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ /* Render some data and write it to the fifo */
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && pollfd->revents) {
+ pa_usec_t usec;
+ int64_t n;
+
+ for (;;) {
+ ssize_t l;
+ void *p;
+
+ if (u->memchunk.length <= 0)
+ pa_sink_render(u->sink, u->block_size, &u->memchunk);
+
+ pa_assert(u->memchunk.length > 0);
+
+ p = pa_memblock_acquire(u->memchunk.memblock);
+ l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
+ pa_memblock_release(u->memchunk.memblock);
+
+ pa_assert(l != 0);
+
+ if (l < 0) {
+
+ if (errno == EINTR)
+ continue;
+ else if (errno == EAGAIN) {
+
+ /* OK, we filled all socket buffers up
+ * now. */
+ goto filled_up;
+
+ } else {
+ pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ } else {
+ u->offset += l;
+
+ u->memchunk.index += (size_t) l;
+ u->memchunk.length -= (size_t) l;
+
+ if (u->memchunk.length <= 0) {
+ pa_memblock_unref(u->memchunk.memblock);
+ pa_memchunk_reset(&u->memchunk);
+ }
+
+ pollfd->revents = 0;
+
+ if (u->memchunk.length > 0)
+
+ /* OK, we wrote less that we asked for,
+ * hence we can assume that the socket
+ * buffers are full now */
+ goto filled_up;
+ }
+ }
+
+ filled_up:
+
+ /* At this spot we know that the socket buffers are
+ * fully filled up. This is the best time to estimate
+ * the playback position of the server */
+
+ n = u->offset;
+
+#ifdef SIOCOUTQ
+ {
+ int l;
+ if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
+ n -= l;
+ }
+#endif
+
+ usec = pa_bytes_to_usec((uint64_t) n, &u->sink->sample_spec);
+
+ if (usec > u->latency)
+ usec -= u->latency;
+ else
+ usec = 0;
+
+ pa_smoother_put(u->smoother, pa_rtclock_now(), usec);
+ }
+
+ /* Hmm, nothing to do. Let's sleep */
+ pollfd->events = (short) (PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0);
+ }
+
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+
+ if (u->rtpoll_item) {
+ struct pollfd* pollfd;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ if (pollfd->revents & ~POLLOUT) {
+ pa_log("FIFO shutdown.");
+ goto fail;
+ }
+ }
}
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
}
static int do_write(struct userdata *u) {
ssize_t r;
- assert(u);
+ pa_assert(u);
if (!pa_iochannel_is_writable(u->io))
return 0;
if (u->write_data) {
- assert(u->write_index < u->write_length);
+ pa_assert(u->write_index < u->write_length);
if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) <= 0) {
- pa_log(__FILE__": write() failed: %s", pa_cstrerror(errno));
+ pa_log("write() failed: %s", pa_cstrerror(errno));
return -1;
}
- u->write_index += r;
- assert(u->write_index <= u->write_length);
-
+ u->write_index += (size_t) r;
+ pa_assert(u->write_index <= u->write_length);
+
if (u->write_index == u->write_length) {
- free(u->write_data);
+ pa_xfree(u->write_data);
u->write_data = NULL;
u->write_index = u->write_length = 0;
}
- } else if (u->state == STATE_RUNNING) {
- pa_module_set_used(u->module, pa_idxset_size(u->sink->inputs) + pa_idxset_size(u->sink->monitor_source->outputs));
-
- if (!u->memchunk.length)
- if (pa_sink_render(u->sink, 8192, &u->memchunk) < 0)
- return 0;
-
- assert(u->memchunk.memblock && u->memchunk.length);
-
- if ((r = pa_iochannel_write(u->io, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length)) < 0) {
- pa_log(__FILE__": write() failed: %s", pa_cstrerror(errno));
- return -1;
- }
+ }
+
+ if (!u->write_data && u->state == STATE_PREPARE) {
+ int so_sndbuf = 0;
+ socklen_t sl = sizeof(int);
+
+ /* OK, we're done with sending all control data we need to, so
+ * let's hand the socket over to the IO thread now */
+
+ pa_assert(u->fd < 0);
+ u->fd = pa_iochannel_get_send_fd(u->io);
+
+ pa_iochannel_set_noclose(u->io, TRUE);
+ pa_iochannel_free(u->io);
+ u->io = NULL;
+
+ pa_make_tcp_socket_low_delay(u->fd);
- u->memchunk.index += r;
- u->memchunk.length -= r;
-
- if (u->memchunk.length <= 0) {
- pa_memblock_unref(u->memchunk.memblock);
- u->memchunk.memblock = NULL;
+ if (getsockopt(u->fd, SOL_SOCKET, SO_SNDBUF, &so_sndbuf, &sl) < 0)
+ pa_log_warn("getsockopt(SO_SNDBUF) failed: %s", pa_cstrerror(errno));
+ else {
+ pa_log_debug("SO_SNDBUF is %zu.", (size_t) so_sndbuf);
+ pa_sink_set_max_request(u->sink, PA_MAX((size_t) so_sndbuf, u->block_size));
}
+
+ pa_log_debug("Connection authenticated, handing fd to IO thread...");
+
+ pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
+ u->state = STATE_RUNNING;
}
-
+
return 0;
}
static int handle_response(struct userdata *u) {
- assert(u);
+ pa_assert(u);
switch (u->state) {
+
case STATE_AUTH:
- assert(u->read_length == sizeof(int32_t));
+ pa_assert(u->read_length == sizeof(int32_t));
/* Process auth data */
if (!*(int32_t*) u->read_data) {
- pa_log(__FILE__": Authentication failed: %s", pa_cstrerror(errno));
+ pa_log("Authentication failed: %s", pa_cstrerror(errno));
return -1;
}
/* Request latency data */
- assert(!u->write_data);
+ pa_assert(!u->write_data);
*(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
u->write_index = 0;
u->state = STATE_LATENCY;
/* Space for next response */
- assert(u->read_length >= sizeof(int32_t));
+ pa_assert(u->read_length >= sizeof(int32_t));
u->read_index = 0;
u->read_length = sizeof(int32_t);
-
+
break;
case STATE_LATENCY: {
int32_t *p;
- assert(u->read_length == sizeof(int32_t));
+ pa_assert(u->read_length == sizeof(int32_t));
/* Process latency info */
u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
if (u->latency > 10000000) {
- pa_log(__FILE__": WARNING! Invalid latency information received from server");
+ pa_log_warn("Invalid latency information received from server");
u->latency = 0;
}
/* Create stream */
- assert(!u->write_data);
+ pa_assert(!u->write_data);
p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
*(p++) = ESD_PROTO_STREAM_PLAY;
*(p++) = u->format;
@@ -214,45 +441,44 @@ static int handle_response(struct userdata *u) {
pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
u->write_index = 0;
- u->state = STATE_RUNNING;
+ u->state = STATE_PREPARE;
/* Don't read any further */
pa_xfree(u->read_data);
u->read_data = NULL;
u->read_index = u->read_length = 0;
-
+
break;
}
-
+
default:
- abort();
+ pa_assert_not_reached();
}
return 0;
}
static int do_read(struct userdata *u) {
- assert(u);
-
+ pa_assert(u);
+
if (!pa_iochannel_is_readable(u->io))
return 0;
-
+
if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
ssize_t r;
-
+
if (!u->read_data)
return 0;
-
- assert(u->read_index < u->read_length);
-
+
+ pa_assert(u->read_index < u->read_length);
+
if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
- pa_log(__FILE__": read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
- cancel(u);
+ pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
return -1;
}
- u->read_index += r;
- assert(u->read_index <= u->read_length);
+ u->read_index += (size_t) r;
+ pa_assert(u->read_index <= u->read_length);
if (u->read_index == u->read_length)
return handle_response(u);
@@ -261,169 +487,225 @@ static int do_read(struct userdata *u) {
return 0;
}
-static void do_work(struct userdata *u) {
- assert(u);
-
- u->core->mainloop->defer_enable(u->defer_event, 0);
-
- if (do_read(u) < 0 || do_write(u) < 0)
- cancel(u);
-}
-
-static void notify_cb(pa_sink*s) {
- struct userdata *u = s->userdata;
- assert(s && u);
-
- if (pa_iochannel_is_writable(u->io))
- u->core->mainloop->defer_enable(u->defer_event, 1);
-}
-
-static pa_usec_t get_latency_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- assert(s && u);
+static void io_callback(pa_iochannel *io, void*userdata) {
+ struct userdata *u = userdata;
+ pa_assert(u);
- return
- u->latency +
- (u->memchunk.memblock ? pa_bytes_to_usec(u->memchunk.length, &s->sample_spec) : 0);
-}
+ if (do_read(u) < 0 || do_write(u) < 0) {
-static void defer_callback(PA_GCC_UNUSED pa_mainloop_api *m, PA_GCC_UNUSED pa_defer_event*e, void *userdata) {
- struct userdata *u = userdata;
- assert(u);
- do_work(u);
-}
+ if (u->io) {
+ pa_iochannel_free(u->io);
+ u->io = NULL;
+ }
-static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
- struct userdata *u = userdata;
- assert(u);
- do_work(u);
+ pa_module_unload_request(u->module, TRUE);
+ }
}
-static void on_connection(PA_GCC_UNUSED pa_socket_client *c, pa_iochannel*io, void *userdata) {
+static void on_connection(pa_socket_client *c, pa_iochannel*io, void *userdata) {
struct userdata *u = userdata;
pa_socket_client_unref(u->client);
u->client = NULL;
-
+
if (!io) {
- pa_log(__FILE__": connection failed: %s", pa_cstrerror(errno));
- cancel(u);
+ pa_log("Connection failed: %s", pa_cstrerror(errno));
+ pa_module_unload_request(u->module, TRUE);
return;
}
-
+
+ pa_assert(!u->io);
u->io = io;
pa_iochannel_set_callback(u->io, io_callback, u);
+
+ pa_log_debug("Connection established, authenticating ...");
}
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
struct userdata *u = NULL;
- const char *p;
pa_sample_spec ss;
pa_modargs *ma = NULL;
- assert(c && m);
-
+ const char *espeaker;
+ uint32_t key;
+ pa_sink_new_data data;
+
+ pa_assert(m);
+
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
+ pa_log("failed to parse module arguments");
goto fail;
}
- ss = c->default_sample_spec;
+ ss = m->core->default_sample_spec;
if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
- pa_log(__FILE__": invalid sample format specification");
+ pa_log("invalid sample format specification");
goto fail;
}
if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
(ss.channels > 2)) {
- pa_log(__FILE__": esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
+ pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
goto fail;
}
-
- u = pa_xmalloc0(sizeof(struct userdata));
- u->core = c;
+
+ u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
u->module = m;
m->userdata = u;
+ u->fd = -1;
+ u->smoother = pa_smoother_new(
+ PA_USEC_PER_SEC,
+ PA_USEC_PER_SEC*2,
+ TRUE,
+ TRUE,
+ 10,
+ 0,
+ FALSE);
+ pa_memchunk_reset(&u->memchunk);
+ u->offset = 0;
+
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+ u->rtpoll_item = NULL;
+
u->format =
(ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
(ss.channels == 2 ? ESD_STEREO : ESD_MONO);
- u->rate = ss.rate;
- u->sink = NULL;
- u->client = NULL;
- u->io = NULL;
+ u->rate = (int32_t) ss.rate;
+ u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
+
u->read_data = u->write_data = NULL;
u->read_index = u->write_index = u->read_length = u->write_length = 0;
+
u->state = STATE_AUTH;
u->latency = 0;
- if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) {
- pa_log(__FILE__": failed to create sink.");
+ if (!(espeaker = getenv("ESPEAKER")))
+ espeaker = ESD_UNIX_SOCKET_NAME;
+
+ espeaker = pa_modargs_get_value(ma, "server", espeaker);
+
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, espeaker);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "esd");
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "EsounD Output on %s", espeaker);
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&data);
goto fail;
}
- if (!(u->client = pa_socket_client_new_string(u->core->mainloop, p = pa_modargs_get_value(ma, "server", ESD_UNIX_SOCKET_NAME), ESD_DEFAULT_PORT))) {
- pa_log(__FILE__": failed to connect to server.");
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
goto fail;
}
+
+ u->sink->parent.process_msg = sink_process_msg;
+ u->sink->userdata = u;
+
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+
+ if (!(u->client = pa_socket_client_new_string(u->core->mainloop, TRUE, espeaker, ESD_DEFAULT_PORT))) {
+ pa_log("Failed to connect to server.");
+ goto fail;
+ }
+
pa_socket_client_set_callback(u->client, on_connection, u);
/* Prepare the initial request */
u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", ".esd_auth"), u->write_data, ESD_KEY_LEN) < 0) {
- pa_log(__FILE__": failed to load cookie");
+ pa_log("Failed to load cookie");
goto fail;
}
- *(int32_t*) ((uint8_t*) u->write_data + ESD_KEY_LEN) = ESD_ENDIAN_KEY;
+
+ key = ESD_ENDIAN_KEY;
+ memcpy((uint8_t*) u->write_data + ESD_KEY_LEN, &key, sizeof(key));
/* Reserve space for the response */
u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
-
- u->sink->notify = notify_cb;
- u->sink->get_latency = get_latency_cb;
- u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Esound sink '%s'", p);
- u->memchunk.memblock = NULL;
- u->memchunk.length = 0;
+ if (!(u->thread = pa_thread_new("esound-sink", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
- u->defer_event = c->mainloop->defer_new(c->mainloop, defer_callback, u);
- c->mainloop->defer_enable(u->defer_event, 0);
+ pa_sink_put(u->sink);
-
pa_modargs_free(ma);
-
+
return 0;
fail:
if (ma)
pa_modargs_free(ma);
-
- pa__done(c, m);
+
+ pa__done(m);
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
struct userdata *u;
- assert(c && m);
+ pa_assert(m);
if (!(u = m->userdata))
return;
- u->module = NULL;
- cancel(u);
-
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->io)
+ pa_iochannel_free(u->io);
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
if (u->memchunk.memblock)
pa_memblock_unref(u->memchunk.memblock);
if (u->client)
pa_socket_client_unref(u->client);
-
+
pa_xfree(u->read_data);
pa_xfree(u->write_data);
- pa_xfree(u);
-}
-
+ if (u->smoother)
+ pa_smoother_free(u->smoother);
+ if (u->fd >= 0)
+ pa_close(u->fd);
+ pa_xfree(u);
+}
diff --git a/src/modules/module-filter-apply.c b/src/modules/module-filter-apply.c
new file mode 100644
index 00000000..c742373a
--- /dev/null
+++ b/src/modules/module-filter-apply.c
@@ -0,0 +1,600 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2011 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/timeval.h>
+#include <pulse/rtclock.h>
+#include <pulse/i18n.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/macro.h>
+#include <pulsecore/hashmap.h>
+#include <pulsecore/hook-list.h>
+#include <pulsecore/core.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/modargs.h>
+
+#include "module-filter-apply-symdef.h"
+
+#define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
+
+PA_MODULE_AUTHOR("Colin Guthrie");
+PA_MODULE_DESCRIPTION("Load filter sinks automatically when needed");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(_("autoclean=<automatically unload unused filters?>"));
+
+static const char* const valid_modargs[] = {
+ "autoclean",
+ NULL
+};
+
+#define DEFAULT_AUTOCLEAN TRUE
+#define HOUSEKEEPING_INTERVAL (10 * PA_USEC_PER_SEC)
+
+struct filter {
+ char *name;
+ uint32_t module_index;
+ pa_bool_t is_sink;
+ pa_object *parent_obj; /* source or sink that the filter is connected to */
+ pa_object *obj; /* source or sink of the filter */
+};
+
+struct userdata {
+ pa_core *core;
+ pa_hashmap *filters;
+ pa_hook_slot
+ *sink_input_put_slot,
+ *sink_input_move_finish_slot,
+ *sink_input_proplist_slot,
+ *sink_input_unlink_slot,
+ *sink_unlink_slot,
+ *source_output_put_slot,
+ *source_output_move_finish_slot,
+ *source_output_proplist_slot,
+ *source_output_unlink_slot,
+ *source_unlink_slot;
+ pa_bool_t autoclean;
+ pa_time_event *housekeeping_time_event;
+};
+
+static unsigned filter_hash(const void *p) {
+ const struct filter *f = p;
+
+ if (f->is_sink)
+ return (unsigned) (PA_SINK(f->parent_obj)->index + pa_idxset_string_hash_func(f->name));
+ else
+ return (unsigned) ((PA_SOURCE(f->parent_obj)->index << 16) + pa_idxset_string_hash_func(f->name));
+}
+
+static int filter_compare(const void *a, const void *b) {
+ const struct filter *fa = a, *fb = b;
+ int r;
+
+ if (fa->parent_obj != fb->parent_obj)
+ return 1;
+ if ((r = strcmp(fa->name, fb->name)))
+ return r;
+
+ return 0;
+}
+
+static struct filter *filter_new(const char *name, pa_object* parent_obj, pa_bool_t is_sink) {
+ struct filter *f;
+
+ f = pa_xnew(struct filter, 1);
+ f->name = pa_xstrdup(name);
+ pa_assert_se(f->parent_obj = parent_obj);
+ f->is_sink = is_sink;
+ f->module_index = PA_INVALID_INDEX;
+ f->obj = NULL;
+ return f;
+}
+
+static void filter_free(struct filter *f) {
+ pa_assert(f);
+
+ pa_xfree(f->name);
+ pa_xfree(f);
+}
+
+static const char* should_filter(pa_object *o, pa_bool_t is_sink_input) {
+ const char *apply;
+ pa_proplist *pl;
+
+ if (is_sink_input)
+ pl = PA_SINK_INPUT(o)->proplist;
+ else
+ pl = PA_SOURCE_OUTPUT(o)->proplist;
+
+ /* If the stream doesn't what any filter, then let it be. */
+ if ((apply = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY)) && !pa_streq(apply, "")) {
+ const char* suppress = pa_proplist_gets(pl, PA_PROP_FILTER_SUPPRESS);
+
+ if (!suppress || !pa_streq(suppress, apply))
+ return apply;
+ }
+
+ return NULL;
+}
+
+static pa_bool_t nothing_attached(pa_object *obj, pa_bool_t is_sink)
+{
+ if (is_sink)
+ return pa_idxset_isempty(PA_SINK(obj)->inputs);
+ else
+ return pa_idxset_isempty(PA_SOURCE(obj)->outputs);
+}
+
+static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
+ struct userdata *u = userdata;
+ struct filter *filter;
+ void *state;
+
+ pa_assert(a);
+ pa_assert(e);
+ pa_assert(u);
+
+ pa_assert(e == u->housekeeping_time_event);
+ u->core->mainloop->time_free(u->housekeeping_time_event);
+ u->housekeeping_time_event = NULL;
+
+ PA_HASHMAP_FOREACH(filter, u->filters, state) {
+ if (filter->obj && nothing_attached(filter->obj, filter->is_sink)) {
+ uint32_t idx;
+
+ pa_log_debug("Detected filter %s as no longer used. Unloading.", filter->name);
+ idx = filter->module_index;
+ pa_hashmap_remove(u->filters, filter);
+ filter_free(filter);
+ pa_module_unload_request_by_index(u->core, idx, TRUE);
+ }
+ }
+
+ pa_log_info("Housekeeping Done.");
+}
+
+static void trigger_housekeeping(struct userdata *u) {
+ pa_assert(u);
+
+ if (!u->autoclean)
+ return;
+
+ if (u->housekeeping_time_event)
+ return;
+
+ u->housekeeping_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + HOUSEKEEPING_INTERVAL, housekeeping_time_callback, u);
+}
+
+static int do_move(pa_object *obj, pa_object *parent, pa_bool_t restore, pa_bool_t is_input) {
+ if (is_input)
+ return pa_sink_input_move_to(PA_SINK_INPUT(obj), PA_SINK(parent), restore);
+ else
+ return pa_source_output_move_to(PA_SOURCE_OUTPUT(obj), PA_SOURCE(parent), restore);
+}
+
+static void move_object_for_filter(pa_object *o, struct filter* filter, pa_bool_t restore, pa_bool_t is_sink_input) {
+ pa_object *parent;
+ pa_proplist *pl;
+ const char *name;
+
+ pa_assert(o);
+ pa_assert(filter);
+
+ pa_assert_se(parent = (restore ? filter->parent_obj : filter->obj));
+
+ if (is_sink_input) {
+ pl = PA_SINK_INPUT(o)->proplist;
+ name = PA_SINK(parent)->name;
+ } else {
+ pl = PA_SOURCE_OUTPUT(o)->proplist;
+ name = PA_SOURCE(parent)->name;
+ }
+
+ pa_proplist_sets(pl, PA_PROP_FILTER_APPLY_MOVING, "1");
+
+ if (do_move(o, parent, FALSE, is_sink_input) < 0)
+ pa_log_info("Failed to move %s for \"%s\" to <%s>.", is_sink_input ? "sink-input" : "source-output",
+ pa_strnull(pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)), name);
+ else
+ pa_log_info("Sucessfully moved %s for \"%s\" to <%s>.", is_sink_input ? "sink-input" : "source-output",
+ pa_strnull(pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)), name);
+
+ pa_proplist_unset(pl, PA_PROP_FILTER_APPLY_MOVING);
+}
+
+static void find_filters_for_module(struct userdata *u, pa_module *m, const char *name) {
+ uint32_t idx;
+ pa_sink *sink;
+ pa_source *source;
+ struct filter *fltr;
+
+ PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
+ if (sink->module == m) {
+ pa_assert(sink->input_to_master != NULL);
+
+ fltr = filter_new(name, PA_OBJECT(sink->input_to_master->sink), TRUE);
+ fltr->module_index = m->index;
+ fltr->obj = PA_OBJECT(sink);
+
+ pa_hashmap_put(u->filters, fltr, fltr);
+ }
+ }
+
+ PA_IDXSET_FOREACH(source, u->core->sources, idx) {
+ if (source->module == m && !source->monitor_of) {
+ pa_assert(source->output_from_master != NULL);
+
+ fltr = filter_new(name, PA_OBJECT(source->output_from_master->source), FALSE);
+ fltr->module_index = m->index;
+ fltr->obj = PA_OBJECT(source);
+
+ pa_hashmap_put(u->filters, fltr, fltr);
+ }
+ }
+}
+
+static pa_bool_t can_unload_module(struct userdata *u, uint32_t idx) {
+ void *state;
+ struct filter *filter;
+
+ /* Check if any other struct filters point to the same module */
+ PA_HASHMAP_FOREACH(filter, u->filters, state) {
+ if (filter->module_index == idx && !nothing_attached(filter->obj, pa_sink_isinstance(filter->obj)))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
+ const char *want;
+ pa_bool_t done_something = FALSE;
+
+ pa_object *parent; /* source/sink of the given source-output/sink-input */
+ const char *parent_name;
+ pa_module *module;
+
+ if (is_sink_input) {
+ parent = PA_OBJECT(PA_SINK_INPUT(o)->sink);
+ parent_name = PA_SINK_INPUT(o)->sink->name;
+ module = PA_SINK_INPUT(o)->sink->module;
+ } else {
+ parent = PA_OBJECT(PA_SOURCE_OUTPUT(o)->source);
+ parent_name = PA_SOURCE_OUTPUT(o)->source->name;
+ module = PA_SOURCE_OUTPUT(o)->source->module;
+ }
+
+ /* If there is no sink yet, we can't do much */
+ if (!parent)
+ return PA_HOOK_OK;
+
+ /* If the stream doesn't what any filter, then let it be. */
+ if ((want = should_filter(o, is_sink_input))) {
+ char *module_name;
+ struct filter *fltr, *filter;
+
+ /* We need to ensure the SI is playing on a sink of this type
+ * attached to the sink it's "officially" playing on */
+
+ if (!module)
+ return PA_HOOK_OK;
+
+ module_name = pa_sprintf_malloc("module-%s", want);
+ if (pa_streq(module->name, module_name)) {
+ pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
+ pa_xfree(module_name);
+ return PA_HOOK_OK;
+ }
+
+ fltr = filter_new(want, parent, is_sink_input);
+
+ if (!(filter = pa_hashmap_get(u->filters, fltr))) {
+ char *args;
+ pa_module *m;
+
+ args = pa_sprintf_malloc("autoloaded=1 %s_master=%s", is_sink_input ? "sink" : "source", parent_name);
+ pa_log_debug("Loading %s with arguments '%s'", module_name, args);
+
+ if ((m = pa_module_load(u->core, module_name, args))) {
+ find_filters_for_module(u, m, want);
+ filter = pa_hashmap_get(u->filters, fltr);
+ done_something = TRUE;
+ }
+ pa_xfree(args);
+ }
+
+ pa_xfree(fltr);
+
+ if (!filter) {
+ pa_log("Unable to load %s for <%s>", module_name, parent_name);
+ pa_xfree(module_name);
+ return PA_HOOK_OK;
+ }
+ pa_xfree(module_name);
+
+ if (filter->obj) {
+ /* We can move the sink_input now as the know the destination.
+ * If this isn't true, we will do it later when the sink appears. */
+ move_object_for_filter(o, filter, FALSE, is_sink_input);
+ done_something = TRUE;
+ }
+ } else {
+ void *state;
+ struct filter *filter = NULL;
+
+ /* We do not want to filter... but are we already filtered?
+ * This can happen if an input's proplist changes */
+ PA_HASHMAP_FOREACH(filter, u->filters, state) {
+ if (parent == filter->obj) {
+ move_object_for_filter(o, filter, TRUE, is_sink_input);
+ done_something = TRUE;
+ break;
+ }
+ }
+ }
+
+ if (done_something)
+ trigger_housekeeping(u);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_sink_input_assert_ref(i);
+
+ return process(u, PA_OBJECT(i), TRUE);
+}
+
+static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_sink_input_assert_ref(i);
+
+ if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
+ return PA_HOOK_OK;
+
+ return process(u, PA_OBJECT(i), TRUE);
+}
+
+static pa_hook_result_t sink_input_proplist_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_sink_input_assert_ref(i);
+
+ return process(u, PA_OBJECT(i), TRUE);
+}
+
+static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_sink_input_assert_ref(i);
+
+ pa_assert(u);
+
+ if (pa_hashmap_size(u->filters) > 0)
+ trigger_housekeeping(u);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct userdata *u) {
+ void *state;
+ struct filter *filter = NULL;
+
+ pa_core_assert_ref(core);
+ pa_sink_assert_ref(sink);
+ pa_assert(u);
+
+ /* If either the parent or the sink we've loaded disappears,
+ * we should remove it from our hashmap */
+ PA_HASHMAP_FOREACH(filter, u->filters, state) {
+ if (filter->parent_obj == PA_OBJECT(sink) || filter->obj == PA_OBJECT(sink)) {
+ uint32_t idx;
+
+ /* Attempt to rescue any streams to the parent sink as this is likely
+ * the best course of action (as opposed to a generic rescue via
+ * module-rescue-streams */
+ if (filter->obj == PA_OBJECT(sink)) {
+ pa_sink_input *i;
+
+ PA_IDXSET_FOREACH(i, sink->inputs, idx)
+ move_object_for_filter(PA_OBJECT(i), filter, TRUE, TRUE);
+ }
+
+ idx = filter->module_index;
+ pa_hashmap_remove(u->filters, filter);
+ filter_free(filter);
+
+ if (can_unload_module(u, idx))
+ pa_module_unload_request_by_index(u->core, idx, TRUE);
+ }
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_source_output_assert_ref(o);
+
+ return process(u, PA_OBJECT(o), FALSE);
+}
+
+static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_source_output_assert_ref(o);
+
+ if (pa_proplist_gets(o->proplist, PA_PROP_FILTER_APPLY_MOVING))
+ return PA_HOOK_OK;
+
+ return process(u, PA_OBJECT(o), FALSE);
+}
+
+static pa_hook_result_t source_output_proplist_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_source_output_assert_ref(o);
+
+ return process(u, PA_OBJECT(o), FALSE);
+}
+
+static pa_hook_result_t source_output_unlink_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_source_output_assert_ref(o);
+
+ pa_assert(u);
+
+ if (pa_hashmap_size(u->filters) > 0)
+ trigger_housekeeping(u);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_unlink_cb(pa_core *core, pa_source *source, struct userdata *u) {
+ void *state;
+ struct filter *filter = NULL;
+
+ pa_core_assert_ref(core);
+ pa_source_assert_ref(source);
+ pa_assert(u);
+
+ /* If either the parent or the source we've loaded disappears,
+ * we should remove it from our hashmap */
+ PA_HASHMAP_FOREACH(filter, u->filters, state) {
+ if (filter->parent_obj == PA_OBJECT(source) || filter->obj == PA_OBJECT(source)) {
+ uint32_t idx;
+
+ /* Attempt to rescue any streams to the parent source as this is likely
+ * the best course of action (as opposed to a generic rescue via
+ * module-rescue-streams */
+ if (filter->obj == PA_OBJECT(source)) {
+ pa_source_output *o;
+
+ PA_IDXSET_FOREACH(o, source->outputs, idx)
+ move_object_for_filter(PA_OBJECT(o), filter, TRUE, FALSE);
+ }
+
+ idx = filter->module_index;
+ pa_hashmap_remove(u->filters, filter);
+ filter_free(filter);
+
+ if (can_unload_module(u, idx))
+ pa_module_unload_request_by_index(u->core, idx, TRUE);
+ }
+ }
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module *m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+
+ u->core = m->core;
+
+ u->autoclean = DEFAULT_AUTOCLEAN;
+ if (pa_modargs_get_value_boolean(ma, "autoclean", &u->autoclean) < 0) {
+ pa_log("Failed to parse autoclean value");
+ goto fail;
+ }
+
+ u->filters = pa_hashmap_new(filter_hash, filter_compare);
+
+ u->sink_input_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_put_cb, u);
+ u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_move_finish_cb, u);
+ u->sink_input_proplist_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_proplist_cb, u);
+ u->sink_input_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_unlink_cb, u);
+ u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_unlink_cb, u);
+ u->source_output_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], PA_HOOK_LATE, (pa_hook_cb_t) source_output_put_cb, u);
+ u->source_output_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_LATE, (pa_hook_cb_t) source_output_move_finish_cb, u);
+ u->source_output_proplist_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) source_output_proplist_cb, u);
+ u->source_output_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_output_unlink_cb, u);
+ u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_unlink_cb, u);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module *m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink_input_put_slot)
+ pa_hook_slot_free(u->sink_input_put_slot);
+ if (u->sink_input_move_finish_slot)
+ pa_hook_slot_free(u->sink_input_move_finish_slot);
+ if (u->sink_input_proplist_slot)
+ pa_hook_slot_free(u->sink_input_proplist_slot);
+ if (u->sink_input_unlink_slot)
+ pa_hook_slot_free(u->sink_input_unlink_slot);
+ if (u->sink_unlink_slot)
+ pa_hook_slot_free(u->sink_unlink_slot);
+ if (u->source_output_put_slot)
+ pa_hook_slot_free(u->source_output_put_slot);
+ if (u->source_output_move_finish_slot)
+ pa_hook_slot_free(u->source_output_move_finish_slot);
+ if (u->source_output_proplist_slot)
+ pa_hook_slot_free(u->source_output_proplist_slot);
+ if (u->source_output_unlink_slot)
+ pa_hook_slot_free(u->source_output_unlink_slot);
+ if (u->source_unlink_slot)
+ pa_hook_slot_free(u->source_unlink_slot);
+
+ if (u->housekeeping_time_event)
+ u->core->mainloop->time_free(u->housekeeping_time_event);
+
+ if (u->filters) {
+ struct filter *f;
+
+ while ((f = pa_hashmap_steal_first(u->filters))) {
+ pa_module_unload_request_by_index(u->core, f->module_index, TRUE);
+ filter_free(f);
+ }
+
+ pa_hashmap_free(u->filters, NULL, NULL);
+ }
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-filter-heuristics.c b/src/modules/module-filter-heuristics.c
new file mode 100644
index 00000000..222787fc
--- /dev/null
+++ b/src/modules/module-filter-heuristics.c
@@ -0,0 +1,217 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2011 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/macro.h>
+#include <pulsecore/hook-list.h>
+#include <pulsecore/core.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/modargs.h>
+
+#include "module-filter-heuristics-symdef.h"
+
+#define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
+#define PA_PROP_FILTER_HEURISTICS "filter.heuristics"
+
+PA_MODULE_AUTHOR("Colin Guthrie");
+PA_MODULE_DESCRIPTION("Detect when various filters are desirable");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+static const char* const valid_modargs[] = {
+ NULL
+};
+
+struct userdata {
+ pa_core *core;
+ pa_hook_slot
+ *sink_input_put_slot,
+ *sink_input_move_finish_slot,
+ *source_output_put_slot,
+ *source_output_move_finish_slot;
+};
+
+static pa_bool_t role_match(pa_proplist *proplist, const char *role) {
+ const char *ir;
+ char *r;
+ const char *state = NULL;
+
+ if (!(ir = pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES)))
+ return FALSE;
+
+ while ((r = pa_split_spaces(ir, &state))) {
+
+ if (pa_streq(role, r)) {
+ pa_xfree(r);
+ return TRUE;
+ }
+
+ pa_xfree(r);
+ }
+
+ return FALSE;
+}
+
+static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
+ const char *want, *stream_role;
+ pa_proplist *pl, *parent_pl;
+
+ if (is_sink_input) {
+ pl = PA_SINK_INPUT(o)->proplist;
+ parent_pl = PA_SINK_INPUT(o)->sink->proplist;
+ } else {
+ pl = PA_SOURCE_OUTPUT(o)->proplist;
+ parent_pl = PA_SOURCE_OUTPUT(o)->source->proplist;
+ }
+
+ /* If the stream already specifies what it must have, then let it be. */
+ if (!pa_proplist_gets(pl, PA_PROP_FILTER_HEURISTICS) && pa_proplist_gets(pl, PA_PROP_FILTER_APPLY))
+ return PA_HOOK_OK;
+
+ want = pa_proplist_gets(pl, PA_PROP_FILTER_WANT);
+ if (!want) {
+ /* This is a phone stream, maybe we want echo cancellation */
+ if ((stream_role = pa_proplist_gets(pl, PA_PROP_MEDIA_ROLE)) && pa_streq(stream_role, "phone"))
+ want = "echo-cancel";
+ }
+
+ /* On phone sinks, make sure we're not applying echo cancellation */
+ if (role_match(parent_pl, "phone")) {
+ const char *apply = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY);
+
+ if (apply && pa_streq(apply, "echo-cancel")) {
+ pa_proplist_unset(pl, PA_PROP_FILTER_APPLY);
+ pa_proplist_unset(pl, PA_PROP_FILTER_HEURISTICS);
+ }
+
+ return PA_HOOK_OK;
+ }
+
+ if (want) {
+ /* There's a filter that we want, ask module-filter-apply to apply it, and remember that we're managing filter.apply */
+ pa_proplist_sets(pl, PA_PROP_FILTER_APPLY, want);
+ pa_proplist_sets(pl, PA_PROP_FILTER_HEURISTICS, "1");
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_sink_input_assert_ref(i);
+ pa_assert(u);
+
+ return process(u, PA_OBJECT(i), TRUE);
+}
+
+static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_sink_input_assert_ref(i);
+ pa_assert(u);
+
+ /* module-filter-apply triggered this move, ignore */
+ if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
+ return PA_HOOK_OK;
+
+ return process(u, PA_OBJECT(i), TRUE);
+}
+
+static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_source_output_assert_ref(i);
+ pa_assert(u);
+
+ return process(u, PA_OBJECT(i), FALSE);
+}
+
+static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
+ pa_core_assert_ref(core);
+ pa_source_output_assert_ref(i);
+ pa_assert(u);
+
+ /* module-filter-apply triggered this move, ignore */
+ if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
+ return PA_HOOK_OK;
+
+ return process(u, PA_OBJECT(i), FALSE);
+}
+
+int pa__init(pa_module *m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+
+ u->core = m->core;
+
+ u->sink_input_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], PA_HOOK_LATE-1, (pa_hook_cb_t) sink_input_put_cb, u);
+ u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_LATE-1, (pa_hook_cb_t) sink_input_move_finish_cb, u);
+ u->source_output_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], PA_HOOK_LATE-1, (pa_hook_cb_t) source_output_put_cb, u);
+ u->source_output_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_LATE-1, (pa_hook_cb_t) source_output_move_finish_cb, u);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+
+
+}
+
+void pa__done(pa_module *m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink_input_put_slot)
+ pa_hook_slot_free(u->sink_input_put_slot);
+ if (u->sink_input_move_finish_slot)
+ pa_hook_slot_free(u->sink_input_move_finish_slot);
+ if (u->source_output_put_slot)
+ pa_hook_slot_free(u->source_output_put_slot);
+ if (u->source_output_move_finish_slot)
+ pa_hook_slot_free(u->source_output_move_finish_slot);
+
+ pa_xfree(u);
+
+}
diff --git a/src/modules/module-hal-detect-compat.c b/src/modules/module-hal-detect-compat.c
new file mode 100644
index 00000000..14cf8143
--- /dev/null
+++ b/src/modules/module-hal-detect-compat.c
@@ -0,0 +1,84 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-util.h>
+
+#include "module-hal-detect-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Compatibility module");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_DEPRECATED("Please use module-udev-detect instead of module-hal-detect!");
+
+static const char* const valid_modargs[] = {
+ "api",
+ "tsched",
+ "subdevices",
+ NULL,
+};
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ pa_bool_t tsched = TRUE;
+ pa_module *n;
+ char *t;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "tsched", &tsched) < 0) {
+ pa_log("tsched= expects boolean arguments");
+ goto fail;
+ }
+
+ pa_log_warn("We will now load module-udev-detect. Please make sure to remove module-hal-detect from your configuration.");
+
+ t = pa_sprintf_malloc("tsched=%s", pa_yes_no(tsched));
+ n = pa_module_load(m->core, "module-udev-detect", t);
+ pa_xfree(t);
+
+ if (n)
+ pa_module_unload_request(m, TRUE);
+
+ pa_modargs_free(ma);
+
+ return n ? 0 : -1;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
diff --git a/src/modules/module-hal-detect.c b/src/modules/module-hal-detect.c
new file mode 100644
index 00000000..62f0f203
--- /dev/null
+++ b/src/modules/module-hal-detect.c
@@ -0,0 +1,860 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006 Lennart Poettering
+ Copyright 2006 Shams E. King
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/log.h>
+#include <pulsecore/hashmap.h>
+#include <pulsecore/idxset.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/dbus-shared.h>
+
+#include <hal/libhal.h>
+
+#include "module-hal-detect-symdef.h"
+
+PA_MODULE_AUTHOR("Shahms King");
+PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+#if defined(HAVE_ALSA) && defined(HAVE_OSS_OUTPUT)
+PA_MODULE_USAGE("api=<alsa or oss> "
+ "tsched=<enable system timer based scheduling mode?> "
+ "subdevices=<init all subdevices>");
+#elif defined(HAVE_ALSA)
+PA_MODULE_USAGE("api=<alsa> "
+ "tsched=<enable system timer based scheduling mode?>");
+#elif defined(HAVE_OSS_OUTPUT)
+PA_MODULE_USAGE("api=<oss> "
+ "subdevices=<init all subdevices>");
+#endif
+PA_MODULE_DEPRECATED("Please use module-udev-detect instead of module-hal-detect!");
+
+struct device {
+ char *udi, *originating_udi;
+ char *card_name, *sink_name, *source_name;
+ uint32_t module;
+ pa_bool_t acl_race_fix;
+};
+
+struct userdata {
+ pa_core *core;
+ LibHalContext *context;
+ pa_dbus_connection *connection;
+ pa_hashmap *devices; /* Every entry is indexed twice in this table: by the udi we found the device with and by the originating device's udi */
+ const char *capability;
+#ifdef HAVE_ALSA
+ pa_bool_t use_tsched;
+#endif
+#ifdef HAVE_OSS_OUTPUT
+ pa_bool_t init_subdevs;
+#endif
+ pa_bool_t filter_added:1;
+};
+
+#define CAPABILITY_ALSA "alsa"
+#define CAPABILITY_OSS "oss"
+
+static const char* const valid_modargs[] = {
+ "api",
+#ifdef HAVE_ALSA
+ "tsched",
+#endif
+#ifdef HAVE_OSS_OUTPUT
+ "subdevices",
+#endif
+ NULL
+};
+
+static void device_free(struct device* d) {
+ pa_assert(d);
+
+ pa_xfree(d->udi);
+ pa_xfree(d->originating_udi);
+ pa_xfree(d->sink_name);
+ pa_xfree(d->source_name);
+ pa_xfree(d->card_name);
+ pa_xfree(d);
+}
+
+static const char *strip_udi(const char *udi) {
+ const char *slash;
+
+ pa_assert(udi);
+
+ if ((slash = strrchr(udi, '/')))
+ return slash+1;
+
+ return udi;
+}
+
+#ifdef HAVE_ALSA
+
+enum alsa_type {
+ ALSA_TYPE_PLAYBACK,
+ ALSA_TYPE_CAPTURE,
+ ALSA_TYPE_CONTROL,
+ ALSA_TYPE_OTHER
+};
+
+static enum alsa_type hal_alsa_device_get_type(LibHalContext *context, const char *udi) {
+ char *type;
+ enum alsa_type t = ALSA_TYPE_OTHER;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ pa_assert(context);
+ pa_assert(udi);
+
+ if (!(type = libhal_device_get_property_string(context, udi, "alsa.type", &error)))
+ goto finish;
+
+ if (pa_streq(type, "playback"))
+ t = ALSA_TYPE_PLAYBACK;
+ else if (pa_streq(type, "capture"))
+ t = ALSA_TYPE_CAPTURE;
+ else if (pa_streq(type, "control"))
+ t = ALSA_TYPE_CONTROL;
+
+ libhal_free_string(type);
+
+finish:
+ if (dbus_error_is_set(&error)) {
+ pa_log_error("D-Bus error while parsing HAL ALSA data: %s: %s", error.name, error.message);
+ dbus_error_free(&error);
+ }
+
+ return t;
+}
+
+static pa_bool_t hal_alsa_device_is_modem(LibHalContext *context, const char *udi) {
+ char *class;
+ pa_bool_t r = FALSE;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ pa_assert(context);
+ pa_assert(udi);
+
+ if (!(class = libhal_device_get_property_string(context, udi, "alsa.pcm_class", &error)))
+ goto finish;
+
+ r = pa_streq(class, "modem");
+ libhal_free_string(class);
+
+finish:
+ if (dbus_error_is_set(&error)) {
+ if (!dbus_error_has_name(&error, "org.freedesktop.Hal.NoSuchProperty"))
+ pa_log_error("D-Bus error while parsing HAL ALSA data: %s: %s", error.name, error.message);
+ dbus_error_free(&error);
+ }
+
+ return r;
+}
+
+static int hal_device_load_alsa(struct userdata *u, const char *udi, struct device *d) {
+ enum alsa_type type;
+ int card;
+ DBusError error;
+ pa_module *m;
+ char *args, *originating_udi = NULL, *card_name = NULL;
+
+ dbus_error_init(&error);
+
+ pa_assert(u);
+ pa_assert(udi);
+ pa_assert(d);
+
+ /* We only care for PCM devices */
+ type = hal_alsa_device_get_type(u->context, udi);
+
+ /* For each ALSA card that appears the control device will be the
+ * last one to be created, this is considered part of the ALSA
+ * usperspace API. We rely on this and load our modules only when
+ * the control device is available assuming that *all* device
+ * nodes have been properly created and assigned the right ACLs at
+ * that time. Also see:
+ *
+ * http://mailman.alsa-project.org/pipermail/alsa-devel/2009-April/015958.html
+ *
+ * and the associated thread.*/
+
+ if (type != ALSA_TYPE_CONTROL)
+ goto fail;
+
+ /* We don't care for modems -- this is most likely not set for
+ * control devices, so kind of pointless here. */
+ if (hal_alsa_device_is_modem(u->context, udi))
+ goto fail;
+
+ /* We store only one entry per card, hence we look for the originating device */
+ originating_udi = libhal_device_get_property_string(u->context, udi, "alsa.originating_device", &error);
+ if (dbus_error_is_set(&error) || !originating_udi)
+ goto fail;
+
+ /* Make sure we only load one module per card */
+ if (pa_hashmap_get(u->devices, originating_udi))
+ goto fail;
+
+ /* We need the identifier */
+ card = libhal_device_get_property_int(u->context, udi, "alsa.card", &error);
+ if (dbus_error_is_set(&error))
+ goto fail;
+
+ card_name = pa_sprintf_malloc("alsa_card.%s", strip_udi(originating_udi));
+ args = pa_sprintf_malloc("device_id=%u name=\"%s\" card_name=\"%s\" tsched=%i card_properties=\"module-hal-detect.discovered=1\"", card, strip_udi(originating_udi), card_name, (int) u->use_tsched);
+
+ pa_log_debug("Loading module-alsa-card with arguments '%s'", args);
+ m = pa_module_load(u->core, "module-alsa-card", args);
+ pa_xfree(args);
+
+ if (!m)
+ goto fail;
+
+ d->originating_udi = originating_udi;
+ d->module = m->index;
+ d->card_name = card_name;
+
+ return 0;
+
+fail:
+ if (dbus_error_is_set(&error)) {
+ pa_log_error("D-Bus error while parsing HAL ALSA data: %s: %s", error.name, error.message);
+ dbus_error_free(&error);
+ }
+
+ pa_xfree(originating_udi);
+ pa_xfree(card_name);
+
+ return -1;
+}
+
+#endif
+
+#ifdef HAVE_OSS_OUTPUT
+
+static pa_bool_t hal_oss_device_is_pcm(LibHalContext *context, const char *udi, pa_bool_t init_subdevices) {
+ char *class = NULL, *dev = NULL, *e;
+ int device;
+ pa_bool_t r = FALSE;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ pa_assert(context);
+ pa_assert(udi);
+
+ /* We only care for PCM devices */
+ class = libhal_device_get_property_string(context, udi, "oss.type", &error);
+ if (dbus_error_is_set(&error) || !class)
+ goto finish;
+
+ if (!pa_streq(class, "pcm"))
+ goto finish;
+
+ /* We don't like /dev/audio */
+ dev = libhal_device_get_property_string(context, udi, "oss.device_file", &error);
+ if (dbus_error_is_set(&error) || !dev)
+ goto finish;
+
+ if ((e = strrchr(dev, '/')))
+ if (pa_startswith(e + 1, "audio"))
+ goto finish;
+
+ /* We only care for the main device */
+ device = libhal_device_get_property_int(context, udi, "oss.device", &error);
+ if (dbus_error_is_set(&error) || (device != 0 && init_subdevices == FALSE))
+ goto finish;
+
+ r = TRUE;
+
+finish:
+
+ if (dbus_error_is_set(&error)) {
+ pa_log_error("D-Bus error while parsing HAL OSS data: %s: %s", error.name, error.message);
+ dbus_error_free(&error);
+ }
+
+ libhal_free_string(class);
+ libhal_free_string(dev);
+
+ return r;
+}
+
+static int hal_device_load_oss(struct userdata *u, const char *udi, struct device *d) {
+ DBusError error;
+ pa_module *m;
+ char *args, *originating_udi = NULL, *device, *sink_name = NULL, *source_name = NULL;
+
+ dbus_error_init(&error);
+
+ pa_assert(u);
+ pa_assert(udi);
+ pa_assert(d);
+
+ /* We only care for OSS PCM devices */
+ if (!hal_oss_device_is_pcm(u->context, udi, u->init_subdevs))
+ goto fail;
+
+ /* We store only one entry per card, hence we look for the originating device */
+ originating_udi = libhal_device_get_property_string(u->context, udi, "oss.originating_device", &error);
+ if (dbus_error_is_set(&error) || !originating_udi)
+ goto fail;
+
+ /* Make sure we only load one module per card */
+ if (pa_hashmap_get(u->devices, originating_udi))
+ goto fail;
+
+ /* We need the device file */
+ device = libhal_device_get_property_string(u->context, udi, "oss.device_file", &error);
+ if (!device || dbus_error_is_set(&error))
+ goto fail;
+
+ sink_name = pa_sprintf_malloc("oss_output.%s", strip_udi(udi));
+ source_name = pa_sprintf_malloc("oss_input.%s", strip_udi(udi));
+ args = pa_sprintf_malloc("device=%s sink_name=%s source_name=%s", device, sink_name, source_name);
+
+ libhal_free_string(device);
+
+ pa_log_debug("Loading module-oss with arguments '%s'", args);
+ m = pa_module_load(u->core, "module-oss", args);
+ pa_xfree(args);
+
+ if (!m)
+ goto fail;
+
+ d->originating_udi = originating_udi;
+ d->module = m->index;
+ d->sink_name = sink_name;
+ d->source_name = source_name;
+
+ return 0;
+
+fail:
+ if (dbus_error_is_set(&error)) {
+ pa_log_error("D-Bus error while parsing OSS HAL data: %s: %s", error.name, error.message);
+ dbus_error_free(&error);
+ }
+
+ pa_xfree(originating_udi);
+ pa_xfree(source_name);
+ pa_xfree(sink_name);
+
+ return -1;
+}
+#endif
+
+static struct device* hal_device_add(struct userdata *u, const char *udi) {
+ struct device *d;
+ int r;
+
+ pa_assert(u);
+ pa_assert(u->capability);
+
+ d = pa_xnew(struct device, 1);
+ d->acl_race_fix = FALSE;
+ d->udi = pa_xstrdup(udi);
+ d->originating_udi = NULL;
+ d->module = PA_INVALID_INDEX;
+ d->sink_name = d->source_name = d->card_name = NULL;
+ r = -1;
+
+#ifdef HAVE_ALSA
+ if (pa_streq(u->capability, CAPABILITY_ALSA))
+ r = hal_device_load_alsa(u, udi, d);
+#endif
+#ifdef HAVE_OSS_OUTPUT
+ if (pa_streq(u->capability, CAPABILITY_OSS))
+ r = hal_device_load_oss(u, udi, d);
+#endif
+
+ if (r < 0) {
+ device_free(d);
+ return NULL;
+ }
+
+ pa_hashmap_put(u->devices, d->udi, d);
+ pa_hashmap_put(u->devices, d->originating_udi, d);
+
+ return d;
+}
+
+static int hal_device_add_all(struct userdata *u) {
+ int n, count = 0;
+ char** udis;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ pa_assert(u);
+
+ udis = libhal_find_device_by_capability(u->context, u->capability, &n, &error);
+ if (dbus_error_is_set(&error) || !udis)
+ goto fail;
+
+ if (n > 0) {
+ int i;
+
+ for (i = 0; i < n; i++) {
+ if (hal_device_add(u, udis[i])) {
+ count++;
+ pa_log_debug("Loaded device %s", udis[i]);
+ } else
+ pa_log_debug("Not loaded device %s", udis[i]);
+ }
+ }
+
+ libhal_free_string_array(udis);
+
+ return count;
+
+fail:
+ if (dbus_error_is_set(&error)) {
+ pa_log_error("D-Bus error while parsing HAL data: %s: %s", error.name, error.message);
+ dbus_error_free(&error);
+ }
+
+ return -1;
+}
+
+static void device_added_cb(LibHalContext *context, const char *udi) {
+ DBusError error;
+ struct userdata *u;
+ pa_bool_t good = FALSE;
+
+ dbus_error_init(&error);
+
+ pa_assert(context);
+ pa_assert(udi);
+
+ pa_assert_se(u = libhal_ctx_get_user_data(context));
+
+ good = libhal_device_query_capability(context, udi, u->capability, &error);
+ if (dbus_error_is_set(&error) || !good)
+ goto finish;
+
+ if (!hal_device_add(u, udi))
+ pa_log_debug("Not loaded device %s", udi);
+ else
+ pa_log_debug("Loaded device %s", udi);
+
+finish:
+ if (dbus_error_is_set(&error)) {
+ if (!dbus_error_has_name(&error, "org.freedesktop.Hal.NoSuchProperty"))
+ pa_log_error("D-Bus error while parsing HAL data: %s: %s", error.name, error.message);
+ dbus_error_free(&error);
+ }
+}
+
+static void device_removed_cb(LibHalContext* context, const char *udi) {
+ struct device *d;
+ struct userdata *u;
+
+ pa_assert(context);
+ pa_assert(udi);
+
+ pa_assert_se(u = libhal_ctx_get_user_data(context));
+
+ if (!(d = pa_hashmap_get(u->devices, udi)))
+ return;
+
+ pa_hashmap_remove(u->devices, d->originating_udi);
+ pa_hashmap_remove(u->devices, d->udi);
+
+ pa_log_debug("Removing HAL device: %s", d->originating_udi);
+
+ pa_module_unload_request_by_index(u->core, d->module, TRUE);
+ device_free(d);
+}
+
+static void new_capability_cb(LibHalContext *context, const char *udi, const char* capability) {
+ struct userdata *u;
+
+ pa_assert(context);
+ pa_assert(udi);
+ pa_assert(capability);
+
+ pa_assert_se(u = libhal_ctx_get_user_data(context));
+
+ if (pa_streq(u->capability, capability))
+ /* capability we care about, pretend it's a new device */
+ device_added_cb(context, udi);
+}
+
+static void lost_capability_cb(LibHalContext *context, const char *udi, const char* capability) {
+ struct userdata *u;
+
+ pa_assert(context);
+ pa_assert(udi);
+ pa_assert(capability);
+
+ pa_assert_se(u = libhal_ctx_get_user_data(context));
+
+ if (pa_streq(u->capability, capability))
+ /* capability we care about, pretend it was removed */
+ device_removed_cb(context, udi);
+}
+
+static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, void *userdata) {
+ struct userdata*u;
+ DBusError error;
+
+ pa_assert(bus);
+ pa_assert(message);
+ pa_assert_se(u = userdata);
+
+ dbus_error_init(&error);
+
+ pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
+ dbus_message_get_interface(message),
+ dbus_message_get_path(message),
+ dbus_message_get_member(message));
+
+ if (dbus_message_is_signal(message, "org.freedesktop.Hal.Device.AccessControl", "ACLAdded") ||
+ dbus_message_is_signal(message, "org.freedesktop.Hal.Device.AccessControl", "ACLRemoved")) {
+ uint32_t uid;
+ pa_bool_t suspend = strcmp(dbus_message_get_member(message), "ACLRemoved") == 0;
+
+ if (!dbus_message_get_args(message, &error, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
+ pa_log_error("Failed to parse ACL message: %s: %s", error.name, error.message);
+ goto finish;
+ }
+
+ /* Check if this is about us? */
+ if (uid == getuid() || uid == geteuid()) {
+ struct device *d;
+ const char *udi;
+
+ udi = dbus_message_get_path(message);
+
+ if ((d = pa_hashmap_get(u->devices, udi))) {
+ pa_bool_t send_acl_race_fix_message = FALSE;
+ d->acl_race_fix = FALSE;
+
+ if (d->sink_name) {
+ pa_sink *sink;
+
+ if ((sink = pa_namereg_get(u->core, d->sink_name, PA_NAMEREG_SINK))) {
+ pa_bool_t success = pa_sink_suspend(sink, suspend, PA_SUSPEND_SESSION) >= 0;
+
+ if (!success && !suspend)
+ d->acl_race_fix = TRUE; /* resume failed, let's try again */
+ else if (suspend)
+ send_acl_race_fix_message = TRUE; /* suspend finished, let's tell everyone to try again */
+ }
+ }
+
+ if (d->source_name) {
+ pa_source *source;
+
+ if ((source = pa_namereg_get(u->core, d->source_name, PA_NAMEREG_SOURCE))) {
+ pa_bool_t success = pa_source_suspend(source, suspend, PA_SUSPEND_SESSION) >= 0;
+
+ if (!success && !suspend)
+ d->acl_race_fix = TRUE; /* resume failed, let's try again */
+ else if (suspend)
+ send_acl_race_fix_message = TRUE; /* suspend finished, let's tell everyone to try again */
+ }
+ }
+
+ if (d->card_name) {
+ pa_card *card;
+
+ if ((card = pa_namereg_get(u->core, d->card_name, PA_NAMEREG_CARD))) {
+ pa_bool_t success = pa_card_suspend(card, suspend, PA_SUSPEND_SESSION) >= 0;
+
+ if (!success && !suspend)
+ d->acl_race_fix = TRUE; /* resume failed, let's try again */
+ else if (suspend)
+ send_acl_race_fix_message = TRUE; /* suspend finished, let's tell everyone to try again */
+ }
+ }
+
+ if (send_acl_race_fix_message) {
+ DBusMessage *msg;
+ msg = dbus_message_new_signal(udi, "org.pulseaudio.Server", "DirtyGiveUpMessage");
+ dbus_connection_send(pa_dbus_connection_get(u->connection), msg, NULL);
+ dbus_message_unref(msg);
+ }
+
+ } else if (!suspend)
+ device_added_cb(u->context, udi);
+
+ }
+
+ } else if (dbus_message_is_signal(message, "org.pulseaudio.Server", "DirtyGiveUpMessage")) {
+ /* We use this message to avoid a dirty race condition when we
+ get an ACLAdded message before the previously owning PA
+ sever has closed the device. We can remove this as
+ soon as HAL learns frevoke() */
+
+ struct device *d;
+ const char *udi;
+
+ udi = dbus_message_get_path(message);
+
+ if ((d = pa_hashmap_get(u->devices, udi))) {
+
+ if (d->acl_race_fix) {
+ d->acl_race_fix = FALSE;
+ pa_log_debug("Got dirty give up message for '%s', trying resume ...", udi);
+
+ if (d->sink_name) {
+ pa_sink *sink;
+
+ if ((sink = pa_namereg_get(u->core, d->sink_name, PA_NAMEREG_SINK)))
+ pa_sink_suspend(sink, FALSE, PA_SUSPEND_SESSION);
+ }
+
+ if (d->source_name) {
+ pa_source *source;
+
+ if ((source = pa_namereg_get(u->core, d->source_name, PA_NAMEREG_SOURCE)))
+ pa_source_suspend(source, FALSE, PA_SUSPEND_SESSION);
+ }
+
+ if (d->card_name) {
+ pa_card *card;
+
+ if ((card = pa_namereg_get(u->core, d->source_name, PA_NAMEREG_CARD)))
+ pa_card_suspend(card, FALSE, PA_SUSPEND_SESSION);
+ }
+ }
+
+ } else
+ /* Yes, we don't check the UDI for validity, but hopefully HAL will */
+ device_added_cb(u->context, udi);
+
+ }
+
+finish:
+ dbus_error_free(&error);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static void hal_context_free(LibHalContext* hal_context) {
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ libhal_ctx_shutdown(hal_context, &error);
+ libhal_ctx_free(hal_context);
+
+ dbus_error_free(&error);
+}
+
+static LibHalContext* hal_context_new(DBusConnection *connection) {
+ DBusError error;
+ LibHalContext *hal_context = NULL;
+
+ dbus_error_init(&error);
+
+ pa_assert(connection);
+
+ if (!(hal_context = libhal_ctx_new())) {
+ pa_log_error("libhal_ctx_new() failed");
+ goto fail;
+ }
+
+ if (!libhal_ctx_set_dbus_connection(hal_context, connection)) {
+ pa_log_error("Error establishing DBUS connection: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ if (!libhal_ctx_init(hal_context, &error)) {
+ pa_log_error("Couldn't connect to hald: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ return hal_context;
+
+fail:
+ if (hal_context)
+ hal_context_free(hal_context);
+
+ dbus_error_free(&error);
+
+ return NULL;
+}
+
+int pa__init(pa_module*m) {
+ DBusError error;
+ struct userdata *u = NULL;
+ int n = 0;
+ pa_modargs *ma;
+ const char *api;
+
+ pa_assert(m);
+
+ dbus_error_init(&error);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+#ifdef HAVE_ALSA
+ u->use_tsched = TRUE;
+
+ if (pa_modargs_get_value_boolean(ma, "tsched", &u->use_tsched) < 0) {
+ pa_log("Failed to parse tsched argument.");
+ goto fail;
+ }
+
+ api = pa_modargs_get_value(ma, "api", "alsa");
+
+ if (pa_streq(api, "alsa"))
+ u->capability = CAPABILITY_ALSA;
+#else
+ api = pa_modargs_get_value(ma, "api", "oss");
+#endif
+
+#ifdef HAVE_OSS_OUTPUT
+ if (pa_streq(api, "oss"))
+ u->capability = CAPABILITY_OSS;
+#endif
+
+ if (!u->capability) {
+ pa_log_error("Invalid API specification.");
+ goto fail;
+ }
+
+#ifdef HAVE_OSS_OUTPUT
+ if (pa_modargs_get_value_boolean(ma, "subdevices", &u->init_subdevs) < 0) {
+ pa_log("Failed to parse subdevices= argument.");
+ goto fail;
+ }
+#endif
+
+ if (!(u->connection = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &error)) || dbus_error_is_set(&error)) {
+ pa_log_error("Unable to contact DBUS system bus: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ if (!(u->context = hal_context_new(pa_dbus_connection_get(u->connection)))) {
+ /* pa_hal_context_new() logs appropriate errors */
+ goto fail;
+ }
+
+ n = hal_device_add_all(u);
+
+ libhal_ctx_set_user_data(u->context, u);
+ libhal_ctx_set_device_added(u->context, device_added_cb);
+ libhal_ctx_set_device_removed(u->context, device_removed_cb);
+ libhal_ctx_set_device_new_capability(u->context, new_capability_cb);
+ libhal_ctx_set_device_lost_capability(u->context, lost_capability_cb);
+
+ if (!libhal_device_property_watch_all(u->context, &error)) {
+ pa_log_error("Error monitoring device list: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ if (!dbus_connection_add_filter(pa_dbus_connection_get(u->connection), filter_cb, u, NULL)) {
+ pa_log_error("Failed to add filter function");
+ goto fail;
+ }
+ u->filter_added = TRUE;
+
+ if (pa_dbus_add_matches(
+ pa_dbus_connection_get(u->connection), &error,
+ "type='signal',sender='org.freedesktop.Hal',interface='org.freedesktop.Hal.Device.AccessControl',member='ACLAdded'",
+ "type='signal',sender='org.freedesktop.Hal',interface='org.freedesktop.Hal.Device.AccessControl',member='ACLRemoved'",
+ "type='signal',interface='org.pulseaudio.Server',member='DirtyGiveUpMessage'", NULL) < 0) {
+ pa_log_error("Unable to subscribe to HAL ACL signals: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ pa_log_info("Loaded %i modules.", n);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ dbus_error_free(&error);
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->context)
+ hal_context_free(u->context);
+
+ if (u->devices) {
+ struct device *d;
+
+ while ((d = pa_hashmap_first(u->devices))) {
+ pa_hashmap_remove(u->devices, d->udi);
+ pa_hashmap_remove(u->devices, d->originating_udi);
+ device_free(d);
+ }
+
+ pa_hashmap_free(u->devices, NULL, NULL);
+ }
+
+ if (u->connection) {
+ pa_dbus_remove_matches(
+ pa_dbus_connection_get(u->connection),
+ "type='signal',sender='org.freedesktop.Hal',interface='org.freedesktop.Hal.Device.AccessControl',member='ACLAdded'",
+ "type='signal',sender='org.freedesktop.Hal',interface='org.freedesktop.Hal.Device.AccessControl',member='ACLRemoved'",
+ "type='signal',interface='org.pulseaudio.Server',member='DirtyGiveUpMessage'", NULL);
+
+ if (u->filter_added)
+ dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), filter_cb, u);
+ pa_dbus_connection_unref(u->connection);
+ }
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-intended-roles.c b/src/modules/module-intended-roles.c
new file mode 100644
index 00000000..9ba893b2
--- /dev/null
+++ b/src/modules/module-intended-roles.c
@@ -0,0 +1,464 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/source-output.h>
+#include <pulsecore/namereg.h>
+
+#include "module-intended-roles-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Automatically set device of streams based of intended roles of devices");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(
+ "on_hotplug=<When new device becomes available, recheck streams?> "
+ "on_rescue=<When device becomes unavailable, recheck streams?>");
+
+static const char* const valid_modargs[] = {
+ "on_hotplug",
+ "on_rescue",
+ NULL
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+
+ pa_hook_slot
+ *sink_input_new_hook_slot,
+ *source_output_new_hook_slot,
+ *sink_put_hook_slot,
+ *source_put_hook_slot,
+ *sink_unlink_hook_slot,
+ *source_unlink_hook_slot;
+
+ pa_bool_t on_hotplug:1;
+ pa_bool_t on_rescue:1;
+};
+
+static pa_bool_t role_match(pa_proplist *proplist, const char *role) {
+ const char *ir;
+ char *r;
+ const char *state = NULL;
+
+ if (!(ir = pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES)))
+ return FALSE;
+
+ while ((r = pa_split_spaces(ir, &state))) {
+
+ if (pa_streq(role, r)) {
+ pa_xfree(r);
+ return TRUE;
+ }
+
+ pa_xfree(r);
+ }
+
+ return FALSE;
+}
+
+static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
+ const char *role;
+ pa_sink *s, *def;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+
+ if (!new_data->proplist) {
+ pa_log_debug("New stream lacks property data.");
+ return PA_HOOK_OK;
+ }
+
+ if (new_data->sink) {
+ pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
+ return PA_HOOK_OK;
+ }
+
+ if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
+ pa_log_debug("Not setting device for stream %s, because it lacks role.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
+ return PA_HOOK_OK;
+ }
+
+ /* Prefer the default sink over any other sink, just in case... */
+ if ((def = pa_namereg_get_default_sink(c)))
+ if (role_match(def->proplist, role) && pa_sink_input_new_data_set_sink(new_data, def, FALSE))
+ return PA_HOOK_OK;
+
+ /* @todo: favour the highest priority device, not the first one we find? */
+ PA_IDXSET_FOREACH(s, c->sinks, idx) {
+ if (s == def)
+ continue;
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)))
+ continue;
+
+ if (role_match(s->proplist, role) && pa_sink_input_new_data_set_sink(new_data, s, FALSE))
+ return PA_HOOK_OK;
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
+ const char *role;
+ pa_source *s, *def;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+
+ if (!new_data->proplist) {
+ pa_log_debug("New stream lacks property data.");
+ return PA_HOOK_OK;
+ }
+
+ if (new_data->source) {
+ pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
+ return PA_HOOK_OK;
+ }
+
+ if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
+ pa_log_debug("Not setting device for stream %s, because it lacks role.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
+ return PA_HOOK_OK;
+ }
+
+ /* Prefer the default source over any other source, just in case... */
+ if ((def = pa_namereg_get_default_source(c)))
+ if (role_match(def->proplist, role)) {
+ pa_source_output_new_data_set_source(new_data, def, FALSE);
+ return PA_HOOK_OK;
+ }
+
+ PA_IDXSET_FOREACH(s, c->sources, idx) {
+ if (s->monitor_of)
+ continue;
+
+ if (s == def)
+ continue;
+
+ if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
+ continue;
+
+ /* @todo: favour the highest priority device, not the first one we find? */
+ if (role_match(s->proplist, role)) {
+ pa_source_output_new_data_set_source(new_data, s, FALSE);
+ return PA_HOOK_OK;
+ }
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
+ pa_sink_input *si;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(sink);
+ pa_assert(u);
+ pa_assert(u->on_hotplug);
+
+ PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
+ const char *role;
+
+ if (si->sink == sink)
+ continue;
+
+ if (si->save_sink)
+ continue;
+
+ /* Skip this if it is already in the process of being moved
+ * anyway */
+ if (!si->sink)
+ continue;
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
+ continue;
+
+ if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
+ continue;
+
+ if (role_match(si->sink->proplist, role))
+ continue;
+
+ if (!role_match(sink->proplist, role))
+ continue;
+
+ pa_sink_input_move_to(si, sink, FALSE);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
+ pa_source_output *so;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(source);
+ pa_assert(u);
+ pa_assert(u->on_hotplug);
+
+ if (source->monitor_of)
+ return PA_HOOK_OK;
+
+ PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
+ const char *role;
+
+ if (so->source == source)
+ continue;
+
+ if (so->save_source)
+ continue;
+
+ if (so->direct_on_input)
+ continue;
+
+ /* Skip this if it is already in the process of being moved
+ * anyway */
+ if (!so->source)
+ continue;
+
+ /* It might happen that a stream and a source are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
+ continue;
+
+ if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
+ continue;
+
+ if (role_match(so->source->proplist, role))
+ continue;
+
+ if (!role_match(source->proplist, role))
+ continue;
+
+ pa_source_output_move_to(so, source, FALSE);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
+ pa_sink_input *si;
+ uint32_t idx;
+ pa_sink *def;
+
+ pa_assert(c);
+ pa_assert(sink);
+ pa_assert(u);
+ pa_assert(u->on_rescue);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ /* If there not default sink, then there is no sink at all */
+ if (!(def = pa_namereg_get_default_sink(c)))
+ return PA_HOOK_OK;
+
+ PA_IDXSET_FOREACH(si, sink->inputs, idx) {
+ const char *role;
+ uint32_t jdx;
+ pa_sink *d;
+
+ if (!si->sink)
+ continue;
+
+ if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
+ continue;
+
+ /* Would the default sink fit? If so, let's use it */
+ if (def != sink && role_match(def->proplist, role))
+ if (pa_sink_input_move_to(si, def, FALSE) >= 0)
+ continue;
+
+ /* Try to find some other fitting sink */
+ /* @todo: favour the highest priority device, not the first one we find? */
+ PA_IDXSET_FOREACH(d, c->sinks, jdx) {
+ if (d == def || d == sink)
+ continue;
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(d)))
+ continue;
+
+ if (role_match(d->proplist, role))
+ if (pa_sink_input_move_to(si, d, FALSE) >= 0)
+ break;
+ }
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
+ pa_source_output *so;
+ uint32_t idx;
+ pa_source *def;
+
+ pa_assert(c);
+ pa_assert(source);
+ pa_assert(u);
+ pa_assert(u->on_rescue);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ /* If there not default source, then there is no source at all */
+ if (!(def = pa_namereg_get_default_source(c)))
+ return PA_HOOK_OK;
+
+ PA_IDXSET_FOREACH(so, source->outputs, idx) {
+ const char *role;
+ uint32_t jdx;
+ pa_source *d;
+
+ if (so->direct_on_input)
+ continue;
+
+ if (!so->source)
+ continue;
+
+ if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
+ continue;
+
+ /* Would the default source fit? If so, let's use it */
+ if (def != source && role_match(def->proplist, role) && !source->monitor_of == !def->monitor_of) {
+ pa_source_output_move_to(so, def, FALSE);
+ continue;
+ }
+
+ /* Try to find some other fitting source */
+ /* @todo: favour the highest priority device, not the first one we find? */
+ PA_IDXSET_FOREACH(d, c->sources, jdx) {
+ if (d == def || d == source)
+ continue;
+
+ if (!PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
+ continue;
+
+ /* If moving from a monitor, move to another monitor */
+ if (!source->monitor_of == !d->monitor_of && role_match(d->proplist, role)) {
+ pa_source_output_move_to(so, d, FALSE);
+ break;
+ }
+ }
+ }
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+ pa_bool_t on_hotplug = TRUE, on_rescue = TRUE;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
+ pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
+ pa_log("on_hotplug= and on_rescue= expect boolean arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->on_hotplug = on_hotplug;
+ u->on_rescue = on_rescue;
+
+ /* A little bit later than module-stream-restore */
+ u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY+10, (pa_hook_cb_t) sink_input_new_hook_callback, u);
+ u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY+10, (pa_hook_cb_t) source_output_new_hook_callback, u);
+
+ if (on_hotplug) {
+ /* A little bit later than module-stream-restore */
+ u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_put_hook_callback, u);
+ u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+10, (pa_hook_cb_t) source_put_hook_callback, u);
+ }
+
+ if (on_rescue) {
+ /* A little bit later than module-stream-restore, a little bit earlier than module-rescue-streams, ... */
+ u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_unlink_hook_callback, u);
+ u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) source_unlink_hook_callback, u);
+ }
+
+ pa_modargs_free(ma);
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink_input_new_hook_slot)
+ pa_hook_slot_free(u->sink_input_new_hook_slot);
+ if (u->source_output_new_hook_slot)
+ pa_hook_slot_free(u->source_output_new_hook_slot);
+
+ if (u->sink_put_hook_slot)
+ pa_hook_slot_free(u->sink_put_hook_slot);
+ if (u->source_put_hook_slot)
+ pa_hook_slot_free(u->source_put_hook_slot);
+
+ if (u->sink_unlink_hook_slot)
+ pa_hook_slot_free(u->sink_unlink_hook_slot);
+ if (u->source_unlink_hook_slot)
+ pa_hook_slot_free(u->source_unlink_hook_slot);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c
deleted file mode 100644
index c645caa9..00000000
--- a/src/modules/module-jack-sink.c
+++ /dev/null
@@ -1,404 +0,0 @@
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <assert.h>
-#include <errno.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <limits.h>
-#include <pthread.h>
-
-#include <jack/jack.h>
-
-#include <pulse/xmalloc.h>
-
-#include <pulsecore/core-error.h>
-#include <pulsecore/iochannel.h>
-#include <pulsecore/sink.h>
-#include <pulsecore/module.h>
-#include <pulsecore/core-util.h>
-#include <pulsecore/modargs.h>
-#include <pulsecore/log.h>
-#include <pulse/mainloop-api.h>
-
-#include "module-jack-sink-symdef.h"
-
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Jack Sink")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE(
- "sink_name=<name of sink> "
- "server_name=<jack server name> "
- "client_name=<jack client name> "
- "channels=<number of channels> "
- "connect=<connect ports?> "
- "channel_map=<channel map>")
-
-#define DEFAULT_SINK_NAME "jack_out"
-
-struct userdata {
- pa_core *core;
- pa_module *module;
-
- pa_sink *sink;
-
- unsigned channels;
-
- jack_port_t* port[PA_CHANNELS_MAX];
- jack_client_t *client;
-
- pthread_mutex_t mutex;
- pthread_cond_t cond;
-
- void * buffer[PA_CHANNELS_MAX];
- jack_nframes_t frames_requested;
- int quit_requested;
-
- int pipe_fd_type;
- int pipe_fds[2];
- pa_io_event *io_event;
-
- jack_nframes_t frames_in_buffer;
- jack_nframes_t timestamp;
-};
-
-static const char* const valid_modargs[] = {
- "sink_name",
- "server_name",
- "client_name",
- "channels",
- "connect",
- "channel_map",
- NULL
-};
-
-static void stop_sink(struct userdata *u) {
- assert (u);
-
- jack_client_close(u->client);
- u->client = NULL;
- u->core->mainloop->io_free(u->io_event);
- u->io_event = NULL;
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- u->sink = NULL;
- pa_module_unload_request(u->module);
-}
-
-static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
- struct userdata *u = userdata;
- char x;
-
- assert(m);
- assert(e);
- assert(flags == PA_IO_EVENT_INPUT);
- assert(u);
- assert(u->pipe_fds[0] == fd);
-
- pa_read(fd, &x, 1, &u->pipe_fd_type);
-
- if (u->quit_requested) {
- stop_sink(u);
- u->quit_requested = 0;
- return;
- }
-
- pthread_mutex_lock(&u->mutex);
-
- if (u->frames_requested > 0) {
- unsigned fs;
- jack_nframes_t frame_idx;
- pa_memchunk chunk;
-
- fs = pa_frame_size(&u->sink->sample_spec);
-
- pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk);
-
- for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) {
- unsigned c;
-
- for (c = 0; c < u->channels; c++) {
- float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c;
- float *d = ((float*) u->buffer[c]) + frame_idx;
-
- *d = *s;
- }
- }
-
- pa_memblock_unref(chunk.memblock);
-
- u->frames_requested = 0;
-
- pthread_cond_signal(&u->cond);
- }
-
- pthread_mutex_unlock(&u->mutex);
-}
-
-static void request_render(struct userdata *u) {
- char c = 'x';
- assert(u);
-
- assert(u->pipe_fds[1] >= 0);
- pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type);
-}
-
-static void jack_shutdown(void *arg) {
- struct userdata *u = arg;
- assert(u);
-
- u->quit_requested = 1;
- request_render(u);
-}
-
-static int jack_process(jack_nframes_t nframes, void *arg) {
- struct userdata *u = arg;
- assert(u);
-
- if (jack_transport_query(u->client, NULL) == JackTransportRolling) {
- unsigned c;
-
- pthread_mutex_lock(&u->mutex);
-
- u->frames_requested = nframes;
-
- for (c = 0; c < u->channels; c++) {
- u->buffer[c] = jack_port_get_buffer(u->port[c], nframes);
- assert(u->buffer[c]);
- }
-
- request_render(u);
-
- pthread_cond_wait(&u->cond, &u->mutex);
-
- u->frames_in_buffer = nframes;
- u->timestamp = jack_get_current_transport_frame(u->client);
-
- pthread_mutex_unlock(&u->mutex);
- }
-
- return 0;
-}
-
-static pa_usec_t sink_get_latency_cb(pa_sink *s) {
- struct userdata *u;
- jack_nframes_t n, l, d;
-
- assert(s);
- u = s->userdata;
-
- if (jack_transport_query(u->client, NULL) != JackTransportRolling)
- return 0;
-
- n = jack_get_current_transport_frame(u->client);
-
- if (n < u->timestamp)
- return 0;
-
- d = n - u->timestamp;
- l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer;
-
- if (d >= l)
- return 0;
-
- return pa_bytes_to_usec((l - d) * pa_frame_size(&s->sample_spec), &s->sample_spec);
-}
-
-static void jack_error_func(const char*t) {
- pa_log_warn(__FILE__": JACK error >%s<", t);
-}
-
-int pa__init(pa_core *c, pa_module*m) {
- struct userdata *u = NULL;
- pa_sample_spec ss;
- pa_channel_map map;
- pa_modargs *ma = NULL;
- jack_status_t status;
- const char *server_name, *client_name;
- uint32_t channels = 0;
- int do_connect = 1;
- unsigned i;
- const char **ports = NULL, **p;
-
- assert(c);
- assert(m);
-
- jack_set_error_function(jack_error_func);
-
- if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
- goto fail;
- }
-
- if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) {
- pa_log(__FILE__": failed to parse connect= argument.");
- goto fail;
- }
-
- server_name = pa_modargs_get_value(ma, "server_name", NULL);
- client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio");
-
- u = pa_xnew0(struct userdata, 1);
- m->userdata = u;
- u->core = c;
- u->module = m;
- u->pipe_fds[0] = u->pipe_fds[1] = -1;
- u->pipe_fd_type = 0;
-
- pthread_mutex_init(&u->mutex, NULL);
- pthread_cond_init(&u->cond, NULL);
-
- if (pipe(u->pipe_fds) < 0) {
- pa_log(__FILE__": pipe() failed: %s", pa_cstrerror(errno));
- goto fail;
- }
-
- pa_make_nonblock_fd(u->pipe_fds[1]);
-
- if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
- pa_log(__FILE__": jack_client_open() failed.");
- goto fail;
- }
-
- ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
-
- channels = 0;
- for (p = ports; *p; p++)
- channels++;
-
- if (!channels)
- channels = c->default_sample_spec.channels;
-
- if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) {
- pa_log(__FILE__": failed to parse channels= argument.");
- goto fail;
- }
-
- pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA);
- if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) {
- pa_log(__FILE__": failed to parse channel_map= argument.");
- goto fail;
- }
-
- pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client));
-
- ss.channels = u->channels = channels;
- ss.rate = jack_get_sample_rate(u->client);
- ss.format = PA_SAMPLE_FLOAT32NE;
-
- assert(pa_sample_spec_valid(&ss));
-
- for (i = 0; i < ss.channels; i++) {
- if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) {
- pa_log(__FILE__": jack_port_register() failed.");
- goto fail;
- }
- }
-
- if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": failed to create sink.");
- goto fail;
- }
-
- u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client));
- u->sink->get_latency = sink_get_latency_cb;
-
- jack_set_process_callback(u->client, jack_process, u);
- jack_on_shutdown(u->client, jack_shutdown, u);
-
- if (jack_activate(u->client)) {
- pa_log(__FILE__": jack_activate() failed");
- goto fail;
- }
-
- if (do_connect) {
- for (i = 0, p = ports; i < ss.channels; i++, p++) {
-
- if (!*p) {
- pa_log(__FILE__": not enough physical output ports, leaving unconnected.");
- break;
- }
-
- pa_log_info(__FILE__": connecting %s to %s", jack_port_name(u->port[i]), *p);
-
- if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) {
- pa_log(__FILE__": failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
- break;
- }
- }
-
- }
-
- u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u);
-
- free(ports);
- pa_modargs_free(ma);
-
- return 0;
-
-fail:
- if (ma)
- pa_modargs_free(ma);
-
- free(ports);
-
- pa__done(c, m);
-
- return -1;
-}
-
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata *u;
- assert(c && m);
-
- if (!(u = m->userdata))
- return;
-
- if (u->client)
- jack_client_close(u->client);
-
- if (u->io_event)
- c->mainloop->io_free(u->io_event);
-
- if (u->sink) {
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- }
-
- if (u->pipe_fds[0] >= 0)
- close(u->pipe_fds[0]);
- if (u->pipe_fds[1] >= 0)
- close(u->pipe_fds[1]);
-
- pthread_mutex_destroy(&u->mutex);
- pthread_cond_destroy(&u->cond);
- pa_xfree(u);
-}
diff --git a/src/modules/module-jack-source.c b/src/modules/module-jack-source.c
deleted file mode 100644
index 2a492929..00000000
--- a/src/modules/module-jack-source.c
+++ /dev/null
@@ -1,402 +0,0 @@
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <assert.h>
-#include <errno.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <limits.h>
-#include <pthread.h>
-
-#include <jack/jack.h>
-
-#include <pulse/xmalloc.h>
-
-#include <pulsecore/core-error.h>
-#include <pulsecore/iochannel.h>
-#include <pulsecore/source.h>
-#include <pulsecore/module.h>
-#include <pulsecore/core-util.h>
-#include <pulsecore/modargs.h>
-#include <pulsecore/log.h>
-#include <pulse/mainloop-api.h>
-
-#include "module-jack-source-symdef.h"
-
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Jack Source")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE(
- "source_name=<name of source> "
- "server_name=<jack server name> "
- "client_name=<jack client name> "
- "channels=<number of channels> "
- "connect=<connect ports?>"
- "channel_map=<channel map>")
-
-#define DEFAULT_SOURCE_NAME "jack_in"
-
-struct userdata {
- pa_core *core;
- pa_module *module;
-
- pa_source *source;
-
- unsigned channels;
-
- jack_port_t* port[PA_CHANNELS_MAX];
- jack_client_t *client;
-
- pthread_mutex_t mutex;
- pthread_cond_t cond;
-
- void * buffer[PA_CHANNELS_MAX];
- jack_nframes_t frames_posted;
- int quit_requested;
-
- int pipe_fds[2];
- int pipe_fd_type;
- pa_io_event *io_event;
-
- jack_nframes_t frames_in_buffer;
- jack_nframes_t timestamp;
-};
-
-static const char* const valid_modargs[] = {
- "source_name",
- "server_name",
- "client_name",
- "channels",
- "connect",
- "channel_map",
- NULL
-};
-
-static void stop_source(struct userdata *u) {
- assert (u);
-
- jack_client_close(u->client);
- u->client = NULL;
- u->core->mainloop->io_free(u->io_event);
- u->io_event = NULL;
- pa_source_disconnect(u->source);
- pa_source_unref(u->source);
- u->source = NULL;
- pa_module_unload_request(u->module);
-}
-
-static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
- struct userdata *u = userdata;
- char x;
-
- assert(m);
- assert(flags == PA_IO_EVENT_INPUT);
- assert(u);
- assert(u->pipe_fds[0] == fd);
-
- pa_read(fd, &x, 1, &u->pipe_fd_type);
-
- if (u->quit_requested) {
- stop_source(u);
- u->quit_requested = 0;
- return;
- }
-
- pthread_mutex_lock(&u->mutex);
-
- if (u->frames_posted > 0) {
- unsigned fs;
- jack_nframes_t frame_idx;
- pa_memchunk chunk;
-
- fs = pa_frame_size(&u->source->sample_spec);
-
- chunk.memblock = pa_memblock_new(chunk.length = u->frames_posted * fs, u->core->memblock_stat);
- chunk.index = 0;
-
- for (frame_idx = 0; frame_idx < u->frames_posted; frame_idx ++) {
- unsigned c;
-
- for (c = 0; c < u->channels; c++) {
- float *s = ((float*) u->buffer[c]) + frame_idx;
- float *d = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c;
-
- *d = *s;
- }
- }
-
- pa_source_post(u->source, &chunk);
- pa_memblock_unref(chunk.memblock);
-
- u->frames_posted = 0;
-
- pthread_cond_signal(&u->cond);
- }
-
- pthread_mutex_unlock(&u->mutex);
-}
-
-static void request_post(struct userdata *u) {
- char c = 'x';
- assert(u);
-
- assert(u->pipe_fds[1] >= 0);
- pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type);
-}
-
-static void jack_shutdown(void *arg) {
- struct userdata *u = arg;
- assert(u);
-
- u->quit_requested = 1;
- request_post(u);
-}
-
-static int jack_process(jack_nframes_t nframes, void *arg) {
- struct userdata *u = arg;
- assert(u);
-
- if (jack_transport_query(u->client, NULL) == JackTransportRolling) {
- unsigned c;
-
- pthread_mutex_lock(&u->mutex);
-
- u->frames_posted = nframes;
-
- for (c = 0; c < u->channels; c++) {
- u->buffer[c] = jack_port_get_buffer(u->port[c], nframes);
- assert(u->buffer[c]);
- }
-
- request_post(u);
-
- pthread_cond_wait(&u->cond, &u->mutex);
-
- u->frames_in_buffer = nframes;
- u->timestamp = jack_get_current_transport_frame(u->client);
-
- pthread_mutex_unlock(&u->mutex);
- }
-
- return 0;
-}
-
-static pa_usec_t source_get_latency_cb(pa_source *s) {
- struct userdata *u;
- jack_nframes_t n, l, d;
-
- assert(s);
- u = s->userdata;
-
- if (jack_transport_query(u->client, NULL) != JackTransportRolling)
- return 0;
-
- n = jack_get_current_transport_frame(u->client);
-
- if (n < u->timestamp)
- return 0;
-
- d = n - u->timestamp;
- l = jack_port_get_total_latency(u->client, u->port[0]);
-
- return pa_bytes_to_usec((l + d) * pa_frame_size(&s->sample_spec), &s->sample_spec);
-}
-
-static void jack_error_func(const char*t) {
- pa_log_warn(__FILE__": JACK error >%s<", t);
-}
-
-int pa__init(pa_core *c, pa_module*m) {
- struct userdata *u = NULL;
- pa_sample_spec ss;
- pa_channel_map map;
- pa_modargs *ma = NULL;
- jack_status_t status;
- const char *server_name, *client_name;
- uint32_t channels = 0;
- int do_connect = 1;
- unsigned i;
- const char **ports = NULL, **p;
-
- assert(c);
- assert(m);
-
- jack_set_error_function(jack_error_func);
-
- if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
- goto fail;
- }
-
- if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) {
- pa_log(__FILE__": failed to parse connect= argument.");
- goto fail;
- }
-
- server_name = pa_modargs_get_value(ma, "server_name", NULL);
- client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio");
-
- u = pa_xnew0(struct userdata, 1);
- m->userdata = u;
- u->core = c;
- u->module = m;
- u->pipe_fds[0] = u->pipe_fds[1] = -1;
- u->pipe_fd_type = 0;
-
- pthread_mutex_init(&u->mutex, NULL);
- pthread_cond_init(&u->cond, NULL);
-
- if (pipe(u->pipe_fds) < 0) {
- pa_log(__FILE__": pipe() failed: %s", pa_cstrerror(errno));
- goto fail;
- }
-
- pa_make_nonblock_fd(u->pipe_fds[1]);
-
- if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
- pa_log(__FILE__": jack_client_open() failed.");
- goto fail;
- }
-
- ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
-
- channels = 0;
- for (p = ports; *p; p++)
- channels++;
-
- if (!channels)
- channels = c->default_sample_spec.channels;
-
- if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) {
- pa_log(__FILE__": failed to parse channels= argument.");
- goto fail;
- }
-
- pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA);
- if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) {
- pa_log(__FILE__": failed to parse channel_map= argument.");
- goto fail;
- }
-
- pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client));
-
- ss.channels = u->channels = channels;
- ss.rate = jack_get_sample_rate(u->client);
- ss.format = PA_SAMPLE_FLOAT32NE;
-
- assert(pa_sample_spec_valid(&ss));
-
- for (i = 0; i < ss.channels; i++) {
- if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0))) {
- pa_log(__FILE__": jack_port_register() failed.");
- goto fail;
- }
- }
-
- if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": failed to create source.");
- goto fail;
- }
-
- u->source->userdata = u;
- pa_source_set_owner(u->source, m);
- u->source->description = pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u->client));
- u->source->get_latency = source_get_latency_cb;
-
- jack_set_process_callback(u->client, jack_process, u);
- jack_on_shutdown(u->client, jack_shutdown, u);
-
- if (jack_activate(u->client)) {
- pa_log(__FILE__": jack_activate() failed");
- goto fail;
- }
-
- if (do_connect) {
- for (i = 0, p = ports; i < ss.channels; i++, p++) {
-
- if (!*p) {
- pa_log(__FILE__": not enough physical output ports, leaving unconnected.");
- break;
- }
-
- pa_log_info(__FILE__": connecting %s to %s", jack_port_name(u->port[i]), *p);
-
- if (jack_connect(u->client, *p, jack_port_name(u->port[i]))) {
- pa_log(__FILE__": failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
- break;
- }
- }
-
- }
-
- u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u);
-
- free(ports);
- pa_modargs_free(ma);
-
- return 0;
-
-fail:
- if (ma)
- pa_modargs_free(ma);
-
- free(ports);
-
- pa__done(c, m);
-
- return -1;
-}
-
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata *u;
- assert(c && m);
-
- if (!(u = m->userdata))
- return;
-
- if (u->client)
- jack_client_close(u->client);
-
- if (u->io_event)
- c->mainloop->io_free(u->io_event);
-
- if (u->source) {
- pa_source_disconnect(u->source);
- pa_source_unref(u->source);
- }
-
- if (u->pipe_fds[0] >= 0)
- close(u->pipe_fds[0]);
- if (u->pipe_fds[1] >= 0)
- close(u->pipe_fds[1]);
-
- pthread_mutex_destroy(&u->mutex);
- pthread_cond_destroy(&u->cond);
- pa_xfree(u);
-}
diff --git a/src/modules/module-ladspa-sink.c b/src/modules/module-ladspa-sink.c
new file mode 100644
index 00000000..9cce269d
--- /dev/null
+++ b/src/modules/module-ladspa-sink.c
@@ -0,0 +1,1024 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2008 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* TODO: Some plugins cause latency, and some even report it by using a control
+ out port. We don't currently use the latency information. */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <math.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+
+#include <pulsecore/namereg.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/ltdl-helper.h>
+
+#include "module-ladspa-sink-symdef.h"
+#include "ladspa.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION(_("Virtual LADSPA sink"));
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ _("sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "master=<name of sink to filter> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<input channel map> "
+ "plugin=<ladspa plugin name> "
+ "label=<ladspa plugin label> "
+ "control=<comma seperated list of input control values> "
+ "input_ladspaport_map=<comma separated list of input LADSPA port names> "
+ "output_ladspaport_map=<comma separated list of output LADSPA port names> "));
+
+#define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
+
+/* PLEASE NOTICE: The PortAudio ports and the LADSPA ports are two different concepts.
+They are not related and where possible the names of the LADSPA port variables contains "ladspa" to avoid confusion */
+
+struct userdata {
+ pa_module *module;
+
+ pa_sink *sink;
+ pa_sink_input *sink_input;
+
+ const LADSPA_Descriptor *descriptor;
+ LADSPA_Handle handle[PA_CHANNELS_MAX];
+ unsigned long max_ladspaport_count, input_count, output_count, channels;
+ LADSPA_Data **input, **output;
+ size_t block_size;
+ LADSPA_Data *control;
+
+ /* This is a dummy buffer. Every port must be connected, but we don't care
+ about control out ports. We connect them all to this single buffer. */
+ LADSPA_Data control_out;
+
+ pa_memblockq *memblockq;
+
+ pa_bool_t auto_desc;
+};
+
+static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
+ "master",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "plugin",
+ "label",
+ "control",
+ "input_ladspaport_map",
+ "output_ladspaport_map",
+ NULL
+};
+
+/* Called from I/O thread context */
+static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_GET_LATENCY:
+
+ /* The sink is _put() before the sink input is, so let's
+ * make sure we don't access it in that time. Also, the
+ * sink input is first shut down, the sink second. */
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
+ *((pa_usec_t*) data) = 0;
+ return 0;
+ }
+
+ *((pa_usec_t*) data) =
+
+ /* Get the latency of the master sink */
+ pa_sink_get_latency_within_thread(u->sink_input->sink) +
+
+ /* Add the latency internal to our sink input on top */
+ pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
+
+ return 0;
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from main context */
+static int sink_set_state_cb(pa_sink *s, pa_sink_state_t state) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(state) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return 0;
+
+ pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void sink_request_rewind_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_request_rewind(u->sink_input,
+ s->thread_info.rewind_nbytes +
+ pa_memblockq_get_length(u->memblockq), TRUE, FALSE, FALSE);
+}
+
+/* Called from I/O thread context */
+static void sink_update_requested_latency_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_set_requested_latency_within_thread(
+ u->sink_input,
+ pa_sink_get_requested_latency_within_thread(s));
+}
+
+/* Called from main context */
+static void sink_set_volume_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return;
+
+ pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, TRUE);
+}
+
+/* Called from main context */
+static void sink_set_mute_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return;
+
+ pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
+}
+
+/* Called from I/O thread context */
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
+ struct userdata *u;
+ float *src, *dst;
+ size_t fs;
+ unsigned n, h, c;
+ pa_memchunk tchunk;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert(chunk);
+ pa_assert_se(u = i->userdata);
+
+ /* Hmm, process any rewind request that might be queued up */
+ pa_sink_process_rewind(u->sink, 0);
+
+ while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
+ pa_memchunk nchunk;
+
+ pa_sink_render(u->sink, nbytes, &nchunk);
+ pa_memblockq_push(u->memblockq, &nchunk);
+ pa_memblock_unref(nchunk.memblock);
+ }
+
+ tchunk.length = PA_MIN(nbytes, tchunk.length);
+ pa_assert(tchunk.length > 0);
+
+ fs = pa_frame_size(&i->sample_spec);
+ n = (unsigned) (PA_MIN(tchunk.length, u->block_size) / fs);
+
+ pa_assert(n > 0);
+
+ chunk->index = 0;
+ chunk->length = n*fs;
+ chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
+
+ pa_memblockq_drop(u->memblockq, chunk->length);
+
+ src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
+ dst = (float*) pa_memblock_acquire(chunk->memblock);
+
+ for (h = 0; h < (u->channels / u->max_ladspaport_count); h++) {
+ for (c = 0; c < u->input_count; c++)
+ pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input[c], sizeof(float), src+ h*u->max_ladspaport_count + c, u->channels*sizeof(float), n);
+ u->descriptor->run(u->handle[h], n);
+ for (c = 0; c < u->output_count; c++)
+ pa_sample_clamp(PA_SAMPLE_FLOAT32NE, dst + h*u->max_ladspaport_count + c, u->channels*sizeof(float), u->output[c], sizeof(float), n);
+ }
+
+ pa_memblock_release(tchunk.memblock);
+ pa_memblock_release(chunk->memblock);
+
+ pa_memblock_unref(tchunk.memblock);
+
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+ size_t amount = 0;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ if (u->sink->thread_info.rewind_nbytes > 0) {
+ size_t max_rewrite;
+
+ max_rewrite = nbytes + pa_memblockq_get_length(u->memblockq);
+ amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
+ u->sink->thread_info.rewind_nbytes = 0;
+
+ if (amount > 0) {
+ unsigned c;
+
+ pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE);
+
+ pa_log_debug("Resetting plugin");
+
+ /* Reset the plugin */
+ if (u->descriptor->deactivate)
+ for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
+ u->descriptor->deactivate(u->handle[c]);
+ if (u->descriptor->activate)
+ for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
+ u->descriptor->activate(u->handle[c]);
+ }
+ }
+
+ pa_sink_process_rewind(u->sink, amount);
+ pa_memblockq_rewind(u->memblockq, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_memblockq_set_maxrewind(u->memblockq, nbytes);
+ pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_max_request_within_thread(u->sink, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_detach_within_thread(u->sink);
+
+ pa_sink_set_rtpoll(u->sink, NULL);
+}
+
+/* Called from I/O thread context */
+static void sink_input_attach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+ pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
+ pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
+
+ pa_sink_attach_within_thread(u->sink);
+}
+
+/* Called from main context */
+static void sink_input_kill_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* The order here matters! We first kill the sink input, followed
+ * by the sink. That means the sink callbacks must be protected
+ * against an unconnected sink input! */
+ pa_sink_input_unlink(u->sink_input);
+ pa_sink_unlink(u->sink);
+
+ pa_sink_input_unref(u->sink_input);
+ u->sink_input = NULL;
+
+ pa_sink_unref(u->sink);
+ u->sink = NULL;
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from IO thread context */
+static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* If we are added for the first time, ask for a rewinding so that
+ * we are heard right-away. */
+ if (PA_SINK_INPUT_IS_LINKED(state) &&
+ i->thread_info.state == PA_SINK_INPUT_INIT) {
+ pa_log_debug("Requesting rewind due to state change.");
+ pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
+ }
+}
+
+/* Called from main context */
+static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ return u->sink != dest;
+}
+
+/* Called from main context */
+static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ if (dest) {
+ pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
+ pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
+ } else
+ pa_sink_set_asyncmsgq(u->sink, NULL);
+
+ if (u->auto_desc && dest) {
+ const char *z;
+ pa_proplist *pl;
+
+ pl = pa_proplist_new();
+ z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s",
+ pa_proplist_gets(u->sink->proplist, "device.ladspa.name"), z ? z : dest->name);
+
+ pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
+ pa_proplist_free(pl);
+ }
+}
+
+/* Called from main context */
+static void sink_input_volume_changed_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_volume_changed(u->sink, &i->volume);
+}
+
+/* Called from main context */
+static void sink_input_mute_changed_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_mute_changed(u->sink, i->muted);
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_modargs *ma;
+ char *t;
+ pa_sink *master;
+ pa_sink_input_new_data sink_input_data;
+ pa_sink_new_data sink_data;
+ const char *plugin, *label, *input_ladspaport_map, *output_ladspaport_map;
+ LADSPA_Descriptor_Function descriptor_func;
+ unsigned long input_ladspaport[PA_CHANNELS_MAX], output_ladspaport[PA_CHANNELS_MAX];
+ const char *e, *cdata;
+ const LADSPA_Descriptor *d;
+ unsigned long p, h, j, n_control, c;
+ pa_bool_t *use_default = NULL;
+
+ pa_assert(m);
+
+ pa_assert_cc(sizeof(LADSPA_Data) == sizeof(float));
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
+ pa_log("Master sink not found");
+ goto fail;
+ }
+
+ ss = master->sample_spec;
+ ss.format = PA_SAMPLE_FLOAT32;
+ map = master->channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+ pa_log("Invalid sample format specification or channel map");
+ goto fail;
+ }
+
+ if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
+ pa_log("Missing LADSPA plugin name");
+ goto fail;
+ }
+
+ if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
+ pa_log("Missing LADSPA plugin label");
+ goto fail;
+ }
+
+ if (!(input_ladspaport_map = pa_modargs_get_value(ma, "input_ladspaport_map", NULL)))
+ pa_log_debug("Using default input ladspa port mapping");
+
+ if (!(output_ladspaport_map = pa_modargs_get_value(ma, "output_ladspaport_map", NULL)))
+ pa_log_debug("Using default output ladspa port mapping");
+
+ cdata = pa_modargs_get_value(ma, "control", NULL);
+
+ u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ m->userdata = u;
+ u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
+ u->max_ladspaport_count = 1; /*to avoid division by zero etc. in pa__done when failing before this value has been set*/
+ u->channels = 0;
+ u->input = NULL;
+ u->output = NULL;
+
+ if (!(e = getenv("LADSPA_PATH")))
+ e = LADSPA_PATH;
+
+ /* FIXME: This is not exactly thread safe */
+ t = pa_xstrdup(lt_dlgetsearchpath());
+ lt_dlsetsearchpath(e);
+ m->dl = lt_dlopenext(plugin);
+ lt_dlsetsearchpath(t);
+ pa_xfree(t);
+
+ if (!m->dl) {
+ pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
+ goto fail;
+ }
+
+ if (!(descriptor_func = (LADSPA_Descriptor_Function) pa_load_sym(m->dl, NULL, "ladspa_descriptor"))) {
+ pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
+ goto fail;
+ }
+
+ for (j = 0;; j++) {
+
+ if (!(d = descriptor_func(j))) {
+ pa_log("Failed to find plugin label '%s' in plugin '%s'.", label, plugin);
+ goto fail;
+ }
+
+ if (strcmp(d->Label, label) == 0)
+ break;
+ }
+
+ u->descriptor = d;
+
+ pa_log_debug("Module: %s", plugin);
+ pa_log_debug("Label: %s", d->Label);
+ pa_log_debug("Unique ID: %lu", d->UniqueID);
+ pa_log_debug("Name: %s", d->Name);
+ pa_log_debug("Maker: %s", d->Maker);
+ pa_log_debug("Copyright: %s", d->Copyright);
+
+ n_control = 0;
+ u->channels = ss.channels;
+
+ /*
+ * Enumerate ladspa ports
+ * Default mapping is in order given by the plugin
+ */
+ for (p = 0; p < d->PortCount; p++) {
+ if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
+ if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
+ pa_log_debug("Port %lu is input: %s", p, d->PortNames[p]);
+ input_ladspaport[u->input_count] = p;
+ u->input_count++;
+ } else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
+ pa_log_debug("Port %lu is output: %s", p, d->PortNames[p]);
+ output_ladspaport[u->output_count] = p;
+ u->output_count++;
+ }
+ } else if (LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]) && LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
+ pa_log_debug("Port %lu is control: %s", p, d->PortNames[p]);
+ n_control++;
+ } else
+ pa_log_debug("Ignored port %s", d->PortNames[p]);
+ /* XXX: Has anyone ever seen an in-place plugin with non-equal number of input and output ports? */
+ /* Could be if the plugin is for up-mixing stereo to 5.1 channels */
+ /* Or if the plugin is down-mixing 5.1 to two channel stereo or binaural encoded signal */
+ if (u->input_count > u->max_ladspaport_count)
+ u->max_ladspaport_count = u->input_count;
+ else
+ u->max_ladspaport_count = u->output_count;
+ }
+
+ if (u->channels % u->max_ladspaport_count) {
+ pa_log("Cannot handle non-integral number of plugins required for given number of channels");
+ goto fail;
+ }
+
+ pa_log_debug("Will run %lu plugin instances", u->channels / u->max_ladspaport_count);
+
+ /* Parse data for input ladspa port map */
+ if (input_ladspaport_map) {
+ const char *state = NULL;
+ char *pname;
+ c = 0;
+ while ((pname = pa_split(input_ladspaport_map, ",", &state))) {
+ if (c == u->input_count) {
+ pa_log("Too many ports in input ladspa port map");
+ goto fail;
+ }
+
+
+ for (p = 0; p < d->PortCount; p++) {
+ if (strcmp(d->PortNames[p], pname) == 0) {
+ if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p]) && LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
+ input_ladspaport[c] = p;
+ } else {
+ pa_log("Port %s is not an audio input ladspa port", pname);
+ pa_xfree(pname);
+ goto fail;
+ }
+ }
+ }
+ c++;
+ pa_xfree(pname);
+ }
+ }
+
+ /* Parse data for output port map */
+ if (output_ladspaport_map) {
+ const char *state = NULL;
+ char *pname;
+ c = 0;
+ while ((pname = pa_split(output_ladspaport_map, ",", &state))) {
+ if (c == u->output_count) {
+ pa_log("Too many ports in output ladspa port map");
+ goto fail;
+ }
+ for (p = 0; p < d->PortCount; p++) {
+ if (strcmp(d->PortNames[p], pname) == 0) {
+ if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p]) && LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
+ output_ladspaport[c] = p;
+ } else {
+ pa_log("Port %s is not an output ladspa port", pname);
+ pa_xfree(pname);
+ goto fail;
+ }
+ }
+ }
+ c++;
+ pa_xfree(pname);
+ }
+ }
+
+
+ u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
+
+ /* Create buffers */
+ if (LADSPA_IS_INPLACE_BROKEN(d->Properties)) {
+ u->input = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->input_count);
+ for (c = 0; c < u->input_count; c++)
+ u->input[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
+ u->output = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->output_count);
+ for (c = 0; c < u->output_count; c++)
+ u->output[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
+ } else {
+ u->input = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->max_ladspaport_count);
+ for (c = 0; c < u->max_ladspaport_count; c++)
+ u->input[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
+ u->output = u->input;
+ }
+ /* Initialize plugin instances */
+ for (h = 0; h < (u->channels / u->max_ladspaport_count); h++) {
+ if (!(u->handle[h] = d->instantiate(d, ss.rate))) {
+ pa_log("Failed to instantiate plugin %s with label %s", plugin, d->Label);
+ goto fail;
+ }
+
+ for (c = 0; c < u->input_count; c++)
+ d->connect_port(u->handle[h], input_ladspaport[c], u->input[c]);
+ for (c = 0; c < u->output_count; c++)
+ d->connect_port(u->handle[h], output_ladspaport[c], u->output[c]);
+ }
+
+ if (!cdata && n_control > 0) {
+ pa_log("This plugin requires specification of %lu control parameters.", n_control);
+ goto fail;
+ }
+
+ if (n_control > 0) {
+ const char *state = NULL;
+ char *k;
+
+ u->control = pa_xnew(LADSPA_Data, (unsigned) n_control);
+ use_default = pa_xnew(pa_bool_t, (unsigned) n_control);
+ p = 0;
+
+ while ((k = pa_split(cdata, ",", &state)) && p < n_control) {
+ double f;
+
+ if (*k == 0) {
+ use_default[p++] = TRUE;
+ pa_xfree(k);
+ continue;
+ }
+
+ if (pa_atod(k, &f) < 0) {
+ pa_log("Failed to parse control value '%s'", k);
+ pa_xfree(k);
+ goto fail;
+ }
+
+ pa_xfree(k);
+
+ use_default[p] = FALSE;
+ u->control[p++] = (LADSPA_Data) f;
+ }
+
+ /* The previous loop doesn't take the last control value into account
+ if it is left empty, so we do it here. */
+ if (*cdata == 0 || cdata[strlen(cdata) - 1] == ',') {
+ if (p < n_control)
+ use_default[p] = TRUE;
+ p++;
+ }
+
+ if (p > n_control || k) {
+ pa_log("Too many control values passed, %lu expected.", n_control);
+ pa_xfree(k);
+ goto fail;
+ }
+
+ if (p < n_control) {
+ pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control, p);
+ goto fail;
+ }
+
+ h = 0;
+ for (p = 0; p < d->PortCount; p++) {
+ LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
+
+ if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
+ continue;
+
+ if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
+ for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
+ d->connect_port(u->handle[c], p, &u->control_out);
+ continue;
+ }
+
+ pa_assert(h < n_control);
+
+ if (use_default[h]) {
+ LADSPA_Data lower, upper;
+
+ if (!LADSPA_IS_HINT_HAS_DEFAULT(hint)) {
+ pa_log("Control port value left empty but plugin defines no default.");
+ goto fail;
+ }
+
+ lower = d->PortRangeHints[p].LowerBound;
+ upper = d->PortRangeHints[p].UpperBound;
+
+ if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
+ lower *= (LADSPA_Data) ss.rate;
+ upper *= (LADSPA_Data) ss.rate;
+ }
+
+ switch (hint & LADSPA_HINT_DEFAULT_MASK) {
+
+ case LADSPA_HINT_DEFAULT_MINIMUM:
+ u->control[h] = lower;
+ break;
+
+ case LADSPA_HINT_DEFAULT_MAXIMUM:
+ u->control[h] = upper;
+ break;
+
+ case LADSPA_HINT_DEFAULT_LOW:
+ if (LADSPA_IS_HINT_LOGARITHMIC(hint))
+ u->control[h] = (LADSPA_Data) exp(log(lower) * 0.75 + log(upper) * 0.25);
+ else
+ u->control[h] = (LADSPA_Data) (lower * 0.75 + upper * 0.25);
+ break;
+
+ case LADSPA_HINT_DEFAULT_MIDDLE:
+ if (LADSPA_IS_HINT_LOGARITHMIC(hint))
+ u->control[h] = (LADSPA_Data) exp(log(lower) * 0.5 + log(upper) * 0.5);
+ else
+ u->control[h] = (LADSPA_Data) (lower * 0.5 + upper * 0.5);
+ break;
+
+ case LADSPA_HINT_DEFAULT_HIGH:
+ if (LADSPA_IS_HINT_LOGARITHMIC(hint))
+ u->control[h] = (LADSPA_Data) exp(log(lower) * 0.25 + log(upper) * 0.75);
+ else
+ u->control[h] = (LADSPA_Data) (lower * 0.25 + upper * 0.75);
+ break;
+
+ case LADSPA_HINT_DEFAULT_0:
+ u->control[h] = 0;
+ break;
+
+ case LADSPA_HINT_DEFAULT_1:
+ u->control[h] = 1;
+ break;
+
+ case LADSPA_HINT_DEFAULT_100:
+ u->control[h] = 100;
+ break;
+
+ case LADSPA_HINT_DEFAULT_440:
+ u->control[h] = 440;
+ break;
+
+ default:
+ pa_assert_not_reached();
+ }
+ }
+
+ if (LADSPA_IS_HINT_INTEGER(hint))
+ u->control[h] = roundf(u->control[h]);
+
+ pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
+
+ for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
+ d->connect_port(u->handle[c], p, &u->control[h]);
+
+ h++;
+ }
+
+ pa_assert(h == n_control);
+ }
+
+ if (d->activate)
+ for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
+ d->activate(u->handle[c]);
+
+ /* Create sink */
+ pa_sink_new_data_init(&sink_data);
+ sink_data.driver = __FILE__;
+ sink_data.module = m;
+ if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
+ sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
+ pa_sink_new_data_set_sample_spec(&sink_data, &ss);
+ pa_sink_new_data_set_channel_map(&sink_data, &map);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.label", d->Label);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.name", d->Name);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.maker", d->Maker);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright);
+ pa_proplist_setf(sink_data.proplist, "device.ladspa.unique_id", "%lu", (unsigned long) d->UniqueID);
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&sink_data);
+ goto fail;
+ }
+
+ if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
+ const char *z;
+
+ z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", d->Name, z ? z : master->name);
+ }
+
+ u->sink = pa_sink_new(m->core, &sink_data,
+ PA_SINK_HW_MUTE_CTRL|PA_SINK_HW_VOLUME_CTRL|PA_SINK_DECIBEL_VOLUME|
+ (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY)));
+ pa_sink_new_data_done(&sink_data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg_cb;
+ u->sink->set_state = sink_set_state_cb;
+ u->sink->update_requested_latency = sink_update_requested_latency_cb;
+ u->sink->request_rewind = sink_request_rewind_cb;
+ u->sink->set_volume = sink_set_volume_cb;
+ u->sink->set_mute = sink_set_mute_cb;
+ u->sink->userdata = u;
+
+ pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
+
+ /* Create sink input */
+ pa_sink_input_new_data_init(&sink_input_data);
+ sink_input_data.driver = __FILE__;
+ sink_input_data.module = m;
+ pa_sink_input_new_data_set_sink(&sink_input_data, master, FALSE);
+ sink_input_data.origin_sink = u->sink;
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
+ pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
+ pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
+
+ pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
+ pa_sink_input_new_data_done(&sink_input_data);
+
+ if (!u->sink_input)
+ goto fail;
+
+ u->sink_input->pop = sink_input_pop_cb;
+ u->sink_input->process_rewind = sink_input_process_rewind_cb;
+ u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
+ u->sink_input->update_max_request = sink_input_update_max_request_cb;
+ u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
+ u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
+ u->sink_input->kill = sink_input_kill_cb;
+ u->sink_input->attach = sink_input_attach_cb;
+ u->sink_input->detach = sink_input_detach_cb;
+ u->sink_input->state_change = sink_input_state_change_cb;
+ u->sink_input->may_move_to = sink_input_may_move_to_cb;
+ u->sink_input->moving = sink_input_moving_cb;
+ u->sink_input->volume_changed = sink_input_volume_changed_cb;
+ u->sink_input->mute_changed = sink_input_mute_changed_cb;
+ u->sink_input->userdata = u;
+
+ u->sink->input_to_master = u->sink_input;
+
+ pa_sink_put(u->sink);
+ pa_sink_input_put(u->sink_input);
+
+ pa_modargs_free(ma);
+ pa_xfree(use_default);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa_xfree(use_default);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+ unsigned c;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ /* See comments in sink_input_kill_cb() above regarding
+ * destruction order! */
+
+ if (u->sink_input)
+ pa_sink_input_unlink(u->sink_input);
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->sink_input)
+ pa_sink_input_unref(u->sink_input);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ for (c = 0; c < (u->channels / u->max_ladspaport_count); c++) {
+ if (u->handle[c]) {
+ if (u->descriptor->deactivate)
+ u->descriptor->deactivate(u->handle[c]);
+ u->descriptor->cleanup(u->handle[c]);
+ }
+ }
+
+ if (u->output == u->input) {
+ if (u->input != NULL) {
+ for (c = 0; c < u->max_ladspaport_count; c++)
+ pa_xfree(u->input[c]);
+ pa_xfree(u->input);
+ }
+ } else {
+ if (u->input != NULL) {
+ for (c = 0; c < u->input_count; c++)
+ pa_xfree(u->input[c]);
+ pa_xfree(u->input);
+ }
+ if (u->output != NULL) {
+ for (c = 0; c < u->output_count; c++)
+ pa_xfree(u->output[c]);
+ pa_xfree(u->output);
+ }
+ }
+
+ if (u->memblockq)
+ pa_memblockq_free(u->memblockq);
+
+ pa_xfree(u->control);
+ pa_xfree(u);
+}
diff --git a/src/modules/module-lirc.c b/src/modules/module-lirc.c
index a93a3b92..15f3442d 100644
--- a/src/modules/module-lirc.c
+++ b/src/modules/module-lirc.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2005-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,12 +24,12 @@
#endif
#include <stdio.h>
-#include <assert.h>
#include <unistd.h>
#include <string.h>
-#include <lirc/lirc_client.h>
#include <stdlib.h>
+#include <lirc/lirc_client.h>
+
#include <pulse/xmalloc.h>
#include <pulsecore/module.h>
@@ -37,18 +37,22 @@
#include <pulsecore/namereg.h>
#include <pulsecore/sink.h>
#include <pulsecore/modargs.h>
+#include <pulsecore/macro.h>
#include "module-lirc-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("LIRC volume control")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("config=<config file> sink=<sink name> appname=<lirc application name>")
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("LIRC volume control");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE("config=<config file> sink=<sink name> appname=<lirc application name> volume_limit=<volume limit> volume_step=<volume change step>");
static const char* const valid_modargs[] = {
"config",
"sink",
"appname",
+ "volume_limit",
+ "volume_step",
NULL,
};
@@ -59,34 +63,35 @@ struct userdata {
char *sink_name;
pa_module *module;
float mute_toggle_save;
+ pa_volume_t volume_limit;
+ pa_volume_t volume_step;
};
-static int lirc_in_use = 0;
-
-static void io_callback(pa_mainloop_api *io, PA_GCC_UNUSED pa_io_event *e, PA_GCC_UNUSED int fd, pa_io_event_flags_t events, void*userdata) {
+static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event_flags_t events, void*userdata) {
struct userdata *u = userdata;
char *name = NULL, *code = NULL;
- assert(io);
- assert(u);
+
+ pa_assert(io);
+ pa_assert(u);
if (events & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) {
- pa_log(__FILE__": lost connection to LIRC daemon.");
+ pa_log("Lost connection to LIRC daemon.");
goto fail;
}
-
+
if (events & PA_IO_EVENT_INPUT) {
char *c;
-
+
if (lirc_nextcode(&code) != 0 || !code) {
- pa_log(__FILE__": lirc_nextcode() failed.");
+ pa_log("lirc_nextcode() failed.");
goto fail;
}
-
+
c = pa_xstrdup(code);
c[strcspn(c, "\n\r")] = 0;
- pa_log_debug(__FILE__": raw IR code '%s'", c);
+ pa_log_debug("Raw IR code '%s'", c);
pa_xfree(c);
-
+
while (lirc_code2char(u->config, code, &name) == 0 && name) {
enum {
INVALID,
@@ -96,9 +101,9 @@ static void io_callback(pa_mainloop_api *io, PA_GCC_UNUSED pa_io_event *e, PA_GC
RESET,
MUTE_TOGGLE
} volchange = INVALID;
-
- pa_log_info(__FILE__": translated IR code '%s'", name);
-
+
+ pa_log_info("Translated IR code '%s'", name);
+
if (strcasecmp(name, "volume-up") == 0)
volchange = UP;
else if (strcasecmp(name, "volume-down") == 0)
@@ -109,58 +114,42 @@ static void io_callback(pa_mainloop_api *io, PA_GCC_UNUSED pa_io_event *e, PA_GC
volchange = MUTE_TOGGLE;
else if (strcasecmp(name, "reset") == 0)
volchange = RESET;
-
+
if (volchange == INVALID)
- pa_log_warn(__FILE__": recieved unknown IR code '%s'", name);
+ pa_log_warn("Received unknown IR code '%s'", name);
else {
pa_sink *s;
-
- if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK, 1)))
- pa_log(__FILE__": failed to get sink '%s'", u->sink_name);
- else {
- int i;
- pa_cvolume cv = *pa_sink_get_volume(s, PA_MIXER_HARDWARE);
-#define DELTA (PA_VOLUME_NORM/20)
+ if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK)))
+ pa_log("Failed to get sink '%s'", u->sink_name);
+ else {
+ pa_cvolume cv = *pa_sink_get_volume(s, FALSE);
switch (volchange) {
case UP:
- for (i = 0; i < cv.channels; i++) {
- cv.values[i] += DELTA;
-
- if (cv.values[i] > PA_VOLUME_NORM)
- cv.values[i] = PA_VOLUME_NORM;
- }
-
- pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
+ pa_cvolume_inc_clamp(&cv, u->volume_step, u->volume_limit);
+ pa_sink_set_volume(s, &cv, TRUE, TRUE);
break;
-
+
case DOWN:
- for (i = 0; i < cv.channels; i++) {
- if (cv.values[i] >= DELTA)
- cv.values[i] -= DELTA;
- else
- cv.values[i] = PA_VOLUME_MUTED;
- }
-
- pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
+ pa_cvolume_dec(&cv, u->volume_step);
+ pa_sink_set_volume(s, &cv, TRUE, TRUE);
break;
-
+
case MUTE:
- pa_sink_set_mute(s, PA_MIXER_HARDWARE, 0);
+ pa_sink_set_mute(s, TRUE, TRUE);
break;
-
+
case RESET:
- pa_sink_set_mute(s, PA_MIXER_HARDWARE, 1);
+ pa_sink_set_mute(s, FALSE, TRUE);
break;
-
- case MUTE_TOGGLE:
- pa_sink_set_mute(s, PA_MIXER_HARDWARE, !pa_sink_get_mute(s, PA_MIXER_HARDWARE));
+ case MUTE_TOGGLE:
+ pa_sink_set_mute(s, !pa_sink_get_mute(s, FALSE), TRUE);
break;
case INVALID:
- ;
+ pa_assert_not_reached();
}
}
}
@@ -170,55 +159,63 @@ static void io_callback(pa_mainloop_api *io, PA_GCC_UNUSED pa_io_event *e, PA_GC
pa_xfree(code);
return;
-
+
fail:
u->module->core->mainloop->io_free(u->io);
u->io = NULL;
- pa_module_unload_request(u->module);
+ pa_module_unload_request(u->module, TRUE);
- free(code);
+ pa_xfree(code);
}
-
-int pa__init(pa_core *c, pa_module*m) {
+
+int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
struct userdata *u;
- assert(c && m);
+ pa_volume_t volume_limit = PA_CLAMP_VOLUME(PA_VOLUME_NORM*3/2);
+ pa_volume_t volume_step = PA_VOLUME_NORM/20;
+
+ pa_assert(m);
- if (lirc_in_use) {
- pa_log(__FILE__": module-lirc may no be loaded twice.");
- return -1;
- }
-
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": Failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto fail;
}
- m->userdata = u = pa_xmalloc(sizeof(struct userdata));
+ if (pa_modargs_get_value_u32(ma, "volume_limit", &volume_limit) < 0) {
+ pa_log("Failed to parse volume limit");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_u32(ma, "volume_step", &volume_step) < 0) {
+ pa_log("Failed to parse volume step");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
u->module = m;
u->io = NULL;
u->config = NULL;
u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
u->lirc_fd = -1;
u->mute_toggle_save = 0;
+ u->volume_limit = PA_CLAMP_VOLUME(volume_limit);
+ u->volume_step = PA_CLAMP_VOLUME(volume_step);
if ((u->lirc_fd = lirc_init((char*) pa_modargs_get_value(ma, "appname", "pulseaudio"), 1)) < 0) {
- pa_log(__FILE__": lirc_init() failed.");
+ pa_log("lirc_init() failed.");
goto fail;
}
if (lirc_readconfig((char*) pa_modargs_get_value(ma, "config", NULL), &u->config, NULL) < 0) {
- pa_log(__FILE__": lirc_readconfig() failed.");
+ pa_log("lirc_readconfig() failed.");
goto fail;
}
-
- u->io = c->mainloop->io_new(c->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
- lirc_in_use = 1;
+ u->io = m->core->mainloop->io_new(m->core->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
pa_modargs_free(ma);
-
+
return 0;
fail:
@@ -226,14 +223,13 @@ fail:
if (ma)
pa_modargs_free(ma);
- pa__done(c, m);
+ pa__done(m);
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata *u;
- assert(c);
- assert(m);
+ pa_assert(m);
if (!(u = m->userdata))
return;
@@ -249,6 +245,4 @@ void pa__done(pa_core *c, pa_module*m) {
pa_xfree(u->sink_name);
pa_xfree(u);
-
- lirc_in_use = 0;
}
diff --git a/src/modules/module-loopback.c b/src/modules/module-loopback.c
new file mode 100644
index 00000000..cf88267d
--- /dev/null
+++ b/src/modules/module-loopback.c
@@ -0,0 +1,857 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Intel Corporation
+ Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/sink-input.h>
+#include <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-util.h>
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+
+#include "module-loopback-symdef.h"
+
+PA_MODULE_AUTHOR("Pierre-Louis Bossart");
+PA_MODULE_DESCRIPTION("Loopback from source to sink");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "source=<source to connect to> "
+ "sink=<sink to connect to> "
+ "adjust_time=<how often to readjust rates in s> "
+ "latency_msec=<latency in ms> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "sink_input_name=<custom name for the sink input> "
+ "source_output_name=<custom name for the source output> "
+ "sink_input_role=<media.role for the sink input> "
+ "source_output_role=<media.role for the source output> "
+ "source_dont_move=<boolean> "
+ "sink_dont_move=<boolean> "
+ "remix=<remix channels?> ");
+
+#define DEFAULT_LATENCY_MSEC 200
+
+#define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
+
+#define DEFAULT_ADJUST_TIME_USEC (10*PA_USEC_PER_SEC)
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+
+ pa_sink_input *sink_input;
+ pa_source_output *source_output;
+
+ pa_asyncmsgq *asyncmsgq;
+ pa_memblockq *memblockq;
+
+ pa_rtpoll_item *rtpoll_item_read, *rtpoll_item_write;
+
+ pa_time_event *time_event;
+ pa_usec_t adjust_time;
+
+ int64_t recv_counter;
+ int64_t send_counter;
+
+ size_t skip;
+ pa_usec_t latency;
+
+ pa_bool_t in_pop;
+ size_t min_memblockq_length;
+
+ struct {
+ int64_t send_counter;
+ size_t source_output_buffer;
+ pa_usec_t source_latency;
+
+ int64_t recv_counter;
+ size_t sink_input_buffer;
+ pa_usec_t sink_latency;
+
+ size_t min_memblockq_length;
+ size_t max_request;
+ } latency_snapshot;
+};
+
+static const char* const valid_modargs[] = {
+ "source",
+ "sink",
+ "adjust_time",
+ "latency_msec",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "sink_input_name",
+ "source_output_name",
+ "sink_input_role",
+ "source_output_role",
+ "source_dont_move",
+ "sink_dont_move",
+ "remix",
+ NULL,
+};
+
+enum {
+ SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX,
+ SINK_INPUT_MESSAGE_REWIND,
+ SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT,
+ SINK_INPUT_MESSAGE_MAX_REQUEST_CHANGED
+};
+
+enum {
+ SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
+};
+
+/* Called from main context */
+static void teardown(struct userdata *u) {
+ pa_assert(u);
+ pa_assert_ctl_context();
+
+ if (u->sink_input)
+ pa_sink_input_unlink(u->sink_input);
+
+ if (u->source_output)
+ pa_source_output_unlink(u->source_output);
+
+ if (u->sink_input) {
+ pa_sink_input_unref(u->sink_input);
+ u->sink_input = NULL;
+ }
+
+ if (u->source_output) {
+ pa_source_output_unref(u->source_output);
+ u->source_output = NULL;
+ }
+}
+
+/* Called from main context */
+static void adjust_rates(struct userdata *u) {
+ size_t buffer, fs;
+ uint32_t old_rate, base_rate, new_rate;
+ pa_usec_t buffer_latency;
+
+ pa_assert(u);
+ pa_assert_ctl_context();
+
+ pa_asyncmsgq_send(u->source_output->source->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT, NULL, 0, NULL);
+ pa_asyncmsgq_send(u->sink_input->sink->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT, NULL, 0, NULL);
+
+ buffer =
+ u->latency_snapshot.sink_input_buffer +
+ u->latency_snapshot.source_output_buffer;
+
+ if (u->latency_snapshot.recv_counter <= u->latency_snapshot.send_counter)
+ buffer += (size_t) (u->latency_snapshot.send_counter - u->latency_snapshot.recv_counter);
+ else
+ buffer += PA_CLIP_SUB(buffer, (size_t) (u->latency_snapshot.recv_counter - u->latency_snapshot.send_counter));
+
+ buffer_latency = pa_bytes_to_usec(buffer, &u->sink_input->sample_spec);
+
+ pa_log_debug("Loopback overall latency is %0.2f ms + %0.2f ms + %0.2f ms = %0.2f ms",
+ (double) u->latency_snapshot.sink_latency / PA_USEC_PER_MSEC,
+ (double) buffer_latency / PA_USEC_PER_MSEC,
+ (double) u->latency_snapshot.source_latency / PA_USEC_PER_MSEC,
+ ((double) u->latency_snapshot.sink_latency + buffer_latency + u->latency_snapshot.source_latency) / PA_USEC_PER_MSEC);
+
+ pa_log_debug("Should buffer %zu bytes, buffered at minimum %zu bytes",
+ u->latency_snapshot.max_request*2,
+ u->latency_snapshot.min_memblockq_length);
+
+ fs = pa_frame_size(&u->sink_input->sample_spec);
+ old_rate = u->sink_input->sample_spec.rate;
+ base_rate = u->source_output->sample_spec.rate;
+
+ if (u->latency_snapshot.min_memblockq_length < u->latency_snapshot.max_request*2)
+ new_rate = base_rate - (((u->latency_snapshot.max_request*2 - u->latency_snapshot.min_memblockq_length) / fs) *PA_USEC_PER_SEC)/u->adjust_time;
+ else
+ new_rate = base_rate + (((u->latency_snapshot.min_memblockq_length - u->latency_snapshot.max_request*2) / fs) *PA_USEC_PER_SEC)/u->adjust_time;
+
+ if (new_rate < (uint32_t) (base_rate*0.8) || new_rate > (uint32_t) (base_rate*1.25)) {
+ pa_log_warn("Sample rates too different, not adjusting (%u vs. %u).", base_rate, new_rate);
+ new_rate = base_rate;
+ } else {
+ if (base_rate < new_rate + 20 && new_rate < base_rate + 20)
+ new_rate = base_rate;
+ /* Do the adjustment in small steps; 2‰ can be considered inaudible */
+ if (new_rate < (uint32_t) (old_rate*0.998) || new_rate > (uint32_t) (old_rate*1.002)) {
+ pa_log_info("New rate of %u Hz not within 2‰ of %u Hz, forcing smaller adjustment", new_rate, old_rate);
+ new_rate = PA_CLAMP(new_rate, (uint32_t) (old_rate*0.998), (uint32_t) (old_rate*1.002));
+ }
+ }
+
+ pa_sink_input_set_rate(u->sink_input, new_rate);
+ pa_log_debug("[%s] Updated sampling rate to %lu Hz.", u->sink_input->sink->name, (unsigned long) new_rate);
+
+ pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
+}
+
+/* Called from main context */
+static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+ pa_assert(a);
+ pa_assert(u->time_event == e);
+
+ adjust_rates(u);
+}
+
+/* Called from input thread context */
+static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
+ struct userdata *u;
+ pa_memchunk copy;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ if (u->skip > chunk->length) {
+ u->skip -= chunk->length;
+ return;
+ }
+
+ if (u->skip > 0) {
+ copy = *chunk;
+ copy.index += u->skip;
+ copy.length -= u->skip;
+ u->skip = 0;
+
+ chunk = &copy;
+ }
+
+ pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, chunk, NULL);
+ u->send_counter += (int64_t) chunk->length;
+}
+
+/* Called from input thread context */
+static void source_output_process_rewind_cb(pa_source_output *o, size_t nbytes) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_REWIND, NULL, (int64_t) nbytes, NULL, NULL);
+ u->send_counter -= (int64_t) nbytes;
+}
+
+/* Called from output thread context */
+static int source_output_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE_OUTPUT(obj)->userdata;
+
+ switch (code) {
+
+ case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT: {
+ size_t length;
+
+ length = pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq);
+
+ u->latency_snapshot.send_counter = u->send_counter;
+ u->latency_snapshot.source_output_buffer = u->source_output->thread_info.resampler ? pa_resampler_result(u->source_output->thread_info.resampler, length) : length;
+ u->latency_snapshot.source_latency = pa_source_get_latency_within_thread(u->source_output->source);
+
+ return 0;
+ }
+ }
+
+ return pa_source_output_process_msg(obj, code, data, offset, chunk);
+}
+
+/* Called from output thread context */
+static void source_output_attach_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ u->rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
+ o->source->thread_info.rtpoll,
+ PA_RTPOLL_LATE,
+ u->asyncmsgq);
+}
+
+/* Called from output thread context */
+static void source_output_detach_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ if (u->rtpoll_item_write) {
+ pa_rtpoll_item_free(u->rtpoll_item_write);
+ u->rtpoll_item_write = NULL;
+ }
+}
+
+/* Called from output thread context */
+static void source_output_state_change_cb(pa_source_output *o, pa_source_output_state_t state) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ if (PA_SOURCE_OUTPUT_IS_LINKED(state) && o->thread_info.state == PA_SOURCE_OUTPUT_INIT) {
+
+ u->skip = pa_usec_to_bytes(PA_CLIP_SUB(pa_source_get_latency_within_thread(o->source),
+ u->latency),
+ &o->sample_spec);
+
+ pa_log_info("Skipping %lu bytes", (unsigned long) u->skip);
+ }
+}
+
+/* Called from main thread */
+static void source_output_kill_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert_se(u = o->userdata);
+
+ teardown(u);
+ pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from main thread */
+static pa_bool_t source_output_may_move_to_cb(pa_source_output *o, pa_source *dest) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert_se(u = o->userdata);
+
+ return dest != u->sink_input->sink->monitor_source;
+}
+
+/* Called from main thread */
+static void source_output_moving_cb(pa_source_output *o, pa_source *dest) {
+ pa_proplist *p;
+ const char *n;
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert_se(u = o->userdata);
+
+ p = pa_proplist_new();
+ pa_proplist_setf(p, PA_PROP_MEDIA_NAME, "Loopback of %s", pa_strnull(pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+
+ if ((n = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_ICON_NAME)))
+ pa_proplist_sets(p, PA_PROP_MEDIA_ICON_NAME, n);
+
+ pa_sink_input_update_proplist(u->sink_input, PA_UPDATE_REPLACE, p);
+ pa_proplist_free(p);
+}
+
+/* Called from output thread context */
+static void update_min_memblockq_length(struct userdata *u) {
+ size_t length;
+
+ pa_assert(u);
+ pa_sink_input_assert_io_context(u->sink_input);
+
+ length = pa_memblockq_get_length(u->memblockq);
+
+ if (u->min_memblockq_length == (size_t) -1 ||
+ length < u->min_memblockq_length)
+ u->min_memblockq_length = length;
+}
+
+/* Called from output thread context */
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_sink_input_assert_io_context(i);
+ pa_assert_se(u = i->userdata);
+ pa_assert(chunk);
+
+ u->in_pop = TRUE;
+ while (pa_asyncmsgq_process_one(u->asyncmsgq) > 0)
+ ;
+ u->in_pop = FALSE;
+
+ if (pa_memblockq_peek(u->memblockq, chunk) < 0) {
+ pa_log_info("Could not peek into queue");
+ return -1;
+ }
+
+ chunk->length = PA_MIN(chunk->length, nbytes);
+ pa_memblockq_drop(u->memblockq, chunk->length);
+
+ update_min_memblockq_length(u);
+
+ return 0;
+}
+
+/* Called from output thread context */
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_sink_input_assert_io_context(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_memblockq_rewind(u->memblockq, nbytes);
+}
+
+/* Called from output thread context */
+static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK_INPUT(obj)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
+ pa_usec_t *r = data;
+
+ pa_sink_input_assert_io_context(u->sink_input);
+
+ *r = pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &u->sink_input->sample_spec);
+
+ /* Fall through, the default handler will add in the extra
+ * latency added by the resampler */
+ break;
+ }
+
+ case SINK_INPUT_MESSAGE_POST:
+
+ pa_sink_input_assert_io_context(u->sink_input);
+
+ if (PA_SINK_IS_OPENED(u->sink_input->sink->thread_info.state))
+ pa_memblockq_push_align(u->memblockq, chunk);
+ else
+ pa_memblockq_flush_write(u->memblockq, TRUE);
+
+ update_min_memblockq_length(u);
+
+ /* Is this the end of an underrun? Then let's start things
+ * right-away */
+ if (!u->in_pop &&
+ u->sink_input->thread_info.underrun_for > 0 &&
+ pa_memblockq_is_readable(u->memblockq)) {
+
+ pa_log_debug("Requesting rewind due to end of underrun.");
+ pa_sink_input_request_rewind(u->sink_input,
+ (size_t) (u->sink_input->thread_info.underrun_for == (size_t) -1 ? 0 : u->sink_input->thread_info.underrun_for),
+ FALSE, TRUE, FALSE);
+ }
+
+ u->recv_counter += (int64_t) chunk->length;
+
+ return 0;
+
+ case SINK_INPUT_MESSAGE_REWIND:
+
+ pa_sink_input_assert_io_context(u->sink_input);
+
+ if (PA_SINK_IS_OPENED(u->sink_input->sink->thread_info.state))
+ pa_memblockq_seek(u->memblockq, -offset, PA_SEEK_RELATIVE, TRUE);
+ else
+ pa_memblockq_flush_write(u->memblockq, TRUE);
+
+ u->recv_counter -= offset;
+
+ update_min_memblockq_length(u);
+
+ return 0;
+
+ case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT: {
+ size_t length;
+
+ update_min_memblockq_length(u);
+
+ length = pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq);
+
+ u->latency_snapshot.recv_counter = u->recv_counter;
+ u->latency_snapshot.sink_input_buffer =
+ pa_memblockq_get_length(u->memblockq) +
+ (u->sink_input->thread_info.resampler ? pa_resampler_request(u->sink_input->thread_info.resampler, length) : length);
+ u->latency_snapshot.sink_latency = pa_sink_get_latency_within_thread(u->sink_input->sink);
+
+ u->latency_snapshot.max_request = pa_sink_input_get_max_request(u->sink_input);
+
+ u->latency_snapshot.min_memblockq_length = u->min_memblockq_length;
+ u->min_memblockq_length = (size_t) -1;
+
+ return 0;
+ }
+
+ case SINK_INPUT_MESSAGE_MAX_REQUEST_CHANGED: {
+ /* This message is sent from the IO thread to the main
+ * thread! So don't be confused. All the user cases above
+ * are executed in thread context, but this one is not! */
+
+ pa_assert_ctl_context();
+
+ if (u->adjust_time > 0)
+ adjust_rates(u);
+ return 0;
+ }
+ }
+
+ return pa_sink_input_process_msg(obj, code, data, offset, chunk);
+}
+
+/* Called from output thread context */
+static void sink_input_attach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_sink_input_assert_io_context(i);
+ pa_assert_se(u = i->userdata);
+
+ u->rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
+ i->sink->thread_info.rtpoll,
+ PA_RTPOLL_LATE,
+ u->asyncmsgq);
+
+ pa_memblockq_set_prebuf(u->memblockq, pa_sink_input_get_max_request(i)*2);
+ pa_memblockq_set_maxrewind(u->memblockq, pa_sink_input_get_max_rewind(i));
+
+ u->min_memblockq_length = (size_t) -1;
+}
+
+/* Called from output thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_sink_input_assert_io_context(i);
+ pa_assert_se(u = i->userdata);
+
+ if (u->rtpoll_item_read) {
+ pa_rtpoll_item_free(u->rtpoll_item_read);
+ u->rtpoll_item_read = NULL;
+ }
+}
+
+/* Called from output thread context */
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_sink_input_assert_io_context(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_memblockq_set_maxrewind(u->memblockq, nbytes);
+}
+
+/* Called from output thread context */
+static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_sink_input_assert_io_context(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_memblockq_set_prebuf(u->memblockq, nbytes*2);
+ pa_log_info("Max request changed");
+ pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_MAX_REQUEST_CHANGED, NULL, 0, NULL, NULL);
+}
+
+/* Called from main thread */
+static void sink_input_kill_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_ctl_context();
+ pa_assert_se(u = i->userdata);
+
+ teardown(u);
+ pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from main thread */
+static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+ pa_proplist *p;
+ const char *n;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_ctl_context();
+ pa_assert_se(u = i->userdata);
+
+ p = pa_proplist_new();
+ pa_proplist_setf(p, PA_PROP_MEDIA_NAME, "Loopback to %s", pa_strnull(pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+
+ if ((n = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_ICON_NAME)))
+ pa_proplist_sets(p, PA_PROP_MEDIA_ICON_NAME, n);
+
+ pa_source_output_update_proplist(u->source_output, PA_UPDATE_REPLACE, p);
+ pa_proplist_free(p);
+}
+
+/* Called from main thread */
+static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_ctl_context();
+ pa_assert_se(u = i->userdata);
+
+ if (!u->source_output->source->monitor_of)
+ return TRUE;
+
+ return dest != u->source_output->source->monitor_of;
+}
+
+int pa__init(pa_module *m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+ pa_sink *sink;
+ pa_sink_input_new_data sink_input_data;
+ pa_bool_t sink_dont_move;
+ pa_source *source;
+ pa_source_output_new_data source_output_data;
+ pa_bool_t source_dont_move;
+ uint32_t latency_msec;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_memchunk silence;
+ uint32_t adjust_time_sec;
+ const char *n;
+ pa_bool_t remix = TRUE;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (!(source = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE))) {
+ pa_log("No such source.");
+ goto fail;
+ }
+
+ if (!(sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK))) {
+ pa_log("No such sink.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "remix", &remix) < 0) {
+ pa_log("Invalid boolean remix parameter");
+ goto fail;
+ }
+
+ ss = sink->sample_spec;
+ map = sink->channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+ pa_log("Invalid sample format specification or channel map");
+ goto fail;
+ }
+
+ latency_msec = DEFAULT_LATENCY_MSEC;
+ if (pa_modargs_get_value_u32(ma, "latency_msec", &latency_msec) < 0 || latency_msec < 1 || latency_msec > 2000) {
+ pa_log("Invalid latency specification");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->latency = (pa_usec_t) latency_msec * PA_USEC_PER_MSEC;
+
+ adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
+ if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
+ pa_log("Failed to parse adjust_time value");
+ goto fail;
+ }
+
+ if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
+ u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
+ else
+ u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
+
+ pa_sink_input_new_data_init(&sink_input_data);
+ sink_input_data.driver = __FILE__;
+ sink_input_data.module = m;
+ pa_sink_input_new_data_set_sink(&sink_input_data, sink, FALSE);
+
+ if ((n = pa_modargs_get_value(ma, "sink_input_name", NULL)))
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, n);
+ else
+ pa_proplist_setf(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Loopback from %s",
+ pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+
+ if ((n = pa_modargs_get_value(ma, "sink_input_role", NULL)))
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, n);
+ else
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
+
+ if ((n = pa_proplist_gets(source->proplist, PA_PROP_DEVICE_ICON_NAME)))
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ICON_NAME, n);
+
+ pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
+ pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
+ sink_input_data.flags = PA_SINK_INPUT_VARIABLE_RATE | (remix ? 0 : PA_SINK_INPUT_NO_REMIX);
+
+ sink_dont_move = FALSE;
+ if (pa_modargs_get_value_boolean(ma, "sink_dont_move", &sink_dont_move) < 0) {
+ pa_log("sink_dont_move= expects a boolean argument.");
+ goto fail;
+ }
+
+ if (sink_dont_move)
+ sink_input_data.flags |= PA_SINK_INPUT_DONT_MOVE;
+
+ pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
+ pa_sink_input_new_data_done(&sink_input_data);
+
+ if (!u->sink_input)
+ goto fail;
+
+ u->sink_input->parent.process_msg = sink_input_process_msg_cb;
+ u->sink_input->pop = sink_input_pop_cb;
+ u->sink_input->process_rewind = sink_input_process_rewind_cb;
+ u->sink_input->kill = sink_input_kill_cb;
+ u->sink_input->attach = sink_input_attach_cb;
+ u->sink_input->detach = sink_input_detach_cb;
+ u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
+ u->sink_input->update_max_request = sink_input_update_max_request_cb;
+ u->sink_input->may_move_to = sink_input_may_move_to_cb;
+ u->sink_input->moving = sink_input_moving_cb;
+ u->sink_input->userdata = u;
+
+ pa_sink_input_set_requested_latency(u->sink_input, u->latency/3);
+
+ pa_source_output_new_data_init(&source_output_data);
+ source_output_data.driver = __FILE__;
+ source_output_data.module = m;
+ pa_source_output_new_data_set_source(&source_output_data, source, FALSE);
+
+ if ((n = pa_modargs_get_value(ma, "source_output_name", NULL)))
+ pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_NAME, n);
+ else
+ pa_proplist_setf(source_output_data.proplist, PA_PROP_MEDIA_NAME, "Loopback to %s",
+ pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+
+ if ((n = pa_modargs_get_value(ma, "source_output_role", NULL)))
+ pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ROLE, n);
+ else
+ pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
+
+ if ((n = pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_ICON_NAME)))
+ pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ICON_NAME, n);
+
+ pa_source_output_new_data_set_sample_spec(&source_output_data, &ss);
+ pa_source_output_new_data_set_channel_map(&source_output_data, &map);
+ source_output_data.flags = (remix ? 0 : PA_SOURCE_OUTPUT_NO_REMIX);
+
+ source_dont_move = FALSE;
+ if (pa_modargs_get_value_boolean(ma, "source_dont_move", &source_dont_move) < 0) {
+ pa_log("source_dont_move= expects a boolean argument.");
+ goto fail;
+ }
+
+ if (source_dont_move)
+ source_output_data.flags |= PA_SOURCE_OUTPUT_DONT_MOVE;
+
+ pa_source_output_new(&u->source_output, m->core, &source_output_data);
+ pa_source_output_new_data_done(&source_output_data);
+
+ if (!u->source_output)
+ goto fail;
+
+ u->source_output->parent.process_msg = source_output_process_msg_cb;
+ u->source_output->push = source_output_push_cb;
+ u->source_output->process_rewind = source_output_process_rewind_cb;
+ u->source_output->kill = source_output_kill_cb;
+ u->source_output->attach = source_output_attach_cb;
+ u->source_output->detach = source_output_detach_cb;
+ u->source_output->state_change = source_output_state_change_cb;
+ u->source_output->may_move_to = source_output_may_move_to_cb;
+ u->source_output->moving = source_output_moving_cb;
+ u->source_output->userdata = u;
+
+ pa_source_output_set_requested_latency(u->source_output, u->latency/3);
+
+ pa_sink_input_get_silence(u->sink_input, &silence);
+ u->memblockq = pa_memblockq_new(
+ 0, /* idx */
+ MEMBLOCKQ_MAXLENGTH, /* maxlength */
+ MEMBLOCKQ_MAXLENGTH, /* tlength */
+ pa_frame_size(&ss), /* base */
+ 0, /* prebuf */
+ 0, /* minreq */
+ 0, /* maxrewind */
+ &silence); /* silence frame */
+ pa_memblock_unref(silence.memblock);
+
+ u->asyncmsgq = pa_asyncmsgq_new(0);
+
+ pa_sink_input_put(u->sink_input);
+ pa_source_output_put(u->source_output);
+
+ if (u->adjust_time > 0)
+ u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
+
+ pa_modargs_free(ma);
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ teardown(u);
+
+ if (u->memblockq)
+ pa_memblockq_free(u->memblockq);
+
+ if (u->asyncmsgq)
+ pa_asyncmsgq_unref(u->asyncmsgq);
+
+ if (u->time_event)
+ u->core->mainloop->time_free(u->time_event);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-match.c b/src/modules/module-match.c
index ab94b02d..c94ef790 100644
--- a/src/modules/module-match.c
+++ b/src/modules/module-match.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,14 +24,18 @@
#endif
#include <unistd.h>
-#include <assert.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
-#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
+#if defined(HAVE_REGEX_H)
+#include <regex.h>
+#elif defined(HAVE_PCREPOSIX_H)
+#include <pcreposix.h>
+#endif
+
#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h>
@@ -39,36 +43,44 @@
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
-#include <pulsecore/core-subscribe.h>
#include <pulsecore/sink-input.h>
#include <pulsecore/core-util.h>
#include "module-match-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Playback stream expression matching module")
-PA_MODULE_USAGE("table=<filename>")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Playback stream expression matching module");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE("table=<filename> "
+ "key=<property_key>");
#define WHITESPACE "\n\r \t"
#define DEFAULT_MATCH_TABLE_FILE PA_DEFAULT_CONFIG_DIR"/match.table"
#define DEFAULT_MATCH_TABLE_FILE_USER "match.table"
+#define UPDATE_REPLACE "replace"
+#define UPDATE_MERGE "merge"
+
static const char* const valid_modargs[] = {
"table",
+ "key",
NULL,
};
struct rule {
regex_t regex;
pa_volume_t volume;
+ pa_proplist *proplist;
+ pa_update_mode_t mode;
struct rule *next;
};
struct userdata {
struct rule *rules;
- pa_subscription *subscription;
+ char *property_key;
+ pa_hook_slot *sink_input_new_hook_slot;
};
static int load_rules(struct userdata *u, const char *filename) {
@@ -78,60 +90,106 @@ static int load_rules(struct userdata *u, const char *filename) {
struct rule *end = NULL;
char *fn = NULL;
- f = filename ?
- fopen(fn = pa_xstrdup(filename), "r") :
- pa_open_config_file(DEFAULT_MATCH_TABLE_FILE, DEFAULT_MATCH_TABLE_FILE_USER, NULL, &fn, "r");
+ pa_assert(u);
+
+ if (filename)
+ f = pa_fopen_cloexec(fn = pa_xstrdup(filename), "r");
+ else
+ f = pa_open_config_file(DEFAULT_MATCH_TABLE_FILE, DEFAULT_MATCH_TABLE_FILE_USER, NULL, &fn);
if (!f) {
- pa_log(__FILE__": failed to open file '%s': %s", fn, pa_cstrerror(errno));
+ pa_log("Failed to open file config file: %s", pa_cstrerror(errno));
goto finish;
}
pa_lock_fd(fileno(f), 1);
-
+
while (!feof(f)) {
- char *d, *v;
- pa_volume_t volume;
+ char *token_end, *value_str;
+ pa_volume_t volume = PA_VOLUME_NORM;
uint32_t k;
regex_t regex;
char ln[256];
struct rule *rule;
-
+ pa_proplist *proplist = NULL;
+ pa_update_mode_t mode = (pa_update_mode_t) -1;
+
if (!fgets(ln, sizeof(ln), f))
break;
n++;
-
+
pa_strip_nl(ln);
if (ln[0] == '#' || !*ln )
continue;
- d = ln+strcspn(ln, WHITESPACE);
- v = d+strspn(d, WHITESPACE);
+ token_end = ln + strcspn(ln, WHITESPACE);
+ value_str = token_end + strspn(token_end, WHITESPACE);
+ *token_end = 0;
-
- if (!*v) {
- pa_log(__FILE__ ": [%s:%u] failed to parse line - too few words", filename, n);
+ if (!*ln) {
+ pa_log("[%s:%u] failed to parse line - missing regexp", fn, n);
goto finish;
}
- *d = 0;
- if (pa_atou(v, &k) < 0) {
- pa_log(__FILE__": [%s:%u] failed to parse volume", filename, n);
+ if (!*value_str) {
+ pa_log("[%s:%u] failed to parse line - too few words", fn, n);
goto finish;
}
- volume = (pa_volume_t) k;
+ if (pa_atou(value_str, &k) >= 0)
+ volume = (pa_volume_t) PA_CLAMP_VOLUME(k);
+ else {
+ size_t len;
+
+ token_end = value_str + strcspn(value_str, WHITESPACE);
+
+ len = token_end - value_str;
+ if (len == (sizeof(UPDATE_REPLACE) - 1) && !strncmp(value_str, UPDATE_REPLACE, len))
+ mode = PA_UPDATE_REPLACE;
+ else if (len == (sizeof(UPDATE_MERGE) - 1) && !strncmp(value_str, UPDATE_MERGE, len))
+ mode = PA_UPDATE_MERGE;
+
+ if (mode != (pa_update_mode_t) -1) {
+ value_str = token_end + strspn(token_end, WHITESPACE);
+
+ if (!*value_str) {
+ pa_log("[%s:%u] failed to parse line - too few words", fn, n);
+ goto finish;
+ }
+ } else
+ mode = PA_UPDATE_MERGE;
+
+ if (*value_str == '"') {
+ value_str++;
+
+ token_end = strchr(value_str, '"');
+ if (!token_end) {
+ pa_log("[%s:%u] failed to parse line - missing role closing quote", fn, n);
+ goto finish;
+ }
+ } else
+ token_end = value_str + strcspn(value_str, WHITESPACE);
+
+ *token_end = 0;
+
+ value_str = pa_sprintf_malloc("media.role=\"%s\"", value_str);
+ proplist = pa_proplist_from_string(value_str);
+ pa_xfree(value_str);
+ }
-
if (regcomp(&regex, ln, REG_EXTENDED|REG_NOSUB) != 0) {
- pa_log(__FILE__": [%s:%u] invalid regular expression", filename, n);
+ pa_log("[%s:%u] invalid regular expression", fn, n);
+ if (proplist)
+ pa_proplist_free(proplist);
goto finish;
}
- rule = pa_xmalloc(sizeof(struct rule));
+ rule = pa_xnew(struct rule, 1);
rule->regex = regex;
+ rule->proplist = proplist;
+ rule->mode = mode;
rule->volume = volume;
rule->next = NULL;
@@ -140,99 +198,108 @@ static int load_rules(struct userdata *u, const char *filename) {
else
u->rules = rule;
end = rule;
-
- *d = 0;
}
ret = 0;
-
+
finish:
if (f) {
pa_lock_fd(fileno(f), 0);
fclose(f);
}
- if (fn)
- pa_xfree(fn);
+ pa_xfree(fn);
return ret;
}
-static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
- struct userdata *u = userdata;
- pa_sink_input *si;
+static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *si, struct userdata *u) {
struct rule *r;
- assert(c && u);
+ const char *n;
- if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW))
- return;
+ pa_assert(c);
+ pa_assert(u);
- if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
- return;
+ if (!(n = pa_proplist_gets(si->proplist, u->property_key)))
+ return PA_HOOK_OK;
+
+ pa_log_debug("Matching with %s", n);
- if (!si->name)
- return;
-
for (r = u->rules; r; r = r->next) {
- if (!regexec(&r->regex, si->name, 0, NULL, 0)) {
- pa_cvolume cv;
- pa_log_debug(__FILE__": changing volume of sink input '%s' to 0x%03x", si->name, r->volume);
- pa_cvolume_set(&cv, r->volume, si->sample_spec.channels);
- pa_sink_input_set_volume(si, &cv);
+ if (!regexec(&r->regex, n, 0, NULL, 0)) {
+ if (r->proplist) {
+ pa_log_debug("updating proplist of sink input '%s'", n);
+ pa_proplist_update(si->proplist, r->mode, r->proplist);
+ } else if (si->volume_writable) {
+ pa_cvolume cv;
+ pa_log_debug("changing volume of sink input '%s' to 0x%03x", n, r->volume);
+ pa_cvolume_set(&cv, si->sample_spec.channels, r->volume);
+ pa_sink_input_new_data_set_volume(si, &cv);
+ } else
+ pa_log_debug("the volume of sink input '%s' is not writable, can't change it", n);
}
}
+
+ return PA_HOOK_OK;
}
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
struct userdata *u;
- assert(c && m);
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": Failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto fail;
}
- u = pa_xmalloc(sizeof(struct userdata));
+ u = pa_xnew0(struct userdata, 1);
u->rules = NULL;
- u->subscription = NULL;
m->userdata = u;
-
+
+ u->property_key = pa_xstrdup(pa_modargs_get_value(ma, "key", PA_PROP_MEDIA_NAME));
+
if (load_rules(u, pa_modargs_get_value(ma, "table", NULL)) < 0)
goto fail;
- u->subscription = pa_subscription_new(c, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
+ /* hook EARLY - 1, to match before stream-restore */
+ u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY - 1, (pa_hook_cb_t) sink_input_new_hook_callback, u);
pa_modargs_free(ma);
return 0;
fail:
- pa__done(c, m);
+ pa__done(m);
if (ma)
pa_modargs_free(ma);
- return -1;
+ return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata* u;
struct rule *r, *n;
- assert(c && m);
+
+ pa_assert(m);
if (!(u = m->userdata))
return;
- if (u->subscription)
- pa_subscription_free(u->subscription);
-
+ if (u->sink_input_new_hook_slot)
+ pa_hook_slot_free(u->sink_input_new_hook_slot);
+
+ if (u->property_key)
+ pa_xfree(u->property_key);
+
for (r = u->rules; r; r = n) {
n = r->next;
regfree(&r->regex);
+ if (r->proplist)
+ pa_proplist_free(r->proplist);
pa_xfree(r);
}
pa_xfree(u);
}
-
-
diff --git a/src/modules/module-mmkbd-evdev.c b/src/modules/module-mmkbd-evdev.c
index c3d07396..4e89aed9 100644
--- a/src/modules/module-mmkbd-evdev.c
+++ b/src/modules/module-mmkbd-evdev.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2005-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,7 +24,6 @@
#endif
#include <stdio.h>
-#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
@@ -45,27 +44,19 @@
#include "module-mmkbd-evdev-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Multimedia keyboard support via Linux evdev")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("device=<evdev device> sink=<sink name>")
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Multimedia keyboard support via Linux evdev");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE("device=<evdev device> sink=<sink name> volume_limit=<volume limit> volume_step=<volume change step>");
#define DEFAULT_DEVICE "/dev/input/event0"
-/*
- * This isn't defined in older kernel headers and there is no way of
- * detecting it.
- */
-struct _input_id {
- __u16 bustype;
- __u16 vendor;
- __u16 product;
- __u16 version;
-};
-
static const char* const valid_modargs[] = {
"device",
"sink",
+ "volume_limit",
+ "volume_step",
NULL,
};
@@ -74,78 +65,78 @@ struct userdata {
pa_io_event *io;
char *sink_name;
pa_module *module;
+ pa_volume_t volume_limit;
+ pa_volume_t volume_step;
};
-static void io_callback(pa_mainloop_api *io, PA_GCC_UNUSED pa_io_event *e, PA_GCC_UNUSED int fd, pa_io_event_flags_t events, void*userdata) {
+static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event_flags_t events, void*userdata) {
struct userdata *u = userdata;
- assert(io);
- assert(u);
+
+ pa_assert(io);
+ pa_assert(u);
if (events & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) {
- pa_log(__FILE__": lost connection to evdev device.");
+ pa_log("Lost connection to evdev device.");
goto fail;
}
-
+
if (events & PA_IO_EVENT_INPUT) {
struct input_event ev;
if (pa_loop_read(u->fd, &ev, sizeof(ev), &u->fd_type) <= 0) {
- pa_log(__FILE__": failed to read from event device: %s", pa_cstrerror(errno));
+ pa_log("Failed to read from event device: %s", pa_cstrerror(errno));
goto fail;
}
if (ev.type == EV_KEY && (ev.value == 1 || ev.value == 2)) {
- enum { INVALID, UP, DOWN, MUTE_TOGGLE } volchange = INVALID;
+ enum {
+ INVALID,
+ UP,
+ DOWN,
+ MUTE_TOGGLE
+ } volchange = INVALID;
- pa_log_debug(__FILE__": key code=%u, value=%u", ev.code, ev.value);
+ pa_log_debug("Key code=%u, value=%u", ev.code, ev.value);
switch (ev.code) {
- case KEY_VOLUMEDOWN: volchange = DOWN; break;
- case KEY_VOLUMEUP: volchange = UP; break;
- case KEY_MUTE: volchange = MUTE_TOGGLE; break;
+ case KEY_VOLUMEDOWN:
+ volchange = DOWN;
+ break;
+
+ case KEY_VOLUMEUP:
+ volchange = UP;
+ break;
+
+ case KEY_MUTE:
+ volchange = MUTE_TOGGLE;
+ break;
}
if (volchange != INVALID) {
pa_sink *s;
-
- if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK, 1)))
- pa_log(__FILE__": failed to get sink '%s'", u->sink_name);
+
+ if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK)))
+ pa_log("Failed to get sink '%s'", u->sink_name);
else {
- int i;
- pa_cvolume cv = *pa_sink_get_volume(s, PA_MIXER_HARDWARE);
-
-#define DELTA (PA_VOLUME_NORM/20)
-
+ pa_cvolume cv = *pa_sink_get_volume(s, FALSE);
+
switch (volchange) {
case UP:
- for (i = 0; i < cv.channels; i++) {
- cv.values[i] += DELTA;
-
- if (cv.values[i] > PA_VOLUME_NORM)
- cv.values[i] = PA_VOLUME_NORM;
- }
-
- pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
+ pa_cvolume_inc_clamp(&cv, u->volume_step, u->volume_limit);
+ pa_sink_set_volume(s, &cv, TRUE, TRUE);
break;
-
+
case DOWN:
- for (i = 0; i < cv.channels; i++) {
- if (cv.values[i] >= DELTA)
- cv.values[i] -= DELTA;
- else
- cv.values[i] = PA_VOLUME_MUTED;
- }
-
- pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
+ pa_cvolume_dec(&cv, u->volume_step);
+ pa_sink_set_volume(s, &cv, TRUE, TRUE);
break;
-
- case MUTE_TOGGLE:
- pa_sink_set_mute(s, PA_MIXER_HARDWARE, !pa_sink_get_mute(s, PA_MIXER_HARDWARE));
+ case MUTE_TOGGLE:
+ pa_sink_set_mute(s, !pa_sink_get_mute(s, FALSE), TRUE);
break;
case INVALID:
- ;
+ pa_assert_not_reached();
}
}
}
@@ -153,80 +144,96 @@ static void io_callback(pa_mainloop_api *io, PA_GCC_UNUSED pa_io_event *e, PA_GC
}
return;
-
+
fail:
u->module->core->mainloop->io_free(u->io);
u->io = NULL;
- pa_module_unload_request(u->module);
+ pa_module_unload_request(u->module, TRUE);
}
#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
-
-int pa__init(pa_core *c, pa_module*m) {
+
+int pa__init(pa_module*m) {
+
pa_modargs *ma = NULL;
struct userdata *u;
int version;
- struct _input_id input_id;
+ struct input_id input_id;
char name[256];
uint8_t evtype_bitmask[EV_MAX/8 + 1];
- assert(c && m);
+ pa_volume_t volume_limit = PA_VOLUME_NORM*3/2;
+ pa_volume_t volume_step = PA_VOLUME_NORM/20;
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": Failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto fail;
}
- m->userdata = u = pa_xmalloc(sizeof(struct userdata));
+ if (pa_modargs_get_value_u32(ma, "volume_limit", &volume_limit) < 0) {
+ pa_log("Failed to parse volume limit");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_u32(ma, "volume_step", &volume_step) < 0) {
+ pa_log("Failed to parse volume step");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
u->module = m;
u->io = NULL;
u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
u->fd = -1;
u->fd_type = 0;
+ u->volume_limit = PA_CLAMP_VOLUME(volume_limit);
+ u->volume_step = PA_CLAMP_VOLUME(volume_step);
- if ((u->fd = open(pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), O_RDONLY)) < 0) {
- pa_log(__FILE__": failed to open evdev device: %s", pa_cstrerror(errno));
+ if ((u->fd = pa_open_cloexec(pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), O_RDONLY, 0)) < 0) {
+ pa_log("Failed to open evdev device: %s", pa_cstrerror(errno));
goto fail;
}
if (ioctl(u->fd, EVIOCGVERSION, &version) < 0) {
- pa_log(__FILE__": EVIOCGVERSION failed: %s", pa_cstrerror(errno));
+ pa_log("EVIOCGVERSION failed: %s", pa_cstrerror(errno));
goto fail;
}
- pa_log_info(__FILE__": evdev driver version %i.%i.%i", version >> 16, (version >> 8) & 0xff, version & 0xff);
+ pa_log_info("evdev driver version %i.%i.%i", version >> 16, (version >> 8) & 0xff, version & 0xff);
if(ioctl(u->fd, EVIOCGID, &input_id)) {
- pa_log(__FILE__": EVIOCGID failed: %s", pa_cstrerror(errno));
+ pa_log("EVIOCGID failed: %s", pa_cstrerror(errno));
goto fail;
}
- pa_log_info(__FILE__": evdev vendor 0x%04hx product 0x%04hx version 0x%04hx bustype %u",
+ pa_log_info("evdev vendor 0x%04hx product 0x%04hx version 0x%04hx bustype %u",
input_id.vendor, input_id.product, input_id.version, input_id.bustype);
memset(name, 0, sizeof(name));
- if(ioctl(u->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
- pa_log(__FILE__": EVIOCGNAME failed: %s", pa_cstrerror(errno));
+ if (ioctl(u->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
+ pa_log("EVIOCGNAME failed: %s", pa_cstrerror(errno));
goto fail;
}
- pa_log_info(__FILE__": evdev device name: %s", name);
+ pa_log_info("evdev device name: %s", name);
memset(evtype_bitmask, 0, sizeof(evtype_bitmask));
if (ioctl(u->fd, EVIOCGBIT(0, EV_MAX), evtype_bitmask) < 0) {
- pa_log(__FILE__": EVIOCGBIT failed: %s", pa_cstrerror(errno));
+ pa_log("EVIOCGBIT failed: %s", pa_cstrerror(errno));
goto fail;
}
if (!test_bit(EV_KEY, evtype_bitmask)) {
- pa_log(__FILE__": device has no keys.");
+ pa_log("Device has no keys.");
goto fail;
}
- u->io = c->mainloop->io_new(c->mainloop, u->fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
+ u->io = m->core->mainloop->io_new(m->core->mainloop, u->fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
pa_modargs_free(ma);
-
+
return 0;
fail:
@@ -234,14 +241,14 @@ fail:
if (ma)
pa_modargs_free(ma);
- pa__done(c, m);
+ pa__done(m);
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata *u;
- assert(c);
- assert(m);
+
+ pa_assert(m);
if (!(u = m->userdata))
return;
@@ -250,7 +257,7 @@ void pa__done(pa_core *c, pa_module*m) {
m->core->mainloop->io_free(u->io);
if (u->fd >= 0)
- close(u->fd);
+ pa_assert_se(pa_close(u->fd) == 0);
pa_xfree(u->sink_name);
pa_xfree(u);
diff --git a/src/modules/module-native-protocol-fd.c b/src/modules/module-native-protocol-fd.c
index 9e1cac09..eed0505b 100644
--- a/src/modules/module-native-protocol-fd.c
+++ b/src/modules/module-native-protocol-fd.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,10 +24,10 @@
#endif
#include <stdio.h>
-#include <assert.h>
#include <unistd.h>
#include <pulsecore/module.h>
+#include <pulsecore/macro.h>
#include <pulsecore/iochannel.h>
#include <pulsecore/modargs.h>
#include <pulsecore/protocol-native.h>
@@ -35,51 +35,62 @@
#include "module-native-protocol-fd-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Native protocol autospawn helper")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Native protocol autospawn helper");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
static const char* const valid_modargs[] = {
"fd",
- "public",
- "cookie",
NULL,
};
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
pa_iochannel *io;
pa_modargs *ma;
- int fd, r = -1;
- assert(c && m);
+ int32_t fd;
+ int r = -1;
+ pa_native_options *options = NULL;
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
+ pa_log("Failed to parse module arguments.");
goto finish;
}
if (pa_modargs_get_value_s32(ma, "fd", &fd) < 0) {
- pa_log(__FILE__": invalid file descriptor.");
+ pa_log("Invalid file descriptor.");
goto finish;
}
-
- io = pa_iochannel_new(c->mainloop, fd, fd);
- if (!(m->userdata = pa_protocol_native_new_iochannel(c, io, m, ma))) {
- pa_iochannel_free(io);
- goto finish;
- }
+ m->userdata = pa_native_protocol_get(m->core);
+
+ io = pa_iochannel_new(m->core->mainloop, fd, fd);
+
+ options = pa_native_options_new();
+ options->module = m;
+ options->auth_anonymous = TRUE;
+
+ pa_native_protocol_connect(m->userdata, io, options);
r = 0;
finish:
if (ma)
pa_modargs_free(ma);
-
+
+ if (options)
+ pa_native_options_unref(options);
+
return r;
}
-void pa__done(pa_core *c, pa_module*m) {
- assert(c && m);
+void pa__done(pa_module*m) {
+ pa_assert(m);
- pa_protocol_native_free(m->userdata);
+ if (m->userdata) {
+ pa_native_protocol_disconnect(m->userdata, m);
+ pa_native_protocol_unref(m->userdata);
+ }
}
diff --git a/src/modules/module-null-sink.c b/src/modules/module-null-sink.c
index 470be6bc..bedd6efc 100644
--- a/src/modules/module-null-sink.c
+++ b/src/modules/module-null-sink.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2008 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,156 +24,344 @@
#endif
#include <stdlib.h>
-#include <sys/stat.h>
#include <stdio.h>
-#include <assert.h>
#include <errno.h>
-#include <string.h>
-#include <fcntl.h>
#include <unistd.h>
-#include <limits.h>
+#include <pulse/rtclock.h>
#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
-#include <pulsecore/iochannel.h>
+#include <pulsecore/macro.h>
#include <pulsecore/sink.h>
#include <pulsecore/module.h>
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
#include "module-null-sink-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Clocked NULL sink")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION(_("Clocked NULL sink"));
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
PA_MODULE_USAGE(
+ "sink_name=<name of sink> "
+ "sink_properties=<properties for the sink> "
"format=<sample format> "
- "channels=<number of channels> "
"rate=<sample rate> "
- "sink_name=<name of sink>"
- "channel_map=<channel map>")
+ "channels=<number of channels> "
+ "channel_map=<channel map>");
#define DEFAULT_SINK_NAME "null"
+#define BLOCK_USEC (PA_USEC_PER_SEC * 2)
struct userdata {
pa_core *core;
pa_module *module;
pa_sink *sink;
- pa_time_event *time_event;
- size_t block_size;
- uint64_t n_bytes;
- struct timeval start_time;
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+
+ pa_usec_t block_usec;
+ pa_usec_t timestamp;
};
static const char* const valid_modargs[] = {
- "rate",
+ "sink_name",
+ "sink_properties",
"format",
+ "rate",
"channels",
- "sink_name",
- "channel_map",
+ "channel_map",
NULL
};
-static void time_callback(pa_mainloop_api *m, pa_time_event*e, const struct timeval *tv, void *userdata) {
- struct userdata *u = userdata;
- pa_memchunk chunk;
- struct timeval ntv = *tv;
- size_t l;
+static int sink_process_msg(
+ pa_msgobject *o,
+ int code,
+ void *data,
+ int64_t offset,
+ pa_memchunk *chunk) {
+
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+ case PA_SINK_MESSAGE_SET_STATE:
+
+ if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING)
+ u->timestamp = pa_rtclock_now();
+
+ break;
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ pa_usec_t now;
+
+ now = pa_rtclock_now();
+ *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0ULL;
+
+ return 0;
+ }
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+static void sink_update_requested_latency_cb(pa_sink *s) {
+ struct userdata *u;
+ size_t nbytes;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ u->block_usec = pa_sink_get_requested_latency_within_thread(s);
+
+ if (u->block_usec == (pa_usec_t) -1)
+ u->block_usec = s->thread_info.max_latency;
+
+ nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
+ pa_sink_set_max_rewind_within_thread(s, nbytes);
+ pa_sink_set_max_request_within_thread(s, nbytes);
+}
+
+static void process_rewind(struct userdata *u, pa_usec_t now) {
+ size_t rewind_nbytes, in_buffer;
+ pa_usec_t delay;
+
+ pa_assert(u);
+
+ /* Figure out how much we shall rewind and reset the counter */
+ rewind_nbytes = u->sink->thread_info.rewind_nbytes;
+ u->sink->thread_info.rewind_nbytes = 0;
+
+ pa_assert(rewind_nbytes > 0);
+ pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
+
+ if (u->timestamp <= now)
+ goto do_nothing;
+
+ delay = u->timestamp - now;
+ in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
+
+ if (in_buffer <= 0)
+ goto do_nothing;
+
+ if (rewind_nbytes > in_buffer)
+ rewind_nbytes = in_buffer;
+
+ pa_sink_process_rewind(u->sink, rewind_nbytes);
+ u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
+
+ pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
+ return;
+
+do_nothing:
+
+ pa_sink_process_rewind(u->sink, 0);
+}
+
+static void process_render(struct userdata *u, pa_usec_t now) {
+ size_t ate = 0;
+
+ pa_assert(u);
+
+ /* This is the configured latency. Sink inputs connected to us
+ might not have a single frame more than the maxrequest value
+ queed. Hence: at maximum read this many bytes from the sink
+ inputs. */
- assert(u);
+ /* Fill the buffer up the the latency size */
+ while (u->timestamp < now + u->block_usec) {
+ pa_memchunk chunk;
- if (pa_sink_render(u->sink, u->block_size, &chunk) >= 0) {
- l = chunk.length;
+ pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
pa_memblock_unref(chunk.memblock);
- } else
- l = u->block_size;
- pa_timeval_add(&ntv, pa_bytes_to_usec(l, &u->sink->sample_spec));
- m->time_restart(e, &ntv);
+/* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
+ u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
- u->n_bytes += l;
+ ate += chunk.length;
+
+ if (ate >= u->sink->thread_info.max_request)
+ break;
+ }
+
+/* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
}
-static pa_usec_t get_latency(pa_sink *s) {
- struct userdata *u = s->userdata;
- pa_usec_t a, b;
- struct timeval now;
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ u->timestamp = pa_rtclock_now();
+
+ for (;;) {
+ int ret;
+
+ /* Render some data and drop it immediately */
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+ pa_usec_t now;
+
+ now = pa_rtclock_now();
+
+ if (u->sink->thread_info.rewind_requested) {
+ if (u->sink->thread_info.rewind_nbytes > 0)
+ process_rewind(u, now);
+ else
+ pa_sink_process_rewind(u->sink, 0);
+ }
- a = pa_timeval_diff(pa_gettimeofday(&now), &u->start_time);
- b = pa_bytes_to_usec(u->n_bytes, &s->sample_spec);
+ if (u->timestamp <= now)
+ process_render(u, now);
- return b > a ? b - a : 0;
+ pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
+ } else
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
+
+ /* Hmm, nothing to do. Let's sleep */
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
}
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
struct userdata *u = NULL;
pa_sample_spec ss;
pa_channel_map map;
pa_modargs *ma = NULL;
-
- assert(c);
- assert(m);
-
+ pa_sink_new_data data;
+ size_t nbytes;
+
+ pa_assert(m);
+
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
+ pa_log("Failed to parse module arguments.");
goto fail;
}
- ss = c->default_sample_spec;
+ ss = m->core->default_sample_spec;
+ map = m->core->default_channel_map;
if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
- pa_log(__FILE__": invalid sample format specification or channel map.");
+ pa_log("Invalid sample format specification or channel map");
goto fail;
}
-
- u = pa_xnew0(struct userdata, 1);
- u->core = c;
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
u->module = m;
- m->userdata = u;
-
- if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": failed to create sink.");
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Null Output"));
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&data);
goto fail;
}
- u->sink->get_latency = get_latency;
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink object.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg;
+ u->sink->update_requested_latency = sink_update_requested_latency_cb;
u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_xstrdup("NULL sink");
- u->n_bytes = 0;
- pa_gettimeofday(&u->start_time);
-
- u->time_event = c->mainloop->time_new(c->mainloop, &u->start_time, time_callback, u);
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+
+ u->block_usec = BLOCK_USEC;
+ nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
+ pa_sink_set_max_rewind(u->sink, nbytes);
+ pa_sink_set_max_request(u->sink, nbytes);
+
+ if (!(u->thread = pa_thread_new("null-sink", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ pa_sink_put(u->sink);
- u->block_size = pa_bytes_per_second(&ss) / 10;
-
pa_modargs_free(ma);
-
+
return 0;
fail:
if (ma)
pa_modargs_free(ma);
-
- pa__done(c, m);
+
+ pa__done(m);
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+int pa__get_n_used(pa_module *m) {
struct userdata *u;
- assert(c && m);
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
if (!(u = m->userdata))
return;
-
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- u->core->mainloop->time_free(u->time_event);
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
pa_xfree(u);
}
diff --git a/src/modules/module-null-source.c b/src/modules/module-null-source.c
new file mode 100644
index 00000000..b2981c34
--- /dev/null
+++ b/src/modules/module-null-source.c
@@ -0,0 +1,288 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2008 Lennart Poettering
+ Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/module.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/source.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/thread.h>
+
+#include "module-null-source-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering & Marc-Andre Lureau");
+PA_MODULE_DESCRIPTION("Clocked NULL source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "format=<sample format> "
+ "channels=<number of channels> "
+ "rate=<sample rate> "
+ "source_name=<name of source> "
+ "channel_map=<channel map> "
+ "description=<description for the source> "
+ "latency_time=<latency time in ms>");
+
+#define DEFAULT_SOURCE_NAME "source.null"
+#define DEFAULT_LATENCY_TIME 20
+#define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2)
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_source *source;
+
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+
+ size_t block_size;
+
+ pa_usec_t block_usec;
+ pa_usec_t timestamp;
+ pa_usec_t latency_time;
+};
+
+static const char* const valid_modargs[] = {
+ "rate",
+ "format",
+ "channels",
+ "source_name",
+ "channel_map",
+ "description",
+ "latency_time",
+ NULL
+};
+
+static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE(o)->userdata;
+
+ switch (code) {
+ case PA_SOURCE_MESSAGE_SET_STATE:
+
+ if (PA_PTR_TO_UINT(data) == PA_SOURCE_RUNNING)
+ u->timestamp = pa_rtclock_now();
+
+ break;
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ pa_usec_t now;
+
+ now = pa_rtclock_now();
+ *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0;
+
+ return 0;
+ }
+ }
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
+}
+
+static void source_update_requested_latency_cb(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ u = s->userdata;
+ pa_assert(u);
+
+ u->block_usec = pa_source_get_requested_latency_within_thread(s);
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ u->timestamp = pa_rtclock_now();
+
+ for (;;) {
+ int ret;
+
+ /* Generate some null data */
+ if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
+ pa_usec_t now;
+ pa_memchunk chunk;
+
+ now = pa_rtclock_now();
+
+ if ((chunk.length = pa_usec_to_bytes(now - u->timestamp, &u->source->sample_spec)) > 0) {
+
+ chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1); /* or chunk.length? */
+ chunk.index = 0;
+ pa_source_post(u->source, &chunk);
+ pa_memblock_unref(chunk.memblock);
+
+ u->timestamp = now;
+ }
+
+ pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp + u->latency_time * PA_USEC_PER_MSEC);
+ } else
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
+
+ /* Hmm, nothing to do. Let's sleep */
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u = NULL;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_modargs *ma = NULL;
+ pa_source_new_data data;
+ uint32_t latency_time = DEFAULT_LATENCY_TIME;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ ss = m->core->default_sample_spec;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+ pa_log("Invalid sample format specification or channel map");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "Null Input"));
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
+
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY);
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
+ pa_log("Failed to create source object.");
+ goto fail;
+ }
+
+ u->latency_time = DEFAULT_LATENCY_TIME;
+ if (pa_modargs_get_value_u32(ma, "latency_time", &latency_time) < 0) {
+ pa_log("Failed to parse latency_time value.");
+ goto fail;
+ }
+ u->latency_time = latency_time;
+
+ u->source->parent.process_msg = source_process_msg;
+ u->source->update_requested_latency = source_update_requested_latency_cb;
+ u->source->userdata = u;
+
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+
+ pa_source_set_latency_range(u->source, 0, MAX_LATENCY_USEC);
+ u->block_usec = u->source->thread_info.max_latency;
+
+ u->source->thread_info.max_rewind =
+ pa_usec_to_bytes(u->block_usec, &u->source->sample_spec);
+
+ if (!(u->thread = pa_thread_new("null-source", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ pa_source_put(u->source);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->source)
+ pa_source_unlink(u->source);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->source)
+ pa_source_unref(u->source);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-oss-mmap.c b/src/modules/module-oss-mmap.c
deleted file mode 100644
index d6f37633..00000000
--- a/src/modules/module-oss-mmap.c
+++ /dev/null
@@ -1,579 +0,0 @@
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <sys/soundcard.h>
-#include <sys/ioctl.h>
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <assert.h>
-#include <errno.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <limits.h>
-#include <sys/mman.h>
-
-#include <pulse/xmalloc.h>
-
-#include <pulsecore/core-error.h>
-#include <pulsecore/iochannel.h>
-#include <pulsecore/sink.h>
-#include <pulsecore/source.h>
-#include <pulsecore/module.h>
-#include <pulsecore/sample-util.h>
-#include <pulsecore/core-util.h>
-#include <pulsecore/modargs.h>
-#include <pulsecore/log.h>
-
-#include "oss-util.h"
-#include "module-oss-mmap-symdef.h"
-
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE(
- "sink_name=<name for the sink> "
- "source_name=<name for the source> "
- "device=<OSS device> "
- "record=<enable source?> "
- "playback=<enable sink?> "
- "format=<sample format> "
- "channels=<number of channels> "
- "rate=<sample rate> "
- "fragments=<number of fragments> "
- "fragment_size=<fragment size> "
- "channel_map=<channel map>")
-
-struct userdata {
- pa_sink *sink;
- pa_source *source;
- pa_core *core;
- pa_sample_spec sample_spec;
-
- size_t in_fragment_size, out_fragment_size;
- unsigned in_fragments, out_fragments;
- unsigned out_blocks_saved, in_blocks_saved;
-
- int fd;
-
- void *in_mmap, *out_mmap;
- size_t in_mmap_length, out_mmap_length;
-
- pa_io_event *io_event;
-
- pa_memblock **in_memblocks, **out_memblocks;
- unsigned out_current, in_current;
- pa_module *module;
-};
-
-static const char* const valid_modargs[] = {
- "sink_name",
- "source_name",
- "device",
- "record",
- "playback",
- "fragments",
- "fragment_size",
- "format",
- "rate",
- "channels",
- "channel_map",
- NULL
-};
-
-#define DEFAULT_SINK_NAME "oss_output"
-#define DEFAULT_SOURCE_NAME "oss_input"
-#define DEFAULT_DEVICE "/dev/dsp"
-#define DEFAULT_NFRAGS 12
-#define DEFAULT_FRAGSIZE 1024
-
-static void update_usage(struct userdata *u) {
- pa_module_set_used(u->module,
- (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
- (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
- (u->source ? pa_idxset_size(u->source->outputs) : 0));
-}
-
-static void out_fill_memblocks(struct userdata *u, unsigned n) {
- assert(u && u->out_memblocks);
-
- while (n > 0) {
- pa_memchunk chunk;
-
- if (u->out_memblocks[u->out_current])
- pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
-
- chunk.memblock = u->out_memblocks[u->out_current] =
- pa_memblock_new_fixed(
- (uint8_t*) u->out_mmap+u->out_fragment_size*u->out_current,
- u->out_fragment_size,
- 1,
- u->core->memblock_stat);
- assert(chunk.memblock);
- chunk.length = chunk.memblock->length;
- chunk.index = 0;
-
- pa_sink_render_into_full(u->sink, &chunk);
-
- u->out_current++;
- while (u->out_current >= u->out_fragments)
- u->out_current -= u->out_fragments;
-
- n--;
- }
-}
-
-static void do_write(struct userdata *u) {
- struct count_info info;
- assert(u && u->sink);
-
- update_usage(u);
-
- if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
- return;
- }
-
- info.blocks += u->out_blocks_saved;
- u->out_blocks_saved = 0;
-
- if (!info.blocks)
- return;
-
- out_fill_memblocks(u, info.blocks);
-}
-
-static void in_post_memblocks(struct userdata *u, unsigned n) {
- assert(u && u->in_memblocks);
-
- while (n > 0) {
- pa_memchunk chunk;
-
- if (!u->in_memblocks[u->in_current]) {
- chunk.memblock = u->in_memblocks[u->in_current] = pa_memblock_new_fixed((uint8_t*) u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size, 1, u->core->memblock_stat);
- chunk.length = chunk.memblock->length;
- chunk.index = 0;
-
- pa_source_post(u->source, &chunk);
- }
-
- u->in_current++;
- while (u->in_current >= u->in_fragments)
- u->in_current -= u->in_fragments;
-
- n--;
- }
-}
-
-static void in_clear_memblocks(struct userdata*u, unsigned n) {
- unsigned i = u->in_current;
- assert(u && u->in_memblocks);
-
- if (n > u->in_fragments)
- n = u->in_fragments;
-
- while (n > 0) {
- if (u->in_memblocks[i]) {
- pa_memblock_unref_fixed(u->in_memblocks[i]);
- u->in_memblocks[i] = NULL;
- }
-
- i++;
- while (i >= u->in_fragments)
- i -= u->in_fragments;
-
- n--;
- }
-}
-
-static void do_read(struct userdata *u) {
- struct count_info info;
- assert(u && u->source);
-
- update_usage(u);
-
- if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
- return;
- }
-
- info.blocks += u->in_blocks_saved;
- u->in_blocks_saved = 0;
-
- if (!info.blocks)
- return;
-
- in_post_memblocks(u, info.blocks);
- in_clear_memblocks(u, u->in_fragments/2);
-}
-
-static void io_callback(pa_mainloop_api *m, pa_io_event *e, PA_GCC_UNUSED int fd, pa_io_event_flags_t f, void *userdata) {
- struct userdata *u = userdata;
- assert (u && u->core->mainloop == m && u->io_event == e);
-
- if (f & PA_IO_EVENT_INPUT)
- do_read(u);
- if (f & PA_IO_EVENT_OUTPUT)
- do_write(u);
-}
-
-static pa_usec_t sink_get_latency_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- struct count_info info;
- size_t bpos, n, total;
- assert(s && u);
-
- if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
- return 0;
- }
-
- u->out_blocks_saved += info.blocks;
-
- total = u->out_fragments * u->out_fragment_size;
- bpos = ((u->out_current + u->out_blocks_saved) * u->out_fragment_size) % total;
-
- if (bpos <= (size_t) info.ptr)
- n = total - (info.ptr - bpos);
- else
- n = bpos - info.ptr;
-
-/* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->out_fragment_size, u->out_fragments); */
-
- return pa_bytes_to_usec(n, &s->sample_spec);
-}
-
-static pa_usec_t source_get_latency_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- struct count_info info;
- size_t bpos, n, total;
- assert(s && u);
-
- if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
- return 0;
- }
-
- u->in_blocks_saved += info.blocks;
-
- total = u->in_fragments * u->in_fragment_size;
- bpos = ((u->in_current + u->in_blocks_saved) * u->in_fragment_size) % total;
-
- if (bpos <= (size_t) info.ptr)
- n = info.ptr - bpos;
- else
- n = (u->in_fragments * u->in_fragment_size) - bpos + info.ptr;
-
-/* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->in_fragment_size, u->in_fragments); */
-
- return pa_bytes_to_usec(n, &s->sample_spec);
-}
-
-static int sink_get_hw_volume(pa_sink *s) {
- struct userdata *u = s->userdata;
-
- if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
- pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
- s->get_hw_volume = NULL;
- return -1;
- }
-
- return 0;
-}
-
-static int sink_set_hw_volume(pa_sink *s) {
- struct userdata *u = s->userdata;
-
- if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
- pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
- s->set_hw_volume = NULL;
- return -1;
- }
-
- return 0;
-}
-
-static int source_get_hw_volume(pa_source *s) {
- struct userdata *u = s->userdata;
-
- if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
- pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
- s->get_hw_volume = NULL;
- return -1;
- }
-
- return 0;
-}
-
-static int source_set_hw_volume(pa_source *s) {
- struct userdata *u = s->userdata;
-
- if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
- pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
- s->set_hw_volume = NULL;
- return -1;
- }
-
- return 0;
-}
-
-int pa__init(pa_core *c, pa_module*m) {
- struct audio_buf_info info;
- struct userdata *u = NULL;
- const char *p;
- int nfrags, frag_size;
- int mode, caps;
- int enable_bits = 0, zero = 0;
- int playback = 1, record = 1;
- pa_modargs *ma = NULL;
- char hwdesc[64];
- pa_channel_map map;
-
- assert(c);
- assert(m);
-
- m->userdata = u = pa_xnew0(struct userdata, 1);
- u->module = m;
- u->fd = -1;
- u->core = c;
-
- if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
- goto fail;
- }
-
- if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
- pa_log(__FILE__": record= and playback= expect numeric arguments.");
- goto fail;
- }
-
- if (!playback && !record) {
- pa_log(__FILE__": neither playback nor record enabled for device.");
- goto fail;
- }
-
- mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
-
- nfrags = DEFAULT_NFRAGS;
- frag_size = DEFAULT_FRAGSIZE;
- if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
- pa_log(__FILE__": failed to parse fragments arguments");
- goto fail;
- }
-
- u->sample_spec = c->default_sample_spec;
- if (pa_modargs_get_sample_spec_and_channel_map(ma, &u->sample_spec, &map, PA_CHANNEL_MAP_OSS) < 0) {
- pa_log(__FILE__": failed to parse sample specification or channel map");
- goto fail;
- }
-
- if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
- goto fail;
-
- if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_REALTIME) || !(caps & DSP_CAP_TRIGGER)) {
- pa_log(__FILE__": OSS device not mmap capable.");
- goto fail;
- }
-
- pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
-
- if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
- pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
- else
- hwdesc[0] = 0;
-
- if (nfrags >= 2 && frag_size >= 1)
- if (pa_oss_set_fragments(u->fd, nfrags, frag_size) < 0)
- goto fail;
-
- if (pa_oss_auto_format(u->fd, &u->sample_spec) < 0)
- goto fail;
-
- if (mode != O_WRONLY) {
- if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
- goto fail;
- }
-
- pa_log_info(__FILE__": input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
- u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);
-
- if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
- if (mode == O_RDWR) {
- pa_log(__FILE__": mmap failed for input. Changing to O_WRONLY mode.");
- mode = O_WRONLY;
- } else {
- pa_log(__FILE__": mmap(): %s", pa_cstrerror(errno));
- goto fail;
- }
- } else {
-
- if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &u->sample_spec, &map)))
- goto fail;
-
- u->source->userdata = u;
- u->source->get_latency = source_get_latency_cb;
- u->source->get_hw_volume = source_get_hw_volume;
- u->source->set_hw_volume = source_set_hw_volume;
- pa_source_set_owner(u->source, m);
- u->source->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
- p,
- hwdesc[0] ? " (" : "",
- hwdesc[0] ? hwdesc : "",
- hwdesc[0] ? ")" : "");
- u->source->is_hardware = 1;
-
- u->in_memblocks = pa_xnew0(pa_memblock*, u->in_fragments);
-
- enable_bits |= PCM_ENABLE_INPUT;
- }
- }
-
- if (mode != O_RDONLY) {
- if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
- goto fail;
- }
-
- pa_log_info(__FILE__": output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
- u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);
-
- if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
- if (mode == O_RDWR) {
- pa_log(__FILE__": mmap filed for output. Changing to O_RDONLY mode.");
- mode = O_RDONLY;
- } else {
- pa_log(__FILE__": mmap(): %s", pa_cstrerror(errno));
- goto fail;
- }
- } else {
- pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
-
- if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &u->sample_spec, &map)))
- goto fail;
-
- u->sink->get_latency = sink_get_latency_cb;
- u->sink->get_hw_volume = sink_get_hw_volume;
- u->sink->set_hw_volume = sink_set_hw_volume;
- u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
- p,
- hwdesc[0] ? " (" : "",
- hwdesc[0] ? hwdesc : "",
- hwdesc[0] ? ")" : "");
-
- u->sink->is_hardware = 1;
- u->out_memblocks = pa_xmalloc0(sizeof(struct memblock *)*u->out_fragments);
-
- enable_bits |= PCM_ENABLE_OUTPUT;
- }
- }
-
- zero = 0;
- if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
- goto fail;
- }
-
- if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
- goto fail;
- }
-
- assert(u->source || u->sink);
-
- u->io_event = c->mainloop->io_new(c->mainloop, u->fd, (u->source ? PA_IO_EVENT_INPUT : 0) | (u->sink ? PA_IO_EVENT_OUTPUT : 0), io_callback, u);
- assert(u->io_event);
-
- pa_modargs_free(ma);
-
- /* Read mixer settings */
- if (u->source)
- source_get_hw_volume(u->source);
- if (u->sink)
- sink_get_hw_volume(u->sink);
-
- return 0;
-
-fail:
- pa__done(c, m);
-
- if (ma)
- pa_modargs_free(ma);
-
- return -1;
-}
-
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata *u;
-
- assert(c);
- assert(m);
-
- if (!(u = m->userdata))
- return;
-
- if (u->out_memblocks) {
- unsigned i;
- for (i = 0; i < u->out_fragments; i++)
- if (u->out_memblocks[i])
- pa_memblock_unref_fixed(u->out_memblocks[i]);
- pa_xfree(u->out_memblocks);
- }
-
- if (u->in_memblocks) {
- unsigned i;
- for (i = 0; i < u->in_fragments; i++)
- if (u->in_memblocks[i])
- pa_memblock_unref_fixed(u->in_memblocks[i]);
- pa_xfree(u->in_memblocks);
- }
-
- if (u->in_mmap && u->in_mmap != MAP_FAILED)
- munmap(u->in_mmap, u->in_mmap_length);
-
- if (u->out_mmap && u->out_mmap != MAP_FAILED)
- munmap(u->out_mmap, u->out_mmap_length);
-
- if (u->sink) {
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- }
-
- if (u->source) {
- pa_source_disconnect(u->source);
- pa_source_unref(u->source);
- }
-
- if (u->io_event)
- u->core->mainloop->io_free(u->io_event);
-
- if (u->fd >= 0)
- close(u->fd);
-
- pa_xfree(u);
-}
diff --git a/src/modules/module-oss.c b/src/modules/module-oss.c
deleted file mode 100644
index cde7f311..00000000
--- a/src/modules/module-oss.c
+++ /dev/null
@@ -1,520 +0,0 @@
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <sys/soundcard.h>
-#include <sys/ioctl.h>
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <assert.h>
-#include <errno.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <limits.h>
-
-#include <pulse/xmalloc.h>
-
-#include <pulsecore/core-error.h>
-#include <pulsecore/iochannel.h>
-#include <pulsecore/sink.h>
-#include <pulsecore/source.h>
-#include <pulsecore/module.h>
-#include <pulsecore/sample-util.h>
-#include <pulsecore/core-util.h>
-#include <pulsecore/modargs.h>
-#include <pulsecore/log.h>
-
-#include "oss-util.h"
-#include "module-oss-symdef.h"
-
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("OSS Sink/Source")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE(
- "sink_name=<name for the sink> "
- "source_name=<name for the source> "
- "device=<OSS device> "
- "record=<enable source?> "
- "playback=<enable sink?> "
- "format=<sample format> "
- "channels=<number of channels> "
- "rate=<sample rate> "
- "fragments=<number of fragments> "
- "fragment_size=<fragment size> "
- "channel_map=<channel map>")
-
-struct userdata {
- pa_sink *sink;
- pa_source *source;
- pa_iochannel *io;
- pa_core *core;
-
- pa_memchunk memchunk, silence;
-
- uint32_t in_fragment_size, out_fragment_size, sample_size;
- int use_getospace, use_getispace;
-
- int fd;
- pa_module *module;
-};
-
-static const char* const valid_modargs[] = {
- "sink_name",
- "source_name",
- "device",
- "record",
- "playback",
- "fragments",
- "fragment_size",
- "format",
- "rate",
- "channels",
- "channel_map",
- NULL
-};
-
-#define DEFAULT_SINK_NAME "oss_output"
-#define DEFAULT_SOURCE_NAME "oss_input"
-#define DEFAULT_DEVICE "/dev/dsp"
-
-static void update_usage(struct userdata *u) {
- pa_module_set_used(u->module,
- (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
- (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
- (u->source ? pa_idxset_size(u->source->outputs) : 0));
-}
-
-static void do_write(struct userdata *u) {
- pa_memchunk *memchunk;
- ssize_t r;
- size_t l;
- int loop = 0;
-
- assert(u);
-
- if (!u->sink || !pa_iochannel_is_writable(u->io))
- return;
-
- update_usage(u);
-
- l = u->out_fragment_size;
-
- if (u->use_getospace) {
- audio_buf_info info;
-
- if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
- u->use_getospace = 0;
- else {
- if (info.bytes/l > 0) {
- l = (info.bytes/l)*l;
- loop = 1;
- }
- }
- }
-
- do {
- memchunk = &u->memchunk;
-
- if (!memchunk->length)
- if (pa_sink_render(u->sink, l, memchunk) < 0)
- memchunk = &u->silence;
-
- assert(memchunk->memblock);
- assert(memchunk->memblock->data);
- assert(memchunk->length);
-
- if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) {
- pa_log(__FILE__": write() failed: %s", pa_cstrerror(errno));
- break;
- }
-
- if (memchunk == &u->silence)
- assert(r % u->sample_size == 0);
- else {
- u->memchunk.index += r;
- u->memchunk.length -= r;
-
- if (u->memchunk.length <= 0) {
- pa_memblock_unref(u->memchunk.memblock);
- u->memchunk.memblock = NULL;
- }
- }
-
- l = l > (size_t) r ? l - r : 0;
- } while (loop && l > 0);
-}
-
-static void do_read(struct userdata *u) {
- pa_memchunk memchunk;
- ssize_t r;
- size_t l;
- int loop = 0;
- assert(u);
-
- if (!u->source || !pa_iochannel_is_readable(u->io) || !pa_idxset_size(u->source->outputs))
- return;
-
- update_usage(u);
-
- l = u->in_fragment_size;
-
- if (u->use_getispace) {
- audio_buf_info info;
-
- if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0)
- u->use_getispace = 0;
- else {
- if (info.bytes/l > 0) {
- l = (info.bytes/l)*l;
- loop = 1;
- }
- }
- }
-
- do {
- memchunk.memblock = pa_memblock_new(l, u->core->memblock_stat);
- assert(memchunk.memblock);
- if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
- pa_memblock_unref(memchunk.memblock);
- if (errno != EAGAIN)
- pa_log(__FILE__": read() failed: %s", pa_cstrerror(errno));
- break;
- }
-
- assert(r <= (ssize_t) memchunk.memblock->length);
- memchunk.length = memchunk.memblock->length = r;
- memchunk.index = 0;
-
- pa_source_post(u->source, &memchunk);
- pa_memblock_unref(memchunk.memblock);
-
- l = l > (size_t) r ? l - r : 0;
- } while (loop && l > 0);
-}
-
-static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
- struct userdata *u = userdata;
- assert(u);
- do_write(u);
- do_read(u);
-}
-
-static void source_notify_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- assert(u);
- do_read(u);
-}
-
-static pa_usec_t sink_get_latency_cb(pa_sink *s) {
- pa_usec_t r = 0;
- int arg;
- struct userdata *u = s->userdata;
- assert(s && u && u->sink);
-
- if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
- pa_log_info(__FILE__": device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
- s->get_latency = NULL;
- return 0;
- }
-
- r += pa_bytes_to_usec(arg, &s->sample_spec);
-
- if (u->memchunk.memblock)
- r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
-
- return r;
-}
-
-static pa_usec_t source_get_latency_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- audio_buf_info info;
- assert(s && u && u->source);
-
- if (!u->use_getispace)
- return 0;
-
- if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
- u->use_getispace = 0;
- return 0;
- }
-
- if (info.bytes <= 0)
- return 0;
-
- return pa_bytes_to_usec(info.bytes, &s->sample_spec);
-}
-
-static int sink_get_hw_volume(pa_sink *s) {
- struct userdata *u = s->userdata;
-
- if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
- pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
- s->get_hw_volume = NULL;
- return -1;
- }
-
- return 0;
-}
-
-static int sink_set_hw_volume(pa_sink *s) {
- struct userdata *u = s->userdata;
-
- if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
- pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
- s->set_hw_volume = NULL;
- return -1;
- }
-
- return 0;
-}
-
-static int source_get_hw_volume(pa_source *s) {
- struct userdata *u = s->userdata;
-
- if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
- pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
- s->get_hw_volume = NULL;
- return -1;
- }
-
- return 0;
-}
-
-static int source_set_hw_volume(pa_source *s) {
- struct userdata *u = s->userdata;
-
- if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
- pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
- s->set_hw_volume = NULL;
- return -1;
- }
-
- return 0;
-}
-
-int pa__init(pa_core *c, pa_module*m) {
- struct audio_buf_info info;
- struct userdata *u = NULL;
- const char *p;
- int fd = -1;
- int nfrags, frag_size, in_frag_size, out_frag_size;
- int mode;
- int record = 1, playback = 1;
- pa_sample_spec ss;
- pa_channel_map map;
- pa_modargs *ma = NULL;
- char hwdesc[64];
-
- assert(c);
- assert(m);
-
- if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
- goto fail;
- }
-
- if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
- pa_log(__FILE__": record= and playback= expect numeric argument.");
- goto fail;
- }
-
- if (!playback && !record) {
- pa_log(__FILE__": neither playback nor record enabled for device.");
- goto fail;
- }
-
- mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
-
- ss = c->default_sample_spec;
- if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
- pa_log(__FILE__": failed to parse sample specification or channel map");
- goto fail;
- }
-
- /* Fix latency to 100ms */
- nfrags = 12;
- frag_size = pa_bytes_per_second(&ss)/128;
-
- if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
- pa_log(__FILE__": failed to parse fragments arguments");
- goto fail;
- }
-
- if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
- goto fail;
-
- if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
- pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
- else
- hwdesc[0] = 0;
-
- pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
-
- if (nfrags >= 2 && frag_size >= 1)
- if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
- goto fail;
-
- if (pa_oss_auto_format(fd, &ss) < 0)
- goto fail;
-
- if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
- goto fail;
- }
- assert(frag_size);
- in_frag_size = out_frag_size = frag_size;
-
- u = pa_xmalloc(sizeof(struct userdata));
- u->core = c;
- u->use_getospace = u->use_getispace = 0;
-
- if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
- pa_log_info(__FILE__": input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
- in_frag_size = info.fragsize;
- u->use_getispace = 1;
- }
-
- if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
- pa_log_info(__FILE__": output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
- out_frag_size = info.fragsize;
- u->use_getospace = 1;
- }
-
- if (mode != O_WRONLY) {
- if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map)))
- goto fail;
-
- u->source->userdata = u;
- u->source->notify = source_notify_cb;
- u->source->get_latency = source_get_latency_cb;
- u->source->get_hw_volume = source_get_hw_volume;
- u->source->set_hw_volume = source_set_hw_volume;
- pa_source_set_owner(u->source, m);
- u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
- p,
- hwdesc[0] ? " (" : "",
- hwdesc[0] ? hwdesc : "",
- hwdesc[0] ? ")" : "");
- u->source->is_hardware = 1;
- } else
- u->source = NULL;
-
- if (mode != O_RDONLY) {
- if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map)))
- goto fail;
-
- u->sink->get_latency = sink_get_latency_cb;
- u->sink->get_hw_volume = sink_get_hw_volume;
- u->sink->set_hw_volume = sink_set_hw_volume;
- u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
- p,
- hwdesc[0] ? " (" : "",
- hwdesc[0] ? hwdesc : "",
- hwdesc[0] ? ")" : "");
- u->sink->is_hardware = 1;
- } else
- u->sink = NULL;
-
- assert(u->source || u->sink);
-
- u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : -1);
- assert(u->io);
- pa_iochannel_set_callback(u->io, io_callback, u);
- u->fd = fd;
-
- u->memchunk.memblock = NULL;
- u->memchunk.length = 0;
- u->sample_size = pa_frame_size(&ss);
-
- u->out_fragment_size = out_frag_size;
- u->in_fragment_size = in_frag_size;
- u->silence.memblock = pa_memblock_new(u->silence.length = u->out_fragment_size, u->core->memblock_stat);
- assert(u->silence.memblock);
- pa_silence_memblock(u->silence.memblock, &ss);
- u->silence.index = 0;
-
- u->module = m;
- m->userdata = u;
-
- pa_modargs_free(ma);
-
- /*
- * Some crappy drivers do not start the recording until we read something.
- * Without this snippet, poll will never register the fd as ready.
- */
- if (u->source) {
- char *buf = pa_xnew(char, u->sample_size);
- pa_read(u->fd, buf, u->sample_size, NULL);
- pa_xfree(buf);
- }
-
- /* Read mixer settings */
- if (u->source)
- source_get_hw_volume(u->source);
- if (u->sink)
- sink_get_hw_volume(u->sink);
-
- return 0;
-
-fail:
- if (fd >= 0)
- close(fd);
-
- if (ma)
- pa_modargs_free(ma);
-
- return -1;
-}
-
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata *u;
-
- assert(c);
- assert(m);
-
- if (!(u = m->userdata))
- return;
-
- if (u->memchunk.memblock)
- pa_memblock_unref(u->memchunk.memblock);
- if (u->silence.memblock)
- pa_memblock_unref(u->silence.memblock);
-
- if (u->sink) {
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- }
-
- if (u->source) {
- pa_source_disconnect(u->source);
- pa_source_unref(u->source);
- }
-
- pa_iochannel_free(u->io);
- pa_xfree(u);
-}
diff --git a/src/modules/module-pipe-sink.c b/src/modules/module-pipe-sink.c
index 4878a1a1..91e01f99 100644
--- a/src/modules/module-pipe-sink.c
+++ b/src/modules/module-pipe-sink.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -26,222 +26,355 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
-#include <assert.h>
#include <errno.h>
-#include <string.h>
#include <fcntl.h>
#include <unistd.h>
-#include <limits.h>
+#include <sys/ioctl.h>
+
+#ifdef HAVE_SYS_FILIO_H
+#include <sys/filio.h>
+#endif
#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h>
-#include <pulsecore/iochannel.h>
#include <pulsecore/sink.h>
#include <pulsecore/module.h>
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/poll.h>
#include "module-pipe-sink-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("UNIX pipe sink")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("UNIX pipe sink");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
PA_MODULE_USAGE(
"sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
"file=<path of the FIFO> "
"format=<sample format> "
+ "rate=<sample rate> "
"channels=<number of channels> "
- "rate=<sample rate>"
- "channel_map=<channel map>")
+ "channel_map=<channel map>");
-#define DEFAULT_FIFO_NAME "/tmp/music.output"
+#define DEFAULT_FILE_NAME "fifo_output"
#define DEFAULT_SINK_NAME "fifo_output"
struct userdata {
pa_core *core;
+ pa_module *module;
+ pa_sink *sink;
+
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
char *filename;
-
- pa_sink *sink;
- pa_iochannel *io;
- pa_defer_event *defer_event;
+ int fd;
pa_memchunk memchunk;
- pa_module *module;
+
+ pa_rtpoll_item *rtpoll_item;
+
+ int write_type;
};
static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
"file",
- "rate",
"format",
+ "rate",
"channels",
- "sink_name",
"channel_map",
NULL
};
-static void do_write(struct userdata *u) {
- ssize_t r;
- assert(u);
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
- u->core->mainloop->defer_enable(u->defer_event, 0);
-
- if (!pa_iochannel_is_writable(u->io))
- return;
+ switch (code) {
- pa_module_set_used(u->module, pa_idxset_size(u->sink->inputs) + pa_idxset_size(u->sink->monitor_source->outputs));
-
- if (!u->memchunk.length)
- if (pa_sink_render(u->sink, PIPE_BUF, &u->memchunk) < 0)
- return;
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ size_t n = 0;
- assert(u->memchunk.memblock && u->memchunk.length);
-
- if ((r = pa_iochannel_write(u->io, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length)) < 0) {
- pa_log(__FILE__": write(): %s", pa_cstrerror(errno));
- return;
- }
+#ifdef FIONREAD
+ int l;
- u->memchunk.index += r;
- u->memchunk.length -= r;
-
- if (u->memchunk.length <= 0) {
- pa_memblock_unref(u->memchunk.memblock);
- u->memchunk.memblock = NULL;
+ if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
+ n = (size_t) l;
+#endif
+
+ n += u->memchunk.length;
+
+ *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
+ return 0;
+ }
}
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
}
-static void notify_cb(pa_sink*s) {
- struct userdata *u = s->userdata;
- assert(s && u);
+static int process_render(struct userdata *u) {
+ pa_assert(u);
- if (pa_iochannel_is_writable(u->io))
- u->core->mainloop->defer_enable(u->defer_event, 1);
-}
+ if (u->memchunk.length <= 0)
+ pa_sink_render(u->sink, pa_pipe_buf(u->fd), &u->memchunk);
-static pa_usec_t get_latency_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- assert(s && u);
+ pa_assert(u->memchunk.length > 0);
- return u->memchunk.memblock ? pa_bytes_to_usec(u->memchunk.length, &s->sample_spec) : 0;
-}
+ for (;;) {
+ ssize_t l;
+ void *p;
-static void defer_callback(PA_GCC_UNUSED pa_mainloop_api *m, PA_GCC_UNUSED pa_defer_event*e, void *userdata) {
- struct userdata *u = userdata;
- assert(u);
- do_write(u);
+ p = pa_memblock_acquire(u->memchunk.memblock);
+ l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &u->write_type);
+ pa_memblock_release(u->memchunk.memblock);
+
+ pa_assert(l != 0);
+
+ if (l < 0) {
+
+ if (errno == EINTR)
+ continue;
+ else if (errno == EAGAIN)
+ return 0;
+ else {
+ pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
+ return -1;
+ }
+
+ } else {
+
+ u->memchunk.index += (size_t) l;
+ u->memchunk.length -= (size_t) l;
+
+ if (u->memchunk.length <= 0) {
+ pa_memblock_unref(u->memchunk.memblock);
+ pa_memchunk_reset(&u->memchunk);
+ }
+ }
+
+ return 0;
+ }
}
-static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
+static void thread_func(void *userdata) {
struct userdata *u = userdata;
- assert(u);
- do_write(u);
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ for (;;) {
+ struct pollfd *pollfd;
+ int ret;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ /* Render some data and write it to the fifo */
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+
+ if (u->sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(u->sink, 0);
+
+ if (pollfd->revents) {
+ if (process_render(u) < 0)
+ goto fail;
+
+ pollfd->revents = 0;
+ }
+ }
+
+ /* Hmm, nothing to do. Let's sleep */
+ pollfd->events = (short) (u->sink->thread_info.state == PA_SINK_RUNNING ? POLLOUT : 0);
+
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ if (pollfd->revents & ~POLLOUT) {
+ pa_log("FIFO shutdown.");
+ goto fail;
+ }
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
}
-int pa__init(pa_core *c, pa_module*m) {
- struct userdata *u = NULL;
+int pa__init(pa_module*m) {
+ struct userdata *u;
struct stat st;
- const char *p;
- int fd = -1;
pa_sample_spec ss;
pa_channel_map map;
- pa_modargs *ma = NULL;
- assert(c && m);
-
+ pa_modargs *ma;
+ struct pollfd *pollfd;
+ pa_sink_new_data data;
+
+ pa_assert(m);
+
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
+ pa_log("Failed to parse module arguments.");
goto fail;
}
- ss = c->default_sample_spec;
+ ss = m->core->default_sample_spec;
+ map = m->core->default_channel_map;
if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
- pa_log(__FILE__": invalid sample format specification");
+ pa_log("Invalid sample format specification or channel map");
goto fail;
}
-
- mkfifo(p = pa_modargs_get_value(ma, "file", DEFAULT_FIFO_NAME), 0777);
- if ((fd = open(p, O_RDWR)) < 0) {
- pa_log(__FILE__": open('%s'): %s", p, pa_cstrerror(errno));
+ u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ m->userdata = u;
+ pa_memchunk_reset(&u->memchunk);
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+ u->write_type = 0;
+
+ u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
+
+ mkfifo(u->filename, 0666);
+ if ((u->fd = pa_open_cloexec(u->filename, O_RDWR, 0)) < 0) {
+ pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
goto fail;
}
- pa_fd_set_cloexec(fd, 1);
-
- if (fstat(fd, &st) < 0) {
- pa_log(__FILE__": fstat('%s'): %s", p, pa_cstrerror(errno));
+ pa_make_fd_nonblock(u->fd);
+
+ if (fstat(u->fd, &st) < 0) {
+ pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
goto fail;
}
if (!S_ISFIFO(st.st_mode)) {
- pa_log(__FILE__": '%s' is not a FIFO.", p);
+ pa_log("'%s' is not a FIFO.", u->filename);
goto fail;
}
- u = pa_xmalloc0(sizeof(struct userdata));
- u->filename = pa_xstrdup(p);
- u->core = c;
- u->module = m;
- m->userdata = u;
-
- if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": failed to create sink.");
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO sink %s", u->filename);
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&data);
+ goto fail;
+ }
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
goto fail;
}
- u->sink->notify = notify_cb;
- u->sink->get_latency = get_latency_cb;
+
+ u->sink->parent.process_msg = sink_process_msg;
u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Unix FIFO sink '%s'", p);
- assert(u->sink->description);
- u->io = pa_iochannel_new(c->mainloop, -1, fd);
- assert(u->io);
- pa_iochannel_set_callback(u->io, io_callback, u);
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+ pa_sink_set_max_request(u->sink, pa_pipe_buf(u->fd));
+ pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(pa_pipe_buf(u->fd), &u->sink->sample_spec));
+
+ u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->fd = u->fd;
+ pollfd->events = pollfd->revents = 0;
- u->memchunk.memblock = NULL;
- u->memchunk.length = 0;
+ if (!(u->thread = pa_thread_new("pipe-sink", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
- u->defer_event = c->mainloop->defer_new(c->mainloop, defer_callback, u);
- assert(u->defer_event);
- c->mainloop->defer_enable(u->defer_event, 0);
+ pa_sink_put(u->sink);
pa_modargs_free(ma);
-
+
return 0;
fail:
if (ma)
pa_modargs_free(ma);
-
- if (fd >= 0)
- close(fd);
- pa__done(c, m);
+ pa__done(m);
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
struct userdata *u;
- assert(c && m);
+
+ pa_assert(m);
if (!(u = m->userdata))
return;
-
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
if (u->memchunk.memblock)
pa_memblock_unref(u->memchunk.memblock);
-
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- pa_iochannel_free(u->io);
- u->core->mainloop->defer_free(u->defer_event);
-
- assert(u->filename);
- unlink(u->filename);
- pa_xfree(u->filename);
-
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ if (u->filename) {
+ unlink(u->filename);
+ pa_xfree(u->filename);
+ }
+
+ if (u->fd >= 0)
+ pa_assert_se(pa_close(u->fd) == 0);
+
pa_xfree(u);
}
diff --git a/src/modules/module-pipe-source.c b/src/modules/module-pipe-source.c
index f2f214c7..a941f088 100644
--- a/src/modules/module-pipe-source.c
+++ b/src/modules/module-pipe-source.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -26,195 +26,339 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
-#include <assert.h>
#include <errno.h>
-#include <string.h>
#include <fcntl.h>
#include <unistd.h>
-#include <limits.h>
+#include <sys/ioctl.h>
+
+#ifdef HAVE_SYS_FILIO_H
+#include <sys/filio.h>
+#endif
#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h>
-#include <pulsecore/iochannel.h>
#include <pulsecore/source.h>
#include <pulsecore/module.h>
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/poll.h>
#include "module-pipe-source-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("UNIX pipe source")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("UNIX pipe source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
PA_MODULE_USAGE(
"source_name=<name for the source> "
+ "source_properties=<properties for the source> "
"file=<path of the FIFO> "
"format=<sample format> "
- "channels=<number of channels> "
"rate=<sample rate> "
- "channel_map=<channel map>")
+ "channels=<number of channels> "
+ "channel_map=<channel map>");
-#define DEFAULT_FIFO_NAME "/tmp/music.input"
+#define DEFAULT_FILE_NAME "/tmp/music.input"
#define DEFAULT_SOURCE_NAME "fifo_input"
struct userdata {
pa_core *core;
+ pa_module *module;
+ pa_source *source;
+
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
char *filename;
-
- pa_source *source;
- pa_iochannel *io;
- pa_module *module;
- pa_memchunk chunk;
+ int fd;
+
+ pa_memchunk memchunk;
+
+ pa_rtpoll_item *rtpoll_item;
};
static const char* const valid_modargs[] = {
+ "source_name",
+ "source_properties",
"file",
+ "format",
"rate",
"channels",
- "format",
- "source_name",
"channel_map",
NULL
};
-static void do_read(struct userdata *u) {
- ssize_t r;
- pa_memchunk chunk;
- assert(u);
+static int source_process_msg(
+ pa_msgobject *o,
+ int code,
+ void *data,
+ int64_t offset,
+ pa_memchunk *chunk) {
- if (!pa_iochannel_is_readable(u->io))
- return;
+ struct userdata *u = PA_SOURCE(o)->userdata;
- pa_module_set_used(u->module, pa_idxset_size(u->source->outputs));
+ switch (code) {
- if (!u->chunk.memblock) {
- u->chunk.memblock = pa_memblock_new(1024, u->core->memblock_stat);
- u->chunk.index = chunk.length = 0;
- }
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ size_t n = 0;
- assert(u->chunk.memblock && u->chunk.memblock->length > u->chunk.index);
- if ((r = pa_iochannel_read(u->io, (uint8_t*) u->chunk.memblock->data + u->chunk.index, u->chunk.memblock->length - u->chunk.index)) <= 0) {
- pa_log(__FILE__": read(): %s", pa_cstrerror(errno));
- return;
- }
+#ifdef FIONREAD
+ int l;
- u->chunk.length = r;
- pa_source_post(u->source, &u->chunk);
- u->chunk.index += r;
+ if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
+ n = (size_t) l;
+#endif
- if (u->chunk.index >= u->chunk.memblock->length) {
- u->chunk.index = u->chunk.length = 0;
- pa_memblock_unref(u->chunk.memblock);
- u->chunk.memblock = NULL;
+ *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
+ return 0;
+ }
}
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
}
-static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
+static void thread_func(void *userdata) {
struct userdata *u = userdata;
- assert(u);
- do_read(u);
+ int read_type = 0;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ for (;;) {
+ int ret;
+ struct pollfd *pollfd;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ /* Try to read some data and pass it on to the source driver */
+ if (u->source->thread_info.state == PA_SOURCE_RUNNING && pollfd->revents) {
+ ssize_t l;
+ void *p;
+
+ if (!u->memchunk.memblock) {
+ u->memchunk.memblock = pa_memblock_new(u->core->mempool, pa_pipe_buf(u->fd));
+ u->memchunk.index = u->memchunk.length = 0;
+ }
+
+ pa_assert(pa_memblock_get_length(u->memchunk.memblock) > u->memchunk.index);
+
+ p = pa_memblock_acquire(u->memchunk.memblock);
+ l = pa_read(u->fd, (uint8_t*) p + u->memchunk.index, pa_memblock_get_length(u->memchunk.memblock) - u->memchunk.index, &read_type);
+ pa_memblock_release(u->memchunk.memblock);
+
+ pa_assert(l != 0); /* EOF cannot happen, since we opened the fifo for both reading and writing */
+
+ if (l < 0) {
+
+ if (errno == EINTR)
+ continue;
+ else if (errno != EAGAIN) {
+ pa_log("Failed to read data from FIFO: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ } else {
+
+ u->memchunk.length = (size_t) l;
+ pa_source_post(u->source, &u->memchunk);
+ u->memchunk.index += (size_t) l;
+
+ if (u->memchunk.index >= pa_memblock_get_length(u->memchunk.memblock)) {
+ pa_memblock_unref(u->memchunk.memblock);
+ pa_memchunk_reset(&u->memchunk);
+ }
+
+ pollfd->revents = 0;
+ }
+ }
+
+ /* Hmm, nothing to do. Let's sleep */
+ pollfd->events = (short) (u->source->thread_info.state == PA_SOURCE_RUNNING ? POLLIN : 0);
+
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ if (pollfd->revents & ~POLLIN) {
+ pa_log("FIFO shutdown.");
+ goto fail;
+ }
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
}
-int pa__init(pa_core *c, pa_module*m) {
- struct userdata *u = NULL;
+int pa__init(pa_module*m) {
+ struct userdata *u;
struct stat st;
- const char *p;
- int fd = -1;
pa_sample_spec ss;
pa_channel_map map;
- pa_modargs *ma = NULL;
- assert(c && m);
-
+ pa_modargs *ma;
+ struct pollfd *pollfd;
+ pa_source_new_data data;
+
+ pa_assert(m);
+
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
+ pa_log("failed to parse module arguments.");
goto fail;
}
- ss = c->default_sample_spec;
+ ss = m->core->default_sample_spec;
+ map = m->core->default_channel_map;
if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
- pa_log(__FILE__": invalid sample format specification or channel map");
+ pa_log("invalid sample format specification or channel map");
goto fail;
}
-
- mkfifo(p = pa_modargs_get_value(ma, "file", DEFAULT_FIFO_NAME), 0777);
- if ((fd = open(p, O_RDWR)) < 0) {
- pa_log(__FILE__": open('%s'): %s", p, pa_cstrerror(errno));
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ pa_memchunk_reset(&u->memchunk);
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
+
+ mkfifo(u->filename, 0666);
+ if ((u->fd = pa_open_cloexec(u->filename, O_RDWR, 0)) < 0) {
+ pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
goto fail;
}
- pa_fd_set_cloexec(fd, 1);
-
- if (fstat(fd, &st) < 0) {
- pa_log(__FILE__": fstat('%s'): %s", p, pa_cstrerror(errno));
+ pa_make_fd_nonblock(u->fd);
+
+ if (fstat(u->fd, &st) < 0) {
+ pa_log("fstat('%s'): %s",u->filename, pa_cstrerror(errno));
goto fail;
}
if (!S_ISFIFO(st.st_mode)) {
- pa_log(__FILE__": '%s' is not a FIFO.", p);
+ pa_log("'%s' is not a FIFO.", u->filename);
goto fail;
}
- u = pa_xmalloc0(sizeof(struct userdata));
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO source %s", u->filename);
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+
+ if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&data);
+ goto fail;
+ }
- u->filename = pa_xstrdup(p);
- u->core = c;
-
- if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": failed to create source.");
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
+ pa_log("Failed to create source.");
goto fail;
}
+
+ u->source->parent.process_msg = source_process_msg;
u->source->userdata = u;
- pa_source_set_owner(u->source, m);
- u->source->description = pa_sprintf_malloc("Unix FIFO source '%s'", p);
- assert(u->source->description);
- u->io = pa_iochannel_new(c->mainloop, fd, -1);
- assert(u->io);
- pa_iochannel_set_callback(u->io, io_callback, u);
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+ pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(PIPE_BUF, &u->source->sample_spec));
- u->chunk.memblock = NULL;
- u->chunk.index = u->chunk.length = 0;
-
- u->module = m;
- m->userdata = u;
+ u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->fd = u->fd;
+ pollfd->events = pollfd->revents = 0;
+
+ if (!(u->thread = pa_thread_new("pipe-source", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ pa_source_put(u->source);
pa_modargs_free(ma);
-
+
return 0;
fail:
if (ma)
pa_modargs_free(ma);
-
- if (fd >= 0)
- close(fd);
- pa__done(c, m);
+ pa__done(m);
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+int pa__get_n_used(pa_module *m) {
struct userdata *u;
- assert(c && m);
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_source_linked_by(u->source);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
if (!(u = m->userdata))
return;
-
- if (u->chunk.memblock)
- pa_memblock_unref(u->chunk.memblock);
-
- pa_source_disconnect(u->source);
- pa_source_unref(u->source);
- pa_iochannel_free(u->io);
-
- assert(u->filename);
- unlink(u->filename);
- pa_xfree(u->filename);
-
+
+ if (u->source)
+ pa_source_unlink(u->source);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->source)
+ pa_source_unref(u->source);
+
+ if (u->memchunk.memblock)
+ pa_memblock_unref(u->memchunk.memblock);
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ if (u->filename) {
+ unlink(u->filename);
+ pa_xfree(u->filename);
+ }
+
+ if (u->fd >= 0)
+ pa_assert_se(pa_close(u->fd) == 0);
+
pa_xfree(u);
}
diff --git a/src/modules/module-position-event-sounds.c b/src/modules/module-position-event-sounds.c
new file mode 100644
index 00000000..091453a4
--- /dev/null
+++ b/src/modules/module-position-event-sounds.c
@@ -0,0 +1,179 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/volume.h>
+#include <pulse/channelmap.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/sink-input.h>
+
+#include "module-position-event-sounds-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Position event sounds between L and R depending on the position on screen of the widget triggering them.");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+static const char* const valid_modargs[] = {
+ NULL
+};
+
+struct userdata {
+ pa_hook_slot *sink_input_fixate_hook_slot;
+};
+
+static int parse_pos(const char *pos, double *f) {
+
+ if (pa_atod(pos, f) < 0) {
+ pa_log_warn("Failed to parse hpos/vpos property '%s'.", pos);
+ return -1;
+ }
+
+ if (*f < 0.0 || *f > 1.0) {
+ pa_log_debug("Property hpos/vpos out of range %0.2f", *f);
+
+ *f = PA_CLAMP(*f, 0.0, 1.0);
+ }
+
+ return 0;
+}
+
+static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *core, pa_sink_input_new_data *data, struct userdata *u) {
+ const char *hpos, *vpos, *role, *id;
+ double f;
+ char t[PA_CVOLUME_SNPRINT_MAX];
+ pa_cvolume v;
+
+ pa_assert(data);
+
+ if (!(role = pa_proplist_gets(data->proplist, PA_PROP_MEDIA_ROLE)))
+ return PA_HOOK_OK;
+
+ if (!pa_streq(role, "event"))
+ return PA_HOOK_OK;
+
+ if ((id = pa_proplist_gets(data->proplist, PA_PROP_EVENT_ID))) {
+
+ /* The test sounds should never be positioned in space, since
+ * they might be trigered themselves to configure the speakers
+ * in space, which we don't want to mess up. */
+
+ if (pa_startswith(id, "audio-channel-"))
+ return PA_HOOK_OK;
+
+ if (pa_streq(id, "audio-volume-change"))
+ return PA_HOOK_OK;
+
+ if (pa_streq(id, "audio-test-signal"))
+ return PA_HOOK_OK;
+ }
+
+ if (!(hpos = pa_proplist_gets(data->proplist, PA_PROP_EVENT_MOUSE_HPOS)))
+ hpos = pa_proplist_gets(data->proplist, PA_PROP_WINDOW_HPOS);
+
+ if (!(vpos = pa_proplist_gets(data->proplist, PA_PROP_EVENT_MOUSE_VPOS)))
+ vpos = pa_proplist_gets(data->proplist, PA_PROP_WINDOW_VPOS);
+
+ if (!hpos && !vpos)
+ return PA_HOOK_OK;
+
+ pa_cvolume_reset(&v, data->sink->sample_spec.channels);
+
+ if (hpos) {
+ if (parse_pos(hpos, &f) < 0)
+ return PA_HOOK_OK;
+
+ if (pa_channel_map_can_balance(&data->sink->channel_map)) {
+ pa_log_debug("Positioning event sound '%s' horizontally at %0.2f.", pa_strnull(pa_proplist_gets(data->proplist, PA_PROP_EVENT_ID)), f);
+ pa_cvolume_set_balance(&v, &data->sink->channel_map, f*2.0-1.0);
+ }
+ }
+
+ if (vpos) {
+ if (parse_pos(vpos, &f) < 0)
+ return PA_HOOK_OK;
+
+ if (pa_channel_map_can_fade(&data->sink->channel_map)) {
+ pa_log_debug("Positioning event sound '%s' vertically at %0.2f.", pa_strnull(pa_proplist_gets(data->proplist, PA_PROP_EVENT_ID)), f);
+ pa_cvolume_set_fade(&v, &data->sink->channel_map, f*2.0-1.0);
+ }
+ }
+
+ pa_log_debug("Final volume factor %s.", pa_cvolume_snprint(t, sizeof(t), &v));
+ pa_sink_input_new_data_apply_volume_factor_sink(data, &v);
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+ u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_fixate_hook_callback, u);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink_input_fixate_hook_slot)
+ pa_hook_slot_free(u->sink_input_fixate_hook_slot);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-protocol-stub.c b/src/modules/module-protocol-stub.c
index 36d7db89..e1bf3977 100644
--- a/src/modules/module-protocol-stub.c
+++ b/src/modules/module-protocol-stub.c
@@ -1,18 +1,19 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -23,29 +24,19 @@
#include <config.h>
#endif
-#include <string.h>
#include <errno.h>
#include <stdio.h>
-#include <assert.h>
#include <unistd.h>
-#include <limits.h>
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-#ifdef HAVE_ARPA_INET_H
-#include <arpa/inet.h>
-#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
-#include "../pulsecore/winsock.h"
-
#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h>
#include <pulsecore/module.h>
+#include <pulsecore/socket.h>
#include <pulsecore/socket-server.h>
#include <pulsecore/socket-util.h>
#include <pulsecore/core-util.h>
@@ -53,6 +44,7 @@
#include <pulsecore/log.h>
#include <pulsecore/native-common.h>
#include <pulsecore/creds.h>
+#include <pulsecore/arpa-inet.h>
#ifdef USE_TCP_SOCKETS
#define SOCKET_DESCRIPTION "(TCP sockets)"
@@ -63,19 +55,19 @@
#endif
#if defined(USE_PROTOCOL_SIMPLE)
- #include <pulsecore/protocol-simple.h>
- #define protocol_new pa_protocol_simple_new
- #define protocol_free pa_protocol_simple_free
- #define TCPWRAP_SERVICE "pulseaudio-simple"
- #define IPV4_PORT 4711
- #define UNIX_SOCKET "simple"
- #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
- #if defined(USE_TCP_SOCKETS)
- #include "module-simple-protocol-tcp-symdef.h"
- #else
- #include "module-simple-protocol-unix-symdef.h"
- #endif
- PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
+# include <pulsecore/protocol-simple.h>
+# define TCPWRAP_SERVICE "pulseaudio-simple"
+# define IPV4_PORT 4711
+# define UNIX_SOCKET "simple"
+# define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
+
+# if defined(USE_TCP_SOCKETS)
+# include "module-simple-protocol-tcp-symdef.h"
+# else
+# include "module-simple-protocol-unix-symdef.h"
+# endif
+
+ PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION);
PA_MODULE_USAGE("rate=<sample rate> "
"format=<sample format> "
"channels=<number of channels> "
@@ -83,90 +75,103 @@
"source=<source to connect to> "
"playback=<enable playback?> "
"record=<enable record?> "
- SOCKET_USAGE)
+ SOCKET_USAGE);
#elif defined(USE_PROTOCOL_CLI)
- #include <pulsecore/protocol-cli.h>
- #define protocol_new pa_protocol_cli_new
- #define protocol_free pa_protocol_cli_free
- #define TCPWRAP_SERVICE "pulseaudio-cli"
- #define IPV4_PORT 4712
- #define UNIX_SOCKET "cli"
- #define MODULE_ARGUMENTS
- #ifdef USE_TCP_SOCKETS
- #include "module-cli-protocol-tcp-symdef.h"
- #else
- #include "module-cli-protocol-unix-symdef.h"
- #endif
- PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
- PA_MODULE_USAGE(SOCKET_USAGE)
+# include <pulsecore/protocol-cli.h>
+# define TCPWRAP_SERVICE "pulseaudio-cli"
+# define IPV4_PORT 4712
+# define UNIX_SOCKET "cli"
+# define MODULE_ARGUMENTS
+
+# ifdef USE_TCP_SOCKETS
+# include "module-cli-protocol-tcp-symdef.h"
+# else
+# include "module-cli-protocol-unix-symdef.h"
+# endif
+
+ PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION);
+ PA_MODULE_USAGE(SOCKET_USAGE);
#elif defined(USE_PROTOCOL_HTTP)
- #include <pulsecore/protocol-http.h>
- #define protocol_new pa_protocol_http_new
- #define protocol_free pa_protocol_http_free
- #define TCPWRAP_SERVICE "pulseaudio-http"
- #define IPV4_PORT 4714
- #define UNIX_SOCKET "http"
- #define MODULE_ARGUMENTS
- #ifdef USE_TCP_SOCKETS
- #include "module-http-protocol-tcp-symdef.h"
- #else
- #include "module-http-protocol-unix-symdef.h"
- #endif
- PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION)
- PA_MODULE_USAGE(SOCKET_USAGE)
+# include <pulsecore/protocol-http.h>
+# define TCPWRAP_SERVICE "pulseaudio-http"
+# define IPV4_PORT 4714
+# define UNIX_SOCKET "http"
+# define MODULE_ARGUMENTS
+
+# ifdef USE_TCP_SOCKETS
+# include "module-http-protocol-tcp-symdef.h"
+# else
+# include "module-http-protocol-unix-symdef.h"
+# endif
+
+ PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION);
+ PA_MODULE_USAGE(SOCKET_USAGE);
#elif defined(USE_PROTOCOL_NATIVE)
- #include <pulsecore/protocol-native.h>
- #define protocol_new pa_protocol_native_new
- #define protocol_free pa_protocol_native_free
- #define TCPWRAP_SERVICE "pulseaudio-native"
- #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
- #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
- #define MODULE_ARGUMENTS_COMMON "cookie", "auth-anonymous",
- #ifdef USE_TCP_SOCKETS
- #include "module-native-protocol-tcp-symdef.h"
- #else
- #include "module-native-protocol-unix-symdef.h"
- #endif
-
- #if defined(HAVE_CREDS) && !defined(USE_TCP_SOCKETS)
- #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group", "auth-group-enable="
- #define AUTH_USAGE "auth-group=<system group to allow access> auth-group-enable=<enable auth by UNIX group?> "
- #else
- #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
- #define AUTH_USAGE
- #endif
-
- PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
+# include <pulsecore/protocol-native.h>
+# define TCPWRAP_SERVICE "pulseaudio-native"
+# define IPV4_PORT PA_NATIVE_DEFAULT_PORT
+# define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
+# define MODULE_ARGUMENTS_COMMON "cookie", "auth-cookie", "auth-cookie-enabled", "auth-anonymous",
+
+# ifdef USE_TCP_SOCKETS
+# include "module-native-protocol-tcp-symdef.h"
+# else
+# include "module-native-protocol-unix-symdef.h"
+# endif
+
+# if defined(HAVE_CREDS) && !defined(USE_TCP_SOCKETS)
+# define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group", "auth-group-enable",
+# define AUTH_USAGE "auth-group=<system group to allow access> auth-group-enable=<enable auth by UNIX group?> "
+# elif defined(USE_TCP_SOCKETS)
+# define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
+# define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
+# else
+# define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
+# define AUTH_USAGE
+# endif
+
+ PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION);
PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> "
- "cookie=<path to cookie file> "
+ "auth-cookie=<path to cookie file> "
+ "auth-cookie-enabled=<enable cookie authentification?> "
AUTH_USAGE
- SOCKET_USAGE)
+ SOCKET_USAGE);
#elif defined(USE_PROTOCOL_ESOUND)
- #include <pulsecore/protocol-esound.h>
- #include <pulsecore/esound.h>
- #define protocol_new pa_protocol_esound_new
- #define protocol_free pa_protocol_esound_free
- #define TCPWRAP_SERVICE "esound"
- #define IPV4_PORT ESD_DEFAULT_PORT
- #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
- #define MODULE_ARGUMENTS "sink", "source", "auth-anonymous", "cookie",
- #ifdef USE_TCP_SOCKETS
- #include "module-esound-protocol-tcp-symdef.h"
- #else
- #include "module-esound-protocol-unix-symdef.h"
- #endif
- PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION)
+# include <pulsecore/protocol-esound.h>
+# include <pulsecore/esound.h>
+# define TCPWRAP_SERVICE "esound"
+# define IPV4_PORT ESD_DEFAULT_PORT
+# define MODULE_ARGUMENTS_COMMON "sink", "source", "auth-anonymous", "cookie", "auth-cookie", "auth-cookie-enabled",
+
+# ifdef USE_TCP_SOCKETS
+# include "module-esound-protocol-tcp-symdef.h"
+# else
+# include "module-esound-protocol-unix-symdef.h"
+# endif
+
+# if defined(USE_TCP_SOCKETS)
+# define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
+# define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
+# else
+# define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
+# define AUTH_USAGE
+# endif
+
+ PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION);
PA_MODULE_USAGE("sink=<sink to connect to> "
"source=<source to connect to> "
"auth-anonymous=<don't verify cookies?> "
- "cookie=<path to cookie file> "
- SOCKET_USAGE)
+ "auth-cookie=<path to cookie file> "
+ "auth-cookie-enabled=<enable cookie authentification?> "
+ AUTH_USAGE
+ SOCKET_USAGE);
#else
- #error "Broken build system"
+# error "Broken build system"
#endif
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_VERSION(PACKAGE_VERSION);
static const char* const valid_modargs[] = {
MODULE_ARGUMENTS
@@ -180,169 +185,331 @@ static const char* const valid_modargs[] = {
};
struct userdata {
+ pa_module *module;
+
+#if defined(USE_PROTOCOL_SIMPLE)
+ pa_simple_protocol *simple_protocol;
+ pa_simple_options *simple_options;
+#elif defined(USE_PROTOCOL_CLI)
+ pa_cli_protocol *cli_protocol;
+#elif defined(USE_PROTOCOL_HTTP)
+ pa_http_protocol *http_protocol;
+#elif defined(USE_PROTOCOL_NATIVE)
+ pa_native_protocol *native_protocol;
+ pa_native_options *native_options;
+#else
+ pa_esound_protocol *esound_protocol;
+ pa_esound_options *esound_options;
+#endif
+
#if defined(USE_TCP_SOCKETS)
- void *protocol_ipv4;
- void *protocol_ipv6;
+ pa_socket_server *socket_server_ipv4;
+# ifdef HAVE_IPV6
+ pa_socket_server *socket_server_ipv6;
+# endif
#else
- void *protocol_unix;
+ pa_socket_server *socket_server_unix;
char *socket_path;
#endif
};
-int pa__init(pa_core *c, pa_module*m) {
- pa_modargs *ma = NULL;
- int ret = -1;
+static void socket_server_on_connection_cb(pa_socket_server*s, pa_iochannel *io, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(s);
+ pa_assert(io);
+ pa_assert(u);
+
+#if defined(USE_PROTOCOL_SIMPLE)
+ pa_simple_protocol_connect(u->simple_protocol, io, u->simple_options);
+#elif defined(USE_PROTOCOL_CLI)
+ pa_cli_protocol_connect(u->cli_protocol, io, u->module);
+#elif defined(USE_PROTOCOL_HTTP)
+ pa_http_protocol_connect(u->http_protocol, io, u->module);
+#elif defined(USE_PROTOCOL_NATIVE)
+ pa_native_protocol_connect(u->native_protocol, io, u->native_options);
+#else
+ pa_esound_protocol_connect(u->esound_protocol, io, u->esound_options);
+#endif
+}
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
struct userdata *u = NULL;
#if defined(USE_TCP_SOCKETS)
- pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
uint32_t port = IPV4_PORT;
+ pa_bool_t port_fallback = TRUE;
const char *listen_on;
#else
- pa_socket_server *s;
int r;
- char tmp[PATH_MAX];
#endif
- assert(c && m);
+#if defined(USE_PROTOCOL_NATIVE) || defined(USE_PROTOCOL_HTTP)
+ char t[256];
+#endif
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": Failed to parse module arguments");
- goto finish;
+ pa_log("Failed to parse module arguments");
+ goto fail;
}
- u = pa_xnew0(struct userdata, 1);
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+
+#if defined(USE_PROTOCOL_SIMPLE)
+ u->simple_protocol = pa_simple_protocol_get(m->core);
+
+ u->simple_options = pa_simple_options_new();
+ if (pa_simple_options_parse(u->simple_options, m->core, ma) < 0)
+ goto fail;
+ u->simple_options->module = m;
+#elif defined(USE_PROTOCOL_CLI)
+ u->cli_protocol = pa_cli_protocol_get(m->core);
+#elif defined(USE_PROTOCOL_HTTP)
+ u->http_protocol = pa_http_protocol_get(m->core);
+#elif defined(USE_PROTOCOL_NATIVE)
+ u->native_protocol = pa_native_protocol_get(m->core);
+
+ u->native_options = pa_native_options_new();
+ if (pa_native_options_parse(u->native_options, m->core, ma) < 0)
+ goto fail;
+ u->native_options->module = m;
+#else
+ u->esound_protocol = pa_esound_protocol_get(m->core);
+
+ u->esound_options = pa_esound_options_new();
+ if (pa_esound_options_parse(u->esound_options, m->core, ma) < 0)
+ goto fail;
+ u->esound_options->module = m;
+#endif
#if defined(USE_TCP_SOCKETS)
+
+ if (pa_in_system_mode() || pa_modargs_get_value(ma, "port", NULL))
+ port_fallback = FALSE;
+
if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
- pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
+ pa_log("port= expects a numerical argument between 1 and 65535.");
goto fail;
}
listen_on = pa_modargs_get_value(ma, "listen", NULL);
if (listen_on) {
- s_ipv6 = pa_socket_server_new_ipv6_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
- s_ipv4 = pa_socket_server_new_ipv4_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
+# ifdef HAVE_IPV6
+ u->socket_server_ipv6 = pa_socket_server_new_ipv6_string(m->core->mainloop, listen_on, (uint16_t) port, port_fallback, TCPWRAP_SERVICE);
+# endif
+ u->socket_server_ipv4 = pa_socket_server_new_ipv4_string(m->core->mainloop, listen_on, (uint16_t) port, port_fallback, TCPWRAP_SERVICE);
} else {
- s_ipv6 = pa_socket_server_new_ipv6_any(c->mainloop, port, TCPWRAP_SERVICE);
- s_ipv4 = pa_socket_server_new_ipv4_any(c->mainloop, port, TCPWRAP_SERVICE);
+# ifdef HAVE_IPV6
+ u->socket_server_ipv6 = pa_socket_server_new_ipv6_any(m->core->mainloop, (uint16_t) port, port_fallback, TCPWRAP_SERVICE);
+# endif
+ u->socket_server_ipv4 = pa_socket_server_new_ipv4_any(m->core->mainloop, (uint16_t) port, port_fallback, TCPWRAP_SERVICE);
}
- if (!s_ipv4 && !s_ipv6)
+# ifdef HAVE_IPV6
+ if (!u->socket_server_ipv4 && !u->socket_server_ipv6)
+# else
+ if (!u->socket_server_ipv4)
+# endif
goto fail;
- if (s_ipv4)
- if (!(u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma)))
- pa_socket_server_unref(s_ipv4);
-
- if (s_ipv6)
- if (!(u->protocol_ipv6 = protocol_new(c, s_ipv6, m, ma)))
- pa_socket_server_unref(s_ipv6);
-
- if (!u->protocol_ipv4 && !u->protocol_ipv6)
- goto fail;
+ if (u->socket_server_ipv4)
+ pa_socket_server_set_callback(u->socket_server_ipv4, socket_server_on_connection_cb, u);
+# ifdef HAVE_IPV6
+ if (u->socket_server_ipv6)
+ pa_socket_server_set_callback(u->socket_server_ipv6, socket_server_on_connection_cb, u);
+# endif
#else
- pa_runtime_path(pa_modargs_get_value(ma, "socket", UNIX_SOCKET), tmp, sizeof(tmp));
- u->socket_path = pa_xstrdup(tmp);
+# if defined(USE_PROTOCOL_ESOUND)
-#if defined(USE_PROTOCOL_ESOUND)
+# if defined(USE_PER_USER_ESOUND_SOCKET)
+ u->socket_path = pa_sprintf_malloc("/tmp/.esd-%lu/socket", (unsigned long) getuid());
+# else
+ u->socket_path = pa_xstrdup("/tmp/.esd/socket");
+# endif
/* This socket doesn't reside in our own runtime dir but in
* /tmp/.esd/, hence we have to create the dir first */
-
- if (pa_make_secure_parent_dir(u->socket_path, c->is_system_instance ? 0755 : 0700, (uid_t)-1, (gid_t)-1) < 0) {
- pa_log(__FILE__": Failed to create socket directory: %s\n", pa_cstrerror(errno));
+
+ if (pa_make_secure_parent_dir(u->socket_path, pa_in_system_mode() ? 0755U : 0700U, (uid_t)-1, (gid_t)-1) < 0) {
+ pa_log("Failed to create socket directory '%s': %s\n", u->socket_path, pa_cstrerror(errno));
goto fail;
}
-#endif
-
- if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
- pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s", tmp, pa_cstrerror(errno));
+
+# else
+ if (!(u->socket_path = pa_runtime_path(pa_modargs_get_value(ma, "socket", UNIX_SOCKET)))) {
+ pa_log("Failed to generate socket path.");
goto fail;
}
-
- if (r)
- pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
-
- if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
+# endif
+
+ if ((r = pa_unix_socket_remove_stale(u->socket_path)) < 0) {
+ pa_log("Failed to remove stale UNIX socket '%s': %s", u->socket_path, pa_cstrerror(errno));
goto fail;
+ } else if (r > 0)
+ pa_log_info("Removed stale UNIX socket '%s'.", u->socket_path);
- if (!(u->protocol_unix = protocol_new(c, s, m, ma)))
+ if (!(u->socket_server_unix = pa_socket_server_new_unix(m->core->mainloop, u->socket_path)))
goto fail;
+ pa_socket_server_set_callback(u->socket_server_unix, socket_server_on_connection_cb, u);
+
#endif
- m->userdata = u;
+#if defined(USE_PROTOCOL_NATIVE)
+# if defined(USE_TCP_SOCKETS)
+ if (u->socket_server_ipv4)
+ if (pa_socket_server_get_address(u->socket_server_ipv4, t, sizeof(t)))
+ pa_native_protocol_add_server_string(u->native_protocol, t);
+
+# ifdef HAVE_IPV6
+ if (u->socket_server_ipv6)
+ if (pa_socket_server_get_address(u->socket_server_ipv6, t, sizeof(t)))
+ pa_native_protocol_add_server_string(u->native_protocol, t);
+# endif
+# else
+ if (pa_socket_server_get_address(u->socket_server_unix, t, sizeof(t)))
+ pa_native_protocol_add_server_string(u->native_protocol, t);
+
+# endif
+#endif
- ret = 0;
+#if defined(USE_PROTOCOL_HTTP)
+#if defined(USE_TCP_SOCKETS)
+ if (u->socket_server_ipv4)
+ if (pa_socket_server_get_address(u->socket_server_ipv4, t, sizeof(t)))
+ pa_http_protocol_add_server_string(u->http_protocol, t);
+
+#ifdef HAVE_IPV6
+ if (u->socket_server_ipv6)
+ if (pa_socket_server_get_address(u->socket_server_ipv6, t, sizeof(t)))
+ pa_http_protocol_add_server_string(u->http_protocol, t);
+#endif /* HAVE_IPV6 */
+#else /* USE_TCP_SOCKETS */
+ if (pa_socket_server_get_address(u->socket_server_unix, t, sizeof(t)))
+ pa_http_protocol_add_server_string(u->http_protocol, t);
+
+#endif /* USE_TCP_SOCKETS */
+#endif /* USE_PROTOCOL_HTTP */
-finish:
if (ma)
pa_modargs_free(ma);
- return ret;
+ return 0;
fail:
- if (u) {
-#if defined(USE_TCP_SOCKETS)
- if (u->protocol_ipv4)
- protocol_free(u->protocol_ipv4);
- if (u->protocol_ipv6)
- protocol_free(u->protocol_ipv6);
-#else
- if (u->protocol_unix)
- protocol_free(u->protocol_unix);
- if (u->socket_path)
- pa_xfree(u->socket_path);
-#endif
+ if (ma)
+ pa_modargs_free(ma);
- pa_xfree(u);
- } else {
-#if defined(USE_TCP_SOCKETS)
- if (s_ipv4)
- pa_socket_server_unref(s_ipv4);
- if (s_ipv6)
- pa_socket_server_unref(s_ipv6);
-#else
- if (s)
- pa_socket_server_unref(s);
-#endif
- }
+ pa__done(m);
- goto finish;
+ return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata *u;
-
- assert(c);
- assert(m);
- u = m->userdata;
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+#if defined(USE_PROTOCOL_SIMPLE)
+ if (u->simple_protocol) {
+ pa_simple_protocol_disconnect(u->simple_protocol, u->module);
+ pa_simple_protocol_unref(u->simple_protocol);
+ }
+ if (u->simple_options)
+ pa_simple_options_unref(u->simple_options);
+#elif defined(USE_PROTOCOL_CLI)
+ if (u->cli_protocol) {
+ pa_cli_protocol_disconnect(u->cli_protocol, u->module);
+ pa_cli_protocol_unref(u->cli_protocol);
+ }
+#elif defined(USE_PROTOCOL_HTTP)
+ if (u->http_protocol) {
+ char t[256];
+
+#if defined(USE_TCP_SOCKETS)
+ if (u->socket_server_ipv4)
+ if (pa_socket_server_get_address(u->socket_server_ipv4, t, sizeof(t)))
+ pa_http_protocol_remove_server_string(u->http_protocol, t);
+
+#ifdef HAVE_IPV6
+ if (u->socket_server_ipv6)
+ if (pa_socket_server_get_address(u->socket_server_ipv6, t, sizeof(t)))
+ pa_http_protocol_remove_server_string(u->http_protocol, t);
+#endif /* HAVE_IPV6 */
+#else /* USE_TCP_SOCKETS */
+ if (u->socket_server_unix)
+ if (pa_socket_server_get_address(u->socket_server_unix, t, sizeof(t)))
+ pa_http_protocol_remove_server_string(u->http_protocol, t);
+#endif /* USE_PROTOCOL_HTTP */
+
+ pa_http_protocol_disconnect(u->http_protocol, u->module);
+ pa_http_protocol_unref(u->http_protocol);
+ }
+#elif defined(USE_PROTOCOL_NATIVE)
+ if (u->native_protocol) {
+
+ char t[256];
+
+# if defined(USE_TCP_SOCKETS)
+ if (u->socket_server_ipv4)
+ if (pa_socket_server_get_address(u->socket_server_ipv4, t, sizeof(t)))
+ pa_native_protocol_remove_server_string(u->native_protocol, t);
+
+# ifdef HAVE_IPV6
+ if (u->socket_server_ipv6)
+ if (pa_socket_server_get_address(u->socket_server_ipv6, t, sizeof(t)))
+ pa_native_protocol_remove_server_string(u->native_protocol, t);
+# endif
+# else
+ if (u->socket_server_unix)
+ if (pa_socket_server_get_address(u->socket_server_unix, t, sizeof(t)))
+ pa_native_protocol_remove_server_string(u->native_protocol, t);
+# endif
+
+ pa_native_protocol_disconnect(u->native_protocol, u->module);
+ pa_native_protocol_unref(u->native_protocol);
+ }
+ if (u->native_options)
+ pa_native_options_unref(u->native_options);
+#else
+ if (u->esound_protocol) {
+ pa_esound_protocol_disconnect(u->esound_protocol, u->module);
+ pa_esound_protocol_unref(u->esound_protocol);
+ }
+ if (u->esound_options)
+ pa_esound_options_unref(u->esound_options);
+#endif
#if defined(USE_TCP_SOCKETS)
- if (u->protocol_ipv4)
- protocol_free(u->protocol_ipv4);
- if (u->protocol_ipv6)
- protocol_free(u->protocol_ipv6);
+ if (u->socket_server_ipv4)
+ pa_socket_server_unref(u->socket_server_ipv4);
+# ifdef HAVE_IPV6
+ if (u->socket_server_ipv6)
+ pa_socket_server_unref(u->socket_server_ipv6);
+# endif
#else
- if (u->protocol_unix)
- protocol_free(u->protocol_unix);
+ if (u->socket_server_unix)
+ pa_socket_server_unref(u->socket_server_unix);
-#if defined(USE_PROTOCOL_ESOUND)
+# if defined(USE_PROTOCOL_ESOUND) && !defined(USE_PER_USER_ESOUND_SOCKET)
if (u->socket_path) {
char *p = pa_parent_dir(u->socket_path);
rmdir(p);
pa_xfree(p);
}
-#endif
-
-
+# endif
+
pa_xfree(u->socket_path);
#endif
diff --git a/src/modules/module-remap-sink.c b/src/modules/module-remap-sink.c
new file mode 100644
index 00000000..2822a7fc
--- /dev/null
+++ b/src/modules/module-remap-sink.c
@@ -0,0 +1,498 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/namereg.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/rtpoll.h>
+
+#include "module-remap-sink-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Virtual channel remapping sink");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "master=<name of sink to remap> "
+ "master_channel_map=<channel map> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "remix=<remix channels?>");
+
+struct userdata {
+ pa_module *module;
+
+ pa_sink *sink;
+ pa_sink_input *sink_input;
+
+ pa_bool_t auto_desc;
+};
+
+static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
+ "master",
+ "master_channel_map",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "remix",
+ NULL
+};
+
+/* Called from I/O thread context */
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_GET_LATENCY:
+
+ /* The sink is _put() before the sink input is, so let's
+ * make sure we don't access it yet */
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
+ *((pa_usec_t*) data) = 0;
+ return 0;
+ }
+
+ *((pa_usec_t*) data) =
+ /* Get the latency of the master sink */
+ pa_sink_get_latency_within_thread(u->sink_input->sink) +
+
+ /* Add the latency internal to our sink input on top */
+ pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
+
+ return 0;
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from main context */
+static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(state) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return 0;
+
+ pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void sink_request_rewind(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes, TRUE, FALSE, FALSE);
+}
+
+/* Called from I/O thread context */
+static void sink_update_requested_latency(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_set_requested_latency_within_thread(
+ u->sink_input,
+ pa_sink_get_requested_latency_within_thread(s));
+}
+
+/* Called from I/O thread context */
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert(chunk);
+ pa_assert_se(u = i->userdata);
+
+ /* Hmm, process any rewind request that might be queued up */
+ pa_sink_process_rewind(u->sink, 0);
+
+ pa_sink_render(u->sink, nbytes, chunk);
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ size_t amount = 0;
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ if (u->sink->thread_info.rewind_nbytes > 0) {
+ amount = PA_MIN(u->sink->thread_info.rewind_nbytes, nbytes);
+ u->sink->thread_info.rewind_nbytes = 0;
+ }
+
+ pa_sink_process_rewind(u->sink, amount);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_max_request_within_thread(u->sink, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_detach_within_thread(u->sink);
+
+ pa_sink_set_rtpoll(u->sink, NULL);
+}
+
+/* Called from I/O thread context */
+static void sink_input_attach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+ pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
+ pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
+
+ pa_sink_attach_within_thread(u->sink);
+}
+
+/* Called from main context */
+static void sink_input_kill_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* The order here matters! We first kill the sink input, followed
+ * by the sink. That means the sink callbacks must be protected
+ * against an unconnected sink input! */
+ pa_sink_input_unlink(u->sink_input);
+ pa_sink_unlink(u->sink);
+
+ pa_sink_input_unref(u->sink_input);
+ u->sink_input = NULL;
+
+ pa_sink_unref(u->sink);
+ u->sink = NULL;
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from IO thread context */
+static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* If we are added for the first time, ask for a rewinding so that
+ * we are heard right-away. */
+ if (PA_SINK_INPUT_IS_LINKED(state) &&
+ i->thread_info.state == PA_SINK_INPUT_INIT) {
+ pa_log_debug("Requesting rewind due to state change.");
+ pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
+ }
+}
+
+/* Called from main context */
+static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ return u->sink != dest;
+}
+
+/* Called from main context */
+static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ if (dest) {
+ pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
+ pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
+ } else
+ pa_sink_set_asyncmsgq(u->sink, NULL);
+
+ if (u->auto_desc && dest) {
+ const char *k;
+ pa_proplist *pl;
+
+ pl = pa_proplist_new();
+ k = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : dest->name);
+
+ pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
+ pa_proplist_free(pl);
+ }
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_sample_spec ss;
+ pa_channel_map sink_map, stream_map;
+ pa_modargs *ma;
+ pa_sink *master;
+ pa_sink_input_new_data sink_input_data;
+ pa_sink_new_data sink_data;
+ pa_bool_t remix = TRUE;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
+ pa_log("Master sink not found");
+ goto fail;
+ }
+
+ ss = master->sample_spec;
+ sink_map = master->channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &sink_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+ pa_log("Invalid sample format specification or channel map");
+ goto fail;
+ }
+
+ stream_map = sink_map;
+ if (pa_modargs_get_channel_map(ma, "master_channel_map", &stream_map) < 0) {
+ pa_log("Invalid master channel map");
+ goto fail;
+ }
+
+ if (stream_map.channels != ss.channels) {
+ pa_log("Number of channels doesn't match");
+ goto fail;
+ }
+
+ if (pa_channel_map_equal(&stream_map, &master->channel_map))
+ pa_log_warn("No remapping configured, proceeding nonetheless!");
+
+ if (pa_modargs_get_value_boolean(ma, "remix", &remix) < 0) {
+ pa_log("Invalid boolean remix parameter");
+ goto fail;
+ }
+
+ u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ m->userdata = u;
+
+ /* Create sink */
+ pa_sink_new_data_init(&sink_data);
+ sink_data.driver = __FILE__;
+ sink_data.module = m;
+ if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
+ sink_data.name = pa_sprintf_malloc("%s.remapped", master->name);
+ pa_sink_new_data_set_sample_spec(&sink_data, &ss);
+ pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&sink_data);
+ goto fail;
+ }
+
+ if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
+ const char *k;
+
+ k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : master->name);
+ }
+
+ u->sink = pa_sink_new(m->core, &sink_data, master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY));
+ pa_sink_new_data_done(&sink_data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg;
+ u->sink->set_state = sink_set_state;
+ u->sink->update_requested_latency = sink_update_requested_latency;
+ u->sink->request_rewind = sink_request_rewind;
+ u->sink->userdata = u;
+
+ pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
+
+ /* Create sink input */
+ pa_sink_input_new_data_init(&sink_input_data);
+ sink_input_data.driver = __FILE__;
+ sink_input_data.module = m;
+ pa_sink_input_new_data_set_sink(&sink_input_data, master, FALSE);
+ sink_input_data.origin_sink = u->sink;
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Remapped Stream");
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
+ pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
+ pa_sink_input_new_data_set_channel_map(&sink_input_data, &stream_map);
+ sink_input_data.flags = (remix ? 0 : PA_SINK_INPUT_NO_REMIX);
+
+ pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
+ pa_sink_input_new_data_done(&sink_input_data);
+
+ if (!u->sink_input)
+ goto fail;
+
+ u->sink_input->pop = sink_input_pop_cb;
+ u->sink_input->process_rewind = sink_input_process_rewind_cb;
+ u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
+ u->sink_input->update_max_request = sink_input_update_max_request_cb;
+ u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
+ u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
+ u->sink_input->attach = sink_input_attach_cb;
+ u->sink_input->detach = sink_input_detach_cb;
+ u->sink_input->kill = sink_input_kill_cb;
+ u->sink_input->state_change = sink_input_state_change_cb;
+ u->sink_input->may_move_to = sink_input_may_move_to_cb;
+ u->sink_input->moving = sink_input_moving_cb;
+ u->sink_input->userdata = u;
+
+ u->sink->input_to_master = u->sink_input;
+
+ pa_sink_put(u->sink);
+ pa_sink_input_put(u->sink_input);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ /* See comments in sink_input_kill_cb() above regarding
+ * destruction order! */
+
+ if (u->sink_input)
+ pa_sink_input_unlink(u->sink_input);
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->sink_input)
+ pa_sink_input_unref(u->sink_input);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-rescue-streams.c b/src/modules/module-rescue-streams.c
new file mode 100644
index 00000000..8b35809a
--- /dev/null
+++ b/src/modules/module-rescue-streams.c
@@ -0,0 +1,277 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/source-output.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/core-util.h>
+
+#include "module-rescue-streams-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("When a sink/source is removed, try to move their streams to the default sink/source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+static const char* const valid_modargs[] = {
+ NULL,
+};
+
+struct userdata {
+ pa_hook_slot
+ *sink_unlink_slot,
+ *source_unlink_slot,
+ *sink_input_move_fail_slot,
+ *source_output_move_fail_slot;
+};
+
+static pa_sink* find_evacuation_sink(pa_core *c, pa_sink_input *i, pa_sink *skip) {
+ pa_sink *target, *def;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(i);
+
+ def = pa_namereg_get_default_sink(c);
+
+ if (def && def != skip && pa_sink_input_may_move_to(i, def))
+ return def;
+
+ PA_IDXSET_FOREACH(target, c->sinks, idx) {
+ if (target == def)
+ continue;
+
+ if (target == skip)
+ continue;
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(target)))
+ continue;
+
+ if (pa_sink_input_may_move_to(i, target))
+ return target;
+ }
+
+ pa_log_debug("No evacuation sink found.");
+ return NULL;
+}
+
+static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
+ pa_sink_input *i;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(sink);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ if (pa_idxset_size(sink->inputs) <= 0) {
+ pa_log_debug("No sink inputs to move away.");
+ return PA_HOOK_OK;
+ }
+
+ PA_IDXSET_FOREACH(i, sink->inputs, idx) {
+ pa_sink *target;
+
+ if (!(target = find_evacuation_sink(c, i, sink)))
+ continue;
+
+ if (pa_sink_input_move_to(i, target, FALSE) < 0)
+ pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+ else
+ pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_move_fail_hook_callback(pa_core *c, pa_sink_input *i, void *userdata) {
+ pa_sink *target;
+
+ pa_assert(c);
+ pa_assert(i);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ if (!(target = find_evacuation_sink(c, i, NULL)))
+ return PA_HOOK_OK;
+
+ if (pa_sink_input_finish_move(i, target, FALSE) < 0) {
+ pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+ return PA_HOOK_OK;
+
+ } else {
+ pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+ return PA_HOOK_STOP;
+ }
+}
+
+static pa_source* find_evacuation_source(pa_core *c, pa_source_output *o, pa_source *skip) {
+ pa_source *target, *def;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(o);
+
+ def = pa_namereg_get_default_source(c);
+
+ if (def && def != skip && pa_source_output_may_move_to(o, def))
+ return def;
+
+ PA_IDXSET_FOREACH(target, c->sources, idx) {
+ if (target == def)
+ continue;
+
+ if (target == skip)
+ continue;
+
+ if (skip && !target->monitor_of != !skip->monitor_of)
+ continue;
+
+ if (!PA_SOURCE_IS_LINKED(pa_source_get_state(target)))
+ continue;
+
+ if (pa_source_output_may_move_to(o, target))
+ return target;
+ }
+
+ pa_log_debug("No evacuation source found.");
+ return NULL;
+}
+
+static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, void* userdata) {
+ pa_source_output *o;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(source);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ if (pa_idxset_size(source->outputs) <= 0) {
+ pa_log_debug("No source outputs to move away.");
+ return PA_HOOK_OK;
+ }
+
+ PA_IDXSET_FOREACH(o, source->outputs, idx) {
+ pa_source *target;
+
+ if (!(target = find_evacuation_source(c, o, source)))
+ continue;
+
+ if (pa_source_output_move_to(o, target, FALSE) < 0)
+ pa_log_info("Failed to move source output %u \"%s\" to %s.", o->index,
+ pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+ else
+ pa_log_info("Successfully moved source output %u \"%s\" to %s.", o->index,
+ pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_move_fail_hook_callback(pa_core *c, pa_source_output *i, void *userdata) {
+ pa_source *target;
+
+ pa_assert(c);
+ pa_assert(i);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ if (!(target = find_evacuation_source(c, i, NULL)))
+ return PA_HOOK_OK;
+
+ if (pa_source_output_finish_move(i, target, FALSE) < 0) {
+ pa_log_info("Failed to move source input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+ return PA_HOOK_OK;
+
+ } else {
+ pa_log_info("Successfully moved source input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+ return PA_HOOK_STOP;
+ }
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma;
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ return -1;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+
+ /* A little bit later than module-stream-restore, module-intended-roles... */
+ u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_unlink_hook_callback, u);
+ u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) source_unlink_hook_callback, u);
+
+ u->sink_input_move_fail_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_input_move_fail_hook_callback, u);
+ u->source_output_move_fail_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], PA_HOOK_LATE+20, (pa_hook_cb_t) source_output_move_fail_hook_callback, u);
+
+ pa_modargs_free(ma);
+ return 0;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink_unlink_slot)
+ pa_hook_slot_free(u->sink_unlink_slot);
+ if (u->source_unlink_slot)
+ pa_hook_slot_free(u->source_unlink_slot);
+
+ if (u->sink_input_move_fail_slot)
+ pa_hook_slot_free(u->sink_input_move_fail_slot);
+ if (u->source_output_move_fail_slot)
+ pa_hook_slot_free(u->source_output_move_fail_slot);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-rygel-media-server.c b/src/modules/module-rygel-media-server.c
new file mode 100644
index 00000000..22930749
--- /dev/null
+++ b/src/modules/module-rygel-media-server.c
@@ -0,0 +1,1125 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2005-2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <pulse/gccmacro.h>
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+#include <pulse/utf8.h>
+
+#include <pulsecore/sink.h>
+#include <pulsecore/source.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/dbus-shared.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/mime-type.h>
+#include <pulsecore/strbuf.h>
+#include <pulsecore/protocol-http.h>
+#include <pulsecore/parseaddr.h>
+
+#include "module-rygel-media-server-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("UPnP MediaServer Plugin for Rygel");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE("display_name=<UPnP Media Server name>");
+
+/* This implements http://live.gnome.org/Rygel/MediaServer2Spec */
+
+#define SERVICE_NAME "org.gnome.UPnP.MediaServer2.PulseAudio"
+
+#define OBJECT_ROOT "/org/gnome/UPnP/MediaServer2/PulseAudio"
+#define OBJECT_SINKS "/org/gnome/UPnP/MediaServer2/PulseAudio/Sinks"
+#define OBJECT_SOURCES "/org/gnome/UPnP/MediaServer2/PulseAudio/Sources"
+
+#define CONTAINER_INTROSPECT_XML_PREFIX \
+ DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
+ "<node>" \
+ " <!-- If you are looking for documentation make sure to check out" \
+ " http://live.gnome.org/Rygel/MediaServer2Spec -->" \
+ " <interface name=\"org.gnome.UPnP.MediaContainer2\">" \
+ " <method name='ListChildren'>" \
+ " <arg direction='in' name='offset' type='u' />" \
+ " <arg direction='in' name='max' type='u' />" \
+ " <arg direction='in' name='filter' type='as' />" \
+ " <arg direction='out' type='aa{sv}' />" \
+ " </method>" \
+ " <method name='ListContainers'>" \
+ " <arg direction='in' name='offset' type='u' />" \
+ " <arg direction='in' name='max' type='u' />" \
+ " <arg direction='in' name='filter' type='as' />" \
+ " <arg direction='out' type='aa{sv}' />" \
+ " </method>" \
+ " <method name='ListItems'>" \
+ " <arg direction='in' name='offset' type='u' />" \
+ " <arg direction='in' name='max' type='u' />" \
+ " <arg direction='in' name='filter' type='as' />" \
+ " <arg direction='out' type='aa{sv}' />" \
+ " </method>" \
+ " <signal name=\"Updated\">" \
+ " <arg name=\"path\" type=\"o\"/>" \
+ " </signal>" \
+ " <property name=\"ChildCount\" type=\"u\" access=\"read\"/>" \
+ " <property name=\"ItemCount\" type=\"u\" access=\"read\"/>" \
+ " <property name=\"ContainerCount\" type=\"u\" access=\"read\"/>" \
+ " <property name=\"Searchable\" type=\"b\" access=\"read\"/>" \
+ " </interface>" \
+ " <interface name=\"org.gnome.UPnP.MediaObject2\">" \
+ " <property name=\"Parent\" type=\"s\" access=\"read\"/>" \
+ " <property name=\"Type\" type=\"s\" access=\"read\"/>" \
+ " <property name=\"Path\" type=\"s\" access=\"read\"/>" \
+ " <property name=\"DisplayName\" type=\"s\" access=\"read\"/>" \
+ " </interface>" \
+ " <interface name=\"org.freedesktop.DBus.Properties\">" \
+ " <method name=\"Get\">" \
+ " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \
+ " <arg name=\"property\" direction=\"in\" type=\"s\"/>" \
+ " <arg name=\"value\" direction=\"out\" type=\"v\"/>" \
+ " </method>" \
+ " <method name=\"GetAll\">" \
+ " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \
+ " <arg name=\"properties\" direction=\"out\" type=\"a{sv}\"/>" \
+ " </method>" \
+ " </interface>" \
+ " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
+ " <method name=\"Introspect\">" \
+ " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
+ " </method>" \
+ " </interface>"
+
+#define CONTAINER_INTROSPECT_XML_POSTFIX \
+ "</node>"
+
+#define ROOT_INTROSPECT_XML \
+ CONTAINER_INTROSPECT_XML_PREFIX \
+ "<node name=\"Sinks\"/>" \
+ "<node name=\"Sources\"/>" \
+ CONTAINER_INTROSPECT_XML_POSTFIX
+
+#define ITEM_INTROSPECT_XML \
+ DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
+ "<node>" \
+ " <!-- If you are looking for documentation make sure to check out" \
+ " http://live.gnome.org/Rygel/MediaProvider2Spec -->" \
+ " <interface name=\"org.gnome.UPnP.MediaItem2\">" \
+ " <property name=\"URLs\" type=\"as\" access=\"read\"/>" \
+ " <property name=\"MIMEType\" type=\"s\" access=\"read\"/>" \
+ " <interface name=\"org.gnome.UPnP.MediaObject2\">" \
+ " <property name=\"Parent\" type=\"s\" access=\"read\"/>" \
+ " <property name=\"Type\" type=\"s\" access=\"read\"/>" \
+ " <property name=\"Path\" type=\"s\" access=\"read\"/>" \
+ " <property name=\"DisplayName\" type=\"s\" access=\"read\"/>" \
+ " </interface>" \
+ " <interface name=\"org.freedesktop.DBus.Properties\">" \
+ " <method name=\"Get\">" \
+ " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \
+ " <arg name=\"property\" direction=\"in\" type=\"s\"/>" \
+ " <arg name=\"value\" direction=\"out\" type=\"v\"/>" \
+ " </method>" \
+ " <method name=\"GetAll\">" \
+ " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \
+ " <arg name=\"properties\" direction=\"out\" type=\"a{sv}\"/>" \
+ " </method>" \
+ " </interface>" \
+ " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
+ " <method name=\"Introspect\">" \
+ " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
+ " </method>" \
+ " </interface>" \
+ "</node>"
+
+
+static const char* const valid_modargs[] = {
+ "display_name",
+ NULL
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+
+ pa_dbus_connection *bus;
+ pa_bool_t got_name:1;
+
+ char *display_name;
+
+ pa_hook_slot *source_new_slot, *source_unlink_slot;
+
+ pa_http_protocol *http;
+};
+
+static char *compute_url(const struct userdata *u, const char *name);
+
+static void send_signal(struct userdata *u, pa_source *s) {
+ DBusMessage *m;
+ const char *parent;
+
+ pa_assert(u);
+ pa_source_assert_ref(s);
+
+ if (u->core->state == PA_CORE_SHUTDOWN)
+ return;
+
+ if (s->monitor_of)
+ parent = OBJECT_SINKS;
+ else
+ parent = OBJECT_SOURCES;
+
+ pa_assert_se(m = dbus_message_new_signal(parent, "org.gnome.UPnP.MediaContainer2", "Updated"));
+ pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), m, NULL));
+
+ dbus_message_unref(m);
+}
+
+static pa_hook_result_t source_new_or_unlink_cb(pa_core *c, pa_source *s, struct userdata *u) {
+ pa_assert(c);
+ pa_source_assert_ref(s);
+
+ send_signal(u, s);
+
+ return PA_HOOK_OK;
+}
+
+static pa_bool_t message_is_property_get(DBusMessage *m, const char *interface, const char *property) {
+ const char *i, *p;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ pa_assert(m);
+
+ if (!dbus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get"))
+ return FALSE;
+
+ if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &i, DBUS_TYPE_STRING, &p, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
+ dbus_error_free(&error);
+ return FALSE;
+ }
+
+ return pa_streq(i, interface) && pa_streq(p, property);
+}
+
+static pa_bool_t message_is_property_get_all(DBusMessage *m, const char *interface) {
+ const char *i;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ pa_assert(m);
+
+ if (!dbus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "GetAll"))
+ return FALSE;
+
+ if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &i, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
+ dbus_error_free(&error);
+ return FALSE;
+ }
+
+ return pa_streq(i, interface);
+}
+
+static void append_variant_object_array(DBusMessage *m, DBusMessageIter *iter, const char *path[], unsigned n) {
+ DBusMessageIter _iter, variant, array;
+ unsigned c;
+
+ pa_assert(m);
+ pa_assert(path);
+
+ if (!iter) {
+ dbus_message_iter_init_append(m, &_iter);
+ iter = &_iter;
+ }
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "ao", &variant));
+ pa_assert_se(dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY, "o", &array));
+
+ for (c = 0; c < n; c++)
+ pa_assert_se(dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH, path + c));
+
+ pa_assert_se(dbus_message_iter_close_container(&variant, &array));
+ pa_assert_se(dbus_message_iter_close_container(iter, &variant));
+}
+
+static void append_variant_string(DBusMessage *m, DBusMessageIter *iter, const char *s) {
+ DBusMessageIter _iter, sub;
+
+ pa_assert(m);
+ pa_assert(s);
+
+ if (!iter) {
+ dbus_message_iter_init_append(m, &_iter);
+ iter = &_iter;
+ }
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "s", &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &s));
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_variant_object(DBusMessage *m, DBusMessageIter *iter, const char *s) {
+ DBusMessageIter _iter, sub;
+
+ pa_assert(m);
+ pa_assert(s);
+
+ if (!iter) {
+ dbus_message_iter_init_append(m, &_iter);
+ iter = &_iter;
+ }
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "o", &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &s));
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_variant_unsigned(DBusMessage *m, DBusMessageIter *iter, unsigned u) {
+ DBusMessageIter _iter, sub;
+
+ pa_assert(m);
+
+ if (!iter) {
+ dbus_message_iter_init_append(m, &_iter);
+ iter = &_iter;
+ }
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "u", &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u));
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_variant_boolean(DBusMessage *m, DBusMessageIter *iter, dbus_bool_t b) {
+ DBusMessageIter _iter, sub;
+
+ pa_assert(m);
+
+ if (!iter) {
+ dbus_message_iter_init_append(m, &_iter);
+ iter = &_iter;
+ }
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "b", &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_BOOLEAN, &b));
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_variant_urls(DBusMessage *m, DBusMessageIter *iter, const struct userdata *u, pa_sink *sink, pa_source *source) {
+ DBusMessageIter _iter, sub, array;
+ char *url;
+
+ pa_assert(m);
+ pa_assert(u);
+ pa_assert(sink || source);
+
+ if (!iter) {
+ dbus_message_iter_init_append(m, &_iter);
+ iter = &_iter;
+ }
+
+ url = compute_url(u, sink ? sink->monitor_source->name : source->name);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "as", &sub));
+ pa_assert_se(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "s", &array));
+ pa_assert_se(dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &url));
+ pa_assert_se(dbus_message_iter_close_container(&sub, &array));
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+
+ pa_xfree(url);
+}
+
+static void append_variant_mime_type(DBusMessage *m, DBusMessageIter *iter, pa_sink *sink, pa_source *source) {
+ char *mime_type;
+
+ pa_assert(sink || source);
+
+ if (sink)
+ mime_type = pa_sample_spec_to_mime_type_mimefy(&sink->sample_spec, &sink->channel_map);
+ else
+ mime_type = pa_sample_spec_to_mime_type_mimefy(&source->sample_spec, &source->channel_map);
+
+ append_variant_string(m, iter, mime_type);
+
+ pa_xfree(mime_type);
+}
+
+static void append_variant_item_display_name(DBusMessage *m, DBusMessageIter *iter, pa_sink *sink, pa_source *source) {
+ const char *display_name;
+
+ pa_assert(sink || source);
+
+ display_name = pa_strna(pa_proplist_gets(sink ? sink->proplist : source->proplist, PA_PROP_DEVICE_DESCRIPTION));
+ append_variant_string(m, iter, display_name);
+}
+
+PA_GCC_UNUSED
+static void append_property_dict_entry_object_array(DBusMessage *m, DBusMessageIter *iter, const char *name, const char *path[], unsigned n) {
+ DBusMessageIter sub;
+
+ pa_assert(iter);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
+ append_variant_object_array(m, &sub, path, n);
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_property_dict_entry_string(DBusMessage *m, DBusMessageIter *iter, const char *name, const char *value) {
+ DBusMessageIter sub;
+
+ pa_assert(iter);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
+ append_variant_string(m, &sub, value);
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_property_dict_entry_object(DBusMessage *m, DBusMessageIter *iter, const char *name, const char *value) {
+ DBusMessageIter sub;
+
+ pa_assert(iter);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
+ append_variant_object(m, &sub, value);
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_property_dict_entry_unsigned(DBusMessage *m, DBusMessageIter *iter, const char *name, unsigned u) {
+ DBusMessageIter sub;
+
+ pa_assert(iter);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
+ append_variant_unsigned(m, &sub, u);
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_property_dict_entry_boolean(DBusMessage *m, DBusMessageIter *iter, const char *name, dbus_bool_t b) {
+ DBusMessageIter sub;
+
+ pa_assert(iter);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
+ append_variant_boolean(m, &sub, b);
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_property_dict_entry_urls(DBusMessage *m, DBusMessageIter *iter, const struct userdata *u, pa_sink *sink, pa_source *source) {
+ DBusMessageIter sub;
+ const char *property_name = "URLs";
+
+ pa_assert(iter);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &property_name));
+ append_variant_urls(m, &sub, u, sink, source);
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_property_dict_entry_mime_type(DBusMessage *m, DBusMessageIter *iter, pa_sink *sink, pa_source *source) {
+ DBusMessageIter sub;
+ const char *property_name = "MIMEType";
+
+ pa_assert(iter);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &property_name));
+ append_variant_mime_type(m, &sub, sink, source);
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_property_dict_entry_item_display_name(DBusMessage *m, DBusMessageIter *iter, pa_sink *sink, pa_source *source) {
+ DBusMessageIter sub;
+ const char *property_name = "DisplayName";
+
+ pa_assert(iter);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
+ pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &property_name));
+ append_variant_item_display_name(m, &sub, sink, source);
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static pa_bool_t get_mediacontainer2_list_args(DBusMessage *m, unsigned *offset, unsigned *max_entries, char ***filter, int *filter_len) {
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ pa_assert(m);
+ pa_assert(offset);
+ pa_assert(max_entries);
+ pa_assert(filter);
+
+ if (!dbus_message_get_args(m, &error, DBUS_TYPE_UINT32, offset, DBUS_TYPE_UINT32, max_entries, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, filter, filter_len, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
+ dbus_error_free(&error);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static unsigned get_sinks_or_sources_count(const char *path, const struct userdata *u) {
+ unsigned n, k;
+
+ n = pa_idxset_size(u->core->sinks);
+ k = pa_idxset_size(u->core->sources);
+ pa_assert(k >= n);
+
+ return pa_streq(path, OBJECT_SINKS) ? n : k - n;
+}
+
+static void append_sink_or_source_container_mediaobject2_properties(DBusMessage *r, DBusMessageIter *sub, const char *path) {
+ append_property_dict_entry_object(r, sub, "Parent", OBJECT_ROOT);
+ append_property_dict_entry_string(r, sub, "Type", "container");
+ append_property_dict_entry_object(r, sub, "Path", path);
+ append_property_dict_entry_string(r, sub, "DisplayName",
+ pa_streq(path, OBJECT_SINKS) ?
+ _("Output Devices") :
+ _("Input Devices"));
+}
+
+static void append_sink_or_source_container_properties(
+ DBusMessage *r, DBusMessageIter *iter,
+ const char *path, const struct userdata *user_data,
+ char * const * filter, int filter_len) {
+
+ DBusMessageIter sub;
+
+ pa_assert(r);
+ pa_assert(iter);
+ pa_assert(path);
+ pa_assert(filter);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
+
+ if (filter_len == 1 && (*filter)[0] == '*' && (*filter)[1] == '\0') {
+ append_sink_or_source_container_mediaobject2_properties(r, &sub, path);
+ append_property_dict_entry_unsigned(r, &sub, "ChildCount", get_sinks_or_sources_count(path, user_data));
+ append_property_dict_entry_boolean(r, &sub, "Searchable", FALSE);
+ }
+ else {
+ for (int i = 0; i < filter_len; ++i) {
+ const char *property_name = filter[i];
+ if (pa_streq(property_name, "Parent")) {
+ append_property_dict_entry_object(r, &sub, "Parent", OBJECT_ROOT);
+ }
+ else if (pa_streq(property_name, "Type")) {
+ append_property_dict_entry_string(r, &sub, "Type", "container");
+ }
+ else if (pa_streq(property_name, "Path")) {
+ append_property_dict_entry_object(r, &sub, "Path", path);
+ }
+ else if (pa_streq(property_name, "DisplayName")) {
+ append_property_dict_entry_string(r, &sub, "DisplayName",
+ pa_streq(path, OBJECT_SINKS) ?
+ _("Output Devices") :
+ _("Input Devices"));
+ }
+ else if (pa_streq(property_name, "ChildCount")) {
+ append_property_dict_entry_unsigned(r, &sub, "ChildCount", get_sinks_or_sources_count(path, user_data));
+ }
+ else if (pa_streq(property_name, "Searchable")) {
+ append_property_dict_entry_boolean(r, &sub, "Searchable", FALSE);
+ }
+ }
+ }
+
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static void append_sink_or_source_item_mediaobject2_properties(DBusMessage *r, DBusMessageIter *sub, const char *path, pa_sink *sink, pa_source *source) {
+ append_property_dict_entry_object(r, sub, "Parent", sink ? OBJECT_SINKS : OBJECT_SOURCES);
+ append_property_dict_entry_string(r, sub, "Type", "audio");
+ append_property_dict_entry_object(r, sub, "Path", path);
+ append_property_dict_entry_item_display_name(r, sub, sink, source);
+}
+
+static void append_sink_or_source_item_properties(
+ DBusMessage *r, DBusMessageIter *iter,
+ const char *path, const struct userdata *user_data,
+ pa_sink *sink, pa_source *source,
+ char * const * filter, int filter_len) {
+
+ DBusMessageIter sub;
+
+ pa_assert(r);
+ pa_assert(iter);
+ pa_assert(path);
+ pa_assert(filter);
+ pa_assert(sink || source);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
+
+ if (filter_len == 1 && (*filter)[0] == '*' && (*filter)[1] == '\0') {
+ append_sink_or_source_item_mediaobject2_properties(r, &sub, path, sink, source);
+ append_property_dict_entry_urls(r, &sub, user_data, sink, source);
+ append_property_dict_entry_mime_type(r, &sub, sink, source);
+ }
+ else {
+ for (int i = 0; i < filter_len; ++i) {
+ const char *property_name = filter[i];
+ if (pa_streq(property_name, "Parent")) {
+ append_property_dict_entry_object(r, &sub, "Parent", sink ? OBJECT_SINKS : OBJECT_SOURCES);
+ }
+ else if (pa_streq(property_name, "Type")) {
+ append_property_dict_entry_string(r, &sub, "Type", "audio");
+ }
+ else if (pa_streq(property_name, "Path")) {
+ append_property_dict_entry_object(r, &sub, "Path", path);
+ }
+ else if (pa_streq(property_name, "DisplayName")) {
+ append_property_dict_entry_item_display_name(r, &sub, sink, source);
+ }
+ else if (pa_streq(property_name, "URLs")) {
+ append_property_dict_entry_urls(r, &sub, user_data, sink, source);
+ }
+ else if (pa_streq(property_name, "MIMEType")) {
+ append_property_dict_entry_mime_type(r, &sub, sink, source);
+ }
+ }
+ }
+
+ pa_assert_se(dbus_message_iter_close_container(iter, &sub));
+}
+
+static const char *array_root_containers[] = { OBJECT_SINKS, OBJECT_SOURCES };
+static const char *array_no_children[] = { };
+
+static DBusHandlerResult root_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
+ struct userdata *u = userdata;
+ DBusMessage *r = NULL;
+
+ pa_assert(u);
+
+ if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ChildCount")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_unsigned(r, NULL, PA_ELEMENTSOF(array_root_containers));
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ItemCount")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_unsigned(r, NULL, PA_ELEMENTSOF(array_no_children));
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ContainerCount")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_unsigned(r, NULL, PA_ELEMENTSOF(array_root_containers));
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "Searchable")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_boolean(r, NULL, FALSE);
+
+ } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaContainer2")) {
+ DBusMessageIter iter, sub;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ dbus_message_iter_init_append(r, &iter);
+
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
+ append_property_dict_entry_unsigned(r, &sub, "ChildCount", PA_ELEMENTSOF(array_root_containers));
+ append_property_dict_entry_unsigned(r, &sub, "ItemCount", PA_ELEMENTSOF(array_no_children));
+ append_property_dict_entry_unsigned(r, &sub, "ContainerCount", PA_ELEMENTSOF(array_root_containers));
+ append_property_dict_entry_boolean(r, &sub, "Searchable", FALSE);
+ pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
+
+ } else if (dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListChildren")
+ || dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListContainers")) {
+ DBusMessageIter iter, sub;
+ unsigned offset, max;
+ char ** filter;
+ int filter_len;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ dbus_message_iter_init_append(r, &iter);
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "a{sv}", &sub));
+
+ if (get_mediacontainer2_list_args(m, &offset, &max, &filter, &filter_len)) {
+ unsigned end = (max != 0 && offset + max < PA_ELEMENTSOF(array_root_containers))
+ ? max + offset
+ : PA_ELEMENTSOF(array_root_containers);
+
+ for (unsigned i = offset; i < end; ++i) {
+ const char *container_path = array_root_containers[i];
+ append_sink_or_source_container_properties(r, &sub, container_path, u, filter, filter_len);
+ }
+
+ dbus_free_string_array(filter);
+ }
+
+ pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
+
+ } else if (dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListItems")) {
+ DBusMessageIter iter, sub;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ dbus_message_iter_init_append(r, &iter);
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "a{sv}", &sub));
+ pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Parent")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_object(r, NULL, OBJECT_ROOT);
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Type")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_string(r, NULL, "container");
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Path")) {
+ const char *path = dbus_message_get_path(m);
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_object(r, NULL, path);
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "DisplayName")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_string(r, NULL, u->display_name);
+
+ } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaObject2")) {
+ DBusMessageIter iter, sub;
+ const char *path = dbus_message_get_path(m);
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ dbus_message_iter_init_append(r, &iter);
+
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
+ append_property_dict_entry_object(r, &sub, "Parent", OBJECT_ROOT);
+ append_property_dict_entry_string(r, &sub, "Type", "container");
+ append_property_dict_entry_object(r, &sub, "Path", path);
+ append_property_dict_entry_string(r, &sub, "DisplayName", u->display_name);
+ pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
+
+ } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
+ const char *xml = ROOT_INTROSPECT_XML;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
+
+ } else
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ if (r) {
+ pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), r, NULL));
+ dbus_message_unref(r);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static char *compute_url(const struct userdata *u, const char *name) {
+ pa_strlist *i;
+
+ pa_assert(u);
+ pa_assert(name);
+
+ for (i = pa_http_protocol_servers(u->http); i; i = pa_strlist_next(i)) {
+ pa_parsed_address a;
+
+ if (pa_parse_address(pa_strlist_data(i), &a) >= 0 &&
+ (a.type == PA_PARSED_ADDRESS_TCP4 ||
+ a.type == PA_PARSED_ADDRESS_TCP6 ||
+ a.type == PA_PARSED_ADDRESS_TCP_AUTO)) {
+
+ const char *address;
+ char *s;
+
+ if (pa_is_ip_address(a.path_or_host))
+ address = a.path_or_host;
+ else
+ address = "@ADDRESS@";
+
+ if (a.port <= 0)
+ a.port = 4714;
+
+ s = pa_sprintf_malloc("http://%s:%u/listen/source/%s", address, a.port, name);
+
+ pa_xfree(a.path_or_host);
+ return s;
+ }
+
+ pa_xfree(a.path_or_host);
+ }
+
+ return pa_sprintf_malloc("http://@ADDRESS@:4714/listen/source/%s", name);
+}
+
+static DBusHandlerResult sinks_and_sources_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
+ struct userdata *u = userdata;
+ DBusMessage *r = NULL;
+ const char *path;
+
+ pa_assert(u);
+
+ path = dbus_message_get_path(m);
+
+ if (pa_streq(path, OBJECT_SINKS) || pa_streq(path, OBJECT_SOURCES)) {
+
+ /* Container nodes */
+
+ if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ChildCount")
+ || message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ItemCount")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_unsigned(r, NULL, get_sinks_or_sources_count(path, u));
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ContainerCount")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_unsigned(r, NULL, 0);
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "Searchable")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_boolean(r, NULL, FALSE);
+
+ } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaContainer2")) {
+ DBusMessageIter iter, sub;
+ unsigned item_count;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ dbus_message_iter_init_append(r, &iter);
+
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
+
+ item_count = get_sinks_or_sources_count(path, u);
+
+ append_property_dict_entry_unsigned(r, &sub, "ChildCount", item_count);
+ append_property_dict_entry_unsigned(r, &sub, "ItemCount", item_count);
+ append_property_dict_entry_unsigned(r, &sub, "ContainerCount", 0);
+ append_property_dict_entry_boolean(r, &sub, "Searchable", FALSE);
+
+ pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
+
+ } else if (dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListChildren")
+ || dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListItems")) {
+ DBusMessageIter iter, sub;
+ unsigned offset, max;
+ char **filter;
+ int filter_len;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ dbus_message_iter_init_append(r, &iter);
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "a{sv}", &sub));
+
+ if (get_mediacontainer2_list_args(m, &offset, &max, &filter, &filter_len)) {
+ unsigned end = (max != 0) ? max + offset : UINT_MAX;
+
+ if (pa_streq(path, OBJECT_SINKS)) {
+ pa_sink *sink;
+ char sink_path[sizeof(OBJECT_SINKS) + 32];
+ char *path_end = sink_path + sizeof(OBJECT_SINKS);
+ unsigned item_index = 0;
+ uint32_t idx;
+
+ strcpy(sink_path, OBJECT_SINKS "/");
+
+ PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
+ if (item_index >= offset && item_index < end) {
+ sprintf(path_end, "%u", sink->index);
+ append_sink_or_source_item_properties(r, &sub, sink_path, u, sink, NULL, filter, filter_len);
+ }
+ ++item_index;
+ }
+ } else {
+ pa_source *source;
+ char source_path[sizeof(OBJECT_SOURCES) + 32];
+ char *path_end = source_path + sizeof(OBJECT_SOURCES);
+ unsigned item_index = 0;
+ uint32_t idx;
+
+ strcpy(source_path, OBJECT_SOURCES "/");
+
+ PA_IDXSET_FOREACH(source, u->core->sources, idx)
+ if (!source->monitor_of) {
+ if (item_index >= offset && item_index < end) {
+ sprintf(path_end, "%u", source->index);
+ append_sink_or_source_item_properties(r, &sub, source_path, u, NULL, source, filter, filter_len);
+ }
+ ++item_index;
+ }
+ }
+
+ dbus_free_string_array(filter);
+ }
+
+ pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
+
+ } else if (dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListContainers")) {
+ DBusMessageIter iter, sub;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ dbus_message_iter_init_append(r, &iter);
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "a{sv}", &sub));
+ pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Parent")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_object(r, NULL, OBJECT_ROOT);
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Type")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_string(r, NULL, "container");
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Path")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_object(r, NULL, path);
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "DisplayName")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_string(r,
+ NULL,
+ pa_streq(path, OBJECT_SINKS) ?
+ _("Output Devices") :
+ _("Input Devices"));
+
+ } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaObject2")) {
+ DBusMessageIter iter, sub;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+
+ dbus_message_iter_init_append(r, &iter);
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
+ append_sink_or_source_container_mediaobject2_properties(r, &sub, path);
+
+ } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
+ pa_strbuf *sb;
+ char *xml;
+ uint32_t idx;
+
+ sb = pa_strbuf_new();
+ pa_strbuf_puts(sb, CONTAINER_INTROSPECT_XML_PREFIX);
+
+ if (pa_streq(path, OBJECT_SINKS)) {
+ pa_sink *sink;
+
+ PA_IDXSET_FOREACH(sink, u->core->sinks, idx)
+ pa_strbuf_printf(sb, "<node name=\"%u\"/>", sink->index);
+ } else {
+ pa_source *source;
+
+ PA_IDXSET_FOREACH(source, u->core->sources, idx)
+ if (!source->monitor_of)
+ pa_strbuf_printf(sb, "<node name=\"%u\"/>", source->index);
+ }
+
+ pa_strbuf_puts(sb, CONTAINER_INTROSPECT_XML_POSTFIX);
+ xml = pa_strbuf_tostring_free(sb);
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
+
+ pa_xfree(xml);
+ } else
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ } else {
+ pa_sink *sink = NULL;
+ pa_source *source = NULL;
+
+ /* Child nodes */
+
+ if (pa_startswith(path, OBJECT_SINKS "/"))
+ sink = pa_namereg_get(u->core, path + sizeof(OBJECT_SINKS), PA_NAMEREG_SINK);
+ else if (pa_startswith(path, OBJECT_SOURCES "/"))
+ source = pa_namereg_get(u->core, path + sizeof(OBJECT_SOURCES), PA_NAMEREG_SOURCE);
+
+ if (!sink && !source)
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Parent")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_object(r, NULL, sink ? OBJECT_SINKS : OBJECT_SOURCES);
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Type")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_string(r, NULL, "audio");
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Path")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_object(r, NULL, path);
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "DisplayName")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_item_display_name(r, NULL, sink, source);
+
+ } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaObject2")) {
+ DBusMessageIter iter, sub;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ dbus_message_iter_init_append(r, &iter);
+
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
+ append_sink_or_source_item_mediaobject2_properties(r, &sub, path, sink, source);
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaItem2", "MIMEType")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_mime_type(r, NULL, sink, source);
+
+ } else if (message_is_property_get(m, "org.gnome.UPnP.MediaItem2", "URLs")) {
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ append_variant_urls(r, NULL, u, sink, source);
+
+ } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaItem2")) {
+ DBusMessageIter iter, sub;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ dbus_message_iter_init_append(r, &iter);
+
+ pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
+
+ append_property_dict_entry_mime_type(r, &sub, sink, source);
+ append_property_dict_entry_urls(r, &sub, u, sink, source);
+
+ pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
+
+ } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
+ const char *xml =
+ ITEM_INTROSPECT_XML;
+
+ pa_assert_se(r = dbus_message_new_method_return(m));
+ pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
+
+ } else
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ }
+
+ if (r) {
+ pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), r, NULL));
+ dbus_message_unref(r);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+int pa__init(pa_module *m) {
+
+ struct userdata *u;
+ pa_modargs *ma = NULL;
+ DBusError error;
+ const char *t;
+
+ static const DBusObjectPathVTable vtable_root = {
+ .message_function = root_handler,
+ };
+ static const DBusObjectPathVTable vtable_sinks_and_sources = {
+ .message_function = sinks_and_sources_handler,
+ };
+
+ dbus_error_init(&error);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->http = pa_http_protocol_get(u->core);
+
+ if ((t = pa_modargs_get_value(ma, "display_name", NULL)))
+ u->display_name = pa_utf8_filter(t);
+ else
+ u->display_name = pa_xstrdup(_("Audio on @HOSTNAME@"));
+
+ u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) source_new_or_unlink_cb, u);
+ u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_new_or_unlink_cb, u);
+
+ if (!(u->bus = pa_dbus_bus_get(m->core, DBUS_BUS_SESSION, &error))) {
+ pa_log("Failed to get session bus connection: %s", error.message);
+ goto fail;
+ }
+
+ pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(u->bus), OBJECT_ROOT, &vtable_root, u));
+ pa_assert_se(dbus_connection_register_fallback(pa_dbus_connection_get(u->bus), OBJECT_SINKS, &vtable_sinks_and_sources, u));
+ pa_assert_se(dbus_connection_register_fallback(pa_dbus_connection_get(u->bus), OBJECT_SOURCES, &vtable_sinks_and_sources, u));
+
+ if (dbus_bus_request_name(pa_dbus_connection_get(u->bus), SERVICE_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
+ pa_log("Failed to request service name " SERVICE_NAME ": %s", error.message);
+ goto fail;
+ }
+
+ u->got_name = TRUE;
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ dbus_error_free(&error);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata*u;
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->source_new_slot)
+ pa_hook_slot_free(u->source_new_slot);
+ if (u->source_unlink_slot)
+ pa_hook_slot_free(u->source_unlink_slot);
+
+ if (u->bus) {
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_ROOT);
+ dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_SINKS);
+ dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_SOURCES);
+
+ if (u->got_name) {
+ if (dbus_bus_release_name(pa_dbus_connection_get(u->bus), SERVICE_NAME, &error) != DBUS_RELEASE_NAME_REPLY_RELEASED) {
+ pa_log("Failed to release service name " SERVICE_NAME ": %s", error.message);
+ dbus_error_free(&error);
+ }
+ }
+
+ pa_dbus_connection_unref(u->bus);
+ }
+
+ pa_xfree(u->display_name);
+
+ if (u->http)
+ pa_http_protocol_unref(u->http);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-sine-source.c b/src/modules/module-sine-source.c
new file mode 100644
index 00000000..20a68680
--- /dev/null
+++ b/src/modules/module-sine-source.c
@@ -0,0 +1,326 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/source.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+
+#include "module-sine-source-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Sine wave generator source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "source_name=<name for the source> "
+ "source_properties=<properties for the source> "
+ "rate=<sample rate> "
+ "frequency=<frequency in Hz>");
+
+#define DEFAULT_SOURCE_NAME "sine_input"
+#define BLOCK_USEC (PA_USEC_PER_SEC * 2)
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_source *source;
+
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+
+ pa_memchunk memchunk;
+ size_t peek_index;
+
+ pa_usec_t block_usec; /* how much to push at once */
+ pa_usec_t timestamp; /* when to push next */
+};
+
+static const char* const valid_modargs[] = {
+ "source_name",
+ "source_properties",
+ "rate",
+ "frequency",
+ NULL
+};
+
+static int source_process_msg(
+ pa_msgobject *o,
+ int code,
+ void *data,
+ int64_t offset,
+ pa_memchunk *chunk) {
+
+ struct userdata *u = PA_SOURCE(o)->userdata;
+
+ switch (code) {
+
+ case PA_SOURCE_MESSAGE_SET_STATE:
+
+ if (PA_PTR_TO_UINT(data) == PA_SOURCE_RUNNING)
+ u->timestamp = pa_rtclock_now();
+
+ break;
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ pa_usec_t now, left_to_fill;
+
+ now = pa_rtclock_now();
+ left_to_fill = u->timestamp > now ? u->timestamp - now : 0ULL;
+
+ *((pa_usec_t*) data) = u->block_usec > left_to_fill ? u->block_usec - left_to_fill : 0ULL;
+
+ return 0;
+ }
+ }
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
+}
+
+static void source_update_requested_latency_cb(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ u->block_usec = pa_source_get_requested_latency_within_thread(s);
+
+ if (u->block_usec == (pa_usec_t) -1)
+ u->block_usec = s->thread_info.max_latency;
+
+ pa_log_debug("new block msec = %lu", (unsigned long) (u->block_usec / PA_USEC_PER_MSEC));
+}
+
+static void process_render(struct userdata *u, pa_usec_t now) {
+ pa_assert(u);
+
+ while (u->timestamp < now + u->block_usec) {
+ pa_memchunk chunk;
+ size_t k;
+
+ k = pa_usec_to_bytes_round_up(now + u->block_usec - u->timestamp, &u->source->sample_spec);
+
+ chunk = u->memchunk;
+ chunk.index += u->peek_index;
+ chunk.length = PA_MIN(chunk.length - u->peek_index, k);
+
+/* pa_log_debug("posting %lu", (unsigned long) chunk.length); */
+ pa_source_post(u->source, &chunk);
+
+ u->peek_index += chunk.length;
+ while (u->peek_index >= u->memchunk.length)
+ u->peek_index -= u->memchunk.length;
+
+ u->timestamp += pa_bytes_to_usec(chunk.length, &u->source->sample_spec);
+ }
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ u->timestamp = pa_rtclock_now();
+
+ for (;;) {
+ int ret;
+
+ if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
+ pa_usec_t now;
+
+ now = pa_rtclock_now();
+
+ if (u->timestamp <= now)
+ process_render(u, now);
+
+ pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
+ } else
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
+
+ /* Hmm, nothing to do. Let's sleep */
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_modargs *ma;
+ pa_source_new_data data;
+ uint32_t frequency;
+ pa_sample_spec ss;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("failed to parse module arguments.");
+ goto fail;
+ }
+
+ ss.format = PA_SAMPLE_FLOAT32;
+ ss.channels = 1;
+ ss.rate = 44100;
+
+ if (pa_modargs_get_value_u32(ma, "rate", &ss.rate) < 0 || ss.rate <= 1) {
+ pa_log("Invalid rate specification");
+ goto fail;
+ }
+
+ frequency = 440;
+ if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
+ pa_log("Invalid frequency specification");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ u->peek_index = 0;
+ pa_memchunk_sine(&u->memchunk, m->core->mempool, ss.rate, frequency);
+
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Sine source at %u Hz", (unsigned) frequency);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
+ pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency);
+ pa_source_new_data_set_sample_spec(&data, &ss);
+
+ if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&data);
+ goto fail;
+ }
+
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
+ pa_log("Failed to create source.");
+ goto fail;
+ }
+
+ u->source->parent.process_msg = source_process_msg;
+ u->source->update_requested_latency = source_update_requested_latency_cb;
+ u->source->userdata = u;
+
+ u->block_usec = BLOCK_USEC;
+
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+ pa_source_set_fixed_latency(u->source, u->block_usec);
+
+ if (!(u->thread = pa_thread_new("sine-source", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ pa_source_put(u->source);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_source_linked_by(u->source);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->source)
+ pa_source_unlink(u->source);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->source)
+ pa_source_unref(u->source);
+
+ if (u->memchunk.memblock)
+ pa_memblock_unref(u->memchunk.memblock);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-sine.c b/src/modules/module-sine.c
index f4392b9a..c6d73039 100644
--- a/src/modules/module-sine.c
+++ b/src/modules/module-sine.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,8 +24,6 @@
#endif
#include <stdio.h>
-#include <assert.h>
-#include <math.h>
#include <pulse/xmalloc.h>
@@ -37,16 +35,19 @@
#include "module-sine-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Sine wave generator")
-PA_MODULE_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Sine wave generator");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "sink=<sink to connect to> "
+ "frequency=<frequency in Hz>");
struct userdata {
pa_core *core;
pa_module *module;
pa_sink_input *sink_input;
- pa_memblock *memblock;
+ pa_memchunk memchunk;
size_t peek_index;
};
@@ -56,75 +57,80 @@ static const char* const valid_modargs[] = {
NULL,
};
-static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
struct userdata *u;
- assert(i && chunk && i->userdata);
- u = i->userdata;
- chunk->memblock = pa_memblock_ref(u->memblock);
- chunk->index = u->peek_index;
- chunk->length = u->memblock->length - u->peek_index;
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+ pa_assert(chunk);
+
+ *chunk = u->memchunk;
+ pa_memblock_ref(chunk->memblock);
+
+ chunk->index += u->peek_index;
+ chunk->length -= u->peek_index;
+
+ u->peek_index = 0;
+
return 0;
}
-static void sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
struct userdata *u;
- assert(i && chunk && length && i->userdata);
- u = i->userdata;
- assert(chunk->memblock == u->memblock && length <= u->memblock->length-u->peek_index);
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
- u->peek_index += length;
+ nbytes %= u->memchunk.length;
- if (u->peek_index >= u->memblock->length)
- u->peek_index = 0;
+ if (u->peek_index >= nbytes)
+ u->peek_index -= nbytes;
+ else
+ u->peek_index = u->memchunk.length + u->peek_index - nbytes;
}
-static void sink_input_kill(pa_sink_input *i) {
+static void sink_input_kill_cb(pa_sink_input *i) {
struct userdata *u;
- assert(i && i->userdata);
- u = i->userdata;
- pa_sink_input_disconnect(u->sink_input);
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_input_unlink(u->sink_input);
pa_sink_input_unref(u->sink_input);
u->sink_input = NULL;
- pa_module_unload_request(u->module);
+ pa_module_unload_request(u->module, TRUE);
}
-static void calc_sine(float *f, size_t l, float freq) {
- size_t i;
+/* Called from IO thread context */
+static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
- l /= sizeof(float);
-
- for (i = 0; i < l; i++)
- f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
+ /* If we are added for the first time, ask for a rewinding so that
+ * we are heard right-away. */
+ if (PA_SINK_INPUT_IS_LINKED(state) &&
+ i->thread_info.state == PA_SINK_INPUT_INIT)
+ pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
}
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
struct userdata *u;
pa_sink *sink;
- const char *sink_name;
pa_sample_spec ss;
uint32_t frequency;
- char t[256];
+ pa_sink_input_new_data data;
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": Failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto fail;
}
-
- m->userdata = u = pa_xmalloc(sizeof(struct userdata));
- u->core = c;
- u->module = m;
- u->sink_input = NULL;
- u->memblock = NULL;
-
- sink_name = pa_modargs_get_value(ma, "sink", NULL);
- if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
- pa_log(__FILE__": No such sink.");
+ if (!(sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK))) {
+ pa_log("No such sink.");
goto fail;
}
@@ -134,50 +140,67 @@ int pa__init(pa_core *c, pa_module*m) {
frequency = 440;
if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
- pa_log(__FILE__": Invalid frequency specification");
+ pa_log("Invalid frequency specification");
goto fail;
}
-
- u->memblock = pa_memblock_new(pa_bytes_per_second(&ss), c->memblock_stat);
- calc_sine(u->memblock->data, u->memblock->length, frequency);
- snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
- if (!(u->sink_input = pa_sink_input_new(sink, __FILE__, t, &ss, NULL, NULL, 0, -1)))
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->sink_input = NULL;
+
+ u->peek_index = 0;
+ pa_memchunk_sine(&u->memchunk, m->core->mempool, ss.rate, frequency);
+
+ pa_sink_input_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_input_new_data_set_sink(&data, sink, FALSE);
+ pa_proplist_setf(data.proplist, PA_PROP_MEDIA_NAME, "%u Hz Sine", frequency);
+ pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
+ pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency);
+ pa_sink_input_new_data_set_sample_spec(&data, &ss);
+
+ pa_sink_input_new(&u->sink_input, m->core, &data);
+ pa_sink_input_new_data_done(&data);
+
+ if (!u->sink_input)
goto fail;
- u->sink_input->peek = sink_input_peek;
- u->sink_input->drop = sink_input_drop;
- u->sink_input->kill = sink_input_kill;
+ u->sink_input->pop = sink_input_pop_cb;
+ u->sink_input->process_rewind = sink_input_process_rewind_cb;
+ u->sink_input->kill = sink_input_kill_cb;
+ u->sink_input->state_change = sink_input_state_change_cb;
u->sink_input->userdata = u;
- u->sink_input->owner = m;
- u->peek_index = 0;
-
+ pa_sink_input_put(u->sink_input);
+
pa_modargs_free(ma);
return 0;
-
+
fail:
if (ma)
pa_modargs_free(ma);
- pa__done(c, m);
+ pa__done(m);
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata *u = m->userdata;
- assert(c && m);
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
- if (!u)
+ if (!(u = m->userdata))
return;
if (u->sink_input) {
- pa_sink_input_disconnect(u->sink_input);
+ pa_sink_input_unlink(u->sink_input);
pa_sink_input_unref(u->sink_input);
}
-
- if (u->memblock)
- pa_memblock_unref(u->memblock);
+
+ if (u->memchunk.memblock)
+ pa_memblock_unref(u->memchunk.memblock);
+
pa_xfree(u);
}
-
diff --git a/src/modules/module-solaris.c b/src/modules/module-solaris.c
index 02ef4bc4..0e4e4017 100644
--- a/src/modules/module-solaris.c
+++ b/src/modules/module-solaris.c
@@ -1,18 +1,20 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+ Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
+ Copyright 2009 Finn Thain
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -25,14 +27,10 @@
#include <stdlib.h>
#include <stdio.h>
-#include <assert.h>
#include <errno.h>
-#include <string.h>
#include <fcntl.h>
#include <unistd.h>
-#include <limits.h>
#include <sys/ioctl.h>
-#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
@@ -40,11 +38,12 @@
#include <sys/conf.h>
#include <sys/audio.h>
-#include <pulse/error.h>
#include <pulse/mainloop-signal.h>
#include <pulse/xmalloc.h>
+#include <pulse/timeval.h>
+#include <pulse/util.h>
+#include <pulse/rtclock.h>
-#include <pulsecore/iochannel.h>
#include <pulsecore/sink.h>
#include <pulsecore/source.h>
#include <pulsecore/module.h>
@@ -52,49 +51,74 @@
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/time-smoother.h>
#include "module-solaris-symdef.h"
-PA_MODULE_AUTHOR("Pierre Ossman")
-PA_MODULE_DESCRIPTION("Solaris Sink/Source")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Pierre Ossman");
+PA_MODULE_DESCRIPTION("Solaris Sink/Source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
PA_MODULE_USAGE(
"sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
"source_name=<name for the source> "
- "device=<OSS device> record=<enable source?> "
+ "source_properties=<properties for the source> "
+ "device=<audio device file name> "
+ "record=<enable source?> "
"playback=<enable sink?> "
"format=<sample format> "
"channels=<number of channels> "
"rate=<sample rate> "
- "buffer_size=<record buffer size> "
- "channel_map=<channel map>")
+ "buffer_length=<milliseconds> "
+ "channel_map=<channel map>");
+PA_MODULE_LOAD_ONCE(FALSE);
struct userdata {
+ pa_core *core;
pa_sink *sink;
pa_source *source;
- pa_iochannel *io;
- pa_core *core;
- pa_time_event *timer;
- pa_usec_t poll_timeout;
+
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+
pa_signal_event *sig;
- pa_memchunk memchunk, silence;
+ pa_memchunk memchunk;
uint32_t frame_size;
- uint32_t buffer_size;
- unsigned int written_bytes, read_bytes;
+ int32_t buffer_size;
+ uint64_t written_bytes, read_bytes;
+ char *device_name;
+ int mode;
int fd;
+ pa_rtpoll_item *rtpoll_item;
pa_module *module;
+
+ pa_bool_t sink_suspended, source_suspended;
+
+ uint32_t play_samples_msw, record_samples_msw;
+ uint32_t prev_playback_samples, prev_record_samples;
+
+ int32_t minimum_request;
+
+ pa_smoother *smoother;
};
static const char* const valid_modargs[] = {
"sink_name",
+ "sink_properties",
"source_name",
+ "source_properties",
"device",
"record",
"playback",
- "buffer_size",
+ "buffer_length",
"format",
"rate",
"channels",
@@ -102,312 +126,133 @@ static const char* const valid_modargs[] = {
NULL
};
-#define DEFAULT_SINK_NAME "solaris_output"
-#define DEFAULT_SOURCE_NAME "solaris_input"
#define DEFAULT_DEVICE "/dev/audio"
-#define CHUNK_SIZE 2048
+#define MAX_RENDER_HZ (300)
+/* This render rate limit imposes a minimum latency, but without it we waste too much CPU time. */
-static void update_usage(struct userdata *u) {
- pa_module_set_used(u->module,
- (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
- (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
- (u->source ? pa_idxset_size(u->source->outputs) : 0));
-}
+#define MAX_BUFFER_SIZE (128 * 1024)
+/* An attempt to buffer more than 128 KB causes write() to fail with errno == EAGAIN. */
-static void do_write(struct userdata *u) {
+static uint64_t get_playback_buffered_bytes(struct userdata *u) {
audio_info_t info;
+ uint64_t played_bytes;
int err;
- pa_memchunk *memchunk;
- size_t len;
- ssize_t r;
-
- assert(u);
-
- /* We cannot check pa_iochannel_is_writable() because of our buffer hack */
- if (!u->sink)
- return;
- update_usage(u);
+ pa_assert(u->sink);
err = ioctl(u->fd, AUDIO_GETINFO, &info);
- assert(err >= 0);
-
- /*
- * Since we cannot modify the size of the output buffer we fake it
- * by not filling it more than u->buffer_size.
- */
- len = u->buffer_size;
- len -= u->written_bytes - (info.play.samples * u->frame_size);
-
- /* The sample counter can sometimes go backwards :( */
- if (len > u->buffer_size)
- len = 0;
-
- if (len == u->buffer_size)
- pa_log_debug(__FILE__": Solaris buffer underflow!");
-
- len -= len % u->frame_size;
-
- if (len == 0)
- return;
-
- memchunk = &u->memchunk;
-
- if (!memchunk->length)
- if (pa_sink_render(u->sink, len, memchunk) < 0)
- memchunk = &u->silence;
-
- assert(memchunk->memblock);
- assert(memchunk->memblock->data);
- assert(memchunk->length);
-
- if (memchunk->length < len) {
- len = memchunk->length;
- len -= len % u->frame_size;
- assert(len);
- }
-
- if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, len)) < 0) {
- pa_log(__FILE__": write() failed: %s", pa_cstrerror(errno));
- return;
- }
-
- assert(r % u->frame_size == 0);
-
- if (memchunk != &u->silence) {
- u->memchunk.index += r;
- u->memchunk.length -= r;
-
- if (u->memchunk.length <= 0) {
- pa_memblock_unref(u->memchunk.memblock);
- u->memchunk.memblock = NULL;
+ pa_assert(err >= 0);
+
+ /* Handle wrap-around of the device's sample counter, which is a uint_32. */
+ if (u->prev_playback_samples > info.play.samples) {
+ /*
+ * Unfortunately info.play.samples can sometimes go backwards, even before it wraps!
+ * The bug seems to be absent on Solaris x86 nv117 with audio810 driver, at least on this (UP) machine.
+ * The bug is present on a different (SMP) machine running Solaris x86 nv103 with audioens driver.
+ * An earlier revision of this file mentions the same bug independently (unknown configuration).
+ */
+ if (u->prev_playback_samples + info.play.samples < 240000) {
+ ++u->play_samples_msw;
+ } else {
+ pa_log_debug("play.samples went backwards %d bytes", u->prev_playback_samples - info.play.samples);
}
}
+ u->prev_playback_samples = info.play.samples;
+ played_bytes = (((uint64_t)u->play_samples_msw << 32) + info.play.samples) * u->frame_size;
- u->written_bytes += r;
-}
-
-static void do_read(struct userdata *u) {
- pa_memchunk memchunk;
- int err, l;
- ssize_t r;
- assert(u);
-
- if (!u->source || !pa_iochannel_is_readable(u->io))
- return;
-
- update_usage(u);
-
- err = ioctl(u->fd, I_NREAD, &l);
- assert(err >= 0);
-
- memchunk.memblock = pa_memblock_new(l, u->core->memblock_stat);
- assert(memchunk.memblock);
- if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
- pa_memblock_unref(memchunk.memblock);
- if (errno != EAGAIN)
- pa_log(__FILE__": read() failed: %s", pa_cstrerror(errno));
- return;
- }
-
- assert(r <= (ssize_t) memchunk.memblock->length);
- memchunk.length = memchunk.memblock->length = r;
- memchunk.index = 0;
-
- pa_source_post(u->source, &memchunk);
- pa_memblock_unref(memchunk.memblock);
-
- u->read_bytes += r;
-}
-
-static void io_callback(pa_iochannel *io, void*userdata) {
- struct userdata *u = userdata;
- assert(u);
- do_write(u);
- do_read(u);
-}
-
-static void timer_cb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) {
- struct userdata *u = userdata;
- struct timeval ntv;
-
- assert(u);
-
- do_write(u);
-
- pa_gettimeofday(&ntv);
- pa_timeval_add(&ntv, u->poll_timeout);
-
- a->time_restart(e, &ntv);
-}
-
-static void sig_callback(pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata) {
- struct userdata *u = userdata;
- pa_cvolume old_vol;
-
- assert(u);
-
- if (u->sink) {
- assert(u->sink->get_hw_volume);
- memcpy(&old_vol, &u->sink->hw_volume, sizeof(pa_cvolume));
- if (u->sink->get_hw_volume(u->sink) < 0)
- return;
- if (memcmp(&old_vol, &u->sink->hw_volume, sizeof(pa_cvolume)) != 0) {
- pa_subscription_post(u->sink->core,
- PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
- u->sink->index);
- }
- }
+ pa_smoother_put(u->smoother, pa_rtclock_now(), pa_bytes_to_usec(played_bytes, &u->sink->sample_spec));
- if (u->source) {
- assert(u->source->get_hw_volume);
- memcpy(&old_vol, &u->source->hw_volume, sizeof(pa_cvolume));
- if (u->source->get_hw_volume(u->source) < 0)
- return;
- if (memcmp(&old_vol, &u->source->hw_volume, sizeof(pa_cvolume)) != 0) {
- pa_subscription_post(u->source->core,
- PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
- u->source->index);
- }
- }
+ return u->written_bytes - played_bytes;
}
-static pa_usec_t sink_get_latency_cb(pa_sink *s) {
+static pa_usec_t sink_get_latency(struct userdata *u, pa_sample_spec *ss) {
pa_usec_t r = 0;
- audio_info_t info;
- int err;
- struct userdata *u = s->userdata;
- assert(s && u && u->sink);
-
- err = ioctl(u->fd, AUDIO_GETINFO, &info);
- assert(err >= 0);
-
- r += pa_bytes_to_usec(u->written_bytes, &s->sample_spec);
- r -= pa_bytes_to_usec(info.play.samples * u->frame_size, &s->sample_spec);
- if (u->memchunk.memblock)
- r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
+ pa_assert(u);
+ pa_assert(ss);
+ if (u->fd >= 0) {
+ r = pa_bytes_to_usec(get_playback_buffered_bytes(u), ss);
+ if (u->memchunk.memblock)
+ r += pa_bytes_to_usec(u->memchunk.length, ss);
+ }
return r;
}
-static pa_usec_t source_get_latency_cb(pa_source *s) {
- pa_usec_t r = 0;
- struct userdata *u = s->userdata;
+static uint64_t get_recorded_bytes(struct userdata *u) {
audio_info_t info;
+ uint64_t result;
int err;
- assert(s && u && u->source);
- err = ioctl(u->fd, AUDIO_GETINFO, &info);
- assert(err >= 0);
-
- r += pa_bytes_to_usec(info.record.samples * u->frame_size, &s->sample_spec);
- r -= pa_bytes_to_usec(u->read_bytes, &s->sample_spec);
-
- return r;
-}
-
-static int sink_get_hw_volume_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- audio_info_t info;
- int err;
+ pa_assert(u->source);
err = ioctl(u->fd, AUDIO_GETINFO, &info);
- assert(err >= 0);
+ pa_assert(err >= 0);
- pa_cvolume_set(&s->hw_volume, s->hw_volume.channels,
- info.play.gain * PA_VOLUME_NORM / AUDIO_MAX_GAIN);
+ if (u->prev_record_samples > info.record.samples)
+ ++u->record_samples_msw;
+ u->prev_record_samples = info.record.samples;
+ result = (((uint64_t)u->record_samples_msw << 32) + info.record.samples) * u->frame_size;
- return 0;
+ return result;
}
-static int sink_set_hw_volume_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
+static pa_usec_t source_get_latency(struct userdata *u, pa_sample_spec *ss) {
+ pa_usec_t r = 0;
audio_info_t info;
- AUDIO_INITINFO(&info);
+ pa_assert(u);
+ pa_assert(ss);
- info.play.gain = pa_cvolume_avg(&s->hw_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
- assert(info.play.gain <= AUDIO_MAX_GAIN);
+ if (u->fd) {
+ int err = ioctl(u->fd, AUDIO_GETINFO, &info);
+ pa_assert(err >= 0);
- if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
- if (errno == EINVAL)
- pa_log(__FILE__": AUDIO_SETINFO: Unsupported volume.");
- else
- pa_log(__FILE__": AUDIO_SETINFO: %s", pa_cstrerror(errno));
- return -1;
+ r = pa_bytes_to_usec(get_recorded_bytes(u), ss) - pa_bytes_to_usec(u->read_bytes, ss);
}
-
- return 0;
+ return r;
}
-static int sink_get_hw_mute_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- audio_info_t info;
- int err;
-
- err = ioctl(u->fd, AUDIO_GETINFO, &info);
- assert(err >= 0);
+static void build_pollfd(struct userdata *u) {
+ struct pollfd *pollfd;
- s->hw_muted = !!info.output_muted;
+ pa_assert(u);
+ pa_assert(!u->rtpoll_item);
+ u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
- return 0;
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->fd = u->fd;
+ pollfd->events = 0;
+ pollfd->revents = 0;
}
-static int sink_set_hw_mute_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
+static int set_buffer(int fd, int buffer_size) {
audio_info_t info;
- AUDIO_INITINFO(&info);
-
- info.output_muted = !!s->hw_muted;
-
- if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
- pa_log(__FILE__": AUDIO_SETINFO: %s", pa_cstrerror(errno));
- return -1;
- }
-
- return 0;
-}
-
-static int source_get_hw_volume_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- audio_info_t info;
- int err;
-
- err = ioctl(u->fd, AUDIO_GETINFO, &info);
- assert(err >= 0);
-
- pa_cvolume_set(&s->hw_volume, s->hw_volume.channels,
- info.record.gain * PA_VOLUME_NORM / AUDIO_MAX_GAIN);
-
- return 0;
-}
-
-static int source_set_hw_volume_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- audio_info_t info;
+ pa_assert(fd >= 0);
AUDIO_INITINFO(&info);
+ info.play.buffer_size = buffer_size;
+ info.record.buffer_size = buffer_size;
- info.record.gain = pa_cvolume_avg(&s->hw_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
- assert(info.record.gain <= AUDIO_MAX_GAIN);
-
- if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
+ if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
if (errno == EINVAL)
- pa_log(__FILE__": AUDIO_SETINFO: Unsupported volume.");
+ pa_log("AUDIO_SETINFO: Unsupported buffer size.");
else
- pa_log(__FILE__": AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
return -1;
}
return 0;
}
-static int pa_solaris_auto_format(int fd, int mode, pa_sample_spec *ss) {
+static int auto_format(int fd, int mode, pa_sample_spec *ss) {
audio_info_t info;
+ pa_assert(fd >= 0);
+ pa_assert(ss);
+
AUDIO_INITINFO(&info);
if (mode != O_RDONLY) {
@@ -431,6 +276,7 @@ static int pa_solaris_auto_format(int fd, int mode, pa_sample_spec *ss) {
info.play.encoding = AUDIO_ENCODING_LINEAR;
break;
default:
+ pa_log("AUDIO_SETINFO: Unsupported sample format.");
return -1;
}
}
@@ -456,208 +302,807 @@ static int pa_solaris_auto_format(int fd, int mode, pa_sample_spec *ss) {
info.record.encoding = AUDIO_ENCODING_LINEAR;
break;
default:
+ pa_log("AUDIO_SETINFO: Unsupported sample format.");
return -1;
}
}
if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
if (errno == EINVAL)
- pa_log(__FILE__": AUDIO_SETINFO: Unsupported sample format.");
+ pa_log("AUDIO_SETINFO: Failed to set sample format.");
else
- pa_log(__FILE__": AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
return -1;
}
return 0;
}
-static int pa_solaris_set_buffer(int fd, int buffer_size) {
- audio_info_t info;
+static int open_audio_device(struct userdata *u, pa_sample_spec *ss) {
+ pa_assert(u);
+ pa_assert(ss);
- AUDIO_INITINFO(&info);
+ if ((u->fd = pa_open_cloexec(u->device_name, u->mode | O_NONBLOCK, 0)) < 0) {
+ pa_log_warn("open %s failed (%s)", u->device_name, pa_cstrerror(errno));
+ return -1;
+ }
- info.record.buffer_size = buffer_size;
+ pa_log_info("device opened in %s mode.", u->mode == O_WRONLY ? "O_WRONLY" : (u->mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
- if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
- if (errno == EINVAL)
- pa_log(__FILE__": AUDIO_SETINFO: Unsupported buffer size.");
- else
- pa_log(__FILE__": AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ if (auto_format(u->fd, u->mode, ss) < 0)
+ return -1;
+
+ if (set_buffer(u->fd, u->buffer_size) < 0)
return -1;
+
+ u->written_bytes = u->read_bytes = 0;
+ u->play_samples_msw = u->record_samples_msw = 0;
+ u->prev_playback_samples = u->prev_record_samples = 0;
+
+ return u->fd;
+}
+
+static int suspend(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(u->fd >= 0);
+
+ pa_log_info("Suspending...");
+
+ ioctl(u->fd, AUDIO_DRAIN, NULL);
+ pa_close(u->fd);
+ u->fd = -1;
+
+ if (u->rtpoll_item) {
+ pa_rtpoll_item_free(u->rtpoll_item);
+ u->rtpoll_item = NULL;
}
+ pa_log_info("Device suspended.");
+
+ return 0;
+}
+
+static int unsuspend(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(u->fd < 0);
+
+ pa_log_info("Resuming...");
+
+ if (open_audio_device(u, u->sink ? &u->sink->sample_spec : &u->source->sample_spec) < 0)
+ return -1;
+
+ build_pollfd(u);
+
+ pa_log_info("Device resumed.");
+
return 0;
}
-int pa__init(pa_core *c, pa_module*m) {
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_GET_LATENCY:
+ *((pa_usec_t*) data) = sink_get_latency(u, &PA_SINK(o)->sample_spec);
+ return 0;
+
+ case PA_SINK_MESSAGE_SET_STATE:
+
+ switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
+
+ case PA_SINK_SUSPENDED:
+
+ pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
+
+ pa_smoother_pause(u->smoother, pa_rtclock_now());
+
+ if (!u->source || u->source_suspended) {
+ if (suspend(u) < 0)
+ return -1;
+ }
+ u->sink_suspended = TRUE;
+ break;
+
+ case PA_SINK_IDLE:
+ case PA_SINK_RUNNING:
+
+ if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
+ pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE);
+
+ if (!u->source || u->source_suspended) {
+ if (unsuspend(u) < 0)
+ return -1;
+ u->sink->get_volume(u->sink);
+ u->sink->get_mute(u->sink);
+ }
+ u->sink_suspended = FALSE;
+ }
+ break;
+
+ case PA_SINK_INVALID_STATE:
+ case PA_SINK_UNLINKED:
+ case PA_SINK_INIT:
+ ;
+ }
+
+ break;
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE(o)->userdata;
+
+ switch (code) {
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY:
+ *((pa_usec_t*) data) = source_get_latency(u, &PA_SOURCE(o)->sample_spec);
+ return 0;
+
+ case PA_SOURCE_MESSAGE_SET_STATE:
+
+ switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
+
+ case PA_SOURCE_SUSPENDED:
+
+ pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
+
+ if (!u->sink || u->sink_suspended) {
+ if (suspend(u) < 0)
+ return -1;
+ }
+ u->source_suspended = TRUE;
+ break;
+
+ case PA_SOURCE_IDLE:
+ case PA_SOURCE_RUNNING:
+
+ if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
+ if (!u->sink || u->sink_suspended) {
+ if (unsuspend(u) < 0)
+ return -1;
+ u->source->get_volume(u->source);
+ }
+ u->source_suspended = FALSE;
+ }
+ break;
+
+ case PA_SOURCE_UNLINKED:
+ case PA_SOURCE_INIT:
+ case PA_SOURCE_INVALID_STATE:
+ ;
+
+ }
+ break;
+
+ }
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
+}
+
+static void sink_set_volume(pa_sink *s) {
+ struct userdata *u;
+ audio_info_t info;
+
+ pa_assert_se(u = s->userdata);
+
+ if (u->fd >= 0) {
+ AUDIO_INITINFO(&info);
+
+ info.play.gain = pa_cvolume_max(&s->real_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
+ assert(info.play.gain <= AUDIO_MAX_GAIN);
+
+ if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
+ if (errno == EINVAL)
+ pa_log("AUDIO_SETINFO: Unsupported volume.");
+ else
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ }
+ }
+}
+
+static void sink_get_volume(pa_sink *s) {
+ struct userdata *u;
+ audio_info_t info;
+
+ pa_assert_se(u = s->userdata);
+
+ if (u->fd >= 0) {
+ if (ioctl(u->fd, AUDIO_GETINFO, &info) < 0)
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ else
+ pa_cvolume_set(&s->real_volume, s->sample_spec.channels, info.play.gain * PA_VOLUME_NORM / AUDIO_MAX_GAIN);
+ }
+}
+
+static void source_set_volume(pa_source *s) {
+ struct userdata *u;
+ audio_info_t info;
+
+ pa_assert_se(u = s->userdata);
+
+ if (u->fd >= 0) {
+ AUDIO_INITINFO(&info);
+
+ info.play.gain = pa_cvolume_max(&s->volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
+ assert(info.play.gain <= AUDIO_MAX_GAIN);
+
+ if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
+ if (errno == EINVAL)
+ pa_log("AUDIO_SETINFO: Unsupported volume.");
+ else
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ }
+ }
+}
+
+static void source_get_volume(pa_source *s) {
+ struct userdata *u;
+ audio_info_t info;
+
+ pa_assert_se(u = s->userdata);
+
+ if (u->fd >= 0) {
+ if (ioctl(u->fd, AUDIO_GETINFO, &info) < 0)
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ else
+ pa_cvolume_set(&s->volume, s->sample_spec.channels, info.play.gain * PA_VOLUME_NORM / AUDIO_MAX_GAIN);
+ }
+}
+
+static void sink_set_mute(pa_sink *s) {
+ struct userdata *u = s->userdata;
+ audio_info_t info;
+
+ pa_assert(u);
+
+ if (u->fd >= 0) {
+ AUDIO_INITINFO(&info);
+
+ info.output_muted = !!s->muted;
+
+ if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0)
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ }
+}
+
+static void sink_get_mute(pa_sink *s) {
+ struct userdata *u = s->userdata;
+ audio_info_t info;
+
+ pa_assert(u);
+
+ if (u->fd >= 0) {
+ if (ioctl(u->fd, AUDIO_GETINFO, &info) < 0)
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ else
+ s->muted = !!info.output_muted;
+ }
+}
+
+static void process_rewind(struct userdata *u) {
+ size_t rewind_nbytes;
+
+ pa_assert(u);
+
+ /* Figure out how much we shall rewind and reset the counter */
+ rewind_nbytes = u->sink->thread_info.rewind_nbytes;
+ u->sink->thread_info.rewind_nbytes = 0;
+
+ if (rewind_nbytes > 0) {
+ pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
+ rewind_nbytes = PA_MIN(u->memchunk.length, rewind_nbytes);
+ u->memchunk.length -= rewind_nbytes;
+ if (u->memchunk.length <= 0 && u->memchunk.memblock) {
+ pa_memblock_unref(u->memchunk.memblock);
+ pa_memchunk_reset(&u->memchunk);
+ }
+ pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
+ }
+
+ pa_sink_process_rewind(u->sink, rewind_nbytes);
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+ unsigned short revents = 0;
+ int ret, err;
+ audio_info_t info;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority);
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ pa_smoother_set_time_offset(u->smoother, pa_rtclock_now());
+
+ for (;;) {
+ /* Render some data and write it to the dsp */
+
+ if (u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+ pa_usec_t xtime0, ysleep_interval, xsleep_interval;
+ uint64_t buffered_bytes;
+
+ if (u->sink->thread_info.rewind_requested)
+ process_rewind(u);
+
+ err = ioctl(u->fd, AUDIO_GETINFO, &info);
+ if (err < 0) {
+ pa_log("AUDIO_GETINFO ioctl failed: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ if (info.play.error) {
+ pa_log_debug("buffer under-run!");
+
+ AUDIO_INITINFO(&info);
+ info.play.error = 0;
+ if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0)
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
+
+ pa_smoother_reset(u->smoother, pa_rtclock_now(), TRUE);
+ }
+
+ for (;;) {
+ void *p;
+ ssize_t w;
+ size_t len;
+ int write_type = 1;
+
+ /*
+ * Since we cannot modify the size of the output buffer we fake it
+ * by not filling it more than u->buffer_size.
+ */
+ xtime0 = pa_rtclock_now();
+ buffered_bytes = get_playback_buffered_bytes(u);
+ if (buffered_bytes >= (uint64_t)u->buffer_size)
+ break;
+
+ len = u->buffer_size - buffered_bytes;
+ len -= len % u->frame_size;
+
+ if (len < (size_t) u->minimum_request)
+ break;
+
+ if (!u->memchunk.length)
+ pa_sink_render(u->sink, u->sink->thread_info.max_request, &u->memchunk);
+
+ len = PA_MIN(u->memchunk.length, len);
+
+ p = pa_memblock_acquire(u->memchunk.memblock);
+ w = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, len, &write_type);
+ pa_memblock_release(u->memchunk.memblock);
+
+ if (w <= 0) {
+ if (errno == EINTR) {
+ continue;
+ } else if (errno == EAGAIN) {
+ /* We may have realtime priority so yield the CPU to ensure that fd can become writable again. */
+ pa_log_debug("EAGAIN with %llu bytes buffered.", buffered_bytes);
+ break;
+ } else {
+ pa_log("Failed to write data to DSP: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+ } else {
+ pa_assert(w % u->frame_size == 0);
+
+ u->written_bytes += w;
+ u->memchunk.index += w;
+ u->memchunk.length -= w;
+ if (u->memchunk.length <= 0) {
+ pa_memblock_unref(u->memchunk.memblock);
+ pa_memchunk_reset(&u->memchunk);
+ }
+ }
+ }
+
+ ysleep_interval = pa_bytes_to_usec(buffered_bytes / 2, &u->sink->sample_spec);
+ xsleep_interval = pa_smoother_translate(u->smoother, xtime0, ysleep_interval);
+ pa_rtpoll_set_timer_absolute(u->rtpoll, xtime0 + PA_MIN(xsleep_interval, ysleep_interval));
+ } else
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
+
+ /* Try to read some data and pass it on to the source driver */
+
+ if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state) && (revents & POLLIN)) {
+ pa_memchunk memchunk;
+ void *p;
+ ssize_t r;
+ size_t len;
+
+ err = ioctl(u->fd, AUDIO_GETINFO, &info);
+ pa_assert(err >= 0);
+
+ if (info.record.error) {
+ pa_log_debug("buffer overflow!");
+
+ AUDIO_INITINFO(&info);
+ info.record.error = 0;
+ if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0)
+ pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
+ }
+
+ err = ioctl(u->fd, I_NREAD, &len);
+ pa_assert(err >= 0);
+
+ if (len > 0) {
+ memchunk.memblock = pa_memblock_new(u->core->mempool, len);
+ pa_assert(memchunk.memblock);
+
+ p = pa_memblock_acquire(memchunk.memblock);
+ r = pa_read(u->fd, p, len, NULL);
+ pa_memblock_release(memchunk.memblock);
+
+ if (r < 0) {
+ pa_memblock_unref(memchunk.memblock);
+ if (errno == EAGAIN)
+ break;
+ else {
+ pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+ } else {
+ u->read_bytes += r;
+
+ memchunk.index = 0;
+ memchunk.length = r;
+
+ pa_source_post(u->source, &memchunk);
+ pa_memblock_unref(memchunk.memblock);
+
+ revents &= ~POLLIN;
+ }
+ }
+ }
+
+ if (u->rtpoll_item) {
+ struct pollfd *pollfd;
+
+ pa_assert(u->fd >= 0);
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->events = (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state)) ? POLLIN : 0;
+ }
+
+ /* Hmm, nothing to do. Let's sleep */
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+
+ if (u->rtpoll_item) {
+ struct pollfd *pollfd;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ if (pollfd->revents & ~(POLLOUT|POLLIN)) {
+ pa_log("DSP shutdown.");
+ goto fail;
+ }
+
+ revents = pollfd->revents;
+ } else
+ revents = 0;
+ }
+
+fail:
+ /* We have to continue processing messages until we receive the
+ * SHUTDOWN message */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+static void sig_callback(pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata) {
+ struct userdata *u = userdata;
+
+ assert(u);
+
+ pa_log_debug("caught signal");
+
+ if (u->sink) {
+ pa_sink_get_volume(u->sink, TRUE);
+ pa_sink_get_mute(u->sink, TRUE);
+ }
+
+ if (u->source)
+ pa_source_get_volume(u->source, TRUE);
+}
+
+int pa__init(pa_module *m) {
struct userdata *u = NULL;
- const char *p;
- int fd = -1;
- int buffer_size;
- int mode;
- int record = 1, playback = 1;
+ pa_bool_t record = TRUE, playback = TRUE;
pa_sample_spec ss;
pa_channel_map map;
pa_modargs *ma = NULL;
- struct timeval tv;
- assert(c && m);
+ uint32_t buffer_length_msec;
+ int fd = -1;
+ pa_sink_new_data sink_new_data;
+ pa_source_new_data source_new_data;
+ char const *name;
+ char *name_buf;
+ pa_bool_t namereg_fail;
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
+ pa_log("failed to parse module arguments.");
goto fail;
}
-
+
if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
- pa_log(__FILE__": record= and playback= expect numeric argument.");
+ pa_log("record= and playback= expect a boolean argument.");
goto fail;
}
if (!playback && !record) {
- pa_log(__FILE__": neither playback nor record enabled for device.");
+ pa_log("neither playback nor record enabled for device.");
goto fail;
}
- mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
+ u = pa_xnew0(struct userdata, 1);
- buffer_size = 16384;
- if (pa_modargs_get_value_s32(ma, "buffer_size", &buffer_size) < 0) {
- pa_log(__FILE__": failed to parse buffer size argument");
+ if (!(u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC * 2, TRUE, TRUE, 10, pa_rtclock_now(), TRUE)))
goto fail;
- }
- ss = c->default_sample_spec;
+ /*
+ * For a process (or several processes) to use the same audio device for both
+ * record and playback at the same time, the device's mixer must be enabled.
+ * See mixerctl(1). It may be turned off for playback only or record only.
+ */
+ u->mode = (playback && record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
+
+ ss = m->core->default_sample_spec;
if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
- pa_log(__FILE__": failed to parse sample specification");
+ pa_log("failed to parse sample specification");
+ goto fail;
+ }
+ u->frame_size = pa_frame_size(&ss);
+
+ u->minimum_request = pa_usec_to_bytes(PA_USEC_PER_SEC / MAX_RENDER_HZ, &ss);
+
+ buffer_length_msec = 100;
+ if (pa_modargs_get_value_u32(ma, "buffer_length", &buffer_length_msec) < 0) {
+ pa_log("failed to parse buffer_length argument");
+ goto fail;
+ }
+ u->buffer_size = pa_usec_to_bytes(1000 * buffer_length_msec, &ss);
+ if (u->buffer_size < 2 * u->minimum_request) {
+ pa_log("buffer_length argument cannot be smaller than %u",
+ (unsigned)(pa_bytes_to_usec(2 * u->minimum_request, &ss) / 1000));
goto fail;
}
-
- if ((fd = open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), mode | O_NONBLOCK)) < 0)
+ if (u->buffer_size > MAX_BUFFER_SIZE) {
+ pa_log("buffer_length argument cannot be greater than %u",
+ (unsigned)(pa_bytes_to_usec(MAX_BUFFER_SIZE, &ss) / 1000));
goto fail;
+ }
- pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
+ u->device_name = pa_xstrdup(pa_modargs_get_value(ma, "device", DEFAULT_DEVICE));
- if (pa_solaris_auto_format(fd, mode, &ss) < 0)
+ if ((fd = open_audio_device(u, &ss)) < 0)
goto fail;
- if ((mode != O_WRONLY) && (buffer_size >= 1))
- if (pa_solaris_set_buffer(fd, buffer_size) < 0)
+ u->core = m->core;
+ u->module = m;
+ m->userdata = u;
+
+ pa_memchunk_reset(&u->memchunk);
+
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ u->rtpoll_item = NULL;
+ build_pollfd(u);
+
+ if (u->mode != O_WRONLY) {
+ name_buf = NULL;
+ namereg_fail = TRUE;
+
+ if (!(name = pa_modargs_get_value(ma, "source_name", NULL))) {
+ name = name_buf = pa_sprintf_malloc("solaris_input.%s", pa_path_get_filename(u->device_name));
+ namereg_fail = FALSE;
+ }
+
+ pa_source_new_data_init(&source_new_data);
+ source_new_data.driver = __FILE__;
+ source_new_data.module = m;
+ pa_source_new_data_set_name(&source_new_data, name);
+ source_new_data.namereg_fail = namereg_fail;
+ pa_source_new_data_set_sample_spec(&source_new_data, &ss);
+ pa_source_new_data_set_channel_map(&source_new_data, &map);
+ pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
+ pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_API, "solaris");
+ pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Solaris PCM source");
+ pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, "serial");
+ pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) u->buffer_size);
+
+ if (pa_modargs_get_proplist(ma, "source_properties", source_new_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&source_new_data);
goto fail;
+ }
- u = pa_xmalloc(sizeof(struct userdata));
- u->core = c;
+ u->source = pa_source_new(m->core, &source_new_data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY|PA_SOURCE_HW_VOLUME_CTRL);
+ pa_source_new_data_done(&source_new_data);
+ pa_xfree(name_buf);
+
+ if (!u->source) {
+ pa_log("Failed to create source object");
+ goto fail;
+ }
- if (mode != O_WRONLY) {
- u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map);
- assert(u->source);
u->source->userdata = u;
- u->source->get_latency = source_get_latency_cb;
- u->source->get_hw_volume = source_get_hw_volume_cb;
- u->source->set_hw_volume = source_set_hw_volume_cb;
- pa_source_set_owner(u->source, m);
- u->source->description = pa_sprintf_malloc("Solaris PCM on '%s'", p);
- u->source->is_hardware = 1;
+ u->source->parent.process_msg = source_process_msg;
+
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+ pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(u->buffer_size, &u->source->sample_spec));
+
+ u->source->get_volume = source_get_volume;
+ u->source->set_volume = source_set_volume;
+ u->source->refresh_volume = TRUE;
} else
u->source = NULL;
- if (mode != O_RDONLY) {
- u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map);
- assert(u->sink);
- u->sink->get_latency = sink_get_latency_cb;
- u->sink->get_hw_volume = sink_get_hw_volume_cb;
- u->sink->set_hw_volume = sink_set_hw_volume_cb;
- u->sink->get_hw_mute = sink_get_hw_mute_cb;
- u->sink->set_hw_mute = sink_set_hw_mute_cb;
+ if (u->mode != O_RDONLY) {
+ name_buf = NULL;
+ namereg_fail = TRUE;
+ if (!(name = pa_modargs_get_value(ma, "sink_name", NULL))) {
+ name = name_buf = pa_sprintf_malloc("solaris_output.%s", pa_path_get_filename(u->device_name));
+ namereg_fail = FALSE;
+ }
+
+ pa_sink_new_data_init(&sink_new_data);
+ sink_new_data.driver = __FILE__;
+ sink_new_data.module = m;
+ pa_sink_new_data_set_name(&sink_new_data, name);
+ sink_new_data.namereg_fail = namereg_fail;
+ pa_sink_new_data_set_sample_spec(&sink_new_data, &ss);
+ pa_sink_new_data_set_channel_map(&sink_new_data, &map);
+ pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
+ pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_API, "solaris");
+ pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Solaris PCM sink");
+ pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, "serial");
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", sink_new_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&sink_new_data);
+ goto fail;
+ }
+
+ u->sink = pa_sink_new(m->core, &sink_new_data, PA_SINK_HARDWARE|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL|PA_SINK_HW_MUTE_CTRL);
+ pa_sink_new_data_done(&sink_new_data);
+
+ pa_assert(u->sink);
u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Solaris PCM on '%s'", p);
- u->sink->is_hardware = 1;
+ u->sink->parent.process_msg = sink_process_msg;
+
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+ pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(u->buffer_size, &u->sink->sample_spec));
+ pa_sink_set_max_request(u->sink, u->buffer_size);
+ pa_sink_set_max_rewind(u->sink, u->buffer_size);
+
+ u->sink->get_volume = sink_get_volume;
+ u->sink->set_volume = sink_set_volume;
+ u->sink->get_mute = sink_get_mute;
+ u->sink->set_mute = sink_set_mute;
+ u->sink->refresh_volume = u->sink->refresh_muted = TRUE;
} else
u->sink = NULL;
- assert(u->source || u->sink);
-
- u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : 0);
- assert(u->io);
- pa_iochannel_set_callback(u->io, io_callback, u);
- u->fd = fd;
+ pa_assert(u->source || u->sink);
- u->memchunk.memblock = NULL;
- u->memchunk.length = 0;
- u->frame_size = pa_frame_size(&ss);
- u->buffer_size = buffer_size;
-
- u->silence.memblock = pa_memblock_new(u->silence.length = CHUNK_SIZE, u->core->memblock_stat);
- assert(u->silence.memblock);
- pa_silence_memblock(u->silence.memblock, &ss);
- u->silence.index = 0;
+ u->sig = pa_signal_new(SIGPOLL, sig_callback, u);
+ if (u->sig)
+ ioctl(u->fd, I_SETSIG, S_MSG);
+ else
+ pa_log_warn("Could not register SIGPOLL handler");
- u->written_bytes = 0;
- u->read_bytes = 0;
+ if (!(u->thread = pa_thread_new("solaris", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
- u->module = m;
- m->userdata = u;
+ /* Read mixer settings */
+ if (u->sink) {
+ if (sink_new_data.volume_is_set)
+ u->sink->set_volume(u->sink);
+ else
+ u->sink->get_volume(u->sink);
- u->poll_timeout = pa_bytes_to_usec(u->buffer_size / 10, &ss);
+ if (sink_new_data.muted_is_set)
+ u->sink->set_mute(u->sink);
+ else
+ u->sink->get_mute(u->sink);
- pa_gettimeofday(&tv);
- pa_timeval_add(&tv, u->poll_timeout);
+ pa_sink_put(u->sink);
+ }
- u->timer = c->mainloop->time_new(c->mainloop, &tv, timer_cb, u);
- assert(u->timer);
+ if (u->source) {
+ if (source_new_data.volume_is_set)
+ u->source->set_volume(u->source);
+ else
+ u->source->get_volume(u->source);
- u->sig = pa_signal_new(SIGPOLL, sig_callback, u);
- assert(u->sig);
- ioctl(u->fd, I_SETSIG, S_MSG);
+ pa_source_put(u->source);
+ }
pa_modargs_free(ma);
- /* Read mixer settings */
- if (u->source)
- source_get_hw_volume_cb(u->source);
- if (u->sink) {
- sink_get_hw_volume_cb(u->sink);
- sink_get_hw_mute_cb(u->sink);
- }
-
return 0;
fail:
- if (fd >= 0)
+ if (u)
+ pa__done(m);
+ else if (fd >= 0)
close(fd);
if (ma)
pa_modargs_free(ma);
-
+
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module *m) {
struct userdata *u;
- assert(c && m);
+
+ pa_assert(m);
if (!(u = m->userdata))
return;
- if (u->timer)
- c->mainloop->time_free(u->timer);
- ioctl(u->fd, I_SETSIG, 0);
- pa_signal_free(u->sig);
-
- if (u->memchunk.memblock)
- pa_memblock_unref(u->memchunk.memblock);
- if (u->silence.memblock)
- pa_memblock_unref(u->silence.memblock);
+ if (u->sig) {
+ ioctl(u->fd, I_SETSIG, 0);
+ pa_signal_free(u->sig);
+ }
- if (u->sink) {
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->source)
+ pa_source_unlink(u->source);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
}
-
- if (u->source) {
- pa_source_disconnect(u->source);
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->source)
pa_source_unref(u->source);
- }
-
- pa_iochannel_free(u->io);
+
+ if (u->memchunk.memblock)
+ pa_memblock_unref(u->memchunk.memblock);
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ if (u->fd >= 0)
+ close(u->fd);
+
+ if (u->smoother)
+ pa_smoother_free(u->smoother);
+
+ pa_xfree(u->device_name);
+
pa_xfree(u);
}
diff --git a/src/modules/module-stream-restore.c b/src/modules/module-stream-restore.c
new file mode 100644
index 00000000..19c09bb6
--- /dev/null
+++ b/src/modules/module-stream-restore.c
@@ -0,0 +1,2286 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Lennart Poettering
+ Copyright 2009 Tanu Kaskinen
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pulse/gccmacro.h>
+#include <pulse/xmalloc.h>
+#include <pulse/volume.h>
+#include <pulse/timeval.h>
+#include <pulse/rtclock.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-subscribe.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/source-output.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/protocol-native.h>
+#include <pulsecore/pstream.h>
+#include <pulsecore/pstream-util.h>
+#include <pulsecore/database.h>
+#include <pulsecore/tagstruct.h>
+
+#ifdef HAVE_DBUS
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/protocol-dbus.h>
+#endif
+
+#include "module-stream-restore-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Automatically restore the volume/mute/device state of streams");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(
+ "restore_device=<Save/restore sinks/sources?> "
+ "restore_volume=<Save/restore volumes?> "
+ "restore_muted=<Save/restore muted states?> "
+ "on_hotplug=<When new device becomes available, recheck streams?> "
+ "on_rescue=<When device becomes unavailable, recheck streams?>");
+
+#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
+#define IDENTIFICATION_PROPERTY "module-stream-restore.id"
+
+static const char* const valid_modargs[] = {
+ "restore_device",
+ "restore_volume",
+ "restore_muted",
+ "on_hotplug",
+ "on_rescue",
+ NULL
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_subscription *subscription;
+ pa_hook_slot
+ *sink_input_new_hook_slot,
+ *sink_input_fixate_hook_slot,
+ *source_output_new_hook_slot,
+ *sink_put_hook_slot,
+ *source_put_hook_slot,
+ *sink_unlink_hook_slot,
+ *source_unlink_hook_slot,
+ *connection_unlink_hook_slot;
+ pa_time_event *save_time_event;
+ pa_database* database;
+
+ pa_bool_t restore_device:1;
+ pa_bool_t restore_volume:1;
+ pa_bool_t restore_muted:1;
+ pa_bool_t on_hotplug:1;
+ pa_bool_t on_rescue:1;
+
+ pa_native_protocol *protocol;
+ pa_idxset *subscribed;
+
+#ifdef HAVE_DBUS
+ pa_dbus_protocol *dbus_protocol;
+ pa_hashmap *dbus_entries;
+ uint32_t next_index; /* For generating object paths for entries. */
+#endif
+};
+
+#define ENTRY_VERSION 1
+
+struct entry {
+ uint8_t version;
+ pa_bool_t muted_valid, volume_valid, device_valid, card_valid;
+ pa_bool_t muted;
+ pa_channel_map channel_map;
+ pa_cvolume volume;
+ char* device;
+ char* card;
+};
+
+enum {
+ SUBCOMMAND_TEST,
+ SUBCOMMAND_READ,
+ SUBCOMMAND_WRITE,
+ SUBCOMMAND_DELETE,
+ SUBCOMMAND_SUBSCRIBE,
+ SUBCOMMAND_EVENT
+};
+
+
+static struct entry* entry_new(void);
+static void entry_free(struct entry *e);
+static struct entry *entry_read(struct userdata *u, const char *name);
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e, pa_bool_t replace);
+static struct entry* entry_copy(const struct entry *e);
+static void entry_apply(struct userdata *u, const char *name, struct entry *e);
+static void trigger_save(struct userdata *u);
+
+#ifdef HAVE_DBUS
+
+#define OBJECT_PATH "/org/pulseaudio/stream_restore1"
+#define ENTRY_OBJECT_NAME "entry"
+#define INTERFACE_STREAM_RESTORE "org.PulseAudio.Ext.StreamRestore1"
+#define INTERFACE_ENTRY INTERFACE_STREAM_RESTORE ".RestoreEntry"
+
+#define DBUS_INTERFACE_REVISION 0
+
+struct dbus_entry {
+ struct userdata *userdata;
+
+ char *entry_name;
+ uint32_t index;
+ char *object_path;
+};
+
+static void handle_get_interface_revision(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_entries(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_add_entry(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_get_entry_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_entry_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_entry_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_entry_get_device(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_entry_set_device(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_entry_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_entry_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+static void handle_entry_get_mute(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_entry_set_mute(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
+
+static void handle_entry_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+static void handle_entry_remove(DBusConnection *conn, DBusMessage *msg, void *userdata);
+
+enum property_handler_index {
+ PROPERTY_HANDLER_INTERFACE_REVISION,
+ PROPERTY_HANDLER_ENTRIES,
+ PROPERTY_HANDLER_MAX
+};
+
+enum entry_property_handler_index {
+ ENTRY_PROPERTY_HANDLER_INDEX,
+ ENTRY_PROPERTY_HANDLER_NAME,
+ ENTRY_PROPERTY_HANDLER_DEVICE,
+ ENTRY_PROPERTY_HANDLER_VOLUME,
+ ENTRY_PROPERTY_HANDLER_MUTE,
+ ENTRY_PROPERTY_HANDLER_MAX
+};
+
+static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
+ [PROPERTY_HANDLER_INTERFACE_REVISION] = { .property_name = "InterfaceRevision", .type = "u", .get_cb = handle_get_interface_revision, .set_cb = NULL },
+ [PROPERTY_HANDLER_ENTRIES] = { .property_name = "Entries", .type = "ao", .get_cb = handle_get_entries, .set_cb = NULL }
+};
+
+static pa_dbus_property_handler entry_property_handlers[ENTRY_PROPERTY_HANDLER_MAX] = {
+ [ENTRY_PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_entry_get_index, .set_cb = NULL },
+ [ENTRY_PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_entry_get_name, .set_cb = NULL },
+ [ENTRY_PROPERTY_HANDLER_DEVICE] = { .property_name = "Device", .type = "s", .get_cb = handle_entry_get_device, .set_cb = handle_entry_set_device },
+ [ENTRY_PROPERTY_HANDLER_VOLUME] = { .property_name = "Volume", .type = "a(uu)", .get_cb = handle_entry_get_volume, .set_cb = handle_entry_set_volume },
+ [ENTRY_PROPERTY_HANDLER_MUTE] = { .property_name = "Mute", .type = "b", .get_cb = handle_entry_get_mute, .set_cb = handle_entry_set_mute }
+};
+
+enum method_handler_index {
+ METHOD_HANDLER_ADD_ENTRY,
+ METHOD_HANDLER_GET_ENTRY_BY_NAME,
+ METHOD_HANDLER_MAX
+};
+
+enum entry_method_handler_index {
+ ENTRY_METHOD_HANDLER_REMOVE,
+ ENTRY_METHOD_HANDLER_MAX
+};
+
+static pa_dbus_arg_info add_entry_args[] = { { "name", "s", "in" },
+ { "device", "s", "in" },
+ { "volume", "a(uu)", "in" },
+ { "mute", "b", "in" },
+ { "entry", "o", "out" } };
+static pa_dbus_arg_info get_entry_by_name_args[] = { { "name", "s", "in" }, { "entry", "o", "out" } };
+
+static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
+ [METHOD_HANDLER_ADD_ENTRY] = {
+ .method_name = "AddEntry",
+ .arguments = add_entry_args,
+ .n_arguments = sizeof(add_entry_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_add_entry },
+ [METHOD_HANDLER_GET_ENTRY_BY_NAME] = {
+ .method_name = "GetEntryByName",
+ .arguments = get_entry_by_name_args,
+ .n_arguments = sizeof(get_entry_by_name_args) / sizeof(pa_dbus_arg_info),
+ .receive_cb = handle_get_entry_by_name }
+};
+
+static pa_dbus_method_handler entry_method_handlers[ENTRY_METHOD_HANDLER_MAX] = {
+ [ENTRY_METHOD_HANDLER_REMOVE] = {
+ .method_name = "Remove",
+ .arguments = NULL,
+ .n_arguments = 0,
+ .receive_cb = handle_entry_remove }
+};
+
+enum signal_index {
+ SIGNAL_NEW_ENTRY,
+ SIGNAL_ENTRY_REMOVED,
+ SIGNAL_MAX
+};
+
+enum entry_signal_index {
+ ENTRY_SIGNAL_DEVICE_UPDATED,
+ ENTRY_SIGNAL_VOLUME_UPDATED,
+ ENTRY_SIGNAL_MUTE_UPDATED,
+ ENTRY_SIGNAL_MAX
+};
+
+static pa_dbus_arg_info new_entry_args[] = { { "entry", "o", NULL } };
+static pa_dbus_arg_info entry_removed_args[] = { { "entry", "o", NULL } };
+
+static pa_dbus_arg_info entry_device_updated_args[] = { { "device", "s", NULL } };
+static pa_dbus_arg_info entry_volume_updated_args[] = { { "volume", "a(uu)", NULL } };
+static pa_dbus_arg_info entry_mute_updated_args[] = { { "muted", "b", NULL } };
+
+static pa_dbus_signal_info signals[SIGNAL_MAX] = {
+ [SIGNAL_NEW_ENTRY] = { .name = "NewEntry", .arguments = new_entry_args, .n_arguments = 1 },
+ [SIGNAL_ENTRY_REMOVED] = { .name = "EntryRemoved", .arguments = entry_removed_args, .n_arguments = 1 }
+};
+
+static pa_dbus_signal_info entry_signals[ENTRY_SIGNAL_MAX] = {
+ [ENTRY_SIGNAL_DEVICE_UPDATED] = { .name = "DeviceUpdated", .arguments = entry_device_updated_args, .n_arguments = 1 },
+ [ENTRY_SIGNAL_VOLUME_UPDATED] = { .name = "VolumeUpdated", .arguments = entry_volume_updated_args, .n_arguments = 1 },
+ [ENTRY_SIGNAL_MUTE_UPDATED] = { .name = "MuteUpdated", .arguments = entry_mute_updated_args, .n_arguments = 1 }
+};
+
+static pa_dbus_interface_info stream_restore_interface_info = {
+ .name = INTERFACE_STREAM_RESTORE,
+ .method_handlers = method_handlers,
+ .n_method_handlers = METHOD_HANDLER_MAX,
+ .property_handlers = property_handlers,
+ .n_property_handlers = PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_get_all,
+ .signals = signals,
+ .n_signals = SIGNAL_MAX
+};
+
+static pa_dbus_interface_info entry_interface_info = {
+ .name = INTERFACE_ENTRY,
+ .method_handlers = entry_method_handlers,
+ .n_method_handlers = ENTRY_METHOD_HANDLER_MAX,
+ .property_handlers = entry_property_handlers,
+ .n_property_handlers = ENTRY_PROPERTY_HANDLER_MAX,
+ .get_all_properties_cb = handle_entry_get_all,
+ .signals = entry_signals,
+ .n_signals = ENTRY_SIGNAL_MAX
+};
+
+static struct dbus_entry *dbus_entry_new(struct userdata *u, const char *entry_name) {
+ struct dbus_entry *de;
+
+ pa_assert(u);
+ pa_assert(entry_name);
+ pa_assert(*entry_name);
+
+ de = pa_xnew(struct dbus_entry, 1);
+ de->userdata = u;
+ de->entry_name = pa_xstrdup(entry_name);
+ de->index = u->next_index++;
+ de->object_path = pa_sprintf_malloc("%s/%s%u", OBJECT_PATH, ENTRY_OBJECT_NAME, de->index);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(u->dbus_protocol, de->object_path, &entry_interface_info, de) >= 0);
+
+ return de;
+}
+
+static void dbus_entry_free(struct dbus_entry *de) {
+ pa_assert(de);
+
+ pa_assert_se(pa_dbus_protocol_remove_interface(de->userdata->dbus_protocol, de->object_path, entry_interface_info.name) >= 0);
+
+ pa_xfree(de->entry_name);
+ pa_xfree(de->object_path);
+}
+
+/* Reads an array [(UInt32, UInt32)] from the iterator. The struct items are
+ * are a channel position and a volume value, respectively. The result is
+ * stored in the map and vol arguments. The iterator must point to a "a(uu)"
+ * element. If the data is invalid, an error reply is sent and a negative
+ * number is returned. In case of a failure we make no guarantees about the
+ * state of map and vol. In case of an empty array the channels field of both
+ * map and vol are set to 0. This function calls dbus_message_iter_next(iter)
+ * before returning. */
+static int get_volume_arg(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, pa_channel_map *map, pa_cvolume *vol) {
+ DBusMessageIter array_iter;
+ DBusMessageIter struct_iter;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(pa_streq(dbus_message_iter_get_signature(iter), "a(uu)"));
+ pa_assert(map);
+ pa_assert(vol);
+
+ pa_channel_map_init(map);
+ pa_cvolume_init(vol);
+
+ map->channels = 0;
+ vol->channels = 0;
+
+ dbus_message_iter_recurse(iter, &array_iter);
+
+ while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID) {
+ dbus_uint32_t chan_pos;
+ dbus_uint32_t chan_vol;
+
+ dbus_message_iter_recurse(&array_iter, &struct_iter);
+
+ dbus_message_iter_get_basic(&struct_iter, &chan_pos);
+
+ if (chan_pos >= PA_CHANNEL_POSITION_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid channel position: %u", chan_pos);
+ return -1;
+ }
+
+ pa_assert_se(dbus_message_iter_next(&struct_iter));
+ dbus_message_iter_get_basic(&struct_iter, &chan_vol);
+
+ if (!PA_VOLUME_IS_VALID(chan_vol)) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid volume: %u", chan_vol);
+ return -1;
+ }
+
+ if (map->channels < PA_CHANNELS_MAX) {
+ map->map[map->channels] = chan_pos;
+ vol->values[map->channels] = chan_vol;
+ }
+ ++map->channels;
+ ++vol->channels;
+
+ dbus_message_iter_next(&array_iter);
+ }
+
+ if (map->channels > PA_CHANNELS_MAX) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Too many channels: %u. The maximum is %u.", map->channels, PA_CHANNELS_MAX);
+ return -1;
+ }
+
+ dbus_message_iter_next(iter);
+
+ return 0;
+}
+
+static void append_volume(DBusMessageIter *iter, struct entry *e) {
+ DBusMessageIter array_iter;
+ DBusMessageIter struct_iter;
+ unsigned i;
+
+ pa_assert(iter);
+ pa_assert(e);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "(uu)", &array_iter));
+
+ if (!e->volume_valid) {
+ pa_assert_se(dbus_message_iter_close_container(iter, &array_iter));
+ return;
+ }
+
+ for (i = 0; i < e->channel_map.channels; ++i) {
+ pa_assert_se(dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter));
+
+ pa_assert_se(dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_UINT32, &e->channel_map.map[i]));
+ pa_assert_se(dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_UINT32, &e->volume.values[i]));
+
+ pa_assert_se(dbus_message_iter_close_container(&array_iter, &struct_iter));
+ }
+
+ pa_assert_se(dbus_message_iter_close_container(iter, &array_iter));
+}
+
+static void append_volume_variant(DBusMessageIter *iter, struct entry *e) {
+ DBusMessageIter variant_iter;
+
+ pa_assert(iter);
+ pa_assert(e);
+
+ pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a(uu)", &variant_iter));
+
+ append_volume(&variant_iter, e);
+
+ pa_assert_se(dbus_message_iter_close_container(iter, &variant_iter));
+}
+
+static void send_new_entry_signal(struct dbus_entry *entry) {
+ DBusMessage *signal_msg;
+
+ pa_assert(entry);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(OBJECT_PATH, INTERFACE_STREAM_RESTORE, signals[SIGNAL_NEW_ENTRY].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &entry->object_path, DBUS_TYPE_INVALID));
+ pa_dbus_protocol_send_signal(entry->userdata->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+}
+
+static void send_entry_removed_signal(struct dbus_entry *entry) {
+ DBusMessage *signal_msg;
+
+ pa_assert(entry);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(OBJECT_PATH, INTERFACE_STREAM_RESTORE, signals[SIGNAL_ENTRY_REMOVED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &entry->object_path, DBUS_TYPE_INVALID));
+ pa_dbus_protocol_send_signal(entry->userdata->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+}
+
+static void send_device_updated_signal(struct dbus_entry *de, struct entry *e) {
+ DBusMessage *signal_msg;
+ const char *device;
+
+ pa_assert(de);
+ pa_assert(e);
+
+ device = e->device_valid ? e->device : "";
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(de->object_path, INTERFACE_ENTRY, entry_signals[ENTRY_SIGNAL_DEVICE_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_STRING, &device, DBUS_TYPE_INVALID));
+ pa_dbus_protocol_send_signal(de->userdata->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+}
+
+static void send_volume_updated_signal(struct dbus_entry *de, struct entry *e) {
+ DBusMessage *signal_msg;
+ DBusMessageIter msg_iter;
+
+ pa_assert(de);
+ pa_assert(e);
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(de->object_path, INTERFACE_ENTRY, entry_signals[ENTRY_SIGNAL_VOLUME_UPDATED].name));
+ dbus_message_iter_init_append(signal_msg, &msg_iter);
+ append_volume(&msg_iter, e);
+ pa_dbus_protocol_send_signal(de->userdata->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+}
+
+static void send_mute_updated_signal(struct dbus_entry *de, struct entry *e) {
+ DBusMessage *signal_msg;
+ dbus_bool_t muted;
+
+ pa_assert(de);
+ pa_assert(e);
+
+ pa_assert(e->muted_valid);
+
+ muted = e->muted;
+
+ pa_assert_se(signal_msg = dbus_message_new_signal(de->object_path, INTERFACE_ENTRY, entry_signals[ENTRY_SIGNAL_MUTE_UPDATED].name));
+ pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_BOOLEAN, &muted, DBUS_TYPE_INVALID));
+ pa_dbus_protocol_send_signal(de->userdata->dbus_protocol, signal_msg);
+ dbus_message_unref(signal_msg);
+}
+
+static void handle_get_interface_revision(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ dbus_uint32_t interface_revision = DBUS_INTERFACE_REVISION;
+
+ pa_assert(conn);
+ pa_assert(msg);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &interface_revision);
+}
+
+/* The caller frees the array, but not the strings. */
+static const char **get_entries(struct userdata *u, unsigned *n) {
+ const char **entries;
+ unsigned i = 0;
+ void *state = NULL;
+ struct dbus_entry *de;
+
+ pa_assert(u);
+ pa_assert(n);
+
+ *n = pa_hashmap_size(u->dbus_entries);
+
+ if (*n == 0)
+ return NULL;
+
+ entries = pa_xnew(const char *, *n);
+
+ PA_HASHMAP_FOREACH(de, u->dbus_entries, state)
+ entries[i++] = de->object_path;
+
+ return entries;
+}
+
+static void handle_get_entries(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct userdata *u = userdata;
+ const char **entries;
+ unsigned n;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+
+ entries = get_entries(u, &n);
+
+ pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, entries, n);
+
+ pa_xfree(entries);
+}
+
+static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct userdata *u = userdata;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ dbus_uint32_t interface_revision;
+ const char **entries;
+ unsigned n_entries;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+
+ interface_revision = DBUS_INTERFACE_REVISION;
+ entries = get_entries(u, &n_entries);
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INTERFACE_REVISION].property_name, DBUS_TYPE_UINT32, &interface_revision);
+ pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_ENTRIES].property_name, DBUS_TYPE_OBJECT_PATH, entries, n_entries);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+
+ pa_xfree(entries);
+}
+
+static void handle_add_entry(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct userdata *u = userdata;
+ DBusMessageIter msg_iter;
+ const char *name = NULL;
+ const char *device = NULL;
+ pa_channel_map map;
+ pa_cvolume vol;
+ dbus_bool_t muted = FALSE;
+ dbus_bool_t apply_immediately = FALSE;
+ struct dbus_entry *dbus_entry = NULL;
+ struct entry *e = NULL;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+
+ pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &name);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &device);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ if (get_volume_arg(conn, msg, &msg_iter, &map, &vol) < 0)
+ return;
+
+ dbus_message_iter_get_basic(&msg_iter, &muted);
+
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &apply_immediately);
+
+ if (!*name) {
+ pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "An empty string was given as the entry name.");
+ return;
+ }
+
+ if ((dbus_entry = pa_hashmap_get(u->dbus_entries, name))) {
+ pa_bool_t mute_updated = FALSE;
+ pa_bool_t volume_updated = FALSE;
+ pa_bool_t device_updated = FALSE;
+
+ pa_assert_se(e = entry_read(u, name));
+ mute_updated = e->muted != muted;
+ e->muted = muted;
+ e->muted_valid = TRUE;
+
+ volume_updated = (e->volume_valid != !!map.channels) || !pa_cvolume_equal(&e->volume, &vol);
+ e->volume = vol;
+ e->channel_map = map;
+ e->volume_valid = !!map.channels;
+
+ device_updated = (e->device_valid != !!device[0]) || !pa_streq(e->device, device);
+ pa_xfree(e->device);
+ e->device = pa_xstrdup(device);
+ e->device_valid = !!device[0];
+
+ if (mute_updated)
+ send_mute_updated_signal(dbus_entry, e);
+ if (volume_updated)
+ send_volume_updated_signal(dbus_entry, e);
+ if (device_updated)
+ send_device_updated_signal(dbus_entry, e);
+
+ } else {
+ dbus_entry = dbus_entry_new(u, name);
+ pa_assert_se(pa_hashmap_put(u->dbus_entries, dbus_entry->entry_name, dbus_entry) == 0);
+
+ e = entry_new();
+ e->muted_valid = TRUE;
+ e->volume_valid = !!map.channels;
+ e->device_valid = !!device[0];
+ e->muted = muted;
+ e->volume = vol;
+ e->channel_map = map;
+ e->device = pa_xstrdup(device);
+
+ send_new_entry_signal(dbus_entry);
+ }
+
+ pa_assert_se(entry_write(u, name, e, TRUE));
+
+ if (apply_immediately)
+ entry_apply(u, name, e);
+
+ trigger_save(u);
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ entry_free(e);
+}
+
+static void handle_get_entry_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct userdata *u = userdata;
+ const char *name;
+ struct dbus_entry *de;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(u);
+
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
+
+ if (!(de = pa_hashmap_get(u->dbus_entries, name))) {
+ pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "No such stream restore entry.");
+ return;
+ }
+
+ pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &de->object_path);
+}
+
+static void handle_entry_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct dbus_entry *de = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(de);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &de->index);
+}
+
+static void handle_entry_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct dbus_entry *de = userdata;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(de);
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &de->entry_name);
+}
+
+static void handle_entry_get_device(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct dbus_entry *de = userdata;
+ struct entry *e;
+ const char *device;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(de);
+
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
+
+ device = e->device_valid ? e->device : "";
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &device);
+
+ entry_free(e);
+}
+
+static void handle_entry_set_device(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ struct dbus_entry *de = userdata;
+ const char *device;
+ struct entry *e;
+ pa_bool_t updated;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(de);
+
+ dbus_message_iter_get_basic(iter, &device);
+
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
+
+ updated = (e->device_valid != !!device[0]) || !pa_streq(e->device, device);
+
+ if (updated) {
+ pa_xfree(e->device);
+ e->device = pa_xstrdup(device);
+ e->device_valid = !!device[0];
+
+ pa_assert_se(entry_write(de->userdata, de->entry_name, e, TRUE));
+
+ entry_apply(de->userdata, de->entry_name, e);
+ send_device_updated_signal(de, e);
+ trigger_save(de->userdata);
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ entry_free(e);
+}
+
+static void handle_entry_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct dbus_entry *de = userdata;
+ DBusMessage *reply;
+ DBusMessageIter msg_iter;
+ struct entry *e;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(de);
+
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
+
+ pa_assert_se(reply = dbus_message_new_method_return(msg));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ append_volume_variant(&msg_iter, e);
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ entry_free(e);
+}
+
+static void handle_entry_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ struct dbus_entry *de = userdata;
+ pa_channel_map map;
+ pa_cvolume vol;
+ struct entry *e = NULL;
+ pa_bool_t updated = FALSE;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(de);
+
+ if (get_volume_arg(conn, msg, iter, &map, &vol) < 0)
+ return;
+
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
+
+ updated = (e->volume_valid != !!map.channels) || !pa_cvolume_equal(&e->volume, &vol);
+
+ if (updated) {
+ e->volume = vol;
+ e->channel_map = map;
+ e->volume_valid = !!map.channels;
+
+ pa_assert_se(entry_write(de->userdata, de->entry_name, e, TRUE));
+
+ entry_apply(de->userdata, de->entry_name, e);
+ send_volume_updated_signal(de, e);
+ trigger_save(de->userdata);
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ entry_free(e);
+}
+
+static void handle_entry_get_mute(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct dbus_entry *de = userdata;
+ struct entry *e;
+ dbus_bool_t mute;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(de);
+
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
+
+ mute = e->muted_valid ? e->muted : FALSE;
+
+ pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &mute);
+
+ entry_free(e);
+}
+
+static void handle_entry_set_mute(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
+ struct dbus_entry *de = userdata;
+ dbus_bool_t mute;
+ struct entry *e;
+ pa_bool_t updated;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(iter);
+ pa_assert(de);
+
+ dbus_message_iter_get_basic(iter, &mute);
+
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
+
+ updated = !e->muted_valid || e->muted != mute;
+
+ if (updated) {
+ e->muted = mute;
+ e->muted_valid = TRUE;
+
+ pa_assert_se(entry_write(de->userdata, de->entry_name, e, TRUE));
+
+ entry_apply(de->userdata, de->entry_name, e);
+ send_mute_updated_signal(de, e);
+ trigger_save(de->userdata);
+ }
+
+ pa_dbus_send_empty_reply(conn, msg);
+
+ entry_free(e);
+}
+
+static void handle_entry_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct dbus_entry *de = userdata;
+ struct entry *e;
+ DBusMessage *reply = NULL;
+ DBusMessageIter msg_iter;
+ DBusMessageIter dict_iter;
+ DBusMessageIter dict_entry_iter;
+ const char *device;
+ dbus_bool_t mute;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(de);
+
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
+
+ device = e->device_valid ? e->device : "";
+ mute = e->muted_valid ? e->muted : FALSE;
+
+ pa_assert_se((reply = dbus_message_new_method_return(msg)));
+
+ dbus_message_iter_init_append(reply, &msg_iter);
+ pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, entry_property_handlers[ENTRY_PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &de->index);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, entry_property_handlers[ENTRY_PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &de->entry_name);
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, entry_property_handlers[ENTRY_PROPERTY_HANDLER_DEVICE].property_name, DBUS_TYPE_STRING, &device);
+
+ pa_assert_se(dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
+
+ pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &entry_property_handlers[ENTRY_PROPERTY_HANDLER_VOLUME].property_name));
+ append_volume_variant(&dict_entry_iter, e);
+
+ pa_assert_se(dbus_message_iter_close_container(&dict_iter, &dict_entry_iter));
+
+ pa_dbus_append_basic_variant_dict_entry(&dict_iter, entry_property_handlers[ENTRY_PROPERTY_HANDLER_MUTE].property_name, DBUS_TYPE_BOOLEAN, &mute);
+
+ pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
+
+ pa_assert_se(dbus_connection_send(conn, reply, NULL));
+
+ dbus_message_unref(reply);
+
+ entry_free(e);
+}
+
+static void handle_entry_remove(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+ struct dbus_entry *de = userdata;
+ pa_datum key;
+
+ pa_assert(conn);
+ pa_assert(msg);
+ pa_assert(de);
+
+ key.data = de->entry_name;
+ key.size = strlen(de->entry_name);
+
+ pa_assert_se(pa_database_unset(de->userdata->database, &key) == 0);
+
+ send_entry_removed_signal(de);
+ trigger_save(de->userdata);
+
+ pa_assert_se(pa_hashmap_remove(de->userdata->dbus_entries, de->entry_name));
+ dbus_entry_free(de);
+
+ pa_dbus_send_empty_reply(conn, msg);
+}
+
+#endif /* HAVE_DBUS */
+
+static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(a);
+ pa_assert(e);
+ pa_assert(u);
+
+ pa_assert(e == u->save_time_event);
+ u->core->mainloop->time_free(u->save_time_event);
+ u->save_time_event = NULL;
+
+ pa_database_sync(u->database);
+ pa_log_info("Synced.");
+}
+
+static char *get_name(pa_proplist *p, const char *prefix) {
+ const char *r;
+ char *t;
+
+ if (!p)
+ return NULL;
+
+ if ((r = pa_proplist_gets(p, IDENTIFICATION_PROPERTY)))
+ return pa_xstrdup(r);
+
+ if ((r = pa_proplist_gets(p, PA_PROP_MEDIA_ROLE)))
+ t = pa_sprintf_malloc("%s-by-media-role:%s", prefix, r);
+ else if ((r = pa_proplist_gets(p, PA_PROP_APPLICATION_ID)))
+ t = pa_sprintf_malloc("%s-by-application-id:%s", prefix, r);
+ else if ((r = pa_proplist_gets(p, PA_PROP_APPLICATION_NAME)))
+ t = pa_sprintf_malloc("%s-by-application-name:%s", prefix, r);
+ else if ((r = pa_proplist_gets(p, PA_PROP_MEDIA_NAME)))
+ t = pa_sprintf_malloc("%s-by-media-name:%s", prefix, r);
+ else
+ t = pa_sprintf_malloc("%s-fallback:%s", prefix, r);
+
+ pa_proplist_sets(p, IDENTIFICATION_PROPERTY, t);
+ return t;
+}
+
+static struct entry* entry_new(void) {
+ struct entry *r = pa_xnew0(struct entry, 1);
+ r->version = ENTRY_VERSION;
+ return r;
+}
+
+static void entry_free(struct entry* e) {
+ pa_assert(e);
+
+ pa_xfree(e->device);
+ pa_xfree(e->card);
+ pa_xfree(e);
+}
+
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e, pa_bool_t replace) {
+ pa_tagstruct *t;
+ pa_datum key, data;
+ pa_bool_t r;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu8(t, e->version);
+ pa_tagstruct_put_boolean(t, e->volume_valid);
+ pa_tagstruct_put_channel_map(t, &e->channel_map);
+ pa_tagstruct_put_cvolume(t, &e->volume);
+ pa_tagstruct_put_boolean(t, e->muted_valid);
+ pa_tagstruct_put_boolean(t, e->muted);
+ pa_tagstruct_put_boolean(t, e->device_valid);
+ pa_tagstruct_puts(t, e->device);
+ pa_tagstruct_put_boolean(t, e->card_valid);
+ pa_tagstruct_puts(t, e->card);
+
+ key.data = (char *) name;
+ key.size = strlen(name);
+
+ data.data = (void*)pa_tagstruct_data(t, &data.size);
+
+ r = (pa_database_set(u->database, &key, &data, replace) == 0);
+
+ pa_tagstruct_free(t);
+
+ return r;
+}
+
+#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
+
+#define LEGACY_ENTRY_VERSION 3
+static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) {
+ struct legacy_entry {
+ uint8_t version;
+ pa_bool_t muted_valid:1, volume_valid:1, device_valid:1, card_valid:1;
+ pa_bool_t muted:1;
+ pa_channel_map channel_map;
+ pa_cvolume volume;
+ char device[PA_NAME_MAX];
+ char card[PA_NAME_MAX];
+ } PA_GCC_PACKED;
+ struct legacy_entry *le;
+ struct entry *e;
+
+ pa_assert(u);
+ pa_assert(data);
+
+ if (data->size != sizeof(struct legacy_entry)) {
+ pa_log_debug("Size does not match.");
+ return NULL;
+ }
+
+ le = (struct legacy_entry*)data->data;
+
+ if (le->version != LEGACY_ENTRY_VERSION) {
+ pa_log_debug("Version mismatch.");
+ return NULL;
+ }
+
+ if (!memchr(le->device, 0, sizeof(le->device))) {
+ pa_log_warn("Device has missing NUL byte.");
+ return NULL;
+ }
+
+ if (!memchr(le->card, 0, sizeof(le->card))) {
+ pa_log_warn("Card has missing NUL byte.");
+ return NULL;
+ }
+
+ e = entry_new();
+ e->card = pa_xstrdup(le->card);
+ return e;
+}
+#endif
+
+static struct entry *entry_read(struct userdata *u, const char *name) {
+ pa_datum key, data;
+ struct entry *e = NULL;
+ pa_tagstruct *t = NULL;
+ const char *device, *card;
+
+ pa_assert(u);
+ pa_assert(name);
+
+ key.data = (char*) name;
+ key.size = strlen(name);
+
+ pa_zero(data);
+
+ if (!pa_database_get(u->database, &key, &data))
+ goto fail;
+
+ t = pa_tagstruct_new(data.data, data.size);
+ e = entry_new();
+
+ if (pa_tagstruct_getu8(t, &e->version) < 0 ||
+ e->version > ENTRY_VERSION ||
+ pa_tagstruct_get_boolean(t, &e->volume_valid) < 0 ||
+ pa_tagstruct_get_channel_map(t, &e->channel_map) < 0 ||
+ pa_tagstruct_get_cvolume(t, &e->volume) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->muted_valid) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->muted) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->device_valid) < 0 ||
+ pa_tagstruct_gets(t, &device) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->card_valid) < 0 ||
+ pa_tagstruct_gets(t, &card) < 0) {
+
+ goto fail;
+ }
+
+ e->device = pa_xstrdup(device);
+ e->card = pa_xstrdup(card);
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ if (e->device_valid && !pa_namereg_is_valid_name(e->device)) {
+ pa_log_warn("Invalid device name stored in database for stream %s", name);
+ goto fail;
+ }
+
+ if (e->card_valid && !pa_namereg_is_valid_name(e->card)) {
+ pa_log_warn("Invalid card name stored in database for stream %s", name);
+ goto fail;
+ }
+
+ if (e->volume_valid && !pa_channel_map_valid(&e->channel_map)) {
+ pa_log_warn("Invalid channel map stored in database for stream %s", name);
+ goto fail;
+ }
+
+ if (e->volume_valid && (!pa_cvolume_valid(&e->volume) || !pa_cvolume_compatible_with_channel_map(&e->volume, &e->channel_map))) {
+ pa_log_warn("Invalid volume stored in database for stream %s", name);
+ goto fail;
+ }
+
+ pa_tagstruct_free(t);
+ pa_datum_free(&data);
+
+ return e;
+
+fail:
+
+ pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
+
+ if (e)
+ entry_free(e);
+ if (t)
+ pa_tagstruct_free(t);
+
+#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
+ pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name);
+ if ((e = legacy_entry_read(u, &data))) {
+ pa_log_debug("Success. Saving new format for key: %s", name);
+ if (entry_write(u, name, e, TRUE))
+ trigger_save(u);
+ pa_datum_free(&data);
+ return e;
+ } else
+ pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name);
+#endif
+
+ pa_datum_free(&data);
+ return NULL;
+}
+
+static struct entry* entry_copy(const struct entry *e) {
+ struct entry* r;
+
+ pa_assert(e);
+ r = entry_new();
+ *r = *e;
+ r->device = pa_xstrdup(e->device);
+ r->card = pa_xstrdup(e->card);
+ return r;
+}
+
+static void trigger_save(struct userdata *u) {
+ pa_native_connection *c;
+ uint32_t idx;
+
+ for (c = pa_idxset_first(u->subscribed, &idx); c; c = pa_idxset_next(u->subscribed, &idx)) {
+ pa_tagstruct *t;
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(t, PA_COMMAND_EXTENSION);
+ pa_tagstruct_putu32(t, 0);
+ pa_tagstruct_putu32(t, u->module->index);
+ pa_tagstruct_puts(t, u->module->name);
+ pa_tagstruct_putu32(t, SUBCOMMAND_EVENT);
+
+ pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), t);
+ }
+
+ if (u->save_time_event)
+ return;
+
+ u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
+}
+
+static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) {
+ pa_cvolume t;
+
+ pa_assert(a);
+ pa_assert(b);
+
+ if (a->device_valid != b->device_valid ||
+ (a->device_valid && !pa_streq(a->device, b->device)))
+ return FALSE;
+
+ if (a->card_valid != b->card_valid ||
+ (a->card_valid && !pa_streq(a->card, b->card)))
+ return FALSE;
+
+ if (a->muted_valid != b->muted_valid ||
+ (a->muted_valid && (a->muted != b->muted)))
+ return FALSE;
+
+ t = b->volume;
+ if (a->volume_valid != b->volume_valid ||
+ (a->volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->volume)))
+ return FALSE;
+
+ return TRUE;
+}
+
+static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+ struct userdata *u = userdata;
+ struct entry *entry, *old = NULL;
+ char *name = NULL;
+
+ /* These are only used when D-Bus is enabled, but in order to reduce ifdef
+ * clutter these are defined here unconditionally. */
+ pa_bool_t created_new_entry = TRUE;
+ pa_bool_t device_updated = FALSE;
+ pa_bool_t volume_updated = FALSE;
+ pa_bool_t mute_updated = FALSE;
+
+#ifdef HAVE_DBUS
+ struct dbus_entry *de = NULL;
+#endif
+
+ pa_assert(c);
+ pa_assert(u);
+
+ if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
+ t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE) &&
+ t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
+ t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
+ return;
+
+ if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
+ pa_sink_input *sink_input;
+
+ if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, idx)))
+ return;
+
+ if (!(name = get_name(sink_input->proplist, "sink-input")))
+ return;
+
+ if ((old = entry_read(u, name))) {
+ entry = entry_copy(old);
+ created_new_entry = FALSE;
+ } else
+ entry = entry_new();
+
+ if (sink_input->save_volume && pa_sink_input_is_volume_readable(sink_input)) {
+ pa_assert(sink_input->volume_writable);
+
+ entry->channel_map = sink_input->channel_map;
+ pa_sink_input_get_volume(sink_input, &entry->volume, FALSE);
+ entry->volume_valid = TRUE;
+
+ volume_updated = !created_new_entry
+ && (!old->volume_valid
+ || !pa_channel_map_equal(&entry->channel_map, &old->channel_map)
+ || !pa_cvolume_equal(&entry->volume, &old->volume));
+ }
+
+ if (sink_input->save_muted) {
+ entry->muted = pa_sink_input_get_mute(sink_input);
+ entry->muted_valid = TRUE;
+
+ mute_updated = !created_new_entry && (!old->muted_valid || entry->muted != old->muted);
+ }
+
+ if (sink_input->save_sink) {
+ pa_xfree(entry->device);
+ entry->device = pa_xstrdup(sink_input->sink->name);
+ entry->device_valid = TRUE;
+
+ device_updated = !created_new_entry && (!old->device_valid || !pa_streq(entry->device, old->device));
+ if (sink_input->sink->card) {
+ pa_xfree(entry->card);
+ entry->card = pa_xstrdup(sink_input->sink->card->name);
+ entry->card_valid = TRUE;
+ }
+ }
+
+ } else {
+ pa_source_output *source_output;
+
+ pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT);
+
+ if (!(source_output = pa_idxset_get_by_index(c->source_outputs, idx)))
+ return;
+
+ if (!(name = get_name(source_output->proplist, "source-output")))
+ return;
+
+ if ((old = entry_read(u, name))) {
+ entry = entry_copy(old);
+ created_new_entry = FALSE;
+ } else
+ entry = entry_new();
+
+ if (source_output->save_source) {
+ pa_xfree(entry->device);
+ entry->device = pa_xstrdup(source_output->source->name);
+ entry->device_valid = TRUE;
+
+ device_updated = !created_new_entry && (!old->device_valid || !pa_streq(entry->device, old->device));
+
+ if (source_output->source->card) {
+ pa_xfree(entry->card);
+ entry->card = pa_xstrdup(source_output->source->card->name);
+ entry->card_valid = TRUE;
+ }
+ }
+ }
+
+ pa_assert(entry);
+
+ if (old) {
+
+ if (entries_equal(old, entry)) {
+ entry_free(old);
+ entry_free(entry);
+ pa_xfree(name);
+ return;
+ }
+
+ entry_free(old);
+ }
+
+ pa_log_info("Storing volume/mute/device for stream %s.", name);
+
+ if (entry_write(u, name, entry, TRUE))
+ trigger_save(u);
+
+#ifdef HAVE_DBUS
+ if (created_new_entry) {
+ de = dbus_entry_new(u, name);
+ pa_assert_se(pa_hashmap_put(u->dbus_entries, de->entry_name, de) == 0);
+ send_new_entry_signal(de);
+ } else {
+ pa_assert_se(de = pa_hashmap_get(u->dbus_entries, name));
+
+ if (device_updated)
+ send_device_updated_signal(de, entry);
+ if (volume_updated)
+ send_volume_updated_signal(de, entry);
+ if (mute_updated)
+ send_mute_updated_signal(de, entry);
+ }
+#endif
+
+ entry_free(entry);
+ pa_xfree(name);
+}
+
+static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
+ char *name;
+ struct entry *e;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+ pa_assert(u->restore_device);
+
+ if (!(name = get_name(new_data->proplist, "sink-input")))
+ return PA_HOOK_OK;
+
+ if (new_data->sink)
+ pa_log_debug("Not restoring device for stream %s, because already set to '%s'.", name, new_data->sink->name);
+ else if ((e = entry_read(u, name))) {
+ pa_sink *s = NULL;
+
+ if (e->device_valid)
+ s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK);
+
+ if (!s && e->card_valid) {
+ pa_card *card;
+
+ if ((card = pa_namereg_get(c, e->card, PA_NAMEREG_CARD)))
+ s = pa_idxset_first(card->sinks, NULL);
+ }
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (s && PA_SINK_IS_LINKED(pa_sink_get_state(s)))
+ if (pa_sink_input_new_data_set_sink(new_data, s, TRUE))
+ pa_log_info("Restoring device for stream %s.", name);
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
+ char *name;
+ struct entry *e;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+ pa_assert(u->restore_volume || u->restore_muted);
+
+ if (!(name = get_name(new_data->proplist, "sink-input")))
+ return PA_HOOK_OK;
+
+ if ((e = entry_read(u, name))) {
+
+ if (u->restore_volume && e->volume_valid) {
+ if (!new_data->volume_writable)
+ pa_log_debug("Not restoring volume for sink input %s, because its volume can't be changed.", name);
+ else if (new_data->volume_is_set)
+ pa_log_debug("Not restoring volume for sink input %s, because already set.", name);
+ else {
+ pa_cvolume v;
+
+ pa_log_info("Restoring volume for sink input %s.", name);
+
+ v = e->volume;
+ pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map);
+ pa_sink_input_new_data_set_volume(new_data, &v);
+
+ new_data->volume_is_absolute = FALSE;
+ new_data->save_volume = TRUE;
+ }
+ }
+
+ if (u->restore_muted && e->muted_valid) {
+
+ if (!new_data->muted_is_set) {
+ pa_log_info("Restoring mute state for sink input %s.", name);
+ pa_sink_input_new_data_set_muted(new_data, e->muted);
+ new_data->save_muted = TRUE;
+ } else
+ pa_log_debug("Not restoring mute state for sink input %s, because already set.", name);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
+ char *name;
+ struct entry *e;
+
+ pa_assert(c);
+ pa_assert(new_data);
+ pa_assert(u);
+ pa_assert(u->restore_device);
+
+ if (new_data->direct_on_input)
+ return PA_HOOK_OK;
+
+ if (!(name = get_name(new_data->proplist, "source-output")))
+ return PA_HOOK_OK;
+
+ if (new_data->source)
+ pa_log_debug("Not restoring device for stream %s, because already set", name);
+ else if ((e = entry_read(u, name))) {
+ pa_source *s = NULL;
+
+ if (e->device_valid)
+ s = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE);
+
+ if (!s && e->card_valid) {
+ pa_card *card;
+
+ if ((card = pa_namereg_get(c, e->card, PA_NAMEREG_CARD)))
+ s = pa_idxset_first(card->sources, NULL);
+ }
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (s && PA_SOURCE_IS_LINKED(pa_source_get_state(s))) {
+ pa_log_info("Restoring device for stream %s.", name);
+ pa_source_output_new_data_set_source(new_data, s, TRUE);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
+ pa_sink_input *si;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(sink);
+ pa_assert(u);
+ pa_assert(u->on_hotplug && u->restore_device);
+
+ PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
+ char *name;
+ struct entry *e;
+
+ if (si->sink == sink)
+ continue;
+
+ if (si->save_sink)
+ continue;
+
+ /* Skip this if it is already in the process of being moved
+ * anyway */
+ if (!si->sink)
+ continue;
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
+ continue;
+
+ if (!(name = get_name(si->proplist, "sink-input")))
+ continue;
+
+ if ((e = entry_read(u, name))) {
+ if (e->device_valid && pa_streq(e->device, sink->name))
+ pa_sink_input_move_to(si, sink, TRUE);
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
+ pa_source_output *so;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(source);
+ pa_assert(u);
+ pa_assert(u->on_hotplug && u->restore_device);
+
+ PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
+ char *name;
+ struct entry *e;
+
+ if (so->source == source)
+ continue;
+
+ if (so->save_source)
+ continue;
+
+ if (so->direct_on_input)
+ continue;
+
+ /* Skip this if it is already in the process of being moved anyway */
+ if (!so->source)
+ continue;
+
+ /* It might happen that a stream and a source are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
+ continue;
+
+ if (!(name = get_name(so->proplist, "source-output")))
+ continue;
+
+ if ((e = entry_read(u, name))) {
+ if (e->device_valid && pa_streq(e->device, source->name))
+ pa_source_output_move_to(so, source, TRUE);
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
+ pa_sink_input *si;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(sink);
+ pa_assert(u);
+ pa_assert(u->on_rescue && u->restore_device);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ PA_IDXSET_FOREACH(si, sink->inputs, idx) {
+ char *name;
+ struct entry *e;
+
+ if (!si->sink)
+ continue;
+
+ if (!(name = get_name(si->proplist, "sink-input")))
+ continue;
+
+ if ((e = entry_read(u, name))) {
+
+ if (e->device_valid) {
+ pa_sink *d;
+
+ if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SINK)) &&
+ d != sink &&
+ PA_SINK_IS_LINKED(pa_sink_get_state(d)))
+ pa_sink_input_move_to(si, d, TRUE);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
+ pa_source_output *so;
+ uint32_t idx;
+
+ pa_assert(c);
+ pa_assert(source);
+ pa_assert(u);
+ pa_assert(u->on_rescue && u->restore_device);
+
+ /* There's no point in doing anything if the core is shut down anyway */
+ if (c->state == PA_CORE_SHUTDOWN)
+ return PA_HOOK_OK;
+
+ PA_IDXSET_FOREACH(so, source->outputs, idx) {
+ char *name;
+ struct entry *e;
+
+ if (so->direct_on_input)
+ continue;
+
+ if (!so->source)
+ continue;
+
+ if (!(name = get_name(so->proplist, "source-output")))
+ continue;
+
+ if ((e = entry_read(u, name))) {
+
+ if (e->device_valid) {
+ pa_source *d;
+
+ if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE)) &&
+ d != source &&
+ PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
+ pa_source_output_move_to(so, d, TRUE);
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+ }
+
+ return PA_HOOK_OK;
+}
+
+#define EXT_VERSION 1
+
+static void entry_apply(struct userdata *u, const char *name, struct entry *e) {
+ pa_sink_input *si;
+ pa_source_output *so;
+ uint32_t idx;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ PA_IDXSET_FOREACH(si, u->core->sink_inputs, idx) {
+ char *n;
+ pa_sink *s;
+
+ if (!(n = get_name(si->proplist, "sink-input")))
+ continue;
+
+ if (!pa_streq(name, n)) {
+ pa_xfree(n);
+ continue;
+ }
+ pa_xfree(n);
+
+ if (u->restore_volume && e->volume_valid && si->volume_writable) {
+ pa_cvolume v;
+
+ v = e->volume;
+ pa_log_info("Restoring volume for sink input %s.", name);
+ pa_cvolume_remap(&v, &e->channel_map, &si->channel_map);
+ pa_sink_input_set_volume(si, &v, TRUE, FALSE);
+ }
+
+ if (u->restore_muted && e->muted_valid) {
+ pa_log_info("Restoring mute state for sink input %s.", name);
+ pa_sink_input_set_mute(si, e->muted, TRUE);
+ }
+
+ if (u->restore_device) {
+ if (!e->device_valid) {
+ if (si->save_sink) {
+ pa_log_info("Ensuring device is not saved for stream %s.", name);
+ /* If the device is not valid we should make sure the
+ save flag is cleared as the user may have specifically
+ removed the sink element from the rule. */
+ si->save_sink = FALSE;
+ /* This is cheating a bit. The sink input itself has not changed
+ but the rules governing it's routing have, so we fire this event
+ such that other routing modules (e.g. module-device-manager)
+ will pick up the change and reapply their routing */
+ pa_subscription_post(si->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, si->index);
+ }
+ } else if ((s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SINK))) {
+ pa_log_info("Restoring device for stream %s.", name);
+ pa_sink_input_move_to(si, s, TRUE);
+ }
+ }
+ }
+
+ PA_IDXSET_FOREACH(so, u->core->source_outputs, idx) {
+ char *n;
+ pa_source *s;
+
+ if (!(n = get_name(so->proplist, "source-output")))
+ continue;
+
+ if (!pa_streq(name, n)) {
+ pa_xfree(n);
+ continue;
+ }
+ pa_xfree(n);
+
+ if (u->restore_device) {
+ if (!e->device_valid) {
+ if (so->save_source) {
+ pa_log_info("Ensuring device is not saved for stream %s.", name);
+ /* If the device is not valid we should make sure the
+ save flag is cleared as the user may have specifically
+ removed the source element from the rule. */
+ so->save_source = FALSE;
+ /* This is cheating a bit. The source output itself has not changed
+ but the rules governing it's routing have, so we fire this event
+ such that other routing modules (e.g. module-device-manager)
+ will pick up the change and reapply their routing */
+ pa_subscription_post(so->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, so->index);
+ }
+ } else if ((s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SOURCE))) {
+ pa_log_info("Restoring device for stream %s.", name);
+ pa_source_output_move_to(so, s, TRUE);
+ }
+ }
+ }
+}
+
+#ifdef DEBUG_VOLUME
+PA_GCC_UNUSED static void stream_restore_dump_database(struct userdata *u) {
+ pa_datum key;
+ pa_bool_t done;
+
+ done = !pa_database_first(u->database, &key, NULL);
+
+ while (!done) {
+ pa_datum next_key;
+ struct entry *e;
+ char *name;
+
+ done = !pa_database_next(u->database, &key, &next_key, NULL);
+
+ name = pa_xstrndup(key.data, key.size);
+ pa_datum_free(&key);
+
+ if ((e = entry_read(u, name))) {
+ char t[256];
+ pa_log("name=%s", name);
+ pa_log("device=%s %s", e->device, pa_yes_no(e->device_valid));
+ pa_log("channel_map=%s", pa_channel_map_snprint(t, sizeof(t), &e->channel_map));
+ pa_log("volume=%s %s", pa_cvolume_snprint(t, sizeof(t), &e->volume), pa_yes_no(e->volume_valid));
+ pa_log("mute=%s %s", pa_yes_no(e->muted), pa_yes_no(e->volume_valid));
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ key = next_key;
+ }
+}
+#endif
+
+static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connection *c, uint32_t tag, pa_tagstruct *t) {
+ struct userdata *u;
+ uint32_t command;
+ pa_tagstruct *reply = NULL;
+
+ pa_assert(p);
+ pa_assert(m);
+ pa_assert(c);
+ pa_assert(t);
+
+ u = m->userdata;
+
+ if (pa_tagstruct_getu32(t, &command) < 0)
+ goto fail;
+
+ reply = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
+ pa_tagstruct_putu32(reply, tag);
+
+ switch (command) {
+ case SUBCOMMAND_TEST: {
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ pa_tagstruct_putu32(reply, EXT_VERSION);
+ break;
+ }
+
+ case SUBCOMMAND_READ: {
+ pa_datum key;
+ pa_bool_t done;
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ done = !pa_database_first(u->database, &key, NULL);
+
+ while (!done) {
+ pa_datum next_key;
+ struct entry *e;
+ char *name;
+
+ done = !pa_database_next(u->database, &key, &next_key, NULL);
+
+ name = pa_xstrndup(key.data, key.size);
+ pa_datum_free(&key);
+
+ if ((e = entry_read(u, name))) {
+ pa_cvolume r;
+ pa_channel_map cm;
+
+ pa_tagstruct_puts(reply, name);
+ pa_tagstruct_put_channel_map(reply, e->volume_valid ? &e->channel_map : pa_channel_map_init(&cm));
+ pa_tagstruct_put_cvolume(reply, e->volume_valid ? &e->volume : pa_cvolume_init(&r));
+ pa_tagstruct_puts(reply, e->device_valid ? e->device : NULL);
+ pa_tagstruct_put_boolean(reply, e->muted_valid ? e->muted : FALSE);
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ key = next_key;
+ }
+
+ break;
+ }
+
+ case SUBCOMMAND_WRITE: {
+ uint32_t mode;
+ pa_bool_t apply_immediately = FALSE;
+
+ if (pa_tagstruct_getu32(t, &mode) < 0 ||
+ pa_tagstruct_get_boolean(t, &apply_immediately) < 0)
+ goto fail;
+
+ if (mode != PA_UPDATE_MERGE &&
+ mode != PA_UPDATE_REPLACE &&
+ mode != PA_UPDATE_SET)
+ goto fail;
+
+ if (mode == PA_UPDATE_SET) {
+#ifdef HAVE_DBUS
+ struct dbus_entry *de;
+ void *state = NULL;
+
+ PA_HASHMAP_FOREACH(de, u->dbus_entries, state) {
+ send_entry_removed_signal(de);
+ dbus_entry_free(pa_hashmap_remove(u->dbus_entries, de->entry_name));
+ }
+#endif
+ pa_database_clear(u->database);
+ }
+
+ while (!pa_tagstruct_eof(t)) {
+ const char *name, *device;
+ pa_bool_t muted;
+ struct entry *entry;
+#ifdef HAVE_DBUS
+ struct entry *old;
+#endif
+
+ entry = entry_new();
+
+ if (pa_tagstruct_gets(t, &name) < 0 ||
+ pa_tagstruct_get_channel_map(t, &entry->channel_map) ||
+ pa_tagstruct_get_cvolume(t, &entry->volume) < 0 ||
+ pa_tagstruct_gets(t, &device) < 0 ||
+ pa_tagstruct_get_boolean(t, &muted) < 0)
+ goto fail;
+
+ if (!name || !*name) {
+ entry_free(entry);
+ goto fail;
+ }
+
+ entry->volume_valid = entry->volume.channels > 0;
+
+ if (entry->volume_valid)
+ if (!pa_cvolume_compatible_with_channel_map(&entry->volume, &entry->channel_map)) {
+ entry_free(entry);
+ goto fail;
+ }
+
+ entry->muted = muted;
+ entry->muted_valid = TRUE;
+
+ entry->device = pa_xstrdup(device);
+ entry->device_valid = device && !!entry->device[0];
+
+ if (entry->device_valid && !pa_namereg_is_valid_name(entry->device)) {
+ entry_free(entry);
+ goto fail;
+ }
+
+#ifdef HAVE_DBUS
+ old = entry_read(u, name);
+#endif
+
+ pa_log_debug("Client %s changes entry %s.",
+ pa_strnull(pa_proplist_gets(pa_native_connection_get_client(c)->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)),
+ name);
+
+ if (entry_write(u, name, entry, mode == PA_UPDATE_REPLACE)) {
+#ifdef HAVE_DBUS
+ struct dbus_entry *de;
+
+ if (old) {
+ pa_assert_se((de = pa_hashmap_get(u->dbus_entries, name)));
+
+ if ((old->device_valid != entry->device_valid)
+ || (entry->device_valid && !pa_streq(entry->device, old->device)))
+ send_device_updated_signal(de, entry);
+
+ if ((old->volume_valid != entry->volume_valid)
+ || (entry->volume_valid && (!pa_cvolume_equal(&entry->volume, &old->volume)
+ || !pa_channel_map_equal(&entry->channel_map, &old->channel_map))))
+ send_volume_updated_signal(de, entry);
+
+ if (!old->muted_valid || (entry->muted != old->muted))
+ send_mute_updated_signal(de, entry);
+
+ } else {
+ de = dbus_entry_new(u, name);
+ pa_assert_se(pa_hashmap_put(u->dbus_entries, de->entry_name, de) == 0);
+ send_new_entry_signal(de);
+ }
+#endif
+
+ if (apply_immediately)
+ entry_apply(u, name, entry);
+ }
+
+#ifdef HAVE_DBUS
+ if (old)
+ entry_free(old);
+#endif
+ entry_free(entry);
+ }
+
+ trigger_save(u);
+
+ break;
+ }
+
+ case SUBCOMMAND_DELETE:
+
+ while (!pa_tagstruct_eof(t)) {
+ const char *name;
+ pa_datum key;
+#ifdef HAVE_DBUS
+ struct dbus_entry *de;
+#endif
+
+ if (pa_tagstruct_gets(t, &name) < 0)
+ goto fail;
+
+#ifdef HAVE_DBUS
+ if ((de = pa_hashmap_get(u->dbus_entries, name))) {
+ send_entry_removed_signal(de);
+ dbus_entry_free(pa_hashmap_remove(u->dbus_entries, name));
+ }
+#endif
+
+ key.data = (char*) name;
+ key.size = strlen(name);
+
+ pa_database_unset(u->database, &key);
+ }
+
+ trigger_save(u);
+
+ break;
+
+ case SUBCOMMAND_SUBSCRIBE: {
+
+ pa_bool_t enabled;
+
+ if (pa_tagstruct_get_boolean(t, &enabled) < 0 ||
+ !pa_tagstruct_eof(t))
+ goto fail;
+
+ if (enabled)
+ pa_idxset_put(u->subscribed, c, NULL);
+ else
+ pa_idxset_remove_by_data(u->subscribed, c, NULL);
+
+ break;
+ }
+
+ default:
+ goto fail;
+ }
+
+ pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), reply);
+ return 0;
+
+fail:
+
+ if (reply)
+ pa_tagstruct_free(reply);
+
+ return -1;
+}
+
+static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_native_connection *c, struct userdata *u) {
+ pa_assert(p);
+ pa_assert(c);
+ pa_assert(u);
+
+ pa_idxset_remove_by_data(u->subscribed, c, NULL);
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+ char *fname;
+ pa_sink_input *si;
+ pa_source_output *so;
+ uint32_t idx;
+ pa_bool_t restore_device = TRUE, restore_volume = TRUE, restore_muted = TRUE, on_hotplug = TRUE, on_rescue = TRUE;
+#ifdef HAVE_DBUS
+ pa_datum key;
+ pa_bool_t done;
+#endif
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "restore_device", &restore_device) < 0 ||
+ pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
+ pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0 ||
+ pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
+ pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
+ pa_log("restore_device=, restore_volume=, restore_muted=, on_hotplug= and on_rescue= expect boolean arguments");
+ goto fail;
+ }
+
+ if (!restore_muted && !restore_volume && !restore_device)
+ pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring device enabled!");
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->restore_device = restore_device;
+ u->restore_volume = restore_volume;
+ u->restore_muted = restore_muted;
+ u->on_hotplug = on_hotplug;
+ u->on_rescue = on_rescue;
+ u->subscribed = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ u->protocol = pa_native_protocol_get(m->core);
+ pa_native_protocol_install_ext(u->protocol, m, extension_cb);
+
+ u->connection_unlink_hook_slot = pa_hook_connect(&pa_native_protocol_hooks(u->protocol)[PA_NATIVE_HOOK_CONNECTION_UNLINK], PA_HOOK_NORMAL, (pa_hook_cb_t) connection_unlink_hook_cb, u);
+
+ u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, subscribe_callback, u);
+
+ if (restore_device) {
+ /* A little bit earlier than module-intended-roles ... */
+ u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_new_hook_callback, u);
+ u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_output_new_hook_callback, u);
+ }
+
+ if (restore_device && on_hotplug) {
+ /* A little bit earlier than module-intended-roles ... */
+ u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_put_hook_callback, u);
+ u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) source_put_hook_callback, u);
+ }
+
+ if (restore_device && on_rescue) {
+ /* A little bit earlier than module-intended-roles, module-rescue-streams, ... */
+ u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_unlink_hook_callback, u);
+ u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_unlink_hook_callback, u);
+ }
+
+ if (restore_volume || restore_muted)
+ u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_fixate_hook_callback, u);
+
+ if (!(fname = pa_state_path("stream-volumes", TRUE)))
+ goto fail;
+
+ if (!(u->database = pa_database_open(fname, TRUE))) {
+ pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
+ pa_xfree(fname);
+ goto fail;
+ }
+
+ pa_log_info("Successfully opened database file '%s'.", fname);
+ pa_xfree(fname);
+
+#ifdef HAVE_DBUS
+ u->dbus_protocol = pa_dbus_protocol_get(u->core);
+ u->dbus_entries = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ pa_assert_se(pa_dbus_protocol_add_interface(u->dbus_protocol, OBJECT_PATH, &stream_restore_interface_info, u) >= 0);
+ pa_assert_se(pa_dbus_protocol_register_extension(u->dbus_protocol, INTERFACE_STREAM_RESTORE) >= 0);
+
+ /* Create the initial dbus entries. */
+ done = !pa_database_first(u->database, &key, NULL);
+ while (!done) {
+ pa_datum next_key;
+ char *name;
+ struct dbus_entry *de;
+ struct entry *e;
+
+ done = !pa_database_next(u->database, &key, &next_key, NULL);
+
+ name = pa_xstrndup(key.data, key.size);
+ pa_datum_free(&key);
+
+ /* Use entry_read() for checking that the entry is valid. */
+ if ((e = entry_read(u, name))) {
+ de = dbus_entry_new(u, name);
+ pa_assert_se(pa_hashmap_put(u->dbus_entries, de->entry_name, de) == 0);
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+
+ key = next_key;
+ }
+#endif
+
+ PA_IDXSET_FOREACH(si, m->core->sink_inputs, idx)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, si->index, u);
+
+ PA_IDXSET_FOREACH(so, m->core->source_outputs, idx)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, so->index, u);
+
+ pa_modargs_free(ma);
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+#ifdef HAVE_DBUS
+static void free_dbus_entry_cb(void *p, void *userdata) {
+ struct dbus_entry *de = p;
+
+ pa_assert(de);
+
+ dbus_entry_free(de);
+}
+#endif
+
+void pa__done(pa_module*m) {
+ struct userdata* u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+#ifdef HAVE_DBUS
+ if (u->dbus_protocol) {
+ pa_assert(u->dbus_entries);
+
+ pa_assert_se(pa_dbus_protocol_unregister_extension(u->dbus_protocol, INTERFACE_STREAM_RESTORE) >= 0);
+ pa_assert_se(pa_dbus_protocol_remove_interface(u->dbus_protocol, OBJECT_PATH, stream_restore_interface_info.name) >= 0);
+
+ pa_hashmap_free(u->dbus_entries, free_dbus_entry_cb, NULL);
+
+ pa_dbus_protocol_unref(u->dbus_protocol);
+ }
+#endif
+
+ if (u->subscription)
+ pa_subscription_free(u->subscription);
+
+ if (u->sink_input_new_hook_slot)
+ pa_hook_slot_free(u->sink_input_new_hook_slot);
+ if (u->sink_input_fixate_hook_slot)
+ pa_hook_slot_free(u->sink_input_fixate_hook_slot);
+ if (u->source_output_new_hook_slot)
+ pa_hook_slot_free(u->source_output_new_hook_slot);
+
+ if (u->sink_put_hook_slot)
+ pa_hook_slot_free(u->sink_put_hook_slot);
+ if (u->source_put_hook_slot)
+ pa_hook_slot_free(u->source_put_hook_slot);
+
+ if (u->sink_unlink_hook_slot)
+ pa_hook_slot_free(u->sink_unlink_hook_slot);
+ if (u->source_unlink_hook_slot)
+ pa_hook_slot_free(u->source_unlink_hook_slot);
+
+ if (u->connection_unlink_hook_slot)
+ pa_hook_slot_free(u->connection_unlink_hook_slot);
+
+ if (u->save_time_event)
+ u->core->mainloop->time_free(u->save_time_event);
+
+ if (u->database)
+ pa_database_close(u->database);
+
+ if (u->protocol) {
+ pa_native_protocol_remove_ext(u->protocol, m);
+ pa_native_protocol_unref(u->protocol);
+ }
+
+ if (u->subscribed)
+ pa_idxset_free(u->subscribed, NULL, NULL);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-suspend-on-idle.c b/src/modules/module-suspend-on-idle.c
new file mode 100644
index 00000000..e7242628
--- /dev/null
+++ b/src/modules/module-suspend-on-idle.c
@@ -0,0 +1,567 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+#include <pulse/timeval.h>
+#include <pulse/rtclock.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/source-output.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+
+#include "module-suspend-on-idle-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(
+ "timeout=<timeout> "
+ "mempool_vacuum=<vacuum memory if all sinks and sources are suspended?>");
+
+static const char* const valid_modargs[] = {
+ "timeout",
+ "mempool_vacuum",
+ NULL,
+};
+
+struct userdata {
+ pa_core *core;
+ pa_usec_t timeout;
+ pa_hashmap *device_infos;
+ pa_hook_slot
+ *sink_new_slot,
+ *source_new_slot,
+ *sink_unlink_slot,
+ *source_unlink_slot,
+ *sink_state_changed_slot,
+ *source_state_changed_slot;
+
+ pa_hook_slot
+ *sink_input_new_slot,
+ *source_output_new_slot,
+ *sink_input_unlink_slot,
+ *source_output_unlink_slot,
+ *sink_input_move_start_slot,
+ *source_output_move_start_slot,
+ *sink_input_move_finish_slot,
+ *source_output_move_finish_slot,
+ *sink_input_state_changed_slot,
+ *source_output_state_changed_slot;
+
+ pa_bool_t mempool_vacuum:1;
+};
+
+struct device_info {
+ struct userdata *userdata;
+ pa_sink *sink;
+ pa_source *source;
+ pa_usec_t last_use;
+ pa_time_event *time_event;
+};
+
+static void check_meempool_vacuum(struct device_info *d) {
+ pa_sink *si;
+ pa_source *so;
+ uint32_t idx;
+
+ pa_assert(d);
+ pa_assert(d->userdata);
+ pa_assert(d->userdata->core);
+
+ idx = 0;
+ PA_IDXSET_FOREACH(si, d->userdata->core->sinks, idx)
+ if (pa_sink_get_state(si) != PA_SINK_SUSPENDED)
+ return;
+
+ idx = 0;
+ PA_IDXSET_FOREACH(so, d->userdata->core->sources, idx)
+ if (pa_source_get_state(so) != PA_SOURCE_SUSPENDED)
+ return;
+
+ pa_log_info("All sinks and sources are suspended, vacuuming memory");
+ pa_mempool_vacuum(d->userdata->core->mempool);
+}
+
+static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
+ struct device_info *d = userdata;
+
+ pa_assert(d);
+
+ d->userdata->core->mainloop->time_restart(d->time_event, NULL);
+
+ if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && !(d->sink->suspend_cause & PA_SUSPEND_IDLE)) {
+ pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
+ pa_sink_suspend(d->sink, TRUE, PA_SUSPEND_IDLE);
+ if (d->userdata->mempool_vacuum)
+ check_meempool_vacuum(d);
+ }
+
+ if (d->source && pa_source_check_suspend(d->source) <= 0 && !(d->source->suspend_cause & PA_SUSPEND_IDLE)) {
+ pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
+ pa_source_suspend(d->source, TRUE, PA_SUSPEND_IDLE);
+ if (d->userdata->mempool_vacuum)
+ check_meempool_vacuum(d);
+ }
+}
+
+static void restart(struct device_info *d) {
+ pa_usec_t now;
+ const char *s;
+ uint32_t timeout;
+
+ pa_assert(d);
+ pa_assert(d->sink || d->source);
+
+ d->last_use = now = pa_rtclock_now();
+
+ s = pa_proplist_gets(d->sink ? d->sink->proplist : d->source->proplist, "module-suspend-on-idle.timeout");
+ if (!s || pa_atou(s, &timeout) < 0)
+ timeout = d->userdata->timeout;
+
+ pa_core_rttime_restart(d->userdata->core, d->time_event, now + timeout * PA_USEC_PER_SEC);
+
+ if (d->sink)
+ pa_log_debug("Sink %s becomes idle, timeout in %u seconds.", d->sink->name, timeout);
+ if (d->source)
+ pa_log_debug("Source %s becomes idle, timeout in %u seconds.", d->source->name, timeout);
+}
+
+static void resume(struct device_info *d) {
+ pa_assert(d);
+
+ d->userdata->core->mainloop->time_restart(d->time_event, NULL);
+
+ if (d->sink) {
+ pa_sink_suspend(d->sink, FALSE, PA_SUSPEND_IDLE);
+
+ pa_log_debug("Sink %s becomes busy.", d->sink->name);
+ }
+
+ if (d->source) {
+ pa_source_suspend(d->source, FALSE, PA_SUSPEND_IDLE);
+
+ pa_log_debug("Source %s becomes busy.", d->source->name);
+ }
+}
+
+static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
+ struct device_info *d;
+
+ pa_assert(c);
+ pa_assert(data);
+ pa_assert(u);
+
+ /* We need to resume the audio device here even for
+ * PA_SINK_INPUT_START_CORKED, since we need the device parameters
+ * to be fully available while the stream is set up. */
+
+ if ((d = pa_hashmap_get(u->device_infos, data->sink)))
+ resume(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
+ struct device_info *d;
+
+ pa_assert(c);
+ pa_assert(data);
+ pa_assert(u);
+
+ if (data->source->monitor_of)
+ d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
+ else
+ d = pa_hashmap_get(u->device_infos, data->source);
+
+ if (d)
+ resume(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
+ pa_assert(c);
+ pa_sink_input_assert_ref(s);
+ pa_assert(u);
+
+ if (!s->sink)
+ return PA_HOOK_OK;
+
+ if (pa_sink_check_suspend(s->sink) <= 0) {
+ struct device_info *d;
+ if ((d = pa_hashmap_get(u->device_infos, s->sink)))
+ restart(d);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
+ struct device_info *d = NULL;
+
+ pa_assert(c);
+ pa_source_output_assert_ref(s);
+ pa_assert(u);
+
+ if (!s->source)
+ return PA_HOOK_OK;
+
+ if (s->source->monitor_of) {
+ if (pa_sink_check_suspend(s->source->monitor_of) <= 0)
+ d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
+ } else {
+ if (pa_source_check_suspend(s->source) <= 0)
+ d = pa_hashmap_get(u->device_infos, s->source);
+ }
+
+ if (d)
+ restart(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
+ struct device_info *d;
+
+ pa_assert(c);
+ pa_sink_input_assert_ref(s);
+ pa_assert(u);
+
+ if (pa_sink_check_suspend(s->sink) <= 1)
+ if ((d = pa_hashmap_get(u->device_infos, s->sink)))
+ restart(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
+ struct device_info *d;
+ pa_sink_input_state_t state;
+
+ pa_assert(c);
+ pa_sink_input_assert_ref(s);
+ pa_assert(u);
+
+ state = pa_sink_input_get_state(s);
+ if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED)
+ return PA_HOOK_OK;
+
+ if ((d = pa_hashmap_get(u->device_infos, s->sink)))
+ resume(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
+ struct device_info *d = NULL;
+
+ pa_assert(c);
+ pa_source_output_assert_ref(s);
+ pa_assert(u);
+
+ if (s->source->monitor_of) {
+ if (pa_sink_check_suspend(s->source->monitor_of) <= 1)
+ d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
+ } else {
+ if (pa_source_check_suspend(s->source) <= 1)
+ d = pa_hashmap_get(u->device_infos, s->source);
+ }
+
+ if (d)
+ restart(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
+ struct device_info *d;
+
+ pa_assert(c);
+ pa_source_output_assert_ref(s);
+ pa_assert(u);
+
+ if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING)
+ return PA_HOOK_OK;
+
+ if (s->source->monitor_of)
+ d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
+ else
+ d = pa_hashmap_get(u->device_infos, s->source);
+
+ if (d)
+ resume(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
+ struct device_info *d;
+ pa_sink_input_state_t state;
+
+ pa_assert(c);
+ pa_sink_input_assert_ref(s);
+ pa_assert(u);
+
+ state = pa_sink_input_get_state(s);
+ if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
+ if ((d = pa_hashmap_get(u->device_infos, s->sink)))
+ resume(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
+ pa_assert(c);
+ pa_source_output_assert_ref(s);
+ pa_assert(u);
+
+ if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) {
+ struct device_info *d;
+
+ if (s->source->monitor_of)
+ d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
+ else
+ d = pa_hashmap_get(u->device_infos, s->source);
+
+ if (d)
+ resume(d);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
+ struct device_info *d;
+ pa_source *source;
+ pa_sink *sink;
+
+ pa_assert(c);
+ pa_object_assert_ref(o);
+ pa_assert(u);
+
+ source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
+ sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
+
+ /* Never suspend monitors */
+ if (source && source->monitor_of)
+ return PA_HOOK_OK;
+
+ pa_assert(source || sink);
+
+ d = pa_xnew(struct device_info, 1);
+ d->userdata = u;
+ d->source = source ? pa_source_ref(source) : NULL;
+ d->sink = sink ? pa_sink_ref(sink) : NULL;
+ d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d);
+ pa_hashmap_put(u->device_infos, o, d);
+
+ if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
+ (d->source && pa_source_check_suspend(d->source) <= 0))
+ restart(d);
+
+ return PA_HOOK_OK;
+}
+
+static void device_info_free(struct device_info *d) {
+ pa_assert(d);
+
+ if (d->source)
+ pa_source_unref(d->source);
+ if (d->sink)
+ pa_sink_unref(d->sink);
+
+ d->userdata->core->mainloop->time_free(d->time_event);
+
+ pa_xfree(d);
+}
+
+static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
+ struct device_info *d;
+
+ pa_assert(c);
+ pa_object_assert_ref(o);
+ pa_assert(u);
+
+ if ((d = pa_hashmap_remove(u->device_infos, o)))
+ device_info_free(d);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
+ struct device_info *d;
+
+ pa_assert(c);
+ pa_object_assert_ref(o);
+ pa_assert(u);
+
+ if (!(d = pa_hashmap_get(u->device_infos, o)))
+ return PA_HOOK_OK;
+
+ if (pa_sink_isinstance(o)) {
+ pa_sink *s = PA_SINK(o);
+ pa_sink_state_t state = pa_sink_get_state(s);
+
+ if (pa_sink_check_suspend(s) <= 0)
+ if (PA_SINK_IS_OPENED(state))
+ restart(d);
+
+ } else if (pa_source_isinstance(o)) {
+ pa_source *s = PA_SOURCE(o);
+ pa_source_state_t state = pa_source_get_state(s);
+
+ if (pa_source_check_suspend(s) <= 0)
+ if (PA_SOURCE_IS_OPENED(state))
+ restart(d);
+ }
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma = NULL;
+ struct userdata *u;
+ uint32_t timeout = 5;
+ pa_bool_t mempool_vacuum = FALSE;
+ uint32_t idx;
+ pa_sink *sink;
+ pa_source *source;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
+ pa_log("Failed to parse timeout value.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "mempool_vacuum", &mempool_vacuum) < 0) {
+ pa_log("Failed to parse mempool_vacuum boolean parameter.");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+ u->core = m->core;
+ u->timeout = timeout;
+ u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ u->mempool_vacuum = mempool_vacuum;
+
+ for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
+ device_new_hook_cb(m->core, PA_OBJECT(sink), u);
+
+ for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
+ device_new_hook_cb(m->core, PA_OBJECT(source), u);
+
+ u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
+ u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
+ u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
+ u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
+ u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
+ u->source_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
+
+ u->sink_input_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_fixate_hook_cb, u);
+ u->source_output_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_fixate_hook_cb, u);
+ u->sink_input_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_unlink_hook_cb, u);
+ u->source_output_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_unlink_hook_cb, u);
+ u->sink_input_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_start_hook_cb, u);
+ u->source_output_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_start_hook_cb, u);
+ u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_finish_hook_cb, u);
+ u->source_output_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_finish_hook_cb, u);
+ u->sink_input_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_state_changed_hook_cb, u);
+ u->source_output_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_state_changed_hook_cb, u);
+
+ pa_modargs_free(ma);
+ return 0;
+
+fail:
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+ struct device_info *d;
+
+ pa_assert(m);
+
+ if (!m->userdata)
+ return;
+
+ u = m->userdata;
+
+ if (u->sink_new_slot)
+ pa_hook_slot_free(u->sink_new_slot);
+ if (u->sink_unlink_slot)
+ pa_hook_slot_free(u->sink_unlink_slot);
+ if (u->sink_state_changed_slot)
+ pa_hook_slot_free(u->sink_state_changed_slot);
+
+ if (u->source_new_slot)
+ pa_hook_slot_free(u->source_new_slot);
+ if (u->source_unlink_slot)
+ pa_hook_slot_free(u->source_unlink_slot);
+ if (u->source_state_changed_slot)
+ pa_hook_slot_free(u->source_state_changed_slot);
+
+ if (u->sink_input_new_slot)
+ pa_hook_slot_free(u->sink_input_new_slot);
+ if (u->sink_input_unlink_slot)
+ pa_hook_slot_free(u->sink_input_unlink_slot);
+ if (u->sink_input_move_start_slot)
+ pa_hook_slot_free(u->sink_input_move_start_slot);
+ if (u->sink_input_move_finish_slot)
+ pa_hook_slot_free(u->sink_input_move_finish_slot);
+ if (u->sink_input_state_changed_slot)
+ pa_hook_slot_free(u->sink_input_state_changed_slot);
+
+ if (u->source_output_new_slot)
+ pa_hook_slot_free(u->source_output_new_slot);
+ if (u->source_output_unlink_slot)
+ pa_hook_slot_free(u->source_output_unlink_slot);
+ if (u->source_output_move_start_slot)
+ pa_hook_slot_free(u->source_output_move_start_slot);
+ if (u->source_output_move_finish_slot)
+ pa_hook_slot_free(u->source_output_move_finish_slot);
+ if (u->source_output_state_changed_slot)
+ pa_hook_slot_free(u->source_output_state_changed_slot);
+
+ while ((d = pa_hashmap_steal_first(u->device_infos)))
+ device_info_free(d);
+
+ pa_hashmap_free(u->device_infos, NULL, NULL);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-switch-on-connect.c b/src/modules/module-switch-on-connect.c
new file mode 100644
index 00000000..b121fd9a
--- /dev/null
+++ b/src/modules/module-switch-on-connect.c
@@ -0,0 +1,187 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2006 Lennart Poettering
+ Copyright 2009 Canonical Ltd
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/source-output.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/core-util.h>
+
+#include "module-switch-on-connect-symdef.h"
+
+PA_MODULE_AUTHOR("Michael Terry");
+PA_MODULE_DESCRIPTION("When a sink/source is added, switch to it");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+static const char* const valid_modargs[] = {
+ NULL,
+};
+
+struct userdata {
+ pa_hook_slot
+ *sink_put_slot,
+ *source_put_slot;
+};
+
+static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
+ pa_sink_input *i;
+ uint32_t idx;
+ pa_sink *def;
+ const char *s;
+
+ pa_assert(c);
+ pa_assert(sink);
+
+ /* Don't want to run during startup or shutdown */
+ if (c->state != PA_CORE_RUNNING)
+ return PA_HOOK_OK;
+
+ /* Don't switch to any internal devices */
+ if ((s = pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_BUS))) {
+ if (pa_streq(s, "pci"))
+ return PA_HOOK_OK;
+ else if (pa_streq(s, "isa"))
+ return PA_HOOK_OK;
+ }
+
+ def = pa_namereg_get_default_sink(c);
+ if (def == sink)
+ return PA_HOOK_OK;
+
+ /* Actually do the switch to the new sink */
+ pa_namereg_set_default_sink(c, sink);
+
+ /* Now move all old inputs over */
+ if (pa_idxset_size(def->inputs) <= 0) {
+ pa_log_debug("No sink inputs to move away.");
+ return PA_HOOK_OK;
+ }
+
+ PA_IDXSET_FOREACH(i, def->inputs, idx) {
+ if (i->save_sink)
+ continue;
+
+ if (pa_sink_input_move_to(i, sink, FALSE) < 0)
+ pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), sink->name);
+ else
+ pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), sink->name);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, void* userdata) {
+ pa_source_output *o;
+ uint32_t idx;
+ pa_source *def;
+ const char *s;
+
+ pa_assert(c);
+ pa_assert(source);
+
+ /* Don't want to run during startup or shutdown */
+ if (c->state != PA_CORE_RUNNING)
+ return PA_HOOK_OK;
+
+ /* Don't switch to any internal devices */
+ if ((s = pa_proplist_gets(source->proplist, PA_PROP_DEVICE_BUS))) {
+ if (pa_streq(s, "pci"))
+ return PA_HOOK_OK;
+ else if (pa_streq(s, "isa"))
+ return PA_HOOK_OK;
+ }
+
+ def = pa_namereg_get_default_source(c);
+ if (def == source)
+ return PA_HOOK_OK;
+
+ /* Actually do the switch to the new source */
+ pa_namereg_set_default_source(c, source);
+
+ /* Now move all old outputs over */
+ if (pa_idxset_size(def->outputs) <= 0) {
+ pa_log_debug("No source outputs to move away.");
+ return PA_HOOK_OK;
+ }
+
+ PA_IDXSET_FOREACH(o, def->outputs, idx) {
+ if (o->save_source)
+ continue;
+
+ if (pa_source_output_move_to(o, source, FALSE) < 0)
+ pa_log_info("Failed to move source output %u \"%s\" to %s.", o->index,
+ pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), source->name);
+ else
+ pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o->index,
+ pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), source->name);
+ }
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma;
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ return -1;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+
+ /* A little bit later than module-rescue-streams... */
+ u->sink_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+30, (pa_hook_cb_t) sink_put_hook_callback, u);
+ u->source_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+20, (pa_hook_cb_t) source_put_hook_callback, u);
+
+ pa_modargs_free(ma);
+ return 0;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink_put_slot)
+ pa_hook_slot_free(u->sink_put_slot);
+ if (u->source_put_slot)
+ pa_hook_slot_free(u->source_put_slot);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-tunnel.c b/src/modules/module-tunnel.c
index 2fb34d12..4b1ae7df 100644
--- a/src/modules/module-tunnel.c
+++ b/src/modules/module-tunnel.c
@@ -1,18 +1,19 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,13 +25,13 @@
#endif
#include <unistd.h>
-#include <assert.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
+#include <pulse/rtclock.h>
#include <pulse/timeval.h>
#include <pulse/util.h>
#include <pulse/version.h>
@@ -41,56 +42,54 @@
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
#include <pulsecore/core-subscribe.h>
-#include <pulsecore/sink-input.h>
#include <pulsecore/pdispatch.h>
#include <pulsecore/pstream.h>
#include <pulsecore/pstream-util.h>
-#include <pulsecore/authkey.h>
#include <pulsecore/socket-client.h>
-#include <pulsecore/socket-util.h>
-#include <pulsecore/authkey-prop.h>
+#include <pulsecore/time-smoother.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/core-rtclock.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/proplist-util.h>
+#include <pulsecore/auth-cookie.h>
+#include <pulsecore/mcalign.h>
#ifdef TUNNEL_SINK
#include "module-tunnel-sink-symdef.h"
-PA_MODULE_DESCRIPTION("Tunnel module for sinks")
+#else
+#include "module-tunnel-source-symdef.h"
+#endif
+
+#ifdef TUNNEL_SINK
+PA_MODULE_DESCRIPTION("Tunnel module for sinks");
PA_MODULE_USAGE(
+ "sink_name=<name for the local sink> "
+ "sink_properties=<properties for the local sink> "
"server=<address> "
"sink=<remote sink name> "
"cookie=<filename> "
"format=<sample format> "
"channels=<number of channels> "
"rate=<sample rate> "
- "sink_name=<name for the local sink> "
- "channel_map=<channel map>")
+ "channel_map=<channel map>");
#else
-#include "module-tunnel-source-symdef.h"
-PA_MODULE_DESCRIPTION("Tunnel module for sources")
+PA_MODULE_DESCRIPTION("Tunnel module for sources");
PA_MODULE_USAGE(
+ "source_name=<name for the local source> "
+ "source_properties=<properties for the local source> "
"server=<address> "
"source=<remote source name> "
"cookie=<filename> "
"format=<sample format> "
"channels=<number of channels> "
"rate=<sample rate> "
- "source_name=<name for the local source> "
- "channel_map=<channel map>")
+ "channel_map=<channel map>");
#endif
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-
-#define DEFAULT_SINK_NAME "tunnel"
-#define DEFAULT_SOURCE_NAME "tunnel"
-
-#define DEFAULT_TLENGTH (44100*2*2/10) //(10240*8)
-#define DEFAULT_MAXLENGTH ((DEFAULT_TLENGTH*3)/2)
-#define DEFAULT_MINREQ 512
-#define DEFAULT_PREBUF (DEFAULT_TLENGTH-DEFAULT_MINREQ)
-#define DEFAULT_FRAGSIZE 1024
-
-#define DEFAULT_TIMEOUT 5
-
-#define LATENCY_INTERVAL 10
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
static const char* const valid_modargs[] = {
"server",
@@ -100,32 +99,88 @@ static const char* const valid_modargs[] = {
"rate",
#ifdef TUNNEL_SINK
"sink_name",
+ "sink_properties",
"sink",
#else
"source_name",
+ "source_properties",
"source",
#endif
"channel_map",
NULL,
};
-static void command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
-static void command_subscribe_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
+#define DEFAULT_TIMEOUT 5
+
+#define LATENCY_INTERVAL (10*PA_USEC_PER_SEC)
+
+#define MIN_NETWORK_LATENCY_USEC (8*PA_USEC_PER_MSEC)
+
+#ifdef TUNNEL_SINK
+
+enum {
+ SINK_MESSAGE_REQUEST = PA_SINK_MESSAGE_MAX,
+ SINK_MESSAGE_REMOTE_SUSPEND,
+ SINK_MESSAGE_UPDATE_LATENCY,
+ SINK_MESSAGE_POST
+};
+
+#define DEFAULT_TLENGTH_MSEC 150
+#define DEFAULT_MINREQ_MSEC 25
+
+#else
+
+enum {
+ SOURCE_MESSAGE_POST = PA_SOURCE_MESSAGE_MAX,
+ SOURCE_MESSAGE_REMOTE_SUSPEND,
+ SOURCE_MESSAGE_UPDATE_LATENCY
+};
+
+#define DEFAULT_FRAGSIZE_MSEC 25
+
+#endif
#ifdef TUNNEL_SINK
static void command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
+static void command_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
#endif
+static void command_subscribe_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
+static void command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
+static void command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
+static void command_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
+static void command_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
+static void command_stream_or_client_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
+static void command_stream_buffer_attr_changed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
#ifdef TUNNEL_SINK
[PA_COMMAND_REQUEST] = command_request,
-#endif
+ [PA_COMMAND_STARTED] = command_started,
+#endif
+ [PA_COMMAND_SUBSCRIBE_EVENT] = command_subscribe_event,
+ [PA_COMMAND_OVERFLOW] = command_overflow_or_underflow,
+ [PA_COMMAND_UNDERFLOW] = command_overflow_or_underflow,
[PA_COMMAND_PLAYBACK_STREAM_KILLED] = command_stream_killed,
[PA_COMMAND_RECORD_STREAM_KILLED] = command_stream_killed,
- [PA_COMMAND_SUBSCRIBE_EVENT] = command_subscribe_event,
+ [PA_COMMAND_PLAYBACK_STREAM_SUSPENDED] = command_suspended,
+ [PA_COMMAND_RECORD_STREAM_SUSPENDED] = command_suspended,
+ [PA_COMMAND_PLAYBACK_STREAM_MOVED] = command_moved,
+ [PA_COMMAND_RECORD_STREAM_MOVED] = command_moved,
+ [PA_COMMAND_PLAYBACK_STREAM_EVENT] = command_stream_or_client_event,
+ [PA_COMMAND_RECORD_STREAM_EVENT] = command_stream_or_client_event,
+ [PA_COMMAND_CLIENT_EVENT] = command_stream_or_client_event,
+ [PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED] = command_stream_buffer_attr_changed,
+ [PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED] = command_stream_buffer_attr_changed
};
struct userdata {
+ pa_core *core;
+ pa_module *module;
+
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+ pa_thread *thread;
+
pa_socket_client *client;
pa_pstream *pstream;
pa_pdispatch *pdispatch;
@@ -134,236 +189,687 @@ struct userdata {
#ifdef TUNNEL_SINK
char *sink_name;
pa_sink *sink;
- uint32_t requested_bytes;
+ size_t requested_bytes;
#else
char *source_name;
pa_source *source;
+ pa_mcalign *mcalign;
#endif
-
- pa_module *module;
- pa_core *core;
- uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
+ pa_auth_cookie *auth_cookie;
uint32_t version;
uint32_t ctag;
uint32_t device_index;
uint32_t channel;
-
- pa_usec_t host_latency;
- pa_time_event *time_event;
+ int64_t counter, counter_delta;
- int auth_cookie_in_property;
-};
+ pa_bool_t remote_corked:1;
+ pa_bool_t remote_suspended:1;
-static void close_stuff(struct userdata *u) {
- assert(u);
-
- if (u->pstream) {
- pa_pstream_close(u->pstream);
- pa_pstream_unref(u->pstream);
- u->pstream = NULL;
- }
+ pa_usec_t transport_usec; /* maintained in the main thread */
+ pa_usec_t thread_transport_usec; /* maintained in the IO thread */
- if (u->pdispatch) {
- pa_pdispatch_unref(u->pdispatch);
- u->pdispatch = NULL;
- }
+ uint32_t ignore_latency_before;
- if (u->client) {
- pa_socket_client_unref(u->client);
- u->client = NULL;
- }
+ pa_time_event *time_event;
+
+ pa_smoother *smoother;
+
+ char *device_description;
+ char *server_fqdn;
+ char *user_name;
+ uint32_t maxlength;
#ifdef TUNNEL_SINK
- if (u->sink) {
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- u->sink = NULL;
- }
+ uint32_t tlength;
+ uint32_t minreq;
+ uint32_t prebuf;
#else
- if (u->source) {
- pa_source_disconnect(u->source);
- pa_source_unref(u->source);
- u->source = NULL;
- }
+ uint32_t fragsize;
#endif
+};
- if (u->time_event) {
- u->core->mainloop->time_free(u->time_event);
- u->time_event = NULL;
- }
-}
+static void request_latency(struct userdata *u);
-static void die(struct userdata *u) {
- assert(u);
- close_stuff(u);
- pa_module_unload_request(u->module);
+/* Called from main context */
+static void command_stream_or_client_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ pa_log_debug("Got stream or client event.");
}
-static void command_stream_killed(pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
+/* Called from main context */
+static void command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
struct userdata *u = userdata;
- assert(pd && t && u && u->pdispatch == pd);
- pa_log(__FILE__": stream killed");
- die(u);
+ pa_assert(pd);
+ pa_assert(t);
+ pa_assert(u);
+ pa_assert(u->pdispatch == pd);
+
+ pa_log_warn("Stream killed");
+ pa_module_unload_request(u->module, TRUE);
}
-static void request_info(struct userdata *u);
+/* Called from main context */
+static void command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ struct userdata *u = userdata;
-static void command_subscribe_event(pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
+ pa_assert(pd);
+ pa_assert(t);
+ pa_assert(u);
+ pa_assert(u->pdispatch == pd);
+
+ pa_log_info("Server signalled buffer overrun/underrun.");
+ request_latency(u);
+}
+
+/* Called from main context */
+static void command_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
struct userdata *u = userdata;
- pa_subscription_event_type_t e;
- uint32_t idx;
+ uint32_t channel;
+ pa_bool_t suspended;
- assert(pd && t && u);
- assert(command == PA_COMMAND_SUBSCRIBE_EVENT);
+ pa_assert(pd);
+ pa_assert(t);
+ pa_assert(u);
+ pa_assert(u->pdispatch == pd);
- if (pa_tagstruct_getu32(t, &e) < 0 ||
- pa_tagstruct_getu32(t, &idx) < 0 ||
+ if (pa_tagstruct_getu32(t, &channel) < 0 ||
+ pa_tagstruct_get_boolean(t, &suspended) < 0 ||
!pa_tagstruct_eof(t)) {
- pa_log(__FILE__": invalid protocol reply");
- die(u);
+
+ pa_log("Invalid packet.");
+ pa_module_unload_request(u->module, TRUE);
return;
}
+ pa_log_debug("Server reports device suspend.");
+
#ifdef TUNNEL_SINK
- if (e != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE))
+ pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REMOTE_SUSPEND, PA_UINT32_TO_PTR(!!suspended), 0, NULL);
+#else
+ pa_asyncmsgq_send(u->source->asyncmsgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_REMOTE_SUSPEND, PA_UINT32_TO_PTR(!!suspended), 0, NULL);
+#endif
+
+ request_latency(u);
+}
+
+/* Called from main context */
+static void command_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ struct userdata *u = userdata;
+ uint32_t channel, di;
+ const char *dn;
+ pa_bool_t suspended;
+
+ pa_assert(pd);
+ pa_assert(t);
+ pa_assert(u);
+ pa_assert(u->pdispatch == pd);
+
+ if (pa_tagstruct_getu32(t, &channel) < 0 ||
+ pa_tagstruct_getu32(t, &di) < 0 ||
+ pa_tagstruct_gets(t, &dn) < 0 ||
+ pa_tagstruct_get_boolean(t, &suspended) < 0) {
+
+ pa_log_error("Invalid packet.");
+ pa_module_unload_request(u->module, TRUE);
return;
+ }
+
+ pa_log_debug("Server reports a stream move.");
+
+#ifdef TUNNEL_SINK
+ pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REMOTE_SUSPEND, PA_UINT32_TO_PTR(!!suspended), 0, NULL);
#else
- if (e != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
+ pa_asyncmsgq_send(u->source->asyncmsgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_REMOTE_SUSPEND, PA_UINT32_TO_PTR(!!suspended), 0, NULL);
+#endif
+
+ request_latency(u);
+}
+
+static void command_stream_buffer_attr_changed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ struct userdata *u = userdata;
+ uint32_t channel, maxlength, tlength = 0, fragsize, prebuf, minreq;
+ pa_usec_t usec;
+
+ pa_assert(pd);
+ pa_assert(t);
+ pa_assert(u);
+ pa_assert(u->pdispatch == pd);
+
+ if (pa_tagstruct_getu32(t, &channel) < 0 ||
+ pa_tagstruct_getu32(t, &maxlength) < 0) {
+
+ pa_log_error("Invalid packet.");
+ pa_module_unload_request(u->module, TRUE);
return;
+ }
+
+ if (command == PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED) {
+ if (pa_tagstruct_getu32(t, &fragsize) < 0 ||
+ pa_tagstruct_get_usec(t, &usec) < 0) {
+
+ pa_log_error("Invalid packet.");
+ pa_module_unload_request(u->module, TRUE);
+ return;
+ }
+ } else {
+ if (pa_tagstruct_getu32(t, &tlength) < 0 ||
+ pa_tagstruct_getu32(t, &prebuf) < 0 ||
+ pa_tagstruct_getu32(t, &minreq) < 0 ||
+ pa_tagstruct_get_usec(t, &usec) < 0) {
+
+ pa_log_error("Invalid packet.");
+ pa_module_unload_request(u->module, TRUE);
+ return;
+ }
+ }
+
+#ifdef TUNNEL_SINK
+ pa_log_debug("Server reports buffer attrs changed. tlength now at %lu, before %lu.", (unsigned long) tlength, (unsigned long) u->tlength);
#endif
- request_info(u);
+ request_latency(u);
}
#ifdef TUNNEL_SINK
-static void send_prebuf_request(struct userdata *u) {
+
+/* Called from main context */
+static void command_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(pd);
+ pa_assert(t);
+ pa_assert(u);
+ pa_assert(u->pdispatch == pd);
+
+ pa_log_debug("Server reports playback started.");
+ request_latency(u);
+}
+
+#endif
+
+/* Called from IO thread context */
+static void check_smoother_status(struct userdata *u, pa_bool_t past) {
+ pa_usec_t x;
+
+ pa_assert(u);
+
+ x = pa_rtclock_now();
+
+ /* Correct by the time the requested issued needs to travel to the
+ * other side. This is a valid thread-safe access, because the
+ * main thread is waiting for us */
+
+ if (past)
+ x -= u->thread_transport_usec;
+ else
+ x += u->thread_transport_usec;
+
+ if (u->remote_suspended || u->remote_corked)
+ pa_smoother_pause(u->smoother, x);
+ else
+ pa_smoother_resume(u->smoother, x, TRUE);
+}
+
+/* Called from IO thread context */
+static void stream_cork_within_thread(struct userdata *u, pa_bool_t cork) {
+ pa_assert(u);
+
+ if (u->remote_corked == cork)
+ return;
+
+ u->remote_corked = cork;
+ check_smoother_status(u, FALSE);
+}
+
+/* Called from main context */
+static void stream_cork(struct userdata *u, pa_bool_t cork) {
pa_tagstruct *t;
+ pa_assert(u);
+
+ if (!u->pstream)
+ return;
t = pa_tagstruct_new(NULL, 0);
- pa_tagstruct_putu32(t, PA_COMMAND_PREBUF_PLAYBACK_STREAM);
+#ifdef TUNNEL_SINK
+ pa_tagstruct_putu32(t, PA_COMMAND_CORK_PLAYBACK_STREAM);
+#else
+ pa_tagstruct_putu32(t, PA_COMMAND_CORK_RECORD_STREAM);
+#endif
pa_tagstruct_putu32(t, u->ctag++);
pa_tagstruct_putu32(t, u->channel);
+ pa_tagstruct_put_boolean(t, !!cork);
pa_pstream_send_tagstruct(u->pstream, t);
+
+ request_latency(u);
}
-static void send_bytes(struct userdata *u) {
- assert(u);
+/* Called from IO thread context */
+static void stream_suspend_within_thread(struct userdata *u, pa_bool_t suspend) {
+ pa_assert(u);
- if (!u->pstream)
+ if (u->remote_suspended == suspend)
return;
+ u->remote_suspended = suspend;
+ check_smoother_status(u, TRUE);
+}
+
+#ifdef TUNNEL_SINK
+
+/* Called from IO thread context */
+static void send_data(struct userdata *u) {
+ pa_assert(u);
+
while (u->requested_bytes > 0) {
- pa_memchunk chunk;
- if (pa_sink_render(u->sink, u->requested_bytes, &chunk) < 0) {
-
- if (u->requested_bytes >= DEFAULT_TLENGTH-DEFAULT_PREBUF)
- send_prebuf_request(u);
-
- return;
+ pa_memchunk memchunk;
+
+ pa_sink_render(u->sink, u->requested_bytes, &memchunk);
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_POST, NULL, 0, &memchunk, NULL);
+ pa_memblock_unref(memchunk.memblock);
+
+ u->requested_bytes -= memchunk.length;
+
+ u->counter += (int64_t) memchunk.length;
+ }
+}
+
+/* This function is called from IO context -- except when it is not. */
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_SET_STATE: {
+ int r;
+
+ /* First, change the state, because otherwide pa_sink_render() would fail */
+ if ((r = pa_sink_process_msg(o, code, data, offset, chunk)) >= 0) {
+
+ stream_cork_within_thread(u, u->sink->state == PA_SINK_SUSPENDED);
+
+ if (PA_SINK_IS_OPENED(u->sink->state))
+ send_data(u);
+ }
+
+ return r;
}
- pa_pstream_send_memblock(u->pstream, u->channel, 0, PA_SEEK_RELATIVE, &chunk);
- pa_memblock_unref(chunk.memblock);
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ pa_usec_t yl, yr, *usec = data;
- if (chunk.length > u->requested_bytes)
- u->requested_bytes = 0;
- else
- u->requested_bytes -= chunk.length;
+ yl = pa_bytes_to_usec((uint64_t) u->counter, &u->sink->sample_spec);
+ yr = pa_smoother_get(u->smoother, pa_rtclock_now());
+
+ *usec = yl > yr ? yl - yr : 0;
+ return 0;
+ }
+
+ case SINK_MESSAGE_REQUEST:
+
+ pa_assert(offset > 0);
+ u->requested_bytes += (size_t) offset;
+
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
+ send_data(u);
+
+ return 0;
+
+
+ case SINK_MESSAGE_REMOTE_SUSPEND:
+
+ stream_suspend_within_thread(u, !!PA_PTR_TO_UINT(data));
+ return 0;
+
+
+ case SINK_MESSAGE_UPDATE_LATENCY: {
+ pa_usec_t y;
+
+ y = pa_bytes_to_usec((uint64_t) u->counter, &u->sink->sample_spec);
+
+ if (y > (pa_usec_t) offset)
+ y -= (pa_usec_t) offset;
+ else
+ y = 0;
+
+ pa_smoother_put(u->smoother, pa_rtclock_now(), y);
+
+ /* We can access this freely here, since the main thread is waiting for us */
+ u->thread_transport_usec = u->transport_usec;
+
+ return 0;
+ }
+
+ case SINK_MESSAGE_POST:
+
+ /* OK, This might be a bit confusing. This message is
+ * delivered to us from the main context -- NOT from the
+ * IO thread context where the rest of the messages are
+ * dispatched. Yeah, ugly, but I am a lazy bastard. */
+
+ pa_pstream_send_memblock(u->pstream, u->channel, 0, PA_SEEK_RELATIVE, chunk);
+
+ u->counter_delta += (int64_t) chunk->length;
+
+ return 0;
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from main context */
+static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
+ struct userdata *u;
+ pa_sink_assert_ref(s);
+ u = s->userdata;
+
+ switch ((pa_sink_state_t) state) {
+
+ case PA_SINK_SUSPENDED:
+ pa_assert(PA_SINK_IS_OPENED(s->state));
+ stream_cork(u, TRUE);
+ break;
+
+ case PA_SINK_IDLE:
+ case PA_SINK_RUNNING:
+ if (s->state == PA_SINK_SUSPENDED)
+ stream_cork(u, FALSE);
+ break;
+
+ case PA_SINK_UNLINKED:
+ case PA_SINK_INIT:
+ case PA_SINK_INVALID_STATE:
+ ;
+ }
+
+ return 0;
+}
+
+#else
+
+/* This function is called from IO context -- except when it is not. */
+static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE(o)->userdata;
+
+ switch (code) {
+
+ case PA_SOURCE_MESSAGE_SET_STATE: {
+ int r;
+
+ if ((r = pa_source_process_msg(o, code, data, offset, chunk)) >= 0)
+ stream_cork_within_thread(u, u->source->state == PA_SOURCE_SUSPENDED);
+
+ return r;
+ }
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ pa_usec_t yr, yl, *usec = data;
+
+ yl = pa_bytes_to_usec((uint64_t) u->counter, &PA_SOURCE(o)->sample_spec);
+ yr = pa_smoother_get(u->smoother, pa_rtclock_now());
+
+ *usec = yr > yl ? yr - yl : 0;
+ return 0;
+ }
+
+ case SOURCE_MESSAGE_POST: {
+ pa_memchunk c;
+
+ pa_mcalign_push(u->mcalign, chunk);
+
+ while (pa_mcalign_pop(u->mcalign, &c) >= 0) {
+
+ if (PA_SOURCE_IS_OPENED(u->source->thread_info.state))
+ pa_source_post(u->source, &c);
+
+ pa_memblock_unref(c.memblock);
+
+ u->counter += (int64_t) c.length;
+ }
+
+ return 0;
+ }
+
+ case SOURCE_MESSAGE_REMOTE_SUSPEND:
+
+ stream_suspend_within_thread(u, !!PA_PTR_TO_UINT(data));
+ return 0;
+
+ case SOURCE_MESSAGE_UPDATE_LATENCY: {
+ pa_usec_t y;
+
+ y = pa_bytes_to_usec((uint64_t) u->counter, &u->source->sample_spec);
+ y += (pa_usec_t) offset;
+
+ pa_smoother_put(u->smoother, pa_rtclock_now(), y);
+
+ /* We can access this freely here, since the main thread is waiting for us */
+ u->thread_transport_usec = u->transport_usec;
+
+ return 0;
+ }
}
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
}
-static void command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
+/* Called from main context */
+static int source_set_state(pa_source *s, pa_source_state_t state) {
+ struct userdata *u;
+ pa_source_assert_ref(s);
+ u = s->userdata;
+
+ switch ((pa_source_state_t) state) {
+
+ case PA_SOURCE_SUSPENDED:
+ pa_assert(PA_SOURCE_IS_OPENED(s->state));
+ stream_cork(u, TRUE);
+ break;
+
+ case PA_SOURCE_IDLE:
+ case PA_SOURCE_RUNNING:
+ if (s->state == PA_SOURCE_SUSPENDED)
+ stream_cork(u, FALSE);
+ break;
+
+ case PA_SOURCE_UNLINKED:
+ case PA_SOURCE_INIT:
+ case PA_SINK_INVALID_STATE:
+ ;
+ }
+
+ return 0;
+}
+
+#endif
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ for (;;) {
+ int ret;
+
+#ifdef TUNNEL_SINK
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
+ if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
+ pa_sink_process_rewind(u->sink, 0);
+#endif
+
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+#ifdef TUNNEL_SINK
+/* Called from main context */
+static void command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
struct userdata *u = userdata;
uint32_t bytes, channel;
- assert(pd && command == PA_COMMAND_REQUEST && t && u && u->pdispatch == pd);
+
+ pa_assert(pd);
+ pa_assert(command == PA_COMMAND_REQUEST);
+ pa_assert(t);
+ pa_assert(u);
+ pa_assert(u->pdispatch == pd);
if (pa_tagstruct_getu32(t, &channel) < 0 ||
- pa_tagstruct_getu32(t, &bytes) < 0 ||
- !pa_tagstruct_eof(t)) {
- pa_log(__FILE__": invalid protocol reply");
- die(u);
- return;
+ pa_tagstruct_getu32(t, &bytes) < 0) {
+ pa_log("Invalid protocol reply");
+ goto fail;
}
if (channel != u->channel) {
- pa_log(__FILE__": recieved data for invalid channel");
- die(u);
- return;
+ pa_log("Received data for invalid channel");
+ goto fail;
}
-
- u->requested_bytes += bytes;
- send_bytes(u);
+
+ pa_asyncmsgq_post(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REQUEST, NULL, bytes, NULL, NULL);
+ return;
+
+fail:
+ pa_module_unload_request(u->module, TRUE);
}
#endif
-static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
+/* Called from main context */
+static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
struct userdata *u = userdata;
- pa_usec_t sink_usec, source_usec, transport_usec;
- int playing;
+ pa_usec_t sink_usec, source_usec;
+ pa_bool_t playing;
int64_t write_index, read_index;
struct timeval local, remote, now;
- assert(pd && u);
+ pa_sample_spec *ss;
+ int64_t delay;
+
+ pa_assert(pd);
+ pa_assert(u);
if (command != PA_COMMAND_REPLY) {
if (command == PA_COMMAND_ERROR)
- pa_log(__FILE__": failed to get latency.");
+ pa_log("Failed to get latency.");
else
- pa_log(__FILE__": protocol error.");
- die(u);
- return;
+ pa_log("Protocol error.");
+ goto fail;
}
-
+
if (pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
pa_tagstruct_get_usec(t, &source_usec) < 0 ||
pa_tagstruct_get_boolean(t, &playing) < 0 ||
pa_tagstruct_get_timeval(t, &local) < 0 ||
pa_tagstruct_get_timeval(t, &remote) < 0 ||
pa_tagstruct_gets64(t, &write_index) < 0 ||
- pa_tagstruct_gets64(t, &read_index) < 0 ||
- !pa_tagstruct_eof(t)) {
- pa_log(__FILE__": invalid reply. (latency)");
- die(u);
+ pa_tagstruct_gets64(t, &read_index) < 0) {
+ pa_log("Invalid reply.");
+ goto fail;
+ }
+
+#ifdef TUNNEL_SINK
+ if (u->version >= 13) {
+ uint64_t underrun_for = 0, playing_for = 0;
+
+ if (pa_tagstruct_getu64(t, &underrun_for) < 0 ||
+ pa_tagstruct_getu64(t, &playing_for) < 0) {
+ pa_log("Invalid reply.");
+ goto fail;
+ }
+ }
+#endif
+
+ if (!pa_tagstruct_eof(t)) {
+ pa_log("Invalid reply.");
+ goto fail;
+ }
+
+ if (tag < u->ignore_latency_before) {
return;
}
pa_gettimeofday(&now);
+ /* Calculate transport usec */
if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
/* local and remote seem to have synchronized clocks */
#ifdef TUNNEL_SINK
- transport_usec = pa_timeval_diff(&remote, &local);
+ u->transport_usec = pa_timeval_diff(&remote, &local);
#else
- transport_usec = pa_timeval_diff(&now, &remote);
-#endif
+ u->transport_usec = pa_timeval_diff(&now, &remote);
+#endif
} else
- transport_usec = pa_timeval_diff(&now, &local)/2;
+ u->transport_usec = pa_timeval_diff(&now, &local)/2;
+ /* First, take the device's delay */
#ifdef TUNNEL_SINK
- u->host_latency = sink_usec + transport_usec;
+ delay = (int64_t) sink_usec;
+ ss = &u->sink->sample_spec;
#else
- u->host_latency = source_usec + transport_usec;
- if (u->host_latency > sink_usec)
- u->host_latency -= sink_usec;
+ delay = (int64_t) source_usec;
+ ss = &u->source->sample_spec;
+#endif
+
+ /* Add the length of our server-side buffer */
+ if (write_index >= read_index)
+ delay += (int64_t) pa_bytes_to_usec((uint64_t) (write_index-read_index), ss);
else
- u->host_latency = 0;
+ delay -= (int64_t) pa_bytes_to_usec((uint64_t) (read_index-write_index), ss);
+
+ /* Our measurements are already out of date, hence correct by the *
+ * transport latency */
+#ifdef TUNNEL_SINK
+ delay -= (int64_t) u->transport_usec;
+#else
+ delay += (int64_t) u->transport_usec;
#endif
-/* pa_log(__FILE__": estimated host latency: %0.0f usec", (double) u->host_latency); */
+ /* Now correct by what we have have read/written since we requested the update */
+#ifdef TUNNEL_SINK
+ delay += (int64_t) pa_bytes_to_usec((uint64_t) u->counter_delta, ss);
+#else
+ delay -= (int64_t) pa_bytes_to_usec((uint64_t) u->counter_delta, ss);
+#endif
+
+#ifdef TUNNEL_SINK
+ pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_UPDATE_LATENCY, 0, delay, NULL);
+#else
+ pa_asyncmsgq_send(u->source->asyncmsgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_UPDATE_LATENCY, 0, delay, NULL);
+#endif
+
+ return;
+
+fail:
+
+ pa_module_unload_request(u->module, TRUE);
}
+/* Called from main context */
static void request_latency(struct userdata *u) {
pa_tagstruct *t;
struct timeval now;
uint32_t tag;
- assert(u);
+ pa_assert(u);
t = pa_tagstruct_new(NULL, 0);
-#ifdef TUNNEL_SINK
+#ifdef TUNNEL_SINK
pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
#else
pa_tagstruct_putu32(t, PA_COMMAND_GET_RECORD_LATENCY);
@@ -371,39 +877,157 @@ static void request_latency(struct userdata *u) {
pa_tagstruct_putu32(t, tag = u->ctag++);
pa_tagstruct_putu32(t, u->channel);
- pa_gettimeofday(&now);
- pa_tagstruct_put_timeval(t, &now);
-
+ pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
+
pa_pstream_send_tagstruct(u->pstream, t);
pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u, NULL);
+
+ u->ignore_latency_before = tag;
+ u->counter_delta = 0;
}
-static void stream_get_info_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
+/* Called from main context */
+static void timeout_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *t, void *userdata) {
struct userdata *u = userdata;
- uint32_t idx, owner_module, monitor_source;
- pa_usec_t latency;
+
+ pa_assert(m);
+ pa_assert(e);
+ pa_assert(u);
+
+ request_latency(u);
+
+ pa_core_rttime_restart(u->core, e, pa_rtclock_now() + LATENCY_INTERVAL);
+}
+
+/* Called from main context */
+static void update_description(struct userdata *u) {
+ char *d;
+ char un[128], hn[128];
+ pa_tagstruct *t;
+
+ pa_assert(u);
+
+ if (!u->server_fqdn || !u->user_name || !u->device_description)
+ return;
+
+ d = pa_sprintf_malloc("%s on %s@%s", u->device_description, u->user_name, u->server_fqdn);
+
+#ifdef TUNNEL_SINK
+ pa_sink_set_description(u->sink, d);
+ pa_proplist_sets(u->sink->proplist, "tunnel.remote.user", u->user_name);
+ pa_proplist_sets(u->sink->proplist, "tunnel.remote.fqdn", u->server_fqdn);
+ pa_proplist_sets(u->sink->proplist, "tunnel.remote.description", u->device_description);
+#else
+ pa_source_set_description(u->source, d);
+ pa_proplist_sets(u->source->proplist, "tunnel.remote.user", u->user_name);
+ pa_proplist_sets(u->source->proplist, "tunnel.remote.fqdn", u->server_fqdn);
+ pa_proplist_sets(u->source->proplist, "tunnel.remote.description", u->device_description);
+#endif
+
+ pa_xfree(d);
+
+ d = pa_sprintf_malloc("%s for %s@%s", u->device_description,
+ pa_get_user_name(un, sizeof(un)),
+ pa_get_host_name(hn, sizeof(hn)));
+
+ t = pa_tagstruct_new(NULL, 0);
+#ifdef TUNNEL_SINK
+ pa_tagstruct_putu32(t, PA_COMMAND_SET_PLAYBACK_STREAM_NAME);
+#else
+ pa_tagstruct_putu32(t, PA_COMMAND_SET_RECORD_STREAM_NAME);
+#endif
+ pa_tagstruct_putu32(t, u->ctag++);
+ pa_tagstruct_putu32(t, u->channel);
+ pa_tagstruct_puts(t, d);
+ pa_pstream_send_tagstruct(u->pstream, t);
+
+ pa_xfree(d);
+}
+
+/* Called from main context */
+static void server_info_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ struct userdata *u = userdata;
+ pa_sample_spec ss;
+ pa_channel_map cm;
+ const char *server_name, *server_version, *user_name, *host_name, *default_sink_name, *default_source_name;
+ uint32_t cookie;
+
+ pa_assert(pd);
+ pa_assert(u);
+
+ if (command != PA_COMMAND_REPLY) {
+ if (command == PA_COMMAND_ERROR)
+ pa_log("Failed to get info.");
+ else
+ pa_log("Protocol error.");
+ goto fail;
+ }
+
+ if (pa_tagstruct_gets(t, &server_name) < 0 ||
+ pa_tagstruct_gets(t, &server_version) < 0 ||
+ pa_tagstruct_gets(t, &user_name) < 0 ||
+ pa_tagstruct_gets(t, &host_name) < 0 ||
+ pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
+ pa_tagstruct_gets(t, &default_sink_name) < 0 ||
+ pa_tagstruct_gets(t, &default_source_name) < 0 ||
+ pa_tagstruct_getu32(t, &cookie) < 0 ||
+ (u->version >= 15 && pa_tagstruct_get_channel_map(t, &cm) < 0)) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+
+ if (!pa_tagstruct_eof(t)) {
+ pa_log("Packet too long");
+ goto fail;
+ }
+
+ pa_xfree(u->server_fqdn);
+ u->server_fqdn = pa_xstrdup(host_name);
+
+ pa_xfree(u->user_name);
+ u->user_name = pa_xstrdup(user_name);
+
+ update_description(u);
+
+ return;
+
+fail:
+ pa_module_unload_request(u->module, TRUE);
+}
+
+#ifdef TUNNEL_SINK
+
+/* Called from main context */
+static void sink_info_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ struct userdata *u = userdata;
+ uint32_t idx, owner_module, monitor_source, flags;
const char *name, *description, *monitor_source_name, *driver;
- int mute;
- uint32_t flags;
- pa_sample_spec sample_spec;
- pa_channel_map channel_map;
+ pa_sample_spec ss;
+ pa_channel_map cm;
pa_cvolume volume;
- assert(pd && u);
+ pa_bool_t mute;
+ pa_usec_t latency;
+ pa_proplist *pl;
+
+ pa_assert(pd);
+ pa_assert(u);
+
+ pl = pa_proplist_new();
if (command != PA_COMMAND_REPLY) {
if (command == PA_COMMAND_ERROR)
- pa_log(__FILE__": failed to get info.");
+ pa_log("Failed to get info.");
else
- pa_log(__FILE__": protocol error.");
- die(u);
- return;
+ pa_log("Protocol error.");
+ goto fail;
}
if (pa_tagstruct_getu32(t, &idx) < 0 ||
pa_tagstruct_gets(t, &name) < 0 ||
pa_tagstruct_gets(t, &description) < 0 ||
- pa_tagstruct_get_sample_spec(t, &sample_spec) < 0 ||
- pa_tagstruct_get_channel_map(t, &channel_map) < 0 ||
+ pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
+ pa_tagstruct_get_channel_map(t, &cm) < 0 ||
pa_tagstruct_getu32(t, &owner_module) < 0 ||
pa_tagstruct_get_cvolume(t, &volume) < 0 ||
pa_tagstruct_get_boolean(t, &mute) < 0 ||
@@ -411,117 +1035,540 @@ static void stream_get_info_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_
pa_tagstruct_gets(t, &monitor_source_name) < 0 ||
pa_tagstruct_get_usec(t, &latency) < 0 ||
pa_tagstruct_gets(t, &driver) < 0 ||
- pa_tagstruct_getu32(t, &flags) < 0 ||
- !pa_tagstruct_eof(t)) {
- pa_log(__FILE__": invalid reply. (get_info)");
- die(u);
+ pa_tagstruct_getu32(t, &flags) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+
+ if (u->version >= 13) {
+ pa_usec_t configured_latency;
+
+ if (pa_tagstruct_get_proplist(t, pl) < 0 ||
+ pa_tagstruct_get_usec(t, &configured_latency) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (u->version >= 15) {
+ pa_volume_t base_volume;
+ uint32_t state, n_volume_steps, card;
+
+ if (pa_tagstruct_get_volume(t, &base_volume) < 0 ||
+ pa_tagstruct_getu32(t, &state) < 0 ||
+ pa_tagstruct_getu32(t, &n_volume_steps) < 0 ||
+ pa_tagstruct_getu32(t, &card) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (u->version >= 16) {
+ uint32_t n_ports;
+ const char *s;
+
+ if (pa_tagstruct_getu32(t, &n_ports)) {
+ pa_log("Parse failure");
+ goto fail;
+ }
+
+ for (uint32_t j = 0; j < n_ports; j++) {
+ uint32_t priority;
+
+ if (pa_tagstruct_gets(t, &s) < 0 || /* name */
+ pa_tagstruct_gets(t, &s) < 0 || /* description */
+ pa_tagstruct_getu32(t, &priority) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (pa_tagstruct_gets(t, &s) < 0) { /* active port */
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (u->version >= 21) {
+ uint8_t n_formats;
+ pa_format_info format;
+
+ if (pa_tagstruct_getu8(t, &n_formats) < 0) { /* no. of formats */
+ pa_log("Parse failure");
+ goto fail;
+ }
+
+ for (uint8_t j = 0; j < n_formats; j++) {
+ if (pa_tagstruct_get_format_info(t, &format)) { /* format info */
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+ }
+
+ if (!pa_tagstruct_eof(t)) {
+ pa_log("Packet too long");
+ goto fail;
+ }
+
+ pa_proplist_free(pl);
+
+ if (!u->sink_name || strcmp(name, u->sink_name))
return;
+
+ pa_xfree(u->device_description);
+ u->device_description = pa_xstrdup(description);
+
+ update_description(u);
+
+ return;
+
+fail:
+ pa_module_unload_request(u->module, TRUE);
+ pa_proplist_free(pl);
+}
+
+/* Called from main context */
+static void sink_input_info_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ struct userdata *u = userdata;
+ uint32_t idx, owner_module, client, sink;
+ pa_usec_t buffer_usec, sink_usec;
+ const char *name, *driver, *resample_method;
+ pa_bool_t mute = FALSE;
+ pa_sample_spec sample_spec;
+ pa_channel_map channel_map;
+ pa_cvolume volume;
+ pa_proplist *pl;
+ pa_bool_t b;
+
+ pa_assert(pd);
+ pa_assert(u);
+
+ pl = pa_proplist_new();
+
+ if (command != PA_COMMAND_REPLY) {
+ if (command == PA_COMMAND_ERROR)
+ pa_log("Failed to get info.");
+ else
+ pa_log("Protocol error.");
+ goto fail;
}
-#ifdef TUNNEL_SINK
- assert(u->sink);
- if ((!!mute == !!u->sink->hw_muted) &&
- pa_cvolume_equal(&volume, &u->sink->hw_volume))
+ if (pa_tagstruct_getu32(t, &idx) < 0 ||
+ pa_tagstruct_gets(t, &name) < 0 ||
+ pa_tagstruct_getu32(t, &owner_module) < 0 ||
+ pa_tagstruct_getu32(t, &client) < 0 ||
+ pa_tagstruct_getu32(t, &sink) < 0 ||
+ pa_tagstruct_get_sample_spec(t, &sample_spec) < 0 ||
+ pa_tagstruct_get_channel_map(t, &channel_map) < 0 ||
+ pa_tagstruct_get_cvolume(t, &volume) < 0 ||
+ pa_tagstruct_get_usec(t, &buffer_usec) < 0 ||
+ pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
+ pa_tagstruct_gets(t, &resample_method) < 0 ||
+ pa_tagstruct_gets(t, &driver) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+
+ if (u->version >= 11) {
+ if (pa_tagstruct_get_boolean(t, &mute) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (u->version >= 13) {
+ if (pa_tagstruct_get_proplist(t, pl) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (u->version >= 19) {
+ if (pa_tagstruct_get_boolean(t, &b) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (u->version >= 20) {
+ if (pa_tagstruct_get_boolean(t, &b) < 0 ||
+ pa_tagstruct_get_boolean(t, &b) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (u->version >= 21) {
+ pa_format_info format;
+
+ if (pa_tagstruct_get_format_info(t, &format) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (!pa_tagstruct_eof(t)) {
+ pa_log("Packet too long");
+ goto fail;
+ }
+
+ pa_proplist_free(pl);
+
+ if (idx != u->device_index)
return;
-#else
- assert(u->source);
- if ((!!mute == !!u->source->hw_muted) &&
- pa_cvolume_equal(&volume, &u->source->hw_volume))
+
+ pa_assert(u->sink);
+
+ if ((u->version < 11 || !!mute == !!u->sink->muted) &&
+ pa_cvolume_equal(&volume, &u->sink->real_volume))
return;
-#endif
-#ifdef TUNNEL_SINK
- memcpy(&u->sink->hw_volume, &volume, sizeof(pa_cvolume));
- u->sink->hw_muted = !!mute;
+ pa_sink_volume_changed(u->sink, &volume);
+
+ if (u->version >= 11)
+ pa_sink_mute_changed(u->sink, mute);
+
+ return;
+
+fail:
+ pa_module_unload_request(u->module, TRUE);
+ pa_proplist_free(pl);
+}
- pa_subscription_post(u->sink->core,
- PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
- u->sink->index);
#else
- memcpy(&u->source->hw_volume, &volume, sizeof(pa_cvolume));
- u->source->hw_muted = !!mute;
- pa_subscription_post(u->source->core,
- PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
- u->source->index);
-#endif
+/* Called from main context */
+static void source_info_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ struct userdata *u = userdata;
+ uint32_t idx, owner_module, monitor_of_sink, flags;
+ const char *name, *description, *monitor_of_sink_name, *driver;
+ pa_sample_spec ss;
+ pa_channel_map cm;
+ pa_cvolume volume;
+ pa_bool_t mute;
+ pa_usec_t latency, configured_latency;
+ pa_proplist *pl;
+
+ pa_assert(pd);
+ pa_assert(u);
+
+ pl = pa_proplist_new();
+
+ if (command != PA_COMMAND_REPLY) {
+ if (command == PA_COMMAND_ERROR)
+ pa_log("Failed to get info.");
+ else
+ pa_log("Protocol error.");
+ goto fail;
+ }
+
+ if (pa_tagstruct_getu32(t, &idx) < 0 ||
+ pa_tagstruct_gets(t, &name) < 0 ||
+ pa_tagstruct_gets(t, &description) < 0 ||
+ pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
+ pa_tagstruct_get_channel_map(t, &cm) < 0 ||
+ pa_tagstruct_getu32(t, &owner_module) < 0 ||
+ pa_tagstruct_get_cvolume(t, &volume) < 0 ||
+ pa_tagstruct_get_boolean(t, &mute) < 0 ||
+ pa_tagstruct_getu32(t, &monitor_of_sink) < 0 ||
+ pa_tagstruct_gets(t, &monitor_of_sink_name) < 0 ||
+ pa_tagstruct_get_usec(t, &latency) < 0 ||
+ pa_tagstruct_gets(t, &driver) < 0 ||
+ pa_tagstruct_getu32(t, &flags) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+
+ if (u->version >= 13) {
+ if (pa_tagstruct_get_proplist(t, pl) < 0 ||
+ pa_tagstruct_get_usec(t, &configured_latency) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (u->version >= 15) {
+ pa_volume_t base_volume;
+ uint32_t state, n_volume_steps, card;
+
+ if (pa_tagstruct_get_volume(t, &base_volume) < 0 ||
+ pa_tagstruct_getu32(t, &state) < 0 ||
+ pa_tagstruct_getu32(t, &n_volume_steps) < 0 ||
+ pa_tagstruct_getu32(t, &card) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (u->version >= 16) {
+ uint32_t n_ports;
+ const char *s;
+
+ if (pa_tagstruct_getu32(t, &n_ports)) {
+ pa_log("Parse failure");
+ goto fail;
+ }
+
+ for (uint32_t j = 0; j < n_ports; j++) {
+ uint32_t priority;
+
+ if (pa_tagstruct_gets(t, &s) < 0 || /* name */
+ pa_tagstruct_gets(t, &s) < 0 || /* description */
+ pa_tagstruct_getu32(t, &priority) < 0) {
+
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (pa_tagstruct_gets(t, &s) < 0) { /* active port */
+ pa_log("Parse failure");
+ goto fail;
+ }
+ }
+
+ if (!pa_tagstruct_eof(t)) {
+ pa_log("Packet too long");
+ goto fail;
+ }
+
+ pa_proplist_free(pl);
+
+ if (!u->source_name || strcmp(name, u->source_name))
+ return;
+
+ pa_xfree(u->device_description);
+ u->device_description = pa_xstrdup(description);
+
+ update_description(u);
+
+ return;
+
+fail:
+ pa_module_unload_request(u->module, TRUE);
+ pa_proplist_free(pl);
}
+#endif
+
+/* Called from main context */
static void request_info(struct userdata *u) {
pa_tagstruct *t;
uint32_t tag;
- assert(u);
+ pa_assert(u);
t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(t, PA_COMMAND_GET_SERVER_INFO);
+ pa_tagstruct_putu32(t, tag = u->ctag++);
+ pa_pstream_send_tagstruct(u->pstream, t);
+ pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, server_info_cb, u, NULL);
+
#ifdef TUNNEL_SINK
- pa_tagstruct_putu32(t, PA_COMMAND_GET_SINK_INFO);
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(t, PA_COMMAND_GET_SINK_INPUT_INFO);
+ pa_tagstruct_putu32(t, tag = u->ctag++);
+ pa_tagstruct_putu32(t, u->device_index);
+ pa_pstream_send_tagstruct(u->pstream, t);
+ pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, sink_input_info_cb, u, NULL);
+
+ if (u->sink_name) {
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(t, PA_COMMAND_GET_SINK_INFO);
+ pa_tagstruct_putu32(t, tag = u->ctag++);
+ pa_tagstruct_putu32(t, PA_INVALID_INDEX);
+ pa_tagstruct_puts(t, u->sink_name);
+ pa_pstream_send_tagstruct(u->pstream, t);
+ pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, sink_info_cb, u, NULL);
+ }
#else
- pa_tagstruct_putu32(t, PA_COMMAND_GET_SOURCE_INFO);
+ if (u->source_name) {
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(t, PA_COMMAND_GET_SOURCE_INFO);
+ pa_tagstruct_putu32(t, tag = u->ctag++);
+ pa_tagstruct_putu32(t, PA_INVALID_INDEX);
+ pa_tagstruct_puts(t, u->source_name);
+ pa_pstream_send_tagstruct(u->pstream, t);
+ pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, source_info_cb, u, NULL);
+ }
#endif
- pa_tagstruct_putu32(t, tag = u->ctag++);
+}
+
+/* Called from main context */
+static void command_subscribe_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+ struct userdata *u = userdata;
+ pa_subscription_event_type_t e;
+ uint32_t idx;
+
+ pa_assert(pd);
+ pa_assert(t);
+ pa_assert(u);
+ pa_assert(command == PA_COMMAND_SUBSCRIBE_EVENT);
- pa_tagstruct_putu32(t, PA_INVALID_INDEX);
+ if (pa_tagstruct_getu32(t, &e) < 0 ||
+ pa_tagstruct_getu32(t, &idx) < 0) {
+ pa_log("Invalid protocol reply");
+ pa_module_unload_request(u->module, TRUE);
+ return;
+ }
+
+ if (e != (PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE) &&
#ifdef TUNNEL_SINK
- pa_tagstruct_puts(t, u->sink_name);
+ e != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE) &&
+ e != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE)
#else
- pa_tagstruct_puts(t, u->source_name);
+ e != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE)
#endif
+ )
+ return;
- pa_pstream_send_tagstruct(u->pstream, t);
- pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_info_callback, u, NULL);
+ request_info(u);
}
+/* Called from main context */
static void start_subscribe(struct userdata *u) {
pa_tagstruct *t;
- uint32_t tag;
- assert(u);
+ pa_assert(u);
t = pa_tagstruct_new(NULL, 0);
pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE);
- pa_tagstruct_putu32(t, tag = u->ctag++);
-
+ pa_tagstruct_putu32(t, u->ctag++);
+ pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SERVER|
#ifdef TUNNEL_SINK
- pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SINK);
+ PA_SUBSCRIPTION_MASK_SINK_INPUT|PA_SUBSCRIPTION_MASK_SINK
#else
- pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SOURCE);
+ PA_SUBSCRIPTION_MASK_SOURCE
#endif
+ );
pa_pstream_send_tagstruct(u->pstream, t);
}
-static void create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
+/* Called from main context */
+static void create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
struct userdata *u = userdata;
- assert(pd && u && u->pdispatch == pd);
+#ifdef TUNNEL_SINK
+ uint32_t bytes;
+#endif
+
+ pa_assert(pd);
+ pa_assert(u);
+ pa_assert(u->pdispatch == pd);
if (command != PA_COMMAND_REPLY) {
if (command == PA_COMMAND_ERROR)
- pa_log(__FILE__": failed to create stream.");
+ pa_log("Failed to create stream.");
else
- pa_log(__FILE__": protocol error.");
- die(u);
- return;
+ pa_log("Protocol error.");
+ goto fail;
}
if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
- pa_tagstruct_getu32(t, &u->device_index) < 0 ||
-#ifdef TUNNEL_SINK
- pa_tagstruct_getu32(t, &u->requested_bytes) < 0 ||
-#endif
- !pa_tagstruct_eof(t)) {
- pa_log(__FILE__": invalid reply. (create stream)");
- die(u);
- return;
+ pa_tagstruct_getu32(t, &u->device_index) < 0
+#ifdef TUNNEL_SINK
+ || pa_tagstruct_getu32(t, &bytes) < 0
+#endif
+ )
+ goto parse_error;
+
+ if (u->version >= 9) {
+#ifdef TUNNEL_SINK
+ if (pa_tagstruct_getu32(t, &u->maxlength) < 0 ||
+ pa_tagstruct_getu32(t, &u->tlength) < 0 ||
+ pa_tagstruct_getu32(t, &u->prebuf) < 0 ||
+ pa_tagstruct_getu32(t, &u->minreq) < 0)
+ goto parse_error;
+#else
+ if (pa_tagstruct_getu32(t, &u->maxlength) < 0 ||
+ pa_tagstruct_getu32(t, &u->fragsize) < 0)
+ goto parse_error;
+#endif
}
+ if (u->version >= 12) {
+ pa_sample_spec ss;
+ pa_channel_map cm;
+ uint32_t device_index;
+ const char *dn;
+ pa_bool_t suspended;
+
+ if (pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
+ pa_tagstruct_get_channel_map(t, &cm) < 0 ||
+ pa_tagstruct_getu32(t, &device_index) < 0 ||
+ pa_tagstruct_gets(t, &dn) < 0 ||
+ pa_tagstruct_get_boolean(t, &suspended) < 0)
+ goto parse_error;
+
+#ifdef TUNNEL_SINK
+ pa_xfree(u->sink_name);
+ u->sink_name = pa_xstrdup(dn);
+#else
+ pa_xfree(u->source_name);
+ u->source_name = pa_xstrdup(dn);
+#endif
+ }
+
+ if (u->version >= 13) {
+ pa_usec_t usec;
+
+ if (pa_tagstruct_get_usec(t, &usec) < 0)
+ goto parse_error;
+
+/* #ifdef TUNNEL_SINK */
+/* pa_sink_set_latency_range(u->sink, usec + MIN_NETWORK_LATENCY_USEC, 0); */
+/* #else */
+/* pa_source_set_latency_range(u->source, usec + MIN_NETWORK_LATENCY_USEC, 0); */
+/* #endif */
+ }
+
+ if (u->version >= 21) {
+ pa_format_info format;
+
+ if (pa_tagstruct_get_format_info(t, &format) < 0)
+ goto parse_error;
+ }
+
+ if (!pa_tagstruct_eof(t))
+ goto parse_error;
+
start_subscribe(u);
request_info(u);
+ pa_assert(!u->time_event);
+ u->time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + LATENCY_INTERVAL, timeout_callback, u);
+
request_latency(u);
+
+ pa_log_debug("Stream created.");
+
#ifdef TUNNEL_SINK
- send_bytes(u);
+ pa_asyncmsgq_post(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REQUEST, NULL, bytes, NULL, NULL);
#endif
+
+ return;
+
+parse_error:
+ pa_log("Invalid reply. (Create stream)");
+
+fail:
+ pa_module_unload_request(u->module, TRUE);
+
}
+/* Called from main context */
static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
struct userdata *u = userdata;
pa_tagstruct *reply;
@@ -529,347 +1576,357 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
#ifdef TUNNEL_SINK
pa_cvolume volume;
#endif
- assert(pd && u && u->pdispatch == pd);
+
+ pa_assert(pd);
+ pa_assert(u);
+ pa_assert(u->pdispatch == pd);
if (command != PA_COMMAND_REPLY ||
pa_tagstruct_getu32(t, &u->version) < 0 ||
!pa_tagstruct_eof(t)) {
+
if (command == PA_COMMAND_ERROR)
- pa_log(__FILE__": failed to authenticate");
+ pa_log("Failed to authenticate");
else
- pa_log(__FILE__": protocol error.");
- die(u);
- return;
+ pa_log("Protocol error.");
+
+ goto fail;
}
/* Minimum supported protocol version */
if (u->version < 8) {
- pa_log(__FILE__": incompatible protocol version");
- die(u);
- return;
+ pa_log("Incompatible protocol version");
+ goto fail;
}
+ /* Starting with protocol version 13 the MSB of the version tag
+ reflects if shm is enabled for this connection or not. We don't
+ support SHM here at all, so we just ignore this. */
+
+ if (u->version >= 13)
+ u->version &= 0x7FFFFFFFU;
+
+ pa_log_debug("Protocol version: remote %u, local %u", u->version, PA_PROTOCOL_VERSION);
+
#ifdef TUNNEL_SINK
- snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', sink '%s'",
- pa_get_host_name(hn, sizeof(hn)),
- pa_get_user_name(un, sizeof(un)),
- u->sink->name);
+ pa_proplist_setf(u->sink->proplist, "tunnel.remote_version", "%u", u->version);
+ pa_sink_update_proplist(u->sink, 0, NULL);
+
+ pa_snprintf(name, sizeof(name), "%s for %s@%s",
+ u->sink_name,
+ pa_get_user_name(un, sizeof(un)),
+ pa_get_host_name(hn, sizeof(hn)));
#else
- snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', source '%s'",
- pa_get_host_name(hn, sizeof(hn)),
- pa_get_user_name(un, sizeof(un)),
- u->source->name);
+ pa_proplist_setf(u->source->proplist, "tunnel.remote_version", "%u", u->version);
+ pa_source_update_proplist(u->source, 0, NULL);
+
+ pa_snprintf(name, sizeof(name), "%s for %s@%s",
+ u->source_name,
+ pa_get_user_name(un, sizeof(un)),
+ pa_get_host_name(hn, sizeof(hn)));
#endif
-
+
reply = pa_tagstruct_new(NULL, 0);
pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
- pa_tagstruct_putu32(reply, tag = u->ctag++);
- pa_tagstruct_puts(reply, name);
+ pa_tagstruct_putu32(reply, u->ctag++);
+
+ if (u->version >= 13) {
+ pa_proplist *pl;
+ pl = pa_proplist_new();
+ pa_proplist_sets(pl, PA_PROP_APPLICATION_ID, "org.PulseAudio.PulseAudio");
+ pa_proplist_sets(pl, PA_PROP_APPLICATION_VERSION, PACKAGE_VERSION);
+ pa_init_proplist(pl);
+ pa_tagstruct_put_proplist(reply, pl);
+ pa_proplist_free(pl);
+ } else
+ pa_tagstruct_puts(reply, "PulseAudio");
+
pa_pstream_send_tagstruct(u->pstream, reply);
/* We ignore the server's reply here */
reply = pa_tagstruct_new(NULL, 0);
-#ifdef TUNNEL_SINK
+
+ if (u->version < 13)
+ /* Only for older PA versions we need to fill in the maxlength */
+ u->maxlength = 4*1024*1024;
+
+#ifdef TUNNEL_SINK
+ u->tlength = (uint32_t) pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_TLENGTH_MSEC, &u->sink->sample_spec);
+ u->minreq = (uint32_t) pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_MINREQ_MSEC, &u->sink->sample_spec);
+ u->prebuf = u->tlength;
+#else
+ u->fragsize = (uint32_t) pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_FRAGSIZE_MSEC, &u->source->sample_spec);
+#endif
+
+#ifdef TUNNEL_SINK
pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
pa_tagstruct_putu32(reply, tag = u->ctag++);
- pa_tagstruct_puts(reply, name);
+
+ if (u->version < 13)
+ pa_tagstruct_puts(reply, name);
+
pa_tagstruct_put_sample_spec(reply, &u->sink->sample_spec);
pa_tagstruct_put_channel_map(reply, &u->sink->channel_map);
pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
pa_tagstruct_puts(reply, u->sink_name);
- pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
- pa_tagstruct_put_boolean(reply, 0);
- pa_tagstruct_putu32(reply, DEFAULT_TLENGTH);
- pa_tagstruct_putu32(reply, DEFAULT_PREBUF);
- pa_tagstruct_putu32(reply, DEFAULT_MINREQ);
+ pa_tagstruct_putu32(reply, u->maxlength);
+ pa_tagstruct_put_boolean(reply, !PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)));
+ pa_tagstruct_putu32(reply, u->tlength);
+ pa_tagstruct_putu32(reply, u->prebuf);
+ pa_tagstruct_putu32(reply, u->minreq);
pa_tagstruct_putu32(reply, 0);
pa_cvolume_reset(&volume, u->sink->sample_spec.channels);
pa_tagstruct_put_cvolume(reply, &volume);
#else
pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_RECORD_STREAM);
pa_tagstruct_putu32(reply, tag = u->ctag++);
- pa_tagstruct_puts(reply, name);
+
+ if (u->version < 13)
+ pa_tagstruct_puts(reply, name);
+
pa_tagstruct_put_sample_spec(reply, &u->source->sample_spec);
pa_tagstruct_put_channel_map(reply, &u->source->channel_map);
pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
pa_tagstruct_puts(reply, u->source_name);
- pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
- pa_tagstruct_put_boolean(reply, 0);
- pa_tagstruct_putu32(reply, DEFAULT_FRAGSIZE);
+ pa_tagstruct_putu32(reply, u->maxlength);
+ pa_tagstruct_put_boolean(reply, !PA_SOURCE_IS_OPENED(pa_source_get_state(u->source)));
+ pa_tagstruct_putu32(reply, u->fragsize);
#endif
-
+
+ if (u->version >= 12) {
+ pa_tagstruct_put_boolean(reply, FALSE); /* no_remap */
+ pa_tagstruct_put_boolean(reply, FALSE); /* no_remix */
+ pa_tagstruct_put_boolean(reply, FALSE); /* fix_format */
+ pa_tagstruct_put_boolean(reply, FALSE); /* fix_rate */
+ pa_tagstruct_put_boolean(reply, FALSE); /* fix_channels */
+ pa_tagstruct_put_boolean(reply, TRUE); /* no_move */
+ pa_tagstruct_put_boolean(reply, FALSE); /* variable_rate */
+ }
+
+ if (u->version >= 13) {
+ pa_proplist *pl;
+
+ pa_tagstruct_put_boolean(reply, FALSE); /* start muted/peak detect*/
+ pa_tagstruct_put_boolean(reply, TRUE); /* adjust_latency */
+
+ pl = pa_proplist_new();
+ pa_proplist_sets(pl, PA_PROP_MEDIA_NAME, name);
+ pa_proplist_sets(pl, PA_PROP_MEDIA_ROLE, "abstract");
+ pa_tagstruct_put_proplist(reply, pl);
+ pa_proplist_free(pl);
+
+#ifndef TUNNEL_SINK
+ pa_tagstruct_putu32(reply, PA_INVALID_INDEX); /* direct on input */
+#endif
+ }
+
+ if (u->version >= 14) {
+#ifdef TUNNEL_SINK
+ pa_tagstruct_put_boolean(reply, FALSE); /* volume_set */
+#endif
+ pa_tagstruct_put_boolean(reply, TRUE); /* early rquests */
+ }
+
+ if (u->version >= 15) {
+#ifdef TUNNEL_SINK
+ pa_tagstruct_put_boolean(reply, FALSE); /* muted_set */
+#endif
+ pa_tagstruct_put_boolean(reply, FALSE); /* don't inhibit auto suspend */
+ pa_tagstruct_put_boolean(reply, FALSE); /* fail on suspend */
+ }
+
+#ifdef TUNNEL_SINK
+ if (u->version >= 17)
+ pa_tagstruct_put_boolean(reply, FALSE); /* relative volume */
+
+ if (u->version >= 18)
+ pa_tagstruct_put_boolean(reply, FALSE); /* passthrough stream */
+#endif
+
+#ifdef TUNNEL_SINK
+ if (u->version >= 21) {
+ /* We're not using the extended API, so n_formats = 0 and that's that */
+ pa_tagstruct_putu8(t, 0);
+ }
+#endif
+
pa_pstream_send_tagstruct(u->pstream, reply);
pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u, NULL);
+
+ pa_log_debug("Connection authenticated, creating stream ...");
+
+ return;
+
+fail:
+ pa_module_unload_request(u->module, TRUE);
}
+/* Called from main context */
static void pstream_die_callback(pa_pstream *p, void *userdata) {
struct userdata *u = userdata;
- assert(p && u);
- pa_log(__FILE__": stream died.");
- die(u);
-}
+ pa_assert(p);
+ pa_assert(u);
+ pa_log_warn("Stream died.");
+ pa_module_unload_request(u->module, TRUE);
+}
+/* Called from main context */
static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
struct userdata *u = userdata;
- assert(p && packet && u);
+
+ pa_assert(p);
+ pa_assert(packet);
+ pa_assert(u);
if (pa_pdispatch_run(u->pdispatch, packet, creds, u) < 0) {
- pa_log(__FILE__": invalid packet");
- die(u);
+ pa_log("Invalid packet");
+ pa_module_unload_request(u->module, TRUE);
+ return;
}
}
#ifndef TUNNEL_SINK
-static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, PA_GCC_UNUSED int64_t offset, PA_GCC_UNUSED pa_seek_mode_t seek, const pa_memchunk *chunk, void *userdata) {
+/* Called from main context */
+static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t offset, pa_seek_mode_t seek, const pa_memchunk *chunk, void *userdata) {
struct userdata *u = userdata;
- assert(p && chunk && u);
+
+ pa_assert(p);
+ pa_assert(chunk);
+ pa_assert(u);
if (channel != u->channel) {
- pa_log(__FILE__": recieved memory block on bad channel.");
- die(u);
+ pa_log("Received memory block on bad channel.");
+ pa_module_unload_request(u->module, TRUE);
return;
}
-
- pa_source_post(u->source, chunk);
+
+ pa_asyncmsgq_send(u->source->asyncmsgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_POST, PA_UINT_TO_PTR(seek), offset, chunk);
+
+ u->counter_delta += (int64_t) chunk->length;
}
#endif
+/* Called from main context */
static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
struct userdata *u = userdata;
pa_tagstruct *t;
uint32_t tag;
- assert(sc && u && u->client == sc);
+
+ pa_assert(sc);
+ pa_assert(u);
+ pa_assert(u->client == sc);
pa_socket_client_unref(u->client);
u->client = NULL;
-
+
if (!io) {
- pa_log(__FILE__": connection failed.");
- pa_module_unload_request(u->module);
+ pa_log("Connection failed: %s", pa_cstrerror(errno));
+ pa_module_unload_request(u->module, TRUE);
return;
}
- u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->memblock_stat);
- u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
+ u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->mempool);
+ u->pdispatch = pa_pdispatch_new(u->core->mainloop, TRUE, command_table, PA_COMMAND_MAX);
pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u);
#ifndef TUNNEL_SINK
pa_pstream_set_recieve_memblock_callback(u->pstream, pstream_memblock_callback, u);
#endif
-
+
t = pa_tagstruct_new(NULL, 0);
pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
pa_tagstruct_putu32(t, tag = u->ctag++);
pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
- pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
- pa_pstream_send_tagstruct(u->pstream, t);
- pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u, NULL);
-
-}
-
-#ifdef TUNNEL_SINK
-static void sink_notify(pa_sink*sink) {
- struct userdata *u;
- assert(sink && sink->userdata);
- u = sink->userdata;
-
- send_bytes(u);
-}
-static pa_usec_t sink_get_latency(pa_sink *sink) {
- struct userdata *u;
- uint32_t l;
- pa_usec_t usec = 0;
- assert(sink && sink->userdata);
- u = sink->userdata;
+ pa_tagstruct_put_arbitrary(t, pa_auth_cookie_read(u->auth_cookie, PA_NATIVE_COOKIE_LENGTH), PA_NATIVE_COOKIE_LENGTH);
- l = DEFAULT_TLENGTH;
+#ifdef HAVE_CREDS
+{
+ pa_creds ucred;
- if (l > u->requested_bytes) {
- l -= u->requested_bytes;
- usec += pa_bytes_to_usec(l, &u->sink->sample_spec);
- }
+ if (pa_iochannel_creds_supported(io))
+ pa_iochannel_creds_enable(io);
- usec += u->host_latency;
+ ucred.uid = getuid();
+ ucred.gid = getgid();
- return usec;
+ pa_pstream_send_tagstruct_with_creds(u->pstream, t, &ucred);
}
-
-static int sink_get_hw_volume(pa_sink *sink) {
- struct userdata *u;
- assert(sink && sink->userdata);
- u = sink->userdata;
-
- return 0;
-}
-
-static int sink_set_hw_volume(pa_sink *sink) {
- struct userdata *u;
- pa_tagstruct *t;
- uint32_t tag;
- assert(sink && sink->userdata);
- u = sink->userdata;
-
- t = pa_tagstruct_new(NULL, 0);
- pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_VOLUME);
- pa_tagstruct_putu32(t, tag = u->ctag++);
-
- pa_tagstruct_putu32(t, PA_INVALID_INDEX);
- pa_tagstruct_puts(t, u->sink_name);
- pa_tagstruct_put_cvolume(t, &sink->hw_volume);
+#else
pa_pstream_send_tagstruct(u->pstream, t);
+#endif
- return 0;
-}
-
-static int sink_get_hw_mute(pa_sink *sink) {
- struct userdata *u;
- assert(sink && sink->userdata);
- u = sink->userdata;
+ pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u, NULL);
- return 0;
+ pa_log_debug("Connection established, authenticating ...");
}
-static int sink_set_hw_mute(pa_sink *sink) {
+#ifdef TUNNEL_SINK
+
+/* Called from main context */
+static void sink_set_volume(pa_sink *sink) {
struct userdata *u;
pa_tagstruct *t;
- uint32_t tag;
- assert(sink && sink->userdata);
+
+ pa_assert(sink);
u = sink->userdata;
+ pa_assert(u);
t = pa_tagstruct_new(NULL, 0);
- pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_MUTE);
- pa_tagstruct_putu32(t, tag = u->ctag++);
-
- pa_tagstruct_putu32(t, PA_INVALID_INDEX);
- pa_tagstruct_puts(t, u->sink_name);
- pa_tagstruct_put_boolean(t, !!sink->hw_muted);
+ pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_INPUT_VOLUME);
+ pa_tagstruct_putu32(t, u->ctag++);
+ pa_tagstruct_putu32(t, u->device_index);
+ pa_tagstruct_put_cvolume(t, &sink->real_volume);
pa_pstream_send_tagstruct(u->pstream, t);
-
- return 0;
-}
-#else
-static pa_usec_t source_get_latency(pa_source *source) {
- struct userdata *u;
- assert(source && source->userdata);
- u = source->userdata;
-
- return u->host_latency;
}
-static int source_get_hw_volume(pa_source *source) {
- struct userdata *u;
- assert(source && source->userdata);
- u = source->userdata;
-
- return 0;
-}
-
-static int source_set_hw_volume(pa_source *source) {
+/* Called from main context */
+static void sink_set_mute(pa_sink *sink) {
struct userdata *u;
pa_tagstruct *t;
- uint32_t tag;
- assert(source && source->userdata);
- u = source->userdata;
-
- t = pa_tagstruct_new(NULL, 0);
- pa_tagstruct_putu32(t, PA_COMMAND_SET_SOURCE_VOLUME);
- pa_tagstruct_putu32(t, tag = u->ctag++);
-
- pa_tagstruct_putu32(t, PA_INVALID_INDEX);
- pa_tagstruct_puts(t, u->source_name);
- pa_tagstruct_put_cvolume(t, &source->hw_volume);
- pa_pstream_send_tagstruct(u->pstream, t);
-
- return 0;
-}
-
-static int source_get_hw_mute(pa_source *source) {
- struct userdata *u;
- assert(source && source->userdata);
- u = source->userdata;
- return 0;
-}
+ pa_assert(sink);
+ u = sink->userdata;
+ pa_assert(u);
-static int source_set_hw_mute(pa_source *source) {
- struct userdata *u;
- pa_tagstruct *t;
- uint32_t tag;
- assert(source && source->userdata);
- u = source->userdata;
+ if (u->version < 11)
+ return;
t = pa_tagstruct_new(NULL, 0);
- pa_tagstruct_putu32(t, PA_COMMAND_SET_SOURCE_MUTE);
- pa_tagstruct_putu32(t, tag = u->ctag++);
-
- pa_tagstruct_putu32(t, PA_INVALID_INDEX);
- pa_tagstruct_puts(t, u->source_name);
- pa_tagstruct_put_boolean(t, !!source->hw_muted);
+ pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_INPUT_MUTE);
+ pa_tagstruct_putu32(t, u->ctag++);
+ pa_tagstruct_putu32(t, u->device_index);
+ pa_tagstruct_put_boolean(t, !!sink->muted);
pa_pstream_send_tagstruct(u->pstream, t);
-
- return 0;
}
-#endif
-
-static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
- struct userdata *u = userdata;
- struct timeval ntv;
- assert(m && e && u);
-
- request_latency(u);
-
- pa_gettimeofday(&ntv);
- ntv.tv_sec += LATENCY_INTERVAL;
- m->time_restart(e, &ntv);
-}
-
-static int load_key(struct userdata *u, const char*fn) {
- assert(u);
-
- u->auth_cookie_in_property = 0;
-
- if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
- pa_log_debug(__FILE__": using already loaded auth cookie.");
- pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
- u->auth_cookie_in_property = 1;
- return 0;
- }
-
- if (!fn)
- fn = PA_NATIVE_COOKIE_FILE;
-
- if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
- return -1;
-
- pa_log_debug(__FILE__": loading cookie from disk.");
-
- if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
- u->auth_cookie_in_property = 1;
- return 0;
-}
+#endif
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
struct userdata *u = NULL;
pa_sample_spec ss;
pa_channel_map map;
- struct timeval ntv;
- assert(c && m);
+ char *dn = NULL;
+#ifdef TUNNEL_SINK
+ pa_sink_new_data data;
+#else
+ pa_source_new_data data;
+#endif
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto fail;
}
- u = pa_xmalloc(sizeof(struct userdata));
- m->userdata = u;
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
u->module = m;
- u->core = c;
u->client = NULL;
u->pdispatch = NULL;
u->pstream = NULL;
@@ -882,97 +1939,230 @@ int pa__init(pa_core *c, pa_module*m) {
u->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));;
u->source = NULL;
#endif
+ u->smoother = pa_smoother_new(
+ PA_USEC_PER_SEC,
+ PA_USEC_PER_SEC*2,
+ TRUE,
+ TRUE,
+ 10,
+ pa_rtclock_now(),
+ FALSE);
u->ctag = 1;
u->device_index = u->channel = PA_INVALID_INDEX;
- u->host_latency = 0;
- u->auth_cookie_in_property = 0;
u->time_event = NULL;
-
- if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
+ u->ignore_latency_before = 0;
+ u->transport_usec = u->thread_transport_usec = 0;
+ u->remote_suspended = u->remote_corked = FALSE;
+ u->counter = u->counter_delta = 0;
+
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+ if (!(u->auth_cookie = pa_auth_cookie_get(u->core, pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), PA_NATIVE_COOKIE_LENGTH)))
goto fail;
-
+
if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
- pa_log(__FILE__": no server specified.");
+ pa_log("No server specified.");
goto fail;
}
- ss = c->default_sample_spec;
+ ss = m->core->default_sample_spec;
+ map = m->core->default_channel_map;
if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
- pa_log(__FILE__": invalid sample format specification");
+ pa_log("Invalid sample format specification");
goto fail;
}
- if (!(u->client = pa_socket_client_new_string(c->mainloop, u->server_name, PA_NATIVE_DEFAULT_PORT))) {
- pa_log(__FILE__": failed to connect to server '%s'", u->server_name);
+ if (!(u->client = pa_socket_client_new_string(m->core->mainloop, TRUE, u->server_name, PA_NATIVE_DEFAULT_PORT))) {
+ pa_log("Failed to connect to server '%s'", u->server_name);
goto fail;
}
-
- if (!u->client)
- goto fail;
pa_socket_client_set_callback(u->client, on_connection, u);
#ifdef TUNNEL_SINK
- if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": failed to create sink.");
+
+ if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
+ dn = pa_sprintf_malloc("tunnel-sink.%s", u->server_name);
+
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ data.namereg_fail = TRUE;
+ pa_sink_new_data_set_name(&data, dn);
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "%s%s%s", pa_strempty(u->sink_name), u->sink_name ? " on " : "", u->server_name);
+ pa_proplist_sets(data.proplist, "tunnel.remote.server", u->server_name);
+ if (u->sink_name)
+ pa_proplist_sets(data.proplist, "tunnel.remote.sink", u->sink_name);
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&data);
goto fail;
}
- u->sink->notify = sink_notify;
- u->sink->get_latency = sink_get_latency;
- u->sink->get_hw_volume = sink_get_hw_volume;
- u->sink->set_hw_volume = sink_set_hw_volume;
- u->sink->get_hw_mute = sink_get_hw_mute;
- u->sink->set_hw_mute = sink_set_hw_mute;
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_NETWORK|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL|PA_SINK_HW_MUTE_CTRL);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg;
u->sink->userdata = u;
- u->sink->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->sink_name ? u->sink_name : "", u->sink_name ? "@" : "", u->server_name);
+ u->sink->set_state = sink_set_state;
+ u->sink->set_volume = sink_set_volume;
+ u->sink->set_mute = sink_set_mute;
+
+ u->sink->refresh_volume = u->sink->refresh_muted = FALSE;
+
+/* pa_sink_set_latency_range(u->sink, MIN_NETWORK_LATENCY_USEC, 0); */
+
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
- pa_sink_set_owner(u->sink, m);
#else
- if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
- pa_log(__FILE__": failed to create source.");
+
+ if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
+ dn = pa_sprintf_malloc("tunnel-source.%s", u->server_name);
+
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ data.namereg_fail = TRUE;
+ pa_source_new_data_set_name(&data, dn);
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "%s%s%s", pa_strempty(u->source_name), u->source_name ? " on " : "", u->server_name);
+ pa_proplist_sets(data.proplist, "tunnel.remote.server", u->server_name);
+ if (u->source_name)
+ pa_proplist_sets(data.proplist, "tunnel.remote.source", u->source_name);
+
+ if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&data);
+ goto fail;
+ }
+
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_NETWORK|PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
+ pa_log("Failed to create source.");
goto fail;
}
- u->source->get_latency = source_get_latency;
- u->source->get_hw_volume = source_get_hw_volume;
- u->source->set_hw_volume = source_set_hw_volume;
- u->source->get_hw_mute = source_get_hw_mute;
- u->source->set_hw_mute = source_set_hw_mute;
+ u->source->parent.process_msg = source_process_msg;
+ u->source->set_state = source_set_state;
u->source->userdata = u;
- u->source->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->source_name ? u->source_name : "", u->source_name ? "@" : "", u->server_name);
- pa_source_set_owner(u->source, m);
+/* pa_source_set_latency_range(u->source, MIN_NETWORK_LATENCY_USEC, 0); */
+
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+
+ u->mcalign = pa_mcalign_new(pa_frame_size(&u->source->sample_spec));
+#endif
+
+ pa_xfree(dn);
+
+ u->time_event = NULL;
+
+ u->maxlength = (uint32_t) -1;
+#ifdef TUNNEL_SINK
+ u->tlength = u->minreq = u->prebuf = (uint32_t) -1;
+#else
+ u->fragsize = (uint32_t) -1;
+#endif
+
+ if (!(u->thread = pa_thread_new("module-tunnel", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+#ifdef TUNNEL_SINK
+ pa_sink_put(u->sink);
+#else
+ pa_source_put(u->source);
#endif
-
- pa_gettimeofday(&ntv);
- ntv.tv_sec += LATENCY_INTERVAL;
- u->time_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, u);
pa_modargs_free(ma);
return 0;
-
+
fail:
- pa__done(c, m);
+ pa__done(m);
if (ma)
pa_modargs_free(ma);
- return -1;
+
+ pa_xfree(dn);
+
+ return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata* u;
- assert(c && m);
+
+ pa_assert(m);
if (!(u = m->userdata))
return;
- close_stuff(u);
+#ifdef TUNNEL_SINK
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+#else
+ if (u->source)
+ pa_source_unlink(u->source);
+#endif
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+#ifdef TUNNEL_SINK
+ if (u->sink)
+ pa_sink_unref(u->sink);
+#else
+ if (u->source)
+ pa_source_unref(u->source);
+#endif
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ if (u->pstream) {
+ pa_pstream_unlink(u->pstream);
+ pa_pstream_unref(u->pstream);
+ }
+
+ if (u->pdispatch)
+ pa_pdispatch_unref(u->pdispatch);
+
+ if (u->client)
+ pa_socket_client_unref(u->client);
+
+ if (u->auth_cookie)
+ pa_auth_cookie_unref(u->auth_cookie);
+
+ if (u->smoother)
+ pa_smoother_free(u->smoother);
+
+ if (u->time_event)
+ u->core->mainloop->time_free(u->time_event);
+
+#ifndef TUNNEL_SINK
+ if (u->mcalign)
+ pa_mcalign_free(u->mcalign);
+#endif
- if (u->auth_cookie_in_property)
- pa_authkey_prop_unref(c, PA_NATIVE_COOKIE_PROPERTY_NAME);
-
#ifdef TUNNEL_SINK
pa_xfree(u->sink_name);
#else
@@ -980,7 +2170,9 @@ void pa__done(pa_core *c, pa_module*m) {
#endif
pa_xfree(u->server_name);
+ pa_xfree(u->device_description);
+ pa_xfree(u->server_fqdn);
+ pa_xfree(u->user_name);
+
pa_xfree(u);
}
-
-
diff --git a/src/modules/module-udev-detect.c b/src/modules/module-udev-detect.c
new file mode 100644
index 00000000..63ad1952
--- /dev/null
+++ b/src/modules/module-udev-detect.c
@@ -0,0 +1,809 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <limits.h>
+#include <dirent.h>
+#include <sys/inotify.h>
+#include <libudev.h>
+
+#include <pulse/timeval.h>
+
+#include <pulsecore/modargs.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/ratelimit.h>
+
+#include "module-udev-detect-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_USAGE(
+ "tsched=<enable system timer based scheduling mode?> "
+ "ignore_dB=<ignore dB information from the device?> "
+ "sync_volume=<syncronize sw and hw voluchanges in IO-thread?>");
+
+struct device {
+ char *path;
+ pa_bool_t need_verify;
+ char *card_name;
+ char *args;
+ uint32_t module;
+ pa_ratelimit ratelimit;
+};
+
+struct userdata {
+ pa_core *core;
+ pa_hashmap *devices;
+
+ pa_bool_t use_tsched:1;
+ pa_bool_t ignore_dB:1;
+ pa_bool_t sync_volume:1;
+
+ struct udev* udev;
+ struct udev_monitor *monitor;
+ pa_io_event *udev_io;
+
+ int inotify_fd;
+ pa_io_event *inotify_io;
+};
+
+static const char* const valid_modargs[] = {
+ "tsched",
+ "ignore_dB",
+ "sync_volume",
+ NULL
+};
+
+static int setup_inotify(struct userdata *u);
+
+static void device_free(struct device *d) {
+ pa_assert(d);
+
+ pa_xfree(d->path);
+ pa_xfree(d->card_name);
+ pa_xfree(d->args);
+ pa_xfree(d);
+}
+
+static const char *path_get_card_id(const char *path) {
+ const char *e;
+
+ if (!path)
+ return NULL;
+
+ if (!(e = strrchr(path, '/')))
+ return NULL;
+
+ if (!pa_startswith(e, "/card"))
+ return NULL;
+
+ return e + 5;
+}
+
+static char *card_get_sysattr(const char *card_idx, const char *name) {
+ struct udev *udev;
+ struct udev_device *card = NULL;
+ char *t, *r = NULL;
+ const char *v;
+
+ pa_assert(card_idx);
+ pa_assert(name);
+
+ if (!(udev = udev_new())) {
+ pa_log_error("Failed to allocate udev context.");
+ goto finish;
+ }
+
+ t = pa_sprintf_malloc("%s/class/sound/card%s", udev_get_sys_path(udev), card_idx);
+ card = udev_device_new_from_syspath(udev, t);
+ pa_xfree(t);
+
+ if (!card) {
+ pa_log_error("Failed to get card object.");
+ goto finish;
+ }
+
+ if ((v = udev_device_get_sysattr_value(card, name)) && *v)
+ r = pa_xstrdup(v);
+
+finish:
+
+ if (card)
+ udev_device_unref(card);
+
+ if (udev)
+ udev_unref(udev);
+
+ return r;
+}
+
+static pa_bool_t pcm_is_modem(const char *card_idx, const char *pcm) {
+ char *sysfs_path, *pcm_class;
+ pa_bool_t is_modem;
+
+ pa_assert(card_idx);
+ pa_assert(pcm);
+
+ /* Check /sys/class/sound/card.../pcmC...../pcm_class. An HDA
+ * modem can be used simultaneously with generic
+ * playback/record. */
+
+ sysfs_path = pa_sprintf_malloc("pcmC%sD%s/pcm_class", card_idx, pcm);
+ pcm_class = card_get_sysattr(card_idx, sysfs_path);
+ is_modem = pcm_class && pa_streq(pcm_class, "modem");
+ pa_xfree(pcm_class);
+ pa_xfree(sysfs_path);
+
+ return is_modem;
+}
+
+static pa_bool_t is_card_busy(const char *id) {
+ char *card_path = NULL, *pcm_path = NULL, *sub_status = NULL;
+ DIR *card_dir = NULL, *pcm_dir = NULL;
+ FILE *status_file = NULL;
+ size_t len;
+ struct dirent *space = NULL, *de;
+ pa_bool_t busy = FALSE;
+ int r;
+
+ pa_assert(id);
+
+ /* This simply uses /proc/asound/card.../pcm.../sub.../status to
+ * check whether there is still a process using this audio device. */
+
+ card_path = pa_sprintf_malloc("/proc/asound/card%s", id);
+
+ if (!(card_dir = opendir(card_path))) {
+ pa_log_warn("Failed to open %s: %s", card_path, pa_cstrerror(errno));
+ goto fail;
+ }
+
+ len = offsetof(struct dirent, d_name) + fpathconf(dirfd(card_dir), _PC_NAME_MAX) + 1;
+ space = pa_xmalloc(len);
+
+ for (;;) {
+ de = NULL;
+
+ if ((r = readdir_r(card_dir, space, &de)) != 0) {
+ pa_log_warn("readdir_r() failed: %s", pa_cstrerror(r));
+ goto fail;
+ }
+
+ if (!de)
+ break;
+
+ if (!pa_startswith(de->d_name, "pcm"))
+ continue;
+
+ if (pcm_is_modem(id, de->d_name + 3))
+ continue;
+
+ pa_xfree(pcm_path);
+ pcm_path = pa_sprintf_malloc("%s/%s", card_path, de->d_name);
+
+ if (pcm_dir)
+ closedir(pcm_dir);
+
+ if (!(pcm_dir = opendir(pcm_path))) {
+ pa_log_warn("Failed to open %s: %s", pcm_path, pa_cstrerror(errno));
+ continue;
+ }
+
+ for (;;) {
+ char line[32];
+
+ if ((r = readdir_r(pcm_dir, space, &de)) != 0) {
+ pa_log_warn("readdir_r() failed: %s", pa_cstrerror(r));
+ goto fail;
+ }
+
+ if (!de)
+ break;
+
+ if (!pa_startswith(de->d_name, "sub"))
+ continue;
+
+ pa_xfree(sub_status);
+ sub_status = pa_sprintf_malloc("%s/%s/status", pcm_path, de->d_name);
+
+ if (status_file)
+ fclose(status_file);
+
+ if (!(status_file = pa_fopen_cloexec(sub_status, "r"))) {
+ pa_log_warn("Failed to open %s: %s", sub_status, pa_cstrerror(errno));
+ continue;
+ }
+
+ if (!(fgets(line, sizeof(line)-1, status_file))) {
+ pa_log_warn("Failed to read from %s: %s", sub_status, pa_cstrerror(errno));
+ continue;
+ }
+
+ if (!pa_streq(line, "closed\n")) {
+ busy = TRUE;
+ break;
+ }
+ }
+ }
+
+fail:
+
+ pa_xfree(card_path);
+ pa_xfree(pcm_path);
+ pa_xfree(sub_status);
+ pa_xfree(space);
+
+ if (card_dir)
+ closedir(card_dir);
+
+ if (pcm_dir)
+ closedir(pcm_dir);
+
+ if (status_file)
+ fclose(status_file);
+
+ return busy;
+}
+
+static void verify_access(struct userdata *u, struct device *d) {
+ char *cd;
+ pa_card *card;
+ pa_bool_t accessible;
+
+ pa_assert(u);
+ pa_assert(d);
+
+ cd = pa_sprintf_malloc("%s/snd/controlC%s", udev_get_dev_path(u->udev), path_get_card_id(d->path));
+ accessible = access(cd, R_OK|W_OK) >= 0;
+ pa_log_debug("%s is accessible: %s", cd, pa_yes_no(accessible));
+
+ pa_xfree(cd);
+
+ if (d->module == PA_INVALID_INDEX) {
+
+ /* If we are not loaded, try to load */
+
+ if (accessible) {
+ pa_module *m;
+ pa_bool_t busy;
+
+ /* Check if any of the PCM devices that belong to this
+ * card are currently busy. If they are, don't try to load
+ * right now, to make sure the probing phase can
+ * successfully complete. When the current user of the
+ * device closes it we will get another notification via
+ * inotify and can then recheck. */
+
+ busy = is_card_busy(path_get_card_id(d->path));
+ pa_log_debug("%s is busy: %s", d->path, pa_yes_no(busy));
+
+ if (!busy) {
+
+ /* So, why do we rate limit here? It's certainly ugly,
+ * but there seems to be no other way. Problem is
+ * this: if we are unable to configure/probe an audio
+ * device after opening it we will close it again and
+ * the module initialization will fail. This will then
+ * cause an inotify event on the device node which
+ * will be forwarded to us. We then try to reopen the
+ * audio device again, practically entering a busy
+ * loop.
+ *
+ * A clean fix would be if we would be able to ignore
+ * our own inotify close events. However, inotify
+ * lacks such functionality. Also, during probing of
+ * the device we cannot really distuingish between
+ * other processes causing EBUSY or ourselves, which
+ * means we have no way to figure out if the probing
+ * during opening was canceled by a "try again"
+ * failure or a "fatal" failure. */
+
+ if (pa_ratelimit_test(&d->ratelimit, PA_LOG_DEBUG)) {
+ pa_log_debug("Loading module-alsa-card with arguments '%s'", d->args);
+ m = pa_module_load(u->core, "module-alsa-card", d->args);
+
+ if (m) {
+ d->module = m->index;
+ pa_log_info("Card %s (%s) module loaded.", d->path, d->card_name);
+ } else
+ pa_log_info("Card %s (%s) failed to load module.", d->path, d->card_name);
+ } else
+ pa_log_warn("Tried to configure %s (%s) more often than %u times in %llus",
+ d->path,
+ d->card_name,
+ d->ratelimit.burst,
+ (long long unsigned) (d->ratelimit.interval / PA_USEC_PER_SEC));
+ }
+ }
+
+ } else {
+
+ /* If we are already loaded update suspend status with
+ * accessible boolean */
+
+ if ((card = pa_namereg_get(u->core, d->card_name, PA_NAMEREG_CARD)))
+ pa_card_suspend(card, !accessible, PA_SUSPEND_SESSION);
+ }
+}
+
+static void card_changed(struct userdata *u, struct udev_device *dev) {
+ struct device *d;
+ const char *path;
+ const char *t;
+ char *n;
+
+ pa_assert(u);
+ pa_assert(dev);
+
+ /* Maybe /dev/snd is now available? */
+ setup_inotify(u);
+
+ path = udev_device_get_devpath(dev);
+
+ if ((d = pa_hashmap_get(u->devices, path))) {
+ verify_access(u, d);
+ return;
+ }
+
+ d = pa_xnew0(struct device, 1);
+ d->path = pa_xstrdup(path);
+ d->module = PA_INVALID_INDEX;
+ PA_INIT_RATELIMIT(d->ratelimit, 10*PA_USEC_PER_SEC, 5);
+
+ if (!(t = udev_device_get_property_value(dev, "PULSE_NAME")))
+ if (!(t = udev_device_get_property_value(dev, "ID_ID")))
+ if (!(t = udev_device_get_property_value(dev, "ID_PATH")))
+ t = path_get_card_id(path);
+
+ n = pa_namereg_make_valid_name(t);
+ d->card_name = pa_sprintf_malloc("alsa_card.%s", n);
+ d->args = pa_sprintf_malloc("device_id=\"%s\" "
+ "name=\"%s\" "
+ "card_name=\"%s\" "
+ "namereg_fail=false "
+ "tsched=%s "
+ "ignore_dB=%s "
+ "sync_volume=%s "
+ "card_properties=\"module-udev-detect.discovered=1\"",
+ path_get_card_id(path),
+ n,
+ d->card_name,
+ pa_yes_no(u->use_tsched),
+ pa_yes_no(u->ignore_dB),
+ pa_yes_no(u->sync_volume));
+ pa_xfree(n);
+
+ pa_hashmap_put(u->devices, d->path, d);
+
+ verify_access(u, d);
+}
+
+static void remove_card(struct userdata *u, struct udev_device *dev) {
+ struct device *d;
+
+ pa_assert(u);
+ pa_assert(dev);
+
+ if (!(d = pa_hashmap_remove(u->devices, udev_device_get_devpath(dev))))
+ return;
+
+ pa_log_info("Card %s removed.", d->path);
+
+ if (d->module != PA_INVALID_INDEX)
+ pa_module_unload_request_by_index(u->core, d->module, TRUE);
+
+ device_free(d);
+}
+
+static void process_device(struct userdata *u, struct udev_device *dev) {
+ const char *action, *ff;
+
+ pa_assert(u);
+ pa_assert(dev);
+
+ if (udev_device_get_property_value(dev, "PULSE_IGNORE")) {
+ pa_log_debug("Ignoring %s, because marked so.", udev_device_get_devpath(dev));
+ return;
+ }
+
+ if ((ff = udev_device_get_property_value(dev, "SOUND_CLASS")) &&
+ pa_streq(ff, "modem")) {
+ pa_log_debug("Ignoring %s, because it is a modem.", udev_device_get_devpath(dev));
+ return;
+ }
+
+ action = udev_device_get_action(dev);
+
+ if (action && pa_streq(action, "remove"))
+ remove_card(u, dev);
+ else if ((!action || pa_streq(action, "change")) && udev_device_get_property_value(dev, "SOUND_INITIALIZED"))
+ card_changed(u, dev);
+
+ /* For an explanation why we don't look for 'add' events here
+ * have a look into /lib/udev/rules.d/78-sound-card.rules! */
+}
+
+static void process_path(struct userdata *u, const char *path) {
+ struct udev_device *dev;
+
+ if (!path_get_card_id(path))
+ return;
+
+ if (!(dev = udev_device_new_from_syspath(u->udev, path))) {
+ pa_log("Failed to get udev device object from udev.");
+ return;
+ }
+
+ process_device(u, dev);
+ udev_device_unref(dev);
+}
+
+static void monitor_cb(
+ pa_mainloop_api*a,
+ pa_io_event* e,
+ int fd,
+ pa_io_event_flags_t events,
+ void *userdata) {
+
+ struct userdata *u = userdata;
+ struct udev_device *dev;
+
+ pa_assert(a);
+
+ if (!(dev = udev_monitor_receive_device(u->monitor))) {
+ pa_log("Failed to get udev device object from monitor.");
+ goto fail;
+ }
+
+ if (!path_get_card_id(udev_device_get_devpath(dev))) {
+ udev_device_unref(dev);
+ return;
+ }
+
+ process_device(u, dev);
+ udev_device_unref(dev);
+ return;
+
+fail:
+ a->io_free(u->udev_io);
+ u->udev_io = NULL;
+}
+
+static pa_bool_t pcm_node_belongs_to_device(
+ struct device *d,
+ const char *node) {
+
+ char *cd;
+ pa_bool_t b;
+
+ cd = pa_sprintf_malloc("pcmC%sD", path_get_card_id(d->path));
+ b = pa_startswith(node, cd);
+ pa_xfree(cd);
+
+ return b;
+}
+
+static pa_bool_t control_node_belongs_to_device(
+ struct device *d,
+ const char *node) {
+
+ char *cd;
+ pa_bool_t b;
+
+ cd = pa_sprintf_malloc("controlC%s", path_get_card_id(d->path));
+ b = pa_streq(node, cd);
+ pa_xfree(cd);
+
+ return b;
+}
+
+static void inotify_cb(
+ pa_mainloop_api*a,
+ pa_io_event* e,
+ int fd,
+ pa_io_event_flags_t events,
+ void *userdata) {
+
+ struct {
+ struct inotify_event e;
+ char name[NAME_MAX];
+ } buf;
+ struct userdata *u = userdata;
+ static int type = 0;
+ pa_bool_t deleted = FALSE;
+ struct device *d;
+ void *state;
+
+ for (;;) {
+ ssize_t r;
+ struct inotify_event *event;
+
+ pa_zero(buf);
+ if ((r = pa_read(fd, &buf, sizeof(buf), &type)) <= 0) {
+
+ if (r < 0 && errno == EAGAIN)
+ break;
+
+ pa_log("read() from inotify failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
+ goto fail;
+ }
+
+ event = &buf.e;
+ while (r > 0) {
+ size_t len;
+
+ if ((size_t) r < sizeof(struct inotify_event)) {
+ pa_log("read() too short.");
+ goto fail;
+ }
+
+ len = sizeof(struct inotify_event) + event->len;
+
+ if ((size_t) r < len) {
+ pa_log("Payload missing.");
+ goto fail;
+ }
+
+ /* From udev we get the guarantee that the control
+ * device's ACL is changed last. To avoid races when ACLs
+ * are changed we hence watch only the control device */
+ if (((event->mask & IN_ATTRIB) && pa_startswith(event->name, "controlC")))
+ PA_HASHMAP_FOREACH(d, u->devices, state)
+ if (control_node_belongs_to_device(d, event->name))
+ d->need_verify = TRUE;
+
+ /* ALSA doesn't really give us any guarantee on the closing
+ * order, so let's simply hope */
+ if (((event->mask & IN_CLOSE_WRITE) && pa_startswith(event->name, "pcmC")))
+ PA_HASHMAP_FOREACH(d, u->devices, state)
+ if (pcm_node_belongs_to_device(d, event->name))
+ d->need_verify = TRUE;
+
+ /* /dev/snd/ might have been removed */
+ if ((event->mask & (IN_DELETE_SELF|IN_MOVE_SELF)))
+ deleted = TRUE;
+
+ event = (struct inotify_event*) ((uint8_t*) event + len);
+ r -= len;
+ }
+ }
+
+ PA_HASHMAP_FOREACH(d, u->devices, state)
+ if (d->need_verify) {
+ d->need_verify = FALSE;
+ verify_access(u, d);
+ }
+
+ if (!deleted)
+ return;
+
+fail:
+ if (u->inotify_io) {
+ a->io_free(u->inotify_io);
+ u->inotify_io = NULL;
+ }
+
+ if (u->inotify_fd >= 0) {
+ pa_close(u->inotify_fd);
+ u->inotify_fd = -1;
+ }
+}
+
+static int setup_inotify(struct userdata *u) {
+ char *dev_snd;
+ int r;
+
+ if (u->inotify_fd >= 0)
+ return 0;
+
+ if ((u->inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
+ pa_log("inotify_init1() failed: %s", pa_cstrerror(errno));
+ return -1;
+ }
+
+ dev_snd = pa_sprintf_malloc("%s/snd", udev_get_dev_path(u->udev));
+ r = inotify_add_watch(u->inotify_fd, dev_snd, IN_ATTRIB|IN_CLOSE_WRITE|IN_DELETE_SELF|IN_MOVE_SELF);
+ pa_xfree(dev_snd);
+
+ if (r < 0) {
+ int saved_errno = errno;
+
+ pa_close(u->inotify_fd);
+ u->inotify_fd = -1;
+
+ if (saved_errno == ENOENT) {
+ pa_log_debug("/dev/snd/ is apparently not existing yet, retrying to create inotify watch later.");
+ return 0;
+ }
+
+ if (saved_errno == ENOSPC) {
+ pa_log("You apparently ran out of inotify watches, probably because Tracker/Beagle took them all away. "
+ "I wished people would do their homework first and fix inotify before using it for watching whole "
+ "directory trees which is something the current inotify is certainly not useful for. "
+ "Please make sure to drop the Tracker/Beagle guys a line complaining about their broken use of inotify.");
+ return 0;
+ }
+
+ pa_log("inotify_add_watch() failed: %s", pa_cstrerror(saved_errno));
+ return -1;
+ }
+
+ pa_assert_se(u->inotify_io = u->core->mainloop->io_new(u->core->mainloop, u->inotify_fd, PA_IO_EVENT_INPUT, inotify_cb, u));
+
+ return 0;
+}
+
+int pa__init(pa_module *m) {
+ struct userdata *u = NULL;
+ pa_modargs *ma;
+ struct udev_enumerate *enumerate = NULL;
+ struct udev_list_entry *item = NULL, *first = NULL;
+ int fd;
+ pa_bool_t use_tsched = TRUE, ignore_dB = FALSE, sync_volume = m->core->sync_volume;
+
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ u->inotify_fd = -1;
+
+ if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
+ pa_log("Failed to parse tsched= argument.");
+ goto fail;
+ }
+ u->use_tsched = use_tsched;
+
+ if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
+ pa_log("Failed to parse ignore_dB= argument.");
+ goto fail;
+ }
+ u->ignore_dB = ignore_dB;
+
+ if (pa_modargs_get_value_boolean(ma, "sync_volume", &sync_volume) < 0) {
+ pa_log("Failed to parse sync_volume= argument.");
+ goto fail;
+ }
+ u->sync_volume = sync_volume;
+
+ if (!(u->udev = udev_new())) {
+ pa_log("Failed to initialize udev library.");
+ goto fail;
+ }
+
+ if (setup_inotify(u) < 0)
+ goto fail;
+
+ if (!(u->monitor = udev_monitor_new_from_netlink(u->udev, "udev"))) {
+ pa_log("Failed to initialize monitor.");
+ goto fail;
+ }
+
+ if (udev_monitor_filter_add_match_subsystem_devtype(u->monitor, "sound", NULL) < 0) {
+ pa_log("Failed to subscribe to sound devices.");
+ goto fail;
+ }
+
+ errno = 0;
+ if (udev_monitor_enable_receiving(u->monitor) < 0) {
+ pa_log("Failed to enable monitor: %s", pa_cstrerror(errno));
+ if (errno == EPERM)
+ pa_log_info("Most likely your kernel is simply too old and "
+ "allows only priviliged processes to listen to device events. "
+ "Please upgrade your kernel to at least 2.6.30.");
+ goto fail;
+ }
+
+ if ((fd = udev_monitor_get_fd(u->monitor)) < 0) {
+ pa_log("Failed to get udev monitor fd.");
+ goto fail;
+ }
+
+ pa_assert_se(u->udev_io = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, monitor_cb, u));
+
+ if (!(enumerate = udev_enumerate_new(u->udev))) {
+ pa_log("Failed to initialize udev enumerator.");
+ goto fail;
+ }
+
+ if (udev_enumerate_add_match_subsystem(enumerate, "sound") < 0) {
+ pa_log("Failed to match to subsystem.");
+ goto fail;
+ }
+
+ if (udev_enumerate_scan_devices(enumerate) < 0) {
+ pa_log("Failed to scan for devices.");
+ goto fail;
+ }
+
+ first = udev_enumerate_get_list_entry(enumerate);
+ udev_list_entry_foreach(item, first)
+ process_path(u, udev_list_entry_get_name(item));
+
+ udev_enumerate_unref(enumerate);
+
+ pa_log_info("Found %u cards.", pa_hashmap_size(u->devices));
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+
+ if (enumerate)
+ udev_enumerate_unref(enumerate);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->udev_io)
+ m->core->mainloop->io_free(u->udev_io);
+
+ if (u->monitor)
+ udev_monitor_unref(u->monitor);
+
+ if (u->udev)
+ udev_unref(u->udev);
+
+ if (u->inotify_io)
+ m->core->mainloop->io_free(u->inotify_io);
+
+ if (u->inotify_fd >= 0)
+ pa_close(u->inotify_fd);
+
+ if (u->devices) {
+ struct device *d;
+
+ while ((d = pa_hashmap_steal_first(u->devices)))
+ device_free(d);
+
+ pa_hashmap_free(u->devices, NULL, NULL);
+ }
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-virtual-sink.c b/src/modules/module-virtual-sink.c
new file mode 100644
index 00000000..a6be2446
--- /dev/null
+++ b/src/modules/module-virtual-sink.c
@@ -0,0 +1,669 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2010 Intel Corporation
+ Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* TODO: Some plugins cause latency, and some even report it by using a control
+ out port. We don't currently use the latency information. */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/gccmacro.h>
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+
+#include <pulsecore/namereg.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/ltdl-helper.h>
+
+#include "module-virtual-sink-symdef.h"
+
+PA_MODULE_AUTHOR("Pierre-Louis Bossart");
+PA_MODULE_DESCRIPTION(_("Virtual sink"));
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ _("sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "master=<name of sink to filter> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "use_volume_sharing=<yes or no> "
+ "force_flat_volume=<yes or no> "
+ ));
+
+#define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
+
+struct userdata {
+ pa_module *module;
+
+ /* FIXME: Uncomment this and take "autoloaded" as a modarg if this is a filter */
+ /* pa_bool_t autoloaded; */
+
+ pa_sink *sink;
+ pa_sink_input *sink_input;
+
+ pa_memblockq *memblockq;
+
+ pa_bool_t auto_desc;
+ unsigned channels;
+};
+
+static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
+ "master",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "use_volume_sharing",
+ "force_flat_volume",
+ NULL
+};
+
+/* Called from I/O thread context */
+static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_GET_LATENCY:
+
+ /* The sink is _put() before the sink input is, so let's
+ * make sure we don't access it in that time. Also, the
+ * sink input is first shut down, the sink second. */
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
+ *((pa_usec_t*) data) = 0;
+ return 0;
+ }
+
+ *((pa_usec_t*) data) =
+
+ /* Get the latency of the master sink */
+ pa_sink_get_latency_within_thread(u->sink_input->sink) +
+
+ /* Add the latency internal to our sink input on top */
+ pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
+
+ return 0;
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from main context */
+static int sink_set_state_cb(pa_sink *s, pa_sink_state_t state) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(state) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return 0;
+
+ pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void sink_request_rewind_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_request_rewind(u->sink_input,
+ s->thread_info.rewind_nbytes +
+ pa_memblockq_get_length(u->memblockq), TRUE, FALSE, FALSE);
+}
+
+/* Called from I/O thread context */
+static void sink_update_requested_latency_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+ !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+ return;
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_set_requested_latency_within_thread(
+ u->sink_input,
+ pa_sink_get_requested_latency_within_thread(s));
+}
+
+/* Called from main context */
+static void sink_set_volume_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return;
+
+ pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, TRUE);
+}
+
+/* Called from main context */
+static void sink_set_mute_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
+ !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+ return;
+
+ pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
+}
+
+/* Called from I/O thread context */
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
+ struct userdata *u;
+ float *src, *dst;
+ size_t fs;
+ unsigned n, c;
+ pa_memchunk tchunk;
+ pa_usec_t current_latency PA_GCC_UNUSED;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert(chunk);
+ pa_assert_se(u = i->userdata);
+
+ /* Hmm, process any rewind request that might be queued up */
+ pa_sink_process_rewind(u->sink, 0);
+
+ /* (1) IF YOU NEED A FIXED BLOCK SIZE USE
+ * pa_memblockq_peek_fixed_size() HERE INSTEAD. NOTE THAT FILTERS
+ * WHICH CAN DEAL WITH DYNAMIC BLOCK SIZES ARE HIGHLY
+ * PREFERRED. */
+ while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
+ pa_memchunk nchunk;
+
+ pa_sink_render(u->sink, nbytes, &nchunk);
+ pa_memblockq_push(u->memblockq, &nchunk);
+ pa_memblock_unref(nchunk.memblock);
+ }
+
+ /* (2) IF YOU NEED A FIXED BLOCK SIZE, THIS NEXT LINE IS NOT
+ * NECESSARY */
+ tchunk.length = PA_MIN(nbytes, tchunk.length);
+ pa_assert(tchunk.length > 0);
+
+ fs = pa_frame_size(&i->sample_spec);
+ n = (unsigned) (tchunk.length / fs);
+
+ pa_assert(n > 0);
+
+ chunk->index = 0;
+ chunk->length = n*fs;
+ chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
+
+ pa_memblockq_drop(u->memblockq, chunk->length);
+
+ src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
+ dst = (float*) pa_memblock_acquire(chunk->memblock);
+
+ /* (3) PUT YOUR CODE HERE TO DO SOMETHING WITH THE DATA */
+
+ /* As an example, copy input to output */
+ for (c = 0; c < u->channels; c++) {
+ pa_sample_clamp(PA_SAMPLE_FLOAT32NE,
+ dst+c, u->channels * sizeof(float),
+ src+c, u->channels * sizeof(float),
+ n);
+ }
+
+ pa_memblock_release(tchunk.memblock);
+ pa_memblock_release(chunk->memblock);
+
+ pa_memblock_unref(tchunk.memblock);
+
+ /* (4) IF YOU NEED THE LATENCY FOR SOMETHING ACQUIRE IT LIKE THIS: */
+ current_latency =
+ /* Get the latency of the master sink */
+ pa_sink_get_latency_within_thread(i->sink) +
+
+ /* Add the latency internal to our sink input on top */
+ pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
+
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+ size_t amount = 0;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ if (u->sink->thread_info.rewind_nbytes > 0) {
+ size_t max_rewrite;
+
+ max_rewrite = nbytes + pa_memblockq_get_length(u->memblockq);
+ amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
+ u->sink->thread_info.rewind_nbytes = 0;
+
+ if (amount > 0) {
+ pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE);
+
+ /* (5) PUT YOUR CODE HERE TO RESET YOUR FILTER */
+ }
+ }
+
+ pa_sink_process_rewind(u->sink, amount);
+ pa_memblockq_rewind(u->memblockq, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_memblockq_set_maxrewind(u->memblockq, nbytes);
+ pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* (6) IF YOU NEED A FIXED BLOCK SIZE ROUND nbytes UP TO MULTIPLES
+ * OF IT HERE. THE PA_ROUND_UP MACRO IS USEFUL FOR THAT. */
+
+ pa_sink_set_max_request_within_thread(u->sink, nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* (7) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
+ * BLOCK MINUS ONE SAMPLE HERE. pa_usec_to_bytes_round_up() IS
+ * USEFUL FOR THAT. */
+
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_detach_within_thread(u->sink);
+
+ pa_sink_set_rtpoll(u->sink, NULL);
+}
+
+/* Called from I/O thread context */
+static void sink_input_attach_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
+ pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+
+ /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
+ * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
+ pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+
+ /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
+ * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
+ * HERE. SEE (6) */
+ pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
+ pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
+
+ pa_sink_attach_within_thread(u->sink);
+}
+
+/* Called from main context */
+static void sink_input_kill_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* The order here matters! We first kill the sink input, followed
+ * by the sink. That means the sink callbacks must be protected
+ * against an unconnected sink input! */
+ pa_sink_input_unlink(u->sink_input);
+ pa_sink_unlink(u->sink);
+
+ pa_sink_input_unref(u->sink_input);
+ u->sink_input = NULL;
+
+ pa_sink_unref(u->sink);
+ u->sink = NULL;
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from IO thread context */
+static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ /* If we are added for the first time, ask for a rewinding so that
+ * we are heard right-away. */
+ if (PA_SINK_INPUT_IS_LINKED(state) &&
+ i->thread_info.state == PA_SINK_INPUT_INIT) {
+ pa_log_debug("Requesting rewind due to state change.");
+ pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
+ }
+}
+
+/* Called from main context */
+static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ return u->sink != dest;
+}
+
+/* Called from main context */
+static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ if (dest) {
+ pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
+ pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
+ } else
+ pa_sink_set_asyncmsgq(u->sink, NULL);
+
+ if (u->auto_desc && dest) {
+ const char *z;
+ pa_proplist *pl;
+
+ pl = pa_proplist_new();
+ z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Virtual Sink %s on %s",
+ pa_proplist_gets(u->sink->proplist, "device.vsink.name"), z ? z : dest->name);
+
+ pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
+ pa_proplist_free(pl);
+ }
+}
+
+/* Called from main context */
+static void sink_input_volume_changed_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_volume_changed(u->sink, &i->volume);
+}
+
+/* Called from main context */
+static void sink_input_mute_changed_cb(pa_sink_input *i) {
+ struct userdata *u;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
+
+ pa_sink_mute_changed(u->sink, i->muted);
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_modargs *ma;
+ pa_sink *master=NULL;
+ pa_sink_input_new_data sink_input_data;
+ pa_sink_new_data sink_data;
+ pa_bool_t use_volume_sharing = FALSE;
+ pa_bool_t force_flat_volume = FALSE;
+ pa_memchunk silence;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
+ pa_log("Master sink not found");
+ goto fail;
+ }
+
+ pa_assert(master);
+
+ ss = master->sample_spec;
+ ss.format = PA_SAMPLE_FLOAT32;
+ map = master->channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+ pa_log("Invalid sample format specification or channel map");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "use_volume_sharing", &use_volume_sharing) < 0) {
+ pa_log("use_volume_sharing= expects a boolean argument");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "force_flat_volume", &force_flat_volume) < 0) {
+ pa_log("force_flat_volume= expects a boolean argument");
+ goto fail;
+ }
+
+ if (use_volume_sharing && force_flat_volume) {
+ pa_log("Flat volume can't be forced when using volume sharing.");
+ goto fail;
+ }
+
+ u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ m->userdata = u;
+ u->channels = ss.channels;
+
+ /* Create sink */
+ pa_sink_new_data_init(&sink_data);
+ sink_data.driver = __FILE__;
+ sink_data.module = m;
+ if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
+ sink_data.name = pa_sprintf_malloc("%s.vsink", master->name);
+ pa_sink_new_data_set_sample_spec(&sink_data, &ss);
+ pa_sink_new_data_set_channel_map(&sink_data, &map);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
+ pa_proplist_sets(sink_data.proplist, "device.vsink.name", sink_data.name);
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&sink_data);
+ goto fail;
+ }
+
+ if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
+ const char *z;
+
+ z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Virtual Sink %s on %s", sink_data.name, z ? z : master->name);
+ }
+
+ u->sink = pa_sink_new(m->core, &sink_data, (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY))
+ | (use_volume_sharing ? PA_SINK_SHARE_VOLUME_WITH_MASTER : 0)
+ | (force_flat_volume ? PA_SINK_FLAT_VOLUME : 0));
+ pa_sink_new_data_done(&sink_data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg_cb;
+ u->sink->set_state = sink_set_state_cb;
+ u->sink->update_requested_latency = sink_update_requested_latency_cb;
+ u->sink->request_rewind = sink_request_rewind_cb;
+ u->sink->set_volume = use_volume_sharing ? NULL : sink_set_volume_cb;
+ u->sink->set_mute = sink_set_mute_cb;
+ u->sink->userdata = u;
+
+ pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
+
+ /* Create sink input */
+ pa_sink_input_new_data_init(&sink_input_data);
+ sink_input_data.driver = __FILE__;
+ sink_input_data.module = m;
+ pa_sink_input_new_data_set_sink(&sink_input_data, master, FALSE);
+ sink_input_data.origin_sink = u->sink;
+ pa_proplist_setf(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Virtual Sink Stream from %s", pa_proplist_gets(u->sink->proplist, PA_PROP_DEVICE_DESCRIPTION));
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
+ pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
+ pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
+
+ pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
+ pa_sink_input_new_data_done(&sink_input_data);
+
+ if (!u->sink_input)
+ goto fail;
+
+ u->sink_input->pop = sink_input_pop_cb;
+ u->sink_input->process_rewind = sink_input_process_rewind_cb;
+ u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
+ u->sink_input->update_max_request = sink_input_update_max_request_cb;
+ u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
+ u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
+ u->sink_input->kill = sink_input_kill_cb;
+ u->sink_input->attach = sink_input_attach_cb;
+ u->sink_input->detach = sink_input_detach_cb;
+ u->sink_input->state_change = sink_input_state_change_cb;
+ u->sink_input->may_move_to = sink_input_may_move_to_cb;
+ u->sink_input->moving = sink_input_moving_cb;
+ u->sink_input->volume_changed = use_volume_sharing ? NULL : sink_input_volume_changed_cb;
+ u->sink_input->mute_changed = sink_input_mute_changed_cb;
+ u->sink_input->userdata = u;
+
+ u->sink->input_to_master = u->sink_input;
+
+ pa_sink_input_get_silence(u->sink_input, &silence);
+ u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, &silence);
+ pa_memblock_unref(silence.memblock);
+
+ /* (9) INITIALIZE ANYTHING ELSE YOU NEED HERE */
+
+ pa_sink_put(u->sink);
+ pa_sink_input_put(u->sink_input);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ /* See comments in sink_input_kill_cb() above regarding
+ * destruction order! */
+
+ if (u->sink_input)
+ pa_sink_input_unlink(u->sink_input);
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->sink_input)
+ pa_sink_input_unref(u->sink_input);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->memblockq)
+ pa_memblockq_free(u->memblockq);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-virtual-source.c b/src/modules/module-virtual-source.c
new file mode 100644
index 00000000..e15f4b94
--- /dev/null
+++ b/src/modules/module-virtual-source.c
@@ -0,0 +1,746 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2010 Intel Corporation
+ Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+
+#include <pulsecore/macro.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/ltdl-helper.h>
+
+#include "module-virtual-source-symdef.h"
+
+PA_MODULE_AUTHOR("Pierre-Louis Bossart");
+PA_MODULE_DESCRIPTION("Virtual source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ _("source_name=<name for the source> "
+ "source_properties=<properties for the source> "
+ "master=<name of source to filter> "
+ "uplink_sink=<name> (optional)"
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "use_volume_sharing=<yes or no> "
+ "force_flat_volume=<yes or no> "
+ ));
+
+#define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
+#define BLOCK_USEC 1000 /* FIXME */
+
+struct userdata {
+ pa_module *module;
+
+ /* FIXME: Uncomment this and take "autoloaded" as a modarg if this is a filter */
+ /* pa_bool_t autoloaded; */
+
+ pa_source *source;
+ pa_source_output *source_output;
+
+ pa_memblockq *memblockq;
+
+ pa_bool_t auto_desc;
+ unsigned channels;
+
+ /* optional fields for uplink sink */
+ pa_sink *sink;
+ pa_usec_t block_usec;
+ pa_memblockq *sink_memblockq;
+
+};
+
+static const char* const valid_modargs[] = {
+ "source_name",
+ "source_properties",
+ "master",
+ "uplink_sink",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "use_volume_sharing",
+ "force_flat_volume",
+ NULL
+};
+
+/* Called from I/O thread context */
+static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_GET_LATENCY:
+
+ /* there's no real latency here */
+ *((pa_usec_t*) data) = 0;
+
+ return 0;
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from main context */
+static int sink_set_state_cb(pa_sink *s, pa_sink_state_t state) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SINK_IS_LINKED(state)) {
+ return 0;
+ }
+
+ if (state == PA_SINK_RUNNING) {
+ /* need to wake-up source if it was suspended */
+ pa_source_suspend(u->source, FALSE, PA_SUSPEND_ALL);
+
+ /* FIXME: if there's no client connected, the source will suspend
+ and playback will be stuck. You'd want to prevent the source from
+ sleeping when the uplink sink is active; even if the audio is
+ discarded at least the app isn't stuck */
+
+ } else {
+ /* nothing to do, if the sink becomes idle or suspended let
+ module-suspend-idle handle the sources later */
+ }
+
+ return 0;
+}
+
+static void sink_update_requested_latency_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ /* FIXME: there's no latency support */
+
+}
+
+
+/* Called from I/O thread context */
+static void sink_request_rewind_cb(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ /* Do nothing */
+ pa_sink_process_rewind(u->sink, 0);
+
+}
+
+/* Called from I/O thread context */
+static int source_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE(o)->userdata;
+
+ switch (code) {
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY:
+
+ /* The source is _put() before the source output is, so let's
+ * make sure we don't access it in that time. Also, the
+ * source output is first shut down, the source second. */
+ if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state)) {
+ *((pa_usec_t*) data) = 0;
+ return 0;
+ }
+
+ *((pa_usec_t*) data) =
+
+ /* Get the latency of the master source */
+ pa_source_get_latency_within_thread(u->source_output->source) +
+
+ /* Add the latency internal to our source output on top */
+ /* FIXME, no idea what I am doing here */
+ pa_bytes_to_usec(pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq), &u->source_output->source->sample_spec);
+
+ return 0;
+ }
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from main context */
+static int source_set_state_cb(pa_source *s, pa_source_state_t state) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(state) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
+ return 0;
+
+ pa_source_output_cork(u->source_output, state == PA_SOURCE_SUSPENDED);
+ return 0;
+}
+
+/* Called from I/O thread context */
+static void source_update_requested_latency_cb(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state))
+ return;
+
+ /* Just hand this one over to the master source */
+ pa_source_output_set_requested_latency_within_thread(
+ u->source_output,
+ pa_source_get_requested_latency_within_thread(s));
+}
+
+/* Called from main context */
+static void source_set_volume_cb(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
+ return;
+
+ pa_source_output_set_volume(u->source_output, &s->real_volume, s->save_volume, TRUE);
+}
+
+/* Called from main context */
+static void source_set_mute_cb(pa_source *s) {
+ struct userdata *u;
+
+ pa_source_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
+ !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
+ return;
+
+ pa_source_output_set_mute(u->source_output, s->muted, s->save_muted);
+}
+
+/* Called from input thread context */
+static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output))) {
+ pa_log("push when no link?");
+ return;
+ }
+
+ /* PUT YOUR CODE HERE TO DO SOMETHING WITH THE SOURCE DATA */
+
+ /* if uplink sink exists, pull data from there; simplify by using
+ same length as chunk provided by source */
+ if(u->sink && (pa_sink_get_state(u->sink) == PA_SINK_RUNNING)) {
+ pa_memchunk tchunk;
+ size_t nbytes = chunk->length;
+ pa_mix_info streams[2];
+ pa_memchunk target_chunk;
+ void *target;
+ int ch;
+
+ /* Hmm, process any rewind request that might be queued up */
+ pa_sink_process_rewind(u->sink, 0);
+
+ /* get data from the sink */
+ while (pa_memblockq_peek(u->sink_memblockq, &tchunk) < 0) {
+ pa_memchunk nchunk;
+
+ /* make sure we get nbytes from the sink with render_full,
+ otherwise we cannot mix with the uplink */
+ pa_sink_render_full(u->sink, nbytes, &nchunk);
+ pa_memblockq_push(u->sink_memblockq, &nchunk);
+ pa_memblock_unref(nchunk.memblock);
+ }
+ pa_assert(tchunk.length == chunk->length);
+
+ /* move the read pointer for sink memblockq */
+ pa_memblockq_drop(u->sink_memblockq, tchunk.length);
+
+ /* allocate target chunk */
+ /* this could probably be done in-place, but having chunk as both
+ the input and output creates issues with reference counts */
+ target_chunk.index = 0;
+ target_chunk.length = chunk->length;
+ pa_assert(target_chunk.length == chunk->length);
+
+ target_chunk.memblock = pa_memblock_new(o->source->core->mempool,
+ target_chunk.length);
+ pa_assert( target_chunk.memblock );
+
+ /* get target pointer */
+ target = (void*)((uint8_t*)pa_memblock_acquire(target_chunk.memblock)
+ + target_chunk.index);
+
+ /* set-up mixing structure
+ volume was taken care of in sink and source already */
+ streams[0].chunk = *chunk;
+ for(ch=0;ch<o->sample_spec.channels;ch++)
+ streams[0].volume.values[ch] = PA_VOLUME_NORM; /* FIXME */
+ streams[0].volume.channels = o->sample_spec.channels;
+
+ streams[1].chunk = tchunk;
+ for(ch=0;ch<o->sample_spec.channels;ch++)
+ streams[1].volume.values[ch] = PA_VOLUME_NORM; /* FIXME */
+ streams[1].volume.channels = o->sample_spec.channels;
+
+ /* do mixing */
+ pa_mix(streams, /* 2 streams to be mixed */
+ 2,
+ target, /* put result in target chunk */
+ chunk->length, /* same length as input */
+ (const pa_sample_spec *)&o->sample_spec, /* same sample spec for input and output */
+ NULL, /* no volume information */
+ FALSE); /* no mute */
+
+ pa_memblock_release(target_chunk.memblock);
+ pa_memblock_unref(tchunk.memblock); /* clean-up */
+
+ /* forward the data to the virtual source */
+ pa_source_post(u->source, &target_chunk);
+
+ pa_memblock_unref(target_chunk.memblock); /* clean-up */
+
+ } else {
+ /* forward the data to the virtual source */
+ pa_source_post(u->source, chunk);
+ }
+
+
+}
+
+/* Called from input thread context */
+static void source_output_process_rewind_cb(pa_source_output *o, size_t nbytes) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ /* FIXME, no idea what I am doing here */
+#if 0
+ pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_REWIND, NULL, (int64_t) nbytes, NULL, NULL);
+ u->send_counter -= (int64_t) nbytes;
+#endif
+}
+
+/* Called from output thread context */
+static int source_output_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+
+ /* FIXME, nothing to do here ? */
+
+ return pa_source_output_process_msg(obj, code, data, offset, chunk);
+}
+
+/* Called from output thread context */
+static void source_output_attach_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_source_set_rtpoll(u->source, o->source->thread_info.rtpoll);
+ pa_source_set_latency_range_within_thread(u->source, o->source->thread_info.min_latency, o->source->thread_info.max_latency);
+ pa_source_set_fixed_latency_within_thread(u->source, o->source->thread_info.fixed_latency);
+ pa_source_set_max_rewind_within_thread(u->source, pa_source_output_get_max_rewind(o));
+
+ pa_source_attach_within_thread(u->source);
+}
+
+/* Called from output thread context */
+static void source_output_detach_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ pa_source_detach_within_thread(u->source);
+ pa_source_set_rtpoll(u->source, NULL);
+}
+
+/* Called from output thread context */
+static void source_output_state_change_cb(pa_source_output *o, pa_source_output_state_t state) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_source_output_assert_io_context(o);
+ pa_assert_se(u = o->userdata);
+
+ /* FIXME */
+#if 0
+ if (PA_SOURCE_OUTPUT_IS_LINKED(state) && o->thread_info.state == PA_SOURCE_OUTPUT_INIT) {
+
+ u->skip = pa_usec_to_bytes(PA_CLIP_SUB(pa_source_get_latency_within_thread(o->source),
+ u->latency),
+ &o->sample_spec);
+
+ pa_log_info("Skipping %lu bytes", (unsigned long) u->skip);
+ }
+#endif
+}
+
+/* Called from main thread */
+static void source_output_kill_cb(pa_source_output *o) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert_se(u = o->userdata);
+
+ /* The order here matters! We first kill the source output, followed
+ * by the source. That means the source callbacks must be protected
+ * against an unconnected source output! */
+ pa_source_output_unlink(u->source_output);
+ pa_source_unlink(u->source);
+
+ pa_source_output_unref(u->source_output);
+ u->source_output = NULL;
+
+ pa_source_unref(u->source);
+ u->source = NULL;
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from main thread */
+static pa_bool_t source_output_may_move_to_cb(pa_source_output *o, pa_source *dest) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert_se(u = o->userdata);
+
+ /* FIXME */
+ //return dest != u->source_input->source->monitor_source;
+
+ return TRUE;
+}
+
+/* Called from main thread */
+static void source_output_moving_cb(pa_source_output *o, pa_source *dest) {
+ struct userdata *u;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert_se(u = o->userdata);
+
+ if (dest) {
+ pa_source_set_asyncmsgq(u->source, dest->asyncmsgq);
+ pa_source_update_flags(u->source, PA_SOURCE_LATENCY|PA_SOURCE_DYNAMIC_LATENCY, dest->flags);
+ } else
+ pa_source_set_asyncmsgq(u->source, NULL);
+
+ if (u->auto_desc && dest) {
+ const char *z;
+ pa_proplist *pl;
+
+ pl = pa_proplist_new();
+ z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Virtual Source %s on %s",
+ pa_proplist_gets(u->source->proplist, "device.vsource.name"), z ? z : dest->name);
+
+ pa_source_update_proplist(u->source, PA_UPDATE_REPLACE, pl);
+ pa_proplist_free(pl);
+ }
+}
+
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_modargs *ma;
+ pa_source *master=NULL;
+ pa_source_output_new_data source_output_data;
+ pa_source_new_data source_data;
+ pa_bool_t use_volume_sharing = FALSE;
+ pa_bool_t force_flat_volume = FALSE;
+
+ /* optional for uplink_sink */
+ pa_sink_new_data sink_data;
+ size_t nbytes;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SOURCE))) {
+ pa_log("Master source not found");
+ goto fail;
+ }
+
+ pa_assert(master);
+
+ ss = master->sample_spec;
+ ss.format = PA_SAMPLE_FLOAT32;
+ map = master->channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+ pa_log("Invalid sample format specification or channel map");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "use_volume_sharing", &use_volume_sharing) < 0) {
+ pa_log("use_volume_sharing= expects a boolean argument");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "force_flat_volume", &force_flat_volume) < 0) {
+ pa_log("force_flat_volume= expects a boolean argument");
+ goto fail;
+ }
+
+ if (use_volume_sharing && force_flat_volume) {
+ pa_log("Flat volume can't be forced when using volume sharing.");
+ goto fail;
+ }
+
+ u = pa_xnew0(struct userdata, 1);
+ if (!u) {
+ pa_log("Failed to alloc userdata");
+ goto fail;
+ }
+ u->module = m;
+ m->userdata = u;
+ u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
+ if (!u->memblockq) {
+ pa_log("Failed to create source memblockq.");
+ goto fail;
+ }
+ u->channels = ss.channels;
+
+ /* Create source */
+ pa_source_new_data_init(&source_data);
+ source_data.driver = __FILE__;
+ source_data.module = m;
+ if (!(source_data.name = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
+ source_data.name = pa_sprintf_malloc("%s.vsource", master->name);
+ pa_source_new_data_set_sample_spec(&source_data, &ss);
+ pa_source_new_data_set_channel_map(&source_data, &map);
+ pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
+ pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
+ pa_proplist_sets(source_data.proplist, "device.vsource.name", source_data.name);
+
+ if (pa_modargs_get_proplist(ma, "source_properties", source_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&source_data);
+ goto fail;
+ }
+
+ if ((u->auto_desc = !pa_proplist_contains(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
+ const char *z;
+
+ z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Virtual Source %s on %s", source_data.name, z ? z : master->name);
+ }
+
+ u->source = pa_source_new(m->core, &source_data, (master->flags & (PA_SOURCE_LATENCY|PA_SOURCE_DYNAMIC_LATENCY))
+ | (use_volume_sharing ? PA_SOURCE_SHARE_VOLUME_WITH_MASTER : 0)
+ | (force_flat_volume ? PA_SOURCE_FLAT_VOLUME : 0));
+
+ pa_source_new_data_done(&source_data);
+
+ if (!u->source) {
+ pa_log("Failed to create source.");
+ goto fail;
+ }
+
+ u->source->parent.process_msg = source_process_msg_cb;
+ u->source->set_state = source_set_state_cb;
+ u->source->update_requested_latency = source_update_requested_latency_cb;
+ u->source->set_volume = use_volume_sharing ? NULL : source_set_volume_cb;
+ u->source->set_mute = source_set_mute_cb;
+ u->source->userdata = u;
+
+ pa_source_set_asyncmsgq(u->source, master->asyncmsgq);
+
+ /* Create source output */
+ pa_source_output_new_data_init(&source_output_data);
+ source_output_data.driver = __FILE__;
+ source_output_data.module = m;
+ pa_source_output_new_data_set_source(&source_output_data, master, FALSE);
+ source_output_data.destination_source = u->source;
+ /* FIXME
+ source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
+
+ pa_proplist_setf(source_output_data.proplist, PA_PROP_MEDIA_NAME, "Virtual Source Stream of %s", pa_proplist_gets(u->source->proplist, PA_PROP_DEVICE_DESCRIPTION));
+ pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
+ pa_source_output_new_data_set_sample_spec(&source_output_data, &ss);
+ pa_source_output_new_data_set_channel_map(&source_output_data, &map);
+
+ pa_source_output_new(&u->source_output, m->core, &source_output_data);
+ pa_source_output_new_data_done(&source_output_data);
+
+ if (!u->source_output)
+ goto fail;
+
+ u->source_output->parent.process_msg = source_output_process_msg_cb;
+ u->source_output->push = source_output_push_cb;
+ u->source_output->process_rewind = source_output_process_rewind_cb;
+ u->source_output->kill = source_output_kill_cb;
+ u->source_output->attach = source_output_attach_cb;
+ u->source_output->detach = source_output_detach_cb;
+ u->source_output->state_change = source_output_state_change_cb;
+ u->source_output->may_move_to = source_output_may_move_to_cb;
+ u->source_output->moving = source_output_moving_cb;
+ u->source_output->userdata = u;
+
+ u->source->output_from_master = u->source_output;
+
+ pa_source_put(u->source);
+ pa_source_output_put(u->source_output);
+
+ /* Create optional uplink sink */
+ pa_sink_new_data_init(&sink_data);
+ sink_data.driver = __FILE__;
+ sink_data.module = m;
+ if ((sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "uplink_sink", NULL)))) {
+ pa_sink_new_data_set_sample_spec(&sink_data, &ss);
+ pa_sink_new_data_set_channel_map(&sink_data, &map);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "uplink sink");
+ pa_proplist_sets(sink_data.proplist, "device.uplink_sink.name", sink_data.name);
+
+ if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
+ const char *z;
+
+ z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Uplink Sink %s on %s", sink_data.name, z ? z : master->name);
+ }
+
+ u->sink_memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
+ if (!u->sink_memblockq) {
+ pa_log("Failed to create sink memblockq.");
+ goto fail;
+ }
+
+ u->sink = pa_sink_new(m->core, &sink_data, 0); /* FIXME, sink has no capabilities */
+ pa_sink_new_data_done(&sink_data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg_cb;
+ u->sink->update_requested_latency = sink_update_requested_latency_cb;
+ u->sink->request_rewind = sink_request_rewind_cb;
+ u->sink->set_state = sink_set_state_cb;
+ u->sink->userdata = u;
+
+ pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
+
+ /* FIXME: no idea what I am doing here */
+ u->block_usec = BLOCK_USEC;
+ nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
+ pa_sink_set_max_rewind(u->sink, nbytes);
+ pa_sink_set_max_request(u->sink, nbytes);
+
+ pa_sink_put(u->sink);
+ } else {
+ /* optional uplink sink not enabled */
+ u->sink = NULL;
+ }
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_source_linked_by(u->source);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ /* See comments in source_output_kill_cb() above regarding
+ * destruction order! */
+
+ if (u->source_output)
+ pa_source_output_unlink(u->source_output);
+
+ if (u->source)
+ pa_source_unlink(u->source);
+
+ if (u->source_output)
+ pa_source_output_unref(u->source_output);
+
+ if (u->source)
+ pa_source_unref(u->source);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->memblockq)
+ pa_memblockq_free(u->memblockq);
+
+ if (u->sink_memblockq)
+ pa_memblockq_free(u->sink_memblockq);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-volume-restore.c b/src/modules/module-volume-restore.c
index d0956509..a344c5eb 100644
--- a/src/modules/module-volume-restore.c
+++ b/src/modules/module-volume-restore.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -23,348 +23,63 @@
#include <config.h>
#endif
-#include <unistd.h>
-#include <assert.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-
#include <pulse/xmalloc.h>
-#include <pulsecore/core-error.h>
#include <pulsecore/module.h>
-#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
-#include <pulsecore/core-subscribe.h>
-#include <pulsecore/sink-input.h>
#include <pulsecore/core-util.h>
-#include <pulse/volume.h>
#include "module-volume-restore-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Playback stream automatic volume restore module")
-PA_MODULE_USAGE("table=<filename>")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-
-#define WHITESPACE "\n\r \t"
-
-#define DEFAULT_VOLUME_TABLE_FILE "volume.table"
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Compatibility module");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_DEPRECATED("Please use module-stream-restore instead of module-volume-restore!");
static const char* const valid_modargs[] = {
"table",
+ "restore_device",
+ "restore_volume",
NULL,
};
-struct rule {
- char* name;
- pa_cvolume volume;
-};
-
-struct userdata {
- pa_hashmap *hashmap;
- pa_subscription *subscription;
- int modified;
- char *table_file;
-};
-
-static pa_cvolume* parse_volume(const char *s, pa_cvolume *v) {
- char *p;
- long k;
- unsigned i;
-
- assert(s);
- assert(v);
-
- if (!isdigit(*s))
- return NULL;
-
- k = strtol(s, &p, 0);
- if (k <= 0 || k > PA_CHANNELS_MAX)
- return NULL;
-
- v->channels = (unsigned) k;
-
- for (i = 0; i < v->channels; i++) {
- p += strspn(p, WHITESPACE);
-
- if (!isdigit(*p))
- return NULL;
-
- k = strtol(p, &p, 0);
-
- if (k < PA_VOLUME_MUTED)
- return NULL;
-
- v->values[i] = (pa_volume_t) k;
- }
-
- if (*p != 0)
- return NULL;
-
- return v;
-}
-
-static int load_rules(struct userdata *u) {
- FILE *f;
- int n = 0;
- int ret = -1;
- char buf_name[256], buf_volume[256];
- char *ln = buf_name;
-
- f = u->table_file ?
- fopen(u->table_file, "r") :
- pa_open_config_file(NULL, DEFAULT_VOLUME_TABLE_FILE, NULL, &u->table_file, "r");
-
- if (!f) {
- if (errno == ENOENT) {
- pa_log_info(__FILE__": starting with empty ruleset.");
- ret = 0;
- } else
- pa_log(__FILE__": failed to open file '%s': %s", u->table_file, pa_cstrerror(errno));
-
- goto finish;
- }
-
- pa_lock_fd(fileno(f), 1);
-
- while (!feof(f)) {
- struct rule *rule;
- pa_cvolume v;
-
- if (!fgets(ln, sizeof(buf_name), f))
- break;
-
- n++;
-
- pa_strip_nl(ln);
-
- if (ln[0] == '#' || !*ln )
- continue;
-
- if (ln == buf_name) {
- ln = buf_volume;
- continue;
- }
-
- assert(ln == buf_volume);
-
- if (!parse_volume(buf_volume, &v)) {
- pa_log(__FILE__": parse failure in %s:%u, stopping parsing", u->table_file, n);
- goto finish;
- }
-
- ln = buf_name;
-
- if (pa_hashmap_get(u->hashmap, buf_name)) {
- pa_log(__FILE__": double entry in %s:%u, ignoring", u->table_file, n);
- goto finish;
- }
-
- rule = pa_xnew(struct rule, 1);
- rule->name = pa_xstrdup(buf_name);
- rule->volume = v;
-
- pa_hashmap_put(u->hashmap, rule->name, rule);
- }
-
- if (ln == buf_volume) {
- pa_log(__FILE__": invalid number of lines in %s.", u->table_file);
- goto finish;
- }
-
- ret = 0;
-
-finish:
- if (f) {
- pa_lock_fd(fileno(f), 0);
- fclose(f);
- }
-
- return ret;
-}
-
-static int save_rules(struct userdata *u) {
- FILE *f;
- int ret = -1;
- void *state = NULL;
- struct rule *rule;
-
- f = u->table_file ?
- fopen(u->table_file, "w") :
- pa_open_config_file(NULL, DEFAULT_VOLUME_TABLE_FILE, NULL, &u->table_file, "w");
-
- if (!f) {
- pa_log(__FILE__": failed to open file '%s': %s", u->table_file, pa_cstrerror(errno));
- goto finish;
- }
-
- pa_lock_fd(fileno(f), 1);
-
- while ((rule = pa_hashmap_iterate(u->hashmap, &state, NULL))) {
- unsigned i;
-
- fprintf(f, "%s\n%u", rule->name, rule->volume.channels);
-
- for (i = 0; i < rule->volume.channels; i++)
- fprintf(f, " %u", rule->volume.values[i]);
-
- fprintf(f, "\n");
- }
-
- ret = 0;
-
-finish:
- if (f) {
- pa_lock_fd(fileno(f), 0);
- fclose(f);
- }
-
- return ret;
-}
-
-static char* client_name(pa_client *c) {
- char *t, *e;
-
- if (!c->name || !c->driver)
- return NULL;
-
- t = pa_sprintf_malloc("%s$%s", c->driver, c->name);
- t[strcspn(t, "\n\r#")] = 0;
-
- if (!*t)
- return NULL;
-
- if ((e = strrchr(t, '('))) {
- char *k = e + 1 + strspn(e + 1, "0123456789-");
-
- /* Dirty trick: truncate all trailing parens with numbers in
- * between, since they are usually used to identify multiple
- * sessions of the same application, which is something we
- * explicitly don't want. Besides other stuff this makes xmms
- * with esound work properly for us. */
-
- if (*k == ')' && *(k+1) == 0)
- *e = 0;
- }
-
- return t;
-}
-
-static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
- struct userdata *u = userdata;
- pa_sink_input *si;
- struct rule *r;
- char *name;
-
- assert(c);
- assert(u);
-
- if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
- t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
- return;
-
- if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
- return;
-
- if (!si->client || !(name = client_name(si->client)))
- return;
-
- if ((r = pa_hashmap_get(u->hashmap, name))) {
- pa_xfree(name);
-
- if (((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) && si->sample_spec.channels == r->volume.channels) {
- pa_log_info(__FILE__": Restoring volume for <%s>", r->name);
- pa_sink_input_set_volume(si, &r->volume);
- } else if (!pa_cvolume_equal(pa_sink_input_get_volume(si), &r->volume)) {
- pa_log_info(__FILE__": Saving volume for <%s>", r->name);
- r->volume = *pa_sink_input_get_volume(si);
- u->modified = 1;
- }
-
- } else {
- pa_log_info(__FILE__": Creating new entry for <%s>", name);
-
- r = pa_xnew(struct rule, 1);
- r->name = name;
- r->volume = *pa_sink_input_get_volume(si);
- pa_hashmap_put(u->hashmap, r->name, r);
-
- u->modified = 1;
- }
-}
-
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
- struct userdata *u;
-
- assert(c);
- assert(m);
+ pa_bool_t restore_device = TRUE, restore_volume = TRUE;
+ pa_module *n;
+ char *t;
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": Failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto fail;
}
- u = pa_xnew(struct userdata, 1);
- u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
- u->subscription = NULL;
- u->table_file = pa_xstrdup(pa_modargs_get_value(ma, "table", NULL));
- u->modified = 0;
-
- m->userdata = u;
-
- if (load_rules(u) < 0)
+ if (pa_modargs_get_value_boolean(ma, "restore_device", &restore_device) < 0 ||
+ pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0) {
+ pa_log("restore_volume= and restore_device= expect boolean arguments");
goto fail;
+ }
+
+ pa_log_warn("We will now load module-stream-restore. Please make sure to remove module-volume-restore from your configuration.");
- u->subscription = pa_subscription_new(c, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
+ t = pa_sprintf_malloc("restore_volume=%s restore_device=%s", pa_yes_no(restore_volume), pa_yes_no(restore_device));
+ n = pa_module_load(m->core, "module-stream-restore", t);
+ pa_xfree(t);
+
+ if (n)
+ pa_module_unload_request(m, TRUE);
pa_modargs_free(ma);
- return 0;
-fail:
- pa__done(c, m);
+ return n ? 0 : -1;
+fail:
if (ma)
pa_modargs_free(ma);
-
- return -1;
-}
-
-static void free_func(void *p, void *userdata) {
- struct rule *r = p;
- assert(r);
- pa_xfree(r->name);
- pa_xfree(r);
+ return -1;
}
-
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata* u;
-
- assert(c);
- assert(m);
-
- if (!(u = m->userdata))
- return;
-
- if (u->subscription)
- pa_subscription_free(u->subscription);
-
- if (u->hashmap) {
-
- if (u->modified)
- save_rules(u);
-
- pa_hashmap_free(u->hashmap, free_func, NULL);
- }
-
- pa_xfree(u->table_file);
- pa_xfree(u);
-}
-
-
diff --git a/src/modules/module-waveout.c b/src/modules/module-waveout.c
index 8fd60b6a..cb02723c 100644
--- a/src/modules/module-waveout.c
+++ b/src/modules/module-waveout.c
@@ -1,18 +1,19 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+ Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -25,11 +26,9 @@
#include <windows.h>
#include <mmsystem.h>
-#include <assert.h>
-
-#include <pulse/mainloop-api.h>
#include <pulse/xmalloc.h>
+#include <pulse/timeval.h>
#include <pulsecore/sink.h>
#include <pulsecore/source.h>
@@ -38,23 +37,27 @@
#include <pulsecore/sample-util.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
#include "module-waveout-symdef.h"
-PA_MODULE_AUTHOR("Pierre Ossman")
-PA_MODULE_DESCRIPTION("Windows waveOut Sink/Source")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Pierre Ossman");
+PA_MODULE_DESCRIPTION("Windows waveOut Sink/Source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
PA_MODULE_USAGE(
"sink_name=<name for the sink> "
- "source_name=<name for the source>"
+ "source_name=<name for the source> "
+ "device=<device number> "
+ "device_name=<name of the device> "
"record=<enable source?> "
"playback=<enable sink?> "
"format=<sample format> "
- "channels=<number of channels> "
"rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
"fragments=<number of fragments> "
- "fragment_size=<fragment size> "
- "channel_map=<channel map>")
+ "fragment_size=<fragment size>");
#define DEFAULT_SINK_NAME "wave_output"
#define DEFAULT_SOURCE_NAME "wave_input"
@@ -65,20 +68,21 @@ struct userdata {
pa_sink *sink;
pa_source *source;
pa_core *core;
- pa_time_event *event;
- pa_defer_event *defer;
pa_usec_t poll_timeout;
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+
uint32_t fragments, fragment_size;
uint32_t free_ofrags, free_ifrags;
DWORD written_bytes;
+ int sink_underflow;
int cur_ohdr, cur_ihdr;
- unsigned int oremain;
WAVEHDR *ohdrs, *ihdrs;
- pa_memchunk silence;
HWAVEOUT hwo;
HWAVEIN hwi;
@@ -90,6 +94,8 @@ struct userdata {
static const char* const valid_modargs[] = {
"sink_name",
"source_name",
+ "device",
+ "device_name",
"record",
"playback",
"fragments",
@@ -101,116 +107,102 @@ static const char* const valid_modargs[] = {
NULL
};
-static void update_usage(struct userdata *u) {
- pa_module_set_used(u->module,
- (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
- (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
- (u->source ? pa_idxset_size(u->source->outputs) : 0));
-}
-
-static void do_write(struct userdata *u)
-{
- uint32_t free_frags, remain;
- pa_memchunk memchunk, *cur_chunk;
+static void do_write(struct userdata *u) {
+ uint32_t free_frags;
+ pa_memchunk memchunk;
WAVEHDR *hdr;
MMRESULT res;
+ void *p;
if (!u->sink)
return;
- EnterCriticalSection(&u->crit);
+ if (!PA_SINK_IS_LINKED(u->sink->state))
+ return;
+ EnterCriticalSection(&u->crit);
free_frags = u->free_ofrags;
- u->free_ofrags = 0;
-
LeaveCriticalSection(&u->crit);
- if (free_frags == u->fragments)
- pa_log_debug(__FILE__": WaveOut underflow!");
+ if (!u->sink_underflow && (free_frags == u->fragments))
+ pa_log_debug("WaveOut underflow!");
while (free_frags) {
hdr = &u->ohdrs[u->cur_ohdr];
if (hdr->dwFlags & WHDR_PREPARED)
waveOutUnprepareHeader(u->hwo, hdr, sizeof(WAVEHDR));
- remain = u->oremain;
- while (remain) {
- cur_chunk = &memchunk;
+ hdr->dwBufferLength = 0;
+ while (hdr->dwBufferLength < u->fragment_size) {
+ size_t len;
- if (pa_sink_render(u->sink, remain, cur_chunk) < 0) {
- /*
- * Don't fill with silence unless we're getting close to
- * underflowing.
- */
- if (free_frags > u->fragments/2)
- cur_chunk = &u->silence;
- else {
- EnterCriticalSection(&u->crit);
+ len = u->fragment_size - hdr->dwBufferLength;
- u->free_ofrags += free_frags;
+ pa_sink_render(u->sink, len, &memchunk);
- LeaveCriticalSection(&u->crit);
+ pa_assert(memchunk.memblock);
+ pa_assert(memchunk.length);
- u->oremain = remain;
- return;
- }
- }
+ if (memchunk.length < len)
+ len = memchunk.length;
- assert(cur_chunk->memblock);
- assert(cur_chunk->memblock->data);
- assert(cur_chunk->length);
+ p = pa_memblock_acquire(memchunk.memblock);
+ memcpy(hdr->lpData + hdr->dwBufferLength, (char*) p + memchunk.index, len);
+ pa_memblock_release(memchunk.memblock);
- memcpy(hdr->lpData + u->fragment_size - remain,
- (char*)cur_chunk->memblock->data + cur_chunk->index,
- (cur_chunk->length < remain)?cur_chunk->length:remain);
+ hdr->dwBufferLength += len;
- remain -= (cur_chunk->length < remain)?cur_chunk->length:remain;
+ pa_memblock_unref(memchunk.memblock);
+ memchunk.memblock = NULL;
+ }
- if (cur_chunk != &u->silence) {
- pa_memblock_unref(cur_chunk->memblock);
- cur_chunk->memblock = NULL;
- }
+ /* Underflow detection */
+ if (hdr->dwBufferLength == 0) {
+ u->sink_underflow = 1;
+ break;
}
+ u->sink_underflow = 0;
res = waveOutPrepareHeader(u->hwo, hdr, sizeof(WAVEHDR));
- if (res != MMSYSERR_NOERROR) {
- pa_log_error(__FILE__ ": ERROR: Unable to prepare waveOut block: %d",
- res);
- }
+ if (res != MMSYSERR_NOERROR)
+ pa_log_error("Unable to prepare waveOut block: %d", res);
+
res = waveOutWrite(u->hwo, hdr, sizeof(WAVEHDR));
- if (res != MMSYSERR_NOERROR) {
- pa_log_error(__FILE__ ": ERROR: Unable to write waveOut block: %d",
- res);
- }
-
- u->written_bytes += u->fragment_size;
+ if (res != MMSYSERR_NOERROR)
+ pa_log_error("Unable to write waveOut block: %d", res);
+
+ u->written_bytes += hdr->dwBufferLength;
+
+ EnterCriticalSection(&u->crit);
+ u->free_ofrags--;
+ LeaveCriticalSection(&u->crit);
free_frags--;
u->cur_ohdr++;
u->cur_ohdr %= u->fragments;
- u->oremain = u->fragment_size;
}
}
-static void do_read(struct userdata *u)
-{
+static void do_read(struct userdata *u) {
uint32_t free_frags;
pa_memchunk memchunk;
WAVEHDR *hdr;
MMRESULT res;
+ void *p;
if (!u->source)
return;
- EnterCriticalSection(&u->crit);
+ if (!PA_SOURCE_IS_LINKED(u->source->state))
+ return;
+ EnterCriticalSection(&u->crit);
free_frags = u->free_ifrags;
u->free_ifrags = 0;
-
LeaveCriticalSection(&u->crit);
if (free_frags == u->fragments)
- pa_log_debug(__FILE__": WaveIn overflow!");
+ pa_log_debug("WaveIn overflow!");
while (free_frags) {
hdr = &u->ihdrs[u->cur_ihdr];
@@ -218,12 +210,14 @@ static void do_read(struct userdata *u)
waveInUnprepareHeader(u->hwi, hdr, sizeof(WAVEHDR));
if (hdr->dwBytesRecorded) {
- memchunk.memblock = pa_memblock_new(hdr->dwBytesRecorded, u->core->memblock_stat);
- assert(memchunk.memblock);
+ memchunk.memblock = pa_memblock_new(u->core->mempool, hdr->dwBytesRecorded);
+ pa_assert(memchunk.memblock);
- memcpy((char*)memchunk.memblock->data, hdr->lpData, hdr->dwBytesRecorded);
+ p = pa_memblock_acquire(memchunk.memblock);
+ memcpy((char*) p, hdr->lpData, hdr->dwBytesRecorded);
+ pa_memblock_release(memchunk.memblock);
- memchunk.length = memchunk.memblock->length = hdr->dwBytesRecorded;
+ memchunk.length = hdr->dwBytesRecorded;
memchunk.index = 0;
pa_source_post(u->source, &memchunk);
@@ -231,188 +225,221 @@ static void do_read(struct userdata *u)
}
res = waveInPrepareHeader(u->hwi, hdr, sizeof(WAVEHDR));
- if (res != MMSYSERR_NOERROR) {
- pa_log_error(__FILE__ ": ERROR: Unable to prepare waveIn block: %d",
- res);
- }
+ if (res != MMSYSERR_NOERROR)
+ pa_log_error("Unable to prepare waveIn block: %d", res);
+
res = waveInAddBuffer(u->hwi, hdr, sizeof(WAVEHDR));
- if (res != MMSYSERR_NOERROR) {
- pa_log_error(__FILE__ ": ERROR: Unable to add waveIn block: %d",
- res);
- }
-
+ if (res != MMSYSERR_NOERROR)
+ pa_log_error("Unable to add waveIn block: %d", res);
+
free_frags--;
u->cur_ihdr++;
u->cur_ihdr %= u->fragments;
}
}
-static void poll_cb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) {
+static void thread_func(void *userdata) {
struct userdata *u = userdata;
- struct timeval ntv;
- assert(u);
+ pa_assert(u);
+ pa_assert(u->sink || u->source);
- update_usage(u);
+ pa_log_debug("Thread starting up");
- do_write(u);
- do_read(u);
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority);
- pa_gettimeofday(&ntv);
- pa_timeval_add(&ntv, u->poll_timeout);
+ pa_thread_mq_install(&u->thread_mq);
- a->time_restart(e, &ntv);
-}
+ for (;;) {
+ int ret;
+ pa_bool_t need_timer = FALSE;
-static void defer_cb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
- struct userdata *u = userdata;
+ if (u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+ if (u->sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(u->sink, 0);
- assert(u);
+ do_write(u);
+ need_timer = TRUE;
+ }
+ if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
+ do_read(u);
+ need_timer = TRUE;
+ }
- a->defer_enable(e, 0);
+ if (need_timer)
+ pa_rtpoll_set_timer_relative(u->rtpoll, u->poll_timeout);
+ else
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
+
+ /* Hmm, nothing to do. Let's sleep */
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
- do_write(u);
- do_read(u);
+ if (ret == 0)
+ goto finish;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
}
static void CALLBACK chunk_done_cb(HWAVEOUT hwo, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) {
- struct userdata *u = (struct userdata *)inst;
+ struct userdata *u = (struct userdata*) inst;
+ if (msg == WOM_OPEN)
+ pa_log_debug("WaveOut subsystem opened.");
+ if (msg == WOM_CLOSE)
+ pa_log_debug("WaveOut subsystem closed.");
if (msg != WOM_DONE)
return;
EnterCriticalSection(&u->crit);
-
u->free_ofrags++;
- assert(u->free_ofrags <= u->fragments);
-
+ pa_assert(u->free_ofrags <= u->fragments);
LeaveCriticalSection(&u->crit);
}
static void CALLBACK chunk_ready_cb(HWAVEIN hwi, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) {
- struct userdata *u = (struct userdata *)inst;
+ struct userdata *u = (struct userdata*) inst;
+ if (msg == WIM_OPEN)
+ pa_log_debug("WaveIn subsystem opened.");
+ if (msg == WIM_CLOSE)
+ pa_log_debug("WaveIn subsystem closed.");
if (msg != WIM_DATA)
return;
EnterCriticalSection(&u->crit);
-
u->free_ifrags++;
- assert(u->free_ifrags <= u->fragments);
-
+ pa_assert(u->free_ifrags <= u->fragments);
LeaveCriticalSection(&u->crit);
}
-static pa_usec_t sink_get_latency_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
+static pa_usec_t sink_get_latency(struct userdata *u) {
uint32_t free_frags;
MMTIME mmt;
- assert(s && u && u->sink);
+ pa_assert(u);
+ pa_assert(u->sink);
memset(&mmt, 0, sizeof(mmt));
mmt.wType = TIME_BYTES;
if (waveOutGetPosition(u->hwo, &mmt, sizeof(mmt)) == MMSYSERR_NOERROR)
- return pa_bytes_to_usec(u->written_bytes - mmt.u.cb, &s->sample_spec);
+ return pa_bytes_to_usec(u->written_bytes - mmt.u.cb, &u->sink->sample_spec);
else {
EnterCriticalSection(&u->crit);
-
free_frags = u->free_ofrags;
-
LeaveCriticalSection(&u->crit);
- return pa_bytes_to_usec((u->fragments - free_frags) * u->fragment_size,
- &s->sample_spec);
+ return pa_bytes_to_usec((u->fragments - free_frags) * u->fragment_size, &u->sink->sample_spec);
}
}
-static pa_usec_t source_get_latency_cb(pa_source *s) {
+static pa_usec_t source_get_latency(struct userdata *u) {
pa_usec_t r = 0;
- struct userdata *u = s->userdata;
uint32_t free_frags;
- assert(s && u && u->sink);
+ pa_assert(u);
+ pa_assert(u->source);
EnterCriticalSection(&u->crit);
-
free_frags = u->free_ifrags;
-
LeaveCriticalSection(&u->crit);
- r += pa_bytes_to_usec((free_frags + 1) * u->fragment_size, &s->sample_spec);
+ r += pa_bytes_to_usec((free_frags + 1) * u->fragment_size, &u->source->sample_spec);
return r;
}
-static void notify_sink_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- assert(u);
+static int process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u;
- u->core->mainloop->defer_enable(u->defer, 1);
-}
+ if (pa_sink_isinstance(o)) {
+ u = PA_SINK(o)->userdata;
-static void notify_source_cb(pa_source *s) {
- struct userdata *u = s->userdata;
- assert(u);
+ switch (code) {
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ pa_usec_t r = 0;
+ if (u->hwo)
+ r = sink_get_latency(u);
+ *((pa_usec_t*) data) = r;
+ return 0;
+ }
+
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+ }
- u->core->mainloop->defer_enable(u->defer, 1);
+ if (pa_source_isinstance(o)) {
+ u = PA_SOURCE(o)->userdata;
+
+ switch (code) {
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ pa_usec_t r = 0;
+ if (u->hwi)
+ r = source_get_latency(u);
+ *((pa_usec_t*) data) = r;
+ return 0;
+ }
+
+ }
+
+ return pa_source_process_msg(o, code, data, offset, chunk);
+ }
+
+ return -1;
}
-static int sink_get_hw_volume_cb(pa_sink *s) {
+static void sink_get_volume_cb(pa_sink *s) {
struct userdata *u = s->userdata;
DWORD vol;
pa_volume_t left, right;
if (waveOutGetVolume(u->hwo, &vol) != MMSYSERR_NOERROR)
- return -1;
+ return;
- left = (vol & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME;
- right = ((vol >> 16) & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME;
+ left = PA_CLAMP_VOLUME((vol & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME);
+ right = PA_CLAMP_VOLUME(((vol >> 16) & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME);
/* Windows supports > 2 channels, except for volume control */
- if (s->hw_volume.channels > 2)
- pa_cvolume_set(&s->hw_volume, s->hw_volume.channels, (left + right)/2);
+ if (s->real_volume.channels > 2)
+ pa_cvolume_set(&s->real_volume, s->real_volume.channels, (left + right)/2);
- s->hw_volume.values[0] = left;
- if (s->hw_volume.channels > 1)
- s->hw_volume.values[1] = right;
-
- return 0;
+ s->real_volume.values[0] = left;
+ if (s->real_volume.channels > 1)
+ s->real_volume.values[1] = right;
}
-static int sink_set_hw_volume_cb(pa_sink *s) {
+static void sink_set_volume_cb(pa_sink *s) {
struct userdata *u = s->userdata;
DWORD vol;
- vol = s->hw_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM;
- if (s->hw_volume.channels > 1)
- vol |= (s->hw_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM) << 16;
+ vol = s->real_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM;
+ if (s->real_volume.channels > 1)
+ vol |= (s->real_volume.values[1] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM) << 16;
if (waveOutSetVolume(u->hwo, vol) != MMSYSERR_NOERROR)
- return -1;
-
- return 0;
+ return;
}
static int ss_to_waveformat(pa_sample_spec *ss, LPWAVEFORMATEX wf) {
wf->wFormatTag = WAVE_FORMAT_PCM;
if (ss->channels > 2) {
- pa_log_error(__FILE__": ERROR: More than two channels not supported.");
+ pa_log_error("More than two channels not supported.");
return -1;
}
wf->nChannels = ss->channels;
- switch (ss->rate) {
- case 8000:
- case 11025:
- case 22005:
- case 44100:
- break;
- default:
- pa_log_error(__FILE__": ERROR: Unsupported sample rate.");
- return -1;
- }
-
wf->nSamplesPerSec = ss->rate;
if (ss->format == PA_SAMPLE_U8)
@@ -420,7 +447,7 @@ static int ss_to_waveformat(pa_sample_spec *ss, LPWAVEFORMATEX wf) {
else if (ss->format == PA_SAMPLE_S16NE)
wf->wBitsPerSample = 16;
else {
- pa_log_error(__FILE__": ERROR: Unsupported sample format.");
+ pa_log_error("Unsupported sample format, only u8 and s16 are supported.");
return -1;
}
@@ -432,46 +459,86 @@ static int ss_to_waveformat(pa_sample_spec *ss, LPWAVEFORMATEX wf) {
return 0;
}
-int pa__init(pa_core *c, pa_module*m) {
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+ pa_assert(m);
+ pa_assert(m->userdata);
+ u = (struct userdata*) m->userdata;
+
+ return (u->sink ? pa_sink_used_by(u->sink) : 0) +
+ (u->source ? pa_source_used_by(u->source) : 0);
+}
+
+int pa__init(pa_module *m) {
struct userdata *u = NULL;
HWAVEOUT hwo = INVALID_HANDLE_VALUE;
HWAVEIN hwi = INVALID_HANDLE_VALUE;
WAVEFORMATEX wf;
+ WAVEOUTCAPS pwoc;
+ MMRESULT result;
int nfrags, frag_size;
- int record = 1, playback = 1;
+ pa_bool_t record = TRUE, playback = TRUE;
+ unsigned int device;
pa_sample_spec ss;
pa_channel_map map;
pa_modargs *ma = NULL;
+ const char *device_name = NULL;
unsigned int i;
- struct timeval tv;
- assert(c && m);
+ pa_assert(m);
+ pa_assert(m->core);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
+ pa_log("failed to parse module arguments.");
goto fail;
}
if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
- pa_log(__FILE__": record= and playback= expect boolean argument.");
+ pa_log("record= and playback= expect boolean argument.");
goto fail;
}
if (!playback && !record) {
- pa_log(__FILE__": neither playback nor record enabled for device.");
+ pa_log("neither playback nor record enabled for device.");
goto fail;
}
+ /* Set the device to be opened. If set device_name is used,
+ * else device if set and lastly WAVE_MAPPER is the default */
+ device = WAVE_MAPPER;
+ if (pa_modargs_get_value_u32(ma, "device", &device) < 0) {
+ pa_log("failed to parse device argument");
+ goto fail;
+ }
+ if ((device_name = pa_modargs_get_value(ma, "device_name", NULL)) != NULL) {
+ unsigned int num_devices = waveOutGetNumDevs();
+ for (i = 0; i < num_devices; i++) {
+ if (waveOutGetDevCaps(i, &pwoc, sizeof(pwoc)) == MMSYSERR_NOERROR)
+ if (_stricmp(device_name, pwoc.szPname) == 0)
+ break;
+ }
+ if (i < num_devices)
+ device = i;
+ else {
+ pa_log("device not found: %s", device_name);
+ goto fail;
+ }
+ }
+ if (waveOutGetDevCaps(device, &pwoc, sizeof(pwoc)) == MMSYSERR_NOERROR)
+ device_name = pwoc.szPname;
+ else
+ device_name = "unknown";
+
nfrags = 5;
frag_size = 8192;
if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
- pa_log(__FILE__": failed to parse fragments arguments");
+ pa_log("failed to parse fragments arguments");
goto fail;
}
- ss = c->default_sample_spec;
+ ss = m->core->default_sample_spec;
if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_WAVEEX) < 0) {
- pa_log(__FILE__": failed to parse sample specification");
+ pa_log("failed to parse sample specification");
goto fail;
}
@@ -481,50 +548,89 @@ int pa__init(pa_core *c, pa_module*m) {
u = pa_xmalloc(sizeof(struct userdata));
if (record) {
- if (waveInOpen(&hwi, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_ready_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
+ result = waveInOpen(&hwi, device, &wf, 0, 0, WAVE_FORMAT_DIRECT | WAVE_FORMAT_QUERY);
+ if (result != MMSYSERR_NOERROR) {
+ pa_log_warn("Sample spec not supported by WaveIn, falling back to default sample rate.");
+ ss.rate = wf.nSamplesPerSec = m->core->default_sample_spec.rate;
+ }
+ result = waveInOpen(&hwi, device, &wf, (DWORD_PTR) chunk_ready_cb, (DWORD_PTR) u, CALLBACK_FUNCTION);
+ if (result != MMSYSERR_NOERROR) {
+ char errortext[MAXERRORLENGTH];
+ pa_log("Failed to open WaveIn.");
+ if (waveInGetErrorText(result, errortext, sizeof(errortext)) == MMSYSERR_NOERROR)
+ pa_log("Error: %s", errortext);
goto fail;
- if (waveInStart(hwi) != MMSYSERR_NOERROR)
+ }
+ if (waveInStart(hwi) != MMSYSERR_NOERROR) {
+ pa_log("failed to start waveIn");
goto fail;
- pa_log_debug(__FILE__": Opened waveIn subsystem.");
+ }
}
if (playback) {
- if (waveOutOpen(&hwo, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_done_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
+ result = waveOutOpen(&hwo, device, &wf, 0, 0, WAVE_FORMAT_DIRECT | WAVE_FORMAT_QUERY);
+ if (result != MMSYSERR_NOERROR) {
+ pa_log_warn("Sample spec not supported by WaveOut, falling back to default sample rate.");
+ ss.rate = wf.nSamplesPerSec = m->core->default_sample_spec.rate;
+ }
+ result = waveOutOpen(&hwo, device, &wf, (DWORD_PTR) chunk_done_cb, (DWORD_PTR) u, CALLBACK_FUNCTION);
+ if (result != MMSYSERR_NOERROR) {
+ char errortext[MAXERRORLENGTH];
+ pa_log("Failed to open WaveOut.");
+ if (waveOutGetErrorText(result, errortext, sizeof(errortext)) == MMSYSERR_NOERROR)
+ pa_log("Error: %s", errortext);
goto fail;
- pa_log_debug(__FILE__": Opened waveOut subsystem.");
+ }
}
InitializeCriticalSection(&u->crit);
if (hwi != INVALID_HANDLE_VALUE) {
- u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map);
- assert(u->source);
+ char *description = pa_sprintf_malloc("WaveIn on %s", device_name);
+ pa_source_new_data data;
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+ pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
+
+ pa_assert(u->source);
u->source->userdata = u;
- u->source->notify = notify_source_cb;
- u->source->get_latency = source_get_latency_cb;
- pa_source_set_owner(u->source, m);
- u->source->description = pa_sprintf_malloc("Windows waveIn PCM");
- u->source->is_hardware = 1;
+ pa_source_set_description(u->source, description);
+ u->source->parent.process_msg = process_msg;
+ pa_xfree(description);
} else
u->source = NULL;
if (hwo != INVALID_HANDLE_VALUE) {
- u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map);
- assert(u->sink);
- u->sink->notify = notify_sink_cb;
- u->sink->get_latency = sink_get_latency_cb;
- u->sink->get_hw_volume = sink_get_hw_volume_cb;
- u->sink->set_hw_volume = sink_set_hw_volume_cb;
+ char *description = pa_sprintf_malloc("WaveOut on %s", device_name);
+ pa_sink_new_data data;
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
+
+ pa_assert(u->sink);
+ u->sink->get_volume = sink_get_volume_cb;
+ u->sink->set_volume = sink_set_volume_cb;
u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Windows waveOut PCM");
- u->sink->is_hardware = 1;
+ pa_sink_set_description(u->sink, description);
+ u->sink->parent.process_msg = process_msg;
+ pa_xfree(description);
} else
u->sink = NULL;
- assert(u->source || u->sink);
+ pa_assert(u->source || u->sink);
+ pa_modargs_free(ma);
- u->core = c;
+ u->core = m->core;
u->hwi = hwi;
u->hwo = hwo;
@@ -534,94 +640,94 @@ int pa__init(pa_core *c, pa_module*m) {
u->fragment_size = frag_size - (frag_size % pa_frame_size(&ss));
u->written_bytes = 0;
-
- u->oremain = u->fragment_size;
+ u->sink_underflow = 1;
u->poll_timeout = pa_bytes_to_usec(u->fragments * u->fragment_size / 10, &ss);
-
- pa_gettimeofday(&tv);
- pa_timeval_add(&tv, u->poll_timeout);
-
- u->event = c->mainloop->time_new(c->mainloop, &tv, poll_cb, u);
- assert(u->event);
-
- u->defer = c->mainloop->defer_new(c->mainloop, defer_cb, u);
- assert(u->defer);
- c->mainloop->defer_enable(u->defer, 0);
+ pa_log_debug("Poll timeout = %.1f ms", (double) u->poll_timeout / PA_USEC_PER_MSEC);
u->cur_ihdr = 0;
u->cur_ohdr = 0;
u->ihdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments);
- assert(u->ihdrs);
+ pa_assert(u->ihdrs);
u->ohdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments);
- assert(u->ohdrs);
- for (i = 0;i < u->fragments;i++) {
+ pa_assert(u->ohdrs);
+ for (i = 0; i < u->fragments; i++) {
u->ihdrs[i].dwBufferLength = u->fragment_size;
u->ohdrs[i].dwBufferLength = u->fragment_size;
u->ihdrs[i].lpData = pa_xmalloc(u->fragment_size);
- assert(u->ihdrs);
+ pa_assert(u->ihdrs);
u->ohdrs[i].lpData = pa_xmalloc(u->fragment_size);
- assert(u->ohdrs);
+ pa_assert(u->ohdrs);
}
-
- u->silence.length = u->fragment_size;
- u->silence.memblock = pa_memblock_new(u->silence.length, u->core->memblock_stat);
- assert(u->silence.memblock);
- pa_silence_memblock(u->silence.memblock, &ss);
- u->silence.index = 0;
u->module = m;
m->userdata = u;
- pa_modargs_free(ma);
-
/* Read mixer settings */
if (u->sink)
- sink_get_hw_volume_cb(u->sink);
+ sink_get_volume_cb(u->sink);
- return 0;
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
-fail:
- if (hwi != INVALID_HANDLE_VALUE)
- waveInClose(hwi);
+ if (u->sink) {
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+ }
+ if (u->source) {
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+ }
- if (hwo != INVALID_HANDLE_VALUE)
- waveOutClose(hwo);
+ if (!(u->thread = pa_thread_new("waveout", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
- if (u)
- pa_xfree(u);
+ if (u->sink)
+ pa_sink_put(u->sink);
+ if (u->source)
+ pa_source_put(u->source);
+
+ return 0;
+fail:
if (ma)
pa_modargs_free(ma);
-
+
+ pa__done(m);
+
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module *m) {
struct userdata *u;
unsigned int i;
- assert(c && m);
+ pa_assert(m);
+ pa_assert(m->core);
if (!(u = m->userdata))
return;
-
- if (u->event)
- c->mainloop->time_free(u->event);
- if (u->defer)
- c->mainloop->defer_free(u->defer);
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+ if (u->source)
+ pa_source_unlink(u->source);
- if (u->sink) {
- pa_sink_disconnect(u->sink);
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ if (u->thread)
+ pa_thread_free(u->thread);
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
pa_sink_unref(u->sink);
- }
-
- if (u->source) {
- pa_source_disconnect(u->source);
+ if (u->source)
pa_source_unref(u->source);
- }
-
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
if (u->hwi != INVALID_HANDLE_VALUE) {
waveInReset(u->hwi);
waveInClose(u->hwi);
@@ -632,7 +738,7 @@ void pa__done(pa_core *c, pa_module*m) {
waveOutClose(u->hwo);
}
- for (i = 0;i < u->fragments;i++) {
+ for (i = 0; i < u->fragments; i++) {
pa_xfree(u->ihdrs[i].lpData);
pa_xfree(u->ohdrs[i].lpData);
}
@@ -641,6 +747,6 @@ void pa__done(pa_core *c, pa_module*m) {
pa_xfree(u->ohdrs);
DeleteCriticalSection(&u->crit);
-
+
pa_xfree(u);
}
diff --git a/src/modules/module-x11-publish.c b/src/modules/module-x11-publish.c
deleted file mode 100644
index 0ee453cc..00000000
--- a/src/modules/module-x11-publish.c
+++ /dev/null
@@ -1,191 +0,0 @@
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdio.h>
-#include <assert.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <X11/Xlib.h>
-#include <X11/Xatom.h>
-
-#include <pulse/util.h>
-#include <pulse/xmalloc.h>
-
-#include <pulsecore/module.h>
-#include <pulsecore/sink.h>
-#include <pulsecore/core-scache.h>
-#include <pulsecore/modargs.h>
-#include <pulsecore/namereg.h>
-#include <pulsecore/log.h>
-#include <pulsecore/x11wrap.h>
-#include <pulsecore/core-util.h>
-#include <pulsecore/native-common.h>
-#include <pulsecore/authkey-prop.h>
-#include <pulsecore/authkey.h>
-#include <pulsecore/x11prop.h>
-#include <pulsecore/strlist.h>
-#include <pulsecore/props.h>
-
-#include "module-x11-publish-symdef.h"
-
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("X11 Credential Publisher")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("display=<X11 display>")
-
-static const char* const valid_modargs[] = {
- "display",
- "sink",
- "source",
- "cookie",
- NULL
-};
-
-struct userdata {
- pa_core *core;
- pa_x11_wrapper *x11_wrapper;
- char *id;
- uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
- int auth_cookie_in_property;
-};
-
-static int load_key(struct userdata *u, const char*fn) {
- assert(u);
-
- u->auth_cookie_in_property = 0;
-
- if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
- pa_log_debug(__FILE__": using already loaded auth cookie.");
- pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
- u->auth_cookie_in_property = 1;
- return 0;
- }
-
- if (!fn)
- fn = PA_NATIVE_COOKIE_FILE;
-
- if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
- return -1;
-
- pa_log_debug(__FILE__": loading cookie from disk.");
-
- if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
- u->auth_cookie_in_property = 1;
-
- return 0;
-}
-
-int pa__init(pa_core *c, pa_module*m) {
- struct userdata *u;
- pa_modargs *ma = NULL;
- char hn[256], un[128];
- char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
- const char *t;
- char *s;
- pa_strlist *l;
-
- if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
- goto fail;
- }
-
- m->userdata = u = pa_xmalloc(sizeof(struct userdata));
- u->core = c;
- u->id = NULL;
- u->auth_cookie_in_property = 0;
-
- if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
- goto fail;
-
- if (!(u->x11_wrapper = pa_x11_wrapper_get(c, pa_modargs_get_value(ma, "display", NULL))))
- goto fail;
-
- if (!(l = pa_property_get(c, PA_NATIVE_SERVER_PROPERTY_NAME)))
- goto fail;
-
- s = pa_strlist_tostring(l);
- pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER", s);
- pa_xfree(s);
-
- if (!pa_get_fqdn(hn, sizeof(hn)) || !pa_get_user_name(un, sizeof(un)))
- goto fail;
-
- u->id = pa_sprintf_malloc("%s@%s/%u", un, hn, (unsigned) getpid());
- pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", u->id);
-
- if ((t = pa_modargs_get_value(ma, "source", NULL)))
- pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE", t);
-
- if ((t = pa_modargs_get_value(ma, "sink", NULL)))
- pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK", t);
-
- pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_COOKIE", pa_hexstr(u->auth_cookie, sizeof(u->auth_cookie), hx, sizeof(hx)));
-
- pa_modargs_free(ma);
- return 0;
-
-fail:
- if (ma)
- pa_modargs_free(ma);
-
- pa__done(c, m);
- return -1;
-}
-
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata*u;
- assert(c && m);
-
- if (!(u = m->userdata))
- return;
-
- if (u->x11_wrapper) {
- char t[256];
-
- /* Yes, here is a race condition */
- if (!pa_x11_get_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", t, sizeof(t)) || strcmp(t, u->id))
- pa_log_warn(__FILE__": PulseAudio information vanished from X11!");
- else {
- pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID");
- pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER");
- pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK");
- pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE");
- pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_COOKIE");
- XSync(pa_x11_wrapper_get_display(u->x11_wrapper), False);
- }
- }
-
- if (u->x11_wrapper)
- pa_x11_wrapper_unref(u->x11_wrapper);
-
- if (u->auth_cookie_in_property)
- pa_authkey_prop_unref(c, PA_NATIVE_COOKIE_PROPERTY_NAME);
-
- pa_xfree(u->id);
- pa_xfree(u);
-}
-
diff --git a/src/modules/module-zeroconf-discover.c b/src/modules/module-zeroconf-discover.c
new file mode 100644
index 00000000..cd076aab
--- /dev/null
+++ b/src/modules/module-zeroconf-discover.c
@@ -0,0 +1,433 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <avahi-client/client.h>
+#include <avahi-client/lookup.h>
+#include <avahi-common/alternative.h>
+#include <avahi-common/error.h>
+#include <avahi-common/domain.h>
+#include <avahi-common/malloc.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/hashmap.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/avahi-wrap.h>
+
+#include "module-zeroconf-discover-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Discovery");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+#define SERVICE_TYPE_SINK "_pulse-sink._tcp"
+#define SERVICE_TYPE_SOURCE "_non-monitor._sub._pulse-source._tcp"
+
+static const char* const valid_modargs[] = {
+ NULL
+};
+
+struct tunnel {
+ AvahiIfIndex interface;
+ AvahiProtocol protocol;
+ char *name, *type, *domain;
+ uint32_t module_index;
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ AvahiPoll *avahi_poll;
+ AvahiClient *client;
+ AvahiServiceBrowser *source_browser, *sink_browser;
+
+ pa_hashmap *tunnels;
+};
+
+static unsigned tunnel_hash(const void *p) {
+ const struct tunnel *t = p;
+
+ return
+ (unsigned) t->interface +
+ (unsigned) t->protocol +
+ pa_idxset_string_hash_func(t->name) +
+ pa_idxset_string_hash_func(t->type) +
+ pa_idxset_string_hash_func(t->domain);
+}
+
+static int tunnel_compare(const void *a, const void *b) {
+ const struct tunnel *ta = a, *tb = b;
+ int r;
+
+ if (ta->interface != tb->interface)
+ return 1;
+ if (ta->protocol != tb->protocol)
+ return 1;
+ if ((r = strcmp(ta->name, tb->name)))
+ return r;
+ if ((r = strcmp(ta->type, tb->type)))
+ return r;
+ if ((r = strcmp(ta->domain, tb->domain)))
+ return r;
+
+ return 0;
+}
+
+static struct tunnel *tunnel_new(
+ AvahiIfIndex interface, AvahiProtocol protocol,
+ const char *name, const char *type, const char *domain) {
+
+ struct tunnel *t;
+ t = pa_xnew(struct tunnel, 1);
+ t->interface = interface;
+ t->protocol = protocol;
+ t->name = pa_xstrdup(name);
+ t->type = pa_xstrdup(type);
+ t->domain = pa_xstrdup(domain);
+ t->module_index = PA_IDXSET_INVALID;
+ return t;
+}
+
+static void tunnel_free(struct tunnel *t) {
+ pa_assert(t);
+ pa_xfree(t->name);
+ pa_xfree(t->type);
+ pa_xfree(t->domain);
+ pa_xfree(t);
+}
+
+static void resolver_cb(
+ AvahiServiceResolver *r,
+ AvahiIfIndex interface, AvahiProtocol protocol,
+ AvahiResolverEvent event,
+ const char *name, const char *type, const char *domain,
+ const char *host_name, const AvahiAddress *a, uint16_t port,
+ AvahiStringList *txt,
+ AvahiLookupResultFlags flags,
+ void *userdata) {
+
+ struct userdata *u = userdata;
+ struct tunnel *tnl;
+
+ pa_assert(u);
+
+ tnl = tunnel_new(interface, protocol, name, type, domain);
+
+ if (event != AVAHI_RESOLVER_FOUND)
+ pa_log("Resolving of '%s' failed: %s", name, avahi_strerror(avahi_client_errno(u->client)));
+ else {
+ char *device = NULL, *dname, *module_name, *args;
+ const char *t;
+ char at[AVAHI_ADDRESS_STR_MAX], cmt[PA_CHANNEL_MAP_SNPRINT_MAX];
+ pa_sample_spec ss;
+ pa_channel_map cm;
+ AvahiStringList *l;
+ pa_bool_t channel_map_set = FALSE;
+ pa_module *m;
+
+ ss = u->core->default_sample_spec;
+ cm = u->core->default_channel_map;
+
+ for (l = txt; l; l = l->next) {
+ char *key, *value;
+ pa_assert_se(avahi_string_list_get_pair(l, &key, &value, NULL) == 0);
+
+ if (strcmp(key, "device") == 0) {
+ pa_xfree(device);
+ device = value;
+ value = NULL;
+ } else if (strcmp(key, "rate") == 0)
+ ss.rate = (uint32_t) atoi(value);
+ else if (strcmp(key, "channels") == 0)
+ ss.channels = (uint8_t) atoi(value);
+ else if (strcmp(key, "format") == 0)
+ ss.format = pa_parse_sample_format(value);
+ else if (strcmp(key, "channel_map") == 0) {
+ pa_channel_map_parse(&cm, value);
+ channel_map_set = TRUE;
+ }
+
+ avahi_free(key);
+ avahi_free(value);
+ }
+
+ if (!channel_map_set && cm.channels != ss.channels)
+ pa_channel_map_init_extend(&cm, ss.channels, PA_CHANNEL_MAP_DEFAULT);
+
+ if (!pa_sample_spec_valid(&ss)) {
+ pa_log("Service '%s' contains an invalid sample specification.", name);
+ avahi_free(device);
+ goto finish;
+ }
+
+ if (!pa_channel_map_valid(&cm) || cm.channels != ss.channels) {
+ pa_log("Service '%s' contains an invalid channel map.", name);
+ avahi_free(device);
+ goto finish;
+ }
+
+ if (device)
+ dname = pa_sprintf_malloc("tunnel.%s.%s", host_name, device);
+ else
+ dname = pa_sprintf_malloc("tunnel.%s", host_name);
+
+ if (!pa_namereg_is_valid_name(dname)) {
+ pa_log("Cannot construct valid device name from credentials of service '%s'.", dname);
+ avahi_free(device);
+ pa_xfree(dname);
+ goto finish;
+ }
+
+ t = strstr(type, "sink") ? "sink" : "source";
+
+ module_name = pa_sprintf_malloc("module-tunnel-%s", t);
+ args = pa_sprintf_malloc("server=[%s]:%u "
+ "%s=%s "
+ "format=%s "
+ "channels=%u "
+ "rate=%u "
+ "%s_name=%s "
+ "channel_map=%s",
+ avahi_address_snprint(at, sizeof(at), a), port,
+ t, device,
+ pa_sample_format_to_string(ss.format),
+ ss.channels,
+ ss.rate,
+ t, dname,
+ pa_channel_map_snprint(cmt, sizeof(cmt), &cm));
+
+ pa_log_debug("Loading %s with arguments '%s'", module_name, args);
+
+ if ((m = pa_module_load(u->core, module_name, args))) {
+ tnl->module_index = m->index;
+ pa_hashmap_put(u->tunnels, tnl, tnl);
+ tnl = NULL;
+ }
+
+ pa_xfree(module_name);
+ pa_xfree(dname);
+ pa_xfree(args);
+ avahi_free(device);
+ }
+
+finish:
+
+ avahi_service_resolver_free(r);
+
+ if (tnl)
+ tunnel_free(tnl);
+}
+
+static void browser_cb(
+ AvahiServiceBrowser *b,
+ AvahiIfIndex interface, AvahiProtocol protocol,
+ AvahiBrowserEvent event,
+ const char *name, const char *type, const char *domain,
+ AvahiLookupResultFlags flags,
+ void *userdata) {
+
+ struct userdata *u = userdata;
+ struct tunnel *t;
+
+ pa_assert(u);
+
+ if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
+ return;
+
+ t = tunnel_new(interface, protocol, name, type, domain);
+
+ if (event == AVAHI_BROWSER_NEW) {
+
+ if (!pa_hashmap_get(u->tunnels, t))
+ if (!(avahi_service_resolver_new(u->client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolver_cb, u)))
+ pa_log("avahi_service_resolver_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+
+ /* We ignore the returned resolver object here, since the we don't
+ * need to attach any special data to it, and we can still destroy
+ * it from the callback */
+
+ } else if (event == AVAHI_BROWSER_REMOVE) {
+ struct tunnel *t2;
+
+ if ((t2 = pa_hashmap_get(u->tunnels, t))) {
+ pa_module_unload_request_by_index(u->core, t2->module_index, TRUE);
+ pa_hashmap_remove(u->tunnels, t2);
+ tunnel_free(t2);
+ }
+ }
+
+ tunnel_free(t);
+}
+
+static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(c);
+ pa_assert(u);
+
+ u->client = c;
+
+ switch (state) {
+ case AVAHI_CLIENT_S_REGISTERING:
+ case AVAHI_CLIENT_S_RUNNING:
+ case AVAHI_CLIENT_S_COLLISION:
+
+ if (!u->sink_browser) {
+
+ if (!(u->sink_browser = avahi_service_browser_new(
+ c,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ SERVICE_TYPE_SINK,
+ NULL,
+ 0,
+ browser_cb, u))) {
+
+ pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
+ pa_module_unload_request(u->module, TRUE);
+ }
+ }
+
+ if (!u->source_browser) {
+
+ if (!(u->source_browser = avahi_service_browser_new(
+ c,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ SERVICE_TYPE_SOURCE,
+ NULL,
+ 0,
+ browser_cb, u))) {
+
+ pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
+ pa_module_unload_request(u->module, TRUE);
+ }
+ }
+
+ break;
+
+ case AVAHI_CLIENT_FAILURE:
+ if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
+ int error;
+
+ pa_log_debug("Avahi daemon disconnected.");
+
+ if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
+ pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
+ pa_module_unload_request(u->module, TRUE);
+ }
+ }
+
+ /* Fall through */
+
+ case AVAHI_CLIENT_CONNECTING:
+
+ if (u->sink_browser) {
+ avahi_service_browser_free(u->sink_browser);
+ u->sink_browser = NULL;
+ }
+
+ if (u->source_browser) {
+ avahi_service_browser_free(u->source_browser);
+ u->source_browser = NULL;
+ }
+
+ break;
+
+ default: ;
+ }
+}
+
+int pa__init(pa_module*m) {
+
+ struct userdata *u;
+ pa_modargs *ma = NULL;
+ int error;
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->sink_browser = u->source_browser = NULL;
+
+ u->tunnels = pa_hashmap_new(tunnel_hash, tunnel_compare);
+
+ u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
+
+ if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
+ pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
+ goto fail;
+ }
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata*u;
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->client)
+ avahi_client_free(u->client);
+
+ if (u->avahi_poll)
+ pa_avahi_poll_free(u->avahi_poll);
+
+ if (u->tunnels) {
+ struct tunnel *t;
+
+ while ((t = pa_hashmap_steal_first(u->tunnels))) {
+ pa_module_unload_request_by_index(u->core, t->module_index, TRUE);
+ tunnel_free(t);
+ }
+
+ pa_hashmap_free(u->tunnels, NULL, NULL);
+ }
+
+ pa_xfree(u);
+}
diff --git a/src/modules/module-zeroconf-publish.c b/src/modules/module-zeroconf-publish.c
index 23a188b3..0c20cf6c 100644
--- a/src/modules/module-zeroconf-publish.c
+++ b/src/modules/module-zeroconf-publish.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
- published by the Free Software Foundation; either version 2 of the
+ published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public
License along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,432 +24,454 @@
#endif
#include <stdio.h>
-#include <assert.h>
#include <stdlib.h>
-#include <string.h>
#include <unistd.h>
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/alternative.h>
#include <avahi-common/error.h>
+#include <avahi-common/domain.h>
#include <pulse/xmalloc.h>
#include <pulse/util.h>
-#include <pulsecore/autoload.h>
+#include <pulsecore/parseaddr.h>
#include <pulsecore/sink.h>
#include <pulsecore/source.h>
#include <pulsecore/native-common.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
-#include <pulsecore/core-subscribe.h>
#include <pulsecore/dynarray.h>
#include <pulsecore/modargs.h>
#include <pulsecore/avahi-wrap.h>
-#include <pulsecore/endianmacros.h>
+#include <pulsecore/protocol-native.h>
#include "module-zeroconf-publish-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("port=<IP port number>")
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
#define SERVICE_TYPE_SINK "_pulse-sink._tcp"
#define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
#define SERVICE_TYPE_SERVER "_pulse-server._tcp"
+#define SERVICE_SUBTYPE_SINK_HARDWARE "_hardware._sub."SERVICE_TYPE_SINK
+#define SERVICE_SUBTYPE_SINK_VIRTUAL "_virtual._sub."SERVICE_TYPE_SINK
+#define SERVICE_SUBTYPE_SOURCE_HARDWARE "_hardware._sub."SERVICE_TYPE_SOURCE
+#define SERVICE_SUBTYPE_SOURCE_VIRTUAL "_virtual._sub."SERVICE_TYPE_SOURCE
+#define SERVICE_SUBTYPE_SOURCE_MONITOR "_monitor._sub."SERVICE_TYPE_SOURCE
+#define SERVICE_SUBTYPE_SOURCE_NON_MONITOR "_non-monitor._sub."SERVICE_TYPE_SOURCE
static const char* const valid_modargs[] = {
- "port",
NULL
};
+enum service_subtype {
+ SUBTYPE_HARDWARE,
+ SUBTYPE_VIRTUAL,
+ SUBTYPE_MONITOR
+};
+
struct service {
struct userdata *userdata;
AvahiEntryGroup *entry_group;
char *service_name;
- char *name;
- enum { UNPUBLISHED, PUBLISHED_REAL, PUBLISHED_AUTOLOAD } published ;
-
- struct {
- int valid;
- pa_namereg_type_t type;
- uint32_t index;
- } loaded;
-
- struct {
- int valid;
- pa_namereg_type_t type;
- uint32_t index;
- } autoload;
+ pa_object *device;
+ enum service_subtype subtype;
};
struct userdata {
pa_core *core;
+ pa_module *module;
+
AvahiPoll *avahi_poll;
AvahiClient *client;
+
pa_hashmap *services;
- pa_dynarray *sink_dynarray, *source_dynarray, *autoload_dynarray;
- pa_subscription *subscription;
char *service_name;
AvahiEntryGroup *main_entry_group;
- uint16_t port;
+ pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot;
+
+ pa_native_protocol *native;
};
-static void get_service_data(struct userdata *u, struct service *s, pa_sample_spec *ret_ss, char **ret_description) {
- assert(u && s && s->loaded.valid && ret_ss && ret_description);
+static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_channel_map *ret_map, const char **ret_name, pa_proplist **ret_proplist, enum service_subtype *ret_subtype) {
+ pa_assert(s);
+ pa_assert(ret_ss);
+ pa_assert(ret_proplist);
+ pa_assert(ret_subtype);
+
+ if (pa_sink_isinstance(s->device)) {
+ pa_sink *sink = PA_SINK(s->device);
- if (s->loaded.type == PA_NAMEREG_SINK) {
- pa_sink *sink = pa_idxset_get_by_index(u->core->sinks, s->loaded.index);
- assert(sink);
*ret_ss = sink->sample_spec;
- *ret_description = sink->description;
- } else if (s->loaded.type == PA_NAMEREG_SOURCE) {
- pa_source *source = pa_idxset_get_by_index(u->core->sources, s->loaded.index);
- assert(source);
+ *ret_map = sink->channel_map;
+ *ret_name = sink->name;
+ *ret_proplist = sink->proplist;
+ *ret_subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
+
+ } else if (pa_source_isinstance(s->device)) {
+ pa_source *source = PA_SOURCE(s->device);
+
*ret_ss = source->sample_spec;
- *ret_description = source->description;
+ *ret_map = source->channel_map;
+ *ret_name = source->name;
+ *ret_proplist = source->proplist;
+ *ret_subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
+
} else
- assert(0);
+ pa_assert_not_reached();
}
static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
char s[128];
- assert(c);
+ char *t;
+
+ pa_assert(c);
l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
- l = avahi_string_list_add_pair(l, "user-name", pa_get_user_name(s, sizeof(s)));
+
+ t = pa_get_user_name_malloc();
+ l = avahi_string_list_add_pair(l, "user-name", t);
+ pa_xfree(t);
+
+ t = pa_machine_id();
+ l = avahi_string_list_add_pair(l, "machine-id", t);
+ pa_xfree(t);
+
+ t = pa_uname_string();
+ l = avahi_string_list_add_pair(l, "uname", t);
+ pa_xfree(t);
+
l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
return l;
}
-static int publish_service(struct userdata *u, struct service *s);
+static int publish_service(struct service *s);
static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
struct service *s = userdata;
- if (state == AVAHI_ENTRY_GROUP_COLLISION) {
- char *t;
+ pa_assert(s);
+
+ switch (state) {
+
+ case AVAHI_ENTRY_GROUP_ESTABLISHED:
+ pa_log_info("Successfully established service %s.", s->service_name);
+ break;
+
+ case AVAHI_ENTRY_GROUP_COLLISION: {
+ char *t;
+
+ t = avahi_alternative_service_name(s->service_name);
+ pa_log_info("Name collision, renaming %s to %s.", s->service_name, t);
+ pa_xfree(s->service_name);
+ s->service_name = t;
+
+ publish_service(s);
+ break;
+ }
- t = avahi_alternative_service_name(s->service_name);
- pa_xfree(s->service_name);
- s->service_name = t;
+ case AVAHI_ENTRY_GROUP_FAILURE: {
+ pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
- publish_service(s->userdata, s);
+ avahi_entry_group_free(g);
+ s->entry_group = NULL;
+
+ break;
+ }
+
+ case AVAHI_ENTRY_GROUP_UNCOMMITED:
+ case AVAHI_ENTRY_GROUP_REGISTERING:
+ ;
}
}
-static int publish_service(struct userdata *u, struct service *s) {
- int r = -1;
- AvahiStringList *txt = NULL;
+static void service_free(struct service *s);
- assert(u);
- assert(s);
+static uint16_t compute_port(struct userdata *u) {
+ pa_strlist *i;
- if (!u->client || avahi_client_get_state(u->client) != AVAHI_CLIENT_S_RUNNING)
- return 0;
-
- if ((s->published == PUBLISHED_REAL && s->loaded.valid) ||
- (s->published == PUBLISHED_AUTOLOAD && s->autoload.valid && !s->loaded.valid))
+ pa_assert(u);
+
+ for (i = pa_native_protocol_servers(u->native); i; i = pa_strlist_next(i)) {
+ pa_parsed_address a;
+
+ if (pa_parse_address(pa_strlist_data(i), &a) >= 0 &&
+ (a.type == PA_PARSED_ADDRESS_TCP4 ||
+ a.type == PA_PARSED_ADDRESS_TCP6 ||
+ a.type == PA_PARSED_ADDRESS_TCP_AUTO) &&
+ a.port > 0) {
+
+ pa_xfree(a.path_or_host);
+ return a.port;
+ }
+
+ pa_xfree(a.path_or_host);
+ }
+
+ return PA_NATIVE_DEFAULT_PORT;
+}
+
+static int publish_service(struct service *s) {
+ int r = -1;
+ AvahiStringList *txt = NULL;
+ const char *name = NULL, *t;
+ pa_proplist *proplist = NULL;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
+ enum service_subtype subtype;
+
+ const char * const subtype_text[] = {
+ [SUBTYPE_HARDWARE] = "hardware",
+ [SUBTYPE_VIRTUAL] = "virtual",
+ [SUBTYPE_MONITOR] = "monitor"
+ };
+
+ pa_assert(s);
+
+ if (!s->userdata->client || avahi_client_get_state(s->userdata->client) != AVAHI_CLIENT_S_RUNNING)
return 0;
- if (s->published != UNPUBLISHED) {
- avahi_entry_group_reset(s->entry_group);
- s->published = UNPUBLISHED;
- }
-
- if (s->loaded.valid || s->autoload.valid) {
- pa_namereg_type_t type;
-
- if (!s->entry_group) {
- if (!(s->entry_group = avahi_entry_group_new(u->client, service_entry_group_callback, s))) {
- pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(u->client)));
- goto finish;
- }
+ if (!s->entry_group) {
+ if (!(s->entry_group = avahi_entry_group_new(s->userdata->client, service_entry_group_callback, s))) {
+ pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
+ goto finish;
}
-
- txt = avahi_string_list_add_pair(txt, "device", s->name);
- txt = txt_record_server_data(u->core, txt);
-
- if (s->loaded.valid) {
- char *description;
- pa_sample_spec ss;
-
- get_service_data(u, s, &ss, &description);
-
- txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
- txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
- txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
- txt = avahi_string_list_add_pair(txt, "description", description);
-
- type = s->loaded.type;
- } else if (s->autoload.valid)
- type = s->autoload.type;
-
- if (avahi_entry_group_add_service_strlst(
+ } else
+ avahi_entry_group_reset(s->entry_group);
+
+ txt = txt_record_server_data(s->userdata->core, txt);
+
+ get_service_data(s, &ss, &map, &name, &proplist, &subtype);
+ txt = avahi_string_list_add_pair(txt, "device", name);
+ txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
+ txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
+ txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
+ txt = avahi_string_list_add_pair(txt, "channel_map", pa_channel_map_snprint(cm, sizeof(cm), &map));
+ txt = avahi_string_list_add_pair(txt, "subtype", subtype_text[subtype]);
+
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ txt = avahi_string_list_add_pair(txt, "description", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_ICON_NAME)))
+ txt = avahi_string_list_add_pair(txt, "icon-name", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_VENDOR_NAME)))
+ txt = avahi_string_list_add_pair(txt, "vendor-name", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_PRODUCT_NAME)))
+ txt = avahi_string_list_add_pair(txt, "product-name", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_CLASS)))
+ txt = avahi_string_list_add_pair(txt, "class", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_FORM_FACTOR)))
+ txt = avahi_string_list_add_pair(txt, "form-factor", t);
+
+ if (avahi_entry_group_add_service_strlst(
+ s->entry_group,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ 0,
+ s->service_name,
+ pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
+ NULL,
+ NULL,
+ compute_port(s->userdata),
+ txt) < 0) {
+
+ pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
+ goto finish;
+ }
+
+ if (avahi_entry_group_add_service_subtype(
+ s->entry_group,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ 0,
+ s->service_name,
+ pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
+ NULL,
+ pa_sink_isinstance(s->device) ? (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SINK_HARDWARE : SERVICE_SUBTYPE_SINK_VIRTUAL) :
+ (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SOURCE_HARDWARE : (subtype == SUBTYPE_VIRTUAL ? SERVICE_SUBTYPE_SOURCE_VIRTUAL : SERVICE_SUBTYPE_SOURCE_MONITOR))) < 0) {
+
+ pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
+ goto finish;
+ }
+
+ if (pa_source_isinstance(s->device) && subtype != SUBTYPE_MONITOR) {
+ if (avahi_entry_group_add_service_subtype(
s->entry_group,
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
0,
s->service_name,
- type == PA_NAMEREG_SINK ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
- NULL,
+ SERVICE_TYPE_SOURCE,
NULL,
- u->port,
- txt) < 0) {
-
- pa_log(__FILE__": avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(u->client)));
- goto finish;
- }
-
- if (avahi_entry_group_commit(s->entry_group) < 0) {
- pa_log(__FILE__": avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(u->client)));
+ SERVICE_SUBTYPE_SOURCE_NON_MONITOR) < 0) {
+
+ pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
goto finish;
}
-
- if (s->loaded.valid)
- s->published = PUBLISHED_REAL;
- else if (s->autoload.valid)
- s->published = PUBLISHED_AUTOLOAD;
}
-
+
+ if (avahi_entry_group_commit(s->entry_group) < 0) {
+ pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
+ goto finish;
+ }
+
r = 0;
-
+ pa_log_debug("Successfully created entry group for %s.", s->service_name);
+
finish:
- if (s->published == UNPUBLISHED) {
- /* Remove this service */
+ /* Remove this service */
+ if (r < 0)
+ service_free(s);
- if (s->entry_group) {
- avahi_entry_group_free(s->entry_group);
- }
-
- pa_hashmap_remove(u->services, s->name);
- pa_xfree(s->name);
- pa_xfree(s->service_name);
- pa_xfree(s);
- }
+ avahi_string_list_free(txt);
- if (txt)
- avahi_string_list_free(txt);
-
return r;
}
-static struct service *get_service(struct userdata *u, const char *name) {
+static struct service *get_service(struct userdata *u, pa_object *device) {
struct service *s;
- char hn[64];
-
- if ((s = pa_hashmap_get(u->services, name)))
+ char *hn, *un;
+ const char *n;
+
+ pa_assert(u);
+ pa_object_assert_ref(device);
+
+ if ((s = pa_hashmap_get(u->services, device)))
return s;
-
+
s = pa_xnew(struct service, 1);
s->userdata = u;
s->entry_group = NULL;
- s->published = UNPUBLISHED;
- s->name = pa_xstrdup(name);
- s->loaded.valid = s->autoload.valid = 0;
- s->service_name = pa_sprintf_malloc("%s on %s", s->name, pa_get_host_name(hn, sizeof(hn)));
-
- pa_hashmap_put(u->services, s->name, s);
-
- return s;
-}
+ s->device = device;
+
+ if (pa_sink_isinstance(device)) {
+ if (!(n = pa_proplist_gets(PA_SINK(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ n = PA_SINK(device)->name;
+ } else {
+ if (!(n = pa_proplist_gets(PA_SOURCE(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ n = PA_SOURCE(device)->name;
+ }
-static int publish_sink(struct userdata *u, pa_sink *s) {
- struct service *svc;
- assert(u && s);
+ hn = pa_get_host_name_malloc();
+ un = pa_get_user_name_malloc();
- svc = get_service(u, s->name);
- if (svc->loaded.valid)
- return publish_service(u, svc);
+ s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", un, hn, n), AVAHI_LABEL_MAX-1);
- svc->loaded.valid = 1;
- svc->loaded.type = PA_NAMEREG_SINK;
- svc->loaded.index = s->index;
+ pa_xfree(un);
+ pa_xfree(hn);
- pa_dynarray_put(u->sink_dynarray, s->index, svc);
+ pa_hashmap_put(u->services, s->device, s);
- return publish_service(u, svc);
+ return s;
}
-static int publish_source(struct userdata *u, pa_source *s) {
- struct service *svc;
- assert(u && s);
+static void service_free(struct service *s) {
+ pa_assert(s);
- svc = get_service(u, s->name);
- if (svc->loaded.valid)
- return publish_service(u, svc);
+ pa_hashmap_remove(s->userdata->services, s->device);
- svc->loaded.valid = 1;
- svc->loaded.type = PA_NAMEREG_SOURCE;
- svc->loaded.index = s->index;
+ if (s->entry_group) {
+ pa_log_debug("Removing entry group for %s.", s->service_name);
+ avahi_entry_group_free(s->entry_group);
+ }
- pa_dynarray_put(u->source_dynarray, s->index, svc);
-
- return publish_service(u, svc);
+ pa_xfree(s->service_name);
+ pa_xfree(s);
}
-static int publish_autoload(struct userdata *u, pa_autoload_entry *s) {
- struct service *svc;
- assert(u && s);
+static pa_bool_t shall_ignore(pa_object *o) {
+ pa_object_assert_ref(o);
- svc = get_service(u, s->name);
- if (svc->autoload.valid)
- return publish_service(u, svc);
+ if (pa_sink_isinstance(o))
+ return !!(PA_SINK(o)->flags & PA_SINK_NETWORK);
- svc->autoload.valid = 1;
- svc->autoload.type = s->type;
- svc->autoload.index = s->index;
+ if (pa_source_isinstance(o))
+ return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK);
- pa_dynarray_put(u->autoload_dynarray, s->index, svc);
-
- return publish_service(u, svc);
+ pa_assert_not_reached();
}
-static int remove_sink(struct userdata *u, uint32_t idx) {
- struct service *svc;
- assert(u && idx != PA_INVALID_INDEX);
+static pa_hook_result_t device_new_or_changed_cb(pa_core *c, pa_object *o, struct userdata *u) {
+ pa_assert(c);
+ pa_object_assert_ref(o);
- if (!(svc = pa_dynarray_get(u->sink_dynarray, idx)))
- return 0;
+ if (!shall_ignore(o))
+ publish_service(get_service(u, o));
- if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SINK)
- return 0;
-
- svc->loaded.valid = 0;
- pa_dynarray_put(u->sink_dynarray, idx, NULL);
-
- return publish_service(u, svc);
+ return PA_HOOK_OK;
}
-static int remove_source(struct userdata *u, uint32_t idx) {
- struct service *svc;
- assert(u && idx != PA_INVALID_INDEX);
-
- if (!(svc = pa_dynarray_get(u->source_dynarray, idx)))
- return 0;
+static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
+ struct service *s;
- if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SOURCE)
- return 0;
+ pa_assert(c);
+ pa_object_assert_ref(o);
- svc->loaded.valid = 0;
- pa_dynarray_put(u->source_dynarray, idx, NULL);
+ if ((s = pa_hashmap_get(u->services, o)))
+ service_free(s);
- return publish_service(u, svc);
+ return PA_HOOK_OK;
}
-static int remove_autoload(struct userdata *u, uint32_t idx) {
- struct service *svc;
- assert(u && idx != PA_INVALID_INDEX);
-
- if (!(svc = pa_dynarray_get(u->autoload_dynarray, idx)))
- return 0;
-
- if (!svc->autoload.valid)
- return 0;
-
- svc->autoload.valid = 0;
- pa_dynarray_put(u->autoload_dynarray, idx, NULL);
-
- return publish_service(u, svc);
-}
+static int publish_main_service(struct userdata *u);
-static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
struct userdata *u = userdata;
- assert(u && c);
+ pa_assert(u);
- switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
- case PA_SUBSCRIPTION_EVENT_SINK: {
- if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
- pa_sink *sink;
+ switch (state) {
- if ((sink = pa_idxset_get_by_index(c->sinks, idx))) {
- if (publish_sink(u, sink) < 0)
- goto fail;
- }
- } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
- if (remove_sink(u, idx) < 0)
- goto fail;
- }
-
+ case AVAHI_ENTRY_GROUP_ESTABLISHED:
+ pa_log_info("Successfully established main service.");
break;
- case PA_SUBSCRIPTION_EVENT_SOURCE:
+ case AVAHI_ENTRY_GROUP_COLLISION: {
+ char *t;
- if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
- pa_source *source;
-
- if ((source = pa_idxset_get_by_index(c->sources, idx))) {
- if (publish_source(u, source) < 0)
- goto fail;
- }
- } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
- if (remove_source(u, idx) < 0)
- goto fail;
- }
-
- break;
+ t = avahi_alternative_service_name(u->service_name);
+ pa_log_info("Name collision: renaming main service %s to %s.", u->service_name, t);
+ pa_xfree(u->service_name);
+ u->service_name = t;
- case PA_SUBSCRIPTION_EVENT_AUTOLOAD:
- if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
- pa_autoload_entry *autoload;
-
- if ((autoload = pa_idxset_get_by_index(c->autoload_idxset, idx))) {
- if (publish_autoload(u, autoload) < 0)
- goto fail;
- }
- } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
- if (remove_autoload(u, idx) < 0)
- goto fail;
- }
-
+ publish_main_service(u);
break;
- }
-
- return;
-
-fail:
- if (u->subscription) {
- pa_subscription_free(u->subscription);
- u->subscription = NULL;
- }
-}
-
-static int publish_main_service(struct userdata *u);
-
-static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
- struct userdata *u = userdata;
- assert(u);
+ }
- if (state == AVAHI_ENTRY_GROUP_COLLISION) {
- char *t;
+ case AVAHI_ENTRY_GROUP_FAILURE: {
+ pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
- t = avahi_alternative_service_name(u->service_name);
- pa_xfree(u->service_name);
- u->service_name = t;
+ avahi_entry_group_free(g);
+ u->main_entry_group = NULL;
+ break;
+ }
- publish_main_service(u);
+ case AVAHI_ENTRY_GROUP_UNCOMMITED:
+ case AVAHI_ENTRY_GROUP_REGISTERING:
+ break;
}
}
static int publish_main_service(struct userdata *u) {
AvahiStringList *txt = NULL;
int r = -1;
-
+
+ pa_assert(u);
+
if (!u->main_entry_group) {
if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
- pa_log(__FILE__": avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+ pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
goto fail;
}
} else
avahi_entry_group_reset(u->main_entry_group);
-
- txt = txt_record_server_data(u->core, NULL);
+
+ txt = txt_record_server_data(u->core, txt);
if (avahi_entry_group_add_service_strlst(
u->main_entry_group,
@@ -459,20 +481,20 @@ static int publish_main_service(struct userdata *u) {
SERVICE_TYPE_SERVER,
NULL,
NULL,
- u->port,
+ compute_port(u),
txt) < 0) {
-
- pa_log(__FILE__": avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+
+ pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
goto fail;
}
-
+
if (avahi_entry_group_commit(u->main_entry_group) < 0) {
- pa_log(__FILE__": avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+ pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
goto fail;
}
r = 0;
-
+
fail:
avahi_string_list_free(txt);
@@ -482,196 +504,197 @@ fail:
static int publish_all_services(struct userdata *u) {
pa_sink *sink;
pa_source *source;
- pa_autoload_entry *autoload;
int r = -1;
uint32_t idx;
-
- assert(u);
- pa_log_debug(__FILE__": Publishing services in Zeroconf");
+ pa_assert(u);
- for (sink = pa_idxset_first(u->core->sinks, &idx); sink; sink = pa_idxset_next(u->core->sinks, &idx))
- if (publish_sink(u, sink) < 0)
- goto fail;
+ pa_log_debug("Publishing services in Zeroconf");
- for (source = pa_idxset_first(u->core->sources, &idx); source; source = pa_idxset_next(u->core->sources, &idx))
- if (publish_source(u, source) < 0)
- goto fail;
+ for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
+ if (!shall_ignore(PA_OBJECT(sink)))
+ publish_service(get_service(u, PA_OBJECT(sink)));
- if (u->core->autoload_idxset)
- for (autoload = pa_idxset_first(u->core->autoload_idxset, &idx); autoload; autoload = pa_idxset_next(u->core->autoload_idxset, &idx))
- if (publish_autoload(u, autoload) < 0)
- goto fail;
+ for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
+ if (!shall_ignore(PA_OBJECT(source)))
+ publish_service(get_service(u, PA_OBJECT(source)));
if (publish_main_service(u) < 0)
goto fail;
-
+
r = 0;
-
+
fail:
return r;
}
-static void unpublish_all_services(struct userdata *u, int rem) {
+static void unpublish_all_services(struct userdata *u, pa_bool_t rem) {
void *state = NULL;
struct service *s;
-
- assert(u);
- pa_log_debug(__FILE__": Unpublishing services in Zeroconf");
+ pa_assert(u);
+
+ pa_log_debug("Unpublishing services in Zeroconf");
while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
if (s->entry_group) {
if (rem) {
+ pa_log_debug("Removing entry group for %s.", s->service_name);
avahi_entry_group_free(s->entry_group);
s->entry_group = NULL;
- } else
+ } else {
avahi_entry_group_reset(s->entry_group);
+ pa_log_debug("Resetting entry group for %s.", s->service_name);
+ }
}
-
- s->published = UNPUBLISHED;
}
if (u->main_entry_group) {
if (rem) {
+ pa_log_debug("Removing main entry group.");
avahi_entry_group_free(u->main_entry_group);
u->main_entry_group = NULL;
- } else
+ } else {
avahi_entry_group_reset(u->main_entry_group);
+ pa_log_debug("Resetting main entry group.");
+ }
}
}
static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
struct userdata *u = userdata;
- assert(c);
+
+ pa_assert(c);
+ pa_assert(u);
u->client = c;
-
+
switch (state) {
case AVAHI_CLIENT_S_RUNNING:
publish_all_services(u);
break;
-
+
case AVAHI_CLIENT_S_COLLISION:
- unpublish_all_services(u, 0);
+ pa_log_debug("Host name collision");
+ unpublish_all_services(u, FALSE);
break;
case AVAHI_CLIENT_FAILURE:
if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
int error;
- unpublish_all_services(u, 1);
+
+ pa_log_debug("Avahi daemon disconnected.");
+
+ unpublish_all_services(u, TRUE);
avahi_client_free(u->client);
- if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error)))
- pa_log(__FILE__": pa_avahi_client_new() failed: %s", avahi_strerror(error));
+ if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
+ pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
+ pa_module_unload_request(u->module, TRUE);
+ }
}
-
+
break;
default: ;
}
}
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
+
struct userdata *u;
- uint32_t port = PA_NATIVE_DEFAULT_PORT;
pa_modargs *ma = NULL;
- char hn[256];
+ char *hn, *un;
int error;
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
- goto fail;
- }
-
- if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port == 0 || port >= 0xFFFF) {
- pa_log(__FILE__": invalid port specified.");
+ pa_log("Failed to parse module arguments.");
goto fail;
}
m->userdata = u = pa_xnew(struct userdata, 1);
- u->core = c;
- u->port = (uint16_t) port;
+ u->core = m->core;
+ u->module = m;
+ u->native = pa_native_protocol_get(u->core);
+
+ u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
- u->avahi_poll = pa_avahi_poll_new(c->mainloop);
-
- u->services = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
- u->sink_dynarray = pa_dynarray_new();
- u->source_dynarray = pa_dynarray_new();
- u->autoload_dynarray = pa_dynarray_new();
+ u->services = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
- u->subscription = pa_subscription_new(c,
- PA_SUBSCRIPTION_MASK_SINK|
- PA_SUBSCRIPTION_MASK_SOURCE|
- PA_SUBSCRIPTION_MASK_AUTOLOAD, subscribe_callback, u);
+ u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->sink_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
+ u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->source_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
u->main_entry_group = NULL;
- u->service_name = pa_xstrdup(pa_get_host_name(hn, sizeof(hn)));
+ un = pa_get_user_name_malloc();
+ hn = pa_get_host_name_malloc();
+ u->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s", un, hn), AVAHI_LABEL_MAX-1);
+ pa_xfree(un);
+ pa_xfree(hn);
if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
- pa_log(__FILE__": pa_avahi_client_new() failed: %s", avahi_strerror(error));
+ pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
goto fail;
}
pa_modargs_free(ma);
-
+
return 0;
-
+
fail:
- pa__done(c, m);
+ pa__done(m);
if (ma)
pa_modargs_free(ma);
-
- return -1;
-}
-
-static void service_free(void *p, void *userdata) {
- struct service *s = p;
- struct userdata *u = userdata;
- assert(s);
- assert(u);
-
- if (s->entry_group)
- avahi_entry_group_free(s->entry_group);
-
- pa_xfree(s->service_name);
- pa_xfree(s->name);
- pa_xfree(s);
+ return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata*u;
- assert(c && m);
+ pa_assert(m);
if (!(u = m->userdata))
return;
- if (u->services)
- pa_hashmap_free(u->services, service_free, u);
+ if (u->services) {
+ struct service *s;
+
+ while ((s = pa_hashmap_first(u->services)))
+ service_free(s);
+
+ pa_hashmap_free(u->services, NULL, NULL);
+ }
- if (u->sink_dynarray)
- pa_dynarray_free(u->sink_dynarray, NULL, NULL);
- if (u->source_dynarray)
- pa_dynarray_free(u->source_dynarray, NULL, NULL);
- if (u->autoload_dynarray)
- pa_dynarray_free(u->autoload_dynarray, NULL, NULL);
-
- if (u->subscription)
- pa_subscription_free(u->subscription);
+ if (u->sink_new_slot)
+ pa_hook_slot_free(u->sink_new_slot);
+ if (u->source_new_slot)
+ pa_hook_slot_free(u->source_new_slot);
+ if (u->sink_changed_slot)
+ pa_hook_slot_free(u->sink_changed_slot);
+ if (u->source_changed_slot)
+ pa_hook_slot_free(u->source_changed_slot);
+ if (u->sink_unlink_slot)
+ pa_hook_slot_free(u->sink_unlink_slot);
+ if (u->source_unlink_slot)
+ pa_hook_slot_free(u->source_unlink_slot);
if (u->main_entry_group)
avahi_entry_group_free(u->main_entry_group);
-
+
if (u->client)
avahi_client_free(u->client);
-
+
if (u->avahi_poll)
pa_avahi_poll_free(u->avahi_poll);
+ if (u->native)
+ pa_native_protocol_unref(u->native);
+
pa_xfree(u->service_name);
pa_xfree(u);
}
-
diff --git a/src/modules/oss-util.c b/src/modules/oss-util.c
deleted file mode 100644
index f4cc45de..00000000
--- a/src/modules/oss-util.c
+++ /dev/null
@@ -1,299 +0,0 @@
-/* $Id$ */
-
-/***
- This file is part of PulseAudio.
-
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
-
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <assert.h>
-#include <sys/soundcard.h>
-#include <sys/ioctl.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#include <pulsecore/core-error.h>
-#include <pulsecore/core-util.h>
-#include <pulsecore/log.h>
-
-#include "oss-util.h"
-
-int pa_oss_open(const char *device, int *mode, int* pcaps) {
- int fd = -1;
- assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
-
- if (*mode == O_RDWR) {
- if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
- int dcaps, *tcaps;
- ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
-
- tcaps = pcaps ? pcaps : &dcaps;
-
- if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
- goto fail;
- }
-
- if (*tcaps & DSP_CAP_DUPLEX)
- goto success;
-
- pa_log_warn(__FILE__": '%s' doesn't support full duplex", device);
-
- close(fd);
- }
-
- if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY)) < 0) {
- if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY)) < 0) {
- pa_log(__FILE__": open('%s'): %s", device, pa_cstrerror(errno));
- goto fail;
- }
- }
- } else {
- if ((fd = open(device, *mode|O_NDELAY)) < 0) {
- pa_log(__FILE__": open('%s'): %s", device, pa_cstrerror(errno));
- goto fail;
- }
- }
-
-success:
-
- if (pcaps) {
- if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
- goto fail;
- }
- }
-
- pa_fd_set_cloexec(fd, 1);
-
- return fd;
-
-fail:
- if (fd >= 0)
- close(fd);
- return -1;
-}
-
-int pa_oss_auto_format(int fd, pa_sample_spec *ss) {
- int format, channels, speed, reqformat;
- static const int format_trans[PA_SAMPLE_MAX] = {
- [PA_SAMPLE_U8] = AFMT_U8,
- [PA_SAMPLE_ALAW] = AFMT_A_LAW,
- [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
- [PA_SAMPLE_S16LE] = AFMT_S16_LE,
- [PA_SAMPLE_S16BE] = AFMT_S16_BE,
- [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
- [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
- };
-
- assert(fd >= 0 && ss);
-
- reqformat = format = format_trans[ss->format];
- if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
- format = AFMT_S16_NE;
- if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
- int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
- format = f;
- if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
- format = AFMT_U8;
- if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
- pa_log(__FILE__": SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : pa_cstrerror(errno));
- return -1;
- } else
- ss->format = PA_SAMPLE_U8;
- } else
- ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
- } else
- ss->format = PA_SAMPLE_S16NE;
- }
-
- channels = ss->channels;
- if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno));
- return -1;
- }
- assert(channels);
- ss->channels = channels;
-
- speed = ss->rate;
- if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno));
- return -1;
- }
- assert(speed);
- ss->rate = speed;
-
- return 0;
-}
-
-static int simple_log2(int v) {
- int k = 0;
-
- for (;;) {
- v >>= 1;
- if (!v) break;
- k++;
- }
-
- return k;
-}
-
-int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
- int arg;
- arg = ((int) nfrags << 16) | simple_log2(frag_size);
-
- if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno));
- return -1;
- }
-
- return 0;
-}
-
-static int pa_oss_get_volume(int fd, int mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
- char cv[PA_CVOLUME_SNPRINT_MAX];
- unsigned vol;
-
- assert(fd >= 0);
- assert(ss);
- assert(volume);
-
- if (ioctl(fd, mixer, &vol) < 0)
- return -1;
-
- volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
-
- if ((volume->channels = ss->channels) >= 2)
- volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
-
- pa_log_debug(__FILE__": Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
- return 0;
-}
-
-static int pa_oss_set_volume(int fd, int mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
- char cv[PA_CVOLUME_SNPRINT_MAX];
- unsigned vol;
- pa_volume_t l, r;
-
- l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
-
- vol = (l*100)/PA_VOLUME_NORM;
-
- if (ss->channels >= 2) {
- r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
- vol |= ((r*100)/PA_VOLUME_NORM) << 8;
- }
-
- if (ioctl(fd, mixer, &vol) < 0)
- return -1;
-
- pa_log_debug(__FILE__": Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
- return 0;
-}
-
-int pa_oss_get_pcm_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
- return pa_oss_get_volume(fd, SOUND_MIXER_READ_PCM, ss, volume);
-}
-
-int pa_oss_set_pcm_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
- return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_PCM, ss, volume);
-}
-
-int pa_oss_get_input_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
- return pa_oss_get_volume(fd, SOUND_MIXER_READ_IGAIN, ss, volume);
-}
-
-int pa_oss_set_input_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
- return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_IGAIN, ss, volume);
-}
-
-int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
- FILE *f;
- const char *e = NULL;
- int n, r = -1;
- int b = 0;
-
- if (strncmp(dev, "/dev/dsp", 8) == 0)
- e = dev+8;
- else if (strncmp(dev, "/dev/adsp", 9) == 0)
- e = dev+9;
- else
- return -1;
-
- if (*e == 0)
- n = 0;
- else if (*e >= '0' && *e <= '9' && *(e+1) == 0)
- n = *e - '0';
- else
- return -1;
-
- if (!(f = fopen("/dev/sndstat", "r")) &&
- !(f = fopen("/proc/sndstat", "r")) &&
- !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
-
- if (errno != ENOENT)
- pa_log_warn(__FILE__": failed to open OSS sndstat device: %s", pa_cstrerror(errno));
-
- return -1;
- }
-
- while (!feof(f)) {
- char line[64];
- int device;
-
- if (!fgets(line, sizeof(line), f))
- break;
-
- line[strcspn(line, "\r\n")] = 0;
-
- if (!b) {
- b = strcmp(line, "Audio devices:") == 0;
- continue;
- }
-
- if (line[0] == 0)
- break;
-
- if (sscanf(line, "%i: ", &device) != 1)
- continue;
-
- if (device == n) {
- char *k = strchr(line, ':');
- assert(k);
- k++;
- k += strspn(k, " ");
-
- if (pa_endswith(k, " (DUPLEX)"))
- k[strlen(k)-9] = 0;
-
- pa_strlcpy(name, k, l);
- r = 0;
- break;
- }
- }
-
- fclose(f);
- return r;
-}
diff --git a/src/modules/oss/module-oss.c b/src/modules/oss/module-oss.c
new file mode 100644
index 00000000..2a99d119
--- /dev/null
+++ b/src/modules/oss/module-oss.c
@@ -0,0 +1,1570 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/* General power management rules:
+ *
+ * When SUSPENDED we close the audio device.
+ *
+ * We make no difference between IDLE and RUNNING in our handling.
+ *
+ * As long as we are in RUNNING/IDLE state we will *always* write data to
+ * the device. If none is avilable from the inputs, we write silence
+ * instead.
+ *
+ * If power should be saved on IDLE module-suspend-on-idle should be used.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_SYS_MMAN_H
+#include <sys/mman.h>
+#endif
+
+#include <sys/soundcard.h>
+#include <sys/ioctl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/util.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/source.h>
+#include <pulsecore/module.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/poll.h>
+
+#if defined(__NetBSD__) && !defined(SNDCTL_DSP_GETODELAY)
+#include <sys/audioio.h>
+#include <sys/syscall.h>
+#endif
+
+#include "oss-util.h"
+#include "module-oss-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("OSS Sink/Source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "source_name=<name for the source> "
+ "source_properties=<properties for the source> "
+ "device=<OSS device> "
+ "record=<enable source?> "
+ "playback=<enable sink?> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
+ "fragments=<number of fragments> "
+ "fragment_size=<fragment size> "
+ "mmap=<enable memory mapping?>");
+#ifdef __linux__
+PA_MODULE_DEPRECATED("Please use module-alsa-card instead of module-oss!");
+#endif
+
+#define DEFAULT_DEVICE "/dev/dsp"
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_sink *sink;
+ pa_source *source;
+
+ pa_thread *thread;
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+
+ char *device_name;
+
+ pa_memchunk memchunk;
+
+ size_t frame_size;
+ uint32_t in_fragment_size, out_fragment_size, in_nfrags, out_nfrags, in_hwbuf_size, out_hwbuf_size;
+ pa_bool_t use_getospace, use_getispace;
+ pa_bool_t use_getodelay;
+
+ pa_bool_t sink_suspended, source_suspended;
+
+ int fd;
+ int mode;
+
+ int mixer_fd;
+ int mixer_devmask;
+
+ int nfrags, frag_size, orig_frag_size;
+
+ pa_bool_t use_mmap;
+ unsigned out_mmap_current, in_mmap_current;
+ void *in_mmap, *out_mmap;
+ pa_memblock **in_mmap_memblocks, **out_mmap_memblocks;
+
+ int in_mmap_saved_nfrags, out_mmap_saved_nfrags;
+
+ pa_rtpoll_item *rtpoll_item;
+};
+
+static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
+ "source_name",
+ "source_properties",
+ "device",
+ "record",
+ "playback",
+ "fragments",
+ "fragment_size",
+ "format",
+ "rate",
+ "channels",
+ "channel_map",
+ "mmap",
+ NULL
+};
+
+static void trigger(struct userdata *u, pa_bool_t quick) {
+ int enable_bits = 0, zero = 0;
+
+ pa_assert(u);
+
+ if (u->fd < 0)
+ return;
+
+ pa_log_debug("trigger");
+
+ if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state))
+ enable_bits |= PCM_ENABLE_INPUT;
+
+ if (u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state))
+ enable_bits |= PCM_ENABLE_OUTPUT;
+
+ pa_log_debug("trigger: %i", enable_bits);
+
+
+ if (u->use_mmap) {
+
+ if (!quick)
+ ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero);
+
+#ifdef SNDCTL_DSP_HALT
+ if (enable_bits == 0)
+ if (ioctl(u->fd, SNDCTL_DSP_HALT, NULL) < 0)
+ pa_log_warn("SNDCTL_DSP_HALT: %s", pa_cstrerror(errno));
+#endif
+
+ if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0)
+ pa_log_warn("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
+
+ if (u->sink && !(enable_bits & PCM_ENABLE_OUTPUT)) {
+ pa_log_debug("clearing playback buffer");
+ pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &u->sink->sample_spec);
+ }
+
+ } else {
+
+ if (enable_bits)
+ if (ioctl(u->fd, SNDCTL_DSP_POST, NULL) < 0)
+ pa_log_warn("SNDCTL_DSP_POST: %s", pa_cstrerror(errno));
+
+ if (!quick) {
+ /*
+ * Some crappy drivers do not start the recording until we
+ * read something. Without this snippet, poll will never
+ * register the fd as ready.
+ */
+
+ if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
+ uint8_t *buf = pa_xnew(uint8_t, u->in_fragment_size);
+ pa_read(u->fd, buf, u->in_fragment_size, NULL);
+ pa_xfree(buf);
+ }
+ }
+ }
+}
+
+static void mmap_fill_memblocks(struct userdata *u, unsigned n) {
+ pa_assert(u);
+ pa_assert(u->out_mmap_memblocks);
+
+/* pa_log("Mmmap writing %u blocks", n); */
+
+ while (n > 0) {
+ pa_memchunk chunk;
+
+ if (u->out_mmap_memblocks[u->out_mmap_current])
+ pa_memblock_unref_fixed(u->out_mmap_memblocks[u->out_mmap_current]);
+
+ chunk.memblock = u->out_mmap_memblocks[u->out_mmap_current] =
+ pa_memblock_new_fixed(
+ u->core->mempool,
+ (uint8_t*) u->out_mmap + u->out_fragment_size * u->out_mmap_current,
+ u->out_fragment_size,
+ 1);
+
+ chunk.length = pa_memblock_get_length(chunk.memblock);
+ chunk.index = 0;
+
+ pa_sink_render_into_full(u->sink, &chunk);
+
+ u->out_mmap_current++;
+ while (u->out_mmap_current >= u->out_nfrags)
+ u->out_mmap_current -= u->out_nfrags;
+
+ n--;
+ }
+}
+
+static int mmap_write(struct userdata *u) {
+ struct count_info info;
+
+ pa_assert(u);
+ pa_assert(u->sink);
+
+/* pa_log("Mmmap writing..."); */
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
+ pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
+ return -1;
+ }
+
+ info.blocks += u->out_mmap_saved_nfrags;
+ u->out_mmap_saved_nfrags = 0;
+
+ if (info.blocks > 0)
+ mmap_fill_memblocks(u, (unsigned) info.blocks);
+
+ return info.blocks;
+}
+
+static void mmap_post_memblocks(struct userdata *u, unsigned n) {
+ pa_assert(u);
+ pa_assert(u->in_mmap_memblocks);
+
+/* pa_log("Mmmap reading %u blocks", n); */
+
+ while (n > 0) {
+ pa_memchunk chunk;
+
+ if (!u->in_mmap_memblocks[u->in_mmap_current]) {
+
+ chunk.memblock = u->in_mmap_memblocks[u->in_mmap_current] =
+ pa_memblock_new_fixed(
+ u->core->mempool,
+ (uint8_t*) u->in_mmap + u->in_fragment_size*u->in_mmap_current,
+ u->in_fragment_size,
+ 1);
+
+ chunk.length = pa_memblock_get_length(chunk.memblock);
+ chunk.index = 0;
+
+ pa_source_post(u->source, &chunk);
+ }
+
+ u->in_mmap_current++;
+ while (u->in_mmap_current >= u->in_nfrags)
+ u->in_mmap_current -= u->in_nfrags;
+
+ n--;
+ }
+}
+
+static void mmap_clear_memblocks(struct userdata*u, unsigned n) {
+ unsigned i = u->in_mmap_current;
+
+ pa_assert(u);
+ pa_assert(u->in_mmap_memblocks);
+
+ if (n > u->in_nfrags)
+ n = u->in_nfrags;
+
+ while (n > 0) {
+ if (u->in_mmap_memblocks[i]) {
+ pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
+ u->in_mmap_memblocks[i] = NULL;
+ }
+
+ i++;
+ while (i >= u->in_nfrags)
+ i -= u->in_nfrags;
+
+ n--;
+ }
+}
+
+static int mmap_read(struct userdata *u) {
+ struct count_info info;
+ pa_assert(u);
+ pa_assert(u->source);
+
+/* pa_log("Mmmap reading..."); */
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
+ pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
+ return -1;
+ }
+
+/* pa_log("... %i", info.blocks); */
+
+ info.blocks += u->in_mmap_saved_nfrags;
+ u->in_mmap_saved_nfrags = 0;
+
+ if (info.blocks > 0) {
+ mmap_post_memblocks(u, (unsigned) info.blocks);
+ mmap_clear_memblocks(u, u->in_nfrags/2);
+ }
+
+ return info.blocks;
+}
+
+static pa_usec_t mmap_sink_get_latency(struct userdata *u) {
+ struct count_info info;
+ size_t bpos, n;
+
+ pa_assert(u);
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
+ pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
+ return 0;
+ }
+
+ u->out_mmap_saved_nfrags += info.blocks;
+
+ bpos = ((u->out_mmap_current + (unsigned) u->out_mmap_saved_nfrags) * u->out_fragment_size) % u->out_hwbuf_size;
+
+ if (bpos <= (size_t) info.ptr)
+ n = u->out_hwbuf_size - ((size_t) info.ptr - bpos);
+ else
+ n = bpos - (size_t) info.ptr;
+
+/* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->out_fragment_size, u->out_fragments); */
+
+ return pa_bytes_to_usec(n, &u->sink->sample_spec);
+}
+
+static pa_usec_t mmap_source_get_latency(struct userdata *u) {
+ struct count_info info;
+ size_t bpos, n;
+
+ pa_assert(u);
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
+ pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
+ return 0;
+ }
+
+ u->in_mmap_saved_nfrags += info.blocks;
+ bpos = ((u->in_mmap_current + (unsigned) u->in_mmap_saved_nfrags) * u->in_fragment_size) % u->in_hwbuf_size;
+
+ if (bpos <= (size_t) info.ptr)
+ n = (size_t) info.ptr - bpos;
+ else
+ n = u->in_hwbuf_size - bpos + (size_t) info.ptr;
+
+/* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->in_fragment_size, u->in_fragments); */
+
+ return pa_bytes_to_usec(n, &u->source->sample_spec);
+}
+
+static pa_usec_t io_sink_get_latency(struct userdata *u) {
+ pa_usec_t r = 0;
+
+ pa_assert(u);
+
+ if (u->use_getodelay) {
+ int arg;
+#if defined(__NetBSD__) && !defined(SNDCTL_DSP_GETODELAY)
+#if defined(AUDIO_GETBUFINFO)
+ struct audio_info info;
+ if (syscall(SYS_ioctl, u->fd, AUDIO_GETBUFINFO, &info) < 0) {
+ pa_log_info("Device doesn't support AUDIO_GETBUFINFO: %s", pa_cstrerror(errno));
+ u->use_getodelay = 0;
+ } else {
+ arg = info.play.seek + info.blocksize / 2;
+ r = pa_bytes_to_usec((size_t) arg, &u->sink->sample_spec);
+ }
+#else
+ pa_log_info("System doesn't support AUDIO_GETBUFINFO");
+ u->use_getodelay = 0;
+#endif
+#else
+ if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
+ pa_log_info("Device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
+ u->use_getodelay = 0;
+ } else
+ r = pa_bytes_to_usec((size_t) arg, &u->sink->sample_spec);
+#endif
+ }
+
+ if (!u->use_getodelay && u->use_getospace) {
+ struct audio_buf_info info;
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
+ pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
+ u->use_getospace = 0;
+ } else
+ r = pa_bytes_to_usec((size_t) info.bytes, &u->sink->sample_spec);
+ }
+
+ if (u->memchunk.memblock)
+ r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
+
+ return r;
+}
+
+static pa_usec_t io_source_get_latency(struct userdata *u) {
+ pa_usec_t r = 0;
+
+ pa_assert(u);
+
+ if (u->use_getispace) {
+ struct audio_buf_info info;
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
+ pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
+ u->use_getispace = 0;
+ } else
+ r = pa_bytes_to_usec((size_t) info.bytes, &u->source->sample_spec);
+ }
+
+ return r;
+}
+
+static void build_pollfd(struct userdata *u) {
+ struct pollfd *pollfd;
+
+ pa_assert(u);
+ pa_assert(u->fd >= 0);
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+
+ u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->fd = u->fd;
+ pollfd->events = 0;
+ pollfd->revents = 0;
+}
+
+/* Called from IO context */
+static int suspend(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(u->fd >= 0);
+
+ pa_log_info("Suspending...");
+
+ if (u->out_mmap_memblocks) {
+ unsigned i;
+ for (i = 0; i < u->out_nfrags; i++)
+ if (u->out_mmap_memblocks[i]) {
+ pa_memblock_unref_fixed(u->out_mmap_memblocks[i]);
+ u->out_mmap_memblocks[i] = NULL;
+ }
+ }
+
+ if (u->in_mmap_memblocks) {
+ unsigned i;
+ for (i = 0; i < u->in_nfrags; i++)
+ if (u->in_mmap_memblocks[i]) {
+ pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
+ u->in_mmap_memblocks[i] = NULL;
+ }
+ }
+
+ if (u->in_mmap && u->in_mmap != MAP_FAILED) {
+ munmap(u->in_mmap, u->in_hwbuf_size);
+ u->in_mmap = NULL;
+ }
+
+ if (u->out_mmap && u->out_mmap != MAP_FAILED) {
+ munmap(u->out_mmap, u->out_hwbuf_size);
+ u->out_mmap = NULL;
+ }
+
+ /* Let's suspend */
+ ioctl(u->fd, SNDCTL_DSP_SYNC, NULL);
+ pa_close(u->fd);
+ u->fd = -1;
+
+ if (u->rtpoll_item) {
+ pa_rtpoll_item_free(u->rtpoll_item);
+ u->rtpoll_item = NULL;
+ }
+
+ pa_log_info("Device suspended...");
+
+ return 0;
+}
+
+/* Called from IO context */
+static int unsuspend(struct userdata *u) {
+ int m;
+ pa_sample_spec ss, *ss_original;
+ int frag_size, in_frag_size, out_frag_size;
+ int in_nfrags, out_nfrags;
+ struct audio_buf_info info;
+
+ pa_assert(u);
+ pa_assert(u->fd < 0);
+
+ m = u->mode;
+
+ pa_log_info("Trying resume...");
+
+ if ((u->fd = pa_oss_open(u->device_name, &m, NULL)) < 0) {
+ pa_log_warn("Resume failed, device busy (%s)", pa_cstrerror(errno));
+ return -1;
+ }
+
+ if (m != u->mode) {
+ pa_log_warn("Resume failed, couldn't open device with original access mode.");
+ goto fail;
+ }
+
+ if (u->nfrags >= 2 && u->frag_size >= 1)
+ if (pa_oss_set_fragments(u->fd, u->nfrags, u->orig_frag_size) < 0) {
+ pa_log_warn("Resume failed, couldn't set original fragment settings.");
+ goto fail;
+ }
+
+ ss = *(ss_original = u->sink ? &u->sink->sample_spec : &u->source->sample_spec);
+ if (pa_oss_auto_format(u->fd, &ss) < 0 || !pa_sample_spec_equal(&ss, ss_original)) {
+ pa_log_warn("Resume failed, couldn't set original sample format settings.");
+ goto fail;
+ }
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
+ pa_log_warn("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ in_frag_size = out_frag_size = frag_size;
+ in_nfrags = out_nfrags = u->nfrags;
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
+ in_frag_size = info.fragsize;
+ in_nfrags = info.fragstotal;
+ }
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
+ out_frag_size = info.fragsize;
+ out_nfrags = info.fragstotal;
+ }
+
+ if ((u->source && (in_frag_size != (int) u->in_fragment_size || in_nfrags != (int) u->in_nfrags)) ||
+ (u->sink && (out_frag_size != (int) u->out_fragment_size || out_nfrags != (int) u->out_nfrags))) {
+ pa_log_warn("Resume failed, input fragment settings don't match.");
+ goto fail;
+ }
+
+ if (u->use_mmap) {
+ if (u->source) {
+ if ((u->in_mmap = mmap(NULL, u->in_hwbuf_size, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
+ pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
+ goto fail;
+ }
+ }
+
+ if (u->sink) {
+ if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
+ pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
+ if (u->in_mmap && u->in_mmap != MAP_FAILED) {
+ munmap(u->in_mmap, u->in_hwbuf_size);
+ u->in_mmap = NULL;
+ }
+
+ goto fail;
+ }
+
+ pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
+ }
+ }
+
+ u->out_mmap_current = u->in_mmap_current = 0;
+ u->out_mmap_saved_nfrags = u->in_mmap_saved_nfrags = 0;
+
+ pa_assert(!u->rtpoll_item);
+
+ build_pollfd(u);
+
+ if (u->sink && u->sink->get_volume)
+ u->sink->get_volume(u->sink);
+ if (u->source && u->source->get_volume)
+ u->source->get_volume(u->source);
+
+ pa_log_info("Resumed successfully...");
+
+ return 0;
+
+fail:
+ pa_close(u->fd);
+ u->fd = -1;
+ return -1;
+}
+
+/* Called from IO context */
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+ int ret;
+ pa_bool_t do_trigger = FALSE, quick = TRUE;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ pa_usec_t r = 0;
+
+ if (u->fd >= 0) {
+ if (u->use_mmap)
+ r = mmap_sink_get_latency(u);
+ else
+ r = io_sink_get_latency(u);
+ }
+
+ *((pa_usec_t*) data) = r;
+
+ return 0;
+ }
+
+ case PA_SINK_MESSAGE_SET_STATE:
+
+ switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
+
+ case PA_SINK_SUSPENDED:
+ pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
+
+ if (!u->source || u->source_suspended) {
+ if (suspend(u) < 0)
+ return -1;
+ }
+
+ do_trigger = TRUE;
+
+ u->sink_suspended = TRUE;
+ break;
+
+ case PA_SINK_IDLE:
+ case PA_SINK_RUNNING:
+
+ if (u->sink->thread_info.state == PA_SINK_INIT) {
+ do_trigger = TRUE;
+ quick = u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state);
+ }
+
+ if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
+
+ if (!u->source || u->source_suspended) {
+ if (unsuspend(u) < 0)
+ return -1;
+ quick = FALSE;
+ }
+
+ do_trigger = TRUE;
+
+ u->out_mmap_current = 0;
+ u->out_mmap_saved_nfrags = 0;
+
+ u->sink_suspended = FALSE;
+ }
+
+ break;
+
+ case PA_SINK_INVALID_STATE:
+ case PA_SINK_UNLINKED:
+ case PA_SINK_INIT:
+ ;
+ }
+
+ break;
+
+ }
+
+ ret = pa_sink_process_msg(o, code, data, offset, chunk);
+
+ if (do_trigger)
+ trigger(u, quick);
+
+ return ret;
+}
+
+static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SOURCE(o)->userdata;
+ int ret;
+ int do_trigger = FALSE, quick = TRUE;
+
+ switch (code) {
+
+ case PA_SOURCE_MESSAGE_GET_LATENCY: {
+ pa_usec_t r = 0;
+
+ if (u->fd >= 0) {
+ if (u->use_mmap)
+ r = mmap_source_get_latency(u);
+ else
+ r = io_source_get_latency(u);
+ }
+
+ *((pa_usec_t*) data) = r;
+ return 0;
+ }
+
+ case PA_SOURCE_MESSAGE_SET_STATE:
+
+ switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
+ case PA_SOURCE_SUSPENDED:
+ pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
+
+ if (!u->sink || u->sink_suspended) {
+ if (suspend(u) < 0)
+ return -1;
+ }
+
+ do_trigger = TRUE;
+
+ u->source_suspended = TRUE;
+ break;
+
+ case PA_SOURCE_IDLE:
+ case PA_SOURCE_RUNNING:
+
+ if (u->source->thread_info.state == PA_SOURCE_INIT) {
+ do_trigger = TRUE;
+ quick = u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state);
+ }
+
+ if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
+
+ if (!u->sink || u->sink_suspended) {
+ if (unsuspend(u) < 0)
+ return -1;
+ quick = FALSE;
+ }
+
+ do_trigger = TRUE;
+
+ u->in_mmap_current = 0;
+ u->in_mmap_saved_nfrags = 0;
+
+ u->source_suspended = FALSE;
+ }
+ break;
+
+ case PA_SOURCE_UNLINKED:
+ case PA_SOURCE_INIT:
+ case PA_SOURCE_INVALID_STATE:
+ ;
+
+ }
+ break;
+
+ }
+
+ ret = pa_source_process_msg(o, code, data, offset, chunk);
+
+ if (do_trigger)
+ trigger(u, quick);
+
+ return ret;
+}
+
+static void sink_get_volume(pa_sink *s) {
+ struct userdata *u;
+
+ pa_assert_se(u = s->userdata);
+
+ pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
+
+ if (u->mixer_devmask & SOUND_MASK_VOLUME)
+ if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_VOLUME, &s->sample_spec, &s->real_volume) >= 0)
+ return;
+
+ if (u->mixer_devmask & SOUND_MASK_PCM)
+ if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_PCM, &s->sample_spec, &s->real_volume) >= 0)
+ return;
+
+ pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
+}
+
+static void sink_set_volume(pa_sink *s) {
+ struct userdata *u;
+
+ pa_assert_se(u = s->userdata);
+
+ pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
+
+ if (u->mixer_devmask & SOUND_MASK_VOLUME)
+ if (pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_VOLUME, &s->sample_spec, &s->real_volume) >= 0)
+ return;
+
+ if (u->mixer_devmask & SOUND_MASK_PCM)
+ if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_PCM, &s->sample_spec, &s->real_volume) >= 0)
+ return;
+
+ pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
+}
+
+static void source_get_volume(pa_source *s) {
+ struct userdata *u;
+
+ pa_assert_se(u = s->userdata);
+
+ pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
+
+ if (u->mixer_devmask & SOUND_MASK_IGAIN)
+ if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_IGAIN, &s->sample_spec, &s->real_volume) >= 0)
+ return;
+
+ if (u->mixer_devmask & SOUND_MASK_RECLEV)
+ if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_RECLEV, &s->sample_spec, &s->real_volume) >= 0)
+ return;
+
+ pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
+}
+
+static void source_set_volume(pa_source *s) {
+ struct userdata *u;
+
+ pa_assert_se(u = s->userdata);
+
+ pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
+
+ if (u->mixer_devmask & SOUND_MASK_IGAIN)
+ if (pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_IGAIN, &s->sample_spec, &s->real_volume) >= 0)
+ return;
+
+ if (u->mixer_devmask & SOUND_MASK_RECLEV)
+ if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_RECLEV, &s->sample_spec, &s->real_volume) >= 0)
+ return;
+
+ pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+ int write_type = 0, read_type = 0;
+ short revents = 0;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ if (u->core->realtime_scheduling)
+ pa_make_realtime(u->core->realtime_priority);
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ for (;;) {
+ int ret;
+
+/* pa_log("loop"); */
+
+ if (u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state))
+ if (u->sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(u->sink, 0);
+
+ /* Render some data and write it to the dsp */
+
+ if (u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state) && ((revents & POLLOUT) || u->use_mmap || u->use_getospace)) {
+
+ if (u->use_mmap) {
+
+ if ((ret = mmap_write(u)) < 0)
+ goto fail;
+
+ revents &= ~POLLOUT;
+
+ if (ret > 0)
+ continue;
+
+ } else {
+ ssize_t l;
+ pa_bool_t loop = FALSE, work_done = FALSE;
+
+ l = (ssize_t) u->out_fragment_size;
+
+ if (u->use_getospace) {
+ audio_buf_info info;
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
+ pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
+ u->use_getospace = FALSE;
+ } else {
+ l = info.bytes;
+
+ /* We loop only if GETOSPACE worked and we
+ * actually *know* that we can write more than
+ * one fragment at a time */
+ loop = TRUE;
+ }
+ }
+
+ /* Round down to multiples of the fragment size,
+ * because OSS needs that (at least some versions
+ * do) */
+ l = (l/(ssize_t) u->out_fragment_size) * (ssize_t) u->out_fragment_size;
+
+ /* Hmm, so poll() signalled us that we can read
+ * something, but GETOSPACE told us there was nothing?
+ * Hmm, make the best of it, try to read some data, to
+ * avoid spinning forever. */
+ if (l <= 0 && (revents & POLLOUT)) {
+ l = (ssize_t) u->out_fragment_size;
+ loop = FALSE;
+ }
+
+ while (l > 0) {
+ void *p;
+ ssize_t t;
+
+ if (u->memchunk.length <= 0)
+ pa_sink_render(u->sink, (size_t) l, &u->memchunk);
+
+ pa_assert(u->memchunk.length > 0);
+
+ p = pa_memblock_acquire(u->memchunk.memblock);
+ t = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
+ pa_memblock_release(u->memchunk.memblock);
+
+/* pa_log("wrote %i bytes of %u", t, l); */
+
+ pa_assert(t != 0);
+
+ if (t < 0) {
+
+ if (errno == EINTR)
+ continue;
+
+ else if (errno == EAGAIN) {
+ pa_log_debug("EAGAIN");
+
+ revents &= ~POLLOUT;
+ break;
+
+ } else {
+ pa_log("Failed to write data to DSP: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ } else {
+
+ u->memchunk.index += (size_t) t;
+ u->memchunk.length -= (size_t) t;
+
+ if (u->memchunk.length <= 0) {
+ pa_memblock_unref(u->memchunk.memblock);
+ pa_memchunk_reset(&u->memchunk);
+ }
+
+ l -= t;
+
+ revents &= ~POLLOUT;
+ work_done = TRUE;
+ }
+
+ if (!loop)
+ break;
+ }
+
+ if (work_done)
+ continue;
+ }
+ }
+
+ /* Try to read some data and pass it on to the source driver. */
+
+ if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state) && ((revents & POLLIN) || u->use_mmap || u->use_getispace)) {
+
+ if (u->use_mmap) {
+
+ if ((ret = mmap_read(u)) < 0)
+ goto fail;
+
+ revents &= ~POLLIN;
+
+ if (ret > 0)
+ continue;
+
+ } else {
+
+ void *p;
+ ssize_t l;
+ pa_memchunk memchunk;
+ pa_bool_t loop = FALSE, work_done = FALSE;
+
+ l = (ssize_t) u->in_fragment_size;
+
+ if (u->use_getispace) {
+ audio_buf_info info;
+
+ if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
+ pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
+ u->use_getispace = FALSE;
+ } else {
+ l = info.bytes;
+ loop = TRUE;
+ }
+ }
+
+ l = (l/(ssize_t) u->in_fragment_size) * (ssize_t) u->in_fragment_size;
+
+ if (l <= 0 && (revents & POLLIN)) {
+ l = (ssize_t) u->in_fragment_size;
+ loop = FALSE;
+ }
+
+ while (l > 0) {
+ ssize_t t;
+ size_t k;
+
+ pa_assert(l > 0);
+
+ memchunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
+
+ k = pa_memblock_get_length(memchunk.memblock);
+
+ if (k > (size_t) l)
+ k = (size_t) l;
+
+ k = (k/u->frame_size)*u->frame_size;
+
+ p = pa_memblock_acquire(memchunk.memblock);
+ t = pa_read(u->fd, p, k, &read_type);
+ pa_memblock_release(memchunk.memblock);
+
+ pa_assert(t != 0); /* EOF cannot happen */
+
+/* pa_log("read %i bytes of %u", t, l); */
+
+ if (t < 0) {
+ pa_memblock_unref(memchunk.memblock);
+
+ if (errno == EINTR)
+ continue;
+
+ else if (errno == EAGAIN) {
+ pa_log_debug("EAGAIN");
+
+ revents &= ~POLLIN;
+ break;
+
+ } else {
+ pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ } else {
+ memchunk.index = 0;
+ memchunk.length = (size_t) t;
+
+ pa_source_post(u->source, &memchunk);
+ pa_memblock_unref(memchunk.memblock);
+
+ l -= t;
+
+ revents &= ~POLLIN;
+ work_done = TRUE;
+ }
+
+ if (!loop)
+ break;
+ }
+
+ if (work_done)
+ continue;
+ }
+ }
+
+/* pa_log("loop2 revents=%i", revents); */
+
+ if (u->rtpoll_item) {
+ struct pollfd *pollfd;
+
+ pa_assert(u->fd >= 0);
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->events = (short)
+ (((u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state)) ? POLLIN : 0) |
+ ((u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state)) ? POLLOUT : 0));
+ }
+
+ /* Hmm, nothing to do. Let's sleep */
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+
+ if (u->rtpoll_item) {
+ struct pollfd *pollfd;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ if (pollfd->revents & ~(POLLOUT|POLLIN)) {
+ pa_log("DSP shutdown.");
+ goto fail;
+ }
+
+ revents = pollfd->revents;
+ } else
+ revents = 0;
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+}
+
+int pa__init(pa_module*m) {
+
+ struct audio_buf_info info;
+ struct userdata *u = NULL;
+ const char *dev;
+ int fd = -1;
+ int nfrags, orig_frag_size, frag_size;
+ int mode, caps;
+ pa_bool_t record = TRUE, playback = TRUE, use_mmap = TRUE;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ pa_modargs *ma = NULL;
+ char hwdesc[64];
+ const char *name;
+ pa_bool_t namereg_fail;
+ pa_sink_new_data sink_new_data;
+ pa_source_new_data source_new_data;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
+ pa_log("record= and playback= expect boolean argument.");
+ goto fail;
+ }
+
+ if (!playback && !record) {
+ pa_log("Neither playback nor record enabled for device.");
+ goto fail;
+ }
+
+ mode = (playback && record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
+
+ ss = m->core->default_sample_spec;
+ map = m->core->default_channel_map;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
+ pa_log("Failed to parse sample specification or channel map");
+ goto fail;
+ }
+
+ nfrags = (int) m->core->default_n_fragments;
+ frag_size = (int) pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
+ if (frag_size <= 0)
+ frag_size = (int) pa_frame_size(&ss);
+
+ if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
+ pa_log("Failed to parse fragments arguments");
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
+ pa_log("Failed to parse mmap argument.");
+ goto fail;
+ }
+
+ if ((fd = pa_oss_open(dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
+ goto fail;
+
+ if (use_mmap && (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_TRIGGER))) {
+ pa_log_info("OSS device not mmap capable, falling back to UNIX read/write mode.");
+ use_mmap = FALSE;
+ }
+
+ if (use_mmap && mode == O_WRONLY) {
+ pa_log_info("Device opened for playback only, cannot do memory mapping, falling back to UNIX write() mode.");
+ use_mmap = FALSE;
+ }
+
+ if (pa_oss_get_hw_description(dev, hwdesc, sizeof(hwdesc)) >= 0)
+ pa_log_info("Hardware name is '%s'.", hwdesc);
+ else
+ hwdesc[0] = 0;
+
+ pa_log_info("Device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
+
+ orig_frag_size = frag_size;
+ if (nfrags >= 2 && frag_size >= 1)
+ if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
+ goto fail;
+
+ if (pa_oss_auto_format(fd, &ss) < 0)
+ goto fail;
+
+ if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
+ pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+ pa_assert(frag_size > 0);
+
+ u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ m->userdata = u;
+ u->fd = fd;
+ u->mixer_fd = -1;
+ u->mixer_devmask = 0;
+ u->use_getospace = u->use_getispace = TRUE;
+ u->use_getodelay = TRUE;
+ u->mode = mode;
+ u->frame_size = pa_frame_size(&ss);
+ u->device_name = pa_xstrdup(dev);
+ u->in_nfrags = u->out_nfrags = (uint32_t) (u->nfrags = nfrags);
+ u->out_fragment_size = u->in_fragment_size = (uint32_t) (u->frag_size = frag_size);
+ u->orig_frag_size = orig_frag_size;
+ u->use_mmap = use_mmap;
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+ u->rtpoll_item = NULL;
+ build_pollfd(u);
+
+ if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
+ pa_log_info("Input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
+ u->in_fragment_size = (uint32_t) info.fragsize;
+ u->in_nfrags = (uint32_t) info.fragstotal;
+ u->use_getispace = TRUE;
+ }
+
+ if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
+ pa_log_info("Output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
+ u->out_fragment_size = (uint32_t) info.fragsize;
+ u->out_nfrags = (uint32_t) info.fragstotal;
+ u->use_getospace = TRUE;
+ }
+
+ u->in_hwbuf_size = u->in_nfrags * u->in_fragment_size;
+ u->out_hwbuf_size = u->out_nfrags * u->out_fragment_size;
+
+ if (mode != O_WRONLY) {
+ char *name_buf = NULL;
+
+ if (use_mmap) {
+ if ((u->in_mmap = mmap(NULL, u->in_hwbuf_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
+ pa_log_warn("mmap(PROT_READ) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno));
+ use_mmap = u->use_mmap = FALSE;
+ u->in_mmap = NULL;
+ } else
+ pa_log_debug("Successfully mmap()ed input buffer.");
+ }
+
+ if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
+ namereg_fail = TRUE;
+ else {
+ name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(dev));
+ namereg_fail = FALSE;
+ }
+
+ pa_source_new_data_init(&source_new_data);
+ source_new_data.driver = __FILE__;
+ source_new_data.module = m;
+ pa_source_new_data_set_name(&source_new_data, name);
+ source_new_data.namereg_fail = namereg_fail;
+ pa_source_new_data_set_sample_spec(&source_new_data, &ss);
+ pa_source_new_data_set_channel_map(&source_new_data, &map);
+ pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_STRING, dev);
+ pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_API, "oss");
+ pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, hwdesc[0] ? hwdesc : dev);
+ pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, use_mmap ? "mmap" : "serial");
+ pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (u->in_hwbuf_size));
+ pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (u->in_fragment_size));
+
+ if (pa_modargs_get_proplist(ma, "source_properties", source_new_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_source_new_data_done(&source_new_data);
+ goto fail;
+ }
+
+ u->source = pa_source_new(m->core, &source_new_data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&source_new_data);
+ pa_xfree(name_buf);
+
+ if (!u->source) {
+ pa_log("Failed to create source object");
+ goto fail;
+ }
+
+ u->source->parent.process_msg = source_process_msg;
+ u->source->userdata = u;
+
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+ pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(u->in_hwbuf_size, &u->source->sample_spec));
+ u->source->refresh_volume = TRUE;
+
+ if (use_mmap)
+ u->in_mmap_memblocks = pa_xnew0(pa_memblock*, u->in_nfrags);
+ }
+
+ if (mode != O_RDONLY) {
+ char *name_buf = NULL;
+
+ if (use_mmap) {
+ if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
+ if (mode == O_RDWR) {
+ pa_log_debug("mmap() failed for input. Changing to O_WRONLY mode.");
+ mode = O_WRONLY;
+ goto go_on;
+ } else {
+ pa_log_warn("mmap(PROT_WRITE) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno));
+ u->use_mmap = use_mmap = FALSE;
+ u->out_mmap = NULL;
+ }
+ } else {
+ pa_log_debug("Successfully mmap()ed output buffer.");
+ pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
+ }
+ }
+
+ if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
+ namereg_fail = TRUE;
+ else {
+ name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(dev));
+ namereg_fail = FALSE;
+ }
+
+ pa_sink_new_data_init(&sink_new_data);
+ sink_new_data.driver = __FILE__;
+ sink_new_data.module = m;
+ pa_sink_new_data_set_name(&sink_new_data, name);
+ sink_new_data.namereg_fail = namereg_fail;
+ pa_sink_new_data_set_sample_spec(&sink_new_data, &ss);
+ pa_sink_new_data_set_channel_map(&sink_new_data, &map);
+ pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_STRING, dev);
+ pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_API, "oss");
+ pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, hwdesc[0] ? hwdesc : dev);
+ pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, use_mmap ? "mmap" : "serial");
+ pa_proplist_setf(sink_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (u->out_hwbuf_size));
+ pa_proplist_setf(sink_new_data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (u->out_fragment_size));
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", sink_new_data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&sink_new_data);
+ goto fail;
+ }
+
+ u->sink = pa_sink_new(m->core, &sink_new_data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
+ pa_sink_new_data_done(&sink_new_data);
+ pa_xfree(name_buf);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink object");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg;
+ u->sink->userdata = u;
+
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+ pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(u->out_hwbuf_size, &u->sink->sample_spec));
+ u->sink->refresh_volume = TRUE;
+
+ pa_sink_set_max_request(u->sink, u->out_hwbuf_size);
+
+ if (use_mmap)
+ u->out_mmap_memblocks = pa_xnew0(pa_memblock*, u->out_nfrags);
+ }
+
+ if ((u->mixer_fd = pa_oss_open_mixer_for_device(u->device_name)) >= 0) {
+ pa_bool_t do_close = TRUE;
+
+ if (ioctl(fd, SOUND_MIXER_READ_DEVMASK, &u->mixer_devmask) < 0)
+ pa_log_warn("SOUND_MIXER_READ_DEVMASK failed: %s", pa_cstrerror(errno));
+
+ else {
+ if (u->sink && (u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM))) {
+ pa_log_debug("Found hardware mixer track for playback.");
+ u->sink->flags |= PA_SINK_HW_VOLUME_CTRL;
+ u->sink->get_volume = sink_get_volume;
+ u->sink->set_volume = sink_set_volume;
+ u->sink->n_volume_steps = 101;
+ do_close = FALSE;
+ }
+
+ if (u->source && (u->mixer_devmask & (SOUND_MASK_RECLEV|SOUND_MASK_IGAIN))) {
+ pa_log_debug("Found hardware mixer track for recording.");
+ u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL;
+ u->source->get_volume = source_get_volume;
+ u->source->set_volume = source_set_volume;
+ u->source->n_volume_steps = 101;
+ do_close = FALSE;
+ }
+ }
+
+ if (do_close) {
+ pa_close(u->mixer_fd);
+ u->mixer_fd = -1;
+ u->mixer_devmask = 0;
+ }
+ }
+
+go_on:
+
+ pa_assert(u->source || u->sink);
+
+ pa_memchunk_reset(&u->memchunk);
+
+ if (!(u->thread = pa_thread_new("oss", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ /* Read mixer settings */
+ if (u->sink) {
+ if (sink_new_data.volume_is_set) {
+ if (u->sink->set_volume)
+ u->sink->set_volume(u->sink);
+ } else {
+ if (u->sink->get_volume)
+ u->sink->get_volume(u->sink);
+ }
+ }
+
+ if (u->source) {
+ if (source_new_data.volume_is_set) {
+ if (u->source->set_volume)
+ u->source->set_volume(u->source);
+ } else {
+ if (u->source->get_volume)
+ u->source->get_volume(u->source);
+ }
+ }
+
+ if (u->sink)
+ pa_sink_put(u->sink);
+ if (u->source)
+ pa_source_put(u->source);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+
+ if (u)
+ pa__done(m);
+ else if (fd >= 0)
+ pa_close(fd);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->source)
+ pa_source_unlink(u->source);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->source)
+ pa_source_unref(u->source);
+
+ if (u->memchunk.memblock)
+ pa_memblock_unref(u->memchunk.memblock);
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ if (u->out_mmap_memblocks) {
+ unsigned i;
+ for (i = 0; i < u->out_nfrags; i++)
+ if (u->out_mmap_memblocks[i])
+ pa_memblock_unref_fixed(u->out_mmap_memblocks[i]);
+ pa_xfree(u->out_mmap_memblocks);
+ }
+
+ if (u->in_mmap_memblocks) {
+ unsigned i;
+ for (i = 0; i < u->in_nfrags; i++)
+ if (u->in_mmap_memblocks[i])
+ pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
+ pa_xfree(u->in_mmap_memblocks);
+ }
+
+ if (u->in_mmap && u->in_mmap != MAP_FAILED)
+ munmap(u->in_mmap, u->in_hwbuf_size);
+
+ if (u->out_mmap && u->out_mmap != MAP_FAILED)
+ munmap(u->out_mmap, u->out_hwbuf_size);
+
+ if (u->fd >= 0)
+ pa_close(u->fd);
+
+ if (u->mixer_fd >= 0)
+ pa_close(u->mixer_fd);
+
+ pa_xfree(u->device_name);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/oss/oss-util.c b/src/modules/oss/oss-util.c
new file mode 100644
index 00000000..04899afe
--- /dev/null
+++ b/src/modules/oss/oss-util.c
@@ -0,0 +1,429 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/soundcard.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <fcntl.h>
+
+#include <pulse/xmalloc.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+
+#include "oss-util.h"
+
+int pa_oss_open(const char *device, int *mode, int* pcaps) {
+ int fd = -1;
+ int caps;
+ char *t;
+
+ pa_assert(device);
+ pa_assert(mode);
+ pa_assert(*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY);
+
+ if(!pcaps)
+ pcaps = &caps;
+
+ if (*mode == O_RDWR) {
+ if ((fd = pa_open_cloexec(device, O_RDWR|O_NDELAY, 0)) >= 0) {
+ ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
+
+ if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
+ pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ if (*pcaps & DSP_CAP_DUPLEX)
+ goto success;
+
+ pa_log_warn("'%s' doesn't support full duplex", device);
+
+ pa_close(fd);
+ }
+
+ if ((fd = pa_open_cloexec(device, (*mode = O_WRONLY)|O_NDELAY, 0)) < 0) {
+ if ((fd = pa_open_cloexec(device, (*mode = O_RDONLY)|O_NDELAY, 0)) < 0) {
+ pa_log("open('%s'): %s", device, pa_cstrerror(errno));
+ goto fail;
+ }
+ }
+ } else {
+ if ((fd = pa_open_cloexec(device, *mode|O_NDELAY, 0)) < 0) {
+ pa_log("open('%s'): %s", device, pa_cstrerror(errno));
+ goto fail;
+ }
+ }
+
+ *pcaps = 0;
+
+ if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
+ pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+success:
+
+ t = pa_sprintf_malloc(
+ "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
+ *pcaps & DSP_CAP_BATCH ? " BATCH" : "",
+#ifdef DSP_CAP_BIND
+ *pcaps & DSP_CAP_BIND ? " BIND" : "",
+#else
+ "",
+#endif
+ *pcaps & DSP_CAP_COPROC ? " COPROC" : "",
+ *pcaps & DSP_CAP_DUPLEX ? " DUPLEX" : "",
+#ifdef DSP_CAP_FREERATE
+ *pcaps & DSP_CAP_FREERATE ? " FREERATE" : "",
+#else
+ "",
+#endif
+#ifdef DSP_CAP_INPUT
+ *pcaps & DSP_CAP_INPUT ? " INPUT" : "",
+#else
+ "",
+#endif
+ *pcaps & DSP_CAP_MMAP ? " MMAP" : "",
+#ifdef DSP_CAP_MODEM
+ *pcaps & DSP_CAP_MODEM ? " MODEM" : "",
+#else
+ "",
+#endif
+#ifdef DSP_CAP_MULTI
+ *pcaps & DSP_CAP_MULTI ? " MULTI" : "",
+#else
+ "",
+#endif
+#ifdef DSP_CAP_OUTPUT
+ *pcaps & DSP_CAP_OUTPUT ? " OUTPUT" : "",
+#else
+ "",
+#endif
+ *pcaps & DSP_CAP_REALTIME ? " REALTIME" : "",
+#ifdef DSP_CAP_SHADOW
+ *pcaps & DSP_CAP_SHADOW ? " SHADOW" : "",
+#else
+ "",
+#endif
+#ifdef DSP_CAP_VIRTUAL
+ *pcaps & DSP_CAP_VIRTUAL ? " VIRTUAL" : "",
+#else
+ "",
+#endif
+ *pcaps & DSP_CAP_TRIGGER ? " TRIGGER" : "");
+
+ pa_log_debug("capabilities:%s", t);
+ pa_xfree(t);
+
+ return fd;
+
+fail:
+ if (fd >= 0)
+ pa_close(fd);
+ return -1;
+}
+
+int pa_oss_auto_format(int fd, pa_sample_spec *ss) {
+ int format, channels, speed, reqformat;
+ pa_sample_format_t orig_format;
+
+ static const int format_trans[PA_SAMPLE_MAX] = {
+ [PA_SAMPLE_U8] = AFMT_U8,
+ [PA_SAMPLE_ALAW] = AFMT_A_LAW,
+ [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
+ [PA_SAMPLE_S16LE] = AFMT_S16_LE,
+ [PA_SAMPLE_S16BE] = AFMT_S16_BE,
+ [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
+ [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
+ [PA_SAMPLE_S32LE] = AFMT_QUERY, /* not supported */
+ [PA_SAMPLE_S32BE] = AFMT_QUERY, /* not supported */
+ [PA_SAMPLE_S24LE] = AFMT_QUERY, /* not supported */
+ [PA_SAMPLE_S24BE] = AFMT_QUERY, /* not supported */
+ [PA_SAMPLE_S24_32LE] = AFMT_QUERY, /* not supported */
+ [PA_SAMPLE_S24_32BE] = AFMT_QUERY, /* not supported */
+ };
+
+ pa_assert(fd >= 0);
+ pa_assert(ss);
+
+ orig_format = ss->format;
+
+ reqformat = format = format_trans[ss->format];
+ if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
+ format = AFMT_S16_NE;
+ if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
+ int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
+ format = f;
+ if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
+ format = AFMT_U8;
+ if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
+ pa_log("SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : pa_cstrerror(errno));
+ return -1;
+ } else
+ ss->format = PA_SAMPLE_U8;
+ } else
+ ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
+ } else
+ ss->format = PA_SAMPLE_S16NE;
+ }
+
+ if (orig_format != ss->format)
+ pa_log_warn("device doesn't support sample format %s, changed to %s.",
+ pa_sample_format_to_string(orig_format),
+ pa_sample_format_to_string(ss->format));
+
+ channels = ss->channels;
+ if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
+ pa_log("SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno));
+ return -1;
+ }
+ pa_assert(channels > 0);
+
+ if (ss->channels != channels) {
+ pa_log_warn("device doesn't support %i channels, using %i channels.", ss->channels, channels);
+ ss->channels = (uint8_t) channels;
+ }
+
+ speed = (int) ss->rate;
+ if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
+ pa_log("SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno));
+ return -1;
+ }
+ pa_assert(speed > 0);
+
+ if (ss->rate != (unsigned) speed) {
+ pa_log_warn("device doesn't support %i Hz, changed to %i Hz.", ss->rate, speed);
+
+ /* If the sample rate deviates too much, we need to resample */
+ if (speed < ss->rate*.95 || speed > ss->rate*1.05)
+ ss->rate = (uint32_t) speed;
+ }
+
+ return 0;
+}
+
+static int simple_log2(int v) {
+ int k = 0;
+
+ for (;;) {
+ v >>= 1;
+ if (!v) break;
+ k++;
+ }
+
+ return k;
+}
+
+int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
+ int arg;
+ arg = ((int) nfrags << 16) | simple_log2(frag_size);
+
+ pa_log_debug("Asking for %i fragments of size %i (requested %i)", nfrags, 1 << simple_log2(frag_size), frag_size);
+
+ if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
+ pa_log("SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
+int pa_oss_get_volume(int fd, unsigned long mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
+ char cv[PA_CVOLUME_SNPRINT_MAX];
+ unsigned vol;
+
+ pa_assert(fd >= 0);
+ pa_assert(ss);
+ pa_assert(volume);
+
+ if (ioctl(fd, mixer, &vol) < 0)
+ return -1;
+
+ pa_cvolume_reset(volume, ss->channels);
+
+ volume->values[0] = PA_CLAMP_VOLUME(((vol & 0xFF) * PA_VOLUME_NORM) / 100);
+
+ if (volume->channels >= 2)
+ volume->values[1] = PA_CLAMP_VOLUME((((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100);
+
+ pa_log_debug("Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
+ return 0;
+}
+
+int pa_oss_set_volume(int fd, unsigned long mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
+ char cv[PA_CVOLUME_SNPRINT_MAX];
+ unsigned vol;
+ pa_volume_t l, r;
+
+ l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
+
+ vol = (l*100)/PA_VOLUME_NORM;
+
+ if (ss->channels >= 2) {
+ r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
+ vol |= ((r*100)/PA_VOLUME_NORM) << 8;
+ }
+
+ if (ioctl(fd, mixer, &vol) < 0)
+ return -1;
+
+ pa_log_debug("Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
+ return 0;
+}
+
+static int get_device_number(const char *dev) {
+ const char *p, *e;
+ char *rp = NULL;
+ int r;
+
+ if (!(p = rp = pa_readlink(dev))) {
+#ifdef ENOLINK
+ if (errno != EINVAL && errno != ENOLINK) {
+#else
+ if (errno != EINVAL) {
+#endif
+ r = -1;
+ goto finish;
+ }
+
+ p = dev;
+ }
+
+ if ((e = strrchr(p, '/')))
+ p = e+1;
+
+ if (p == 0) {
+ r = 0;
+ goto finish;
+ }
+
+ p = strchr(p, 0) -1;
+
+ if (*p >= '0' && *p <= '9') {
+ r = *p - '0';
+ goto finish;
+ }
+
+ r = -1;
+
+finish:
+ pa_xfree(rp);
+ return r;
+}
+
+int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
+ FILE *f;
+ int n, r = -1;
+ int b = 0;
+
+ if ((n = get_device_number(dev)) < 0)
+ return -1;
+
+ if (!(f = pa_fopen_cloexec("/dev/sndstat", "r")) &&
+ !(f = pa_fopen_cloexec("/proc/sndstat", "r")) &&
+ !(f = pa_fopen_cloexec("/proc/asound/oss/sndstat", "r"))) {
+
+ if (errno != ENOENT)
+ pa_log_warn("failed to open OSS sndstat device: %s", pa_cstrerror(errno));
+
+ return -1;
+ }
+
+ while (!feof(f)) {
+ char line[64];
+ int device;
+
+ if (!fgets(line, sizeof(line), f))
+ break;
+
+ line[strcspn(line, "\r\n")] = 0;
+
+ if (!b) {
+ b = strcmp(line, "Audio devices:") == 0;
+ continue;
+ }
+
+ if (line[0] == 0)
+ break;
+
+ if (sscanf(line, "%i: ", &device) != 1)
+ continue;
+
+ if (device == n) {
+ char *k = strchr(line, ':');
+ pa_assert(k);
+ k++;
+ k += strspn(k, " ");
+
+ if (pa_endswith(k, " (DUPLEX)"))
+ k[strlen(k)-9] = 0;
+
+ pa_strlcpy(name, k, l);
+ r = 0;
+ break;
+ }
+ }
+
+ fclose(f);
+ return r;
+}
+
+static int open_mixer(const char *mixer) {
+ int fd;
+
+ if ((fd = pa_open_cloexec(mixer, O_RDWR|O_NDELAY, 0)) >= 0)
+ return fd;
+
+ return -1;
+}
+
+int pa_oss_open_mixer_for_device(const char *device) {
+ int n;
+ char *fn;
+ int fd;
+
+ if ((n = get_device_number(device)) < 0)
+ return -1;
+
+ if (n == 0)
+ if ((fd = open_mixer("/dev/mixer")) >= 0)
+ return fd;
+
+ fn = pa_sprintf_malloc("/dev/mixer%i", n);
+ fd = open_mixer(fn);
+ pa_xfree(fn);
+
+ if (fd < 0)
+ pa_log_warn("Failed to open mixer '%s': %s", device, pa_cstrerror(errno));
+
+ return fd;
+}
diff --git a/src/modules/oss-util.h b/src/modules/oss/oss-util.h
index 12855f4e..845b0c8f 100644
--- a/src/modules/oss-util.h
+++ b/src/modules/oss/oss-util.h
@@ -1,21 +1,22 @@
#ifndef fooossutilhfoo
#define fooossutilhfoo
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -30,12 +31,11 @@ int pa_oss_auto_format(int fd, pa_sample_spec *ss);
int pa_oss_set_fragments(int fd, int frags, int frag_size);
-int pa_oss_get_pcm_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume);
-int pa_oss_set_pcm_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume);
-
-int pa_oss_get_input_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume);
-int pa_oss_set_input_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume);
+int pa_oss_set_volume(int fd, unsigned long mixer, const pa_sample_spec *ss, const pa_cvolume *volume);
+int pa_oss_get_volume(int fd, unsigned long mixer, const pa_sample_spec *ss, pa_cvolume *volume);
int pa_oss_get_hw_description(const char *dev, char *name, size_t l);
+int pa_oss_open_mixer_for_device(const char *device);
+
#endif
diff --git a/src/modules/raop/base64.c b/src/modules/raop/base64.c
new file mode 100644
index 00000000..37e47628
--- /dev/null
+++ b/src/modules/raop/base64.c
@@ -0,0 +1,126 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/*
+ This file was originally inspired by a file developed by
+ Kungliga Tekniska H�gskolan
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <pulse/xmalloc.h>
+
+#include "base64.h"
+
+static const char base64_chars[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+static int pos(char c) {
+ if (c >= 'A' && c <= 'Z') return c - 'A' + 0;
+ if (c >= 'a' && c <= 'z') return c - 'a' + 26;
+ if (c >= '0' && c <= '9') return c - '0' + 52;
+ if (c == '+') return 62;
+ if (c == '/') return 63;
+ return -1;
+}
+
+int pa_base64_encode(const void *data, int size, char **str) {
+ char *s, *p;
+ int i;
+ int c;
+ const unsigned char *q;
+
+ p = s = pa_xnew(char, size * 4 / 3 + 4);
+ q = (const unsigned char *) data;
+ for (i = 0; i < size;) {
+ c = q[i++];
+ c *= 256;
+ if (i < size)
+ c += q[i];
+ i++;
+ c *= 256;
+ if (i < size)
+ c += q[i];
+ i++;
+ p[0] = base64_chars[(c & 0x00fc0000) >> 18];
+ p[1] = base64_chars[(c & 0x0003f000) >> 12];
+ p[2] = base64_chars[(c & 0x00000fc0) >> 6];
+ p[3] = base64_chars[(c & 0x0000003f) >> 0];
+ if (i > size)
+ p[3] = '=';
+ if (i > size + 1)
+ p[2] = '=';
+ p += 4;
+ }
+ *p = 0;
+ *str = s;
+ return strlen(s);
+}
+
+#define DECODE_ERROR 0xffffffff
+
+static unsigned int token_decode(const char *token) {
+ int i;
+ unsigned int val = 0;
+ int marker = 0;
+ if (strlen(token) < 4)
+ return DECODE_ERROR;
+ for (i = 0; i < 4; i++) {
+ val *= 64;
+ if (token[i] == '=')
+ marker++;
+ else if (marker > 0)
+ return DECODE_ERROR;
+ else {
+ int lpos = pos(token[i]);
+ if (lpos < 0)
+ return DECODE_ERROR;
+ val += lpos;
+ }
+ }
+ if (marker > 2)
+ return DECODE_ERROR;
+ return (marker << 24) | val;
+}
+
+int pa_base64_decode(const char *str, void *data) {
+ const char *p;
+ unsigned char *q;
+
+ q = data;
+ for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
+ unsigned int val = token_decode(p);
+ unsigned int marker = (val >> 24) & 0xff;
+ if (val == DECODE_ERROR)
+ return -1;
+ *q++ = (val >> 16) & 0xff;
+ if (marker < 2)
+ *q++ = (val >> 8) & 0xff;
+ if (marker < 1)
+ *q++ = val & 0xff;
+ }
+ return q - (unsigned char *) data;
+}
diff --git a/src/modules/raop/base64.h b/src/modules/raop/base64.h
new file mode 100644
index 00000000..7a973b68
--- /dev/null
+++ b/src/modules/raop/base64.h
@@ -0,0 +1,34 @@
+#ifndef foobase64hfoo
+#define foobase64hfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Colin Guthrie
+ Copyright Kungliga Tekniska Høgskolan
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/*
+ This file was originally inspired by a file developed by
+ Kungliga Tekniska Høgskolan
+*/
+
+int pa_base64_encode(const void *data, int size, char **str);
+int pa_base64_decode(const char *str, void *data);
+
+#endif
diff --git a/src/modules/raop/module-raop-discover.c b/src/modules/raop/module-raop-discover.c
new file mode 100644
index 00000000..de1a2b1c
--- /dev/null
+++ b/src/modules/raop/module-raop-discover.c
@@ -0,0 +1,392 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2008 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <avahi-client/client.h>
+#include <avahi-client/lookup.h>
+#include <avahi-common/alternative.h>
+#include <avahi-common/error.h>
+#include <avahi-common/domain.h>
+#include <avahi-common/malloc.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/hashmap.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/avahi-wrap.h>
+
+#include "module-raop-discover-symdef.h"
+
+PA_MODULE_AUTHOR("Colin Guthrie");
+PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Discovery of RAOP devices");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+#define SERVICE_TYPE_SINK "_raop._tcp"
+
+static const char* const valid_modargs[] = {
+ NULL
+};
+
+struct tunnel {
+ AvahiIfIndex interface;
+ AvahiProtocol protocol;
+ char *name, *type, *domain;
+ uint32_t module_index;
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ AvahiPoll *avahi_poll;
+ AvahiClient *client;
+ AvahiServiceBrowser *sink_browser;
+
+ pa_hashmap *tunnels;
+};
+
+static unsigned tunnel_hash(const void *p) {
+ const struct tunnel *t = p;
+
+ return
+ (unsigned) t->interface +
+ (unsigned) t->protocol +
+ pa_idxset_string_hash_func(t->name) +
+ pa_idxset_string_hash_func(t->type) +
+ pa_idxset_string_hash_func(t->domain);
+}
+
+static int tunnel_compare(const void *a, const void *b) {
+ const struct tunnel *ta = a, *tb = b;
+ int r;
+
+ if (ta->interface != tb->interface)
+ return 1;
+ if (ta->protocol != tb->protocol)
+ return 1;
+ if ((r = strcmp(ta->name, tb->name)))
+ return r;
+ if ((r = strcmp(ta->type, tb->type)))
+ return r;
+ if ((r = strcmp(ta->domain, tb->domain)))
+ return r;
+
+ return 0;
+}
+
+static struct tunnel *tunnel_new(
+ AvahiIfIndex interface, AvahiProtocol protocol,
+ const char *name, const char *type, const char *domain) {
+
+ struct tunnel *t;
+ t = pa_xnew(struct tunnel, 1);
+ t->interface = interface;
+ t->protocol = protocol;
+ t->name = pa_xstrdup(name);
+ t->type = pa_xstrdup(type);
+ t->domain = pa_xstrdup(domain);
+ t->module_index = PA_IDXSET_INVALID;
+ return t;
+}
+
+static void tunnel_free(struct tunnel *t) {
+ pa_assert(t);
+ pa_xfree(t->name);
+ pa_xfree(t->type);
+ pa_xfree(t->domain);
+ pa_xfree(t);
+}
+
+static void resolver_cb(
+ AvahiServiceResolver *r,
+ AvahiIfIndex interface, AvahiProtocol protocol,
+ AvahiResolverEvent event,
+ const char *name, const char *type, const char *domain,
+ const char *host_name, const AvahiAddress *a, uint16_t port,
+ AvahiStringList *txt,
+ AvahiLookupResultFlags flags,
+ void *userdata) {
+
+ struct userdata *u = userdata;
+ struct tunnel *tnl;
+
+ pa_assert(u);
+
+ tnl = tunnel_new(interface, protocol, name, type, domain);
+
+ if (event != AVAHI_RESOLVER_FOUND)
+ pa_log("Resolving of '%s' failed: %s", name, avahi_strerror(avahi_client_errno(u->client)));
+ else {
+ char *device = NULL, *nicename, *dname, *vname, *args;
+ char at[AVAHI_ADDRESS_STR_MAX];
+ AvahiStringList *l;
+ pa_module *m;
+
+ if ((nicename = strstr(name, "@"))) {
+ ++nicename;
+ if (strlen(nicename) > 0) {
+ pa_log_debug("Found RAOP: %s", nicename);
+ }
+ }
+
+ for (l = txt; l; l = l->next) {
+ char *key, *value;
+ pa_assert_se(avahi_string_list_get_pair(l, &key, &value, NULL) == 0);
+
+ pa_log_debug("Found key: '%s' with value: '%s'", key, value);
+ if (strcmp(key, "device") == 0) {
+ pa_xfree(device);
+ device = value;
+ value = NULL;
+ }
+ avahi_free(key);
+ avahi_free(value);
+ }
+
+ if (device)
+ dname = pa_sprintf_malloc("raop.%s.%s", host_name, device);
+ else
+ dname = pa_sprintf_malloc("raop.%s", host_name);
+
+ if (!(vname = pa_namereg_make_valid_name(dname))) {
+ pa_log("Cannot construct valid device name from '%s'.", dname);
+ avahi_free(device);
+ pa_xfree(dname);
+ goto finish;
+ }
+ pa_xfree(dname);
+
+ /*
+ TODO: allow this syntax of server name in things....
+ args = pa_sprintf_malloc("server=[%s]:%u "
+ "sink_name=%s",
+ avahi_address_snprint(at, sizeof(at), a), port,
+ vname);*/
+ if (nicename) {
+ args = pa_sprintf_malloc("server=%s "
+ "sink_name=%s "
+ "sink_properties=device.description=\"%s\"",
+ avahi_address_snprint(at, sizeof(at), a),
+ vname,
+ nicename);
+
+ } else {
+ args = pa_sprintf_malloc("server=%s "
+ "sink_name=%s",
+ avahi_address_snprint(at, sizeof(at), a),
+ vname);
+ }
+
+ pa_log_debug("Loading module-raop-sink with arguments '%s'", args);
+
+ if ((m = pa_module_load(u->core, "module-raop-sink", args))) {
+ tnl->module_index = m->index;
+ pa_hashmap_put(u->tunnels, tnl, tnl);
+ tnl = NULL;
+ }
+
+ pa_xfree(vname);
+ pa_xfree(args);
+ avahi_free(device);
+ }
+
+finish:
+
+ avahi_service_resolver_free(r);
+
+ if (tnl)
+ tunnel_free(tnl);
+}
+
+static void browser_cb(
+ AvahiServiceBrowser *b,
+ AvahiIfIndex interface, AvahiProtocol protocol,
+ AvahiBrowserEvent event,
+ const char *name, const char *type, const char *domain,
+ AvahiLookupResultFlags flags,
+ void *userdata) {
+
+ struct userdata *u = userdata;
+ struct tunnel *t;
+
+ pa_assert(u);
+
+ if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
+ return;
+
+ t = tunnel_new(interface, protocol, name, type, domain);
+
+ if (event == AVAHI_BROWSER_NEW) {
+
+ if (!pa_hashmap_get(u->tunnels, t))
+ if (!(avahi_service_resolver_new(u->client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolver_cb, u)))
+ pa_log("avahi_service_resolver_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+
+ /* We ignore the returned resolver object here, since the we don't
+ * need to attach any special data to it, and we can still destroy
+ * it from the callback */
+
+ } else if (event == AVAHI_BROWSER_REMOVE) {
+ struct tunnel *t2;
+
+ if ((t2 = pa_hashmap_get(u->tunnels, t))) {
+ pa_module_unload_request_by_index(u->core, t2->module_index, TRUE);
+ pa_hashmap_remove(u->tunnels, t2);
+ tunnel_free(t2);
+ }
+ }
+
+ tunnel_free(t);
+}
+
+static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(c);
+ pa_assert(u);
+
+ u->client = c;
+
+ switch (state) {
+ case AVAHI_CLIENT_S_REGISTERING:
+ case AVAHI_CLIENT_S_RUNNING:
+ case AVAHI_CLIENT_S_COLLISION:
+
+ if (!u->sink_browser) {
+
+ if (!(u->sink_browser = avahi_service_browser_new(
+ c,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ SERVICE_TYPE_SINK,
+ NULL,
+ 0,
+ browser_cb, u))) {
+
+ pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
+ pa_module_unload_request(u->module, TRUE);
+ }
+ }
+
+ break;
+
+ case AVAHI_CLIENT_FAILURE:
+ if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
+ int error;
+
+ pa_log_debug("Avahi daemon disconnected.");
+
+ if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
+ pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
+ pa_module_unload_request(u->module, TRUE);
+ }
+ }
+
+ /* Fall through */
+
+ case AVAHI_CLIENT_CONNECTING:
+
+ if (u->sink_browser) {
+ avahi_service_browser_free(u->sink_browser);
+ u->sink_browser = NULL;
+ }
+
+ break;
+
+ default: ;
+ }
+}
+
+int pa__init(pa_module*m) {
+
+ struct userdata *u;
+ pa_modargs *ma = NULL;
+ int error;
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->sink_browser = NULL;
+
+ u->tunnels = pa_hashmap_new(tunnel_hash, tunnel_compare);
+
+ u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
+
+ if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
+ pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
+ goto fail;
+ }
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ pa__done(m);
+
+ if (ma)
+ pa_modargs_free(ma);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata*u;
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->client)
+ avahi_client_free(u->client);
+
+ if (u->avahi_poll)
+ pa_avahi_poll_free(u->avahi_poll);
+
+ if (u->tunnels) {
+ struct tunnel *t;
+
+ while ((t = pa_hashmap_steal_first(u->tunnels))) {
+ pa_module_unload_request_by_index(u->core, t->module_index, TRUE);
+ tunnel_free(t);
+ }
+
+ pa_hashmap_free(u->tunnels, NULL, NULL);
+ }
+
+ pa_xfree(u);
+}
diff --git a/src/modules/raop/module-raop-sink.c b/src/modules/raop/module-raop-sink.c
new file mode 100644
index 00000000..87e7bc17
--- /dev/null
+++ b/src/modules/raop/module-raop-sink.c
@@ -0,0 +1,686 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2008 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <sys/ioctl.h>
+
+#ifdef HAVE_LINUX_SOCKIOS_H
+#include <linux/sockios.h>
+#endif
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/socket-client.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/time-smoother.h>
+#include <pulsecore/poll.h>
+
+#include "module-raop-sink-symdef.h"
+#include "rtp.h"
+#include "sdp.h"
+#include "sap.h"
+#include "raop_client.h"
+
+PA_MODULE_AUTHOR("Colin Guthrie");
+PA_MODULE_DESCRIPTION("RAOP Sink");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "sink_name=<name for the sink> "
+ "sink_properties=<properties for the sink> "
+ "server=<address> "
+ "format=<sample format> "
+ "rate=<sample rate> "
+ "channels=<number of channels>");
+
+#define DEFAULT_SINK_NAME "raop"
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_sink *sink;
+
+ pa_thread_mq thread_mq;
+ pa_rtpoll *rtpoll;
+ pa_rtpoll_item *rtpoll_item;
+ pa_thread *thread;
+
+ pa_memchunk raw_memchunk;
+ pa_memchunk encoded_memchunk;
+
+ void *write_data;
+ size_t write_length, write_index;
+
+ void *read_data;
+ size_t read_length, read_index;
+
+ pa_usec_t latency;
+
+ /*esd_format_t format;*/
+ int32_t rate;
+
+ pa_smoother *smoother;
+ int fd;
+
+ int64_t offset;
+ int64_t encoding_overhead;
+ int32_t next_encoding_overhead;
+ double encoding_ratio;
+
+ pa_raop_client *raop;
+
+ size_t block_size;
+};
+
+static const char* const valid_modargs[] = {
+ "sink_name",
+ "sink_properties",
+ "server",
+ "format",
+ "rate",
+ "channels",
+ NULL
+};
+
+enum {
+ SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX,
+ SINK_MESSAGE_RIP_SOCKET
+};
+
+/* Forward declaration */
+static void sink_set_volume_cb(pa_sink *);
+
+static void on_connection(int fd, void*userdata) {
+ int so_sndbuf = 0;
+ socklen_t sl = sizeof(int);
+ struct userdata *u = userdata;
+ pa_assert(u);
+
+ pa_assert(u->fd < 0);
+ u->fd = fd;
+
+ if (getsockopt(u->fd, SOL_SOCKET, SO_SNDBUF, &so_sndbuf, &sl) < 0)
+ pa_log_warn("getsockopt(SO_SNDBUF) failed: %s", pa_cstrerror(errno));
+ else {
+ pa_log_debug("SO_SNDBUF is %zu.", (size_t) so_sndbuf);
+ pa_sink_set_max_request(u->sink, PA_MAX((size_t) so_sndbuf, u->block_size));
+ }
+
+ /* Set the initial volume */
+ sink_set_volume_cb(u->sink);
+
+ pa_log_debug("Connection authenticated, handing fd to IO thread...");
+
+ pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
+}
+
+static void on_close(void*userdata) {
+ struct userdata *u = userdata;
+ pa_assert(u);
+
+ pa_log_debug("Connection closed, informing IO thread...");
+
+ pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_RIP_SOCKET, NULL, 0, NULL, NULL);
+}
+
+static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
+
+ switch (code) {
+
+ case PA_SINK_MESSAGE_SET_STATE:
+
+ switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
+
+ case PA_SINK_SUSPENDED:
+ pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
+
+ pa_smoother_pause(u->smoother, pa_rtclock_now());
+
+ /* Issue a FLUSH if we are connected */
+ if (u->fd >= 0) {
+ pa_raop_flush(u->raop);
+ }
+ break;
+
+ case PA_SINK_IDLE:
+ case PA_SINK_RUNNING:
+
+ if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
+ pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE);
+
+ /* The connection can be closed when idle, so check to
+ see if we need to reestablish it */
+ if (u->fd < 0)
+ pa_raop_connect(u->raop);
+ else
+ pa_raop_flush(u->raop);
+ }
+
+ break;
+
+ case PA_SINK_UNLINKED:
+ case PA_SINK_INIT:
+ case PA_SINK_INVALID_STATE:
+ ;
+ }
+
+ break;
+
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ pa_usec_t w, r;
+
+ r = pa_smoother_get(u->smoother, pa_rtclock_now());
+ w = pa_bytes_to_usec((u->offset - u->encoding_overhead + (u->encoded_memchunk.length / u->encoding_ratio)), &u->sink->sample_spec);
+
+ *((pa_usec_t*) data) = w > r ? w - r : 0;
+ return 0;
+ }
+
+ case SINK_MESSAGE_PASS_SOCKET: {
+ struct pollfd *pollfd;
+
+ pa_assert(!u->rtpoll_item);
+
+ u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->fd = u->fd;
+ pollfd->events = POLLOUT;
+ /*pollfd->events = */pollfd->revents = 0;
+
+ if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
+ /* Our stream has been suspended so we just flush it.... */
+ pa_raop_flush(u->raop);
+ }
+ return 0;
+ }
+
+ case SINK_MESSAGE_RIP_SOCKET: {
+ pa_assert(u->fd >= 0);
+
+ pa_close(u->fd);
+ u->fd = -1;
+
+ if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
+
+ pa_log_debug("RTSP control connection closed, but we're suspended so let's not worry about it... we'll open it again later");
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+ u->rtpoll_item = NULL;
+ } else {
+ /* Quesiton: is this valid here: or should we do some sort of:
+ return pa_sink_process_msg(PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL);
+ ?? */
+ pa_module_unload_request(u->module, TRUE);
+ }
+ return 0;
+ }
+ }
+
+ return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+static void sink_set_volume_cb(pa_sink *s) {
+ struct userdata *u = s->userdata;
+ pa_cvolume hw;
+ pa_volume_t v;
+ char t[PA_CVOLUME_SNPRINT_MAX];
+
+ pa_assert(u);
+
+ /* If we're muted we don't need to do anything */
+ if (s->muted)
+ return;
+
+ /* Calculate the max volume of all channels.
+ We'll use this as our (single) volume on the APEX device and emulate
+ any variation in channel volumes in software */
+ v = pa_cvolume_max(&s->real_volume);
+
+ /* Create a pa_cvolume version of our single value */
+ pa_cvolume_set(&hw, s->sample_spec.channels, v);
+
+ /* Perform any software manipulation of the volume needed */
+ pa_sw_cvolume_divide(&s->soft_volume, &s->real_volume, &hw);
+
+ pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->real_volume));
+ pa_log_debug("Got hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &hw));
+ pa_log_debug("Calculated software volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->soft_volume));
+
+ /* Any necessary software volume manipulateion is done so set
+ our hw volume (or v as a single value) on the device */
+ pa_raop_client_set_volume(u->raop, v);
+}
+
+static void sink_set_mute_cb(pa_sink *s) {
+ struct userdata *u = s->userdata;
+
+ pa_assert(u);
+
+ if (s->muted) {
+ pa_raop_client_set_volume(u->raop, PA_VOLUME_MUTED);
+ } else {
+ sink_set_volume_cb(s);
+ }
+}
+
+static void thread_func(void *userdata) {
+ struct userdata *u = userdata;
+ int write_type = 0;
+ pa_memchunk silence;
+ uint32_t silence_overhead = 0;
+ double silence_ratio = 0;
+
+ pa_assert(u);
+
+ pa_log_debug("Thread starting up");
+
+ pa_thread_mq_install(&u->thread_mq);
+
+ pa_smoother_set_time_offset(u->smoother, pa_rtclock_now());
+
+ /* Create a chunk of memory that is our encoded silence sample. */
+ pa_memchunk_reset(&silence);
+
+ for (;;) {
+ int ret;
+
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
+ if (u->sink->thread_info.rewind_requested)
+ pa_sink_process_rewind(u->sink, 0);
+
+ if (u->rtpoll_item) {
+ struct pollfd *pollfd;
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ /* Render some data and write it to the fifo */
+ if (/*PA_SINK_IS_OPENED(u->sink->thread_info.state) && */pollfd->revents) {
+ pa_usec_t usec;
+ int64_t n;
+ void *p;
+
+ if (!silence.memblock) {
+ pa_memchunk silence_tmp;
+
+ pa_memchunk_reset(&silence_tmp);
+ silence_tmp.memblock = pa_memblock_new(u->core->mempool, 4096);
+ silence_tmp.length = 4096;
+ p = pa_memblock_acquire(silence_tmp.memblock);
+ memset(p, 0, 4096);
+ pa_memblock_release(silence_tmp.memblock);
+ pa_raop_client_encode_sample(u->raop, &silence_tmp, &silence);
+ pa_assert(0 == silence_tmp.length);
+ silence_overhead = silence_tmp.length - 4096;
+ silence_ratio = silence_tmp.length / 4096;
+ pa_memblock_unref(silence_tmp.memblock);
+ }
+
+ for (;;) {
+ ssize_t l;
+
+ if (u->encoded_memchunk.length <= 0) {
+ if (u->encoded_memchunk.memblock)
+ pa_memblock_unref(u->encoded_memchunk.memblock);
+ if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+ size_t rl;
+
+ /* We render real data */
+ if (u->raw_memchunk.length <= 0) {
+ if (u->raw_memchunk.memblock)
+ pa_memblock_unref(u->raw_memchunk.memblock);
+ pa_memchunk_reset(&u->raw_memchunk);
+
+ /* Grab unencoded data */
+ pa_sink_render(u->sink, u->block_size, &u->raw_memchunk);
+ }
+ pa_assert(u->raw_memchunk.length > 0);
+
+ /* Encode it */
+ rl = u->raw_memchunk.length;
+ u->encoding_overhead += u->next_encoding_overhead;
+ pa_raop_client_encode_sample(u->raop, &u->raw_memchunk, &u->encoded_memchunk);
+ u->next_encoding_overhead = (u->encoded_memchunk.length - (rl - u->raw_memchunk.length));
+ u->encoding_ratio = u->encoded_memchunk.length / (rl - u->raw_memchunk.length);
+ } else {
+ /* We render some silence into our memchunk */
+ memcpy(&u->encoded_memchunk, &silence, sizeof(pa_memchunk));
+ pa_memblock_ref(silence.memblock);
+
+ /* Calculate/store some values to be used with the smoother */
+ u->next_encoding_overhead = silence_overhead;
+ u->encoding_ratio = silence_ratio;
+ }
+ }
+ pa_assert(u->encoded_memchunk.length > 0);
+
+ p = pa_memblock_acquire(u->encoded_memchunk.memblock);
+ l = pa_write(u->fd, (uint8_t*) p + u->encoded_memchunk.index, u->encoded_memchunk.length, &write_type);
+ pa_memblock_release(u->encoded_memchunk.memblock);
+
+ pa_assert(l != 0);
+
+ if (l < 0) {
+
+ if (errno == EINTR)
+ continue;
+ else if (errno == EAGAIN) {
+
+ /* OK, we filled all socket buffers up
+ * now. */
+ goto filled_up;
+
+ } else {
+ pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ } else {
+ u->offset += l;
+
+ u->encoded_memchunk.index += l;
+ u->encoded_memchunk.length -= l;
+
+ pollfd->revents = 0;
+
+ if (u->encoded_memchunk.length > 0) {
+ /* we've completely written the encoded data, so update our overhead */
+ u->encoding_overhead += u->next_encoding_overhead;
+
+ /* OK, we wrote less that we asked for,
+ * hence we can assume that the socket
+ * buffers are full now */
+ goto filled_up;
+ }
+ }
+ }
+
+ filled_up:
+
+ /* At this spot we know that the socket buffers are
+ * fully filled up. This is the best time to estimate
+ * the playback position of the server */
+
+ n = u->offset - u->encoding_overhead;
+
+#ifdef SIOCOUTQ
+ {
+ int l;
+ if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
+ n -= (l / u->encoding_ratio);
+ }
+#endif
+
+ usec = pa_bytes_to_usec(n, &u->sink->sample_spec);
+
+ if (usec > u->latency)
+ usec -= u->latency;
+ else
+ usec = 0;
+
+ pa_smoother_put(u->smoother, pa_rtclock_now(), usec);
+ }
+
+ /* Hmm, nothing to do. Let's sleep */
+ pollfd->events = POLLOUT; /*PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0;*/
+ }
+
+ if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ goto fail;
+
+ if (ret == 0)
+ goto finish;
+
+ if (u->rtpoll_item) {
+ struct pollfd* pollfd;
+
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+
+ if (pollfd->revents & ~POLLOUT) {
+ if (u->sink->thread_info.state != PA_SINK_SUSPENDED) {
+ pa_log("FIFO shutdown.");
+ goto fail;
+ }
+
+ /* We expect this to happen on occasion if we are not sending data.
+ It's perfectly natural and normal and natural */
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+ u->rtpoll_item = NULL;
+ }
+ }
+ }
+
+fail:
+ /* If this was no regular exit from the loop we have to continue
+ * processing messages until we received PA_MESSAGE_SHUTDOWN */
+ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ if (silence.memblock)
+ pa_memblock_unref(silence.memblock);
+ pa_log_debug("Thread shutting down");
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u = NULL;
+ pa_sample_spec ss;
+ pa_modargs *ma = NULL;
+ const char *server;
+ pa_sink_new_data data;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("failed to parse module arguments");
+ goto fail;
+ }
+
+ ss = m->core->default_sample_spec;
+ if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
+ pa_log("invalid sample format specification");
+ goto fail;
+ }
+
+ if ((/*ss.format != PA_SAMPLE_U8 &&*/ ss.format != PA_SAMPLE_S16NE) ||
+ (ss.channels > 2)) {
+ pa_log("sample type support is limited to mono/stereo and U8 or S16NE sample data");
+ goto fail;
+ }
+
+ u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ m->userdata = u;
+ u->fd = -1;
+ u->smoother = pa_smoother_new(
+ PA_USEC_PER_SEC,
+ PA_USEC_PER_SEC*2,
+ TRUE,
+ TRUE,
+ 10,
+ 0,
+ FALSE);
+ pa_memchunk_reset(&u->raw_memchunk);
+ pa_memchunk_reset(&u->encoded_memchunk);
+ u->offset = 0;
+ u->encoding_overhead = 0;
+ u->next_encoding_overhead = 0;
+ u->encoding_ratio = 1.0;
+
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+ u->rtpoll_item = NULL;
+
+ /*u->format =
+ (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
+ (ss.channels == 2 ? ESD_STEREO : ESD_MONO);*/
+ u->rate = ss.rate;
+ u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
+
+ u->read_data = u->write_data = NULL;
+ u->read_index = u->write_index = u->read_length = u->write_length = 0;
+
+ /*u->state = STATE_AUTH;*/
+ u->latency = 0;
+
+ if (!(server = pa_modargs_get_value(ma, "server", NULL))) {
+ pa_log("No server argument given.");
+ goto fail;
+ }
+
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "music");
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "RAOP sink '%s'", server);
+
+ if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_sink_new_data_done(&data);
+ goto fail;
+ }
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
+ goto fail;
+ }
+
+ u->sink->parent.process_msg = sink_process_msg;
+ u->sink->userdata = u;
+ u->sink->set_volume = sink_set_volume_cb;
+ u->sink->set_mute = sink_set_mute_cb;
+ u->sink->flags = PA_SINK_LATENCY|PA_SINK_NETWORK|PA_SINK_HW_VOLUME_CTRL;
+
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+
+ if (!(u->raop = pa_raop_client_new(u->core, server))) {
+ pa_log("Failed to connect to server.");
+ goto fail;
+ }
+
+ pa_raop_client_set_callback(u->raop, on_connection, u);
+ pa_raop_client_set_closed_callback(u->raop, on_close, u);
+
+ if (!(u->thread = pa_thread_new("raop-sink", thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ pa_sink_put(u->sink);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink)
+ pa_sink_unlink(u->sink);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ pa_thread_mq_done(&u->thread_mq);
+
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
+ if (u->rtpoll_item)
+ pa_rtpoll_item_free(u->rtpoll_item);
+
+ if (u->rtpoll)
+ pa_rtpoll_free(u->rtpoll);
+
+ if (u->raw_memchunk.memblock)
+ pa_memblock_unref(u->raw_memchunk.memblock);
+
+ if (u->encoded_memchunk.memblock)
+ pa_memblock_unref(u->encoded_memchunk.memblock);
+
+ if (u->raop)
+ pa_raop_client_free(u->raop);
+
+ pa_xfree(u->read_data);
+ pa_xfree(u->write_data);
+
+ if (u->smoother)
+ pa_smoother_free(u->smoother);
+
+ if (u->fd >= 0)
+ pa_close(u->fd);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/raop/raop_client.c b/src/modules/raop/raop_client.c
new file mode 100644
index 00000000..cba7af9a
--- /dev/null
+++ b/src/modules/raop/raop_client.c
@@ -0,0 +1,551 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+
+#ifdef HAVE_SYS_FILIO_H
+#include <sys/filio.h>
+#endif
+
+/* TODO: Replace OpenSSL with NSS */
+#include <openssl/err.h>
+#include <openssl/rand.h>
+#include <openssl/aes.h>
+#include <openssl/rsa.h>
+#include <openssl/engine.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/iochannel.h>
+#include <pulsecore/socket-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/memchunk.h>
+#include <pulsecore/random.h>
+
+#include "raop_client.h"
+#include "rtsp_client.h"
+#include "base64.h"
+
+#define AES_CHUNKSIZE 16
+
+#define JACK_STATUS_DISCONNECTED 0
+#define JACK_STATUS_CONNECTED 1
+
+#define JACK_TYPE_ANALOG 0
+#define JACK_TYPE_DIGITAL 1
+
+#define VOLUME_DEF -30
+#define VOLUME_MIN -144
+#define VOLUME_MAX 0
+
+
+struct pa_raop_client {
+ pa_core *core;
+ char *host;
+ char *sid;
+ pa_rtsp_client *rtsp;
+
+ uint8_t jack_type;
+ uint8_t jack_status;
+
+ /* Encryption Related bits */
+ AES_KEY aes;
+ uint8_t aes_iv[AES_CHUNKSIZE]; /* initialization vector for aes-cbc */
+ uint8_t aes_nv[AES_CHUNKSIZE]; /* next vector for aes-cbc */
+ uint8_t aes_key[AES_CHUNKSIZE]; /* key for aes-cbc */
+
+ pa_socket_client *sc;
+ int fd;
+
+ uint16_t seq;
+ uint32_t rtptime;
+
+ pa_raop_client_cb_t callback;
+ void* userdata;
+ pa_raop_client_closed_cb_t closed_callback;
+ void* closed_userdata;
+};
+
+/**
+ * Function to write bits into a buffer.
+ * @param buffer Handle to the buffer. It will be incremented if new data requires it.
+ * @param bit_pos A pointer to a position buffer to keep track the current write location (0 for MSB, 7 for LSB)
+ * @param size A pointer to the byte size currently written. This allows the calling function to do simple buffer overflow checks
+ * @param data The data to write
+ * @param data_bit_len The number of bits from data to write
+ */
+static inline void bit_writer(uint8_t **buffer, uint8_t *bit_pos, int *size, uint8_t data, uint8_t data_bit_len) {
+ int bits_left, bit_overflow;
+ uint8_t bit_data;
+
+ if (!data_bit_len)
+ return;
+
+ /* If bit pos is zero, we will definatly use at least one bit from the current byte so size increments. */
+ if (!*bit_pos)
+ *size += 1;
+
+ /* Calc the number of bits left in the current byte of buffer */
+ bits_left = 7 - *bit_pos + 1;
+ /* Calc the overflow of bits in relation to how much space we have left... */
+ bit_overflow = bits_left - data_bit_len;
+ if (bit_overflow >= 0) {
+ /* We can fit the new data in our current byte */
+ /* As we write from MSB->LSB we need to left shift by the overflow amount */
+ bit_data = data << bit_overflow;
+ if (*bit_pos)
+ **buffer |= bit_data;
+ else
+ **buffer = bit_data;
+ /* If our data fits exactly into the current byte, we need to increment our pointer */
+ if (0 == bit_overflow) {
+ /* Do not increment size as it will be incremeneted on next call as bit_pos is zero */
+ *buffer += 1;
+ *bit_pos = 0;
+ } else {
+ *bit_pos += data_bit_len;
+ }
+ } else {
+ /* bit_overflow is negative, there for we will need a new byte from our buffer */
+ /* Firstly fill up what's left in the current byte */
+ bit_data = data >> -bit_overflow;
+ **buffer |= bit_data;
+ /* Increment our buffer pointer and size counter*/
+ *buffer += 1;
+ *size += 1;
+ **buffer = data << (8 + bit_overflow);
+ *bit_pos = -bit_overflow;
+ }
+}
+
+static int rsa_encrypt(uint8_t *text, int len, uint8_t *res) {
+ const char n[] =
+ "59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUtwC"
+ "5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZHJ2YCCLlDR"
+ "KSKv6kDqnw4UwPdpOMXziC/AMj3Z/lUVX1G7WSHCAWKf1zNS1eLvqr+boEjXuB"
+ "OitnZ/bDzPHrTOZz0Dew0uowxf/+sG+NCK3eQJVxqcaJ/vEHKIVd2M+5qL71yJ"
+ "Q+87X6oV3eaYvt3zWZYD6z5vYTcrtij2VZ9Zmni/UAaHqn9JdsBWLUEpVviYnh"
+ "imNVvYFZeCXg/IdTQ+x4IRdiXNv5hEew==";
+ const char e[] = "AQAB";
+ uint8_t modules[256];
+ uint8_t exponent[8];
+ int size;
+ RSA *rsa;
+
+ rsa = RSA_new();
+ size = pa_base64_decode(n, modules);
+ rsa->n = BN_bin2bn(modules, size, NULL);
+ size = pa_base64_decode(e, exponent);
+ rsa->e = BN_bin2bn(exponent, size, NULL);
+
+ size = RSA_public_encrypt(len, text, res, rsa, RSA_PKCS1_OAEP_PADDING);
+ RSA_free(rsa);
+ return size;
+}
+
+static int aes_encrypt(pa_raop_client* c, uint8_t *data, int size) {
+ uint8_t *buf;
+ int i=0, j;
+
+ pa_assert(c);
+
+ memcpy(c->aes_nv, c->aes_iv, AES_CHUNKSIZE);
+ while (i+AES_CHUNKSIZE <= size) {
+ buf = data + i;
+ for (j=0; j<AES_CHUNKSIZE; ++j)
+ buf[j] ^= c->aes_nv[j];
+
+ AES_encrypt(buf, buf, &c->aes);
+ memcpy(c->aes_nv, buf, AES_CHUNKSIZE);
+ i += AES_CHUNKSIZE;
+ }
+ return i;
+}
+
+static inline void rtrimchar(char *str, char rc) {
+ char *sp = str + strlen(str) - 1;
+ while (sp >= str && *sp == rc) {
+ *sp = '\0';
+ sp -= 1;
+ }
+}
+
+static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
+ pa_raop_client *c = userdata;
+
+ pa_assert(sc);
+ pa_assert(c);
+ pa_assert(c->sc == sc);
+ pa_assert(c->fd < 0);
+ pa_assert(c->callback);
+
+ pa_socket_client_unref(c->sc);
+ c->sc = NULL;
+
+ if (!io) {
+ pa_log("Connection failed: %s", pa_cstrerror(errno));
+ return;
+ }
+
+ c->fd = pa_iochannel_get_send_fd(io);
+
+ pa_iochannel_set_noclose(io, TRUE);
+ pa_iochannel_free(io);
+
+ pa_make_tcp_socket_low_delay(c->fd);
+
+ pa_log_debug("Connection established");
+ c->callback(c->fd, c->userdata);
+}
+
+static void rtsp_cb(pa_rtsp_client *rtsp, pa_rtsp_state state, pa_headerlist* headers, void *userdata) {
+ pa_raop_client* c = userdata;
+ pa_assert(c);
+ pa_assert(rtsp);
+ pa_assert(rtsp == c->rtsp);
+
+ switch (state) {
+ case STATE_CONNECT: {
+ int i;
+ uint8_t rsakey[512];
+ char *key, *iv, *sac, *sdp;
+ uint16_t rand_data;
+ const char *ip;
+ char *url;
+
+ pa_log_debug("RAOP: CONNECTED");
+ ip = pa_rtsp_localip(c->rtsp);
+ /* First of all set the url properly */
+ url = pa_sprintf_malloc("rtsp://%s/%s", ip, c->sid);
+ pa_rtsp_set_url(c->rtsp, url);
+ pa_xfree(url);
+
+ /* Now encrypt our aes_public key to send to the device */
+ i = rsa_encrypt(c->aes_key, AES_CHUNKSIZE, rsakey);
+ pa_base64_encode(rsakey, i, &key);
+ rtrimchar(key, '=');
+ pa_base64_encode(c->aes_iv, AES_CHUNKSIZE, &iv);
+ rtrimchar(iv, '=');
+
+ pa_random(&rand_data, sizeof(rand_data));
+ pa_base64_encode(&rand_data, AES_CHUNKSIZE, &sac);
+ rtrimchar(sac, '=');
+ pa_rtsp_add_header(c->rtsp, "Apple-Challenge", sac);
+ sdp = pa_sprintf_malloc(
+ "v=0\r\n"
+ "o=iTunes %s 0 IN IP4 %s\r\n"
+ "s=iTunes\r\n"
+ "c=IN IP4 %s\r\n"
+ "t=0 0\r\n"
+ "m=audio 0 RTP/AVP 96\r\n"
+ "a=rtpmap:96 AppleLossless\r\n"
+ "a=fmtp:96 4096 0 16 40 10 14 2 255 0 0 44100\r\n"
+ "a=rsaaeskey:%s\r\n"
+ "a=aesiv:%s\r\n",
+ c->sid, ip, c->host, key, iv);
+ pa_rtsp_announce(c->rtsp, sdp);
+ pa_xfree(key);
+ pa_xfree(iv);
+ pa_xfree(sac);
+ pa_xfree(sdp);
+ break;
+ }
+
+ case STATE_ANNOUNCE:
+ pa_log_debug("RAOP: ANNOUNCED");
+ pa_rtsp_remove_header(c->rtsp, "Apple-Challenge");
+ pa_rtsp_setup(c->rtsp);
+ break;
+
+ case STATE_SETUP: {
+ char *aj = pa_xstrdup(pa_headerlist_gets(headers, "Audio-Jack-Status"));
+ pa_log_debug("RAOP: SETUP");
+ if (aj) {
+ char *token, *pc;
+ char delimiters[] = ";";
+ const char* token_state = NULL;
+ c->jack_type = JACK_TYPE_ANALOG;
+ c->jack_status = JACK_STATUS_DISCONNECTED;
+
+ while ((token = pa_split(aj, delimiters, &token_state))) {
+ if ((pc = strstr(token, "="))) {
+ *pc = 0;
+ if (!strcmp(token, "type") && !strcmp(pc+1, "digital")) {
+ c->jack_type = JACK_TYPE_DIGITAL;
+ }
+ } else {
+ if (!strcmp(token,"connected"))
+ c->jack_status = JACK_STATUS_CONNECTED;
+ }
+ pa_xfree(token);
+ }
+ pa_xfree(aj);
+ } else {
+ pa_log_warn("Audio Jack Status missing");
+ }
+ pa_rtsp_record(c->rtsp, &c->seq, &c->rtptime);
+ break;
+ }
+
+ case STATE_RECORD: {
+ uint32_t port = pa_rtsp_serverport(c->rtsp);
+ pa_log_debug("RAOP: RECORDED");
+
+ if (!(c->sc = pa_socket_client_new_string(c->core->mainloop, TRUE, c->host, port))) {
+ pa_log("failed to connect to server '%s:%d'", c->host, port);
+ return;
+ }
+ pa_socket_client_set_callback(c->sc, on_connection, c);
+ break;
+ }
+
+ case STATE_FLUSH:
+ pa_log_debug("RAOP: FLUSHED");
+ break;
+
+ case STATE_TEARDOWN:
+ pa_log_debug("RAOP: TEARDOWN");
+ break;
+
+ case STATE_SET_PARAMETER:
+ pa_log_debug("RAOP: SET_PARAMETER");
+ break;
+
+ case STATE_DISCONNECTED:
+ pa_assert(c->closed_callback);
+ pa_assert(c->rtsp);
+
+ pa_log_debug("RTSP control channel closed");
+ pa_rtsp_client_free(c->rtsp);
+ c->rtsp = NULL;
+ if (c->fd > 0) {
+ /* We do not close the fd, we leave it to the closed callback to do that */
+ c->fd = -1;
+ }
+ if (c->sc) {
+ pa_socket_client_unref(c->sc);
+ c->sc = NULL;
+ }
+ pa_xfree(c->sid);
+ c->sid = NULL;
+ c->closed_callback(c->closed_userdata);
+ break;
+ }
+}
+
+pa_raop_client* pa_raop_client_new(pa_core *core, const char* host) {
+ pa_raop_client* c = pa_xnew0(pa_raop_client, 1);
+
+ pa_assert(core);
+ pa_assert(host);
+
+ c->core = core;
+ c->fd = -1;
+ c->host = pa_xstrdup(host);
+
+ if (pa_raop_connect(c)) {
+ pa_raop_client_free(c);
+ return NULL;
+ }
+ return c;
+}
+
+
+void pa_raop_client_free(pa_raop_client* c) {
+ pa_assert(c);
+
+ if (c->rtsp)
+ pa_rtsp_client_free(c->rtsp);
+ pa_xfree(c->host);
+ pa_xfree(c);
+}
+
+
+int pa_raop_connect(pa_raop_client* c) {
+ char *sci;
+ struct {
+ uint32_t a;
+ uint32_t b;
+ uint32_t c;
+ } rand_data;
+
+ pa_assert(c);
+
+ if (c->rtsp) {
+ pa_log_debug("Connection already in progress");
+ return 0;
+ }
+
+ c->rtsp = pa_rtsp_client_new(c->core->mainloop, c->host, 5000, "iTunes/4.6 (Macintosh; U; PPC Mac OS X 10.3)");
+
+ /* Initialise the AES encryption system */
+ pa_random(c->aes_iv, sizeof(c->aes_iv));
+ pa_random(c->aes_key, sizeof(c->aes_key));
+ memcpy(c->aes_nv, c->aes_iv, sizeof(c->aes_nv));
+ AES_set_encrypt_key(c->aes_key, 128, &c->aes);
+
+ /* Generate random instance id */
+ pa_random(&rand_data, sizeof(rand_data));
+ c->sid = pa_sprintf_malloc("%u", rand_data.a);
+ sci = pa_sprintf_malloc("%08x%08x",rand_data.b, rand_data.c);
+ pa_rtsp_add_header(c->rtsp, "Client-Instance", sci);
+ pa_xfree(sci);
+ pa_rtsp_set_callback(c->rtsp, rtsp_cb, c);
+ return pa_rtsp_connect(c->rtsp);
+}
+
+
+int pa_raop_flush(pa_raop_client* c) {
+ pa_assert(c);
+
+ pa_rtsp_flush(c->rtsp, c->seq, c->rtptime);
+ return 0;
+}
+
+
+int pa_raop_client_set_volume(pa_raop_client* c, pa_volume_t volume) {
+ int rv;
+ double db;
+ char *param;
+
+ pa_assert(c);
+
+ db = pa_sw_volume_to_dB(volume);
+ if (db < VOLUME_MIN)
+ db = VOLUME_MIN;
+ else if (db > VOLUME_MAX)
+ db = VOLUME_MAX;
+
+ param = pa_sprintf_malloc("volume: %0.6f\r\n", db);
+
+ /* We just hit and hope, cannot wait for the callback */
+ rv = pa_rtsp_setparameter(c->rtsp, param);
+ pa_xfree(param);
+ return rv;
+}
+
+
+int pa_raop_client_encode_sample(pa_raop_client* c, pa_memchunk* raw, pa_memchunk* encoded) {
+ uint16_t len;
+ size_t bufmax;
+ uint8_t *bp, bpos;
+ uint8_t *ibp, *maxibp;
+ int size;
+ uint8_t *b, *p;
+ uint32_t bsize;
+ size_t length;
+ static uint8_t header[] = {
+ 0x24, 0x00, 0x00, 0x00,
+ 0xF0, 0xFF, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ };
+ int header_size = sizeof(header);
+
+ pa_assert(c);
+ pa_assert(c->fd > 0);
+ pa_assert(raw);
+ pa_assert(raw->memblock);
+ pa_assert(raw->length > 0);
+ pa_assert(encoded);
+
+ /* We have to send 4 byte chunks */
+ bsize = (int)(raw->length / 4);
+ length = bsize * 4;
+
+ /* Leave 16 bytes extra to allow for the ALAC header which is about 55 bits */
+ bufmax = length + header_size + 16;
+ pa_memchunk_reset(encoded);
+ encoded->memblock = pa_memblock_new(c->core->mempool, bufmax);
+ b = pa_memblock_acquire(encoded->memblock);
+ memcpy(b, header, header_size);
+
+ /* Now write the actual samples */
+ bp = b + header_size;
+ size = bpos = 0;
+ bit_writer(&bp,&bpos,&size,1,3); /* channel=1, stereo */
+ bit_writer(&bp,&bpos,&size,0,4); /* unknown */
+ bit_writer(&bp,&bpos,&size,0,8); /* unknown */
+ bit_writer(&bp,&bpos,&size,0,4); /* unknown */
+ bit_writer(&bp,&bpos,&size,1,1); /* hassize */
+ bit_writer(&bp,&bpos,&size,0,2); /* unused */
+ bit_writer(&bp,&bpos,&size,1,1); /* is-not-compressed */
+
+ /* size of data, integer, big endian */
+ bit_writer(&bp,&bpos,&size,(bsize>>24)&0xff,8);
+ bit_writer(&bp,&bpos,&size,(bsize>>16)&0xff,8);
+ bit_writer(&bp,&bpos,&size,(bsize>>8)&0xff,8);
+ bit_writer(&bp,&bpos,&size,(bsize)&0xff,8);
+
+ ibp = p = pa_memblock_acquire(raw->memblock);
+ maxibp = p + raw->length - 4;
+ while (ibp <= maxibp) {
+ /* Byte swap stereo data */
+ bit_writer(&bp,&bpos,&size,*(ibp+1),8);
+ bit_writer(&bp,&bpos,&size,*(ibp+0),8);
+ bit_writer(&bp,&bpos,&size,*(ibp+3),8);
+ bit_writer(&bp,&bpos,&size,*(ibp+2),8);
+ ibp += 4;
+ raw->index += 4;
+ raw->length -= 4;
+ }
+ pa_memblock_release(raw->memblock);
+ encoded->length = header_size + size;
+
+ /* store the lenght (endian swapped: make this better) */
+ len = size + header_size - 4;
+ *(b + 2) = len >> 8;
+ *(b + 3) = len & 0xff;
+
+ /* encrypt our data */
+ aes_encrypt(c, (b + header_size), size);
+
+ /* We're done with the chunk */
+ pa_memblock_release(encoded->memblock);
+
+ return 0;
+}
+
+
+void pa_raop_client_set_callback(pa_raop_client* c, pa_raop_client_cb_t callback, void *userdata) {
+ pa_assert(c);
+
+ c->callback = callback;
+ c->userdata = userdata;
+}
+
+void pa_raop_client_set_closed_callback(pa_raop_client* c, pa_raop_client_closed_cb_t callback, void *userdata) {
+ pa_assert(c);
+
+ c->closed_callback = callback;
+ c->closed_userdata = userdata;
+}
diff --git a/src/modules/raop/raop_client.h b/src/modules/raop/raop_client.h
new file mode 100644
index 00000000..ce81f392
--- /dev/null
+++ b/src/modules/raop/raop_client.h
@@ -0,0 +1,44 @@
+#ifndef fooraopclientfoo
+#define fooraopclientfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <pulsecore/core.h>
+
+typedef struct pa_raop_client pa_raop_client;
+
+pa_raop_client* pa_raop_client_new(pa_core *core, const char* host);
+void pa_raop_client_free(pa_raop_client* c);
+
+int pa_raop_connect(pa_raop_client* c);
+int pa_raop_flush(pa_raop_client* c);
+
+int pa_raop_client_set_volume(pa_raop_client* c, pa_volume_t volume);
+int pa_raop_client_encode_sample(pa_raop_client* c, pa_memchunk* raw, pa_memchunk* encoded);
+
+typedef void (*pa_raop_client_cb_t)(int fd, void *userdata);
+void pa_raop_client_set_callback(pa_raop_client* c, pa_raop_client_cb_t callback, void *userdata);
+
+typedef void (*pa_raop_client_closed_cb_t)(void *userdata);
+void pa_raop_client_set_closed_callback(pa_raop_client* c, pa_raop_client_closed_cb_t callback, void *userdata);
+
+#endif
diff --git a/src/modules/reserve-monitor.c b/src/modules/reserve-monitor.c
new file mode 100644
index 00000000..ab453e61
--- /dev/null
+++ b/src/modules/reserve-monitor.c
@@ -0,0 +1,256 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/
+
+/***
+ Copyright 2009 Lennart Poettering
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation files
+ (the "Software"), to deal in the Software without restriction,
+ including without limitation the rights to use, copy, modify, merge,
+ publish, distribute, sublicense, and/or sell copies of the Software,
+ and to permit persons to whom the Software is furnished to do so,
+ subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+***/
+
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+
+#include "reserve-monitor.h"
+
+struct rm_monitor {
+ int ref;
+
+ char *device_name;
+ char *service_name;
+ char *match;
+
+ DBusConnection *connection;
+
+ unsigned busy:1;
+ unsigned filtering:1;
+ unsigned matching:1;
+
+ rm_change_cb_t change_cb;
+ void *userdata;
+};
+
+#define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
+
+#define SERVICE_FILTER \
+ "type='signal'," \
+ "sender='" DBUS_SERVICE_DBUS "'," \
+ "interface='" DBUS_INTERFACE_DBUS "'," \
+ "member='NameOwnerChanged'," \
+ "arg0='%s'"
+
+static DBusHandlerResult filter_handler(
+ DBusConnection *c,
+ DBusMessage *s,
+ void *userdata) {
+
+ rm_monitor *m;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ m = userdata;
+ assert(m->ref >= 1);
+
+ if (dbus_message_is_signal(s, "org.freedesktop.DBus", "NameOwnerChanged")) {
+ const char *name, *old, *new;
+
+ if (!dbus_message_get_args(
+ s,
+ &error,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_STRING, &old,
+ DBUS_TYPE_STRING, &new,
+ DBUS_TYPE_INVALID))
+ goto invalid;
+
+ if (strcmp(name, m->service_name) == 0) {
+ m->busy = !!(new && *new);
+
+ /* If we ourselves own the device, then don't consider this 'busy' */
+ if (m->busy) {
+ const char *un;
+
+ if ((un = dbus_bus_get_unique_name(c)))
+ if (strcmp(new, un) == 0)
+ m->busy = FALSE;
+ }
+
+ if (m->change_cb) {
+ m->ref++;
+ m->change_cb(m);
+ rm_release(m);
+ }
+ }
+ }
+
+invalid:
+ dbus_error_free(&error);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+int rm_watch(
+ rm_monitor **_m,
+ DBusConnection *connection,
+ const char*device_name,
+ rm_change_cb_t change_cb,
+ DBusError *error) {
+
+ rm_monitor *m = NULL;
+ int r;
+ DBusError _error;
+
+ if (!error)
+ error = &_error;
+
+ dbus_error_init(error);
+
+ if (!_m)
+ return -EINVAL;
+
+ if (!connection)
+ return -EINVAL;
+
+ if (!device_name)
+ return -EINVAL;
+
+ if (!(m = calloc(sizeof(rm_monitor), 1)))
+ return -ENOMEM;
+
+ m->ref = 1;
+
+ if (!(m->device_name = strdup(device_name))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ m->connection = dbus_connection_ref(connection);
+ m->change_cb = change_cb;
+
+ if (!(m->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+ sprintf(m->service_name, SERVICE_PREFIX "%s", m->device_name);
+
+ if (!(dbus_connection_add_filter(m->connection, filter_handler, m, NULL))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ m->filtering = 1;
+
+ if (!(m->match = malloc(sizeof(SERVICE_FILTER) - 2 + strlen(m->service_name)))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ sprintf(m->match, SERVICE_FILTER, m->service_name);
+ dbus_bus_add_match(m->connection, m->match, error);
+
+ if (dbus_error_is_set(error)) {
+ r = -EIO;
+ goto fail;
+ }
+
+ m->matching = 1;
+
+ m->busy = dbus_bus_name_has_owner(m->connection, m->service_name, error);
+
+ if (dbus_error_is_set(error)) {
+ r = -EIO;
+ goto fail;
+ }
+
+ *_m = m;
+ return 0;
+
+fail:
+ if (&_error == error)
+ dbus_error_free(&_error);
+
+ if (m)
+ rm_release(m);
+
+ return r;
+}
+
+void rm_release(rm_monitor *m) {
+ if (!m)
+ return;
+
+ assert(m->ref > 0);
+
+ if (--m->ref > 0)
+ return;
+
+ if (m->matching)
+ dbus_bus_remove_match(
+ m->connection,
+ m->match,
+ NULL);
+
+ if (m->filtering)
+ dbus_connection_remove_filter(
+ m->connection,
+ filter_handler,
+ m);
+
+ free(m->device_name);
+ free(m->service_name);
+ free(m->match);
+
+ if (m->connection)
+ dbus_connection_unref(m->connection);
+
+ free(m);
+}
+
+int rm_busy(rm_monitor *m) {
+ if (!m)
+ return -EINVAL;
+
+ assert(m->ref > 0);
+
+ return m->busy;
+}
+
+void rm_set_userdata(rm_monitor *m, void *userdata) {
+
+ if (!m)
+ return;
+
+ assert(m->ref > 0);
+ m->userdata = userdata;
+}
+
+void* rm_get_userdata(rm_monitor *m) {
+
+ if (!m)
+ return NULL;
+
+ assert(m->ref > 0);
+
+ return m->userdata;
+}
diff --git a/src/modules/reserve-monitor.h b/src/modules/reserve-monitor.h
new file mode 100644
index 00000000..3408680f
--- /dev/null
+++ b/src/modules/reserve-monitor.h
@@ -0,0 +1,71 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/
+
+#ifndef fooreservemonitorhfoo
+#define fooreservemonitorhfoo
+
+/***
+ Copyright 2009 Lennart Poettering
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation files
+ (the "Software"), to deal in the Software without restriction,
+ including without limitation the rights to use, copy, modify, merge,
+ publish, distribute, sublicense, and/or sell copies of the Software,
+ and to permit persons to whom the Software is furnished to do so,
+ subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+***/
+
+#include <dbus/dbus.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct rm_monitor rm_monitor;
+
+/* Prototype for a function that is called whenever the reservation
+ * device of a device changes. Use rm_monitor_busy() to find out the
+ * new state.*/
+typedef void (*rm_change_cb_t)(rm_monitor *m);
+
+/* Creates a monitor for watching the lock status of a device. Returns
+ * 0 on success, a negative errno style return value on error. The
+ * DBus error might be set as well if the error was caused D-Bus. */
+int rm_watch(
+ rm_monitor **m, /* On success a pointer to the newly allocated rm_device object will be filled in here */
+ DBusConnection *connection, /* Session bus (when D-Bus learns about user busses we should switchg to user busses) */
+ const char *device_name, /* The device to monitor, e.g. "Audio0" */
+ rm_change_cb_t change_cb, /* Will be called whenever the lock status changes. May be NULL */
+ DBusError *error); /* If we fail due to a D-Bus related issue the error will be filled in here. May be NULL. */
+
+/* Free a rm_monitor object */
+void rm_release(rm_monitor *m);
+
+/* Checks whether the device is currently reserved, and returns 1
+ * then, 0 if not, negative errno style error code value on error. */
+int rm_busy(rm_monitor *m);
+
+/* Attach a userdata pointer to an rm_monitor */
+void rm_set_userdata(rm_monitor *m, void *userdata);
+
+/* Query the userdata pointer from an rm_monitor. Returns NULL if no
+ * userdata was set. */
+void* rm_get_userdata(rm_monitor *m);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/modules/reserve-wrap.c b/src/modules/reserve-wrap.c
new file mode 100644
index 00000000..4be19c73
--- /dev/null
+++ b/src/modules/reserve-wrap.c
@@ -0,0 +1,344 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/i18n.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/shared.h>
+
+#ifdef HAVE_DBUS
+#include <pulsecore/dbus-shared.h>
+#include "reserve.h"
+#include "reserve-monitor.h"
+#endif
+
+#include "reserve-wrap.h"
+
+struct pa_reserve_wrapper {
+ PA_REFCNT_DECLARE;
+ pa_core *core;
+ pa_hook hook;
+ char *shared_name;
+#ifdef HAVE_DBUS
+ pa_dbus_connection *connection;
+ struct rd_device *device;
+#endif
+};
+
+struct pa_reserve_monitor_wrapper {
+ PA_REFCNT_DECLARE;
+ pa_core *core;
+ pa_hook hook;
+ char *shared_name;
+#ifdef HAVE_DBUS
+ pa_dbus_connection *connection;
+ struct rm_monitor *monitor;
+#endif
+};
+
+static void reserve_wrapper_free(pa_reserve_wrapper *r) {
+ pa_assert(r);
+
+#ifdef HAVE_DBUS
+ if (r->device)
+ rd_release(r->device);
+
+ if (r->connection)
+ pa_dbus_connection_unref(r->connection);
+#endif
+
+ pa_hook_done(&r->hook);
+
+ if (r->shared_name) {
+ pa_assert_se(pa_shared_remove(r->core, r->shared_name) >= 0);
+ pa_xfree(r->shared_name);
+ }
+
+ pa_xfree(r);
+}
+
+#ifdef HAVE_DBUS
+static int request_cb(rd_device *d, int forced) {
+ pa_reserve_wrapper *r;
+ int k;
+
+ pa_assert(d);
+ pa_assert_se(r = rd_get_userdata(d));
+ pa_assert(PA_REFCNT_VALUE(r) >= 1);
+
+ PA_REFCNT_INC(r);
+
+ k = pa_hook_fire(&r->hook, PA_INT_TO_PTR(forced));
+ pa_log_debug("Device unlock of %s has been requested and %s.", r->shared_name, k < 0 ? "failed" : "succeeded");
+
+ pa_reserve_wrapper_unref(r);
+
+ return k < 0 ? -1 : 1;
+}
+#endif
+
+pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) {
+ pa_reserve_wrapper *r;
+ int k;
+ char *t;
+#ifdef HAVE_DBUS
+ DBusError error;
+
+ dbus_error_init(&error);
+#endif
+
+ pa_assert(c);
+ pa_assert(device_name);
+
+ t = pa_sprintf_malloc("reserve-wrapper@%s", device_name);
+
+ if ((r = pa_shared_get(c, t))) {
+ pa_xfree(t);
+
+ pa_assert(PA_REFCNT_VALUE(r) >= 1);
+ PA_REFCNT_INC(r);
+
+ return r;
+ }
+
+ r = pa_xnew0(pa_reserve_wrapper, 1);
+ PA_REFCNT_INIT(r);
+ r->core = c;
+ pa_hook_init(&r->hook, r);
+ r->shared_name = t;
+
+ pa_assert_se(pa_shared_set(c, r->shared_name, r) >= 0);
+
+#ifdef HAVE_DBUS
+ if (!(r->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
+ pa_log_debug("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);
+
+ /* We don't treat this as error here because we want allow PA
+ * to run even when no session bus is available. */
+ return r;
+ }
+
+ if ((k = rd_acquire(
+ &r->device,
+ pa_dbus_connection_get(r->connection),
+ device_name,
+ _("PulseAudio Sound Server"),
+ 0,
+ request_cb,
+ NULL)) < 0) {
+
+ if (k == -EBUSY) {
+ pa_log_debug("Device '%s' already locked.", device_name);
+ goto fail;
+ } else {
+ pa_log_debug("Failed to acquire reservation lock on device '%s': %s", device_name, pa_cstrerror(-k));
+ return r;
+ }
+ }
+
+ pa_log_debug("Successfully acquired reservation lock on device '%s'", device_name);
+
+ rd_set_userdata(r->device, r);
+
+ return r;
+fail:
+ dbus_error_free(&error);
+
+ reserve_wrapper_free(r);
+
+ return NULL;
+#else
+ return r;
+#endif
+}
+
+void pa_reserve_wrapper_unref(pa_reserve_wrapper *r) {
+ pa_assert(r);
+ pa_assert(PA_REFCNT_VALUE(r) >= 1);
+
+ if (PA_REFCNT_DEC(r) > 0)
+ return;
+
+ reserve_wrapper_free(r);
+}
+
+pa_hook* pa_reserve_wrapper_hook(pa_reserve_wrapper *r) {
+ pa_assert(r);
+ pa_assert(PA_REFCNT_VALUE(r) >= 1);
+
+ return &r->hook;
+}
+
+void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const char *name) {
+ pa_assert(r);
+ pa_assert(PA_REFCNT_VALUE(r) >= 1);
+
+#ifdef HAVE_DBUS
+ rd_set_application_device_name(r->device, name);
+#endif
+}
+
+static void reserve_monitor_wrapper_free(pa_reserve_monitor_wrapper *w) {
+ pa_assert(w);
+
+#ifdef HAVE_DBUS
+ if (w->monitor)
+ rm_release(w->monitor);
+
+ if (w->connection)
+ pa_dbus_connection_unref(w->connection);
+#endif
+
+ pa_hook_done(&w->hook);
+
+ if (w->shared_name) {
+ pa_assert_se(pa_shared_remove(w->core, w->shared_name) >= 0);
+ pa_xfree(w->shared_name);
+ }
+
+ pa_xfree(w);
+}
+
+#ifdef HAVE_DBUS
+static void change_cb(rm_monitor *m) {
+ pa_reserve_monitor_wrapper *w;
+ int k;
+
+ pa_assert(m);
+ pa_assert_se(w = rm_get_userdata(m));
+ pa_assert(PA_REFCNT_VALUE(w) >= 1);
+
+ PA_REFCNT_INC(w);
+
+ if ((k = rm_busy(w->monitor)) < 0)
+ return;
+
+ pa_hook_fire(&w->hook, PA_INT_TO_PTR(!!k));
+ pa_log_debug("Device lock status of %s changed: %s", w->shared_name, k ? "busy" : "not busy");
+
+ pa_reserve_monitor_wrapper_unref(w);
+}
+#endif
+
+pa_reserve_monitor_wrapper* pa_reserve_monitor_wrapper_get(pa_core *c, const char *device_name) {
+ pa_reserve_monitor_wrapper *w;
+ int k;
+ char *t;
+#ifdef HAVE_DBUS
+ DBusError error;
+
+ dbus_error_init(&error);
+#endif
+
+ pa_assert(c);
+ pa_assert(device_name);
+
+ t = pa_sprintf_malloc("reserve-monitor-wrapper@%s", device_name);
+
+ if ((w = pa_shared_get(c, t))) {
+ pa_xfree(t);
+
+ pa_assert(PA_REFCNT_VALUE(w) >= 1);
+ PA_REFCNT_INC(w);
+
+ return w;
+ }
+
+ w = pa_xnew0(pa_reserve_monitor_wrapper, 1);
+ PA_REFCNT_INIT(w);
+ w->core = c;
+ pa_hook_init(&w->hook, w);
+ w->shared_name = t;
+
+ pa_assert_se(pa_shared_set(c, w->shared_name, w) >= 0);
+
+#ifdef HAVE_DBUS
+ if (!(w->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
+ pa_log_debug("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);
+
+ /* We don't treat this as error here because we want allow PA
+ * to run even when no session bus is available. */
+ return w;
+ }
+
+ if ((k = rm_watch(
+ &w->monitor,
+ pa_dbus_connection_get(w->connection),
+ device_name,
+ change_cb,
+ NULL)) < 0) {
+
+ pa_log_debug("Failed to create watch on device '%s': %s", device_name, pa_cstrerror(-k));
+ goto fail;
+ }
+
+ pa_log_debug("Successfully create reservation lock monitor for device '%s'", device_name);
+
+ rm_set_userdata(w->monitor, w);
+ return w;
+
+fail:
+ dbus_error_free(&error);
+
+ reserve_monitor_wrapper_free(w);
+
+ return NULL;
+#else
+ return w;
+#endif
+}
+
+void pa_reserve_monitor_wrapper_unref(pa_reserve_monitor_wrapper *w) {
+ pa_assert(w);
+ pa_assert(PA_REFCNT_VALUE(w) >= 1);
+
+ if (PA_REFCNT_DEC(w) > 0)
+ return;
+
+ reserve_monitor_wrapper_free(w);
+}
+
+pa_hook* pa_reserve_monitor_wrapper_hook(pa_reserve_monitor_wrapper *w) {
+ pa_assert(w);
+ pa_assert(PA_REFCNT_VALUE(w) >= 1);
+
+ return &w->hook;
+}
+
+pa_bool_t pa_reserve_monitor_wrapper_busy(pa_reserve_monitor_wrapper *w) {
+ pa_assert(w);
+
+ pa_assert(PA_REFCNT_VALUE(w) >= 1);
+
+#ifdef HAVE_DBUS
+ return rm_busy(w->monitor) > 0;
+#else
+ return FALSE;
+#endif
+}
diff --git a/src/modules/reserve-wrap.h b/src/modules/reserve-wrap.h
new file mode 100644
index 00000000..2de6c093
--- /dev/null
+++ b/src/modules/reserve-wrap.h
@@ -0,0 +1,45 @@
+#ifndef fooreservewraphfoo
+#define fooreservewraphfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <pulsecore/core.h>
+#include <pulsecore/hook-list.h>
+
+typedef struct pa_reserve_wrapper pa_reserve_wrapper;
+
+pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name);
+void pa_reserve_wrapper_unref(pa_reserve_wrapper *r);
+
+pa_hook* pa_reserve_wrapper_hook(pa_reserve_wrapper *r);
+
+void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const char *name);
+
+typedef struct pa_reserve_monitor_wrapper pa_reserve_monitor_wrapper;
+
+pa_reserve_monitor_wrapper* pa_reserve_monitor_wrapper_get(pa_core *c, const char *device_name);
+void pa_reserve_monitor_wrapper_unref(pa_reserve_monitor_wrapper *m);
+
+pa_hook* pa_reserve_monitor_wrapper_hook(pa_reserve_monitor_wrapper *m);
+pa_bool_t pa_reserve_monitor_wrapper_busy(pa_reserve_monitor_wrapper *m);
+
+#endif
diff --git a/src/modules/reserve.c b/src/modules/reserve.c
new file mode 100644
index 00000000..b4c168cf
--- /dev/null
+++ b/src/modules/reserve.c
@@ -0,0 +1,608 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/
+
+/***
+ Copyright 2009 Lennart Poettering
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation files
+ (the "Software"), to deal in the Software without restriction,
+ including without limitation the rights to use, copy, modify, merge,
+ publish, distribute, sublicense, and/or sell copies of the Software,
+ and to permit persons to whom the Software is furnished to do so,
+ subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+***/
+
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+
+#include "reserve.h"
+
+struct rd_device {
+ int ref;
+
+ char *device_name;
+ char *application_name;
+ char *application_device_name;
+ char *service_name;
+ char *object_path;
+ int32_t priority;
+
+ DBusConnection *connection;
+
+ unsigned owning:1;
+ unsigned registered:1;
+ unsigned filtering:1;
+ unsigned gave_up:1;
+
+ rd_request_cb_t request_cb;
+ void *userdata;
+};
+
+#define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
+#define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/"
+
+static const char introspection[] =
+ DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
+ "<node>"
+ " <!-- If you are looking for documentation make sure to check out\n"
+ " http://git.0pointer.de/?p=reserve.git;a=blob;f=reserve.txt -->\n"
+ " <interface name=\"org.freedesktop.ReserveDevice1\">"
+ " <method name=\"RequestRelease\">"
+ " <arg name=\"priority\" type=\"i\" direction=\"in\"/>"
+ " <arg name=\"result\" type=\"b\" direction=\"out\"/>"
+ " </method>"
+ " <property name=\"Priority\" type=\"i\" access=\"read\"/>"
+ " <property name=\"ApplicationName\" type=\"s\" access=\"read\"/>"
+ " <property name=\"ApplicationDeviceName\" type=\"s\" access=\"read\"/>"
+ " </interface>"
+ " <interface name=\"org.freedesktop.DBus.Properties\">"
+ " <method name=\"Get\">"
+ " <arg name=\"interface\" direction=\"in\" type=\"s\"/>"
+ " <arg name=\"property\" direction=\"in\" type=\"s\"/>"
+ " <arg name=\"value\" direction=\"out\" type=\"v\"/>"
+ " </method>"
+ " </interface>"
+ " <interface name=\"org.freedesktop.DBus.Introspectable\">"
+ " <method name=\"Introspect\">"
+ " <arg name=\"data\" type=\"s\" direction=\"out\"/>"
+ " </method>"
+ " </interface>"
+ "</node>";
+
+static dbus_bool_t add_variant(
+ DBusMessage *m,
+ int type,
+ const void *data) {
+
+ DBusMessageIter iter, sub;
+ char t[2];
+
+ t[0] = (char) type;
+ t[1] = 0;
+
+ dbus_message_iter_init_append(m, &iter);
+
+ if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, t, &sub))
+ return FALSE;
+
+ if (!dbus_message_iter_append_basic(&sub, type, data))
+ return FALSE;
+
+ if (!dbus_message_iter_close_container(&iter, &sub))
+ return FALSE;
+
+ return TRUE;
+}
+
+static DBusHandlerResult object_handler(
+ DBusConnection *c,
+ DBusMessage *m,
+ void *userdata) {
+
+ rd_device *d;
+ DBusError error;
+ DBusMessage *reply = NULL;
+
+ dbus_error_init(&error);
+
+ d = userdata;
+ assert(d->ref >= 1);
+
+ if (dbus_message_is_method_call(
+ m,
+ "org.freedesktop.ReserveDevice1",
+ "RequestRelease")) {
+
+ int32_t priority;
+ dbus_bool_t ret;
+
+ if (!dbus_message_get_args(
+ m,
+ &error,
+ DBUS_TYPE_INT32, &priority,
+ DBUS_TYPE_INVALID))
+ goto invalid;
+
+ ret = FALSE;
+
+ if (priority > d->priority && d->request_cb) {
+ d->ref++;
+
+ if (d->request_cb(d, 0) > 0) {
+ ret = TRUE;
+ d->gave_up = 1;
+ }
+
+ rd_release(d);
+ }
+
+ if (!(reply = dbus_message_new_method_return(m)))
+ goto oom;
+
+ if (!dbus_message_append_args(
+ reply,
+ DBUS_TYPE_BOOLEAN, &ret,
+ DBUS_TYPE_INVALID))
+ goto oom;
+
+ if (!dbus_connection_send(c, reply, NULL))
+ goto oom;
+
+ dbus_message_unref(reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+ } else if (dbus_message_is_method_call(
+ m,
+ "org.freedesktop.DBus.Properties",
+ "Get")) {
+
+ const char *interface, *property;
+
+ if (!dbus_message_get_args(
+ m,
+ &error,
+ DBUS_TYPE_STRING, &interface,
+ DBUS_TYPE_STRING, &property,
+ DBUS_TYPE_INVALID))
+ goto invalid;
+
+ if (strcmp(interface, "org.freedesktop.ReserveDevice1") == 0) {
+ const char *empty = "";
+
+ if (strcmp(property, "ApplicationName") == 0 && d->application_name) {
+ if (!(reply = dbus_message_new_method_return(m)))
+ goto oom;
+
+ if (!add_variant(
+ reply,
+ DBUS_TYPE_STRING,
+ d->application_name ? (const char**) &d->application_name : &empty))
+ goto oom;
+
+ } else if (strcmp(property, "ApplicationDeviceName") == 0) {
+ if (!(reply = dbus_message_new_method_return(m)))
+ goto oom;
+
+ if (!add_variant(
+ reply,
+ DBUS_TYPE_STRING,
+ d->application_device_name ? (const char**) &d->application_device_name : &empty))
+ goto oom;
+
+ } else if (strcmp(property, "Priority") == 0) {
+ if (!(reply = dbus_message_new_method_return(m)))
+ goto oom;
+
+ if (!add_variant(
+ reply,
+ DBUS_TYPE_INT32,
+ &d->priority))
+ goto oom;
+ } else {
+ if (!(reply = dbus_message_new_error_printf(
+ m,
+ DBUS_ERROR_UNKNOWN_METHOD,
+ "Unknown property %s",
+ property)))
+ goto oom;
+ }
+
+ if (!dbus_connection_send(c, reply, NULL))
+ goto oom;
+
+ dbus_message_unref(reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ } else if (dbus_message_is_method_call(
+ m,
+ "org.freedesktop.DBus.Introspectable",
+ "Introspect")) {
+ const char *i = introspection;
+
+ if (!(reply = dbus_message_new_method_return(m)))
+ goto oom;
+
+ if (!dbus_message_append_args(
+ reply,
+ DBUS_TYPE_STRING,
+ &i,
+ DBUS_TYPE_INVALID))
+ goto oom;
+
+ if (!dbus_connection_send(c, reply, NULL))
+ goto oom;
+
+ dbus_message_unref(reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+invalid:
+ if (reply)
+ dbus_message_unref(reply);
+
+ if (!(reply = dbus_message_new_error(
+ m,
+ DBUS_ERROR_INVALID_ARGS,
+ "Invalid arguments")))
+ goto oom;
+
+ if (!dbus_connection_send(c, reply, NULL))
+ goto oom;
+
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+oom:
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ return DBUS_HANDLER_RESULT_NEED_MEMORY;
+}
+
+static DBusHandlerResult filter_handler(
+ DBusConnection *c,
+ DBusMessage *m,
+ void *userdata) {
+
+ rd_device *d;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ d = userdata;
+ assert(d->ref >= 1);
+
+ if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) {
+ const char *name;
+
+ if (!dbus_message_get_args(
+ m,
+ &error,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INVALID))
+ goto invalid;
+
+ if (strcmp(name, d->service_name) == 0 && d->owning) {
+ d->owning = 0;
+
+ if (!d->gave_up) {
+ d->ref++;
+
+ if (d->request_cb)
+ d->request_cb(d, 1);
+ d->gave_up = 1;
+
+ rd_release(d);
+ }
+
+ }
+ }
+
+invalid:
+ dbus_error_free(&error);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+
+static const struct DBusObjectPathVTable vtable ={
+ .message_function = object_handler
+};
+
+int rd_acquire(
+ rd_device **_d,
+ DBusConnection *connection,
+ const char *device_name,
+ const char *application_name,
+ int32_t priority,
+ rd_request_cb_t request_cb,
+ DBusError *error) {
+
+ rd_device *d = NULL;
+ int r, k;
+ DBusError _error;
+ DBusMessage *m = NULL, *reply = NULL;
+ dbus_bool_t good;
+
+ if (!error)
+ error = &_error;
+
+ dbus_error_init(error);
+
+ if (!_d)
+ return -EINVAL;
+
+ if (!connection)
+ return -EINVAL;
+
+ if (!device_name)
+ return -EINVAL;
+
+ if (!request_cb && priority != INT32_MAX)
+ return -EINVAL;
+
+ if (!(d = calloc(sizeof(rd_device), 1)))
+ return -ENOMEM;
+
+ d->ref = 1;
+
+ if (!(d->device_name = strdup(device_name))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if (!(d->application_name = strdup(application_name))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ d->priority = priority;
+ d->connection = dbus_connection_ref(connection);
+ d->request_cb = request_cb;
+
+ if (!(d->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+ sprintf(d->service_name, SERVICE_PREFIX "%s", d->device_name);
+
+ if (!(d->object_path = malloc(sizeof(OBJECT_PREFIX) + strlen(device_name)))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+ sprintf(d->object_path, OBJECT_PREFIX "%s", d->device_name);
+
+ if ((k = dbus_bus_request_name(
+ d->connection,
+ d->service_name,
+ DBUS_NAME_FLAG_DO_NOT_QUEUE|
+ (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0),
+ error)) < 0) {
+ r = -EIO;
+ goto fail;
+ }
+
+ if (k == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+ goto success;
+
+ if (k != DBUS_REQUEST_NAME_REPLY_EXISTS) {
+ r = -EIO;
+ goto fail;
+ }
+
+ if (priority <= INT32_MIN) {
+ r = -EBUSY;
+ goto fail;
+ }
+
+ if (!(m = dbus_message_new_method_call(
+ d->service_name,
+ d->object_path,
+ "org.freedesktop.ReserveDevice1",
+ "RequestRelease"))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if (!dbus_message_append_args(
+ m,
+ DBUS_TYPE_INT32, &d->priority,
+ DBUS_TYPE_INVALID)) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if (!(reply = dbus_connection_send_with_reply_and_block(
+ d->connection,
+ m,
+ 5000, /* 5s */
+ error))) {
+
+ if (dbus_error_has_name(error, DBUS_ERROR_TIMED_OUT) ||
+ dbus_error_has_name(error, DBUS_ERROR_UNKNOWN_METHOD) ||
+ dbus_error_has_name(error, DBUS_ERROR_NO_REPLY)) {
+ /* This must be treated as denied. */
+ r = -EBUSY;
+ goto fail;
+ }
+
+ r = -EIO;
+ goto fail;
+ }
+
+ if (!dbus_message_get_args(
+ reply,
+ error,
+ DBUS_TYPE_BOOLEAN, &good,
+ DBUS_TYPE_INVALID)) {
+ r = -EIO;
+ goto fail;
+ }
+
+ if (!good) {
+ r = -EBUSY;
+ goto fail;
+ }
+
+ if ((k = dbus_bus_request_name(
+ d->connection,
+ d->service_name,
+ DBUS_NAME_FLAG_DO_NOT_QUEUE|
+ (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0)|
+ DBUS_NAME_FLAG_REPLACE_EXISTING,
+ error)) < 0) {
+ r = -EIO;
+ goto fail;
+ }
+
+ if (k != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
+ r = -EIO;
+ goto fail;
+ }
+
+success:
+ d->owning = 1;
+
+ if (!(dbus_connection_register_object_path(
+ d->connection,
+ d->object_path,
+ &vtable,
+ d))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ d->registered = 1;
+
+ if (!dbus_connection_add_filter(
+ d->connection,
+ filter_handler,
+ d,
+ NULL)) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ d->filtering = 1;
+
+ *_d = d;
+ return 0;
+
+fail:
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ if (&_error == error)
+ dbus_error_free(&_error);
+
+ if (d)
+ rd_release(d);
+
+ return r;
+}
+
+void rd_release(
+ rd_device *d) {
+
+ if (!d)
+ return;
+
+ assert(d->ref > 0);
+
+ if (--d->ref > 0)
+ return;
+
+
+ if (d->filtering)
+ dbus_connection_remove_filter(
+ d->connection,
+ filter_handler,
+ d);
+
+ if (d->registered)
+ dbus_connection_unregister_object_path(
+ d->connection,
+ d->object_path);
+
+ if (d->owning)
+ dbus_bus_release_name(
+ d->connection,
+ d->service_name,
+ NULL);
+
+ free(d->device_name);
+ free(d->application_name);
+ free(d->application_device_name);
+ free(d->service_name);
+ free(d->object_path);
+
+ if (d->connection)
+ dbus_connection_unref(d->connection);
+
+ free(d);
+}
+
+int rd_set_application_device_name(rd_device *d, const char *n) {
+ char *t;
+
+ if (!d)
+ return -EINVAL;
+
+ assert(d->ref > 0);
+
+ if (!(t = strdup(n)))
+ return -ENOMEM;
+
+ free(d->application_device_name);
+ d->application_device_name = t;
+ return 0;
+}
+
+void rd_set_userdata(rd_device *d, void *userdata) {
+
+ if (!d)
+ return;
+
+ assert(d->ref > 0);
+ d->userdata = userdata;
+}
+
+void* rd_get_userdata(rd_device *d) {
+
+ if (!d)
+ return NULL;
+
+ assert(d->ref > 0);
+
+ return d->userdata;
+}
diff --git a/src/modules/reserve.h b/src/modules/reserve.h
new file mode 100644
index 00000000..9ae49cf5
--- /dev/null
+++ b/src/modules/reserve.h
@@ -0,0 +1,79 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/
+
+#ifndef fooreservehfoo
+#define fooreservehfoo
+
+/***
+ Copyright 2009 Lennart Poettering
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation files
+ (the "Software"), to deal in the Software without restriction,
+ including without limitation the rights to use, copy, modify, merge,
+ publish, distribute, sublicense, and/or sell copies of the Software,
+ and to permit persons to whom the Software is furnished to do so,
+ subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+***/
+
+#include <dbus/dbus.h>
+#include <inttypes.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct rd_device rd_device;
+
+/* Prototype for a function that is called whenever someone else wants
+ * your application to release the device it has locked. A return
+ * value <= 0 denies the request, a positive return value agrees to
+ * it. Before returning your application should close the device in
+ * question completely to make sure the new application may access
+ * it. */
+typedef int (*rd_request_cb_t)(
+ rd_device *d,
+ int forced); /* Non-zero if an application forcibly took the lock away without asking. If this is the case then the return value of this call is ignored. */
+
+/* Try to lock the device. Returns 0 on success, a negative errno
+ * style return value on error. The DBus error might be set as well if
+ * the error was caused D-Bus. */
+int rd_acquire(
+ rd_device **d, /* On success a pointer to the newly allocated rd_device object will be filled in here */
+ DBusConnection *connection, /* Session bus (when D-Bus learns about user busses we should switchg to user busses) */
+ const char *device_name, /* The device to lock, e.g. "Audio0" */
+ const char *application_name, /* A human readable name of the application, e.g. "PulseAudio Sound Server" */
+ int32_t priority, /* The priority for this application. If unsure use 0 */
+ rd_request_cb_t request_cb, /* Will be called whenever someone requests that this device shall be released. May be NULL if priority is INT32_MAX */
+ DBusError *error); /* If we fail due to a D-Bus related issue the error will be filled in here. May be NULL. */
+
+/* Unlock (if needed) and destroy an rd_device object again */
+void rd_release(rd_device *d);
+
+/* Set the application device name for an rd_device object. Returns 0
+ * on success, a negative errno style return value on error. */
+int rd_set_application_device_name(rd_device *d, const char *name);
+
+/* Attach a userdata pointer to an rd_device */
+void rd_set_userdata(rd_device *d, void *userdata);
+
+/* Query the userdata pointer from an rd_device. Returns NULL if no
+ * userdata was set. */
+void* rd_get_userdata(rd_device *d);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/modules/rtp/Makefile b/src/modules/rtp/Makefile
deleted file mode 100644
index 316beb72..00000000
--- a/src/modules/rtp/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-# This is a dirty trick just to ease compilation with emacs
-#
-# This file is not intended to be distributed or anything
-#
-# So: don't touch it, even better ignore it!
-
-all:
- $(MAKE) -C ../..
-
-clean:
- $(MAKE) -C ../.. clean
-
-.PHONY: all clean
diff --git a/src/modules/rtp/headerlist.c b/src/modules/rtp/headerlist.c
new file mode 100644
index 00000000..0fef835b
--- /dev/null
+++ b/src/modules/rtp/headerlist.c
@@ -0,0 +1,186 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Colin Guthrie
+ Copyright 2007 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/hashmap.h>
+#include <pulsecore/strbuf.h>
+#include <pulsecore/core-util.h>
+
+#include "headerlist.h"
+
+struct header {
+ char *key;
+ void *value;
+ size_t nbytes;
+};
+
+#define MAKE_HASHMAP(p) ((pa_hashmap*) (p))
+#define MAKE_HEADERLIST(p) ((pa_headerlist*) (p))
+
+static void header_free(struct header *hdr) {
+ pa_assert(hdr);
+
+ pa_xfree(hdr->key);
+ pa_xfree(hdr->value);
+ pa_xfree(hdr);
+}
+
+pa_headerlist* pa_headerlist_new(void) {
+ return MAKE_HEADERLIST(pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func));
+}
+
+void pa_headerlist_free(pa_headerlist* p) {
+ struct header *hdr;
+
+ while ((hdr = pa_hashmap_steal_first(MAKE_HASHMAP(p))))
+ header_free(hdr);
+
+ pa_hashmap_free(MAKE_HASHMAP(p), NULL, NULL);
+}
+
+int pa_headerlist_puts(pa_headerlist *p, const char *key, const char *value) {
+ struct header *hdr;
+ pa_bool_t add = FALSE;
+
+ pa_assert(p);
+ pa_assert(key);
+
+ if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
+ hdr = pa_xnew(struct header, 1);
+ hdr->key = pa_xstrdup(key);
+ add = TRUE;
+ } else
+ pa_xfree(hdr->value);
+
+ hdr->value = pa_xstrdup(value);
+ hdr->nbytes = strlen(value)+1;
+
+ if (add)
+ pa_hashmap_put(MAKE_HASHMAP(p), hdr->key, hdr);
+
+ return 0;
+}
+
+int pa_headerlist_putsappend(pa_headerlist *p, const char *key, const char *value) {
+ struct header *hdr;
+ pa_bool_t add = FALSE;
+
+ pa_assert(p);
+ pa_assert(key);
+
+ if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
+ hdr = pa_xnew(struct header, 1);
+ hdr->key = pa_xstrdup(key);
+ hdr->value = pa_xstrdup(value);
+ add = TRUE;
+ } else {
+ void *newval = pa_sprintf_malloc("%s%s", (char*)hdr->value, value);
+ pa_xfree(hdr->value);
+ hdr->value = newval;
+ }
+ hdr->nbytes = strlen(hdr->value)+1;
+
+ if (add)
+ pa_hashmap_put(MAKE_HASHMAP(p), hdr->key, hdr);
+
+ return 0;
+}
+
+const char *pa_headerlist_gets(pa_headerlist *p, const char *key) {
+ struct header *hdr;
+
+ pa_assert(p);
+ pa_assert(key);
+
+ if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key)))
+ return NULL;
+
+ if (hdr->nbytes <= 0)
+ return NULL;
+
+ if (((char*) hdr->value)[hdr->nbytes-1] != 0)
+ return NULL;
+
+ if (strlen((char*) hdr->value) != hdr->nbytes-1)
+ return NULL;
+
+ return (char*) hdr->value;
+}
+
+int pa_headerlist_remove(pa_headerlist *p, const char *key) {
+ struct header *hdr;
+
+ pa_assert(p);
+ pa_assert(key);
+
+ if (!(hdr = pa_hashmap_remove(MAKE_HASHMAP(p), key)))
+ return -1;
+
+ header_free(hdr);
+ return 0;
+}
+
+const char *pa_headerlist_iterate(pa_headerlist *p, void **state) {
+ struct header *hdr;
+
+ if (!(hdr = pa_hashmap_iterate(MAKE_HASHMAP(p), state, NULL)))
+ return NULL;
+
+ return hdr->key;
+}
+
+char *pa_headerlist_to_string(pa_headerlist *p) {
+ const char *key;
+ void *state = NULL;
+ pa_strbuf *buf;
+
+ pa_assert(p);
+
+ buf = pa_strbuf_new();
+
+ while ((key = pa_headerlist_iterate(p, &state))) {
+
+ const char *v;
+
+ if ((v = pa_headerlist_gets(p, key)))
+ pa_strbuf_printf(buf, "%s: %s\r\n", key, v);
+ }
+
+ return pa_strbuf_tostring_free(buf);
+}
+
+int pa_headerlist_contains(pa_headerlist *p, const char *key) {
+ pa_assert(p);
+ pa_assert(key);
+
+ if (!(pa_hashmap_get(MAKE_HASHMAP(p), key)))
+ return 0;
+
+ return 1;
+}
diff --git a/src/modules/rtp/headerlist.h b/src/modules/rtp/headerlist.h
new file mode 100644
index 00000000..4b9c6433
--- /dev/null
+++ b/src/modules/rtp/headerlist.h
@@ -0,0 +1,46 @@
+#ifndef foopulseheaderlisthfoo
+#define foopulseheaderlisthfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Colin Guthrie
+ Copyright 2007 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <pulsecore/macro.h>
+
+typedef struct pa_headerlist pa_headerlist;
+
+pa_headerlist* pa_headerlist_new(void);
+void pa_headerlist_free(pa_headerlist* p);
+
+int pa_headerlist_puts(pa_headerlist *p, const char *key, const char *value);
+int pa_headerlist_putsappend(pa_headerlist *p, const char *key, const char *value);
+
+const char *pa_headerlist_gets(pa_headerlist *p, const char *key);
+
+int pa_headerlist_remove(pa_headerlist *p, const char *key);
+
+const char *pa_headerlist_iterate(pa_headerlist *p, void **state);
+
+char *pa_headerlist_to_string(pa_headerlist *p);
+
+int pa_headerlist_contains(pa_headerlist *p, const char *key);
+
+#endif
diff --git a/src/modules/rtp/module-rtp-recv.c b/src/modules/rtp/module-rtp-recv.c
index 0359a43b..7025c15a 100644
--- a/src/modules/rtp/module-rtp-recv.c
+++ b/src/modules/rtp/module-rtp-recv.c
@@ -1,17 +1,19 @@
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -22,15 +24,15 @@
#include <config.h>
#endif
-#include <assert.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
-#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
+#include <math.h>
+#include <pulse/rtclock.h>
#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
@@ -41,10 +43,17 @@
#include <pulsecore/sink-input.h>
#include <pulsecore/memblockq.h>
#include <pulsecore/log.h>
+#include <pulsecore/core-rtclock.h>
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/namereg.h>
#include <pulsecore/sample-util.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/socket-util.h>
+#include <pulsecore/atomic.h>
+#include <pulsecore/once.h>
+#include <pulsecore/poll.h>
+#include <pulsecore/arpa-inet.h>
#include "module-rtp-recv-symdef.h"
@@ -52,19 +61,22 @@
#include "sdp.h"
#include "sap.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Recieve data from a network via RTP/SAP/SDP")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Receive data from a network via RTP/SAP/SDP");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
PA_MODULE_USAGE(
"sink=<name of the sink> "
"sap_address=<multicast address to listen on> "
-)
+);
#define SAP_PORT 9875
#define DEFAULT_SAP_ADDRESS "224.0.0.56"
-#define MEMBLOCKQ_MAXLENGTH (1024*170)
+#define MEMBLOCKQ_MAXLENGTH (1024*1024*40)
#define MAX_SESSIONS 16
-#define DEATH_TIMEOUT 20000000
+#define DEATH_TIMEOUT 20
+#define RATE_UPDATE_INTERVAL (5*PA_USEC_PER_SEC)
+#define LATENCY_USEC (500*PA_USEC_PER_MSEC)
static const char* const valid_modargs[] = {
"sink",
@@ -74,20 +86,30 @@ static const char* const valid_modargs[] = {
struct session {
struct userdata *userdata;
+ PA_LLIST_FIELDS(struct session);
pa_sink_input *sink_input;
pa_memblockq *memblockq;
- pa_time_event *death_event;
-
- int first_packet;
+ pa_bool_t first_packet;
uint32_t ssrc;
uint32_t offset;
struct pa_sdp_info sdp_info;
pa_rtp_context rtp_context;
- pa_io_event* rtp_event;
+
+ pa_rtpoll_item *rtpoll_item;
+
+ pa_atomic_t timestamp;
+
+ pa_usec_t intended_latency;
+ pa_usec_t sink_latency;
+
+ pa_usec_t last_rate_update;
+ pa_usec_t last_latency;
+ double estimated_rate;
+ double avg_estimated_rate;
};
struct userdata {
@@ -97,83 +119,135 @@ struct userdata {
pa_sap_context sap_context;
pa_io_event* sap_event;
- pa_hashmap *by_origin;
+ pa_time_event *check_death_event;
char *sink_name;
+ PA_LLIST_HEAD(struct session, sessions);
+ pa_hashmap *by_origin;
int n_sessions;
};
-static void session_free(struct session *s, int from_hash);
+static void session_free(struct session *s);
+
+/* Called from I/O thread context */
+static int sink_input_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct session *s = PA_SINK_INPUT(o)->userdata;
-static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
+ switch (code) {
+ case PA_SINK_INPUT_MESSAGE_GET_LATENCY:
+ *((pa_usec_t*) data) = pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
+
+ /* Fall through, the default handler will add in the extra
+ * latency added by the resampler */
+ break;
+ }
+
+ return pa_sink_input_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from I/O thread context */
+static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
struct session *s;
- assert(i);
- s = i->userdata;
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(s = i->userdata);
+
+ if (pa_memblockq_peek(s->memblockq, chunk) < 0)
+ return -1;
- return pa_memblockq_peek(s->memblockq, chunk);
+ pa_memblockq_drop(s->memblockq, chunk->length);
+
+ return 0;
}
-static void sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
+/* Called from I/O thread context */
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
struct session *s;
- assert(i);
- s = i->userdata;
- pa_memblockq_drop(s->memblockq, chunk, length);
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(s = i->userdata);
+
+ pa_memblockq_rewind(s->memblockq, nbytes);
}
+/* Called from I/O thread context */
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct session *s;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(s = i->userdata);
+
+ pa_memblockq_set_maxrewind(s->memblockq, nbytes);
+}
+
+/* Called from main context */
static void sink_input_kill(pa_sink_input* i) {
struct session *s;
- assert(i);
- s = i->userdata;
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(s = i->userdata);
- session_free(s, 1);
+ session_free(s);
}
-static pa_usec_t sink_input_get_latency(pa_sink_input *i) {
+/* Called from IO context */
+static void sink_input_suspend_within_thread(pa_sink_input* i, pa_bool_t b) {
struct session *s;
- assert(i);
- s = i->userdata;
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(s = i->userdata);
- return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &i->sample_spec);
+ if (b)
+ pa_memblockq_flush_read(s->memblockq);
+ else
+ s->first_packet = FALSE;
}
-static void rtp_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
- struct session *s = userdata;
+/* Called from I/O thread context */
+static int rtpoll_work_cb(pa_rtpoll_item *i) {
pa_memchunk chunk;
int64_t k, j, delta;
- struct timeval tv;
-
- assert(m);
- assert(e);
- assert(s);
- assert(fd == s->rtp_context.fd);
- assert(flags == PA_IO_EVENT_INPUT);
-
- if (pa_rtp_recv(&s->rtp_context, &chunk, s->userdata->core->memblock_stat) < 0)
- return;
+ struct timeval now = { 0, 0 };
+ struct session *s;
+ struct pollfd *p;
+
+ pa_assert_se(s = pa_rtpoll_item_get_userdata(i));
+
+ p = pa_rtpoll_item_get_pollfd(i, NULL);
+
+ if (p->revents & (POLLERR|POLLNVAL|POLLHUP|POLLOUT)) {
+ pa_log("poll() signalled bad revents.");
+ return -1;
+ }
+
+ if ((p->revents & POLLIN) == 0)
+ return 0;
+
+ p->revents = 0;
- if (s->sdp_info.payload != s->rtp_context.payload) {
+ if (pa_rtp_recv(&s->rtp_context, &chunk, s->userdata->module->core->mempool, &now) < 0)
+ return 0;
+
+ if (s->sdp_info.payload != s->rtp_context.payload ||
+ !PA_SINK_IS_OPENED(s->sink_input->sink->thread_info.state)) {
pa_memblock_unref(chunk.memblock);
- return;
+ return 0;
}
-
+
if (!s->first_packet) {
- s->first_packet = 1;
+ s->first_packet = TRUE;
s->ssrc = s->rtp_context.ssrc;
s->offset = s->rtp_context.timestamp;
- if (s->ssrc == s->userdata->core->cookie)
- pa_log_warn(__FILE__": WARNING! Detected RTP packet loop!");
+ if (s->ssrc == s->userdata->module->core->cookie)
+ pa_log_warn("Detected RTP packet loop!");
} else {
if (s->ssrc != s->rtp_context.ssrc) {
pa_memblock_unref(chunk.memblock);
- return;
+ return 0;
}
}
- /* Check wheter there was a timestamp overflow */
+ /* Check whether there was a timestamp overflow */
k = (int64_t) s->rtp_context.timestamp - (int64_t) s->offset;
j = (int64_t) 0x100000000LL - (int64_t) s->offset + (int64_t) s->rtp_context.timestamp;
@@ -181,76 +255,217 @@ static void rtp_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event
delta = k;
else
delta = j;
-
- pa_memblockq_seek(s->memblockq, delta * s->rtp_context.frame_size, PA_SEEK_RELATIVE);
+
+ pa_memblockq_seek(s->memblockq, delta * (int64_t) s->rtp_context.frame_size, PA_SEEK_RELATIVE, TRUE);
+
+ if (now.tv_sec == 0) {
+ PA_ONCE_BEGIN {
+ pa_log_warn("Using artificial time instead of timestamp");
+ } PA_ONCE_END;
+ pa_rtclock_get(&now);
+ } else
+ pa_rtclock_from_wallclock(&now);
if (pa_memblockq_push(s->memblockq, &chunk) < 0) {
- /* queue overflow, let's flush it and try again */
- pa_memblockq_flush(s->memblockq);
- pa_memblockq_push(s->memblockq, &chunk);
+ pa_log_warn("Queue overrun");
+ pa_memblockq_seek(s->memblockq, (int64_t) chunk.length, PA_SEEK_RELATIVE, TRUE);
}
-
- /* The next timestamp we expect */
- s->offset = s->rtp_context.timestamp + (chunk.length / s->rtp_context.frame_size);
-
+
+/* pa_log("blocks in q: %u", pa_memblockq_get_nblocks(s->memblockq)); */
+
pa_memblock_unref(chunk.memblock);
- /* Reset death timer */
- pa_gettimeofday(&tv);
- pa_timeval_add(&tv, DEATH_TIMEOUT);
- m->time_restart(s->death_event, &tv);
+ /* The next timestamp we expect */
+ s->offset = s->rtp_context.timestamp + (uint32_t) (chunk.length / s->rtp_context.frame_size);
+
+ pa_atomic_store(&s->timestamp, (int) now.tv_sec);
+
+ if (s->last_rate_update + RATE_UPDATE_INTERVAL < pa_timeval_load(&now)) {
+ pa_usec_t wi, ri, render_delay, sink_delay = 0, latency;
+ uint32_t base_rate = s->sink_input->sink->sample_spec.rate;
+ uint32_t current_rate = s->sink_input->sample_spec.rate;
+ uint32_t new_rate;
+ double estimated_rate, alpha = 0.02;
+
+ pa_log_debug("Updating sample rate");
+
+ wi = pa_bytes_to_usec((uint64_t) pa_memblockq_get_write_index(s->memblockq), &s->sink_input->sample_spec);
+ ri = pa_bytes_to_usec((uint64_t) pa_memblockq_get_read_index(s->memblockq), &s->sink_input->sample_spec);
+
+ pa_log_debug("wi=%lu ri=%lu", (unsigned long) wi, (unsigned long) ri);
+
+ sink_delay = pa_sink_get_latency_within_thread(s->sink_input->sink);
+ render_delay = pa_bytes_to_usec(pa_memblockq_get_length(s->sink_input->thread_info.render_memblockq), &s->sink_input->sink->sample_spec);
+
+ if (ri > render_delay+sink_delay)
+ ri -= render_delay+sink_delay;
+ else
+ ri = 0;
+
+ if (wi < ri)
+ latency = 0;
+ else
+ latency = wi - ri;
+
+ pa_log_debug("Write index deviates by %0.2f ms, expected %0.2f ms", (double) latency/PA_USEC_PER_MSEC, (double) s->intended_latency/PA_USEC_PER_MSEC);
+
+ /* The buffer is filling with some unknown rate R̂ samples/second. If the rate of reading in
+ * the last T seconds was Rⁿ, then the increase in buffer latency ΔLⁿ = Lⁿ - Lⁿ⁻ⁱ in that
+ * same period is ΔLⁿ = (TR̂ - TRⁿ) / R̂, giving the estimated target rate
+ * T
+ * R̂ = ─────────────── Rⁿ . (1)
+ * T - (Lⁿ - Lⁿ⁻ⁱ)
+ *
+ * Setting the sample rate to R̂ results in the latency being constant (if the estimate of R̂
+ * is correct). But there is also the requirement to keep the buffer at a predefined target
+ * latency L̂. So instead of setting Rⁿ⁺ⁱ to R̂ immediately, the strategy will be to reduce R
+ * from Rⁿ⁺ⁱ to R̂ in a steps of T seconds, where Rⁿ⁺ⁱ is chosen such that in the total time
+ * aT the latency is reduced from Lⁿ to L̂. This strategy translates to the requirements
+ * ₐ R̂ - Rⁿ⁺ʲ a-j+1 j-1
+ * Σ T ────────── = L̂ - Lⁿ with Rⁿ⁺ʲ = ───── Rⁿ⁺ⁱ + ───── R̂ .
+ * ʲ⁼ⁱ R̂ a a
+ * Solving for Rⁿ⁺ⁱ gives
+ * T - ²∕ₐ₊₁(L̂ - Lⁿ)
+ * Rⁿ⁺ⁱ = ───────────────── R̂ . (2)
+ * T
+ * In the code below a = 7 is used.
+ *
+ * Equation (1) is not directly used in (2), but instead an exponentially weighted average
+ * of the estimated rate R̂ is used. This average R̅ is defined as
+ * R̅ⁿ = α R̂ⁿ + (1-α) R̅ⁿ⁻ⁱ .
+ * Because it is difficult to find a fixed value for the coefficient α such that the
+ * averaging is without significant lag but oscillations are filtered out, a heuristic is
+ * used. When the successive estimates R̂ⁿ do not change much then α→1, but when there is a
+ * sudden spike in the estimated rate α→0, such that the deviation is given little weight.
+ */
+ estimated_rate = (double) current_rate * (double) RATE_UPDATE_INTERVAL / (double) (RATE_UPDATE_INTERVAL + s->last_latency - latency);
+ if (fabs(s->estimated_rate - s->avg_estimated_rate) > 1) {
+ double ratio = (estimated_rate + s->estimated_rate - 2*s->avg_estimated_rate) / (s->estimated_rate - s->avg_estimated_rate);
+ alpha = PA_CLAMP(2 * (ratio + fabs(ratio)) / (4 + ratio*ratio), 0.02, 0.8);
+ }
+ s->avg_estimated_rate = alpha * estimated_rate + (1-alpha) * s->avg_estimated_rate;
+ s->estimated_rate = estimated_rate;
+ pa_log_debug("Estimated target rate: %.0f Hz, using average of %.0f Hz (α=%.3f)", estimated_rate, s->avg_estimated_rate, alpha);
+ new_rate = (uint32_t) ((double) (RATE_UPDATE_INTERVAL + latency/4 - s->intended_latency/4) / (double) RATE_UPDATE_INTERVAL * s->avg_estimated_rate);
+ s->last_latency = latency;
+
+ if (new_rate < (uint32_t) (base_rate*0.8) || new_rate > (uint32_t) (base_rate*1.25)) {
+ pa_log_warn("Sample rates too different, not adjusting (%u vs. %u).", base_rate, new_rate);
+ new_rate = base_rate;
+ } else {
+ if (base_rate < new_rate + 20 && new_rate < base_rate + 20)
+ new_rate = base_rate;
+ /* Do the adjustment in small steps; 2‰ can be considered inaudible */
+ if (new_rate < (uint32_t) (current_rate*0.998) || new_rate > (uint32_t) (current_rate*1.002)) {
+ pa_log_info("New rate of %u Hz not within 2‰ of %u Hz, forcing smaller adjustment", new_rate, current_rate);
+ new_rate = PA_CLAMP(new_rate, (uint32_t) (current_rate*0.998), (uint32_t) (current_rate*1.002));
+ }
+ }
+ s->sink_input->sample_spec.rate = new_rate;
+
+ pa_assert(pa_sample_spec_valid(&s->sink_input->sample_spec));
+
+ pa_resampler_set_input_rate(s->sink_input->thread_info.resampler, s->sink_input->sample_spec.rate);
+
+ pa_log_debug("Updated sampling rate to %lu Hz.", (unsigned long) s->sink_input->sample_spec.rate);
+
+ s->last_rate_update = pa_timeval_load(&now);
+ }
+
+ if (pa_memblockq_is_readable(s->memblockq) &&
+ s->sink_input->thread_info.underrun_for > 0) {
+ pa_log_debug("Requesting rewind due to end of underrun");
+ pa_sink_input_request_rewind(s->sink_input,
+ (size_t) (s->sink_input->thread_info.underrun_for == (uint64_t) -1 ? 0 : s->sink_input->thread_info.underrun_for),
+ FALSE, TRUE, FALSE);
+ }
+
+ return 1;
}
-static void death_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
- struct session *s = userdata;
-
- assert(m);
- assert(t);
- assert(tv);
- assert(s);
+/* Called from I/O thread context */
+static void sink_input_attach(pa_sink_input *i) {
+ struct session *s;
+ struct pollfd *p;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(s = i->userdata);
- session_free(s, 1);
+ pa_assert(!s->rtpoll_item);
+ s->rtpoll_item = pa_rtpoll_item_new(i->sink->thread_info.rtpoll, PA_RTPOLL_LATE, 1);
+
+ p = pa_rtpoll_item_get_pollfd(s->rtpoll_item, NULL);
+ p->fd = s->rtp_context.fd;
+ p->events = POLLIN;
+ p->revents = 0;
+
+ pa_rtpoll_item_set_work_callback(s->rtpoll_item, rtpoll_work_cb);
+ pa_rtpoll_item_set_userdata(s->rtpoll_item, s);
+}
+
+/* Called from I/O thread context */
+static void sink_input_detach(pa_sink_input *i) {
+ struct session *s;
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(s = i->userdata);
+
+ pa_assert(s->rtpoll_item);
+ pa_rtpoll_item_free(s->rtpoll_item);
+ s->rtpoll_item = NULL;
}
static int mcast_socket(const struct sockaddr* sa, socklen_t salen) {
int af, fd = -1, r, one;
-
+
+ pa_assert(sa);
+ pa_assert(salen > 0);
+
af = sa->sa_family;
- if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
- pa_log(__FILE__": Failed to create socket: %s", pa_cstrerror(errno));
+ if ((fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) {
+ pa_log("Failed to create socket: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ pa_make_udp_socket_low_delay(fd);
+
+ one = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one)) < 0) {
+ pa_log("SO_TIMESTAMP failed: %s", pa_cstrerror(errno));
goto fail;
}
one = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
- pa_log(__FILE__": SO_REUSEADDR failed: %s", pa_cstrerror(errno));
+ pa_log("SO_REUSEADDR failed: %s", pa_cstrerror(errno));
goto fail;
}
-
+
if (af == AF_INET) {
struct ip_mreq mr4;
memset(&mr4, 0, sizeof(mr4));
mr4.imr_multiaddr = ((const struct sockaddr_in*) sa)->sin_addr;
r = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr4, sizeof(mr4));
+#ifdef HAVE_IPV6
} else {
struct ipv6_mreq mr6;
memset(&mr6, 0, sizeof(mr6));
mr6.ipv6mr_multiaddr = ((const struct sockaddr_in6*) sa)->sin6_addr;
r = setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mr6, sizeof(mr6));
+#endif
}
if (r < 0) {
- pa_log_info(__FILE__": Joining mcast group failed: %s", pa_cstrerror(errno));
+ pa_log_info("Joining mcast group failed: %s", pa_cstrerror(errno));
goto fail;
}
-
+
if (bind(fd, sa, salen) < 0) {
- pa_log(__FILE__": bind() failed: %s", pa_cstrerror(errno));
+ pa_log("bind() failed: %s", pa_cstrerror(errno));
goto fail;
}
return fd;
-
+
fail:
if (fd >= 0)
close(fd);
@@ -260,130 +475,150 @@ fail:
static struct session *session_new(struct userdata *u, const pa_sdp_info *sdp_info) {
struct session *s = NULL;
- struct timeval tv;
- char *c;
pa_sink *sink;
int fd = -1;
- pa_memblock *silence;
+ pa_memchunk silence;
+ pa_sink_input_new_data data;
+ struct timeval now;
+
+ pa_assert(u);
+ pa_assert(sdp_info);
if (u->n_sessions >= MAX_SESSIONS) {
- pa_log(__FILE__": session limit reached.");
+ pa_log("Session limit reached.");
goto fail;
}
-
- if (!(sink = pa_namereg_get(u->core, u->sink_name, PA_NAMEREG_SINK, 1))) {
- pa_log(__FILE__": sink does not exist.");
+
+ if (!(sink = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK))) {
+ pa_log("Sink does not exist.");
goto fail;
}
+ pa_rtclock_get(&now);
+
s = pa_xnew0(struct session, 1);
s->userdata = u;
- s->first_packet = 0;
+ s->first_packet = FALSE;
s->sdp_info = *sdp_info;
+ s->rtpoll_item = NULL;
+ s->intended_latency = LATENCY_USEC;
+ s->last_rate_update = pa_timeval_load(&now);
+ s->last_latency = LATENCY_USEC;
+ s->estimated_rate = (double) sink->sample_spec.rate;
+ s->avg_estimated_rate = (double) sink->sample_spec.rate;
+ pa_atomic_store(&s->timestamp, (int) now.tv_sec);
if ((fd = mcast_socket((const struct sockaddr*) &sdp_info->sa, sdp_info->salen)) < 0)
goto fail;
- c = pa_sprintf_malloc("RTP Stream%s%s%s",
- sdp_info->session_name ? " (" : "",
- sdp_info->session_name ? sdp_info->session_name : "",
- sdp_info->session_name ? ")" : "");
+ pa_sink_input_new_data_init(&data);
+ pa_sink_input_new_data_set_sink(&data, sink, FALSE);
+ data.driver = __FILE__;
+ pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "stream");
+ pa_proplist_setf(data.proplist, PA_PROP_MEDIA_NAME,
+ "RTP Stream%s%s%s",
+ sdp_info->session_name ? " (" : "",
+ sdp_info->session_name ? sdp_info->session_name : "",
+ sdp_info->session_name ? ")" : "");
+
+ if (sdp_info->session_name)
+ pa_proplist_sets(data.proplist, "rtp.session", sdp_info->session_name);
+ pa_proplist_sets(data.proplist, "rtp.origin", sdp_info->origin);
+ pa_proplist_setf(data.proplist, "rtp.payload", "%u", (unsigned) sdp_info->payload);
+ data.module = u->module;
+ pa_sink_input_new_data_set_sample_spec(&data, &sdp_info->sample_spec);
+ data.flags = PA_SINK_INPUT_VARIABLE_RATE;
+
+ pa_sink_input_new(&s->sink_input, u->module->core, &data);
+ pa_sink_input_new_data_done(&data);
- s->sink_input = pa_sink_input_new(sink, __FILE__, c, &sdp_info->sample_spec, NULL, NULL, 0, PA_RESAMPLER_INVALID);
- pa_xfree(c);
-
if (!s->sink_input) {
- pa_log(__FILE__": failed to create sink input.");
+ pa_log("Failed to create sink input.");
goto fail;
}
s->sink_input->userdata = s;
- s->sink_input->owner = u->module;
- s->sink_input->peek = sink_input_peek;
- s->sink_input->drop = sink_input_drop;
+ s->sink_input->parent.process_msg = sink_input_process_msg;
+ s->sink_input->pop = sink_input_pop_cb;
+ s->sink_input->process_rewind = sink_input_process_rewind_cb;
+ s->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
s->sink_input->kill = sink_input_kill;
- s->sink_input->get_latency = sink_input_get_latency;
+ s->sink_input->attach = sink_input_attach;
+ s->sink_input->detach = sink_input_detach;
+ s->sink_input->suspend_within_thread = sink_input_suspend_within_thread;
+
+ pa_sink_input_get_silence(s->sink_input, &silence);
+
+ s->sink_latency = pa_sink_input_set_requested_latency(s->sink_input, s->intended_latency/2);
+
+ if (s->intended_latency < s->sink_latency*2)
+ s->intended_latency = s->sink_latency*2;
- silence = pa_silence_memblock_new(&s->sink_input->sample_spec,
- (pa_bytes_per_second(&s->sink_input->sample_spec)/128/pa_frame_size(&s->sink_input->sample_spec))*
- pa_frame_size(&s->sink_input->sample_spec),
- s->userdata->core->memblock_stat);
-
s->memblockq = pa_memblockq_new(
0,
MEMBLOCKQ_MAXLENGTH,
MEMBLOCKQ_MAXLENGTH,
pa_frame_size(&s->sink_input->sample_spec),
- pa_bytes_per_second(&s->sink_input->sample_spec)/10+1,
+ pa_usec_to_bytes(s->intended_latency - s->sink_latency, &s->sink_input->sample_spec),
0,
- silence,
- u->core->memblock_stat);
+ 0,
+ &silence);
- pa_memblock_unref(silence);
+ pa_memblock_unref(silence.memblock);
- s->rtp_event = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, rtp_event_cb, s);
-
- pa_gettimeofday(&tv);
- pa_timeval_add(&tv, DEATH_TIMEOUT);
- s->death_event = u->core->mainloop->time_new(u->core->mainloop, &tv, death_event_cb, s);
+ pa_rtp_context_init_recv(&s->rtp_context, fd, pa_frame_size(&s->sdp_info.sample_spec));
pa_hashmap_put(s->userdata->by_origin, s->sdp_info.origin, s);
+ u->n_sessions++;
+ PA_LLIST_PREPEND(struct session, s->userdata->sessions, s);
- pa_rtp_context_init_recv(&s->rtp_context, fd, pa_frame_size(&s->sdp_info.sample_spec));
+ pa_sink_input_put(s->sink_input);
- pa_log_info(__FILE__": Found new session '%s'", s->sdp_info.session_name);
+ pa_log_info("New session '%s'", s->sdp_info.session_name);
- u->n_sessions++;
-
return s;
fail:
- if (s) {
- if (fd >= 0)
- close(fd);
-
- pa_xfree(s);
- }
+ pa_xfree(s);
+
+ if (fd >= 0)
+ pa_close(fd);
return NULL;
}
-static void session_free(struct session *s, int from_hash) {
- assert(s);
-
- pa_log_info(__FILE__": Freeing session '%s'", s->sdp_info.session_name);
-
- s->userdata->core->mainloop->time_free(s->death_event);
- s->userdata->core->mainloop->io_free(s->rtp_event);
+static void session_free(struct session *s) {
+ pa_assert(s);
- if (from_hash)
- pa_hashmap_remove(s->userdata->by_origin, s->sdp_info.origin);
+ pa_log_info("Freeing session '%s'", s->sdp_info.session_name);
- pa_sink_input_disconnect(s->sink_input);
+ pa_sink_input_unlink(s->sink_input);
pa_sink_input_unref(s->sink_input);
+ PA_LLIST_REMOVE(struct session, s->userdata->sessions, s);
+ pa_assert(s->userdata->n_sessions >= 1);
+ s->userdata->n_sessions--;
+ pa_hashmap_remove(s->userdata->by_origin, s->sdp_info.origin);
+
pa_memblockq_free(s->memblockq);
pa_sdp_info_destroy(&s->sdp_info);
pa_rtp_context_destroy(&s->rtp_context);
- assert(s->userdata->n_sessions >= 1);
- s->userdata->n_sessions--;
-
pa_xfree(s);
}
static void sap_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
struct userdata *u = userdata;
- int goodbye;
+ pa_bool_t goodbye = FALSE;
pa_sdp_info info;
struct session *s;
-
- assert(m);
- assert(e);
- assert(u);
- assert(fd == u->sap_context.fd);
- assert(flags == PA_IO_EVENT_INPUT);
+
+ pa_assert(m);
+ pa_assert(e);
+ pa_assert(u);
+ pa_assert(fd == u->sap_context.fd);
+ pa_assert(flags == PA_IO_EVENT_INPUT);
if (pa_sap_recv(&u->sap_context, &goodbye) < 0)
return;
@@ -394,78 +629,107 @@ static void sap_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event
if (goodbye) {
if ((s = pa_hashmap_get(u->by_origin, info.origin)))
- session_free(s, 1);
+ session_free(s);
pa_sdp_info_destroy(&info);
} else {
if (!(s = pa_hashmap_get(u->by_origin, info.origin))) {
- if (!(s = session_new(u, &info)))
+ if (!session_new(u, &info))
pa_sdp_info_destroy(&info);
-
+
} else {
- struct timeval tv;
-
- pa_gettimeofday(&tv);
- pa_timeval_add(&tv, DEATH_TIMEOUT);
- m->time_restart(s->death_event, &tv);
-
+ struct timeval now;
+ pa_rtclock_get(&now);
+ pa_atomic_store(&s->timestamp, (int) now.tv_sec);
+
pa_sdp_info_destroy(&info);
}
}
}
-int pa__init(pa_core *c, pa_module*m) {
+static void check_death_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
+ struct session *s, *n;
+ struct userdata *u = userdata;
+ struct timeval now;
+
+ pa_assert(m);
+ pa_assert(t);
+ pa_assert(u);
+
+ pa_rtclock_get(&now);
+
+ pa_log_debug("Checking for dead streams ...");
+
+ for (s = u->sessions; s; s = n) {
+ int k;
+ n = s->next;
+
+ k = pa_atomic_load(&s->timestamp);
+
+ if (k + DEATH_TIMEOUT < now.tv_sec)
+ session_free(s);
+ }
+
+ /* Restart timer */
+ pa_core_rttime_restart(u->module->core, t, pa_rtclock_now() + DEATH_TIMEOUT * PA_USEC_PER_SEC);
+}
+
+int pa__init(pa_module*m) {
struct userdata *u;
pa_modargs *ma = NULL;
struct sockaddr_in sa4;
+#ifdef HAVE_IPV6
struct sockaddr_in6 sa6;
+#endif
struct sockaddr *sa;
socklen_t salen;
const char *sap_address;
int fd = -1;
-
- assert(c);
- assert(m);
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
+ pa_log("failed to parse module arguments");
goto fail;
}
sap_address = pa_modargs_get_value(ma, "sap_address", DEFAULT_SAP_ADDRESS);
-
- if (inet_pton(AF_INET6, sap_address, &sa6.sin6_addr) > 0) {
- sa6.sin6_family = AF_INET6;
- sa6.sin6_port = htons(SAP_PORT);
- sa = (struct sockaddr*) &sa6;
- salen = sizeof(sa6);
- } else if (inet_pton(AF_INET, sap_address, &sa4.sin_addr) > 0) {
+
+ if (inet_pton(AF_INET, sap_address, &sa4.sin_addr) > 0) {
sa4.sin_family = AF_INET;
sa4.sin_port = htons(SAP_PORT);
sa = (struct sockaddr*) &sa4;
salen = sizeof(sa4);
+#ifdef HAVE_IPV6
+ } else if (inet_pton(AF_INET6, sap_address, &sa6.sin6_addr) > 0) {
+ sa6.sin6_family = AF_INET6;
+ sa6.sin6_port = htons(SAP_PORT);
+ sa = (struct sockaddr*) &sa6;
+ salen = sizeof(sa6);
+#endif
} else {
- pa_log(__FILE__": invalid SAP address '%s'", sap_address);
+ pa_log("Invalid SAP address '%s'", sap_address);
goto fail;
}
if ((fd = mcast_socket(sa, salen)) < 0)
goto fail;
- u = pa_xnew(struct userdata, 1);
- m->userdata = u;
+ m->userdata = u = pa_xnew(struct userdata, 1);
u->module = m;
- u->core = c;
+ u->core = m->core;
u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
- u->n_sessions = 0;
- u->sap_event = c->mainloop->io_new(c->mainloop, fd, PA_IO_EVENT_INPUT, sap_event_cb, u);
+ u->sap_event = m->core->mainloop->io_new(m->core->mainloop, fd, PA_IO_EVENT_INPUT, sap_event_cb, u);
+ pa_sap_context_init_recv(&u->sap_context, fd);
+ PA_LLIST_HEAD_INIT(struct session, u->sessions);
+ u->n_sessions = 0;
u->by_origin = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
-
- pa_sap_context_init_recv(&u->sap_context, fd);
-
+
+ u->check_death_event = pa_core_rttime_new(m->core, pa_rtclock_now() + DEATH_TIMEOUT * PA_USEC_PER_SEC, check_death_event_cb, u);
+
pa_modargs_free(ma);
return 0;
@@ -475,28 +739,35 @@ fail:
pa_modargs_free(ma);
if (fd >= 0)
- close(fd);
-
- return -1;
-}
+ pa_close(fd);
-static void free_func(void *p, PA_GCC_UNUSED void *userdata) {
- session_free(p, 0);
+ return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata *u;
- assert(c);
- assert(m);
+ struct session *s;
+
+ pa_assert(m);
if (!(u = m->userdata))
return;
- c->mainloop->io_free(u->sap_event);
+ if (u->sap_event)
+ m->core->mainloop->io_free(u->sap_event);
+
+ if (u->check_death_event)
+ m->core->mainloop->time_free(u->check_death_event);
+
pa_sap_context_destroy(&u->sap_context);
- pa_hashmap_free(u->by_origin, free_func, NULL);
-
+ if (u->by_origin) {
+ while ((s = pa_hashmap_first(u->by_origin)))
+ session_free(s);
+
+ pa_hashmap_free(u->by_origin, NULL, NULL);
+ }
+
pa_xfree(u->sink_name);
pa_xfree(u);
}
diff --git a/src/modules/rtp/module-rtp-send.c b/src/modules/rtp/module-rtp-send.c
index 5c47047f..7131629c 100644
--- a/src/modules/rtp/module-rtp-send.c
+++ b/src/modules/rtp/module-rtp-send.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -23,22 +23,19 @@
#include <config.h>
#endif
-#include <assert.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
-#include <arpa/inet.h>
#include <errno.h>
-#include <string.h>
#include <unistd.h>
+#include <pulse/rtclock.h>
#include <pulse/timeval.h>
#include <pulse/util.h>
#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h>
#include <pulsecore/module.h>
-#include <pulsecore/llist.h>
#include <pulsecore/source.h>
#include <pulsecore/source-output.h>
#include <pulsecore/memblockq.h>
@@ -46,6 +43,10 @@
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/namereg.h>
+#include <pulsecore/sample-util.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/socket-util.h>
+#include <pulsecore/arpa-inet.h>
#include "module-rtp-send-symdef.h"
@@ -53,9 +54,10 @@
#include "sdp.h"
#include "sap.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP/SAP/SDP")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP/SAP/SDP");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
PA_MODULE_USAGE(
"source=<name of the source> "
"format=<sample format> "
@@ -64,15 +66,17 @@ PA_MODULE_USAGE(
"destination=<destination IP address> "
"port=<port number> "
"mtu=<maximum transfer unit> "
- "loop=<loopback to local host?>"
-)
+ "loop=<loopback to local host?> "
+ "ttl=<ttl value>"
+);
#define DEFAULT_PORT 46000
+#define DEFAULT_TTL 1
#define SAP_PORT 9875
#define DEFAULT_DESTINATION "224.0.0.56"
#define MEMBLOCKQ_MAXLENGTH (1024*170)
#define DEFAULT_MTU 1280
-#define SAP_INTERVAL 5000000
+#define SAP_INTERVAL (5*PA_USEC_PER_SEC)
static const char* const valid_modargs[] = {
"source",
@@ -81,13 +85,14 @@ static const char* const valid_modargs[] = {
"rate",
"destination",
"port",
+ "mtu" ,
"loop",
+ "ttl",
NULL
};
struct userdata {
pa_module *module;
- pa_core *core;
pa_source_output *source_output;
pa_memblockq *memblockq;
@@ -99,91 +104,101 @@ struct userdata {
pa_time_event *sap_event;
};
+/* Called from I/O thread context */
+static int source_output_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+ struct userdata *u;
+ pa_assert_se(u = PA_SOURCE_OUTPUT(o)->userdata);
+
+ switch (code) {
+ case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY:
+ *((pa_usec_t*) data) = pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &u->source_output->sample_spec);
+
+ /* Fall through, the default handler will add in the extra
+ * latency added by the resampler */
+ break;
+ }
+
+ return pa_source_output_process_msg(o, code, data, offset, chunk);
+}
+
+/* Called from I/O thread context */
static void source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
struct userdata *u;
- assert(o);
- u = o->userdata;
+ pa_source_output_assert_ref(o);
+ pa_assert_se(u = o->userdata);
if (pa_memblockq_push(u->memblockq, chunk) < 0) {
- pa_log(__FILE__": Failed to push chunk into memblockq.");
+ pa_log_warn("Failed to push chunk into memblockq.");
return;
}
-
+
pa_rtp_send(&u->rtp_context, u->mtu, u->memblockq);
}
+/* Called from main context */
static void source_output_kill(pa_source_output* o) {
struct userdata *u;
- assert(o);
- u = o->userdata;
+ pa_source_output_assert_ref(o);
+ pa_assert_se(u = o->userdata);
- pa_module_unload_request(u->module);
+ pa_module_unload_request(u->module, TRUE);
- pa_source_output_disconnect(u->source_output);
+ pa_source_output_unlink(u->source_output);
pa_source_output_unref(u->source_output);
u->source_output = NULL;
}
-static pa_usec_t source_output_get_latency (pa_source_output *o) {
- struct userdata *u;
- assert(o);
- u = o->userdata;
-
- return pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &o->sample_spec);
-}
-
static void sap_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
struct userdata *u = userdata;
- struct timeval next;
-
- assert(m);
- assert(t);
- assert(tv);
- assert(u);
+
+ pa_assert(m);
+ pa_assert(t);
+ pa_assert(u);
pa_sap_send(&u->sap_context, 0);
- pa_gettimeofday(&next);
- pa_timeval_add(&next, SAP_INTERVAL);
- m->time_restart(t, &next);
+ pa_core_rttime_restart(u->module->core, t, pa_rtclock_now() + SAP_INTERVAL);
}
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
struct userdata *u;
pa_modargs *ma = NULL;
const char *dest;
uint32_t port = DEFAULT_PORT, mtu;
- int af, fd = -1, sap_fd = -1;
+ uint32_t ttl = DEFAULT_TTL;
+ sa_family_t af;
+ int fd = -1, sap_fd = -1;
pa_source *s;
pa_sample_spec ss;
pa_channel_map cm;
struct sockaddr_in sa4, sap_sa4;
+#ifdef HAVE_IPV6
struct sockaddr_in6 sa6, sap_sa6;
+#endif
struct sockaddr_storage sa_dst;
pa_source_output *o = NULL;
uint8_t payload;
char *p;
- int r;
+ int r, j;
socklen_t k;
- struct timeval tv;
char hn[128], *n;
- int loop = 0;
-
- assert(c);
- assert(m);
+ pa_bool_t loop = FALSE;
+ pa_source_output_new_data data;
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto fail;
}
- if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE, 1))) {
- pa_log(__FILE__": source does not exist.");
+ if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE))) {
+ pa_log("Source does not exist.");
goto fail;
}
if (pa_modargs_get_value_boolean(ma, "loop", &loop) < 0) {
- pa_log(__FILE__": failed to parse \"loop\" parameter.");
+ pa_log("Failed to parse \"loop\" parameter.");
goto fail;
}
@@ -191,12 +206,12 @@ int pa__init(pa_core *c, pa_module*m) {
pa_rtp_sample_spec_fixup(&ss);
cm = s->channel_map;
if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
- pa_log(__FILE__": failed to parse sample specification");
+ pa_log("Failed to parse sample specification");
goto fail;
}
if (!pa_rtp_sample_spec_valid(&ss)) {
- pa_log(__FILE__": specified sample type not compatible with RTP");
+ pa_log("Specified sample type not compatible with RTP");
goto fail;
}
@@ -205,83 +220,133 @@ int pa__init(pa_core *c, pa_module*m) {
payload = pa_rtp_payload_from_sample_spec(&ss);
- mtu = (DEFAULT_MTU/pa_frame_size(&ss))*pa_frame_size(&ss);
-
+ mtu = (uint32_t) pa_frame_align(DEFAULT_MTU, &ss);
+
if (pa_modargs_get_value_u32(ma, "mtu", &mtu) < 0 || mtu < 1 || mtu % pa_frame_size(&ss) != 0) {
- pa_log(__FILE__": invalid mtu.");
+ pa_log("Invalid MTU.");
goto fail;
}
- port = DEFAULT_PORT + ((rand() % 512) << 1);
+ port = DEFAULT_PORT + ((uint32_t) (rand() % 512) << 1);
if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
- pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
+ pa_log("port= expects a numerical argument between 1 and 65535.");
goto fail;
}
if (port & 1)
- pa_log_warn(__FILE__": WARNING: port number not even as suggested in RFC3550!");
+ pa_log_warn("Port number not even as suggested in RFC3550!");
+
+ if (pa_modargs_get_value_u32(ma, "ttl", &ttl) < 0 || ttl < 1 || ttl > 0xFF) {
+ pa_log("ttl= expects a numerical argument between 1 and 255.");
+ goto fail;
+ }
dest = pa_modargs_get_value(ma, "destination", DEFAULT_DESTINATION);
- if (inet_pton(AF_INET6, dest, &sa6.sin6_addr) > 0) {
- sa6.sin6_family = af = AF_INET6;
- sa6.sin6_port = htons(port);
- sap_sa6 = sa6;
- sap_sa6.sin6_port = htons(SAP_PORT);
- } else if (inet_pton(AF_INET, dest, &sa4.sin_addr) > 0) {
+ if (inet_pton(AF_INET, dest, &sa4.sin_addr) > 0) {
sa4.sin_family = af = AF_INET;
- sa4.sin_port = htons(port);
+ sa4.sin_port = htons((uint16_t) port);
sap_sa4 = sa4;
sap_sa4.sin_port = htons(SAP_PORT);
+#ifdef HAVE_IPV6
+ } else if (inet_pton(AF_INET6, dest, &sa6.sin6_addr) > 0) {
+ sa6.sin6_family = af = AF_INET6;
+ sa6.sin6_port = htons((uint16_t) port);
+ sap_sa6 = sa6;
+ sap_sa6.sin6_port = htons(SAP_PORT);
+#endif
} else {
- pa_log(__FILE__": invalid destination '%s'", dest);
+ pa_log("Invalid destination '%s'", dest);
goto fail;
}
-
- if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
- pa_log(__FILE__": socket() failed: %s", pa_cstrerror(errno));
+
+ if ((fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) {
+ pa_log("socket() failed: %s", pa_cstrerror(errno));
goto fail;
}
- if (connect(fd, af == AF_INET ? (struct sockaddr*) &sa4 : (struct sockaddr*) &sa6, af == AF_INET ? sizeof(sa4) : sizeof(sa6)) < 0) {
- pa_log(__FILE__": connect() failed: %s", pa_cstrerror(errno));
+ if (af == AF_INET && connect(fd, (struct sockaddr*) &sa4, sizeof(sa4)) < 0) {
+ pa_log("connect() failed: %s", pa_cstrerror(errno));
goto fail;
+#ifdef HAVE_IPV6
+ } else if (af == AF_INET6 && connect(fd, (struct sockaddr*) &sa6, sizeof(sa6)) < 0) {
+ pa_log("connect() failed: %s", pa_cstrerror(errno));
+ goto fail;
+#endif
}
- if ((sap_fd = socket(af, SOCK_DGRAM, 0)) < 0) {
- pa_log(__FILE__": socket() failed: %s", pa_cstrerror(errno));
+ if ((sap_fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) {
+ pa_log("socket() failed: %s", pa_cstrerror(errno));
goto fail;
}
- if (connect(sap_fd, af == AF_INET ? (struct sockaddr*) &sap_sa4 : (struct sockaddr*) &sap_sa6, af == AF_INET ? sizeof(sap_sa4) : sizeof(sap_sa6)) < 0) {
- pa_log(__FILE__": connect() failed: %s", pa_cstrerror(errno));
+ if (af == AF_INET && connect(sap_fd, (struct sockaddr*) &sap_sa4, sizeof(sap_sa4)) < 0) {
+ pa_log("connect() failed: %s", pa_cstrerror(errno));
+ goto fail;
+#ifdef HAVE_IPV6
+ } else if (af == AF_INET6 && connect(sap_fd, (struct sockaddr*) &sap_sa6, sizeof(sap_sa6)) < 0) {
+ pa_log("connect() failed: %s", pa_cstrerror(errno));
goto fail;
+#endif
}
- if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0 ||
- setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) {
- pa_log(__FILE__": IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno));
+ j = !!loop;
+ if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0 ||
+ setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0) {
+ pa_log("IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno));
goto fail;
}
-
- if (!(o = pa_source_output_new(s, __FILE__, "RTP Monitor Stream", &ss, &cm, PA_RESAMPLER_INVALID))) {
- pa_log(__FILE__": failed to create source output.");
+
+ if (ttl != DEFAULT_TTL) {
+ int _ttl = (int) ttl;
+
+ if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) {
+ pa_log("IP_MULTICAST_TTL failed: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ if (setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) {
+ pa_log("IP_MULTICAST_TTL (sap) failed: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+ }
+
+ /* If the socket queue is full, let's drop packets */
+ pa_make_fd_nonblock(fd);
+ pa_make_udp_socket_low_delay(fd);
+
+ pa_source_output_new_data_init(&data);
+ pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, "RTP Monitor Stream");
+ pa_proplist_sets(data.proplist, "rtp.destination", dest);
+ pa_proplist_setf(data.proplist, "rtp.mtu", "%lu", (unsigned long) mtu);
+ pa_proplist_setf(data.proplist, "rtp.port", "%lu", (unsigned long) port);
+ pa_proplist_setf(data.proplist, "rtp.ttl", "%lu", (unsigned long) ttl);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_output_new_data_set_source(&data, s, FALSE);
+ pa_source_output_new_data_set_sample_spec(&data, &ss);
+ pa_source_output_new_data_set_channel_map(&data, &cm);
+ data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
+
+ pa_source_output_new(&o, m->core, &data);
+ pa_source_output_new_data_done(&data);
+
+ if (!o) {
+ pa_log("failed to create source output.");
goto fail;
}
+ o->parent.process_msg = source_output_process_msg;
o->push = source_output_push;
o->kill = source_output_kill;
- o->get_latency = source_output_get_latency;
- o->owner = m;
-
- u = pa_xnew(struct userdata, 1);
- m->userdata = u;
- o->userdata = u;
+ pa_log_info("Configured source latency of %llu ms.",
+ (unsigned long long) pa_source_output_set_requested_latency(o, pa_bytes_to_usec(mtu, &o->sample_spec)) / PA_USEC_PER_MSEC);
+
+ m->userdata = o->userdata = u = pa_xnew(struct userdata, 1);
u->module = m;
- u->core = c;
u->source_output = o;
-
+
u->memblockq = pa_memblockq_new(
0,
MEMBLOCKQ_MAXLENGTH,
@@ -289,35 +354,43 @@ int pa__init(pa_core *c, pa_module*m) {
pa_frame_size(&ss),
1,
0,
- NULL,
- c->memblock_stat);
+ 0,
+ NULL);
u->mtu = mtu;
-
+
k = sizeof(sa_dst);
- r = getsockname(fd, (struct sockaddr*) &sa_dst, &k);
- assert(r >= 0);
+ pa_assert_se((r = getsockname(fd, (struct sockaddr*) &sa_dst, &k)) >= 0);
n = pa_sprintf_malloc("PulseAudio RTP Stream on %s", pa_get_fqdn(hn, sizeof(hn)));
-
- p = pa_sdp_build(af,
- af == AF_INET ? (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr : (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
- af == AF_INET ? (void*) &sa4.sin_addr : (void*) &sa6.sin6_addr,
- n, port, payload, &ss);
+
+ if (af == AF_INET) {
+ p = pa_sdp_build(af,
+ (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr,
+ (void*) &sa4.sin_addr,
+ n, (uint16_t) port, payload, &ss);
+#ifdef HAVE_IPV6
+ } else {
+ p = pa_sdp_build(af,
+ (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
+ (void*) &sa6.sin6_addr,
+ n, (uint16_t) port, payload, &ss);
+#endif
+ }
pa_xfree(n);
-
- pa_rtp_context_init_send(&u->rtp_context, fd, c->cookie, payload, pa_frame_size(&ss));
+
+ pa_rtp_context_init_send(&u->rtp_context, fd, m->core->cookie, payload, pa_frame_size(&ss));
pa_sap_context_init_send(&u->sap_context, sap_fd, p);
- pa_log_info(__FILE__": RTP stream initialized with mtu %u on %s:%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu, dest, port, u->rtp_context.ssrc, payload, u->rtp_context.sequence);
- pa_log_info(__FILE__": SDP-Data:\n%s\n"__FILE__": EOF", p);
+ pa_log_info("RTP stream initialized with mtu %u on %s:%u ttl=%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu, dest, port, ttl, u->rtp_context.ssrc, payload, u->rtp_context.sequence);
+ pa_log_info("SDP-Data:\n%s\nEOF", p);
pa_sap_send(&u->sap_context, 0);
- pa_gettimeofday(&tv);
- pa_timeval_add(&tv, SAP_INTERVAL);
- u->sap_event = c->mainloop->time_new(c->mainloop, &tv, sap_event_cb, u);
+ u->sap_event = pa_core_rttime_new(m->core, pa_rtclock_now() + SAP_INTERVAL, sap_event_cb, u);
+
+ pa_source_output_put(u->source_output);
pa_modargs_free(ma);
@@ -328,31 +401,31 @@ fail:
pa_modargs_free(ma);
if (fd >= 0)
- close(fd);
-
+ pa_close(fd);
+
if (sap_fd >= 0)
- close(sap_fd);
+ pa_close(sap_fd);
if (o) {
- pa_source_output_disconnect(o);
+ pa_source_output_unlink(o);
pa_source_output_unref(o);
}
-
+
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata *u;
- assert(c);
- assert(m);
+ pa_assert(m);
if (!(u = m->userdata))
return;
- c->mainloop->time_free(u->sap_event);
-
+ if (u->sap_event)
+ m->core->mainloop->time_free(u->sap_event);
+
if (u->source_output) {
- pa_source_output_disconnect(u->source_output);
+ pa_source_output_unlink(u->source_output);
pa_source_output_unref(u->source_output);
}
@@ -361,7 +434,8 @@ void pa__done(pa_core *c, pa_module*m) {
pa_sap_send(&u->sap_context, 1);
pa_sap_context_destroy(&u->sap_context);
- pa_memblockq_free(u->memblockq);
-
+ if (u->memblockq)
+ pa_memblockq_free(u->memblockq);
+
pa_xfree(u);
}
diff --git a/src/modules/rtp/rtp.c b/src/modules/rtp/rtp.c
index ee037d42..05c736a7 100644
--- a/src/modules/rtp/rtp.c
+++ b/src/modules/rtp/rtp.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -23,12 +23,9 @@
#include <config.h>
#endif
-#include <assert.h>
-#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
-#include <arpa/inet.h>
#include <unistd.h>
#include <sys/ioctl.h>
@@ -36,22 +33,31 @@
#include <sys/filio.h>
#endif
+#ifdef HAVE_SYS_UIO_H
+#include <sys/uio.h>
+#endif
+
#include <pulsecore/core-error.h>
#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/arpa-inet.h>
#include "rtp.h"
pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload, size_t frame_size) {
- assert(c);
- assert(fd >= 0);
+ pa_assert(c);
+ pa_assert(fd >= 0);
c->fd = fd;
c->sequence = (uint16_t) (rand()*rand());
c->timestamp = 0;
c->ssrc = ssrc ? ssrc : (uint32_t) (rand()*rand());
- c->payload = payload & 127;
+ c->payload = (uint8_t) (payload & 127U);
c->frame_size = frame_size;
-
+
+ pa_memchunk_reset(&c->memchunk);
+
return c;
}
@@ -61,40 +67,43 @@ int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
struct iovec iov[MAX_IOVECS];
pa_memblock* mb[MAX_IOVECS];
int iov_idx = 1;
- size_t n = 0, skip = 0;
-
- assert(c);
- assert(size > 0);
- assert(q);
+ size_t n = 0;
+
+ pa_assert(c);
+ pa_assert(size > 0);
+ pa_assert(q);
if (pa_memblockq_get_length(q) < size)
return 0;
-
+
for (;;) {
int r;
pa_memchunk chunk;
+ pa_memchunk_reset(&chunk);
+
if ((r = pa_memblockq_peek(q, &chunk)) >= 0) {
size_t k = n + chunk.length > size ? size - n : chunk.length;
- if (chunk.memblock) {
- iov[iov_idx].iov_base = (void*)((uint8_t*) chunk.memblock->data + chunk.index);
- iov[iov_idx].iov_len = k;
- mb[iov_idx] = chunk.memblock;
- iov_idx ++;
+ pa_assert(chunk.memblock);
- n += k;
- }
+ iov[iov_idx].iov_base = ((uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index);
+ iov[iov_idx].iov_len = k;
+ mb[iov_idx] = chunk.memblock;
+ iov_idx ++;
- skip += k;
- pa_memblockq_drop(q, &chunk, k);
+ n += k;
+ pa_memblockq_drop(q, k);
}
- if (r < 0 || !chunk.memblock || n >= size || iov_idx >= MAX_IOVECS) {
+ pa_assert(n % c->frame_size == 0);
+
+ if (r < 0 || n >= size || iov_idx >= MAX_IOVECS) {
uint32_t header[3];
struct msghdr m;
- int k, i;
+ ssize_t k;
+ int i;
if (n > 0) {
header[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c->payload << 16) | ((uint32_t) c->sequence));
@@ -103,37 +112,38 @@ int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
iov[0].iov_base = (void*)header;
iov[0].iov_len = sizeof(header);
-
+
m.msg_name = NULL;
m.msg_namelen = 0;
m.msg_iov = iov;
- m.msg_iovlen = iov_idx;
+ m.msg_iovlen = (size_t) iov_idx;
m.msg_control = NULL;
m.msg_controllen = 0;
m.msg_flags = 0;
-
+
k = sendmsg(c->fd, &m, MSG_DONTWAIT);
- for (i = 1; i < iov_idx; i++)
+ for (i = 1; i < iov_idx; i++) {
+ pa_memblock_release(mb[i]);
pa_memblock_unref(mb[i]);
+ }
c->sequence++;
} else
k = 0;
- c->timestamp += skip/c->frame_size;
-
+ c->timestamp += (unsigned) (n/c->frame_size);
+
if (k < 0) {
- if (errno != EAGAIN) /* If the queue is full, just ignore it */
- pa_log(__FILE__": sendmsg() failed: %s", pa_cstrerror(errno));
+ if (errno != EAGAIN && errno != EINTR) /* If the queue is full, just ignore it */
+ pa_log("sendmsg() failed: %s", pa_cstrerror(errno));
return -1;
}
-
+
if (r < 0 || pa_memblockq_get_length(q) < size)
break;
n = 0;
- skip = 0;
iov_idx = 1;
}
}
@@ -142,97 +152,143 @@ int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
}
pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size) {
- assert(c);
+ pa_assert(c);
c->fd = fd;
c->frame_size = frame_size;
+
+ pa_memchunk_reset(&c->memchunk);
return c;
}
-int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_memblock_stat *st) {
+int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool, struct timeval *tstamp) {
int size;
struct msghdr m;
+ struct cmsghdr *cm;
struct iovec iov;
uint32_t header;
- int cc;
+ unsigned cc;
ssize_t r;
-
- assert(c);
- assert(chunk);
+ uint8_t aux[1024];
+ pa_bool_t found_tstamp = FALSE;
+
+ pa_assert(c);
+ pa_assert(chunk);
- chunk->memblock = NULL;
+ pa_memchunk_reset(chunk);
if (ioctl(c->fd, FIONREAD, &size) < 0) {
- pa_log(__FILE__": FIONREAD failed: %s", pa_cstrerror(errno));
+ pa_log_warn("FIONREAD failed: %s", pa_cstrerror(errno));
goto fail;
}
- if (!size)
+ if (size <= 0)
return 0;
- chunk->memblock = pa_memblock_new(size, st);
+ if (c->memchunk.length < (unsigned) size) {
+ size_t l;
+
+ if (c->memchunk.memblock)
+ pa_memblock_unref(c->memchunk.memblock);
+
+ l = PA_MAX((size_t) size, pa_mempool_block_size_max(pool));
- iov.iov_base = chunk->memblock->data;
- iov.iov_len = size;
+ c->memchunk.memblock = pa_memblock_new(pool, l);
+ c->memchunk.index = 0;
+ c->memchunk.length = pa_memblock_get_length(c->memchunk.memblock);
+ }
+
+ pa_assert(c->memchunk.length >= (size_t) size);
+
+ chunk->memblock = pa_memblock_ref(c->memchunk.memblock);
+ chunk->index = c->memchunk.index;
+
+ iov.iov_base = (uint8_t*) pa_memblock_acquire(chunk->memblock) + chunk->index;
+ iov.iov_len = (size_t) size;
m.msg_name = NULL;
m.msg_namelen = 0;
m.msg_iov = &iov;
m.msg_iovlen = 1;
- m.msg_control = NULL;
- m.msg_controllen = 0;
+ m.msg_control = aux;
+ m.msg_controllen = sizeof(aux);
m.msg_flags = 0;
-
- if ((r = recvmsg(c->fd, &m, 0)) != size) {
- pa_log(__FILE__": recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
+
+ r = recvmsg(c->fd, &m, 0);
+ pa_memblock_release(chunk->memblock);
+
+ if (r != size) {
+ if (r < 0 && errno != EAGAIN && errno != EINTR)
+ pa_log_warn("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
+
goto fail;
}
if (size < 12) {
- pa_log(__FILE__": RTP packet too short.");
+ pa_log_warn("RTP packet too short.");
goto fail;
}
- memcpy(&header, chunk->memblock->data, sizeof(uint32_t));
- memcpy(&c->timestamp, (uint8_t*) chunk->memblock->data + 4, sizeof(uint32_t));
- memcpy(&c->ssrc, (uint8_t*) chunk->memblock->data + 8, sizeof(uint32_t));
-
+ memcpy(&header, iov.iov_base, sizeof(uint32_t));
+ memcpy(&c->timestamp, (uint8_t*) iov.iov_base + 4, sizeof(uint32_t));
+ memcpy(&c->ssrc, (uint8_t*) iov.iov_base + 8, sizeof(uint32_t));
+
header = ntohl(header);
c->timestamp = ntohl(c->timestamp);
c->ssrc = ntohl(c->ssrc);
if ((header >> 30) != 2) {
- pa_log(__FILE__": Unsupported RTP version.");
+ pa_log_warn("Unsupported RTP version.");
goto fail;
}
if ((header >> 29) & 1) {
- pa_log(__FILE__": RTP padding not supported.");
+ pa_log_warn("RTP padding not supported.");
goto fail;
}
if ((header >> 28) & 1) {
- pa_log(__FILE__": RTP header extensions not supported.");
+ pa_log_warn("RTP header extensions not supported.");
goto fail;
}
cc = (header >> 24) & 0xF;
- c->payload = (header >> 16) & 127;
- c->sequence = header & 0xFFFF;
+ c->payload = (uint8_t) ((header >> 16) & 127U);
+ c->sequence = (uint16_t) (header & 0xFFFFU);
- if (12 + cc*4 > size) {
- pa_log(__FILE__": RTP packet too short. (CSRC)");
+ if (12 + cc*4 > (unsigned) size) {
+ pa_log_warn("RTP packet too short. (CSRC)");
goto fail;
}
- chunk->index = 12 + cc*4;
- chunk->length = size - chunk->index;
+ chunk->index += 12 + cc*4;
+ chunk->length = (size_t) size - 12 + cc*4;
if (chunk->length % c->frame_size != 0) {
- pa_log(__FILE__": Vad RTP packet size.");
+ pa_log_warn("Bad RTP packet size.");
goto fail;
}
-
+
+ c->memchunk.index = chunk->index + chunk->length;
+ c->memchunk.length = pa_memblock_get_length(c->memchunk.memblock) - c->memchunk.index;
+
+ if (c->memchunk.length <= 0) {
+ pa_memblock_unref(c->memchunk.memblock);
+ pa_memchunk_reset(&c->memchunk);
+ }
+
+ for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm))
+ if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SO_TIMESTAMP) {
+ memcpy(tstamp, CMSG_DATA(cm), sizeof(struct timeval));
+ found_tstamp = TRUE;
+ break;
+ }
+
+ if (!found_tstamp) {
+ pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!");
+ memset(tstamp, 0, sizeof(tstamp));
+ }
+
return 0;
fail:
@@ -243,7 +299,7 @@ fail:
}
uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss) {
- assert(ss);
+ pa_assert(ss);
if (ss->format == PA_SAMPLE_ULAW && ss->rate == 8000 && ss->channels == 1)
return 0;
@@ -253,12 +309,12 @@ uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss) {
return 10;
if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 1)
return 11;
-
+
return 127;
}
pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec *ss) {
- assert(ss);
+ pa_assert(ss);
switch (payload) {
case 0:
@@ -278,7 +334,7 @@ pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec
ss->format = PA_SAMPLE_S16BE;
ss->rate = 44100;
break;
-
+
case 11:
ss->channels = 1;
ss->format = PA_SAMPLE_S16BE;
@@ -293,17 +349,17 @@ pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec
}
pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss) {
- assert(ss);
+ pa_assert(ss);
if (!pa_rtp_sample_spec_valid(ss))
ss->format = PA_SAMPLE_S16BE;
- assert(pa_rtp_sample_spec_valid(ss));
+ pa_assert(pa_rtp_sample_spec_valid(ss));
return ss;
}
int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
- assert(ss);
+ pa_assert(ss);
if (!pa_sample_spec_valid(ss))
return 0;
@@ -316,9 +372,12 @@ int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
}
void pa_rtp_context_destroy(pa_rtp_context *c) {
- assert(c);
+ pa_assert(c);
+
+ pa_assert_se(pa_close(c->fd) == 0);
- close(c->fd);
+ if (c->memchunk.memblock)
+ pa_memblock_unref(c->memchunk.memblock);
}
const char* pa_rtp_format_to_string(pa_sample_format_t f) {
@@ -337,8 +396,8 @@ const char* pa_rtp_format_to_string(pa_sample_format_t f) {
}
pa_sample_format_t pa_rtp_string_to_format(const char *s) {
- assert(s);
-
+ pa_assert(s);
+
if (!(strcmp(s, "L16")))
return PA_SAMPLE_S16BE;
else if (!strcmp(s, "L8"))
@@ -350,4 +409,3 @@ pa_sample_format_t pa_rtp_string_to_format(const char *s) {
else
return PA_SAMPLE_INVALID;
}
-
diff --git a/src/modules/rtp/rtp.h b/src/modules/rtp/rtp.h
index 35fbbd35..e975e750 100644
--- a/src/modules/rtp/rtp.h
+++ b/src/modules/rtp/rtp.h
@@ -1,21 +1,21 @@
#ifndef foortphfoo
#define foortphfoo
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -35,13 +35,18 @@ typedef struct pa_rtp_context {
uint32_t ssrc;
uint8_t payload;
size_t frame_size;
+
+ pa_memchunk memchunk;
} pa_rtp_context;
pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload, size_t frame_size);
+
+/* If the memblockq doesn't have a silence memchunk set, then the caller must
+ * guarantee that the current read index doesn't point to a hole. */
int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q);
pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size);
-int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_memblock_stat *st);
+int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool, struct timeval *tstamp);
void pa_rtp_context_destroy(pa_rtp_context *c);
diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c
new file mode 100644
index 00000000..ecf85b89
--- /dev/null
+++ b/src/modules/rtp/rtsp_client.c
@@ -0,0 +1,530 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+
+#ifdef HAVE_SYS_FILIO_H
+#include <sys/filio.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/strbuf.h>
+#include <pulsecore/ioline.h>
+#include <pulsecore/arpa-inet.h>
+
+#include "rtsp_client.h"
+
+struct pa_rtsp_client {
+ pa_mainloop_api *mainloop;
+ char *hostname;
+ uint16_t port;
+
+ pa_socket_client *sc;
+ pa_ioline *ioline;
+
+ pa_rtsp_cb_t callback;
+
+ void *userdata;
+ const char *useragent;
+
+ pa_rtsp_state state;
+ uint8_t waiting;
+
+ pa_headerlist* headers;
+ char *last_header;
+ pa_strbuf *header_buffer;
+ pa_headerlist* response_headers;
+
+ char *localip;
+ char *url;
+ uint16_t rtp_port;
+ uint32_t cseq;
+ char *session;
+ char *transport;
+};
+
+pa_rtsp_client* pa_rtsp_client_new(pa_mainloop_api *mainloop, const char* hostname, uint16_t port, const char* useragent) {
+ pa_rtsp_client *c;
+
+ pa_assert(mainloop);
+ pa_assert(hostname);
+ pa_assert(port > 0);
+
+ c = pa_xnew0(pa_rtsp_client, 1);
+ c->mainloop = mainloop;
+ c->hostname = pa_xstrdup(hostname);
+ c->port = port;
+ c->headers = pa_headerlist_new();
+
+ if (useragent)
+ c->useragent = useragent;
+ else
+ c->useragent = "PulseAudio RTSP Client";
+
+ return c;
+}
+
+
+void pa_rtsp_client_free(pa_rtsp_client* c) {
+ pa_assert(c);
+
+ if (c->sc)
+ pa_socket_client_unref(c->sc);
+
+ pa_rtsp_disconnect(c);
+
+ pa_xfree(c->hostname);
+ pa_xfree(c->url);
+ pa_xfree(c->localip);
+ pa_xfree(c->session);
+ pa_xfree(c->transport);
+ pa_xfree(c->last_header);
+ if (c->header_buffer)
+ pa_strbuf_free(c->header_buffer);
+ if (c->response_headers)
+ pa_headerlist_free(c->response_headers);
+ pa_headerlist_free(c->headers);
+
+ pa_xfree(c);
+}
+
+
+static void headers_read(pa_rtsp_client *c) {
+ char* token;
+ char delimiters[] = ";";
+
+ pa_assert(c);
+ pa_assert(c->response_headers);
+ pa_assert(c->callback);
+
+ /* Deal with a SETUP response */
+ if (STATE_SETUP == c->state) {
+ const char* token_state = NULL;
+ const char* pc = NULL;
+ c->session = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Session"));
+ c->transport = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Transport"));
+
+ if (!c->session || !c->transport) {
+ pa_log("Invalid SETUP response.");
+ return;
+ }
+
+ /* Now parse out the server port component of the response. */
+ while ((token = pa_split(c->transport, delimiters, &token_state))) {
+ if ((pc = strstr(token, "="))) {
+ if (0 == strncmp(token, "server_port", 11)) {
+ pa_atou(pc+1, (uint32_t*)(&c->rtp_port));
+ pa_xfree(token);
+ break;
+ }
+ }
+ pa_xfree(token);
+ }
+ if (0 == c->rtp_port) {
+ /* Error no server_port in response */
+ pa_log("Invalid SETUP response (no port number).");
+ return;
+ }
+ }
+
+ /* Call our callback */
+ c->callback(c, c->state, c->response_headers, c->userdata);
+}
+
+
+static void line_callback(pa_ioline *line, const char *s, void *userdata) {
+ char *delimpos;
+ char *s2, *s2p;
+
+ pa_rtsp_client *c = userdata;
+ pa_assert(line);
+ pa_assert(c);
+ pa_assert(c->callback);
+
+ if (!s) {
+ /* Keep the ioline/iochannel open as they will be freed automatically */
+ c->ioline = NULL;
+ c->callback(c, STATE_DISCONNECTED, NULL, c->userdata);
+ return;
+ }
+
+ s2 = pa_xstrdup(s);
+ /* Trim trailing carriage returns */
+ s2p = s2 + strlen(s2) - 1;
+ while (s2p >= s2 && '\r' == *s2p) {
+ *s2p = '\0';
+ s2p -= 1;
+ }
+ if (c->waiting && 0 == strcmp("RTSP/1.0 200 OK", s2)) {
+ c->waiting = 0;
+ if (c->response_headers)
+ pa_headerlist_free(c->response_headers);
+ c->response_headers = pa_headerlist_new();
+ goto exit;
+ }
+ if (c->waiting) {
+ pa_log_warn("Unexpected response: %s", s2);
+ goto exit;;
+ }
+ if (!strlen(s2)) {
+ /* End of headers */
+ /* We will have a header left from our looping iteration, so add it in :) */
+ if (c->last_header) {
+ char *tmp = pa_strbuf_tostring_free(c->header_buffer);
+ /* This is not a continuation header so let's dump it into our proplist */
+ pa_headerlist_puts(c->response_headers, c->last_header, tmp);
+ pa_xfree(tmp);
+ pa_xfree(c->last_header);
+ c->last_header = NULL;
+ c->header_buffer = NULL;
+ }
+
+ pa_log_debug("Full response received. Dispatching");
+ headers_read(c);
+ c->waiting = 1;
+ goto exit;
+ }
+
+ /* Read and parse a header (we know it's not empty) */
+ /* TODO: Move header reading into the headerlist. */
+
+ /* If the first character is a space, it's a continuation header */
+ if (c->last_header && ' ' == s2[0]) {
+ pa_assert(c->header_buffer);
+
+ /* Add this line to the buffer (sans the space. */
+ pa_strbuf_puts(c->header_buffer, &(s2[1]));
+ goto exit;
+ }
+
+ if (c->last_header) {
+ char *tmp = pa_strbuf_tostring_free(c->header_buffer);
+ /* This is not a continuation header so let's dump the full
+ header/value into our proplist */
+ pa_headerlist_puts(c->response_headers, c->last_header, tmp);
+ pa_xfree(tmp);
+ pa_xfree(c->last_header);
+ c->last_header = NULL;
+ c->header_buffer = NULL;
+ }
+
+ delimpos = strstr(s2, ":");
+ if (!delimpos) {
+ pa_log_warn("Unexpected response when expecting header: %s", s);
+ goto exit;
+ }
+
+ pa_assert(!c->header_buffer);
+ pa_assert(!c->last_header);
+
+ c->header_buffer = pa_strbuf_new();
+ if (strlen(delimpos) > 1) {
+ /* Cut our line off so we can copy the header name out */
+ *delimpos++ = '\0';
+
+ /* Trim the front of any spaces */
+ while (' ' == *delimpos)
+ ++delimpos;
+
+ pa_strbuf_puts(c->header_buffer, delimpos);
+ } else {
+ /* Cut our line off so we can copy the header name out */
+ *delimpos = '\0';
+ }
+
+ /* Save the header name */
+ c->last_header = pa_xstrdup(s2);
+ exit:
+ pa_xfree(s2);
+}
+
+
+static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
+ pa_rtsp_client *c = userdata;
+ union {
+ struct sockaddr sa;
+ struct sockaddr_in in;
+ struct sockaddr_in6 in6;
+ } sa;
+ socklen_t sa_len = sizeof(sa);
+
+ pa_assert(sc);
+ pa_assert(c);
+ pa_assert(STATE_CONNECT == c->state);
+ pa_assert(c->sc == sc);
+ pa_socket_client_unref(c->sc);
+ c->sc = NULL;
+
+ if (!io) {
+ pa_log("Connection failed: %s", pa_cstrerror(errno));
+ return;
+ }
+ pa_assert(!c->ioline);
+
+ c->ioline = pa_ioline_new(io);
+ pa_ioline_set_callback(c->ioline, line_callback, c);
+
+ /* Get the local IP address for use externally */
+ if (0 == getsockname(pa_iochannel_get_recv_fd(io), &sa.sa, &sa_len)) {
+ char buf[INET6_ADDRSTRLEN];
+ const char *res = NULL;
+
+ if (AF_INET == sa.sa.sa_family) {
+ if ((res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf)))) {
+ c->localip = pa_xstrdup(res);
+ }
+ } else if (AF_INET6 == sa.sa.sa_family) {
+ if ((res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf)))) {
+ c->localip = pa_sprintf_malloc("[%s]", res);
+ }
+ }
+ }
+ pa_log_debug("Established RTSP connection from local ip %s", c->localip);
+
+ if (c->callback)
+ c->callback(c, c->state, NULL, c->userdata);
+}
+
+int pa_rtsp_connect(pa_rtsp_client *c) {
+ pa_assert(c);
+ pa_assert(!c->sc);
+
+ pa_xfree(c->session);
+ c->session = NULL;
+
+ if (!(c->sc = pa_socket_client_new_string(c->mainloop, TRUE, c->hostname, c->port))) {
+ pa_log("failed to connect to server '%s:%d'", c->hostname, c->port);
+ return -1;
+ }
+
+ pa_socket_client_set_callback(c->sc, on_connection, c);
+ c->waiting = 1;
+ c->state = STATE_CONNECT;
+ return 0;
+}
+
+void pa_rtsp_set_callback(pa_rtsp_client *c, pa_rtsp_cb_t callback, void *userdata) {
+ pa_assert(c);
+
+ c->callback = callback;
+ c->userdata = userdata;
+}
+
+void pa_rtsp_disconnect(pa_rtsp_client *c) {
+ pa_assert(c);
+
+ if (c->ioline)
+ pa_ioline_close(c->ioline);
+ c->ioline = NULL;
+}
+
+
+const char* pa_rtsp_localip(pa_rtsp_client* c) {
+ pa_assert(c);
+
+ return c->localip;
+}
+
+uint32_t pa_rtsp_serverport(pa_rtsp_client* c) {
+ pa_assert(c);
+
+ return c->rtp_port;
+}
+
+void pa_rtsp_set_url(pa_rtsp_client* c, const char* url) {
+ pa_assert(c);
+
+ c->url = pa_xstrdup(url);
+}
+
+void pa_rtsp_add_header(pa_rtsp_client *c, const char* key, const char* value) {
+ pa_assert(c);
+ pa_assert(key);
+ pa_assert(value);
+
+ pa_headerlist_puts(c->headers, key, value);
+}
+
+void pa_rtsp_remove_header(pa_rtsp_client *c, const char* key) {
+ pa_assert(c);
+ pa_assert(key);
+
+ pa_headerlist_remove(c->headers, key);
+}
+
+static int rtsp_exec(pa_rtsp_client* c, const char* cmd,
+ const char* content_type, const char* content,
+ int expect_response,
+ pa_headerlist* headers) {
+ pa_strbuf* buf;
+ char* hdrs;
+
+ pa_assert(c);
+ pa_assert(c->url);
+ pa_assert(cmd);
+ pa_assert(c->ioline);
+
+ pa_log_debug("Sending command: %s", cmd);
+
+ buf = pa_strbuf_new();
+ pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq);
+ if (c->session)
+ pa_strbuf_printf(buf, "Session: %s\r\n", c->session);
+
+ /* Add the headers */
+ if (headers) {
+ hdrs = pa_headerlist_to_string(headers);
+ pa_strbuf_puts(buf, hdrs);
+ pa_xfree(hdrs);
+ }
+
+ if (content_type && content) {
+ pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n",
+ content_type, (int)strlen(content));
+ }
+
+ pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent);
+
+ if (c->headers) {
+ hdrs = pa_headerlist_to_string(c->headers);
+ pa_strbuf_puts(buf, hdrs);
+ pa_xfree(hdrs);
+ }
+
+ pa_strbuf_puts(buf, "\r\n");
+
+ if (content_type && content) {
+ pa_strbuf_puts(buf, content);
+ }
+
+ /* Our packet is created... now we can send it :) */
+ hdrs = pa_strbuf_tostring_free(buf);
+ /*pa_log_debug("Submitting request:");
+ pa_log_debug(hdrs);*/
+ pa_ioline_puts(c->ioline, hdrs);
+ pa_xfree(hdrs);
+
+ return 0;
+}
+
+
+int pa_rtsp_announce(pa_rtsp_client *c, const char* sdp) {
+ pa_assert(c);
+ if (!sdp)
+ return -1;
+
+ c->state = STATE_ANNOUNCE;
+ return rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL);
+}
+
+
+int pa_rtsp_setup(pa_rtsp_client* c) {
+ pa_headerlist* headers;
+ int rv;
+
+ pa_assert(c);
+
+ headers = pa_headerlist_new();
+ pa_headerlist_puts(headers, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
+
+ c->state = STATE_SETUP;
+ rv = rtsp_exec(c, "SETUP", NULL, NULL, 1, headers);
+ pa_headerlist_free(headers);
+ return rv;
+}
+
+
+int pa_rtsp_record(pa_rtsp_client* c, uint16_t* seq, uint32_t* rtptime) {
+ pa_headerlist* headers;
+ int rv;
+ char *info;
+
+ pa_assert(c);
+ if (!c->session) {
+ /* No session in progress */
+ return -1;
+ }
+
+ /* Todo: Generate these values randomly as per spec */
+ *seq = *rtptime = 0;
+
+ headers = pa_headerlist_new();
+ pa_headerlist_puts(headers, "Range", "npt=0-");
+ info = pa_sprintf_malloc("seq=%u;rtptime=%u", *seq, *rtptime);
+ pa_headerlist_puts(headers, "RTP-Info", info);
+ pa_xfree(info);
+
+ c->state = STATE_RECORD;
+ rv = rtsp_exec(c, "RECORD", NULL, NULL, 1, headers);
+ pa_headerlist_free(headers);
+ return rv;
+}
+
+
+int pa_rtsp_teardown(pa_rtsp_client *c) {
+ pa_assert(c);
+
+ c->state = STATE_TEARDOWN;
+ return rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL);
+}
+
+
+int pa_rtsp_setparameter(pa_rtsp_client *c, const char* param) {
+ pa_assert(c);
+ if (!param)
+ return -1;
+
+ c->state = STATE_SET_PARAMETER;
+ return rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL);
+}
+
+
+int pa_rtsp_flush(pa_rtsp_client *c, uint16_t seq, uint32_t rtptime) {
+ pa_headerlist* headers;
+ int rv;
+ char *info;
+
+ pa_assert(c);
+
+ headers = pa_headerlist_new();
+ info = pa_sprintf_malloc("seq=%u;rtptime=%u", seq, rtptime);
+ pa_headerlist_puts(headers, "RTP-Info", info);
+ pa_xfree(info);
+
+ c->state = STATE_FLUSH;
+ rv = rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers);
+ pa_headerlist_free(headers);
+ return rv;
+}
diff --git a/src/modules/rtp/rtsp_client.h b/src/modules/rtp/rtsp_client.h
new file mode 100644
index 00000000..a56b9324
--- /dev/null
+++ b/src/modules/rtp/rtsp_client.h
@@ -0,0 +1,71 @@
+#ifndef foortspclienthfoo
+#define foortspclienthfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Colin Guthrie
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <inttypes.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netdb.h>
+
+#include <pulsecore/socket-client.h>
+#include <pulse/mainloop-api.h>
+
+#include "headerlist.h"
+
+typedef struct pa_rtsp_client pa_rtsp_client;
+typedef enum {
+ STATE_CONNECT,
+ STATE_ANNOUNCE,
+ STATE_SETUP,
+ STATE_RECORD,
+ STATE_FLUSH,
+ STATE_TEARDOWN,
+ STATE_SET_PARAMETER,
+ STATE_DISCONNECTED
+} pa_rtsp_state;
+typedef void (*pa_rtsp_cb_t)(pa_rtsp_client *c, pa_rtsp_state state, pa_headerlist* hl, void *userdata);
+
+pa_rtsp_client* pa_rtsp_client_new(pa_mainloop_api *mainloop, const char* hostname, uint16_t port, const char* useragent);
+void pa_rtsp_client_free(pa_rtsp_client* c);
+
+int pa_rtsp_connect(pa_rtsp_client* c);
+void pa_rtsp_set_callback(pa_rtsp_client *c, pa_rtsp_cb_t callback, void *userdata);
+
+void pa_rtsp_disconnect(pa_rtsp_client* c);
+
+const char* pa_rtsp_localip(pa_rtsp_client* c);
+uint32_t pa_rtsp_serverport(pa_rtsp_client* c);
+void pa_rtsp_set_url(pa_rtsp_client* c, const char* url);
+void pa_rtsp_add_header(pa_rtsp_client *c, const char* key, const char* value);
+void pa_rtsp_remove_header(pa_rtsp_client *c, const char* key);
+
+int pa_rtsp_announce(pa_rtsp_client* c, const char* sdp);
+
+int pa_rtsp_setup(pa_rtsp_client* c);
+int pa_rtsp_record(pa_rtsp_client* c, uint16_t* seq, uint32_t* rtptime);
+int pa_rtsp_teardown(pa_rtsp_client* c);
+
+int pa_rtsp_setparameter(pa_rtsp_client* c, const char* param);
+int pa_rtsp_flush(pa_rtsp_client* c, uint16_t seq, uint32_t rtptime);
+
+#endif
diff --git a/src/modules/rtp/sap.c b/src/modules/rtp/sap.c
index 615b8e4e..4d8bf668 100644
--- a/src/modules/rtp/sap.c
+++ b/src/modules/rtp/sap.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -23,13 +23,10 @@
#include <config.h>
#endif
-#include <assert.h>
-#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
-#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
@@ -39,11 +36,17 @@
#include <sys/filio.h>
#endif
+#ifdef HAVE_SYS_UIO_H
+#include <sys/uio.h>
+#endif
+
#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/arpa-inet.h>
#include "sap.h"
#include "sdp.h"
@@ -51,57 +54,70 @@
#define MIME_TYPE "application/sdp"
pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data) {
- assert(c);
- assert(fd >= 0);
- assert(sdp_data);
+ pa_assert(c);
+ pa_assert(fd >= 0);
+ pa_assert(sdp_data);
c->fd = fd;
c->sdp_data = sdp_data;
c->msg_id_hash = (uint16_t) (rand()*rand());
-
- return c;
+
+ return c;
}
void pa_sap_context_destroy(pa_sap_context *c) {
- assert(c);
+ pa_assert(c);
- close(c->fd);
+ pa_close(c->fd);
pa_xfree(c->sdp_data);
}
-int pa_sap_send(pa_sap_context *c, int goodbye) {
+int pa_sap_send(pa_sap_context *c, pa_bool_t goodbye) {
uint32_t header;
struct sockaddr_storage sa_buf;
struct sockaddr *sa = (struct sockaddr*) &sa_buf;
socklen_t salen = sizeof(sa_buf);
struct iovec iov[4];
struct msghdr m;
- int k;
+ ssize_t k;
if (getsockname(c->fd, sa, &salen) < 0) {
pa_log("getsockname() failed: %s\n", pa_cstrerror(errno));
return -1;
}
- assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
-
+#ifdef HAVE_IPV6
+ pa_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
+#else
+ pa_assert(sa->sa_family == AF_INET);
+#endif
+
header = htonl(((uint32_t) 1 << 29) |
+#ifdef HAVE_IPV6
(sa->sa_family == AF_INET6 ? (uint32_t) 1 << 28 : 0) |
+#endif
(goodbye ? (uint32_t) 1 << 26 : 0) |
(c->msg_id_hash));
iov[0].iov_base = &header;
iov[0].iov_len = sizeof(header);
- iov[1].iov_base = sa->sa_family == AF_INET ? (void*) &((struct sockaddr_in*) sa)->sin_addr : (void*) &((struct sockaddr_in6*) sa)->sin6_addr;
- iov[1].iov_len = sa->sa_family == AF_INET ? 4 : 16;
+ if (sa->sa_family == AF_INET) {
+ iov[1].iov_base = (void*) &((struct sockaddr_in*) sa)->sin_addr;
+ iov[1].iov_len = 4U;
+#ifdef HAVE_IPV6
+ } else {
+ iov[1].iov_base = (void*) &((struct sockaddr_in6*) sa)->sin6_addr;
+ iov[1].iov_len = 16U;
+#endif
+ }
iov[2].iov_base = (char*) MIME_TYPE;
iov[2].iov_len = sizeof(MIME_TYPE);
iov[3].iov_base = c->sdp_data;
iov[3].iov_len = strlen(c->sdp_data);
-
+
m.msg_name = NULL;
m.msg_namelen = 0;
m.msg_iov = iov;
@@ -109,47 +125,44 @@ int pa_sap_send(pa_sap_context *c, int goodbye) {
m.msg_control = NULL;
m.msg_controllen = 0;
m.msg_flags = 0;
-
+
if ((k = sendmsg(c->fd, &m, MSG_DONTWAIT)) < 0)
- pa_log("sendmsg() failed: %s\n", pa_cstrerror(errno));
+ pa_log_warn("sendmsg() failed: %s\n", pa_cstrerror(errno));
- return k;
+ return (int) k;
}
pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd) {
- assert(c);
- assert(fd >= 0);
+ pa_assert(c);
+ pa_assert(fd >= 0);
c->fd = fd;
c->sdp_data = NULL;
return c;
}
-int pa_sap_recv(pa_sap_context *c, int *goodbye) {
+int pa_sap_recv(pa_sap_context *c, pa_bool_t *goodbye) {
struct msghdr m;
struct iovec iov;
- int size, k;
+ int size;
char *buf = NULL, *e;
uint32_t header;
- int six, ac;
+ unsigned six, ac, k;
ssize_t r;
-
- assert(c);
- assert(goodbye);
+
+ pa_assert(c);
+ pa_assert(goodbye);
if (ioctl(c->fd, FIONREAD, &size) < 0) {
- pa_log(__FILE__": FIONREAD failed: %s", pa_cstrerror(errno));
+ pa_log_warn("FIONREAD failed: %s", pa_cstrerror(errno));
goto fail;
}
- if (!size)
- return 0;
-
- buf = pa_xnew(char, size+1);
+ buf = pa_xnew(char, (unsigned) size+1);
buf[size] = 0;
-
+
iov.iov_base = buf;
- iov.iov_len = size;
+ iov.iov_len = (size_t) size;
m.msg_name = NULL;
m.msg_namelen = 0;
@@ -158,14 +171,14 @@ int pa_sap_recv(pa_sap_context *c, int *goodbye) {
m.msg_control = NULL;
m.msg_controllen = 0;
m.msg_flags = 0;
-
+
if ((r = recvmsg(c->fd, &m, 0)) != size) {
- pa_log(__FILE__": recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
+ pa_log_warn("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
goto fail;
}
if (size < 4) {
- pa_log(__FILE__": SAP packet too short.");
+ pa_log_warn("SAP packet too short.");
goto fail;
}
@@ -173,48 +186,48 @@ int pa_sap_recv(pa_sap_context *c, int *goodbye) {
header = ntohl(header);
if (header >> 29 != 1) {
- pa_log(__FILE__": Unsupported SAP version.");
+ pa_log_warn("Unsupported SAP version.");
goto fail;
}
if ((header >> 25) & 1) {
- pa_log(__FILE__": Encrypted SAP not supported.");
+ pa_log_warn("Encrypted SAP not supported.");
goto fail;
}
if ((header >> 24) & 1) {
- pa_log(__FILE__": Compressed SAP not supported.");
+ pa_log_warn("Compressed SAP not supported.");
goto fail;
}
- six = (header >> 28) & 1;
- ac = (header >> 16) & 0xFF;
+ six = (header >> 28) & 1U;
+ ac = (header >> 16) & 0xFFU;
- k = 4 + (six ? 16 : 4) + ac*4;
- if (size < k) {
- pa_log(__FILE__": SAP packet too short (AD).");
+ k = 4 + (six ? 16U : 4U) + ac*4U;
+ if ((unsigned) size < k) {
+ pa_log_warn("SAP packet too short (AD).");
goto fail;
}
e = buf + k;
- size -= k;
+ size -= (int) k;
if ((unsigned) size >= sizeof(MIME_TYPE) && !strcmp(e, MIME_TYPE)) {
e += sizeof(MIME_TYPE);
- size -= sizeof(MIME_TYPE);
+ size -= (int) sizeof(MIME_TYPE);
} else if ((unsigned) size < sizeof(PA_SDP_HEADER)-1 || strncmp(e, PA_SDP_HEADER, sizeof(PA_SDP_HEADER)-1)) {
- pa_log(__FILE__": Invalid SDP header.");
+ pa_log_warn("Invalid SDP header.");
goto fail;
}
if (c->sdp_data)
pa_xfree(c->sdp_data);
-
- c->sdp_data = pa_xstrndup(e, size);
+
+ c->sdp_data = pa_xstrndup(e, (unsigned) size);
pa_xfree(buf);
-
+
*goodbye = !!((header >> 26) & 1);
-
+
return 0;
fail:
diff --git a/src/modules/rtp/sap.h b/src/modules/rtp/sap.h
index 987403e3..ae4ad426 100644
--- a/src/modules/rtp/sap.h
+++ b/src/modules/rtp/sap.h
@@ -1,21 +1,21 @@
#ifndef foosaphfoo
#define foosaphfoo
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -38,9 +38,9 @@ typedef struct pa_sap_context {
pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data);
void pa_sap_context_destroy(pa_sap_context *c);
-int pa_sap_send(pa_sap_context *c, int goodbye);
+int pa_sap_send(pa_sap_context *c, pa_bool_t goodbye);
pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd);
-int pa_sap_recv(pa_sap_context *c, int *goodbye);
+int pa_sap_recv(pa_sap_context *c, pa_bool_t *goodbye);
#endif
diff --git a/src/modules/rtp/sdp.c b/src/modules/rtp/sdp.c
index e707e4bb..3e61d9b8 100644
--- a/src/modules/rtp/sdp.c
+++ b/src/modules/rtp/sdp.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -23,47 +23,48 @@
#include <config.h>
#endif
-#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
-#include <arpa/inet.h>
#include <string.h>
#include <pulse/xmalloc.h>
+#include <pulse/util.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/arpa-inet.h>
#include "sdp.h"
#include "rtp.h"
-
char *pa_sdp_build(int af, const void *src, const void *dst, const char *name, uint16_t port, uint8_t payload, const pa_sample_spec *ss) {
uint32_t ntp;
- char buf_src[64], buf_dst[64];
- const char *u, *f, *a;
-
- assert(src);
- assert(dst);
- assert(af == AF_INET || af == AF_INET6);
-
- f = pa_rtp_format_to_string(ss->format);
- assert(f);
-
- if (!(u = getenv("USER")))
- if (!(u = getenv("USERNAME")))
- u = "-";
-
- ntp = time(NULL) + 2208988800U;
-
- a = inet_ntop(af, src, buf_src, sizeof(buf_src));
- assert(a);
- a = inet_ntop(af, dst, buf_dst, sizeof(buf_dst));
- assert(a);
-
+ char buf_src[64], buf_dst[64], un[64];
+ const char *u, *f;
+
+ pa_assert(src);
+ pa_assert(dst);
+
+#ifdef HAVE_IPV6
+ pa_assert(af == AF_INET || af == AF_INET6);
+#else
+ pa_assert(af == AF_INET);
+#endif
+
+ pa_assert_se(f = pa_rtp_format_to_string(ss->format));
+
+ if (!(u = pa_get_user_name(un, sizeof(un))))
+ u = "-";
+
+ ntp = (uint32_t) time(NULL) + 2208988800U;
+
+ pa_assert_se(inet_ntop(af, src, buf_src, sizeof(buf_src)));
+ pa_assert_se(inet_ntop(af, dst, buf_dst, sizeof(buf_dst)));
+
return pa_sprintf_malloc(
PA_SDP_HEADER
"o=%s %lu 0 IN %s %s\n"
@@ -84,8 +85,8 @@ char *pa_sdp_build(int af, const void *src, const void *dst, const char *name, u
static pa_sample_spec *parse_sdp_sample_spec(pa_sample_spec *ss, char *c) {
unsigned rate, channels;
- assert(ss);
- assert(c);
+ pa_assert(ss);
+ pa_assert(c);
if (pa_startswith(c, "L16/")) {
ss->format = PA_SAMPLE_S16BE;
@@ -103,10 +104,10 @@ static pa_sample_spec *parse_sdp_sample_spec(pa_sample_spec *ss, char *c) {
return NULL;
if (sscanf(c, "%u/%u", &rate, &channels) == 2) {
- ss->rate = rate;
- ss->channels = channels;
+ ss->rate = (uint32_t) rate;
+ ss->channels = (uint8_t) channels;
} else if (sscanf(c, "%u", &rate) == 2) {
- ss->rate = rate;
+ ss->rate = (uint32_t) rate;
ss->channels = 1;
} else
return NULL;
@@ -119,17 +120,17 @@ static pa_sample_spec *parse_sdp_sample_spec(pa_sample_spec *ss, char *c) {
pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
uint16_t port = 0;
- int ss_valid = 0;
+ pa_bool_t ss_valid = FALSE;
+
+ pa_assert(t);
+ pa_assert(i);
- assert(t);
- assert(i);
-
i->origin = i->session_name = NULL;
i->salen = 0;
i->payload = 255;
-
+
if (!pa_startswith(t, PA_SDP_HEADER)) {
- pa_log(__FILE__": Failed to parse SDP data: invalid header.");
+ pa_log("Failed to parse SDP data: invalid header.");
goto fail;
}
@@ -141,7 +142,7 @@ pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
l = strcspn(t, "\n");
if (l <= 2) {
- pa_log(__FILE__": Failed to parse SDP data: line too short: >%s<.", t);
+ pa_log("Failed to parse SDP data: line too short: >%s<.", t);
goto fail;
}
@@ -154,49 +155,51 @@ pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
size_t k;
k = l-8 > sizeof(a) ? sizeof(a) : l-8;
-
+
pa_strlcpy(a, t+9, k);
a[strcspn(a, "/")] = 0;
if (inet_pton(AF_INET, a, &((struct sockaddr_in*) &i->sa)->sin_addr) <= 0) {
- pa_log(__FILE__": Failed to parse SDP data: bad address: >%s<.", a);
+ pa_log("Failed to parse SDP data: bad address: >%s<.", a);
goto fail;
}
((struct sockaddr_in*) &i->sa)->sin_family = AF_INET;
((struct sockaddr_in*) &i->sa)->sin_port = 0;
i->salen = sizeof(struct sockaddr_in);
+#ifdef HAVE_IPV6
} else if (pa_startswith(t, "c=IN IP6 ")) {
char a[64];
size_t k;
k = l-8 > sizeof(a) ? sizeof(a) : l-8;
-
+
pa_strlcpy(a, t+9, k);
a[strcspn(a, "/")] = 0;
if (inet_pton(AF_INET6, a, &((struct sockaddr_in6*) &i->sa)->sin6_addr) <= 0) {
- pa_log(__FILE__": Failed to parse SDP data: bad address: >%s<.", a);
+ pa_log("Failed to parse SDP data: bad address: >%s<.", a);
goto fail;
}
((struct sockaddr_in6*) &i->sa)->sin6_family = AF_INET6;
((struct sockaddr_in6*) &i->sa)->sin6_port = 0;
i->salen = sizeof(struct sockaddr_in6);
+#endif
} else if (pa_startswith(t, "m=audio ")) {
if (i->payload > 127) {
int _port, _payload;
-
+
if (sscanf(t+8, "%i RTP/AVP %i", &_port, &_payload) == 2) {
if (_port <= 0 || _port > 0xFFFF) {
- pa_log(__FILE__": Failed to parse SDP data: invalid port %i.", _port);
+ pa_log("Failed to parse SDP data: invalid port %i.", _port);
goto fail;
}
if (_payload < 0 || _payload > 127) {
- pa_log(__FILE__": Failed to parse SDP data: invalid payload %i.", _payload);
+ pa_log("Failed to parse SDP data: invalid payload %i.", _payload);
goto fail;
}
@@ -204,7 +207,7 @@ pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
i->payload = (uint8_t) _payload;
if (pa_rtp_sample_spec_from_payload(i->payload, &i->sample_spec))
- ss_valid = 1;
+ ss_valid = TRUE;
}
}
} else if (pa_startswith(t, "a=rtpmap:")) {
@@ -216,28 +219,28 @@ pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
if (sscanf(t+9, "%i %64c", &_payload, c) == 2) {
if (_payload < 0 || _payload > 127) {
- pa_log(__FILE__": Failed to parse SDP data: invalid payload %i.", _payload);
+ pa_log("Failed to parse SDP data: invalid payload %i.", _payload);
goto fail;
}
if (_payload == i->payload) {
c[strcspn(c, "\n")] = 0;
-
+
if (parse_sdp_sample_spec(&i->sample_spec, c))
- ss_valid = 1;
+ ss_valid = TRUE;
}
}
}
}
-
+
t += l;
-
+
if (*t == '\n')
t++;
}
if (!i->origin || (!is_goodbye && (!i->salen || i->payload > 127 || !ss_valid || port == 0))) {
- pa_log(__FILE__": Failed to parse SDP data: missing data.");
+ pa_log("Failed to parse SDP data: missing data.");
goto fail;
}
@@ -245,7 +248,7 @@ pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
((struct sockaddr_in*) &i->sa)->sin_port = htons(port);
else
((struct sockaddr_in6*) &i->sa)->sin6_port = htons(port);
-
+
return i;
fail:
@@ -256,7 +259,7 @@ fail:
}
void pa_sdp_info_destroy(pa_sdp_info *i) {
- assert(i);
+ pa_assert(i);
pa_xfree(i->origin);
pa_xfree(i->session_name);
diff --git a/src/modules/rtp/sdp.h b/src/modules/rtp/sdp.h
index b95ca633..4cb3b203 100644
--- a/src/modules/rtp/sdp.h
+++ b/src/modules/rtp/sdp.h
@@ -1,21 +1,21 @@
#ifndef foosdphfoo
#define foosdphfoo
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
diff --git a/src/modules/udev-util.c b/src/modules/udev-util.c
new file mode 100644
index 00000000..356f7736
--- /dev/null
+++ b/src/modules/udev-util.c
@@ -0,0 +1,301 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <libudev.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/proplist.h>
+
+#include <pulsecore/log.h>
+#include <pulsecore/core-util.h>
+
+#include "udev-util.h"
+
+static int read_id(struct udev_device *d, const char *n) {
+ const char *v;
+ unsigned u;
+
+ pa_assert(d);
+ pa_assert(n);
+
+ if (!(v = udev_device_get_property_value(d, n)))
+ return -1;
+
+ if (pa_startswith(v, "0x"))
+ v += 2;
+
+ if (!*v)
+ return -1;
+
+ if (sscanf(v, "%04x", &u) != 1)
+ return -1;
+
+ if (u > 0xFFFFU)
+ return -1;
+
+ return u;
+}
+
+static int dehex(char x) {
+ if (x >= '0' && x <= '9')
+ return x - '0';
+
+ if (x >= 'A' && x <= 'F')
+ return x - 'A' + 10;
+
+ if (x >= 'a' && x <= 'f')
+ return x - 'a' + 10;
+
+ return -1;
+}
+
+static void proplist_sets_unescape(pa_proplist *p, const char *prop, const char *s) {
+ const char *f;
+ char *t, *r;
+ int c = 0;
+
+ enum {
+ TEXT,
+ BACKSLASH,
+ EX,
+ FIRST
+ } state = TEXT;
+
+ /* The resulting string is definitely shorter than the source string */
+ r = pa_xnew(char, strlen(s)+1);
+
+ for (f = s, t = r; *f; f++) {
+
+ switch (state) {
+
+ case TEXT:
+ if (*f == '\\')
+ state = BACKSLASH;
+ else
+ *(t++) = *f;
+ break;
+
+ case BACKSLASH:
+ if (*f == 'x')
+ state = EX;
+ else {
+ *(t++) = '\\';
+ *(t++) = *f;
+ state = TEXT;
+ }
+ break;
+
+ case EX:
+ c = dehex(*f);
+
+ if (c < 0) {
+ *(t++) = '\\';
+ *(t++) = 'x';
+ *(t++) = *f;
+ state = TEXT;
+ } else
+ state = FIRST;
+
+ break;
+
+ case FIRST: {
+ int d = dehex(*f);
+
+ if (d < 0) {
+ *(t++) = '\\';
+ *(t++) = 'x';
+ *(t++) = *(f-1);
+ *(t++) = *f;
+ } else
+ *(t++) = (char) (c << 4) | d;
+
+ state = TEXT;
+ break;
+ }
+ }
+ }
+
+ switch (state) {
+
+ case TEXT:
+ break;
+
+ case BACKSLASH:
+ *(t++) = '\\';
+ break;
+
+ case EX:
+ *(t++) = '\\';
+ *(t++) = 'x';
+ break;
+
+ case FIRST:
+ *(t++) = '\\';
+ *(t++) = 'x';
+ *(t++) = *(f-1);
+ break;
+ }
+
+ *t = 0;
+
+ pa_proplist_sets(p, prop, r);
+ pa_xfree(r);
+}
+
+int pa_udev_get_info(int card_idx, pa_proplist *p) {
+ int r = -1;
+ struct udev *udev;
+ struct udev_device *card = NULL;
+ char *t;
+ const char *v;
+ int id;
+
+ pa_assert(p);
+ pa_assert(card_idx >= 0);
+
+ if (!(udev = udev_new())) {
+ pa_log_error("Failed to allocate udev context.");
+ goto finish;
+ }
+
+ t = pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev), card_idx);
+ card = udev_device_new_from_syspath(udev, t);
+ pa_xfree(t);
+
+ if (!card) {
+ pa_log_error("Failed to get card object.");
+ goto finish;
+ }
+
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS_PATH))
+ if (((v = udev_device_get_property_value(card, "ID_PATH")) && *v) ||
+ (v = udev_device_get_devpath(card)))
+ pa_proplist_sets(p, PA_PROP_DEVICE_BUS_PATH, v);
+
+ if (!pa_proplist_contains(p, "sysfs.path"))
+ if ((v = udev_device_get_devpath(card)))
+ pa_proplist_sets(p, "sysfs.path", v);
+
+ if (!pa_proplist_contains(p, "udev.id"))
+ if ((v = udev_device_get_property_value(card, "ID_ID")) && *v)
+ pa_proplist_sets(p, "udev.id", v);
+
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS))
+ if ((v = udev_device_get_property_value(card, "ID_BUS")) && *v)
+ pa_proplist_sets(p, PA_PROP_DEVICE_BUS, v);
+
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_ID))
+ if ((id = read_id(card, "ID_VENDOR_ID")) > 0)
+ pa_proplist_setf(p, PA_PROP_DEVICE_VENDOR_ID, "%04x", id);
+
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_NAME)) {
+ if ((v = udev_device_get_property_value(card, "ID_VENDOR_FROM_DATABASE")) && *v)
+ pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
+ else if ((v = udev_device_get_property_value(card, "ID_VENDOR_ENC")) && *v)
+ proplist_sets_unescape(p, PA_PROP_DEVICE_VENDOR_NAME, v);
+ else if ((v = udev_device_get_property_value(card, "ID_VENDOR")) && *v)
+ pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
+ }
+
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_ID))
+ if ((id = read_id(card, "ID_MODEL_ID")) >= 0)
+ pa_proplist_setf(p, PA_PROP_DEVICE_PRODUCT_ID, "%04x", id);
+
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_NAME)) {
+ if ((v = udev_device_get_property_value(card, "ID_MODEL_FROM_DATABASE")) && *v)
+ pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
+ else if ((v = udev_device_get_property_value(card, "ID_MODEL_ENC")) && *v)
+ proplist_sets_unescape(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
+ else if ((v = udev_device_get_property_value(card, "ID_MODEL")) && *v)
+ pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
+ }
+
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_SERIAL))
+ if ((v = udev_device_get_property_value(card, "ID_SERIAL")) && *v)
+ pa_proplist_sets(p, PA_PROP_DEVICE_SERIAL, v);
+
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_CLASS))
+ if ((v = udev_device_get_property_value(card, "SOUND_CLASS")) && *v)
+ pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, v);
+
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_FORM_FACTOR))
+ if ((v = udev_device_get_property_value(card, "SOUND_FORM_FACTOR")) && *v)
+ pa_proplist_sets(p, PA_PROP_DEVICE_FORM_FACTOR, v);
+
+ /* This is normaly not set by the udev rules but may be useful to
+ * allow administrators to overwrite the device description.*/
+ if (!pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION))
+ if ((v = udev_device_get_property_value(card, "SOUND_DESCRIPTION")) && *v)
+ pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, v);
+
+ r = 0;
+
+finish:
+
+ if (card)
+ udev_device_unref(card);
+
+ if (udev)
+ udev_unref(udev);
+
+ return r;
+}
+
+char* pa_udev_get_property(int card_idx, const char *name) {
+ struct udev *udev;
+ struct udev_device *card = NULL;
+ char *t, *r = NULL;
+ const char *v;
+
+ pa_assert(card_idx >= 0);
+ pa_assert(name);
+
+ if (!(udev = udev_new())) {
+ pa_log_error("Failed to allocate udev context.");
+ goto finish;
+ }
+
+ t = pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev), card_idx);
+ card = udev_device_new_from_syspath(udev, t);
+ pa_xfree(t);
+
+ if (!card) {
+ pa_log_error("Failed to get card object.");
+ goto finish;
+ }
+
+ if ((v = udev_device_get_property_value(card, name)) && *v)
+ r = pa_xstrdup(v);
+
+finish:
+
+ if (card)
+ udev_device_unref(card);
+
+ if (udev)
+ udev_unref(udev);
+
+ return r;
+}
diff --git a/src/modules/udev-util.h b/src/modules/udev-util.h
new file mode 100644
index 00000000..a978178f
--- /dev/null
+++ b/src/modules/udev-util.h
@@ -0,0 +1,31 @@
+#ifndef fooudevutilhfoo
+#define fooudevutilhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+
+#include <pulse/proplist.h>
+
+int pa_udev_get_info(int card_idx, pa_proplist *p);
+char* pa_udev_get_property(int card_idx, const char *name);
+
+#endif
diff --git a/src/modules/module-x11-bell.c b/src/modules/x11/module-x11-bell.c
index 7ac5f558..37ab2e78 100644
--- a/src/modules/module-x11-bell.c
+++ b/src/modules/x11/module-x11-bell.c
@@ -1,18 +1,18 @@
-/* $Id$ */
-
/***
This file is part of PulseAudio.
-
+
+ Copyright 2004-2006 Lennart Poettering
+
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
+ by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
-
+
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -24,39 +24,25 @@
#endif
#include <stdio.h>
-#include <assert.h>
#include <stdlib.h>
-#include <string.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <pulse/xmalloc.h>
-#include <pulsecore/iochannel.h>
-#include <pulsecore/sink.h>
#include <pulsecore/core-scache.h>
#include <pulsecore/modargs.h>
-#include <pulsecore/namereg.h>
#include <pulsecore/log.h>
#include <pulsecore/x11wrap.h>
#include "module-x11-bell-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("X11 Bell interceptor")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("sink=<sink to connect to> sample=<sample name> display=<X11 display>")
-
-struct userdata {
- pa_core *core;
- int xkb_event_base;
- char *sink_name;
- char *scache_item;
-
- pa_x11_wrapper *x11_wrapper;
- pa_x11_client *x11_client;
-};
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("X11 bell interceptor");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE("sink=<sink to connect to> sample=<sample name> display=<X11 display>");
static const char* const valid_modargs[] = {
"sink",
@@ -65,72 +51,97 @@ static const char* const valid_modargs[] = {
NULL
};
-static int ring_bell(struct userdata *u, int percent) {
- pa_sink *s;
- assert(u);
+struct userdata {
+ pa_core *core;
+ pa_module *module;
- if (!(s = pa_namereg_get(u->core, u->sink_name, PA_NAMEREG_SINK, 1))) {
- pa_log(__FILE__": Invalid sink: %s", u->sink_name);
- return -1;
- }
+ int xkb_event_base;
- pa_scache_play_item(u->core, u->scache_item, s, (percent*PA_VOLUME_NORM)/100);
- return 0;
-}
+ char *sink_name;
+ char *scache_item;
+
+ pa_x11_wrapper *x11_wrapper;
+ pa_x11_client *x11_client;
+};
-static int x11_event_callback(pa_x11_wrapper *w, XEvent *e, void *userdata) {
+static int x11_event_cb(pa_x11_wrapper *w, XEvent *e, void *userdata) {
XkbBellNotifyEvent *bne;
struct userdata *u = userdata;
- assert(w && e && u && u->x11_wrapper == w);
+
+ pa_assert(w);
+ pa_assert(e);
+ pa_assert(u);
+ pa_assert(u->x11_wrapper == w);
if (((XkbEvent*) e)->any.xkb_type != XkbBellNotify)
return 0;
bne = (XkbBellNotifyEvent*) e;
- if (ring_bell(u, bne->percent) < 0) {
- pa_log_info(__FILE__": Ringing bell failed, reverting to X11 device bell.");
+ if (pa_scache_play_item_by_name(u->core, u->scache_item, u->sink_name, ((pa_volume_t) bne->percent*PA_VOLUME_NORM)/100U, NULL, NULL) < 0) {
+ pa_log_info("Ringing bell failed, reverting to X11 device bell.");
XkbForceDeviceBell(pa_x11_wrapper_get_display(w), bne->device, bne->bell_class, bne->bell_id, bne->percent);
}
return 1;
}
-int pa__init(pa_core *c, pa_module*m) {
+static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(w);
+ pa_assert(u);
+ pa_assert(u->x11_wrapper == w);
+
+ if (u->x11_client)
+ pa_x11_client_free(u->x11_client);
+
+ if (u->x11_wrapper)
+ pa_x11_wrapper_unref(u->x11_wrapper);
+
+ u->x11_client = NULL;
+ u->x11_wrapper = NULL;
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+int pa__init(pa_module*m) {
+
struct userdata *u = NULL;
pa_modargs *ma = NULL;
int major, minor;
unsigned int auto_ctrls, auto_values;
- assert(c && m);
+
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments");
+ pa_log("Failed to parse module arguments");
goto fail;
}
-
- m->userdata = u = pa_xmalloc(sizeof(struct userdata));
- u->core = c;
- u->scache_item = pa_xstrdup(pa_modargs_get_value(ma, "sample", "x11-bell"));
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->scache_item = pa_xstrdup(pa_modargs_get_value(ma, "sample", "bell-window-system"));
u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
u->x11_client = NULL;
- if (!(u->x11_wrapper = pa_x11_wrapper_get(c, pa_modargs_get_value(ma, "display", NULL))))
+ if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
goto fail;
major = XkbMajorVersion;
minor = XkbMinorVersion;
-
+
if (!XkbLibraryVersion(&major, &minor)) {
- pa_log(__FILE__": XkbLibraryVersion() failed");
+ pa_log("XkbLibraryVersion() failed");
goto fail;
}
major = XkbMajorVersion;
minor = XkbMinorVersion;
-
if (!XkbQueryExtension(pa_x11_wrapper_get_display(u->x11_wrapper), NULL, &u->xkb_event_base, NULL, &major, &minor)) {
- pa_log(__FILE__": XkbQueryExtension() failed");
+ pa_log("XkbQueryExtension() failed");
goto fail;
}
@@ -139,23 +150,28 @@ int pa__init(pa_core *c, pa_module*m) {
XkbSetAutoResetControls(pa_x11_wrapper_get_display(u->x11_wrapper), XkbAudibleBellMask, &auto_ctrls, &auto_values);
XkbChangeEnabledControls(pa_x11_wrapper_get_display(u->x11_wrapper), XkbUseCoreKbd, XkbAudibleBellMask, 0);
- u->x11_client = pa_x11_client_new(u->x11_wrapper, x11_event_callback, u);
-
+ u->x11_client = pa_x11_client_new(u->x11_wrapper, x11_event_cb, x11_kill_cb, u);
+
pa_modargs_free(ma);
-
+
return 0;
-
+
fail:
if (ma)
pa_modargs_free(ma);
- if (m->userdata)
- pa__done(c, m);
+
+ pa__done(m);
+
return -1;
}
-void pa__done(pa_core *c, pa_module*m) {
- struct userdata *u = m->userdata;
- assert(c && m && u);
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
pa_xfree(u->scache_item);
pa_xfree(u->sink_name);
diff --git a/src/modules/x11/module-x11-cork-request.c b/src/modules/x11/module-x11-cork-request.c
new file mode 100644
index 00000000..0e67db00
--- /dev/null
+++ b/src/modules/x11/module-x11-cork-request.c
@@ -0,0 +1,187 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <X11/Xlib.h>
+#include <X11/extensions/XTest.h>
+#include <X11/XF86keysym.h>
+#include <X11/keysym.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/x11wrap.h>
+#include <pulsecore/core-util.h>
+
+#include "module-x11-cork-request-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("Synthesize X11 media key events when cork/uncork is requested");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE("display=<X11 display>");
+
+static const char* const valid_modargs[] = {
+ "display",
+ NULL
+};
+
+struct userdata {
+ pa_module *module;
+
+ pa_x11_wrapper *x11_wrapper;
+ pa_x11_client *x11_client;
+
+ pa_hook_slot *hook_slot;
+};
+
+static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(w);
+ pa_assert(u);
+ pa_assert(u->x11_wrapper == w);
+
+ if (u->x11_client) {
+ pa_x11_client_free(u->x11_client);
+ u->x11_client = NULL;
+ }
+
+ if (u->x11_wrapper) {
+ pa_x11_wrapper_unref(u->x11_wrapper);
+ u->x11_wrapper = NULL;
+ }
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+static pa_hook_result_t sink_input_send_event_hook_cb(
+ pa_core *c,
+ pa_sink_input_send_event_hook_data *data,
+ struct userdata *u) {
+
+ KeySym sym;
+ KeyCode code;
+ Display *display;
+
+ pa_assert(c);
+ pa_assert(data);
+ pa_assert(u);
+
+ if (pa_streq(data->event, PA_STREAM_EVENT_REQUEST_CORK))
+ sym = XF86XK_AudioPause;
+ else if (pa_streq(data->event, PA_STREAM_EVENT_REQUEST_UNCORK))
+ sym = XF86XK_AudioPlay;
+ else
+ return PA_HOOK_OK;
+
+ pa_log_debug("Triggering X11 keysym: %s", XKeysymToString(sym));
+
+ display = pa_x11_wrapper_get_display(u->x11_wrapper);
+ code = XKeysymToKeycode(display, sym);
+
+ XTestFakeKeyEvent(display, code, True, CurrentTime);
+ XSync(display, False);
+
+ XTestFakeKeyEvent(display, code, False, CurrentTime);
+ XSync(display, False);
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module *m) {
+ struct userdata *u;
+ pa_modargs *ma;
+ int xtest_event_base, xtest_error_base;
+ int major_version, minor_version;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+
+ if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
+ goto fail;
+
+ if (!XTestQueryExtension(
+ pa_x11_wrapper_get_display(u->x11_wrapper),
+ &xtest_event_base, &xtest_error_base,
+ &major_version, &minor_version)) {
+
+ pa_log("XTest extension not supported.");
+ goto fail;
+ }
+
+ pa_log_debug("XTest %i.%i supported.", major_version, minor_version);
+
+ u->x11_client = pa_x11_client_new(u->x11_wrapper, NULL, x11_kill_cb, u);
+
+ u->hook_slot = pa_hook_connect(
+ &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT],
+ PA_HOOK_NORMAL,
+ (pa_hook_cb_t) sink_input_send_event_hook_cb, u);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata*u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->x11_client)
+ pa_x11_client_free(u->x11_client);
+
+ if (u->x11_wrapper)
+ pa_x11_wrapper_unref(u->x11_wrapper);
+
+ if (u->hook_slot)
+ pa_hook_slot_free(u->hook_slot);
+
+ pa_xfree(u);
+}
diff --git a/src/modules/x11/module-x11-publish.c b/src/modules/x11/module-x11-publish.c
new file mode 100644
index 00000000..716fe0b8
--- /dev/null
+++ b/src/modules/x11/module-x11-publish.c
@@ -0,0 +1,246 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <xcb/xcb.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/x11wrap.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/native-common.h>
+#include <pulsecore/auth-cookie.h>
+#include <pulsecore/x11prop.h>
+#include <pulsecore/strlist.h>
+#include <pulsecore/protocol-native.h>
+
+#include "module-x11-publish-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("X11 credential publisher");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+ "display=<X11 display> "
+ "sink=<Sink to publish> "
+ "source=<Source to publish> "
+ "cookie=<Cookie file to publish> ");
+
+static const char* const valid_modargs[] = {
+ "display",
+ "sink",
+ "source",
+ "cookie",
+ NULL
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_native_protocol *protocol;
+
+ char *id;
+ pa_auth_cookie *auth_cookie;
+
+ pa_x11_wrapper *x11_wrapper;
+ pa_x11_client *x11_client;
+
+ pa_hook_slot *hook_slot;
+};
+
+static void publish_servers(struct userdata *u, pa_strlist *l) {
+
+ int screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
+
+ if (l) {
+ char *s;
+
+ l = pa_strlist_reverse(l);
+ s = pa_strlist_tostring(l);
+ pa_strlist_reverse(l);
+
+ pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SERVER", s);
+ pa_xfree(s);
+ } else
+ pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SERVER");
+}
+
+static pa_hook_result_t servers_changed_cb(void *hook_data, void *call_data, void *slot_data) {
+ pa_strlist *servers = call_data;
+ struct userdata *u = slot_data;
+ char t[256];
+ int screen;
+
+ pa_assert(u);
+
+ screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
+ if (!pa_x11_get_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID", t, sizeof(t)) || strcmp(t, u->id)) {
+ pa_log_warn("PulseAudio information vanished from X11!");
+ return PA_HOOK_OK;
+ }
+
+ publish_servers(u, servers);
+ return PA_HOOK_OK;
+}
+
+static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
+ struct userdata *u = userdata;
+
+ pa_assert(w);
+ pa_assert(u);
+ pa_assert(u->x11_wrapper == w);
+
+ if (u->x11_client)
+ pa_x11_client_free(u->x11_client);
+
+ if (u->x11_wrapper)
+ pa_x11_wrapper_unref(u->x11_wrapper);
+
+ u->x11_client = NULL;
+ u->x11_wrapper = NULL;
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+int pa__init(pa_module*m) {
+ struct userdata *u;
+ pa_modargs *ma = NULL;
+ char *mid, *sid;
+ char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
+ const char *t;
+ int screen;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->protocol = pa_native_protocol_get(m->core);
+ u->id = NULL;
+ u->auth_cookie = NULL;
+ u->x11_client = NULL;
+ u->x11_wrapper = NULL;
+
+ u->hook_slot = pa_hook_connect(&pa_native_protocol_hooks(u->protocol)[PA_NATIVE_HOOK_SERVERS_CHANGED], PA_HOOK_NORMAL, servers_changed_cb, u);
+
+ if (!(u->auth_cookie = pa_auth_cookie_get(m->core, pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), PA_NATIVE_COOKIE_LENGTH)))
+ goto fail;
+
+ if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
+ goto fail;
+
+ screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
+ mid = pa_machine_id();
+ u->id = pa_sprintf_malloc("%lu@%s/%lu", (unsigned long) getuid(), mid, (unsigned long) getpid());
+ pa_xfree(mid);
+
+ pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID", u->id);
+
+ if ((sid = pa_session_id())) {
+ pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SESSION_ID", sid);
+ pa_xfree(sid);
+ }
+
+ publish_servers(u, pa_native_protocol_servers(u->protocol));
+
+ if ((t = pa_modargs_get_value(ma, "source", NULL)))
+ pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SOURCE", t);
+
+ if ((t = pa_modargs_get_value(ma, "sink", NULL)))
+ pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SINK", t);
+
+ pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_COOKIE",
+ pa_hexstr(pa_auth_cookie_read(u->auth_cookie, PA_NATIVE_COOKIE_LENGTH), PA_NATIVE_COOKIE_LENGTH, hx, sizeof(hx)));
+
+ u->x11_client = pa_x11_client_new(u->x11_wrapper, NULL, x11_kill_cb, u);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata*u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->x11_client)
+ pa_x11_client_free(u->x11_client);
+
+ if (u->x11_wrapper) {
+ char t[256];
+ int screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
+
+ /* Yes, here is a race condition */
+ if (!pa_x11_get_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID", t, sizeof(t)) || strcmp(t, u->id))
+ pa_log_warn("PulseAudio information vanished from X11!");
+ else {
+ pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID");
+ pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SERVER");
+ pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SINK");
+ pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SOURCE");
+ pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_COOKIE");
+ pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SESSION_ID");
+ xcb_flush(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper));
+ }
+
+ pa_x11_wrapper_unref(u->x11_wrapper);
+ }
+
+ if (u->auth_cookie)
+ pa_auth_cookie_unref(u->auth_cookie);
+
+ if (u->hook_slot)
+ pa_hook_slot_free(u->hook_slot);
+
+ if (u->protocol)
+ pa_native_protocol_unref(u->protocol);
+
+ pa_xfree(u->id);
+ pa_xfree(u);
+}
diff --git a/src/modules/x11/module-x11-xsmp.c b/src/modules/x11/module-x11-xsmp.c
new file mode 100644
index 00000000..6a6116ff
--- /dev/null
+++ b/src/modules/x11/module-x11-xsmp.c
@@ -0,0 +1,249 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <X11/Xlib.h>
+#include <X11/SM/SMlib.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/util.h>
+
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/x11wrap.h>
+
+#include "module-x11-xsmp-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("X11 session management");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE("session_manager=<session manager string> display=<X11 display>");
+
+static pa_bool_t ice_in_use = FALSE;
+
+static const char* const valid_modargs[] = {
+ "session_manager",
+ "display",
+ NULL
+};
+
+struct userdata {
+ pa_core *core;
+ pa_module *module;
+ pa_client *client;
+ SmcConn connection;
+ pa_x11_wrapper *x11;
+};
+
+static void die_cb(SmcConn connection, SmPointer client_data){
+ struct userdata *u = client_data;
+ pa_assert(u);
+
+ pa_log_debug("Got die message from XSMP.");
+
+ pa_x11_wrapper_kill(u->x11);
+
+ pa_x11_wrapper_unref(u->x11);
+ u->x11 = NULL;
+
+ pa_module_unload_request(u->module, TRUE);
+}
+
+static void save_complete_cb(SmcConn connection, SmPointer client_data) {
+}
+
+static void shutdown_cancelled_cb(SmcConn connection, SmPointer client_data) {
+ SmcSaveYourselfDone(connection, True);
+}
+
+static void save_yourself_cb(SmcConn connection, SmPointer client_data, int save_type, Bool _shutdown, int interact_style, Bool fast) {
+ SmcSaveYourselfDone(connection, True);
+}
+
+static void ice_io_cb(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
+ IceConn connection = userdata;
+
+ if (IceProcessMessages(connection, NULL, NULL) == IceProcessMessagesIOError) {
+ IceSetShutdownNegotiation(connection, False);
+ IceCloseConnection(connection);
+ }
+}
+
+static void new_ice_connection(IceConn connection, IcePointer client_data, Bool opening, IcePointer *watch_data) {
+ struct pa_core *c = client_data;
+
+ if (opening)
+ *watch_data = c->mainloop->io_new(
+ c->mainloop,
+ IceConnectionNumber(connection),
+ PA_IO_EVENT_INPUT,
+ ice_io_cb,
+ connection);
+ else
+ c->mainloop->io_free(*watch_data);
+}
+
+int pa__init(pa_module*m) {
+
+ pa_modargs *ma = NULL;
+ char t[256], *vendor, *client_id;
+ SmcCallbacks callbacks;
+ SmProp prop_program, prop_user;
+ SmProp *prop_list[2];
+ SmPropValue val_program, val_user;
+ struct userdata *u;
+ const char *e;
+ pa_client_new_data data;
+
+ pa_assert(m);
+
+ if (ice_in_use) {
+ pa_log("module-x11-xsmp may not be loaded twice.");
+ return -1;
+ }
+
+ IceAddConnectionWatch(new_ice_connection, m->core);
+ ice_in_use = TRUE;
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->client = NULL;
+ u->connection = NULL;
+ u->x11 = NULL;
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ if (!(u->x11 = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
+ goto fail;
+
+ e = pa_modargs_get_value(ma, "session_manager", NULL);
+
+ if (!e && !getenv("SESSION_MANAGER")) {
+ pa_log("X11 session manager not running.");
+ goto fail;
+ }
+
+ memset(&callbacks, 0, sizeof(callbacks));
+ callbacks.die.callback = die_cb;
+ callbacks.die.client_data = u;
+ callbacks.save_yourself.callback = save_yourself_cb;
+ callbacks.save_yourself.client_data = m->core;
+ callbacks.save_complete.callback = save_complete_cb;
+ callbacks.save_complete.client_data = m->core;
+ callbacks.shutdown_cancelled.callback = shutdown_cancelled_cb;
+ callbacks.shutdown_cancelled.client_data = m->core;
+
+ if (!(u->connection = SmcOpenConnection(
+ (char*) e, m->core,
+ SmProtoMajor, SmProtoMinor,
+ SmcSaveYourselfProcMask | SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
+ &callbacks, NULL, &client_id,
+ sizeof(t), t))) {
+
+ pa_log("Failed to open connection to session manager: %s", t);
+ goto fail;
+ }
+
+ prop_program.name = (char*) SmProgram;
+ prop_program.type = (char*) SmARRAY8;
+ val_program.value = (char*) PACKAGE_NAME;
+ val_program.length = (int) strlen(val_program.value);
+ prop_program.num_vals = 1;
+ prop_program.vals = &val_program;
+ prop_list[0] = &prop_program;
+
+ prop_user.name = (char*) SmUserID;
+ prop_user.type = (char*) SmARRAY8;
+ pa_get_user_name(t, sizeof(t));
+ val_user.value = t;
+ val_user.length = (int) strlen(val_user.value);
+ prop_user.num_vals = 1;
+ prop_user.vals = &val_user;
+ prop_list[1] = &prop_user;
+
+ SmcSetProperties(u->connection, PA_ELEMENTSOF(prop_list), prop_list);
+
+ pa_log_info("Connected to session manager '%s' as '%s'.", vendor = SmcVendor(u->connection), client_id);
+
+ pa_client_new_data_init(&data);
+ data.module = m;
+ data.driver = __FILE__;
+ pa_proplist_setf(data.proplist, PA_PROP_APPLICATION_NAME, "XSMP Session on %s as %s", vendor, client_id);
+ pa_proplist_sets(data.proplist, "xsmp.vendor", vendor);
+ pa_proplist_sets(data.proplist, "xsmp.client.id", client_id);
+ u->client = pa_client_new(u->core, &data);
+ pa_client_new_data_done(&data);
+
+ free(vendor);
+ free(client_id);
+
+ if (!u->client)
+ goto fail;
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if ((u = m->userdata)) {
+
+ if (u->connection)
+ SmcCloseConnection(u->connection, 0, NULL);
+
+ if (u->client)
+ pa_client_free(u->client);
+
+ if (u->x11)
+ pa_x11_wrapper_unref(u->x11);
+
+ pa_xfree(u);
+ }
+
+ if (ice_in_use) {
+ IceRemoveConnectionWatch(new_ice_connection, m->core);
+ ice_in_use = FALSE;
+ }
+}