chore: 优化代码

This commit is contained in:
Del Wang
2025-12-29 19:14:42 +08:00
parent f8f217c455
commit fb9a9dd439
3 changed files with 168 additions and 209 deletions
+3 -14
View File
@@ -1,9 +1,7 @@
use anyhow::Result; #![cfg(target_os = "linux")]
#[cfg(not(target_os = "linux"))]
use hello::audio::OpusCodec;
#[cfg(target_os = "linux")]
use hello::audio::{AudioPlayer, OpusCodec};
use anyhow::Result;
use hello::audio::{AudioPlayer, OpusCodec};
use hello::config::AudioConfig; use hello::config::AudioConfig;
use hello::net::{AudioPacket, ChannelRole, ControlPacket, Discovery}; use hello::net::{AudioPacket, ChannelRole, ControlPacket, Discovery};
use hello::sync::{ClockSync, now_us}; use hello::sync::{ClockSync, now_us};
@@ -15,14 +13,6 @@ use tokio::net::TcpStream;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
#[cfg(not(target_os = "linux"))]
{
println!("Client is only supported on Linux (ALSA required)");
return Ok(());
}
#[cfg(target_os = "linux")]
{
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if args.len() < 2 { if args.len() < 2 {
eprintln!("Usage: {} [left|right]", args[0]); eprintln!("Usage: {} [left|right]", args[0]);
@@ -130,5 +120,4 @@ async fn main() -> Result<()> {
tokio::time::sleep(Duration::from_millis(5)).await; tokio::time::sleep(Duration::from_millis(5)).await;
} }
} }
}
} }
+4 -23
View File
@@ -1,5 +1,6 @@
#![cfg(target_os = "linux")]
use anyhow::{Context, Result}; use anyhow::{Context, Result};
#[cfg(target_os = "linux")]
use hello::audio::{AudioPlayer, OpusCodec}; use hello::audio::{AudioPlayer, OpusCodec};
use hello::config::AudioConfig; use hello::config::AudioConfig;
use hello::net::{AudioPacket, ChannelRole, ControlPacket, DISCOVERY_PORT, SERVER_PORT}; use hello::net::{AudioPacket, ChannelRole, ControlPacket, DISCOVERY_PORT, SERVER_PORT};
@@ -14,14 +15,10 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream, UdpSocket}; use tokio::net::{TcpListener, TcpStream, UdpSocket};
use tokio::signal; use tokio::signal;
#[cfg(target_os = "linux")]
const FIFO_PATH: &str = "/tmp/stereo_out.fifo"; const FIFO_PATH: &str = "/tmp/stereo_out.fifo";
#[cfg(target_os = "linux")]
const TEMP_ASOUND_CONF: &str = "/tmp/asound.stereo.conf";
#[cfg(target_os = "linux")]
const REAL_ASOUND_CONF: &str = "/etc/asound.conf"; const REAL_ASOUND_CONF: &str = "/etc/asound.conf";
const TEMP_ASOUND_CONF: &str = "/tmp/asound.stereo.conf";
#[cfg(target_os = "linux")]
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
enum DeviceModel { enum DeviceModel {
Lx06, Lx06,
@@ -29,7 +26,6 @@ enum DeviceModel {
Unknown, Unknown,
} }
#[cfg(target_os = "linux")]
fn detect_model() -> DeviceModel { fn detect_model() -> DeviceModel {
let model = Command::new("sh") let model = Command::new("sh")
.args(["-c", "micocfg_model"]) .args(["-c", "micocfg_model"])
@@ -45,7 +41,6 @@ fn detect_model() -> DeviceModel {
DeviceModel::Unknown DeviceModel::Unknown
} }
#[cfg(target_os = "linux")]
fn setup_alsa_config(model: DeviceModel) -> Result<()> { fn setup_alsa_config(model: DeviceModel) -> Result<()> {
cleanup_alsa_config(); cleanup_alsa_config();
@@ -89,14 +84,13 @@ pcm.stereo_interceptor {{
.context("Failed to execute mount command")?; .context("Failed to execute mount command")?;
if !status.success() { if !status.success() {
return Err(anyhow::anyhow!("Failed to mount asound.conf, need root?")); return Err(anyhow::anyhow!("Failed to mount asound.conf"));
} }
println!("Successfully redirected ALSA output to {}", FIFO_PATH); println!("Successfully redirected ALSA output to {}", FIFO_PATH);
Ok(()) Ok(())
} }
#[cfg(target_os = "linux")]
fn cleanup_alsa_config() { fn cleanup_alsa_config() {
println!("Cleaning up ALSA configurations..."); println!("Cleaning up ALSA configurations...");
let _ = Command::new("umount") let _ = Command::new("umount")
@@ -109,14 +103,6 @@ fn cleanup_alsa_config() {
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
#[cfg(not(target_os = "linux"))]
{
println!("Stereo 模式仅支持 Linux (需要 ALSA)");
return Ok(());
}
#[cfg(target_os = "linux")]
{
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if args.len() < 3 { if args.len() < 3 {
eprintln!("用法: {} [master|slave] [left|right]", args[0]); eprintln!("用法: {} [master|slave] [left|right]", args[0]);
@@ -149,10 +135,8 @@ async fn main() -> Result<()> {
cleanup_alsa_config(); cleanup_alsa_config();
return result; return result;
}
} }
#[cfg(target_os = "linux")]
async fn run_master(role: ChannelRole) -> Result<()> { async fn run_master(role: ChannelRole) -> Result<()> {
let config = AudioConfig { let config = AudioConfig {
sample_rate: 48000, sample_rate: 48000,
@@ -241,7 +225,6 @@ async fn run_master(role: ChannelRole) -> Result<()> {
} }
} }
#[cfg(target_os = "linux")]
async fn run_master_audio_loop( async fn run_master_audio_loop(
slave_stream: &mut TcpStream, slave_stream: &mut TcpStream,
role: ChannelRole, role: ChannelRole,
@@ -347,7 +330,6 @@ async fn run_master_audio_loop(
} }
} }
#[cfg(target_os = "linux")]
async fn run_slave(role: ChannelRole) -> Result<()> { async fn run_slave(role: ChannelRole) -> Result<()> {
let config = AudioConfig { let config = AudioConfig {
sample_rate: 48000, sample_rate: 48000,
@@ -453,7 +435,6 @@ async fn run_slave(role: ChannelRole) -> Result<()> {
} }
} }
#[cfg(target_os = "linux")]
async fn run_slave_audio_loop( async fn run_slave_audio_loop(
stream: TcpStream, stream: TcpStream,
config: &AudioConfig, config: &AudioConfig,
+2 -13
View File
@@ -1,22 +1,12 @@
#[cfg(not(target_os = "linux"))] #![cfg(target_os = "linux")]
use hello::audio::OpusCodec;
#[cfg(target_os = "linux")]
use hello::audio::{AudioPlayer, AudioRecorder, OpusCodec};
use anyhow::Result; use anyhow::Result;
use hello::audio::{AudioPlayer, AudioRecorder, OpusCodec};
use hello::config::AudioConfig; use hello::config::AudioConfig;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
fn main() -> Result<()> { fn main() -> Result<()> {
#[cfg(not(target_os = "linux"))]
{
println!("Test utility is only supported on Linux (ALSA required)");
return Ok(());
}
#[cfg(target_os = "linux")]
{
let config = AudioConfig::default(); let config = AudioConfig::default();
println!("Starting Audio Modular Demo with 1s delay"); println!("Starting Audio Modular Demo with 1s delay");
println!( println!(
@@ -69,5 +59,4 @@ fn main() -> Result<()> {
} }
} }
} }
}
} }