[pulseaudio-commits] [SCM] PulseAudio Sound Server branch, master, updated. v1.0-dev-211-g99e37b6

Colin Guthrie gitmailer-noreply at 0pointer.de
Thu Mar 24 02:30:45 PDT 2011


This is an automated email from the git hooks/post-receive script. It was
generated because of a push to the "PulseAudio Sound Server" repository.

The master branch has been updated
      from  11da057d81a1a5d28134d1cc8cc2c6469524561d (commit)

- Log -----------------------------------------------------------------
99e37b6 bluetooth: Get rid of warnings about unused stuff when building against a D-Bus version that doesn't have fd-passing support.
51163d0 bluetooth: Don't log an error if an endpoint type is disabled.
9334833 Move compile-time checks around pa_run_from_build_tree to core-util
72323bc win32: Simplify dl_search_path code
ae35ec1 Merge remote-tracking branch 'zonique/osx'
fc339a6 osx: add routines for real-time thread scheduling
85c5a2d configure.ac: add --mac-universal directive for OS X
00d96c0 osx: add -headerpad_max_install_names to LDFLAGS
249384c osx: re-order module locations
9a4ae4c module-coreaudio-detect: fix variable assignment in pa__done()
-----------------------------------------------------------------------

Summary of changes:
 configure.ac                                       |   13 ++++++
 src/Makefile.am                                    |   10 +++-
 src/daemon/daemon-conf.c                           |   30 +++----------
 src/modules/alsa/alsa-mixer.c                      |   12 ++----
 src/modules/bluetooth/bluetooth-util.c             |   43 ++++++++++++++-----
 src/modules/bluetooth/bluetooth-util.h             |    2 +
 src/modules/{ => macosx}/module-bonjour-publish.c  |    0
 .../module-coreaudio-detect.c                      |    4 +-
 .../module-coreaudio-device.c                      |    0
 src/pulsecore/core-util.c                          |   41 +++++++++++++++++--
 src/pulsecore/core-util.h                          |    5 ++
 11 files changed, 109 insertions(+), 51 deletions(-)
 rename src/modules/{ => macosx}/module-bonjour-publish.c (100%)
 rename src/modules/{coreaudio => macosx}/module-coreaudio-detect.c (99%)
 rename src/modules/{coreaudio => macosx}/module-coreaudio-device.c (100%)

-----------------------------------------------------------------------

commit 9a4ae4cbd5ff324c354a3decbe1bae414d92e6a9
Author: Daniel Mack <zonque at gmail.com>
Date:   Mon Mar 14 13:39:30 2011 +0100

    module-coreaudio-detect: fix variable assignment in pa__done()
    
    Do not dereference 'u' prior to its assignment.

