Merge branch 'master' of git://0pointer.de/pulseaudio into dbus-work
authorTanu Kaskinen <tanuk@iki.fi>
Wed, 26 Aug 2009 11:29:55 +0000 (14:29 +0300)
committerTanu Kaskinen <tanuk@iki.fi>
Wed, 26 Aug 2009 11:29:55 +0000 (14:29 +0300)
Conflicts:
src/Makefile.am

src/Makefile.am
src/modules/bluetooth/bluetooth-util.c
src/modules/bluetooth/bluetooth-util.h
src/modules/bluetooth/module-bluetooth-device.c
src/modules/bluetooth/module-bluetooth-discover.c
src/modules/module-combine.c
src/modules/module-detect.c
src/modules/module-loopback.c [new file with mode: 0644]
src/modules/module-solaris.c
src/pulsecore/macro.h
src/pulsecore/remap_mmx.c

index c3ca1eb..16834ee 100644 (file)
@@ -837,8 +837,9 @@ libpulsecore_@PA_MAJORMINORMICRO@_la_SOURCES = \
                pulsecore/resampler.c pulsecore/resampler.h \
                pulsecore/rtpoll.c pulsecore/rtpoll.h \
                pulsecore/sample-util.c pulsecore/sample-util.h \
-               pulsecore/cpu-arm.c pulsecore/cpu-x86.c \
-               pulsecore/svolume_c.c pulsecore/svolume_arm.c\
+               pulsecore/cpu-arm.c pulsecore/cpu-arm.h \
+               pulsecore/cpu-x86.c pulsecore/cpu-x86.h \
+               pulsecore/svolume_c.c pulsecore/svolume_arm.c \
                pulsecore/svolume_mmx.c pulsecore/svolume_sse.c \
                pulsecore/sconv-s16be.c pulsecore/sconv-s16be.h \
                pulsecore/sconv-s16le.c pulsecore/sconv-s16le.h \
@@ -1012,8 +1013,8 @@ modlibexec_LTLIBRARIES += \
                module-tunnel-source.la \
                module-position-event-sounds.la \
                module-augment-properties.la \
-               module-cork-music-on-phone.la
-
+               module-cork-music-on-phone.la \
+               module-loopback.la
 
 # See comment at librtp.la above
 if !OS_IS_WIN32
@@ -1253,7 +1254,8 @@ SYMDEF_FILES = \
                modules/module-augment-properties-symdef.h \
                modules/module-cork-music-on-phone-symdef.h \
                modules/module-console-kit-symdef.h \
-               modules/dbus/module-dbus-protocol-symdef.h
+               modules/dbus/module-dbus-protocol-symdef.h \
+               modules/module-loopback-symdef.h
 
 EXTRA_DIST += $(SYMDEF_FILES)
 BUILT_SOURCES += $(SYMDEF_FILES)
@@ -1413,6 +1415,10 @@ module_tunnel_source_la_SOURCES = modules/module-tunnel.c
 module_tunnel_source_la_LDFLAGS = $(MODULE_LDFLAGS)
 module_tunnel_source_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
 
+module_loopback_la_SOURCES = modules/module-loopback.c
+module_loopback_la_LDFLAGS = $(MODULE_LDFLAGS)
+module_loopback_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
+
 # X11
 
 module_x11_bell_la_SOURCES = modules/x11/module-x11-bell.c
index f576823..d0c89aa 100644 (file)
@@ -1,7 +1,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2008 Joao Paulo Rechi Vita
+  Copyright 2008-2009 Joao Paulo Rechi Vita
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
@@ -95,6 +95,7 @@ static pa_bluetooth_device* device_new(const char *path) {
 
     d->audio_state = PA_BT_AUDIO_STATE_INVALID;
     d->audio_sink_state = PA_BT_AUDIO_STATE_INVALID;
+    d->audio_source_state = PA_BT_AUDIO_STATE_INVALID;
     d->headset_state = PA_BT_AUDIO_STATE_INVALID;
 
     return d;
@@ -124,6 +125,7 @@ static pa_bool_t device_is_audio(pa_bluetooth_device *d) {
         d->device_info_valid &&
         (d->audio_state != PA_BT_AUDIO_STATE_INVALID &&
          (d->audio_sink_state != PA_BT_AUDIO_STATE_INVALID ||
+          d->audio_source_state != PA_BT_AUDIO_STATE_INVALID ||
           d->headset_state != PA_BT_AUDIO_STATE_INVALID));
 }
 
@@ -233,6 +235,9 @@ static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device
                     } 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);
+                    } 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);
                     }
 
                     /* 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 */
@@ -278,7 +283,7 @@ static int parse_audio_property(pa_bluetooth_discovery *u, int *state, DBusMessa
 
     dbus_message_iter_recurse(i, &variant_i);
 
-/*     pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|Headset}.%s", key); */
+/*     pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|AudioSource|Headset}.%s", key); */
 
     switch (dbus_message_iter_get_arg_type(&variant_i)) {
 
@@ -390,6 +395,9 @@ static void get_properties_reply(DBusPendingCall *pending, void *userdata) {
             }  else if (dbus_message_has_interface(p->message, "org.bluez.AudioSink")) {
                 if (parse_audio_property(y, &d->audio_sink_state, &dict_i) < 0)
                     goto finish;
+            }  else if (dbus_message_has_interface(p->message, "org.bluez.AudioSource")) {
+                if (parse_audio_property(y, &d->audio_source_state, &dict_i) < 0)
+                    goto finish;
             }
         }
 
@@ -440,8 +448,8 @@ static void found_device(pa_bluetooth_discovery *y, const char* path) {
     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);
 
-    /* Before we read the other properties (Audio, AudioSink, Headset) we wait
-     * that the UUID is read */
+    /* Before we read the other properties (Audio, AudioSink, AudioSource,
+     * Headset) we wait that the UUID is read */
 }
 
 static void list_devices_reply(DBusPendingCall *pending, void *userdata) {
@@ -616,6 +624,7 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us
     } else if (dbus_message_is_signal(m, "org.bluez.Audio", "PropertyChanged") ||
                dbus_message_is_signal(m, "org.bluez.Headset", "PropertyChanged") ||
                dbus_message_is_signal(m, "org.bluez.AudioSink", "PropertyChanged") ||
+               dbus_message_is_signal(m, "org.bluez.AudioSource", "PropertyChanged") ||
                dbus_message_is_signal(m, "org.bluez.Device", "PropertyChanged")) {
 
         pa_bluetooth_device *d;
@@ -643,6 +652,9 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us
             }  else if (dbus_message_has_interface(m, "org.bluez.AudioSink")) {
                 if (parse_audio_property(y, &d->audio_sink_state, &arg_i) < 0)
                     goto fail;
+            }  else if (dbus_message_has_interface(m, "org.bluez.AudioSource")) {
+                if (parse_audio_property(y, &d->audio_source_state, &arg_i) < 0)
+                    goto fail;
             }
 
             run_callback(y, d, FALSE);
