Android4.1 中出现了一个新的类,BinderService,所有的Native Service 都会继承这个类。
class BinderService { public: static status_t publish(bool allowIsolated = false) { sp<IServiceManager> sm(defaultServiceManager()); return sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated); } static void publishAndJoinThreadPool(bool allowIsolated = false) { sp<IServiceManager> sm(defaultServiceManager()); sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated); ProcessState::self()->startThreadPool(); IPCThreadState::self()->joinThreadPool(); } static void instantiate() { publish(); } static status_t shutdown() { return NO_ERROR; } }
从代码中可以得知,这个类的publish(),就是将Native Service 注册到ServiceManager,同时 BinderService 作为 NativeService 的友元类。这是因为BinderService 需要访问 Native Service 的getServiceName方法。
class AudioPolicyService : public BinderService<AudioPolicyService>, public BnAudioPolicyService, public IBinder::DeathRecipient AudioPolicyService继承了BinderService接口,用于把AudioPolicyService本地服务注册到ServiceManager中; AudioPolicyService继承了BnAudioPolicyService接口,这样AudioPolicyService就可以基于Android的Binder机制,做为服务器向外部提供服务; AudioPolicyService有一个内部线程类AudioCommandThread,顾名思义,所有的命令(音量控制,输入、输出的切换等)最终都会在该线程中排队执行;
namespace {
struct audio_policy_service_ops aps_ops = {
open_output : aps_open_output,
open_duplicate_output : aps_open_dup_output,
close_output : aps_close_output,
suspend_output : aps_suspend_output,
restore_output : aps_restore_output,
open_input : aps_open_input,
close_input : aps_close_input,
set_stream_volume : aps_set_stream_volume,
set_stream_output : aps_set_stream_output,
set_parameters : aps_set_parameters,
get_parameters : aps_get_parameters,
start_tone : aps_start_tone,
stop_tone : aps_stop_tone,
set_voice_volume : aps_set_voice_volume,
move_effects : aps_move_effects,
load_hw_module : aps_load_hw_module,
open_output_on_module : aps_open_output_on_module,
open_input_on_module : aps_open_input_on_module,
};
}
aps_ops:主要是通过IAudioFlinger,调用AudioSystem中的接口
\frameworks\av\services\audioflinger\AudioPolicyService.cpp
AudioPolicyService::AudioPolicyService() : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL) { char value[PROPERTY_VALUE_MAX]; const struct hw_module_t *module; int forced_val; int rc; Mutex::Autolock _l(mLock); // start tone playback thread mTonePlaybackThread = new AudioCommandThread(String8("")); // start audio commands thread mAudioCommandThread = new AudioCommandThread(String8("ApmCommand")); /* instantiate the audio policy manager */ rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); rc = audio_policy_dev_open(module, &mpAudioPolicyDev); rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, &mpAudioPolicy); rc = mpAudioPolicy->init_check(mpAudioPolicy); // load audio pre processing modules if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) { loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE); } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) { loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE); } }
\hardware\libhardware_legacy\audio\audio_policy_hal.cpp
struct legacy_ap_module HAL_MODULE_INFO_SYM = { module: { common: { tag: HARDWARE_MODULE_TAG, version_major: 1, version_minor: 0, id: AUDIO_POLICY_HARDWARE_MODULE_ID, name: "LEGACY Audio Policy HAL", author: "The Android Open Source Project", methods: &legacy_ap_module_methods, dso : NULL, reserved : {0}, }, }, }; static int create_legacy_ap(const struct audio_policy_device *device, struct audio_policy_service_ops *aps_ops, void *service, struct audio_policy **ap) { struct legacy_audio_policy *lap; int ret; if (!service || !aps_ops) return -EINVAL; lap = (struct legacy_audio_policy *)calloc(1, sizeof(*lap)); if (!lap) return -ENOMEM; lap->policy.set_device_connection_state = ap_set_device_connection_state; lap->policy.get_device_connection_state = ap_get_device_connection_state; lap->policy.set_phone_state = ap_set_phone_state; lap->policy.set_ringer_mode = ap_set_ringer_mode; lap->policy.set_force_use = ap_set_force_use; lap->policy.get_force_use = ap_get_force_use; lap->policy.set_can_mute_enforced_audible = ap_set_can_mute_enforced_audible; lap->policy.init_check = ap_init_check; lap->policy.get_output = ap_get_output; lap->policy.start_output = ap_start_output; lap->policy.stop_output = ap_stop_output; lap->policy.release_output = ap_release_output; lap->policy.get_input = ap_get_input; lap->policy.start_input = ap_start_input; lap->policy.stop_input = ap_stop_input; lap->policy.release_input = ap_release_input; lap->policy.init_stream_volume = ap_init_stream_volume; lap->policy.set_stream_volume_index = ap_set_stream_volume_index; lap->policy.get_stream_volume_index = ap_get_stream_volume_index; lap->policy.set_stream_volume_index_for_device = ap_set_stream_volume_index_for_device; lap->policy.get_stream_volume_index_for_device = ap_get_stream_volume_index_for_device; lap->policy.get_strategy_for_stream = ap_get_strategy_for_stream; lap->policy.get_devices_for_stream = ap_get_devices_for_stream; lap->policy.get_output_for_effect = ap_get_output_for_effect; lap->policy.register_effect = ap_register_effect; lap->policy.unregister_effect = ap_unregister_effect; lap->policy.set_effect_enabled = ap_set_effect_enabled; lap->policy.is_stream_active = ap_is_stream_active; lap->policy.is_source_active = ap_is_source_active; lap->policy.dump = ap_dump; lap->service = service; lap->aps_ops = aps_ops; lap->service_client = new AudioPolicyCompatClient(aps_ops, service);/* 创建 */ if (!lap->service_client) { ret = -ENOMEM; goto err_new_compat_client; } lap->apm = createAudioPolicyManager(lap->service_client); if (!lap->apm) { ret = -ENOMEM; goto err_create_apm; } *ap = &lap->policy; return 0; err_create_apm: delete lap->service_client; err_new_compat_client: free(lap); *ap = NULL; return ret; } create_legacy_ap:主要是创建了AudioPolicyCompatClient,并将它做为参数创建AudioPolicyManager。
frameworks/av/services/audioflinger/AudioPolicyCompatClient.h
AudioPolicyCompatClient(struct audio_policy_service_ops *serviceOps, void *service) : mServiceOps(serviceOps) AudioPolicyCompatClient的serviceOps指向AudioPolicyService的aps_ops
Android4.1 中出现了一个新的类,BinderService,所有的Native Service 都会继承这个类。、
Android4.1 中出现了一个新的类,BinderService,所有的Native Service 都会继承这个类。
Android4.1 中出现了一个新的类,BinderService,所有的Native Service 都会继承这个类。
Android4.1 中出现了一个新的类,BinderService,所有的Native Service 都会继承这个类。
Android4.1 中出现了一个新的类,BinderService,所有的Native Service 都会继承这个类。
Android4.1 中出现了一个新的类,BinderService,所有的Native Service 都会继承这个类。