diff --git a/apps/hello/.gitignore b/apps/hello/.gitignore index bf9e782..87c6086 100644 --- a/apps/hello/.gitignore +++ b/apps/hello/.gitignore @@ -1 +1,2 @@ -*.mp3 \ No newline at end of file +*.mp3 +*.wav \ No newline at end of file diff --git a/apps/hello/Cargo.lock b/apps/hello/Cargo.lock index daf78ab..c3c2b98 100644 --- a/apps/hello/Cargo.lock +++ b/apps/hello/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "aho-corasick" -version = "1.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" -dependencies = [ - "memchr", -] - [[package]] name = "alsa" version = "0.11.0" @@ -33,56 +24,6 @@ dependencies = [ "pkg-config", ] -[[package]] -name = "anstream" -version = "0.6.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is_terminal_polyfill", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" - -[[package]] -name = "anstyle-parse" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" -dependencies = [ - "windows-sys 0.61.2", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" -dependencies = [ - "anstyle", - "once_cell_polyfill", - "windows-sys 0.61.2", -] - [[package]] name = "anyhow" version = "1.0.100" @@ -179,12 +120,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "colorchoice" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" - [[package]] name = "critical-section" version = "1.2.0" @@ -212,29 +147,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "env_filter" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf3c259d255ca70051b30e2e95b5446cdb8949ac4cd22c0d7fd634d89f568e2" -dependencies = [ - "log", - "regex", -] - -[[package]] -name = "env_logger" -version = "0.11.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f" -dependencies = [ - "anstream", - "anstyle", - "env_filter", - "jiff", - "log", -] - [[package]] name = "errno" version = "0.3.14" @@ -242,7 +154,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -286,9 +198,6 @@ version = "0.1.0" dependencies = [ "alsa", "anyhow", - "byteorder", - "env_logger", - "log", "opus", "postcard", "serde", @@ -296,36 +205,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "is_terminal_polyfill" -version = "1.70.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" - -[[package]] -name = "jiff" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a87d9b8105c23642f50cbbae03d1f75d8422c5cb98ce7ee9271f7ff7505be6b8" -dependencies = [ - "jiff-static", - "log", - "portable-atomic", - "portable-atomic-util", - "serde_core", -] - -[[package]] -name = "jiff-static" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b787bebb543f8969132630c51fd0afab173a86c6abae56ff3b9e5e3e3f9f6e58" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "lazy_static" version = "1.5.0" @@ -353,12 +232,6 @@ version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" -[[package]] -name = "memchr" -version = "2.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" - [[package]] name = "mio" version = "1.1.1" @@ -370,12 +243,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "once_cell_polyfill" -version = "1.70.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" - [[package]] name = "opus" version = "0.3.0" @@ -421,21 +288,6 @@ version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" -[[package]] -name = "portable-atomic" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f89776e4d69bb58bc6993e99ffa1d11f228b839984854c7daeb5d37f87cbe950" - -[[package]] -name = "portable-atomic-util" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" -dependencies = [ - "portable-atomic", -] - [[package]] name = "postcard" version = "1.1.3" @@ -476,35 +328,6 @@ dependencies = [ "bitflags 2.10.0", ] -[[package]] -name = "regex" -version = "1.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" - [[package]] name = "rustc_version" version = "0.4.1" @@ -616,7 +439,6 @@ dependencies = [ "symphonia-codec-pcm", "symphonia-codec-vorbis", "symphonia-core", - "symphonia-format-isomp4", "symphonia-format-mkv", "symphonia-format-ogg", "symphonia-format-riff", @@ -691,19 +513,6 @@ dependencies = [ "log", ] -[[package]] -name = "symphonia-format-isomp4" -version = "0.5.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "243739585d11f81daf8dac8d9f3d18cc7898f6c09a259675fc364b382c30e0a5" -dependencies = [ - "encoding_rs", - "log", - "symphonia-core", - "symphonia-metadata", - "symphonia-utils-xiph", -] - [[package]] name = "symphonia-format-mkv" version = "0.5.5" @@ -828,12 +637,6 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" -[[package]] -name = "utf8parse" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" - [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" diff --git a/apps/hello/Cargo.toml b/apps/hello/Cargo.toml index ed05169..4df68e0 100644 --- a/apps/hello/Cargo.toml +++ b/apps/hello/Cargo.toml @@ -14,13 +14,12 @@ debug = false [dependencies] opus = "0.3" anyhow = "1.0" -tokio = { version = "1.47", features = ["full"] } -symphonia = { version = "0.5", features = ["mp3", "isomp4"] } +tokio = { version = "1.48", features = ["full"] } serde = { version = "1.0", features = ["derive"] } postcard = { version = "1.0", features = ["alloc", "use-std"] } -log = "0.4" -env_logger = "0.11" -byteorder = "1.5" + +[target.'cfg(not(target_os = "linux"))'.dependencies] +symphonia = { version = "0.5", features = ["mp3", "wav", "pcm"] } [target.'cfg(target_os = "linux")'.dependencies] alsa = "0.11" diff --git a/apps/hello/build.sh b/apps/hello/build.sh index 8ed4e00..f3fa505 100755 --- a/apps/hello/build.sh +++ b/apps/hello/build.sh @@ -2,24 +2,27 @@ set -e -# SSH_PASSWORD=open-xiaoai +SSH_PASSWORD=open-xiaoai -# # 1. 编译 demo -# echo "Compiling hello demo..." -# docker run --rm -v $(pwd):/app/hello open-xiaoai-runtime \ -# cargo build --manifest-path hello/Cargo.toml --target armv7-unknown-linux-gnueabihf --bin client --release +# 1. 编译 client +echo "Compiling client..." +docker run --rm -v $(pwd):/app/hello open-xiaoai-runtime \ + cargo build --manifest-path hello/Cargo.toml --target armv7-unknown-linux-gnueabihf --bin client --release -# # 2. 上传二进制文件到小爱音箱 -# function upload_to_xiaoai() { -# local binary_name=$1 -# local ip=$2 -# dd if=target/armv7-unknown-linux-gnueabihf/release/$binary_name \ -# | sshpass -p $SSH_PASSWORD ssh -o HostKeyAlgorithms=+ssh-rsa root@$ip "dd of=/data/$binary_name" \ -# && echo "✅ Uploaded $binary_name to $ip" -# } +# 2. 上传 client 到小爱音箱 +function upload_to_xiaoai() { + local binary_name=$1 + local ip=$2 + dd if=target/armv7-unknown-linux-gnueabihf/release/$binary_name \ + | sshpass -p $SSH_PASSWORD ssh -o HostKeyAlgorithms=+ssh-rsa root@$ip "dd of=/data/$binary_name" \ + && echo "✅ Uploaded $binary_name to $ip" +} -# upload_to_xiaoai client 192.168.31.153 # left -# upload_to_xiaoai client 192.168.31.235 # right +upload_to_xiaoai client 192.168.31.153 # left +upload_to_xiaoai client 192.168.31.235 # right +# 3. 编译 server cargo build --bin server --release ./target/release/server + +# dd if=test.wav | sshpass -p open-xiaoai ssh -o HostKeyAlgorithms=+ssh-rsa root@192.168.31.235 "dd of=/tmp/test.wav" \ No newline at end of file diff --git a/apps/hello/src/audio/codec.rs b/apps/hello/src/audio/codec.rs index 9b1e4b9..6ac2dd2 100644 --- a/apps/hello/src/audio/codec.rs +++ b/apps/hello/src/audio/codec.rs @@ -10,7 +10,7 @@ pub struct OpusCodec { impl OpusCodec { pub fn new(config: &AudioConfig) -> Result { let channels = if config.channels == 1 { Channels::Mono } else { Channels::Stereo }; - let mut encoder = Encoder::new(config.sample_rate, channels, Application::Voip) + let mut encoder = Encoder::new(config.sample_rate, channels, Application::Audio) .context("Failed to create Opus encoder")?; encoder.set_bitrate(Bitrate::Bits(config.bitrate))?; diff --git a/apps/hello/src/audio/mod.rs b/apps/hello/src/audio/mod.rs index 04d19e1..01141bb 100644 --- a/apps/hello/src/audio/mod.rs +++ b/apps/hello/src/audio/mod.rs @@ -1,12 +1,12 @@ -#[cfg(target_os = "linux")] -pub mod recorder; -#[cfg(target_os = "linux")] -pub mod player; pub mod codec; +pub mod player; +pub mod reader; +pub mod recorder; +pub use codec::*; #[cfg(target_os = "linux")] -pub use recorder::AudioRecorder; +pub use player::*; +#[cfg(not(target_os = "linux"))] +pub use reader::*; #[cfg(target_os = "linux")] -pub use player::AudioPlayer; -pub use codec::OpusCodec; - +pub use recorder::*; diff --git a/apps/hello/src/audio/reader.rs b/apps/hello/src/audio/reader.rs new file mode 100644 index 0000000..f9c9de8 --- /dev/null +++ b/apps/hello/src/audio/reader.rs @@ -0,0 +1,140 @@ +#![cfg(not(target_os = "linux"))] +use anyhow::{Context, Result}; +use std::fs::File; +use std::path::Path; +use symphonia::core::audio::SampleBuffer; +use symphonia::core::codecs::{Decoder, DecoderOptions}; +use symphonia::core::formats::{FormatOptions, FormatReader}; +use symphonia::core::io::MediaSourceStream; +use symphonia::core::meta::MetadataOptions; +use symphonia::core::probe::Hint; + +pub struct AudioReader { + format: Box, + decoder: Box, + track_id: u32, + sample_buf: Option>, + channels: usize, + left_buffer: Vec, + right_buffer: Vec, +} + +impl AudioReader { + pub fn new(path: &str) -> Result { + let src = + File::open(Path::new(path)).context(format!("Failed to open audio file: {}", path))?; + let mss = MediaSourceStream::new(Box::new(src), Default::default()); + + let mut hint = Hint::new(); + if path.ends_with(".wav") { + hint.with_extension("wav"); + } else if path.ends_with(".mp3") { + hint.with_extension("mp3"); + } + + let probed = symphonia::default::get_probe() + .format( + &hint, + mss, + &FormatOptions::default(), + &MetadataOptions::default(), + ) + .context("Failed to probe audio format")?; + + let format = probed.format; + let track = format + .tracks() + .iter() + .find(|t| t.codec_params.codec != symphonia::core::codecs::CODEC_TYPE_NULL) + .context("No supported audio track found")?; + + let track_id = track.id; + let decoder = symphonia::default::get_codecs() + .make(&track.codec_params, &DecoderOptions::default()) + .context("Failed to create decoder")?; + + let channels = track.codec_params.channels.map(|c| c.count()).unwrap_or(1); + + Ok(Self { + format, + decoder, + track_id, + sample_buf: None, + channels, + left_buffer: Vec::new(), + right_buffer: Vec::new(), + }) + } + + pub fn read_chunk(&mut self, chunk_size: usize) -> Result, Vec)>> { + while self.left_buffer.len() < chunk_size { + if let Some((l, r)) = self.read_frame_internal()? { + self.left_buffer.extend(l); + self.right_buffer.extend(r); + } else { + break; + } + } + + if self.left_buffer.is_empty() { + return Ok(None); + } + + let actual_size = std::cmp::min(chunk_size, self.left_buffer.len()); + let left = self.left_buffer.drain(0..actual_size).collect(); + let right = self.right_buffer.drain(0..actual_size).collect(); + + Ok(Some((left, right))) + } + + fn read_frame_internal(&mut self) -> Result, Vec)>> { + loop { + let packet = match self.format.next_packet() { + Ok(packet) => packet, + Err(symphonia::core::errors::Error::IoError(ref e)) + if e.kind() == std::io::ErrorKind::UnexpectedEof => + { + return Ok(None); + } + Err(e) => return Err(e.into()), + }; + + if packet.track_id() != self.track_id { + continue; + } + + let decoded = self + .decoder + .decode(&packet) + .context("Failed to decode packet")?; + + if self.sample_buf.is_none() { + self.sample_buf = Some(SampleBuffer::::new( + decoded.capacity() as u64, + *decoded.spec(), + )); + } + + if let Some(buf) = self.sample_buf.as_mut() { + buf.copy_interleaved_ref(decoded); + let samples = buf.samples(); + + let mut left = Vec::with_capacity(samples.len() / self.channels); + let mut right = Vec::with_capacity(samples.len() / self.channels); + + if self.channels == 2 { + for i in (0..samples.len()).step_by(2) { + left.push(samples[i]); + right.push(samples[i + 1]); + } + } else { + for &s in samples { + left.push(s); + right.push(s); + } + } + return Ok(Some((left, right))); + } + } + } +} diff --git a/apps/hello/src/bin/client.rs b/apps/hello/src/bin/client.rs index 264c2f9..75ba8f0 100644 --- a/apps/hello/src/bin/client.rs +++ b/apps/hello/src/bin/client.rs @@ -23,7 +23,6 @@ async fn main() -> Result<()> { #[cfg(target_os = "linux")] { - env_logger::init(); let args: Vec = env::args().collect(); if args.len() < 2 { eprintln!("Usage: {} [left|right]", args[0]); @@ -39,6 +38,7 @@ async fn main() -> Result<()> { sample_rate: 48000, channels: 1, frame_size: 960, + bitrate: 32000, ..AudioConfig::default() }; diff --git a/apps/hello/src/bin/server.rs b/apps/hello/src/bin/server.rs index 7e879cd..fdf3b15 100644 --- a/apps/hello/src/bin/server.rs +++ b/apps/hello/src/bin/server.rs @@ -1,29 +1,21 @@ -use anyhow::{Context, Result}; -use hello::audio::OpusCodec; +use anyhow::Result; +use hello::audio::{AudioReader, OpusCodec}; use hello::config::AudioConfig; use hello::net::{AudioPacket, ChannelRole, ControlPacket, Discovery, SERVER_PORT}; use hello::sync::now_us; use std::collections::HashMap; -use std::fs::File; -use std::path::Path; use std::sync::Arc; -use symphonia::core::audio::SampleBuffer; -use symphonia::core::codecs::DecoderOptions; -use symphonia::core::formats::FormatOptions; -use symphonia::core::io::MediaSourceStream; -use symphonia::core::meta::MetadataOptions; -use symphonia::core::probe::Hint; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::{TcpListener, TcpStream}; use tokio::sync::Mutex; #[tokio::main] async fn main() -> Result<()> { - env_logger::init(); let config = AudioConfig { sample_rate: 48000, channels: 1, frame_size: 960, // 20ms at 48kHz + bitrate: 32000, // 32kbps ..AudioConfig::default() }; @@ -76,34 +68,17 @@ async fn main() -> Result<()> { println!("Both clients connected. Starting stream in 1s..."); tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; - stream_mp3("test.mp3", &config, clients).await?; + stream_audio("test.mp3", &config, clients).await?; Ok(()) } -async fn stream_mp3( +async fn stream_audio( path: &str, config: &AudioConfig, clients: Arc>>, ) -> Result<()> { - let src = File::open(Path::new(path)).context("Failed to open test.mp3")?; - let mss = MediaSourceStream::new(Box::new(src), Default::default()); - let probed = symphonia::default::get_probe().format( - &Hint::new(), - mss, - &FormatOptions::default(), - &MetadataOptions::default(), - )?; - - let mut format = probed.format; - let track = format - .tracks() - .iter() - .find(|t| t.codec_params.codec == symphonia::core::codecs::CODEC_TYPE_MP3) - .context("No track")?; - let mut decoder = - symphonia::default::get_codecs().make(&track.codec_params, &DecoderOptions::default())?; - let track_id = track.id; + let mut reader = AudioReader::new(path)?; let mut left_codec = OpusCodec::new(config)?; let mut right_codec = OpusCodec::new(config)?; @@ -113,76 +88,22 @@ async fn stream_mp3( let frame_duration_us = (config.frame_size as f64 / config.sample_rate as f64 * 1_000_000.0) as u128; - let mut sample_buf = None; - - loop { - let packet = match format.next_packet() { - Ok(packet) => packet, - Err(_) => break, - }; - if packet.track_id() != track_id { - continue; + while let Some((left_pcm, right_pcm)) = reader.read_chunk(config.frame_size)? { + if left_pcm.len() < config.frame_size { + break; } - let decoded = decoder.decode(&packet)?; - if sample_buf.is_none() { - sample_buf = Some(SampleBuffer::::new( - decoded.capacity() as u64, - *decoded.spec(), - )); - } - - if let Some(buf) = sample_buf.as_mut() { - buf.copy_interleaved_ref(decoded.clone()); - let spec = decoded.spec(); - let num_channels = spec.channels.count(); - - if num_channels == 2 { - for chunk in buf.samples().chunks(config.frame_size * 2) { - if chunk.len() < config.frame_size * 2 { - break; - } - - let mut left_pcm = vec![0i16; config.frame_size]; - let mut right_pcm = vec![0i16; config.frame_size]; - - for i in 0..config.frame_size { - left_pcm[i] = chunk[i * 2]; - right_pcm[i] = chunk[i * 2 + 1]; - } - - send_packets( - &clients, - &mut left_codec, - &mut right_codec, - &left_pcm, - &right_pcm, - current_frame_ts, - ) - .await?; - current_frame_ts += frame_duration_us; - pace(current_frame_ts).await; - } - } else { - for chunk in buf.samples().chunks(config.frame_size) { - if chunk.len() < config.frame_size { - break; - } - - send_packets( - &clients, - &mut left_codec, - &mut right_codec, - chunk, - chunk, - current_frame_ts, - ) - .await?; - current_frame_ts += frame_duration_us; - pace(current_frame_ts).await; - } - } - } + send_packets( + &clients, + &mut left_codec, + &mut right_codec, + &left_pcm, + &right_pcm, + current_frame_ts, + ) + .await?; + current_frame_ts += frame_duration_us; + pace(current_frame_ts).await; } Ok(()) }