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