2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <pulse/xmalloc.h>
27 #include <pulse/i18n.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/queue.h>
33 #include <modules/reserve-wrap.h>
35 #include "alsa-util.h"
36 #include "alsa-sink.h"
37 #include "alsa-source.h"
38 #include "module-alsa-card-symdef.h"
40 PA_MODULE_AUTHOR("Lennart Poettering");
41 PA_MODULE_DESCRIPTION("ALSA Card");
42 PA_MODULE_VERSION(PACKAGE_VERSION);
43 PA_MODULE_LOAD_ONCE(FALSE);
45 "name=<name for the card/sink/source, to be prefixed> "
46 "card_name=<name for the card> "
47 "card_properties=<properties for the card> "
48 "sink_name=<name for the sink> "
49 "sink_properties=<properties for the sink> "
50 "source_name=<name for the source> "
51 "source_properties=<properties for the source> "
52 "device_id=<ALSA card index> "
53 "format=<sample format> "
55 "fragments=<number of fragments> "
56 "fragment_size=<fragment size> "
57 "mmap=<enable memory mapping?> "
58 "tsched=<enable system timer based scheduling mode?> "
59 "tsched_buffer_size=<buffer size when using timer based scheduling> "
60 "tsched_buffer_watermark=<lower fill watermark> "
61 "profile=<profile name> "
62 "ignore_dB=<ignore dB information from the device?>");
64 static const char* const valid_modargs[] = {
80 "tsched_buffer_watermark",
86 #define DEFAULT_DEVICE_ID "0"
100 pa_hashmap *profiles;
103 struct profile_data {
104 const pa_alsa_profile_info *sink_profile, *source_profile;
107 static void enumerate_cb(
108 const pa_alsa_profile_info *sink,
109 const pa_alsa_profile_info *source,
112 struct userdata *u = userdata;
115 struct profile_data *d;
118 if (sink && source) {
119 n = pa_sprintf_malloc("output-%s+input-%s", sink->name, source->name);
120 t = pa_sprintf_malloc(_("Output %s + Input %s"), sink->description, _(source->description));
122 n = pa_sprintf_malloc("output-%s", sink->name);
123 t = pa_sprintf_malloc(_("Output %s"), _(sink->description));
126 n = pa_sprintf_malloc("input-%s", source->name);
127 t = pa_sprintf_malloc(_("Input %s"), _(source->description));
131 if (pa_channel_map_equal(&sink->map, &u->core->default_channel_map))
133 else if (sink->map.channels == u->core->default_channel_map.channels)
138 if (pa_channel_map_equal(&source->map, &u->core->default_channel_map))
140 else if (source->map.channels == u->core->default_channel_map.channels)
144 pa_log_info("Found profile '%s'", t);
146 p = pa_card_profile_new(n, t, sizeof(struct profile_data));
152 (sink ? sink->priority : 0) * 100 +
153 (source ? source->priority : 0) +
157 p->n_sources = !!source;
160 p->max_sink_channels = sink->map.channels;
162 p->max_source_channels = source->map.channels;
164 d = PA_CARD_PROFILE_DATA(p);
166 d->sink_profile = sink;
167 d->source_profile = source;
169 pa_hashmap_put(u->profiles, p->name, p);
172 static void add_disabled_profile(pa_hashmap *profiles) {
174 struct profile_data *d;
176 p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
178 d = PA_CARD_PROFILE_DATA(p);
179 d->sink_profile = d->source_profile = NULL;
181 pa_hashmap_put(profiles, p->name, p);
184 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
186 struct profile_data *nd, *od;
189 pa_assert(new_profile);
190 pa_assert_se(u = c->userdata);
192 nd = PA_CARD_PROFILE_DATA(new_profile);
193 od = PA_CARD_PROFILE_DATA(c->active_profile);
195 if (od->sink_profile != nd->sink_profile) {
196 pa_queue *inputs = NULL;
199 if (nd->sink_profile)
200 inputs = pa_sink_move_all_start(u->sink);
202 pa_alsa_sink_free(u->sink);
206 if (nd->sink_profile) {
207 u->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, nd->sink_profile);
211 pa_sink_move_all_finish(u->sink, inputs, FALSE);
213 pa_sink_move_all_fail(inputs);
218 if (od->source_profile != nd->source_profile) {
219 pa_queue *outputs = NULL;
222 if (nd->source_profile)
223 outputs = pa_source_move_all_start(u->source);
225 pa_alsa_source_free(u->source);
229 if (nd->source_profile) {
230 u->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, nd->source_profile);
234 pa_source_move_all_finish(u->source, outputs, FALSE);
236 pa_source_move_all_fail(outputs);
244 static void init_profile(struct userdata *u) {
245 struct profile_data *d;
249 d = PA_CARD_PROFILE_DATA(u->card->active_profile);
252 u->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, d->sink_profile);
254 if (d->source_profile)
255 u->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, d->source_profile);
258 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
264 pa_assert(device_id);
266 if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
267 pa_card_new_data_set_name(data, n);
268 data->namereg_fail = TRUE;
272 if ((n = pa_modargs_get_value(ma, "name", NULL)))
273 data->namereg_fail = TRUE;
276 data->namereg_fail = FALSE;
279 t = pa_sprintf_malloc("alsa_card.%s", n);
280 pa_card_new_data_set_name(data, t);
284 int pa__init(pa_module *m) {
285 pa_card_new_data data;
290 pa_reserve_wrapper *reserve = NULL;
291 const char *description;
293 pa_alsa_redirect_errors_inc();
294 snd_config_update_free_global();
298 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
299 pa_log("Failed to parse module arguments");
303 m->userdata = u = pa_xnew(struct userdata, 1);
306 u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
312 if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
313 pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(alsa_card_index));
317 pa_snprintf(rname, sizeof(rname), "Audio%i", alsa_card_index);
319 if (!pa_in_system_mode())
320 if (!(reserve = pa_reserve_wrapper_get(m->core, rname)))
323 pa_card_new_data_init(&data);
324 data.driver = __FILE__;
326 pa_alsa_init_proplist_card(m->core, data.proplist, alsa_card_index);
327 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
328 pa_alsa_init_description(data.proplist);
329 set_card_name(&data, ma, u->device_id);
332 if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
333 pa_reserve_wrapper_set_application_device_name(reserve, description);
335 u->profiles = data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
336 if (pa_alsa_probe_profiles(u->device_id, &m->core->default_sample_spec, enumerate_cb, u) < 0) {
337 pa_card_new_data_done(&data);
341 if (pa_hashmap_isempty(data.profiles)) {
342 pa_log("Failed to find a working profile.");
343 pa_card_new_data_done(&data);
347 add_disabled_profile(data.profiles);
349 if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
350 pa_log("Invalid properties");
351 pa_card_new_data_done(&data);
355 u->card = pa_card_new(m->core, &data);
356 pa_card_new_data_done(&data);
361 u->card->userdata = u;
362 u->card->set_profile = card_set_profile;
367 pa_reserve_wrapper_unref(reserve);
373 pa_reserve_wrapper_unref(reserve);
380 int pa__get_n_used(pa_module *m) {
384 pa_assert_se(u = m->userdata);
387 (u->sink ? pa_sink_linked_by(u->sink) : 0) +
388 (u->source ? pa_source_linked_by(u->source) : 0);
391 void pa__done(pa_module*m) {
396 if (!(u = m->userdata))
400 pa_alsa_sink_free(u->sink);
403 pa_alsa_source_free(u->source);
406 pa_card_free(u->card);
409 pa_modargs_free(u->modargs);
411 pa_xfree(u->device_id);
415 snd_config_update_free_global();
416 pa_alsa_redirect_errors_dec();