]> code.delx.au - pulseaudio/blob - src/modules/alsa/module-alsa-card.c
module-alsa-card: New argument: namereg_fail.
[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.1 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 <pulse/i18n.h>
28
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/queue.h>
32
33 #include <modules/reserve-wrap.h>
34
35 #ifdef HAVE_UDEV
36 #include <modules/udev-util.h>
37 #endif
38
39 #include "alsa-util.h"
40 #include "alsa-sink.h"
41 #include "alsa-source.h"
42 #include "module-alsa-card-symdef.h"
43
44 PA_MODULE_AUTHOR("Lennart Poettering");
45 PA_MODULE_DESCRIPTION("ALSA Card");
46 PA_MODULE_VERSION(PACKAGE_VERSION);
47 PA_MODULE_LOAD_ONCE(FALSE);
48 PA_MODULE_USAGE(
49 "name=<name for the card/sink/source, to be prefixed> "
50 "card_name=<name for the card> "
51 "card_properties=<properties for the card> "
52 "sink_name=<name for the sink> "
53 "sink_properties=<properties for the sink> "
54 "source_name=<name for the source> "
55 "source_properties=<properties for the source> "
56 "namereg_fail=<pa_namereg_register() fail parameter value> "
57 "device_id=<ALSA card index> "
58 "format=<sample format> "
59 "rate=<sample rate> "
60 "fragments=<number of fragments> "
61 "fragment_size=<fragment size> "
62 "mmap=<enable memory mapping?> "
63 "tsched=<enable system timer based scheduling mode?> "
64 "tsched_buffer_size=<buffer size when using timer based scheduling> "
65 "tsched_buffer_watermark=<lower fill watermark> "
66 "profile=<profile name> "
67 "ignore_dB=<ignore dB information from the device?>");
68
69 static const char* const valid_modargs[] = {
70 "name",
71 "card_name",
72 "card_properties",
73 "sink_name",
74 "sink_properties",
75 "source_name",
76 "source_properties",
77 "namereg_fail",
78 "device_id",
79 "format",
80 "rate",
81 "fragments",
82 "fragment_size",
83 "mmap",
84 "tsched",
85 "tsched_buffer_size",
86 "tsched_buffer_watermark",
87 "profile",
88 "ignore_dB",
89 NULL
90 };
91
92 #define DEFAULT_DEVICE_ID "0"
93
94 struct userdata {
95 pa_core *core;
96 pa_module *module;
97
98 char *device_id;
99
100 pa_card *card;
101
102 pa_modargs *modargs;
103
104 pa_alsa_profile_set *profile_set;
105 };
106
107 struct profile_data {
108 pa_alsa_profile *profile;
109 };
110
111 static void add_profiles(struct userdata *u, pa_hashmap *h) {
112 pa_alsa_profile *ap;
113 void *state;
114
115 pa_assert(u);
116 pa_assert(h);
117
118 PA_HASHMAP_FOREACH(ap, u->profile_set->profiles, state) {
119 struct profile_data *d;
120 pa_card_profile *cp;
121 pa_alsa_mapping *m;
122 uint32_t idx;
123
124 cp = pa_card_profile_new(ap->name, ap->description, sizeof(struct profile_data));
125 cp->priority = ap->priority;
126
127 if (ap->output_mappings) {
128 cp->n_sinks = pa_idxset_size(ap->output_mappings);
129
130 PA_IDXSET_FOREACH(m, ap->output_mappings, idx)
131 if (m->channel_map.channels > cp->max_sink_channels)
132 cp->max_sink_channels = m->channel_map.channels;
133 }
134
135 if (ap->input_mappings) {
136 cp->n_sources = pa_idxset_size(ap->input_mappings);
137
138 PA_IDXSET_FOREACH(m, ap->input_mappings, idx)
139 if (m->channel_map.channels > cp->max_source_channels)
140 cp->max_source_channels = m->channel_map.channels;
141 }
142
143 d = PA_CARD_PROFILE_DATA(cp);
144 d->profile = ap;
145
146 pa_hashmap_put(h, cp->name, cp);
147 }
148 }
149
150 static void add_disabled_profile(pa_hashmap *profiles) {
151 pa_card_profile *p;
152 struct profile_data *d;
153
154 p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
155
156 d = PA_CARD_PROFILE_DATA(p);
157 d->profile = NULL;
158
159 pa_hashmap_put(profiles, p->name, p);
160 }
161
162 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
163 struct userdata *u;
164 struct profile_data *nd, *od;
165 uint32_t idx;
166 pa_alsa_mapping *am;
167 pa_queue *sink_inputs = NULL, *source_outputs = NULL;
168
169 pa_assert(c);
170 pa_assert(new_profile);
171 pa_assert_se(u = c->userdata);
172
173 nd = PA_CARD_PROFILE_DATA(new_profile);
174 od = PA_CARD_PROFILE_DATA(c->active_profile);
175
176 if (od->profile && od->profile->output_mappings)
177 PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) {
178 if (!am->sink)
179 continue;
180
181 if (nd->profile &&
182 nd->profile->output_mappings &&
183 pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL))
184 continue;
185
186 sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs);
187 pa_alsa_sink_free(am->sink);
188 am->sink = NULL;
189 }
190
191 if (od->profile && od->profile->input_mappings)
192 PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) {
193 if (!am->source)
194 continue;
195
196 if (nd->profile &&
197 nd->profile->input_mappings &&
198 pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL))
199 continue;
200
201 source_outputs = pa_source_move_all_start(am->source, source_outputs);
202 pa_alsa_source_free(am->source);
203 am->source = NULL;
204 }
205
206 if (nd->profile && nd->profile->output_mappings)
207 PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) {
208
209 if (!am->sink)
210 am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am);
211
212 if (sink_inputs && am->sink) {
213 pa_sink_move_all_finish(am->sink, sink_inputs, FALSE);
214 sink_inputs = NULL;
215 }
216 }
217
218 if (nd->profile && nd->profile->input_mappings)
219 PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) {
220
221 if (!am->source)
222 am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am);
223
224 if (source_outputs && am->source) {
225 pa_source_move_all_finish(am->source, source_outputs, FALSE);
226 source_outputs = NULL;
227 }
228 }
229
230 if (sink_inputs)
231 pa_sink_move_all_fail(sink_inputs);
232
233 if (source_outputs)
234 pa_source_move_all_fail(source_outputs);
235
236 return 0;
237 }
238
239 static void init_profile(struct userdata *u) {
240 uint32_t idx;
241 pa_alsa_mapping *am;
242 struct profile_data *d;
243
244 pa_assert(u);
245
246 d = PA_CARD_PROFILE_DATA(u->card->active_profile);
247
248 if (d->profile && d->profile->output_mappings)
249 PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx)
250 am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am);
251
252 if (d->profile && d->profile->input_mappings)
253 PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx)
254 am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am);
255 }
256
257 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
258 char *t;
259 const char *n;
260
261 pa_assert(data);
262 pa_assert(ma);
263 pa_assert(device_id);
264
265 if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
266 pa_card_new_data_set_name(data, n);
267 data->namereg_fail = TRUE;
268 return;
269 }
270
271 if ((n = pa_modargs_get_value(ma, "name", NULL)))
272 data->namereg_fail = TRUE;
273 else {
274 n = device_id;
275 data->namereg_fail = FALSE;
276 }
277
278 t = pa_sprintf_malloc("alsa_card.%s", n);
279 pa_card_new_data_set_name(data, t);
280 pa_xfree(t);
281 }
282
283 int pa__init(pa_module *m) {
284 pa_card_new_data data;
285 pa_modargs *ma;
286 int alsa_card_index;
287 struct userdata *u;
288 pa_reserve_wrapper *reserve = NULL;
289 const char *description;
290 char *fn = NULL;
291 pa_bool_t namereg_fail = FALSE;
292
293 pa_alsa_refcnt_inc();
294
295 pa_assert(m);
296
297 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
298 pa_log("Failed to parse module arguments");
299 goto fail;
300 }
301
302 m->userdata = u = pa_xnew0(struct userdata, 1);
303 u->core = m->core;
304 u->module = m;
305 u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
306 u->modargs = ma;
307
308 if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
309 pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(alsa_card_index));
310 goto fail;
311 }
312
313 if (!pa_in_system_mode()) {
314 char *rname;
315
316 if ((rname = pa_alsa_get_reserve_name(u->device_id))) {
317 reserve = pa_reserve_wrapper_get(m->core, rname);
318 pa_xfree(rname);
319
320 if (!reserve)
321 goto fail;
322 }
323 }
324
325 #ifdef HAVE_UDEV
326 fn = pa_udev_get_property(alsa_card_index, "PULSE_PROFILE_SET");
327 #endif
328
329 u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map);
330 pa_xfree(fn);
331
332 if (!u->profile_set)
333 goto fail;
334
335 pa_alsa_profile_set_probe(u->profile_set, u->device_id, &m->core->default_sample_spec, m->core->default_n_fragments, m->core->default_fragment_size_msec);
336
337 pa_card_new_data_init(&data);
338 data.driver = __FILE__;
339 data.module = m;
340
341 pa_alsa_init_proplist_card(m->core, data.proplist, alsa_card_index);
342
343 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
344 pa_alsa_init_description(data.proplist);
345 set_card_name(&data, ma, u->device_id);
346
347 /* We need to give pa_modargs_get_value_boolean() a pointer to a local
348 * variable instead of using &data.namereg_fail directly, because
349 * data.namereg_fail is a bitfield and taking the address of a bitfield
350 * variable is impossible. */
351 namereg_fail = data.namereg_fail;
352 if (pa_modargs_get_value_boolean(ma, "namereg_fail", &namereg_fail) < 0) {
353 pa_log("Failed to parse boolean argument namereg_fail.");
354 pa_card_new_data_done(&data);
355 goto fail;
356 }
357 data.namereg_fail = namereg_fail;
358
359 if (reserve)
360 if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
361 pa_reserve_wrapper_set_application_device_name(reserve, description);
362
363 data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
364 add_profiles(u, data.profiles);
365
366 if (pa_hashmap_isempty(data.profiles)) {
367 pa_log("Failed to find a working profile.");
368 pa_card_new_data_done(&data);
369 goto fail;
370 }
371
372 add_disabled_profile(data.profiles);
373
374 if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
375 pa_log("Invalid properties");
376 pa_card_new_data_done(&data);
377 goto fail;
378 }
379
380 u->card = pa_card_new(m->core, &data);
381 pa_card_new_data_done(&data);
382
383 if (!u->card)
384 goto fail;
385
386 u->card->userdata = u;
387 u->card->set_profile = card_set_profile;
388
389 init_profile(u);
390
391 if (reserve)
392 pa_reserve_wrapper_unref(reserve);
393
394 return 0;
395
396 fail:
397 if (reserve)
398 pa_reserve_wrapper_unref(reserve);
399
400 pa__done(m);
401
402 return -1;
403 }
404
405 int pa__get_n_used(pa_module *m) {
406 struct userdata *u;
407 int n = 0;
408 uint32_t idx;
409 pa_sink *sink;
410 pa_source *source;
411
412 pa_assert(m);
413 pa_assert_se(u = m->userdata);
414 pa_assert(u->card);
415
416 PA_IDXSET_FOREACH(sink, u->card->sinks, idx)
417 n += pa_sink_linked_by(sink);
418
419 PA_IDXSET_FOREACH(source, u->card->sources, idx)
420 n += pa_source_linked_by(source);
421
422 return n;
423 }
424
425 void pa__done(pa_module*m) {
426 struct userdata *u;
427
428 pa_assert(m);
429
430 if (!(u = m->userdata))
431 goto finish;
432
433 if (u->card && u->card->sinks) {
434 pa_sink *s;
435
436 while ((s = pa_idxset_steal_first(u->card->sinks, NULL)))
437 pa_alsa_sink_free(s);
438 }
439
440 if (u->card && u->card->sources) {
441 pa_source *s;
442
443 while ((s = pa_idxset_steal_first(u->card->sources, NULL)))
444 pa_alsa_source_free(s);
445 }
446
447 if (u->card)
448 pa_card_free(u->card);
449
450 if (u->modargs)
451 pa_modargs_free(u->modargs);
452
453 if (u->profile_set)
454 pa_alsa_profile_set_free(u->profile_set);
455
456 pa_xfree(u->device_id);
457 pa_xfree(u);
458
459 finish:
460 pa_alsa_refcnt_dec();
461 }