feat: 优化音频传输参数
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
*.mp3
|
*.mp3
|
||||||
|
*.wav
|
||||||
Generated
+1
-198
@@ -2,15 +2,6 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
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]]
|
[[package]]
|
||||||
name = "alsa"
|
name = "alsa"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
@@ -33,56 +24,6 @@ dependencies = [
|
|||||||
"pkg-config",
|
"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]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.100"
|
version = "1.0.100"
|
||||||
@@ -179,12 +120,6 @@ dependencies = [
|
|||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "colorchoice"
|
|
||||||
version = "1.0.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "critical-section"
|
name = "critical-section"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
@@ -212,29 +147,6 @@ dependencies = [
|
|||||||
"cfg-if",
|
"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]]
|
[[package]]
|
||||||
name = "errno"
|
name = "errno"
|
||||||
version = "0.3.14"
|
version = "0.3.14"
|
||||||
@@ -242,7 +154,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.60.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -286,9 +198,6 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"alsa",
|
"alsa",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"byteorder",
|
|
||||||
"env_logger",
|
|
||||||
"log",
|
|
||||||
"opus",
|
"opus",
|
||||||
"postcard",
|
"postcard",
|
||||||
"serde",
|
"serde",
|
||||||
@@ -296,36 +205,6 @@ dependencies = [
|
|||||||
"tokio",
|
"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]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
@@ -353,12 +232,6 @@ version = "0.4.29"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
|
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "memchr"
|
|
||||||
version = "2.7.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "1.1.1"
|
version = "1.1.1"
|
||||||
@@ -370,12 +243,6 @@ dependencies = [
|
|||||||
"windows-sys 0.61.2",
|
"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]]
|
[[package]]
|
||||||
name = "opus"
|
name = "opus"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
@@ -421,21 +288,6 @@ version = "0.3.32"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
|
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]]
|
[[package]]
|
||||||
name = "postcard"
|
name = "postcard"
|
||||||
version = "1.1.3"
|
version = "1.1.3"
|
||||||
@@ -476,35 +328,6 @@ dependencies = [
|
|||||||
"bitflags 2.10.0",
|
"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]]
|
[[package]]
|
||||||
name = "rustc_version"
|
name = "rustc_version"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
@@ -616,7 +439,6 @@ dependencies = [
|
|||||||
"symphonia-codec-pcm",
|
"symphonia-codec-pcm",
|
||||||
"symphonia-codec-vorbis",
|
"symphonia-codec-vorbis",
|
||||||
"symphonia-core",
|
"symphonia-core",
|
||||||
"symphonia-format-isomp4",
|
|
||||||
"symphonia-format-mkv",
|
"symphonia-format-mkv",
|
||||||
"symphonia-format-ogg",
|
"symphonia-format-ogg",
|
||||||
"symphonia-format-riff",
|
"symphonia-format-riff",
|
||||||
@@ -691,19 +513,6 @@ dependencies = [
|
|||||||
"log",
|
"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]]
|
[[package]]
|
||||||
name = "symphonia-format-mkv"
|
name = "symphonia-format-mkv"
|
||||||
version = "0.5.5"
|
version = "0.5.5"
|
||||||
@@ -828,12 +637,6 @@ version = "1.0.22"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "utf8parse"
|
|
||||||
version = "0.2.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.11.1+wasi-snapshot-preview1"
|
version = "0.11.1+wasi-snapshot-preview1"
|
||||||
|
|||||||
@@ -14,13 +14,12 @@ debug = false
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
opus = "0.3"
|
opus = "0.3"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
tokio = { version = "1.47", features = ["full"] }
|
tokio = { version = "1.48", features = ["full"] }
|
||||||
symphonia = { version = "0.5", features = ["mp3", "isomp4"] }
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
postcard = { version = "1.0", features = ["alloc", "use-std"] }
|
postcard = { version = "1.0", features = ["alloc", "use-std"] }
|
||||||
log = "0.4"
|
|
||||||
env_logger = "0.11"
|
[target.'cfg(not(target_os = "linux"))'.dependencies]
|
||||||
byteorder = "1.5"
|
symphonia = { version = "0.5", features = ["mp3", "wav", "pcm"] }
|
||||||
|
|
||||||
[target.'cfg(target_os = "linux")'.dependencies]
|
[target.'cfg(target_os = "linux")'.dependencies]
|
||||||
alsa = "0.11"
|
alsa = "0.11"
|
||||||
|
|||||||
+18
-15
@@ -2,24 +2,27 @@
|
|||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# SSH_PASSWORD=open-xiaoai
|
SSH_PASSWORD=open-xiaoai
|
||||||
|
|
||||||
# # 1. 编译 demo
|
# 1. 编译 client
|
||||||
# echo "Compiling hello demo..."
|
echo "Compiling client..."
|
||||||
# docker run --rm -v $(pwd):/app/hello open-xiaoai-runtime \
|
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
|
cargo build --manifest-path hello/Cargo.toml --target armv7-unknown-linux-gnueabihf --bin client --release
|
||||||
|
|
||||||
# # 2. 上传二进制文件到小爱音箱
|
# 2. 上传 client 到小爱音箱
|
||||||
# function upload_to_xiaoai() {
|
function upload_to_xiaoai() {
|
||||||
# local binary_name=$1
|
local binary_name=$1
|
||||||
# local ip=$2
|
local ip=$2
|
||||||
# dd if=target/armv7-unknown-linux-gnueabihf/release/$binary_name \
|
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" \
|
| sshpass -p $SSH_PASSWORD ssh -o HostKeyAlgorithms=+ssh-rsa root@$ip "dd of=/data/$binary_name" \
|
||||||
# && echo "✅ Uploaded $binary_name to $ip"
|
&& echo "✅ Uploaded $binary_name to $ip"
|
||||||
# }
|
}
|
||||||
|
|
||||||
# upload_to_xiaoai client 192.168.31.153 # left
|
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.235 # right
|
||||||
|
|
||||||
|
# 3. 编译 server
|
||||||
cargo build --bin server --release
|
cargo build --bin server --release
|
||||||
./target/release/server
|
./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"
|
||||||
@@ -10,7 +10,7 @@ pub struct OpusCodec {
|
|||||||
impl OpusCodec {
|
impl OpusCodec {
|
||||||
pub fn new(config: &AudioConfig) -> Result<Self> {
|
pub fn new(config: &AudioConfig) -> Result<Self> {
|
||||||
let channels = if config.channels == 1 { Channels::Mono } else { Channels::Stereo };
|
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")?;
|
.context("Failed to create Opus encoder")?;
|
||||||
encoder.set_bitrate(Bitrate::Bits(config.bitrate))?;
|
encoder.set_bitrate(Bitrate::Bits(config.bitrate))?;
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
#[cfg(target_os = "linux")]
|
|
||||||
pub mod recorder;
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
pub mod player;
|
|
||||||
pub mod codec;
|
pub mod codec;
|
||||||
|
pub mod player;
|
||||||
|
pub mod reader;
|
||||||
|
pub mod recorder;
|
||||||
|
|
||||||
|
pub use codec::*;
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
pub use recorder::AudioRecorder;
|
pub use player::*;
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
pub use reader::*;
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
pub use player::AudioPlayer;
|
pub use recorder::*;
|
||||||
pub use codec::OpusCodec;
|
|
||||||
|
|
||||||
|
|||||||
@@ -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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,7 +23,6 @@ async fn main() -> Result<()> {
|
|||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
env_logger::init();
|
|
||||||
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]);
|
||||||
@@ -39,6 +38,7 @@ async fn main() -> Result<()> {
|
|||||||
sample_rate: 48000,
|
sample_rate: 48000,
|
||||||
channels: 1,
|
channels: 1,
|
||||||
frame_size: 960,
|
frame_size: 960,
|
||||||
|
bitrate: 32000,
|
||||||
..AudioConfig::default()
|
..AudioConfig::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +1,21 @@
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::Result;
|
||||||
use hello::audio::OpusCodec;
|
use hello::audio::{AudioReader, OpusCodec};
|
||||||
use hello::config::AudioConfig;
|
use hello::config::AudioConfig;
|
||||||
use hello::net::{AudioPacket, ChannelRole, ControlPacket, Discovery, SERVER_PORT};
|
use hello::net::{AudioPacket, ChannelRole, ControlPacket, Discovery, SERVER_PORT};
|
||||||
use hello::sync::now_us;
|
use hello::sync::now_us;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::sync::Arc;
|
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::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
use tokio::net::{TcpListener, TcpStream};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
env_logger::init();
|
|
||||||
let config = AudioConfig {
|
let config = AudioConfig {
|
||||||
sample_rate: 48000,
|
sample_rate: 48000,
|
||||||
channels: 1,
|
channels: 1,
|
||||||
frame_size: 960, // 20ms at 48kHz
|
frame_size: 960, // 20ms at 48kHz
|
||||||
|
bitrate: 32000, // 32kbps
|
||||||
..AudioConfig::default()
|
..AudioConfig::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -76,34 +68,17 @@ async fn main() -> Result<()> {
|
|||||||
println!("Both clients connected. Starting stream in 1s...");
|
println!("Both clients connected. Starting stream in 1s...");
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn stream_mp3(
|
async fn stream_audio(
|
||||||
path: &str,
|
path: &str,
|
||||||
config: &AudioConfig,
|
config: &AudioConfig,
|
||||||
clients: Arc<Mutex<HashMap<ChannelRole, TcpStream>>>,
|
clients: Arc<Mutex<HashMap<ChannelRole, TcpStream>>>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let src = File::open(Path::new(path)).context("Failed to open test.mp3")?;
|
let mut reader = AudioReader::new(path)?;
|
||||||
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 left_codec = OpusCodec::new(config)?;
|
let mut left_codec = OpusCodec::new(config)?;
|
||||||
let mut right_codec = OpusCodec::new(config)?;
|
let mut right_codec = OpusCodec::new(config)?;
|
||||||
@@ -113,76 +88,22 @@ async fn stream_mp3(
|
|||||||
let frame_duration_us =
|
let frame_duration_us =
|
||||||
(config.frame_size as f64 / config.sample_rate as f64 * 1_000_000.0) as u128;
|
(config.frame_size as f64 / config.sample_rate as f64 * 1_000_000.0) as u128;
|
||||||
|
|
||||||
let mut sample_buf = None;
|
while let Some((left_pcm, right_pcm)) = reader.read_chunk(config.frame_size)? {
|
||||||
|
if left_pcm.len() < config.frame_size {
|
||||||
loop {
|
break;
|
||||||
let packet = match format.next_packet() {
|
|
||||||
Ok(packet) => packet,
|
|
||||||
Err(_) => break,
|
|
||||||
};
|
|
||||||
if packet.track_id() != track_id {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let decoded = decoder.decode(&packet)?;
|
send_packets(
|
||||||
if sample_buf.is_none() {
|
&clients,
|
||||||
sample_buf = Some(SampleBuffer::<i16>::new(
|
&mut left_codec,
|
||||||
decoded.capacity() as u64,
|
&mut right_codec,
|
||||||
*decoded.spec(),
|
&left_pcm,
|
||||||
));
|
&right_pcm,
|
||||||
}
|
current_frame_ts,
|
||||||
|
)
|
||||||
if let Some(buf) = sample_buf.as_mut() {
|
.await?;
|
||||||
buf.copy_interleaved_ref(decoded.clone());
|
current_frame_ts += frame_duration_us;
|
||||||
let spec = decoded.spec();
|
pace(current_frame_ts).await;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user