diff options
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/alsa/alsa-sink.c | 148 | ||||
-rw-r--r-- | src/modules/alsa/alsa-source.c | 143 | ||||
-rw-r--r-- | src/modules/alsa/alsa-util.c | 123 | ||||
-rw-r--r-- | src/modules/alsa/alsa-util.h | 5 | ||||
-rw-r--r-- | src/modules/alsa/module-alsa-card.c | 52 | ||||
-rw-r--r-- | src/modules/bluetooth/module-bluetooth-device.c | 188 | ||||
-rw-r--r-- | src/modules/module-augment-properties.c | 2 | ||||
-rw-r--r-- | src/modules/module-combine.c | 3 | ||||
-rw-r--r-- | src/modules/module-jack-sink.c | 10 | ||||
-rw-r--r-- | src/modules/module-jack-source.c | 10 | ||||
-rw-r--r-- | src/modules/module-null-sink.c | 1 | ||||
-rw-r--r-- | src/modules/module-pipe-sink.c | 1 | ||||
-rw-r--r-- | src/modules/module-pipe-source.c | 1 | ||||
-rw-r--r-- | src/modules/module-raop-discover.c | 6 | ||||
-rw-r--r-- | src/modules/module-raop-sink.c | 6 | ||||
-rw-r--r-- | src/modules/module-tunnel.c | 1 | ||||
-rw-r--r-- | src/modules/module-zeroconf-discover.c | 2 | ||||
-rw-r--r-- | src/modules/oss/module-oss.c | 1 | ||||
-rw-r--r-- | src/modules/reserve-wrap.c | 168 | ||||
-rw-r--r-- | src/modules/reserve-wrap.h | 38 | ||||
-rw-r--r-- | src/modules/reserve.c | 635 | ||||
-rw-r--r-- | src/modules/reserve.h | 68 |
22 files changed, 1509 insertions, 103 deletions
diff --git a/src/modules/alsa/alsa-sink.c b/src/modules/alsa/alsa-sink.c index 239a9d78..dbd95b63 100644 --- a/src/modules/alsa/alsa-sink.c +++ b/src/modules/alsa/alsa-sink.c @@ -53,6 +53,8 @@ #include <pulsecore/rtclock.h> #include <pulsecore/time-smoother.h> +#include <modules/reserve-wrap.h> + #include "alsa-util.h" #include "alsa-sink.h" @@ -101,10 +103,75 @@ struct userdata { pa_smoother *smoother; uint64_t write_count; uint64_t since_start; + + pa_reserve_wrapper *reserve; + pa_hook_slot *reserve_slot; }; static void userdata_free(struct userdata *u); +static pa_hook_result_t reserve_cb(pa_reserve_wrapper *r, void *forced, struct userdata *u) { + pa_assert(r); + pa_assert(u); + + if (pa_sink_suspend(u->sink, TRUE) < 0) + return PA_HOOK_CANCEL; + + return PA_HOOK_OK; +} + +static void reserve_done(struct userdata *u) { + pa_assert(u); + + if (u->reserve_slot) { + pa_hook_slot_free(u->reserve_slot); + u->reserve_slot = NULL; + } + + if (u->reserve) { + pa_reserve_wrapper_unref(u->reserve); + u->reserve = NULL; + } +} + +static void reserve_update(struct userdata *u) { + const char *description; + pa_assert(u); + + if (!u->sink) + return; + + if ((description = pa_proplist_gets(u->sink->proplist, PA_PROP_DEVICE_DESCRIPTION))) + pa_reserve_wrapper_set_application_device_name(u->reserve, description); +} + +static int reserve_init(struct userdata *u, const char *dname) { + char *rname; + + pa_assert(u); + pa_assert(dname); + + if (u->reserve) + return 0; + + /* We are resuming, try to lock the device */ + if (!(rname = pa_alsa_get_reserve_name(dname))) + return 0; + + u->reserve = pa_reserve_wrapper_get(u->core, rname); + pa_xfree(rname); + + if (!(u->reserve)) + return -1; + + reserve_update(u); + + pa_assert(!u->reserve_slot); + u->reserve_slot = pa_hook_connect(pa_reserve_wrapper_hook(u->reserve), PA_HOOK_NORMAL, (pa_hook_cb_t) reserve_cb, u); + + return 0; +} + static void fix_min_sleep_wakeup(struct userdata *u) { size_t max_use, max_use_2; @@ -255,6 +322,7 @@ static int mmap_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polle pa_bool_t work_done = TRUE; pa_usec_t max_sleep_usec = 0, process_usec = 0; size_t left_to_play; + unsigned j = 0; pa_assert(u); pa_sink_assert_ref(u->sink); @@ -304,10 +372,15 @@ static int mmap_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polle if (PA_UNLIKELY(n_bytes <= u->hwbuf_unused)) { - if (polled && pa_log_ratelimit()) - pa_log(_("ALSA woke us up to write new data to the device, but there was actually nothing to write! " - "Most likely this is an ALSA driver bug. Please report this issue to the ALSA developers. " - "We were woken up with POLLOUT set -- however a subsequent snd_pcm_avail_update() returned 0.")); + if (polled) + PA_ONCE_BEGIN { + char *dn = pa_alsa_get_driver_name_by_pcm(u->pcm_handle); + pa_log(_("ALSA woke us up to write new data to the device, but there was actually nothing to write!\n" + "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers.\n" + "We were woken up with POLLOUT set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail."), + pa_strnull(dn)); + pa_xfree(dn); + } PA_ONCE_END; #ifdef DEBUG_TIMING pa_log_debug("Not filling up, because not necessary."); @@ -315,6 +388,15 @@ static int mmap_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polle break; } + + if (++j > 10) { +#ifdef DEBUG_TIMING + pa_log_debug("Not filling up, because already too many iterations."); +#endif + + break; + } + n_bytes -= u->hwbuf_unused; polled = FALSE; @@ -394,6 +476,7 @@ static int unix_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polle pa_bool_t work_done = FALSE; pa_usec_t max_sleep_usec = 0, process_usec = 0; size_t left_to_play; + unsigned j = 0; pa_assert(u); pa_sink_assert_ref(u->sink); @@ -431,10 +514,23 @@ static int unix_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polle if (PA_UNLIKELY(n_bytes <= u->hwbuf_unused)) { - if (polled && pa_log_ratelimit()) - pa_log(_("ALSA woke us up to write new data to the device, but there was actually nothing to write! " - "Most likely this is an ALSA driver bug. Please report this issue to the ALSA developers. " - "We were woken up with POLLOUT set -- however a subsequent snd_pcm_avail_update() returned 0.")); + if (polled) + PA_ONCE_BEGIN { + char *dn = pa_alsa_get_driver_name_by_pcm(u->pcm_handle); + pa_log(_("ALSA woke us up to write new data to the device, but there was actually nothing to write!\n" + "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers.\n" + "We were woken up with POLLOUT set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail."), + pa_strnull(dn)); + pa_xfree(dn); + } PA_ONCE_END; + + break; + } + + if (++j > 10) { +#ifdef DEBUG_TIMING + pa_log_debug("Not filling up, because already too many iterations."); +#endif break; } @@ -512,7 +608,7 @@ static void update_smoother(struct userdata *u) { /* Let's update the time smoother */ - if (PA_UNLIKELY((err = snd_pcm_delay(u->pcm_handle, &delay)) < 0)) { + if (PA_UNLIKELY((err = pa_alsa_safe_delay(u->pcm_handle, &delay, u->hwbuf_size, &u->sink->sample_spec)) < 0)) { pa_log_warn("Failed to query DSP status data: %s", snd_strerror(err)); return; } @@ -572,6 +668,7 @@ static int build_pollfd(struct userdata *u) { return 0; } +/* Called from IO context */ static int suspend(struct userdata *u) { pa_assert(u); pa_assert(u->pcm_handle); @@ -593,6 +690,7 @@ static int suspend(struct userdata *u) { return 0; } +/* Called from IO context */ static int update_sw_params(struct userdata *u) { snd_pcm_uframes_t avail_min; int err; @@ -648,6 +746,7 @@ static int update_sw_params(struct userdata *u) { return 0; } +/* Called from IO context */ static int unsuspend(struct userdata *u) { pa_sample_spec ss; int err; @@ -720,6 +819,7 @@ fail: return -1; } +/* Called from IO context */ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; @@ -775,6 +875,25 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse return pa_sink_process_msg(o, code, data, offset, chunk); } +/* Called from main context */ +static int sink_set_state_cb(pa_sink *s, pa_sink_state_t new_state) { + pa_sink_state_t old_state; + struct userdata *u; + + pa_sink_assert_ref(s); + pa_assert_se(u = s->userdata); + + old_state = pa_sink_get_state(u->sink); + + if (PA_SINK_IS_OPENED(old_state) && new_state == PA_SINK_SUSPENDED) + reserve_done(u); + else if (old_state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(new_state)) + if (reserve_init(u, u->device_name) < 0) + return -1; + + return 0; +} + static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) { struct userdata *u = snd_mixer_elem_get_callback_private(elem); @@ -1378,6 +1497,7 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca pa_assert(ma); ss = m->core->default_sample_spec; + map = m->core->default_channel_map; if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) { pa_log("Failed to parse sample specification and channel map"); goto fail; @@ -1438,6 +1558,11 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca pa_smoother_set_time_offset(u->smoother, usec); pa_smoother_pause(u->smoother, usec); + if (reserve_init(u, pa_modargs_get_value( + ma, "device_id", + pa_modargs_get_value(ma, "device", DEFAULT_DEVICE))) < 0) + goto fail; + b = use_mmap; d = use_tsched; @@ -1539,6 +1664,7 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca u->sink->parent.process_msg = sink_process_msg; u->sink->update_requested_latency = sink_update_requested_latency_cb; + u->sink->set_state = sink_set_state_cb; u->sink->userdata = u; pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); @@ -1571,6 +1697,8 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca pa_log_info("Time scheduling watermark is %0.2fms", (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC); + reserve_update(u); + if (update_sw_params(u) < 0) goto fail; @@ -1651,6 +1779,8 @@ static void userdata_free(struct userdata *u) { if (u->smoother) pa_smoother_free(u->smoother); + reserve_done(u); + pa_xfree(u->device_name); pa_xfree(u); } diff --git a/src/modules/alsa/alsa-source.c b/src/modules/alsa/alsa-source.c index 50cdb310..39df4a91 100644 --- a/src/modules/alsa/alsa-source.c +++ b/src/modules/alsa/alsa-source.c @@ -54,6 +54,8 @@ #include <pulsecore/time-smoother.h> #include <pulsecore/rtclock.h> +#include <modules/reserve-wrap.h> + #include "alsa-util.h" #include "alsa-source.h" @@ -99,10 +101,75 @@ struct userdata { pa_smoother *smoother; uint64_t read_count; + + pa_reserve_wrapper *reserve; + pa_hook_slot *reserve_slot; }; static void userdata_free(struct userdata *u); +static pa_hook_result_t reserve_cb(pa_reserve_wrapper *r, void *forced, struct userdata *u) { + pa_assert(r); + pa_assert(u); + + if (pa_source_suspend(u->source, TRUE) < 0) + return PA_HOOK_CANCEL; + + return PA_HOOK_OK; +} + +static void reserve_done(struct userdata *u) { + pa_assert(u); + + if (u->reserve_slot) { + pa_hook_slot_free(u->reserve_slot); + u->reserve_slot = NULL; + } + + if (u->reserve) { + pa_reserve_wrapper_unref(u->reserve); + u->reserve = NULL; + } +} + +static void reserve_update(struct userdata *u) { + const char *description; + pa_assert(u); + + if (!u->source) + return; + + if ((description = pa_proplist_gets(u->source->proplist, PA_PROP_DEVICE_DESCRIPTION))) + pa_reserve_wrapper_set_application_device_name(u->reserve, description); +} + +static int reserve_init(struct userdata *u, const char *dname) { + char *rname; + + pa_assert(u); + pa_assert(dname); + + if (u->reserve) + return 0; + + /* We are resuming, try to lock the device */ + if (!(rname = pa_alsa_get_reserve_name(dname))) + return 0; + + u->reserve = pa_reserve_wrapper_get(u->core, rname); + pa_xfree(rname); + + if (!(u->reserve)) + return -1; + + reserve_update(u); + + pa_assert(!u->reserve_slot); + u->reserve_slot = pa_hook_connect(pa_reserve_wrapper_hook(u->reserve), PA_HOOK_NORMAL, (pa_hook_cb_t) reserve_cb, u); + + return 0; +} + static void fix_min_sleep_wakeup(struct userdata *u) { size_t max_use, max_use_2; pa_assert(u); @@ -248,6 +315,7 @@ static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled pa_bool_t work_done = FALSE; pa_usec_t max_sleep_usec = 0, process_usec = 0; size_t left_to_record; + unsigned j = 0; pa_assert(u); pa_source_assert_ref(u->source); @@ -287,10 +355,15 @@ static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled if (PA_UNLIKELY(n_bytes <= 0)) { - if (polled && pa_log_ratelimit()) - pa_log(_("ALSA woke us up to read new data from the device, but there was actually nothing to read! " - "Most likely this is an ALSA driver bug. Please report this issue to the ALSA developers. " - "We were woken up with POLLIN set -- however a subsequent snd_pcm_avail_update() returned 0.")); + if (polled) + PA_ONCE_BEGIN { + char *dn = pa_alsa_get_driver_name_by_pcm(u->pcm_handle); + pa_log(_("ALSA woke us up to read new data from the device, but there was actually nothing to read!\n" + "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers.\n" + "We were woken up with POLLIN set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail."), + pa_strnull(dn)); + pa_xfree(dn); + } PA_ONCE_END; #ifdef DEBUG_TIMING pa_log_debug("Not reading, because not necessary."); @@ -298,6 +371,14 @@ static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled break; } + if (++j > 10) { +#ifdef DEBUG_TIMING + pa_log_debug("Not filling up, because already too many iterations."); +#endif + + break; + } + polled = FALSE; #ifdef DEBUG_TIMING @@ -376,6 +457,7 @@ static int unix_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled int work_done = FALSE; pa_usec_t max_sleep_usec = 0, process_usec = 0; size_t left_to_record; + unsigned j = 0; pa_assert(u); pa_source_assert_ref(u->source); @@ -406,10 +488,23 @@ static int unix_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled if (PA_UNLIKELY(n_bytes <= 0)) { - if (polled && pa_log_ratelimit()) - pa_log(_("ALSA woke us up to read new data from the device, but there was actually nothing to read! " - "Most likely this is an ALSA driver bug. Please report this issue to the ALSA developers. " - "We were woken up with POLLIN set -- however a subsequent snd_pcm_avail_update() returned 0.")); + if (polled) + PA_ONCE_BEGIN { + char *dn = pa_alsa_get_driver_name_by_pcm(u->pcm_handle); + pa_log(_("ALSA woke us up to read new data from the device, but there was actually nothing to read!\n" + "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers.\n" + "We were woken up with POLLIN set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail."), + pa_strnull(dn)); + pa_xfree(dn); + } PA_ONCE_END; + + break; + } + + if (++j > 10) { +#ifdef DEBUG_TIMING + pa_log_debug("Not filling up, because already too many iterations."); +#endif break; } @@ -482,7 +577,7 @@ static void update_smoother(struct userdata *u) { /* Let's update the time smoother */ - if (PA_UNLIKELY((err = snd_pcm_delay(u->pcm_handle, &delay)) < 0)) { + if (PA_UNLIKELY((err = pa_alsa_safe_delay(u->pcm_handle, &delay, u->hwbuf_size, &u->source->sample_spec)) < 0)) { pa_log_warn("Failed to get delay: %s", snd_strerror(err)); return; } @@ -737,6 +832,25 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off return pa_source_process_msg(o, code, data, offset, chunk); } +/* Called from main context */ +static int source_set_state_cb(pa_source *s, pa_source_state_t new_state) { + pa_source_state_t old_state; + struct userdata *u; + + pa_source_assert_ref(s); + pa_assert_se(u = s->userdata); + + old_state = pa_source_get_state(u->source); + + if (PA_SINK_IS_OPENED(old_state) && new_state == PA_SINK_SUSPENDED) + reserve_done(u); + else if (old_state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(new_state)) + if (reserve_init(u, u->device_name) < 0) + return -1; + + return 0; +} + static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) { struct userdata *u = snd_mixer_elem_get_callback_private(elem); @@ -1229,6 +1343,7 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p pa_assert(ma); ss = m->core->default_sample_spec; + map = m->core->default_channel_map; if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) { pa_log("Failed to parse sample specification"); goto fail; @@ -1287,6 +1402,11 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p u->smoother = pa_smoother_new(DEFAULT_TSCHED_WATERMARK_USEC*2, DEFAULT_TSCHED_WATERMARK_USEC*2, TRUE, 5); pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec()); + if (reserve_init(u, pa_modargs_get_value( + ma, "device_id", + pa_modargs_get_value(ma, "device", DEFAULT_DEVICE))) < 0) + goto fail; + b = use_mmap; d = use_tsched; @@ -1385,6 +1505,7 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p u->source->parent.process_msg = source_process_msg; u->source->update_requested_latency = source_update_requested_latency_cb; + u->source->set_state = source_set_state_cb; u->source->userdata = u; pa_source_set_asyncmsgq(u->source, u->thread_mq.inq); @@ -1414,6 +1535,8 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p pa_log_info("Time scheduling watermark is %0.2fms", (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC); + reserve_update(u); + if (update_sw_params(u) < 0) goto fail; @@ -1490,6 +1613,8 @@ static void userdata_free(struct userdata *u) { if (u->smoother) pa_smoother_free(u->smoother); + reserve_done(u); + pa_xfree(u->device_name); pa_xfree(u); } diff --git a/src/modules/alsa/alsa-util.c b/src/modules/alsa/alsa-util.c index d00a80f1..6740c069 100644 --- a/src/modules/alsa/alsa-util.c +++ b/src/modules/alsa/alsa-util.c @@ -39,6 +39,7 @@ #include <pulsecore/core-util.h> #include <pulsecore/atomic.h> #include <pulsecore/core-error.h> +#include <pulsecore/once.h> #include "alsa-util.h" @@ -1026,6 +1027,7 @@ snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const pa_assert(name); snd_mixer_selem_id_set_name(sid, name); + snd_mixer_selem_id_set_index(sid, 0); if ((elem = snd_mixer_find_selem(mixer, sid))) { @@ -1042,6 +1044,7 @@ snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const if (fallback) { snd_mixer_selem_id_set_name(sid, fallback); + snd_mixer_selem_id_set_index(sid, 0); if ((fallback_elem = snd_mixer_find_selem(mixer, sid))) { @@ -1084,7 +1087,6 @@ success: return elem; } - int pa_alsa_find_mixer_and_elem( snd_pcm_t *pcm, snd_mixer_t **_m, @@ -1446,9 +1448,9 @@ void pa_alsa_init_proplist_pcm_info(pa_core *c, pa_proplist *p, snd_pcm_info_t * cn = pa_proplist_gets(p, "alsa.card_name"); } - if (cn && n) - pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, "%s - %s", cn, n); - else if (cn) + if (cn && n && !strstr(cn, n) && !strstr(n, cn)) + pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, "%s, %s", cn, n); + else if (cn && (!n || strstr(cn, n))) pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, cn); else if (n) pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, n); @@ -1572,15 +1574,70 @@ snd_pcm_sframes_t pa_alsa_safe_avail(snd_pcm_t *pcm, size_t hwbuf_size, const pa k = (size_t) n * pa_frame_size(ss); - if (k >= hwbuf_size * 3 || - k >= pa_bytes_per_second(ss)*10) - pa_log(_("snd_pcm_avail_update() returned a value that is exceptionally large: %lu bytes (%lu ms). " - "Most likely this is an ALSA driver bug. Please report this issue to the ALSA developers."), - (unsigned long) k, (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC)); + if (k >= hwbuf_size * 5 || + k >= pa_bytes_per_second(ss)*10) { + + PA_ONCE_BEGIN { + char *dn = pa_alsa_get_driver_name_by_pcm(pcm); + pa_log(_("snd_pcm_avail() returned a value that is exceptionally large: %lu bytes (%lu ms).\n" + "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."), + (unsigned long) k, + (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC), + pa_strnull(dn)); + pa_xfree(dn); + } PA_ONCE_END; + + /* Mhmm, let's try not to fail completely */ + n = (snd_pcm_sframes_t) (hwbuf_size / pa_frame_size(ss)); + } return n; } +int pa_alsa_safe_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delay, size_t hwbuf_size, const pa_sample_spec *ss) { + ssize_t k; + size_t abs_k; + int r; + + pa_assert(pcm); + pa_assert(delay); + pa_assert(hwbuf_size > 0); + pa_assert(ss); + + /* Some ALSA driver expose weird bugs, let's inform the user about + * what is going on */ + + if ((r = snd_pcm_delay(pcm, delay)) < 0) + return r; + + k = (ssize_t) *delay * (ssize_t) pa_frame_size(ss); + + abs_k = k >= 0 ? (size_t) k : (size_t) -k; + + if (abs_k >= hwbuf_size * 5 || + abs_k >= pa_bytes_per_second(ss)*10) { + + PA_ONCE_BEGIN { + char *dn = pa_alsa_get_driver_name_by_pcm(pcm); + pa_log(_("snd_pcm_delay() returned a value that is exceptionally large: %li bytes (%s%lu ms).\n" + "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."), + (signed long) k, + k < 0 ? "-" : "", + (unsigned long) (pa_bytes_to_usec(abs_k, ss) / PA_USEC_PER_MSEC), + pa_strnull(dn)); + pa_xfree(dn); + } PA_ONCE_END; + + /* Mhmm, let's try not to fail completely */ + if (k < 0) + *delay = -(snd_pcm_sframes_t) (hwbuf_size / pa_frame_size(ss)); + else + *delay = (snd_pcm_sframes_t) (hwbuf_size / pa_frame_size(ss)); + } + + return 0; +} + int pa_alsa_safe_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames, size_t hwbuf_size, const pa_sample_spec *ss) { int r; snd_pcm_uframes_t before; @@ -1606,9 +1663,15 @@ int pa_alsa_safe_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas k >= hwbuf_size * 3 || k >= pa_bytes_per_second(ss)*10) - pa_log(_("snd_pcm_mmap_begin() returned a value that is exceptionally large: %lu bytes (%lu ms). " - "Most likely this is an ALSA driver bug. Please report this issue to the ALSA developers."), - (unsigned long) k, (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC)); + PA_ONCE_BEGIN { + char *dn = pa_alsa_get_driver_name_by_pcm(pcm); + pa_log(_("snd_pcm_mmap_begin() returned a value that is exceptionally large: %lu bytes (%lu ms).\n" + "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."), + (unsigned long) k, + (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC), + pa_strnull(dn)); + pa_xfree(dn); + } PA_ONCE_END; return r; } @@ -1630,3 +1693,39 @@ char *pa_alsa_get_driver_name(int card) { return n; } + +char *pa_alsa_get_driver_name_by_pcm(snd_pcm_t *pcm) { + int card; + + snd_pcm_info_t* info; + snd_pcm_info_alloca(&info); + + if (snd_pcm_info(pcm, info) < 0) + return NULL; + + if ((card = snd_pcm_info_get_card(info)) < 0) + return NULL; + + return pa_alsa_get_driver_name(card); +} + +char *pa_alsa_get_reserve_name(const char *device) { + const char *t; + int i; + + pa_assert(device); + + if ((t = strchr(device, ':'))) + device = t+1; + + if ((i = snd_card_get_index(device)) < 0) { + int32_t k; + + if (pa_atoi(device, &k) < 0) + return NULL; + + i = (int) k; + } + + return pa_sprintf_malloc("Audio%i", i); +} diff --git a/src/modules/alsa/alsa-util.h b/src/modules/alsa/alsa-util.h index 2d0f407e..899532e2 100644 --- a/src/modules/alsa/alsa-util.h +++ b/src/modules/alsa/alsa-util.h @@ -129,8 +129,13 @@ int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents); pa_rtpoll_item* pa_alsa_build_pollfd(snd_pcm_t *pcm, pa_rtpoll *rtpoll); snd_pcm_sframes_t pa_alsa_safe_avail(snd_pcm_t *pcm, size_t hwbuf_size, const pa_sample_spec *ss); +int pa_alsa_safe_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delay, size_t hwbuf_size, const pa_sample_spec *ss); int pa_alsa_safe_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames, size_t hwbuf_size, const pa_sample_spec *ss); char *pa_alsa_get_driver_name(int card); +char *pa_alsa_get_driver_name_by_pcm(snd_pcm_t *pcm); + +char *pa_alsa_get_reserve_name(const char *device); + #endif diff --git a/src/modules/alsa/module-alsa-card.c b/src/modules/alsa/module-alsa-card.c index e517ddcc..fc6b886b 100644 --- a/src/modules/alsa/module-alsa-card.c +++ b/src/modules/alsa/module-alsa-card.c @@ -30,6 +30,8 @@ #include <pulsecore/modargs.h> #include <pulsecore/queue.h> +#include <modules/reserve-wrap.h> + #include "alsa-util.h" #include "alsa-sink.h" #include "alsa-source.h" @@ -88,6 +90,8 @@ struct userdata { pa_source *source; pa_modargs *modargs; + + pa_hashmap *profiles; }; struct profile_data { @@ -99,10 +103,11 @@ static void enumerate_cb( const pa_alsa_profile_info *source, void *userdata) { - pa_hashmap *profiles = (pa_hashmap *) userdata; + struct userdata *u = userdata; char *t, *n; pa_card_profile *p; struct profile_data *d; + unsigned bonus = 0; if (sink && source) { n = pa_sprintf_malloc("output-%s+input-%s", sink->name, source->name); @@ -116,6 +121,20 @@ static void enumerate_cb( t = pa_sprintf_malloc(_("Input %s"), _(source->description)); } + if (sink) { + if (pa_channel_map_equal(&sink->map, &u->core->default_channel_map)) + bonus += 50000; + else if (sink->map.channels == u->core->default_channel_map.channels) + bonus += 40000; + } + + if (source) { + if (pa_channel_map_equal(&source->map, &u->core->default_channel_map)) + bonus += 30000; + else if (source->map.channels == u->core->default_channel_map.channels) + bonus += 20000; + } + pa_log_info("Found output profile '%s'", t); p = pa_card_profile_new(n, t, sizeof(struct profile_data)); @@ -123,7 +142,11 @@ static void enumerate_cb( pa_xfree(t); pa_xfree(n); - p->priority = (sink ? sink->priority : 0)*100 + (source ? source->priority : 0); + p->priority = + (sink ? sink->priority : 0) * 100 + + (source ? source->priority : 0) + + bonus; + p->n_sinks = !!sink; p->n_sources = !!source; @@ -137,7 +160,7 @@ static void enumerate_cb( d->sink_profile = sink; d->source_profile = source; - pa_hashmap_put(profiles, p->name, p); + pa_hashmap_put(u->profiles, p->name, p); } static void add_disabled_profile(pa_hashmap *profiles) { @@ -252,11 +275,14 @@ static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *de pa_xfree(t); } -int pa__init(pa_module*m) { +int pa__init(pa_module *m) { pa_card_new_data data; pa_modargs *ma; int alsa_card_index; struct userdata *u; + char rname[32]; + pa_reserve_wrapper *reserve = NULL; + const char *description; pa_alsa_redirect_errors_inc(); snd_config_update_free_global(); @@ -282,6 +308,11 @@ int pa__init(pa_module*m) { goto fail; } + pa_snprintf(rname, sizeof(rname), "Audio%i", alsa_card_index); + + if (!(reserve = pa_reserve_wrapper_get(m->core, rname))) + goto fail; + pa_card_new_data_init(&data); data.driver = __FILE__; data.module = m; @@ -289,8 +320,12 @@ int pa__init(pa_module*m) { pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id); set_card_name(&data, ma, u->device_id); - data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); - if (pa_alsa_probe_profiles(u->device_id, &m->core->default_sample_spec, enumerate_cb, data.profiles) < 0) { + if (reserve) + if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION))) + pa_reserve_wrapper_set_application_device_name(reserve, description); + + u->profiles = data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); + if (pa_alsa_probe_profiles(u->device_id, &m->core->default_sample_spec, enumerate_cb, u) < 0) { pa_card_new_data_done(&data); goto fail; } @@ -314,11 +349,16 @@ int pa__init(pa_module*m) { init_profile(u); + pa_reserve_wrapper_unref(reserve); + return 0; fail: + if (reserve) + pa_reserve_wrapper_unref(reserve); pa__done(m); + return -1; } diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index ac8344f0..b2fb1db1 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -653,7 +653,8 @@ static int set_conf(struct userdata *u) { return 0; } -static int setup_stream_fd(struct userdata *u) { +/* from IO thread */ +static int start_stream_fd(struct userdata *u) { union { bt_audio_msg_header_t rsp; struct bt_start_stream_req start_req; @@ -662,8 +663,11 @@ static int setup_stream_fd(struct userdata *u) { bt_audio_error_t error; uint8_t buf[BT_SUGGESTED_BUFFER_SIZE]; } msg; + struct pollfd *pollfd; pa_assert(u); + pa_assert(u->rtpoll); + pa_assert(!u->rtpoll_item); pa_assert(u->stream_fd < 0); memset(msg.buf, 0, BT_SUGGESTED_BUFFER_SIZE); @@ -691,11 +695,53 @@ static int setup_stream_fd(struct userdata *u) { pa_make_fd_nonblock(u->stream_fd); pa_make_socket_low_delay(u->stream_fd); + u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); + pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); + pollfd->fd = u->stream_fd; + pollfd->events = pollfd->revents = 0; + return 0; } +/* from IO thread */ +static int stop_stream_fd(struct userdata *u) { + union { + bt_audio_msg_header_t rsp; + struct bt_stop_stream_req start_req; + struct bt_stop_stream_rsp start_rsp; + bt_audio_error_t error; + uint8_t buf[BT_SUGGESTED_BUFFER_SIZE]; + } msg; + int r = 0; + + pa_assert(u); + pa_assert(u->rtpoll); + pa_assert(u->rtpoll_item); + pa_assert(u->stream_fd >= 0); + + pa_rtpoll_item_free(u->rtpoll_item); + u->rtpoll_item = NULL; + + memset(msg.buf, 0, BT_SUGGESTED_BUFFER_SIZE); + msg.start_req.h.type = BT_REQUEST; + msg.start_req.h.name = BT_STOP_STREAM; + msg.start_req.h.length = sizeof(msg.start_req); + + if (service_send(u, &msg.start_req.h) < 0 || + service_expect(u, &msg.rsp, sizeof(msg), BT_STOP_STREAM, sizeof(msg.start_rsp)) < 0) + r = -1; + + pa_close(u->stream_fd); + u->stream_fd = -1; + + return r; +} + static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; + pa_bool_t failed = FALSE; + int r; + pa_assert(u->sink == PA_SINK(o)); pa_log_debug("got message: %d", code); @@ -707,13 +753,27 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse case PA_SINK_SUSPENDED: pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state)); + + /* Stop the device if the source is suspended as well */ + if (!u->source || u->source->state == PA_SOURCE_SUSPENDED) + /* We deliberately ignore whether stopping + * actually worked. Since the stream_fd is + * closed it doesn't really matter */ + stop_stream_fd(u); + break; case PA_SINK_IDLE: case PA_SINK_RUNNING: - if (!PA_SINK_IS_OPENED(u->sink->thread_info.state)) - u->started_at = pa_rtclock_usec(); + if (u->sink->thread_info.state != PA_SINK_SUSPENDED) + break; + + /* Resume the device if the source was suspended as well */ + if (!u->source || u->source->state == PA_SOURCE_SUSPENDED) + if (start_stream_fd(u) < 0) + failed = TRUE; + u->started_at = pa_rtclock_usec(); break; case PA_SINK_UNLINKED: @@ -727,14 +787,17 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse *((pa_usec_t*) data) = 0; return 0; } - } - return pa_sink_process_msg(o, code, data, offset, chunk); + r = pa_sink_process_msg(o, code, data, offset, chunk); + + return (r < 0 || !failed) ? r : -1; } static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SOURCE(o)->userdata; + pa_bool_t failed = FALSE; + int r; pa_assert(u->source == PA_SOURCE(o)); @@ -746,13 +809,26 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) { case PA_SOURCE_SUSPENDED: + pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state)); + + /* Stop the device if the sink is suspended as well */ + if (!u->sink || u->sink->state == PA_SINK_SUSPENDED) + stop_stream_fd(u); + pa_smoother_pause(u->read_smoother, pa_rtclock_usec()); break; case PA_SOURCE_IDLE: case PA_SOURCE_RUNNING: - if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) - pa_smoother_resume(u->read_smoother, pa_rtclock_usec()); + if (u->source->thread_info.state != PA_SOURCE_SUSPENDED) + break; + + /* Resume the device if the sink was suspended as well */ + if (!u->sink || u->sink->thread_info.state == PA_SINK_SUSPENDED) + if (start_stream_fd(u) < 0) + failed = TRUE; + + pa_smoother_resume(u->read_smoother, pa_rtclock_usec()); break; case PA_SOURCE_UNLINKED: @@ -769,7 +845,9 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off } - return pa_source_process_msg(o, code, data, offset, chunk); + r = pa_source_process_msg(o, code, data, offset, chunk); + + return (r < 0 || !failed) ? r : -1; } static int hsp_process_render(struct userdata *u) { @@ -994,6 +1072,9 @@ static void thread_func(void *userdata) { if (u->core->realtime_scheduling) pa_make_realtime(u->core->realtime_priority); + if (start_stream_fd(u) < 0) + goto fail; + pa_thread_mq_install(&u->thread_mq); pa_rtpoll_install(u->rtpoll); @@ -1002,12 +1083,13 @@ static void thread_func(void *userdata) { for (;;) { struct pollfd *pollfd; int ret; + pa_bool_t disable_timer = TRUE; - pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); + pollfd = u->rtpoll_item ? pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL) : NULL; if (u->source && PA_SOURCE_IS_LINKED(u->source->thread_info.state)) { - if (pollfd->revents & POLLIN) { + if (pollfd && (pollfd->revents & POLLIN)) { if (hsp_process_push(u) < 0) goto fail; @@ -1022,57 +1104,63 @@ static void thread_func(void *userdata) { if (u->sink->thread_info.rewind_requested) pa_sink_process_rewind(u->sink, 0); - if (pollfd->revents & POLLOUT) - writable = TRUE; + if (pollfd) { + if (pollfd->revents & POLLOUT) + writable = TRUE; - if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write && writable) { - pa_usec_t time_passed; - uint64_t should_have_written; + if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write && writable) { + pa_usec_t time_passed; + uint64_t should_have_written; - /* Hmm, there is no input stream we could synchronize - * to. So let's do things by time */ + /* Hmm, there is no input stream we could synchronize + * to. So let's do things by time */ - time_passed = pa_rtclock_usec() - u->started_at; - should_have_written = pa_usec_to_bytes(time_passed, &u->sink->sample_spec); + time_passed = pa_rtclock_usec() - u->started_at; + should_have_written = pa_usec_to_bytes(time_passed, &u->sink->sample_spec); - do_write = u->write_index <= should_have_written ; + do_write = u->write_index <= should_have_written ; /* pa_log_debug("Time has come: %s", pa_yes_no(do_write)); */ - } + } - if (writable && do_write) { + if (writable && do_write) { - if (u->profile == PROFILE_A2DP) { - if (a2dp_process_render(u) < 0) - goto fail; - } else { - if (hsp_process_render(u) < 0) - goto fail; - } + if (u->profile == PROFILE_A2DP) { + if (a2dp_process_render(u) < 0) + goto fail; + } else { + if (hsp_process_render(u) < 0) + goto fail; + } - do_write = FALSE; - writable = FALSE; - } + do_write = FALSE; + writable = FALSE; + } - if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write) { - pa_usec_t time_passed, next_write_at, sleep_for; + if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write) { + pa_usec_t time_passed, next_write_at, sleep_for; - /* Hmm, there is no input stream we could synchronize - * to. So let's estimate when we need to wake up the latest */ + /* Hmm, there is no input stream we could synchronize + * to. So let's estimate when we need to wake up the latest */ - time_passed = pa_rtclock_usec() - u->started_at; - next_write_at = pa_bytes_to_usec(u->write_index, &u->sink->sample_spec); - sleep_for = time_passed < next_write_at ? next_write_at - time_passed : 0; + time_passed = pa_rtclock_usec() - u->started_at; + next_write_at = pa_bytes_to_usec(u->write_index, &u->sink->sample_spec); + sleep_for = time_passed < next_write_at ? next_write_at - time_passed : 0; /* pa_log("Sleeping for %lu; time passed %lu, next write at %lu", (unsigned long) sleep_for, (unsigned long) time_passed, (unsigned long)next_write_at); */ - pa_rtpoll_set_timer_relative(u->rtpoll, sleep_for); + pa_rtpoll_set_timer_relative(u->rtpoll, sleep_for); + disable_timer = FALSE; + } } - } else + } + + if (disable_timer) pa_rtpoll_set_timer_disabled(u->rtpoll); /* Hmm, nothing to do. Let's sleep */ - pollfd->events = (short) (((u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state) && !writable) ? POLLOUT : 0) | - (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state) ? POLLIN : 0)); + if (pollfd) + pollfd->events = (short) (((u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state) && !writable) ? POLLOUT : 0) | + (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state) ? POLLIN : 0)); if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) goto fail; @@ -1080,9 +1168,9 @@ static void thread_func(void *userdata) { if (ret == 0) goto finish; - pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); + pollfd = u->rtpoll_item ? pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL) : NULL; - if (pollfd->revents & ~(POLLOUT|POLLIN)) { + if (pollfd && (pollfd->revents & ~(POLLOUT|POLLIN))) { pa_log_error("FD error."); goto fail; } @@ -1463,9 +1551,6 @@ static int setup_bt(struct userdata *u) { return 0; } - if (setup_stream_fd(u) < 0) - return -1; - pa_log_debug("Got the stream socket"); return 0; @@ -1533,8 +1618,6 @@ static void stop_thread(struct userdata *u) { } static int start_thread(struct userdata *u) { - struct pollfd *pollfd; - pa_assert(u); pa_assert(!u->thread); pa_assert(!u->rtpoll); @@ -1549,11 +1632,6 @@ static int start_thread(struct userdata *u) { u->rtpoll = pa_rtpoll_new(); pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll); - u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); - pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); - pollfd->fd = u->stream_fd; - pollfd->events = pollfd->revents = 0; - if (!(u->thread = pa_thread_new(thread_func, u))) { pa_log_error("Failed to create IO thread"); stop_thread(u); diff --git a/src/modules/module-augment-properties.c b/src/modules/module-augment-properties.c index 90bfbe7d..99111868 100644 --- a/src/modules/module-augment-properties.c +++ b/src/modules/module-augment-properties.c @@ -195,6 +195,8 @@ static pa_hook_result_t process(struct userdata *u, pa_proplist *p) { time(&now); + pa_log_debug("Looking for .desktop file for %s", pn); + if ((r = pa_hashmap_get(u->cache, pn))) { if (now-r->timestamp > STAT_INTERVAL) { r->timestamp = now; diff --git a/src/modules/module-combine.c b/src/modules/module-combine.c index 7f1ef24c..6ed4f141 100644 --- a/src/modules/module-combine.c +++ b/src/modules/module-combine.c @@ -1052,8 +1052,9 @@ int pa__init(pa_module*m) { slaves = pa_modargs_get_value(ma, "slaves", NULL); u->automatic = !slaves; - ss = m->core->default_sample_spec; + ss = m->core->default_sample_spec; + map = m->core->default_channel_map; if ((pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0)) { pa_log("Invalid sample specification."); goto fail; diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index b448e84e..1739f46a 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -329,12 +329,18 @@ int pa__init(pa_module*m) { if (!channels) channels = m->core->default_sample_spec.channels; - if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { + if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || + channels <= 0 || + channels > PA_CHANNELS_MAX) { pa_log("Failed to parse channels= argument."); goto fail; } - pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA); + if (channels == m->core->default_channel_map.channels) + map = m->core->default_channel_map; + else + pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA); + if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) { pa_log("Failed to parse channel_map= argument."); goto fail; diff --git a/src/modules/module-jack-source.c b/src/modules/module-jack-source.c index 0c7ee535..38b63751 100644 --- a/src/modules/module-jack-source.c +++ b/src/modules/module-jack-source.c @@ -296,12 +296,18 @@ int pa__init(pa_module*m) { if (!channels) channels = m->core->default_sample_spec.channels; - if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { + if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || + channels <= 0 || + channels >= PA_CHANNELS_MAX) { pa_log("failed to parse channels= argument."); goto fail; } - pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA); + if (channels == m->core->default_channel_map.channels) + map = m->core->default_channel_map; + else + pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA); + if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) { pa_log("failed to parse channel_map= argument."); goto fail; diff --git a/src/modules/module-null-sink.c b/src/modules/module-null-sink.c index 570f8be4..e18da5fd 100644 --- a/src/modules/module-null-sink.c +++ b/src/modules/module-null-sink.c @@ -262,6 +262,7 @@ int pa__init(pa_module*m) { } ss = m->core->default_sample_spec; + map = m->core->default_channel_map; if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) { pa_log("Invalid sample format specification or channel map"); goto fail; diff --git a/src/modules/module-pipe-sink.c b/src/modules/module-pipe-sink.c index 7dd44098..f3b0e8b0 100644 --- a/src/modules/module-pipe-sink.c +++ b/src/modules/module-pipe-sink.c @@ -234,6 +234,7 @@ int pa__init(pa_module*m) { } ss = m->core->default_sample_spec; + map = m->core->default_channel_map; if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) { pa_log("Invalid sample format specification or channel map"); goto fail; diff --git a/src/modules/module-pipe-source.c b/src/modules/module-pipe-source.c index 975090c2..a42c53c3 100644 --- a/src/modules/module-pipe-source.c +++ b/src/modules/module-pipe-source.c @@ -221,6 +221,7 @@ int pa__init(pa_module*m) { } ss = m->core->default_sample_spec; + map = m->core->default_channel_map; if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) { pa_log("invalid sample format specification or channel map"); goto fail; diff --git a/src/modules/module-raop-discover.c b/src/modules/module-raop-discover.c index 3706d921..df393151 100644 --- a/src/modules/module-raop-discover.c +++ b/src/modules/module-raop-discover.c @@ -53,7 +53,7 @@ #include "module-raop-discover-symdef.h" PA_MODULE_AUTHOR("Colin Guthrie"); -PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Discovery of Airtunes"); +PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Discovery of RAOP devices"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(TRUE); @@ -172,9 +172,9 @@ static void resolver_cb( } if (device) - dname = pa_sprintf_malloc("airtunes.%s.%s", host_name, device); + dname = pa_sprintf_malloc("raop.%s.%s", host_name, device); else - dname = pa_sprintf_malloc("airtunes.%s", host_name); + dname = pa_sprintf_malloc("raop.%s", host_name); if (!(vname = pa_namereg_make_valid_name(dname))) { pa_log("Cannot construct valid device name from '%s'.", dname); diff --git a/src/modules/module-raop-sink.c b/src/modules/module-raop-sink.c index 1784b2cc..da338f5d 100644 --- a/src/modules/module-raop-sink.c +++ b/src/modules/module-raop-sink.c @@ -67,7 +67,7 @@ #include "raop_client.h" PA_MODULE_AUTHOR("Colin Guthrie"); -PA_MODULE_DESCRIPTION("RAOP Sink (Apple Airtunes)"); +PA_MODULE_DESCRIPTION("RAOP Sink"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( @@ -77,7 +77,7 @@ PA_MODULE_USAGE( "channels=<number of channels> " "rate=<sample rate>"); -#define DEFAULT_SINK_NAME "airtunes" +#define DEFAULT_SINK_NAME "raop" struct userdata { pa_core *core; @@ -564,7 +564,7 @@ int pa__init(pa_module*m) { pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)); pa_sink_new_data_set_sample_spec(&data, &ss); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server); - pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Airtunes sink '%s'", server); + pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "RAOP sink '%s'", server); u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK); pa_sink_new_data_done(&data); diff --git a/src/modules/module-tunnel.c b/src/modules/module-tunnel.c index 26da2575..63ae740a 100644 --- a/src/modules/module-tunnel.c +++ b/src/modules/module-tunnel.c @@ -1717,6 +1717,7 @@ int pa__init(pa_module*m) { } ss = m->core->default_sample_spec; + map = m->core->default_channel_map; if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) { pa_log("Invalid sample format specification"); goto fail; diff --git a/src/modules/module-zeroconf-discover.c b/src/modules/module-zeroconf-discover.c index 9a867cb5..5123ead8 100644 --- a/src/modules/module-zeroconf-discover.c +++ b/src/modules/module-zeroconf-discover.c @@ -162,7 +162,7 @@ static void resolver_cb( pa_module *m; ss = u->core->default_sample_spec; - pa_channel_map_init_extend(&cm, ss.channels, PA_CHANNEL_MAP_DEFAULT); + cm = u->core->default_channel_map; for (l = txt; l; l = l->next) { char *key, *value; diff --git a/src/modules/oss/module-oss.c b/src/modules/oss/module-oss.c index eac0c8e6..54d1679f 100644 --- a/src/modules/oss/module-oss.c +++ b/src/modules/oss/module-oss.c @@ -1185,6 +1185,7 @@ int pa__init(pa_module*m) { mode = (playback && record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0)); ss = m->core->default_sample_spec; + map = m->core->default_channel_map; if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) { pa_log("Failed to parse sample specification or channel map"); goto fail; diff --git a/src/modules/reserve-wrap.c b/src/modules/reserve-wrap.c new file mode 100644 index 00000000..709cb060 --- /dev/null +++ b/src/modules/reserve-wrap.c @@ -0,0 +1,168 @@ +/*** + This file is part of PulseAudio. + + Copyright 2009 Lennart Poettering + + PulseAudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + PulseAudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <pulse/xmalloc.h> +#include <pulse/i18n.h> + +#include <pulsecore/core-error.h> +#include <pulsecore/core-util.h> +#include <pulsecore/shared.h> + +#include <modules/dbus-util.h> + +#include "reserve.h" +#include "reserve-wrap.h" + +struct pa_reserve_wrapper { + PA_REFCNT_DECLARE; + pa_core *core; + pa_dbus_connection *connection; + pa_hook hook; + struct rd_device *device; + char *shared_name; +}; + +static void reserve_wrapper_free(pa_reserve_wrapper *r) { + pa_assert(r); + + if (r->device) + rd_release(r->device); + + pa_hook_done(&r->hook); + + if (r->connection) + pa_dbus_connection_unref(r->connection); + + if (r->shared_name) { + pa_assert_se(pa_shared_remove(r->core, r->shared_name) >= 0); + pa_xfree(r->shared_name); + } + + pa_xfree(r); +} + +static int request_cb(rd_device *d, int forced) { + pa_reserve_wrapper *r; + int k; + + pa_assert(d); + pa_assert_se(r = rd_get_userdata(d)); + pa_assert(PA_REFCNT_VALUE(r) >= 1); + + PA_REFCNT_INC(r); + + k = pa_hook_fire(&r->hook, PA_INT_TO_PTR(forced)); + pa_log_debug("Device unlock has been requested and %s.", k < 0 ? "failed" : "succeeded"); + + pa_reserve_wrapper_unref(r); + + return k < 0 ? -1 : 1; +} + +pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) { + pa_reserve_wrapper *r; + DBusError error; + int k; + char *t; + + dbus_error_init(&error); + + pa_assert(c); + pa_assert(device_name); + + t = pa_sprintf_malloc("reserve-wrapper@%s", device_name); + + if ((r = pa_shared_get(c, t))) { + pa_xfree(t); + + pa_assert(PA_REFCNT_VALUE(r) >= 1); + PA_REFCNT_INC(r); + + return r; + } + + r = pa_xnew0(pa_reserve_wrapper, 1); + PA_REFCNT_INIT(r); + r->core = c; + pa_hook_init(&r->hook, r); + r->shared_name = t; + + pa_assert_se(pa_shared_set(c, r->shared_name, r) >= 0); + + if (!(r->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) { + pa_log_error("Unable to contact D-Bus session bus: %s: %s", error.name, error.message); + goto fail; + } + + if ((k = rd_acquire( + &r->device, + pa_dbus_connection_get(r->connection), + device_name, + _("PulseAudio Sound Server"), + 0, + request_cb, + NULL)) < 0) { + + pa_log_error("Failed to acquire reservation lock on device '%s': %s", device_name, pa_cstrerror(-k)); + goto fail; + } + + pa_log_debug("Successfully acquired reservation lock on device '%s'", device_name); + + rd_set_userdata(r->device, r); + + return r; + +fail: + dbus_error_free(&error); + + reserve_wrapper_free(r); + + return NULL; +} + +void pa_reserve_wrapper_unref(pa_reserve_wrapper *r) { + pa_assert(r); + pa_assert(PA_REFCNT_VALUE(r) >= 1); + + if (PA_REFCNT_DEC(r) > 0) + return; + + reserve_wrapper_free(r); +} + +pa_hook* pa_reserve_wrapper_hook(pa_reserve_wrapper *r) { + pa_assert(r); + pa_assert(PA_REFCNT_VALUE(r) >= 1); + + return &r->hook; +} + +void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const char *name) { + pa_assert(r); + pa_assert(PA_REFCNT_VALUE(r) >= 1); + + rd_set_application_device_name(r->device, name); +} diff --git a/src/modules/reserve-wrap.h b/src/modules/reserve-wrap.h new file mode 100644 index 00000000..4625fe68 --- /dev/null +++ b/src/modules/reserve-wrap.h @@ -0,0 +1,38 @@ +#ifndef fooreservewraphfoo +#define fooreservewraphfoo + +/*** + This file is part of PulseAudio. + + Copyright 2009 Lennart Poettering + + PulseAudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + PulseAudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include <pulsecore/core.h> +#include <pulsecore/hook-list.h> + +typedef struct pa_reserve_wrapper pa_reserve_wrapper; + +pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name); + +void pa_reserve_wrapper_unref(pa_reserve_wrapper *r); + +pa_hook* pa_reserve_wrapper_hook(pa_reserve_wrapper *r); + +void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const char *name); + +#endif diff --git a/src/modules/reserve.c b/src/modules/reserve.c new file mode 100644 index 00000000..9a9591d2 --- /dev/null +++ b/src/modules/reserve.c @@ -0,0 +1,635 @@ +/*** + Copyright 2009 Lennart Poettering + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +***/ + +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <assert.h> + +#include "reserve.h" + +struct rd_device { + int ref; + + char *device_name; + char *application_name; + char *application_device_name; + char *service_name; + char *object_path; + int32_t priority; + + DBusConnection *connection; + + int owning:1; + int registered:1; + int filtering:1; + int gave_up:1; + + rd_request_cb_t request_cb; + void *userdata; +}; + + +#define SERVICE_PREFIX "org.freedesktop.ReserveDevice1." +#define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/" + +static const char introspection[] = + DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE + "<node>" + " <!-- If you are looking for documentation make sure to check out\n" + " http://git.0pointer.de/?p=reserve.git;a=blob;f=reserve.txt -->\n" + " <interface name=\"org.freedesktop.ReserveDevice1\">" + " <method name=\"RequestRelease\">" + " <arg name=\"priority\" type=\"i\" direction=\"in\"/>" + " <arg name=\"result\" type=\"b\" direction=\"out\"/>" + " </method>" + " <property name=\"Priority\" type=\"i\" access=\"read\"/>" + " <property name=\"ApplicationName\" type=\"s\" access=\"read\"/>" + " <property name=\"ApplicationDeviceName\" type=\"s\" access=\"read\"/>" + " </interface>" + " <interface name=\"org.freedesktop.DBus.Properties\">" + " <method name=\"Get\">" + " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" + " <arg name=\"property\" direction=\"in\" type=\"s\"/>" + " <arg name=\"value\" direction=\"out\" type=\"v\"/>" + " </method>" + " </interface>" + " <interface name=\"org.freedesktop.DBus.Introspectable\">" + " <method name=\"Introspect\">" + " <arg name=\"data\" type=\"s\" direction=\"out\"/>" + " </method>" + " </interface>" + "</node>"; + +static dbus_bool_t add_variant( + DBusMessage *m, + int type, + const void *data) { + + DBusMessageIter iter, sub; + char t[2]; + + t[0] = (char) type; + t[1] = 0; + + dbus_message_iter_init_append(m, &iter); + + if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, t, &sub)) + return FALSE; + + if (!dbus_message_iter_append_basic(&sub, type, data)) + return FALSE; + + if (!dbus_message_iter_close_container(&iter, &sub)) + return FALSE; + + return TRUE; +} + +static DBusHandlerResult object_handler( + DBusConnection *c, + DBusMessage *m, + void *userdata) { + + rd_device *d; + DBusError error; + DBusMessage *reply = NULL; + + dbus_error_init(&error); + + d = userdata; + assert(d->ref >= 1); + + if (dbus_message_is_method_call( + m, + "org.freedesktop.ReserveDevice1", + "RequestRelease")) { + + int32_t priority; + dbus_bool_t ret; + + if (!dbus_message_get_args( + m, + &error, + DBUS_TYPE_INT32, &priority, + DBUS_TYPE_INVALID)) + goto invalid; + + ret = FALSE; + + if (priority > d->priority && d->request_cb) { + d->ref++; + + if (d->request_cb(d, 0) > 0) { + ret = TRUE; + d->gave_up = 1; + } + + rd_release(d); + } + + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!dbus_message_append_args( + reply, + DBUS_TYPE_BOOLEAN, &ret, + DBUS_TYPE_INVALID)) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; + + } else if (dbus_message_is_method_call( + m, + "org.freedesktop.DBus.Properties", + "Get")) { + + const char *interface, *property; + + if (!dbus_message_get_args( + m, + &error, + DBUS_TYPE_STRING, &interface, + DBUS_TYPE_STRING, &property, + DBUS_TYPE_INVALID)) + goto invalid; + + if (strcmp(interface, "org.freedesktop.ReserveDevice1") == 0) { + const char *empty = ""; + + if (strcmp(property, "ApplicationName") == 0 && d->application_name) { + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!add_variant( + reply, + DBUS_TYPE_STRING, + d->application_name ? (const char**) &d->application_name : &empty)) + goto oom; + + } else if (strcmp(property, "ApplicationDeviceName") == 0) { + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!add_variant( + reply, + DBUS_TYPE_STRING, + d->application_device_name ? (const char**) &d->application_device_name : &empty)) + goto oom; + + } else if (strcmp(property, "Priority") == 0) { + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!add_variant( + reply, + DBUS_TYPE_INT32, + &d->priority)) + goto oom; + } else { + if (!(reply = dbus_message_new_error_printf( + m, + DBUS_ERROR_UNKNOWN_METHOD, + "Unknown property %s", + property))) + goto oom; + } + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + } else if (dbus_message_is_method_call( + m, + "org.freedesktop.DBus.Introspectable", + "Introspect")) { + const char *i = introspection; + + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!dbus_message_append_args( + reply, + DBUS_TYPE_STRING, + &i, + DBUS_TYPE_INVALID)) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + +invalid: + if (reply) + dbus_message_unref(reply); + + if (!(reply = dbus_message_new_error( + m, + DBUS_ERROR_INVALID_ARGS, + "Invalid arguments"))) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_HANDLED; + +oom: + if (reply) + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_NEED_MEMORY; +} + +static DBusHandlerResult filter_handler( + DBusConnection *c, + DBusMessage *m, + void *userdata) { + + DBusMessage *reply; + rd_device *d; + DBusError error; + + dbus_error_init(&error); + + d = userdata; + + if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) { + const char *name; + + if (!dbus_message_get_args( + m, + &error, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_INVALID)) + goto invalid; + + if (strcmp(name, d->service_name) == 0 && d->owning) { + d->owning = 0; + + if (!d->gave_up) { + d->ref++; + + if (d->request_cb) + d->request_cb(d, 1); + d->gave_up = 1; + + rd_release(d); + } + + return DBUS_HANDLER_RESULT_HANDLED; + } + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + +invalid: + if (!(reply = dbus_message_new_error( + m, + DBUS_ERROR_INVALID_ARGS, + "Invalid arguments"))) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_HANDLED; + +oom: + if (reply) + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_NEED_MEMORY; +} + + +static const struct DBusObjectPathVTable vtable ={ + .message_function = object_handler +}; + +int rd_acquire( + rd_device **_d, + DBusConnection *connection, + const char *device_name, + const char *application_name, + int32_t priority, + rd_request_cb_t request_cb, + DBusError *error) { + + rd_device *d = NULL; + int r, k; + DBusError _error; + DBusMessage *m = NULL, *reply = NULL; + dbus_bool_t good; + + if (!error) + error = &_error; + + dbus_error_init(error); + + if (!_d) + return -EINVAL; + + if (!connection) + return -EINVAL; + + if (!device_name) + return -EINVAL; + + if (!request_cb && priority != INT32_MAX) + return -EINVAL; + + if (!(d = calloc(sizeof(rd_device), 1))) + return -ENOMEM; + + d->ref = 1; + + if (!(d->device_name = strdup(device_name))) { + r = -ENOMEM; + goto fail; + } + + if (!(d->application_name = strdup(application_name))) { + r = -ENOMEM; + goto fail; + } + + d->priority = priority; + d->connection = dbus_connection_ref(connection); + d->request_cb = request_cb; + + if (!(d->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) { + r = -ENOMEM; + goto fail; + } + sprintf(d->service_name, SERVICE_PREFIX "%s", d->device_name); + + if (!(d->object_path = malloc(sizeof(OBJECT_PREFIX) + strlen(device_name)))) { + r = -ENOMEM; + goto fail; + } + sprintf(d->object_path, OBJECT_PREFIX "%s", d->device_name); + + if ((k = dbus_bus_request_name( + d->connection, + d->service_name, + DBUS_NAME_FLAG_DO_NOT_QUEUE| + (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0), + error)) < 0) { + r = -EIO; + goto fail; + } + + if (k == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) + goto success; + + if (k != DBUS_REQUEST_NAME_REPLY_EXISTS) { + r = -EIO; + goto fail; + } + + if (priority <= INT32_MIN) { + r = -EBUSY; + goto fail; + } + + if (!(m = dbus_message_new_method_call( + d->service_name, + d->object_path, + "org.freedesktop.ReserveDevice1", + "RequestRelease"))) { + r = -ENOMEM; + goto fail; + } + + if (!dbus_message_append_args( + m, + DBUS_TYPE_INT32, &d->priority, + DBUS_TYPE_INVALID)) { + r = -ENOMEM; + goto fail; + } + + if (!(reply = dbus_connection_send_with_reply_and_block( + d->connection, + m, + 5000, /* 5s */ + error))) { + + if (dbus_error_has_name(error, DBUS_ERROR_TIMED_OUT) || + dbus_error_has_name(error, DBUS_ERROR_UNKNOWN_METHOD) || + dbus_error_has_name(error, DBUS_ERROR_NO_REPLY)) { + /* This must be treated as denied. */ + r = -EBUSY; + goto fail; + } + + r = -EIO; + goto fail; + } + + if (!dbus_message_get_args( + reply, + error, + DBUS_TYPE_BOOLEAN, &good, + DBUS_TYPE_INVALID)) { + r = -EIO; + goto fail; + } + + if (!good) { + r = -EBUSY; + goto fail; + } + + if ((k = dbus_bus_request_name( + d->connection, + d->service_name, + DBUS_NAME_FLAG_DO_NOT_QUEUE| + (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0)| + DBUS_NAME_FLAG_REPLACE_EXISTING, + error)) < 0) { + r = -EIO; + goto fail; + } + + if (k != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { + r = -EIO; + goto fail; + } + +success: + d->owning = 1; + + if (!(dbus_connection_register_object_path( + d->connection, + d->object_path, + &vtable, + d))) { + r = -ENOMEM; + goto fail; + } + + d->registered = 1; + + if (!dbus_connection_add_filter( + d->connection, + filter_handler, + d, + NULL)) { + r = -ENOMEM; + goto fail; + } + + d->filtering = 1; + + *_d = d; + return 0; + +fail: + if (m) + dbus_message_unref(m); + + if (reply) + dbus_message_unref(reply); + + if (&_error == error) + dbus_error_free(&_error); + + if (d) + rd_release(d); + + return r; +} + +void rd_release( + rd_device *d) { + + if (!d) + return; + + assert(d->ref > 0); + + if (--d->ref) + return; + + + if (d->filtering) + dbus_connection_remove_filter( + d->connection, + filter_handler, + d); + + if (d->registered) + dbus_connection_unregister_object_path( + d->connection, + d->object_path); + + if (d->owning) { + DBusError error; + dbus_error_init(&error); + + dbus_bus_release_name( + d->connection, + d->service_name, + &error); + + dbus_error_free(&error); + } + + free(d->device_name); + free(d->application_name); + free(d->application_device_name); + free(d->service_name); + free(d->object_path); + + if (d->connection) + dbus_connection_unref(d->connection); + + free(d); +} + +int rd_set_application_device_name(rd_device *d, const char *n) { + char *t; + + if (!d) + return -EINVAL; + + assert(d->ref > 0); + + if (!(t = strdup(n))) + return -ENOMEM; + + free(d->application_device_name); + d->application_device_name = t; + return 0; +} + +void rd_set_userdata(rd_device *d, void *userdata) { + + if (!d) + return; + + assert(d->ref > 0); + d->userdata = userdata; +} + +void* rd_get_userdata(rd_device *d) { + + if (!d) + return NULL; + + assert(d->ref > 0); + + return d->userdata; +} diff --git a/src/modules/reserve.h b/src/modules/reserve.h new file mode 100644 index 00000000..ceb1ad11 --- /dev/null +++ b/src/modules/reserve.h @@ -0,0 +1,68 @@ +#ifndef fooreservehfoo +#define fooreservehfoo + +/*** + Copyright 2009 Lennart Poettering + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +***/ + +#include <dbus/dbus.h> +#include <inttypes.h> + +typedef struct rd_device rd_device; + +/* Prototype for a function that is called whenever someone else wants + * your app to release the device you having locked. A return value <= + * 0 denies the request, a positive return value agrees to it. Before + * returning your application should close the device in question + * completely to make sure the new application may acceess it. */ +typedef int (*rd_request_cb_t)( + rd_device *d, + int forced); /* Non-zero if an application forcibly took the lock away without asking. If this is the case then the return value of this call is ignored. */ + +/* Try to lock the device. Returns 0 on success, a negative errno + * style return value on error. The DBus error might be set as well if + * the error was caused D-Bus. */ +int rd_acquire( + rd_device **d, /* On success a pointer to the newly allocated rd_device object will be filled in here */ + DBusConnection *connection, + const char *device_name, /* The device to lock, e.g. "Audio0" */ + const char *application_name, /* A human readable name of the application, e.g. "PulseAudio Sound Server" */ + int32_t priority, /* The priority for this application. If unsure use 0 */ + rd_request_cb_t request_cb, /* Will be called whenever someone asks that this device shall be released. May be NULL if priority is INT32_MAX */ + DBusError *error); /* If we fail due to a D-Bus related issue the error will be filled in here. May be NULL. */ + +/* Unlock (if needed) and destroy a rd_device object again */ +void rd_release(rd_device *d); + +/* Set the application device name for a rd_device object Returns 0 on + * success, a negative errno style return value on error. */ +int rd_set_application_device_name(rd_device *d, const char *name); + +/* Attach a userdata pointer to a rd_device */ +void rd_set_userdata(rd_device *d, void *userdata); + +/* Query the userdata pointer from a rd_device. Returns NULL if no + * userdata was set. */ +void* rd_get_userdata(rd_device *d); + +#endif |