天天看點

ANDROID音頻系統散記之四:4.0音頻系統HAL初探

昨天(2011-11-15)釋出了Android4.0的源碼,今天download下來,開始挺進4.0時代。簡單看了一下,發現音頻系統方面與2.3的有較多地方不同,下面逐一描述。

一、代碼子產品位置

1、AudioFlinger

view plain print ?

  1. frameworks/base/services/audioflinger/  
  2. +-- Android.mk  
  3. +-- AudioBufferProvider.h  
  4. +-- AudioFlinger.cpp  
  5. +-- AudioFlinger.h  
  6. +-- AudioMixer.cpp  
  7. +-- AudioMixer.h  
  8. +-- AudioPolicyService.cpp  
  9. +-- AudioPolicyService.h  
  10. +-- AudioResampler.cpp  
  11. +-- AudioResamplerCubic.cpp  
  12. +-- AudioResamplerCubic.h  
  13. +-- AudioResampler.h  
  14. +-- AudioResamplerSinc.cpp  
  15. +-- 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 ?

  1. hardware/libhardware_legacy/audio/  
  2. +-- A2dpAudioInterface.cpp  
  3. +-- A2dpAudioInterface.h  
  4. +-- Android.mk  
  5. +-- AudioDumpInterface.cpp  
  6. +-- AudioDumpInterface.h  
  7. +-- AudioHardwareGeneric.cpp  
  8. +-- AudioHardwareGeneric.h  
  9. +-- AudioHardwareInterface.cpp  
  10. +-- AudioHardwareStub.cpp  
  11. +-- AudioHardwareStub.h  
  12. +-- audio_hw_hal.cpp  
  13. +-- AudioPolicyCompatClient.cpp  
  14. +-- AudioPolicyCompatClient.h  
  15. +-- audio_policy_hal.cpp  
  16. +-- AudioPolicyManagerBase.cpp  
  17. +-- AudioPolicyManagerDefault.cpp  
  18. +-- 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 ?

  1. hardware/libhardware/modules/audio/  
  2. +-- Android.mk  
  3. +-- audio_hw.c  
  4. +-- 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 ?

  1. device/samsung/tuna/audio/  
  2. +-- Android.mk  
  3. +-- audio_hw.c  
  4. +-- ril_interface.c  
  5. +-- 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 ?

  1. external/tinyalsa/  
  2. +-- Android.mk  
  3. +-- include  
  4. |   +-- tinyalsa  
  5. |       +-- asoundlib.h  
  6. +-- mixer.c      ##類alsa-lib的control,作用音頻部件開關、音量調節等  
  7. +-- pcm.c        ##類alsa-lib的pcm,作用音頻pcm資料回放錄制  
  8. +-- README  
  9. +-- tinycap.c    ## 類alsa_arecord  
  10. +-- tinymix.c    ##類 alsa_amixer  
  11. +-- 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 ?

  1. // 加載audio hardware hal  
  2. static int load_audio_interface(const char *if_name, const hw_module_t **mod,  
  3.                                 audio_hw_device_t **dev)  
  4. {  
  5.     int rc;  
  6.     //根據classid和if_name找到指定的動态庫并加載,這裡加載的是音頻動态庫,如libaudio.primary.tuna.so  
  7.     rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod);  
  8.     if (rc)  
  9.         goto out;  
  10.     //加載好的動态庫子產品必有個open方法,調用open方法打開音頻裝置子產品  
  11.     rc = audio_hw_device_open(*mod, dev);  
  12.     LOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)",  
  13.             AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));  
  14.     if (rc)  
  15.         goto out;  
  16.     return 0;  
  17. out:  
  18.     *mod = NULL;  
  19.     *dev = NULL;  
  20.     return rc;  
  21. }  
  22. //音頻裝置接口,hw_get_module_by_class需要根據這些字元串找到相關的音頻子產品庫  
  23. static const char *audio_interfaces[] = {  
  24.     "primary", //主音頻裝置,一般為本機codec  
  25.     "a2dp",    //a2dp裝置,藍牙高保真音頻  
  26.     "usb",     //usb-audio裝置,這個東東我2.3就考慮要實作了,現在終于支援了  
  27. };  
  28. #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))  
  29. // ----------------------------------------------------------------------------  
  30. AudioFlinger::AudioFlinger()  
  31.     : BnAudioFlinger(),  
  32.         mPrimaryHardwareDev(0), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1),  
  33.         mBtNrecIsOff(false)  
  34. {  
  35. }  
  36. void AudioFlinger::onFirstRef()  
  37. {  
  38.     int rc = 0;  
  39.     Mutex::Autolock _l(mLock);  
  40.     mHardwareStatus = AUDIO_HW_IDLE;  
  41.     //打開audio_interfaces數組定義的所有音頻裝置  
  42.     for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {  
  43.         const hw_module_t *mod;  
  44.         audio_hw_device_t *dev;  
  45.         rc = load_audio_interface(audio_interfaces[i], &mod, &dev);  
  46.         if (rc)  
  47.             continue;  
  48.         LOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i],  
  49.              mod->name, mod->id);  
  50.         mAudioHwDevs.push(dev); //mAudioHwDevs 是一個Vector,存儲已打開的audio hw devices  
  51.         if (!mPrimaryHardwareDev) {  
  52.             mPrimaryHardwareDev = dev;  
  53.             LOGI("Using '%s' (%s.%s) as the primary audio interface",  
  54.                  mod->name, mod->id, audio_interfaces[i]);  
  55.         }  
  56.     }  
  57.     mHardwareStatus = AUDIO_HW_INIT;  
  58.     if (!mPrimaryHardwareDev || mAudioHwDevs.size() == 0) {  
  59.         LOGE("Primary audio interface not found");  
  60.         return;  
  61.     }  
  62.     //對audio hw devices進行一些初始化,如 mode、master volume的設定  
  63.     for (size_t i = 0; i < mAudioHwDevs.size(); i++) {  
  64.         audio_hw_device_t *dev = mAudioHwDevs[i];  
  65.         mHardwareStatus = AUDIO_HW_INIT;  
  66.         rc = dev->init_check(dev);  
  67.         if (rc == 0) {  
  68.             AutoMutex lock(mHardwareLock);  
  69.             mMode = AUDIO_MODE_NORMAL;  
  70.             mHardwareStatus = AUDIO_HW_SET_MODE;  
  71.             dev->set_mode(dev, mMode);  
  72.             mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;  
  73.             dev->set_master_volume(dev, 1.0f);  
  74.             mHardwareStatus = AUDIO_HW_IDLE;  
  75.         }  
  76.     }  
  77. }  

