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