diff --git a/crates/voelin-core/src/link/tests.rs b/crates/voelin-core/src/link/tests.rs index 3bbc2e1..751703f 100644 --- a/crates/voelin-core/src/link/tests.rs +++ b/crates/voelin-core/src/link/tests.rs @@ -1433,3 +1433,44 @@ async fn the_engine_consumes_any_session_a_connector_makes() { assert_eq!(reason, None); assert_eq!(connector.connects().len(), 1); } + +/// What the second adapter costs per voice packet (the last row of the risk +/// table in `docs/session-seam.md`): every packet is framed as RTP and taken +/// apart again on its way, in each direction. A generous bound in a debug +/// build; the figures are printed. +#[tokio::test(flavor = "multi_thread")] +async fn a_voice_packet_costs_microseconds_through_both_adapters() { + const PACKETS: usize = 5_000; + let mut rig = Rig::new(everything()).await; + let started = std::time::Instant::now(); + for _ in 0..PACKETS { + rig.give(LinkCmd::Audio(EncodedVoice { + codec: VoiceCodec::Voice, + payload: OPUS_20_MS.to_vec(), + })); + } + for _ in 0..PACKETS { + assert!(matches!(rig.inner_cmd().await, LinkCmd::Audio(_))); + } + let uplink = started.elapsed() / PACKETS as u32; + + let started = std::time::Instant::now(); + for seq in 0..PACKETS { + rig.play(LinkEvent::Audio(VoicePacket { + talker: TalkerId(8), + seq: seq as u16, + codec: VoiceCodec::Voice, + whisper: false, + payload: OPUS_20_MS.to_vec(), + })); + } + let mut heard = 0; + while heard < PACKETS { + rig.event(|e| matches!(e, LinkEvent::Audio(_)).then_some(())).await; + heard += 1; + } + let downlink = started.elapsed() / PACKETS as u32; + eprintln!("voice through both adapters: {uplink:?} per packet up, {downlink:?} down"); + assert!(uplink < Duration::from_micros(500), "{uplink:?}"); + assert!(downlink < Duration::from_micros(500), "{downlink:?}"); +} diff --git a/crates/voelin-core/src/ts_session/mod.rs b/crates/voelin-core/src/ts_session/mod.rs index e5c6212..ccd4b1c 100644 --- a/crates/voelin-core/src/ts_session/mod.rs +++ b/crates/voelin-core/src/ts_session/mod.rs @@ -147,10 +147,9 @@ fn inner_options(params: &ConnectParams, ctx: &LocalCtx) -> Result link, _ => { - return Err(SessionError::Protocol(format!( - "not a TeamSpeak server address: {}", - params.address - ))); + // Without the query: a channel password rides in it. + let shown = params.address.split('?').next().unwrap_or_default(); + return Err(SessionError::Protocol(format!("not a TeamSpeak server address: {shown}"))); } }; let identity = match ¶ms.credential { diff --git a/crates/voelin-core/tests/session_backends.rs b/crates/voelin-core/tests/session_backends.rs new file mode 100644 index 0000000..a6a46da --- /dev/null +++ b/crates/voelin-core/tests/session_backends.rs @@ -0,0 +1,124 @@ +//! The engine on a `voelin_proto::link::Session` that no TeamSpeak code +//! made: `Engine::start_with_backends` takes the connector registry, and the +//! scheme of a session's address picks the connector. The fake host of the +//! protocol repository stands for a Voelin host here (the fuller tests of the +//! seam on the fake host come with the next slice of +//! `docs/session-seam.md`). + +use std::sync::Arc; +use std::time::Duration; + +use tokio::sync::broadcast; +use voelin_core::settings::Settings; +use voelin_core::{ + Backends, Command, Engine, Event, History, SessionState, VoiceOptions, VoiceState, +}; +use voelin_proto::test_util::FakeConnector; +use voelin_proto::v1::{self, entity::Kind}; + +const SESSION: u64 = 1; + +async fn next( + events: &mut broadcast::Receiver, + mut f: impl FnMut(Event) -> Option, +) -> T { + tokio::time::timeout(Duration::from_secs(10), async { + loop { + if let Some(t) = f(events.recv().await.expect("engine events")) { + return t; + } + } + }) + .await + .expect("an event in time") +} + +fn host_with_two_channels(connector: &FakeConnector) { + let host = connector.host(); + let put = |kind: Kind| host.put(v1::Entity { kind: Some(kind) }).unwrap(); + put(Kind::Server(v1::Server { + id: 1, + name: "Fake Host".into(), + uid: "fake-host".into(), + default_channel_id: 10, + ..Default::default() + })); + for (id, name, position) in [(10, "Lobby", 0), (11, "Games", 1024)] { + put(Kind::Channel(v1::Channel { + id, + server_id: 1, + kind: v1::ChannelKind::Voice as i32, + name: name.into(), + position, + ..Default::default() + })); + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_session_of_a_registered_connector_is_the_engines_voice_source() { + let connector = FakeConnector::new(); + host_with_two_channels(&connector); + let mut backends = Backends::empty(); + backends.register(Arc::new(connector.clone())); + let engine = Engine::start_with_backends(Settings::in_memory(), History::in_memory(), backends); + let mut events = engine.subscribe(); + + engine.send(Command::ConnectVoice { + session: SESSION, + options: Box::new(VoiceOptions::new("fake://host", "tester")), + }); + let (name, backend) = next(&mut events, |e| match e { + Event::ServerInfo { name, backend, .. } => Some((name, backend)), + _ => None, + }) + .await; + assert_eq!((name.as_str(), backend.product.as_str()), ("Fake Host", "voelin-fake")); + let presence = next(&mut events, |e| match e { + Event::Presence { presence, .. } if !presence.channels.is_empty() => Some(presence), + _ => None, + }) + .await; + let channels: Vec<_> = + presence.channels.values().map(|c| (c.id, c.name.as_str(), c.order)).collect(); + assert_eq!(channels, [(10, "Lobby", 0), (11, "Games", 10)]); + // The host was asked once, with the nickname and the TeamSpeak-shaped + // credential the engine makes of its options for now. + let connects = connector.connects(); + assert_eq!(connects.len(), 1); + assert_eq!(connects[0].address, "fake://host"); + assert_eq!(connects[0].hello.nickname, "tester"); + + // A move is a request to the host; the host's voice state is where we are. + engine.send(Command::MoveToChannel { session: SESSION, channel: 11, password: None }); + let state: SessionState = next(&mut events, |e| match e { + Event::State { state, .. } if state.own_channel == Some(11) => Some(state), + _ => None, + }) + .await; + assert_eq!(state.voice, VoiceState::Connected); + + engine.send(Command::DisconnectVoice { session: SESSION }); + next(&mut events, |e| match e { + Event::State { state, .. } if state.voice == VoiceState::Disconnected => Some(()), + _ => None, + }) + .await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_address_no_connector_serves_fails_with_the_registry_s_words() { + let engine = + Engine::start_with_backends(Settings::in_memory(), History::in_memory(), Backends::empty()); + let mut events = engine.subscribe(); + engine.send(Command::ConnectVoice { + session: SESSION, + options: Box::new(VoiceOptions::new("voelin://host/i/code", "tester")), + }); + let error = next(&mut events, |e| match e { + Event::Error { message, .. } => Some(message), + _ => None, + }) + .await; + assert_eq!(error, "voice: no connector for voelin://host/i/code"); +} diff --git a/docs/session-seam.md b/docs/session-seam.md index b57a88b..2fb0086 100644 --- a/docs/session-seam.md +++ b/docs/session-seam.md @@ -843,7 +843,7 @@ Nothing here blocks this half: each has a workaround above. | The inner bus of `LinkSession` lags and a request never gets its answer. | Capacity 4096, a timeout on every pending request, lag logged. | | A uid that round-trips through `User.uid` on one side and through `Foreign` on the other (F10) would key contacts and private history differently. | Both sides use one rule (canonical base64 → bytes, anything else → `Foreign`), asserted by the round-trip test on TeamSpeak 3 and 6 uids, ServerQuery login names and the FakeLink fixtures' `alice=`; checked live (contacts and private history keyed by uid). | | The model crate switch collides with other branches. | 11.3; the switch is manifest-only. | -| Two adapters cost CPU per voice packet (RTP wrap and unwrap in process). | About a hundred bytes copied per 20 ms per talker; measured once in slice 1 against the baseline's CPU with the smoke's voice scenario. | +| Two adapters cost CPU per voice packet (RTP wrap and unwrap in process). | About a hundred bytes copied per 20 ms per talker; measured in slice 1 by a unit test (`a_voice_packet_costs_microseconds_through_both_adapters`, section 13). | ## 13. Slice 1 as implemented @@ -924,7 +924,18 @@ What ran against the baseline (main `59fec5b`; the dev servers of `cargo test -p voelin-core` has two failures that exist on the baseline too (`discover::tests::without_records_the_gateway_itself_is_asked`, -`web::tests::pictures_go_through_the_proxy_named_for_each_hop`: they need -the network). Not measured: the CPU cost of the second adapter per voice -packet (section 12); the packets are a hundred bytes each way every 20 ms -per talker. +`web::tests::pictures_go_through_the_proxy_named_for_each_hop`: both reach a +server bound to `127.0.0.1` through the name `localhost`, which resolves to +`::1` only on the machine the tests ran on; the modules are unchanged). + +The CPU cost of the second adapter per voice packet (section 12) is measured +by the unit test `a_voice_packet_costs_microseconds_through_both_adapters` +(`link/tests.rs`): 5,000 Opus packets of 20 ms each way through both +adapters and the recording link, in a debug build, took about 7 microseconds +per packet to the host and 10 microseconds from it. At one talker that is +well under a tenth of a percent of a core; the test bounds each direction at +500 microseconds. `tests/session_backends.rs` adds two integration tests: +the engine over the protocol repository's `FakeConnector` through +`Engine::start_with_backends` (connect, the server's name, the channels, a +move, disconnect), and the registry's error text for an address no connector +serves.