[Android][Audio] audio_policy.conf文件分析
不同的Android产品在音频的设计上通常是存在差异的,而这些差异可以同过Audio的配置文件audio_policy.conf来获得。在Android系统中音频配置文件存放路径有两处,存放地址可以从AudioPolicyManagerBase.cpp文件中知道:
#define AUDIO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_policy.conf" #define AUDIO_POLICY_CONFIG_FILE "/system/etc/audio_policy.conf"
在AudioPolicyManager.cpp文件中可以知道系统会首先加载vendor/etc目录下的configure文件,再加载system/etc目录下的configure文件。若这两者加载都发生错误的话,系统会加载default配置文件,并命名为primary module,从这可以看出,音频系统中一定必须存在的module就是primary了。
1 if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) { 2 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) { 3 ALOGE("could not load audio policy configuration file, setting defaults"); 4 defaultAudioPolicyConfig(); 5 } 6 }
【摘录audio_policy.conf】
1 # Global configuration section: lists input and output devices always present on the device 2 # as well as the output device selected by default. 3 # Devices are designated by a string that corresponds to the enum in audio.h 4 5 global_configuration { 6 attached_output_devices AUDIO_DEVICE_OUT_SPEAKER 7 default_output_device AUDIO_DEVICE_OUT_SPEAKER 8 attached_input_devices AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_VOICE_CALL|AUDIO_DEVICE_IN_REMOTE_SUBMIX 9 custom_properties { 10 voice_volume_applied_after_mixing_in_call true 11 voice_volume_applied_after_mixing_in_communication false 12 in_call_music_attenuation_dB 6 13 } 14 } 15 16 # audio hardware module section: contains descriptors for all audio hw modules present on the 17 # device. Each hw module node is named after the corresponding hw module library base name. 18 # For instance, "primary" corresponds to audio.primary.<device>.so. 19 # The "primary" module is mandatory and must include at least one output with 20 # AUDIO_OUTPUT_FLAG_PRIMARY flag. 21 # Each module descriptor contains one or more output profile descriptors and zero or more 22 # input profile descriptors. Each profile lists all the parameters supported by a given output 23 # or input stream category. 24 # The "channel_masks", "formats", "devices" and "flags" are specified using strings corresponding 25 # to enums in audio.h and audio_policy.h. They are concatenated by use of "|" without space or "\n". 26 27 audio_hw_modules { 28 primary { 29 outputs { 30 primary { 31 sampling_rates 48000 32 channel_masks AUDIO_CHANNEL_OUT_STEREO 33 formats AUDIO_FORMAT_PCM_16_BIT 34 devices AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE|AUDIO_DEVICE_OUT_ALL_SCO 35 flags AUDIO_OUTPUT_FLAG_PRIMARY 36 } 37 deep_buffer { 38 sampling_rates 48000 39 channel_masks AUDIO_CHANNEL_OUT_STEREO 40 formats AUDIO_FORMAT_PCM_16_BIT 41 devices AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE 42 flags AUDIO_OUTPUT_FLAG_DEEP_BUFFER 43 } 44 } 45 inputs { 46 primary { 47 sampling_rates 8000|11025|12000|16000|22050|24000|32000|44100|48000 48 channel_masks AUDIO_CHANNEL_IN_MONO|AUDIO_CHANNEL_IN_STEREO 49 formats AUDIO_FORMAT_PCM_16_BIT 50 devices AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_VOICE_CALL 51 } 52 } 53 } 54 a2dp { 55 outputs { 56 a2dp { 57 sampling_rates 44100 58 channel_masks AUDIO_CHANNEL_OUT_STEREO 59 formats AUDIO_FORMAT_PCM_16_BIT 60 devices AUDIO_DEVICE_OUT_ALL_A2DP 61 } 62 } 63 } 64 hdmi { 65 outputs { 66 hdmi_stereo { 67 sampling_rates 48000 68 channel_masks AUDIO_CHANNEL_OUT_STEREO 69 formats AUDIO_FORMAT_PCM_16_BIT 70 devices AUDIO_DEVICE_OUT_AUX_DIGITAL 71 } 72 hdmi_multi { 73 sampling_rates 32000|44100|48000|88200|96000 74 # channel_masks AUDIO_CHANNEL_OUT_5POINT1 75 channel_masks dynamic 76 formats AUDIO_FORMAT_PCM_16_BIT 77 devices AUDIO_DEVICE_OUT_AUX_DIGITAL 78 flags AUDIO_OUTPUT_FLAG_DIRECT 79 } 80 } 81 } 82 r_submix { 83 outputs { 84 submix { 85 sampling_rates 48000 86 channel_masks AUDIO_CHANNEL_OUT_STEREO 87 formats AUDIO_FORMAT_PCM_16_BIT 88 devices AUDIO_DEVICE_OUT_REMOTE_SUBMIX 89 } 90 } 91 inputs { 92 submix { 93 sampling_rates 48000 94 channel_masks AUDIO_CHANNEL_IN_STEREO 95 formats AUDIO_FORMAT_PCM_16_BIT 96 devices AUDIO_DEVICE_IN_REMOTE_SUBMIX 97 } 98 } 99 } 100 hs_usb { 101 outputs { 102 usb_dgtl { 103 sampling_rates 44100|48000 104 channel_masks AUDIO_CHANNEL_OUT_STEREO 105 formats AUDIO_FORMAT_PCM_16_BIT 106 devices AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET 107 } 108 } 109 } 110 usb { 111 outputs { 112 usb_accessory { 113 sampling_rates 44100 114 channel_masks AUDIO_CHANNEL_OUT_STEREO 115 formats AUDIO_FORMAT_PCM_16_BIT 116 devices AUDIO_DEVICE_OUT_USB_ACCESSORY 117 } 118 # usb_device { 119 # sampling_rates 44100 120 # channel_masks AUDIO_CHANNEL_OUT_STEREO 121 # formats AUDIO_FORMAT_PCM_16_BIT 122 # devices AUDIO_DEVICE_OUT_USB_DEVICE 123 # } 124 } 125 } }
从audio_policy.conf文件中可以发现,系统包含了primary、a2dp、usb等音频接口,对应着系统中的audio.<primary/a2dp/usb>.<device>.so。每个音频接口中又包含了若干个outputs & inputs,并且每个output or input又包含了若干个devices,且还有采样频率,声道数等信息。这些devices信息、采样频率信息 & 声道信息等都会保存在各自module的IOProfile中。按上文中audio_policy.conf配置文件所描述,系统最后会生成6个modules(eg.primary,a2dp,hdmi,r_submix,hs_usb & usb)以及7个outputs。以AUDIO_DEVICE_OUT_SPEAKER为例,该device会定义在primary模块中outputs所属的IOProfile1中,其它设备依次类推。
根据audio_policy.conf文件可以简化Audio module的架构,如下图1所示:
图1