Merge branch 'master' of ssh://rootserver/home/lennart/git/public/pulseaudio
[profile/ivi/pulseaudio-panda.git] / 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
93 struct profile_data {
94     const pa_alsa_profile_info *sink_profile, *source_profile;
95 };
96
97 static void enumerate_cb(
98         const pa_alsa_profile_info *sink,
99         const pa_alsa_profile_info *source,
100         void *userdata) {
101
102     pa_hashmap *profiles = (pa_hashmap *) userdata;
103     char *t, *n;
104     pa_card_profile *p;
105     struct profile_data *d;
106
107     if (sink && source) {
108         n = pa_sprintf_malloc("output-%s+input-%s", sink->name, source->name);
109         t = pa_sprintf_malloc(_("Output %s + Input %s"), sink->description, _(source->description));
110     } else if (sink) {
111         n = pa_sprintf_malloc("output-%s", sink->name);
112         t = pa_sprintf_malloc(_("Output %s"), _(sink->description));
113     } else {
114         pa_assert(source);
115         n = pa_sprintf_malloc("input-%s", source->name);
116         t = pa_sprintf_malloc(_("Input %s"), _(source->description));
117     }
118
119     pa_log_info("Found output profile '%s'", t);
120
121     p = pa_card_profile_new(n, t, sizeof(struct profile_data));
122
123     pa_xfree(t);
124     pa_xfree(n);
125
126     p->priority = (sink ? sink->priority : 0)*100 + (source ? source->priority : 0);
127     p->n_sinks = !!sink;
128     p->n_sources = !!source;
129
130     if (sink)
131         p->max_sink_channels = sink->map.channels;
132     if (source)
133         p->max_source_channels = source->map.channels;
134
135     d = PA_CARD_PROFILE_DATA(p);
136
137     d->sink_profile = sink;
138     d->source_profile = source;
139
140     pa_hashmap_put(profiles, p->name, p);
141 }
142
143 static void add_disabled_profile(pa_hashmap *profiles) {
144     pa_card_profile *p;
145     struct profile_data *d;
146
147     p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
148
149     d = PA_CARD_PROFILE_DATA(p);
150     d->sink_profile = d->source_profile = NULL;
151
152     pa_hashmap_put(profiles, p->name, p);
153 }
154
155 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
156     struct userdata *u;
157     struct profile_data *nd, *od;
158
159     pa_assert(c);
160     pa_assert(new_profile);
161     pa_assert_se(u = c->userdata);
162
163     nd = PA_CARD_PROFILE_DATA(new_profile);
164     od = PA_CARD_PROFILE_DATA(c->active_profile);
165
166     if (od->sink_profile != nd->sink_profile) {
167         pa_queue *inputs = NULL;
168
169         if (u->sink) {
170             if (nd->sink_profile)
171                 inputs = pa_sink_move_all_start(u->sink);
172
173             pa_alsa_sink_free(u->sink);
174             u->sink = NULL;
175         }
176
177         if (nd->sink_profile) {
178             u->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, nd->sink_profile);
179
180             if (inputs) {
181                 if (u->sink)
182                     pa_sink_move_all_finish(u->sink, inputs, FALSE);
183                 else
184                     pa_sink_move_all_fail(inputs);
185             }
186         }
187     }
188
189     if (od->source_profile != nd->source_profile) {
190         pa_queue *outputs = NULL;
191
192         if (u->source) {
193             if (nd->source_profile)
194                 outputs = pa_source_move_all_start(u->source);
195
196             pa_alsa_source_free(u->source);
197             u->source = NULL;
198         }
199
200         if (nd->source_profile) {
201             u->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, nd->source_profile);
202
203             if (outputs) {
204                 if (u->source)
205                     pa_source_move_all_finish(u->source, outputs, FALSE);
206                 else
207                     pa_source_move_all_fail(outputs);
208             }
209         }
210     }
211
212     return 0;
213 }
214
215 static void init_profile(struct userdata *u) {
216     struct profile_data *d;
217
218     pa_assert(u);
219
220     d = PA_CARD_PROFILE_DATA(u->card->active_profile);
221
222     if (d->sink_profile)
223         u->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, d->sink_profile);
224
225     if (d->source_profile)
226         u->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, d->source_profile);
227 }
228
229 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
230     char *t;
231     const char *n;
232
233     pa_assert(data);
234     pa_assert(ma);
235     pa_assert(device_id);
236
237     if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
238         pa_card_new_data_set_name(data, n);
239         data->namereg_fail = TRUE;
240         return;
241     }
242
243     if ((n = pa_modargs_get_value(ma, "name", NULL)))
244         data->namereg_fail = TRUE;
245     else {
246         n = device_id;
247         data->namereg_fail = FALSE;
248     }
249
250     t = pa_sprintf_malloc("alsa_card.%s", n);
251     pa_card_new_data_set_name(data, t);
252     pa_xfree(t);
253 }
254
255 int pa__init(pa_module*m) {
256     pa_card_new_data data;
257     pa_modargs *ma;
258     int alsa_card_index;
259     struct userdata *u;
260
261     pa_alsa_redirect_errors_inc();
262     snd_config_update_free_global();
263
264     pa_assert(m);
265
266     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
267         pa_log("Failed to parse module arguments");
268         goto fail;
269     }
270
271     m->userdata = u = pa_xnew(struct userdata, 1);
272     u->core = m->core;
273     u->module = m;
274     u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
275     u->card = NULL;
276     u->sink = NULL;
277     u->source = NULL;
278     u->modargs = ma;
279
280     if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
281         pa_log("Card '%s' doesn't exist: %s", u->device_id, snd_strerror(alsa_card_index));
282         goto fail;
283     }
284
285     pa_card_new_data_init(&data);
286     data.driver = __FILE__;
287     data.module = m;
288     pa_alsa_init_proplist_card(m->core, data.proplist, alsa_card_index);
289     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
290     set_card_name(&data, ma, u->device_id);
291
292     data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
293     if (pa_alsa_probe_profiles(u->device_id, &m->core->default_sample_spec, enumerate_cb, data.profiles) < 0) {
294         pa_card_new_data_done(&data);
295         goto fail;
296     }
297
298     if (pa_hashmap_isempty(data.profiles)) {
299         pa_log("Failed to find a working profile.");
300         pa_card_new_data_done(&data);
301         goto fail;
302     }
303
304     add_disabled_profile(data.profiles);
305
306     u->card = pa_card_new(m->core, &data);
307     pa_card_new_data_done(&data);
308
309     if (!u->card)
310         goto fail;
311
312     u->card->userdata = u;
313     u->card->set_profile = card_set_profile;
314
315     init_profile(u);
316
317     return 0;
318
319 fail:
320
321     pa__done(m);
322     return -1;
323 }
324
325 int pa__get_n_used(pa_module *m) {
326     struct userdata *u;
327
328     pa_assert(m);
329     pa_assert_se(u = m->userdata);
330
331     return
332         (u->sink ? pa_sink_linked_by(u->sink) : 0) +
333         (u->source ? pa_source_linked_by(u->source) : 0);
334 }
335
336 void pa__done(pa_module*m) {
337     struct userdata *u;
338
339     pa_assert(m);
340
341     if (!(u = m->userdata))
342         goto finish;
343
344     if (u->sink)
345         pa_alsa_sink_free(u->sink);
346
347     if (u->source)
348         pa_alsa_source_free(u->source);
349
350     if (u->card)
351         pa_card_free(u->card);
352
353     if (u->modargs)
354         pa_modargs_free(u->modargs);
355
356     pa_xfree(u->device_id);
357     pa_xfree(u);
358
359 finish:
360     snd_config_update_free_global();
361     pa_alsa_redirect_errors_dec();
362 }