qemu/audio/audio_legacy.c
<<
>>
Prefs
   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 "qemu/timer.h"
  30#include "qapi/error.h"
  31#include "qapi/qapi-visit-audio.h"
  32#include "qapi/visitor-impl.h"
  33
  34#define AUDIO_CAP "audio-legacy"
  35#include "audio_int.h"
  36
  37static uint32_t toui32(const char *str)
  38{
  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);
  43    }
  44    return ret;
  45}
  46
  47/* helper functions to convert env variables */
  48static void get_bool(const char *env, bool *dst, bool *has_dst)
  49{
  50    const char *val = getenv(env);
  51    if (val) {
  52        *dst = toui32(val) != 0;
  53        *has_dst = true;
  54    }
  55}
  56
  57static void get_int(const char *env, uint32_t *dst, bool *has_dst)
  58{
  59    const char *val = getenv(env);
  60    if (val) {
  61        *dst = toui32(val);
  62        *has_dst = true;
  63    }
  64}
  65
  66static void get_str(const char *env, char **dst, bool *has_dst)
  67{
  68    const char *val = getenv(env);
  69    if (val) {
  70        if (*has_dst) {
  71            g_free(*dst);
  72        }
  73        *dst = g_strdup(val);
  74        *has_dst = true;
  75    }
  76}
  77
  78static void get_fmt(const char *env, AudioFormat *dst, bool *has_dst)
  79{
  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;
  88            }
  89        }
  90
  91        dolog("Invalid audio format `%s'\n", val);
  92        exit(1);
  93    }
  94}
  95
  96
  97static void get_millis_to_usecs(const char *env, uint32_t *dst, bool *has_dst)
  98{
  99    const char *val = getenv(env);
 100    if (val) {
 101        *dst = toui32(val) * 1000;
 102        *has_dst = true;
 103    }
 104}
 105
 106static uint32_t frames_to_usecs(uint32_t frames,
 107                                AudiodevPerDirectionOptions *pdo)
 108{
 109    uint32_t freq = pdo->has_frequency ? pdo->frequency : 44100;
 110    return (frames * 1000000 + freq / 2) / freq;
 111}
 112
 113
 114static void get_frames_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
 115                                AudiodevPerDirectionOptions *pdo)
 116{
 117    const char *val = getenv(env);
 118    if (val) {
 119        *dst = frames_to_usecs(toui32(val), pdo);
 120        *has_dst = true;
 121    }
 122}
 123
 124static uint32_t samples_to_usecs(uint32_t samples,
 125                                 AudiodevPerDirectionOptions *pdo)
 126{
 127    uint32_t channels = pdo->has_channels ? pdo->channels : 2;
 128    return frames_to_usecs(samples / channels, pdo);
 129}
 130
 131static void get_samples_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
 132                                 AudiodevPerDirectionOptions *pdo)
 133{
 134    const char *val = getenv(env);
 135    if (val) {
 136        *dst = samples_to_usecs(toui32(val), pdo);
 137        *has_dst = true;
 138    }
 139}
 140
 141static uint32_t bytes_to_usecs(uint32_t bytes, AudiodevPerDirectionOptions *pdo)
 142{
 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);
 146}
 147
 148static void get_bytes_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
 149                               AudiodevPerDirectionOptions *pdo)
 150{
 151    const char *val = getenv(env);
 152    if (val) {
 153        *dst = bytes_to_usecs(toui32(val), pdo);
 154        *has_dst = true;
 155    }
 156}
 157
 158/* backend specific functions */
 159/* ALSA */
 160static void handle_alsa_per_direction(
 161    AudiodevAlsaPerDirectionOptions *apdo, const char *prefix)
 162{
 163    char buf[64];
 164    size_t len = strlen(prefix);
 165    bool size_in_usecs = false;
 166    bool dummy;
 167
 168    memcpy(buf, prefix, len);
 169    strcpy(buf + len, "TRY_POLL");
 170    get_bool(buf, &apdo->try_poll, &apdo->has_try_poll);
 171
 172    strcpy(buf + len, "DEV");
 173    get_str(buf, &apdo->dev, &apdo->has_dev);
 174
 175    strcpy(buf + len, "SIZE_IN_USEC");
 176    get_bool(buf, &size_in_usecs, &dummy);
 177
 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));
 184    }
 185
 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));
 192    }
 193}
 194
 195static void handle_alsa(Audiodev *dev)
 196{
 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_");
 200
 201    get_millis_to_usecs("QEMU_ALSA_THRESHOLD",
 202                        &aopt->threshold, &aopt->has_threshold);
 203}
 204
 205/* coreaudio */
 206static void handle_coreaudio(Audiodev *dev)
 207{
 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);
 216}
 217
 218/* dsound */
 219static void handle_dsound(Audiodev *dev)
 220{
 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);
 231}
 232
 233/* OSS */
 234static void handle_oss_per_direction(
 235    AudiodevOssPerDirectionOptions *opdo, const char *try_poll_env,
 236    const char *dev_env)
 237{
 238    get_bool(try_poll_env, &opdo->try_poll, &opdo->has_try_poll);
 239    get_str(dev_env, &opdo->dev, &opdo->has_dev);
 240
 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);
 246}
 247
 248static void handle_oss(Audiodev *dev)
 249{
 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");
 255
 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);
 259}
 260
 261/* pulseaudio */
 262static void handle_pa_per_direction(
 263    AudiodevPaPerDirectionOptions *ppdo, const char *env)
 264{
 265    get_str(env, &ppdo->name, &ppdo->has_name);
 266}
 267
 268static void handle_pa(Audiodev *dev)
 269{
 270    handle_pa_per_direction(dev->u.pa.in, "QEMU_PA_SOURCE");
 271    handle_pa_per_direction(dev->u.pa.out, "QEMU_PA_SINK");
 272
 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));
 281
 282    get_str("QEMU_PA_SERVER", &dev->u.pa.server, &dev->u.pa.has_server);
 283}
 284
 285/* SDL */
 286static void handle_sdl(Audiodev *dev)
 287{
 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);
 291}
 292
 293/* wav */
 294static void handle_wav(Audiodev *dev)
 295{
 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);
 303}
 304
 305/* general */
 306static void handle_per_direction(
 307    AudiodevPerDirectionOptions *pdo, const char *prefix)
 308{
 309    char buf[64];
 310    size_t len = strlen(prefix);
 311
 312    memcpy(buf, prefix, len);
 313    strcpy(buf + len, "FIXED_SETTINGS");
 314    get_bool(buf, &pdo->fixed_settings, &pdo->has_fixed_settings);
 315
 316    strcpy(buf + len, "FIXED_FREQ");
 317    get_int(buf, &pdo->frequency, &pdo->has_frequency);
 318
 319    strcpy(buf + len, "FIXED_FMT");
 320    get_fmt(buf, &pdo->format, &pdo->has_format);
 321
 322    strcpy(buf + len, "FIXED_CHANNELS");
 323    get_int(buf, &pdo->channels, &pdo->has_channels);
 324
 325    strcpy(buf + len, "VOICES");
 326    get_int(buf, &pdo->voices, &pdo->has_voices);
 327}
 328
 329static AudiodevListEntry *legacy_opt(const char *drvname)
 330{
 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);
 336
 337    audio_create_pdos(e->dev);
 338
 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_");
 341
 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;
 348    }
 349
 350    switch (e->dev->driver) {
 351    case AUDIODEV_DRIVER_ALSA:
 352        handle_alsa(e->dev);
 353        break;
 354
 355    case AUDIODEV_DRIVER_COREAUDIO:
 356        handle_coreaudio(e->dev);
 357        break;
 358
 359    case AUDIODEV_DRIVER_DSOUND:
 360        handle_dsound(e->dev);
 361        break;
 362
 363    case AUDIODEV_DRIVER_OSS:
 364        handle_oss(e->dev);
 365        break;
 366
 367    case AUDIODEV_DRIVER_PA:
 368        handle_pa(e->dev);
 369        break;
 370
 371    case AUDIODEV_DRIVER_SDL:
 372        handle_sdl(e->dev);
 373        break;
 374
 375    case AUDIODEV_DRIVER_WAV:
 376        handle_wav(e->dev);
 377        break;
 378
 379    default:
 380        break;
 381    }
 382
 383    return e;
 384}
 385
 386AudiodevListHead audio_handle_legacy_opts(void)
 387{
 388    const char *drvname = getenv("QEMU_AUDIO_DRV");
 389    AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
 390
 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);
 397        }
 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);
 406            }
 407        }
 408        if (QSIMPLEQ_EMPTY(&head)) {
 409            dolog("Internal error: no default audio driver available\n");
 410            exit(1);
 411        }
 412    }
 413
 414    return head;
 415}
 416
 417/* visitor to print -audiodev option */
 418typedef struct {
 419    Visitor visitor;
 420
 421    bool comma;
 422    GList *path;
 423} LegacyPrintVisitor;
 424
 425static void lv_start_struct(Visitor *v, const char *name, void **obj,
 426                            size_t size, Error **errp)
 427{
 428    LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
 429    lv->path = g_list_append(lv->path, g_strdup(name));
 430}
 431
 432static void lv_end_struct(Visitor *v, void **obj)
 433{
 434    LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
 435    lv->path = g_list_delete_link(lv->path, g_list_last(lv->path));
 436}
 437
 438static void lv_print_key(Visitor *v, const char *name)
 439{
 440    GList *e;
 441    LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
 442    if (lv->comma) {
 443        putchar(',');
 444    } else {
 445        lv->comma = true;
 446    }
 447
 448    for (e = lv->path; e; e = e->next) {
 449        if (e->data) {
 450            printf("%s.", (const char *) e->data);
 451        }
 452    }
 453
 454    printf("%s=", name);
 455}
 456
 457static void lv_type_int64(Visitor *v, const char *name, int64_t *obj,
 458                          Error **errp)
 459{
 460    lv_print_key(v, name);
 461    printf("%" PRIi64, *obj);
 462}
 463
 464static void lv_type_uint64(Visitor *v, const char *name, uint64_t *obj,
 465                           Error **errp)
 466{
 467    lv_print_key(v, name);
 468    printf("%" PRIu64, *obj);
 469}
 470
 471static void lv_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
 472{
 473    lv_print_key(v, name);
 474    printf("%s", *obj ? "on" : "off");
 475}
 476
 477static void lv_type_str(Visitor *v, const char *name, char **obj, Error **errp)
 478{
 479    const char *str = *obj;
 480    lv_print_key(v, name);
 481
 482    while (*str) {
 483        if (*str == ',') {
 484            putchar(',');
 485        }
 486        putchar(*str++);
 487    }
 488}
 489
 490static void lv_complete(Visitor *v, void *opaque)
 491{
 492    LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
 493    assert(lv->path == NULL);
 494}
 495
 496static void lv_free(Visitor *v)
 497{
 498    LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
 499
 500    g_list_free_full(lv->path, g_free);
 501    g_free(lv);
 502}
 503
 504static Visitor *legacy_visitor_new(void)
 505{
 506    LegacyPrintVisitor *lv = g_malloc0(sizeof(LegacyPrintVisitor));
 507
 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;
 515
 516    lv->visitor.type = VISITOR_OUTPUT;
 517    lv->visitor.complete = lv_complete;
 518    lv->visitor.free = lv_free;
 519
 520    return &lv->visitor;
 521}
 522
 523void audio_legacy_help(void)
 524{
 525    AudiodevListHead head;
 526    AudiodevListEntry *e;
 527
 528    printf("Environment variable based configuration deprecated.\n");
 529    printf("Please use the new -audiodev option.\n");
 530
 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");
 536    }
 537
 538    QSIMPLEQ_FOREACH(e, &head, next) {
 539        Visitor *v;
 540        Audiodev *dev = e->dev;
 541        printf("-audiodev ");
 542
 543        v = legacy_visitor_new();
 544        visit_type_Audiodev(v, NULL, &dev, &error_abort);
 545        visit_free(v);
 546
 547        printf("\n");
 548    }
 549    audio_free_audiodev_list(&head);
 550}
 551