August 23rd, 2012, 05:57 PM
Thread Author (OP)
Join Date: Aug 2012
Carrier: Not Provided
Thanked 0 Times in 0 Posts
C Thread priority for streamig USB Audio problems
I'm building a custom application on a custom device (2.3.5, kernel 2.6.38). The android device is acting as a USB Host, consuming audio from a connected USB media player device using standard USB Audio Class. I do have access to generate a new ROM for the custom device.
In a C thread (record thread), I'm opening a handle to the USB alsa recorder device (/dev/snd/), requesting pcm frames from the attached USB device using the snd_pcm_readi alsa API. Frames are now passed from recorder thread to a C playback thread to be queued to speaker output via ALSA and/or OpenSL.
When my application is in the foreground, the recorder thread runs in a timely manner, and audio playback is seamless. However, when my application gets backgrounded, I run another application in forground, or quickly scroll through any listviews or the home screen tiles, the C recorder thread gets starved. The C recorder thread cannot request USB audio frames fast enough, and music starts to skip. When the forground application idles again or I finish interacting with the UI, the music will once again be seamless.
As I understand, C threads in Android do not (and cannot) have real-time priority, verified by my failed attempts to bump up the pthread priority. My view of the solution is to bump up the recorder thread priority somehow, but not sure how to get beyond the android thread priority limitation. Or is there another solution (like not having a recorder/playback thread, and using builtin media features)?
Open to all suggestions or comments.