Feb 25, 2010

Setting up Jack Audio for GStreamer, Flash, and VLC

First of all I need to say that I won't be mentioning Pulseaudio so if that is what you're here for then you are at the wrong place because I don't use Pulseaudio at all, Pulseaudio can be run ontop of Jack but doing so will increase CPU load (a very tiny amount on modern systems). Someday I may find a reason to run Pulseaudio on top of Jack but right now I don't have one.

I'm going to try to keep this guide as distribution agnostic as I can, I'm sure that all of these packages are available for all of the major Linux distributions so I leave it up to you to get them and install them.

Jack Audio

I'm using Jack version 0.116.2

Start by installing jack on your computer. :)

The beautiful thing about jack is its super low latency real-time support, but in order to realize its full potential you will likely need to do a bit of tweaking to your system first.

Fortunately there is a perl script called the "Realtime Config Quickscan" for Jack which you can download and run in order to see what has already been setup on your system and what still needs to be fixed, I highly suggest getting and running that script as the first order of business.

When I run the script on my system the output looks like this:



A couple of things may stand out when you look at that, first you may notice that I'm not running a real-time kernel, surprise! Actually you don't need to be running a real-time kernel in order to get real-time audio with Jack as long as you are running a reasonably recent version of Linux.

Quite frankly compiling your own kernel can take a lot of time especially if you make a mistake, and time is my most precious resource so I prefer to run the standard issue kernels which come with my distribution and I just always make sure they are as up to date as possible.

So here is a list of the bare minimum needed to get real time audio in Linux.

First make sure you are in the audio group, this can be done from the command line like so:

$ sudo gpasswd -a <USER> audio


Change <USER> to your user name.

Next edit the file /etc/security/limits.conf
In that file you should see two lines that look like this:


Move those two lines to the very end of the file, and right above them add the following information:


It should look like this when you are done:


Next, check if you have a shared memory file system mounted on /dev/shm by running this command:
$ mount | grep shm

If you see output then you're good to go, otherwise add this text to the /etc/fstab file:

While you are there messing with your /etc/fstab make sure that all of your major file systems such as /boot, /, and /home, as well as /dev/dvd and /dvd/cdrom and any external hard drives all have "noatime" listed among their options, this can significantly increase file system performance and it will make the Quickscan script happy.

The last thing to do is to set your max_user_watches, open the file /etc/sysctl.conf and add this to the end:

fs.inotify.max_user_watches = 524288

Next restart your computer, yes I know, we all hate to reboot, but sometimes it is necessary and this is one of those times.

After the reboot, as a regular user start jack from the command line:

jackd -R -P89 -s -dalsa -dhw:0 -r48000 -p256 -n3

If all goes well your system now has real time audio capabilities. You will now need to start jack as a regular user (not root) every time you login with the command listed above, I leave it up to you to figure out the best way to do this on your system, personally I just add a little shell script to my users startup applications.

Connecting GStreamer to Jack

Having realtime capability is only great if your applications actually know about it. So lets connect GStreamer to Jack so that Gnome desktop apps like Rhythmbox, or whatever, have access to your new cool sound system.

I am using GStreamer Core Library version 0.10.26