@@ -765,7 +777,8 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
                 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
                 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
                 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
-                "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL) < 0) {
+                "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
+                "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'", NULL) < 0) {
         pa_log("Failed to add D-Bus matches: %s", err.message);
         goto fail;
     }
@@ -817,7 +830,8 @@ void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
                                "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
                                "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
                                "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
-                               "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL);
+                               "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
+                               "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'", NULL);
 
         dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
 
index f15f217..e2a0c3d 100644 (file)
@@ -4,7 +4,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2008 Joao Paulo Rechi Vita
+  Copyright 2008-2009 Joao Paulo Rechi Vita
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
@@ -53,7 +53,7 @@ struct pa_bluetooth_uuid {
     PA_LLIST_FIELDS(pa_bluetooth_uuid);
 };
 
-/* This enum is shared among Audio, Headset, and AudioSink, although not all values are acceptable in all profiles */
+/* This enum is shared among Audio, Headset, AudioSink, and AudioSource, although not all values are acceptable in all profiles */
 typedef enum pa_bt_audio_state {
     PA_BT_AUDIO_STATE_INVALID = -1,
     PA_BT_AUDIO_STATE_DISCONNECTED,
@@ -84,6 +84,9 @@ struct pa_bluetooth_device {
     /* AudioSink state */
     pa_bt_audio_state_t audio_sink_state;
 
+    /* AudioSource state */
+    pa_bt_audio_state_t audio_source_state;
+
     /* Headset state */
     pa_bt_audio_state_t headset_state;
 };
index 4e23862..b8a8804 100644 (file)
@@ -1,7 +1,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2008 Joao Paulo Rechi Vita
+  Copyright 2008-2009 Joao Paulo Rechi Vita
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
@@ -129,6 +129,7 @@ struct hsp_info {
 
 enum profile {
     PROFILE_A2DP,
+    PROFILE_A2DP_SOURCE,
     PROFILE_HSP,
     PROFILE_OFF
 };
@@ -178,6 +179,7 @@ struct userdata {
 };
 
 #define FIXED_LATENCY_PLAYBACK_A2DP (25*PA_USEC_PER_MSEC)
+#define FIXED_LATENCY_RECORD_A2DP (25*PA_USEC_PER_MSEC)
 #define FIXED_LATENCY_PLAYBACK_HSP (125*PA_USEC_PER_MSEC)
 #define FIXED_LATENCY_RECORD_HSP (25*PA_USEC_PER_MSEC)
 
@@ -307,7 +309,7 @@ static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capa
 
     pa_log_debug("Payload size is %lu %lu", (unsigned long) bytes_left, (unsigned long) sizeof(*codec));
 
-    if ((u->profile == PROFILE_A2DP && codec->transport != BT_CAPABILITIES_TRANSPORT_A2DP) ||
+    if (((u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) && codec->transport != BT_CAPABILITIES_TRANSPORT_A2DP) ||
         (u->profile == PROFILE_HSP && codec->transport != BT_CAPABILITIES_TRANSPORT_SCO)) {
         pa_log_error("Got capabilities for wrong codec.");
         return -1;
@@ -344,6 +346,26 @@ static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capa
             return codec->seid;
 
         memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities));
+
+    } else if (u->profile == PROFILE_A2DP_SOURCE) {
+
+        while (bytes_left > 0) {
+            if ((codec->type == BT_A2DP_SBC_SOURCE) && !codec->lock)
+                break;
+
+            bytes_left -= codec->length;
+            codec = (const codec_capabilities_t*) ((const uint8_t*) codec + codec->length);
+        }
+
+        if (bytes_left <= 0 || codec->length != sizeof(u->a2dp.sbc_capabilities))
+            return -1;
+
+        pa_assert(codec->type == BT_A2DP_SBC_SOURCE);
+
+        if (codec->configured && seid == 0)
+            return codec->seid;
+
+        memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities));
     }
 
     return 0;
