Expose stream metadata changes only after server acknowledgement

This commit is contained in:
omar committed 2026-11-27 08:29:31 +03:00
1 parent e52db5b112
commit f563d2ba9a
6 files changed
+326 -44

No files matched your search

+70 -16
View File
@@ -3,7 +3,7 @@
//! `TEAMSPEACH_QA_CORE` to the absolute path of that disposable core executable.
//! Duplex: `cargo run --locked -p teamspeach-core --example duplex_qa`.
//! Whisper: `cargo run --locked -p teamspeach-core --example duplex_qa -- --whisper`.
//! Stream registration: `cargo run --locked -p teamspeach-core --example duplex_qa -- --stream-lifecycle`.
//! Stream registration and update: `cargo run --locked -p teamspeach-core --example duplex_qa -- --stream-lifecycle`.
//! Requires an already running, private TeamSpeak fixture at 127.0.0.1:19987 with
//! an empty default Opus channel (TS6 beta14 for whisper and stream registration).
//! No server or audio device is started.
@@ -882,6 +882,55 @@ fn request_stream_event<T>(peer: &mut Peer, tones: &[Tone; HZ.len()], cmd: &str,
}
}
struct StreamMetadata<'a> {
name: &'a str,
bitrate: u32,
viewer_limit: u16,
audio: bool,
}
impl StreamMetadata<'_> {
const TYPE: u64 = 2;
const ACCESSIBILITY: u64 = 1;
const MODE: u64 = 1;
fn args(&self) -> Value {
json!({"name": self.name, "type": Self::TYPE, "accessibility": Self::ACCESSIBILITY,
"mode": Self::MODE, "bitrate": self.bitrate, "viewer_limit": self.viewer_limit, "audio": self.audio})
}
fn verify_list(&self, observer: &mut Peer, tones: &[Tone; HZ.len()], publisher: u64,
stream_id: &str, phase: &str) -> Result<()> {
let audio = if self.audio { "1" } else { "0" };
request_stream_event(observer, tones, "stream_list", json!({"client_id": publisher}),
Some(publisher), "notifystreaminfo", |row| {
if row["id"].as_str() != Some(stream_id) || stream_owner(row) != Some(publisher) {
return None;
}
Some((|| {
for (field, expected) in [("name", self.name), ("audio", audio)] {
let actual = row[field].as_str();
if actual != Some(expected) {
return Err(format!("{field}: expected {expected:?}, reported {actual:?}"));
}
}
for (field, expected) in [("type", Self::TYPE), ("accessibility", Self::ACCESSIBILITY),
("mode", Self::MODE), ("bitrate", u64::from(self.bitrate)),
("viewer_limit", u64::from(self.viewer_limit))] {
let actual = row[field].as_str();
if actual.and_then(|value| value.parse::<u64>().ok()) != Some(expected) {
return Err(format!("{field}: expected {expected}, reported {actual:?}"));
}
}
Ok(())
})())
}).and_then(|metadata| metadata).map_err(|error| format!("{phase} stream discovery: {error}"))?;
println!("observer: fresh {phase} notifystreaminfo matched stream ID and owner, name={:?}, type={}, accessibility={}, mode={}, bitrate={}, viewer_limit={}, audio={audio}",
self.name, Self::TYPE, Self::ACCESSIBILITY, Self::MODE, self.bitrate, self.viewer_limit);
Ok(())
}
}
fn stream_lifecycle(qa: &mut Duplex, name: &str) -> Result<()> {
for peer in [&mut qa.a, &mut qa.b] {
let mute = peer.request(&qa.tones, "set_audio", json!({"input_muted": true, "output_muted": true}), CONTROL)?;
@@ -910,23 +959,28 @@ fn stream_lifecycle(qa: &mut Duplex, name: &str) -> Result<()> {
})?;
println!("connect: private loopback TS6 beta14; two disposable guests, roster IDs verified, both mutes active");
let stream_id = request_stream_event(&mut qa.a, &qa.tones, "stream_setup", json!({
"name": name, "type": 2, "accessibility": 1, "mode": 1, "bitrate": 1000, "viewer_limit": 0, "audio": false,
}), None, "notifystreamstarted", |row| {
let initial = StreamMetadata { name, bitrate: 1000, viewer_limit: 0, audio: false };
let stream_id = request_stream_event(&mut qa.a, &qa.tones, "stream_setup", initial.args(),
None, "notifystreamstarted", |row| {
(stream_owner(row) == Some(publisher)).then(|| row["id"].as_str().filter(|id| !id.is_empty()).map(str::to_owned)).flatten()
})?;
println!("publisher: fresh own notifystreamstarted supplied the registered stream ID");
let listed = request_stream_event(&mut qa.b, &qa.tones, "stream_list", json!({"client_id": publisher}),
Some(publisher), "notifystreaminfo", |row| {
(row["id"].as_str() == Some(stream_id.as_str()) && row["name"].as_str() == Some(name)
&& stream_owner(row) == Some(publisher)).then_some(())
});
if listed.is_ok() {
println!("observer: fresh notifystreaminfo matched the exact registered ID, name, and owner");
}
let checked = (|| {
initial.verify_list(&mut qa.b, &qa.tones, publisher, &stream_id, "initial")?;
// If discovery failed, still unregister the exact ID before disconnecting.
let updated_name = format!("{name} updated");
let updated = StreamMetadata { name: &updated_name, bitrate: 2500, viewer_limit: 3, audio: true };
let mut args = updated.args();
args["stream_id"] = json!(&stream_id);
let reply = qa.a.request_reply(&qa.tones, "stream_update", args, CONTROL, |_| {})?;
require_stream_ack(&qa.a, "stream_update", &reply, None)?;
// Query only after the update ACK; an unsolicited update notification is not discovery.
updated.verify_list(&mut qa.b, &qa.tones, publisher, &stream_id, "updated")
})();
// If discovery or update failed, still unregister the exact ID before disconnecting.
let stopped = (|| {
let since = Instant::now();
let reply = qa.a.request_reply(&qa.tones, "stream_stop", json!({"stream_id": &stream_id}), CONTROL, |_| {})?;
@@ -941,9 +995,9 @@ fn stream_lifecycle(qa: &mut Duplex, name: &str) -> Result<()> {
println!("observer: fresh notifystreamstopped matched by {matched_by}");
Ok(())
})();
match (listed, stopped) {
match (checked, stopped) {
(Ok(()), result) | (result, Ok(())) => result,
(Err(list), Err(stop)) => Err(format!("{list}; {stop}")),
(Err(check), Err(stop)) => Err(format!("{check}; {stop}")),
}
}
@@ -973,7 +1027,7 @@ fn exercise_stream_lifecycle(exe: &Path, root: &Path) -> Result<String> {
if !errors.is_empty() {
return Err(errors.join("; "));
}
Ok(format!("Stream registration IPC passed. Both cores shut down; server confirmed {confirmed}/2 disconnects. No media or device audio was used."))
Ok(format!("Stream lifecycle IPC passed: registration, metadata update, and stop. Both cores shut down; server confirmed {confirmed}/2 disconnects. No media or device audio was used."))
}
fn run() -> Result<String> {