AudioPolicyService
AudioPolicyInterface AudioPolicyClientInterface
audio_policy audio_policy_service_ops
两个互相对应
/*The AudioPolicyInterface and AudioPolicyClientInterface classes
--------------------------------------------------------------------
* The audio_policy and audio_policy_service_ops structs define the
* communication interfaces between the platform specific audio policy manager
* and Android generic audio policy manager.
* The platform specific audio policy manager must implement methods of the
* audio_policy struct.
* This implementation makes use of the audio_policy_service_ops to control
* the activity and configuration of audio input and output streams.
*
* The platform specific audio policy manager is in charge of the audio
* routing and volume control policies for a given platform.
* The main roles of this module are:
* - keep track of current system state (removable device connections, phone
* state, user requests...).
* System state changes and user actions are notified to audio policy
* manager with methods of the audio_policy.
*
* - process get_output() queries received when AudioTrack objects are
* created: Those queries return a handler on an output that has been
* selected, configured and opened by the audio policy manager and that
* must be used by the AudioTrack when registering to the AudioFlinger
* with the createTrack() method.
* When the AudioTrack object is released, a release_output() query
* is received and the audio policy manager can decide to close or
* reconfigure the output depending on other streams using this output and
* current system state.
*
* - similarly process get_input() and release_input() queries received from
* AudioRecord objects and configure audio inputs.
* - process volume control requests: the stream volume is converted from
* an index value (received from UI) to a float value applicable to each
* output as a function of platform specific settings and current output
* route (destination device). It also make sure that streams are not
* muted if not allowed (e.g. camera shutter sound in some countries).
*/
audio_policy:
* The platform specific audio policy manager must implement methods of the
* audio_policy struct.
audio_policy_service_ops
* This implementation makes use of the audio_policy_service_ops to control
* the activity and configuration of audio input and output streams.
struct audio_policy {
/*
* configuration functions
*/
int (*set_device_connection_state)
audio_policy_dev_state_t (*get_device_connection_state)
void (*set_phone_state)
void (*set_ringer_mode)
void (*set_force_use)
audio_policy_forced_cfg_t (*get_force_use)
void (*set_can_mute_enforced_audible)
int (*init_check)
/*
* Audio routing query functions
*/
audio_io_handle_t (*get_output)
int (*start_output)
int (*stop_output)
void (*release_output)
audio_io_handle_t (*get_input)
int (*start_input)
int (*stop_input)
void (*release_input)
/*
* volume control functions
*/
void (*init_stream_volume)
int (*set_stream_volume_index)
int (*get_stream_volume_index)
int (*set_stream_volume_index_for_device)
int (*get_stream_volume_index_for_device)
uint32_t (*get_strategy_for_stream)
audio_devices_t (*get_devices_for_stream)
/* Audio effect management */
audio_io_handle_t (*get_output_for_effect)
int (*register_effect)
int (*unregister_effect)
int (*set_effect_enabled)
bool (*is_stream_active)
bool (*is_stream_active_remotely)
bool (*is_source_active)
int (*dump)
bool (*is_offload_supported)
}
struct audio_policy_service_ops {
/*
* Audio output Control functions
*/
/* Opens an audio output with the requested parameters.
*
* The parameter values can indicate to use the default values in case the
* audio policy manager has no specific requirements for the output being
* opened.
*
* When the function returns, the parameter values reflect the actual
* values used by the audio hardware output stream.
*
* The audio policy manager can check if the proposed parameters are
* suitable or not and act accordingly.
*/
audio_io_handle_t (*open_output)
audio_io_handle_t (*open_duplicate_output)
int (*close_output)
/* suspends the output.
*
* When an output is suspended, the corresponding audio hardware output
* stream is placed in standby and the AudioTracks attached to the mixer
* thread are still processed but the output mix is discarded.
*/
int (*suspend_output)
int (*restore_output)
audio_io_handle_t (*open_input)
int (*close_input)
/* */
/* misc control functions */
/* */
int (*set_stream_volume)
int (*invalidate_stream)
/* function enabling to send proprietary informations directly from audio
* policy manager to audio hardware interface. */
void (*set_parameters)
/* function enabling to receive proprietary informations directly from
* audio hardware interface to audio policy manager.
*
* Returns a pointer to a heap allocated string. The caller is responsible
* for freeing the memory for it using free().
*/
char * (*get_parameters)(void *service, audio_io_handle_t io_handle,
const char *keys);
int (*start_tone)
int (*stop_tone)
int (*set_voice_volume)
int (*move_effects)
audio_module_handle_t (*load_hw_module)
audio_io_handle_t (*open_output_on_module)
audio_io_handle_t (*open_input_on_module)
/**********************************************************************/
/**
* 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 audio_policy_module {
struct hw_module_t common;
} audio_policy_module_t;
struct audio_policy_device {
/**
* Common methods of the audio policy device. This *must* be the first member of
* audio_policy_device as users of this structure will cast a hw_device_t to
* audio_policy_device pointer in contexts where it's known the hw_device_t references an
* audio_policy_device.
*/
struct hw_device_t common;
int (*create_audio_policy)(const struct audio_policy_device *device,
struct audio_policy_service_ops *aps_ops,
void *service,
struct audio_policy **ap);
int (*destroy_audio_policy)(const struct audio_policy_device *device,
struct audio_policy *ap);
};
static inline int audio_policy_dev_open(const hw_module_t* module,
struct audio_policy_device** device)
{
return module->methods->open(module, AUDIO_POLICY_INTERFACE,
(hw_device_t**)device);
}
======================================================================================================
struct qcom_audio_policy {
struct audio_policy policy;
void *service;
struct audio_policy_service_ops *aps_ops;
AudioPolicyCompatClient *service_client;
AudioPolicyInterface *apm;
};
struct audio_policy_device *mpAudioPolicyDev;
struct audio_policy *mpAudioPolicy;
AudioPolicyInterface *mAudioPolicyManager;
AudioPolicyClient *mAudioPolicyClient;
class AudioPolicyClient : public AudioPolicyClientInterface
======================================================================================================
audio_policy_hal-->audioPolicyManager-->AudioPolicyCompatClient-->audioPolicyClientImplLegacy-->audio_policy_service_ops
AudioPolicyService::AudioPolicyService()
: BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
{
}
void AudioPolicyService::onFirstRef()
// start tone playback thread
mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
// start audio commands thread
mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
// start output activity command thread
mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
#ifdef USE_LEGACY_AUDIO_POLICY
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)
--------------------------------------------------------
static int create_qcom_ap(const struct audio_policy_device *device, //当前device,struct audio_policy_device device
struct audio_policy_service_ops *aps_ops, //传入aps_ops函数表
void *service, //AudioPolicyService
struct audio_policy **ap) //返回的audio_policy
{
struct qcom_audio_policy *qap;
qap->policy.set_device_connection_state = ap_set_device_connection_state;
qap->policy.get_device_connection_state = ap_get_device_connection_state;
qap->policy.set_phone_state = ap_set_phone_state;
...
qap->service = service;
qap->aps_ops = aps_ops;
qap->service_client = new AudioPolicyCompatClient(aps_ops, service); //class AudioPolicyCompatClient : public AudioPolicyClientInterface
qap->apm = createAudioPolicyManager(qap->service_client);
*ap = &qap->policy;
}
--------------------------------------------------------
rc = mpAudioPolicy->init_check(mpAudioPolicy);
#else
ALOGI("AudioPolicyService CSTOR in new mode");
mAudioPolicyClient = new AudioPolicyClient(this);
mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
#endif
======================================================================================================
AudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
{
mpClientInterface = clientInterface; //接口赋值
for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
mForceUse[i] = AudioSystem::FORCE_NONE;
}
//加载配置文件,优先加载vendor下的
//#define AUDIO_POLICY_CONFIG_FILE "/system/etc/audio_policy.conf"
//#define AUDIO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_policy.conf"
if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
ALOGE("could not load audio policy configuration file, setting defaults");
defaultAudioPolicyConfig();
}
}
initializeVolumeCurves();
// open all output streams needed to access attached devices
for (size_t i = 0; i < mHwModules.size(); i++) {
mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
if (mHwModules[i]->mHandle == 0) {
ALOGW("could not open HW module %s", mHwModules[i]->mName);
continue;
}
// open all output streams needed to access attached devices
// except for direct output streams that are only opened when they are actually
// required by an app.
for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
{
const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
if ((outProfile->mSupportedDevices & mAttachedOutputDevices) &&
((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
outProfile->mSupportedDevices);
audio_io_handle_t output = mpClientInterface->openOutput(
outProfile->mModule->mHandle,
&outputDesc->mDevice,
&outputDesc->mSamplingRate,
&outputDesc->mFormat,
&outputDesc->mChannelMask,
&outputDesc->mLatency,
outputDesc->mFlags);
if (output == 0) {
delete outputDesc;
} else {
mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
(outProfile->mSupportedDevices & mAttachedOutputDevices));
if (mPrimaryOutput == 0 &&
outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
mPrimaryOutput = output;
}
addOutput(output, outputDesc);
setOutputDevice(output,
(audio_devices_t)(mDefaultOutputDevice &
outProfile->mSupportedDevices),
true);
}
}
}
}
}
阅读(1616) | 评论(0) | 转发(0) |