From 20d995564df7317073ca05fc329fc4ecc78e1d62 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:39:11 +0700 Subject: [PATCH] fix(usb): drive a DAC with several USB configurations Direct USB refused any DAC whose raw descriptors held more than one configuration ("Multiple USB configurations are not supported"), so a UAC 2.0 DAC that also offers a UAC 1.0 fallback configuration always fell back to the system mixer. Direct USB now asks the DAC which configuration the kernel set (GET_CONFIGURATION) and passes only that one to the descriptor parser, or the first one when the DAC does not answer, as the kernel picks for a UAC1/UAC2 device. --- .../com/zarz/mornye/UsbDirectFormats.kt | 27 ++++++++++++++ .../com/zarz/mornye/UsbDirectPlayback.kt | 18 ++++++++- .../com/zarz/mornye/UsbDirectFormatsTest.kt | 37 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/kotlin/com/zarz/mornye/UsbDirectFormats.kt b/android/app/src/main/kotlin/com/zarz/mornye/UsbDirectFormats.kt index 50d9d427a..ba3b62acb 100644 --- a/android/app/src/main/kotlin/com/zarz/mornye/UsbDirectFormats.kt +++ b/android/app/src/main/kotlin/com/zarz/mornye/UsbDirectFormats.kt @@ -114,6 +114,33 @@ internal object UsbDirectFormats { return (cookie[offset + 5].toInt() and 255).takeIf { it in 1..32 } } + /** + * Android's raw descriptors (the device descriptor, then every + * configuration in full) cut down to the configuration whose + * bConfigurationValue is [active]. The first configuration stands in + * when [active] is unknown or not listed, as the kernel picks it for a + * UAC1/UAC2 device. A malformed list comes back unchanged for the parser + * to reject. + */ + fun activeConfiguration(raw: ByteArray, active: Int?): ByteArray { + if (raw.size < 2 || raw[1].toInt() != 1) return raw + val head = raw[0].toInt() and 255 + if (head < 2 || head > raw.size) return raw + val blocks = mutableListOf>() + var offset = head + while (offset < raw.size) { + if (offset + 9 > raw.size || (raw[offset].toInt() and 255) < 9 || raw[offset + 1].toInt() != 2) return raw + val total = (raw[offset + 2].toInt() and 255) or ((raw[offset + 3].toInt() and 255) shl 8) + if (total < 9 || offset + total > raw.size) return raw + blocks += offset to offset + total + offset += total + } + if (blocks.size < 2) return raw + val (start, end) = blocks.firstOrNull { (start, _) -> (raw[start + 5].toInt() and 255) == active } + ?: blocks.first() + return raw.copyOfRange(0, head) + raw.copyOfRange(start, end) + } + /** Frames before [targetUs] in a buffer starting at [timeUs], rounded up like the PCM path. */ fun skipFrames(timeUs: Long, targetUs: Long, rate: Int): Long = if (timeUs < targetUs) ((targetUs - timeUs) * rate + 999999) / 1000000 else 0 diff --git a/android/app/src/main/kotlin/com/zarz/mornye/UsbDirectPlayback.kt b/android/app/src/main/kotlin/com/zarz/mornye/UsbDirectPlayback.kt index 865137e78..21c53194d 100644 --- a/android/app/src/main/kotlin/com/zarz/mornye/UsbDirectPlayback.kt +++ b/android/app/src/main/kotlin/com/zarz/mornye/UsbDirectPlayback.kt @@ -5,6 +5,7 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter +import android.hardware.usb.UsbConstants import android.hardware.usb.UsbDevice import android.hardware.usb.UsbDeviceConnection import android.hardware.usb.UsbManager @@ -180,6 +181,13 @@ internal class UsbDirectPlayback(private val context: Context, private val emit: /** Stable across replugging, unlike the device id. */ private fun UsbDevice.declineKey() = "$vendorId:$productId" + /** The bConfigurationValue the kernel set (GET_CONFIGURATION), or null when the DAC does not answer. */ + private fun UsbDeviceConnection.activeConfiguration(): Int? { + val value = ByteArray(1) + val read = controlTransfer(UsbConstants.USB_DIR_IN or UsbConstants.USB_TYPE_STANDARD, 8, 0, 0, value, 1, 1000) + return if (read == 1) (value[0].toInt() and 255).takeIf { it != 0 } else null + } + /** Reads headers only, never the device; null lets preparation decide. */ private fun probeFailure(path: String?): Map? { if (path == null) return null @@ -249,6 +257,14 @@ internal class UsbDirectPlayback(private val context: Context, private val emit: connection = usb deviceId = request.device.deviceId val allowDop = request.call.argument("allowDop") == true + // A DAC with several configurations (UAC2 plus a UAC1 fallback) + // is described by the one the kernel set. + val raw = usb.rawDescriptors + val activeConfiguration = usb.activeConfiguration() + val descriptors = UsbDirectFormats.activeConfiguration(raw, activeConfiguration) + if (descriptors.size != raw.size) { + android.util.Log.i("UsbDirect", "Using USB configuration ${activeConfiguration ?: "listed first"} of several") + } requestedBits = bits // Fallbacks keep every sample exact: a mono sample goes to both // channels of a stereo-only DAC, and a lossy source, which has @@ -265,7 +281,7 @@ internal class UsbDirectPlayback(private val context: Context, private val emit: var refused: Exception? = null for ((outChannels, outBits) in attempts) { try { - opened = UsbDirectOutput.open(usb.fileDescriptor, usb.rawDescriptors, rate.toUInt(), outChannels.toUByte(), outBits.toUByte(), isDsd, allowDop) + opened = UsbDirectOutput.open(usb.fileDescriptor, descriptors, rate.toUInt(), outChannels.toUByte(), outBits.toUByte(), isDsd, allowDop) requestedBits = outBits upmixMono = outChannels != channels break diff --git a/android/app/src/test/kotlin/com/zarz/mornye/UsbDirectFormatsTest.kt b/android/app/src/test/kotlin/com/zarz/mornye/UsbDirectFormatsTest.kt index 856b468e2..d252f66cb 100644 --- a/android/app/src/test/kotlin/com/zarz/mornye/UsbDirectFormatsTest.kt +++ b/android/app/src/test/kotlin/com/zarz/mornye/UsbDirectFormatsTest.kt @@ -145,6 +145,43 @@ class UsbDirectFormatsTest { assertEquals(48000 * 3, UsbDirectFormats.skipFrames(2000000, 5000000, 48000)) } + /** A configuration descriptor with [value] and one interface descriptor of [interfaceClass]. */ + private fun configuration(value: Int, interfaceClass: Int): ByteArray = + byteArrayOf(9, 2, 18, 0, 1, value.toByte(), 0, 0x80.toByte(), 50) + + byteArrayOf(9, 4, 0, 0, 0, interfaceClass.toByte(), 1, 0x20, 0) + + private val deviceDescriptor = ByteArray(18).also { + it[0] = 18 + it[1] = 1 + it[17] = 2 + } + + @Test + fun keepsOnlyTheActiveUsbConfiguration() { + val first = configuration(1, 1) + val second = configuration(2, 0xff) + val raw = deviceDescriptor + first + second + assertArrayEquals(deviceDescriptor + second, UsbDirectFormats.activeConfiguration(raw, 2)) + assertArrayEquals(deviceDescriptor + first, UsbDirectFormats.activeConfiguration(raw, 1)) + // Unknown or unlisted: the first configuration, as the kernel picks for UAC1/UAC2. + assertArrayEquals(deviceDescriptor + first, UsbDirectFormats.activeConfiguration(raw, null)) + assertArrayEquals(deviceDescriptor + first, UsbDirectFormats.activeConfiguration(raw, 3)) + } + + @Test + fun leavesSingleAndMalformedUsbConfigurationsUnchanged() { + val single = deviceDescriptor + configuration(1, 1) + assertArrayEquals(single, UsbDirectFormats.activeConfiguration(single, 1)) + assertArrayEquals(single, UsbDirectFormats.activeConfiguration(single, null)) + // wTotalLength runs past the end. + val truncated = deviceDescriptor + configuration(1, 1) + configuration(2, 1).copyOf(12) + assertArrayEquals(truncated, UsbDirectFormats.activeConfiguration(truncated, 2)) + // Something other than a configuration follows the device descriptor. + val stray = deviceDescriptor + configuration(1, 1) + byteArrayOf(9, 4, 0, 0, 0, 1, 1, 0, 0) + assertArrayEquals(stray, UsbDirectFormats.activeConfiguration(stray, 1)) + assertArrayEquals(ByteArray(0), UsbDirectFormats.activeConfiguration(ByteArray(0), 1)) + } + @Test fun readsAlacBitDepthFromBareAndBoxedCookies() { val cookie = ByteArray(24).also {