Android Audio System Audioflinger (iv)Category: Alsa/audio 2014-06-12 17:37 195 people read Comments (0) favorite reports 1.1.1 Audiomixer
Each mixerthread has a unique corresponding audiomixer (denoted by maudiomixer in Mixerthread), and its function, as its name indicates, is to complete the audio mixing operation.
Figure 13?14 Mixerthread
For example, Mixerthread Open interface mainly involves parameter (such as Setparameter), Resampler (such as Setresampler), Volume (such as Adjustvolumeramp), Buffer (such as Setbufferprovider) and track (such as Gettrackname) five parts.
In the internal implementation, the core of Mixerthread is a mstate variable (state_t type), and all of the mixing work is reflected in this variable-especially the tracks array, as follows:
struct state_t {
uint32_t enabledtracks;
uint32_t needschanged;
size_t Framecount;
void (*hook) (state_t* State, int64_tpts); One of process__*, never NULL
int32_t *outputtemp;
int32_t *resampletemp;
int32_t reserved[2];
track_t tracks[max_num_tracks];__attribute__ ((Aligned ()));
};
Max_num_tracks=32, which means that up to 32 simultaneous mixes are supported, which is certainly sufficient for most cases. Data type track_t is a description of each track, and it is conceivable that such a setting is similar to parameter, which ultimately affects the properties of the track.
struct track_t {
...
Union {
int16_t Volume[max_num_channels];
int32_t Volumerl;
};//Volume-related properties
int32_t Prevvolume[max_num_channels];
int32_t Volumeinc[max_num_channels];
...
uint8_t Channelcount; can only be 1 or 2
uint8_t format; Always 16
uint16_t enabled; is actually a Boolean type
audio_channel_mask_t Channelmask;
audiobufferprovider* Bufferprovider;
Mutableaudiobufferprovider::buffer Buffer; 8 bytes
hook_t Hook;
Const void* in; The current position in buffer
audioresampler* Resampler;
uint32_t samplerate;
int32_t* Mainbuffer;
int32_t* Auxbuffer;
...
BOOL Setresampler (uint32_t samplerate,uint32_t devsamplerate);
BOOL Doesresample () const {return resampler!= NULL;}
void Resetresampler () {if (Resampler!=null) Resampler->reset ();}
void Adjustvolumeramp (bool aux);
size_t getunreleasedframes () const {return Resampler! = NULL? Resampler->getunreleasedframes (): 0;};
};
In Audioflinger's threadloop, data is prepared by constantly invoking preparetracks_l, and each prepare is actually an adjustment to all track. If the attribute changes, the Setparamter is passed to inform Audiomixer.
In the previous section, Threadloop_mix is internally audiomixer to achieve mixing, and here's a concrete look:
void Audiomixer::p rocess (int64_t pts)
{
Mstate.hook (&mstate,pts);
}
"Hook" is the meaning of the hook, why take the name? One reason may be that the entity the hook points to is a change, like a hook, which can be flexibly attached to a variety of objects. From a code level, a hook is a function pointer that points to the following function implementations, depending on the current situation:
Process__validate: According to the current situation, the hook is guided to the following several implementations
PROCESS__NOP: Initializing values
Process__onetrack16bitsstereonoresampling: Only one track,16 bit stereo, no resampling
Process__genericnoresampling: Two-way (inclusive) track, no resampling
Process__genericresampling: Two-way (inclusive) track, resampling
Hooks are re-assigned in the following cases
Øaudiomixer when initializing, hook points to Process_nop
Ø when the state changes or the parameter changes (such as Setparameter), call Invalidatestate. At this point the hook points to process__validate
Øaudiomixer::p rocess is the entrance to the external call hook
Let's take a look at the following diagram to see more clearly:
Figure 13?15 Hook use
Where Process_validate's code is implemented as follows:
void Audiomixer::p rocess__validate (state_t* State, int64_t pts)
{ ...
int countactivetracks = 0;
Boolall16bitsstereonoresample = true;
BOOL resampling = false;..
uint32_t en =state->enabledtracks;
while (en) {
const int i =-__BUILTIN_CLZ (en);
En &= ~ (1<<i);
Track count for countactivetracks++;//enabled status
...
}
State->hook = Process__nop;
if (countactivetracks) {
if (resampling) {
...
State->hook = process__genericresampling;
} else {
...
State->hook = process__genericnoresampling;
if (all16bitsstereonoresample &&!volumeramp) {
if (countactivetracks = = 1) {
State->hook = process__onetrack16bitsstereonoresampling;
}
}
}
}
State->hook (State, PTS);
...
}
This function first through the while loop analysis is in the enabled state of the track, statistics of its internal state bits (such as needs_aux__mask, Needs_resample__mask, etc.), to obtain Countactivetracks, Resampling, Volumeramp and all16bitsstereonoresample reasonable values, finally according to these variables to choose the correct hook implementation, and call this hook function to perform the specific work.
As for how Audiomixer handles audio data, we will do a unified analysis of the subsequent audio streams section.
Android Audio System Audioflinger (iv) "Turn"