diff options
author | Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com> | 2011-09-06 20:15:34 -0400 |
---|---|---|
committer | Takashi Iwai <tiwai@suse.de> | 2011-09-12 04:30:20 -0400 |
commit | 294c4fb8ab01728358836f478bcc1174ba7fb9d8 (patch) | |
tree | 2afde5810233ceb00dafa573061f224026238ada /sound/usb/pcm.c | |
parent | 1ef0e0a05345b7411bdabbfca27f58bd33dcc7c8 (diff) |
ALSA: usb: refine delay information with USB frame counter
Existing code only updates the audio delay when URBs were
submitted/retired. This can introduce an uncertainty of 8ms
on the number of samples played out with the default settings,
and a lot more when URBs convey more packets to reduce the
interrupt rate and power consumption.
This patch relies on the USB frame counter to reduce the
uncertainty to less than 2ms worst-case. The delay information
essentially becomes independent of the URB size and number of
packets. This should help applications like PulseAudio which
require accurate audio timing. Clemens Ladisch reported
a decrease of mplayer's A-V difference from nrpacks down to at
most 1ms.
Thanks to Clemens for also pointing out that the implementation
of frame counters varies between different HCDs. Only the
8 lowest-bits are used to estimate the delay.
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
[clemens: changed debug code]
Signed-off-by: Clemens Ladisch <clemens@ladisch.de>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Diffstat (limited to 'sound/usb/pcm.c')
-rw-r--r-- | sound/usb/pcm.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index b8dcbf407bbb..0b699ca1957e 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c | |||
@@ -34,6 +34,30 @@ | |||
34 | #include "clock.h" | 34 | #include "clock.h" |
35 | #include "power.h" | 35 | #include "power.h" |
36 | 36 | ||
37 | /* return the estimated delay based on USB frame counters */ | ||
38 | snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs, | ||
39 | unsigned int rate) | ||
40 | { | ||
41 | int current_frame_number; | ||
42 | int frame_diff; | ||
43 | int est_delay; | ||
44 | |||
45 | current_frame_number = usb_get_current_frame_number(subs->dev); | ||
46 | /* | ||
47 | * HCD implementations use different widths, use lower 8 bits. | ||
48 | * The delay will be managed up to 256ms, which is more than | ||
49 | * enough | ||
50 | */ | ||
51 | frame_diff = (current_frame_number - subs->last_frame_number) & 0xff; | ||
52 | |||
53 | /* Approximation based on number of samples per USB frame (ms), | ||
54 | some truncation for 44.1 but the estimate is good enough */ | ||
55 | est_delay = subs->last_delay - (frame_diff * rate / 1000); | ||
56 | if (est_delay < 0) | ||
57 | est_delay = 0; | ||
58 | return est_delay; | ||
59 | } | ||
60 | |||
37 | /* | 61 | /* |
38 | * return the current pcm pointer. just based on the hwptr_done value. | 62 | * return the current pcm pointer. just based on the hwptr_done value. |
39 | */ | 63 | */ |
@@ -45,6 +69,8 @@ static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream | |||
45 | subs = (struct snd_usb_substream *)substream->runtime->private_data; | 69 | subs = (struct snd_usb_substream *)substream->runtime->private_data; |
46 | spin_lock(&subs->lock); | 70 | spin_lock(&subs->lock); |
47 | hwptr_done = subs->hwptr_done; | 71 | hwptr_done = subs->hwptr_done; |
72 | substream->runtime->delay = snd_usb_pcm_delay(subs, | ||
73 | substream->runtime->rate); | ||
48 | spin_unlock(&subs->lock); | 74 | spin_unlock(&subs->lock); |
49 | return hwptr_done / (substream->runtime->frame_bits >> 3); | 75 | return hwptr_done / (substream->runtime->frame_bits >> 3); |
50 | } | 76 | } |
@@ -417,6 +443,8 @@ static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) | |||
417 | subs->hwptr_done = 0; | 443 | subs->hwptr_done = 0; |
418 | subs->transfer_done = 0; | 444 | subs->transfer_done = 0; |
419 | subs->phase = 0; | 445 | subs->phase = 0; |
446 | subs->last_delay = 0; | ||
447 | subs->last_frame_number = 0; | ||
420 | runtime->delay = 0; | 448 | runtime->delay = 0; |
421 | 449 | ||
422 | return snd_usb_substream_prepare(subs, runtime); | 450 | return snd_usb_substream_prepare(subs, runtime); |