audio HAL 架构

AudioPolicyInterface AudioPolicyClientInterface
audio_policy audio_policy_service_ops
两组互相对应

 

hardware/libhardware/include/hardware/audio_policy.h
//架构定义 通过audio_policy_service_ops调用audio_policy中的函数 是这个意思么? struct
audio_policy { /* * configuration functions */ /* indicate a change in device connection status */ int (*set_device_connection_state)(struct audio_policy *pol, audio_devices_t device, audio_policy_dev_state_t state, const char *device_address); /* retrieve a device connection status */ audio_policy_dev_state_t (*get_device_connection_state)( const struct audio_policy *pol, audio_devices_t device, const char *device_address); /* indicate a change in phone state. Valid phones states are defined * by audio_mode_t */ void (*set_phone_state)(struct audio_policy *pol, audio_mode_t state); /* deprecated, never called (was "indicate a change in ringer mode") */ void (*set_ringer_mode)(struct audio_policy *pol, uint32_t mode, uint32_t mask); /* force using a specific device category for the specified usage */ void (*set_force_use)(struct audio_policy *pol, audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); /* retrieve current device category forced for a given usage */ audio_policy_forced_cfg_t (*get_force_use)(const struct audio_policy *pol, audio_policy_force_use_t usage); /* if can_mute is true, then audio streams that are marked ENFORCED_AUDIBLE * can still be muted. */ void (*set_can_mute_enforced_audible)(struct audio_policy *pol, bool can_mute); /* check proper initialization */ int (*init_check)(const struct audio_policy *pol); /* * Audio routing query functions */ /* request an output appropriate for playback of the supplied stream type and * parameters */ audio_io_handle_t (*get_output)(struct audio_policy *pol, audio_stream_type_t stream, uint32_t samplingRate, audio_format_t format, audio_channel_mask_t channelMask, audio_output_flags_t flags, const audio_offload_info_t *offloadInfo); /* indicates to the audio policy manager that the output starts being used * by corresponding stream. */ int (*start_output)(struct audio_policy *pol, audio_io_handle_t output, audio_stream_type_t stream, int session); /* indicates to the audio policy manager that the output stops being used * by corresponding stream. */ int (*stop_output)(struct audio_policy *pol, audio_io_handle_t output, audio_stream_type_t stream, int session); /* releases the output. */ void (*release_output)(struct audio_policy *pol, audio_io_handle_t output); /* request an input appropriate for record from the supplied device with * supplied parameters. */ audio_io_handle_t (*get_input)(struct audio_policy *pol, audio_source_t inputSource, uint32_t samplingRate, audio_format_t format, audio_channel_mask_t channelMask, audio_in_acoustics_t acoustics); /* indicates to the audio policy manager that the input starts being used */ int (*start_input)(struct audio_policy *pol, audio_io_handle_t input); /* indicates to the audio policy manager that the input stops being used. */ int (*stop_input)(struct audio_policy *pol, audio_io_handle_t input); /* releases the input. */ void (*release_input)(struct audio_policy *pol, audio_io_handle_t input); /* * volume control functions */ /* initialises stream volume conversion parameters by specifying volume * index range. The index range for each stream is defined by AudioService. */ void (*init_stream_volume)(struct audio_policy *pol, audio_stream_type_t stream, int index_min, int index_max); /* sets the new stream volume at a level corresponding to the supplied * index. The index is within the range specified by init_stream_volume() */ int (*set_stream_volume_index)(struct audio_policy *pol, audio_stream_type_t stream, int index); /* retrieve current volume index for the specified stream */ int (*get_stream_volume_index)(const struct audio_policy *pol, audio_stream_type_t stream, int *index); /* sets the new stream volume at a level corresponding to the supplied * index for the specified device. * The index is within the range specified by init_stream_volume() */ int (*set_stream_volume_index_for_device)(struct audio_policy *pol, audio_stream_type_t stream, int index, audio_devices_t device); /* retrieve current volume index for the specified stream for the specified device */ int (*get_stream_volume_index_for_device)(const struct audio_policy *pol, audio_stream_type_t stream, int *index, audio_devices_t device); /* return the strategy corresponding to a given stream type */ uint32_t (*get_strategy_for_stream)(const struct audio_policy *pol, audio_stream_type_t stream); /* return the enabled output devices for the given stream type */ audio_devices_t (*get_devices_for_stream)(const struct audio_policy *pol, audio_stream_type_t stream); /* Audio effect management */ audio_io_handle_t (*get_output_for_effect)(struct audio_policy *pol, const struct effect_descriptor_s *desc); int (*register_effect)(struct audio_policy *pol, const struct effect_descriptor_s *desc, audio_io_handle_t output, uint32_t strategy, int session, int id); int (*unregister_effect)(struct audio_policy *pol, int id); int (*set_effect_enabled)(struct audio_policy *pol, int id, bool enabled); bool (*is_stream_active)(const struct audio_policy *pol, audio_stream_type_t stream, uint32_t in_past_ms); bool (*is_stream_active_remotely)(const struct audio_policy *pol, audio_stream_type_t stream, uint32_t in_past_ms); bool (*is_source_active)(const struct audio_policy *pol, audio_source_t source); /* dump state */ int (*dump)(const struct audio_policy *pol, int fd); /* check if offload is possible for given sample rate, bitrate, duration, ... */ bool (*is_offload_supported)(const struct audio_policy *pol, const audio_offload_info_t *info); }; 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)(void *service, audio_devices_t *pDevices, uint32_t *pSamplingRate, audio_format_t *pFormat, audio_channel_mask_t *pChannelMask, uint32_t *pLatencyMs, audio_output_flags_t flags); /* creates a special output that is duplicated to the two outputs passed as * arguments. The duplication is performed by * a special mixer thread in the AudioFlinger. */ audio_io_handle_t (*open_duplicate_output)(void *service, audio_io_handle_t output1, audio_io_handle_t output2); /* closes the output stream */ int (*close_output)(void *service, audio_io_handle_t 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)(void *service, audio_io_handle_t output); /* restores a suspended output. */ int (*restore_output)(void *service, audio_io_handle_t output); /* */ /* Audio input Control functions */ /* */ /* opens an audio input * deprecated - new implementations should use open_input_on_module, * and the acoustics parameter is ignored */ audio_io_handle_t (*open_input)(void *service, audio_devices_t *pDevices, uint32_t *pSamplingRate, audio_format_t *pFormat, audio_channel_mask_t *pChannelMask, audio_in_acoustics_t acoustics); /* closes an audio input */ int (*close_input)(void *service, audio_io_handle_t input); /* */ /* misc control functions */ /* */ /* set a stream volume for a particular output. * * For the same user setting, a given stream type can have different * volumes for each output (destination device) it is attached to. */ int (*set_stream_volume)(void *service, audio_stream_type_t stream, float volume, audio_io_handle_t output, int delay_ms); /* invalidate a stream type, causing a reroute to an unspecified new output */ int (*invalidate_stream)(void *service, audio_stream_type_t stream); /* function enabling to send proprietary informations directly from audio * policy manager to audio hardware interface. */ void (*set_parameters)(void *service, audio_io_handle_t io_handle, const char *kv_pairs, int delay_ms); /* 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); /* request the playback of a tone on the specified stream. * used for instance to replace notification sounds when playing over a * telephony device during a phone call. */ int (*start_tone)(void *service, audio_policy_tone_t tone, audio_stream_type_t stream); int (*stop_tone)(void *service); /* set down link audio volume. */ int (*set_voice_volume)(void *service, float volume, int delay_ms); /* move effect to the specified output */ int (*move_effects)(void *service, int session, audio_io_handle_t src_output, audio_io_handle_t dst_output); /* loads an audio hw module. * * The module name passed is the base name of the HW module library, e.g "primary" or "a2dp". * The function returns a handle on the module that will be used to specify a particular * module when calling open_output_on_module() or open_input_on_module() */ audio_module_handle_t (*load_hw_module)(void *service, const char *name); /* Opens an audio output on a particular HW module. * * Same as open_output() but specifying a specific HW module on which the output must be opened. */ audio_io_handle_t (*open_output_on_module)(void *service, audio_module_handle_t module, audio_devices_t *pDevices, uint32_t *pSamplingRate, audio_format_t *pFormat, audio_channel_mask_t *pChannelMask, uint32_t *pLatencyMs, audio_output_flags_t flags, const audio_offload_info_t *offloadInfo); /* Opens an audio input on a particular HW module. * * Same as open_input() but specifying a specific HW module on which the input must be opened. * Also removed deprecated acoustics parameter */ audio_io_handle_t (*open_input_on_module)(void *service, audio_module_handle_t module, audio_devices_t *pDevices, uint32_t *pSamplingRate, audio_format_t *pFormat, audio_channel_mask_t *pChannelMask); };

