qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Christian Schoenebeck <qemu_oss@crudebyte.com>
To: qemu-devel@nongnu.org, Volker Ruemelin <vr_qemu@t-online.de>
Cc: Gerd Hoffmann <kraxel@redhat.com>, BALATON Zoltan <balaton@eik.bme.hu>
Subject: Re: [PATCH] alsaaudio: Set try-poll to false by default
Date: Tue, 08 Apr 2025 14:55:27 +0200	[thread overview]
Message-ID: <2024817.RjeAs2xCtm@silver> (raw)
In-Reply-To: <abecc55b-f032-03e3-a9f3-628b1f8f7e5d@eik.bme.hu>

On Friday, April 4, 2025 1:34:27 PM CEST BALATON Zoltan wrote:
> On Fri, 4 Apr 2025, Christian Schoenebeck wrote:
> > On Monday, March 31, 2025 3:05:24 PM CEST BALATON Zoltan wrote:
> >> On Sun, 23 Mar 2025, Christian Schoenebeck wrote:
> >>> On Sunday, March 16, 2025 1:20:46 AM CET BALATON Zoltan wrote:
> >>>> Quoting Volker Rümelin: "try-poll=on tells the ALSA backend to try to
> >>>> use an event loop instead of the audio timer. This works most of the
> >>>> time. But the poll event handler in the ALSA backend has a bug. For
> >>>> example, if the guest can't provide enough audio frames in time, the
> >>>> ALSA buffer is only partly full and the event handler will be called
> >>>> again and again on every iteration of the main loop. This increases
> >>>> the processor load and the guest has less processor time to provide
> >>>> new audio frames in time. I have two examples where a guest can't
> >>>> recover from this situation and the guest seems to hang."
> >>>>
> >>>> One reproducer I've found is booting MorphOS demo iso on
> >>>> qemu-system-ppc -machine pegasos2 -audio alsa which should play a
> >>>> startup sound but instead it freezes. Even when it does not hang it
> >>>> plays choppy sound. Volker suggested using command line to set
> >>>> try-poll=off saying: "The try-poll=off arguments are typically
> >>>> necessary, because the alsa backend has a design issue with
> >>>> try-poll=on. If the guest can't provide enough audio frames, it's
> >>>> really unhelpful to ask for new audio frames on every main loop
> >>>> iteration until the guest can provide enough audio frames. Timer based
> >>>> playback doesn't have that problem."
> >>>>
> >>>> But users cannot easily find this option and having a non-working
> >>>> default is really unhelpful so to make life easier just set it to
> >>>> false by default which works until the issue with the alsa backend can
> >>>> be fixed.
> >>>>
> >>>> Signed-off-by: BALATON Zoltan <balaton@eik.bme.hu>
> >>>> ---
> >>>> This fixes my issue but if somebody has a better fix I'm open to that
> >>>> too.
> >>>>
> >>>>  audio/alsaaudio.c | 2 +-
> >>>>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>>>
> >>>> diff --git a/audio/alsaaudio.c b/audio/alsaaudio.c
> >>>> index cacae1ea59..9b6c01c0ef 100644
> >>>> --- a/audio/alsaaudio.c
> >>>> +++ b/audio/alsaaudio.c
> >>>> @@ -899,7 +899,7 @@ static void alsa_enable_in(HWVoiceIn *hw, bool enable)
> >>>>  static void alsa_init_per_direction(AudiodevAlsaPerDirectionOptions *apdo)
> >>>>  {
> >>>>      if (!apdo->has_try_poll) {
> >>>> -        apdo->try_poll = true;
> >>>> +        apdo->try_poll = false;
> >>>>          apdo->has_try_poll = true;
> >>>>      }
> >>>>  }
> >>>>
> >>>
> >>> Correct me if I am wrong, but AFAICS if polling is not used then no state
> >>> changes would be handled, no? At least I don't see any snd_pcm_state() call
> >>> outside of alsa_poll_handler().
> >>
> >> I have no idea but this fixes the problem (and does the same that can be
> >> also done from command line but nobody can find that command line option)
> >> so unless somebody has a better idea could this be merged as a fix for
> >> now?
> >
> > Well, I understand that if fixes the misbehaviour you encountered. But how
> > helpful would it be if it then breaks behaviour for other people instead?
> 
> What behaviour would it break and how?

There are only a bunch of ALSA states handled right now in the QEMU Alsa
driver (see alsa_poll_handler()):

    state = snd_pcm_state (hlp->handle);
    switch (state) {
    case SND_PCM_STATE_SETUP:
        alsa_recover (hlp->handle);
        break;

    case SND_PCM_STATE_XRUN:
        alsa_recover (hlp->handle);
        break;

    case SND_PCM_STATE_SUSPENDED:
        alsa_resume (hlp->handle);
        break;

    case SND_PCM_STATE_PREPARED:
        audio_run(hlp->s, "alsa run (prepared)");
        break;

    case SND_PCM_STATE_RUNNING:
        audio_run(hlp->s, "alsa run (running)");
        break;

For instance in poll mode it recovers in case of an xrun, which happens on
audio output if the audio output data was not delivered by the application in
time.

The other case is when the system was suspended (standby). It should also
recover the audio session here.

Now I haven't tested whether these would work in callback mode right now, but
looking at the code suggests that they might not.

> > I think it would be better to add a 2nd patch that would handle state changes
> > in callback mode. That would satisfy both groups of people. AFAICS
> > snd_pcm_state() can be called both in polling mode and callback mode.
> 
> I can't do that because I don't quite know neither alsa nor audio in QEMU 
> so I have no idea what to do. Can you give more clues?

Well, as a starting point you might try whether these cases described above
would still work in callback mode. Maybe it is even working, who knows.

/Christian

> > Just my 2 cents. Of course it's up to Gerd to decide.
> 
> If you know it would break something I agree it should be fixed and I can 
> try but I could not reproduce any breakage and most people likely use 
> other audio backends so unlikely to encounter problems with alsa.
> 
> Regards,
> BALATON Zoltan




  reply	other threads:[~2025-04-08 12:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-16  0:20 [PATCH] alsaaudio: Set try-poll to false by default BALATON Zoltan
2025-03-23 13:26 ` BALATON Zoltan
2025-03-23 14:36 ` Christian Schoenebeck
2025-03-31 13:05   ` BALATON Zoltan
2025-04-04  7:05     ` Christian Schoenebeck
2025-04-04 11:34       ` BALATON Zoltan
2025-04-08 12:55         ` Christian Schoenebeck [this message]
2025-04-08 20:13           ` Volker Rümelin
2025-05-06 14:34             ` BALATON Zoltan
2025-05-07 17:35               ` Christian Schoenebeck
2025-05-07 19:38                 ` BALATON Zoltan
2025-05-15 11:51                   ` BALATON Zoltan
2025-04-08 20:58           ` BALATON Zoltan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2024817.RjeAs2xCtm@silver \
    --to=qemu_oss@crudebyte.com \
    --cc=balaton@eik.bme.hu \
    --cc=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=vr_qemu@t-online.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).