《深入理解android》卷1第七章深入理解audio系统的笔记
1.AudioFlinger的启动
./frameworks/av/media/mediaserver/main_mediaserver.cpp
-
int main(int argc, char** argv)
-
{
-
AudioFlinger::instantiate(); //audioFlinger
-
AudioPolicyService::instantiate(); //与audioFlingerService的启动
-
ProcessState::self()->startThreadPool();
-
IPCThreadState::self()->joinThreadPool();
-
}
调用了
instantiate
来启动AF与APS,但是在AudioFlinger.cpp与AudioPolicyServer.cpp中是找不到
instantiate
这个函数的,
为什么呢?
因为看AudioFlinger的定义
-
class AudioFlinger :
-
public BinderService<AudioFlinger>,
-
public BnAudioFlinger
AF是继承自BinderService
,BinderService会创建AudioFlinger这个服务.并调用AudioFlinger的onFirstRef
onFirstRef(它也没有干什么事),执行完成后,AudioFlinger就启动起来了,服务嘛只有别人调它时才会起作用。
2.APS的启动
-
AudioPolicyService::AudioPolicyService() : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
-
{
-
char value[PROPERTY_VALUE_MAX];
-
const struct hw_module_t *module;
-
Mutex::Autolock _l(mLock);
-
mTonePlaybackThread = new AudioCommandThread(String8(""));
-
mAudioCommandThread = new AudioCommandThread(String8("ApmCommand"));
-
hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); //找到库./hardware/libhardware_legacy/audio下的句柄
-
audio_policy_dev_open(module, &mpAudioPolicyDev); //打开库,库指针放在mpAudioPolicyDev中
-
//重要这时mpAudioPolicy就是legacy_audio_policy->policy的一堆函数指针
-
mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, &mpAudioPolicy);
-
mpAudioPolicy->init_check(mpAudioPolicy);
-
if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
-
loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
-
} else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
-
loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
-
}
-
}
2.1 APS调用audio_policy.default的create
在./hardware/libhardware_legacy/audio/audio_policy_hal.cpp中(即库audio_policy.default.so)
-
static int create_legacy_ap(const struct audio_policy_device *device,
-
struct audio_policy_service_ops *aps_ops,
-
void *service,
-
struct audio_policy **ap)
-
{
-
struct legacy_audio_policy *lap;
-
lap = (struct legacy_audio_policy *)calloc(1, sizeof(*lap));
-
//以下初始化lap结构体
-
lap->policy.init_check = ap_init_check;
-
.....一堆lap->policy的函数指针的初始化
-
lap->service = service;
-
lap->aps_ops = aps_ops;
-
//下面这两行看出,以后经常看到的mpClientInterface就是下面的lap->service_client
-
lap->service_client = new AudioPolicyCompatClient(aps_ops, service);
-
lap->apm = createAudioPolicyManager(lap->service_client);
-
-
*ap = &lap->policy; //将lap->policy赋给了mpAudioPolicy,这样fw中的APS就可以调用库中的函数了
-
return 0;
-
}
注1.关于下面这两句
lap
->service_client
= new AudioPolicyCompatClient
(aps_ops
, service
);
lap
->apm
= createAudioPolicyManager
(lap
->service_client
);
--> new AudioPolicyManagerDefault
--> 在构造函数中AudioPolicyManagerBase::AudioPolicyManagerBase
--> mpClientInterface =
lap
->service_client;
所以这个mpClientInterface就是一个AudioPolicyCompatClient
注2.捋一下层次结构
AudioPolicyService-->set_force_use
//在fw层的调用
mpAudioPolicy
->setForce_use //mpAudioPolicy就是audio_policy.default.so的接口
ap_set_force_use
//由此进入库audio_policy.default.so
lap->apm->setForceUse (lap->apm 就是AudioPolicyManager 即AudioPolicyManagerBase)
AudioPolicyManagerBase-->setForceUse
mpClientInterface->setParameters //这个mpClientInterface就是AudioPolicyCompatClient
AudioPolicyCompatClient::setParameters
mServiceOps->set_parameters //这个mServiceOps初始化是传入的fw层的APS
aps_set_parameters
//再次回到fw层
audioPolicyService->setParameters
mAudioCommandThread->parametersCommand
在hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp中
-
AudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
-
:
-
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; //这一句就把APS传来的ops保存在了成员变量中了
-
for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
-
mForceUse[i] = AudioSystem::FORCE_NONE;
-
}
-
initializeVolumeCurves();
-
//在这两个路径下面找auido_policy.conf文件,没有找到的话就用默认的
-
// #define AUDIO_POLICY_CONFIG_FILE "/system/etc/audio_policy.conf"
//#define AUDIO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_policy.conf"
-
//分别把global_configuration与audio_hw_modules加载进来
-
if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
-
if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
-
defaultAudioPolicyConfig();
-
}
-
}
-
-
for (size_t i = 0; i < mHwModules.size(); i++) {
-
mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
-
for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
-
{
-
const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
-
-
if (outProfile->mSupportedDevices & mAttachedOutputDevices) {
-
AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
-
outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice & outProfile->mSupportedDevices);
-
audio_io_handle_t output = mpClientInterface->openOutput(...);
-
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);
-
}
-
}
-
}
-
}
-
updateDevicesAndOutputs();
-
}
循环调用以下函数加载音频设备的库
mpClientInterface
->loadHwModule
aps_load_hw_module(在fw: APS中)
af->loadHwModule (在AudioFlinger中)
af->loadHwModule_l (在AudioFlinger中)
通过调用hw_get_module_by_class把audio.primary.exDroid.so与audio.a2dp.default.so加载进来
注意虽然audio.primary.exDroid.so与audio.a2dp.default.so的id相同都是AUDIO_HARDWARE_MODULE_ID,
但hw_get_module_by_class是通过名字加载的,所以不会冲突
音频库的具体加载过程
./frameworks/av/services/audioflinger/AudioFlinger.cpp
-
audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
-
{
-
audio_hw_device_t *dev;
-
//将audio.primary.exDroid.so与audio.a2dp.default.so通过名字加载进来,因为这儿没有usb的,所以不考虑
-
//有几个库就说明有几个设备,这儿说明在板子上有primary与a2dp两个音频设备
-
int rc = load_audio_interface(name, &dev); //库的接口放在dev中
-
mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags)); //然后把dev(即库的接口)保存在成员变量mAudioHwDevs中
-
return handle;
-
}
注3. AF、APS、audio_policy.default.so与audio.hw.so之间的相互调用
a. fw中的APS调用AF
sp af = AudioSystem::get_audio_flinger();
这就获取了AF,然后就可以通过af调用AudioFilinger的函数接口
b. fw中的APS调用库audio_policy.default.so
在AudioPolicyService的构造函数中,就通过hw_get_module打开了库audio_policy.default.so,
并把接口保存在mpAudioPolicy中,所以就可以通过mpAudioPolicy调用
c. audio_policy.default.so如何调用APS(fw中的AudioPolicyServer)
a. AudioPolicyCompatClient的成员函数audio_policy_service_ops* mServiceOps在构造函数中被初始化为APS的接口指针
b. 这样就可以调用mServiceOps->load_hw_module来访问APS中的函数
d. AF与audio.hw.so之间的调用
在板子上audio.hw.so包括两个:
audio.primary.exDroid.so与audio.a2dp.default.so
AF通过loadHwMoudle_l加载了这两个库,然后分别把库的函数指针保存在mAudioHwDevs
中,
这样就可以通过mAudioHwDevs
来调用
阅读(5113) | 评论(0) | 转发(0) |