mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-07 13:30:03 -05:00
implement device reservation scheme
This commit is contained in:
parent
3c73025bf5
commit
c341010304
10 changed files with 1102 additions and 2 deletions
|
|
@ -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,62 @@ 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 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;
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -601,6 +655,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);
|
||||
|
|
@ -622,6 +677,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;
|
||||
|
|
@ -677,6 +733,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;
|
||||
|
|
@ -749,6 +806,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;
|
||||
|
||||
|
|
@ -804,6 +862,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);
|
||||
|
||||
|
|
@ -1468,6 +1545,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;
|
||||
|
||||
|
|
@ -1569,6 +1651,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);
|
||||
|
|
@ -1681,6 +1764,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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,62 @@ 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 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;
|
||||
|
||||
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);
|
||||
|
|
@ -765,6 +819,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);
|
||||
|
||||
|
|
@ -1316,6 +1389,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;
|
||||
|
||||
|
|
@ -1414,6 +1492,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);
|
||||
|
|
@ -1519,6 +1598,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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1708,3 +1708,24 @@ char *pa_alsa_get_driver_name_by_pcm(snd_pcm_t *pcm) {
|
|||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -136,4 +136,6 @@ 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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
@ -273,11 +275,13 @@ 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;
|
||||
|
||||
pa_alsa_redirect_errors_inc();
|
||||
snd_config_update_free_global();
|
||||
|
|
@ -303,6 +307,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;
|
||||
|
|
@ -335,11 +344,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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue