From c3958aaa07373d4b77e6d8b753e13f48d624523a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 1 Jul 2009 02:20:23 +0200 Subject: bluetooth: handle absence of bluez D-Bus service properly --- src/modules/bluetooth/bluetooth-util.c | 77 +++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 11 deletions(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c index 5c7681d4..66e1c31e 100644 --- a/src/modules/bluetooth/bluetooth-util.c +++ b/src/modules/bluetooth/bluetooth-util.c @@ -309,6 +309,17 @@ static void run_callback(pa_bluetooth_discovery *y, pa_bluetooth_device *d, pa_b pa_hook_fire(&y->hook, d); } +static void remove_all_devices(pa_bluetooth_discovery *y) { + pa_bluetooth_device *d; + + pa_assert(y); + + while ((d = pa_hashmap_steal_first(y->devices))) { + run_callback(y, d, TRUE); + device_free(d); + } +} + static void get_properties_reply(DBusPendingCall *pending, void *userdata) { DBusMessage *r; DBusMessageIter arg_i, element_i; @@ -332,6 +343,12 @@ static void get_properties_reply(DBusPendingCall *pending, void *userdata) { if (dbus_message_is_method_call(p->message, "org.bluez.Device", "GetProperties")) d->device_info_valid = valid; + if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) { + pa_log_debug("Bluetooth daemon is apparently not available."); + remove_all_devices(y); + goto finish2; + } + if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) { if (!dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD)) @@ -383,6 +400,7 @@ static void get_properties_reply(DBusPendingCall *pending, void *userdata) { finish: run_callback(y, d, FALSE); +finish2: dbus_message_unref(r); PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p); @@ -412,6 +430,9 @@ static void found_device(pa_bluetooth_discovery *y, const char* path) { pa_assert(y); pa_assert(path); + if (pa_hashmap_get(y->devices, path)) + return; + d = device_new(path); pa_hashmap_put(y->devices, d->path, d); @@ -439,9 +460,15 @@ static void list_devices_reply(DBusPendingCall *pending, void *userdata) { pa_assert_se(y = p->context_data); pa_assert_se(r = dbus_pending_call_steal_reply(pending)); + if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) { + pa_log_debug("Bluetooth daemon is apparently not available."); + remove_all_devices(y); + goto finish; + } + if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) { pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r)); - goto end; + goto finish; } if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) { @@ -454,7 +481,7 @@ static void list_devices_reply(DBusPendingCall *pending, void *userdata) { found_device(y, paths[i]); } -end: +finish: if (paths) dbus_free_string_array (paths); @@ -487,9 +514,15 @@ static void list_adapters_reply(DBusPendingCall *pending, void *userdata) { pa_assert_se(y = p->context_data); pa_assert_se(r = dbus_pending_call_steal_reply(pending)); + if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) { + pa_log_debug("Bluetooth daemon is apparently not available."); + remove_all_devices(y); + goto finish; + } + if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) { pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r)); - goto end; + goto finish; } if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) { @@ -502,7 +535,7 @@ static void list_adapters_reply(DBusPendingCall *pending, void *userdata) { found_adapter(y, paths[i]); } -end: +finish: if (paths) dbus_free_string_array (paths); @@ -615,6 +648,32 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us run_callback(y, d, FALSE); } + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + } else if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) { + const char *name, *old_owner, *new_owner; + + if (!dbus_message_get_args(m, &err, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_STRING, &old_owner, + DBUS_TYPE_STRING, &new_owner, + DBUS_TYPE_INVALID)) { + pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message); + goto fail; + } + + if (pa_streq(name, "org.bluez")) { + if (old_owner && *old_owner) { + pa_log_debug("Bluetooth daemon disappeared."); + remove_all_devices(y); + } + + if (new_owner && *new_owner) { + pa_log_debug("Bluetooth daemon appeared."); + list_adapters(y); + } + } + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } @@ -699,6 +758,7 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) { if (pa_dbus_add_matches( pa_dbus_connection_get(y->connection), &err, + "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'", "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'", "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'", @@ -734,8 +794,6 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) { } void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) { - pa_bluetooth_device *d; - pa_assert(y); pa_assert(PA_REFCNT_VALUE(y) > 0); @@ -745,16 +803,13 @@ void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) { pa_dbus_free_pending_list(&y->pending); if (y->devices) { - while ((d = pa_hashmap_steal_first(y->devices))) { - run_callback(y, d, TRUE); - device_free(d); - } - + remove_all_devices(y); pa_hashmap_free(y->devices, NULL, NULL); } if (y->connection) { pa_dbus_remove_matches(pa_dbus_connection_get(y->connection), + "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'", "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'", "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'", "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", -- cgit From cd70d7f5b05778d810382c88695fa295a8557c4e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 1 Jul 2009 02:21:17 +0200 Subject: bluetooth: don't busy loop when device is not writable but we want to write --- src/modules/bluetooth/module-bluetooth-device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index e7c6d5e4..c46518e1 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -1355,7 +1355,7 @@ static void thread_func(void *userdata) { writable = FALSE; } - if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0) { + if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0 && writable) { pa_usec_t time_passed, next_write_at, sleep_for; /* Hmm, there is no input stream we could synchronize -- cgit From e93c6c36fe3bb6961be7c868627b99cafc015ca2 Mon Sep 17 00:00:00 2001 From: Marc-André Lureau Date: Wed, 1 Jul 2009 13:11:33 +0300 Subject: bluetooth: warn on EAGAIN after POLLOUT --- src/modules/bluetooth/module-bluetooth-device.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index c46518e1..f7dbfd9c 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -1351,6 +1351,9 @@ static void thread_func(void *userdata) { goto fail; } + if (n_written == 0) + pa_log("Broken kernel: we got EAGAIN on write() after POLLOUT!"); + do_write -= n_written; writable = FALSE; } -- cgit From 95d3faaac8038116d8008477dcf1034a25ab30a0 Mon Sep 17 00:00:00 2001 From: Marc-André Lureau Date: Mon, 29 Jun 2009 17:46:30 +0300 Subject: bluetooth: don't connect on unconnected profile --- src/modules/bluetooth/module-bluetooth-device.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index f7dbfd9c..0560ef32 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -2085,6 +2085,15 @@ static int add_card(struct userdata *u, const pa_bluetooth_device *device) { u->card->userdata = u; u->card->set_profile = card_set_profile; + d = PA_CARD_PROFILE_DATA(u->card->active_profile); + + if ((device->headset_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HSP) || + (device->audio_sink_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_A2DP)) { + pa_log_warn("Default profile not connected, selecting off profile"); + u->card->active_profile = pa_hashmap_get(u->card->profiles, "off"); + u->card->save_profile = FALSE; + } + d = PA_CARD_PROFILE_DATA(u->card->active_profile); u->profile = *d; -- cgit From 51b3899348bf29dd88b56691aeea9f57895dfd14 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 7 Aug 2009 23:55:06 +0200 Subject: core: save volume/mute changes coming from the hardware automatically Volume changes coming from the lower layers are most likely changes triggered by the user, so let's save them automatically. --- src/modules/bluetooth/module-bluetooth-device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index 0560ef32..e682997f 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -1446,12 +1446,12 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us if (u->sink && dbus_message_is_signal(m, "org.bluez.Headset", "SpeakerGainChanged")) { pa_cvolume_set(&v, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); - pa_sink_volume_changed(u->sink, &v, TRUE); + pa_sink_volume_changed(u->sink, &v); } else if (u->source && dbus_message_is_signal(m, "org.bluez.Headset", "MicrophoneGainChanged")) { pa_cvolume_set(&v, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); - pa_source_volume_changed(u->source, &v, TRUE); + pa_source_volume_changed(u->source, &v); } } } -- cgit From 350a2bc846559bb274ba70f928bb42a9472050bf Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 15 Aug 2009 00:48:14 +0200 Subject: core: make fixed latency dynamically changeable This of course makes the name 'fixed' a bit of a misnomer. However the definitions are now like this: fixed latency: the latency may change during runtime, but is solely controlled by the backend, the client has no influence. dynamic latency: the latency may change during runtime, influenced by the requests of the clients. i.e. fixed vs. dynamic is from the perspective of the client. --- src/modules/bluetooth/module-bluetooth-device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index e682997f..93b14a12 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -881,7 +881,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse *((pa_usec_t*) data) = wi > ri ? wi - ri : 0; } - *((pa_usec_t*) data) += u->sink->fixed_latency; + *((pa_usec_t*) data) += u->sink->thread_info.fixed_latency; return 0; } } @@ -943,7 +943,7 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off wi = pa_smoother_get(u->read_smoother, pa_rtclock_now()); ri = pa_bytes_to_usec(u->read_index, &u->sample_spec); - *((pa_usec_t*) data) = (wi > ri ? wi - ri : 0) + u->source->fixed_latency; + *((pa_usec_t*) data) = (wi > ri ? wi - ri : 0) + u->source->thread_info.fixed_latency; return 0; } -- cgit From fd32fee4488b1e06d0a64079485aa77f0c87164b Mon Sep 17 00:00:00 2001 From: Marc-André Lureau Date: Mon, 6 Jul 2009 17:50:51 +0300 Subject: bluetooth: don't call pa_sink_render with 0 bytes request --- src/modules/bluetooth/module-bluetooth-device.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index 93b14a12..d9831854 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -1319,18 +1319,21 @@ static void thread_func(void *userdata) { if (u->write_index > 0 && audio_to_send > MAX_PLAYBACK_CATCH_UP_USEC) { pa_usec_t skip_usec; uint64_t skip_bytes; - pa_memchunk tmp; skip_usec = audio_to_send - MAX_PLAYBACK_CATCH_UP_USEC; skip_bytes = pa_usec_to_bytes(skip_usec, &u->sample_spec); - pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream", - (unsigned long long) skip_usec, - (unsigned long long) skip_bytes); + if (skip_bytes > 0) { + pa_memchunk tmp; - pa_sink_render_full(u->sink, skip_bytes, &tmp); - pa_memblock_unref(tmp.memblock); - u->write_index += skip_bytes; + pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream", + (unsigned long long) skip_usec, + (unsigned long long) skip_bytes); + + pa_sink_render_full(u->sink, skip_bytes, &tmp); + pa_memblock_unref(tmp.memblock); + u->write_index += skip_bytes; + } } do_write = 1; -- cgit From 3ecb80e19ab9aa678b7db6a5068553d956d707fa Mon Sep 17 00:00:00 2001 From: Marc-André Lureau Date: Mon, 20 Jul 2009 13:44:27 +0300 Subject: bluetooth: fix typo with service_{read,write}_type --- src/modules/bluetooth/module-bluetooth-device.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index d9831854..5f119c7a 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -1739,7 +1739,8 @@ static void shutdown_bt(struct userdata *u) { if (u->service_fd >= 0) { pa_close(u->service_fd); u->service_fd = -1; - u->service_write_type = u->service_write_type = 0; + u->service_write_type = 0; + u->service_read_type = 0; } if (u->write_memchunk.memblock) { @@ -1755,7 +1756,8 @@ static int init_bt(struct userdata *u) { shutdown_bt(u); u->stream_write_type = 0; - u->service_write_type = u->service_write_type = 0; + u->service_write_type = 0; + u->service_read_type = 0; if ((u->service_fd = bt_audio_service_open()) < 0) { pa_log_error("Couldn't connect to bluetooth audio service"); -- cgit From 5c90cf2d6a148d5450b4d05edfc98d32ae83a854 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 15 Aug 2009 22:24:13 +0200 Subject: bluetooth: drop PA_BT_AUDIO_STATE_LAST since it is unused and we normally call that _MAX anyway --- src/modules/bluetooth/bluetooth-util.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/bluetooth-util.h b/src/modules/bluetooth/bluetooth-util.h index 265caf40..f15f2170 100644 --- a/src/modules/bluetooth/bluetooth-util.h +++ b/src/modules/bluetooth/bluetooth-util.h @@ -59,8 +59,7 @@ typedef enum pa_bt_audio_state { PA_BT_AUDIO_STATE_DISCONNECTED, PA_BT_AUDIO_STATE_CONNECTING, PA_BT_AUDIO_STATE_CONNECTED, - PA_BT_AUDIO_STATE_PLAYING, - PA_BT_AUDIO_STATE_LAST + PA_BT_AUDIO_STATE_PLAYING } pa_bt_audio_state_t; struct pa_bluetooth_device { -- cgit From fa52a91b1a1d89e0a99faeea821d3e1a3597eb9a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 15 Aug 2009 22:25:21 +0200 Subject: bluetooth: recognize only those BT devices that implement both the Audio and either AudioSink or Headset interfaces --- src/modules/bluetooth/bluetooth-util.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c index 66e1c31e..d5806b96 100644 --- a/src/modules/bluetooth/bluetooth-util.c +++ b/src/modules/bluetooth/bluetooth-util.c @@ -122,9 +122,9 @@ static pa_bool_t device_is_audio(pa_bluetooth_device *d) { return d->device_info_valid && - (d->audio_state != PA_BT_AUDIO_STATE_INVALID || - d->audio_sink_state != PA_BT_AUDIO_STATE_INVALID || - d->headset_state != PA_BT_AUDIO_STATE_INVALID); + (d->audio_state != PA_BT_AUDIO_STATE_INVALID && + (d->audio_sink_state != PA_BT_AUDIO_STATE_INVALID || + d->headset_state != PA_BT_AUDIO_STATE_INVALID)); } static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessageIter *i) { -- cgit From de4968cdded8d78fe2e59b4487a21937b843c570 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 15 Aug 2009 22:25:53 +0200 Subject: bluetooth: ask first for Headset and AudioSink properties, followed by Audio --- src/modules/bluetooth/bluetooth-util.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c index d5806b96..16c29248 100644 --- a/src/modules/bluetooth/bluetooth-util.c +++ b/src/modules/bluetooth/bluetooth-util.c @@ -226,10 +226,6 @@ static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device node = uuid_new(value); PA_LLIST_PREPEND(pa_bluetooth_uuid, d->uuids, node); - /* 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); - /* Vudentz said the interfaces are here when the UUIDs are announced */ 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")); @@ -239,6 +235,10 @@ static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device 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 */ + 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); + if (!dbus_message_iter_next(&ai)) break; } -- cgit From 2a39663ab61614982c52e244bde596dcc1a08f37 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 16 Aug 2009 00:43:22 +0200 Subject: bluetooth: move installation of mq's earlier to avoid context asserts to be triggered --- src/modules/bluetooth/module-bluetooth-device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/modules/bluetooth') diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index 5f119c7a..d6321fc4 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -1262,11 +1262,11 @@ static void thread_func(void *userdata) { if (u->core->realtime_scheduling) pa_make_realtime(u->core->realtime_priority); + pa_thread_mq_install(&u->thread_mq); + if (start_stream_fd(u) < 0) goto fail; - pa_thread_mq_install(&u->thread_mq); - for (;;) { struct pollfd *pollfd; int ret; -- cgit