]> code.delx.au - pulseaudio/blob - src/modules/alsa/module-alsa-card.c
always add 'disabled' profile
[pulseaudio] / 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 "module-alsa-card-symdef.h"
32
33 PA_MODULE_AUTHOR("Lennart Poettering");
34 PA_MODULE_DESCRIPTION("ALSA Card");
35 PA_MODULE_VERSION(PACKAGE_VERSION);
36 PA_MODULE_LOAD_ONCE(FALSE);
37 PA_MODULE_USAGE(
38 "name=<name for the sink/source> "
39 "device_id=<ALSA card index> "
40 "format=<sample format> "
41 "rate=<sample rate> "
42 "fragments=<number of fragments> "
43 "fragment_size=<fragment size> "
44 "mmap=<enable memory mapping?> "
45 "tsched=<enable system timer based scheduling mode?> "
46 "tsched_buffer_size=<buffer size when using timer based scheduling> "
47 "tsched_buffer_watermark=<lower fill watermark> "
48 "profile=<profile name>");
49
50 static const char* const valid_modargs[] = {
51 "sink_name",
52 "device",
53 "device_id",
54 "format",
55 "rate",
56 "channels",
57 "channel_map",
58 "fragments",
59 "fragment_size",
60 "mmap",
61 "tsched",
62 "tsched_buffer_size",
63 "tsched_buffer_watermark",
64 NULL
65 };
66
67 #define DEFAULT_DEVICE_ID "0"
68
69 struct userdata {
70 pa_core *core;
71 pa_module *module;
72
73 char *device_id;
74
75 pa_card *card;
76 };
77
78 struct profile_data {
79 const pa_alsa_profile_info *sink, *source;
80 };
81
82 static void enumerate_cb(
83 const pa_alsa_profile_info *sink,
84 const pa_alsa_profile_info *source,
85 void *userdata) {
86
87 pa_hashmap *profiles = (pa_hashmap *) userdata;
88 char *t, *n;
89 pa_card_profile *p;
90 struct profile_data *d;
91
92 if (sink && source) {
93 n = pa_sprintf_malloc("output-%s+input-%s", sink->name, source->name);
94 t = pa_sprintf_malloc("Output %s + Input %s", sink->description, source->description);
95 } else if (sink) {
96 n = pa_sprintf_malloc("output-%s", sink->name);
97 t = pa_sprintf_malloc("Output %s", sink->description);
98 } else {
99 pa_assert(source);
100 n = pa_sprintf_malloc("input-%s", source->name);
101 t = pa_sprintf_malloc("Input %s", source->description);
102 }
103
104 pa_log_info("Found output profile '%s'", t);
105
106 p = pa_card_profile_new(n, t, sizeof(struct profile_data));
107
108 pa_xfree(t);
109 pa_xfree(n);
110
111 p->n_sinks = !!sink;
112 p->n_sources = !!source;
113
114 if (sink)
115 p->max_sink_channels = sink->map.channels;
116 if (source)
117 p->max_source_channels = source->map.channels;
118
119 d = PA_CARD_PROFILE_DATA(p);
120
121 d->sink = sink;
122 d->source = source;
123
124 pa_hashmap_put(profiles, p->name, p);
125 }
126
127 static void add_disabled_profile(pa_hashmap *profiles) {
128 pa_card_profile *p;
129 struct profile_data *d;
130
131 p = pa_card_profile_new("off", "Off", sizeof(struct profile_data));
132
133 d = PA_CARD_PROFILE_DATA(p);
134 d->sink = d->source = NULL;
135
136 pa_hashmap_put(profiles, p->name, p);
137 }
138
139 int pa__init(pa_module*m) {
140 pa_card_new_data data;
141 pa_modargs *ma;
142 int alsa_card_index;
143 struct userdata *u;
144
145 pa_alsa_redirect_errors_inc();
146
147 pa_assert(m);
148
149 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
150 pa_log("Failed to parse module arguments");
151 goto fail;
152 }
153
154 m->userdata = u = pa_xnew0(struct userdata, 1);
155 u->core = m->core;
156 u->module = m;
157 u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
158
159 if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
160 pa_log("Card '%s' doesn't exist: %s", u->device_id, snd_strerror(alsa_card_index));
161 goto fail;
162 }
163
164 pa_card_new_data_init(&data);
165 data.driver = __FILE__;
166 data.module = m;
167 pa_alsa_init_proplist_card(data.proplist, alsa_card_index);
168 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
169 pa_card_new_data_set_name(&data, pa_modargs_get_value(ma, "name", u->device_id));
170
171 data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
172 if (pa_alsa_probe_profiles(u->device_id, &m->core->default_sample_spec, enumerate_cb, data.profiles) < 0) {
173 pa_card_new_data_done(&data);
174 goto fail;
175 }
176
177 if (pa_hashmap_isempty(data.profiles)) {
178 pa_log("Failed to find a working profile.");
179 pa_card_new_data_done(&data);
180 goto fail;
181 }
182
183 add_disabled_profile(data.profiles);
184
185 u->card = pa_card_new(m->core, &data);
186 pa_card_new_data_done(&data);
187
188 return 0;
189
190 fail:
191
192 if (ma)
193 pa_modargs_free(ma);
194
195 pa__done(m);
196 return -1;
197 }
198
199 void pa__done(pa_module*m) {
200 struct userdata *u;
201
202 pa_assert(m);
203
204 if (!(u = m->userdata))
205 goto finish;
206
207 if (u->card)
208 pa_card_free(u->card);
209
210 pa_xfree(u->device_id);
211 pa_xfree(u);
212
213 finish:
214 snd_config_update_free_global();
215 pa_alsa_redirect_errors_dec();
216 }