\

MediaSever loads two services at system startup: AudioPolicyService and AudioFlinger.

 

1.3.1 AudioPolicyService

AudioPolicyService performs the following tasks

The JAVA application layer accesses the services provided by AudioPolicyService through JNI and the IAudioPolicyService interface

Connection status of input and output devices

Audio strategy switching of the system

Setting of volume/audio parameters

 

1.3.2 AudioFlinger

AudioFlinger Accesses AudioHardware to output audio data and control audio parameters. At the same time, the AudioFlinger provides services through the IAudioFinger interface. Therefore, AudioFlinger plays a connecting role in the Audio system framework of Android

 

1.3.3 how towork

AudioTrack: In the Audio subsystem of Android, each audio stream corresponds to an instance of the AudioTrack class. Each AudioTrack will be registered in the AudioFlinger when it is created, and the AudioFlinger will Mixer all the Audiotracks. The current Froyo version of Android allows for the creation of up to 32 audio streams at a time, with a space of 1 meter and 32K for each track. In other words, the Mixer can process up to 32 AudioTrack data streams simultaneously.

 

Binder: AudioTrack and AudioFlinger are not in the same process; they are linked through the Binder mechanism in Android.

Audio_track_cblk_t implements a circular FIFO; AudioTrack is the data producer of the FIFO;

AudioFlinger is the data consumer of FIFO. The process of establishing the connection is Java layer through JNI, New AudioTrack();

A series of calls to getOutput(), based on arguments such as StreamType;

If necessary, the AudioFlinger turns on different hardware devices based on the StreamType; AudioFlinger creates a mixing thread for the output device: MixerThread() and returns the thread ID to AudioTrack as the return value of getOutput();

AudioTrack calls AudioFlinger’s createTrack() with binder;

AudioFlinger registers the AudioTrack to the MixerThread.

AudioFlinger creates a TrackHandle for control and returns createTrack() with the IAudioTrack interface.

AudioTrack Obtains the FIFO(Audio_track_CBLK_T) created in AudioFlinger through the IAudioTrack interface.

AudioTrack creates its own monitoring thread: AudioTrackThread; Since then, AudioTrack has established full contact with AudioFlinger. Then, AudioTrack can: control the status of the track through the IAudioTrack interface, such as start, Stop,pause and so on; Through the FIFO writing, the realization of continuous audio playback; The monitor thread monitors the occurrence of events and interacts with the user program through the audioCallback callback function; Audio_track_cblk_t structure is the key to FIFO implementation. AudioFlinger applies for the corresponding memory when creating Track, and then returns AudioTrack through IMemory interface. In this way, AudioTrack and AudioFlinger manage the same Audio_track_CBLK_T, through which circular FIFO is realized. AudioTrack writes audio data to the FIFO, and AudioFlinger reads audio data from the FIFO. Mixer was sent to AudioHardware for playback. Primary data members of audio_track_CBLK_T: UserBase — The base position of the AudioTrack write offset, ServerBase — Baseline position of the AudioFlinger read offset. FrameCount – The size of the FIFO, measured in frames of audio data. The size of each frame of 16bit audio is 2 bytes – Buffers – Points to the starting address of the FIFO out – The direction of the audio stream For AudioTrack, out=1, for AudioRecord, out=0 FramesAvailable_l () and framesAvailable() are used to get the amount of writable free space in the FIFO, just the difference between locked and unlocked.