Android Audio System II: audioflinger

Source: Internet
Author: User
Introduction

Audioflinger is one of the two major services of the android audio system, and the other is audiopolicyservice. Both of these services are loaded when the system starts. The loaded code is located: frameworks/base/Media/mediaserver/main_mediaserver.cpp. For more information about audiopolicyservice, see Android audio system 3: audiopolicyservice and audiopolicymanager.

Http://blog.csdn.net/DroidPhone/archive/2010/10/18/5949280.aspx

 

This article mainly introduces audioflinger and audioflinger to access audiohardware downwards to output audio data and control audio parameters. At the same time, audioflinger provides services through the iaudiofinger interface. Therefore, audioflinger plays a crucial role in the android audio system framework. The relevant codes of audioflinger are mainly in frameworks/base/libs/audioflinger, and some related codes are also in frameworks/base/Media/libmedia.

Class Structure of audioflinger

The following illustration describes the internal structure and relationship of the audioflinger class:

 

Figure 1 class structure of audioflinger

I don't know if you are the same as me. The first time you see the definition of the audioflinger class is very depressing-This class is really huge and bloated, but after you clarify its relationship, you may think it is quite reasonable. We will discuss it one by one.

  • Iaudioflinger Interface

This is an interface that audioflinger provides external services, such as openoutput, openinput, createtrack, and openrecord. applications or other services can obtain this interface through servicemanager. This interface is obtained by inheriting bnaudioflinger.

  • Threadbase

In audioflinger, Android creates a processing thread for each sound/recording device and is responsible for the I/O and synthesis of audio data. threadbase is the base class of these threads, all playing and recording threads are derived from threadbase.

  • Trackbase

Each time an application creates an audio track (audiotrack/audiorecord), a corresponding track instance is created in audioflinger. trackbase is the base class of these tracks. Its Derived classes include:

    • Playbacktread: track // used for normal playback, corresponding to audiotrack at the application layer
    • Playbackthread: outputtrack // used for output by multiple devices. used when Bluetooth playback is enabled.
    • Recordthread: recordtrack // used for recording, corresponding to audiorecord at the application layer
  • Play

The default playback thread is mixerthread, which is created by audiopolicymanager. The following code is provided in the constructor of audiopolicymanager:

Mhardwareoutput = mpclientinterface-> openoutput (& outputdesc-> mdevice, <br/> & outputdesc-> msamplingrate, <br/> & outputdesc-> mformat, <br/> & outputdesc-> mchannels, <br/> & outputdesc-> mlatency, <br/> outputdesc-> mflags );

It will eventually enter audioflinger's openout function:

...... <Br/> thread = new mixerthread (this, output, ++ mnextthreadid); <br/> ...... <br/> mplaybackthreads. add (mnextthreadid, thread); <br/> ...... <br/> return mnextthreadid;

As you can see, the created thread stores the thread and its ID in the member variable mplaybackthreads of audioflinger. mplaybackthreads is a vector, and the thread created by audioflinger is saved in it. Finally, openoutput returns the thread ID, which is the so-called audio_io_handle_t. The caller of audioflinger can see this handler. When access is required, the audio_io_handle_t will be passed in. audioflinger will pass the mplaybackthreads, obtain the pointer of the thread.

To play a sound, the application must first call createtrack () through the iaudioflinger interface. For details about the createtrack process, refer to my other article:

Http://blog.csdn.net/DroidPhone/archive/2010/10/14/5941344.aspx

Createtrack will call the createtrack_l function of the playbackthread class:

Track = thread-> createtrack_l (client, streamtype, samplerate, format, <br/> channelcount, framecount, sharedbuffer, & lstatus );

In the createtrack_l function, you can see that the playbackthread: track class is created, and then added to the track list mtracks of the playing thread.

Track = thread-> createtrack_l (client, streamtype, samplerate, format, <br/> channelcount, framecount, sharedbuffer, & lstatus); <br/> ...... <br/> mtracks. add (track );

At the end of createtrack, A trackhandle class is created and returned. trackhandle inherits the iaudiotrack interface. Later, the createtrack caller can use the iaudiotrack interface to interact with the corresponding track instance in audioflinger.

Trackhandle = new trackhandle (track); <br/> ...... <br/> return trackhandle;

Finally, when the system is running, the thread and Track Structure in audioflinger is roughly as shown in: it will have multiple working threads and each thread has multiple tracks.

Figure 2 audioflinger thread structure

The playing thread is actually an instance of mixerthread. In the threadloop () of mixerthread, each track in the thread is mixed, and resample is also performed if necessary, convert to a unified sampling rate (44.1 K), and then output audio data through the audiohardware layer of the audio system.

  • Recording

The recording process is similar to the playing process, but the data flows in the opposite direction. The recording thread changes to recordthread, the track changes to recordtrack, And the openrecord returns recordhandle.

  • Duplicatingthread

Audioflinger has a Special Thread class: duplicatingthread. We can see from figure 1 that it is a subclass of mixerthread. When two devices in the system need to output data at the same time, duplicatingthread is created and duplicatingthread is created using the openduplicateoutput method of iaudioflinger.

Int audioflinger: openduplicateoutput (INT output1, int output2) <br/>{< br/> mutex: autolock _ L (mlock ); <br/> mixerthread * thread1 = checkmixerthread_l (output1); <br/> mixerthread * thread2 = checkmixerthread_l (output2); <br/> ...... <br/> duplicatingthread * thread = new duplicatingthread (this, thread1, ++ mnextthreadid); <br/> thread-> addoutputtrack (thread2); <br/> mplaybackthreads. add (mnextthreadid, thread); <br/> return mnextthreadid; <br/>}

When a duplicatingthread is created, two target thread IDs need to be output at the same time are input. openduplicateoutput first obtains the instance of the corresponding output thread from mplaybackthreads Based on the ID, create a virtual audiotrack ---- outputtrack for each thread, and add the virtual audiotrack to the mtracks list of the target thread. The duplicatingthread is in its threadloop, write good mixer data to two virtual outputtracks at the same time. Because these two outputtracks have been added to the mtracks list of the target thread, the two target threads will output the duplicatingthread sound at the same time.

In fact, the creation of duplicatingthread is initiated in audiopolicymanager in audiopolicyservice. When both the Bluetooth headset and the local output are enabled, audiopolicymanager performs the following actions:

  • First open (or create) the Bluetooth output thread a2dpoutput
  • Use hardwareoutput and a2dpoutput as parameters to call openduplicateoutput and create duplicatingthread
  • Move a track of the strategy_media type to a2dpoutput.
  • Move a track of the strategy_dtmf type to a2dpoutput.
  • Move a track of the strategy_sonification type to duplicateoutput.

The result is that the music and DTMF will only be output in the Bluetooth headset, while the buttons, ringtones, and other prompts will be output in the local device and the Bluetooth headset at the same time.

Figure 3 thread and track during local playback

 

 

 

 

 

 

 

 

 

Figure 4 thread and track during Bluetooth playback

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.