1 /* 2 * QEMU Audio subsystem header 3 * 4 * Copyright (c) 2005 Vassili Karpov (malc) 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 25 #ifdef DAC 26 #define NAME "playback" 27 #define HWBUF hw->mix_buf 28 #define TYPE out 29 #define HW HWVoiceOut 30 #define SW SWVoiceOut 31 #else 32 #define NAME "capture" 33 #define TYPE in 34 #define HW HWVoiceIn 35 #define SW SWVoiceIn 36 #define HWBUF hw->conv_buf 37 #endif 38 39 static void glue(audio_init_nb_voices_, TYPE)(AudioState *s, 40 struct audio_driver *drv, int min_voices) 41 { 42 int max_voices = glue (drv->max_voices_, TYPE); 43 size_t voice_size = glue(drv->voice_size_, TYPE); 44 45 glue (s->nb_hw_voices_, TYPE) = glue(audio_get_pdo_, TYPE)(s->dev)->voices; 46 if (glue (s->nb_hw_voices_, TYPE) > max_voices) { 47 if (!max_voices) { 48 #ifdef DAC 49 dolog ("Driver `%s' does not support " NAME "\n", drv->name); 50 #endif 51 } else { 52 dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n", 53 drv->name, 54 glue (s->nb_hw_voices_, TYPE), 55 max_voices); 56 } 57 glue (s->nb_hw_voices_, TYPE) = max_voices; 58 } 59 60 if (glue (s->nb_hw_voices_, TYPE) < min_voices) { 61 dolog ("Bogus number of " NAME " voices %d, setting to %d\n", 62 glue (s->nb_hw_voices_, TYPE), 63 min_voices); 64 } 65 66 if (audio_bug(__func__, !voice_size && max_voices)) { 67 dolog ("drv=`%s' voice_size=0 max_voices=%d\n", 68 drv->name, max_voices); 69 glue (s->nb_hw_voices_, TYPE) = 0; 70 } 71 72 if (audio_bug(__func__, voice_size && !max_voices)) { 73 dolog("drv=`%s' voice_size=%zu max_voices=0\n", 74 drv->name, voice_size); 75 } 76 } 77 78 static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw) 79 { 80 g_free(hw->buf_emul); 81 g_free(HWBUF.buffer); 82 HWBUF.buffer = NULL; 83 HWBUF.size = 0; 84 } 85 86 static void glue(audio_pcm_hw_alloc_resources_, TYPE)(HW *hw) 87 { 88 if (glue(audio_get_pdo_, TYPE)(hw->s->dev)->mixing_engine) { 89 size_t samples = hw->samples; 90 if (audio_bug(__func__, samples == 0)) { 91 dolog("Attempted to allocate empty buffer\n"); 92 } 93 94 HWBUF.buffer = g_new0(st_sample, samples); 95 HWBUF.size = samples; 96 HWBUF.pos = 0; 97 } else { 98 HWBUF.buffer = NULL; 99 HWBUF.size = 0; 100 } 101 } 102 103 static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw) 104 { 105 g_free(sw->resample_buf.buffer); 106 sw->resample_buf.buffer = NULL; 107 sw->resample_buf.size = 0; 108 109 if (sw->rate) { 110 st_rate_stop (sw->rate); 111 } 112 sw->rate = NULL; 113 } 114 115 static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw) 116 { 117 HW *hw = sw->hw; 118 uint64_t samples; 119 120 if (!glue(audio_get_pdo_, TYPE)(sw->s->dev)->mixing_engine) { 121 return 0; 122 } 123 124 samples = muldiv64(HWBUF.size, sw->info.freq, hw->info.freq); 125 if (samples == 0) { 126 uint64_t f_fe_min; 127 uint64_t f_be = (uint32_t)hw->info.freq; 128 129 /* f_fe_min = ceil(1 [frames] * f_be [Hz] / size_be [frames]) */ 130 f_fe_min = (f_be + HWBUF.size - 1) / HWBUF.size; 131 qemu_log_mask(LOG_UNIMP, 132 AUDIO_CAP ": The guest selected a " NAME " sample rate" 133 " of %d Hz for %s. Only sample rates >= %" PRIu64 " Hz" 134 " are supported.\n", 135 sw->info.freq, sw->name, f_fe_min); 136 return -1; 137 } 138 139 /* 140 * Allocate one additional audio frame that is needed for upsampling 141 * if the resample buffer size is small. For large buffer sizes take 142 * care of overflows and truncation. 143 */ 144 samples = samples < SIZE_MAX ? samples + 1 : SIZE_MAX; 145 sw->resample_buf.buffer = g_new0(st_sample, samples); 146 sw->resample_buf.size = samples; 147 sw->resample_buf.pos = 0; 148 149 #ifdef DAC 150 sw->rate = st_rate_start(sw->info.freq, hw->info.freq); 151 #else 152 sw->rate = st_rate_start(hw->info.freq, sw->info.freq); 153 #endif 154 155 return 0; 156 } 157 158 static int glue (audio_pcm_sw_init_, TYPE) ( 159 SW *sw, 160 HW *hw, 161 const char *name, 162 struct audsettings *as 163 ) 164 { 165 int err; 166 167 audio_pcm_init_info (&sw->info, as); 168 sw->hw = hw; 169 sw->active = 0; 170 #ifdef DAC 171 sw->total_hw_samples_mixed = 0; 172 sw->empty = 1; 173 #endif 174 175 if (sw->info.is_float) { 176 #ifdef DAC 177 sw->conv = mixeng_conv_float[sw->info.nchannels == 2]; 178 #else 179 sw->clip = mixeng_clip_float[sw->info.nchannels == 2]; 180 #endif 181 } else { 182 #ifdef DAC 183 sw->conv = mixeng_conv 184 #else 185 sw->clip = mixeng_clip 186 #endif 187 [sw->info.nchannels == 2] 188 [sw->info.is_signed] 189 [sw->info.swap_endianness] 190 [audio_bits_to_index(sw->info.bits)]; 191 } 192 193 sw->name = g_strdup (name); 194 err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw); 195 if (err) { 196 g_free (sw->name); 197 sw->name = NULL; 198 } 199 return err; 200 } 201 202 static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw) 203 { 204 glue (audio_pcm_sw_free_resources_, TYPE) (sw); 205 g_free (sw->name); 206 sw->name = NULL; 207 } 208 209 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw) 210 { 211 QLIST_INSERT_HEAD (&hw->sw_head, sw, entries); 212 } 213 214 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw) 215 { 216 QLIST_REMOVE (sw, entries); 217 } 218 219 static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp) 220 { 221 HW *hw = *hwp; 222 AudioState *s = hw->s; 223 224 if (!hw->sw_head.lh_first) { 225 #ifdef DAC 226 audio_detach_capture(hw); 227 #endif 228 QLIST_REMOVE(hw, entries); 229 glue(hw->pcm_ops->fini_, TYPE) (hw); 230 glue(s->nb_hw_voices_, TYPE) += 1; 231 glue(audio_pcm_hw_free_resources_ , TYPE) (hw); 232 g_free(hw); 233 *hwp = NULL; 234 } 235 } 236 237 static HW *glue(audio_pcm_hw_find_any_, TYPE)(AudioState *s, HW *hw) 238 { 239 return hw ? hw->entries.le_next : glue (s->hw_head_, TYPE).lh_first; 240 } 241 242 static HW *glue(audio_pcm_hw_find_any_enabled_, TYPE)(AudioState *s, HW *hw) 243 { 244 while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) { 245 if (hw->enabled) { 246 return hw; 247 } 248 } 249 return NULL; 250 } 251 252 static HW *glue(audio_pcm_hw_find_specific_, TYPE)(AudioState *s, HW *hw, 253 struct audsettings *as) 254 { 255 while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) { 256 if (audio_pcm_info_eq (&hw->info, as)) { 257 return hw; 258 } 259 } 260 return NULL; 261 } 262 263 static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioState *s, 264 struct audsettings *as) 265 { 266 HW *hw; 267 struct audio_driver *drv = s->drv; 268 269 if (!glue (s->nb_hw_voices_, TYPE)) { 270 return NULL; 271 } 272 273 if (audio_bug(__func__, !drv)) { 274 dolog ("No host audio driver\n"); 275 return NULL; 276 } 277 278 if (audio_bug(__func__, !drv->pcm_ops)) { 279 dolog ("Host audio driver without pcm_ops\n"); 280 return NULL; 281 } 282 283 /* 284 * Since glue(s->nb_hw_voices_, TYPE) is != 0, glue(drv->voice_size_, TYPE) 285 * is guaranteed to be != 0. See the audio_init_nb_voices_* functions. 286 */ 287 hw = g_malloc0(glue(drv->voice_size_, TYPE)); 288 hw->s = s; 289 hw->pcm_ops = drv->pcm_ops; 290 291 QLIST_INIT (&hw->sw_head); 292 #ifdef DAC 293 QLIST_INIT (&hw->cap_head); 294 #endif 295 if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) { 296 goto err0; 297 } 298 299 if (audio_bug(__func__, hw->samples <= 0)) { 300 dolog("hw->samples=%zd\n", hw->samples); 301 goto err1; 302 } 303 304 if (hw->info.is_float) { 305 #ifdef DAC 306 hw->clip = mixeng_clip_float[hw->info.nchannels == 2]; 307 #else 308 hw->conv = mixeng_conv_float[hw->info.nchannels == 2]; 309 #endif 310 } else { 311 #ifdef DAC 312 hw->clip = mixeng_clip 313 #else 314 hw->conv = mixeng_conv 315 #endif 316 [hw->info.nchannels == 2] 317 [hw->info.is_signed] 318 [hw->info.swap_endianness] 319 [audio_bits_to_index(hw->info.bits)]; 320 } 321 322 glue(audio_pcm_hw_alloc_resources_, TYPE)(hw); 323 324 QLIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries); 325 glue (s->nb_hw_voices_, TYPE) -= 1; 326 #ifdef DAC 327 audio_attach_capture (hw); 328 #endif 329 return hw; 330 331 err1: 332 glue (hw->pcm_ops->fini_, TYPE) (hw); 333 err0: 334 g_free (hw); 335 return NULL; 336 } 337 338 AudiodevPerDirectionOptions *glue(audio_get_pdo_, TYPE)(Audiodev *dev) 339 { 340 switch (dev->driver) { 341 case AUDIODEV_DRIVER_NONE: 342 return dev->u.none.TYPE; 343 #ifdef CONFIG_AUDIO_ALSA 344 case AUDIODEV_DRIVER_ALSA: 345 return qapi_AudiodevAlsaPerDirectionOptions_base(dev->u.alsa.TYPE); 346 #endif 347 #ifdef CONFIG_AUDIO_COREAUDIO 348 case AUDIODEV_DRIVER_COREAUDIO: 349 return qapi_AudiodevCoreaudioPerDirectionOptions_base( 350 dev->u.coreaudio.TYPE); 351 #endif 352 #ifdef CONFIG_DBUS_DISPLAY 353 case AUDIODEV_DRIVER_DBUS: 354 return dev->u.dbus.TYPE; 355 #endif 356 #ifdef CONFIG_AUDIO_DSOUND 357 case AUDIODEV_DRIVER_DSOUND: 358 return dev->u.dsound.TYPE; 359 #endif 360 #ifdef CONFIG_AUDIO_JACK 361 case AUDIODEV_DRIVER_JACK: 362 return qapi_AudiodevJackPerDirectionOptions_base(dev->u.jack.TYPE); 363 #endif 364 #ifdef CONFIG_AUDIO_OSS 365 case AUDIODEV_DRIVER_OSS: 366 return qapi_AudiodevOssPerDirectionOptions_base(dev->u.oss.TYPE); 367 #endif 368 #ifdef CONFIG_AUDIO_PA 369 case AUDIODEV_DRIVER_PA: 370 return qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.TYPE); 371 #endif 372 #ifdef CONFIG_AUDIO_PIPEWIRE 373 case AUDIODEV_DRIVER_PIPEWIRE: 374 return qapi_AudiodevPipewirePerDirectionOptions_base(dev->u.pipewire.TYPE); 375 #endif 376 #ifdef CONFIG_AUDIO_SDL 377 case AUDIODEV_DRIVER_SDL: 378 return qapi_AudiodevSdlPerDirectionOptions_base(dev->u.sdl.TYPE); 379 #endif 380 #ifdef CONFIG_AUDIO_SNDIO 381 case AUDIODEV_DRIVER_SNDIO: 382 return dev->u.sndio.TYPE; 383 #endif 384 #ifdef CONFIG_SPICE 385 case AUDIODEV_DRIVER_SPICE: 386 return dev->u.spice.TYPE; 387 #endif 388 case AUDIODEV_DRIVER_WAV: 389 return dev->u.wav.TYPE; 390 391 case AUDIODEV_DRIVER__MAX: 392 break; 393 } 394 abort(); 395 } 396 397 static HW *glue(audio_pcm_hw_add_, TYPE)(AudioState *s, struct audsettings *as) 398 { 399 HW *hw; 400 AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev); 401 402 if (!pdo->mixing_engine || pdo->fixed_settings) { 403 hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as); 404 if (!pdo->mixing_engine || hw) { 405 return hw; 406 } 407 } 408 409 hw = glue(audio_pcm_hw_find_specific_, TYPE)(s, NULL, as); 410 if (hw) { 411 return hw; 412 } 413 414 hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as); 415 if (hw) { 416 return hw; 417 } 418 419 return glue(audio_pcm_hw_find_any_, TYPE)(s, NULL); 420 } 421 422 static SW *glue(audio_pcm_create_voice_pair_, TYPE)( 423 AudioState *s, 424 const char *sw_name, 425 struct audsettings *as 426 ) 427 { 428 SW *sw; 429 HW *hw; 430 struct audsettings hw_as; 431 AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev); 432 433 if (pdo->fixed_settings) { 434 hw_as = audiodev_to_audsettings(pdo); 435 } else { 436 hw_as = *as; 437 } 438 439 sw = g_new0(SW, 1); 440 sw->s = s; 441 442 hw = glue(audio_pcm_hw_add_, TYPE)(s, &hw_as); 443 if (!hw) { 444 dolog("Could not create a backend for voice `%s'\n", sw_name); 445 goto err1; 446 } 447 448 glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw); 449 450 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) { 451 goto err2; 452 } 453 454 return sw; 455 456 err2: 457 glue (audio_pcm_hw_del_sw_, TYPE) (sw); 458 glue (audio_pcm_hw_gc_, TYPE) (&hw); 459 err1: 460 g_free(sw); 461 return NULL; 462 } 463 464 static void glue (audio_close_, TYPE) (SW *sw) 465 { 466 glue (audio_pcm_sw_fini_, TYPE) (sw); 467 glue (audio_pcm_hw_del_sw_, TYPE) (sw); 468 glue (audio_pcm_hw_gc_, TYPE) (&sw->hw); 469 g_free (sw); 470 } 471 472 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw) 473 { 474 if (sw) { 475 if (audio_bug(__func__, !card)) { 476 dolog ("card=%p\n", card); 477 return; 478 } 479 480 glue (audio_close_, TYPE) (sw); 481 } 482 } 483 484 SW *glue (AUD_open_, TYPE) ( 485 QEMUSoundCard *card, 486 SW *sw, 487 const char *name, 488 void *callback_opaque , 489 audio_callback_fn callback_fn, 490 struct audsettings *as 491 ) 492 { 493 AudioState *s; 494 AudiodevPerDirectionOptions *pdo; 495 496 if (audio_bug(__func__, !card || !name || !callback_fn || !as)) { 497 dolog ("card=%p name=%p callback_fn=%p as=%p\n", 498 card, name, callback_fn, as); 499 goto fail; 500 } 501 502 s = card->state; 503 pdo = glue(audio_get_pdo_, TYPE)(s->dev); 504 505 ldebug ("open %s, freq %d, nchannels %d, fmt %d\n", 506 name, as->freq, as->nchannels, as->fmt); 507 508 if (audio_bug(__func__, audio_validate_settings(as))) { 509 audio_print_settings (as); 510 goto fail; 511 } 512 513 if (audio_bug(__func__, !s->drv)) { 514 dolog ("Can not open `%s' (no host audio driver)\n", name); 515 goto fail; 516 } 517 518 if (sw && audio_pcm_info_eq (&sw->info, as)) { 519 return sw; 520 } 521 522 if (!pdo->fixed_settings && sw) { 523 glue (AUD_close_, TYPE) (card, sw); 524 sw = NULL; 525 } 526 527 if (sw) { 528 HW *hw = sw->hw; 529 530 if (!hw) { 531 dolog("Internal logic error: voice `%s' has no backend\n", 532 SW_NAME(sw)); 533 goto fail; 534 } 535 536 glue (audio_pcm_sw_fini_, TYPE) (sw); 537 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) { 538 goto fail; 539 } 540 } else { 541 sw = glue(audio_pcm_create_voice_pair_, TYPE)(s, name, as); 542 if (!sw) { 543 return NULL; 544 } 545 } 546 547 sw->card = card; 548 sw->vol = nominal_volume; 549 sw->callback.fn = callback_fn; 550 sw->callback.opaque = callback_opaque; 551 552 #ifdef DEBUG_AUDIO 553 dolog ("%s\n", name); 554 audio_pcm_print_info ("hw", &sw->hw->info); 555 audio_pcm_print_info ("sw", &sw->info); 556 #endif 557 558 return sw; 559 560 fail: 561 glue (AUD_close_, TYPE) (card, sw); 562 return NULL; 563 } 564 565 int glue (AUD_is_active_, TYPE) (SW *sw) 566 { 567 return sw ? sw->active : 0; 568 } 569 570 void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts) 571 { 572 if (!sw) { 573 return; 574 } 575 576 ts->old_ts = sw->hw->ts_helper; 577 } 578 579 uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts) 580 { 581 uint64_t delta, cur_ts, old_ts; 582 583 if (!sw) { 584 return 0; 585 } 586 587 cur_ts = sw->hw->ts_helper; 588 old_ts = ts->old_ts; 589 /* dolog ("cur %" PRId64 " old %" PRId64 "\n", cur_ts, old_ts); */ 590 591 if (cur_ts >= old_ts) { 592 delta = cur_ts - old_ts; 593 } else { 594 delta = UINT64_MAX - old_ts + cur_ts; 595 } 596 597 if (!delta) { 598 return 0; 599 } 600 601 return muldiv64 (delta, sw->hw->info.freq, 1000000); 602 } 603 604 #undef TYPE 605 #undef HW 606 #undef SW 607 #undef HWBUF 608 #undef NAME 609