Android Audio (8) - HAL analysis

A, HAL's framework

HAL upper frame 1. tiny4412

Makefile audio.primary.tiny4412.so file:
Device / Friendly-ARM / the Common / for libaudio / Android.mk

LOCAL_SRC_FILES: = AudioHardware. CPP 
LOCAL_MODULE: = $ audio.primary (TARGET_DEVICE) #TARGET_DEVICE is tiny4412, generating audio.primary.tiny4412.so, wherein in the primary / system / etc /. Specify the name of the module of audio_policy.conf. 
LOCAL_MODULE_PATH: = $ (TARGET_OUT_SHARED_LIBRARIES) / hw 
LOCAL_STATIC_LIBRARIES: = libmedia_helper 
LOCAL_SHARED_LIBRARIES: = \ 
    libutils \ 
    liblog \ 
    libhardware_legacy \ # depend on this library 
    libtinyalsa \ # depend on this library 
    libaudioutils 
LOCAL_WHOLE_STATIC_LIBRARIES: = libaudiohw_legacy

libaudiohw_legacy dependent files:

Hardware / libhardware_legacy / Audio / Android.mk 
LOCAL_SRC_FILES: = \ 
    . AudioHardwareInterface cpp \ 
    . audio_hw_hal cpp     is the # version 5.0 Android 4412 of the HAL, the other may not necessarily 

LOCAL_MODULE: = libaudiohw_legacy 
LOCAL_MODULE_TAGS: = optional 
LOCAL_STATIC_LIBRARIES: = libmedia_helper 
LOCAL_CFLAGS: -Wno-unused- = Parameter 

the include $ (BUILD_STATIC_LIBRARY)

So libraries audio.primary.tiny4412.so at least involve:
audio_hw_hal.cpp (Hardware / libhardware_legacy / Audio / audio_hw_hal.cpp)
audioHardwareInterface.cpp (Hardware / libhardware_legacy / Audio / audioHardwareInterface.cpp)
audioHardware.cpp (Device / ARM-Friendly /common/libaudio/audioHardware.cpp) generated audio.primary.tiny4412.so
role:
audio_hw_hal.cpp: up function that provides an interface
audioHardwareInterface.cpp: manufacturers to develop functions to access hardware interfaces
audioHardware.cpp: access to sound hardware vendors to achieve the document

2. The first find out the framework, in order to understand their code.

As can be seen from the picture, HAL HAL audio have this layer, but also of HAL Audio_policy. For Audio_policy of HAL we do not matter, because it has basically abandoned.

To top HAL provides a unified interface, the operation may also have a set of hardware interface or a class, grab these two HAL analysis framework.

3. See audio_hw_hal.cpp, an interface is provided upwardly struct audio_hw_device, which is a configuration of encapsulation of hw_device_t.

audio_hw_hal.cpp located hardware / libhardware_legacy / audio down, since it is old, then there is no new one? In the hardware / libhardware / modules / audio
next there audio_hw.c file, which is the new Audio HAL files, but it is not used in the panel 5.0, which is a function of all it is empty. As can be seen in the version of Android8.0,
this file also did not materialize, so in libhardware_legacy still play the main role. But in Android8.0 Qualcomm HAL in use it is new.

Audio_hw_hal.cpp 4.
A. Audio_hw_device up to provide an interface struct
B. Downwardly hardware access class AudioHardware (implemented in the device / friendly-arm / common / libaudio / audioHardware.cpp) provided by the manufacturer, which used to tinyalsa
library interface. But certainly not at liberty to manufacturers of hardware access interface, Android specifies a set of interfaces to it.
The class hierarchy:

AudioHardwareInterface     // Hardware / AudioHardwareInterface.cpp 

AudioHardwareBase          // Hardware / AudioHardwareBase.h this should be an interface to the audio HAL vendor-defined 

AudioHardware             // Device / Friendly-ARM / the Common / for libaudio / audioHardware.cpp

5. In the HAL manufacturers, AudioHardware (audioHardware.cpp) represent a sound card that uses audio_stream_out structure to represent an output, the input used to represent audio_stream_in.
There audio_stream_out write (), audio_stream_in has read ()


