]> code.delx.au - pulseaudio/blob - src/modules/alsa/module-alsa-card.c
add new paramter ignore_dB= to alsa modules
[pulseaudio] / src / modules / alsa / module-alsa-card.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Lennart Poettering
5
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 of the License,
9 or (at your option) any later version.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/xmalloc.h>
27 #include <pulsecore/core-util.h>
28 #include <pulsecore/modargs.h>
29 #include <pulsecore/queue.h>
30
31 #include "alsa-util.h"
32 #include "alsa-sink.h"
33 #include "alsa-source.h"
34 #include "module-alsa-card-symdef.h"
35
36 PA_MODULE_AUTHOR("Lennart Poettering");
37 PA_MODULE_DESCRIPTION("ALSA Card");
38 PA_MODULE_VERSION(PACKAGE_VERSION);
39 PA_MODULE_LOAD_ONCE(FALSE);
40 PA_MODULE_USAGE(
41 "name=<name for the card/sink/source, to be prefixed> "
42 "card_name=<name for card> "
43 "sink_name=<name for sink> "
44 "source_name=<name for source> "
45 "device_id=<ALSA card index> "
46 "format=<sample format> "
47 "rate=<sample rate> "
48 "fragments=<number of fragments> "
49 "fragment_size=<fragment size> "
50 "mmap=<enable memory mapping?> "
51 "tsched=<enable system timer based scheduling mode?> "
52 "tsched_buffer_size=<buffer size when using timer based scheduling> "
53 "tsched_buffer_watermark=<lower fill watermark> "
54 "profile=<profile name> "
55 "ignore_dB=<ignore dB information from the device?>");
56
57 static const char* const valid_modargs[] = {
58 "name",
59 "card_name",
60 "sink_name",
61 "source_name",
62 "device_id",
63 "format",
64 "rate",
65 "fragments",
66 "fragment_size",
67 "mmap",
68 "tsched",
69 "tsched_buffer_size",
70 "tsched_buffer_watermark",
71 "profile",
72 "ignore_dB",
73 NULL
74 };
75
76 #define DEFAULT_DEVICE_ID "0"
77
78 struct userdata {
79 pa_core *core;
80 pa_module *module;
81
82 char *device_id;
83
84 pa_card *card;
85 pa_sink *sink;
86 pa_source *source;
87
88 pa_modargs *modargs;
89 };
90
91 struct profile_data {
92 const pa_alsa_profile_info *sink_profile, *source_profile;
93 };
94
95 static void enumerate_cb(
96 const pa_alsa_profile_info *sink,
97 const pa_alsa_profile_info *source,
98 void *userdata) {
99
100 pa_hashmap *profiles = (pa_hashmap *) userdata;
101 char *t, *n;
102 pa_card_profile *p;
103 struct profile_data *d;
104
105 if (sink && source) {
106 n = pa_sprintf_malloc("output-%s+input-%s", sink->name, source->name);
107 t = pa_sprintf_malloc("Output %s + Input %s", sink->description, source->description);
108 } else if (sink) {
109 n = pa_sprintf_malloc("output-%s", sink->name);
110 t = pa_sprintf_malloc("Output %s", sink->description);
111 } else {
112 pa_assert(source);
113 n = pa_sprintf_malloc("input-%s", source->name);
114 t = pa_sprintf_malloc("Input %s", source->description);
115 }
116
117 pa_log_info("Found output profile '%s'", t);
118
119 p = pa_card_profile_new(n, t, sizeof(struct profile_data));
120
121 pa_xfree(t);
122 pa_xfree(n);
123
124 p->priority = (sink ? sink->priority : 0)*100 + (source ? source->priority : 0);
125 p->n_sinks = !!sink;
126 p->n_sources = !!source;
127
128 if (sink)
129 p->max_sink_channels = sink->map.channels;
130 if (source)
131 p->max_source_channels = source->map.channels;
132
133 d = PA_CARD_PROFILE_DATA(p);
134
135 d->sink_profile = sink;
136 d->source_profile = source;
137
138 pa_hashmap_put(profiles, p->name, p);
139 }
140
141 static void add_disabled_profile(pa_hashmap *profiles) {
142 pa_card_profile *p;
143 struct profile_data *d;
144
145 p = pa_card_profile_new("off", "Off", sizeof(struct profile_data));
146
147 d = PA_CARD_PROFILE_DATA(p);
148 d->sink_profile = d->source_profile = NULL;
149
150 pa_hashmap_put(profiles, p->name, p);
151 }
152
153 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
154 struct userdata *u;
155 struct profile_data *nd, *od;
156
157 pa_assert(c);
158 pa_assert(new_profile);
159 pa_assert_se(u = c->userdata);
160
161 nd = PA_CARD_PROFILE_DATA(new_profile);
162 od = PA_CARD_PROFILE_DATA(c->active_profile);
163
164 if (od->sink_profile != nd->sink_profile) {
165 pa_queue *inputs = NULL;
166
167 if (u->sink) {
168 if (nd->sink_profile)
169 inputs = pa_sink_move_all_start(u->sink);
170
171 pa_alsa_sink_free(u->sink);
172 u->sink = NULL;
173 }
174
175 if (nd->sink_profile) {
176 u->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, nd->sink_profile);
177
178 if (inputs) {
179 if (u->sink)
180 pa_sink_move_all_finish(u->sink, inputs);
181 else
182 pa_sink_move_all_fail(inputs);
183 }
184 }
185 }
186
187 if (od->source_profile != nd->source_profile) {
188 pa_queue *outputs = NULL;
189
190 if (u->source) {
191 if (nd->source_profile)
192 outputs = pa_source_move_all_start(u->source);
193
194 pa_alsa_source_free(u->source);
195 u->source = NULL;
196 }
197
198 if (nd->source_profile) {
199 u->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, nd->source_profile);
200
201 if (outputs) {
202 if (u->source)
203 pa_source_move_all_finish(u->source, outputs);
204 else
205 pa_source_move_all_fail(outputs);
206 }
207 }
208 }
209
210 return 0;
211 }
212
213 static void init_profile(struct userdata *u) {
214 struct profile_data *d;
215
216 pa_assert(u);
217
218 d = PA_CARD_PROFILE_DATA(u->card->active_profile);
219
220 if (d->sink_profile)
221 u->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, d->sink_profile);
222
223 if (d->source_profile)
224 u->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, d->source_profile);
225 }
226
227 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
228 char *t;
229 const char *n;
230
231 pa_assert(data);
232 pa_assert(ma);
233 pa_assert(device_id);
234
235 if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
236 pa_card_new_data_set_name(data, n);
237 data->namereg_fail = TRUE;
238 return;
239 }
240
241 if ((n = pa_modargs_get_value(ma, "name", NULL)))
242 data->namereg_fail = TRUE;
243 else {
244 n = device_id;
245 data->namereg_fail = FALSE;
246 }
247
248 t = pa_sprintf_malloc("alsa_card.%s", n);
249 pa_card_new_data_set_name(data, t);
250 pa_xfree(t);
251 }
252
253 int pa__init(pa_module*m) {
254 pa_card_new_data data;
255 pa_modargs *ma;
256 int alsa_card_index;
257 struct userdata *u;
258
259 pa_alsa_redirect_errors_inc();
260 snd_config_update_free_global();
261
262 pa_assert(m);
263
264 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
265 pa_log("Failed to parse module arguments");
266 goto fail;
267 }
268
269 m->userdata = u = pa_xnew(struct userdata, 1);
270 u->core = m->core;
271 u->module = m;
272 u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
273 u->card = NULL;
274 u->sink = NULL;
275 u->source = NULL;
276 u->modargs = ma;
277
278 if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
279 pa_log("Card '%s' doesn't exist: %s", u->device_id, snd_strerror(alsa_card_index));
280 goto fail;
281 }
282
283 pa_card_new_data_init(&data);
284 data.driver = __FILE__;
285 data.module = m;
286 pa_alsa_init_proplist_card(m->core, data.proplist, alsa_card_index);
287 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
288 set_card_name(&data, ma, u->device_id);
289
290 data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
291 if (pa_alsa_probe_profiles(u->device_id, &m->core->default_sample_spec, enumerate_cb, data.profiles) < 0) {
292 pa_card_new_data_done(&data);
293 goto fail;
294 }
295
296 if (pa_hashmap_isempty(data.profiles)) {
297 pa_log("Failed to find a working profile.");
298 pa_card_new_data_done(&data);
299 goto fail;
300 }
301
302 add_disabled_profile(data.profiles);
303
304 u->card = pa_card_new(m->core, &data);
305 pa_card_new_data_done(&data);
306
307 if (!u->card)
308 goto fail;
309
310 u->card->userdata = u;
311 u->card->set_profile = card_set_profile;
312
313 init_profile(u);
314
315 return 0;
316
317 fail:
318
319 pa__done(m);
320 return -1;
321 }
322
323 int pa__get_n_used(pa_module *m) {
324 struct userdata *u;
325
326 pa_assert(m);
327 pa_assert_se(u = m->userdata);
328
329 return
330 (u->sink ? pa_sink_linked_by(u->sink) : 0) +
331 (u->source ? pa_source_linked_by(u->source) : 0);
332 }
333
334 void pa__done(pa_module*m) {
335 struct userdata *u;
336
337 pa_assert(m);
338
339 if (!(u = m->userdata))
340 goto finish;
341
342 if (u->sink)
343 pa_alsa_sink_free(u->sink);
344
345 if (u->source)
346 pa_alsa_source_free(u->source);
347
348 if (u->card)
349 pa_card_free(u->card);
350
351 if (u->modargs)
352 pa_modargs_free(u->modargs);
353
354 pa_xfree(u->device_id);
355 pa_xfree(u);
356
357 finish:
358 snd_config_update_free_global();
359 pa_alsa_redirect_errors_dec();
360 }