mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-21 20:00:49 +01:00
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.
This commit is contained in:
1 parent
227ea6be27
commit
20d995564d
3 files changed
+81
-1
No files matched your search
@@ -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<Pair<Int, Int>>()
|
||||
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
|
||||
|
||||
@@ -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<String, Any>? {
|
||||
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<Boolean>("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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user