Android9.0 USB MIC从识别到选择的流程framework层记录
使用的平台是RK3399,Android9.0
主要修改应用:
通过相关系统打印得知,当Usb MIC插入系统时,会触发
UsbAlsaManager.java里面的
void usbDeviceAdded(String deviceAddress, UsbDevice usbDevice,UsbDescriptorParser parser)
void usbDeviceAdded(String deviceAddress, UsbDevice usbDevice,
UsbDescriptorParser parser) {
if (DEBUG) {
Slog.d(TAG, "usbDeviceAdded(): " + usbDevice.getManufacturerName()
+ " nm:" + usbDevice.getProductName());
}
// Scan the Alsa File Space
mCardsParser.scan(); //对Alsa的声卡文件扫描解析
// Find the ALSA spec for this device address//查找此设备地址的ALSA规范
AlsaCardsParser.AlsaCardRecord cardRec =
mCardsParser.findCardNumFor(deviceAddress);
if (cardRec == null) {
return;
}
//通过解析判断USB 声卡设备是否有输入输出等标记
//可以通过在此获取USB 声卡的pid以及vid对声卡进行划分判断优先级
//***" PID: "+usbDevice.getProductId()+" VID: "+usbDevice.getVendorId())***
// Add it to the devices list
boolean hasInput = parser.hasInput();
boolean hasOutput = parser.hasOutput();
if (DEBUG) {
Slog.d(TAG, "hasInput: " + hasInput + " hasOutput:" + hasOutput);
}
if (hasInput || hasOutput) {
boolean isInputHeadset = parser.isInputHeadset();
boolean isOutputHeadset = parser.isOutputHeadset();
if (mAudioService == null) {
Slog.e(TAG, "no AudioService");
return;
}
//新建声卡设备对象
UsbAlsaDevice alsaDevice =
new UsbAlsaDevice(mAudioService, cardRec.getCardNum(), 0 /*device*/,
deviceAddress, hasOutput, hasInput,
isInputHeadset, isOutputHeadset);
if (alsaDevice != null) {
alsaDevice.setDeviceNameAndDescription(
cardRec.getCardName(), cardRec.getCardDescription());
//加入到声卡设备的集合列表中
mAlsaDevices.add(0, alsaDevice);
//选择声卡设备
selectAlsaDevice(alsaDevice);
}
}
..........
if (DEBUG) {
Slog.d(TAG, "deviceAdded() - done");
}
}
最后通过selectAlsaDevice(alsaDevice)进行对USB声卡的选择
/**
* Select the AlsaDevice to be used for AudioService.
* AlsaDevice.start() notifies AudioService of it's connected state.
*
* @param alsaDevice The selected UsbAlsaDevice for system USB audio.
*/
private synchronized void selectAlsaDevice(UsbAlsaDevice alsaDevice) {
if (DEBUG) {
Slog.d(TAG, "selectAlsaDevice " + alsaDevice);
}
if (mSelectedDevice != null) {
deselectAlsaDevice();
}
// FIXME Does not yet handle the case where the setting is changed
// after device connection. Ideally we should handle the settings change
// in SettingsObserver. Here we should log that a USB device is connected
// and disconnected with its address (card , device) and force the
// connection or disconnection when the setting changes.
int isDisabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 0);
if (isDisabled != 0) {
return;
}
//最终调用到alsaDevice.start()对声卡进行启动
mSelectedDevice = alsaDevice;
alsaDevice.start();
}
可以在声卡插入时做优先级判断,同时拔出声卡是也需要做些判断,usbDeviceRemoved()
/* package */ synchronized void usbDeviceRemoved(String deviceAddress/*UsbDevice usbDevice*/