Skip to contentSkip to conversation

ALSA: compress: Document stream states and fix gapless SM

PATCH Superseded Vinod Koul 2 participants 8 messages Jun 29, 2020

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Vinod Koul

author maintainer
#0 lore raw

v4 cover ALSA: compress: Document stream states and fix gapless SM

Srini found issue with gapless implementation which prompted to look deeper into SM for compressed stream.

So documenting SM was first step, so first two patches add that. Last patch fixes the issue by keeping track on partial_drain and then moving state to 'running' in snd_compr_drain_notify() for partial_drain case on success. While at it, noticed snd_compr_drain_notify() is lockless state change, so fixed that as well.

I have tested this on Dragon board RB3, compressed audio works out of the box on that platform and Srini will send driver and fcplay patches for gapless soon.

Changes in v4:
- Add review tag by Charles
- fix the lock deadlock pointed by Charles

Changes in v3:
- Add pause->stop->free transition
- Add setup->free transition
- remove running->free as that goes thru stop

Changes in v2:
- Added tested tag by Srini
- Update compress SM with Free state and compr_stop() transitions

Vinod Koul (3):
ALSA: compress: document the compress audio state machine
ALSA: compress: document the compress gapless audio state machine
ALSA: compress: fix partial_drain completion state

 .../sound/designs/compress-offload.rst        | 83 +++++++++++++++++++
 include/sound/compress_driver.h               | 10 ++-
 sound/core/compress_offload.c                 |  4 +
 3 files changed, 96 insertions(+), 1 deletion(-)
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

PATCH v4 1/3 ALSA: compress: document the compress audio state machine

So we had some discussions of the stream states, so I thought it is a good idea to document the state transitions, so add it documentation

---
 .../sound/designs/compress-offload.rst        | 51 +++++++++++++++++++
 1 file changed, 51 insertions(+)
Documentation/sound/designs/compress-offload.rst +51 -0
index ad4bfbdacc83..b6e9025ae105 100644
@@ -151,6 +151,57 @@ Modifications include:
 - Addition of encoding options when required (derived from OpenMAX IL)
 - Addition of rateControlSupported (missing in OpenMAX AL)
 
+State Machine
+=============
+
+The compressed audio stream state machine is described below ::
+
+                                        +----------+
+                                        |          |
+                                        |   OPEN   |
+                                        |          |
+                                        +----------+
+                                             |
+                                             |
+                                             | compr_set_params()
+                                             |
+                                             v
+         compr_free()                   +----------+
+  +------------------------------------|          |
+  |                                    |   SETUP  |
+  |          +------------------------>|          |<-------------------------+
+  |          | compr_drain_notify()    +----------+                          |
+  |          |         or                   |                                |
+  |          |      compr_stop()            |                                |
+  |          |                              | compr_write()                  |
+  |          |                              |                                |
+  |          |                              v                                |
+  |          |                         +----------+                          |
+  |          |                         |          |   compr_free()           |
+  |          |                         |  PREPARE |---------------> A        |
+  |          |                         |          |                          |
+  |          |                         +----------+                          |
+  |          |                              |                                |
+  |          |                              |                                |
+  |          |                              | compr_start()                  |
+  |          |                              |                                |
+  |          |                              v                                |
+  |    +----------+                    +----------+                          |
+  |    |          |    compr_drain()   |          |        compr_stop()      |
+  |    |  DRAIN   |<-------------------|  RUNNING |--------------------------+
+  |    |          |                    |          |                          |
+  |    +----------+                    +----------+                          |
+  |                                       |    ^                             |
+  |          A                            |    |                             |
+  |          |              compr_pause() |    | compr_resume()              |
+  |          |                            |    |                             |
+  |          v                            v    |                             |
+  |    +----------+                   +----------+                           |
+  |    |          |                   |          |         compr_stop()      |
+  +--->|   FREE   |                   |  PAUSE   |---------------------------+
+       |          |                   |          |
+       +----------+                   +----------+
+
 
 Gapless Playback
 ================
Signature · 1 line
2.26.2
author maintainer
#6 lore raw