@@ -368,7 +390,7 @@ static int get_caps(struct userdata *u, uint8_t seid) {
     msg.getcaps_req.seid = seid;
 
     pa_strlcpy(msg.getcaps_req.object, u->path, sizeof(msg.getcaps_req.object));
-    if (u->profile == PROFILE_A2DP)
+    if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE)
         msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_A2DP;
     else {
         pa_assert(u->profile == PROFILE_HSP);
@@ -451,7 +473,7 @@ static int setup_a2dp(struct userdata *u) {
     };
 
     pa_assert(u);
-    pa_assert(u->profile == PROFILE_A2DP);
+    pa_assert(u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE);
 
     cap = &u->a2dp.sbc_capabilities;
 
@@ -652,8 +674,8 @@ static int set_conf(struct userdata *u) {
     msg.open_req.h.length = sizeof(msg.open_req);
 
     pa_strlcpy(msg.open_req.object, u->path, sizeof(msg.open_req.object));
-    msg.open_req.seid = u->profile == PROFILE_A2DP ? u->a2dp.sbc_capabilities.capability.seid : BT_A2DP_SEID_RANGE + 1;
-    msg.open_req.lock = u->profile == PROFILE_A2DP ? BT_WRITE_LOCK : BT_READ_LOCK | BT_WRITE_LOCK;
+    msg.open_req.seid = (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) ? u->a2dp.sbc_capabilities.capability.seid : BT_A2DP_SEID_RANGE + 1;
+    msg.open_req.lock = (u->profile == PROFILE_A2DP) ? BT_WRITE_LOCK : BT_READ_LOCK | BT_WRITE_LOCK;
 
     if (service_send(u, &msg.open_req.h) < 0)
         return -1;
@@ -661,7 +683,7 @@ static int set_conf(struct userdata *u) {
     if (service_expect(u, &msg.open_rsp.h, sizeof(msg), BT_OPEN, sizeof(msg.open_rsp)) < 0)
         return -1;
 
-    if (u->profile == PROFILE_A2DP ) {
+    if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
         u->sample_spec.format = PA_SAMPLE_S16LE;
 
         if (setup_a2dp(u) < 0)
@@ -679,7 +701,7 @@ static int set_conf(struct userdata *u) {
     msg.setconf_req.h.name = BT_SET_CONFIGURATION;
     msg.setconf_req.h.length = sizeof(msg.setconf_req);
 
-    if (u->profile == PROFILE_A2DP) {
+    if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
         memcpy(&msg.setconf_req.codec, &u->a2dp.sbc_capabilities, sizeof(u->a2dp.sbc_capabilities));
     } else {
         msg.setconf_req.codec.transport = BT_CAPABILITIES_TRANSPORT_SCO;
@@ -697,7 +719,7 @@ static int set_conf(struct userdata *u) {
     u->link_mtu = msg.setconf_rsp.link_mtu;
 
     /* setup SBC encoder now we agree on parameters */
-    if (u->profile == PROFILE_A2DP) {
+    if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
         setup_sbc(&u->a2dp);
 
         u->block_size =
@@ -1250,6 +1272,119 @@ static int a2dp_process_render(struct userdata *u) {
     return ret;
 }
 
+static int a2dp_process_push(struct userdata *u) {
+    int ret = 0;
+    pa_memchunk memchunk;
+
+    pa_assert(u);
+    pa_assert(u->profile == PROFILE_A2DP_SOURCE);
+    pa_assert(u->source);
+    pa_assert(u->read_smoother);
+
+    memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size);
+    memchunk.index = memchunk.length = 0;
+
+    for (;;) {
+        pa_bool_t found_tstamp = FALSE;
+        pa_usec_t tstamp;
+        struct a2dp_info *a2dp;
+        struct rtp_header *header;
+        struct rtp_payload *payload;
+        const void *p;
+        void *d;
+        ssize_t l;
+        size_t to_write, to_decode;
+        unsigned frame_count;
+
+        a2dp_prepare_buffer(u);
+
+        a2dp = &u->a2dp;
+        header = a2dp->buffer;
+        payload = (struct rtp_payload*) ((uint8_t*) a2dp->buffer + sizeof(*header));
+
+        l = pa_read(u->stream_fd, a2dp->buffer, a2dp->buffer_size, &u->stream_write_type);
+
+        if (l <= 0) {
+
+            if (l < 0 && errno == EINTR)
+                /* Retry right away if we got interrupted */
+                continue;
+
+            else if (l < 0 && errno == EAGAIN)
+                /* Hmm, apparently the socket was not readable, give up for now. */
+                break;
+
+            pa_log_error("Failed to read data from socket: %s", l < 0 ? pa_cstrerror(errno) : "EOF");
+            ret = -1;
+            break;
+        }
+
+        pa_assert((size_t) l <= a2dp->buffer_size);
+
+        u->read_index += (uint64_t) l;
+
+        /* TODO: get timestamp from rtp */
+        if (!found_tstamp) {
+            /* pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); */
+            tstamp = pa_rtclock_now();
+        }
+
+        pa_smoother_put(u->read_smoother, tstamp, pa_bytes_to_usec(u->read_index, &u->sample_spec));
+        pa_smoother_resume(u->read_smoother, tstamp, TRUE);
+
+        p = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload);
+        to_decode = l - sizeof(*header) - sizeof(*payload);
+
+        d = pa_memblock_acquire(memchunk.memblock);
+        to_write = memchunk.length = pa_memblock_get_length(memchunk.memblock);
+
+        while (PA_LIKELY(to_decode > 0 && to_write > 0)) {
+            size_t written;
+            ssize_t decoded;
+
+            decoded = sbc_decode(&a2dp->sbc,
+                                 p, to_decode,
+                                 d, to_write,
+                                 &written);
+
+            if (PA_UNLIKELY(decoded <= 0)) {
+                pa_log_error("SBC decoding error (%li)", (long) decoded);
+                pa_memblock_release(memchunk.memblock);
+                pa_memblock_unref(memchunk.memblock);
+                return -1;
+            }
+
+/*             pa_log_debug("SBC: decoded: %lu; written: %lu", (unsigned long) decoded, (unsigned long) written); */
+/*             pa_log_debug("SBC: frame_length: %lu; codesize: %lu", (unsigned long) a2dp->frame_length, (unsigned long) a2dp->codesize); */
+
+            pa_assert_fp((size_t) decoded <= to_decode);
+            pa_assert_fp((size_t) decoded == a2dp->frame_length);
+
+            pa_assert_fp((size_t) written <= to_write);
+            pa_assert_fp((size_t) written == a2dp->codesize);
+
+            p = (const uint8_t*) p + decoded;
+            to_decode -= decoded;
+
+            d = (uint8_t*) d + written;
+            to_write -= written;
+
+            frame_count++;
+        }
+
+        pa_memblock_release(memchunk.memblock);
+
+        pa_source_post(u->source, &memchunk);
+
+        ret = 1;
+        break;
+    }
+
+    pa_memblock_unref(memchunk.memblock);
+
+    return ret;
+}
+
 static void thread_func(void *userdata) {
     struct userdata *u = userdata;
     unsigned do_write = 0;
@@ -1285,7 +1420,12 @@ static void thread_func(void *userdata) {
             if (pollfd && (pollfd->revents & POLLIN)) {
                 int n_read;
 
-                if ((n_read = hsp_process_push(u)) < 0)
+                if (u->profile == PROFILE_HSP)
+                    n_read = hsp_process_push(u);
+                else
+                    n_read = a2dp_process_push(u);
+
+                if (n_read < 0)
                     goto fail;
 
                 /* We just read something, so we are supposed to write something, too */
@@ -1687,7 +1827,7 @@ static int add_source(struct userdata *u) {
         data.driver = __FILE__;
         data.module = u->module;
         pa_source_new_data_set_sample_spec(&data, &u->sample_spec);
-        pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "hsp");
+        pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP_SOURCE ? "a2dp_source" : "hsp");
         if (u->profile == PROFILE_HSP)
             pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
         data.card = u->card;
@@ -1712,7 +1852,7 @@ static int add_source(struct userdata *u) {
         u->source->parent.process_msg = source_process_msg;
 
         pa_source_set_fixed_latency(u->source,
-                                    (/* u->profile == PROFILE_A2DP ? FIXED_LATENCY_RECORD_A2DP : */ FIXED_LATENCY_RECORD_HSP) +
+                                    (u->profile == PROFILE_A2DP_SOURCE ? FIXED_LATENCY_RECORD_A2DP : FIXED_LATENCY_RECORD_HSP) +
                                     pa_bytes_to_usec(u->block_size, &u->sample_spec));
     }
 
@@ -1809,7 +1949,8 @@ static int init_profile(struct userdata *u) {
         if (add_sink(u) < 0)
             r = -1;
 
-    if (u->profile == PROFILE_HSP)
+    if (u->profile == PROFILE_HSP ||
+        u->profile == PROFILE_A2DP_SOURCE)
         if (add_source(u) < 0)
             r = -1;
 
@@ -2050,6 +2191,20 @@ static int add_card(struct userdata *u, const pa_bluetooth_device *device) {
         pa_hashmap_put(data.profiles, p->name, p);
     }
 
+    if (pa_bluetooth_uuid_has(device->uuids, A2DP_SOURCE_UUID)) {
+        p = pa_card_profile_new("a2dp_source", _("High Fidelity Capture (A2DP)"), sizeof(enum profile));
+        p->priority = 10;
+        p->n_sinks = 0;
+        p->n_sources = 1;
+        p->max_sink_channels = 0;
+        p->max_source_channels = 2;
+
+        d = PA_CARD_PROFILE_DATA(p);
+        *d = PROFILE_A2DP_SOURCE;
+
+        pa_hashmap_put(data.profiles, p->name, p);
+    }
+
     if (pa_bluetooth_uuid_has(device->uuids, HSP_HS_UUID) ||
         pa_bluetooth_uuid_has(device->uuids, HFP_HS_UUID)) {
         p = pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile));
index 788fee0..7571e48 100644 (file)
@@ -1,7 +1,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2008 Joao Paulo Rechi Vita
+  Copyright 2008-2009 Joao Paulo Rechi Vita
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
@@ -83,8 +83,9 @@ static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const
 
     mi = pa_hashmap_get(u->hashmap, d->path);
 
+    pa_log("dead: %d, device_connected: %d, audio_state: %d, audio_source_state: %d", d->dead, d->device_connected, d->audio_state, d->audio_source_state);
     if (!d->dead &&
-        d->device_connected > 0 && d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED) {
+        d->device_connected > 0 && (d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED || d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED)) {
 
         if (!mi) {
             pa_module *m = NULL;
@@ -116,6 +117,9 @@ static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const
             }
 #endif
 
+            if (d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED)
+                args = pa_sprintf_malloc("%s profile=\"a2dp_source\"", args);
+
             pa_log_debug("Loading module-bluetooth-device %s", args);
             m = pa_module_load(u->module->core, "module-bluetooth-device", args);
             pa_xfree(args);
index e90ef11..4a57ce2 100644 (file)
@@ -58,7 +58,7 @@ PA_MODULE_USAGE(
         "sink_name=<name for the sink> "
         "sink_properties=<properties for the sink> "
         "slaves=<slave sinks> "
-        "adjust_time=<seconds> "
+        "adjust_time=<how often to readjust rates in s> "
         "resample_method=<method> "
         "format=<sample format> "
         "rate=<sample rate> "
@@ -69,7 +69,7 @@ PA_MODULE_USAGE(
 
 #define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
 
-#define DEFAULT_ADJUST_TIME 10
+#define DEFAULT_ADJUST_TIME_USEC (10*PA_USEC_PER_SEC)
 
 #define BLOCK_USEC (PA_USEC_PER_MSEC * 200)
 
@@ -91,7 +91,6 @@ struct output {
 
     pa_sink *sink;
     pa_sink_input *sink_input;
-
     pa_bool_t ignore_state_change;
 
     pa_asyncmsgq *inq,    /* Message queue from the sink thread to this sink input */
@@ -121,7 +120,7 @@ struct userdata {
     pa_rtpoll *rtpoll;
 
     pa_time_event *time_event;
-    uint32_t adjust_time;
+    pa_usec_t adjust_time;
 
     pa_bool_t automatic;
     pa_bool_t auto_desc;
@@ -221,9 +220,9 @@ static void adjust_rates(struct userdata *u) {
             continue;
 
         if (o->total_latency < target_latency)
-            r -= (uint32_t) ((((double) (target_latency - o->total_latency))/(double)u->adjust_time)*(double)r/PA_USEC_PER_SEC);
+            r -= (uint32_t) ((((double) (target_latency - o->total_latency))/(double)u->adjust_time)*(double)r);
         else if (o->total_latency > target_latency)
-            r += (uint32_t) ((((double) (o->total_latency - target_latency))/(double)u->adjust_time)*(double)r/PA_USEC_PER_SEC);
+            r += (uint32_t) ((((double) (o->total_latency - target_latency))/(double)u->adjust_time)*(double)r);
 
         if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1)) {
             pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->sink->name, base_rate, r);
@@ -246,7 +245,7 @@ static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct tim
 
     adjust_rates(u);
 
-    pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time * PA_USEC_PER_SEC);
+    pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time);
 }
 
 static void process_render_null(struct userdata *u, pa_usec_t now) {
@@ -1105,6 +1104,7 @@ int pa__init(pa_module*m) {
     struct output *o;
     uint32_t idx;
     pa_sink_new_data data;
+    uint32_t adjust_time_sec;
 
     pa_assert(m);
 
@@ -1123,16 +1123,10 @@ int pa__init(pa_module*m) {
     m->userdata = u = pa_xnew0(struct userdata, 1);
     u->core = m->core;
     u->module = m;
-    u->adjust_time = DEFAULT_ADJUST_TIME;
     u->rtpoll = pa_rtpoll_new();
     pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
     u->resample_method = resample_method;
     u->outputs = pa_idxset_new(NULL, NULL);
-    u->sink_put_slot = u->sink_unlink_slot = u->sink_state_changed_slot = NULL;
-    PA_LLIST_HEAD_INIT(struct output, u->thread_info.active_outputs);
-    pa_atomic_store(&u->thread_info.running, FALSE);
-    u->thread_info.in_null_mode = FALSE;
-    u->thread_info.counter = 0;
     u->thread_info.smoother = pa_smoother_new(
             PA_USEC_PER_SEC,
             PA_USEC_PER_SEC*2,
@@ -1142,11 +1136,17 @@ int pa__init(pa_module*m) {
             0,
             FALSE);
 
-    if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
+    adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
+    if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
         pa_log("Failed to parse adjust_time value");
         goto fail;
     }
 
+    if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
+        u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
+    else
+        u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
+
     slaves = pa_modargs_get_value(ma, "slaves", NULL);
     u->automatic = !slaves;
 
@@ -1315,7 +1315,7 @@ int pa__init(pa_module*m) {
         output_verify(o);
 
     if (u->adjust_time > 0)
-        u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time * PA_USEC_PER_SEC, time_callback, u);
+        u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
 
     pa_modargs_free(ma);
 
index 956fe4c..b1f24e1 100644 (file)
@@ -50,7 +50,7 @@ PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers
 PA_MODULE_VERSION(PACKAGE_VERSION);
 PA_MODULE_LOAD_ONCE(TRUE);
 PA_MODULE_USAGE("just-one=<boolean>");
-PA_MODULE_DEPRECATED("Please use module-hal-detect instead of module-detect!");
+PA_MODULE_DEPRECATED("Please use module-udev-detect instead of module-detect!");
 
 static const char* const valid_modargs[] = {
     "just-one",
diff --git a/src/modules/module-loopback.c b/src/modules/module-loopback.c
new file mode 100644 (file)
index 0000000..d9edbac
--- /dev/null
@@ -0,0 +1,783 @@
+/***
+    This file is part of PulseAudio.
+
+    Copyright 2009 Intel Corporation
+    Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
+
+    PulseAudio is free software; you can redistribute it and/or modify
+    it under the terms of the GNU Lesser General Public License as published
+    by the Free Software Foundation; either version 2.1 of the License,
+    or (at your option) any later version.
+
+    PulseAudio is distributed in the hope that it will be useful, but
+    WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+    General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public License
+    along with PulseAudio; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+    USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <math.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/sink-input.h>
+#include <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-util.h>
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+
+#include "module-loopback-symdef.h"
+
+PA_MODULE_AUTHOR("Pierre-Louis Bossart");
+PA_MODULE_DESCRIPTION("Loopback from source to sink");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_USAGE(
+        "source=<source to connect to> "
+        "sink=<sink to connect to> "
+        "adjust_time=<how often to readjust rates in s> "
+        "latency_msec=<latency in ms> "
+        "format=<sample format> "
+        "rate=<sample rate> "
+        "channels=<number of channels> "
+        "channel_map=<channel map>");
+
+#define DEFAULT_LATENCY_MSEC 200
+
+#define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
+
+#define DEFAULT_ADJUST_TIME_USEC (10*PA_USEC_PER_SEC)
+
+struct userdata {
+    pa_core *core;
+    pa_module *module;
+
+    pa_sink_input *sink_input;
+    pa_source_output *source_output;
+
+    pa_asyncmsgq *asyncmsgq;
+    pa_memblockq *memblockq;
+
+    pa_rtpoll_item *rtpoll_item_read, *rtpoll_item_write;
+
+    pa_time_event *time_event;
+    pa_usec_t adjust_time;
+
+    int64_t recv_counter;
+    int64_t send_counter;
+
+    size_t skip;
+    pa_usec_t latency;
+
+    pa_bool_t in_pop;
+    size_t min_memblockq_length;
+
+    struct {
+        int64_t send_counter;
+        size_t source_output_buffer;
+        pa_usec_t source_latency;
+
+        int64_t recv_counter;
+        size_t sink_input_buffer;
+        pa_usec_t sink_latency;
+
+        size_t min_memblockq_length;
+        size_t max_request;
+    } latency_snapshot;
+};
+
+static const char* const valid_modargs[] = {
+    "source",
+    "sink",
+    "latency",
+    "format",
+    "rate",
+    "channels",
+    "channel_map",
+    NULL,
+};
+
+enum {
+    SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX,
+    SINK_INPUT_MESSAGE_REWIND,
+    SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT,
+    SINK_INPUT_MESSAGE_MAX_REQUEST_CHANGED
+};
+
+enum {
+    SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
+};
+
+/* Called from main context */
+static void teardown(struct userdata *u) {
+    pa_assert(u);
+    pa_assert_ctl_context();
+
+    if (u->sink_input)
+        pa_sink_input_unlink(u->sink_input);
+
+    if (u->source_output)
+        pa_source_output_unlink(u->source_output);
+
+    if (u->sink_input) {
+        pa_sink_input_unref(u->sink_input);
+        u->sink_input = NULL;
+    }
+
+    if (u->source_output) {
+        pa_source_output_unref(u->source_output);
+        u->source_output = NULL;
+    }
+}
+
+/* Called from main context */
+static void adjust_rates(struct userdata *u) {
+    size_t buffer, fs;
+    uint32_t old_rate, base_rate, new_rate;
+    pa_usec_t buffer_latency;
+
+    pa_assert(u);
+    pa_assert_ctl_context();
+
+    pa_asyncmsgq_send(u->source_output->source->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT, NULL, 0, NULL);
+    pa_asyncmsgq_send(u->sink_input->sink->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT, NULL, 0, NULL);
+
+    buffer =
+        u->latency_snapshot.sink_input_buffer +
+        u->latency_snapshot.source_output_buffer;
+
+    if (u->latency_snapshot.recv_counter <= u->latency_snapshot.send_counter)
+        buffer += (size_t) (u->latency_snapshot.send_counter - u->latency_snapshot.recv_counter);
+    else
+        buffer += PA_CLIP_SUB(buffer, (size_t) (u->latency_snapshot.recv_counter - u->latency_snapshot.send_counter));
+
+    buffer_latency = pa_bytes_to_usec(buffer, &u->sink_input->sample_spec);
+
+    pa_log_info("Loopback overall latency is %0.2f ms + %0.2f ms + %0.2f ms = %0.2f ms",
+                (double) u->latency_snapshot.sink_latency / PA_USEC_PER_MSEC,
+                (double) buffer_latency / PA_USEC_PER_MSEC,
+                (double) u->latency_snapshot.source_latency / PA_USEC_PER_MSEC,
+                ((double) u->latency_snapshot.sink_latency + buffer_latency + u->latency_snapshot.source_latency) / PA_USEC_PER_MSEC);
+
+    pa_log_info("Should buffer %lu bytes, buffered at minimum %lu bytes",
+                u->latency_snapshot.max_request*2,
+                u->latency_snapshot.min_memblockq_length);
+
+    fs = pa_frame_size(&u->sink_input->sample_spec);
+    old_rate = u->sink_input->sample_spec.rate;
+    base_rate = u->source_output->sample_spec.rate;
+
+    if (u->latency_snapshot.min_memblockq_length < u->latency_snapshot.max_request*2)
+        new_rate = base_rate - (((u->latency_snapshot.max_request*2 - u->latency_snapshot.min_memblockq_length) / fs) *PA_USEC_PER_SEC)/u->adjust_time;
+    else
+        new_rate = base_rate + (((u->latency_snapshot.min_memblockq_length - u->latency_snapshot.max_request*2) / fs) *PA_USEC_PER_SEC)/u->adjust_time;
+
+    pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate, (unsigned long) new_rate);
+
+    pa_sink_input_set_rate(u->sink_input, new_rate);
+
+    pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
+}
+
+/* Called from main context */
+static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
+    struct userdata *u = userdata;
+
+    pa_assert(u);
+    pa_assert(a);
+    pa_assert(u->time_event == e);
+
+    adjust_rates(u);
+}
+
+/* Called from input thread context */
+static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
+    struct userdata *u;
+    pa_memchunk copy;
+
+    pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
+    pa_assert_se(u = o->userdata);
+
+    if (u->skip > chunk->length) {
+        u->skip -= chunk->length;
+        return;
+    }
+
+    if (u->skip > 0) {
+        copy = *chunk;
+        copy.index += u->skip;
+        copy.length -= u->skip;
+        u->skip = 0;
+
+        chunk = &copy;
+    }
+
+    pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, chunk, NULL);
+    u->send_counter += (int64_t) chunk->length;
+}
+
+/* Called from input thread context */
+static void source_output_process_rewind_cb(pa_source_output *o, size_t nbytes) {
+    struct userdata *u;
+
+    pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
+    pa_assert_se(u = o->userdata);
+
+    pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_REWIND, NULL, (int64_t) nbytes, NULL, NULL);
+    u->send_counter -= (int64_t) nbytes;
+}
+
+/* Called from output thread context */
+static int source_output_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+    struct userdata *u = PA_SOURCE_OUTPUT(obj)->userdata;
+
+    switch (code) {
+
+        case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT: {
+            size_t length;
+
+            length = pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq);
+
+            u->latency_snapshot.send_counter = u->send_counter;
+            u->latency_snapshot.source_output_buffer = u->source_output->thread_info.resampler ? pa_resampler_result(u->source_output->thread_info.resampler, length) : length;
+            u->latency_snapshot.source_latency = pa_source_get_latency_within_thread(u->source_output->source);
+
+            return 0;
+        }
+    }
+
+    return pa_source_output_process_msg(obj, code, data, offset, chunk);
+}
+
+/* Called from output thread context */
+static void source_output_attach_cb(pa_source_output *o) {
+    struct userdata *u;
+
+    pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
+    pa_assert_se(u = o->userdata);
+
+    u->rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
+            o->source->thread_info.rtpoll,
+            PA_RTPOLL_LATE,
+            u->asyncmsgq);
+}
+
+/* Called from output thread context */
+static void source_output_detach_cb(pa_source_output *o) {
+    struct userdata *u;
+
+    pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
+    pa_assert_se(u = o->userdata);
+
+    if (u->rtpoll_item_write) {
+        pa_rtpoll_item_free(u->rtpoll_item_write);
+        u->rtpoll_item_write = NULL;
+    }
+}
+
+/* Called from output thread context */
+static void source_output_state_change_cb(pa_source_output *o, pa_source_output_state_t state) {
+    struct userdata *u;
+
+    pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
+    pa_assert_se(u = o->userdata);
+
+    if (PA_SOURCE_OUTPUT_IS_LINKED(state) && o->thread_info.state == PA_SOURCE_OUTPUT_INIT) {
+
+        u->skip = pa_usec_to_bytes(PA_CLIP_SUB(pa_source_get_latency_within_thread(o->source),
+                                               u->latency),
+                                   &o->sample_spec);
+
+        pa_log_info("Skipping %lu bytes", (unsigned long) u->skip);
+    }
+}
+
+/* Called from main thread */
+static void source_output_kill_cb(pa_source_output *o) {
+    struct userdata *u;
+
+    pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
+    pa_assert_se(u = o->userdata);
+
+    teardown(u);
+    pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from main thread */
+static pa_bool_t source_output_may_move_to_cb(pa_source_output *o, pa_source *dest) {
+    struct userdata *u;
+
+    pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
+    pa_assert_se(u = o->userdata);
+
+    return dest != u->sink_input->sink->monitor_source;
+}
+
+/* Called from main thread */
+static void source_output_moving_cb(pa_source_output *o, pa_source *dest) {
+    pa_proplist *p;
+    const char *n;
+    struct userdata *u;
+
+    pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
+    pa_assert_se(u = o->userdata);
+
+    p = pa_proplist_new();
+    pa_proplist_setf(p, PA_PROP_MEDIA_NAME, "Loopback of %s", pa_strnull(pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+
+    if ((n = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_ICON_NAME)))
+        pa_proplist_sets(p, PA_PROP_MEDIA_ICON_NAME, n);
+
+    pa_sink_input_update_proplist(u->sink_input, PA_UPDATE_REPLACE, p);
+    pa_proplist_free(p);
+}
+
+/* Called from output thread context */
+static void update_min_memblockq_length(struct userdata *u) {
+    size_t length;
+
+    pa_assert(u);
+    pa_sink_input_assert_io_context(u->sink_input);
+
+    length = pa_memblockq_get_length(u->memblockq);
+
+    if (u->min_memblockq_length == (size_t) -1 ||
+        length < u->min_memblockq_length)
+        u->min_memblockq_length = length;
+}
+
+/* Called from output thread context */
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
+    pa_assert_se(u = i->userdata);
+    pa_assert(chunk);
+
+    u->in_pop = TRUE;
+    while (pa_asyncmsgq_process_one(u->asyncmsgq) > 0)
+        ;
+    u->in_pop = FALSE;
+
+    if (pa_memblockq_peek(u->memblockq, chunk) < 0) {
+        pa_log_info("Coud not peek into queue");
+        return -1;
+    }
+
+    chunk->length = PA_MIN(chunk->length, nbytes);
+    pa_memblockq_drop(u->memblockq, chunk->length);
+
+    update_min_memblockq_length(u);
+
+    return 0;
+}
+
+/* Called from output thread context */
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
+    pa_assert_se(u = i->userdata);
+
+    pa_memblockq_rewind(u->memblockq, nbytes);
+}
+
+/* Called from output thread context */
+static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
+    struct userdata *u = PA_SINK_INPUT(obj)->userdata;
+
+    switch (code) {
+
+        case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
+             pa_usec_t *r = data;
+
+             pa_sink_input_assert_io_context(u->sink_input);
+
+            *r = pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &u->sink_input->sample_spec);
+
+            /* Fall through, the default handler will add in the extra
+             * latency added by the resampler */
+            break;
+        }
+
+        case SINK_INPUT_MESSAGE_POST:
+
+            pa_sink_input_assert_io_context(u->sink_input);
+
+            if (PA_SINK_IS_OPENED(u->sink_input->sink->thread_info.state))
+                pa_memblockq_push_align(u->memblockq, chunk);
+            else
+                pa_memblockq_flush_write(u->memblockq);
+
+            update_min_memblockq_length(u);
+
+            /* Is this the end of an underrun? Then let's start things
+             * right-away */
+            if (!u->in_pop &&
+                u->sink_input->thread_info.underrun_for > 0 &&
+                pa_memblockq_is_readable(u->memblockq)) {
+
+                pa_log_debug("Requesting rewind due to end of underrun.");
+                pa_sink_input_request_rewind(u->sink_input,
+                                             (size_t) (u->sink_input->thread_info.underrun_for == (size_t) -1 ? 0 : u->sink_input->thread_info.underrun_for),
+                                             FALSE, TRUE, FALSE);
+            }
+
+            u->recv_counter += (int64_t) chunk->length;
+
+            return 0;
+
+        case SINK_INPUT_MESSAGE_REWIND:
+
+            pa_sink_input_assert_io_context(u->sink_input);
+
+            if (PA_SINK_IS_OPENED(u->sink_input->sink->thread_info.state))
+                pa_memblockq_seek(u->memblockq, -offset, PA_SEEK_RELATIVE, TRUE);
+            else
+                pa_memblockq_flush_write(u->memblockq);
+
+            u->recv_counter -= offset;
+
+            update_min_memblockq_length(u);
+
+            return 0;
+
+        case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT: {
+            size_t length;
+
+            update_min_memblockq_length(u);
+
+            length = pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq);
+
+            u->latency_snapshot.recv_counter = u->recv_counter;
+            u->latency_snapshot.sink_input_buffer =
+                pa_memblockq_get_length(u->memblockq) +
+                (u->sink_input->thread_info.resampler ? pa_resampler_request(u->sink_input->thread_info.resampler, length) : length);
+            u->latency_snapshot.sink_latency = pa_sink_get_latency_within_thread(u->sink_input->sink);
+
+            u->latency_snapshot.max_request = pa_sink_input_get_max_request(u->sink_input);
+
+            u->latency_snapshot.min_memblockq_length = u->min_memblockq_length;
+            u->min_memblockq_length = (size_t) -1;
+
+            return 0;
+        }
+
+        case SINK_INPUT_MESSAGE_MAX_REQUEST_CHANGED: {
+            /* This message is sent from the IO thread to the main
+             * thread! So don't be confused. All the user cases above
+             * are executed in thread context, but this one is not! */
+
+            pa_assert_ctl_context();
+
+            adjust_rates(u);
+            return 0;
+        }
+    }
+
+    return pa_sink_input_process_msg(obj, code, data, offset, chunk);
+}
+
+/* Called from output thread context */
+static void sink_input_attach_cb(pa_sink_input *i) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
+    pa_assert_se(u = i->userdata);
+
+    u->rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
+            i->sink->thread_info.rtpoll,
+            PA_RTPOLL_LATE,
+            u->asyncmsgq);
+
+    pa_memblockq_set_prebuf(u->memblockq, pa_sink_input_get_max_request(i)*2);
+    pa_memblockq_set_maxrewind(u->memblockq, pa_sink_input_get_max_rewind(i));
+
+    u->min_memblockq_length = (size_t) -1;
+}
+
+/* Called from output thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
+    pa_assert_se(u = i->userdata);
+
+    if (u->rtpoll_item_read) {
+        pa_rtpoll_item_free(u->rtpoll_item_read);
+        u->rtpoll_item_read = NULL;
+    }
+}
+
+/* Called from output thread context */
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
+    pa_assert_se(u = i->userdata);
+
+    pa_memblockq_set_maxrewind(u->memblockq, nbytes);
+}
+
+/* Called from output thread context */
+static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
+    pa_assert_se(u = i->userdata);
+
+    pa_memblockq_set_prebuf(u->memblockq, nbytes*2);
+    pa_log_info("Max request changed");
+    pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_MAX_REQUEST_CHANGED, NULL, 0, NULL, NULL);
+}
+
+/* Called from main thread */
+static void sink_input_kill_cb(pa_sink_input *i) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
+    pa_assert_se(u = i->userdata);
+
+    teardown(u);
+    pa_module_unload_request(u->module, TRUE);
+}
+
+/* Called from main thread */
+static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
+    struct userdata *u;
+    pa_proplist *p;
+    const char *n;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
+    pa_assert_se(u = i->userdata);
+
+    p = pa_proplist_new();
+    pa_proplist_setf(p, PA_PROP_MEDIA_NAME, "Loopback to %s", pa_strnull(pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+
+    if ((n = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_ICON_NAME)))
+        pa_proplist_sets(p, PA_PROP_MEDIA_ICON_NAME, n);
+
+    pa_source_output_update_proplist(u->source_output, PA_UPDATE_REPLACE, p);
+    pa_proplist_free(p);
+}
+
+/* Called from main thread */
+static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
+    pa_assert_se(u = i->userdata);
+
+    if (!u->source_output->source->monitor_of)
+        return TRUE;
+
+    return dest != u->source_output->source->monitor_of;
+}
+
+int pa__init(pa_module *m) {
+    pa_modargs *ma = NULL;
+    struct userdata *u;
+    pa_sink *sink;
+    pa_sink_input_new_data sink_input_data;
+    pa_source *source;
+    pa_source_output_new_data source_output_data;
+    uint32_t latency_msec;
+    pa_sample_spec ss;
+    pa_channel_map map;
+    pa_memchunk silence;
+    uint32_t adjust_time_sec;
+    const char *n;
+
+    pa_assert(m);
+
+    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+        pa_log("Failed to parse module arguments");
+        goto fail;
+    }
+
+    if (!(source = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE))) {
+        pa_log("No such source.");
+        goto fail;
+    }
+
+    if (!(sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK))) {
+        pa_log("No such sink.");
+        goto fail;
+    }
+
+    ss = sink->sample_spec;
+    map = sink->channel_map;
+    if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+        pa_log("Invalid sample format specification or channel map");
+        goto fail;
+    }
+
+    latency_msec = DEFAULT_LATENCY_MSEC;
+    if (pa_modargs_get_value_u32(ma, "latency_msec", &latency_msec) < 0 || latency_msec < 1 || latency_msec > 2000) {
+        pa_log("Invalid latency specification");
+        goto fail;
+    }
+
+    m->userdata = u = pa_xnew0(struct userdata, 1);
+    u->core = m->core;
+    u->module = m;
+    u->latency = (pa_usec_t) latency_msec * PA_USEC_PER_MSEC;
+
+    adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
+    if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
+        pa_log("Failed to parse adjust_time value");
+        goto fail;
+    }
+
+    if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
+        u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
+    else
+        u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
+
+    pa_sink_input_new_data_init(&sink_input_data);
+    sink_input_data.driver = __FILE__;
+    sink_input_data.module = m;
+    sink_input_data.sink = sink;
+
+    pa_proplist_setf(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Loopback of %s",
+                     pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+    if ((n = pa_proplist_gets(source->proplist, PA_PROP_DEVICE_ICON_NAME)))
+        pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ICON_NAME, n);
+    pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
+    pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
+    pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
+
+    pa_sink_input_new(&u->sink_input, m->core, &sink_input_data, PA_SINK_INPUT_VARIABLE_RATE);
+    pa_sink_input_new_data_done(&sink_input_data);
+
+    if (!u->sink_input)
+        goto fail;
+
+    u->sink_input->parent.process_msg = sink_input_process_msg_cb;
+    u->sink_input->pop = sink_input_pop_cb;
+    u->sink_input->process_rewind = sink_input_process_rewind_cb;
+    u->sink_input->kill = sink_input_kill_cb;
+    u->sink_input->attach = sink_input_attach_cb;
+    u->sink_input->detach = sink_input_detach_cb;
+    u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
+    u->sink_input->update_max_request = sink_input_update_max_request_cb;
+    u->sink_input->may_move_to = sink_input_may_move_to_cb;
+    u->sink_input->moving = sink_input_moving_cb;
+    u->sink_input->userdata = u;
+
+    pa_sink_input_set_requested_latency(u->sink_input, u->latency/3);
+
+    pa_source_output_new_data_init(&source_output_data);
+    source_output_data.driver = __FILE__;
+    source_output_data.module = m;
+    source_output_data.source = source;
+    pa_proplist_setf(source_output_data.proplist, PA_PROP_MEDIA_NAME, "Loopback to %s",
+                     pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
+    if ((n = pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_ICON_NAME)))
+        pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ICON_NAME, n);
+    pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
+    pa_source_output_new_data_set_sample_spec(&source_output_data, &ss);
+    pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
+
+    pa_source_output_new(&u->source_output, m->core, &source_output_data, 0);
+    pa_source_output_new_data_done(&source_output_data);
+
+    if (!u->source_output)
+        goto fail;
+
+    u->source_output->parent.process_msg = source_output_process_msg_cb;
+    u->source_output->push = source_output_push_cb;
+    u->source_output->process_rewind = source_output_process_rewind_cb;
+    u->source_output->kill = source_output_kill_cb;
+    u->source_output->attach = source_output_attach_cb;
+    u->source_output->detach = source_output_detach_cb;
+    u->source_output->state_change = source_output_state_change_cb;
+    u->source_output->may_move_to = source_output_may_move_to_cb;
+    u->source_output->moving = source_output_moving_cb;
+    u->source_output->userdata = u;
+
+    pa_source_output_set_requested_latency(u->source_output, u->latency/3);
+
+    pa_sink_input_get_silence(u->sink_input, &silence);
+    u->memblockq = pa_memblockq_new(
+            0,                      /* idx */
+            MEMBLOCKQ_MAXLENGTH,    /* maxlength */
+            MEMBLOCKQ_MAXLENGTH,    /* tlength */
+            pa_frame_size(&ss),     /* base */
+            0,                      /* prebuf */
+            0,                      /* minreq */
+            0,                      /* maxrewind */
+            &silence);              /* silence frame */
+    pa_memblock_unref(silence.memblock);
+
+    u->asyncmsgq = pa_asyncmsgq_new(0);
+
+    pa_sink_input_put(u->sink_input);
+    pa_source_output_put(u->source_output);
+
+    if (u->adjust_time > 0)
+        u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
+
+    pa_modargs_free(ma);
+    return 0;
+
+fail:
+    if (ma)
+        pa_modargs_free(ma);
+
+    pa__done(m);
+
+    return -1;
+}
+
+void pa__done(pa_module*m) {
+    struct userdata *u;
+
+    pa_assert(m);
+
+    if (!(u = m->userdata))
+        return;
+
+    teardown(u);
+
+    if (u->memblockq)
+        pa_memblockq_free(u->memblockq);
+
+    if (u->asyncmsgq)
+        pa_asyncmsgq_unref(u->asyncmsgq);
+
+    if (u->time_event)
+        u->core->mainloop->time_free(u->time_event);
+
+    pa_xfree(u);
+}
index 71f1407..b0d4db4 100644 (file)
@@ -600,6 +600,10 @@ static void process_rewind(struct userdata *u) {
         pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
         rewind_nbytes = PA_MIN(u->memchunk.length, rewind_nbytes);
         u->memchunk.length -= rewind_nbytes;
+        if (u->memchunk.length <= 0 && u->memchunk.memblock) {
+            pa_memblock_unref(u->memchunk.memblock);
+            pa_memchunk_reset(&u->memchunk);
+        }
         pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
     }
 
