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