android - 在 Android 中使用 libusb 捕获音频

标签 android audio usb audio-recording libusb

我正在开发带有 Android Kitkat 版本的 SBC。 (开发板确实有 USB 主机)。但是由于Android 5.0之后引入了官方的usb音频原生支持,我不能直接在SBC上使用USB音频设备。 我正在使用 libusb 来达到目的。我能够打开特定端点。但是在捕获时获取所有空数据。我在 libusb 调用中没有收到任何错误。

以下是后续步骤的简短伪代码:

libusb_init(NULL)
devId = libusb_open_device_with_vid_pid(NULL, VID, PID)
if(libusb_kernel_driver_active(devId, IFACE_NUM)->detachKernel)
libusb_claim_interface(devId, IFACE_NUM)
libusb_set_interface_alt_setting(devId, IFACE_NUM, ALT_SETTING)
Loop{libusb_alloc_transfer(numOfPackets)
libusb_fill_iso_transfer(callBack)
libusb_set_iso_packet_lengths
libusb_submit_transfer}:NumOfTransfer(10) times
Inside callback: uint8_t* data = libusb_get_iso_packet_buffer_simple()

当我尝试打印数据指向的缓冲区时,所有数据都变为 NULL。当在回调中检查我的 libusb_transfer 结构元素时,它给出的缓冲区长度为 1960,但实际长度为 0。

我正在使用 Logitech C310 网络摄像头,它带有麦克风并充当 USB 音频设备。 我正在使用接口(interface)编号 3 和备用设置 4,因为从设备描述符的下面部分可以清楚地看到

    Interface Descriptor:
------------------------------
0x09    bLength
0x04    bDescriptorType
0x03    bInterfaceNumber
0x04    bAlternateSetting
0x01    bNumEndPoints
0x01    bInterfaceClass   (Audio Device Class)
0x02    bInterfaceSubClass   (Audio Streaming Interface)
0x00    bInterfaceProtocol   
0x00    iInterface

AS Interface Descriptor:
------------------------------
0x07    bLength
0x24    bDescriptorType
0x01    bDescriptorSubtype
0x03    bTerminalLink
0x01    bDelay
0x0001  wFormatTag   (PCM)

AS Format Type 1 Descriptor:
------------------------------
0x0B    bLength
0x24    bDescriptorType
0x02    bDescriptorSubtype
0x01    bFormatType   (FORMAT_TYPE_1)
0x01    bNrChannels   (1 Channels)
0x02    bSubframeSize
0x10    bBitResolution   (16 Bits/sample)
0x01    bSamFreqType   (Discrete sampling frequencies)
0x00BB80    tSamFreq(1)   (48000 Hz)

Endpoint Descriptor (Audio/MIDI):
------------------------------
0x09    bLength
0x05    bDescriptorType
0x86    bEndpointAddress   (IN Endpoint)
0x05    bmAttributes    (Transfer: Isochronous / Synch: Asynchronous / Usage: Data)
0x00C4  wMaxPacketSize   (196 Bytes)
0x01    bInterval
0x00    bRefresh
0x00    bSynchAddress

我没有弄清楚哪里出了问题?任何人都可以分享 Android 较低版本上 USB 音频的任何工作引用吗?

最佳答案

我终于可以让 USB 麦克风在 Kitkat 上工作了。 对于那些在 android Kitkat 版本上苦苦挣扎于 USB 音频的人,下面是我所遵循的内容的简短描述:
我引用了代码 usbaudio-android-demo

此代码是为特定设备编写的,即。 TI 的 PCM2900C 和使用同一设备进行捕获和回放。要使其适用于您的 USB 音频设备,您需要修改 VID、PID、最大数据包大小、接口(interface)编号和正确的备用设置编号,以及能够提供足够带宽的有效端点。一旦能够捕获和播放音频,您需要调整数据包长度、缓冲区中的数据包数量等以获得清晰的音频。

但现在我遇到了另一个问题。我的应用程序不是关于录制音频以及捕获和播放 USB 音频,我需要将此原始 PCM 数据用作电话应用程序中 MIC 的源。我现在唯一能想到的解决方案是通过修改 device/vendor/sub-vendor/hardware/audio/audio_hw.c 将此数据传递给 native 音频代码。这是正确的方法还是更好的解决方案?

关于android - 在 Android 中使用 libusb 捕获音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41044831/

相关文章:

android - 升级到 Android Studio (2020.3.1) Canary 15 导致 Compose 构建错误

javascript - Android 拖动事件不适用于带有 ionic 的 4.4.2

android - 我可以直接从 calllog.calls 提供商处获取照片吗

linux - (Linux)火狐浏览器: audio not working but my system audio works properly

android - 在Android上未调用html5音频对象ondurationchange

linux - 使用 C 代码在接口(interface)上设置 ip

android - 基本 fragment 布局的困难

flash - Flex声音提取并保存mp3

c++ - 桥接 USB 主机到设备

c# - 修复流式 USB 数据中的差距