Skip to contentSkip to conversation

ALSA: usb-audio: Delay account cleanup and latency reduction

PATCH New Takashi Iwai 1 participant 6 messages Jun 1, 2021 Sound

lore
Share & tools
lore
#0 lore raw

cover ALSA: usb-audio: Delay account cleanup and latency reduction

Hi,

this is a patch set for USB-audio driver for the code refactoring of PCM delay account as well as the reduction of the playback latency. The last change is a bit intrusive (despite of the smallness), and hopefully it won't break things. My initial tests through various backends were OK, but if anyone finds a problem, please let me know.

Takashi

===

Takashi Iwai (5):
ALSA: usb-audio: Make snd_usb_pcm_delay() static
ALSA: usb-audio: Pre-calculate buffer byte size
ALSA: usb-audio: Refactoring delay account code
ALSA: usb-audio: Factor out DSD bitrev copy function
ALSA: usb-audio: Reduce latency at playback start

 sound/usb/card.h     |   8 +-
 sound/usb/endpoint.c |   1 +
 sound/usb/pcm.c      | 202 ++++++++++++++++++++-----------------------
 sound/usb/pcm.h      |   3 -
 4 files changed, 101 insertions(+), 113 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/5 ALSA: usb-audio: Make snd_usb_pcm_delay() static

It's a local function, let's make it static.

---
 sound/usb/pcm.c | 4 ++--
 sound/usb/pcm.h | 3 ---
 2 files changed, 2 insertions(+), 5 deletions(-)
sound/usb/pcm.c +2 -2
index e5311b6bb3f6..359c759a7023 100644
@@ -29,8 +29,8 @@
 #define SUBSTREAM_FLAG_SYNC_EP_STARTED	1
 
 /* return the estimated delay based on USB frame counters */
-snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
-				    unsigned int rate)
+static snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
+					   unsigned int rate)
 {
 	int current_frame_number;
 	int frame_diff;
sound/usb/pcm.h +0 -3
index 06c586467d3f..493a4e34d78d 100644
@@ -2,9 +2,6 @@
 #ifndef __USBAUDIO_PCM_H
 #define __USBAUDIO_PCM_H
 
-snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
-				    unsigned int rate);
-
 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream);
 int snd_usb_pcm_suspend(struct snd_usb_stream *as);
 int snd_usb_pcm_resume(struct snd_usb_stream *as);
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/5 ALSA: usb-audio: Pre-calculate buffer byte size

In reply to Takashi Iwai

There are a bunch of lines calculating the buffer size in bytes at each time. Keep the value in subs->buffer_bytes and use it consistently for the code simplicity.

---
 sound/usb/card.h |  1 +
 sound/usb/pcm.c  | 48 ++++++++++++++++++++++++------------------------
 2 files changed, 25 insertions(+), 24 deletions(-)
