sockets: avoid string truncation warnings when copying UNIX path
[qemu/ar7.git] / audio / audio_legacy.c
blob2fd58cb8ef252aaad27ba7c2f7fa45696100a1b6
1 /*
2 * QEMU Audio subsystem: legacy configuration handling
4 * Copyright (c) 2015-2019 Zoltán Kővágó <DirtY.iCE.hu@gmail.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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 "qemu/timer.h"
30 #include "qapi/error.h"
31 #include "qapi/qapi-visit-audio.h"
32 #include "qapi/visitor-impl.h"
34 #define AUDIO_CAP "audio-legacy"
35 #include "audio_int.h"
37 static uint32_t toui32(const char *str)
39 unsigned long long ret;
40 if (parse_uint_full(str, &ret, 10) || ret > UINT32_MAX) {
41 dolog("Invalid integer value `%s'\n", str);
42 exit(1);
44 return ret;
47 /* helper functions to convert env variables */
48 static void get_bool(const char *env, bool *dst, bool *has_dst)
50 const char *val = getenv(env);
51 if (val) {
52 *dst = toui32(val) != 0;
53 *has_dst = true;
57 static void get_int(const char *env, uint32_t *dst, bool *has_dst)
59 const char *val = getenv(env);
60 if (val) {
61 *dst = toui32(val);
62 *has_dst = true;
66 static void get_str(const char *env, char **dst, bool *has_dst)
68 const char *val = getenv(env);
69 if (val) {
70 if (*has_dst) {
71 g_free(*dst);
73 *dst = g_strdup(val);
74 *has_dst = true;
78 static void get_fmt(const char *env, AudioFormat *dst, bool *has_dst)
80 const char *val = getenv(env);
81 if (val) {
82 size_t i;
83 for (i = 0; AudioFormat_lookup.size; ++i) {
84 if (strcasecmp(val, AudioFormat_lookup.array[i]) == 0) {
85 *dst = i;
86 *has_dst = true;
87 return;
91 dolog("Invalid audio format `%s'\n", val);
92 exit(1);
97 static void get_millis_to_usecs(const char *env, uint32_t *dst, bool *has_dst)
99 const char *val = getenv(env);
100 if (val) {
101 *dst = toui32(val) * 1000;
102 *has_dst = true;
106 static uint32_t frames_to_usecs(uint32_t frames,
107 AudiodevPerDirectionOptions *pdo)
109 uint32_t freq = pdo->has_frequency ? pdo->frequency : 44100;
110 return (frames * 1000000 + freq / 2) / freq;
114 static void get_frames_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
115 AudiodevPerDirectionOptions *pdo)
117 const char *val = getenv(env);
118 if (val) {
119 *dst = frames_to_usecs(toui32(val), pdo);
120 *has_dst = true;
124 static uint32_t samples_to_usecs(uint32_t samples,
125 AudiodevPerDirectionOptions *pdo)
127 uint32_t channels = pdo->has_channels ? pdo->channels : 2;
128 return frames_to_usecs(samples / channels, pdo);
131 static void get_samples_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
132 AudiodevPerDirectionOptions *pdo)
134 const char *val = getenv(env);
135 if (val) {
136 *dst = samples_to_usecs(toui32(val), pdo);
137 *has_dst = true;
141 static uint32_t bytes_to_usecs(uint32_t bytes, AudiodevPerDirectionOptions *pdo)
143 AudioFormat fmt = pdo->has_format ? pdo->format : AUDIO_FORMAT_S16;
144 uint32_t bytes_per_sample = audioformat_bytes_per_sample(fmt);
145 return samples_to_usecs(bytes / bytes_per_sample, pdo);
148 static void get_bytes_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
149 AudiodevPerDirectionOptions *pdo)
151 const char *val = getenv(env);
152 if (val) {
153 *dst = bytes_to_usecs(toui32(val), pdo);
154 *has_dst = true;
158 /* backend specific functions */
159 /* ALSA */
160 static void handle_alsa_per_direction(
161 AudiodevAlsaPerDirectionOptions *apdo, const char *prefix)
163 char buf[64];
164 size_t len = strlen(prefix);
165 bool size_in_usecs = false;
166 bool dummy;
168 memcpy(buf, prefix, len);
169 strcpy(buf + len, "TRY_POLL");
170 get_bool(buf, &apdo->try_poll, &apdo->has_try_poll);
172 strcpy(buf + len, "DEV");
173 get_str(buf, &apdo->dev, &apdo->has_dev);
175 strcpy(buf + len, "SIZE_IN_USEC");
176 get_bool(buf, &size_in_usecs, &dummy);
178 strcpy(buf + len, "PERIOD_SIZE");
179 get_int(buf, &apdo->period_length, &apdo->has_period_length);
180 if (apdo->has_period_length && !size_in_usecs) {
181 apdo->period_length = frames_to_usecs(
182 apdo->period_length,
183 qapi_AudiodevAlsaPerDirectionOptions_base(apdo));
186 strcpy(buf + len, "BUFFER_SIZE");
187 get_int(buf, &apdo->buffer_length, &apdo->has_buffer_length);
188 if (apdo->has_buffer_length && !size_in_usecs) {
189 apdo->buffer_length = frames_to_usecs(
190 apdo->buffer_length,
191 qapi_AudiodevAlsaPerDirectionOptions_base(apdo));
195 static void handle_alsa(Audiodev *dev)
197 AudiodevAlsaOptions *aopt = &dev->u.alsa;
198 handle_alsa_per_direction(aopt->in, "QEMU_ALSA_ADC_");
199 handle_alsa_per_direction(aopt->out, "QEMU_ALSA_DAC_");
201 get_millis_to_usecs("QEMU_ALSA_THRESHOLD",
202 &aopt->threshold, &aopt->has_threshold);
205 /* coreaudio */
206 static void handle_coreaudio(Audiodev *dev)
208 get_frames_to_usecs(
209 "QEMU_COREAUDIO_BUFFER_SIZE",
210 &dev->u.coreaudio.out->buffer_length,
211 &dev->u.coreaudio.out->has_buffer_length,
212 qapi_AudiodevCoreaudioPerDirectionOptions_base(dev->u.coreaudio.out));
213 get_int("QEMU_COREAUDIO_BUFFER_COUNT",
214 &dev->u.coreaudio.out->buffer_count,
215 &dev->u.coreaudio.out->has_buffer_count);
218 /* dsound */
219 static void handle_dsound(Audiodev *dev)
221 get_millis_to_usecs("QEMU_DSOUND_LATENCY_MILLIS",
222 &dev->u.dsound.latency, &dev->u.dsound.has_latency);
223 get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_OUT",
224 &dev->u.dsound.out->buffer_length,
225 &dev->u.dsound.out->has_buffer_length,
226 dev->u.dsound.out);
227 get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_IN",
228 &dev->u.dsound.in->buffer_length,
229 &dev->u.dsound.in->has_buffer_length,
230 dev->u.dsound.in);
233 /* OSS */
234 static void handle_oss_per_direction(
235 AudiodevOssPerDirectionOptions *opdo, const char *try_poll_env,
236 const char *dev_env)
238 get_bool(try_poll_env, &opdo->try_poll, &opdo->has_try_poll);
239 get_str(dev_env, &opdo->dev, &opdo->has_dev);
241 get_bytes_to_usecs("QEMU_OSS_FRAGSIZE",
242 &opdo->buffer_length, &opdo->has_buffer_length,
243 qapi_AudiodevOssPerDirectionOptions_base(opdo));
244 get_int("QEMU_OSS_NFRAGS", &opdo->buffer_count,
245 &opdo->has_buffer_count);
248 static void handle_oss(Audiodev *dev)
250 AudiodevOssOptions *oopt = &dev->u.oss;
251 handle_oss_per_direction(oopt->in, "QEMU_AUDIO_ADC_TRY_POLL",
252 "QEMU_OSS_ADC_DEV");
253 handle_oss_per_direction(oopt->out, "QEMU_AUDIO_DAC_TRY_POLL",
254 "QEMU_OSS_DAC_DEV");
256 get_bool("QEMU_OSS_MMAP", &oopt->try_mmap, &oopt->has_try_mmap);
257 get_bool("QEMU_OSS_EXCLUSIVE", &oopt->exclusive, &oopt->has_exclusive);
258 get_int("QEMU_OSS_POLICY", &oopt->dsp_policy, &oopt->has_dsp_policy);
261 /* pulseaudio */
262 static void handle_pa_per_direction(
263 AudiodevPaPerDirectionOptions *ppdo, const char *env)
265 get_str(env, &ppdo->name, &ppdo->has_name);
268 static void handle_pa(Audiodev *dev)
270 handle_pa_per_direction(dev->u.pa.in, "QEMU_PA_SOURCE");
271 handle_pa_per_direction(dev->u.pa.out, "QEMU_PA_SINK");
273 get_samples_to_usecs(
274 "QEMU_PA_SAMPLES", &dev->u.pa.in->buffer_length,
275 &dev->u.pa.in->has_buffer_length,
276 qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.in));
277 get_samples_to_usecs(
278 "QEMU_PA_SAMPLES", &dev->u.pa.out->buffer_length,
279 &dev->u.pa.out->has_buffer_length,
280 qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.out));
282 get_str("QEMU_PA_SERVER", &dev->u.pa.server, &dev->u.pa.has_server);
285 /* SDL */
286 static void handle_sdl(Audiodev *dev)
288 /* SDL is output only */
289 get_samples_to_usecs("QEMU_SDL_SAMPLES", &dev->u.sdl.out->buffer_length,
290 &dev->u.sdl.out->has_buffer_length, dev->u.sdl.out);
293 /* wav */
294 static void handle_wav(Audiodev *dev)
296 get_int("QEMU_WAV_FREQUENCY",
297 &dev->u.wav.out->frequency, &dev->u.wav.out->has_frequency);
298 get_fmt("QEMU_WAV_FORMAT", &dev->u.wav.out->format,
299 &dev->u.wav.out->has_format);
300 get_int("QEMU_WAV_DAC_FIXED_CHANNELS",
301 &dev->u.wav.out->channels, &dev->u.wav.out->has_channels);
302 get_str("QEMU_WAV_PATH", &dev->u.wav.path, &dev->u.wav.has_path);
305 /* general */
306 static void handle_per_direction(
307 AudiodevPerDirectionOptions *pdo, const char *prefix)
309 char buf[64];
310 size_t len = strlen(prefix);
312 memcpy(buf, prefix, len);
313 strcpy(buf + len, "FIXED_SETTINGS");
314 get_bool(buf, &pdo->fixed_settings, &pdo->has_fixed_settings);
316 strcpy(buf + len, "FIXED_FREQ");
317 get_int(buf, &pdo->frequency, &pdo->has_frequency);
319 strcpy(buf + len, "FIXED_FMT");
320 get_fmt(buf, &pdo->format, &pdo->has_format);
322 strcpy(buf + len, "FIXED_CHANNELS");
323 get_int(buf, &pdo->channels, &pdo->has_channels);
325 strcpy(buf + len, "VOICES");
326 get_int(buf, &pdo->voices, &pdo->has_voices);
329 static AudiodevListEntry *legacy_opt(const char *drvname)
331 AudiodevListEntry *e = g_malloc0(sizeof(AudiodevListEntry));
332 e->dev = g_malloc0(sizeof(Audiodev));
333 e->dev->id = g_strdup(drvname);
334 e->dev->driver = qapi_enum_parse(
335 &AudiodevDriver_lookup, drvname, -1, &error_abort);
337 audio_create_pdos(e->dev);
339 handle_per_direction(audio_get_pdo_in(e->dev), "QEMU_AUDIO_ADC_");
340 handle_per_direction(audio_get_pdo_out(e->dev), "QEMU_AUDIO_DAC_");
342 /* Original description: Timer period in HZ (0 - use lowest possible) */
343 get_int("QEMU_AUDIO_TIMER_PERIOD",
344 &e->dev->timer_period, &e->dev->has_timer_period);
345 if (e->dev->has_timer_period && e->dev->timer_period) {
346 e->dev->timer_period = NANOSECONDS_PER_SECOND / 1000 /
347 e->dev->timer_period;
350 switch (e->dev->driver) {
351 case AUDIODEV_DRIVER_ALSA:
352 handle_alsa(e->dev);
353 break;
355 case AUDIODEV_DRIVER_COREAUDIO:
356 handle_coreaudio(e->dev);
357 break;
359 case AUDIODEV_DRIVER_DSOUND:
360 handle_dsound(e->dev);
361 break;
363 case AUDIODEV_DRIVER_OSS:
364 handle_oss(e->dev);
365 break;
367 case AUDIODEV_DRIVER_PA:
368 handle_pa(e->dev);
369 break;
371 case AUDIODEV_DRIVER_SDL:
372 handle_sdl(e->dev);
373 break;
375 case AUDIODEV_DRIVER_WAV:
376 handle_wav(e->dev);
377 break;
379 default:
380 break;
383 return e;
386 AudiodevListHead audio_handle_legacy_opts(void)
388 const char *drvname = getenv("QEMU_AUDIO_DRV");
389 AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
391 if (drvname) {
392 AudiodevListEntry *e;
393 audio_driver *driver = audio_driver_lookup(drvname);
394 if (!driver) {
395 dolog("Unknown audio driver `%s'\n", drvname);
396 exit(1);
398 e = legacy_opt(drvname);
399 QSIMPLEQ_INSERT_TAIL(&head, e, next);
400 } else {
401 for (int i = 0; audio_prio_list[i]; i++) {
402 audio_driver *driver = audio_driver_lookup(audio_prio_list[i]);
403 if (driver && driver->can_be_default) {
404 AudiodevListEntry *e = legacy_opt(driver->name);
405 QSIMPLEQ_INSERT_TAIL(&head, e, next);
408 if (QSIMPLEQ_EMPTY(&head)) {
409 dolog("Internal error: no default audio driver available\n");
410 exit(1);
414 return head;
417 /* visitor to print -audiodev option */
418 typedef struct {
419 Visitor visitor;
421 bool comma;
422 GList *path;
423 } LegacyPrintVisitor;
425 static void lv_start_struct(Visitor *v, const char *name, void **obj,
426 size_t size, Error **errp)
428 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
429 lv->path = g_list_append(lv->path, g_strdup(name));
432 static void lv_end_struct(Visitor *v, void **obj)
434 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
435 lv->path = g_list_delete_link(lv->path, g_list_last(lv->path));
438 static void lv_print_key(Visitor *v, const char *name)
440 GList *e;
441 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
442 if (lv->comma) {
443 putchar(',');
444 } else {
445 lv->comma = true;
448 for (e = lv->path; e; e = e->next) {
449 if (e->data) {
450 printf("%s.", (const char *) e->data);
454 printf("%s=", name);
457 static void lv_type_int64(Visitor *v, const char *name, int64_t *obj,
458 Error **errp)
460 lv_print_key(v, name);
461 printf("%" PRIi64, *obj);
464 static void lv_type_uint64(Visitor *v, const char *name, uint64_t *obj,
465 Error **errp)
467 lv_print_key(v, name);
468 printf("%" PRIu64, *obj);
471 static void lv_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
473 lv_print_key(v, name);
474 printf("%s", *obj ? "on" : "off");
477 static void lv_type_str(Visitor *v, const char *name, char **obj, Error **errp)
479 const char *str = *obj;
480 lv_print_key(v, name);
482 while (*str) {
483 if (*str == ',') {
484 putchar(',');
486 putchar(*str++);
490 static void lv_complete(Visitor *v, void *opaque)
492 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
493 assert(lv->path == NULL);
496 static void lv_free(Visitor *v)
498 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
500 g_list_free_full(lv->path, g_free);
501 g_free(lv);
504 static Visitor *legacy_visitor_new(void)
506 LegacyPrintVisitor *lv = g_malloc0(sizeof(LegacyPrintVisitor));
508 lv->visitor.start_struct = lv_start_struct;
509 lv->visitor.end_struct = lv_end_struct;
510 /* lists not supported */
511 lv->visitor.type_int64 = lv_type_int64;
512 lv->visitor.type_uint64 = lv_type_uint64;
513 lv->visitor.type_bool = lv_type_bool;
514 lv->visitor.type_str = lv_type_str;
516 lv->visitor.type = VISITOR_OUTPUT;
517 lv->visitor.complete = lv_complete;
518 lv->visitor.free = lv_free;
520 return &lv->visitor;
523 void audio_legacy_help(void)
525 AudiodevListHead head;
526 AudiodevListEntry *e;
528 printf("Environment variable based configuration deprecated.\n");
529 printf("Please use the new -audiodev option.\n");
531 head = audio_handle_legacy_opts();
532 printf("\nEquivalent -audiodev to your current environment variables:\n");
533 if (!getenv("QEMU_AUDIO_DRV")) {
534 printf("(Since you didn't specify QEMU_AUDIO_DRV, I'll list all "
535 "possibilities)\n");
538 QSIMPLEQ_FOREACH(e, &head, next) {
539 Visitor *v;
540 Audiodev *dev = e->dev;
541 printf("-audiodev ");
543 v = legacy_visitor_new();
544 visit_type_Audiodev(v, NULL, &dev, &error_abort);
545 visit_free(v);
547 printf("\n");
549 audio_free_audiodev_list(&head);