转---mixer_thread在android启动的时候已经建立了
mixer_thread在android启动的时候已经建立了,而且运行了。在AudioPolicyService创建的时候,就运行了。
看到堆栈信息,可以看到,AudioPolicyService()构造函数的时候调用的,构造mixer_thread。
E/AudioPolicyService( 5256): xiayu aps_open_output_on_module start
D/CallStack( 5256): #00 pc 0004a778 /system/lib/libaudioflinger.so
D/CallStack( 5256): #01 pc 00014190 /system/lib/hw/audio_policy.msm8960.so (android_audio_legacy::AudioPolicyCompatClient::openOutput(int, unsigned int*, unsigned int*, audio_format_t*, unsigned int*, unsigned int*, audio_output_flags_t)+23)
D/CallStack( 5256): #02 pc 00012dca /system/lib/hw/audio_policy.msm8960.so (android_audio_legacy::AudioPolicyManagerBase::AudioPolicyManagerBase(android_audio_legacy::AudioPolicyClientInterface*)+529)
D/CallStack( 5256): #03 pc 0000b544 /system/lib/hw/audio_policy.msm8960.so (createAudioPolicyManager+15)
D/CallStack( 5256): #04 pc 0000a5b6 /system/lib/hw/audio_policy.msm8960.so
D/CallStack( 5256): #05 pc 00049152 /system/lib/libaudioflinger.so (android::AudioPolicyService::AudioPolicyService()+289)
D/CallStack( 5256): #06 pc 00000c96 /system/bin/mediaserver
D/CallStack( 5256): #07 pc 00000d7e /system/bin/mediaserver
D/CallStack( 5256): #08 pc 000127fc /system/lib/libc.so (__libc_init+35)
而AudioPolicyService()是在./frameworks/av/media/mediaserver/main_mediaserver.cpp中实例化的。
AudioPolicyService::instantiate();
从分析AudioPolicyService()开始:
- frameworks/av/services/audioflinger/AudioPolicyService.cpp
AudioPolicyService::AudioPolicyService()
: BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
&mpAudioPolicy);
2.hardware/qcom/audio/alsa_sound/audio_policy_hal.cpp
static int qcom_ap_dev_open(const hw_module_t* module, const char* name,
hw_device_t** device)
dev->device.create_audio_policy = create_qcom_ap;
3.hardware/qcom/audio/alsa_sound/audio_policy_hal.cpp
static int create_qcom_ap(const struct audio_policy_device *device,
struct audio_policy_service_ops *aps_ops,
void *service,
struct audio_policy **ap)
qap->apm = createAudioPolicyManager(qap->service_client);
4.hardware/qcom/audio/alsa_sound/AudioPolicyManagerALSA.cpp
extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
{
return new AudioPolicyManager(clientInterface);
}
5.hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp
class AudioPolicyManager: public AudioPolicyManagerBase
{
public:
AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
: AudioPolicyManagerBase(clientInterface) {}
AudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
:
#ifdef AUDIO_POLICY_TEST
Thread(false),
#endif //AUDIO_POLICY_TEST
mPrimaryOutput((audio_io_handle_t)0),
mAvailableOutputDevices(AUDIO_DEVICE_NONE),
mPhoneState(AudioSystem::MODE_NORMAL),
mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false)
{
mpClientInterface = clientInterface;
for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
mForceUse[i] = AudioSystem::FORCE_NONE;
}
initializeVolumeCurves();
mA2dpDeviceAddress = String8("");
mScoDeviceAddress = String8("");
mUsbCardAndDevice = String8("");
//使用 配置文件 AUDIO_POLICY_CONFIG_FILE ("/system/etc/audio_policy.conf")
if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
ALOGE("could not load audio policy configuration file, setting defaults");
defaultAudioPolicyConfig();
}
// open all output streams needed to access attached devices
for (size_t i = 0; i < mHwModules.size(); i++) {
//会循环两次第一次HW module=primary
// 第二次 HW module= r_submix
//打开按键声音,也是循环两次。
mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
if (mHwModules[i]->mHandle == 0) {
ALOGW("could not open HW module %s", mHwModules[i]->mName);
continue;
}
// open all output streams needed to access attached devices
for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
{
//HW module=primary mOutputProfiles.size()=6 , j=1,5的时候,openOutput. 产生mixer_thread.
//HW module= r_submix不产生mixer_thread.
const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
// V/AudioPolicyManagerBase( 4396): loadGlobalConfig() mAttachedOutputDevices 0003
//判断过程:文件中/system/etc/audio_policy.conf ,global_configuration的attached_output_devices就是mAttachedOutputDevices,outProfile->mSupportedDevices就是每个设备的
if ( (outProfile->mSupportedDevices & mAttachedOutputDevices) &&
!(outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) ){
AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
outProfile->mSupportedDevices);
audio_io_handle_t output = mpClientInterface->openOutput(//生产mixer_thread.
outProfile->mModule->mHandle,
&outputDesc->mDevice,
&outputDesc->mSamplingRate,
&outputDesc->mFormat,
&outputDesc->mChannelMask,
&outputDesc->mLatency,
outputDesc->mFlags);
if (output == 0) {
delete outputDesc;
} else {
mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
(outProfile->mSupportedDevices & mAttachedOutputDevices));
if (mPrimaryOutput == 0 &&
outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
mPrimaryOutput = output;
}
addOutput(output, outputDesc);
setOutputDevice(output,
(audio_devices_t)(mDefaultOutputDevice &
outProfile->mSupportedDevices),
true);
}
}
}
}
audio_io_handle_t AudioPolicyCompatClient::openOutput(audio_module_handle_t module,
audio_devices_t *pDevices,
uint32_t *pSamplingRate,
audio_format_t *pFormat,
audio_channel_mask_t *pChannelMask,
uint32_t *pLatencyMs,
audio_output_flags_t flags)
{
return mServiceOps->open_output_on_module(mService, module, pDevices, pSamplingRate,
pFormat, pChannelMask, pLatencyMs,
flags);
}
7.frameworks/av/services/audioflinger/AudioFlinger.cpp
audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
audio_devices_t *pDevices,
uint32_t *pSamplingRate,
audio_format_t *pFormat,
audio_channel_mask_t *pChannelMask,
uint32_t *pLatencyMs,
audio_output_flags_t flags)
thread = new MixerThread(this, output, id, *pDevices);
两次输出log
openOutput(), module 1 Device 2, SamplingRate 44100, Format 1, Channels 3, flags 2
openOutput(), module 1 Device 2, SamplingRate 8000, Format 1, Channels 3, flags 4
V/AudioFlinger( 295): openOutput() created mixer output: ID 2 thread 0x40b161d8
V/AudioFlinger( 295): openOutput() created mixer output: ID 3 thread 0x40cc3c00
V/AudioFlinger( 268): MixerThread() id=2 device=0x2 type=0
V/AudioFlinger( 268): mSampleRate=48000, mChannelMask=0x3, mChannelCount=2, mFormat=1, mFrameSize=4, mFrameCount=512, mNormalFrameCount=1024
V/AudioFlinger( 268): MixerThread() id=3 device=0x2 type=0
V/AudioFlinger( 268): mSampleRate=48000, mChannelMask=0x3, mChannelCount=2, mFormat=1, mFrameSize=4, mFrameCount=256, mNormalFrameCount=1024
为什么mSampleRate=48000,应该是有一个AudioTrack建立是赋值的。
注意:到这里 MixerThread还没有运行。
接着向下看
if (thread != NULL) {
mPlaybackThreads.add(id, thread);
}
DefaultKeyedVector< audio_io_handle_t, sp<PlaybackThread> > mPlaybackThreads;
看见sp了没有,首次创建sp时会执行onFirstRef
void AudioFlinger::PlaybackThread::onFirstRef()
{
run(mName, ANDROID_PRIORITY_URGENT_AUDIO);
}
到这里Mixthread开始运行。
Vector <uint32_t> mSamplingRates,打印的时候只打印第一个采样。
mSamplingRate = profile->mSamplingRates[0];
附录:
从/system/etc/audio_policy.conf读取频率。
hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp
void AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
{
char *str = strtok(name, "|");
// by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
// rates should be read from the output stream after it is opened for the first time
if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
profile->mSamplingRates.add(0);
return;
}
while (str != NULL) {
uint32_t rate = atoi(str);
if (rate != 0) {
ALOGV("loadSamplingRates() adding rate %d", rate);
profile->mSamplingRates.add(rate);
}
str = strtok(NULL, "|");
}
return;
}