Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/check_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
problems.append("repository: your plugin's GitHub URL, in Cargo.toml")
if not m.get("subscribe"):
problems.append("subscribe: a plugin that subscribes to nothing never hears anything")
known = {"call.start", "call.end", "call.concluded", "unit", "audio", "status"}
known = {"call.start", "call.end", "call.update", "call.concluded", "unit", "audio", "voice", "status", "totals", "tone_pattern", "log"}
for t in m.get("subscribe", []):
if t not in known:
problems.append(f"subscribe: unknown topic {t!r}")
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [0.1.7]

- Audio packets with JSON now carry `patched_talkgroup_tags` (the call's own talkgroup's tag, then those of the
talkgroups patched with it; none when no tag is known), as Trunk Recorder's simplestream can, so a receiver that
names a transmission by them needs no call start packets. A call start's list now has every patched talkgroup's tag,
not only the call's own.
- `src` and `src_tag` (the radio talking, and its name, from the call's sources) follow the call as it's updated,
as Trunk Recorder's simplestream does, instead of staying as they were when the call started. `src_tag` was
always empty before.
- Built with trunk-recorder-plugin 0.15.1, which names the talkgroups patched with a call (`CallInfo.patched`).

## [0.1.5]

- Built with trunk-recorder-plugin 0.5.0, for Trunk Recorder Pro v0.2.0: systems are numbered
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "simplestream"
version = "0.1.6"
version = "0.1.7"
edition = "2024"
description = "Streams the audio of calls as they're recorded to other programs, over UDP or TCP."
license = "GPL-3.0-or-later"
Expand All @@ -9,7 +9,7 @@ authors = ["Luke Berndt <lukekb@gmail.com>"]
publish = false

[dependencies]
trunk-recorder-plugin = "0.13.0"
trunk-recorder-plugin = "0.15.1"
schemars = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ Each packet is one slice of audio, with, depending on the settings:
- **JSON:** its length in 4 bytes, little-endian, then the JSON, then the audio:

```json
{"audio_sample_rate":8000,"event":"audio","freq":851012500,"patched_talkgroups":[101],
"short_name":"county","src":1234,"src_tag":"","talkgroup":101}
{"audio_sample_rate":8000,"event":"audio","freq":851012500,"patched_talkgroup_tags":["Fire Dispatch"],
"patched_talkgroups":[101],"short_name":"county","src":1234,"src_tag":"","talkgroup":101}
```

`patched_talkgroup_tags` is the tag of the call's talkgroup, then of each one
patched with it (one with no tag is left out); it's missing when none has a tag.

A call start is the same with `"event":"call_start"` (plus `talkgroup_tag`
and `patched_talkgroup_tags`) and no audio; a call end has `talkgroup`,
`patched_talkgroups`, `freq`, `short_name` and `"event":"call_end"`.
Expand Down
111 changes: 104 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Plugin for SimpleStream {
fn manifest() -> Manifest {
Manifest {
name: "simplestream".into(),
subscribe: vec![topic::AUDIO.into(), topic::CALL_START.into(), topic::CALL_END.into()],
subscribe: vec![topic::AUDIO.into(), topic::CALL_START.into(), topic::CALL_UPDATE.into(), topic::CALL_END.into()],
// A system can have streams of its own.
system_overrides: vec!["streams".into()],
..trunk_recorder_plugin::manifest!()
Expand Down Expand Up @@ -176,12 +176,12 @@ impl Plugin for SimpleStream {
}
let short_name = call.short_name.clone();
let talkgroups = talkgroups(&call);
let tags = tags(&call);
for o in self.outs.iter().filter(|o| o.stream.send_json && o.stream.send_call_start && o.wants_system(&short_name)) {
if o.stream.talkgroup_of(&talkgroups).is_some() {
let tags: Vec<&str> = if call.talkgroup_tag.is_empty() { vec![] } else { vec![call.talkgroup_tag.as_str()] };
let j = json!({
"src": src(&call),
"src_tag": "",
"src_tag": src_tag(&call),
"talkgroup": call.talkgroup,
"talkgroup_tag": call.talkgroup_tag,
"patched_talkgroups": talkgroups,
Expand All @@ -196,6 +196,13 @@ impl Plugin for SimpleStream {
self.calls.insert(call.id, call);
}

/// The call as it is now: the radio talking, and its name, as the recorder has them.
fn call_update(&mut self, call: CallInfo) {
if let Some(c) = self.calls.get_mut(&call.id) {
*c = call;
}
}

fn audio(&mut self, chunk: AudioChunk) {
let pcm = base64::decode(&chunk.pcm);
// A call heard on several sites plays from whichever copy has it: one that wasn't
Expand All @@ -204,19 +211,24 @@ impl Plugin for SimpleStream {
let call = self.calls.get(&chunk.call_id).or_else(twin);
let short_name = chunk.short_name.as_str();
let talkgroups = call.map_or(vec![chunk.talkgroup], talkgroups);
let tags = call.map_or(vec![], tags);
for o in self.outs.iter().filter(|o| o.wants_system(short_name)) {
let Some(tg) = o.stream.talkgroup_of(&talkgroups) else { continue };
let p = if o.stream.send_json {
let j = json!({
let mut j = json!({
"src": call.map_or(-1, src),
"src_tag": "",
"src_tag": call.map_or(String::new(), src_tag),
"talkgroup": tg,
"patched_talkgroups": talkgroups,
"freq": call.map_or(0, |c| c.freq_hz),
"short_name": short_name,
"audio_sample_rate": chunk.sample_rate,
"event": "audio",
});
// (Left out when there are none, as Trunk Recorder's is: a receiver falls back to the numbers.)
if !tags.is_empty() {
j["patched_talkgroup_tags"] = json!(tags);
}
packet(Some(&j), None, &pcm)
} else {
packet(None, o.stream.send_tgid.then_some(tg), &pcm)
Expand Down Expand Up @@ -264,11 +276,23 @@ fn talkgroups(c: &CallInfo) -> Vec<u32> {
v
}

/// The tags of [`talkgroups`], in its order: the call's own, then each one patched with it
/// (a talkgroup with no tag is left out, as Trunk Recorder leaves it).
fn tags(c: &CallInfo) -> Vec<String> {
std::iter::once(&c.talkgroup_tag).chain(c.patched.iter().map(|p| &p.alpha_tag)).filter(|t| !t.is_empty()).cloned().collect()
}

/// The radio talking (the last heard), or -1.
fn src(c: &CallInfo) -> i64 {
c.units.last().map_or(-1, |&u| u as i64)
}

/// The name of the radio talking, as [`src`] names it, from the call's sources; empty when none is known.
fn src_tag(c: &CallInfo) -> String {
let Some(&u) = c.units.last() else { return String::new() };
c.sources.iter().rev().find(|s| s.unit == u).map(|s| s.tag.clone()).unwrap_or_default()
}

/// A packet: [JSON length (u32 LE), JSON] or [talkgroup (u32 LE)], then the samples.
fn packet(j: Option<&Value>, tgid: Option<u32>, pcm: &[u8]) -> Vec<u8> {
let mut p = Vec::with_capacity(pcm.len() + 256);
Expand Down Expand Up @@ -299,9 +323,10 @@ mod tests {
use std::io::Read;
use std::net::{TcpListener, UdpSocket};
use trunk_recorder_plugin::testing;
use trunk_recorder_plugin::{EXIT_CONFIG, HostMessage};
use trunk_recorder_plugin::{CallSource, EXIT_CONFIG, HostMessage, TalkgroupName};

fn call(id: u32, tg: u32, patched: Vec<u32>) -> CallInfo {
let named = patched.iter().filter(|&&t| t != tg).map(|&t| TalkgroupName { talkgroup: t, alpha_tag: format!("TG {t}") }).collect();
CallInfo {
id,
system: 0,
Expand All @@ -312,6 +337,7 @@ mod tests {
recording: true,
units: vec![1234],
patched_talkgroups: patched,
patched: named,
..Default::default()
}
}
Expand Down Expand Up @@ -408,11 +434,14 @@ mod tests {
assert_eq!(start["event"], "call_start");
assert_eq!(start["talkgroup"], 101);
assert_eq!(start["patched_talkgroups"], json!([101, 202]));
assert_eq!(start["talkgroup_tag"], "TG 101");
assert_eq!(start["patched_talkgroup_tags"], json!(["TG 101", "TG 202"]));
assert_eq!(start["src"], 1234);
assert_eq!(start["short_name"], "sys1");
let (a, pcm) = split(&recv(&s).unwrap());
assert_eq!(a["event"], "audio");
// The talkgroup this stream is for.
// The talkgroup this stream is for, and the tags a call start has.
assert_eq!(a["patched_talkgroup_tags"], start["patched_talkgroup_tags"]);
assert_eq!(a["talkgroup"], 202);
assert_eq!(a["audio_sample_rate"], 8000);
assert_eq!(a["freq"], 851012500u64);
Expand All @@ -422,6 +451,74 @@ mod tests {
assert_eq!(e["patched_talkgroups"], json!([101, 202]));
}

/// A call's update says who is talking now: the audio after it names that radio, as the
/// recorder's simplestream does; the call's start didn't know them.
#[test]
fn the_talking_radio_follows_the_call() {
let (s, url) = udp();
let mut later = call(1, 101, vec![]);
later.units = vec![1234, 5678];
later.sources = vec![
CallSource { unit: 1234, tag: "Engine 5".into(), ..Default::default() },
CallSource { unit: 5678, tag: "Medic 2".into(), ..Default::default() },
];
testing::run::<SimpleStream>([
hello(json!([{ "url": url, "TGID": 0, "sendJSON": true }])),
HostMessage::CallStart(call(1, 101, vec![])),
HostMessage::CallUpdate(later),
audio(1, 101, &[1]),
]);
let (a, _) = split(&recv(&s).unwrap());
assert_eq!((a["src"].clone(), a["src_tag"].clone()), (json!(5678), json!("Medic 2")), "{a}");
}

/// Audio alone (no call start packets) names its talkgroups: the call's own when it isn't
/// patched, each one patched with it when it is. A receiver that names a transmission by them
/// would otherwise be left with the numbers; with none known, the key is left out.
#[test]
fn audio_names_its_talkgroups() {
let (s, url) = udp();
let mut untagged = call(3, 303, vec![]);
untagged.talkgroup_tag = String::new();
// (Patched with 202, which has no tag: it's left out.)
let mut half = call(4, 404, vec![202, 404]);
half.patched[0].alpha_tag = String::new();
testing::run::<SimpleStream>([
hello(json!([{ "url": url, "TGID": 0, "sendJSON": true }])),
HostMessage::CallStart(call(1, 101, vec![])),
HostMessage::CallStart(call(2, 101, vec![101, 202])),
HostMessage::CallStart(untagged),
HostMessage::CallStart(half),
audio(1, 101, &[1]),
audio(2, 101, &[1]),
audio(3, 303, &[1]),
audio(4, 404, &[1]),
]);
let tags: Vec<Value> = (0..4).map(|_| split(&recv(&s).unwrap()).0.get("patched_talkgroup_tags").cloned().unwrap_or(Value::Null)).collect();
assert_eq!(tags, [json!(["TG 101"]), json!(["TG 101", "TG 202"]), Value::Null, json!(["TG 404"])]);
}

/// The radio talking (the last heard) is named by the call's sources, on call start and audio.
#[test]
fn the_talking_radio_is_named() {
let (s, url) = udp();
let mut c = call(1, 101, vec![]);
c.units = vec![1234, 5678];
c.sources = vec![
CallSource { unit: 1234, tag: "Engine 5".into(), ..Default::default() },
CallSource { unit: 5678, tag: "Medic 2".into(), ..Default::default() },
];
testing::run::<SimpleStream>([
hello(json!([{ "url": url, "TGID": 0, "sendJSON": true, "sendCallStart": true }])),
HostMessage::CallStart(c),
audio(1, 101, &[1]),
]);
let (start, _) = split(&recv(&s).unwrap());
assert_eq!((start["src"].clone(), start["src_tag"].clone()), (json!(5678), json!("Medic 2")), "{start}");
let (a, _) = split(&recv(&s).unwrap());
assert_eq!((a["src"].clone(), a["src_tag"].clone()), (json!(5678), json!("Medic 2")), "{a}");
}

#[test]
fn other_systems_are_left_out() {
let (s, url) = udp();
Expand Down
Loading