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