First of all you will need to install the Jack plugin for GStreamer, this can be found in a package called gstreamer-0.10-bad-plugins (I've also seen it called gstreamer-0.10-plugins-bad).

After installing the plugin make sure that you have both jackaudiosink and jackaudiosrc by running this command:

$ gst-inspect-0.10 | grep jack

If you see output for both jackaudiosink and jackaudiosrc then you are good to go. If you are missing jackaudiosrc then you are running an old version of gstreamer and I strongly suggest that you upgrade it. You can still continue onward with this guide but you will be unable to record audio input from a microphone, so if using a microphone with a GStreamer app is important to you then you are out of luck unless you upgrade.

Next, on the command line, launch the program called gstreamer-properties and change the audio settings so that they look like this:

That wasn't so hard, was it.

Rhythmbox and other Gnome audio applications that use GStreamer should work as expected now.

(Some old Gnome audio applications may still not work right, for example gnome-audio-recorder does not work for me, but its not exactly essential so I don't really care. If you need to record audio through your microphone then I suggest using Audacity anyway.)

Introducing libflashsupport-jack

Sometime in the near future Flash may be put out to pasture by newer and better technologies such as html5 but for now most people need it.

I've never had an easier time hooking Jack to Flash than with libflashsupport-jack by Torben Hohn, actually, I've never hooked Jack to Flash at all until now.

You can download it from git, or if you are using Archlinux and packer then you probably don't need me to say anymore ;)

Dear sweet VLC

OK, we all know VLC is great but when it comes to Jack some might have an issue because of VLC's rather silly setting defaults.

I am using VLC version 1.0.5

Open VLC, go to Tools -> Preferences, then click Audio, then change the output type to "JACK audio output". Done? Nope!

Notice that down in the lower left hand corner there is a toggle option button, it is set to Simple by default, change this to All instead.

Then in the "Advanced Preferences" window go to Audio -> Output Modules -> JACK and put a check in the check-box which says "Automatically connect to writable clients." It should look like this:


I have no idea why this isn't enabled by default, or at the very least over in the "Simple" preferences, but anyway thats what you need to do to get VLC to connect automatically to Jack.

I hope you've found this helpful and I hope you have fun using real time audio on Linux!

H9TEBBXVP98X

12 replies:

lily said...

Thank you very much!!!!!
Very very very usefull!!1!!

ChalesOne

cinch said...

Thanks this was helpful.

On Ubuntu 10.04, there's an addition step to do to connect GStreamer apps to Jack:

open gconf-editor
change the value at: /system/gstreamer/0.10/default/musicaudiosink to "jackaudiosink"
and possibly for chataudiosink too.

s said...

hi,

I did all the task you mention, but I still can't play music in vlc when jack is working.

and the libflashsupport-jack don't work for me... when doing sh bootstrap.sh , I get the message :

No package 'jack' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables JACK_CFLAGS
and JACK_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

I am not very good at programming (in fact i'm a complet wanabe) and i don't understand much. can you help? other forums dont have the answer i'm looking for!

thx


sp

s said...

hi,

I did all the task you mention, but I still can't play music in vlc when jack is working.

and the libflashsupport-jack don't work for me... when doing sh bootstrap.sh , I get the message :

No package 'jack' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables JACK_CFLAGS
and JACK_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

I am not very good at programming (in fact i'm a complet wanabe) and i don't understand much. can you help? other forums dont have the answer i'm looking for!

thx


sp

Lee said...

Thanks. Very helpful. But I didn't realize I didn't have vlc-plugin-jack installed, so there were no JACK options in VLC... maybe I missed that in your overview, so figured I post for others.

HTML5Enthused said...

Hi, thanks for leading this initiative. I am almost at a point where jack-audio is the king of my ubuntu 10.10, doing stuff i can't do without it. EXCEPT: HTML Audio, ironically does not play in either Chrome or Firefox, when jack-audio is running. When I turn off jack, I can hear the HTML5 audio in chrome and firefox. (Though i don't get any major freezes/errors when it's NOT working.) Irony! Is there a way to make Chome and Firefox play the HTML5 audio through jack? I would really really covet that.

luxigo said...

Thanks a lot !

but libflashsupport-jack is not working/needed anymore (at least in ubuntu 11.04 natty):

you just have to add the following lines at the end of your ~/.asoundrc so that everything using alsa is redirected to jack :

# http://jackaudio.org/routing_alsa
pcm.rawjack {
    type jack
    playback_ports {
        0 system:playback_1
        1 system:playback_2
    }
    capture_ports {
        0 system:capture_1
        1 system:capture_2
    }
}

pcm.jack {
    type plug
    slave { pcm "rawjack" }
    hint {
        description "JACK Audio Connection Kit"
    }
}

Note that i did also totally disabled pulseaudio before, with the procedure described here: http://www.jeffsplace.net/node/12

luxigo said...

redirecting alsa to jack (adding the .asoundrc lines i did post above) solves also the HTML5 video and audio tag problem mentioned by HTML5Enthused

HTML5Enthused said...

OK, I tried adding the .asoundrc lines as instructed. Didn't help with the HTML5 audio through chrome & FF. Do I have to disable pulseaudio also?

I'm on Ubuntu 10.10

lucas fialho said...

Amazing tutorial mate, specially the part about gstreamer. I'm finally moving on to using jack all the time now, thanks :) !

HTML5Enthused said...

Bump!

anyone?

TechnoBear said...

pulse and jack dont work well together. I'd advise disabling pulse and that should help.

Issue I'm having is that VLC only auto connects to playback_1 in jack not both _1 and _2