Android > 4.0 : Ideas how to record/capture internal audio (e.g. STREAM_MUSIC)?

Martin L. picture Martin L. · Dec 27, 2012 · Viewed 25.3k times · Source

Some months ago, with Android ICS (4.0), I developed an android kernel module which intercepted the "pcmC0D0p"-module to fetch all system audio.

My target is to stream ALL audio (or at least the played music) to a remote speaker via AirPlay.

The kernel module worked, but there where several problems (kernel-versions, root-privileges etc.) so I stopped working on this.

Now, we have Android 4.1 and 4.2 and I have new hope!

Who has an idea how to capture the audio in Android?

I had following ideas:

  1. Connect via bluetooth to the same phone, set routing to BT and grab the audio on the "other end": this shouldn't work

  2. Intercept the audio with a kernel module like done before: hardcore, get it worked but not applicable

  3. JACK Audio Connection Kit: sadly Android uses "tinyALSA" and not "ALSA". TinyALSA does NOT support any filters like JACK (but this brought the idea with the kernel module)

  4. Use PulseAudio as a replacement for AudioFlinger, but this is also not applicable


EDIT (forgot them):

  1. I compiled "tinymix" (baby-version of ALSA-mixer) from tinyALSA (the ALSA on Android) and tried to route the audio-out to mic-in - but with no success (not understandable for me). And this also needs rooting: not applicable

  2. I tested OpenSL ES, but I'm not a C-crack and it ended in "I can record microphone, but no more" (maybe I was wrong?)


I just found ROUTE_TYPE_LIVE_AUDIO:

A device that supports live audio routing will allow the media audio stream to be routed to supported destinations. This can include internal speakers or audio jacks on the device itself, A2DP devices, and more.

Once initiated this routing is transparent to the application. All audio played on the media stream will be routed to the selected destination.

Maybe this helps in any way?

I'm running out of ideas but want to "crack this nut", maybe someone can help me?

EDIT:

I'm really new in C & kernel-coding (but I successfully created a cross-compiled audio-interception-module) - but isn't it in any way possible to listen at the point the PCM-data goes from userspace (JAVA, C-layer?) to the kernel-space (tinyALSA, kernel-module), without hacking & rooting?

Answer

Nik picture Nik · Jul 3, 2013

You must pass audio traffic through your local socket server:

  1. Create local socket server

  2. Modify audio http stream address to path is thru your local socket server, for example for address

    http://example.com/stream.mp3  ->
    
    http://127.0.0.1:8888/http://example.com/stream.mp3
    

    where 8888 is your socket port.

  3. Play

    http://127.0.0.1:8888/http://example.com/stream.mp3 
    

    with default media player

  4. Read all incoming requests to port 8888 in your local socket server, analyze it and pass to example.com server.

  5. Read response from example.com and pass it to local client (media player). HERE you can capture audio stream!