Android開發之系統音訊庫載入全過程
本文基於android7.0分析
在介紹解析音訊配置檔案文章中 已經說了,系統音訊庫的載入與否取決audio_policy.conf是否有配置其資訊。
當將audio_policy.conf解析完成之後,接下來就是根據解析好的配置檔案資料去載入系統音訊庫,我們繼續看到 AudioPolicyManager.cpp 建構函式,原始碼位於: \frameworks\av\services\audiopolicy\managerdefault\AudioPolicyManager.cpp
... AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) : #ifdef AUDIO_POLICY_TEST Thread(false), #endif //AUDIO_POLICY_TEST mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), mA2dpSuspended(false), mAudioPortGeneration(1), mBeaconMuteRefCount(0), mBeaconPlayingRefCount(0), mBeaconMuted(false), mTtsOutputAvailable(false), mMasterMono(false) { ... for (size_t i = 0; i < mHwModules.size(); i++) { mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); if (mHwModules[i]->mHandle == 0) { ALOGW("could not open HW module %s", mHwModules[i]->getName()); continue; } ... } ... }
mpClientInterface 常量為 AudioPolicyClientInterface物件,原始碼位於: \hardware\libhardware_legacy\include\hardware_legacy\AudioPolicyClientInterface.h
AudioPolicyClient物件,實現 AudioPolicyClientInterface.h AudioPolicyClient物件 定義在 \frameworks\av\services\audiopolicy\service\AudioPolicyService.h AudioPolicyClient物件 實現在: \frameworks\av\services\audiopolicy\service\AudioPolicyClientImpl.cpp
遍歷 解析配置檔案後的 mHwModules 所有的子HwModule , 從 mHwModules[i]->getName() 可以看到載入系統音訊庫只用到了 HwModule 的name 屬性值 以 primary 為例子 找到 AudioPolicyClient物件 實現
audio_module_handle_t AudioPolicyService::AudioPolicyClient::loadHwModule(const char *name) { sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); if (af == 0) { ALOGW("%s: could not get AudioFlinger", __func__); return AUDIO_MODULE_HANDLE_NONE; } return af->loadHwModule(name);
最終呼叫到 AudioFlinger 原始碼位於: \frameworks\av\services\audioflinger\AudioFlinger.cpp
audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
{
if (name == NULL) {
return AUDIO_MODULE_HANDLE_NONE;
}
if (!settingsAllowed()) {
return AUDIO_MODULE_HANDLE_NONE;
}
Mutex::Autolock _l(mLock);
return loadHwModule_l(name);
}
// loadHwModule_l() must be called with AudioFlinger::mLock held
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) {
ALOGE("loadHwModule() error %d loading module %s", rc, name);
return AUDIO_MODULE_HANDLE_NONE;
}
mHardwareStatus = AUDIO_HW_INIT;
rc = dev->init_check(dev);
mHardwareStatus = AUDIO_HW_IDLE;
if (rc) {
ALOGE("loadHwModule() init check error %d for module %s", rc, name);
return AUDIO_MODULE_HANDLE_NONE;
}
// Check and cache this HAL's level of support for master mute and master
// volume. If this is the first HAL opened, and it supports the get
// methods, use the initial values provided by the HAL as the current
// master mute and volume settings.
AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
{ // scope for auto-lock pattern
AutoMutex lock(mHardwareLock);
if (0 == mAudioHwDevs.size()) {
mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
if (NULL != dev->get_master_volume) {
float mv;
if (OK == dev->get_master_volume(dev, &mv)) {
mMasterVolume = mv;
}
}
mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
if (NULL != dev->get_master_mute) {
bool mm;
if (OK == dev->get_master_mute(dev, &mm)) {
mMasterMute = mm;
}
}
}
mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
if ((NULL != dev->set_master_volume) &&
(OK == dev->set_master_volume(dev, mMasterVolume))) {
flags = static_cast<AudioHwDevice::Flags>(flags |
AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
}
mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
if ((NULL != dev->set_master_mute) &&
(OK == dev->set_master_mute(dev, mMasterMute))) {
flags = static_cast<AudioHwDevice::Flags>(flags |
AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
}
mHardwareStatus = AUDIO_HW_IDLE;
}
audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
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;
}
可以看到 mAudioHwDevs 是一個 DefaultKeyedVector<audio_module_handle_t, AudioHwDevice*> mAudioHwDevs; key value 的集合容器,
通過 mAudioHwDevs 容器 裝載所有的系統音訊物件 AudioHwDevice AudioHwDevice物件位於: \frameworks\av\services\audioflinger\AudioHwDevice.h
載入每個音訊庫之前,會遍歷 mAudioHwDevs 中是否已經載入,載入成功則不會再次載入
如果沒有載入,則來到 load_audio_interface 函式
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);
ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
if (rc) {
goto out;
}
rc = audio_hw_device_open(mod, dev);
ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
if (rc) {
goto out;
}
if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
rc = BAD_VALUE;
goto out;
}
return 0;
out:
*dev = NULL;
return rc;
}
可以看到其實就是一個系統硬體抽象層載入過程,這裡不再累贅,大家參考之前的文章: Android開發之HAL層
載入成功後,輸出音訊硬體抽象層模組音訊裝置結構體,audio_hw_device_t,作為AudioHwDevice物件的第三個引數 定義在:hardware\libhardware\include\hardware\audio.h
struct audio_hw_device {
/**
* Common methods of the audio device. This *must* be the first member of audio_hw_device
* as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts
* where it's known the hw_device_t references an audio_hw_device.
*/
struct hw_device_t common;
/**
* used by audio flinger to enumerate what devices are supported by
* each audio_hw_device implementation.
*
* Return value is a bitmask of 1 or more values of audio_devices_t
*
* NOTE: audio HAL implementations starting with
* AUDIO_DEVICE_API_VERSION_2_0 do not implement this function.
* All supported devices should be listed in audio_policy.conf
* file and the audio policy manager must choose the appropriate
* audio module based on information in this file.
*/
uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
/**
* check to see if the audio hardware interface has been initialized.
* returns 0 on success, -ENODEV on failure.
*/
int (*init_check)(const struct audio_hw_device *dev);
/** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
/**
* set the audio volume for all audio activities other than voice call.
* Range between 0.0 and 1.0. If any value other than 0 is returned,
* the software mixer will emulate this capability.
*/
int (*set_master_volume)(struct audio_hw_device *dev, float volume);
/**
* Get the current master volume value for the HAL, if the HAL supports
* master volume control. AudioFlinger will query this value from the
* primary audio HAL when the service starts and use the value for setting
* the initial master volume across all HALs. HALs which do not support
* this method may leave it set to NULL.
*/
int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
/**
* set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
* is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
* playing, and AUDIO_MODE_IN_CALL when a call is in progress.
*/
int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
/* mic mute */
int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
/* set/get global audio parameters */
int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
/*
* Returns a pointer to a heap allocated string. The caller is responsible
* for freeing the memory for it using free().
*/
char * (*get_parameters)(const struct audio_hw_device *dev,
const char *keys);
/* Returns audio input buffer size according to parameters passed or
* 0 if one of the parameters is not supported.
* See also get_buffer_size which is for a particular stream.
*/
size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
const struct audio_config *config);
/** This method creates and opens the audio hardware output stream.
* The "address" parameter qualifies the "devices" audio device type if needed.
* The format format depends on the device type:
* - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC"
* - USB devices use the ALSA card and device numbers in the form "card=X;device=Y"
* - Other devices may use a number or any other string.
*/
int (*open_output_stream)(struct audio_hw_device *dev,
audio_io_handle_t handle,
audio_devices_t devices,
audio_output_flags_t flags,
struct audio_config *config,
struct audio_stream_out **stream_out,
const char *address);
void (*close_output_stream)(struct audio_hw_device *dev,
struct audio_stream_out* stream_out);
/** This method creates and opens the audio hardware input stream */
int (*open_input_stream)(struct audio_hw_device *dev,
audio_io_handle_t handle,
audio_devices_t devices,
struct audio_config *config,
struct audio_stream_in **stream_in,
audio_input_flags_t flags,
const char *address,
audio_source_t source);
void (*close_input_stream)(struct audio_hw_device *dev,
struct audio_stream_in *stream_in);
/** This method dumps the state of the audio hardware */
int (*dump)(const struct audio_hw_device *dev, int fd);
/**
* set the audio mute status for all audio activities. If any value other
* than 0 is returned, the software mixer will emulate this capability.
*/
int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
/**
* Get the current master mute status for the HAL, if the HAL supports
* master mute control. AudioFlinger will query this value from the primary
* audio HAL when the service starts and use the value for setting the
* initial master mute across all HALs. HALs which do not support this
* method may leave it set to NULL.
*/
int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
/**
* Routing control
*/
/* Creates an audio patch between several source and sink ports.
* The handle is allocated by the HAL and should be unique for this
* audio HAL module. */
int (*create_audio_patch)(struct audio_hw_device *dev,
unsigned int num_sources,
const struct audio_port_config *sources,
unsigned int num_sinks,
const struct audio_port_config *sinks,
audio_patch_handle_t *handle);
/* Release an audio patch */
int (*release_audio_patch)(struct audio_hw_device *dev,
audio_patch_handle_t handle);
/* Fills the list of supported attributes for a given audio port.
* As input, "port" contains the information (type, role, address etc...)
* needed by the HAL to identify the port.
* As output, "port" contains possible attributes (sampling rates, formats,
* * channel masks, gain controllers...) for this port.
*/
int (*get_audio_port)(struct audio_hw_device *dev,
struct audio_port *port);
/* Set audio port configuration */
int (*set_audio_port_config)(struct audio_hw_device *dev,
const struct audio_port_config *config);
};
typedef struct audio_hw_device audio_hw_device_t;
看到audio_hw_device_t也可以驗證之前我們在Android開發之HAL層 提到的硬體抽象層模組的每一個硬體裝置都必須自定義一個硬體裝置結構體,且他的第一個成員變數的型別必須是hw_device_t ,且裡面定義了許多其他的函式指標,
接著會以此呼叫其 函式指標 init_check、get_master_volume、get_master_mute、set_master_volume、set_master_mute 並同時賦予hardware_call_state狀態,依次為:AUDIO_HW_INIT、AUDIO_HW_IDLE、AUDIO_HW_GET_MASTER_VOLUME、AUDIO_HW_GET_MASTER_MUTE、AUDIO_HW_SET_MASTER_VOLUME、AUDIO_HW_SET_MASTER_MUTE、AUDIO_HW_IDLE
hardware_call_state定義在: \frameworks\av\services\audioflinger\AudioFlinger.h
// for dump, indicates which hardware operation is currently in progress (but not stream ops)
enum hardware_call_state {
AUDIO_HW_IDLE = 0, // no operation in progress
AUDIO_HW_INIT, // init_check
AUDIO_HW_OUTPUT_OPEN, // open_output_stream
AUDIO_HW_OUTPUT_CLOSE, // unused
AUDIO_HW_INPUT_OPEN, // unused
AUDIO_HW_INPUT_CLOSE, // unused
AUDIO_HW_STANDBY, // unused
AUDIO_HW_SET_MASTER_VOLUME, // set_master_volume
AUDIO_HW_GET_ROUTING, // unused
AUDIO_HW_SET_ROUTING, // unused
AUDIO_HW_GET_MODE, // unused
AUDIO_HW_SET_MODE, // set_mode
AUDIO_HW_GET_MIC_MUTE, // get_mic_mute
AUDIO_HW_SET_MIC_MUTE, // set_mic_mute
AUDIO_HW_SET_VOICE_VOLUME, // set_voice_volume
AUDIO_HW_SET_PARAMETER, // set_parameters
AUDIO_HW_GET_INPUT_BUFFER_SIZE, // get_input_buffer_size
AUDIO_HW_GET_MASTER_VOLUME, // get_master_volume
AUDIO_HW_GET_PARAMETER, // get_parameters
AUDIO_HW_SET_MASTER_MUTE, // set_master_mute
AUDIO_HW_GET_MASTER_MUTE, // get_master_mute
};
並且set_master_volume 成功呼叫返回OK,將flags設定為AHWD_CAN_SET_MASTER_VOLUME,set_master_mute 成功呼叫返回OK,則設定為AHWD_CAN_SET_MASTER_MUTE,如果兩者都成功呼叫OK,則flags設定為AHWD_CAN_SET_MASTER_VOLUME+AHWD_CAN_SET_MASTER_MUTE,作為AudioHwDevice物件的第四個引數。
最後新增到mAudioHwDevs容器,其中一個音訊抽象層動態連線庫就載入完成。