Skip to contentSkip to conversation

jack: Speedup of start/stop/recovery

PATCH In review [email protected] 2 participants 5 messages Dec 21, 2018 – Jan 8, 2019

lore
Share & tools
lore
#0 lore raw

cover jack: Speedup of start/stop/recovery

Hi all,

the following patches minimizes the delay of calling snd_pcm_start() and snd_pcm_stop() for the ALSA JACK plugin. This also makes an Xrun recovery faster.

Best regards

Timo

#1 lore raw

PATCH 1/2 jack: Move jack_activate() and jack_connect() to snd_pcm_jack_prepare()

From: Laxmi Devi <[email protected]>

Since the processing of jack_activate() and jack_connect() take a while longer, snd_pcm_jack_start() was blocked.
Consider a usecase of reading the data from capture device and writing to a playback device, since the capture device is already started and the starting of playback device is blocked, it leads to XRUNs for capture device.
Therefore these calls are moved to snd_pcm_jack_prepare(), So that the capture and playback devices can be prepared in advance so that starting of the device doesn't take too long.

jack/pcm_jack.c +50 -40
index b39835e..19d339d 100644
@@ -222,16 +222,8 @@ snd_pcm_jack_process_cb(jack_nframes_t nframes, snd_pcm_ioplug_t *io)
 					      frames, io->format);
 		}
 
-		if (io->state == SND_PCM_STATE_PREPARED) {
-			/* After activating this JACK client with
-			 * jack_activate() this process callback will be called.
-			 * But the processing of snd_pcm_jack_start() would take
-			 * a while longer due to the jack_connect() calls.
-			 * Therefore the device was already started
-			 * but it is not yet in RUNNING state.
-			 * Due to this expected behaviour it is not an Xrun.
-			 */
-		} else {
+		if (io->state == SND_PCM_STATE_RUNNING ||
+		    io->state == SND_PCM_STATE_DRAINING) {
 			/* report Xrun to user application */
 			jack->xrun_detected = true;
 		}
@@ -242,6 +234,30 @@ snd_pcm_jack_process_cb(jack_nframes_t nframes, snd_pcm_ioplug_t *io)
 	return 0;
 }
 
+static void jack_allocate_and_register_ports(snd_pcm_ioplug_t *io)
+{
+	snd_pcm_jack_t *jack = io->private_data;
+	unsigned int i;
+
+	jack->ports = calloc(io->channels, sizeof(jack_port_t *));
+
+	for (i = 0; i < io->channels; i++) {
+		char port_name[32];
+
+		if (io->stream == SND_PCM_STREAM_PLAYBACK) {
+			sprintf(port_name, "out_%03d", i);
+			jack->ports[i] = jack_port_register(jack->client, port_name,
+							    JACK_DEFAULT_AUDIO_TYPE,
+							    JackPortIsOutput, 0);
+		} else {
+			sprintf(port_name, "in_%03d", i);
+			jack->ports[i] = jack_port_register(jack->client, port_name,
+							    JACK_DEFAULT_AUDIO_TYPE,
+							    JackPortIsInput, 0);
+		}
+	}
+}
+
 static int snd_pcm_jack_prepare(snd_pcm_ioplug_t *io)
 {
 	snd_pcm_jack_t *jack = io->private_data;
@@ -269,38 +285,16 @@ static int snd_pcm_jack_prepare(snd_pcm_ioplug_t *io)
 	else
 		pcm_poll_block_check(io); /* block capture pcm if that's XRUN recovery */
 
-	if (jack->ports)
-		return 0;
-
-	jack->ports = calloc(io->channels, sizeof(jack_port_t*));
-
-	for (i = 0; i < io->channels; i++) {
-		char port_name[32];
-		if (io->stream == SND_PCM_STREAM_PLAYBACK) {
-
-			sprintf(port_name, "out_%03d", i);
-			jack->ports[i] = jack_port_register(jack->client, port_name,
-							    JACK_DEFAULT_AUDIO_TYPE,
-							    JackPortIsOutput, 0);
-		} else {
-			sprintf(port_name, "in_%03d", i);
-			jack->ports[i] = jack_port_register(jack->client, port_name,
-							    JACK_DEFAULT_AUDIO_TYPE,
-							    JackPortIsInput, 0);
-		}
+	if (!jack->ports) {
+		jack_allocate_and_register_ports(io);
+		jack_set_process_callback(jack->client,
+					  (JackProcessCallback)snd_pcm_jack_process_cb, io);
 	}
 
-	jack_set_process_callback(jack->client,
-				  (JackProcessCallback)snd_pcm_jack_process_cb, io);
-	return 0;
-}
+	if (jack->activated)
+		return 0;
 
-static int snd_pcm_jack_start(snd_pcm_ioplug_t *io)
-{
-	snd_pcm_jack_t *jack = io->private_data;
-	unsigned int i;
-	
-	if (jack_activate (jack->client))
+	if (jack_activate(jack->client))
 		return -EIO;
 
 	jack->activated = 1;
@@ -321,7 +315,23 @@ static int snd_pcm_jack_start(snd_pcm_ioplug_t *io)
 			}
 		}
 	}
