Date: Mon, 26 Feb 2024 23:22:50 GMT From: Christos Margiolis <christos@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: 75358ca4fb54 - stable/14 - snd_uaudio(4): Adapt buffer length to buffer_ms tunable. Message-ID: <202402262322.41QNMo1E028602@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=75358ca4fb54390c313e5346574771c292bb764f commit 75358ca4fb54390c313e5346574771c292bb764f Author: Florian Walpen <dev@submerge.ch> AuthorDate: 2024-02-12 11:04:57 +0000 Commit: Christos Margiolis <christos@FreeBSD.org> CommitDate: 2024-02-26 23:22:31 +0000 snd_uaudio(4): Adapt buffer length to buffer_ms tunable. Adapt the length of the driver side audio buffer to the USB transfer interval, which is adjustable through the buffer_ms tunable. This eliminates unnecessary latency in USB audio playback. To reduce power consumption caused by frequent CPU wakeups, increase the default buffer_ms value to 4ms. In combination with adaptive buffer length, this still results in less roundtrip latency compared to the previous 2ms default. Extend the buffer_ms value range to 1ms for low latency applications. MFC after: 2 weeks Reviewed by: christos Differential Revision: https://reviews.freebsd.org/D41942 (cherry picked from commit b2e97edffdab6ad1e9103a6bbe90300d78a961ed) --- sys/dev/sound/usb/uaudio.c | 70 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/sys/dev/sound/usb/uaudio.c b/sys/dev/sound/usb/uaudio.c index 917b6bd3f238..b0bf815f7797 100644 --- a/sys/dev/sound/usb/uaudio.c +++ b/sys/dev/sound/usb/uaudio.c @@ -96,7 +96,7 @@ static int uaudio_default_rate = 0; /* use rate list */ static int uaudio_default_bits = 32; static int uaudio_default_channels = 0; /* use default */ -static int uaudio_buffer_ms = 2; +static int uaudio_buffer_ms = 4; static bool uaudio_handle_hid = true; static SYSCTL_NODE(_hw_usb, OID_AUTO, uaudio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, @@ -110,6 +110,9 @@ SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_bits, CTLFLAG_RWTUN, SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_channels, CTLFLAG_RWTUN, &uaudio_default_channels, 0, "uaudio default sample channels"); +#define UAUDIO_BUFFER_MS_MIN 1 +#define UAUDIO_BUFFER_MS_MAX 8 + static int uaudio_buffer_ms_sysctl(SYSCTL_HANDLER_ARGS) { @@ -121,10 +124,10 @@ uaudio_buffer_ms_sysctl(SYSCTL_HANDLER_ARGS) if (err != 0 || req->newptr == NULL || val == uaudio_buffer_ms) return (err); - if (val > 8) - val = 8; - else if (val < 2) - val = 2; + if (val > UAUDIO_BUFFER_MS_MAX) + val = UAUDIO_BUFFER_MS_MAX; + else if (val < UAUDIO_BUFFER_MS_MIN) + val = UAUDIO_BUFFER_MS_MIN; uaudio_buffer_ms = val; @@ -133,7 +136,7 @@ uaudio_buffer_ms_sysctl(SYSCTL_HANDLER_ARGS) SYSCTL_PROC(_hw_usb_uaudio, OID_AUTO, buffer_ms, CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, sizeof(int), uaudio_buffer_ms_sysctl, "I", - "uaudio buffering delay from 2ms to 8ms"); + "uaudio buffering delay in milliseconds, from 1 to 8"); #ifdef USB_DEBUG static int uaudio_debug; @@ -1310,13 +1313,60 @@ uaudio_detach(device_t dev) return (0); } +static uint32_t +uaudio_get_interval_frames(const usb_endpoint_descriptor_audio_t *ed) +{ + uint32_t frames = 1; + /* Isochronous transfer interval is 2^(bInterval - 1) frames. */ + if (ed->bInterval >= 1 && ed->bInterval <= 16) + frames = (1 << (ed->bInterval - 1)); + /* Limit transfer interval to maximum number of frames. */ + if (frames > UAUDIO_NFRAMES) + frames = UAUDIO_NFRAMES; + return (frames); +} + +static uint32_t +uaudio_get_buffer_ms(struct uaudio_softc *sc, uint32_t int_frames) +{ + uint32_t ms = 1; + uint32_t fps = usbd_get_isoc_fps(sc->sc_udev); + /* Make sure a whole USB transfer interval fits into the buffer. */ + if (fps >= 1000 && int_frames > 0 && int_frames <= UAUDIO_NFRAMES) { + /* Convert interval frames to milliseconds. */ + ms = ((int_frames * 1000) / fps); + } + /* Respect minimum buffer length set through buffer_ms tunable. */ + if (ms < uaudio_buffer_ms) + ms = uaudio_buffer_ms; + /* Limit buffer length to 8 milliseconds. */ + if (ms > UAUDIO_BUFFER_MS_MAX) + ms = UAUDIO_BUFFER_MS_MAX; + return (ms); +} + static uint32_t uaudio_get_buffer_size(struct uaudio_chan *ch, uint8_t alt) { struct uaudio_chan_alt *chan_alt = &ch->usb_alt[alt]; - /* We use 2 times 8ms of buffer */ - uint32_t buf_size = chan_alt->sample_size * - howmany(chan_alt->sample_rate * (UAUDIO_NFRAMES / 8), 1000); + uint32_t int_frames, ms, buf_size; + /* USB transfer interval in frames, from endpoint descriptor. */ + int_frames = uaudio_get_interval_frames(chan_alt->p_ed1); + /* Buffer length in milliseconds, and in bytes of audio data. */ + ms = uaudio_get_buffer_ms(ch->priv_sc, int_frames); + buf_size = chan_alt->sample_size * + howmany(chan_alt->sample_rate * ms, 1000); + return (buf_size); +} + +static uint32_t +uaudio_max_buffer_size(struct uaudio_chan *ch, uint8_t alt) +{ + struct uaudio_chan_alt *chan_alt = &ch->usb_alt[alt]; + uint32_t buf_size; + /* Maximum buffer length is 8 milliseconds. */ + buf_size = chan_alt->sample_size * + howmany(chan_alt->sample_rate * UAUDIO_BUFFER_MS_MAX, 1000); return (buf_size); } @@ -2626,7 +2676,7 @@ uaudio_chan_init(struct uaudio_chan *ch, struct snd_dbuf *b, buf_size = 0; for (x = 0; x != ch->num_alt; x++) { - uint32_t temp = uaudio_get_buffer_size(ch, x); + uint32_t temp = uaudio_max_buffer_size(ch, x); if (temp > buf_size) buf_size = temp; }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202402262322.41QNMo1E028602>