本文主要是介绍安卓框架,新增usb mic device 的框架修改,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://blog.csdn.net/u013762045/article/details/82226382
新增usb mic还涉及到了音频部分,先看下audio_policy.conf
usb {
outputs {
usb_accessory {
sampling_rates 44100
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_USB_ACCESSORY
}
usb_device {
sampling_rates dynamic
channel_masks dynamic
formats dynamic
devices AUDIO_DEVICE_OUT_USB_DEVICE
}
}
inputs {
usb_device {
sampling_rates dynamic
channel_masks dynamic
formats dynamic
devices AUDIO_DEVICE_IN_USB_DEVICE
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
关注点:
1.比如你的android手机做host,要获得slave,用UsbDevice表示slave
要是你的android手机做slave,要获得host,用UsbAccessory表示host
2.输入输出设备必须是audio.h中列出的枚举类型
3”channel_masks”、”formats”、”devices”和”flags”必须为audio.h、audio_policy.h中定义的枚
举值,它们可以通过|连接,但不能包含空格和”\n”。
接着模仿新增一个usb
usb2 {
//其它不做说明
inputs {
usb_device {
sampling_rates dynamic
channel_masks dynamic
formats dynamic
devices AUDIO_DEVICE_IN_USB_DEVICE |AUDIO_DEVICE_IN_USB_DEVICE_TYPE2
}
}
}
1
2
3
4
5
6
7
8
9
10
11
之前说devices 在audio.h、audio_policy.h中有对应,一搜AUDIO_DEVICE_IN_USB_DEVICE 果然在audio.h
audio.h
AUDIO_DEVICE_IN_USB_DEVICE = AUDIO_DEVICE_BIT_IN | 0x1000,
在后面新增
AUDIO_DEVICE_IN_USB_DEVICE_TYPE2= AUDIO_DEVICE_BIT_IN | 0x80,
1
2
3
4
5
AudioPolicyManager.cpp文件会加载audio_policy.conf,也需新增参数
const StringToEnum sDeviceNameToEnumTable[] = {
STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
// 新增AUDIO_DEVICE_IN_USB_DEVICE_TYPE2
STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE_TYPE2),
}
audio_devices_t AudioPolicyManager::getDeviceForInputSource 中新增判断AUDIO_DEVICE_IN_USB_DEVICE_TYPE2的方法,模仿AUDIO_DEVICE_IN_USB_DEVICE 的做法就对了
1
2
3
4
5
6
7
8
9
10
11
12
AudioSystem.java 也有对应的比特位
// reserved bits
public static final int DEVICE_BIT_IN = 0x80000000;
对应audio.h的
/* reserved bits */
AUDIO_DEVICE_BIT_IN = 0x80000000,
AUDIO_DEVICE_IN_USB_DEVICE_TYPE2= AUDIO_DEVICE_BIT_IN | 0x80,
所以java端的新增是
AUDIO_DEVICE_IN_USB_DEVICE_TYPE2 = DEVICE_BIT_IN | 0x80,
编译成so库
android-5.1.1/hardware/libhardware/modules/usbaudio/Android.mk
LOCAL_MODULE := audio.usb.default
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_SRC_FILES := \
audio_hw.c \
alsa_device_profile.c \
alsa_device_proxy.c \
logging.c \
format.c
LOCAL_C_INCLUDES += \
external/tinyalsa/include \
$(call include-path-for, audio-utils)
LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa libaudioutils
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS := -Wno-unused-parameter
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := audio.usb2.default
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_SRC_FILES := \
audio_hw.c \
alsa_device_profile.c \
alsa_device_proxy.c \
logging.c \
format.c
LOCAL_C_INCLUDES += \
external/tinyalsa/include \
$(call include-path-for, audio-utils)
LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa libaudioutils
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS := -Wno-unused-parameter
include $(BUILD_SHARED_LIBRARY)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
最后不要忘记
android-5.1.1/device/xxxx/xxxx/device.mk
PRODUCT_PACKAGES += \
audio.a2dp.default \
audio.usb.default \
audio.usb2.default \
1
2
3
4
5
6
7
8
9
10
11
12
补充:
判断声卡的方法
UsbAudioManager.java void deviceAdded 这里会调用区分声卡的方法
AlsaDevicesParser.java
public void scan() {
deviceRecords_.clear();
final String devicesFilePath = "/proc/asound/devices";
File devicesFile = new File(devicesFilePath);
try {
FileReader reader = new FileReader(devicesFile);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = "";
while ((line = bufferedReader.readLine()) != null) {
if (isLineDeviceRecord(line)) {
AlsaDeviceRecord deviceRecord = new AlsaDeviceRecord();
deviceRecord.parse(line);
deviceRecords_.add(deviceRecord);
}
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
涉及到的类
android-5.1.1/device/XXX/XXX/audio_policy.conf
android-5.1.1/device/XXXX/XXXX/device.mk
android-5.1.1/frameworks/av/services/audiopolicy/AudioPolicyManager.cpp
android-5.1.1/frameworks/base/media/java/android/media/AudioService.java
android-5.1.1/frameworks/base/media/java/android/media/AudioSystem.java
android-5.1.1/frameworks/base/services/usb/java/com/android/server/usb/UsbAudioManager.java
android-5.1.1/hardware/libhardware/modules/usbaudio/Android.mk
android-5.1.1/system/core/include/system/audio.h
---------------------
作者:1991415
来源:CSDN
原文:https://blog.csdn.net/u013762045/article/details/82226382
版权声明:本文为博主原创文章,转载请附上博文链接!
这篇关于安卓框架,新增usb mic device 的框架修改的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!