这里主要讲简练地讲了Serivce与HAL的接口关系,两个重要的数据结构,主要是android4.4,不同方案可以有一些不一样,5.1也有些不同。AudioPolicyManager不在HAL层,直接移到framework下面了。
hw_module_t (有通过methods->open成员,这里顺便再讲下hw_methods_t)
hw_device_t (有直接close成员)
这两个成员在hardware.h中,有必要记住成员关系
struct hw_module_t;
struct hw_module_methods_t;
struct hw_device_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;
/**
* The API version of the implemented module. The module owner is
* responsible for updating the version when a module interface has
* changed.
*
* The derived modules such as gralloc and audio own and manage this field.
* The module user must interpret the version field to decide whether or
* not to inter-operate with the supplied module implementation.
* For example, SurfaceFlinger is responsible for making sure that
* it knows how to manage different versions of the gralloc-module API,
* and AudioFlinger must know how to do the same for audio-module API.
*
* The module API version should include a major and a minor component.
* For example, version 1.0 could be represented as 0x0100. This format
* implies that versions 0x0100-0x01ff are all API-compatible.
*
* In the future, libhardware will expose a hw_get_module_version()
* (or equivalent) function that will take minimum/maximum supported
* versions as arguments and would be able to reject modules with
* versions outside of the supplied range.
*/
uint16_t module_api_version;
#define version_major module_api_version
/**
* version_major/version_minor defines are supplied here for temporary
* source code compatibility. They will be removed in the next version.
* ALL clients must convert to the new version format.
*/
/**
* The API version of the HAL module interface. This is meant to
* version the hw_module_t, hw_module_methods_t, and hw_device_t
* structures and definitions.
*
* The HAL interface owns this field. Module users/implementations
* must NOT rely on this value for version information.
*
* Presently, 0 is the only valid value.
*/
uint16_t hal_api_version;
#define version_minor hal_api_version
/** 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); //这个open方法把module和device关联起来了,其它的方法都是与device相关的。
} **hw_module_methods_t**;
/**
* Every device data structure must begin with hw_device_t
* followed by module specific public methods and attributes.
*/
typedef struct **hw_device_t** {
/** tag must be initialized to HARDWARE_DEVICE_TAG */
uint32_t tag;
/**
* Version of the module-specific device API. This value is used by
* the derived-module user to manage different device implementations.
*
* The module user is responsible for checking the module_api_version
* and device version fields to ensure that the user is capable of
* communicating with the specific module implementation.
*
* One module can support multiple devices with different versions. This
* can be useful when a device interface changes in an incompatible way
* but it is still necessary to support older implementations at the same
* time. One such example is the Camera 2.0 API.
*
* This field is interpreted by the module user and is ignored by the
* HAL interface itself.
*/
uint32_t version;
/** reference to the module this device belongs to */
struct hw_module_t* module;
/** padding reserved for future use */
uint32_t reserved[12];
/** Close this device */
int (*close)(struct hw_device_t* device);
} **hw_device_t**;
在4.4中,AudioFlingerService的构造函数(framework/av/service/audioflinger/AudioFlingerService.cpp)
1.1,
hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
先通过hw_get_module这个方法,拿到hal层的module
2.2,
audio_policy_dev_open(module, &mpAudioPolicyDev);
这里对应到hal层hw_module_t的open方法
audio_policy.h
440 static inline int audio_policy_dev_open(const hw_module_t* module,
441 struct audio_policy_device** device)
442 {
443 return module->methods->open(module, AUDIO_POLICY_INTERFACE,
444 (hw_device_t**)device);
445 }
这个open就是对应audio_polic.c的open
310 static int default_ap_dev_open(const hw_module_t* module, const char* name,
311 hw_device_t** device)
312 {
313 struct default_ap_device *dev;
314
315 *device = NULL;
316
317 if (strcmp(name, AUDIO_POLICY_INTERFACE) != 0)
318 return -EINVAL;
319
320 dev = (struct default_ap_device *)calloc(1, sizeof(*dev));
321 if (!dev)
322 return -ENOMEM;
323
324 dev->device.common.tag = HARDWARE_DEVICE_TAG;
325 dev->device.common.version = 0;
326 dev->device.common.module = (hw_module_t *)module;
327 dev->device.common.close = default_ap_dev_close;
328 dev->device.create_audio_policy = create_default_ap;
329 dev->device.destroy_audio_policy = destroy_default_ap;
330
331 *device = &dev->device.common;
332
333 return 0;
334 }
335
336 static struct hw_module_methods_t default_ap_module_methods = {
337 .open = default_ap_dev_open,
338 };
这个create就带出一大串方法,这些方法就供给上层调用,让hal自己来实现。
1.3
rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
&mpAudioPolicy);
这个create_audio_policy就是4.2指的default_ap_dev_open,来看这个函数的实现。
static int create_default_ap(const struct audio_policy_device *device,
struct audio_policy_service_ops *aps_ops,
void *service,
struct audio_policy **ap)
{
struct default_ap_device *dev;
struct default_audio_policy *dap;
int ret;
*ap = NULL;
if (!service || !aps_ops)
return -EINVAL;
dap = (struct default_audio_policy *)calloc(1, sizeof(*dap));
if (!dap)
return -ENOMEM;
dap->policy.set_device_connection_state = ap_set_device_connection_state;
dap->policy.get_device_connection_state = ap_get_device_connection_state;
dap->policy.set_phone_state = ap_set_phone_state;
dap->policy.set_ringer_mode = ap_set_ringer_mode;
dap->policy.set_force_use = ap_set_force_use;
dap->policy.get_force_use = ap_get_force_use;
dap->policy.set_can_mute_enforced_audible =
ap_set_can_mute_enforced_audible;
dap->policy.init_check = ap_init_check;
dap->policy.get_output = ap_get_output;
dap->policy.start_output = ap_start_output;
dap->policy.stop_output = ap_stop_output;
dap->policy.release_output = ap_release_output;
dap->policy.get_input = ap_get_input;
dap->policy.start_input = ap_start_input;
dap->policy.stop_input = ap_stop_input;
dap->policy.release_input = ap_release_input;
dap->policy.init_stream_volume = ap_init_stream_volume;
dap->policy.set_stream_volume_index = ap_set_stream_volume_index;
dap->policy.get_stream_volume_index = ap_get_stream_volume_index;
dap->policy.set_stream_volume_index_for_device = ap_set_stream_volume_index_for_device;
dap->policy.get_stream_volume_index_for_device = ap_get_stream_volume_index_for_device;
dap->policy.get_strategy_for_stream = ap_get_strategy_for_stream;
dap->policy.get_devices_for_stream = ap_get_devices_for_stream;
dap->policy.get_output_for_effect = ap_get_output_for_effect;
dap->policy.register_effect = ap_register_effect;
dap->policy.unregister_effect = ap_unregister_effect;
dap->policy.set_effect_enabled = ap_set_effect_enabled;
dap->policy.is_stream_active = ap_is_stream_active;
dap->policy.dump = ap_dump;
dap->policy.is_offload_supported = ap_is_offload_supported;
dap->service = service;
dap->aps_ops = aps_ops;
*ap = &dap->policy;
return 0;
}
1.4
rc = mpAudioPolicy->init_check(mpAudioPolicy);
ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
if (rc)
return;
init_check调用,hal也没实现。
以上四步基本上就展示了Framework层与hal层交互过程。