Skip to contentSkip to conversation

snd_pcm_drain support

PATCH Superseded [email protected] 2 participants 9 messages Jul 3, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover snd_pcm_drain support

The following patches are required to support draining with the JACK IO plugin. Without these patches

$ speaker-test -D jack -c2
will freeze.

It is required to support the draining feature implemented in commit ce2095c41f2 (pcm: ioplug: Implement proper drain behavior)

#1 lore raw

PATCH 1/1 pcm: ioplug: Provide avail helper function for plugins

From: Timo Wischer <[email protected]>

This function can be called without calling snd_pcm_avail_update().

The call to snd_pcm_avail_update() can take some time. Therefore some developers would not like to call it from a real-time context (e.g. from JACK client context).

include/pcm_ioplug.h +3 -0
index c1310e3..b16fc8b 100644
@@ -235,6 +235,9 @@ int snd_pcm_ioplug_set_param_list(snd_pcm_ioplug_t *io, int type, unsigned int n
 int snd_pcm_ioplug_set_state(snd_pcm_ioplug_t *ioplug, snd_pcm_state_t state);
 
 /* calucalte the available frames */
+snd_pcm_uframes_t snd_pcm_ioplug_avail(const snd_pcm_ioplug_t * const ioplug,
+				       const snd_pcm_uframes_t hw_ptr,
+				       const snd_pcm_uframes_t appl_ptr);
 snd_pcm_uframes_t snd_pcm_ioplug_hw_avail(const snd_pcm_ioplug_t * const ioplug,
 					  const snd_pcm_uframes_t hw_ptr,
 					  const snd_pcm_uframes_t appl_ptr);
src/pcm/pcm_ioplug.c +18 -2
index 4d44ae2..6d52c27 100644
@@ -1221,6 +1221,21 @@ int snd_pcm_ioplug_set_state(snd_pcm_ioplug_t *ioplug, snd_pcm_state_t state)
  * \param ioplug the ioplug handle
  * \param hw_ptr hardware pointer in frames
  * \param appl_ptr application pointer in frames
+ * \return available frames for the application
+ */
+snd_pcm_uframes_t snd_pcm_ioplug_avail(const snd_pcm_ioplug_t * const ioplug,
+				       const snd_pcm_uframes_t hw_ptr,
+				       const snd_pcm_uframes_t appl_ptr)
+{
+	return __snd_pcm_avail(ioplug->pcm, hw_ptr, appl_ptr);
+}
+
+/**
+ * \brief Get the available frames. This function can be used to calculate the
+ * the available frames before calling #snd_pcm_avail_update()
+ * \param ioplug the ioplug handle
+ * \param hw_ptr hardware pointer in frames
+ * \param appl_ptr application pointer in frames
  * \return available frames for the hardware
  */
 snd_pcm_uframes_t snd_pcm_ioplug_hw_avail(const snd_pcm_ioplug_t * const ioplug,
@@ -1230,8 +1245,9 @@ snd_pcm_uframes_t snd_pcm_ioplug_hw_avail(const snd_pcm_ioplug_t * const ioplug,
 	/* available data/space which can be transferred by the user
 	 * application
 	 */
-	const snd_pcm_uframes_t user_avail = __snd_pcm_avail(ioplug->pcm,
-							     hw_ptr, appl_ptr);
+	const snd_pcm_uframes_t user_avail = snd_pcm_ioplug_avail(ioplug,
+								  hw_ptr,
+								  appl_ptr);
 
 	if (user_avail > ioplug->pcm->buffer_size) {
 		/* there was an Xrun */
Signature · 1 line
2.7.4
#2 lore raw

PATCH 1/2 jack: Avoid call to snd_pcm_avail_update() from JACK thread

In reply to [email protected]

From: Timo Wischer <[email protected]>

snd_pcm_avail_update() can call snd_pcm_jack_stop() but snd_pcm_jack_stop() should not be called by the JACK thread. It should only be called by the thread how has called snd_pcm_jack_start().

In addition the execution of snd_pcm_avail_update() can take a while. Therefore it should not be called by the JACK thread to not block this thread.

jack/pcm_jack.c +2 -2
index e3df4d2..b643d93 100644
@@ -67,7 +67,7 @@ static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
 
 	if (io->state == SND_PCM_STATE_RUNNING ||
 	    (io->state == SND_PCM_STATE_PREPARED && io->stream == SND_PCM_STREAM_CAPTURE)) {
-		avail = snd_pcm_avail_update(io->pcm);
+		avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
 		if (avail >= 0 && avail < jack->min_avail) {
 			while (read(io->poll_fd, &buf, sizeof(buf)) == sizeof(buf))
 				;
@@ -84,7 +84,7 @@ static int pcm_poll_unblock_check(snd_pcm_ioplug_t *io)
 	snd_pcm_sframes_t avail;
 	snd_pcm_jack_t *jack = io->private_data;
 
-	avail = snd_pcm_avail_update(io->pcm);
+	avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
 	if (avail < 0 || avail >= jack->min_avail) {
 		write(jack->fd, &buf, 1);
 		return 1;
Signature · 1 line
2.7.4
#3 lore raw

Re: [PATCH - JACK IO plug 1/2] jack: Avoid call to snd_pcm_avail_update() from JACK thread

Quoted 8 lines · On Tue, 03 Jul 2018 13:58:06 +0200, <[email protected]> wrote:

From: Timo Wischer <[email protected]>

snd_pcm_avail_update() can call snd_pcm_jack_stop() but snd_pcm_jack_stop() should not be called by the JACK thread. It should only be called by the thread how has called snd_pcm_jack_start().

Quoted 5 lines

In addition the execution of snd_pcm_avail_update() can take a while. Therefore it should not be called by the JACK thread to not block this thread.

With that change, avail cannot be a negative, as snd_pcm_ioplug_avail() returns snd_pcm_uframes_t.
A couple of checks are redundant and confuse readers as if it's still a signed value.

Takashi

Quoted 26 lines
jack/pcm_jack.c +2 -2
index e3df4d2..b643d93 100644
@@ -67,7 +67,7 @@ static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
 
 	if (io->state == SND_PCM_STATE_RUNNING ||
 	    (io->state == SND_PCM_STATE_PREPARED && io->stream == SND_PCM_STREAM_CAPTURE)) {
-		avail = snd_pcm_avail_update(io->pcm);
+		avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
 		if (avail >= 0 && avail < jack->min_avail) {
 			while (read(io->poll_fd, &buf, sizeof(buf)) == sizeof(buf))
 				;
@@ -84,7 +84,7 @@ static int pcm_poll_unblock_check(snd_pcm_ioplug_t *io)
 	snd_pcm_sframes_t avail;
 	snd_pcm_jack_t *jack = io->private_data;
 
-	avail = snd_pcm_avail_update(io->pcm);
+	avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
 	if (avail < 0 || avail >= jack->min_avail) {
 		write(jack->fd, &buf, 1);
 		return 1;
Signature · 1 line
2.7.4
#4 lore raw

PATCH 1/2 jack: Avoid call to snd_pcm_avail_update() from JACK thread

From: Timo Wischer <[email protected]>

snd_pcm_avail_update() can call snd_pcm_jack_stop() but snd_pcm_jack_stop() should not be called by the JACK thread. It should only be called by the thread how has called snd_pcm_jack_start().

In addition the execution of snd_pcm_avail_update() can take a while. Therefore it should not be called by the JACK thread to not block this thread.

jack/pcm_jack.c +6 -6
index e3df4d2..5a57e13 100644
@@ -62,13 +62,13 @@ static int snd_pcm_jack_stop(snd_pcm_ioplug_t *io);
 static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
 {
 	static char buf[32];
-	snd_pcm_sframes_t avail;
+	snd_pcm_uframes_t avail;
 	snd_pcm_jack_t *jack = io->private_data;
 
 	if (io->state == SND_PCM_STATE_RUNNING ||
 	    (io->state == SND_PCM_STATE_PREPARED && io->stream == SND_PCM_STREAM_CAPTURE)) {
-		avail = snd_pcm_avail_update(io->pcm);
-		if (avail >= 0 && avail < jack->min_avail) {
+		avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
+		if (avail < jack->min_avail) {
 			while (read(io->poll_fd, &buf, sizeof(buf)) == sizeof(buf))
 				;
 			return 1;
@@ -81,11 +81,11 @@ static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
 static int pcm_poll_unblock_check(snd_pcm_ioplug_t *io)
 {
 	static char buf[1];
-	snd_pcm_sframes_t avail;
+	snd_pcm_uframes_t avail;
 	snd_pcm_jack_t *jack = io->private_data;
 
-	avail = snd_pcm_avail_update(io->pcm);
-	if (avail < 0 || avail >= jack->min_avail) {
+	avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
+	if (avail >= jack->min_avail) {
 		write(jack->fd, &buf, 1);
 		return 1;
 	}
Signature · 1 line
2.7.4
#5 lore raw

Re: [PATCH - JACK IO plug 1/2] jack: Avoid call to snd_pcm_avail_update() from JACK thread

Quoted 8 lines · On Tue, 03 Jul 2018 14:23:59 +0200, <[email protected]> wrote:

From: Timo Wischer <[email protected]>

snd_pcm_avail_update() can call snd_pcm_jack_stop() but snd_pcm_jack_stop() should not be called by the JACK thread. It should only be called by the thread how has called snd_pcm_jack_start().

Quoted 5 lines

In addition the execution of snd_pcm_avail_update() can take a while. Therefore it should not be called by the JACK thread to not block this thread.

Please put "v2" into the subject prefix, and give a brief information what changed from v1 to v2. And, don't attach to the old mail thread. As of this post, it's utterly confusing which patch is which version!

thanks,

Takashi

Quoted 45 lines
jack/pcm_jack.c +6 -6
index e3df4d2..5a57e13 100644
@@ -62,13 +62,13 @@ static int snd_pcm_jack_stop(snd_pcm_ioplug_t *io);
 static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
 {
 	static char buf[32];
-	snd_pcm_sframes_t avail;
+	snd_pcm_uframes_t avail;
 	snd_pcm_jack_t *jack = io->private_data;
 
 	if (io->state == SND_PCM_STATE_RUNNING ||
 	    (io->state == SND_PCM_STATE_PREPARED && io->stream == SND_PCM_STREAM_CAPTURE)) {
-		avail = snd_pcm_avail_update(io->pcm);
-		if (avail >= 0 && avail < jack->min_avail) {
+		avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
+		if (avail < jack->min_avail) {
 			while (read(io->poll_fd, &buf, sizeof(buf)) == sizeof(buf))
 				;
 			return 1;
@@ -81,11 +81,11 @@ static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
 static int pcm_poll_unblock_check(snd_pcm_ioplug_t *io)
 {
 	static char buf[1];
-	snd_pcm_sframes_t avail;
+	snd_pcm_uframes_t avail;
 	snd_pcm_jack_t *jack = io->private_data;
 
-	avail = snd_pcm_avail_update(io->pcm);
-	if (avail < 0 || avail >= jack->min_avail) {
+	avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
+	if (avail >= jack->min_avail) {
 		write(jack->fd, &buf, 1);
 		return 1;
 	}
Signature · 6 lines
2.7.4

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#6 lore raw

PATCH 2/2 jack: Update poll_fd also in draining state to support snd_pcm_drain

In reply to [email protected]

From: Timo Wischer <[email protected]>

jack/pcm_jack.c +10 -2
index b643d93..0d1ccb9 100644
@@ -66,6 +66,7 @@ static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
 	snd_pcm_jack_t *jack = io->private_data;
 
 	if (io->state == SND_PCM_STATE_RUNNING ||
+	    io->state == SND_PCM_STATE_DRAINING ||
 	    (io->state == SND_PCM_STATE_PREPARED && io->stream == SND_PCM_STREAM_CAPTURE)) {
 		avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
 		if (avail >= 0 && avail < jack->min_avail) {
@@ -85,7 +86,13 @@ static int pcm_poll_unblock_check(snd_pcm_ioplug_t *io)
 	snd_pcm_jack_t *jack = io->private_data;
 
 	avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
-	if (avail < 0 || avail >= jack->min_avail) {
+	/* In draining state poll_fd is used to wait
+	 * till all pending frames are played.
+	 * Therefore it has to be guarantee that a poll event is also generated
+	 * if the buffer contains less than min_avail frames
+	 */
+	if (avail < 0 || avail >= jack->min_avail ||
+	    io->state == SND_PCM_STATE_DRAINING) {
 		write(jack->fd, &buf, 1);
 		return 1;
 	}
@@ -161,7 +168,8 @@ snd_pcm_jack_process_cb(jack_nframes_t nframes, snd_pcm_ioplug_t *io)
 		jack->areas[channel].step = jack->sample_bits;
 	}
 
-	if (io->state == SND_PCM_STATE_RUNNING) {
+	if (io->state == SND_PCM_STATE_RUNNING ||
+	    io->state == SND_PCM_STATE_DRAINING) {
 		snd_pcm_uframes_t hw_ptr = jack->hw_ptr;
 		const snd_pcm_uframes_t hw_avail = snd_pcm_ioplug_hw_avail(io, hw_ptr,
 									   io->appl_ptr);
Signature · 1 line
2.7.4
#7 lore raw

PATCH 2/2 jack: Update poll_fd also in draining state to support snd_pcm_drain

From: Timo Wischer <[email protected]>

jack/pcm_jack.c +9 -2
index 5a57e13..98a6f7e 100644
@@ -66,6 +66,7 @@ static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
 	snd_pcm_jack_t *jack = io->private_data;
 
 	if (io->state == SND_PCM_STATE_RUNNING ||
+	    io->state == SND_PCM_STATE_DRAINING ||
 	    (io->state == SND_PCM_STATE_PREPARED && io->stream == SND_PCM_STREAM_CAPTURE)) {
 		avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
 		if (avail < jack->min_avail) {
@@ -85,7 +86,12 @@ static int pcm_poll_unblock_check(snd_pcm_ioplug_t *io)
 	snd_pcm_jack_t *jack = io->private_data;
 
 	avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
-	if (avail >= jack->min_avail) {
+	/* In draining state poll_fd is used to wait
+	 * till all pending frames are played.
+	 * Therefore it has to be guarantee that a poll event is also generated
+	 * if the buffer contains less than min_avail frames
+	 */
+	if (avail >= jack->min_avail || io->state == SND_PCM_STATE_DRAINING) {
 		write(jack->fd, &buf, 1);
 		return 1;
 	}
@@ -161,7 +167,8 @@ snd_pcm_jack_process_cb(jack_nframes_t nframes, snd_pcm_ioplug_t *io)
 		jack->areas[channel].step = jack->sample_bits;
 	}
 
-	if (io->state == SND_PCM_STATE_RUNNING) {
+	if (io->state == SND_PCM_STATE_RUNNING ||
+	    io->state == SND_PCM_STATE_DRAINING) {
 		snd_pcm_uframes_t hw_ptr = jack->hw_ptr;
 		const snd_pcm_uframes_t hw_avail = snd_pcm_ioplug_hw_avail(io, hw_ptr,
 									   io->appl_ptr);
Signature · 1 line
2.7.4
#8 lore raw

Re: [PATCH - JACK IO plug 2/2] jack: Update poll_fd also in draining state to support snd_pcm_drain

Quoted 4 lines · On Tue, 03 Jul 2018 14:26:54 +0200, <[email protected]> wrote:

From: Timo Wischer <[email protected]>

Please give some change log text to explain why the patch is needed and what it does. The empty change log indicates already something wrong unless it's a really trivial change.

thanks,

Takashi

Quoted 45 lines
jack/pcm_jack.c +9 -2
index 5a57e13..98a6f7e 100644
@@ -66,6 +66,7 @@ static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
 	snd_pcm_jack_t *jack = io->private_data;
 
 	if (io->state == SND_PCM_STATE_RUNNING ||
+	    io->state == SND_PCM_STATE_DRAINING ||
 	    (io->state == SND_PCM_STATE_PREPARED && io->stream == SND_PCM_STREAM_CAPTURE)) {
 		avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
 		if (avail < jack->min_avail) {
@@ -85,7 +86,12 @@ static int pcm_poll_unblock_check(snd_pcm_ioplug_t *io)
 	snd_pcm_jack_t *jack = io->private_data;
 
 	avail = snd_pcm_ioplug_avail(io, jack->hw_ptr, io->appl_ptr);
-	if (avail >= jack->min_avail) {
+	/* In draining state poll_fd is used to wait
+	 * till all pending frames are played.
+	 * Therefore it has to be guarantee that a poll event is also generated
+	 * if the buffer contains less than min_avail frames
+	 */
+	if (avail >= jack->min_avail || io->state == SND_PCM_STATE_DRAINING) {
 		write(jack->fd, &buf, 1);
 		return 1;
 	}
@@ -161,7 +167,8 @@ snd_pcm_jack_process_cb(jack_nframes_t nframes, snd_pcm_ioplug_t *io)
 		jack->areas[channel].step = jack->sample_bits;
 	}
 
-	if (io->state == SND_PCM_STATE_RUNNING) {
+	if (io->state == SND_PCM_STATE_RUNNING ||
+	    io->state == SND_PCM_STATE_DRAINING) {
 		snd_pcm_uframes_t hw_ptr = jack->hw_ptr;
 		const snd_pcm_uframes_t hw_avail = snd_pcm_ioplug_hw_avail(io, hw_ptr,
 									   io->appl_ptr);
Signature · 6 lines
2.7.4

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel