]> code.delx.au - pulseaudio/blob - src/modules/alsa/module-alsa-card.c
alsa: Handle the "profile" modarg in module-alsa-card
[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
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/i18n.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=<when false attempt to synthesise new names if they are already taken> "
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 "deferred_volume=<Synchronize software and hardware volume changes to avoid momentary jumps?> "
69 "profile_set=<profile set configuration file> "
70 "paths_dir=<directory containing the path configuration files> "
71 );
72
73 static const char* const valid_modargs[] = {
74 "name",
75 "card_name",
76 "card_properties",
77 "sink_name",
78 "sink_properties",
79 "source_name",
80 "source_properties",
81 "namereg_fail",
82 "device_id",
83 "format",
84 "rate",
85 "fragments",
86 "fragment_size",
87 "mmap",
88 "tsched",
89 "tsched_buffer_size",
90 "tsched_buffer_watermark",
91 "profile",
92 "ignore_dB",
93 "deferred_volume",
94 "profile_set",
95 "paths_dir",
96 NULL
97 };
98
99 #define DEFAULT_DEVICE_ID "0"
100
101 struct userdata {
102 pa_core *core;
103 pa_module *module;
104
105 char *device_id;
106
107 pa_card *card;
108
109 pa_modargs *modargs;
110
111 pa_alsa_profile_set *profile_set;
112 };
113
114 struct profile_data {
115 pa_alsa_profile *profile;
116 };
117
118 static void add_profiles(struct userdata *u, pa_hashmap *h) {
119 pa_alsa_profile *ap;
120 void *state;
121
122 pa_assert(u);
123 pa_assert(h);
124
125 PA_HASHMAP_FOREACH(ap, u->profile_set->profiles, state) {
126 struct profile_data *d;
127 pa_card_profile *cp;
128 pa_alsa_mapping *m;
129 uint32_t idx;
130
131 cp = pa_card_profile_new(ap->name, ap->description, sizeof(struct profile_data));
132 cp->priority = ap->priority;
133
134 if (ap->output_mappings) {
135 cp->n_sinks = pa_idxset_size(ap->output_mappings);
136
137 PA_IDXSET_FOREACH(m, ap->output_mappings, idx)
138 if (m->channel_map.channels > cp->max_sink_channels)
139 cp->max_sink_channels = m->channel_map.channels;
140 }
141
142 if (ap->input_mappings) {
143 cp->n_sources = pa_idxset_size(ap->input_mappings);
144
145 PA_IDXSET_FOREACH(m, ap->input_mappings, idx)
146 if (m->channel_map.channels > cp->max_source_channels)
147 cp->max_source_channels = m->channel_map.channels;
148 }
149
150 d = PA_CARD_PROFILE_DATA(cp);
151 d->profile = ap;
152
153 pa_hashmap_put(h, cp->name, cp);
154 }
155 }
156
157 static void add_disabled_profile(pa_hashmap *profiles) {
158 pa_card_profile *p;
159 struct profile_data *d;
160
161 p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
162
163 d = PA_CARD_PROFILE_DATA(p);
164 d->profile = NULL;
165
166 pa_hashmap_put(profiles, p->name, p);
167 }
168
169 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
170 struct userdata *u;
171 struct profile_data *nd, *od;
172 uint32_t idx;
173 pa_alsa_mapping *am;
174 pa_queue *sink_inputs = NULL, *source_outputs = NULL;
175
176 pa_assert(c);
177 pa_assert(new_profile);
178 pa_assert_se(u = c->userdata);
179
180 nd = PA_CARD_PROFILE_DATA(new_profile);
181 od = PA_CARD_PROFILE_DATA(c->active_profile);
182
183 if (od->profile && od->profile->output_mappings)
184 PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) {
185 if (!am->sink)
186 continue;
187
188 if (nd->profile &&
189 nd->profile->output_mappings &&
190 pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL))
191 continue;
192
193 sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs);
194 pa_alsa_sink_free(am->sink);
195 am->sink = NULL;
196 }
197
198 if (od->profile && od->profile->input_mappings)
199 PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) {
200 if (!am->source)
201 continue;
202
203 if (nd->profile &&
204 nd->profile->input_mappings &&
205 pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL))
206 continue;
207
208 source_outputs = pa_source_move_all_start(am->source, source_outputs);
209 pa_alsa_source_free(am->source);
210 am->source = NULL;
211 }
212
213 if (nd->profile && nd->profile->output_mappings)
214 PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) {
215
216 if (!am->sink)
217 am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am);
218
219 if (sink_inputs && am->sink) {
220 pa_sink_move_all_finish(am->sink, sink_inputs, FALSE);
221 sink_inputs = NULL;
222 }
223 }
224
225 if (nd->profile && nd->profile->input_mappings)
226 PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) {
227
228 if (!am->source)
229 am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am);
230
231 if (source_outputs && am->source) {
232 pa_source_move_all_finish(am->source, source_outputs, FALSE);
233 source_outputs = NULL;
234 }
235 }
236
237 if (sink_inputs)
238 pa_sink_move_all_fail(sink_inputs);
239
240 if (source_outputs)
241 pa_source_move_all_fail(source_outputs);
242
243 return 0;
244 }
245
246 static void init_profile(struct userdata *u) {
247 uint32_t idx;
248 pa_alsa_mapping *am;
249 struct profile_data *d;
250
251 pa_assert(u);
252
253 d = PA_CARD_PROFILE_DATA(u->card->active_profile);
254
255 if (d->profile && d->profile->output_mappings)
256 PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx)
257 am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am);
258
259 if (d->profile && d->profile->input_mappings)
260 PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx)
261 am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am);
262 }
263
264 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
265 char *t;
266 const char *n;
267
268 pa_assert(data);
269 pa_assert(ma);
270 pa_assert(device_id);
271
272 if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
273 pa_card_new_data_set_name(data, n);
274 data->namereg_fail = TRUE;
275 return;
276 }
277
278 if ((n = pa_modargs_get_value(ma, "name", NULL)))
279 data->namereg_fail = TRUE;
280 else {
281 n = device_id;
282 data->namereg_fail = FALSE;
283 }
284
285 t = pa_sprintf_malloc("alsa_card.%s", n);
286 pa_card_new_data_set_name(data, t);
287 pa_xfree(t);
288 }
289
290 int pa__init(pa_module *m) {
291 pa_card_new_data data;
292 pa_modargs *ma;
293 int alsa_card_index;
294 struct userdata *u;
295 pa_reserve_wrapper *reserve = NULL;
296 const char *description;
297 const char *profile = NULL;
298 char *fn = NULL;
299 pa_bool_t namereg_fail = FALSE;
300
301 pa_alsa_refcnt_inc();
302
303 pa_assert(m);
304
305 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
306 pa_log("Failed to parse module arguments");
307 goto fail;
308 }
309
310 m->userdata = u = pa_xnew0(struct userdata, 1);
311 u->core = m->core;
312 u->module = m;
313 u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
314 u->modargs = ma;
315
316 if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
317 pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(alsa_card_index));
318 goto fail;
319 }
320
321 if (!pa_in_system_mode()) {
322 char *rname;
323
324 if ((rname = pa_alsa_get_reserve_name(u->device_id))) {
325 reserve = pa_reserve_wrapper_get(m->core, rname);
326 pa_xfree(rname);
327
328 if (!reserve)
329 goto fail;
330 }
331 }
332
333 #ifdef HAVE_UDEV
334 fn = pa_udev_get_property(alsa_card_index, "PULSE_PROFILE_SET");
335 #endif
336
337 if (pa_modargs_get_value(ma, "profile_set", NULL)) {
338 pa_xfree(fn);
339 fn = pa_xstrdup(pa_modargs_get_value(ma, "profile_set", NULL));
340 }
341
342 u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map);
343 pa_xfree(fn);
344
345 if (!u->profile_set)
346 goto fail;
347
348 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);
349 pa_alsa_profile_set_dump(u->profile_set);
350
351 pa_card_new_data_init(&data);
352 data.driver = __FILE__;
353 data.module = m;
354
355 pa_alsa_init_proplist_card(m->core, data.proplist, alsa_card_index);
356
357 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
358 pa_alsa_init_description(data.proplist);
359 set_card_name(&data, ma, u->device_id);
360
361 /* We need to give pa_modargs_get_value_boolean() a pointer to a local
362 * variable instead of using &data.namereg_fail directly, because
363 * data.namereg_fail is a bitfield and taking the address of a bitfield
364 * variable is impossible. */
365 namereg_fail = data.namereg_fail;
366 if (pa_modargs_get_value_boolean(ma, "namereg_fail", &namereg_fail) < 0) {
367 pa_log("Failed to parse namereg_fail argument.");
368 pa_card_new_data_done(&data);
369 goto fail;
370 }
371 data.namereg_fail = namereg_fail;
372
373 if (reserve)
374 if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
375 pa_reserve_wrapper_set_application_device_name(reserve, description);
376
377 data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
378 add_profiles(u, data.profiles);
379
380 if (pa_hashmap_isempty(data.profiles)) {
381 pa_log("Failed to find a working profile.");
382 pa_card_new_data_done(&data);
383 goto fail;
384 }
385
386 add_disabled_profile(data.profiles);
387
388 if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
389 pa_log("Invalid properties");
390 pa_card_new_data_done(&data);
391 goto fail;
392 }
393
394 if ((profile = pa_modargs_get_value(ma, "profile", NULL)))
395 pa_card_new_data_set_profile(&data, profile);
396
397 u->card = pa_card_new(m->core, &data);
398 pa_card_new_data_done(&data);
399
400 if (!u->card)
401 goto fail;
402
403 u->card->userdata = u;
404 u->card->set_profile = card_set_profile;
405
406 init_profile(u);
407
408 if (reserve)
409 pa_reserve_wrapper_unref(reserve);
410
411 if (!pa_hashmap_isempty(u->profile_set->decibel_fixes))
412 pa_log_warn("Card %s uses decibel fixes (i.e. overrides the decibel information for some alsa volume elements). "
413 "Please note that this feature is meant just as a help for figuring out the correct decibel values. "
414 "Pulseaudio is not the correct place to maintain the decibel mappings! The fixed decibel values "
415 "should be sent to ALSA developers so that they can fix the driver. If it turns out that this feature "
416 "is abused (i.e. fixes are not pushed to ALSA), the decibel fix feature may be removed in some future "
417 "Pulseaudio version.", u->card->name);
418
419 return 0;
420
421 fail:
422 if (reserve)
423 pa_reserve_wrapper_unref(reserve);
424
425 pa__done(m);
426
427 return -1;
428 }
429
430 int pa__get_n_used(pa_module *m) {
431 struct userdata *u;
432 int n = 0;
433 uint32_t idx;
434 pa_sink *sink;
435 pa_source *source;
436
437 pa_assert(m);
438 pa_assert_se(u = m->userdata);
439 pa_assert(u->card);
440
441 PA_IDXSET_FOREACH(sink, u->card->sinks, idx)
442 n += pa_sink_linked_by(sink);
443
444 PA_IDXSET_FOREACH(source, u->card->sources, idx)
445 n += pa_source_linked_by(source);
446
447 return n;
448 }
449
450 void pa__done(pa_module*m) {
451 struct userdata *u;
452
453 pa_assert(m);
454
455 if (!(u = m->userdata))
456 goto finish;
457
458 if (u->card && u->card->sinks) {
459 pa_sink *s;
460
461 while ((s = pa_idxset_steal_first(u->card->sinks, NULL)))
462 pa_alsa_sink_free(s);
463 }
464
465 if (u->card && u->card->sources) {
466 pa_source *s;
467
468 while ((s = pa_idxset_steal_first(u->card->sources, NULL)))
469 pa_alsa_source_free(s);
470 }
471
472 if (u->card)
473 pa_card_free(u->card);
474
475 if (u->modargs)
476 pa_modargs_free(u->modargs);
477
478 if (u->profile_set)
479 pa_alsa_profile_set_free(u->profile_set);
480
481 pa_xfree(u->device_id);
482 pa_xfree(u);
483
484 finish:
485 pa_alsa_refcnt_dec();
486 }