6. Audio HAL call flow summary

  Audio_hw_hal.cpp upper application calls the legacy_adev_open () will get a struct audio_hw_device structure, this structure represents the upper layer interface to the hardware used, this structure has the function of a pile, they all rely on the structure struct AudioHardwareInterface provided by the manufacturer (used in the HAL, implemented in the manufacturer code, this embodiment is implemented in the audioHardware.cpp), since the interface of the upper layer HAL provides no direct read / write function, and therefore, application wants to play a sound recording or when the need to hit the card output or input (HAL call the open_output_stream / open_input_stream), to use its inside write / read function write audio data to the sound card hardware.

7. HAL associated data structures

legacy_audio_device {struct
     // standardized interface provided upwardly 
    struct audio_hw_device Device;
     // upwardly dependent interface provided by the manufacturer provides this class to implement 
    struct AudioHardwareInterface * HWIF; 
}; 
The following is the same reason legacy_stream_out and legacy_stream_in 
struct legacy_stream_out { 
    struct Stream audio_stream_out ; 
    AudioStreamOut * legacy_out; 
}; 
struct legacy_stream_in { 
    struct Stream audio_stream_in; 
    AudioStreamIn * legacy_in; 
};

The above is the Android source code commonly used routines.

8. http://androidxref.com/ there are various versions of the Android source code, the code is very easy to see

9. Because of the class hierarchy, SourceInsight works automatically jump is not allowed.

10. AudioFlinger startup, AudioPolicyService configuration process reads the configuration file /system/etc/audio_policy.conf, the profile has a module called "primary", according to the name, would go / system / lib / hw open the library: audio.primary.XXX.so, such as: audio.primary.tiny4412.so


11. The frame described in detail with
audio HAL in:
B.1 to provide a unified interface to up: audio_hw_device, which provided a variety of functions

b.2 these functions to be achieved by means of code to the sound card, sound card features abstract objects AudioHardware

The only function b.3 audio_hw_device see a variety of set, you get, do not see wirte, read, how to play a sound, recorded sound?

b.4 To play the sound,
B.4.1 first open output: There open_output_stream function pointer in the audio_hw_device

b.4.2 open_output_stream audio_stream_out returns a structure, which provides up "Playback Interface"

b.4.3 audio_stream_out also requires manufacturers to provide the code to implement the code provided by the manufacturer for the AudioStreamOutALSA abstract objects

b.5 for recording sound
b.5.1 first open input: There open_input_stream function pointer in the audio_hw_device

b.5.2 open_input_stream audio_stream_in returns a structure, which provides up "Recording Interface"

b.5.3 audio_stream_in also requires manufacturers to provide the code to implement the code provided by the manufacturer of abstract objects AudioStreamInALSA

 

Two, HAL source code analysis of call flow

1. HAL files are typically located in / system / lib / hardware Next, the operation of the audio hardware library file name is specified in / system / etc / policy_config in (HAL part of vendor-supplied), module name such as primary,
to the corresponding audio. primary.tiny4412.so.

2. The system calls directly control a sound card is tinyalsa library, located under the directory / external / tinyalsa, compiled libraries libtinyalsa.so (involving only two files mixer.c, pcm.c), compiler generation tools tinycap, tinymix, tinypcminfo, tinyplay, can be used to directly control the audio channels, broadcast recording test.

tinyalsa used
pcm_open () to open the sound card
pcm_write () to play music
pcm_read () to record

3. HAL call flow source analysis

. a determination of file name HAL:
AudioPolicyManager constructor /system/etc/audio_policy.conf read the configuration file name is determined (vendor name only partially compiled HAL in the name of the library tiny4412, HAL inlet is hardware / libhardware_legacy / audio /audio_hw_hal.cpp)

B. HAL is loaded so the corresponding file.

c. Open HAL file open function, in the structure will audio_hw_device HAL structure, the structure has various kinds of functions, in particular open_output_stream / open_input_stream

AudioFlinger AudioHwDev constructed object from a structure and put mAudioHwDevs audio_hw_device

d open output stream:. audio_hw_device call hal structure of open_output_stream, it will construct a audio_stream_out structure, which has write function

AudioFlinger MixerThread a structure configured according audio_stream_out

. e play a sound (this time really open the sound card driver):
the Thread-> threadLoopg_write => stream.write ==> pcm_open, pcm_write

 

Guess you like

Origin www.cnblogs.com/hellokitty2/p/10943199.html
Recommended