sound/usb/card.h +1 -0
index a741e7da83a2..b346653d4b76 100644
@@ -158,6 +158,7 @@ struct snd_usb_substream {
 
 	unsigned int running: 1;	/* running status */
 
+	unsigned int buffer_bytes;	/* buffer size in bytes */
 	unsigned int hwptr_done;	/* processed byte position in the buffer */
 	unsigned int transfer_done;		/* processed frames since last period update */
 	unsigned int frame_limit;	/* limits number of packets in URB */
sound/usb/pcm.c +24 -24

133 lines not on this page · raw message

Signature · 1 line
2.26.2
#3 lore raw

PATCH 3/5 ALSA: usb-audio: Refactoring delay account code

In reply to Takashi Iwai

The PCM delay accounting in USB-audio driver is a bit complex to follow, and this is an attempt to improve the readability and provide some potential fix.

Basically, the PCM position delay is calculated from two factors: the in-flight data on URBs and the USB frame counter. For the playback stream, we advance the hwptr already at submitting URBs. Those "in-flight" data amount is now tracked, and this is used as the base value for the PCM delay correction. The in-flight data is decreased again at URB completion in return. For the capture stream, OTOH, there is no in-flight data, hence the delay base is zero.

The USB frame counter is used in addition for correcting the current position. The reference frame counter is updated at each submission and receiving time, and the difference from the current counter value is taken into account.

In this patch, each in-flight data bytes is recorded in the new snd_usb_ctx.queued field, and the total in-flight amount is tracked in snd_usb_substream.inflight_bytes field, as the replacement of last_delay field.

Note that updating the hwptr after URB completion doesn't work for PulseAudio who tries to scratch the buffer on the fly; USB-audio is basically a double-buffer implementation, hence the scratching the buffer can't work for the already submitted data. So we always update hwptr beforehand. It's not ideal, but the delay account should give enough correctness.

---
 sound/usb/card.h     |   7 ++-
 sound/usb/endpoint.c |   1 +
 sound/usb/pcm.c      | 128 +++++++++++++++++--------------------------
 3 files changed, 56 insertions(+), 80 deletions(-)
sound/usb/card.h +4 -3
index b346653d4b76..5577a776561b 100644
@@ -54,6 +54,7 @@ struct snd_urb_ctx {
 	struct snd_usb_endpoint *ep;
 	int index;	/* index for urb array */
 	int packets;	/* number of packets per urb */
+	int queued;	/* queued data bytes by this urb */
 	int packet_size[MAX_PACKS_HS]; /* size of packets for next submission */
 	struct list_head ready_list;
 };
@@ -159,8 +160,9 @@ struct snd_usb_substream {
 	unsigned int running: 1;	/* running status */
 
 	unsigned int buffer_bytes;	/* buffer size in bytes */
+	unsigned int inflight_bytes;	/* in-flight data bytes on buffer (for playback) */
 	unsigned int hwptr_done;	/* processed byte position in the buffer */
-	unsigned int transfer_done;		/* processed frames since last period update */
+	unsigned int transfer_done;	/* processed frames since last period update */
 	unsigned int frame_limit;	/* limits number of packets in URB */
 
 	/* data and sync endpoints for this stream */
@@ -175,8 +177,7 @@ struct snd_usb_substream {
 	struct list_head fmt_list;	/* format list */
 	spinlock_t lock;
 
-	int last_frame_number;          /* stored frame number */
-	int last_delay;                 /* stored delay */
+	unsigned int last_frame_number;	/* stored frame number */
 
 	struct {
 		int marker;
sound/usb/endpoint.c +1 -0
index 014c43862826..da649211bff3 100644
@@ -275,6 +275,7 @@ static void prepare_silent_urb(struct snd_usb_endpoint *ep,
 
 	urb->number_of_packets = ctx->packets;
 	urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
+	ctx->queued = 0;
 }
 
 /*
sound/usb/pcm.c +51 -77

235 lines not on this page · raw message

Signature · 1 line
2.26.2
#4 lore raw

PATCH 4/5 ALSA: usb-audio: Factor out DSD bitrev copy function

In reply to Takashi Iwai

Just minor code refactoring. Like DOP DSD code, it can be better in a separate function for code readability.

---
 sound/usb/pcm.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
sound/usb/pcm.c +19 -9
index 8ee45f2e8dce..e26d37365f02 100644
@@ -1250,6 +1250,24 @@ static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
 	urb_ctx_queue_advance(subs, urb, queued);
 }
 
+/* copy bit-reversed bytes onto transfer buffer */
+static void fill_playback_urb_dsd_bitrev(struct snd_usb_substream *subs,
+					 struct urb *urb, unsigned int bytes)
+{
+	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
+	const u8 *src = runtime->dma_area;
+	u8 *buf = urb->transfer_buffer;
+	int i, ofs = subs->hwptr_done;
+
+	for (i = 0; i < bytes; i++) {
+		*buf++ = bitrev8(src[ofs]);
+		if (++ofs >= subs->buffer_bytes)
+			ofs = 0;
+	}
+
+	urb_ctx_queue_advance(subs, urb, bytes);
+}
+
 static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
 			int offset, int stride, unsigned int bytes)
 {
@@ -1360,15 +1378,7 @@ static void prepare_playback_urb(struct snd_usb_substream *subs,
 		fill_playback_urb_dsd_dop(subs, urb, bytes);
 	} else if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U8 &&
 			   subs->cur_audiofmt->dsd_bitrev)) {
-		/* bit-reverse the bytes */
-		u8 *buf = urb->transfer_buffer;
-		for (i = 0; i < bytes; i++) {
-			int idx = (subs->hwptr_done + i) % subs->buffer_bytes;
-
-			buf[i] = bitrev8(runtime->dma_area[idx]);
-		}
-
-		urb_ctx_queue_advance(subs, urb, bytes);
+		fill_playback_urb_dsd_bitrev(subs, urb, bytes);
 	} else {
 		/* usual PCM */
 		if (!subs->tx_length_quirk)
Signature · 1 line
2.26.2
#5 lore raw

PATCH 5/5 ALSA: usb-audio: Reduce latency at playback start

In reply to Takashi Iwai

USB-audio driver behaves a bit strangely for the playback stream -- namely, it starts sending silent packets at PCM prepare state while the actual data is submitted at first when the trigger START is kicked off. This is a workaround for the behavior where URBs are processed too quickly at the beginning. That is, if we start submitting URBs at trigger START, the first few URBs will be immediately completed, and this would result in the immediate period-elapsed calls right after the start, which may confuse applications.

OTOH, submitting the data after silent URBs would, of course, result in a certain delay of the actual data processing, and this is rather more serious problem on modern systems, in practice.

This patch tries to revert the workaround and lets the URB submission starting at PCM trigger for the playback again. As far as I've tested with various backends (native ALSA, PA, JACK, PW), I haven't seen any problems (famous last words :)

Note that the capture stream handling needs no such workaround, since the capture is driven per received URB.

---
 sound/usb/pcm.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
sound/usb/pcm.c +9 -5
index e26d37365f02..c66831ee15f9 100644
@@ -613,11 +613,6 @@ static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
 	subs->last_frame_number = 0;
 	runtime->delay = 0;
 
-	/* for playback, submit the URBs now; otherwise, the first hwptr_done
-	 * updates for all URBs would happen at the same time when starting */
-	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
-		ret = start_endpoints(subs);
-
  unlock:
 	snd_usb_unlock_shutdown(chip);
 	return ret;
@@ -1430,6 +1425,7 @@ static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substrea
 					      int cmd)
 {
 	struct snd_usb_substream *subs = substream->runtime->private_data;
+	int err;
 
 	switch (cmd) {
 	case SNDRV_PCM_TRIGGER_START:
@@ -1440,6 +1436,14 @@ static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substrea
 					      prepare_playback_urb,
 					      retire_playback_urb,
 					      subs);
+		if (cmd == SNDRV_PCM_TRIGGER_START) {
+			err = start_endpoints(subs);
+			if (err < 0) {
+				snd_usb_endpoint_set_callback(subs->data_endpoint,
+							      NULL, NULL, NULL);
+				return err;
+			}
+		}
 		subs->running = 1;
 		dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n",
 			subs->cur_audiofmt->iface,
Signature · 1 line
2.26.2