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
)
67 const char *val
= getenv(env
);
74 static void get_fmt(const char *env
, AudioFormat
*dst
, bool *has_dst
)
76 const char *val
= getenv(env
);
79 for (i
= 0; AudioFormat_lookup
.size
; ++i
) {
80 if (strcasecmp(val
, AudioFormat_lookup
.array
[i
]) == 0) {
87 dolog("Invalid audio format `%s'\n", val
);
93 static void get_millis_to_usecs(const char *env
, uint32_t *dst
, bool *has_dst
)
95 const char *val
= getenv(env
);
97 *dst
= toui32(val
) * 1000;
102 static uint32_t frames_to_usecs(uint32_t frames
,
103 AudiodevPerDirectionOptions
*pdo
)
105 uint32_t freq
= pdo
->has_frequency
? pdo
->frequency
: 44100;
106 return (frames
* 1000000 + freq
/ 2) / freq
;
110 static void get_frames_to_usecs(const char *env
, uint32_t *dst
, bool *has_dst
,
111 AudiodevPerDirectionOptions
*pdo
)
113 const char *val
= getenv(env
);
115 *dst
= frames_to_usecs(toui32(val
), pdo
);
120 static uint32_t samples_to_usecs(uint32_t samples
,
121 AudiodevPerDirectionOptions
*pdo
)
123 uint32_t channels
= pdo
->has_channels
? pdo
->channels
: 2;
124 return frames_to_usecs(samples
/ channels
, pdo
);
127 static void get_samples_to_usecs(const char *env
, uint32_t *dst
, bool *has_dst
,
128 AudiodevPerDirectionOptions
*pdo
)
130 const char *val
= getenv(env
);
132 *dst
= samples_to_usecs(toui32(val
), pdo
);
137 static uint32_t bytes_to_usecs(uint32_t bytes
, AudiodevPerDirectionOptions
*pdo
)
139 AudioFormat fmt
= pdo
->has_format
? pdo
->format
: AUDIO_FORMAT_S16
;
140 uint32_t bytes_per_sample
= audioformat_bytes_per_sample(fmt
);
141 return samples_to_usecs(bytes
/ bytes_per_sample
, pdo
);
144 static void get_bytes_to_usecs(const char *env
, uint32_t *dst
, bool *has_dst
,
145 AudiodevPerDirectionOptions
*pdo
)
147 const char *val
= getenv(env
);
149 *dst
= bytes_to_usecs(toui32(val
), pdo
);
154 /* backend specific functions */
156 static void handle_alsa_per_direction(
157 AudiodevAlsaPerDirectionOptions
*apdo
, const char *prefix
)
160 size_t len
= strlen(prefix
);
161 bool size_in_usecs
= false;
164 memcpy(buf
, prefix
, len
);
165 strcpy(buf
+ len
, "TRY_POLL");
166 get_bool(buf
, &apdo
->try_poll
, &apdo
->has_try_poll
);
168 strcpy(buf
+ len
, "DEV");
169 get_str(buf
, &apdo
->dev
);
171 strcpy(buf
+ len
, "SIZE_IN_USEC");
172 get_bool(buf
, &size_in_usecs
, &dummy
);
174 strcpy(buf
+ len
, "PERIOD_SIZE");
175 get_int(buf
, &apdo
->period_length
, &apdo
->has_period_length
);
176 if (apdo
->has_period_length
&& !size_in_usecs
) {
177 apdo
->period_length
= frames_to_usecs(
179 qapi_AudiodevAlsaPerDirectionOptions_base(apdo
));
182 strcpy(buf
+ len
, "BUFFER_SIZE");
183 get_int(buf
, &apdo
->buffer_length
, &apdo
->has_buffer_length
);
184 if (apdo
->has_buffer_length
&& !size_in_usecs
) {
185 apdo
->buffer_length
= frames_to_usecs(
187 qapi_AudiodevAlsaPerDirectionOptions_base(apdo
));
191 static void handle_alsa(Audiodev
*dev
)
193 AudiodevAlsaOptions
*aopt
= &dev
->u
.alsa
;
194 handle_alsa_per_direction(aopt
->in
, "QEMU_ALSA_ADC_");
195 handle_alsa_per_direction(aopt
->out
, "QEMU_ALSA_DAC_");
197 get_millis_to_usecs("QEMU_ALSA_THRESHOLD",
198 &aopt
->threshold
, &aopt
->has_threshold
);
202 static void handle_coreaudio(Audiodev
*dev
)
205 "QEMU_COREAUDIO_BUFFER_SIZE",
206 &dev
->u
.coreaudio
.out
->buffer_length
,
207 &dev
->u
.coreaudio
.out
->has_buffer_length
,
208 qapi_AudiodevCoreaudioPerDirectionOptions_base(dev
->u
.coreaudio
.out
));
209 get_int("QEMU_COREAUDIO_BUFFER_COUNT",
210 &dev
->u
.coreaudio
.out
->buffer_count
,
211 &dev
->u
.coreaudio
.out
->has_buffer_count
);
215 static void handle_dsound(Audiodev
*dev
)
217 get_millis_to_usecs("QEMU_DSOUND_LATENCY_MILLIS",
218 &dev
->u
.dsound
.latency
, &dev
->u
.dsound
.has_latency
);
219 get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_OUT",
220 &dev
->u
.dsound
.out
->buffer_length
,
221 &dev
->u
.dsound
.out
->has_buffer_length
,
223 get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_IN",
224 &dev
->u
.dsound
.in
->buffer_length
,
225 &dev
->u
.dsound
.in
->has_buffer_length
,
230 static void handle_oss_per_direction(
231 AudiodevOssPerDirectionOptions
*opdo
, const char *try_poll_env
,
234 get_bool(try_poll_env
, &opdo
->try_poll
, &opdo
->has_try_poll
);
235 get_str(dev_env
, &opdo
->dev
);
237 get_bytes_to_usecs("QEMU_OSS_FRAGSIZE",
238 &opdo
->buffer_length
, &opdo
->has_buffer_length
,
239 qapi_AudiodevOssPerDirectionOptions_base(opdo
));
240 get_int("QEMU_OSS_NFRAGS", &opdo
->buffer_count
,
241 &opdo
->has_buffer_count
);
244 static void handle_oss(Audiodev
*dev
)
246 AudiodevOssOptions
*oopt
= &dev
->u
.oss
;
247 handle_oss_per_direction(oopt
->in
, "QEMU_AUDIO_ADC_TRY_POLL",
249 handle_oss_per_direction(oopt
->out
, "QEMU_AUDIO_DAC_TRY_POLL",
252 get_bool("QEMU_OSS_MMAP", &oopt
->try_mmap
, &oopt
->has_try_mmap
);
253 get_bool("QEMU_OSS_EXCLUSIVE", &oopt
->exclusive
, &oopt
->has_exclusive
);
254 get_int("QEMU_OSS_POLICY", &oopt
->dsp_policy
, &oopt
->has_dsp_policy
);
258 static void handle_pa_per_direction(
259 AudiodevPaPerDirectionOptions
*ppdo
, const char *env
)
261 get_str(env
, &ppdo
->name
);
264 static void handle_pa(Audiodev
*dev
)
266 handle_pa_per_direction(dev
->u
.pa
.in
, "QEMU_PA_SOURCE");
267 handle_pa_per_direction(dev
->u
.pa
.out
, "QEMU_PA_SINK");
269 get_samples_to_usecs(
270 "QEMU_PA_SAMPLES", &dev
->u
.pa
.in
->buffer_length
,
271 &dev
->u
.pa
.in
->has_buffer_length
,
272 qapi_AudiodevPaPerDirectionOptions_base(dev
->u
.pa
.in
));
273 get_samples_to_usecs(
274 "QEMU_PA_SAMPLES", &dev
->u
.pa
.out
->buffer_length
,
275 &dev
->u
.pa
.out
->has_buffer_length
,
276 qapi_AudiodevPaPerDirectionOptions_base(dev
->u
.pa
.out
));
278 get_str("QEMU_PA_SERVER", &dev
->u
.pa
.server
);
282 static void handle_sdl(Audiodev
*dev
)
284 /* SDL is output only */
285 get_samples_to_usecs("QEMU_SDL_SAMPLES", &dev
->u
.sdl
.out
->buffer_length
,
286 &dev
->u
.sdl
.out
->has_buffer_length
,
287 qapi_AudiodevSdlPerDirectionOptions_base(dev
->u
.sdl
.out
));
291 static void handle_wav(Audiodev
*dev
)
293 get_int("QEMU_WAV_FREQUENCY",
294 &dev
->u
.wav
.out
->frequency
, &dev
->u
.wav
.out
->has_frequency
);
295 get_fmt("QEMU_WAV_FORMAT", &dev
->u
.wav
.out
->format
,
296 &dev
->u
.wav
.out
->has_format
);
297 get_int("QEMU_WAV_DAC_FIXED_CHANNELS",
298 &dev
->u
.wav
.out
->channels
, &dev
->u
.wav
.out
->has_channels
);
299 get_str("QEMU_WAV_PATH", &dev
->u
.wav
.path
);
303 static void handle_per_direction(
304 AudiodevPerDirectionOptions
*pdo
, const char *prefix
)
307 size_t len
= strlen(prefix
);
309 memcpy(buf
, prefix
, len
);
310 strcpy(buf
+ len
, "FIXED_SETTINGS");
311 get_bool(buf
, &pdo
->fixed_settings
, &pdo
->has_fixed_settings
);
313 strcpy(buf
+ len
, "FIXED_FREQ");
314 get_int(buf
, &pdo
->frequency
, &pdo
->has_frequency
);
316 strcpy(buf
+ len
, "FIXED_FMT");
317 get_fmt(buf
, &pdo
->format
, &pdo
->has_format
);
319 strcpy(buf
+ len
, "FIXED_CHANNELS");
320 get_int(buf
, &pdo
->channels
, &pdo
->has_channels
);
322 strcpy(buf
+ len
, "VOICES");
323 get_int(buf
, &pdo
->voices
, &pdo
->has_voices
);
326 static AudiodevListEntry
*legacy_opt(const char *drvname
)
328 AudiodevListEntry
*e
= g_new0(AudiodevListEntry
, 1);
329 e
->dev
= g_new0(Audiodev
, 1);
330 e
->dev
->id
= g_strdup(drvname
);
331 e
->dev
->driver
= qapi_enum_parse(
332 &AudiodevDriver_lookup
, drvname
, -1, &error_abort
);
334 audio_create_pdos(e
->dev
);
336 handle_per_direction(audio_get_pdo_in(e
->dev
), "QEMU_AUDIO_ADC_");
337 handle_per_direction(audio_get_pdo_out(e
->dev
), "QEMU_AUDIO_DAC_");
339 /* Original description: Timer period in HZ (0 - use lowest possible) */
340 get_int("QEMU_AUDIO_TIMER_PERIOD",
341 &e
->dev
->timer_period
, &e
->dev
->has_timer_period
);
342 if (e
->dev
->has_timer_period
&& e
->dev
->timer_period
) {
343 e
->dev
->timer_period
= NANOSECONDS_PER_SECOND
/ 1000 /
344 e
->dev
->timer_period
;
347 switch (e
->dev
->driver
) {
348 case AUDIODEV_DRIVER_ALSA
:
352 case AUDIODEV_DRIVER_COREAUDIO
:
353 handle_coreaudio(e
->dev
);
356 case AUDIODEV_DRIVER_DSOUND
:
357 handle_dsound(e
->dev
);
360 case AUDIODEV_DRIVER_OSS
:
364 case AUDIODEV_DRIVER_PA
:
368 case AUDIODEV_DRIVER_SDL
:
372 case AUDIODEV_DRIVER_WAV
:
383 AudiodevListHead
audio_handle_legacy_opts(void)
385 const char *drvname
= getenv("QEMU_AUDIO_DRV");
386 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
389 AudiodevListEntry
*e
;
390 audio_driver
*driver
= audio_driver_lookup(drvname
);
392 dolog("Unknown audio driver `%s'\n", drvname
);
395 e
= legacy_opt(drvname
);
396 QSIMPLEQ_INSERT_TAIL(&head
, e
, next
);
398 for (int i
= 0; audio_prio_list
[i
]; i
++) {
399 audio_driver
*driver
= audio_driver_lookup(audio_prio_list
[i
]);
400 if (driver
&& driver
->can_be_default
) {
401 AudiodevListEntry
*e
= legacy_opt(driver
->name
);
402 QSIMPLEQ_INSERT_TAIL(&head
, e
, next
);
405 if (QSIMPLEQ_EMPTY(&head
)) {
406 dolog("Internal error: no default audio driver available\n");
414 /* visitor to print -audiodev option */
420 } LegacyPrintVisitor
;
422 static bool lv_start_struct(Visitor
*v
, const char *name
, void **obj
,
423 size_t size
, Error
**errp
)
425 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
426 lv
->path
= g_list_append(lv
->path
, g_strdup(name
));
430 static void lv_end_struct(Visitor
*v
, void **obj
)
432 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
433 lv
->path
= g_list_delete_link(lv
->path
, g_list_last(lv
->path
));
436 static void lv_print_key(Visitor
*v
, const char *name
)
439 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
446 for (e
= lv
->path
; e
; e
= e
->next
) {
448 printf("%s.", (const char *) e
->data
);
455 static bool lv_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
458 lv_print_key(v
, name
);
459 printf("%" PRIi64
, *obj
);
463 static bool lv_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
466 lv_print_key(v
, name
);
467 printf("%" PRIu64
, *obj
);
471 static bool lv_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
473 lv_print_key(v
, name
);
474 printf("%s", *obj
? "on" : "off");
478 static bool lv_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
480 const char *str
= *obj
;
481 lv_print_key(v
, name
);
492 static void lv_complete(Visitor
*v
, void *opaque
)
494 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
495 assert(lv
->path
== NULL
);
498 static void lv_free(Visitor
*v
)
500 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
502 g_list_free_full(lv
->path
, g_free
);
506 static Visitor
*legacy_visitor_new(void)
508 LegacyPrintVisitor
*lv
= g_new0(LegacyPrintVisitor
, 1);
510 lv
->visitor
.start_struct
= lv_start_struct
;
511 lv
->visitor
.end_struct
= lv_end_struct
;
512 /* lists not supported */
513 lv
->visitor
.type_int64
= lv_type_int64
;
514 lv
->visitor
.type_uint64
= lv_type_uint64
;
515 lv
->visitor
.type_bool
= lv_type_bool
;
516 lv
->visitor
.type_str
= lv_type_str
;
518 lv
->visitor
.type
= VISITOR_OUTPUT
;
519 lv
->visitor
.complete
= lv_complete
;
520 lv
->visitor
.free
= lv_free
;
525 void audio_legacy_help(void)
527 AudiodevListHead head
;
528 AudiodevListEntry
*e
;
530 printf("Environment variable based configuration deprecated.\n");
531 printf("Please use the new -audiodev option.\n");
533 head
= audio_handle_legacy_opts();
534 printf("\nEquivalent -audiodev to your current environment variables:\n");
535 if (!getenv("QEMU_AUDIO_DRV")) {
536 printf("(Since you didn't specify QEMU_AUDIO_DRV, I'll list all "
540 QSIMPLEQ_FOREACH(e
, &head
, next
) {
542 Audiodev
*dev
= e
->dev
;
543 printf("-audiodev ");
545 v
= legacy_visitor_new();
546 visit_type_Audiodev(v
, NULL
, &dev
, &error_abort
);
551 audio_free_audiodev_list(&head
);