All of lore.kernel.org
 help / color / mirror / Atom feed
From: Clemens Ladisch <clemens@ladisch.de>
To: John Lindgren <john.lindgren@tds.net>
Cc: alsa-devel@alsa-project.org
Subject: Re: [PATCH] alsa-lib: snd_pcm_delay and friends do not account for a write being currently in progress
Date: Thu, 03 Jun 2010 08:40:35 +0200	[thread overview]
Message-ID: <4C074E63.90508@ladisch.de> (raw)
In-Reply-To: <1275514176.8032.42.camel@satellite>

John Lindgren wrote:
> In a multi-threaded application it is possible for snd_pcm_delay or an
> equivalent function to be called by one thread while another is sitting
> in snd_pcm_writei.

Alsa-lib is not thread safe.  In theory, you are not even allowed to
call snd_pcm_delay while another function on the same PCM device has not
yet returned.

> Suppose a sound card has a maximum buffer size of 0.1 second.  Then,
> suppose an application writes 0.5 second of audio.  snd_pcm_writei will
> immediately write the first 0.1 second to the ring buffer, then will
> incrementally write the remaining 0.4 second as the buffer drains.

It will write a chunk whenever it is woken up by the driver, which is
usually at period boundaries.

> During this time, snd_pcm_delay will return a constant fill level of 0.1
> second.

Depending on how big the granularity of the hardware's readable DMA
pointer is, but on most hardware, it will fluctuate between a full
buffer and one period less than that, not counting further fluctuations
due to scheduling delays.

> On the application side, the playback time counter will be calculated
> during this time as 0.5 seconds written to ALSA

This is wrong; as long as the write call has not returned, you do not
know how much has been written (and when an error occurs, writing can
stop before).

To keep track of the actual amount of data written, use non-blocking
mode and in a loop, write as much as possible in one call, then update
your write counter, then wait for some more free space in the buffer
with poll().


Regards,
Clemens

  reply	other threads:[~2010-06-03  6:40 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-02 21:29 [PATCH] alsa-lib: snd_pcm_delay and friends do not account for a write being currently in progress John Lindgren
2010-06-03  6:40 ` Clemens Ladisch [this message]
2010-06-03 14:00   ` John Lindgren
2010-06-03 14:48     ` Clemens Ladisch
2010-06-03 16:16       ` John Lindgren
2010-06-03 17:03         ` Clemens Ladisch
2010-06-03 17:51           ` John Lindgren
2010-06-03 14:40 ` [PATCH] " James Courtier-Dutton
2010-06-03 16:10   ` John Lindgren
2010-06-03 16:34     ` James Courtier-Dutton
2010-06-03 18:06       ` John Lindgren
2010-06-03 17:40 ` VDR User
2010-06-03 18:08   ` John Lindgren
2010-06-04  6:50   ` Clemens Ladisch

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=4C074E63.90508@ladisch.de \
    --to=clemens@ladisch.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=john.lindgren@tds.net \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.