//加載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 ?

  1. int hw_get_module_by_class(const char *class_id, const char *inst,  
  2.                            const struct hw_module_t **module)  
  3. {  
  4.     int status;  
  5.     int i;  
  6.     const struct hw_module_t *hmi = NULL;  
  7.     char prop[PATH_MAX];  
  8.     char path[PATH_MAX];  
  9.     char name[PATH_MAX];  
  10.     if (inst)  
  11.         snprintf(name, PATH_MAX, "%s.%s", class_id, inst);  
  12.     else  
  13.         strlcpy(name, class_id, PATH_MAX);  
  14.     //這裡我們以音頻庫為例,AudioFlinger調用到這個函數時,  
  15.     //class_id=AUDIO_HARDWARE_MODULE_ID="audio",inst="primary" (或"a2dp"或"usb")  
  16.     //那麼此時name="audio.primary"  
  17.     for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {  
  18.         if (i < HAL_VARIANT_KEYS_COUNT) {  
  19.               //通過property_get找到廠家标記如"ro.product.board=tuna",這時prop="tuna"  
  20.             if (property_get(variant_keys[i], prop, NULL) == 0) {  
  21.                 continue;  
  22.             }  
  23.             snprintf(path, sizeof(path), "%s/%s.%s.so",  
  24.                      HAL_LIBRARY_PATH2, name, prop); //#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"  
  25.             if (access(path, R_OK) == 0) break;  
  26.             snprintf(path, sizeof(path), "%s/%s.%s.so",  
  27.                      HAL_LIBRARY_PATH1, name, prop); //#define HAL_LIBRARY_PATH1 "/system/lib/hw"  
  28.             if (access(path, R_OK) == 0) break;  
  29.         } else {  
  30.             snprintf(path, sizeof(path), "%s/%s.default.so", //如沒有指定的庫檔案,則加載default.so,即stub-device  
  31.                      HAL_LIBRARY_PATH1, name);  
  32.             if (access(path, R_OK) == 0) break;  
  33.         }  
  34.     }  
  35.     //到這裡,完成一個子產品庫的完整路徑名稱,如path="/system/lib/hw /audio.primary.tuna.so"  
  36.     //如何生成audio.primary.tuna.so?請看相關的Android.mk檔案,其中有定義 LOCAL_MODULE := audio.primary.tuna  
  37.     status = -ENOENT;  
  38.     if (i < HAL_VARIANT_KEYS_COUNT+1) {  
  39.         status = load(class_id, path, module); //加載子產品庫  
  40.     }  
  41.     return status;  
  42. }  

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 ?

  1. typedef struct hw_module_t {  
  2.     uint32_t tag;  
  3.     uint16_t version_major;  
  4.     uint16_t version_minor;  
  5.     const char *id;  
  6.     const char *name;  
  7.     const char *author;  
  8.     struct hw_module_methods_t* methods;  
  9.     void* dso;  
  10.     uint32_t reserved[32-7];  
  11. } hw_module_t;  
  12. typedef struct hw_module_methods_t {  
  13.     int (*open)(const struct hw_module_t* module, const char* id,  
  14.             struct hw_device_t** device);  
  15. } 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 ?

  1. static struct hw_module_methods_t hal_module_methods = {  
  2.     .open = adev_open,  
  3. };  
  4. struct audio_module HAL_MODULE_INFO_SYM = {  
  5.     .common = {  
  6.         .tag = HARDWARE_MODULE_TAG,  
  7.         .version_major = 1,  
  8.         .version_minor = 0,  
  9.         .id = AUDIO_HARDWARE_MODULE_ID,  
  10.         .name = "Tuna audio HW HAL",  
  11.         .author = "The Android Open Source Project",  
  12.         .methods = &hal_module_methods,  
  13.     },  
  14. };  

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 ?

  1. struct audio_hw_device {  
  2.     struct hw_device_t common;  
  3.     uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);  
  4.     int (*init_check)(const struct audio_hw_device *dev);  
  5.     int (*set_voice_volume)(struct audio_hw_device *dev, float volume);  
  6.     int (*set_master_volume)(struct audio_hw_device *dev, float volume);  
  7.     int (*set_mode)(struct audio_hw_device *dev, int mode);  
  8.     int (*set_mic_mute)(struct audio_hw_device *dev, bool state);  
  9.     int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);  
  10.     int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);  
  11.     char * (*get_parameters)(const struct audio_hw_device *dev,  
  12.                              const char *keys);  
  13.     size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,  
  14.                                     uint32_t sample_rate, int format,  
  15.                                     int channel_count);  
  16.     int (*open_output_stream)(struct audio_hw_device *dev, uint32_t devices,  
  17.                               int *format, uint32_t *channels,  
  18.                               uint32_t *sample_rate,  
  19.                               struct audio_stream_out **out);  
  20.     void (*close_output_stream)(struct audio_hw_device *dev,  
  21.                                 struct audio_stream_out* out);  
  22.     int (*open_input_stream)(struct audio_hw_device *dev, uint32_t devices,  
  23.                              int *format, uint32_t *channels,  
  24.                              uint32_t *sample_rate,  
  25.                              audio_in_acoustics_t acoustics,  
  26.                              struct audio_stream_in **stream_in);  
  27.     void (*close_input_stream)(struct audio_hw_device *dev,  
  28.                                struct audio_stream_in *in);  
  29.     int (*dump)(const struct audio_hw_device *dev, int fd);  
  30. };  
  31. 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 ?

  1. static int legacy_adev_open(const hw_module_t* module, const char* name,  
  2.                             hw_device_t** device)  
  3. {  
  4.     ......  
  5.     ladev->hwif = createAudioHardware();  
  6.     if (!ladev->hwif) {  
  7.         ret = -EIO;  
  8.         goto err_create_audio_hw;  
  9.     }  
  10.     ......  
  11. }  

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)???