alsa: Catch role matched streams to enable/disable modifier

In UCM basic functions, we only assign intended roles from modifier
to sink/source, but we don't have a chance to set the ucm modifiers.
Here we amend the functions so that when roled stream starts or
stops, we have the following results:
1. stream will be routed to sink/source specified in modifier by
   module-intended-roles
2. After that, modifier will be enabled or disabled.
3. when multiple streams with matched roles of modifier start, only
   the first one will enable the modifier, and when they end, the
   last one will disable the modifier.

Signed-off-by: Feng Wei <wei.feng@freescale.com>
Signed-off-by: Arun Raghavan <arun.raghavan@collabora.co.uk>
This commit is contained in:
Feng Wei 2012-07-17 18:17:21 +08:00 committed by Arun Raghavan
parent 801f286dfd
commit 3a92bda554
5 changed files with 148 additions and 34 deletions

View file

@ -904,8 +904,6 @@ static int build_pollfd(struct userdata *u) {
/* Called from IO context */ /* Called from IO context */
static int suspend(struct userdata *u) { static int suspend(struct userdata *u) {
const char *mod_name;
pa_assert(u); pa_assert(u);
pa_assert(u->pcm_handle); pa_assert(u->pcm_handle);
@ -916,13 +914,6 @@ static int suspend(struct userdata *u) {
snd_pcm_close(u->pcm_handle); snd_pcm_close(u->pcm_handle);
u->pcm_handle = NULL; u->pcm_handle = NULL;
if ((mod_name = pa_proplist_gets(u->sink->proplist, PA_ALSA_PROP_UCM_MODIFIER))) {
pa_log_info("Disable ucm modifier %s", mod_name);
if (snd_use_case_set(u->ucm_context->ucm->ucm_mgr, "_dismod", mod_name) < 0)
pa_log("Failed to disable ucm modifier %s", mod_name);
}
if (u->alsa_rtpoll_item) { if (u->alsa_rtpoll_item) {
pa_rtpoll_item_free(u->alsa_rtpoll_item); pa_rtpoll_item_free(u->alsa_rtpoll_item);
u->alsa_rtpoll_item = NULL; u->alsa_rtpoll_item = NULL;
@ -1045,20 +1036,12 @@ static int unsuspend(struct userdata *u) {
pa_bool_t b, d; pa_bool_t b, d;
snd_pcm_uframes_t period_size, buffer_size; snd_pcm_uframes_t period_size, buffer_size;
char *device_name = NULL; char *device_name = NULL;
const char *mod_name;
pa_assert(u); pa_assert(u);
pa_assert(!u->pcm_handle); pa_assert(!u->pcm_handle);
pa_log_info("Trying resume..."); pa_log_info("Trying resume...");
if ((mod_name = pa_proplist_gets(u->sink->proplist, PA_ALSA_PROP_UCM_MODIFIER))) {
pa_log_info("Enable ucm modifier %s", mod_name);
if (snd_use_case_set(u->ucm_context->ucm->ucm_mgr, "_enamod", mod_name) < 0)
pa_log("Failed to enable ucm modifier %s", mod_name);
}
if ((is_iec958(u) || is_hdmi(u)) && pa_sink_is_passthrough(u->sink)) { if ((is_iec958(u) || is_hdmi(u)) && pa_sink_is_passthrough(u->sink)) {
/* Need to open device in NONAUDIO mode */ /* Need to open device in NONAUDIO mode */
int len = strlen(u->device_name) + 8; int len = strlen(u->device_name) + 8;

View file

@ -836,8 +836,6 @@ static int build_pollfd(struct userdata *u) {
/* Called from IO context */ /* Called from IO context */
static int suspend(struct userdata *u) { static int suspend(struct userdata *u) {
const char *mod_name;
pa_assert(u); pa_assert(u);
pa_assert(u->pcm_handle); pa_assert(u->pcm_handle);
@ -847,13 +845,6 @@ static int suspend(struct userdata *u) {
snd_pcm_close(u->pcm_handle); snd_pcm_close(u->pcm_handle);
u->pcm_handle = NULL; u->pcm_handle = NULL;
if ((mod_name = pa_proplist_gets(u->source->proplist, PA_ALSA_PROP_UCM_MODIFIER))) {
pa_log_info("Disable ucm modifier %s", mod_name);
if (snd_use_case_set(u->ucm_context->ucm->ucm_mgr, "_dismod", mod_name) < 0)
pa_log("Failed to disable ucm modifier %s", mod_name);
}
if (u->alsa_rtpoll_item) { if (u->alsa_rtpoll_item) {
pa_rtpoll_item_free(u->alsa_rtpoll_item); pa_rtpoll_item_free(u->alsa_rtpoll_item);
u->alsa_rtpoll_item = NULL; u->alsa_rtpoll_item = NULL;
@ -958,20 +949,12 @@ static int unsuspend(struct userdata *u) {
int err; int err;
pa_bool_t b, d; pa_bool_t b, d;
snd_pcm_uframes_t period_size, buffer_size; snd_pcm_uframes_t period_size, buffer_size;
const char *mod_name;
pa_assert(u); pa_assert(u);
pa_assert(!u->pcm_handle); pa_assert(!u->pcm_handle);
pa_log_info("Trying resume..."); pa_log_info("Trying resume...");
if ((mod_name = pa_proplist_gets(u->source->proplist, PA_ALSA_PROP_UCM_MODIFIER))) {
pa_log_info("Enable ucm modifier %s", mod_name);
if (snd_use_case_set(u->ucm_context->ucm->ucm_mgr, "_enamod", mod_name) < 0)
pa_log("Failed to enable ucm modifier %s", mod_name);
}
if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_CAPTURE, if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_CAPTURE,
SND_PCM_NONBLOCK| SND_PCM_NONBLOCK|
SND_PCM_NO_AUTO_RESAMPLE| SND_PCM_NO_AUTO_RESAMPLE|

View file

@ -1561,3 +1561,52 @@ void pa_alsa_ucm_mapping_context_free(pa_alsa_ucm_mapping_context *context) {
pa_idxset_free(context->ucm_modifiers, NULL, NULL); pa_idxset_free(context->ucm_modifiers, NULL, NULL);
} }
} }
/* Enable the modifier when the first stream with matched role starts */
void pa_alsa_ucm_roled_stream_begin(pa_alsa_ucm_config *ucm, const char *role, pa_direction_t dir) {
pa_alsa_ucm_modifier *mod;
if (!ucm->active_verb)
return;
PA_LLIST_FOREACH(mod, ucm->active_verb->modifiers) {
if ((mod->action_direction == dir) && (pa_streq(mod->media_role, role))) {
if (mod->enabled_counter == 0) {
const char *mod_name = pa_proplist_gets(mod->proplist, PA_ALSA_PROP_UCM_NAME);
pa_log_info("Enable ucm modifier %s", mod_name);
if (snd_use_case_set(ucm->ucm_mgr, "_enamod", mod_name) < 0) {
pa_log("Failed to enable ucm modifier %s", mod_name);
}
}
mod->enabled_counter++;
break;
}
}
}
/* Disable the modifier when the last stream with matched role ends */
void pa_alsa_ucm_roled_stream_end(pa_alsa_ucm_config *ucm, const char *role, pa_direction_t dir) {
pa_alsa_ucm_modifier *mod;
if (!ucm->active_verb)
return;
PA_LLIST_FOREACH(mod, ucm->active_verb->modifiers) {
if ((mod->action_direction == dir) && (pa_streq(mod->media_role, role))) {
mod->enabled_counter--;
if (mod->enabled_counter == 0) {
const char *mod_name = pa_proplist_gets(mod->proplist, PA_ALSA_PROP_UCM_NAME);
pa_log_info("Disable ucm modifier %s", mod_name);
if (snd_use_case_set(ucm->ucm_mgr, "_dismod", mod_name) < 0) {
pa_log("Failed to disable ucm modifier %s", mod_name);
}
}
break;
}
}
}

View file

@ -106,6 +106,9 @@ int pa_alsa_ucm_set_port(pa_alsa_ucm_mapping_context *context, pa_device_port *p
void pa_alsa_ucm_free(pa_alsa_ucm_config *ucm); void pa_alsa_ucm_free(pa_alsa_ucm_config *ucm);
void pa_alsa_ucm_mapping_context_free(pa_alsa_ucm_mapping_context *context); void pa_alsa_ucm_mapping_context_free(pa_alsa_ucm_mapping_context *context);
void pa_alsa_ucm_roled_stream_begin(pa_alsa_ucm_config *ucm, const char *role, pa_direction_t dir);
void pa_alsa_ucm_roled_stream_end(pa_alsa_ucm_config *ucm, const char *role, pa_direction_t dir);
/* UCM - Use Case Manager is available on some audio cards */ /* UCM - Use Case Manager is available on some audio cards */
struct pa_alsa_ucm_device { struct pa_alsa_ucm_device {
@ -147,6 +150,9 @@ struct pa_alsa_ucm_modifier {
/* Non-NULL if the modifier has its own PlaybackPCM/CapturePCM */ /* Non-NULL if the modifier has its own PlaybackPCM/CapturePCM */
pa_alsa_mapping *playback_mapping; pa_alsa_mapping *playback_mapping;
pa_alsa_mapping *capture_mapping; pa_alsa_mapping *capture_mapping;
/* Count how many role matched streams are running */
int enabled_counter;
}; };
struct pa_alsa_ucm_verb { struct pa_alsa_ucm_verb {

View file

@ -124,6 +124,13 @@ struct userdata {
/* ucm stuffs */ /* ucm stuffs */
pa_bool_t use_ucm; pa_bool_t use_ucm;
pa_alsa_ucm_config ucm; pa_alsa_ucm_config ucm;
/* hooks for modifier action */
pa_hook_slot
*sink_input_put_hook_slot,
*source_output_put_hook_slot,
*sink_input_unlink_hook_slot,
*source_output_unlink_hook_slot;
}; };
struct profile_data { struct profile_data {
@ -459,6 +466,66 @@ static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *de
pa_xfree(t); pa_xfree(t);
} }
static pa_hook_result_t sink_input_put_hook_callback(pa_core *c, pa_sink_input *sink_input, struct userdata *u) {
const char *role;
pa_sink *sink = sink_input->sink;
pa_assert(sink);
role = pa_proplist_gets(sink_input->proplist, PA_PROP_MEDIA_ROLE);
/* new sink input linked to sink of this card */
if (role && sink->card == u->card)
pa_alsa_ucm_roled_stream_begin(&u->ucm, role, PA_DIRECTION_OUTPUT);
return PA_HOOK_OK;
}
static pa_hook_result_t source_output_put_hook_callback(pa_core *c, pa_source_output *source_output, struct userdata *u) {
const char *role;
pa_source *source = source_output->source;
pa_assert(source);
role = pa_proplist_gets(source_output->proplist, PA_PROP_MEDIA_ROLE);
/* new source output linked to source of this card */
if (role && source->card == u->card)
pa_alsa_ucm_roled_stream_begin(&u->ucm, role, PA_DIRECTION_INPUT);
return PA_HOOK_OK;
}
static pa_hook_result_t sink_input_unlink_hook_callback(pa_core *c, pa_sink_input *sink_input, struct userdata *u) {
const char *role;
pa_sink *sink = sink_input->sink;
pa_assert(sink);
role = pa_proplist_gets(sink_input->proplist, PA_PROP_MEDIA_ROLE);
/* new sink input unlinked from sink of this card */
if (role && sink->card == u->card)
pa_alsa_ucm_roled_stream_end(&u->ucm, role, PA_DIRECTION_OUTPUT);
return PA_HOOK_OK;
}
static pa_hook_result_t source_output_unlink_hook_callback(pa_core *c, pa_source_output *source_output, struct userdata *u) {
const char *role;
pa_source *source = source_output->source;
pa_assert(source);
role = pa_proplist_gets(source_output->proplist, PA_PROP_MEDIA_ROLE);
/* new source output unlinked from source of this card */
if (role && source->card == u->card)
pa_alsa_ucm_roled_stream_end(&u->ucm, role, PA_DIRECTION_INPUT);
return PA_HOOK_OK;
}
int pa__init(pa_module *m) { int pa__init(pa_module *m) {
pa_card_new_data data; pa_card_new_data data;
pa_modargs *ma; pa_modargs *ma;
@ -515,6 +582,20 @@ int pa__init(pa_module *m) {
pa_log_info("Found UCM profiles"); pa_log_info("Found UCM profiles");
u->profile_set = pa_alsa_ucm_add_profile_set(&u->ucm, &u->core->default_channel_map); u->profile_set = pa_alsa_ucm_add_profile_set(&u->ucm, &u->core->default_channel_map);
/* hook start of sink input/source output to enable modifiers */
/* A little bit later than module-role-cork */
u->sink_input_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], PA_HOOK_LATE+10,
(pa_hook_cb_t) sink_input_put_hook_callback, u);
u->source_output_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], PA_HOOK_LATE+10,
(pa_hook_cb_t) source_output_put_hook_callback, u);
/* hook end of sink input/source output to disable modifiers */
/* A little bit later than module-role-cork */
u->sink_input_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], PA_HOOK_LATE+10,
(pa_hook_cb_t) sink_input_unlink_hook_callback, u);
u->source_output_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], PA_HOOK_LATE+10,
(pa_hook_cb_t) source_output_unlink_hook_callback, u);
} }
else { else {
u->use_ucm = FALSE; u->use_ucm = FALSE;
@ -646,6 +727,18 @@ void pa__done(pa_module*m) {
if (!(u = m->userdata)) if (!(u = m->userdata))
goto finish; goto finish;
if (u->sink_input_put_hook_slot)
pa_hook_slot_free(u->sink_input_put_hook_slot);
if (u->sink_input_unlink_hook_slot)
pa_hook_slot_free(u->sink_input_unlink_hook_slot);
if (u->source_output_put_hook_slot)
pa_hook_slot_free(u->source_output_put_hook_slot);
if (u->source_output_unlink_hook_slot)
pa_hook_slot_free(u->source_output_unlink_hook_slot);
if (u->mixer_fdl) if (u->mixer_fdl)
pa_alsa_fdlist_free(u->mixer_fdl); pa_alsa_fdlist_free(u->mixer_fdl);
if (u->mixer_handle) if (u->mixer_handle)