文章目录
- 乘客音频投放到主音频区
- 动态路由配置
- controlhal实现的具体流程
- control hal AudioGain的callback
- AudioModuleChange变化的通知
乘客音频投放到主音频区
-
场景:
是将乘客区的Media 属性的数据通过主屏区的设备进行播放。具体而言 在副屏user11播放的音乐是输出到主屏user10的设备上,当前只针对usage是media的情况。 -
测试用例:
有相关的测试用例
packages/services/Car/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/AudioUserAssignmentFragment.java -
测试流程:
首先主屏中选择ask user 11 To Accept,选择确认后。 在user 11 中播放音乐, 这个是时候这个音乐是从10种输出。 -
修改
默认支持这种的使用方式。 但是建议media的context 独立使用一个bus address。否则跟media context处于同一个bus address的context都会被路由到主音频区的Media设备中。 -
基本原理:
依赖于audioPolicy的setUserIDDeviceAffinity来实现的。这个接口是更新 之前注册audioPolicyMix的情况。是判断注册的policy中
mAudioPolicy.setUserIdDeviceAffinity(userId, devices);
动态路由配置
- 场景:
在同一个区域中 同一个context可以有两个可以播放的device。可以在播放过程中进行切换。 - 测试用例packages/services/Car/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/AudioTestFragment.java
中的switchToZoneConfigSelected。
只有在副屏中才有这样的测试,能够进行zoneConfig的切换。
在选项中选择了另外一个配置之后,进行音乐播放。这个是对应播放数据的地址变成新的配置。
Button zoneConfigSwitchButton = view.findViewById(R.id.switch_zone_configuration_key_event_button);zoneConfigSwitchButton.setOnClickListener((v) -> switchToZoneConfigSelected());
-
需要添加的配置:
car_audio_configuration: 添加两个zoneconfig。这两个zoneConfig的地址不一样, 只能添加到非primary zone的区域。
比如地址为bus101_audio_zone_1 和bus100_audio_zone_1
audio_policy_configuration: 添加同样的device,mixport等等。 -
实现的原理:
有提供给外部切换的接口。当调用该接口的时候,传递的是要切换的zoneconfig。
这个zoneconfig 中包含的是要切换的zone的信息 包括address 等等。
切换就是建立起userid 和 address的关系, 通过audiopolicy的setUserIdDeviceAffinity来实现。
这个时候在audiopolicyManager 中存储的是userid 和 新的address 的mix。
播放的时候会获取关系就获取的是新的address。
### AudioControl hal需要实现的功能
- registerFocusListener: carAudioService 注册回调到control AudioControl hal 向CarAudioService 请求焦点。
- onAudioFocusChange:CarAudioServie通知control 服务层焦点的变化,在control中请求焦点也会在这里告知。
- requestAudioFocus: hal层向CarAudioService 请求焦点
- abandonAudioFocus:hal层放弃某个焦点的请求。
上述 API 可分别用于从 HAL 请求和放弃音频焦点。作为响应,车载音频服务会考虑音频焦点请求,并将结果异步转发给 IAudioControl#onAudioFocusChange 方法。
- 增益变化的通知
将hal层(可能外部接入设备的音量变化的信息)音量信息通知给服务层,服务层获取这个信息 进行ui的改变等等操作。
这个事实上是提供了一个外部的设备(比如dsp 放大器)等等音频设备和android CarAudioService交互的路径。
interface IAudioControl {/*** Registers callback to be used by HAL for reporting unexpected gain(s)* changed and the reason(s) why.** @param callback The {@link IAudioGainCallback}.*/oneway void registerGainCallback(in IAudioGainCallback callback);
}
interface IAudioGainCallback {/*** Used to indicate that one or more audio device port gains have changed,* i.e. initiated by HAL, not by CarAudioService.* This is the counter part of the* {@link onDevicesToDuckChange}, {@link onDevicesToMuteChange} and,* {@link setAudioDeviceGainsChanged} APIs.** @param reasons List of reasons that triggered the given gains changed.* @param gains List of gains affected by the change.*/void onAudioDeviceGainsChanged(in Reasons[] reasons,in AudioGainConfigInfo[] gains);
}
- carVolumeGroupInfo
CarVolumeGroup 更改的事件类型
CarVolumeGroupEvent。每个事件都包含三种关键类型的信息:
CarVolumeGroupInfo列表
EventTypes (位图)
ExtraInfos列表
controlhal实现的具体流程
carAudioService init的时候会注册上层服务的回调到audioControlHal中。
public void init() {setupHalAudioFocusListenerLocked();setupHalAudioGainCallbackLocked();setupHalAudioModuleChangeCallbackLocked();
}
- 首先是AudioFocus的注册
实现在HalAudioFocus中,是将HalAudioFocus的实现注册到AudioControl当中。在默认的AudioControl.cpp的
实现中。通过这个Listener 向CarAudioSerice请求焦点。
public void registerFocusListener() {mAudioControlWrapper.registerFocusListener(this);}ndk::ScopedAStatus AudioControl::registerFocusListener(const shared_ptr<IFocusListener>& in_listener) {if (in_listener) {std::atomic_store(&mFocusListener, in_listener);} else {LOG(ERROR) << "Unexpected nullptr for listener resulting in no-op.";}}binder_status_t AudioControl::cmdRequestFocus(int fd, const char** args, uint32_t numArgs) {AudioFocusChange focusGain = AudioFocusChange(focusGainValue);if (mFocusListener == nullptr) {dprintf(fd, "Unable to request focus - no focus listener registered\n");return STATUS_BAD_VALUE;}mFocusListener->requestAudioFocus(usage, zoneId, focusGain);dprintf(fd, "Requested focus for usage %s, zoneId %d, and focusGain %d\n", usage.c_str(),zoneId, focusGain);return STATUS_OK;
}
回调到carAudioService中 makeAudioFocusRequestLocked进行焦点的请求。
其调用audioManager的requestAudioFocus请求焦点(这个实际也是回调到CarAudioService进行处理)
同时调用onAuioFocusChange通知到audioControl中。
public void requestAudioFocus(@AttributeUsage int usage, int zoneId, int focusGain) {synchronized (mLock) {AudioAttributesWrapper audioAttributesWrapper =CarAudioContext.getAudioAttributeWrapperFromUsage(usage);HalAudioFocusRequest currentRequest =mHalFocusRequestsByZoneAndUsage.get(zoneId).get(audioAttributesWrapper);if (currentRequest != null) {if (Slogf.isLoggable(TAG, Log.DEBUG)) {Slogf.d(TAG, "A request already exists for zoneId " + zoneId + " and usage "+ usage);}mAudioControlWrapper.onAudioFocusChange(usage, zoneId, currentRequest.mFocusStatus);} else {makeAudioFocusRequestLocked(audioAttributesWrapper, zoneId, focusGain);}}}private void makeAudioFocusRequestLocked(AudioAttributesWrapper audioAttributesWrapper,int zoneId, int focusGain) {AudioFocusRequest audioFocusRequest =generateFocusRequestLocked(audioAttributesWrapper, zoneId, focusGain);int requestResult = mAudioManager.requestAudioFocus(audioFocusRequest);int resultingFocusGain = focusGain;if (requestResult == AUDIOFOCUS_REQUEST_GRANTED) {HalAudioFocusRequest halAudioFocusRequest =new HalAudioFocusRequest(audioFocusRequest, focusGain);mHalFocusRequestsByZoneAndUsage.get(zoneId).put(audioAttributesWrapper, halAudioFocusRequest);} else if (requestResult == AUDIOFOCUS_REQUEST_FAILED) {resultingFocusGain = AUDIOFOCUS_LOSS;} else if (requestResult == AUDIOFOCUS_REQUEST_DELAYED) {Slogf.w(TAG, "Delayed result for request with audio attributes "+ audioAttributesWrapper + ", zoneId " + zoneId+ ", and focusGain " + focusGain);resultingFocusGain = AUDIOFOCUS_LOSS;}mAudioControlWrapper.onAudioFocusChange(audioAttributesWrapper.getAudioAttributes().getSystemUsage(),zoneId, resultingFocusGain);}
总结: CarAudioService 和 control hal之间的交互,CarAudioService注册
HalFocusListener到control。 control 通过listener提供的requestAudioFocus传递usage、zoneID和focus请求的类型
请求焦点,carAudioService响应焦点请求 之后通过onAudioFocusChange通知control hal焦点请求的情况。
control hal AudioGain的callback
有关的实现在CarAudioGainMonitor中, 跟audioFocus的流程是一样的,都是先注册callback到control。 然后control利用这个callback 将
增益变化的信息传递出来。传递的信息包括两个部分:
- Reason 增益变化的原因
- AudioGainConfigInfo 包含zoneID、device address、index。
其中reason的定义见如下。
消息回调到CarAudioGainMonitor中 首先调用如下的函数将hal的reason转换为CarVolumeGroupEvent。
carAudioZone.onAudioGainChanged(reasons, gainsByZones.valueAt(i))
最后是调用的CarVolumeGroup的onAudioGainChanged来实现的。
这个过程会根据当前系统的状态结合gain index信息来 转换最后的EVENT_TYPE。
转换后的event通过下面的回调 回调到carAudioService的onVolumeGroupEvent中、
mCarVolumeInfoWrapper.onVolumeGroupEvent(events);
这里面会回调onGroupVolumeChanged给所有调用registerCarVolumeCallback注册callback的应用
比如VolumeSettingsPreferenceController,收到回调后会调整音量和mute的状态。
private void setupHalAudioGainCallbackLocked() {AudioControlWrapper audioControlWrapper = getAudioControlWrapperLocked();if (!audioControlWrapper.supportsFeature(AUDIOCONTROL_FEATURE_AUDIO_GAIN_CALLBACK)) {Slogf.e(CarLog.TAG_AUDIO, "HalAudioGainCallback is not supported on this device");return;}mCarAudioGainMonitor = new CarAudioGainMonitor(mAudioControlWrapper,new CarVolumeInfoWrapper(this), mCarAudioZones);mCarAudioGainMonitor.registerAudioGainListener(mHalAudioGainCallback);}public void registerAudioGainListener(HalAudioGainCallback callback) {Objects.requireNonNull(callback, "Hal Audio Gain callback can not be null");mAudioControlWrapper.registerAudioGainCallback(callback);}ndk::ScopedAStatus AudioControl::registerGainCallback(const std::shared_ptr<IAudioGainCallback>& in_callback) {LOG(DEBUG) << ": " << __func__;if (in_callback) {std::atomic_store(&mAudioGainCallback, in_callback);} else {LOG(ERROR) << "Unexpected nullptr for audio gain callback resulting in no-op.";}return ndk::ScopedAStatus::ok();
}binder_status_t AudioControl::cmdOnAudioDeviceGainsChanged(int fd, const char** args,uint32_t numArgs) std::vector<AudioGainConfigInfo> agcis{};for (uint32_t i = 3; i < numArgs; i += 2) {std::string deviceAddress = std::string(args[i]);int32_t index;if (!safelyParseInt(std::string(args[i + 1]), &index)) {dprintf(fd, "Non-integer index provided with request: %s\n",std::string(args[i + 1]).c_str());return STATUS_BAD_VALUE;}AudioGainConfigInfo agci{zoneId, deviceAddress, index};agcis.push_back(agci);}mAudioGainCallback->onAudioDeviceGainsChanged(reasons, agcis);dprintf(fd, "Fired audio gain callback for reasons=%s and gains=%s\n",toEnumString(reasons).c_str(), toString(agcis).c_str());return STATUS_OK;
}
public @interface Reasons {
public static final int FORCED_MASTER_MUTE = 1;
public static final int REMOTE_MUTE = 2;
public static final int TCU_MUTE = 4;
public static final int ADAS_DUCKING = 8;
public static final int NAV_DUCKING = 16;
public static final int PROJECTION_DUCKING = 32;
public static final int THERMAL_LIMITATION = 64;
public static final int SUSPEND_EXIT_VOL_LIMITATION = 128;
public static final int EXTERNAL_AMP_VOL_FEEDBACK = 256;
public static final int OTHER = -2147483648;
}
parcelable AudioGainConfigInfo {/*** The identifier for the audio zone the audio device port associated to this gain belongs to.**/int zoneId;/*** The Audio Output Device Port Address.** This is the address that can be retrieved at JAVA layer using the introspection* {@link android.media.AudioManager#listAudioDevicePorts} API then* {@link audio.media.AudioDeviceInfo#getAddress} API.** At HAL layer, it corresponds to audio_port_v7.audio_port_device_ext.address.** Devices that does not have an address will indicate an empty string "".*/String devicePortAddress;/*** UI Index of the corresponding AudioGain in AudioPort.gains.*/int volumeIndex;
}
总结:CarAudioService通过注册HalAudioGainCallback到control、然后control 在有Gain变化的时候。通过调调用listener的onAudioDeviceGainsChanged
将Gain变化的信息传递出来。CarAudioService将event转换为group的event。在传递给应用等等。
AudioModuleChange变化的通知
处理在CarAudioModuleChangeMonitor,通过将HalAudioModuleChangeCallback注册到control中。之后在control中创建一个或多个AudioPort。将这个Port通过callback的onAudioPortsChanged回调回carAudioService。最后通过
CarVolume的onVolumeGroupEvent通知到各个应用。
void setModuleChangeCallback(HalAudioModuleChangeCallback callback) {Objects.requireNonNull(callback, "Hal audio module change callback can not be null");mAudioControlWrapper.setModuleChangeCallback(callback);}ndk::ScopedAStatus AudioControl::setModuleChangeCallback(const std::shared_ptr<IModuleChangeCallback>& in_callback) {std::atomic_store(&mModuleChangeCallback, in_callback);return ndk::ScopedAStatus::ok();
}binder_status_t AudioControl::cmdOnAudioPortsChanged(int fd, const char** args, uint32_t numArgs) {if (!checkCallerHasWritePermissions(fd)) {return STATUS_PERMISSION_DENIED;} mModuleChangeCallback->onAudioPortsChanged(ports);
}