feat: 优化音频传输参数

This commit is contained in:
Del Wang
2025-12-29 14:28:21 +08:00
parent d7eeba90fd
commit 8db280ebc1
9 changed files with 195 additions and 328 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ pub struct OpusCodec {
impl OpusCodec {
pub fn new(config: &AudioConfig) -> Result<Self> {
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))?;
+8 -8
View File
@@ -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::*;
+140
View File
@@ -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<dyn FormatReader>,
decoder: Box<dyn Decoder>,
track_id: u32,
sample_buf: Option<SampleBuffer<i16>>,
channels: usize,
left_buffer: Vec<i16>,
right_buffer: Vec<i16>,
}
impl AudioReader {
pub fn new(path: &str) -> Result<Self> {
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<Option<(Vec<i16>, Vec<i16>)>> {
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<Option<(Vec<i16>, Vec<i16>)>> {
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::<i16>::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)));
}
}
}
}
+1 -1
View File
@@ -23,7 +23,6 @@ async fn main() -> Result<()> {
#[cfg(target_os = "linux")]
{
env_logger::init();
let args: Vec<String> = 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()
};
+20 -99
View File
@@ -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<Mutex<HashMap<ChannelRole, TcpStream>>>,
) -> 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::<i16>::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(())
}