AudioPolicyServer audio模块加载
/frameworks/av/media/mediaserver/main_mediaserver.cpp int main(int argc, char** argv) { AudioFlinger::instantiate(); AudioPolicyService::instantiate(); //audioFlingerService启动 ProcessState::self()->startThreadPool(); IPCThreadState::self()->joinThreadPool(); } //hardware/libhardware/hardware.c int hw_get_module(const char *id, const struct hw_module_t **module) { return hw_get_module_by_class(id, NULL, module); }
AudioPolicyService和AudioPolicyManager结构关系
AudioPolicyService中的具体实现实际都是AudioPolicyManager完成的
class AudioPolicyService { sp<AudioCommandThread> mOutputCommandThread; // process stop and release output struct audio_policy_device *mpAudioPolicyDev; struct audio_policy *mpAudioPolicy; //对应的是struct audio_policy policy;(audio_policy_hal.cpp)最后调用 AudioPolicyManager中的方法 AudioPolicyInterface *mAudioPolicyManager; AudioPolicyClient *mAudioPolicyClient; // Manage all effects configured in audio_effects.conf sp<AudioPolicyEffects> mAudioPolicyEffects; audio_mode_t mPhoneState; } class AudioPolicyManager { AudioPolicyClientInterface *mpClientInterface; // 对应的就是AudioPolicyClient audio_io_handle_t mPrimaryOutput; // primary output handle DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mPreviousOutputs; DefaultKeyedVector<audio_io_handle_t, sp<AudioInputDescriptor> > mInputs; // list of input descriptors DeviceVector mAvailableOutputDevices; // all available output devices DeviceVector mAvailableInputDevices; // all available input devices int mPhoneState; } class AudioPolicyCompatClient : public AudioPolicyClientInterface { AudioPolicyCompatClient(struct audio_policy_service_ops *serviceOps, void *service) : mServiceOps(serviceOps) , mService(service) {} private: struct audio_policy_service_ops* mServiceOps; //对应的是 AudioPolicyService的Methods void* mService; //对应的是 AudioPolicyService //...... }
AudioPolicyManager连接 AudioPolicyService
AudioPolicyManager中并没有直接与AudioPolicyService连接,而是通过 AudioPolicyCompatClient连接的
AudioPolicyManager通过AudioPolicyCompatClient调用 AudioPolicyService开放的接口
AudioPolicyService::AudioPolicyService() : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL), mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID) { } void AudioPolicyService::onFirstRef() //初始化的时候会调用这个函数 { char value[PROPERTY_VALUE_MAX]; const struct hw_module_t *module; int forced_val; int rc; { Mutex::Autolock _l(mLock); // start tone playback thread mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this); // start audio commands thread mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this); // start output activity command thread mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
//对于google代码定义了USE_LEGACY_AUDIO_POLICY, 但是运营商会去掉这个宏,从而实现自己的设计方案。 #ifdef USE_LEGACY_AUDIO_POLICY ALOGI("AudioPolicyService CSTOR in legacy mode");
hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
audio_policy_dev_open(module, &mpAudioPolicyDev);
mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, &mpAudioPolicy); #else ALOGI("AudioPolicyService CSTOR in new mode");
// mAudioPolicyClient = new AudioPolicyClient(this); mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);//对应的模块加载放在AudioPolicyManager的构造函数中 #endif } sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects(); { Mutex::Autolock _l(mLock); mAudioPolicyEffects = audioPolicyEffects; } }
frameworks/av/services/audiopolicy/AudioPolicyManager.h //这个类的初始化是解析audio_policy.conf //初始化mHwModules //获得可用的设备mAvailableOutputDevices(先是存放所有设备,然后去掉不可到达的设备) //创建相关的output并存储起来mOutputs,同时根据flags给mPrimaryOutput作为首要的输出output //mDefaultOutputDevice初始化默认的输出设备,这里是speaker class AudioPolicyManager: public AudioPolicyInterface { AudioPolicyClientInterface *mpClientInterface; // audio policy client interface audio_io_handle_t mPrimaryOutput; // primary output handle // list of descriptors for available outputs currently opened DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mOutputs; // copy of mOutputs before setDeviceConnectionState() opens new outputs // reset to mOutputs when updateDevicesAndOutputs() is called. DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mPreviousOutputs; DefaultKeyedVector<audio_io_handle_t, sp<AudioInputDescriptor> > mInputs; // list of input descriptors DeviceVector mAvailableOutputDevices; // all available output devices DeviceVector mAvailableInputDevices; // all available input devices int mPhoneState; // current phone state uint32_t mRingerMode; audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT]; // current forced use configuration sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time, initialized to speaker Vector < sp<HwModule> > mHwModules; }
//frameworks/av/services/audiopolicy/AudioPolicyManager.cpp AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) : mPrimaryOutput((audio_io_handle_t)0), mPhoneState(AUDIO_MODE_NORMAL), mRingerMode(0), mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0), mA2dpSuspended(false), mSpeakerDrcEnabled(false), mNextUniqueId(1), mHdmiAudioDisabled(false), mHdmiAudioEvent(false), mPrevPhoneState(0), mAudioPortGeneration(1), mPrimarySuspended (0), mFastSuspended(0), mDRMMuteRequest(false), mMutedForDRM(false), mMultiChannelSuspended(0), mMediaMutedForTempRoute(false) { mUidCached = getuid(); mpClientInterface = clientInterface; //AudioPolicyService::AudioPolicyClient(继承AudioPolicyClientInterface) //初始化强制使用的一些设备,默认不使用强制设备 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) { mForceUse[i] = AUDIO_POLICY_FORCE_NONE; } //初始化默认使用的设备,这里是speaker mDefaultOutputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_OUT_SPEAKER); //先加载vendor定义的文件audio_policy.conf,如果加载失败则加载默认的配置文件audio_policy.conf if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) { //对sony手机而言,是加载的默认配置(这个配置被sony修改了的) if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) { ALOGE("could not load audio policy configuration file, setting defaults"); defaultAudioPolicyConfig(); } } // must be done after reading the policy initializeVolumeCurves(); // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices // open all output streams needed to access attached devices // 这里是指可用的设备 // 这里是赋值所有可能使用的设备,包括可能没有连接上的设备,比如蓝牙、usb设备 // 后面会将某些不可reach的设备去掉。这样留下的设备是当前存在且可以使用的设备 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); //类似outputDeviceTypes audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; for (size_t i = 0; i < mHwModules.size(); i++) { //这里加载HwModule会很复杂 //主要调用AudioFlinger::loadHwModule()=>...=> //==>audio_hw_primary: adev_open(): enter //adev_open()这个函数只会被调用一次,也就是给硬件模块中的函数指针赋值open() 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 // except for direct output streams that are only opened when they are actually required by an app. // This also validates mAvailableOutputDevices list // 对所有的模块,创建对应的output for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; if (outProfile->mSupportedDevices.isEmpty()) { ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName); continue; } if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { continue; } audio_devices_t profileType = outProfile->mSupportedDevices.types(); if ((profileType & mDefaultOutputDevice->mDeviceType) != AUDIO_DEVICE_NONE) { profileType = mDefaultOutputDevice->mDeviceType; } else { // chose first device present in mSupportedDevices also part of // outputDeviceTypes for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { profileType = outProfile->mSupportedDevices[k]->mDeviceType; if ((profileType & outputDeviceTypes) != 0) { break; } } } if ((profileType & outputDeviceTypes) == 0) { continue; } sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile); outputDesc->mDevice = profileType; audio_config_t config = AUDIO_CONFIG_INITIALIZER; config.sample_rate = outputDesc->mSamplingRate; config.channel_mask = outputDesc->mChannelMask; config.format = outputDesc->mFormat; audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle, &output, &config, &outputDesc->mDevice, String8(""), &outputDesc->mLatency, outputDesc->mFlags); if (status != NO_ERROR) { ALOGW("Cannot open output stream for device %08x on hw module %s", outputDesc->mDevice, mHwModules[i]->mName); } else { outputDesc->mSamplingRate = config.sample_rate; outputDesc->mChannelMask = config.channel_mask; outputDesc->mFormat = config.format; for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType; ssize_t index = mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]); // give a valid ID to an attached device once confirmed it is reachable if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) { mAvailableOutputDevices[index]->mId = nextUniqueId(); mAvailableOutputDevices[index]->mModule = mHwModules[i]; } } if (mPrimaryOutput == 0 && outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { //primaryOutput mPrimaryOutput = output; } //将创建的output加入到mOutputs中 addOutput(output, outputDesc); //切设备 setOutputDevice(output, outputDesc->mDevice, true); } } // open input streams needed to access attached devices to validate // mAvailableInputDevices list // 对所有的模块,创建对应的input for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) { const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; if (inProfile->mSupportedDevices.isEmpty()) { ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName); continue; } // chose first device present in mSupportedDevices also part of // inputDeviceTypes audio_devices_t profileType = AUDIO_DEVICE_NONE; for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { profileType = inProfile->mSupportedDevices[k]->mDeviceType; if (profileType & inputDeviceTypes) { break; } } if ((profileType & inputDeviceTypes) == 0) { continue; } sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile); inputDesc->mInputSource = AUDIO_SOURCE_MIC; inputDesc->mDevice = profileType; audio_config_t config = AUDIO_CONFIG_INITIALIZER; config.sample_rate = inputDesc->mSamplingRate; config.channel_mask = inputDesc->mChannelMask; config.format = inputDesc->mFormat; audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle, &input, &config, &inputDesc->mDevice, String8(""), AUDIO_SOURCE_MIC, AUDIO_INPUT_FLAG_NONE); if (status == NO_ERROR) { for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType; ssize_t index = mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]); // give a valid ID to an attached device once confirmed it is reachable if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) { mAvailableInputDevices[index]->mId = nextUniqueId(); mAvailableInputDevices[index]->mModule = mHwModules[i]; } } mpClientInterface->closeInput(input); } else { ALOGW("Cannot open input stream for device %08x on hw module %s", inputDesc->mDevice, mHwModules[i]->mName); } } } // make sure all attached devices have been allocated a unique ID //将没有分配到id的设备,也就是不可reach的设备从mAvailableOutputDevices去掉。也就是当前该设备不可到达 for (size_t i = 0; i < mAvailableOutputDevices.size();) { if (mAvailableOutputDevices[i]->mId == 0) { ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType); mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); continue; } i++; } for (size_t i = 0; i < mAvailableInputDevices.size();) { if (mAvailableInputDevices[i]->mId == 0) { ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType); mAvailableInputDevices.remove(mAvailableInputDevices[i]); continue; } i++; } // make sure default device is reachable if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType); } ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); updateDevicesAndOutputs(); }
//frameworks/av/services/audioflinger/AudioFlinger.cpp audio_module_handle_t AudioFlinger::loadHwModule(const char *name) { return loadHwModule_l(name); } audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) { for (size_t i = 0; i < mAudioHwDevs.size(); i++) { if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { ALOGW("loadHwModule() module %s already loaded", name); return mAudioHwDevs.keyAt(i); } } audio_hw_device_t *dev; int rc = load_audio_interface(name, &dev); if (rc) { ALOGI("loadHwModule() error %d loading module %s ", rc, name); return 0; } mHardwareStatus = AUDIO_HW_INIT; rc = dev->init_check(dev); mHardwareStatus = AUDIO_HW_IDLE; if (rc) { ALOGI("loadHwModule() init check error %d for module %s ", rc, name); return 0; } audio_module_handle_t handle = nextUniqueId(); mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags)); ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d", name, dev->common.module->name, dev->common.module->id, handle); return handle; } static int load_audio_interface(const char *if_name, audio_hw_device_t **dev) { const hw_module_t *mod; int rc; rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); rc = audio_hw_device_open(mod, dev); }
// hardware/libhardware/include/hardware/audio.h // convenience API for opening and closing a supported device static inline int audio_hw_device_open(const struct hw_module_t* module, struct audio_hw_device** device) { return module->methods->open(module, AUDIO_HARDWARE_INTERFACE, (struct hw_device_t**)device); } static inline int audio_hw_device_close(struct audio_hw_device* device) { return device->common.close(&device->common); }
//hardware/qcom/audio/hal/audio_hw.c static struct hw_module_methods_t hal_module_methods = { .open = adev_open, //函数指针列表赋值 }; struct audio_module HAL_MODULE_INFO_SYM = { .common = { .tag = HARDWARE_MODULE_TAG, .module_api_version = AUDIO_MODULE_API_VERSION_0_1, .hal_api_version = HARDWARE_HAL_API_VERSION, .id = AUDIO_HARDWARE_MODULE_ID, .name = "QCOM Audio HAL", .author = "The Linux Foundation", .methods = &hal_module_methods, }, };
----------------------------------------------------------------------
void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) { ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); bool forceVolumeReeval = false; switch(usage) { case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: //通话时可以考虑强制使用speaker if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) { ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); return; } forceVolumeReeval = true; mForceUse[usage] = config; break; case AUDIO_POLICY_FORCE_FOR_MEDIA: case AUDIO_POLICY_FORCE_FOR_RECORD: case AUDIO_POLICY_FORCE_FOR_DOCK: case AUDIO_POLICY_FORCE_FOR_SYSTEM: //拍照时的声音可能是走的这个 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); return; } forceVolumeReeval = true; mForceUse[usage] = config; break; //.................. } }
                    
                
                
            
        
浙公网安备 33010602011771号