AudioFlinger service 所在进程
frameworks/av/media/audioserver ? ?audioserver git:(0111) ? tree . ├── Android.mk ├── audioserver.rc ├── main_audioserver.cpp └── OWNERS
int main(int argc __unused, char **argv) { ? ? ? ? android::hardware::configureRpcThreadpool(4, false /*callerWillJoin*/); ? ? ? ? sp<ProcessState> proc(ProcessState::self()); ? ? ? ? sp<IServiceManager> sm = defaultServiceManager(); ? ? ? ? ALOGI("ServiceManager: %p", sm.get());
? ? ? ? AudioFlinger::instantiate(); ? ? ? ? ALOGI("ServiceManager: AudioFlinger instantiate done %p", sm.get());
? ? ? ? AudioPolicyService::instantiate(); ? ? ? ? ALOGI("ServiceManager: AudioPolicyService instantiate done %p", sm.get());
? ? ? ? instantiateVRAudioServer(); ? ? ? ? ALOGI("ServiceManager: VRAudioServer instantiate done %p", sm.get());
? ? ? ? ALOGI("ServiceManager: done %p", sm.get()); ? ? ? ? ProcessState::self()->startThreadPool(); ? ? ? ? IPCThreadState::self()->joinThreadPool(); } ?
AudioFlinger::instantiate的实现和AudioPolicyService::instantiate类似,AudioFiliger初始化的过程主要是mDevicesFactoryHal的赋值
AudioFlinger::AudioFlinger() ? ? : BnAudioFlinger(), ? ? ? mMediaLogNotifier(new AudioFlinger::MediaLogNotifier()), ? ? ? mVibratorMsgHandler(NULL), ? ? ? mPrimaryHardwareDev(NULL), ? ? ? mAudioHwDevs(NULL), ? ? ? mHardwareStatus(AUDIO_HW_IDLE), ? ? ? mMasterVolume(1.0f), ? ? ? mMasterMute(false), ? ? ? // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX), ? ? ? mMode(AUDIO_MODE_INVALID), ? ? ? mBtNrecIsOff(false), ? ? ? mIsLowRamDevice(true), ? ? ? mIsDeviceTypeKnown(false), ? ? ? mTotalMemory(0), ? ? ? mClientSharedHeapSize(kMinimumClientSharedHeapSizeBytes), ? ? ? mGlobalEffectEnableTime(0), ? ? ? mPatchPanel(this), ? ? ? mDeviceEffectManager(this), ? ? ? mSystemReady(false) {
? ? mDevicesFactoryHal = DevicesFactoryHalInterface::create();
? ? std::vector<pid_t> halPids; ? ? mDevicesFactoryHal->getHalPids(&halPids);
}
void AudioFlinger::onFirstRef() {
? ? mMode = AUDIO_MODE_NORMAL;
? ? gAudioFlinger = this;
? ? // mDeviceFactoryHal: callback
? ? mDevicesFactoryHalCallback = new DevicesFactoryHalCallbackImpl; ? ? mDevicesFactoryHal->setCallbackOnce(mDevicesFactoryHalCallback); }
mDeviceFactoryHal的创建
mDeviceFactoryHal的数据类型?DevicesFactoryHalInterface
frameworks/av/media/libaudiohal/include/media/audiohal/DevicesFactoryHalInterface.h
class DevicesFactoryHalCallback : public RefBase { ? public: ? ? virtual void onNewDevicesAvailable() = 0; };
class DevicesFactoryHalInterface : public RefBase { ? public: ? ? // Opens a device with the specified name. To close the device, it is ? ? // necessary to release references to the returned object. ? ? virtual status_t openDevice(const char *name, sp<DeviceHalInterface> *device) = 0;
? ? virtual status_t getHalPids(std::vector<pid_t> *pids) = 0;
? ? // Sets a DevicesFactoryHalCallback to notify the client. ? ? // The callback can be only set once. ? ? virtual status_t setCallbackOnce(sp<DevicesFactoryHalCallback> callback) = 0;
? ? static sp<DevicesFactoryHalInterface> create();
? // openDevice的得到的DeviceHalInterface,而create得到是 DevicesFactoryHalInterface,两者什么关系?
? protected: ? ? // Subclasses can not be constructed directly by clients. ? ? DevicesFactoryHalInterface() {}
? ? virtual ~DevicesFactoryHalInterface() {} };
DevicesFactoryHalInterface static函数 create的实现
frameworks/av/media/libaudiohal/DevicesFactoryHalInterface.cpp // static sp<DevicesFactoryHalInterface> DevicesFactoryHalInterface::create() { ? ? return createPreferredImpl<DevicesFactoryHalInterface>( ? ? ? ? ? ? "android.hardware.audio", "IDevicesFactory"); }
create ->?createPreferredImpl
frameworks/av/media/libaudiohal/include/media/audiohal/FactoryHalHidl.h namespace android {
namespace detail {
void* createPreferredImpl(const std::string& package, const std::string& interface);
} ?// namespace detail
/** @Return the preferred available implementation or nullptr if none are available. */ template <class Interface> static sp<Interface> createPreferredImpl(const std::string& package, const std::string& interface) { ? ? return sp<Interface>{static_cast<Interface*>(detail::createPreferredImpl(package, interface))}; }
} // namespace android
createPreferredImpl 遍历 package:?android.hardware.audio的各个版本,寻找接口IDevicesFactory, 找到后就创建HalService: frameworks/av/media/libaudiohal/FactoryHalHidl.cpp void* createPreferredImpl(const std::string& package, const std::string& interface) { ? ? for (auto version = detail::sAudioHALVersions; version != nullptr; ++version) { ? ? ? ? void* rawInterface = nullptr; ? ? ? ? if (hasHalService(package, *version, interface) ? ? ? ? ? ? ? ? && createHalService(*version, interface, &rawInterface)) { ? ? ? ? ? ? return rawInterface; ? ? ? ? } ? ? } ? ? return nullptr; }
const char* sAudioHALVersions[] = { ? ? "6.0", ? ? "5.0", ? ? "4.0", ? ? "2.0", ? ? nullptr };
bool createHalService(const std::string& version, const std::string& interface, ? ? ? ? void** rawInterface) { ? ? const std::string libName = "libaudiohal@" + version + ".so"; ? ? const std::string factoryFunctionName = "create" + interface; ? ? constexpr int dlMode = RTLD_LAZY; ? ? void* handle = nullptr; ? ? dlerror(); // clear ? ? handle = dlopen(libName.c_str(), dlMode); // libName = "libaudiohal@6.0.so" ? ? void* (*factoryFunction)();? ? ? ?// 函数 createIDevicesFactory ? ? *(void **)(&factoryFunction) = dlsym(handle, factoryFunctionName.c_str()); ? ? *rawInterface = (*factoryFunction)(); ? ? ALOGW_IF(!*rawInterface, "Factory function %s from %s returned nullptr", ? ? ? ? ? ? factoryFunctionName.c_str(), libName.c_str()); ? ? return true; }
createIDevicesFactory函数的实现在哪里
frameworks/av/media/libaudiohal/impl/Android.bp
cc_library_shared { ? ? name: "libaudiohal@6.0", ? ? defaults: ["libaudiohal_default"], ? ? shared_libs: [ ? ? ? ? "android.hardware.audio.common@6.0", ? ? ? ? "android.hardware.audio.common@6.0-util", ? ? ? ? "android.hardware.audio.effect@6.0", ? ? ? ? "android.hardware.audio@6.0", ? ? ], }
frameworks/av/media/libaudiohal/impl/DevicesFactoryHalHybrid.cpp extern "C" __attribute__((visibility("default"))) void* createIDevicesFactory() { ? ? auto service = hardware::audio::CPP_VERSION::IDevicesFactory::getService(); ? ? return service ? new CPP_VERSION::DevicesFactoryHalHybrid(service) : nullptr; }
hardware::audio::CPP_VERSION::IDevicesFactory::getService
这里的getService是获得HIDL service, getService是个基类的接口 hardware/interfaces/audio/core/all-versions/vts/functional/DeviceManager.h static sp<IDevicesFactory> createInterfaceInstance(const std::string& name) { ? ? ? ? return IDevicesFactory::getService(name); }
hardware/interfaces/audio/core/all-versions/default/DevicesFactory.cpp namespace android { namespace hardware { namespace audio { namespace CPP_VERSION {? //从这里的命名空间也可以知道getService的实现在这里 namespace implementation { } ?// namespace implementation } ?// namespace CPP_VERSION } ?// namespace audio } ?// namespace hardware } ?// namespace android
new CPP_VERSION::DevicesFactoryHalHybrid(service)
获得HIDL? service的代理 IDeviceFactory 后 创建DevicesFactoryHalHybrid
Hybrid是混合的意思,封装了 local/ hidl的两种实现方法,用户不用关心
DevicesFactoryHalHybrid继承通用接口DeviceFactoryHalInterfaces是DeviceFactoryHalInterface的一个实现,但其中没有 static create();?
frameworks/av/media/libaudiohal/impl/DevicesFactoryHalHybrid.h
using ::android::hardware::audio::CPP_VERSION::IDevicesFactory; namespace android { namespace CPP_VERSION {
class DevicesFactoryHalHybrid : public DevicesFactoryHalInterface { ? public: ? ? DevicesFactoryHalHybrid(sp<IDevicesFactory> hidlFactory);
? ? // Opens a device with the specified name. To close the device, it is ? ? // necessary to release references to the returned object. ? ? virtual status_t openDevice(const char *name, sp<DeviceHalInterface> *device);
? ? ? ? ? ? status_t getHalPids(std::vector<pid_t> *pids) override;
? ? ? ? ? ? status_t setCallbackOnce(sp<DevicesFactoryHalCallback> callback) override;
? private: ? ? sp<DevicesFactoryHalInterface> mLocalFactory; ? ? sp<DevicesFactoryHalInterface> mHidlFactory; };
} // namespace CPP_VERSION } // namespace android
DevicesFactoryHalHybrid构造函数赋值了成员变量
DevicesFactoryHalHybrid::DevicesFactoryHalHybrid(sp<IDevicesFactory> hidlFactory) ? ? ? ? : mLocalFactory(new DevicesFactoryHalLocal()), ? ? ? ? ? mHidlFactory(new DevicesFactoryHalHidl(hidlFactory)) { }
Local:
Local的意思是没有使用HIDL service, 直接local? 加载HAL的so
构造函数并没有什么 DevicesFactoryHalLocal() {}
HidlFactory:
DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) { ? ? ALOG_ASSERT(devicesFactory != nullptr, "Provided default IDevicesFactory service is NULL"); ? ? addDeviceFactory(devicesFactory, false /*needToNotify*/); }
void DevicesFactoryHalHidl::addDeviceFactory(sp<IDevicesFactory> factory, bool needToNotify) { ? ? // It is assumed that the DevicesFactoryHalInterface instance is owned ? ? // by AudioFlinger and thus have the same lifespan. ? ? factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/); ? ? sp<DevicesFactoryHalCallback> callback; ? ? { ? ? ? ? std::lock_guard<std::mutex> lock(mLock); ? ? ? ? mDeviceFactories.push_back(factory); ? ? ? ? if (needToNotify) { ? ? ? ? ? ? callback = mCallback.promote(); ? ? ? ? ? ? if (!callback) { ? ? ? ? ? ? ? ? mHaveUndeliveredNotifications = true; ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? if (callback) { ? ? ? ? callback->onNewDevicesAvailable(); ? ? } }
从openDevice的实现可以看出Hybrid的意义所在
status_t DevicesFactoryHalHybrid::openDevice(const char *name, sp<DeviceHalInterface> *device) { ? ? if (mHidlFactory != 0 && strcmp(AUDIO_HARDWARE_MODULE_ID_A2DP, name) != 0 && ? ? ? ? strcmp(AUDIO_HARDWARE_MODULE_ID_HEARING_AID, name) != 0) { ? ? ? ? return mHidlFactory->openDevice(name, device); ? ? } ? ? return mLocalFactory->openDevice(name, device); }
总结
mDevicesFactoryHal 是访问Device HAL的接口,?mHidlFactory 还是mLocalFactory 是内部使用的成员变量 不用关心。AudioFlinger? service的初始化主要是和HAL、 HAL service的关联,得到访问 HAL device的接口mDeviceFactortyHAL
|