]>
Commit | Line | Data |
---|---|---|
71830221 KZ |
1 | /* |
2 | * QEMU Audio subsystem: legacy configuration handling | |
3 | * | |
4 | * Copyright (c) 2015-2019 Zoltán Kővágó <DirtY.iCE.hu@gmail.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | #include "qemu/osdep.h" | |
25 | #include "audio.h" | |
26 | #include "audio_int.h" | |
27 | #include "qemu-common.h" | |
28 | #include "qemu/cutils.h" | |
29 | #include "qapi/error.h" | |
30 | #include "qapi/qapi-visit-audio.h" | |
31 | #include "qapi/visitor-impl.h" | |
32 | ||
33 | #define AUDIO_CAP "audio-legacy" | |
34 | #include "audio_int.h" | |
35 | ||
36 | static uint32_t toui32(const char *str) | |
37 | { | |
38 | unsigned long long ret; | |
39 | if (parse_uint_full(str, &ret, 10) || ret > UINT32_MAX) { | |
40 | dolog("Invalid integer value `%s'\n", str); | |
41 | exit(1); | |
42 | } | |
43 | return ret; | |
44 | } | |
45 | ||
46 | /* helper functions to convert env variables */ | |
47 | static void get_bool(const char *env, bool *dst, bool *has_dst) | |
48 | { | |
49 | const char *val = getenv(env); | |
50 | if (val) { | |
51 | *dst = toui32(val) != 0; | |
52 | *has_dst = true; | |
53 | } | |
54 | } | |
55 | ||
56 | static void get_int(const char *env, uint32_t *dst, bool *has_dst) | |
57 | { | |
58 | const char *val = getenv(env); | |
59 | if (val) { | |
60 | *dst = toui32(val); | |
61 | *has_dst = true; | |
62 | } | |
63 | } | |
64 | ||
a93f3281 KZ |
65 | static void get_str(const char *env, char **dst, bool *has_dst) |
66 | { | |
67 | const char *val = getenv(env); | |
68 | if (val) { | |
69 | if (*has_dst) { | |
70 | g_free(*dst); | |
71 | } | |
72 | *dst = g_strdup(val); | |
73 | *has_dst = true; | |
74 | } | |
75 | } | |
76 | ||
71830221 KZ |
77 | static void get_fmt(const char *env, AudioFormat *dst, bool *has_dst) |
78 | { | |
79 | const char *val = getenv(env); | |
80 | if (val) { | |
81 | size_t i; | |
82 | for (i = 0; AudioFormat_lookup.size; ++i) { | |
83 | if (strcasecmp(val, AudioFormat_lookup.array[i]) == 0) { | |
84 | *dst = i; | |
85 | *has_dst = true; | |
86 | return; | |
87 | } | |
88 | } | |
89 | ||
90 | dolog("Invalid audio format `%s'\n", val); | |
91 | exit(1); | |
92 | } | |
93 | } | |
94 | ||
a93f3281 KZ |
95 | |
96 | static void get_millis_to_usecs(const char *env, uint32_t *dst, bool *has_dst) | |
97 | { | |
98 | const char *val = getenv(env); | |
99 | if (val) { | |
100 | *dst = toui32(val) * 1000; | |
101 | *has_dst = true; | |
102 | } | |
103 | } | |
104 | ||
105 | static uint32_t frames_to_usecs(uint32_t frames, | |
106 | AudiodevPerDirectionOptions *pdo) | |
107 | { | |
108 | uint32_t freq = pdo->has_frequency ? pdo->frequency : 44100; | |
109 | return (frames * 1000000 + freq / 2) / freq; | |
110 | } | |
111 | ||
71830221 | 112 | /* backend specific functions */ |
a93f3281 KZ |
113 | /* ALSA */ |
114 | static void handle_alsa_per_direction( | |
115 | AudiodevAlsaPerDirectionOptions *apdo, const char *prefix) | |
116 | { | |
117 | char buf[64]; | |
118 | size_t len = strlen(prefix); | |
119 | bool size_in_usecs = false; | |
120 | bool dummy; | |
121 | ||
122 | memcpy(buf, prefix, len); | |
123 | strcpy(buf + len, "TRY_POLL"); | |
124 | get_bool(buf, &apdo->try_poll, &apdo->has_try_poll); | |
125 | ||
126 | strcpy(buf + len, "DEV"); | |
127 | get_str(buf, &apdo->dev, &apdo->has_dev); | |
128 | ||
129 | strcpy(buf + len, "SIZE_IN_USEC"); | |
130 | get_bool(buf, &size_in_usecs, &dummy); | |
131 | ||
132 | strcpy(buf + len, "PERIOD_SIZE"); | |
133 | get_int(buf, &apdo->period_length, &apdo->has_period_length); | |
134 | if (apdo->has_period_length && !size_in_usecs) { | |
135 | apdo->period_length = frames_to_usecs( | |
136 | apdo->period_length, | |
137 | qapi_AudiodevAlsaPerDirectionOptions_base(apdo)); | |
138 | } | |
139 | ||
140 | strcpy(buf + len, "BUFFER_SIZE"); | |
141 | get_int(buf, &apdo->buffer_length, &apdo->has_buffer_length); | |
142 | if (apdo->has_buffer_length && !size_in_usecs) { | |
143 | apdo->buffer_length = frames_to_usecs( | |
144 | apdo->buffer_length, | |
145 | qapi_AudiodevAlsaPerDirectionOptions_base(apdo)); | |
146 | } | |
147 | } | |
148 | ||
149 | static void handle_alsa(Audiodev *dev) | |
150 | { | |
151 | AudiodevAlsaOptions *aopt = &dev->u.alsa; | |
152 | handle_alsa_per_direction(aopt->in, "QEMU_ALSA_ADC_"); | |
153 | handle_alsa_per_direction(aopt->out, "QEMU_ALSA_DAC_"); | |
154 | ||
155 | get_millis_to_usecs("QEMU_ALSA_THRESHOLD", | |
156 | &aopt->threshold, &aopt->has_threshold); | |
157 | } | |
71830221 KZ |
158 | |
159 | /* general */ | |
160 | static void handle_per_direction( | |
161 | AudiodevPerDirectionOptions *pdo, const char *prefix) | |
162 | { | |
163 | char buf[64]; | |
164 | size_t len = strlen(prefix); | |
165 | ||
166 | memcpy(buf, prefix, len); | |
167 | strcpy(buf + len, "FIXED_SETTINGS"); | |
168 | get_bool(buf, &pdo->fixed_settings, &pdo->has_fixed_settings); | |
169 | ||
170 | strcpy(buf + len, "FIXED_FREQ"); | |
171 | get_int(buf, &pdo->frequency, &pdo->has_frequency); | |
172 | ||
173 | strcpy(buf + len, "FIXED_FMT"); | |
174 | get_fmt(buf, &pdo->format, &pdo->has_format); | |
175 | ||
176 | strcpy(buf + len, "FIXED_CHANNELS"); | |
177 | get_int(buf, &pdo->channels, &pdo->has_channels); | |
178 | ||
179 | strcpy(buf + len, "VOICES"); | |
180 | get_int(buf, &pdo->voices, &pdo->has_voices); | |
181 | } | |
182 | ||
183 | static AudiodevListEntry *legacy_opt(const char *drvname) | |
184 | { | |
185 | AudiodevListEntry *e = g_malloc0(sizeof(AudiodevListEntry)); | |
186 | e->dev = g_malloc0(sizeof(Audiodev)); | |
187 | e->dev->id = g_strdup(drvname); | |
188 | e->dev->driver = qapi_enum_parse( | |
189 | &AudiodevDriver_lookup, drvname, -1, &error_abort); | |
190 | ||
191 | audio_create_pdos(e->dev); | |
192 | ||
193 | handle_per_direction(audio_get_pdo_in(e->dev), "QEMU_AUDIO_ADC_"); | |
194 | handle_per_direction(audio_get_pdo_out(e->dev), "QEMU_AUDIO_DAC_"); | |
195 | ||
196 | get_int("QEMU_AUDIO_TIMER_PERIOD", | |
197 | &e->dev->timer_period, &e->dev->has_timer_period); | |
198 | ||
a93f3281 KZ |
199 | switch (e->dev->driver) { |
200 | case AUDIODEV_DRIVER_ALSA: | |
201 | handle_alsa(e->dev); | |
202 | break; | |
203 | ||
204 | default: | |
205 | break; | |
206 | } | |
207 | ||
71830221 KZ |
208 | return e; |
209 | } | |
210 | ||
211 | AudiodevListHead audio_handle_legacy_opts(void) | |
212 | { | |
213 | const char *drvname = getenv("QEMU_AUDIO_DRV"); | |
214 | AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head); | |
215 | ||
216 | if (drvname) { | |
217 | AudiodevListEntry *e; | |
218 | audio_driver *driver = audio_driver_lookup(drvname); | |
219 | if (!driver) { | |
220 | dolog("Unknown audio driver `%s'\n", drvname); | |
221 | exit(1); | |
222 | } | |
223 | e = legacy_opt(drvname); | |
224 | QSIMPLEQ_INSERT_TAIL(&head, e, next); | |
225 | } else { | |
226 | for (int i = 0; audio_prio_list[i]; i++) { | |
227 | audio_driver *driver = audio_driver_lookup(audio_prio_list[i]); | |
228 | if (driver && driver->can_be_default) { | |
229 | AudiodevListEntry *e = legacy_opt(driver->name); | |
230 | QSIMPLEQ_INSERT_TAIL(&head, e, next); | |
231 | } | |
232 | } | |
233 | if (QSIMPLEQ_EMPTY(&head)) { | |
234 | dolog("Internal error: no default audio driver available\n"); | |
235 | exit(1); | |
236 | } | |
237 | } | |
238 | ||
239 | return head; | |
240 | } | |
241 | ||
242 | /* visitor to print -audiodev option */ | |
243 | typedef struct { | |
244 | Visitor visitor; | |
245 | ||
246 | bool comma; | |
247 | GList *path; | |
248 | } LegacyPrintVisitor; | |
249 | ||
250 | static void lv_start_struct(Visitor *v, const char *name, void **obj, | |
251 | size_t size, Error **errp) | |
252 | { | |
253 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
254 | lv->path = g_list_append(lv->path, g_strdup(name)); | |
255 | } | |
256 | ||
257 | static void lv_end_struct(Visitor *v, void **obj) | |
258 | { | |
259 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
260 | lv->path = g_list_delete_link(lv->path, g_list_last(lv->path)); | |
261 | } | |
262 | ||
263 | static void lv_print_key(Visitor *v, const char *name) | |
264 | { | |
265 | GList *e; | |
266 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
267 | if (lv->comma) { | |
268 | putchar(','); | |
269 | } else { | |
270 | lv->comma = true; | |
271 | } | |
272 | ||
273 | for (e = lv->path; e; e = e->next) { | |
274 | if (e->data) { | |
275 | printf("%s.", (const char *) e->data); | |
276 | } | |
277 | } | |
278 | ||
279 | printf("%s=", name); | |
280 | } | |
281 | ||
282 | static void lv_type_int64(Visitor *v, const char *name, int64_t *obj, | |
283 | Error **errp) | |
284 | { | |
285 | lv_print_key(v, name); | |
286 | printf("%" PRIi64, *obj); | |
287 | } | |
288 | ||
289 | static void lv_type_uint64(Visitor *v, const char *name, uint64_t *obj, | |
290 | Error **errp) | |
291 | { | |
292 | lv_print_key(v, name); | |
293 | printf("%" PRIu64, *obj); | |
294 | } | |
295 | ||
296 | static void lv_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) | |
297 | { | |
298 | lv_print_key(v, name); | |
299 | printf("%s", *obj ? "on" : "off"); | |
300 | } | |
301 | ||
302 | static void lv_type_str(Visitor *v, const char *name, char **obj, Error **errp) | |
303 | { | |
304 | const char *str = *obj; | |
305 | lv_print_key(v, name); | |
306 | ||
307 | while (*str) { | |
308 | if (*str == ',') { | |
309 | putchar(','); | |
310 | } | |
311 | putchar(*str++); | |
312 | } | |
313 | } | |
314 | ||
315 | static void lv_complete(Visitor *v, void *opaque) | |
316 | { | |
317 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
318 | assert(lv->path == NULL); | |
319 | } | |
320 | ||
321 | static void lv_free(Visitor *v) | |
322 | { | |
323 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
324 | ||
325 | g_list_free_full(lv->path, g_free); | |
326 | g_free(lv); | |
327 | } | |
328 | ||
329 | static Visitor *legacy_visitor_new(void) | |
330 | { | |
331 | LegacyPrintVisitor *lv = g_malloc0(sizeof(LegacyPrintVisitor)); | |
332 | ||
333 | lv->visitor.start_struct = lv_start_struct; | |
334 | lv->visitor.end_struct = lv_end_struct; | |
335 | /* lists not supported */ | |
336 | lv->visitor.type_int64 = lv_type_int64; | |
337 | lv->visitor.type_uint64 = lv_type_uint64; | |
338 | lv->visitor.type_bool = lv_type_bool; | |
339 | lv->visitor.type_str = lv_type_str; | |
340 | ||
341 | lv->visitor.type = VISITOR_OUTPUT; | |
342 | lv->visitor.complete = lv_complete; | |
343 | lv->visitor.free = lv_free; | |
344 | ||
345 | return &lv->visitor; | |
346 | } | |
347 | ||
348 | void audio_legacy_help(void) | |
349 | { | |
350 | AudiodevListHead head; | |
351 | AudiodevListEntry *e; | |
352 | ||
353 | printf("Environment variable based configuration deprecated.\n"); | |
354 | printf("Please use the new -audiodev option.\n"); | |
355 | ||
356 | head = audio_handle_legacy_opts(); | |
357 | printf("\nEquivalent -audiodev to your current environment variables:\n"); | |
358 | if (!getenv("QEMU_AUDIO_DRV")) { | |
359 | printf("(Since you didn't specify QEMU_AUDIO_DRV, I'll list all " | |
360 | "possibilities)\n"); | |
361 | } | |
362 | ||
363 | QSIMPLEQ_FOREACH(e, &head, next) { | |
364 | Visitor *v; | |
365 | Audiodev *dev = e->dev; | |
366 | printf("-audiodev "); | |
367 | ||
368 | v = legacy_visitor_new(); | |
369 | visit_type_Audiodev(v, NULL, &dev, &error_abort); | |
370 | visit_free(v); | |
371 | ||
372 | printf("\n"); | |
373 | } | |
374 | audio_free_audiodev_list(&head); | |
375 | } |