]>
Commit | Line | Data |
---|---|---|
71830221 KZ |
1 | /* |
2 | * QEMU Audio subsystem: legacy configuration handling | |
3 | * | |
4 | * Copyright (c) 2015-2019 Zoltán Kővágó <[email protected]> | |
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 | ||
17c56dc1 KZ |
112 | |
113 | static void get_frames_to_usecs(const char *env, uint32_t *dst, bool *has_dst, | |
114 | AudiodevPerDirectionOptions *pdo) | |
115 | { | |
116 | const char *val = getenv(env); | |
117 | if (val) { | |
118 | *dst = frames_to_usecs(toui32(val), pdo); | |
119 | *has_dst = true; | |
120 | } | |
121 | } | |
122 | ||
4a3b8b34 KZ |
123 | static uint32_t samples_to_usecs(uint32_t samples, |
124 | AudiodevPerDirectionOptions *pdo) | |
125 | { | |
126 | uint32_t channels = pdo->has_channels ? pdo->channels : 2; | |
127 | return frames_to_usecs(samples / channels, pdo); | |
128 | } | |
129 | ||
2c324b28 KZ |
130 | static void get_samples_to_usecs(const char *env, uint32_t *dst, bool *has_dst, |
131 | AudiodevPerDirectionOptions *pdo) | |
132 | { | |
133 | const char *val = getenv(env); | |
134 | if (val) { | |
135 | *dst = samples_to_usecs(toui32(val), pdo); | |
136 | *has_dst = true; | |
137 | } | |
138 | } | |
139 | ||
4a3b8b34 KZ |
140 | static uint32_t bytes_to_usecs(uint32_t bytes, AudiodevPerDirectionOptions *pdo) |
141 | { | |
142 | AudioFormat fmt = pdo->has_format ? pdo->format : AUDIO_FORMAT_S16; | |
143 | uint32_t bytes_per_sample = audioformat_bytes_per_sample(fmt); | |
144 | return samples_to_usecs(bytes / bytes_per_sample, pdo); | |
145 | } | |
146 | ||
147 | static void get_bytes_to_usecs(const char *env, uint32_t *dst, bool *has_dst, | |
148 | AudiodevPerDirectionOptions *pdo) | |
149 | { | |
150 | const char *val = getenv(env); | |
151 | if (val) { | |
152 | *dst = bytes_to_usecs(toui32(val), pdo); | |
153 | *has_dst = true; | |
154 | } | |
155 | } | |
156 | ||
71830221 | 157 | /* backend specific functions */ |
a93f3281 KZ |
158 | /* ALSA */ |
159 | static void handle_alsa_per_direction( | |
160 | AudiodevAlsaPerDirectionOptions *apdo, const char *prefix) | |
161 | { | |
162 | char buf[64]; | |
163 | size_t len = strlen(prefix); | |
164 | bool size_in_usecs = false; | |
165 | bool dummy; | |
166 | ||
167 | memcpy(buf, prefix, len); | |
168 | strcpy(buf + len, "TRY_POLL"); | |
169 | get_bool(buf, &apdo->try_poll, &apdo->has_try_poll); | |
170 | ||
171 | strcpy(buf + len, "DEV"); | |
172 | get_str(buf, &apdo->dev, &apdo->has_dev); | |
173 | ||
174 | strcpy(buf + len, "SIZE_IN_USEC"); | |
175 | get_bool(buf, &size_in_usecs, &dummy); | |
176 | ||
177 | strcpy(buf + len, "PERIOD_SIZE"); | |
178 | get_int(buf, &apdo->period_length, &apdo->has_period_length); | |
179 | if (apdo->has_period_length && !size_in_usecs) { | |
180 | apdo->period_length = frames_to_usecs( | |
181 | apdo->period_length, | |
182 | qapi_AudiodevAlsaPerDirectionOptions_base(apdo)); | |
183 | } | |
184 | ||
185 | strcpy(buf + len, "BUFFER_SIZE"); | |
186 | get_int(buf, &apdo->buffer_length, &apdo->has_buffer_length); | |
187 | if (apdo->has_buffer_length && !size_in_usecs) { | |
188 | apdo->buffer_length = frames_to_usecs( | |
189 | apdo->buffer_length, | |
190 | qapi_AudiodevAlsaPerDirectionOptions_base(apdo)); | |
191 | } | |
192 | } | |
193 | ||
194 | static void handle_alsa(Audiodev *dev) | |
195 | { | |
196 | AudiodevAlsaOptions *aopt = &dev->u.alsa; | |
197 | handle_alsa_per_direction(aopt->in, "QEMU_ALSA_ADC_"); | |
198 | handle_alsa_per_direction(aopt->out, "QEMU_ALSA_DAC_"); | |
199 | ||
200 | get_millis_to_usecs("QEMU_ALSA_THRESHOLD", | |
201 | &aopt->threshold, &aopt->has_threshold); | |
202 | } | |
71830221 | 203 | |
17c56dc1 KZ |
204 | /* coreaudio */ |
205 | static void handle_coreaudio(Audiodev *dev) | |
206 | { | |
207 | get_frames_to_usecs( | |
208 | "QEMU_COREAUDIO_BUFFER_SIZE", | |
209 | &dev->u.coreaudio.out->buffer_length, | |
210 | &dev->u.coreaudio.out->has_buffer_length, | |
211 | qapi_AudiodevCoreaudioPerDirectionOptions_base(dev->u.coreaudio.out)); | |
212 | get_int("QEMU_COREAUDIO_BUFFER_COUNT", | |
213 | &dev->u.coreaudio.out->buffer_count, | |
214 | &dev->u.coreaudio.out->has_buffer_count); | |
215 | } | |
216 | ||
4a3b8b34 KZ |
217 | /* dsound */ |
218 | static void handle_dsound(Audiodev *dev) | |
219 | { | |
220 | get_millis_to_usecs("QEMU_DSOUND_LATENCY_MILLIS", | |
221 | &dev->u.dsound.latency, &dev->u.dsound.has_latency); | |
222 | get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_OUT", | |
223 | &dev->u.dsound.out->buffer_length, | |
224 | &dev->u.dsound.out->has_buffer_length, | |
225 | dev->u.dsound.out); | |
226 | get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_IN", | |
227 | &dev->u.dsound.in->buffer_length, | |
228 | &dev->u.dsound.in->has_buffer_length, | |
229 | dev->u.dsound.in); | |
230 | } | |
231 | ||
baf6c7f4 KZ |
232 | /* OSS */ |
233 | static void handle_oss_per_direction( | |
234 | AudiodevOssPerDirectionOptions *opdo, const char *try_poll_env, | |
235 | const char *dev_env) | |
236 | { | |
237 | get_bool(try_poll_env, &opdo->try_poll, &opdo->has_try_poll); | |
238 | get_str(dev_env, &opdo->dev, &opdo->has_dev); | |
239 | ||
240 | get_bytes_to_usecs("QEMU_OSS_FRAGSIZE", | |
241 | &opdo->buffer_length, &opdo->has_buffer_length, | |
242 | qapi_AudiodevOssPerDirectionOptions_base(opdo)); | |
243 | get_int("QEMU_OSS_NFRAGS", &opdo->buffer_count, | |
244 | &opdo->has_buffer_count); | |
245 | } | |
246 | ||
247 | static void handle_oss(Audiodev *dev) | |
248 | { | |
249 | AudiodevOssOptions *oopt = &dev->u.oss; | |
250 | handle_oss_per_direction(oopt->in, "QEMU_AUDIO_ADC_TRY_POLL", | |
251 | "QEMU_OSS_ADC_DEV"); | |
252 | handle_oss_per_direction(oopt->out, "QEMU_AUDIO_DAC_TRY_POLL", | |
253 | "QEMU_OSS_DAC_DEV"); | |
254 | ||
255 | get_bool("QEMU_OSS_MMAP", &oopt->try_mmap, &oopt->has_try_mmap); | |
256 | get_bool("QEMU_OSS_EXCLUSIVE", &oopt->exclusive, &oopt->has_exclusive); | |
257 | get_int("QEMU_OSS_POLICY", &oopt->dsp_policy, &oopt->has_dsp_policy); | |
258 | } | |
259 | ||
2c324b28 KZ |
260 | /* pulseaudio */ |
261 | static void handle_pa_per_direction( | |
262 | AudiodevPaPerDirectionOptions *ppdo, const char *env) | |
263 | { | |
264 | get_str(env, &ppdo->name, &ppdo->has_name); | |
265 | } | |
266 | ||
267 | static void handle_pa(Audiodev *dev) | |
268 | { | |
269 | handle_pa_per_direction(dev->u.pa.in, "QEMU_PA_SOURCE"); | |
270 | handle_pa_per_direction(dev->u.pa.out, "QEMU_PA_SINK"); | |
271 | ||
272 | get_samples_to_usecs( | |
273 | "QEMU_PA_SAMPLES", &dev->u.pa.in->buffer_length, | |
274 | &dev->u.pa.in->has_buffer_length, | |
275 | qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.in)); | |
276 | get_samples_to_usecs( | |
277 | "QEMU_PA_SAMPLES", &dev->u.pa.out->buffer_length, | |
278 | &dev->u.pa.out->has_buffer_length, | |
279 | qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.out)); | |
280 | ||
281 | get_str("QEMU_PA_SERVER", &dev->u.pa.server, &dev->u.pa.has_server); | |
282 | } | |
283 | ||
71830221 KZ |
284 | /* general */ |
285 | static void handle_per_direction( | |
286 | AudiodevPerDirectionOptions *pdo, const char *prefix) | |
287 | { | |
288 | char buf[64]; | |
289 | size_t len = strlen(prefix); | |
290 | ||
291 | memcpy(buf, prefix, len); | |
292 | strcpy(buf + len, "FIXED_SETTINGS"); | |
293 | get_bool(buf, &pdo->fixed_settings, &pdo->has_fixed_settings); | |
294 | ||
295 | strcpy(buf + len, "FIXED_FREQ"); | |
296 | get_int(buf, &pdo->frequency, &pdo->has_frequency); | |
297 | ||
298 | strcpy(buf + len, "FIXED_FMT"); | |
299 | get_fmt(buf, &pdo->format, &pdo->has_format); | |
300 | ||
301 | strcpy(buf + len, "FIXED_CHANNELS"); | |
302 | get_int(buf, &pdo->channels, &pdo->has_channels); | |
303 | ||
304 | strcpy(buf + len, "VOICES"); | |
305 | get_int(buf, &pdo->voices, &pdo->has_voices); | |
306 | } | |
307 | ||
308 | static AudiodevListEntry *legacy_opt(const char *drvname) | |
309 | { | |
310 | AudiodevListEntry *e = g_malloc0(sizeof(AudiodevListEntry)); | |
311 | e->dev = g_malloc0(sizeof(Audiodev)); | |
312 | e->dev->id = g_strdup(drvname); | |
313 | e->dev->driver = qapi_enum_parse( | |
314 | &AudiodevDriver_lookup, drvname, -1, &error_abort); | |
315 | ||
316 | audio_create_pdos(e->dev); | |
317 | ||
318 | handle_per_direction(audio_get_pdo_in(e->dev), "QEMU_AUDIO_ADC_"); | |
319 | handle_per_direction(audio_get_pdo_out(e->dev), "QEMU_AUDIO_DAC_"); | |
320 | ||
321 | get_int("QEMU_AUDIO_TIMER_PERIOD", | |
322 | &e->dev->timer_period, &e->dev->has_timer_period); | |
323 | ||
a93f3281 KZ |
324 | switch (e->dev->driver) { |
325 | case AUDIODEV_DRIVER_ALSA: | |
326 | handle_alsa(e->dev); | |
327 | break; | |
328 | ||
17c56dc1 KZ |
329 | case AUDIODEV_DRIVER_COREAUDIO: |
330 | handle_coreaudio(e->dev); | |
331 | break; | |
332 | ||
4a3b8b34 KZ |
333 | case AUDIODEV_DRIVER_DSOUND: |
334 | handle_dsound(e->dev); | |
335 | break; | |
336 | ||
baf6c7f4 KZ |
337 | case AUDIODEV_DRIVER_OSS: |
338 | handle_oss(e->dev); | |
339 | break; | |
340 | ||
2c324b28 KZ |
341 | case AUDIODEV_DRIVER_PA: |
342 | handle_pa(e->dev); | |
343 | break; | |
344 | ||
a93f3281 KZ |
345 | default: |
346 | break; | |
347 | } | |
348 | ||
71830221 KZ |
349 | return e; |
350 | } | |
351 | ||
352 | AudiodevListHead audio_handle_legacy_opts(void) | |
353 | { | |
354 | const char *drvname = getenv("QEMU_AUDIO_DRV"); | |
355 | AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head); | |
356 | ||
357 | if (drvname) { | |
358 | AudiodevListEntry *e; | |
359 | audio_driver *driver = audio_driver_lookup(drvname); | |
360 | if (!driver) { | |
361 | dolog("Unknown audio driver `%s'\n", drvname); | |
362 | exit(1); | |
363 | } | |
364 | e = legacy_opt(drvname); | |
365 | QSIMPLEQ_INSERT_TAIL(&head, e, next); | |
366 | } else { | |
367 | for (int i = 0; audio_prio_list[i]; i++) { | |
368 | audio_driver *driver = audio_driver_lookup(audio_prio_list[i]); | |
369 | if (driver && driver->can_be_default) { | |
370 | AudiodevListEntry *e = legacy_opt(driver->name); | |
371 | QSIMPLEQ_INSERT_TAIL(&head, e, next); | |
372 | } | |
373 | } | |
374 | if (QSIMPLEQ_EMPTY(&head)) { | |
375 | dolog("Internal error: no default audio driver available\n"); | |
376 | exit(1); | |
377 | } | |
378 | } | |
379 | ||
380 | return head; | |
381 | } | |
382 | ||
383 | /* visitor to print -audiodev option */ | |
384 | typedef struct { | |
385 | Visitor visitor; | |
386 | ||
387 | bool comma; | |
388 | GList *path; | |
389 | } LegacyPrintVisitor; | |
390 | ||
391 | static void lv_start_struct(Visitor *v, const char *name, void **obj, | |
392 | size_t size, Error **errp) | |
393 | { | |
394 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
395 | lv->path = g_list_append(lv->path, g_strdup(name)); | |
396 | } | |
397 | ||
398 | static void lv_end_struct(Visitor *v, void **obj) | |
399 | { | |
400 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
401 | lv->path = g_list_delete_link(lv->path, g_list_last(lv->path)); | |
402 | } | |
403 | ||
404 | static void lv_print_key(Visitor *v, const char *name) | |
405 | { | |
406 | GList *e; | |
407 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
408 | if (lv->comma) { | |
409 | putchar(','); | |
410 | } else { | |
411 | lv->comma = true; | |
412 | } | |
413 | ||
414 | for (e = lv->path; e; e = e->next) { | |
415 | if (e->data) { | |
416 | printf("%s.", (const char *) e->data); | |
417 | } | |
418 | } | |
419 | ||
420 | printf("%s=", name); | |
421 | } | |
422 | ||
423 | static void lv_type_int64(Visitor *v, const char *name, int64_t *obj, | |
424 | Error **errp) | |
425 | { | |
426 | lv_print_key(v, name); | |
427 | printf("%" PRIi64, *obj); | |
428 | } | |
429 | ||
430 | static void lv_type_uint64(Visitor *v, const char *name, uint64_t *obj, | |
431 | Error **errp) | |
432 | { | |
433 | lv_print_key(v, name); | |
434 | printf("%" PRIu64, *obj); | |
435 | } | |
436 | ||
437 | static void lv_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) | |
438 | { | |
439 | lv_print_key(v, name); | |
440 | printf("%s", *obj ? "on" : "off"); | |
441 | } | |
442 | ||
443 | static void lv_type_str(Visitor *v, const char *name, char **obj, Error **errp) | |
444 | { | |
445 | const char *str = *obj; | |
446 | lv_print_key(v, name); | |
447 | ||
448 | while (*str) { | |
449 | if (*str == ',') { | |
450 | putchar(','); | |
451 | } | |
452 | putchar(*str++); | |
453 | } | |
454 | } | |
455 | ||
456 | static void lv_complete(Visitor *v, void *opaque) | |
457 | { | |
458 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
459 | assert(lv->path == NULL); | |
460 | } | |
461 | ||
462 | static void lv_free(Visitor *v) | |
463 | { | |
464 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
465 | ||
466 | g_list_free_full(lv->path, g_free); | |
467 | g_free(lv); | |
468 | } | |
469 | ||
470 | static Visitor *legacy_visitor_new(void) | |
471 | { | |
472 | LegacyPrintVisitor *lv = g_malloc0(sizeof(LegacyPrintVisitor)); | |
473 | ||
474 | lv->visitor.start_struct = lv_start_struct; | |
475 | lv->visitor.end_struct = lv_end_struct; | |
476 | /* lists not supported */ | |
477 | lv->visitor.type_int64 = lv_type_int64; | |
478 | lv->visitor.type_uint64 = lv_type_uint64; | |
479 | lv->visitor.type_bool = lv_type_bool; | |
480 | lv->visitor.type_str = lv_type_str; | |
481 | ||
482 | lv->visitor.type = VISITOR_OUTPUT; | |
483 | lv->visitor.complete = lv_complete; | |
484 | lv->visitor.free = lv_free; | |
485 | ||
486 | return &lv->visitor; | |
487 | } | |
488 | ||
489 | void audio_legacy_help(void) | |
490 | { | |
491 | AudiodevListHead head; | |
492 | AudiodevListEntry *e; | |
493 | ||
494 | printf("Environment variable based configuration deprecated.\n"); | |
495 | printf("Please use the new -audiodev option.\n"); | |
496 | ||
497 | head = audio_handle_legacy_opts(); | |
498 | printf("\nEquivalent -audiodev to your current environment variables:\n"); | |
499 | if (!getenv("QEMU_AUDIO_DRV")) { | |
500 | printf("(Since you didn't specify QEMU_AUDIO_DRV, I'll list all " | |
501 | "possibilities)\n"); | |
502 | } | |
503 | ||
504 | QSIMPLEQ_FOREACH(e, &head, next) { | |
505 | Visitor *v; | |
506 | Audiodev *dev = e->dev; | |
507 | printf("-audiodev "); | |
508 | ||
509 | v = legacy_visitor_new(); | |
510 | visit_type_Audiodev(v, NULL, &dev, &error_abort); | |
511 | visit_free(v); | |
512 | ||
513 | printf("\n"); | |
514 | } | |
515 | audio_free_audiodev_list(&head); | |
516 | } |