【Android Audio】Parameter Framework - pfw

Parameter Framework - Android AudioPolicy Engine

使用 libaudiopolicyengineconfigurable.so 来取缔默认安卓音频引擎 libaudiopolicyenginedefault.so,因为默认安卓音频引擎是通过代码来决定策略,然而 libaudiopolicyengineconfigurable 采用读取pfw类型的文件来实现音频策略配置。

1. 优势

不需要修改代码,直接修改pfw配置文件就可以修改音频策略

2. 启用Parameter framework

Android U之前的版本
audio_policy_configuration.xml文件中 globalConfiguration字段增加字段: engine_library="configurable"

Android U之后的版本
使用aidl的hal,读取config的时候检测 audio_policy_engine_criterion_types.xmlaudio_policy_engine_criteria.xml文件存在就会启用

hardware\interfaces\audio\aidl\default\EngineConfigXmlConverter.cpp

void EngineConfigXmlConverter::init() {
...
    if (getXsdcConfig()->hasCriteria() && getXsdcConfig()->hasCriterion_types()) {
        AudioHalEngineConfig::CapSpecificConfig capSpecificConfig;
        capSpecificConfig.criteriaV2 =
                std::make_optional<>(VALUE_OR_FATAL((convertCapCriteriaCollectionToAidl(
                        getXsdcConfig()->getCriteria(), getXsdcConfig()->getCriterion_types()))));
...

3. 功能

当前主要是3大配置

    
        
        
        
    

3.1 输入(input_sources)

device_for_input_source.pfw
不同的audio_source_t使用什么样的输入设备


	
	
	
	
	
	
	
	
	
	
	
	
	
	

3.2 输出设备优先级(product_strategies)

device_for_product_strategy_phone.pfwdevice_for_product_strategy_media.pfw
不同的product_strategy_t使用什么样的输出设备


	
	
	
	
	
	
	
	
	

3.3 音量曲线切换(streams)

volumes.pfw
不同的audio_stream_type_t使用哪个stream type的音量曲线
eg: system的stream可以使用music的音量曲线


	
	
	
	
	
	
	
	
	
	
	
	
	
	

4. 配置pfw文件

4.1 supDomain

  • supDomain定义是对Strategy进行分类命名,可以任意修改
  • 可以定义多级supDomain,生成的xml中是以"."分隔
    (eg: DeviceForProductStrategy.Media)
supDomain: DeviceForProductStrategy
	supDomain: Media
		domain: Device1
			conf: ForceUseBtA2dpSpeaker
				AvailableOutputDevices Includes BLUETOOTH_A2DP_SPEAKER
				ForceUseForMedia Is BT_A2DP
				component: /Policy/policy/product_strategies/vx_1005/selected_output_devices/mask
					speaker = 0
					spdif = 0
					bluetooth_a2dp_speaker = 1
...

4.2 domain

  • supDomain下面可以配置多个domain
  • 多个domain的device是共存的
  • 同一supDomain中不同的domain中不能配置相同的device
    eg:下面Assistant的Strategy中配置了2个domain(Device1、Device2),ARC可以跟其他device共存
supDomain: DeviceForProductStrategy
	supDomain: Assistant
		domain: Device1
			conf: RemoteSubmix
				AvailableOutputDevices Includes REMOTE_SUBMIX
				AvailableOutputDevicesAddresses Includes 0
				component: /Policy/policy/product_strategies/assistant/selected_output_devices/mask
					speaker = 0
					remote_submix = 1
...
		domain: Device2
			#
			# these following domains consists in device(s) that can co-exist with others
			# e.g. ARC, SPDIF, AUX_LINE
			#
			conf: Selected
				AvailableOutputDevices Includes HDMI_ARC
				component: /Policy/policy/product_strategies/assistant/selected_output_devices/mask
					hdmi_arc = 1

			conf: NotSelected
				component: /Policy/policy/product_strategies/assistant/selected_output_devices/mask
					hdmi_arc = 0
...

4.3 config

  • 每一个domain下面的config相当于代码中的switch-case语句。放在前面的config先被执行到。
  • 满足config条件才会被使用,之后不再执行同一domain下的config。如果不满足条件则进行下一个config条件检查。
domain: Device1
	conf: ForceUseBtA2dpSpeaker
		AvailableOutputDevices Includes BLUETOOTH_A2DP_SPEAKER
		ForceUseForMedia Is BT_A2DP
		component: /Policy/policy/product_strategies/vx_1005/selected_output_devices/mask
			speaker = 0
			spdif = 0
			hdmi = 0
			hdmi_arc = 0
			bluetooth_a2dp_speaker = 1
			usb_device = 0
	conf: UsbDevice
		AvailableOutputDevices Includes USB_DEVICE
		component: /Policy/policy/product_strategies/vx_1005/selected_output_devices/mask
			speaker = 0
			spdif = 0
			hdmi = 0
			hdmi_arc = 0
            bluetooth_a2dp_speaker = 0
			usb_device = 1
4.3.1 criterion (准则/标准)

每个criterion 定义: pfw中所有能使用的准则类型 audio_policy_engine_criteria.xml

frameworks/av/services/audiopolicy/engineconfigurable/config/example/common/audio_policy_engine_criteria.xml


    
    
    
...

4.3.2 每个criterion的值:每个准则类型的值

/vendor/etc/audio_policy_engine_criteria.xmlaudio_policy_engine_criterion_typespython脚本根据audio_policy_engine_criterion_types.xml.in文件自动生成。

  • Android 16使用Google最新的capBuildPolicyCriterionTypes.py,之前安卓版本使用buildPolicyCriterionTypes.py的Python脚本。

frameworks/av/services/audiopolicy/engineconfigurable/tools/

frameworks/av/services/audiopolicy/engineconfigurable/config/example/common/audio_policy_engine_criterion_types.xml.in


        
                
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                
        
...
    
        
            
            
            
            
            
            
            
            
        
    
...

4.3.3 使用criterion

例如:配置输出设备策略

    conf: UsbDevice
        AvailableOutputDevices Includes USB_DEVICE
		AvailableOutputDevices Excludes WIRED_HEADPHONE
        ANY
            ForceUseForCommunication Is BT_SCO
            ALL
                ForceUseForCommunication Is SPEAKER
                TelephonyMode IsNot IN_CALL
				component: /Policy/policy/product_strategies/vx_1000/selected_output_devices/mask
...
					usb_accessory = 0
					usb_device = 1
					usb_headset = 0
					speaker = 0
...

上面的config转化为伪代码的case:

if (AvailableOutputDevices 存在 USB_DEVICE) {
    if (AvailableOutputDevices 不存在 WIRED_HEADPHONE) {
        if (ForceUseForCommunication == BT_SCO ||
        (ForceUseForCommunication == SPEAKER && TelephonyMode != IN_CALL)) {
            return USB_DEVICE;
        }
    }
}

当前Google pfw支持3个SubSystem(Stream、InputSource、ProductStrategy)

frameworks\av\services\audiopolicy\engineconfigurable\parameter-framework\plugin\PolicySubsystem.cpp

    // Provide creators to upper layer
    addSubsystemObjectFactory(
        new TSubsystemObjectFactory(
            mStreamComponentName,
            (1 << MappingKeyName))
        );
    addSubsystemObjectFactory(
        new TSubsystemObjectFactory(
            mInputSourceComponentName,
            (1 << MappingKeyName))
        );
    addSubsystemObjectFactory(
        new TSubsystemObjectFactory(
            mProductStrategyComponentName, (1 << MappingKeyName))
        );

分别通过下面3个函数推送到Engine中

frameworks\av\services\audiopolicy\engineconfigurable\parameter-framework\plugin\

Stream::sendToHW
Engine::setVolumeProfileForStream
InputSource::sendToHW
Engine::setDeviceForInputSource
ProductStrategy::sendToHW
Engine::setDeviceTypesForProductStrategy
Engine::setDeviceAddressForProductStrategy

/vendor/etc/parameter-framework/Structure/Policy/PolicySubsystem-CommonTypes.xml

5.1 快速debug

修改完对应的pfw文件之后
parameter framework的pfw所在路径下mm -j,将/vendor/etc/parameter-framework/目录替换到平台即可

你可能感兴趣的:(android,Audio)