1.CarAudioService 的init函数
mUseDynamicRouting变量一般定制会在device overlay目录进行覆盖定制,默认为false,不启动动态路由。在当前环境下,我们需要启动动态路由,所以设置该变量为true。
public void init() {
synchronized (mImplLock) {
if (!mUseDynamicRouting) {
Log.i(CarLog.TAG_AUDIO, "Audio dynamic routing not configured, run in legacy mode");
setupLegacyVolumeChangedListener();
} else {
setupDynamicRouting();
setupVolumeGroups();
}
}
}
2.在setupDynamicRouting中首先获取路由策略,然后调用AudioManager注册路由策略
private void setupDynamicRouting() {
final IAudioControl audioControl = getAudioControl();
if (audioControl == null) {
return;
}
//1. 获取路由策略
AudioPolicy audioPolicy = getDynamicAudioPolicy(audioControl);
//2.注册路由策略
int r = mAudioManager.registerAudioPolicy(audioPolicy);
if (r != AudioManager.SUCCESS) {
throw new RuntimeException("registerAudioPolicy failed " + r);
}
mAudioPolicy = audioPolicy;
}
2.1获取路由策略
@Nullable
private AudioPolicy getDynamicAudioPolicy(@NonNull IAudioControl audioControl) {
AudioPolicy.Builder builder = new AudioPolicy.Builder(mContext);
builder.setLooper(Looper.getMainLooper());
// 1st, enumerate all output bus device ports,获取所有的device端口
//该端口信息在/frameworks/av/services/audiopolicy/managerdefault/AudioPolicyManager.cpp初始化去读取audio_policy_configuration.xml
//该xml描述了输出设备的代号BusNumber和设备的type
//如果type==TYPE_BUS,则把设备信息存储在mCarAudioDeviceInfos容器
AudioDeviceInfo[] deviceInfos = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
if (deviceInfos.length == 0) {
Log.e(CarLog.TAG_AUDIO, "getDynamicAudioPolicy, no output device available, ignore");
return null;
}
for (AudioDeviceInfo info : deviceInfos) {
Log.v(CarLog.TAG_AUDIO, String.format("output id=%d address=%s type=%s",
info.getId(), info.getAddress(), info.getType()));
if (info.getType() == AudioDeviceInfo.TYPE_BUS) {
final CarAudioDeviceInfo carInfo = new CarAudioDeviceInfo(info);
// See also the audio_policy_configuration.xml and getBusForContext in
// audio control HAL, the bus number should be no less than zero.
if (carInfo.getBusNumber() >= 0) {
mCarAudioDeviceInfos.put(carInfo.getBusNumber(), carInfo);
Log.i(CarLog.TAG_AUDIO, "Valid bus found " + carInfo);
}
}
}
// 2nd, map context to physical bus,完成context到bus的匹配
//也就是将CONTEXT_NUMBERS与输出设备代码bus绑定,并将对应关系放在mContextToBus
//在初始化的时候,CarAudioService会通过USAGE_TO_CONTEXT将USAGE和CONTEXT对应,这样就完成了USAGE -》CONTEXT-》BUS(OUTPUT)的对应关系
try {
for (int contextNumber : CONTEXT_NUMBERS) {
int busNumber = audioControl.getBusForContext(contextNumber);
mContextToBus.put(contextNumber, busNumber);
CarAudioDeviceInfo info = mCarAudioDeviceInfos.get(busNumber);
if (info == null) {
Log.w(CarLog.TAG_AUDIO, "No bus configured for context: " + contextNumber);
}
}
} catch (RemoteException e) {
Log.e(CarLog.TAG_AUDIO, "Error mapping context to physical bus", e);
}
// 3rd, enumerate all physical buses and build the routing policy.
// Note that one can not register audio mix for same bus more than once.
//建立路由,通过context完成usage到bus的路由建立
//按照OUTPUT-》CONTEXT-》USAGE的顺序找到每个usage,并为每个OUTPUT新建AudioFormat和AudioMixingRule
//根据AudioFormat和AudioMixingRule创建AudioMix,把mix放人AudioPolicy
for (int i = 0; i < mCarAudioDeviceInfos.size(); i++) {
int busNumber = mCarAudioDeviceInfos.keyAt(i);
boolean hasContext = false;
CarAudioDeviceInfo info = mCarAudioDeviceInfos.valueAt(i);
AudioFormat mixFormat = new AudioFormat.Builder()
.setSampleRate(info.getSampleRate())
.setEncoding(info.getEncodingFormat())
.setChannelMask(info.getChannelCount())
.build();
AudioMixingRule.Builder mixingRuleBuilder = new AudioMixingRule.Builder();
for (int j = 0; j < mContextToBus.size(); j++) {
if (mContextToBus.valueAt(j) == busNumber) {
hasContext = true;
int contextNumber = mContextToBus.keyAt(j);
int[] usages = getUsagesForContext(contextNumber);
for (int usage : usages) {
mixingRuleBuilder.addRule(
new AudioAttributes.Builder().setUsage(usage).build(),
AudioMixingRule.RULE_MATCH_ATTRIBUTE_USAGE);
}
Log.i(CarLog.TAG_AUDIO, "Bus number: " + busNumber
+ " contextNumber: " + contextNumber
+ " sampleRate: " + info.getSampleRate()
+ " channels: " + info.getChannelCount()
+ " usages: " + Arrays.toString(usages));
}
}
if (hasContext) {
// It's a valid case that an audio output bus is defined in
// audio_policy_configuration and no context is assigned to it.
// In such case, do not build a policy mix with zero rules.
// 构造一个AudioMix对象,这个对象包含了所有的usage对应关系和device所需的信息,这样就建立了一条音频路由策略
// 最后会注册到动态路由中去(mAudioManager.registerAudioPolicy)
AudioMix audioMix = new AudioMix.Builder(mixingRuleBuilder.build())
.setFormat(mixFormat)
.setDevice(info.getAudioDeviceInfo())
.setRouteFlags(AudioMix.ROUTE_FLAG_RENDER)
.build();
builder.addMix(audioMix);
}
}
// 4th, attach the {@link AudioPolicyVolumeCallback}
// 设置音量的回调函数,监听音量的变化
builder.setAudioPolicyVolumeCallback(mAudioPolicyVolumeCallback);
return builder.build();
}
AudioPolicy是Java层音频策略的总管,可以通过setAudioPolicyFocusListener和setIsAudioFocusPolicy来自定义音频焦点策略也就是说,如果我们要使用外部焦点策略,我们需要在registerAudioPolicy被调用前,audioPolicy还没完成构造的时候完成一下两步:
- 调用 audioPolicy内部类builder提供的函数builder.setAudioPolicyFocusListener(mFocusHandler)设置自己的mFocusListener,注意,这里的mFocusListener必须继承AudioPolicy.AudioPolicyFocusListener
- 调用 audioPolicy内部类builder提供的函数builder.setIsAudioFocusPolicy(true)设置mIsFocusPolicy为true,表示audioPolicy将要使用外部焦点策略