-	
+	return 0;
+}
+
+static int snd_pcm_jack_start(snd_pcm_ioplug_t *io)
+{
+	(void)io;
+	/*
+	 * Since the processing of jack_activate() and jack_connect() take a
+	 * while longer, snd_pcm_jack_start() was blocked.
+	 * Consider a usecase of reading the data from capture device and
+	 * writing to a playback device, since the capture device is
+	 * already started and the starting of playback device is blocked,
+	 * it leads to XRUNs for capture device.
+	 * Therefore these calls are moved to snd_pcm_jack_prepare(),
+	 * So that the capture and playback devices can be prepared in advance
+	 * and starting of the device doesn't take too long.
+	 */
 	return 0;
 }
 
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/2 jack: Moving jack_deactivate() to snd_pcm_jack_hw_free() to speedup the XRUN Recovery

In reply to [email protected]

From: Laxmi Devi <[email protected]>

Removed snd_pcm_jack_stop() from snd_pcm_jack_prepare(),as on XRUN we do not need to reconnect or reconfigure anything.

jack/pcm_jack.c +8 -6
index 19d339d..af2136e 100644
@@ -58,8 +58,6 @@ typedef struct {
 	bool xrun_detected;
 } snd_pcm_jack_t;
 
-static int snd_pcm_jack_stop(snd_pcm_ioplug_t *io);
-
 /* snd_pcm_ioplug_avail() was introduced after alsa-lib 1.1.6 */
 #if SND_LIB_VERSION < 0x10107
 static snd_pcm_uframes_t snd_pcm_ioplug_avail(const snd_pcm_ioplug_t *io,
@@ -277,9 +275,6 @@ static int snd_pcm_jack_prepare(snd_pcm_ioplug_t *io)
 		snd_pcm_sw_params_get_boundary(swparams, &jack->boundary);
 	}
 
-	/* deactivate jack connections if this is XRUN recovery */
-	snd_pcm_jack_stop(io);
-
 	if (io->stream == SND_PCM_STREAM_PLAYBACK)
 		pcm_poll_unblock_check(io); /* playback pcm initially accepts writes */
 	else
@@ -337,8 +332,14 @@ static int snd_pcm_jack_start(snd_pcm_ioplug_t *io)
 
 static int snd_pcm_jack_stop(snd_pcm_ioplug_t *io)
 {
+	(void)io;
+	return 0;
+}
+
+static int snd_pcm_jack_hw_free(snd_pcm_ioplug_t *io)
+{
 	snd_pcm_jack_t *jack = io->private_data;
-	
+
 	if (jack->activated) {
 		jack_deactivate(jack->client);
 		jack->activated = 0;
@@ -360,6 +361,7 @@ static snd_pcm_ioplug_callback_t jack_pcm_callback = {
 	.start = snd_pcm_jack_start,
 	.stop = snd_pcm_jack_stop,
 	.pointer = snd_pcm_jack_pointer,
+	.hw_free = snd_pcm_jack_hw_free,
 	.prepare = snd_pcm_jack_prepare,
 	.poll_revents = snd_pcm_jack_poll_revents,
 };
Signature · 1 line
2.7.4
#3 lore raw
In reply to [email protected]
Quoted 5 lines · On Fri, 21 Dec 2018 10:29:41 +0100, <[email protected]> wrote:

Hi all,

the following patches minimizes the delay of calling snd_pcm_start() and snd_pcm_stop() for the ALSA JACK plugin. This also makes an Xrun recovery faster.

The patches don't seem applicable on top of the latest git repo. Could you refresh and resubmit?

thanks,

Takashi

#4 lore raw
Quoted 11 lines · Takashi Iwai
Quoted 5 lines · On Fri, 21 Dec 2018 10:29:41 +0100, <[email protected]> wrote:

Hi all,

the following patches minimizes the delay of calling snd_pcm_start() and snd_pcm_stop() for the ALSA JACK plugin. This also makes an Xrun recovery faster.

The patches don't seem applicable on top of the latest git repo. Could you refresh and resubmit?

Never mind, I could apply them now. Unfortunately it slipped from 1.1.8 release, but now it's no git repo.

thanks,

Takashi