]>
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 | ||
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 | ||
130 | static uint32_t bytes_to_usecs(uint32_t bytes, AudiodevPerDirectionOptions *pdo) | |
131 | { | |
132 | AudioFormat fmt = pdo->has_format ? pdo->format : AUDIO_FORMAT_S16; | |
133 | uint32_t bytes_per_sample = audioformat_bytes_per_sample(fmt); | |
134 | return samples_to_usecs(bytes / bytes_per_sample, pdo); | |
135 | } | |
136 | ||
137 | static void get_bytes_to_usecs(const char *env, uint32_t *dst, bool *has_dst, | |
138 | AudiodevPerDirectionOptions *pdo) | |
139 | { | |
140 | const char *val = getenv(env); | |
141 | if (val) { | |
142 | *dst = bytes_to_usecs(toui32(val), pdo); | |
143 | *has_dst = true; | |
144 | } | |
145 | } | |
146 | ||
71830221 | 147 | /* backend specific functions */ |
a93f3281 KZ |
148 | /* ALSA */ |
149 | static void handle_alsa_per_direction( | |
150 | AudiodevAlsaPerDirectionOptions *apdo, const char *prefix) | |
151 | { | |
152 | char buf[64]; | |
153 | size_t len = strlen(prefix); | |
154 | bool size_in_usecs = false; | |
155 | bool dummy; | |
156 | ||
157 | memcpy(buf, prefix, len); | |
158 | strcpy(buf + len, "TRY_POLL"); | |
159 | get_bool(buf, &apdo->try_poll, &apdo->has_try_poll); | |
160 | ||
161 | strcpy(buf + len, "DEV"); | |
162 | get_str(buf, &apdo->dev, &apdo->has_dev); | |
163 | ||
164 | strcpy(buf + len, "SIZE_IN_USEC"); | |
165 | get_bool(buf, &size_in_usecs, &dummy); | |
166 | ||
167 | strcpy(buf + len, "PERIOD_SIZE"); | |
168 | get_int(buf, &apdo->period_length, &apdo->has_period_length); | |
169 | if (apdo->has_period_length && !size_in_usecs) { | |
170 | apdo->period_length = frames_to_usecs( | |
171 | apdo->period_length, | |
172 | qapi_AudiodevAlsaPerDirectionOptions_base(apdo)); | |
173 | } | |
174 | ||
175 | strcpy(buf + len, "BUFFER_SIZE"); | |
176 | get_int(buf, &apdo->buffer_length, &apdo->has_buffer_length); | |
177 | if (apdo->has_buffer_length && !size_in_usecs) { | |
178 | apdo->buffer_length = frames_to_usecs( | |
179 | apdo->buffer_length, | |
180 | qapi_AudiodevAlsaPerDirectionOptions_base(apdo)); | |
181 | } | |
182 | } | |
183 | ||
184 | static void handle_alsa(Audiodev *dev) | |
185 | { | |
186 | AudiodevAlsaOptions *aopt = &dev->u.alsa; | |
187 | handle_alsa_per_direction(aopt->in, "QEMU_ALSA_ADC_"); | |
188 | handle_alsa_per_direction(aopt->out, "QEMU_ALSA_DAC_"); | |
189 | ||
190 | get_millis_to_usecs("QEMU_ALSA_THRESHOLD", | |
191 | &aopt->threshold, &aopt->has_threshold); | |
192 | } | |
71830221 | 193 | |
17c56dc1 KZ |
194 | /* coreaudio */ |
195 | static void handle_coreaudio(Audiodev *dev) | |
196 | { | |
197 | get_frames_to_usecs( | |
198 | "QEMU_COREAUDIO_BUFFER_SIZE", | |
199 | &dev->u.coreaudio.out->buffer_length, | |
200 | &dev->u.coreaudio.out->has_buffer_length, | |
201 | qapi_AudiodevCoreaudioPerDirectionOptions_base(dev->u.coreaudio.out)); | |
202 | get_int("QEMU_COREAUDIO_BUFFER_COUNT", | |
203 | &dev->u.coreaudio.out->buffer_count, | |
204 | &dev->u.coreaudio.out->has_buffer_count); | |
205 | } | |
206 | ||
4a3b8b34 KZ |
207 | /* dsound */ |
208 | static void handle_dsound(Audiodev *dev) | |
209 | { | |
210 | get_millis_to_usecs("QEMU_DSOUND_LATENCY_MILLIS", | |
211 | &dev->u.dsound.latency, &dev->u.dsound.has_latency); | |
212 | get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_OUT", | |
213 | &dev->u.dsound.out->buffer_length, | |
214 | &dev->u.dsound.out->has_buffer_length, | |
215 | dev->u.dsound.out); | |
216 | get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_IN", | |
217 | &dev->u.dsound.in->buffer_length, | |
218 | &dev->u.dsound.in->has_buffer_length, | |
219 | dev->u.dsound.in); | |
220 | } | |
221 | ||
71830221 KZ |
222 | /* general */ |
223 | static void handle_per_direction( | |
224 | AudiodevPerDirectionOptions *pdo, const char *prefix) | |
225 | { | |
226 | char buf[64]; | |
227 | size_t len = strlen(prefix); | |
228 | ||
229 | memcpy(buf, prefix, len); | |
230 | strcpy(buf + len, "FIXED_SETTINGS"); | |
231 | get_bool(buf, &pdo->fixed_settings, &pdo->has_fixed_settings); | |
232 | ||
233 | strcpy(buf + len, "FIXED_FREQ"); | |
234 | get_int(buf, &pdo->frequency, &pdo->has_frequency); | |
235 | ||
236 | strcpy(buf + len, "FIXED_FMT"); | |
237 | get_fmt(buf, &pdo->format, &pdo->has_format); | |
238 | ||
239 | strcpy(buf + len, "FIXED_CHANNELS"); | |
240 | get_int(buf, &pdo->channels, &pdo->has_channels); | |
241 | ||
242 | strcpy(buf + len, "VOICES"); | |
243 | get_int(buf, &pdo->voices, &pdo->has_voices); | |
244 | } | |
245 | ||
246 | static AudiodevListEntry *legacy_opt(const char *drvname) | |
247 | { | |
248 | AudiodevListEntry *e = g_malloc0(sizeof(AudiodevListEntry)); | |
249 | e->dev = g_malloc0(sizeof(Audiodev)); | |
250 | e->dev->id = g_strdup(drvname); | |
251 | e->dev->driver = qapi_enum_parse( | |
252 | &AudiodevDriver_lookup, drvname, -1, &error_abort); | |
253 | ||
254 | audio_create_pdos(e->dev); | |
255 | ||
256 | handle_per_direction(audio_get_pdo_in(e->dev), "QEMU_AUDIO_ADC_"); | |
257 | handle_per_direction(audio_get_pdo_out(e->dev), "QEMU_AUDIO_DAC_"); | |
258 | ||
259 | get_int("QEMU_AUDIO_TIMER_PERIOD", | |
260 | &e->dev->timer_period, &e->dev->has_timer_period); | |
261 | ||
a93f3281 KZ |
262 | switch (e->dev->driver) { |
263 | case AUDIODEV_DRIVER_ALSA: | |
264 | handle_alsa(e->dev); | |
265 | break; | |
266 | ||
17c56dc1 KZ |
267 | case AUDIODEV_DRIVER_COREAUDIO: |
268 | handle_coreaudio(e->dev); | |
269 | break; | |
270 | ||
4a3b8b34 KZ |
271 | case AUDIODEV_DRIVER_DSOUND: |
272 | handle_dsound(e->dev); | |
273 | break; | |
274 | ||
a93f3281 KZ |
275 | default: |
276 | break; | |
277 | } | |
278 | ||
71830221 KZ |
279 | return e; |
280 | } | |
281 | ||
282 | AudiodevListHead audio_handle_legacy_opts(void) | |
283 | { | |
284 | const char *drvname = getenv("QEMU_AUDIO_DRV"); | |
285 | AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head); | |
286 | ||
287 | if (drvname) { | |
288 | AudiodevListEntry *e; | |
289 | audio_driver *driver = audio_driver_lookup(drvname); | |
290 | if (!driver) { | |
291 | dolog("Unknown audio driver `%s'\n", drvname); | |
292 | exit(1); | |
293 | } | |
294 | e = legacy_opt(drvname); | |
295 | QSIMPLEQ_INSERT_TAIL(&head, e, next); | |
296 | } else { | |
297 | for (int i = 0; audio_prio_list[i]; i++) { | |
298 | audio_driver *driver = audio_driver_lookup(audio_prio_list[i]); | |
299 | if (driver && driver->can_be_default) { | |
300 | AudiodevListEntry *e = legacy_opt(driver->name); | |
301 | QSIMPLEQ_INSERT_TAIL(&head, e, next); | |
302 | } | |
303 | } | |
304 | if (QSIMPLEQ_EMPTY(&head)) { | |
305 | dolog("Internal error: no default audio driver available\n"); | |
306 | exit(1); | |
307 | } | |
308 | } | |
309 | ||
310 | return head; | |
311 | } | |
312 | ||
313 | /* visitor to print -audiodev option */ | |
314 | typedef struct { | |
315 | Visitor visitor; | |
316 | ||
317 | bool comma; | |
318 | GList *path; | |
319 | } LegacyPrintVisitor; | |
320 | ||
321 | static void lv_start_struct(Visitor *v, const char *name, void **obj, | |
322 | size_t size, Error **errp) | |
323 | { | |
324 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
325 | lv->path = g_list_append(lv->path, g_strdup(name)); | |
326 | } | |
327 | ||
328 | static void lv_end_struct(Visitor *v, void **obj) | |
329 | { | |
330 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
331 | lv->path = g_list_delete_link(lv->path, g_list_last(lv->path)); | |
332 | } | |
333 | ||
334 | static void lv_print_key(Visitor *v, const char *name) | |
335 | { | |
336 | GList *e; | |
337 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
338 | if (lv->comma) { | |
339 | putchar(','); | |
340 | } else { | |
341 | lv->comma = true; | |
342 | } | |
343 | ||
344 | for (e = lv->path; e; e = e->next) { | |
345 | if (e->data) { | |
346 | printf("%s.", (const char *) e->data); | |
347 | } | |
348 | } | |
349 | ||
350 | printf("%s=", name); | |
351 | } | |
352 | ||
353 | static void lv_type_int64(Visitor *v, const char *name, int64_t *obj, | |
354 | Error **errp) | |
355 | { | |
356 | lv_print_key(v, name); | |
357 | printf("%" PRIi64, *obj); | |
358 | } | |
359 | ||
360 | static void lv_type_uint64(Visitor *v, const char *name, uint64_t *obj, | |
361 | Error **errp) | |
362 | { | |
363 | lv_print_key(v, name); | |
364 | printf("%" PRIu64, *obj); | |
365 | } | |
366 | ||
367 | static void lv_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) | |
368 | { | |
369 | lv_print_key(v, name); | |
370 | printf("%s", *obj ? "on" : "off"); | |
371 | } | |
372 | ||
373 | static void lv_type_str(Visitor *v, const char *name, char **obj, Error **errp) | |
374 | { | |
375 | const char *str = *obj; | |
376 | lv_print_key(v, name); | |
377 | ||
378 | while (*str) { | |
379 | if (*str == ',') { | |
380 | putchar(','); | |
381 | } | |
382 | putchar(*str++); | |
383 | } | |
384 | } | |
385 | ||
386 | static void lv_complete(Visitor *v, void *opaque) | |
387 | { | |
388 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
389 | assert(lv->path == NULL); | |
390 | } | |
391 | ||
392 | static void lv_free(Visitor *v) | |
393 | { | |
394 | LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v; | |
395 | ||
396 | g_list_free_full(lv->path, g_free); | |
397 | g_free(lv); | |
398 | } | |
399 | ||
400 | static Visitor *legacy_visitor_new(void) | |
401 | { | |
402 | LegacyPrintVisitor *lv = g_malloc0(sizeof(LegacyPrintVisitor)); | |
403 | ||
404 | lv->visitor.start_struct = lv_start_struct; | |
405 | lv->visitor.end_struct = lv_end_struct; | |
406 | /* lists not supported */ | |
407 | lv->visitor.type_int64 = lv_type_int64; | |
408 | lv->visitor.type_uint64 = lv_type_uint64; | |
409 | lv->visitor.type_bool = lv_type_bool; | |
410 | lv->visitor.type_str = lv_type_str; | |
411 | ||
412 | lv->visitor.type = VISITOR_OUTPUT; | |
413 | lv->visitor.complete = lv_complete; | |
414 | lv->visitor.free = lv_free; | |
415 | ||
416 | return &lv->visitor; | |
417 | } | |
418 | ||
419 | void audio_legacy_help(void) | |
420 | { | |
421 | AudiodevListHead head; | |
422 | AudiodevListEntry *e; | |
423 | ||
424 | printf("Environment variable based configuration deprecated.\n"); | |
425 | printf("Please use the new -audiodev option.\n"); | |
426 | ||
427 | head = audio_handle_legacy_opts(); | |
428 | printf("\nEquivalent -audiodev to your current environment variables:\n"); | |
429 | if (!getenv("QEMU_AUDIO_DRV")) { | |
430 | printf("(Since you didn't specify QEMU_AUDIO_DRV, I'll list all " | |
431 | "possibilities)\n"); | |
432 | } | |
433 | ||
434 | QSIMPLEQ_FOREACH(e, &head, next) { | |
435 | Visitor *v; | |
436 | Audiodev *dev = e->dev; | |
437 | printf("-audiodev "); | |
438 | ||
439 | v = legacy_visitor_new(); | |
440 | visit_type_Audiodev(v, NULL, &dev, &error_abort); | |
441 | visit_free(v); | |
442 | ||
443 | printf("\n"); | |
444 | } | |
445 | audio_free_audiodev_list(&head); | |
446 | } |