Achieve playback on Android 4.4
Background Android Since ICS began, the audio system has changed a lot, first abandoned the alsalib, then the use of AIO, all levels of the framework, have their own features, and Linux audio applications fade away, forming their own unique audio management and audio configuration features. Overall improvement is still very large, at least in the user experience has greatly surpassed the previous version. We will analyze some of the changes and implementation mechanisms from the 4.4 audio implementations.
Requirements
First, the hardware function is normal, this does not watch. Linux supports ALSA drivers and generates ALSA subsystems, preferably with the ability to test audio playback and recording in advance on other file systems such as BuildRoot.
Hal Audio Hal Simple Implementation, reference Device/asus/grouper/audio, if not too complex audio configuration, basically can be slightly modified on this code basis, mainly some playback and recording parameters. This HAL has implemented a common interface, and the call is also a standard Tinyalsa interface, the portability is very high. We use the wm8904 here, the function is not much, directly use can.
Configuration of Android
Mainly 4 files audio_policy.conf media_profiles.xml media_codecs.xml mixer_paths.xml reference Asus, do not have to change, basic copy, completely can be directly used, open source long live.
Do the file system, this time the system should not use the default stub audio Hal, but instead of the audio Hal we added. But if you can make a sound, it's probably not.
Debugging
The audio system calls the Libtinyalsa Libaudioutils Libaudioroute several underlying libraries. These have migrated some common ALSA devices to open the configuration function, but the specific platform is not necessarily normal execution, mainly these library implementation is very simple, not considered comprehensive, your hardware details may just be ignored by them, as in our wm8904, we do not support time stamp, But Tinyalsa is turned on by default and must be turned off.
Disable Tstamp for wm8904. Change-id:ia22aa6ed39ede6214657487344d0488be93e5544diff--git a/pcm.c b/pcm.cindex 4501777..94cf6ee 100644---A/ pcm.c+++ b/pcm.c@@ -691,7 +691,7 @@ struct PCM *pcm_open (unsigned int card, unsigned int device, memset (&sparams, 0, sizeof (sparams));- Sparams.tstamp_mode = sndrv_pcm_tstamp_enable;+ Sparams.tstamp_mode = sndrv_pcm_ Tstamp_none; Sparams.period_step = 1; if (!config->start_threshold) {
Specifically which parameters are not correct, or inappropriate, requires the Android driver engineer to verify, individually and in accordance with the hardware design and chip manuals, the optimal state of work for an audio system. Then the user experience can be the best.
Android Audio System Analysis The following analysis is based on 4.4.2
Audio HAL
The Tinyalsa and audiorouteandroid audio systems are Linux-based ALSA drivers, Tinyalsa encapsulate the audio interface on a ALSA driver basis, provided to the audio HAL, and the audio HAL provides an interface to Android a Udioflinger and other frameworks. HAL needs to enable the audio hardware to turn on and off (here is what Android thinks of the hardware).
static inline int audio_hw_device_open (const struct hw_module_t* module, struct audio_hw_device** device) { Return Module->methods->open (module, audio_hardware_interface, (struct hw_device_t**) device);} static inline int audio_hw_device_close (struct audio_hw_device* device) { return Device->common.close (& Device->common);}
Data flow struct audio_stream_out struct audio_stream_in required to implement in and out
In the main has the Read method to read the audio data, out mainly has the Write method, writes the data to the device, respectively realizes the recording and the playback. See: Hardware/libhardware/include/hardware/audio.h
The method is also called the Tinyalsa interface, about the operation of the PCM:
/* Open and close a stream */struct PCM *pcm_open (unsigned int card, unsigned int device, unsigned int Flags, struct pcm_config *config), int pcm_close (struct PCM *pcm), int pcm_is_ready (struct PCM *PCM);/* Obtain the Paramete RS for a PCM */struct pcm_params *pcm_params_get (unsigned int card, unsigned int device, unsigned int flags); void Pcm_params_free (struct pcm_params *pcm_params); unsigned int pcm_params_get_min (struct PCM_ params *pcm_params, enum pcm_param param); unsigned int pcm_params_get_max (struct pcm_params *pcm_params, enum pcm_param param);/* Set and get config */int pcm_get_config (struct PCM * PCM, struct pcm_config *config), int pcm_set_config (struct PCM *pcm, struct pcm_config *config);/* Returns a human readable Reason for the last error */const char *pcm_get_error (struct PCM *PCM);/* Returns the sample size in bits for a PCM forma T. * As with ALSA formats is the storage size for the format, whereas the * format represents the number of significant bits. For example, * Pcm_format_s24_le uses of storage. */unsigned int pcm_format_to_bits (enum pcm_format format);/* Returns the buffer size (int frames) that should is used for Pcm_write. */unsigned int pcm_get_buffer_size (struct PCM *pcm) unsigned int pcm_frames_to_bytes (struct PCM *pcm, unsigned int frames ); unsigned int pcm_bytes_to_frames (struct PCM *pcm, unsigned int bytes);/* Returns the PCM latency in MS */unsigned int PC M_get_latency (struct PCM *PCM);/* Returns available frames in PCM buffer and corresponding time stamp. * The clock is clock_monotonic if flag pcm_monotonic were specified in Pcm_open, * Otherwise the clock is clock_realtime. * For a input stream, frames available is frames ready for the * application to read. * For the output stream, frames available is the number of the empty frames available * for the application to write. */int pcm_get_htimestamp (struct PCM *pcm, unsigned int *avail, struct timespec *tstamp);/* Write data to the FIFO. * Would start playback on the first write or to a write that * occurs after a FIFO underrun. */int pcm_write (struct PCM *pcm, const void *data, unsigned int count); int Pcm_read (struct PCM *pcm, void *data, unsigned int count);/* * MMAP () support. */int pcm_mmap_write (struct PCM *pcm, const void *data, unsigned int count); int Pcm_mmap_read (struct PCM *pcm, void *data, unsigned int count); int Pcm_mmap_begin (struct PCM *pcm, void **areas, unsigned int *offset, unsigned in t *frames); int pcm_mmap_commit (struct PCM *pcm, unsigned int offset, unsigned int frames);/* Start and stop a PCM channel That doesn ' t transfer data */int Pcm_start (struct PCM *pcm); int pcm_stop (struct PCM *PCM);/* Interrupt driven API */int pc M_wait (struct PCM *pcm, int timeout);/* Change avail_min after the stream have been opened with no need to stop the stream. * Only accepted if opened with Pcm_mmap and PCM_NOIRQ flags */int pcm_set_avail_min (struct PCM *pcm, int avail_min);
It is worth mentioning that HAL now does not include the operation of the route, audio route to the libaudioroute.so, it is also called Tinyalsa interface, and contains an XML parser, parsing mixer_paths.xml inside the Rou TE configuration data. This allows the system to operate the PCM and mixer of the ALSA, theoretically it should be able to play, using the tools provided by the Tinyalsa to test, of course, the HAL interface can not be tested. Tinycap Tinymix Tinypcminfo Tinyplay
Tinyplay can play pure audio data in WAV format. Tinymix can view and configure audio paths:
[email protected]:/# Tinymix Mixer name: ' wm8904 @ sama5d3ek ' N Umber of controls:41ctl type num name value0 INT 1 EQ1 Vol ume 121 int 1 EQ2 Volume 122 int 1 EQ3 Volume 123 INT 1 EQ4 Volume 124 int 1 EQ5 Volume int 2 Digital Capture Volume 966 enum 1 left caputure Mode single-ended7 enum 1 Right Capture Mode single-ended8 INT 2 Capture Volume 5-A-BOOL 2 Capture Switch Off Off10 BOOL 1 high Pass Filter Switch On11 ENUM 1 High Pass Filter Mode hi-fi12 BOOL 1 ADC 128x OSR Switch On13 int 1 Digital Playback Boost Volume 014 int 2 Digital Playback Volume 9615 INT 2 headphone Volume 4516 BOOL 2 headphone Switch On On17 BOOL 2 headphone ZC Switch on On18 INT 2 Li NE output Volume 5719 BOOL 2 line Output Switch on On20 BOOL 2 line Output ZC switch on On21 BOOL 1 EQ switch Off22 BOOL 1 DRC Switch Off23 ENUM 1 DRC Path ADC24 BOOL 1 DAC OSRx2 Switch Off25 BOOL 1 DAC Deem Phasis Switch Off26 INT 2 Digital sidetone Volume 0 027 ENUM 1 LINER Mux DAC28 enum 1 Linel Mux DAC29 enum 1 HPR MUX DAC30 enum 1 HPL Mux DAC31 enum 1 Right Sidetone None32 ENUM 1 left Sidetone No ne33 enum 1 DACR mux Right34 enum 1 DACL mux Left35 enum 1 Aifoutr Mux Right36 enum 1 aifoutl M UX Left37 enum 1 Right Capture inverting Mux in1r38 ENUM 1 Right capture Mux in2r39 ENUM 1 left Capture inverting Mux in1l4 0 ENUM 1 LEFT Capture Mux in2l
Audioflinger
Audioflinger is an audio voice server that loads the audio HAL and processes the voice application to make an audible request. This analysis has many, refer to the following: http://blog.csdn.net/DroidPhone/article/details/5941344