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