PATCH v4 2/3 ALSA: compress: document the compress gapless audio state machine

In reply to Vinod Koul

Also documented the galpess transitions. Please note that these are not really stream states, but show how the stream steps in gapless mode

---
 .../sound/designs/compress-offload.rst        | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
Documentation/sound/designs/compress-offload.rst +32 -0
index b6e9025ae105..82950dbbc4a9 100644
@@ -250,6 +250,38 @@ Sequence flow for gapless would be:
 
 (note: order for partial_drain and write for next track can be reversed as well)
 
+Gapless Playback SM
+===================
+
+For Gapless, we move from running state to partial drain and back, along
+with setting of meta_data and signalling for next track ::
+
+
+                                        +----------+
+                compr_drain_notify()    |          |
+              +------------------------>|  RUNNING |
+              |                         |          |
+              |                         +----------+
+              |                              |
+              |                              |
+              |                              | compr_next_track()
+              |                              |
+              |                              V
+              |                         +----------+
+              |                         |          |
+              |                         |NEXT_TRACK|
+              |                         |          |
+              |                         +----------+
+              |                              |
+              |                              |
+              |                              | compr_partial_drain()
+              |                              |
+              |                              V
+              |                         +----------+
+              |                         |          |
+              +------------------------ | PARTIAL_ |
+                                        |  DRAIN   |
+                                        +----------+
 
 Not supported
 =============
Signature · 1 line
2.26.2
author maintainer
#7 lore raw

PATCH v4 3/3 ALSA: compress: fix partial_drain completion state

In reply to Vinod Koul

On partial_drain completion we should be in SNDRV_PCM_STATE_RUNNING state, so set that for partially draining streams in snd_compr_drain_notify() and use a flag for partially draining streams

While at it, add locks for stream state change in
snd_compr_drain_notify() as well.

Fixes f44f2a5417b2 ("ALSA: compress: fix drain calls blocking other compress functions (v6)")
---
 include/sound/compress_driver.h | 10 +++++++++-
 sound/core/compress_offload.c   |  4 ++++
 2 files changed, 13 insertions(+), 1 deletion(-)
include/sound/compress_driver.h +9 -1
index 3df8d8c90191..8d23351f7ad7 100644
@@ -66,6 +66,7 @@ struct snd_compr_runtime {
  * @direction: stream direction, playback/recording
  * @metadata_set: metadata set flag, true when set
  * @next_track: has userspace signal next track transition, true when set
+ * @partial_drain: undergoing partial_drain for stream, true when set
  * @private_data: pointer to DSP private data
  * @dma_buffer: allocated buffer if any
  */
@@ -78,6 +79,7 @@ struct snd_compr_stream {
 	enum snd_compr_direction direction;
 	bool metadata_set;
 	bool next_track;
+	bool partial_drain;
 	void *private_data;
 	struct snd_dma_buffer dma_buffer;
 };
@@ -187,7 +189,13 @@ static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
 	if (snd_BUG_ON(!stream))
 		return;
 
-	stream->runtime->state = SNDRV_PCM_STATE_SETUP;
+	/* for partial_drain case we are back to running state on success */
+	if (stream->partial_drain) {
+		stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
+		stream->partial_drain = false; /* clear this flag as well */
+	} else {
+		stream->runtime->state = SNDRV_PCM_STATE_SETUP;
+	}
 
 	wake_up(&stream->runtime->sleep);
 }
