Android audio system learning three the startup process of the AudioPolicyService service (below)

The last article roughly sorted out the process of APM parsing configuration files. Next, let’s look at the initialization process of APM, that is, the initialize part.

Review the constructor of APM as follows:
Take a look at the processing content inside initialize:

1. Create an audio engine object, set APM as a listener, and monitor the status of the audio engine.
You can see that there is not much to do in the code. The main thing is to instantiate an engine object and let APM monitor its status.

2.1 loadHwModule()
is actually called to AudioFlinger at the end: loadHwModule_l(), which is why I mentioned in the previous article why to start AF first, because it is used for APS startup;
in order to save trouble, I only take screenshots, and I still have to draw them later The flowchart is added:

Next, let’s see what openDevice has done;
2.1.1 Let’s first see what hw_get_module_by_class has done, the parameter is the name required by the library name to splicing,
you can see that the path of the lib is generated by passing in the AUDIO_HARDWARE_MODULE_ID and name, and then pass * The load() function parses the hal lib, returns a handle handle and assigns it to the incoming parameter &mod
2.1.2:audio_hw_device_open(mod, dev):*
Here is to call the open function of hal, which
actually calls audio_hw. adev_open() in c will only be called once, that is, assign open() to the function pointer in the hardware module.
Return the structure pointers of these hal methods to the originally passed &dev, so that AF can operate down through hal

2.2, openoutput():
2.2.1, findSuitableHwDev_l()****
seems to find the corresponding Handle that can operate the hal interface according to the incoming module;

2.2.2: AudioStreamOut & outHwDev->openOutputStream():
First create a pointer variable of type AudioStreamOut, then call AudioHwDevice::openoutputstream()
Then an AudioStreamOut object is instantiated, and the AudioHwDevice object is also passed in as a parameter
Then call AudioStreamOut:open(), pass hwDev call to hal’s openOutputStream
Looking at the above logic, since the flag passed in when the system starts is AUDIO_OUTPUT_FLAG_NONE , only MixerThread will be created and bound to the output, so that after the startup is completed, the playback sound will be played according to the output to select the corresponding Thread to play, and the subsequent combing and playback The process will be described in detail. As for OFFLOAD and DIRECT, Thread will be created only when the scene is used;

At this point, most of the startup process of APS has been completed. The next operation is to call addOutput(), and setOutputDevice (to set the device to be opened by default, and applyStreamVolumes). This is also the process of updating and playing later to introduce, and finally go to openInputStream, because The , and setOutputDevice (to set the device to be opened by default, and applyStreamVolumes). This is also the process of updating and playing later to introduce, and finally go to openInputStream, because The system not only has playback but also recording process. OpenInputStream and openOutputStream are similar. Interested students can go and see;

3. Summarize the entire startup process of APS:

Android system startup will parse the init.rc configuration file, and then call the main function in main_audioserver.cpp to start AF and APS

The main function will go to the new AudioPolicyService, which will do the following:
3.1. Create an AudioCommandThread for the audio system to process some commands, such as adjusting the volume, start tone, etc.
3.2. Instantiate an AudioPolicyManager object
3.2.1. Parse the configuration file of the audio system Use
3.2.2 and initialize APM for the next APM initialization :
3.2.2.1: Create an engine object, and set APM as a listener
3.2.2.2.1: Loop to load the hal library according to the parsed module set, And assign a value to the structure function pointer through hal adev_open—-hal interface
3.2.3.2.2: Call multiple outProfiles under each module and then call AF’s openOutput, first get an outputstream of hal, and then create The corresponding playback thread is bound to the output. When playing, you can select the corresponding playback thread according to the output.
3.2.3.2.3: Then setOutputDevice to set the default device and apply volume, etc.
3.2.3.2.4: Then there is the operation of openInputStream

3.3 , instantiate an AudioEffect object

At this point, the startup of APS is basically completed. The key point is to obtain the object dev that can operate the hal interface. Stream and dev are [audio_hw_device] , and outputStream is audio_stream_in and audio_stream_out.

Note: The article is used to record the personal understanding of the learning audio process. If the understanding is wrong, please give more advice, thank you~

Leave a Comment

Your email address will not be published. Required fields are marked *