Fix Jack plugin
PATCH 1/1 Fix Jack plugin
I've uploaded a patch to ticket 2727, at
https://bugtrack.alsa-project.org/alsa-bug/view.php?id=2727
My background: I'm a software developer, but I know nothing about ALSA development. But I use Jack, and needed to get the Jack plugin working. Since no one has touched the plugin in years, I figured I'd take a stab at fixing it myself.
The symptom: Using the Jack plugin causes whatever program is outputting audio to hang after about 10-20 seconds.
The cause: The plugin creates a pipe, so that the fd's could be polled to indicate completion of a transfer. However, the call to write() blocks when playing audio, as nothing actually reads from the pipe, and it fills up until it blocks.
The solution: This is where my inexperience with ALSA shows up. I don't know the best way to fix it. Making the socket non-blocking fixes the problem, and testing with playback and capture both work properly for me now. However, comparing the code with that of pcm-pulse.c (for piping ALSA output to PulseAudio), it's obvious that the Jack plugin was a hack. It could use more work. However, my fix at least gets me running.
The patch:
--- alsa-plugins-1.0.23/jack/pcm_jack.c 2010-04-16 04:11:10.000000000 -0700
+++ alsa-plugins-1.0.23.new/jack/pcm_jack.c 2011-08-03
21:54:51.795378947 -0700
@@ -307,6 +308,18 @@
return 0;
}
+static int make_nonblock(int fd) {
+ int fl;
+
+ if ((fl = fcntl(fd, F_GETFL)) < 0)
+ return fl;
+
+ if (fl & O_NONBLOCK)
+ return 0;
+
+ return fcntl(fd, F_SETFL, fl | O_NONBLOCK);
+}
+
static int snd_pcm_jack_open(snd_pcm_t **pcmp, const char *name,
snd_config_t *playback_conf, snd_config_t *capture_conf,
@@ -363,6 +376,9 @@
socketpair(AF_LOCAL, SOCK_STREAM, 0, fd);
+ make_nonblock(fd[0]);
+ make_nonblock(fd[1]);
+
jack->fd = fd[0];
jack->io.version = SND_PCM_IOPLUG_VERSION;Quoted 36 lines · Kevin Ross
I've uploaded a patch to ticket 2727, at
https://bugtrack.alsa-project.org/alsa-bug/view.php?id=2727The symptom: Using the Jack plugin causes whatever program is outputting audio to hang after about 10-20 seconds.
The cause: The plugin creates a pipe, so that the fd's could be polled to indicate completion of a transfer. However, the call to
write()blocks when playing audio, as nothing actually reads from the pipe, and it fills up until it blocks.The patch:
jack/pcm_jack.c 2011-08-03 renamed from jack/pcm_jack.c 2010-04-16 04:11:10.000000000 -0700 +0
-021:54:51.795378947 -0700
jack/pcm_jack.c 2011-08-03 renamed from jack/pcm_jack.c 2010-04-16 04:11:10.000000000 -0700 +12
-0@@ -307,6 +308,18 @@ return 0; } +static int make_nonblock(int fd) { + int fl; + + if ((fl = fcntl(fd, F_GETFL)) < 0) + return fl; + + if (fl & O_NONBLOCK) + return 0; + + return fcntl(fd, F_SETFL, fl | O_NONBLOCK); +} + static int snd_pcm_jack_open(snd_pcm_t **pcmp, const char *name, snd_config_t *playback_conf, snd_config_t *capture_conf,
jack/pcm_jack.c 2011-08-03 line 384
Quoted 9 lines
jack/pcm_jack.c 2011-08-03 renamed from jack/pcm_jack.c 2010-04-16 04:11:10.000000000 -0700 +3
-0@@ -363,6 +376,9 @@ socketpair(AF_LOCAL, SOCK_STREAM, 0, fd); + make_nonblock(fd[0]); + make_nonblock(fd[1]); + jack->fd = fd[0]; jack->io.version = SND_PCM_IOPLUG_VERSION;
Confirmed to be working. I'm going to send a git patch ASAP.
Cheers
At Thu, 04 Aug 2011 16:11:45 +0200,
Quoted 42 lines · Adrian Knoth
Quoted 39 lines · Kevin Ross
I've uploaded a patch to ticket 2727, at
https://bugtrack.alsa-project.org/alsa-bug/view.php?id=2727The symptom: Using the Jack plugin causes whatever program is outputting audio to hang after about 10-20 seconds.
The cause: The plugin creates a pipe, so that the fd's could be polled to indicate completion of a transfer. However, the call to
write()blocks when playing audio, as nothing actually reads from the pipe, and it fills up until it blocks.The patch:
jack/pcm_jack.c 2011-08-03 renamed from jack/pcm_jack.c 2010-04-16 04:11:10.000000000 -0700 +0
-021:54:51.795378947 -0700
jack/pcm_jack.c 2011-08-03 renamed from jack/pcm_jack.c 2010-04-16 04:11:10.000000000 -0700 +12
-0@@ -307,6 +308,18 @@ return 0; } +static int make_nonblock(int fd) { + int fl; + + if ((fl = fcntl(fd, F_GETFL)) < 0) + return fl; + + if (fl & O_NONBLOCK) + return 0; + + return fcntl(fd, F_SETFL, fl | O_NONBLOCK); +} + static int snd_pcm_jack_open(snd_pcm_t **pcmp, const char *name, snd_config_t *playback_conf, snd_config_t *capture_conf,@@ -363,6 +376,9 @@ socketpair(AF_LOCAL, SOCK_STREAM, 0, fd);
Quoted 8 lines
jack/pcm_jack.c 2011-08-03 line 384
Quoted 6 lines
jack/pcm_jack.c 2011-08-03 renamed from jack/pcm_jack.c 2010-04-16 04:11:10.000000000 -0700 +3
-0+ make_nonblock(fd[0]); + make_nonblock(fd[1]); + jack->fd = fd[0]; jack->io.version = SND_PCM_IOPLUG_VERSION;Confirmed to be working. I'm going to send a git patch ASAP.
OK, it's good to know that it works in multiple systems. I committed the patch now to git tree.
Thanks!
Takashi
PATCH 1/1 jack: Fix hanging applications when using jack plugin
In reply to Kevin RossAs described in issue #2727
https://bugtrack.alsa-project.org/alsa-bug/view.php?id=2727
applications hang after some seconds when using the jack plugin.
Kevin Ross <[email protected]> writes:
The cause: The plugin creates a pipe, so that the fd's could be polled to indicate completion of a transfer. However, the call to write() blocks when playing audio, as nothing actually reads from the pipe, and it fills up until it blocks.
Making the socket non-blocking fixes the problem, and testing with playback and capture both work properly for me now.
I've also tested Kevin's patch and confirm it works.
jack/pcm_jack.c +15 -0
@@ -307,6 +307,18 @@ static int parse_ports(snd_pcm_jack_t *jack, snd_config_t *conf)return 0 ; } +static int make_nonblock (int fd) { +int fl; + +if ((fl =fcntl (fd,F_GETFL )) <0 ) +return fl; + +if (fl &O_NONBLOCK ) +return 0 ; + +return fcntl (fd,F_SETFL , fl |O_NONBLOCK ); +} + static int snd_pcm_jack_open(snd_pcm_t **pcmp, const char *name, snd_config_t *playback_conf, snd_config_t *capture_conf,
@@ -363,6 +375,9 @@ static int snd_pcm_jack_open(snd_pcm_t **pcmp, const char *name,socketpair (AF_LOCAL ,SOCK_STREAM ,0 , fd); +make_nonblock (fd[0 ]); +make_nonblock (fd[1 ]); + jack->fd = fd[0 ]; jack->io.version =SND_PCM_IOPLUG_VERSION ;
Signature · 1 line
1.7.5.4