Android的Audio系统分为两大块,一块是audio的策略管理,即Audio Policy模块;一块是AudioFlinger模块,负责和Audio Hardware Module直接交互。
本文主要讲解Audio Policy模块的以下知识点:
1. audio policy策略文件的加载;
2. 创建AudioRecord的时候,如何根据intput source选择合适的device;
3. 创建AudioTrack的时候,如何根据output stream选择合适的device;
首先讲第一点。
audio policy策略文件即:/system/etc/audio_policy.conf 或者/vendor/etc/audio_policy.conf。在AudioPolicyManagerBase的构造函数中会加载和分析这个配置文件。
那么AudioPolicyManagerBase又是在何时构造的呢?
AudioPolicyService构造函数中会加载audio policy的HAL Module。AudioPolicyService与 audio policy module的组件图如下:
下面是一个audio_policy.conf文件的部分内容:
audio_hw_modules {
primary {
outputs {
primary {
sampling_rates 44100
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE|AUDIO_DEVICE_OUT_AUX_DIGITAL
flags AUDIO_OUTPUT_FLAG_PRIMARY
}
hdmi {
sampling_rates dynamic
channel_masks dynamic
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_AUX_DIGITAL
flags AUDIO_OUTPUT_FLAG_DIRECT
}
}
inputs {
primary {
sampling_rates 8000|11025|16000|22050|24000|32000|44100|48000
channel_masks AUDIO_CHANNEL_IN_MONO|AUDIO_CHANNEL_IN_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_USB_DEVICE
}
}
}
}
audio_hw_modules节点表示系统中包含的audio hardware module。每个子节点都对应一个 hal so。比如primary对应着audio.primary..so。primary可以理解为一个audio interface。
一个audio interface模块有输入模块和输入模块,在文件中的outputs和inputs中定义。primary 部分解析完毕后对应于抽象的class HwModule 。
class HwModule {
public:
HwModule(const char *name);
~HwModule();
void dump(int fd);
const char *const mName; // base name of the audio HW module (primary, a2dp ...)
audio_module_handle_t mHandle;
Vector mOutputProfiles; // output profiles exposed by this module
Vector mInputProfiles; // input profiles exposed by this module
};
typedef enum {
AUDIO_SOURCE_DEFAULT = 0,
AUDIO_SOURCE_MIC = 1,
AUDIO_SOURCE_VOICE_UPLINK = 2,
AUDIO_SOURCE_VOICE_DOWNLINK = 3,
AUDIO_SOURCE_VOICE_CALL = 4,
AUDIO_SOURCE_CAMCORDER = 5,
AUDIO_SOURCE_VOICE_RECOGNITION = 6,
AUDIO_SOURCE_VOICE_COMMUNICATION = 7,
AUDIO_SOURCE_REMOTE_SUBMIX = 8, /* Source for the mix to be presented remotely. */
/* An example of remote presentation is Wifi Display */
/* where a dongle attached to a TV can be used to */
/* play the mix captured by this audio source. */
AUDIO_SOURCE_USB_DEVICE_1 = 9,
AUDIO_SOURCE_USB_DEVICE_2 = 10,
AUDIO_SOURCE_CNT,
AUDIO_SOURCE_MAX = AUDIO_SOURCE_CNT - 1,
} audio_source_t;