sound/core/compress_offload.c +4 -0
index e618580feac4..1c4b2cf450a0 100644
@@ -803,6 +803,9 @@ static int snd_compr_stop(struct snd_compr_stream *stream)
 
 	retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
 	if (!retval) {
+		/* clear flags and stop any drain wait */
+		stream->partial_drain = false;
+		stream->metadata_set = false;
 		snd_compr_drain_notify(stream);
 		stream->runtime->total_bytes_available = 0;
 		stream->runtime->total_bytes_transferred = 0;
@@ -960,6 +963,7 @@ static int snd_compr_partial_drain(struct snd_compr_stream *stream)
 	if (stream->next_track == false)
 		return -EPERM;
 
+	stream->partial_drain = true;
 	retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
 	if (retval) {
 		pr_debug("Partial drain returned failure\n");
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH v4 1/3] ALSA: compress: document the compress audio state machine

In reply to Vinod Koul
Quoted 62 lines · Vinod Koul

So we had some discussions of the stream states, so I thought it is a good idea to document the state transitions, so add it documentation

---
  .../sound/designs/compress-offload.rst        | 51 +++++++++++++++++++
  1 file changed, 51 insertions(+)
Documentation/sound/designs/compress-offload.rst +44 -0
index ad4bfbdacc83..b6e9025ae105 100644
@@ -151,6 +151,57 @@ Modifications include:
  - Addition of encoding options when required (derived from OpenMAX IL)
  - Addition of rateControlSupported (missing in OpenMAX AL)
 
+State Machine
+=============
+
+The compressed audio stream state machine is described below ::
+
+                                        +----------+
+                                        |          |
+                                        |   OPEN   |
+                                        |          |
+                                        +----------+
+                                             |
+                                             |
+                                             | compr_set_params()
+                                             |
+                                             v
+         compr_free()                   +----------+
+  +------------------------------------|          |
+  |                                    |   SETUP  |
+  |          +------------------------>|          |<-------------------------+
+  |          | compr_drain_notify()    +----------+                          |
+  |          |         or                   ^                                |
+  |          |      compr_stop()            |                                |
+  |          |                              | compr_write()                  |
+  |          |                              |                                |
+  |          |                              |                                |
+  |          |                         +----------+                          |
+  |          |                         |          |   compr_free()           |
+  |          |                         |  PREPARE |---------------> A        |
+  |          |                         |          |                          |
+  |          |                         +----------+                          |
+  |          |                              |                                |
+  |          |                              |                                |
+  |          |                              | compr_start()                  |
+  |          |                              |                                |
+  |          |                              v                                |
+  |    +----------+                    +----------+                          |
+  |    |          |    compr_drain()   |          |        compr_stop()      |
+  |    |  DRAIN   |<-------------------|  RUNNING |--------------------------+
+  |    |          |                    |          |                          |
+  |    +----------+                    +----------+                          |
+  |                                       |    ^                             |
+  |          A                            |    |                             |
+  |          |              compr_pause() |    | compr_resume()              |
+  |          |                            |    |                             |

Documentation/sound/designs/compress-offload.rst Modifications include: · line 207

Quoted 10 lines
Documentation/sound/designs/compress-offload.rst +7 -0
+  |          v                            v    |                             |
+  |    +----------+                   +----------+                           |
+  |    |          |                   |          |         compr_stop()      |
+  +--->|   FREE   |                   |  PAUSE   |---------------------------+
+       |          |                   |          |
+       +----------+                   +----------+
+
 
  Gapless Playback
  ================

Line containing compr_free (between SETUP and FREE) seems to be misaligned?

If you move prepare to the left and drain in place of drain, it feels like you won't need this weird indirection with A Something like:

Quoted 29 lines
Quoted 29 lines
(quoted diff) +29 -0
+                                             v
+         compr_free()                   +----------+
+  +------------------------------------|          |
+  |                                    |   SETUP  |
+  |          +------------------------>|          |<-------------------------+
+  |          | compr_write()           +----------+                          |
+  |          |                              ^                                |
+  |          |                              | compr_drain_notify() or        |
+  |          |                              | compr_stop()                   |
+  |          |                              |                                |
+  |          |                              |                                |
+  |          |                         +----------+                          |
+  |          |                         |          |                          |
+  |          |                         |  DRAIN   |                          |
+  |          |                         |          |                          |
+  |          |                         +----------+                          |
+  |          |                              ^                                |
+  |          |                              |                                |
+  |          |                              | compr_drain()                  |
+  |          |                              |                                |
+  |          |                              |                                |
+  |    +----------+                    +----------+                          |
+  |    |          |    compr_start()   |          |        compr_stop()      |
+  |    |  PREPARE |------------------->|  RUNNING |--------------------------+
+  |    |          |                    |          |                          |
+  |    +----------+                    +----------+                          |
+  |          |                            |    ^                             |
+  |          | compr_free()               |    |                             |
+  |          |              compr_pause() |    | compr_resume()              |
Quoted 8 lines

Documentation/sound/designs/compress-offload.rst Modifications include: · line 204

Quoted 8 lines
Documentation/sound/designs/compress-offload.rst +8 -0
+  |          |                            |    |                             |
+  |          v                            v    |                             |
+  |    +----------+                   +----------+                           |
+  |    |          |                   |          |         compr_stop()      |
+  +--->|   FREE   |                   |  PAUSE   |---------------------------+
+       |          |                   |          |
+       +----------+                   +----------+
+

but this makes me question PREPARE state, how do you enter it?

author maintainer
#3 lore raw

Re: [PATCH v4 1/3] ALSA: compress: document the compress audio state machine

Hi Amadeusz,

Quoted 36 lines · Amadeusz Sławiński
Quoted 33 lines · Vinod Koul

So we had some discussions of the stream states, so I thought it is a good idea to document the state transitions, so add it documentation

---
  .../sound/designs/compress-offload.rst        | 51 +++++++++++++++++++
  1 file changed, 51 insertions(+)
Documentation/sound/designs/compress-offload.rst +17 -0
index ad4bfbdacc83..b6e9025ae105 100644
@@ -151,6 +151,57 @@ Modifications include:
  - Addition of encoding options when required (derived from OpenMAX IL)
  - Addition of rateControlSupported (missing in OpenMAX AL)
+State Machine
+=============
+
+The compressed audio stream state machine is described below ::
+
+                                        +----------+
+                                        |          |
+                                        |   OPEN   |
+                                        |          |
+                                        +----------+
+                                             |
+                                             |
+                                             | compr_set_params()
+                                             |
+                                             v
+         compr_free()                   +----------+
+  +------------------------------------|          |
Quoted 8 lines

Documentation/sound/designs/compress-offload.rst Modifications include: · line 184

Quoted 8 lines
Documentation/sound/designs/compress-offload.rst +8 -0
+  |                                    |   SETUP  |
+  |          +------------------------>|          |<-------------------------+
+  |          | compr_drain_notify()    +----------+                          |
+  |          |         or                   ^                                |
+  |          |      compr_stop()            |                                |
+  |          |                              | compr_write()                  |
+  |          |                              |                                |
+  |          |                              |                                |

Not sure, this is not in the version I posted, arrow is from setup -> prepare. See https://lore.kernel.org/alsa-devel/[email protected]/

I don't know how it got reversed in your version, maybe the MUA messed up??

Quoted 30 lines
Quoted 19 lines
Documentation/sound/designs/compress-offload.rst +19 -0
+  |          |                         +----------+                          |
+  |          |                         |          |   compr_free()           |
+  |          |                         |  PREPARE |---------------> A        |
+  |          |                         |          |                          |
+  |          |                         +----------+                          |
+  |          |                              |                                |
+  |          |                              |                                |
+  |          |                              | compr_start()                  |
+  |          |                              |                                |
+  |          |                              v                                |
+  |    +----------+                    +----------+                          |
+  |    |          |    compr_drain()   |          |        compr_stop()      |
+  |    |  DRAIN   |<-------------------|  RUNNING |--------------------------+
+  |    |          |                    |          |                          |
+  |    +----------+                    +----------+                          |
+  |                                       |    ^                             |
+  |          A                            |    |                             |
+  |          |              compr_pause() |    | compr_resume()              |
+  |          |                            |    |                             |

Documentation/sound/designs/compress-offload.rst Modifications include:

Quoted 10 lines
Documentation/sound/designs/compress-offload.rst +7 -0
+  |          v                            v    |                             |
+  |    +----------+                   +----------+                           |
+  |    |          |                   |          |         compr_stop()      |
+  +--->|   FREE   |                   |  PAUSE   |---------------------------+
+       |          |                   |          |
+       +----------+                   +----------+
+
  Gapless Playback
  ================
 
Quoted 1 line

Line containing compr_free (between SETUP and FREE) seems to be misaligned?

not in the version I posted and see on lore

Quoted 2 lines

If you move prepare to the left and drain in place of drain, it feels like you won't need this weird indirection with A

yeah that was a compromise to make it bit neater to me than adding overlapping arrows...

Quoted 41 lines

Something like:

Quoted 29 lines
Quoted 29 lines
(quoted diff) +29 -0
+                                             v
+         compr_free()                   +----------+
+  +------------------------------------|          |
+  |                                    |   SETUP  |
+  |          +------------------------>|          |<-------------------------+
+  |          | compr_write()           +----------+                          |
+  |          |                              ^                                |
+  |          |                              | compr_drain_notify() or        |
+  |          |                              | compr_stop()                   |
+  |          |                              |                                |
+  |          |                              |                                |
+  |          |                         +----------+                          |
+  |          |                         |          |                          |
+  |          |                         |  DRAIN   |                          |
+  |          |                         |          |                          |
+  |          |                         +----------+                          |
+  |          |                              ^                                |
+  |          |                              |                                |
+  |          |                              | compr_drain()                  |
+  |          |                              |                                |
+  |          |                              |                                |
+  |    +----------+                    +----------+                          |
+  |    |          |    compr_start()   |          |        compr_stop()      |
+  |    |  PREPARE |------------------->|  RUNNING |--------------------------+
+  |    |          |                    |          |                          |
+  |    +----------+                    +----------+                          |
+  |          |                            |    ^                             |
+  |          | compr_free()               |    |                             |
+  |          |              compr_pause() |    | compr_resume()              |
Quoted 8 lines

Documentation/sound/designs/compress-offload.rst Modifications include: · line 204

Quoted 8 lines
Documentation/sound/designs/compress-offload.rst +8 -0
+  |          |                            |    |                             |
+  |          v                            v    |                             |
+  |    +----------+                   +----------+                           |
+  |    |          |                   |          |         compr_stop()      |
+  +--->|   FREE   |                   |  PAUSE   |---------------------------+
+       |          |                   |          |
+       +----------+                   +----------+
+
Quoted 1 line

but this makes me question PREPARE state, how do you enter it?

a compr_write() moves from SETUP -> PREPARE. Fixing that above looks better version of mine..

Signature · 1 line
~Vinod
#4 lore raw

Re: [PATCH v4 1/3] ALSA: compress: document the compress audio state machine

Quoted 7 lines · Vinod Koul

Not sure, this is not in the version I posted, arrow is from setup -> prepare. See https://lore.kernel.org/alsa-devel/[email protected]/

I don't know how it got reversed in your version, maybe the MUA messed up??

Ah... I must have edited it myself when preparing edited version, sorry for noise ;)

Quoted 2 lines

a compr_write() moves from SETUP -> PREPARE. Fixing that above looks better version of mine..

Yes, I feel it is easier to understand, A redirect confused me for a short bit when I first looked at graph and it can be easily avoided.

author maintainer
#5 lore raw

Re: [PATCH v4 1/3] ALSA: compress: document the compress audio state machine

Quoted 11 lines · Amadeusz Sławiński
Quoted 3 lines · Vinod Koul

Not sure, this is not in the version I posted, arrow is from setup -> prepare. See

https://lore.kernel.org/alsa-devel/[email protected]/

Quoted 4 lines

I don't know how it got reversed in your version, maybe the MUA messed up??

Ah... I must have edited it myself when preparing edited version, sorry for noise ;)

No worries

Quoted 5 lines
Quoted 3 lines

a compr_write() moves from SETUP -> PREPARE. Fixing that above looks better version of mine..

Yes, I feel it is easier to understand, A redirect confused me for a short bit when I first looked at graph and it can be easily avoided.

Thanks for the suggestion, it was a good one indeed, I will send the update shortly

Signature · 1 line
~Vinod