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
24 #include "qemu/osdep.h"
26 #include "audio_int.h"
27 #include "qemu/cutils.h"
28 #include "qemu/timer.h"
29 #include "qapi/error.h"
30 #include "qapi/qapi-visit-audio.h"
31 #include "qapi/visitor-impl.h"
33 #define AUDIO_CAP "audio-legacy"
34 #include "audio_int.h"
36 static uint32_t toui32(const char *str
)
38 unsigned long long ret
;
39 if (parse_uint_full(str
, &ret
, 10) || ret
> UINT32_MAX
) {
40 dolog("Invalid integer value `%s'\n", str
);
46 /* helper functions to convert env variables */
47 static void get_bool(const char *env
, bool *dst
, bool *has_dst
)
49 const char *val
= getenv(env
);
51 *dst
= toui32(val
) != 0;
56 static void get_int(const char *env
, uint32_t *dst
, bool *has_dst
)
58 const char *val
= getenv(env
);
65 static void get_str(const char *env
, char **dst
, bool *has_dst
)
67 const char *val
= getenv(env
);
77 static void get_fmt(const char *env
, AudioFormat
*dst
, bool *has_dst
)
79 const char *val
= getenv(env
);
82 for (i
= 0; AudioFormat_lookup
.size
; ++i
) {
83 if (strcasecmp(val
, AudioFormat_lookup
.array
[i
]) == 0) {
90 dolog("Invalid audio format `%s'\n", val
);
96 static void get_millis_to_usecs(const char *env
, uint32_t *dst
, bool *has_dst
)
98 const char *val
= getenv(env
);
100 *dst
= toui32(val
) * 1000;
105 static uint32_t frames_to_usecs(uint32_t frames
,
106 AudiodevPerDirectionOptions
*pdo
)
108 uint32_t freq
= pdo
->has_frequency
? pdo
->frequency
: 44100;
109 return (frames
* 1000000 + freq
/ 2) / freq
;
113 static void get_frames_to_usecs(const char *env
, uint32_t *dst
, bool *has_dst
,
114 AudiodevPerDirectionOptions
*pdo
)
116 const char *val
= getenv(env
);
118 *dst
= frames_to_usecs(toui32(val
), pdo
);
123 static uint32_t samples_to_usecs(uint32_t samples
,
124 AudiodevPerDirectionOptions
*pdo
)
126 uint32_t channels
= pdo
->has_channels
? pdo
->channels
: 2;
127 return frames_to_usecs(samples
/ channels
, pdo
);
130 static void get_samples_to_usecs(const char *env
, uint32_t *dst
, bool *has_dst
,
131 AudiodevPerDirectionOptions
*pdo
)
133 const char *val
= getenv(env
);
135 *dst
= samples_to_usecs(toui32(val
), pdo
);
140 static uint32_t bytes_to_usecs(uint32_t bytes
, AudiodevPerDirectionOptions
*pdo
)
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
);
147 static void get_bytes_to_usecs(const char *env
, uint32_t *dst
, bool *has_dst
,
148 AudiodevPerDirectionOptions
*pdo
)
150 const char *val
= getenv(env
);
152 *dst
= bytes_to_usecs(toui32(val
), pdo
);
157 /* backend specific functions */
159 static void handle_alsa_per_direction(
160 AudiodevAlsaPerDirectionOptions
*apdo
, const char *prefix
)
163 size_t len
= strlen(prefix
);
164 bool size_in_usecs
= false;
167 memcpy(buf
, prefix
, len
);
168 strcpy(buf
+ len
, "TRY_POLL");
169 get_bool(buf
, &apdo
->try_poll
, &apdo
->has_try_poll
);
171 strcpy(buf
+ len
, "DEV");
172 get_str(buf
, &apdo
->dev
, &apdo
->has_dev
);
174 strcpy(buf
+ len
, "SIZE_IN_USEC");
175 get_bool(buf
, &size_in_usecs
, &dummy
);
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(
182 qapi_AudiodevAlsaPerDirectionOptions_base(apdo
));
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(
190 qapi_AudiodevAlsaPerDirectionOptions_base(apdo
));
194 static void handle_alsa(Audiodev
*dev
)
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_");
200 get_millis_to_usecs("QEMU_ALSA_THRESHOLD",
201 &aopt
->threshold
, &aopt
->has_threshold
);
205 static void handle_coreaudio(Audiodev
*dev
)
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
);
218 static void handle_dsound(Audiodev
*dev
)
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
,
226 get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_IN",
227 &dev
->u
.dsound
.in
->buffer_length
,
228 &dev
->u
.dsound
.in
->has_buffer_length
,
233 static void handle_oss_per_direction(
234 AudiodevOssPerDirectionOptions
*opdo
, const char *try_poll_env
,
237 get_bool(try_poll_env
, &opdo
->try_poll
, &opdo
->has_try_poll
);
238 get_str(dev_env
, &opdo
->dev
, &opdo
->has_dev
);
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
);
247 static void handle_oss(Audiodev
*dev
)
249 AudiodevOssOptions
*oopt
= &dev
->u
.oss
;
250 handle_oss_per_direction(oopt
->in
, "QEMU_AUDIO_ADC_TRY_POLL",
252 handle_oss_per_direction(oopt
->out
, "QEMU_AUDIO_DAC_TRY_POLL",
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
);
261 static void handle_pa_per_direction(
262 AudiodevPaPerDirectionOptions
*ppdo
, const char *env
)
264 get_str(env
, &ppdo
->name
, &ppdo
->has_name
);
267 static void handle_pa(Audiodev
*dev
)
269 handle_pa_per_direction(dev
->u
.pa
.in
, "QEMU_PA_SOURCE");
270 handle_pa_per_direction(dev
->u
.pa
.out
, "QEMU_PA_SINK");
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
));
281 get_str("QEMU_PA_SERVER", &dev
->u
.pa
.server
, &dev
->u
.pa
.has_server
);
285 static void handle_sdl(Audiodev
*dev
)
287 /* SDL is output only */
288 get_samples_to_usecs("QEMU_SDL_SAMPLES", &dev
->u
.sdl
.out
->buffer_length
,
289 &dev
->u
.sdl
.out
->has_buffer_length
, dev
->u
.sdl
.out
);
293 static void handle_wav(Audiodev
*dev
)
295 get_int("QEMU_WAV_FREQUENCY",
296 &dev
->u
.wav
.out
->frequency
, &dev
->u
.wav
.out
->has_frequency
);
297 get_fmt("QEMU_WAV_FORMAT", &dev
->u
.wav
.out
->format
,
298 &dev
->u
.wav
.out
->has_format
);
299 get_int("QEMU_WAV_DAC_FIXED_CHANNELS",
300 &dev
->u
.wav
.out
->channels
, &dev
->u
.wav
.out
->has_channels
);
301 get_str("QEMU_WAV_PATH", &dev
->u
.wav
.path
, &dev
->u
.wav
.has_path
);
305 static void handle_per_direction(
306 AudiodevPerDirectionOptions
*pdo
, const char *prefix
)
309 size_t len
= strlen(prefix
);
311 memcpy(buf
, prefix
, len
);
312 strcpy(buf
+ len
, "FIXED_SETTINGS");
313 get_bool(buf
, &pdo
->fixed_settings
, &pdo
->has_fixed_settings
);
315 strcpy(buf
+ len
, "FIXED_FREQ");
316 get_int(buf
, &pdo
->frequency
, &pdo
->has_frequency
);
318 strcpy(buf
+ len
, "FIXED_FMT");
319 get_fmt(buf
, &pdo
->format
, &pdo
->has_format
);
321 strcpy(buf
+ len
, "FIXED_CHANNELS");
322 get_int(buf
, &pdo
->channels
, &pdo
->has_channels
);
324 strcpy(buf
+ len
, "VOICES");
325 get_int(buf
, &pdo
->voices
, &pdo
->has_voices
);
328 static AudiodevListEntry
*legacy_opt(const char *drvname
)
330 AudiodevListEntry
*e
= g_malloc0(sizeof(AudiodevListEntry
));
331 e
->dev
= g_malloc0(sizeof(Audiodev
));
332 e
->dev
->id
= g_strdup(drvname
);
333 e
->dev
->driver
= qapi_enum_parse(
334 &AudiodevDriver_lookup
, drvname
, -1, &error_abort
);
336 audio_create_pdos(e
->dev
);
338 handle_per_direction(audio_get_pdo_in(e
->dev
), "QEMU_AUDIO_ADC_");
339 handle_per_direction(audio_get_pdo_out(e
->dev
), "QEMU_AUDIO_DAC_");
341 /* Original description: Timer period in HZ (0 - use lowest possible) */
342 get_int("QEMU_AUDIO_TIMER_PERIOD",
343 &e
->dev
->timer_period
, &e
->dev
->has_timer_period
);
344 if (e
->dev
->has_timer_period
&& e
->dev
->timer_period
) {
345 e
->dev
->timer_period
= NANOSECONDS_PER_SECOND
/ 1000 /
346 e
->dev
->timer_period
;
349 switch (e
->dev
->driver
) {
350 case AUDIODEV_DRIVER_ALSA
:
354 case AUDIODEV_DRIVER_COREAUDIO
:
355 handle_coreaudio(e
->dev
);
358 case AUDIODEV_DRIVER_DSOUND
:
359 handle_dsound(e
->dev
);
362 case AUDIODEV_DRIVER_OSS
:
366 case AUDIODEV_DRIVER_PA
:
370 case AUDIODEV_DRIVER_SDL
:
374 case AUDIODEV_DRIVER_WAV
:
385 AudiodevListHead
audio_handle_legacy_opts(void)
387 const char *drvname
= getenv("QEMU_AUDIO_DRV");
388 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
391 AudiodevListEntry
*e
;
392 audio_driver
*driver
= audio_driver_lookup(drvname
);
394 dolog("Unknown audio driver `%s'\n", drvname
);
397 e
= legacy_opt(drvname
);
398 QSIMPLEQ_INSERT_TAIL(&head
, e
, next
);
400 for (int i
= 0; audio_prio_list
[i
]; i
++) {
401 audio_driver
*driver
= audio_driver_lookup(audio_prio_list
[i
]);
402 if (driver
&& driver
->can_be_default
) {
403 AudiodevListEntry
*e
= legacy_opt(driver
->name
);
404 QSIMPLEQ_INSERT_TAIL(&head
, e
, next
);
407 if (QSIMPLEQ_EMPTY(&head
)) {
408 dolog("Internal error: no default audio driver available\n");
416 /* visitor to print -audiodev option */
422 } LegacyPrintVisitor
;
424 static void lv_start_struct(Visitor
*v
, const char *name
, void **obj
,
425 size_t size
, Error
**errp
)
427 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
428 lv
->path
= g_list_append(lv
->path
, g_strdup(name
));
431 static void lv_end_struct(Visitor
*v
, void **obj
)
433 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
434 lv
->path
= g_list_delete_link(lv
->path
, g_list_last(lv
->path
));
437 static void lv_print_key(Visitor
*v
, const char *name
)
440 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
447 for (e
= lv
->path
; e
; e
= e
->next
) {
449 printf("%s.", (const char *) e
->data
);
456 static void lv_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
459 lv_print_key(v
, name
);
460 printf("%" PRIi64
, *obj
);
463 static void lv_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
466 lv_print_key(v
, name
);
467 printf("%" PRIu64
, *obj
);
470 static void lv_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
472 lv_print_key(v
, name
);
473 printf("%s", *obj
? "on" : "off");
476 static void lv_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
478 const char *str
= *obj
;
479 lv_print_key(v
, name
);
489 static void lv_complete(Visitor
*v
, void *opaque
)
491 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
492 assert(lv
->path
== NULL
);
495 static void lv_free(Visitor
*v
)
497 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
499 g_list_free_full(lv
->path
, g_free
);
503 static Visitor
*legacy_visitor_new(void)
505 LegacyPrintVisitor
*lv
= g_malloc0(sizeof(LegacyPrintVisitor
));
507 lv
->visitor
.start_struct
= lv_start_struct
;
508 lv
->visitor
.end_struct
= lv_end_struct
;
509 /* lists not supported */
510 lv
->visitor
.type_int64
= lv_type_int64
;
511 lv
->visitor
.type_uint64
= lv_type_uint64
;
512 lv
->visitor
.type_bool
= lv_type_bool
;
513 lv
->visitor
.type_str
= lv_type_str
;
515 lv
->visitor
.type
= VISITOR_OUTPUT
;
516 lv
->visitor
.complete
= lv_complete
;
517 lv
->visitor
.free
= lv_free
;
522 void audio_legacy_help(void)
524 AudiodevListHead head
;
525 AudiodevListEntry
*e
;
527 printf("Environment variable based configuration deprecated.\n");
528 printf("Please use the new -audiodev option.\n");
530 head
= audio_handle_legacy_opts();
531 printf("\nEquivalent -audiodev to your current environment variables:\n");
532 if (!getenv("QEMU_AUDIO_DRV")) {
533 printf("(Since you didn't specify QEMU_AUDIO_DRV, I'll list all "
537 QSIMPLEQ_FOREACH(e
, &head
, next
) {
539 Audiodev
*dev
= e
->dev
;
540 printf("-audiodev ");
542 v
= legacy_visitor_new();
543 visit_type_Audiodev(v
, NULL
, &dev
, &error_abort
);
548 audio_free_audiodev_list(&head
);