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