@@ -671,8 +675,8 @@ static void thread_func(void *userdata) {
                 if (len < (size_t) u->minimum_request)
                     break;
 
-                if (u->memchunk.length < len)
-                    pa_sink_render(u->sink, len - u->memchunk.length, &u->memchunk);
+                if (!u->memchunk.length)
+                    pa_sink_render(u->sink, u->sink->thread_info.max_request, &u->memchunk);
 
                 len = PA_MIN(u->memchunk.length, len);
 
index ce88c1b..87684ad 100644 (file)
@@ -157,6 +157,17 @@ static inline size_t PA_PAGE_ALIGN(size_t l) {
 #define PA_ROUND_DOWN(a, b) (((a) / (b)) * (b))
 #endif
 
+#ifdef __GNUC__
+#define PA_CLIP_SUB(a, b)                       \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
+            typeof(b) _b = (b);                 \
+            _a > _b ? _a - _b : 0;              \
+        })
+#else
+#define PA_CLIP_SUB(a, b) ((a) > (b) ? (a) - (b) : 0)
+#endif
+
 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
 #ifdef HAVE_STD_BOOL
 typedef _Bool pa_bool_t;
index bfcae6c..00252da 100644 (file)
@@ -95,6 +95,7 @@
                 "4:                             \n\t"   \
                 " emms                          \n\t"
 
+#if defined (__i386__) || defined (__amd64__)
 static void remap_mono_to_stereo_mmx (pa_remap_t *m, void *dst, const void *src, unsigned n) {
     pa_reg_x86 temp;
 
@@ -138,6 +139,7 @@ static void init_remap_mmx (pa_remap_t *m) {
         pa_log_info("Using MMX mono to stereo remapping");
     }
 }
+#endif /* defined (__i386__) || defined (__amd64__) */
 
 void pa_remap_func_init_mmx (pa_cpu_x86_flag_t flags) {
 #if defined (__i386__) || defined (__amd64__)