audio: basic support for multi backend audio
Audio functions no longer access glob_audio_state, instead they get an AudioState as a parameter. This is required in order to support multiple backends. glob_audio_state is also gone, and replaced with a tailq so we can store more than one states. Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com> Message-id: 67aef54f9e729a7160fe95c465351115e392164b.1566168923.git.DirtY.iCE.hu@gmail.com Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
This commit is contained in:
parent
526fb0581e
commit
ecd97e9592
102
audio/audio.c
102
audio/audio.c
@ -87,7 +87,8 @@ audio_driver *audio_driver_lookup(const char *name)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static AudioState glob_audio_state;
|
static QTAILQ_HEAD(AudioStateHead, AudioState) audio_states =
|
||||||
|
QTAILQ_HEAD_INITIALIZER(audio_states);
|
||||||
|
|
||||||
const struct mixeng_volume nominal_volume = {
|
const struct mixeng_volume nominal_volume = {
|
||||||
.mute = 0,
|
.mute = 0,
|
||||||
@ -1238,11 +1239,14 @@ static void audio_run_capture (AudioState *s)
|
|||||||
|
|
||||||
void audio_run (const char *msg)
|
void audio_run (const char *msg)
|
||||||
{
|
{
|
||||||
AudioState *s = &glob_audio_state;
|
AudioState *s;
|
||||||
|
|
||||||
|
QTAILQ_FOREACH(s, &audio_states, list) {
|
||||||
|
audio_run_out(s);
|
||||||
|
audio_run_in(s);
|
||||||
|
audio_run_capture(s);
|
||||||
|
}
|
||||||
|
|
||||||
audio_run_out (s);
|
|
||||||
audio_run_in (s);
|
|
||||||
audio_run_capture (s);
|
|
||||||
#ifdef DEBUG_POLL
|
#ifdef DEBUG_POLL
|
||||||
{
|
{
|
||||||
static double prevtime;
|
static double prevtime;
|
||||||
@ -1306,13 +1310,11 @@ bool audio_is_cleaning_up(void)
|
|||||||
return is_cleaning_up;
|
return is_cleaning_up;
|
||||||
}
|
}
|
||||||
|
|
||||||
void audio_cleanup(void)
|
static void free_audio_state(AudioState *s)
|
||||||
{
|
{
|
||||||
AudioState *s = &glob_audio_state;
|
|
||||||
HWVoiceOut *hwo, *hwon;
|
HWVoiceOut *hwo, *hwon;
|
||||||
HWVoiceIn *hwi, *hwin;
|
HWVoiceIn *hwi, *hwin;
|
||||||
|
|
||||||
is_cleaning_up = true;
|
|
||||||
QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) {
|
QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) {
|
||||||
SWVoiceCap *sc;
|
SWVoiceCap *sc;
|
||||||
|
|
||||||
@ -1349,6 +1351,17 @@ void audio_cleanup(void)
|
|||||||
qapi_free_Audiodev(s->dev);
|
qapi_free_Audiodev(s->dev);
|
||||||
s->dev = NULL;
|
s->dev = NULL;
|
||||||
}
|
}
|
||||||
|
g_free(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void audio_cleanup(void)
|
||||||
|
{
|
||||||
|
is_cleaning_up = true;
|
||||||
|
while (!QTAILQ_EMPTY(&audio_states)) {
|
||||||
|
AudioState *s = QTAILQ_FIRST(&audio_states);
|
||||||
|
QTAILQ_REMOVE(&audio_states, s, list);
|
||||||
|
free_audio_state(s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const VMStateDescription vmstate_audio = {
|
static const VMStateDescription vmstate_audio = {
|
||||||
@ -1375,28 +1388,33 @@ static AudiodevListEntry *audiodev_find(
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int audio_init(Audiodev *dev)
|
/*
|
||||||
|
* if we have dev, this function was called because of an -audiodev argument =>
|
||||||
|
* initialize a new state with it
|
||||||
|
* if dev == NULL => legacy implicit initialization, return the already created
|
||||||
|
* state or create a new one
|
||||||
|
*/
|
||||||
|
static AudioState *audio_init(Audiodev *dev)
|
||||||
{
|
{
|
||||||
|
static bool atexit_registered;
|
||||||
size_t i;
|
size_t i;
|
||||||
int done = 0;
|
int done = 0;
|
||||||
const char *drvname = NULL;
|
const char *drvname = NULL;
|
||||||
VMChangeStateEntry *e;
|
VMChangeStateEntry *e;
|
||||||
AudioState *s = &glob_audio_state;
|
AudioState *s;
|
||||||
struct audio_driver *driver;
|
struct audio_driver *driver;
|
||||||
/* silence gcc warning about uninitialized variable */
|
/* silence gcc warning about uninitialized variable */
|
||||||
AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
|
AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
|
||||||
|
|
||||||
if (s->drv) {
|
|
||||||
if (dev) {
|
|
||||||
dolog("Cannot create more than one audio backend, sorry\n");
|
|
||||||
qapi_free_Audiodev(dev);
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dev) {
|
if (dev) {
|
||||||
/* -audiodev option */
|
/* -audiodev option */
|
||||||
drvname = AudiodevDriver_str(dev->driver);
|
drvname = AudiodevDriver_str(dev->driver);
|
||||||
|
} else if (!QTAILQ_EMPTY(&audio_states)) {
|
||||||
|
/*
|
||||||
|
* todo: check for -audiodev once we have normal audiodev selection
|
||||||
|
* support
|
||||||
|
*/
|
||||||
|
return QTAILQ_FIRST(&audio_states);
|
||||||
} else {
|
} else {
|
||||||
/* legacy implicit initialization */
|
/* legacy implicit initialization */
|
||||||
head = audio_handle_legacy_opts();
|
head = audio_handle_legacy_opts();
|
||||||
@ -1410,12 +1428,18 @@ static int audio_init(Audiodev *dev)
|
|||||||
dev = QSIMPLEQ_FIRST(&head)->dev;
|
dev = QSIMPLEQ_FIRST(&head)->dev;
|
||||||
audio_validate_opts(dev, &error_abort);
|
audio_validate_opts(dev, &error_abort);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s = g_malloc0(sizeof(AudioState));
|
||||||
s->dev = dev;
|
s->dev = dev;
|
||||||
|
|
||||||
QLIST_INIT (&s->hw_head_out);
|
QLIST_INIT (&s->hw_head_out);
|
||||||
QLIST_INIT (&s->hw_head_in);
|
QLIST_INIT (&s->hw_head_in);
|
||||||
QLIST_INIT (&s->cap_head);
|
QLIST_INIT (&s->cap_head);
|
||||||
atexit(audio_cleanup);
|
if (!atexit_registered) {
|
||||||
|
atexit(audio_cleanup);
|
||||||
|
atexit_registered = true;
|
||||||
|
}
|
||||||
|
QTAILQ_INSERT_TAIL(&audio_states, s, list);
|
||||||
|
|
||||||
s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
|
s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
|
||||||
|
|
||||||
@ -1480,7 +1504,7 @@ static int audio_init(Audiodev *dev)
|
|||||||
|
|
||||||
QLIST_INIT (&s->card_head);
|
QLIST_INIT (&s->card_head);
|
||||||
vmstate_register (NULL, 0, &vmstate_audio, s);
|
vmstate_register (NULL, 0, &vmstate_audio, s);
|
||||||
return 0;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
void audio_free_audiodev_list(AudiodevListHead *head)
|
void audio_free_audiodev_list(AudiodevListHead *head)
|
||||||
@ -1495,10 +1519,13 @@ void audio_free_audiodev_list(AudiodevListHead *head)
|
|||||||
|
|
||||||
void AUD_register_card (const char *name, QEMUSoundCard *card)
|
void AUD_register_card (const char *name, QEMUSoundCard *card)
|
||||||
{
|
{
|
||||||
audio_init(NULL);
|
if (!card->state) {
|
||||||
|
card->state = audio_init(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
card->name = g_strdup (name);
|
card->name = g_strdup (name);
|
||||||
memset (&card->entries, 0, sizeof (card->entries));
|
memset (&card->entries, 0, sizeof (card->entries));
|
||||||
QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
|
QLIST_INSERT_HEAD(&card->state->card_head, card, entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AUD_remove_card (QEMUSoundCard *card)
|
void AUD_remove_card (QEMUSoundCard *card)
|
||||||
@ -1508,16 +1535,21 @@ void AUD_remove_card (QEMUSoundCard *card)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CaptureVoiceOut *AUD_add_capture (
|
CaptureVoiceOut *AUD_add_capture(
|
||||||
|
AudioState *s,
|
||||||
struct audsettings *as,
|
struct audsettings *as,
|
||||||
struct audio_capture_ops *ops,
|
struct audio_capture_ops *ops,
|
||||||
void *cb_opaque
|
void *cb_opaque
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
AudioState *s = &glob_audio_state;
|
|
||||||
CaptureVoiceOut *cap;
|
CaptureVoiceOut *cap;
|
||||||
struct capture_callback *cb;
|
struct capture_callback *cb;
|
||||||
|
|
||||||
|
if (!s) {
|
||||||
|
/* todo: remove when we have normal audiodev selection support */
|
||||||
|
s = audio_init(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
if (audio_validate_settings (as)) {
|
if (audio_validate_settings (as)) {
|
||||||
dolog ("Invalid settings were passed when trying to add capture\n");
|
dolog ("Invalid settings were passed when trying to add capture\n");
|
||||||
audio_print_settings (as);
|
audio_print_settings (as);
|
||||||
@ -1807,3 +1839,25 @@ int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
|
|||||||
return audio_buffer_samples(pdo, as, def_usecs) *
|
return audio_buffer_samples(pdo, as, def_usecs) *
|
||||||
audioformat_bytes_per_sample(as->fmt);
|
audioformat_bytes_per_sample(as->fmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AudioState *audio_state_by_name(const char *name)
|
||||||
|
{
|
||||||
|
AudioState *s;
|
||||||
|
QTAILQ_FOREACH(s, &audio_states, list) {
|
||||||
|
assert(s->dev);
|
||||||
|
if (strcmp(name, s->dev->id) == 0) {
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *audio_get_id(QEMUSoundCard *card)
|
||||||
|
{
|
||||||
|
if (card->state) {
|
||||||
|
assert(card->state->dev);
|
||||||
|
return card->state->dev->id;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -78,8 +78,10 @@ typedef struct SWVoiceOut SWVoiceOut;
|
|||||||
typedef struct CaptureVoiceOut CaptureVoiceOut;
|
typedef struct CaptureVoiceOut CaptureVoiceOut;
|
||||||
typedef struct SWVoiceIn SWVoiceIn;
|
typedef struct SWVoiceIn SWVoiceIn;
|
||||||
|
|
||||||
|
typedef struct AudioState AudioState;
|
||||||
typedef struct QEMUSoundCard {
|
typedef struct QEMUSoundCard {
|
||||||
char *name;
|
char *name;
|
||||||
|
AudioState *state;
|
||||||
QLIST_ENTRY (QEMUSoundCard) entries;
|
QLIST_ENTRY (QEMUSoundCard) entries;
|
||||||
} QEMUSoundCard;
|
} QEMUSoundCard;
|
||||||
|
|
||||||
@ -92,7 +94,8 @@ void AUD_log (const char *cap, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
|
|||||||
|
|
||||||
void AUD_register_card (const char *name, QEMUSoundCard *card);
|
void AUD_register_card (const char *name, QEMUSoundCard *card);
|
||||||
void AUD_remove_card (QEMUSoundCard *card);
|
void AUD_remove_card (QEMUSoundCard *card);
|
||||||
CaptureVoiceOut *AUD_add_capture (
|
CaptureVoiceOut *AUD_add_capture(
|
||||||
|
AudioState *s,
|
||||||
struct audsettings *as,
|
struct audsettings *as,
|
||||||
struct audio_capture_ops *ops,
|
struct audio_capture_ops *ops,
|
||||||
void *opaque
|
void *opaque
|
||||||
@ -160,8 +163,8 @@ static inline void *advance (void *p, int incr)
|
|||||||
#define audio_MAX(a, b) ((a)<(b)?(b):(a))
|
#define audio_MAX(a, b) ((a)<(b)?(b):(a))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int wav_start_capture (CaptureState *s, const char *path, int freq,
|
int wav_start_capture(AudioState *state, CaptureState *s, const char *path,
|
||||||
int bits, int nchannels);
|
int freq, int bits, int nchannels);
|
||||||
|
|
||||||
bool audio_is_cleaning_up(void);
|
bool audio_is_cleaning_up(void);
|
||||||
void audio_cleanup(void);
|
void audio_cleanup(void);
|
||||||
@ -175,4 +178,7 @@ void audio_parse_option(const char *opt);
|
|||||||
void audio_init_audiodevs(void);
|
void audio_init_audiodevs(void);
|
||||||
void audio_legacy_help(void);
|
void audio_legacy_help(void);
|
||||||
|
|
||||||
|
AudioState *audio_state_by_name(const char *name);
|
||||||
|
const char *audio_get_id(QEMUSoundCard *card);
|
||||||
|
|
||||||
#endif /* QEMU_AUDIO_H */
|
#endif /* QEMU_AUDIO_H */
|
||||||
|
@ -196,6 +196,8 @@ typedef struct AudioState {
|
|||||||
|
|
||||||
bool timer_running;
|
bool timer_running;
|
||||||
uint64_t timer_last;
|
uint64_t timer_last;
|
||||||
|
|
||||||
|
QTAILQ_ENTRY(AudioState) list;
|
||||||
} AudioState;
|
} AudioState;
|
||||||
|
|
||||||
extern const struct mixeng_volume nominal_volume;
|
extern const struct mixeng_volume nominal_volume;
|
||||||
|
@ -428,7 +428,7 @@ SW *glue (AUD_open_, TYPE) (
|
|||||||
struct audsettings *as
|
struct audsettings *as
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
AudioState *s = &glob_audio_state;
|
AudioState *s = card->state;
|
||||||
AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
|
AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
|
||||||
|
|
||||||
if (audio_bug(__func__, !card || !name || !callback_fn || !as)) {
|
if (audio_bug(__func__, !card || !name || !callback_fn || !as)) {
|
||||||
|
@ -104,8 +104,8 @@ static struct capture_ops wav_capture_ops = {
|
|||||||
.info = wav_capture_info
|
.info = wav_capture_info
|
||||||
};
|
};
|
||||||
|
|
||||||
int wav_start_capture (CaptureState *s, const char *path, int freq,
|
int wav_start_capture(AudioState *state, CaptureState *s, const char *path,
|
||||||
int bits, int nchannels)
|
int freq, int bits, int nchannels)
|
||||||
{
|
{
|
||||||
WAVState *wav;
|
WAVState *wav;
|
||||||
uint8_t hdr[] = {
|
uint8_t hdr[] = {
|
||||||
@ -170,7 +170,7 @@ int wav_start_capture (CaptureState *s, const char *path, int freq,
|
|||||||
goto error_free;
|
goto error_free;
|
||||||
}
|
}
|
||||||
|
|
||||||
cap = AUD_add_capture (&as, &ops, wav);
|
cap = AUD_add_capture(state, &as, &ops, wav);
|
||||||
if (!cap) {
|
if (!cap) {
|
||||||
error_report("Failed to add audio capture");
|
error_report("Failed to add audio capture");
|
||||||
goto error_free;
|
goto error_free;
|
||||||
|
@ -1156,7 +1156,7 @@ static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
|
|||||||
bits = has_bits ? bits : 16;
|
bits = has_bits ? bits : 16;
|
||||||
nchannels = has_channels ? nchannels : 2;
|
nchannels = has_channels ? nchannels : 2;
|
||||||
|
|
||||||
if (wav_start_capture (s, path, freq, bits, nchannels)) {
|
if (wav_start_capture(NULL, s, path, freq, bits, nchannels)) {
|
||||||
monitor_printf(mon, "Failed to add wave capture\n");
|
monitor_printf(mon, "Failed to add wave capture\n");
|
||||||
g_free (s);
|
g_free (s);
|
||||||
return;
|
return;
|
||||||
|
2
ui/vnc.c
2
ui/vnc.c
@ -1224,7 +1224,7 @@ static void audio_add(VncState *vs)
|
|||||||
ops.destroy = audio_capture_destroy;
|
ops.destroy = audio_capture_destroy;
|
||||||
ops.capture = audio_capture;
|
ops.capture = audio_capture;
|
||||||
|
|
||||||
vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
|
vs->audio_cap = AUD_add_capture(NULL, &vs->as, &ops, vs);
|
||||||
if (!vs->audio_cap) {
|
if (!vs->audio_cap) {
|
||||||
error_report("Failed to add audio capture");
|
error_report("Failed to add audio capture");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user