diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6ccdb35 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,38 @@ +[workspace] +resolver = "3" +members = [ + "crates/proto/tsproto-types", + "crates/proto/tsproto-structs", + "crates/proto/tsproto-packets", + "crates/proto/tsproto", + "crates/proto/ts-bookkeeping", + "crates/proto/tsclientlib", + "tools/tsctl", +] +default-members = [ + "crates/proto/tsproto-types", + "crates/proto/tsproto-structs", + "crates/proto/tsproto-packets", + "crates/proto/tsproto", + "crates/proto/ts-bookkeeping", + "crates/proto/tsclientlib", + "tools/tsctl", +] + +[workspace.package] +edition = "2024" +rust-version = "1.94" +license = "MIT OR Apache-2.0" +repository = "https://github.com/Faumaray/teamspeak_client_rs" +publish = false + +[workspace.lints.rust] +unsafe_code = "forbid" + +[workspace.lints.clippy] +all = { level = "warn", priority = -1 } + +[profile.dev.package.num-bigint] +# The Init1 puzzle does a large modular exponentiation on connect; unoptimized +# it takes seconds. +opt-level = 3 diff --git a/crates/proto/UPSTREAM.md b/crates/proto/UPSTREAM.md index 13cb801..aca1ea7 100644 --- a/crates/proto/UPSTREAM.md +++ b/crates/proto/UPSTREAM.md @@ -31,3 +31,9 @@ files in this directory is listed here, newest last, so it can be diffed against or offered back to upstream. +1. **Workspace integration.** Path dependencies point at the flat `crates/proto/` + layout; every crate gets `publish = false`. +2. **Replace `audiopus` with `opus2` 0.4** (`tsclientlib/src/audio.rs`, + `tsclientlib/examples/audio_utils/audio_to_ts.rs`, `tsclientlib/Cargo.toml`). + `audiopus` is unmaintained (RUSTSEC-2026-0150) and breaks with CMake 4. The + `audiopus-unstable` feature (decoder complexity/DRED via a fork) is dropped. diff --git a/crates/proto/ts-bookkeeping/Cargo.toml b/crates/proto/ts-bookkeeping/Cargo.toml index 155fe46..344cdc1 100644 --- a/crates/proto/ts-bookkeeping/Cargo.toml +++ b/crates/proto/ts-bookkeeping/Cargo.toml @@ -8,6 +8,7 @@ readme = "README.md" keywords = ["teamspeak3", "ts3"] license = "MIT OR Apache-2.0" build = "build/build.rs" +publish = false edition = "2018" autobenches = false include = [ diff --git a/crates/proto/tsclientlib/Cargo.toml b/crates/proto/tsclientlib/Cargo.toml index a58f145..2c0b20d 100644 --- a/crates/proto/tsclientlib/Cargo.toml +++ b/crates/proto/tsclientlib/Cargo.toml @@ -8,6 +8,7 @@ readme = "README.md" keywords = ["teamspeak3", "ts3", "voip"] categories = ["multimedia"] license = "MIT OR Apache-2.0" +publish = false edition = "2018" include = [ "/Cargo.toml", @@ -22,15 +23,13 @@ features = ["unstable"] default = ["audio", "default-tls"] # Enable the unstable api unstable = [] -audio = ["audiopus"] +audio = ["opus2"] # Enable default reqwest features. # Use default-features=false on tsclientlib to configure reqwest differently. default-tls = ["reqwest/charset", "reqwest/default-tls", "reqwest/http2"] -# Use API of audiopus fork at https://github.com/Flakebi/audiopus for more features -audiopus-unstable = [] [dependencies] -audiopus = { version = "0.3.0-rc.0", optional = true } +opus2 = { version = "0.4", optional = true } base64 = "0.22" futures = "0.3" git-testament = "0.2" @@ -46,15 +45,15 @@ tokio-stream = "0.1" tracing = "0.1" hickory-net = "0.26" hickory-resolver = "0.26" -ts-bookkeeping = { path = "../utils/ts-bookkeeping", version = "0.1" } +ts-bookkeeping = { path = "../ts-bookkeeping", version = "0.1" } tsproto = { path = "../tsproto", version = "0.2" } -tsproto-packets = { path = "../utils/tsproto-packets", version = "0.1" } -tsproto-types = { path = "../utils/tsproto-types", version = "0.1" } +tsproto-packets = { path = "../tsproto-packets", version = "0.1" } +tsproto-types = { path = "../tsproto-types", version = "0.1" } url = "2" [dev-dependencies] anyhow = "1" -audiopus = "0.3.0-rc.0" +opus2 = "0.4" cpal = "0.18" once_cell = "1" quickcheck = "1" diff --git a/crates/proto/tsclientlib/examples/audio_utils/audio_to_ts.rs b/crates/proto/tsclientlib/examples/audio_utils/audio_to_ts.rs index 7c61b2a..1893fc4 100644 --- a/crates/proto/tsclientlib/examples/audio_utils/audio_to_ts.rs +++ b/crates/proto/tsclientlib/examples/audio_utils/audio_to_ts.rs @@ -2,7 +2,7 @@ use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::{Arc, Mutex}; use anyhow::{Result, format_err}; -use audiopus::coder::Encoder; +use opus2::Encoder; use cpal::traits::{DeviceTrait, StreamTrait}; use cpal::{Device, Stream, StreamConfig}; use futures::prelude::*; @@ -63,9 +63,9 @@ impl AudioToTs { }; let encoder = Encoder::new( - audiopus::SampleRate::Hz48000, - audiopus::Channels::Mono, - audiopus::Application::Voip, + 48_000, + opus2::Channels::Mono, + opus2::Application::Voip, ) .expect("Could not create encoder"); let mut callback = Callback { diff --git a/crates/proto/tsclientlib/src/audio.rs b/crates/proto/tsclientlib/src/audio.rs index 2b6fc34..4fa3c18 100644 --- a/crates/proto/tsclientlib/src/audio.rs +++ b/crates/proto/tsclientlib/src/audio.rs @@ -11,17 +11,14 @@ use std::convert::TryInto; use std::fmt::Debug; use std::hash::Hash; -use audiopus::coder::Decoder; -#[cfg(feature = "audiopus-unstable")] -use audiopus::coder::GenericCtl; -use audiopus::{Channels, SampleRate, packet}; +use opus2::{Channels, Decoder, packet}; use thiserror::Error; use tracing::{Span, debug, info_span, trace, warn}; use tsproto_packets::packets::{AudioData, CodecType, InAudioBuf}; use crate::ClientId; -const SAMPLE_RATE: SampleRate = SampleRate::Hz48000; +const SAMPLE_RATE: u32 = 48_000; const CHANNELS: Channels = Channels::Stereo; const CHANNEL_NUM: usize = 2; /// If this amount of packets is lost consecutively, we assume the stream stopped. @@ -48,17 +45,17 @@ type Result = std::result::Result; #[non_exhaustive] pub enum Error { #[error("Failed to create opus decoder: {0}")] - CreateDecoder(#[source] audiopus::Error), + CreateDecoder(#[source] opus2::Error), #[error("Opus decode failed: {error} (packet: {packet:?})")] Decode { #[source] - error: audiopus::Error, + error: opus2::Error, packet: Option>, }, #[error("Get duplicate packet id {0}")] Duplicate(u16), #[error("Failed to get packet samples: {0}")] - GetPacketSample(#[source] audiopus::Error), + GetPacketSample(#[source] opus2::Error), #[error("Audio queue is full, dropping")] QueueFull, #[error("Audio packet is too late, dropping (wanted {wanted}, got {got})")] @@ -163,9 +160,8 @@ impl SlidingWindowMinimum { impl AudioQueue { fn new(packet: InAudioBuf) -> Result { let data = packet.data().data(); - let opus_packet = data.data().try_into().map_err(Error::GetPacketSample)?; let last_packet_samples = - packet::nb_samples(opus_packet, SAMPLE_RATE).map_err(Error::GetPacketSample)?; + packet::get_nb_samples(data.data(), SAMPLE_RATE).map_err(Error::GetPacketSample)?; if last_packet_samples > MAX_BUFFER_SIZE { return Err(Error::TooManySamples); } @@ -174,16 +170,6 @@ impl AudioQueue { let whispering = matches!(data, AudioData::S2CWhisper { .. }); let decoder = Decoder::new(SAMPLE_RATE, CHANNELS).map_err(Error::CreateDecoder)?; - // Enable DRED and NoLACE, ignore errors e.g. if unsupported - #[cfg(feature = "audiopus-unstable")] - let decoder = { - let mut decoder = decoder; - if let Err(error) = decoder.set_complexity(7) { - debug!(%error, "Failed setting opus decoder complexity, ignoring"); - } - decoder - }; - let mut res = Self { span: Span::current(), decoder, @@ -236,10 +222,8 @@ impl AudioQueue { // End of stream samples = 0; } else { - let opus_packet = - packet.data().data().data().try_into().map_err(Error::GetPacketSample)?; - samples = - packet::nb_samples(opus_packet, SAMPLE_RATE).map_err(Error::GetPacketSample)?; + samples = packet::get_nb_samples(packet.data().data().data(), SAMPLE_RATE) + .map_err(Error::GetPacketSample)?; if samples > MAX_BUFFER_SIZE { return Err(Error::TooManySamples); } @@ -289,12 +273,12 @@ impl AudioQueue { let packet_data; let len; if let Some(p) = packet { - packet_data = - Some(p.packet.data().data().data().try_into().map_err(Error::GetPacketSample)?); + packet_data = p.packet.data().data().data(); len = p.samples; self.whispering = matches!(p.packet.data().data(), AudioData::S2CWhisper { .. }); } else { - packet_data = None; + // An empty slice signals packet loss to the decoder + packet_data = &[]; len = self.last_packet_samples; } self.packet_loss_num += 1; @@ -303,13 +287,7 @@ impl AudioQueue { self.decoded_buffer.resize(orig_buffer_len + len * CHANNEL_NUM, 0.0); let len = self .decoder - .decode_float( - packet_data, - (&mut self.decoded_buffer[orig_buffer_len..]) - .try_into() - .map_err(Error::GetPacketSample)?, - fec, - ) + .decode_float(packet_data, &mut self.decoded_buffer[orig_buffer_len..], fec) .map_err(|e| Error::Decode { error: e, packet: packet.map(|p| p.packet.raw_data().to_vec()), @@ -558,7 +536,7 @@ impl AudioHandler { #[cfg(test)] mod test { use anyhow::{Result, bail}; - use audiopus::coder::Encoder; + use opus2::Encoder; use tsproto_packets::packets::{Direction, OutAudio}; use super::*; @@ -616,9 +594,9 @@ mod test { match a { SimulateAction::CreateEncoder => { encoder = Some(Encoder::new( - audiopus::SampleRate::Hz48000, - audiopus::Channels::Mono, - audiopus::Application::Voip, + 48_000, + opus2::Channels::Mono, + opus2::Application::Voip, )?); } SimulateAction::ReceivePacket(i, success) => { diff --git a/crates/proto/tsproto-packets/Cargo.toml b/crates/proto/tsproto-packets/Cargo.toml index ac30b82..06b6b3a 100644 --- a/crates/proto/tsproto-packets/Cargo.toml +++ b/crates/proto/tsproto-packets/Cargo.toml @@ -7,6 +7,7 @@ repository = "https://github.com/ReSpeak/tsclientlib/tree/master/utils/tsproto-p readme = "README.md" keywords = ["teamspeak3", "ts3"] license = "MIT OR Apache-2.0" +publish = false edition = "2018" include = [ "/Cargo.toml", diff --git a/crates/proto/tsproto-structs/Cargo.toml b/crates/proto/tsproto-structs/Cargo.toml index e44dd2e..3b9a843 100644 --- a/crates/proto/tsproto-structs/Cargo.toml +++ b/crates/proto/tsproto-structs/Cargo.toml @@ -7,6 +7,7 @@ repository = "https://github.com/ReSpeak/tsclientlib/tree/master/utils/tsproto-s readme = "README.md" keywords = ["teamspeak3", "ts3"] license = "MIT OR Apache-2.0" +publish = false edition = "2018" include = [ "/Cargo.toml", diff --git a/crates/proto/tsproto-types/Cargo.toml b/crates/proto/tsproto-types/Cargo.toml index e9ccf5b..08cec73 100644 --- a/crates/proto/tsproto-types/Cargo.toml +++ b/crates/proto/tsproto-types/Cargo.toml @@ -8,6 +8,7 @@ readme = "README.md" keywords = ["teamspeak3", "ts3"] license = "MIT OR Apache-2.0" build = "build/build.rs" +publish = false edition = "2018" include = [ "/Cargo.toml", diff --git a/crates/proto/tsproto/Cargo.toml b/crates/proto/tsproto/Cargo.toml index b4fd1af..d85476d 100644 --- a/crates/proto/tsproto/Cargo.toml +++ b/crates/proto/tsproto/Cargo.toml @@ -8,6 +8,7 @@ readme = "README.md" keywords = ["teamspeak3", "ts3", "voip"] categories = ["multimedia"] license = "MIT OR Apache-2.0" +publish = false edition = "2018" autobenches = false include = [ @@ -38,8 +39,8 @@ thiserror = "2" time = { version = "0.3", features = ["formatting"] } tokio = { version = "1", features = ["macros", "net", "sync", "time"] } tracing = "0.1" -tsproto-packets = { path = "../utils/tsproto-packets", version = "0.1" } -tsproto-types = { path = "../utils/tsproto-types", version = "0.1" } +tsproto-packets = { path = "../tsproto-packets", version = "0.1" } +tsproto-types = { path = "../tsproto-types", version = "0.1" } quicklz = "0.3" [dev-dependencies] diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..082d590 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "1.94" +components = ["rustfmt", "clippy"] diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..ffcef8e --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,5 @@ +# Stable subset of the vendored crates' style (crates/proto/.rustfmt.toml needs nightly). +edition = "2024" +hard_tabs = true +use_small_heuristics = "Max" +newline_style = "Unix"