diff --git a/src/modules/coreaudio/module-coreaudio-detect.c b/src/modules/coreaudio/module-coreaudio-detect.c
index 1d26b84..263fc72 100644
--- a/src/modules/coreaudio/module-coreaudio-detect.c
+++ b/src/modules/coreaudio/module-coreaudio-detect.c
@@ -232,12 +232,14 @@ fail:
 
 void pa__done(pa_module *m) {
     struct userdata *u;
-    struct ca_device *dev = u->devices;
+    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;

commit 249384c7a2bf0f17f8869998ea019c9d3d22b0da
Author: Daniel Mack <zonque at gmail.com>
Date:   Mon Mar 14 14:05:04 2011 +0100

    osx: re-order module locations
    
    move all Mac OS X related modules to own location.

diff --git a/src/Makefile.am b/src/Makefile.am
index 78ee231..ff98ddb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1541,13 +1541,13 @@ module_oss_la_LIBADD = $(MODULE_LIBADD) liboss-util.la
 
 # COREAUDIO
 
-module_coreaudio_detect_la_SOURCES = modules/coreaudio/module-coreaudio-detect.c
+module_coreaudio_detect_la_SOURCES = modules/macosx/module-coreaudio-detect.c
 module_coreaudio_detect_la_LDFLAGS = $(MODULE_LDFLAGS) \
 		-Wl,-framework -Wl,Cocoa -framework CoreAudio \
 		-Wl,-framework -Wl,AudioUnit -framework AudioUnit
 module_coreaudio_detect_la_LIBADD = $(MODULE_LIBADD)
 
-module_coreaudio_device_la_SOURCES = modules/coreaudio/module-coreaudio-device.c
+module_coreaudio_device_la_SOURCES = modules/macosx/module-coreaudio-device.c
 module_coreaudio_device_la_LDFLAGS = $(MODULE_LDFLAGS) \
 		-Wl,-framework -Wl,Cocoa -framework CoreAudio \
 		-Wl,-framework -Wl,AudioUnit -framework AudioUnit
@@ -1618,7 +1618,7 @@ module_zeroconf_discover_la_CFLAGS = $(AM_CFLAGS) $(AVAHI_CFLAGS)
 
 # Bonjour
 
-module_bonjour_publish_la_SOURCES = modules/module-bonjour-publish.c
+module_bonjour_publish_la_SOURCES = modules/macosx/module-bonjour-publish.c
 module_bonjour_publish_la_LDFLAGS = $(MODULE_LDFLAGS) \
 			-Wl,-framework -Wl,CoreFoundation -framework CoreFoundation
 module_bonjour_publish_la_LIBADD = $(MODULE_LIBADD)
diff --git a/src/modules/coreaudio/module-coreaudio-detect.c b/src/modules/coreaudio/module-coreaudio-detect.c
deleted file mode 100644
index 263fc72..0000000
--- a/src/modules/coreaudio/module-coreaudio-detect.c
+++ /dev/null
@@ -1,268 +0,0 @@
-/***
-  This file is part of PulseAudio.
-
-  Copyright 2009,2010 Daniel Mack <daniel at 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("");
-
-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;
-
-    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;
-
-    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_assert(m);
-
-    m->userdata = u;
-
-    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/coreaudio/module-coreaudio-device.c b/src/modules/coreaudio/module-coreaudio-device.c
deleted file mode 100644
index cc4600b..0000000
--- a/src/modules/coreaudio/module-coreaudio-device.c
+++ /dev/null
@@ -1,866 +0,0 @@
-/***
-  This file is part of PulseAudio.
-
-  Copyright 2009,2010 Daniel Mack <daniel at 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 <pulse/util.h>
-
-#include <pulsecore/core-error.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;
-        }
-
-        case PA_SINK_MESSAGE_SET_STATE:
-            switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
-                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);
-            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) {
-    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;
-        }
-
-        case PA_SOURCE_MESSAGE_SET_STATE:
-            switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
-                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);
-            break;
-    }
-
-    return pa_source_process_msg(o, code, data, offset, chunk);
-}
-
-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", 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;
-
-    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_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", 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;
-
-    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("coreaudio", 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/macosx/module-bonjour-publish.c b/src/modules/macosx/module-bonjour-publish.c
new file mode 100644
index 0000000..095a1d2
--- /dev/null
+++ b/src/modules/macosx/module-bonjour-publish.c
@@ -0,0 +1,515 @@
+/***
+  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/core-subscribe.h>
+#include <pulsecore/dynarray.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/endianmacros.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 0000000..263fc72
--- /dev/null
+++ b/src/modules/macosx/module-coreaudio-detect.c
@@ -0,0 +1,268 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2009,2010 Daniel Mack <daniel at 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("");
+
+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;
+
+    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;
+
+    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_assert(m);
+
+    m->userdata = u;
+
+    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 0000000..cc4600b
--- /dev/null
+++ b/src/modules/macosx/module-coreaudio-device.c
@@ -0,0 +1,866 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2009,2010 Daniel Mack <daniel at 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 <pulse/util.h>
+
+#include <pulsecore/core-error.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;
+        }
+
+        case PA_SINK_MESSAGE_SET_STATE:
+            switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
+                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);
+            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) {
+    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;
+        }
+
+        case PA_SOURCE_MESSAGE_SET_STATE:
+            switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
+                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);
+            break;
+    }
+
+    return pa_source_process_msg(o, code, data, offset, chunk);
+}
+
+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", 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;
+
+    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_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", 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;
+
+    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("coreaudio", 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-bonjour-publish.c b/src/modules/module-bonjour-publish.c
deleted file mode 100644
index 095a1d2..0000000
--- a/src/modules/module-bonjour-publish.c
+++ /dev/null
@@ -1,515 +0,0 @@
-/***
-  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/core-subscribe.h>
-#include <pulsecore/dynarray.h>
-#include <pulsecore/modargs.h>
-#include <pulsecore/endianmacros.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);
-}

commit 00d96c04dcf73fd1c79b2ac6956592e9ba17aebf
Author: Daniel Mack <zonque at gmail.com>
Date:   Thu Mar 17 12:40:26 2011 +0100

    osx: add -headerpad_max_install_names to LDFLAGS
    
    This is needed for sufficient padding of library names in linked
    binaries.

diff --git a/src/Makefile.am b/src/Makefile.am
index ff98ddb..dc49bc1 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -78,6 +78,10 @@ AM_LDFLAGS+=-Wl,--export-all-symbols,--enable-auto-import -no-undefined
 WINSOCK_LIBS=-lwsock32 -lws2_32 -lwininet
 endif
 
+if OS_IS_DARWIN
+AM_LDFLAGS+=-headerpad_max_install_names
+endif
+
 FOREIGN_CFLAGS = -w
 MODULE_LDFLAGS = $(AM_LDFLAGS) -module -disable-static -avoid-version $(LDFLAGS_NOUNDEFINED)
 MODULE_LIBADD = $(AM_LIBADD) libpulsecore- at PA_MAJORMINOR@.la libpulsecommon- at PA_MAJORMINOR@.la libpulse.la

commit 85c5a2d7492cfa58fdbf9fcf747a2c86dc42378b
Author: Daniel Mack <zonque at gmail.com>
Date:   Fri Mar 18 15:53:38 2011 +0100

    configure.ac: add --mac-universal directive for OS X
    
    On a system with all depency libs built as multi-arch binaries, this
    option can now be used to build fat Mach-O binaries for multiple
    architectures.

diff --git a/configure.ac b/configure.ac
index 5a78ffb..78b37c4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -169,6 +169,11 @@ AC_ARG_ENABLE([atomic-arm-memory-barrier],
             esac
         ],)
 
+AC_ARG_ENABLE(mac-universal,
+    AS_HELP_STRING([--enable-mac-universal], [Build Mac universal binaries]),
+		enable_mac_universal=$enableval, enable_mac_universal="no")
+
+
 AC_MSG_CHECKING([target operating system])
 case $host in
         *-*-linux*)
@@ -178,6 +183,14 @@ case $host in
         *-*-darwin*)
             AC_MSG_RESULT([darwin])
             pulse_target_os=darwin
+
+            if test "x$enable_mac_universal" = "xyes" ; then
+	        mac_version_min="-mmacosx-version-min=10.5"
+                mac_arches="-arch i386 -arch x86_64"
+                mac_sysroot="-isysroot /Developer/SDKs/MacOSX10.5.sdk"
+                LDFLAGS="$LDFLAGS $mac_arches $mac_sysroot $mac_version_min"
+                CFLAGS="$CFLAGS $CFLAGS $mac_arches $mac_sysroot $mac_version_min"
+            fi
         ;;
         *)
             AC_MSG_RESULT([unknown])

commit fc339a608e0a295312a1887b6cfb78883f7c556c
Author: Daniel Mack <zonque at gmail.com>
Date:   Sun Mar 20 17:57:49 2011 +0100

    osx: add routines for real-time thread scheduling
    
    Code sniplets are based on examples provided by Apple. See
    http://developer.apple.com/library/mac/#documentation/Darwin/Conceptual/KernelProgramming/scheduler/scheduler.html

diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index a713cae..7d23918 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -107,6 +107,10 @@
 
 #ifdef __APPLE__
 #include <xlocale.h>
+#include <mach/mach_init.h>
+#include <mach/thread_act.h>
+#include <mach/thread_policy.h>
+#include <sys/sysctl.h>
 #endif
 
 #ifdef HAVE_DBUS
@@ -685,7 +689,39 @@ static int set_scheduler(int rtprio) {
  * the thread is already realtime, don't do anything. */
 int pa_make_realtime(int rtprio) {
 
-#ifdef _POSIX_PRIORITY_SCHEDULING
+#if defined(OS_IS_DARWIN)
+    struct thread_time_constraint_policy ttcpolicy;
+    uint64_t freq = 0;
+    size_t size = sizeof(freq);
+    int ret;
+
+    ret = sysctlbyname("hw.cpufrequency", &freq, &size, NULL, 0);
+    if (ret < 0) {
+        pa_log_info("Unable to read CPU frequency, acquisition of real-time scheduling failed.");
+        return -1;
+    }
+
+    pa_log_debug("sysctl for hw.cpufrequency: %llu", freq);
+
+    /* See http://developer.apple.com/library/mac/#documentation/Darwin/Conceptual/KernelProgramming/scheduler/scheduler.html */
+    ttcpolicy.period = freq / 160;
+    ttcpolicy.computation = freq / 3300;
+    ttcpolicy.constraint = freq / 2200;
+    ttcpolicy.preemptible = 1;
+
+    ret = thread_policy_set(mach_thread_self(),
+                            THREAD_TIME_CONSTRAINT_POLICY,
+                            (thread_policy_t) &ttcpolicy,
+                            THREAD_TIME_CONSTRAINT_POLICY_COUNT);
+    if (ret) {
+        pa_log_info("Unable to set real-time thread priority (%08x).", ret);
+        return -1;
+    }
+
+    pa_log_info("Successfully acquired real-time thread priority.");
+    return 0;
+
+#elif _POSIX_PRIORITY_SCHEDULING
     int p;
 
     if (set_scheduler(rtprio) >= 0) {

commit ae35ec1ed5f76aab6a0a60a27adf1d6b92323e70
Merge: 11da057 fc339a6
Author: Colin Guthrie <cguthrie at mandriva.org>
Date:   Thu Mar 24 09:08:05 2011 +0000

    Merge remote-tracking branch 'zonique/osx'


commit 72323bc6bb3486feaefcfe41cd39c08705393a8b
Author: Maarten Bosmans <mkbosmans at gmail.com>
Date:   Tue Mar 22 16:02:23 2011 +0100

    win32: Simplify dl_search_path code
    
    And add #include <sys/stat.h>, needed by the code introduced in f7acd4bd.

diff --git a/src/daemon/daemon-conf.c b/src/daemon/daemon-conf.c
index 9b530a8..59579d9 100644
--- a/src/daemon/daemon-conf.c
+++ b/src/daemon/daemon-conf.c
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <sys/stat.h>
 
 #ifdef HAVE_SCHED_H
 #include <sched.h>
@@ -37,6 +38,7 @@
 #include <pulse/xmalloc.h>
 #include <pulse/timeval.h>
 #include <pulse/i18n.h>
+#include <pulse/version.h>
 
 #include <pulsecore/core-error.h>
 #include <pulsecore/core-util.h>
@@ -147,6 +149,10 @@ pa_daemon_conf* pa_daemon_conf_new(void) {
 
     c = pa_xnewdup(pa_daemon_conf, &default_conf, 1);
 
+#ifdef OS_IS_WIN32
+    c->dl_search_path = pa_sprintf_malloc("%s" PA_PATH_SEP "lib" PA_PATH_SEP "pulse-%d.%d" PA_PATH_SEP "modules",
+                                          pa_win32_get_toplevel(NULL), PA_MAJOR, PA_MINOR);
+#else
 #if defined(__linux__) && !defined(__OPTIMIZE__)
 
     /* We abuse __OPTIMIZE__ as a check whether we are a debug build
@@ -160,19 +166,6 @@ pa_daemon_conf* pa_daemon_conf_new(void) {
     } else
 
 #endif
-#ifdef OS_IS_WIN32
-    {
-        char *t;
-        char *majorminor = pa_xstrdup(VERSION);
-        char *toplevel = pa_win32_get_toplevel(NULL);
-
-        if ((t = strchr(majorminor, '-')))
-          *t = '\0';
-
-        c->dl_search_path = pa_sprintf_malloc("%s" PA_PATH_SEP "lib" PA_PATH_SEP "pulse-%s" PA_PATH_SEP "modules", toplevel, majorminor);
-        pa_xfree(majorminor);
-    }
-#else
         c->dl_search_path = pa_xstrdup(PA_DLSEARCHPATH);
 #endif
 

commit 93348331bb415b1d0a5a1067fbe597f58353c5c9
Author: Maarten Bosmans <mkbosmans at gmail.com>
Date:   Tue Mar 22 16:02:24 2011 +0100

    Move compile-time checks around pa_run_from_build_tree to core-util
    
    To make the code cleaner and have the checks all in one place.

diff --git a/src/daemon/daemon-conf.c b/src/daemon/daemon-conf.c
index 59579d9..2872c74 100644
--- a/src/daemon/daemon-conf.c
+++ b/src/daemon/daemon-conf.c
@@ -144,7 +144,7 @@ static const pa_daemon_conf default_conf = {
 #endif
 };
 
-pa_daemon_conf* pa_daemon_conf_new(void) {
+pa_daemon_conf *pa_daemon_conf_new(void) {
     pa_daemon_conf *c;
 
     c = pa_xnewdup(pa_daemon_conf, &default_conf, 1);
@@ -153,19 +153,10 @@ pa_daemon_conf* pa_daemon_conf_new(void) {
     c->dl_search_path = pa_sprintf_malloc("%s" PA_PATH_SEP "lib" PA_PATH_SEP "pulse-%d.%d" PA_PATH_SEP "modules",
                                           pa_win32_get_toplevel(NULL), PA_MAJOR, PA_MINOR);
 #else
-#if defined(__linux__) && !defined(__OPTIMIZE__)
-
-    /* We abuse __OPTIMIZE__ as a check whether we are a debug build
-     * or not. If we are and are run from the build tree then we
-     * override the search path to point to our build tree */
-
     if (pa_run_from_build_tree()) {
         pa_log_notice("Detected that we are run from the build tree, fixing search path.");
         c->dl_search_path = pa_xstrdup(PA_BUILDDIR "/.libs/");
-
     } else
-
-#endif
         c->dl_search_path = pa_xstrdup(PA_DLSEARCHPATH);
 #endif
 
diff --git a/src/modules/alsa/alsa-mixer.c b/src/modules/alsa/alsa-mixer.c
index 51a6cd6..286931f 100644
--- a/src/modules/alsa/alsa-mixer.c
+++ b/src/modules/alsa/alsa-mixer.c
@@ -85,7 +85,7 @@ struct pa_alsa_fdlist {
     void *userdata;
 };
 
-static void io_cb(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, 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;
@@ -132,7 +132,7 @@ static void io_cb(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t
         snd_mixer_handle_events(fdl->mixer);
 }
 
-static void defer_cb(pa_mainloop_api*a, pa_defer_event* e, void *userdata) {
+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;
@@ -230,7 +230,7 @@ void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
     pa_xfree(fdl);
 }
 
-int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
+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);
@@ -2229,9 +2229,7 @@ pa_alsa_path* pa_alsa_path_new(const char *fname, pa_alsa_direction_t direction)
     items[2].data = &p->name;
 
     fn = pa_maybe_prefix_path(fname,
-#if defined(__linux__) && !defined(__OPTIMIZE__)
                               pa_run_from_build_tree() ? PA_BUILDDIR "/modules/alsa/mixer/paths/" :
-#endif
                               PA_ALSA_PATHS_DIR);
 
     r = pa_config_parse(fn, NULL, items, p);
@@ -2250,7 +2248,7 @@ fail:
     return NULL;
 }
 
-pa_alsa_path* pa_alsa_path_synthesize(const char*element, pa_alsa_direction_t direction) {
+pa_alsa_path *pa_alsa_path_synthesize(const char *element, pa_alsa_direction_t direction) {
     pa_alsa_path *p;
     pa_alsa_element *e;
 
@@ -3744,9 +3742,7 @@ pa_alsa_profile_set* pa_alsa_profile_set_new(const char *fname, const pa_channel
         fname = "default.conf";
 
     fn = pa_maybe_prefix_path(fname,
-#if defined(__linux__) && !defined(__OPTIMIZE__)
                               pa_run_from_build_tree() ? PA_BUILDDIR "/modules/alsa/mixer/profile-sets/" :
-#endif
                               PA_ALSA_PROFILE_SETS_DIR);
 
     r = pa_config_parse(fn, NULL, items, ps);
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index 796ff57..4823198 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -2991,9 +2991,6 @@ pa_bool_t pa_run_from_build_tree(void) {
     char *rp;
     pa_bool_t b = FALSE;
 
-    /* We abuse __OPTIMIZE__ as a check whether we are a debug build
-     * or not. */
-
     if ((rp = pa_readlink("/proc/self/exe"))) {
         b = pa_startswith(rp, PA_BUILDDIR);
         pa_xfree(rp);
diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h
index 80b4739..32641a3 100644
--- a/src/pulsecore/core-util.h
+++ b/src/pulsecore/core-util.h
@@ -254,8 +254,13 @@ size_t pa_pipe_buf(int fd);
 
 void pa_reset_personality(void);
 
+/* We abuse __OPTIMIZE__ as a check whether we are a debug build
+ * or not. If we are and are run from the build tree then we
+ * override the search path to point to our build tree */
 #if defined(__linux__) && !defined(__OPTIMIZE__)
 pa_bool_t pa_run_from_build_tree(void);
+#else
+static inline pa_bool_t pa_run_from_build_tree(void) {return FALSE;}
 #endif
 
 const char *pa_get_temp_dir(void);

commit 51163d0fd94735a514d734ca35644cbd45184ce0
Author: Tanu Kaskinen <tanu.kaskinen at digia.com>
Date:   Mon Mar 21 15:08:34 2011 +0200

    bluetooth: Don't log an error if an endpoint type is disabled.
    
    It's perfectly normal for BlueZ to disable some endpoint types, so printing a
    log message at error level isn't a good idea.
    
    For facilitating an informative message in case some endpoint type is disabled,
    the send_and_add_to_pending() function interface is also changed to be more
    generic (the pa_bluetooth_device pointer is replaced with a void pointer).

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index 293e024..a79ff91 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -73,7 +73,7 @@ struct pa_bluetooth_discovery {
 };
 
 static void get_properties_reply(DBusPendingCall *pending, void *userdata);
-static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessage *m, DBusPendingCallNotifyFunction func);
+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);
@@ -280,21 +280,21 @@ static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device
                     /* 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, d, m, get_properties_reply);
+                        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, d, m, get_properties_reply);
+                        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, d, m, get_properties_reply);
+                        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, d, m, get_properties_reply);
+                        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, d, m, get_properties_reply);
+                    send_and_add_to_pending(y, m, get_properties_reply, d);
 
                     if (!dbus_message_iter_next(&ai))
                         break;
@@ -394,7 +394,7 @@ static pa_bluetooth_device *found_device(pa_bluetooth_discovery *y, const char*
     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, d, m, get_properties_reply);
+    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 */
@@ -503,7 +503,7 @@ finish2:
     pa_dbus_pending_free(p);
 }
 
-static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessage *m, DBusPendingCallNotifyFunction func) {
+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;
 
@@ -512,7 +512,7 @@ static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, pa_bl
 
     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, d);
+    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);
 
@@ -524,6 +524,7 @@ static void register_endpoint_reply(DBusPendingCall *pending, void *userdata) {
     DBusMessage *r;
     pa_dbus_pending *p;
     pa_bluetooth_discovery *y;
+    char *endpoint;
 
     pa_assert(pending);
 
@@ -531,6 +532,7 @@ static void register_endpoint_reply(DBusPendingCall *pending, void *userdata) {
 
     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)) {
@@ -539,6 +541,11 @@ static void register_endpoint_reply(DBusPendingCall *pending, void *userdata) {
         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;
@@ -549,6 +556,8 @@ finish:
 
     PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
     pa_dbus_pending_free(p);
+
+    pa_xfree(endpoint);
 }
 
 static void list_devices_reply(DBusPendingCall *pending, void *userdata) {
@@ -643,14 +652,14 @@ static void register_endpoint(pa_bluetooth_discovery *y, const char *path, const
 
     dbus_message_iter_close_container(&i, &d);
 
-    send_and_add_to_pending(y, NULL, m, register_endpoint_reply);
+    send_and_add_to_pending(y, m, register_endpoint_reply, pa_xstrdup(endpoint));
 }
 
 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, NULL, m, list_devices_reply);
+    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);
@@ -711,7 +720,7 @@ static void list_adapters(pa_bluetooth_discovery *y) {
     pa_assert(y);
 
     pa_assert_se(m = dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
-    send_and_add_to_pending(y, NULL, m, list_adapters_reply);
+    send_and_add_to_pending(y, m, list_adapters_reply, NULL);
 }
 
 int pa_bluetooth_transport_parse_property(pa_bluetooth_transport *t, DBusMessageIter *i)
diff --git a/src/modules/bluetooth/bluetooth-util.h b/src/modules/bluetooth/bluetooth-util.h
index bb0cb24..2054f6e 100644
--- a/src/modules/bluetooth/bluetooth-util.h
+++ b/src/modules/bluetooth/bluetooth-util.h
@@ -29,6 +29,8 @@
 #include <pulsecore/macro.h>
 #include <pulsecore/core-util.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"
 

commit 99e37b600385f0aa035cc82575764ccd46964d51
Author: Tanu Kaskinen <tanu.kaskinen at digia.com>
Date:   Mon Mar 21 15:08:35 2011 +0200

    bluetooth: Get rid of warnings about unused stuff when building against a D-Bus version that doesn't have fd-passing support.

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index a79ff91..f1c7c4c 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -519,6 +519,7 @@ static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, DBusM
     return p;
 }
 
+#ifdef DBUS_TYPE_UNIX_FD
 static void register_endpoint_reply(DBusPendingCall *pending, void *userdata) {
     DBusError e;
     DBusMessage *r;
@@ -559,6 +560,7 @@ finish:
 
     pa_xfree(endpoint);
 }
+#endif
 
 static void list_devices_reply(DBusPendingCall *pending, void *userdata) {
     DBusError e;
@@ -607,6 +609,7 @@ finish:
     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;
@@ -654,6 +657,7 @@ static void register_endpoint(pa_bluetooth_discovery *y, const char *path, const
 
     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;
@@ -1042,7 +1046,9 @@ int pa_bluetooth_transport_acquire(const pa_bluetooth_transport *t, const char *
     if (omtu)
         *omtu = o;
 
+#ifdef DBUS_TYPE_UNIX_FD
 fail:
+#endif
     dbus_message_unref(r);
     return ret;
 }
@@ -1083,6 +1089,7 @@ static int setup_dbus(pa_bluetooth_discovery *y) {
     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;
 
@@ -1435,13 +1442,16 @@ static DBusHandlerResult endpoint_handler(DBusConnection *c, DBusMessage *m, voi
 
     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);
 

-- 
hooks/post-receive
PulseAudio Sound Server



More information about the pulseaudio-commits mailing list