/**
 * 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);
};

 

frameworks/av/services/audiopolicy/AudioPolicyService.cpp

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,
        .invalidate_stream     = aps_invalidate_stream,
        .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,
        //AudioPolicyManager::mpClientInterface->openOutput() => AudioPolicyCompatClient::openOutput() => mServiceOps->open_output_on_module()
        .open_output_on_module = aps_open_output_on_module,
        .open_input_on_module  = aps_open_input_on_module,
    };
}; // namespace <unnamed>

}; // namespace android

 

//frameworks/av/services/audiopolicy/AudioPolicyManager.h
class AudioPolicyManager: public AudioPolicyInterface {
//................
}

hardware/libhardware_legacy/include/hardware_legacy/AudioPolicyInterface.h class
AudioPolicyInterface { public: virtual ~AudioPolicyInterface() {} // // configuration functions // // indicate a change in device connection status virtual status_t setDeviceConnectionState(audio_devices_t device, AudioSystem::device_connection_state state, const char *device_address) = 0; // retrieve a device connection status virtual AudioSystem::device_connection_state getDeviceConnectionState(audio_devices_t device, const char *device_address) = 0; // indicate a change in phone state. Valid phones states are defined by AudioSystem::audio_mode virtual void setPhoneState(int state) = 0; // force using a specific device category for the specified usage virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config) = 0; // retrieve current device category forced for a given usage virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage) = 0; // set a system property (e.g. camera sound always audible) virtual void setSystemProperty(const char* property, const char* value) = 0; // check proper initialization virtual status_t initCheck() = 0; // // Audio routing query functions // // request an output appropriate for playback of the supplied stream type and parameters virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream, uint32_t samplingRate, audio_format_t format, audio_channel_mask_t channelMask, AudioSystem::output_flags flags, const audio_offload_info_t *offloadInfo) = 0; // indicates to the audio policy manager that the output starts being used by corresponding stream. virtual status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream, int session = 0) = 0; // indicates to the audio policy manager that the output stops being used by corresponding stream. virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream, int session = 0) = 0; // releases the output. virtual void releaseOutput(audio_io_handle_t output) = 0; // request an input appropriate for record from the supplied device with supplied parameters. virtual audio_io_handle_t getInput(int inputSource, uint32_t samplingRate, audio_format_t format, audio_channel_mask_t channelMask, AudioSystem::audio_in_acoustics acoustics) = 0; // indicates to the audio policy manager that the input starts being used. virtual status_t startInput(audio_io_handle_t input) = 0; // indicates to the audio policy manager that the input stops being used. virtual status_t stopInput(audio_io_handle_t input) = 0; // releases the input. virtual void releaseInput(audio_io_handle_t input) = 0; // // volume control functions // // initialises stream volume conversion parameters by specifying volume index range. virtual void initStreamVolume(AudioSystem::stream_type stream, int indexMin, int indexMax) = 0; // sets the new stream volume at a level corresponding to the supplied index for the // supplied device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT means // setting volume for all devices virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index, audio_devices_t device) = 0; // retrieve current volume index for the specified stream and the // specified device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT means // querying the volume of the active device. virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index, audio_devices_t device) = 0; // return the strategy corresponding to a given stream type virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream) = 0; // return the enabled output devices for the given stream type virtual audio_devices_t getDevicesForStream(AudioSystem::stream_type stream) = 0; // Audio effect management virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc) = 0; virtual status_t registerEffect(const effect_descriptor_t *desc, audio_io_handle_t io, uint32_t strategy, int session, int id) = 0; virtual status_t unregisterEffect(int id) = 0; virtual status_t setEffectEnabled(int id, bool enabled) = 0; virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const = 0; virtual bool isStreamActiveRemotely(int stream, uint32_t inPastMs = 0) const = 0; virtual bool isSourceActive(audio_source_t source) const = 0; //dump state virtual status_t dump(int fd) = 0; virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo) = 0; }; // Audio Policy client Interface class AudioPolicyClientInterface { public: virtual ~AudioPolicyClientInterface() {} // // Audio HW module functions // // loads a HW module. virtual audio_module_handle_t loadHwModule(const char *name) = 0; // // 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. virtual audio_io_handle_t openOutput(audio_module_handle_t module, audio_devices_t *pDevices, uint32_t *pSamplingRate, audio_format_t *pFormat, audio_channel_mask_t *pChannelMask, uint32_t *pLatencyMs, audio_output_flags_t flags, const audio_offload_info_t *offloadInfo = NULL) = 0; // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by // a special mixer thread in the AudioFlinger. virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2) = 0; // closes the output stream virtual status_t closeOutput(audio_io_handle_t output) = 0; // 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. virtual status_t suspendOutput(audio_io_handle_t output) = 0; // restores a suspended output. virtual status_t restoreOutput(audio_io_handle_t output) = 0; // // Audio input Control functions // // opens an audio input virtual audio_io_handle_t openInput(audio_module_handle_t module, audio_devices_t *pDevices, uint32_t *pSamplingRate, audio_format_t *pFormat, audio_channel_mask_t *pChannelMask) = 0; // closes an audio input virtual status_t closeInput(audio_io_handle_t input) = 0; // // misc control functions // // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes // for each output (destination device) it is attached to. virtual status_t setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output, int delayMs = 0) = 0; // invalidate a stream type, causing a reroute to an unspecified new output virtual status_t invalidateStream(AudioSystem::stream_type stream) = 0; // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface. virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0) = 0; // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager. virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) = 0; // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing // over a telephony device during a phone call. virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream) = 0; virtual status_t stopTone() = 0; // set down link audio volume. virtual status_t setVoiceVolume(float volume, int delayMs = 0) = 0; // move effect to the specified output virtual status_t moveEffects(int session, audio_io_handle_t srcOutput, audio_io_handle_t dstOutput) = 0; };

 

//hardware/libhardware_legacy/audio/AudioPolicyCompatClient.h
class AudioPolicyCompatClient : public AudioPolicyClientInterface {
  //......
}

 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.

posted @ 2015-04-28 10:34  牧 天  阅读(1133)  评论(0)    收藏  举报