昨天(2011-11-15)釋出了Android4.0的源碼,今天download下來,開始挺進4.0時代。簡單看了一下,發現音頻系統方面與2.3的有較多地方不同,下面逐一描述。
一、代碼子產品位置
1、AudioFlinger
view plain print ?
- frameworks/base/services/audioflinger/
- +-- Android.mk
- +-- AudioBufferProvider.h
- +-- AudioFlinger.cpp
- +-- AudioFlinger.h
- +-- AudioMixer.cpp
- +-- AudioMixer.h
- +-- AudioPolicyService.cpp
- +-- AudioPolicyService.h
- +-- AudioResampler.cpp
- +-- AudioResamplerCubic.cpp
- +-- AudioResamplerCubic.h
- +-- AudioResampler.h
- +-- AudioResamplerSinc.cpp
- +-- AudioResamplerSinc.h
frameworks/base/services/audioflinger/
+--
Android.mk
+-- AudioBufferProvider.h
+-- AudioFlinger.cpp
+-- AudioFlinger.h
+-- AudioMixer.cpp
+-- AudioMixer.h
+-- AudioPolicyService.cpp
+-- AudioPolicyService.h
+-- AudioResampler.cpp
+-- AudioResamplerCubic.cpp
+-- AudioResamplerCubic.h
+-- AudioResampler.h
+-- AudioResamplerSinc.cpp
+-- AudioResamplerSinc.hAudioFlinger相關代碼,好像這部分與2.3相差不大,至少接口是相容的。值得注意的是:2.3位于這裡的還有AudioHardwareGeneric、AudioHardwareInterface、 A2dpAudioInterface等一系列接口代碼,現在都移除了。實際上,這些接口變更為legacy(有另外更好的實作方式,但也相容之前的方法),取而代之的是要實作hardware/libhardware/include/hardware/audio.h提供的接口,這是一個較大的變化。
兩種Audio Hardware HAL接口定義: 1/ legacy:hardware/libhardware_legacy/include/hardware_legacy /AudioHardwareInterface.h 2/ 非legacy:hardware/libhardware/include/hardware/audio.h
2、audio_hw
view plain print ?
- hardware/libhardware_legacy/audio/
- +-- A2dpAudioInterface.cpp
- +-- A2dpAudioInterface.h
- +-- Android.mk
- +-- AudioDumpInterface.cpp
- +-- AudioDumpInterface.h
- +-- AudioHardwareGeneric.cpp
- +-- AudioHardwareGeneric.h
- +-- AudioHardwareInterface.cpp
- +-- AudioHardwareStub.cpp
- +-- AudioHardwareStub.h
- +-- audio_hw_hal.cpp
- +-- AudioPolicyCompatClient.cpp
- +-- AudioPolicyCompatClient.h
- +-- audio_policy_hal.cpp
- +-- AudioPolicyManagerBase.cpp
- +-- AudioPolicyManagerDefault.cpp
- +-- AudioPolicyManagerDefault.h
hardware/libhardware_legacy/audio/
+--
A2dpAudioInterface.cpp
+-- A2dpAudioInterface.h
+-- Android.mk
+-- AudioDumpInterface.cpp
+-- AudioDumpInterface.h
+-- AudioHardwareGeneric.cpp
+-- AudioHardwareGeneric.h
+-- AudioHardwareInterface.cpp
+-- AudioHardwareStub.cpp
+-- AudioHardwareStub.h
+-- audio_hw_hal.cpp
+-- AudioPolicyCompatClient.cpp
+-- AudioPolicyCompatClient.h
+-- audio_policy_hal.cpp
+-- AudioPolicyManagerBase.cpp
+-- AudioPolicyManagerDefault.cpp
+-- AudioPolicyManagerDefault.h上面提及的AudioHardwareGeneric、 AudioHardwareInterface、A2dpAudioInterface等都放到libhardware_legacy裡。 事實上legacy也要封裝成非legacy中的audio.h,确切的說需要一個聯系legacy interface和not legacy interface的中間層,這裡的audio_hw_hal.cpp就充當這樣的一個角色了。是以,我們其實也可以把2.3之前的alsa_sound 這一套東西也搬過來。
view plain print ?
- hardware/libhardware/modules/audio/
- +-- Android.mk
- +-- audio_hw.c
- +-- audio_policy.c
hardware/libhardware/modules/audio/
+--
Android.mk
+-- audio_hw.c
+-- audio_policy.c這是一個stub(類似于2.3中的AudioHardwareStub),大多數函數隻是簡單的傳回一個值,并沒有實際操作,隻是保證Android能得到一個audio hardware hal執行個體,進而啟動運作,當然聲音沒有輸出到外設的。在底層音頻驅動或audio hardware hal還沒有實作好的情況下,可以使用這個stub device,先讓Android跑起來。
view plain print ?
- device/samsung/tuna/audio/
- +-- Android.mk
- +-- audio_hw.c
- +-- ril_interface.c
- +-- ril_interface.h
device/samsung/tuna/audio/
+--
Android.mk
+-- audio_hw.c
+-- ril_interface.c
+-- ril_interface.h這是Samsung Tuna的音頻裝置抽象層,很有參考價值,計劃以後就在它的基礎上進行移植。它調用tinyalsa的接口,可見這個方案的底層音頻驅動是alsa。
3、tinyalsa
view plain print ?
- external/tinyalsa/
- +-- Android.mk
- +-- include
- | +-- tinyalsa
- | +-- asoundlib.h
- +-- mixer.c ##類alsa-lib的control,作用音頻部件開關、音量調節等
- +-- pcm.c ##類alsa-lib的pcm,作用音頻pcm資料回放錄制
- +-- README
- +-- tinycap.c ## 類alsa_arecord
- +-- tinymix.c ##類 alsa_amixer
- +-- tinyplay.c ##類 alsa_aplay
external/tinyalsa/
+-- Android.mk
+-- include
| +-- tinyalsa
| +-- asoundlib.h
+-- mixer.c ##類alsa-lib的control,作用音頻部件開關、音量調節等
+-- pcm.c ##類alsa-lib的pcm,作用音頻pcm資料回放錄制
+-- README
+-- tinycap.c ##類alsa_arecord
+-- tinymix.c ##類alsa_amixer
+-- tinyplay.c ##類alsa_aplay在2.3時代,Android還隐晦把它放在 android2.3.1-gingerbread/device/samsung/crespo/libaudio,現在終于把alsa-lib一腳踢開,小三變正室了,正名tinyalsa。 這其實是曆史的必然了,alsa-lib太過複雜繁瑣了,我看得也很不爽;更重要的商業上面的考慮,必須移除被GNU GPL授權證所限制的部份,alsa-lib并不是個例。
注意:上面的hardware/libhardware_legacy/audio/、hardware/libhardware/modules /audio/、device/samsung/tuna/audio/是同層的。之一是legacy audio,用于相容2.2時代的alsa_sound;之二是stub audio接口;之三是Samsung Tuna的音頻抽象層實作。調用層次:AudioFlinger -> audio_hw -> tinyalsa。
二、Audio Hardware HAL加載
1、AudioFlinger
view plain print ?
- // 加載audio hardware hal
- static int load_audio_interface(const char *if_name, const hw_module_t **mod,
- audio_hw_device_t **dev)
- {
- int rc;
- //根據classid和if_name找到指定的動态庫并加載,這裡加載的是音頻動态庫,如libaudio.primary.tuna.so
- rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod);
- if (rc)
- goto out;
- //加載好的動态庫子產品必有個open方法,調用open方法打開音頻裝置子產品
- rc = audio_hw_device_open(*mod, dev);
- LOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)",
- AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
- if (rc)
- goto out;
- return 0;
- out:
- *mod = NULL;
- *dev = NULL;
- return rc;
- }
- //音頻裝置接口,hw_get_module_by_class需要根據這些字元串找到相關的音頻子產品庫
- static const char *audio_interfaces[] = {
- "primary", //主音頻裝置,一般為本機codec
- "a2dp", //a2dp裝置,藍牙高保真音頻
- "usb", //usb-audio裝置,這個東東我2.3就考慮要實作了,現在終于支援了
- };
- #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
- // ----------------------------------------------------------------------------
- AudioFlinger::AudioFlinger()
- : BnAudioFlinger(),
- mPrimaryHardwareDev(0), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1),
- mBtNrecIsOff(false)
- {
- }
- void AudioFlinger::onFirstRef()
- {
- int rc = 0;
- Mutex::Autolock _l(mLock);
- mHardwareStatus = AUDIO_HW_IDLE;
- //打開audio_interfaces數組定義的所有音頻裝置
- for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
- const hw_module_t *mod;
- audio_hw_device_t *dev;
- rc = load_audio_interface(audio_interfaces[i], &mod, &dev);
- if (rc)
- continue;
- LOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i],
- mod->name, mod->id);
- mAudioHwDevs.push(dev); //mAudioHwDevs 是一個Vector,存儲已打開的audio hw devices
- if (!mPrimaryHardwareDev) {
- mPrimaryHardwareDev = dev;
- LOGI("Using '%s' (%s.%s) as the primary audio interface",
- mod->name, mod->id, audio_interfaces[i]);
- }
- }
- mHardwareStatus = AUDIO_HW_INIT;
- if (!mPrimaryHardwareDev || mAudioHwDevs.size() == 0) {
- LOGE("Primary audio interface not found");
- return;
- }
- //對audio hw devices進行一些初始化,如 mode、master volume的設定
- for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
- audio_hw_device_t *dev = mAudioHwDevs[i];
- mHardwareStatus = AUDIO_HW_INIT;
- rc = dev->init_check(dev);
- if (rc == 0) {
- AutoMutex lock(mHardwareLock);
- mMode = AUDIO_MODE_NORMAL;
- mHardwareStatus = AUDIO_HW_SET_MODE;
- dev->set_mode(dev, mMode);
- mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
- dev->set_master_volume(dev, 1.0f);
- mHardwareStatus = AUDIO_HW_IDLE;
- }
- }
- }
//加載audio hardware
hal
static int load_audio_interface(const char *if_name, const hw_module_t
**mod,
audio_hw_device_t **dev)
{
int rc;
//根據classid和if_name找到指定的動态庫并加載,這裡加載的是音頻動态庫,如libaudio.primary.tuna.so
rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod);
if (rc)
goto out;
//加載好的動态庫子產品必有個open方法,調用open方法打開音頻裝置子產品
rc = audio_hw_device_open(*mod, dev);
LOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)",
AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
if (rc)
goto out;
return 0;
out:
*mod = NULL;
*dev = NULL;
return rc;
}
//音頻裝置接口,hw_get_module_by_class需要根據這些字元串找到相關的音頻子產品庫
static const char *audio_interfaces[] = {
"primary", //主音頻裝置,一般為本機codec
"a2dp", //a2dp裝置,藍牙高保真音頻
"usb", //usb-audio裝置,這個東東我2.3就考慮要實作了,現在終于支援了
};
#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
//
----------------------------------------------------------------------------
AudioFlinger::AudioFlinger()
: BnAudioFlinger(),
mPrimaryHardwareDev(0), mMasterVolume(1.0f), mMasterMute(false),
mNextUniqueId(1),
mBtNrecIsOff(false)
{
}
void AudioFlinger::onFirstRef()
{
int rc = 0;
Mutex::Autolock _l(mLock);
/* TODO: move all this work into an Init() function */
mHardwareStatus = AUDIO_HW_IDLE;
//打開audio_interfaces數組定義的所有音頻裝置
for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
const hw_module_t *mod;
audio_hw_device_t *dev;
rc = load_audio_interface(audio_interfaces[i], &mod,
&dev);
if (rc)
continue;
LOGI("Loaded %s audio interface from %s (%s)",
audio_interfaces[i],
mod->name, mod->id);
mAudioHwDevs.push(dev); //mAudioHwDevs是一個Vector,存儲已打開的audio hw
devices
if (!mPrimaryHardwareDev) {
mPrimaryHardwareDev = dev;
LOGI("Using '%s' (%s.%s) as the primary audio interface",
mod->name, mod->id, audio_interfaces[i]);
}
}
mHardwareStatus = AUDIO_HW_INIT;
if (!mPrimaryHardwareDev || mAudioHwDevs.size() == 0) {
LOGE("Primary audio interface not found");
return;
}
//對audio hw devices進行一些初始化,如mode、master volume的設定
for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
audio_hw_device_t *dev = mAudioHwDevs[i];
mHardwareStatus = AUDIO_HW_INIT;
rc = dev->init_check(dev);
if (rc == 0) {
AutoMutex lock(mHardwareLock);
mMode = AUDIO_MODE_NORMAL;
mHardwareStatus = AUDIO_HW_SET_MODE;
dev->set_mode(dev, mMode);
mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
dev->set_master_volume(dev, 1.0f);
mHardwareStatus = AUDIO_HW_IDLE;
}
}
}
以上對AudioFlinger進行的分析,主要是通過hw_get_module_by_class()找到子產品接口名字if_name相比對的子產品庫,加載,然後audio_hw_device_open()調用子產品的open方法,完成音頻裝置子產品的初始化。
留意AudioFlinger的構造函數隻有簡單的私有變量的初始化操作了,把音頻裝置初始化放到onFirstRef(),Android終于改進了這一點,好的設計根本不應該把可能會失敗的操作放到構造函數中。onFirstRef是RefBase類的一個虛函數,在構造sp的時候就會被調用。是以,在構造sp<AudioFlinger>的時候就會觸發onFirstRef方法,進而完成音頻裝置子產品初始化。
2、hw_get_module_by_class
我們接下來看看hw_get_module_by_class,實作在hardware/libhardware/ hardware.c中,它作用加載指定名字的子產品庫(.so檔案),這個應該是用于加載所有硬體裝置相關的庫檔案,并不隻是音頻裝置。
view plain print ?
- int hw_get_module_by_class(const char *class_id, const char *inst,
- const struct hw_module_t **module)
- {
- int status;
- int i;
- const struct hw_module_t *hmi = NULL;
- char prop[PATH_MAX];
- char path[PATH_MAX];
- char name[PATH_MAX];
- if (inst)
- snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
- else
- strlcpy(name, class_id, PATH_MAX);
- //這裡我們以音頻庫為例,AudioFlinger調用到這個函數時,
- //class_id=AUDIO_HARDWARE_MODULE_ID="audio",inst="primary" (或"a2dp"或"usb")
- //那麼此時name="audio.primary"
- for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
- if (i < HAL_VARIANT_KEYS_COUNT) {
- //通過property_get找到廠家标記如"ro.product.board=tuna",這時prop="tuna"
- if (property_get(variant_keys[i], prop, NULL) == 0) {
- continue;
- }
- snprintf(path, sizeof(path), "%s/%s.%s.so",
- HAL_LIBRARY_PATH2, name, prop); //#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
- if (access(path, R_OK) == 0) break;
- snprintf(path, sizeof(path), "%s/%s.%s.so",
- HAL_LIBRARY_PATH1, name, prop); //#define HAL_LIBRARY_PATH1 "/system/lib/hw"
- if (access(path, R_OK) == 0) break;
- } else {
- snprintf(path, sizeof(path), "%s/%s.default.so", //如沒有指定的庫檔案,則加載default.so,即stub-device
- HAL_LIBRARY_PATH1, name);
- if (access(path, R_OK) == 0) break;
- }
- }
- //到這裡,完成一個子產品庫的完整路徑名稱,如path="/system/lib/hw /audio.primary.tuna.so"
- //如何生成audio.primary.tuna.so?請看相關的Android.mk檔案,其中有定義 LOCAL_MODULE := audio.primary.tuna
- status = -ENOENT;
- if (i < HAL_VARIANT_KEYS_COUNT+1) {
- status = load(class_id, path, module); //加載子產品庫
- }
- return status;
- }
int
hw_get_module_by_class(const char *class_id, const char *inst,
const struct hw_module_t **module)
{
int status;
int i;
const struct hw_module_t *hmi = NULL;
char prop[PATH_MAX];
char path[PATH_MAX];
char name[PATH_MAX];
if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
else
strlcpy(name, class_id, PATH_MAX);
//這裡我們以音頻庫為例,AudioFlinger調用到這個函數時,
//class_id=AUDIO_HARDWARE_MODULE_ID="audio",inst="primary"(或"a2dp"
或"usb")
//那麼此時name="audio.primary"
/*
* Here we rely on the fact that calling dlopen multiple times on
* the same .so will simply increment a refcount (and not load
* a new copy of the library).
* We also assume that dlopen() is thread-safe.
*/
/* Loop through the configuration variants looking for a module */
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
if (i < HAL_VARIANT_KEYS_COUNT) {
//通過property_get找到廠家标記如"ro.product.board=tuna",這時prop="tuna"
if (property_get(variant_keys[i], prop, NULL) == 0) {
continue;
}
snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH2, name, prop); //#define
HAL_LIBRARY_PATH2 "/vendor/lib/hw"
if (access(path, R_OK) == 0) break;
snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, prop); //#define
HAL_LIBRARY_PATH1 "/system/lib/hw"
if (access(path, R_OK) == 0) break;
} else {
snprintf(path, sizeof(path), "%s/%s.default.so",
//如沒有指定的庫檔案,則加載default.so,即stub-device
HAL_LIBRARY_PATH1, name);
if (access(path, R_OK) == 0) break;
}
}
//到這裡,完成一個子產品庫的完整路徑名稱,如path="/system/lib/hw/audio.primary.tuna.so"
//如何生成audio.primary.tuna.so?請看相關的Android.mk檔案,其中有定義LOCAL_MODULE :=
audio.primary.tuna
status = -ENOENT;
if (i < HAL_VARIANT_KEYS_COUNT+1) {
/* load the module, if this fails, we're doomed, and we should
not try
* to load a different variant. */
status = load(class_id, path, module); //加載子產品庫
}
return status;
}
load()函數不詳細分析了,它通過dlopen加載庫檔案,然後dlsym找到hal_module_info的首位址。我們先看看 hal_module_info的定義: view plain print ?
- typedef struct hw_module_t {
- uint32_t tag;
- uint16_t version_major;
- uint16_t version_minor;
- const char *id;
- const char *name;
- const char *author;
- struct hw_module_methods_t* methods;
- void* dso;
- uint32_t reserved[32-7];
- } hw_module_t;
- typedef struct hw_module_methods_t {
- int (*open)(const struct hw_module_t* module, const char* id,
- struct hw_device_t** device);
- } hw_module_methods_t;
/**
* Every hardware module must have a data structure named
HAL_MODULE_INFO_SYM
* and the fields of this data structure must begin with hw_module_t
* followed by module specific information.
*/
typedef struct hw_module_t {
/** tag must be initialized to HARDWARE_MODULE_TAG */
uint32_t tag;
/** major version number for the module */
uint16_t version_major;
/** minor version number of the module */
uint16_t version_minor;
/** Identifier of module */
const char *id;
/** Name of this module */
const char *name;
/** Author/owner/implementor of the module */
const char *author;
/** Modules methods */
struct hw_module_methods_t* methods;
/** module's dso */
void* dso;
/** padding to 128 bytes, reserved for future use */
uint32_t reserved[32-7];
} hw_module_t;
typedef struct hw_module_methods_t {
/** Open a specific device */
int (*open)(const struct hw_module_t* module, const char* id,
struct hw_device_t** device);
} hw_module_methods_t;這個結構體很重要,注釋很詳細。dlsym拿到這個結構體的首位址後,就可以調用 Modules methods進行裝置子產品的初始化了。裝置子產品中,都應該按照這個格式初始化好這個結構體,否則dlsym找不到它,也就無法調用Modules methods進行初始化了。
例如,在audio_hw.c中,它是這樣定義的: view plain print ?
- static struct hw_module_methods_t hal_module_methods = {
- .open = adev_open,
- };
- struct audio_module HAL_MODULE_INFO_SYM = {
- .common = {
- .tag = HARDWARE_MODULE_TAG,
- .version_major = 1,
- .version_minor = 0,
- .id = AUDIO_HARDWARE_MODULE_ID,
- .name = "Tuna audio HW HAL",
- .author = "The Android Open Source Project",
- .methods = &hal_module_methods,
- },
- };
static
struct hw_module_methods_t hal_module_methods = {
.open = adev_open,
};
struct audio_module HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = AUDIO_HARDWARE_MODULE_ID,
.name = "Tuna audio HW HAL",
.author = "The Android Open Source Project",
.methods = &hal_module_methods,
},
};
3、audio_hw
好了,經過一番周折,又dlopen又dlsym的,終于進入我們的audio_hw。這部分沒什麼好說的,按照 hardware/libhardware/include/hardware/audio.h定義的接口實作就行了。這些接口全扔到一個結構體裡面的,這樣做的好處是:不必用大量的dlsym來擷取各個接口函數的位址,隻需找到這個結構體即可,從易用性和可擴充性來說,都是首選方式。
接口定義如下:
view plain print ?
- struct audio_hw_device {
- struct hw_device_t common;
- uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
- int (*init_check)(const struct audio_hw_device *dev);
- int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
- int (*set_master_volume)(struct audio_hw_device *dev, float volume);
- int (*set_mode)(struct audio_hw_device *dev, int mode);
- int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
- int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
- int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
- char * (*get_parameters)(const struct audio_hw_device *dev,
- const char *keys);
- size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
- uint32_t sample_rate, int format,
- int channel_count);
- int (*open_output_stream)(struct audio_hw_device *dev, uint32_t devices,
- int *format, uint32_t *channels,
- uint32_t *sample_rate,
- struct audio_stream_out **out);
- void (*close_output_stream)(struct audio_hw_device *dev,
- struct audio_stream_out* out);
- int (*open_input_stream)(struct audio_hw_device *dev, uint32_t devices,
- int *format, uint32_t *channels,
- uint32_t *sample_rate,
- audio_in_acoustics_t acoustics,
- struct audio_stream_in **stream_in);
- void (*close_input_stream)(struct audio_hw_device *dev,
- struct audio_stream_in *in);
- int (*dump)(const struct audio_hw_device *dev, int fd);
- };
- typedef struct audio_hw_device audio_hw_device_t;
struct
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
*/
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);
/**
* setMode 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, int 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.
*/
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
*/
size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
uint32_t sample_rate, int format,
int channel_count);
/** This method creates and opens the audio hardware output stream
*/
int (*open_output_stream)(struct audio_hw_device *dev, uint32_t
devices,
int *format, uint32_t *channels,
uint32_t *sample_rate,
struct audio_stream_out **out);
void (*close_output_stream)(struct audio_hw_device *dev,
struct audio_stream_out* out);
/** This method creates and opens the audio hardware input stream */
int (*open_input_stream)(struct audio_hw_device *dev, uint32_t
devices,
int *format, uint32_t *channels,
uint32_t *sample_rate,
audio_in_acoustics_t acoustics,
struct audio_stream_in **stream_in);
void (*close_input_stream)(struct audio_hw_device *dev,
struct audio_stream_in *in);
/** This method dumps the state of the audio hardware */
int (*dump)(const struct audio_hw_device *dev, int fd);
};
typedef struct audio_hw_device audio_hw_device_t;
注:這是比較标準的C接口設計方法了,但是個人感覺還是用C++比較好,直覺易讀。2.3之前都是用C++實作這些接口設計的,到了4.0,不知道為何采納用C?不會理由是做底層的不懂C++吧?!
三、Audio Hardware HAL的legacy實作
之前提到兩種Audio Hardware HAL接口定義:
1/ legacy:hardware/libhardware_legacy/include/hardware_legacy /AudioHardwareInterface.h
2/ 非legacy:hardware/libhardware/include/hardware/audio.h
前者是2.3及之前的音頻裝置接口定義,後者是4.0的接口定義。
為了相容以前的設計,4.0實作一個中間層:hardware/libhardware_legacy/audio/audio_hw_hal.cpp,結構與其他的audio_hw.c大同小異,差别在于open方法:
view plain print ?
- static int legacy_adev_open(const hw_module_t* module, const char* name,
- hw_device_t** device)
- {
- ......
- ladev->hwif = createAudioHardware();
- if (!ladev->hwif) {
- ret = -EIO;
- goto err_create_audio_hw;
- }
- ......
- }
static
int legacy_adev_open(const hw_module_t* module, const char* name,
hw_device_t** device)
{
......
ladev->hwif = createAudioHardware();
if (!ladev->hwif) {
ret = -EIO;
goto err_create_audio_hw;
}
......
}看到那個熟悉的createAudioHardware()沒有?這是以前我提到的Vendor Specific Audio接口,然後新的接口再調用ladev->hwif的函數就是了。 是以老一套的alsa-lib、alsa-utils和alsa_sound也可以照搬過來,這裡的檔案被編譯成靜态庫的,是以你需要修改 alsa_sound裡面的Android.mk檔案,連結這個靜态庫。還有alsa_sound的命名空間原來是“android”,現在需要改成 “android_audio_legacy”。
四、a2dp Audio HAL的實作
4.0的a2dp audio hal放到bluez裡實作了,我找了好一會才找到:
external/Bluetooth/bluez/audio/android_audio_hw.c
大緻與上面提到的audio_hw.c類似,因為都是基于audio.h定義的接口來實作的。
如果需要編譯這個庫,須在BoardConfig.mk裡定義:
BOARD_HAVE_BLUETOOTH := true
開始還提到現在支援3種audio裝置了,分别是primary、a2dp和usb。目前剩下usb audio hal我沒有找到,不知是否需要自己去實作?其實alsa-driver都支援大部分的usb-audio裝置了,是以上層也可調用tinyalsa的接口,就像samsung tuna的audio_hw.c那樣。
五、音質改進???
可使用audio echo cancel和更好的resampler(SRC)???