refactor(alsa): replace aplay with /proc/asound reads

Remove dependency on alsa-utils package by reading device information
directly from /proc/asound filesystem:

- /proc/asound/cards for card list with names
- /proc/asound/cardX/ for detailed card info
- /proc/asound/cardX/pcmXp/ for PCM device info

This addresses Flathub reviewer feedback about not using CLI tools
when API alternatives exist. The /proc/asound interface provides
all the information we need for device enumeration and descriptions.

Changes:
- alsa_backend.rs: New enumerate_with_proc_descriptions() using /proc
- audio_backends.rs: Remove aplay calls from DAC capabilities query
- check_alsa_utils_installed() now always returns true (no longer needed)
This commit is contained in:
vicrodh committed 2026-02-13 10:30:42 -06:00
1 parent d3a4e4a688
commit 0e6b1fc686
2 files changed
+289 -309

No files matched your search

+261 -243
View File
@@ -6,6 +6,7 @@
//! - Low-latency audio output //! - Low-latency audio output
//! //!
//! Uses CPAL's ALSA host with specific device selection. //! Uses CPAL's ALSA host with specific device selection.
//! Device enumeration reads directly from /proc/asound (no alsa-utils dependency).
use super::backend::{AlsaPlugin, AudioBackend, AudioBackendType, AudioDevice, BackendConfig, BackendResult}; use super::backend::{AlsaPlugin, AudioBackend, AudioBackendType, AudioDevice, BackendConfig, BackendResult};
use rodio::{ use rodio::{
@@ -15,7 +16,8 @@ use rodio::{
}, },
OutputStream, OutputStreamHandle, OutputStream, OutputStreamHandle,
}; };
use std::process::Command; use std::collections::HashMap;
use std::fs;
/// Common audio sample rates to check for device support /// Common audio sample rates to check for device support
const COMMON_SAMPLE_RATES: &[u32] = &[ const COMMON_SAMPLE_RATES: &[u32] = &[
@@ -60,6 +62,181 @@ fn get_supported_sample_rates(device: &rodio::cpal::Device) -> Option<Vec<u32>>
} }
} }
// ============================================================================
// /proc/asound helpers - No aplay dependency
// ============================================================================
/// Information about an ALSA sound card read from /proc/asound
#[derive(Debug, Clone)]
struct ProcCardInfo {
/// Card number (0, 1, 2, ...)
number: String,
/// Short name used in ALSA device IDs (e.g., "C20", "NVidia", "sofhdadsp")
short_name: String,
/// Long descriptive name (e.g., "Cambridge Audio USB Audio 2.0")
long_name: String,
/// PCM playback devices on this card
pcm_playback_devices: Vec<ProcPcmInfo>,
}
/// Information about a PCM device
#[derive(Debug, Clone)]
struct ProcPcmInfo {
/// Device number within the card
device_num: String,
/// Device name (e.g., "USB Audio", "HDMI 0")
name: String,
}
/// Read all sound card information from /proc/asound
fn read_proc_asound_cards() -> Vec<ProcCardInfo> {
let mut cards = Vec::new();
// Parse /proc/asound/cards for basic card info
// Format: " 0 [C20 ]: USB-Audio - Cambridge Audio USB Audio 2.0"
let cards_content = match fs::read_to_string("/proc/asound/cards") {
Ok(c) => c,
Err(e) => {
log::warn!("[ALSA] Cannot read /proc/asound/cards: {}", e);
return cards;
}
};
// Parse cards file - each card has two lines
let lines: Vec<&str> = cards_content.lines().collect();
let mut i = 0;
while i < lines.len() {
let line = lines[i].trim();
// First line format: " 0 [C20 ]: USB-Audio - Cambridge Audio USB Audio 2.0"
if let Some(card_info) = parse_proc_card_line(line) {
// Read PCM devices for this card
let pcm_devices = read_card_pcm_devices(&card_info.0);
cards.push(ProcCardInfo {
number: card_info.0,
short_name: card_info.1,
long_name: card_info.2,
pcm_playback_devices: pcm_devices,
});
}
i += 1;
}
cards
}
/// Parse a line from /proc/asound/cards
/// Returns (card_number, short_name, long_name)
fn parse_proc_card_line(line: &str) -> Option<(String, String, String)> {
// Format: " 0 [C20 ]: USB-Audio - Cambridge Audio USB Audio 2.0"
let line = line.trim();
// Find card number (first number)
let parts: Vec<&str> = line.splitn(2, '[').collect();
if parts.len() < 2 {
return None;
}
let card_num = parts[0].trim().to_string();
if card_num.is_empty() || !card_num.chars().all(|c| c.is_ascii_digit()) {
return None;
}
// Find short name (inside brackets)
let rest = parts[1];
let bracket_end = rest.find(']')?;
let short_name = rest[..bracket_end].trim().to_string();
// Find long name (after " - ")
let long_name = if let Some(dash_pos) = rest.find(" - ") {
rest[dash_pos + 3..].trim().to_string()
} else {
// Fallback: use everything after ]:
rest[bracket_end + 1..]
.trim()
.trim_start_matches(':')
.trim()
.split(" - ")
.last()
.unwrap_or(&short_name)
.to_string()
};
Some((card_num, short_name, long_name))
}
/// Read PCM playback devices for a specific card from /proc/asound
fn read_card_pcm_devices(card_num: &str) -> Vec<ProcPcmInfo> {
let mut devices = Vec::new();
let card_path = format!("/proc/asound/card{}", card_num);
// Read PCM device info files
if let Ok(entries) = fs::read_dir(&card_path) {
for entry in entries.flatten() {
let name = entry.file_name();
let name_str = name.to_string_lossy();
// PCM playback devices are named pcmXp (X = device number, p = playback)
if name_str.starts_with("pcm") && name_str.ends_with('p') {
let info_path = entry.path().join("info");
if let Ok(content) = fs::read_to_string(&info_path) {
let mut pcm_name = String::new();
let mut device_num = String::new();
for line in content.lines() {
if let Some(val) = line.strip_prefix("name: ") {
pcm_name = val.trim().to_string();
}
if let Some(val) = line.strip_prefix("device: ") {
device_num = val.trim().to_string();
}
}
if !device_num.is_empty() {
devices.push(ProcPcmInfo {
device_num,
name: if pcm_name.is_empty() { "Unknown".to_string() } else { pcm_name },
});
}
}
}
}
}
// Sort by device number
devices.sort_by(|a, b| {
a.device_num.parse::<u32>().unwrap_or(0)
.cmp(&b.device_num.parse::<u32>().unwrap_or(0))
});
devices
}
/// Build a map of card_number -> (short_name, long_name) from /proc/asound
fn build_card_info_map() -> HashMap<String, (String, String)> {
let cards = read_proc_asound_cards();
let mut map = HashMap::new();
for card in cards {
map.insert(card.number.clone(), (card.short_name, card.long_name));
}
map
}
/// Find card number by short name (e.g., "C20" -> "0")
fn find_card_number_by_name(short_name: &str) -> Option<String> {
let cards = read_proc_asound_cards();
cards.iter()
.find(|c| c.short_name == short_name)
.map(|c| c.number.clone())
}
// ============================================================================
// ALSA Backend Implementation
// ============================================================================
pub struct AlsaBackend { pub struct AlsaBackend {
host: rodio::cpal::Host, host: rodio::cpal::Host,
} }
@@ -87,136 +264,51 @@ impl AlsaBackend {
Ok(Self { host }) Ok(Self { host })
} }
/// Enumerate ALSA devices via CPAL with descriptions from aplay -L /// Enumerate ALSA devices using /proc/asound for descriptions
fn enumerate_via_aplay(&self) -> BackendResult<Vec<AudioDevice>> { fn enumerate_with_proc_descriptions(&self) -> BackendResult<Vec<AudioDevice>> {
// First: Get devices from CPAL (these are the device IDs that actually work) // First: Get devices from CPAL (these are the device IDs that actually work)
let cpal_devices_result = self.enumerate_via_cpal(); let mut devices = self.enumerate_via_cpal()?;
if cpal_devices_result.is_err() {
return cpal_devices_result;
}
let mut devices = cpal_devices_result.unwrap();
// Second: Build description map from aplay -L // Second: Read card info from /proc/asound
let output = Command::new("aplay") let cards = read_proc_asound_cards();
.arg("-L")
.output()
.map_err(|e| format!("Failed to run aplay -L: {}", e))?;
if !output.status.success() { // Build description map: short_name -> long_name
log::warn!("[ALSA Backend] aplay -L failed, using CPAL names only"); let mut card_descriptions: HashMap<String, String> = HashMap::new();
return Ok(devices); let mut card_num_to_short_name: HashMap<String, String> = HashMap::new();
for card in &cards {
card_descriptions.insert(card.short_name.clone(), card.long_name.clone());
card_num_to_short_name.insert(card.number.clone(), card.short_name.clone());
log::debug!(
"[ALSA Backend] Card {}: {} = {}",
card.number, card.short_name, card.long_name
);
} }
let stdout = String::from_utf8_lossy(&output.stdout); // Third: Update device descriptions
let lines: Vec<&str> = stdout.lines().collect(); // Match by card name in device ID (e.g., "sysdefault:CARD=C20" contains "C20")
// Build map: device_id -> friendly description
let mut description_map = std::collections::HashMap::new();
let mut i = 0;
while i < lines.len() {
let line = lines[i].trim_end();
// Device ID lines don't start with whitespace
if !line.starts_with(' ') && !line.is_empty() {
let device_id = line.to_string();
// Get description from next line (if it exists and is indented)
let description = if i + 1 < lines.len() && lines[i + 1].starts_with(' ') {
Some(lines[i + 1].trim().to_string())
} else {
None
};
if let Some(desc) = description {
description_map.insert(device_id, desc);
}
// Skip description lines
i += 1;
while i < lines.len() && lines[i].starts_with(' ') {
i += 1;
}
} else {
i += 1;
}
}
// Build card number -> (short_name, description) map from aplay -l
// Short name is used for stable device IDs (front:CARD=name,DEV=0)
// Description is the human-readable full name
let mut card_map = std::collections::HashMap::new();
let mut card_num_to_short_name = std::collections::HashMap::new();
let aplay_l_output = Command::new("aplay")
.arg("-l")
.output()
.ok()
.and_then(|o| if o.status.success() { Some(o) } else { None });
if let Some(output) = aplay_l_output {
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if let Some(card_info) = line.strip_prefix("card ") {
// Parse: "card 4: C20 [Cambridge Audio USB Audio 2.0], device 0: ..."
let parts: Vec<&str> = card_info.splitn(2, ':').collect();
if parts.len() == 2 {
let card_num = parts[0].trim();
let rest = parts[1].trim();
// Extract short name (before bracket) and description (inside bracket)
// "C20 [Cambridge Audio USB Audio 2.0], device 0: ..."
let short_name = rest.split_whitespace().next().unwrap_or("Unknown");
if let Some(start) = rest.find('[') {
if let Some(end) = rest.find(']') {
let card_desc = &rest[start + 1..end];
card_map.insert(card_num.to_string(), card_desc.to_string());
card_num_to_short_name.insert(card_num.to_string(), short_name.to_string());
}
}
}
}
}
}
// Third: Update device descriptions from aplay -L map
for device in &mut devices { for device in &mut devices {
if let Some(desc) = description_map.get(&device.name) { for (short_name, long_name) in &card_descriptions {
device.description = Some(desc.clone()); if device.name.contains(short_name) {
device.description = Some(format!("{}, {}", long_name, device.name));
break;
}
} }
} }
// Fourth: Add hw:X,Y and plughw:X,Y devices manually (CPAL doesn't enumerate these) // Fourth: Add hw:X,Y and plughw:X,Y devices manually (CPAL doesn't enumerate these)
// Parse aplay -l to create these devices for card in &cards {
for (card_num, card_desc) in &card_map { for pcm in &card.pcm_playback_devices {
// Create hw:X,0 device (bit-perfect direct hardware access) // Create hw:X,Y device (bit-perfect direct hardware access)
let hw_device_id = format!("hw:{},0", card_num); let hw_device_id = format!("hw:{},{}", card.number, pcm.device_num);
// Test if CPAL can open this device // Check if we already have this device
let can_open = self.host let already_exists = devices.iter().any(|d| d.name == hw_device_id);
.output_devices() if already_exists {
.ok() continue;
.and_then(|mut devs| devs.find(|d| d.name().ok().as_deref() == Some(&hw_device_id)))
.is_some();
if !can_open {
// Try to get supported configs to validate device exists
// Create a test device to check if it's valid
if let Ok(_test_devices) = self.host.output_devices() {
// CPAL may not enumerate hw: devices but can still open them
// We'll add them anyway and let the backend handle errors
devices.push(AudioDevice {
id: hw_device_id.clone(),
name: hw_device_id.clone(),
description: Some(format!("{} (Direct Hardware - Bit-perfect)", card_desc)),
is_default: false,
max_sample_rate: Some(384000), // Assume high sample rate capability
supported_sample_rates: None, // Can't detect without CPAL access
device_bus: None,
is_hardware: true,
});
} }
} else {
// CPAL found it, get real sample rate info // Test if CPAL can open this device
let cpal_device = self.host let cpal_device = self.host
.output_devices() .output_devices()
.ok() .ok()
@@ -240,70 +332,54 @@ impl AlsaBackend {
devices.push(AudioDevice { devices.push(AudioDevice {
id: hw_device_id.clone(), id: hw_device_id.clone(),
name: hw_device_id.clone(), name: hw_device_id.clone(),
description: Some(format!("{} (Direct Hardware - Bit-perfect)", card_desc)), description: Some(format!("{} - {} (Direct Hardware - Bit-perfect)", card.long_name, pcm.name)),
is_default: false, is_default: false,
max_sample_rate, max_sample_rate: max_sample_rate.or(Some(384000)),
supported_sample_rates, supported_sample_rates,
device_bus: None, device_bus: None,
is_hardware: true, is_hardware: true,
}); });
}
// Also create plughw:X,0 device (plugin hardware with auto-conversion) // Also create plughw:X,Y device (plugin hardware with auto-conversion)
let plughw_device_id = format!("plughw:{},0", card_num); let plughw_device_id = format!("plughw:{},{}", card.number, pcm.device_num);
devices.push(AudioDevice { if !devices.iter().any(|d| d.name == plughw_device_id) {
id: plughw_device_id.clone(), devices.push(AudioDevice {
name: plughw_device_id.clone(), id: plughw_device_id.clone(),
description: Some(format!("{} (Plugin Hardware)", card_desc)), name: plughw_device_id.clone(),
is_default: false, description: Some(format!("{} - {} (Plugin Hardware)", card.long_name, pcm.name)),
max_sample_rate: Some(384000), is_default: false,
supported_sample_rates: None, // plughw can convert, so all rates are "supported" max_sample_rate: Some(384000),
device_bus: None, supported_sample_rates: None, // plughw can convert, so all rates are "supported"
is_hardware: true, device_bus: None,
}); is_hardware: true,
});
}
}
} }
// Fifth: Add front:CARD=X,DEV=Y devices from aplay -L (issue #30) // Fifth: Add front:CARD=X,DEV=Y devices (issue #30)
// Some USB DACs (e.g., SMSL AD18) don't expose hw: devices but have working front: devices // Some USB DACs don't expose hw: devices but have working front: devices
// DeaDBeef and Audacious use these for bit-perfect playback for card in &cards {
for (device_id, desc) in &description_map { for pcm in &card.pcm_playback_devices {
// Match front:CARD=X,DEV=Y pattern let front_device_id = format!("front:CARD={},DEV={}", card.short_name, pcm.device_num);
if device_id.starts_with("front:CARD=") {
// Check if we already have this device // Check if we already have this device
let already_exists = devices.iter().any(|d| d.name == *device_id); if devices.iter().any(|d| d.name == front_device_id) {
if already_exists {
continue; continue;
} }
// Extract card name for friendly description
// front:CARD=AMP,DEV=0 -> "AMP"
let card_name = device_id
.strip_prefix("front:CARD=")
.and_then(|s| s.split(',').next())
.unwrap_or("Unknown");
// Use card_map description if available, otherwise use aplay -L description
let friendly_desc = card_map
.iter()
.find(|(_, card_desc)| {
// Match by card short name in the description
card_desc.to_lowercase().contains(&card_name.to_lowercase())
})
.map(|(_, card_desc)| card_desc.clone())
.unwrap_or_else(|| desc.clone());
log::info!( log::info!(
"[ALSA Backend] Adding front: device for bit-perfect: {} ({})", "[ALSA Backend] Adding front: device for bit-perfect: {} ({})",
device_id, front_device_id,
friendly_desc card.long_name
); );
devices.push(AudioDevice { devices.push(AudioDevice {
id: device_id.clone(), id: front_device_id.clone(),
name: device_id.clone(), name: front_device_id.clone(),
description: Some(format!("{} (Direct Hardware - Bit-perfect)", friendly_desc)), description: Some(format!("{} - {} (Direct Hardware - Bit-perfect)", card.long_name, pcm.name)),
is_default: false, is_default: false,
max_sample_rate: Some(384000), // Assume high sample rate capability max_sample_rate: Some(384000),
supported_sample_rates: None, supported_sample_rates: None,
device_bus: None, device_bus: None,
is_hardware: true, is_hardware: true,
@@ -325,7 +401,7 @@ impl AlsaBackend {
Ok(devices) Ok(devices)
} }
/// Enumerate ALSA devices via CPAL (fallback - no descriptions) /// Enumerate ALSA devices via CPAL (basic enumeration, no descriptions)
fn enumerate_via_cpal(&self) -> BackendResult<Vec<AudioDevice>> { fn enumerate_via_cpal(&self) -> BackendResult<Vec<AudioDevice>> {
let mut devices = Vec::new(); let mut devices = Vec::new();
@@ -383,7 +459,7 @@ impl AlsaBackend {
devices.push(AudioDevice { devices.push(AudioDevice {
id: id.clone(), id: id.clone(),
name: name.clone(), name: name.clone(),
description: None, // CPAL doesn't provide descriptions description: None, // Will be filled by enumerate_with_proc_descriptions
is_default, is_default,
max_sample_rate, max_sample_rate,
supported_sample_rates, supported_sample_rates,
@@ -392,30 +468,11 @@ impl AlsaBackend {
}); });
} }
log::info!("[ALSA Backend] Enumerated {} devices via CPAL (fallback)", devices.len()); log::debug!("[ALSA Backend] CPAL enumerated {} base devices", devices.len());
for (idx, dev) in devices.iter().enumerate() {
log::info!(" [{}] {} (max_rate: {:?})", idx, dev.name, dev.max_sample_rate);
}
Ok(devices) Ok(devices)
} }
/// Enumerate ALSA devices with fallback
fn enumerate_alsa_devices(&self) -> BackendResult<Vec<AudioDevice>> {
// Try aplay -L first (preferred - has real hardware descriptions)
match self.enumerate_via_aplay() {
Ok(devices) => Ok(devices),
Err(e) => {
log::warn!(
"[ALSA Backend] aplay -L failed: {}. Falling back to CPAL enumeration (no descriptions). \
Install alsa-utils package for better device names.",
e
);
self.enumerate_via_cpal()
}
}
}
/// Try to create direct ALSA stream for hw: devices (bypasses CPAL) /// Try to create direct ALSA stream for hw: devices (bypasses CPAL)
/// Returns None if device is not a hw: device (should use CPAL instead) /// Returns None if device is not a hw: device (should use CPAL instead)
/// ///
@@ -545,42 +602,21 @@ pub fn normalize_device_id_to_stable(device_id: &str) -> String {
let card_num = parts[0]; let card_num = parts[0];
let device_num = parts[1]; let device_num = parts[1];
// Get card short name from aplay -l // Get card info from /proc/asound
let output = match Command::new("aplay").arg("-l").output() { let card_map = build_card_info_map();
Ok(o) if o.status.success() => o,
_ => {
log::warn!("[ALSA] aplay -l failed, cannot normalize device ID");
return device_id.to_string();
}
};
let stdout = String::from_utf8_lossy(&output.stdout); if let Some((short_name, _long_name)) = card_map.get(card_num) {
let stable_id = format!("front:CARD={},DEV={}", short_name, device_num);
// Parse: "card 0: C20 [Cambridge Audio USB Audio 2.0], device 0: ..." log::info!(
for line in stdout.lines() { "[ALSA] Normalized device ID: {} -> {} (stable)",
if let Some(card_info) = line.strip_prefix("card ") { device_id,
let line_parts: Vec<&str> = card_info.splitn(2, ':').collect(); stable_id
if line_parts.len() == 2 { );
let this_card_num = line_parts[0].trim(); return stable_id;
if this_card_num == card_num {
let rest = line_parts[1].trim();
// Extract short name (first word before the bracket)
if let Some(short_name) = rest.split_whitespace().next() {
let stable_id = format!("front:CARD={},DEV={}", short_name, device_num);
log::info!(
"[ALSA] Normalized device ID: {} -> {} (stable)",
device_id,
stable_id
);
return stable_id;
}
}
}
}
} }
log::warn!( log::warn!(
"[ALSA] Could not find card {} in aplay -l output, keeping original ID", "[ALSA] Could not find card {} in /proc/asound, keeping original ID",
card_num card_num
); );
device_id.to_string() device_id.to_string()
@@ -602,29 +638,11 @@ pub fn resolve_stable_to_current_hw(device_id: &str) -> Option<String> {
let card_name = parts.first()?; let card_name = parts.first()?;
let dev_part = parts.get(1).and_then(|s| s.strip_prefix("DEV=")).unwrap_or("0"); let dev_part = parts.get(1).and_then(|s| s.strip_prefix("DEV=")).unwrap_or("0");
// Find current card number for this name // Find current card number for this name using /proc/asound
let output = Command::new("aplay").arg("-l").output().ok()?; if let Some(card_num) = find_card_number_by_name(card_name) {
if !output.status.success() { let hw_id = format!("hw:{},{}", card_num, dev_part);
return None; log::debug!("[ALSA] Resolved {} -> {}", device_id, hw_id);
} return Some(hw_id);
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if let Some(card_info) = line.strip_prefix("card ") {
let line_parts: Vec<&str> = card_info.splitn(2, ':').collect();
if line_parts.len() == 2 {
let card_num = line_parts[0].trim();
let rest = line_parts[1].trim();
if let Some(short_name) = rest.split_whitespace().next() {
if short_name == *card_name {
let hw_id = format!("hw:{},{}", card_num, dev_part);
log::debug!("[ALSA] Resolved {} -> {}", device_id, hw_id);
return Some(hw_id);
}
}
}
}
} }
log::warn!("[ALSA] Card '{}' not found in current enumeration", card_name); log::warn!("[ALSA] Card '{}' not found in current enumeration", card_name);
@@ -637,7 +655,7 @@ impl AudioBackend for AlsaBackend {
} }
fn enumerate_devices(&self) -> BackendResult<Vec<AudioDevice>> { fn enumerate_devices(&self) -> BackendResult<Vec<AudioDevice>> {
self.enumerate_alsa_devices() self.enumerate_with_proc_descriptions()
} }
fn create_output_stream( fn create_output_stream(
+28 -66
View File
@@ -101,16 +101,13 @@ pub fn get_alsa_plugins() -> Result<Vec<AlsaPluginInfo>, String> {
Ok(plugins) Ok(plugins)
} }
/// Check if alsa-utils is installed (aplay command available) /// Check if ALSA device enumeration is available
/// Note: As of v1.1.14, we read directly from /proc/asound instead of using aplay,
/// so alsa-utils package is no longer required. This always returns true for compatibility.
#[tauri::command] #[tauri::command]
pub fn check_alsa_utils_installed() -> bool { pub fn check_alsa_utils_installed() -> bool {
use std::process::Command; // We no longer depend on alsa-utils - device info comes from /proc/asound
true
Command::new("which")
.arg("aplay")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
} }
/// Linux distribution info for install commands /// Linux distribution info for install commands
@@ -293,10 +290,9 @@ pub fn query_dac_capabilities(node_name: String) -> DacCapabilities {
caps caps
} }
/// Query ALSA hardware capabilities from /proc/asound or aplay /// Query ALSA hardware capabilities from /proc/asound
fn query_alsa_hw_capabilities(node_name: &str, caps: &mut DacCapabilities) { fn query_alsa_hw_capabilities(node_name: &str, caps: &mut DacCapabilities) {
use std::fs; use std::fs;
use std::process::Command;
// Try to extract card info from node name // Try to extract card info from node name
// Format: alsa_output.usb-Manufacturer_Product-00.analog-stereo // Format: alsa_output.usb-Manufacturer_Product-00.analog-stereo
@@ -355,19 +351,13 @@ fn query_alsa_hw_capabilities(node_name: &str, caps: &mut DacCapabilities) {
parse_alsa_stream_info(&stream_info, caps); parse_alsa_stream_info(&stream_info, caps);
} }
// Also try aplay --dump-hw-params for more detailed info // Try /proc/asound/cardX/pcmXp/sub0/hw_params for additional info (if stream is active)
// This requires a valid device, try common subdevices // This provides current hardware params but may not be available if device isn't in use
for subdev in 0..2 { for subdev in 0..2 {
let device = format!("hw:{},{}", card, subdev); let hw_params_path = format!("/proc/asound/card{}/pcm{}p/sub0/hw_params", card, subdev);
let output = Command::new("aplay") if let Ok(hw_params) = fs::read_to_string(&hw_params_path) {
.args(["-D", &device, "--dump-hw-params", "/dev/null"]) if hw_params.contains("rate:") {
.output(); parse_proc_hw_params(&hw_params, caps);
if let Ok(out) = output {
// aplay outputs to stderr for --dump-hw-params
let stderr = String::from_utf8_lossy(&out.stderr);
if stderr.contains("RATE") {
parse_aplay_hw_params(&stderr, caps);
break; break;
} }
} }
@@ -419,58 +409,30 @@ fn parse_alsa_stream_info(output: &str, caps: &mut DacCapabilities) {
} }
} }
/// Parse aplay --dump-hw-params output /// Parse /proc/asound/cardX/pcmXp/sub0/hw_params output (when device is active)
fn parse_aplay_hw_params(output: &str, caps: &mut DacCapabilities) { fn parse_proc_hw_params(output: &str, caps: &mut DacCapabilities) {
for line in output.lines() { for line in output.lines() {
let line = line.trim(); let line = line.trim();
// RATE: [44100 192000] or RATE: 44100 48000 88200 96000 176400 192000 // Format: "rate: 192000 (192000/1)"
if line.starts_with("RATE:") { if line.starts_with("rate:") {
if let Some(rates_part) = line.strip_prefix("RATE:") { if let Some(rate_part) = line.strip_prefix("rate:") {
let rates_str = rates_part.trim(); // Take first number before any parenthesis or space
let rate_str = rate_part.trim().split_whitespace().next().unwrap_or("");
// Check if it's a range [min max] if let Ok(rate) = rate_str.parse::<u32>() {
if rates_str.starts_with('[') { if !caps.sample_rates.contains(&rate) {
// Range format: [44100 192000] caps.sample_rates.push(rate);
// We'll add common rates within this range
let common_rates = [44100, 48000, 88200, 96000, 176400, 192000, 352800, 384000];
let nums: Vec<u32> = rates_str
.trim_matches(|c| c == '[' || c == ']')
.split_whitespace()
.filter_map(|s| s.parse().ok())
.collect();
if nums.len() >= 2 {
let min = nums[0];
let max = nums[1];
for rate in common_rates {
if rate >= min && rate <= max && !caps.sample_rates.contains(&rate) {
caps.sample_rates.push(rate);
}
}
}
} else {
// List format: 44100 48000 88200 96000
for rate_str in rates_str.split_whitespace() {
if let Ok(rate) = rate_str.parse::<u32>() {
if !caps.sample_rates.contains(&rate) {
caps.sample_rates.push(rate);
}
}
} }
} }
} }
} }
// FORMAT: S16_LE S24_LE S32_LE // Format: "format: S32_LE"
if line.starts_with("FORMAT:") { if line.starts_with("format:") {
if let Some(formats_str) = line.strip_prefix("FORMAT:") { if let Some(format_str) = line.strip_prefix("format:") {
for format in formats_str.split_whitespace() { let format = format_str.trim().to_string();
let format = format.trim().to_string(); if !format.is_empty() && !caps.formats.contains(&format) {
if !format.is_empty() && !format.starts_with('[') && !caps.formats.contains(&format) { caps.formats.push(format);
caps.formats.push(format);
}
} }
} }
} }