Pulse-Audio JNI Experiment

After a bit of effort trying to remember C++ (It’s so long ago!) and work out how to link it, I now have a JNI implementation of Pulse Audio Subscribe.

My code at GitHub¬†shows progress so far. It’s a bit of a mix of code bases and not particularly useful as yet. The test for Pulse Audio via JNI is entirely in the m0rjc.pi2adp.pulseaudio.PulseAudio class. It has a main method which subscribes for events.

Linking was an unexpected challenge. I had to solve the problems of being unable to link and being unable to find the Pulse libraries at runtime. The magic recipe ended up as

g++ -shared \
    PulseBase.o PulseClient.o PulseSubscription.o \
    m0rjc_pi2adp_pulseaudio_PulseAudio.o \
    -lstdc++ -lpulse-mainloop-glib -lpulse -lglib-2.0 \
    -o libJavaPulse.so

My code was compile with -fPIC

g++ -c -g -fPIC PulseBase.cpp
g++ -c -g -fPIC PulseClient.cpp
g++ -c -g -fPIC PulseSubscription.cpp
g++ -c -g -I/usr/lib/jvm/default-java/include -fPIC m0rjc_pi2adp_pulseaudio_PulseAudio.cpp

I found JNI easier to work with in C++ than C. C++ also gives me some encapsulation and object orientated features. I’m note sure of the patterns I’ve used, but think it will be easy enough now to add the required methods to read the source, sink and module lists. Then I have to hope for no leakages in a long running system.

The C++ source code is in the src/main/c folder. No easy integration of C++ in Maven.

The requirement to auto connect my phone to the speakers could be achieved purely in C++. There is an argument for this. The C++ code I’ve developed could be a good starting point. Then the Java Web Server could be an optional extra to provide monitoring, but the embedded solution on Raspberry Pi could be kept small.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.