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-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"
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 uint32_t bytes_to_usecs(uint32_t bytes
, AudiodevPerDirectionOptions
*pdo
)
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
);
137 static void get_bytes_to_usecs(const char *env
, uint32_t *dst
, bool *has_dst
,
138 AudiodevPerDirectionOptions
*pdo
)
140 const char *val
= getenv(env
);
142 *dst
= bytes_to_usecs(toui32(val
), pdo
);
147 /* backend specific functions */
149 static void handle_alsa_per_direction(
150 AudiodevAlsaPerDirectionOptions
*apdo
, const char *prefix
)
153 size_t len
= strlen(prefix
);
154 bool size_in_usecs
= false;
157 memcpy(buf
, prefix
, len
);
158 strcpy(buf
+ len
, "TRY_POLL");
159 get_bool(buf
, &apdo
->try_poll
, &apdo
->has_try_poll
);
161 strcpy(buf
+ len
, "DEV");
162 get_str(buf
, &apdo
->dev
, &apdo
->has_dev
);
164 strcpy(buf
+ len
, "SIZE_IN_USEC");
165 get_bool(buf
, &size_in_usecs
, &dummy
);
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(
172 qapi_AudiodevAlsaPerDirectionOptions_base(apdo
));
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(
180 qapi_AudiodevAlsaPerDirectionOptions_base(apdo
));
184 static void handle_alsa(Audiodev
*dev
)
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_");
190 get_millis_to_usecs("QEMU_ALSA_THRESHOLD",
191 &aopt
->threshold
, &aopt
->has_threshold
);
195 static void handle_coreaudio(Audiodev
*dev
)
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
);
208 static void handle_dsound(Audiodev
*dev
)
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
,
216 get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_IN",
217 &dev
->u
.dsound
.in
->buffer_length
,
218 &dev
->u
.dsound
.in
->has_buffer_length
,
223 static void handle_per_direction(
224 AudiodevPerDirectionOptions
*pdo
, const char *prefix
)
227 size_t len
= strlen(prefix
);
229 memcpy(buf
, prefix
, len
);
230 strcpy(buf
+ len
, "FIXED_SETTINGS");
231 get_bool(buf
, &pdo
->fixed_settings
, &pdo
->has_fixed_settings
);
233 strcpy(buf
+ len
, "FIXED_FREQ");
234 get_int(buf
, &pdo
->frequency
, &pdo
->has_frequency
);
236 strcpy(buf
+ len
, "FIXED_FMT");
237 get_fmt(buf
, &pdo
->format
, &pdo
->has_format
);
239 strcpy(buf
+ len
, "FIXED_CHANNELS");
240 get_int(buf
, &pdo
->channels
, &pdo
->has_channels
);
242 strcpy(buf
+ len
, "VOICES");
243 get_int(buf
, &pdo
->voices
, &pdo
->has_voices
);
246 static AudiodevListEntry
*legacy_opt(const char *drvname
)
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
);
254 audio_create_pdos(e
->dev
);
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_");
259 get_int("QEMU_AUDIO_TIMER_PERIOD",
260 &e
->dev
->timer_period
, &e
->dev
->has_timer_period
);
262 switch (e
->dev
->driver
) {
263 case AUDIODEV_DRIVER_ALSA
:
267 case AUDIODEV_DRIVER_COREAUDIO
:
268 handle_coreaudio(e
->dev
);
271 case AUDIODEV_DRIVER_DSOUND
:
272 handle_dsound(e
->dev
);
282 AudiodevListHead
audio_handle_legacy_opts(void)
284 const char *drvname
= getenv("QEMU_AUDIO_DRV");
285 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
288 AudiodevListEntry
*e
;
289 audio_driver
*driver
= audio_driver_lookup(drvname
);
291 dolog("Unknown audio driver `%s'\n", drvname
);
294 e
= legacy_opt(drvname
);
295 QSIMPLEQ_INSERT_TAIL(&head
, e
, next
);
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
);
304 if (QSIMPLEQ_EMPTY(&head
)) {
305 dolog("Internal error: no default audio driver available\n");
313 /* visitor to print -audiodev option */
319 } LegacyPrintVisitor
;
321 static void lv_start_struct(Visitor
*v
, const char *name
, void **obj
,
322 size_t size
, Error
**errp
)
324 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
325 lv
->path
= g_list_append(lv
->path
, g_strdup(name
));
328 static void lv_end_struct(Visitor
*v
, void **obj
)
330 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
331 lv
->path
= g_list_delete_link(lv
->path
, g_list_last(lv
->path
));
334 static void lv_print_key(Visitor
*v
, const char *name
)
337 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
344 for (e
= lv
->path
; e
; e
= e
->next
) {
346 printf("%s.", (const char *) e
->data
);
353 static void lv_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
356 lv_print_key(v
, name
);
357 printf("%" PRIi64
, *obj
);
360 static void lv_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
363 lv_print_key(v
, name
);
364 printf("%" PRIu64
, *obj
);
367 static void lv_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
369 lv_print_key(v
, name
);
370 printf("%s", *obj
? "on" : "off");
373 static void lv_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
375 const char *str
= *obj
;
376 lv_print_key(v
, name
);
386 static void lv_complete(Visitor
*v
, void *opaque
)
388 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
389 assert(lv
->path
== NULL
);
392 static void lv_free(Visitor
*v
)
394 LegacyPrintVisitor
*lv
= (LegacyPrintVisitor
*) v
;
396 g_list_free_full(lv
->path
, g_free
);
400 static Visitor
*legacy_visitor_new(void)
402 LegacyPrintVisitor
*lv
= g_malloc0(sizeof(LegacyPrintVisitor
));
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
;
412 lv
->visitor
.type
= VISITOR_OUTPUT
;
413 lv
->visitor
.complete
= lv_complete
;
414 lv
->visitor
.free
= lv_free
;
419 void audio_legacy_help(void)
421 AudiodevListHead head
;
422 AudiodevListEntry
*e
;
424 printf("Environment variable based configuration deprecated.\n");
425 printf("Please use the new -audiodev option.\n");
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 "
434 QSIMPLEQ_FOREACH(e
, &head
, next
) {
436 Audiodev
*dev
= e
->dev
;
437 printf("-audiodev ");
439 v
= legacy_visitor_new();
440 visit_type_Audiodev(v
, NULL
, &dev
, &error_abort
);
445 audio_free_audiodev_list(&head
);