fix(twin): mirror inbound trunk audio across the twin link

When an external PAIRED=1 peer transmits to a twin pair, it picks a
single sticky socket per transmission and sends every frame on it. The
sticky-selected twin's TrunkLink::handleMsgPeerAudio used to broadcast
locally and to satellites but never relayed to the partner twin — so
local clients on the non-sticky twin were deaf to the audio even though
the talker state was mirrored correctly. The satellite-receive path
already had the symmetric forwarder (forwardSatelliteRawAudioToTwin);
the trunk path was just missing it.

Add Reflector::forwardTrunkAudioToTwin and forwardTrunkFlushToTwin,
modelled on the satellite variants, and invoke them from
TrunkLink::handleMsgPeerAudio and handleMsgPeerFlush after the local +
satellite broadcasts. Loop safety: TwinLink::handleMsgPeerAudio does
not re-forward to trunks, so there is no echo back to the originating
peer. Requires the external peer to use PAIRED=1 with both twin
hostnames in a single section — two independent trunk sections would
defeat the source-trunk exclusion at the external owner and cause
duplicates/echo. The docs now flag PAIRED=1 as mandatory for this
topology.

Test: test_twin.py::test_X4_paired_inbound_reaches_both_twins drives a
refa client on TG 26201 and asserts both ref1 and ref2 receive audio
frames and flush. Also rewrite test_01_twin_handshake and
test_05_twin_wiring_no_errors to read /status.twin instead of scraping
a 120s docker-logs window — the previous form flaked when the mesh had
been up long enough for boot-time log lines to scroll off.
This commit is contained in:
Audric committed 2026-05-14 19:37:44 +02:00
1 parent ef42da52de
commit 6a6cfadd95
6 files changed
+215 -35

No files matched your search

+10
View File
@@ -360,6 +360,16 @@ both endpoints in their `HOSTS` setting for transparent failover. See
sticky-selects one per frame, with instant failover on socket failure.
```
> **Mandatory: every external reflector trunking to a twin pair MUST use
> `PAIRED=1` with both twin hostnames in a single section.** Configuring
> two independent trunk sections (one per twin) is a misconfiguration:
> the external peer's owner-relay no longer sees the pair as one logical
> entity, so source-trunk exclusion fails and audio is delivered both via
> the second trunk *and* via the intra-pair twin-mirror — causing
> duplicates on the partner twin and an echo back to the original
> talker. The `PAIRED=1` form is the only way to keep the pair
> single-logical from outside.
**What's different from trunk and satellite.**
| Property | Trunk | Satellite | Twin |
+24 -2
View File
@@ -180,8 +180,8 @@ holdoff.
### External peer view
An external peer (e.g. refA) that wants to trunk with a twin pair
declares a **single** `[TRUNK_x]` section with **multiple candidate
hosts** — one per twin — and a `PAIRED=1` flag:
**MUST** declare a **single** `[TRUNK_x]` section with **multiple
candidate hosts** — one per twin — and a `PAIRED=1` flag:
```ini
[TRUNK_IT_DE]
@@ -197,6 +197,15 @@ them in parallel. This requires extending `TrunkLink` to manage a list
of TCP endpoints instead of a single outbound + inbound pair (see
§Implementation).
> **Do not configure two separate trunk sections (one per twin).** That
> form is a misconfiguration: the external peer sees the pair as two
> independent peers, so its owner-relay (`forwardTrunkAudioToOtherTrunks`)
> only excludes the source twin and still forwards back to the partner
> twin — which *also* receives the audio via the intra-pair twin-mirror
> (`forwardTrunkAudioToTwin`). The result is a duplicate on the partner
> and an echo back to the original talker. `PAIRED=1` is the only
> supported topology for "external reflector ↔ twin pair".
### Sticky per-frame routing
The external peer picks **one socket at a time** for a given transmission
@@ -227,6 +236,19 @@ from keying up 262xx, just as they would be if they were on the
receiving twin. Loop suppression: a twin does not re-mirror external
state it learned *via* the twin link.
### Cross-twin external audio relay
State alone is not enough — the partner twin's local clients must also
*hear* the inbound audio. `TrunkLink::handleMsgPeerAudio` therefore
forwards the audio across the twin link via
`Reflector::forwardTrunkAudioToTwin` (same shape as the existing
satellite-to-twin mirror), reusing `MsgPeerAudio` on the [TWIN] socket.
`TrunkLink::handleMsgPeerFlush` mirrors the matching flush. The
receiving partner's `TwinLink::handleMsgPeerAudio` broadcasts to its
local clients on the TG and fans the audio out to its own satellites;
it does *not* re-forward to trunk peers, so there is no echo back to
refA.
### refA-side socket selection policy
Initial socket choice: whichever TCP connection succeeds first.
+19
View File
@@ -2855,6 +2855,25 @@ void Reflector::forwardSatelliteFlushToTwin(uint32_t tg)
} /* Reflector::forwardSatelliteFlushToTwin */
void Reflector::forwardTrunkAudioToTwin(uint32_t tg,
const std::vector<uint8_t>& audio)
{
if (m_twin_link != nullptr)
{
m_twin_link->onLocalAudio(tg, audio);
}
} /* Reflector::forwardTrunkAudioToTwin */
void Reflector::forwardTrunkFlushToTwin(uint32_t tg)
{
if (m_twin_link != nullptr)
{
m_twin_link->onLocalFlush(tg);
}
} /* Reflector::forwardTrunkFlushToTwin */
void Reflector::forwardAudioToSatellitesExcept(SatelliteLink* except,
uint32_t tg, const std::vector<uint8_t>& audio)
{
+10
View File
@@ -284,6 +284,16 @@ class Reflector : public sigc::trackable
void forwardTrunkTalkerStopToOtherTrunks(const TrunkLink* src,
uint32_t tg);
// Mirror inbound trunk audio/flush across the [TWIN] link. Without this,
// when an external PAIRED peer's sticky socket lands on one twin, only
// that twin's local clients hear the audio — partner-twin clients on the
// same shared LOCAL_PREFIX would be silent. Parallels the satellite
// forwarders just above; loop safety relies on TwinLink::handleMsgPeerAudio
// not re-forwarding to trunks.
void forwardTrunkAudioToTwin(uint32_t tg,
const std::vector<uint8_t>& audio);
void forwardTrunkFlushToTwin(uint32_t tg);
RedisStore* redisStore(void) const { return m_redis; }
MqttPublisher* mqtt(void) const { return m_mqtt; }
+9
View File
@@ -1170,6 +1170,12 @@ void TrunkLink::handleMsgPeerAudio(std::istream& is)
// Forward trunk audio to connected satellites
m_reflector->forwardAudioToSatellitesExcept(nullptr, local_tg, msg.audio());
// Mirror inbound trunk audio to the [TWIN] partner so PAIRED inbound
// (sticky on one twin) still reaches the non-sticky twin's local clients
// and its satellites. TwinLink::handleMsgPeerAudio does not re-forward to
// trunks, so no echo back to the originating peer.
m_reflector->forwardTrunkAudioToTwin(local_tg, msg.audio());
// Prefix-routed fanout (mirrors talker-start): only fan out if we have
// a route. Cluster TGs without a prefix match stay single-hop.
if (m_reflector->shouldRelayInbound(local_tg))
@@ -1212,6 +1218,9 @@ void TrunkLink::handleMsgPeerFlush(std::istream& is)
// Forward trunk flush to connected satellites
m_reflector->forwardFlushToSatellitesExcept(nullptr, local_tg);
// Mirror flush to the [TWIN] partner — pairs with the audio mirror above.
m_reflector->forwardTrunkFlushToTwin(local_tg);
// Prefix-routed fanout (mirrors talker-start): only fan out if we have
// a route. Cluster TGs without a prefix match stay single-hop.
if (m_reflector->shouldRelayInbound(local_tg))
+143 -33
View File
@@ -184,21 +184,24 @@ class TestTwinIntegration(unittest.TestCase):
# Test 1: Twin handshake — ref1 and ref2 see each other
# ------------------------------------------------------------------
def test_01_twin_handshake(self):
"""Both twin peers log a successful TWIN hello exchange.
"""Both twin peers report a completed TWIN hello exchange in /status.
Verifies that ref1 and ref2 each saw the 'TWIN: hello from partner'
log line, confirming the twin handshake completed on both sides.
Uses the persistent status field rather than scraping logs, so the
test is robust to long-running test meshes where the boot-time
'TWIN: hello from partner' line has scrolled past the docker-logs
rolling window.
"""
for name in ("ref1", "ref2"):
logs = docker_logs(name, since="120s")
# Look for 'TWIN: hello from partner' — emitted by TwinLink.cpp
# after successful HMAC verification on inbound or outbound path
self.assertIn(
"TWIN: hello from partner",
logs,
f"reflector-{name} logs do not show 'TWIN: hello from "
f"partner'. Twin handshake may have failed.\n"
f"Relevant log tail:\n{logs[-2000:]}",
def hello_done(_n=name):
twin = get_status(*_http(_n)).get("twin", {})
return bool(twin.get("outbound_hello")
or twin.get("inbound_hello"))
wait_until(
hello_done,
timeout=15.0,
interval=0.5,
msg=f"reflector-{name} /status.twin shows no completed "
f"hello (outbound_hello=False AND inbound_hello=False)",
)
# ------------------------------------------------------------------
@@ -265,36 +268,42 @@ class TestTwinIntegration(unittest.TestCase):
)
# ------------------------------------------------------------------
# Test 5: Twin link wiring — no startup errors on ref1/ref2
# Test 5: Twin link wiring — server bound + no ERROR[TWIN] lines
# ------------------------------------------------------------------
def test_05_twin_wiring_no_errors(self):
"""ref1 and ref2 startup logs contain no twin-related ERROR lines.
"""ref1 and ref2 have a listening twin server and no ERROR[TWIN].
This is a smoke test: if the [TWIN] section is mis-parsed or the
listen port fails to bind, an ERROR[TWIN] line will appear.
Server-listen evidence comes from /status: if the partner reports
inbound_connected=True, *our* twin server bound its port and
accepted the partner's outbound connection. Status survives the
docker-logs rolling window (unlike the boot-time
'TWIN: server listening' log line).
NOTE: A fuller audio-mirror test (Phase C1 verification) would
require sending audio via a mock SvxLink client and verifying the
mirrored talker state on the twin partner. That is out of scope for
this log-based integration suite and should be covered by a dedicated
E2E audio test when mock clients are available.
The ERROR[TWIN] scan is still log-based but uses the full container
log (no --since) so startup errors are caught even when the test
runs late in a long suite.
"""
for name in ("ref1", "ref2"):
logs = docker_logs(name, since="120s")
# Any ERROR[TWIN] line is a fatal wiring problem
# Server-bind evidence: partner has us as inbound_connected.
def server_listening(_n=name):
twin = get_status(*_http(_n)).get("twin", {})
return bool(twin.get("inbound_connected"))
wait_until(
server_listening,
timeout=15.0,
interval=0.5,
msg=f"reflector-{name} /status.twin.inbound_connected is "
f"False — the partner cannot reach our twin server, "
f"so the listen socket probably failed to bind",
)
# Fatal wiring errors: scan the full container log (no since).
logs = docker_logs(name, since="24h")
error_match = re.search(r"\*\*\* ERROR\[TWIN\]", logs)
self.assertIsNone(
error_match,
f"reflector-{name} has ERROR[TWIN] in startup logs:\n"
f" {error_match.group(0) if error_match else ''}\n"
f"Full log tail:\n{logs[-1500:]}",
)
# Twin server listen line should be present
self.assertIn(
"TWIN: server listening on port",
logs,
f"reflector-{name} does not show 'TWIN: server listening' "
f"— the twin TCP server may not have started.",
f"reflector-{name} has ERROR[TWIN] in container logs:\n"
f" {error_match.group(0) if error_match else ''}",
)
# ------------------------------------------------------------------
@@ -1021,6 +1030,107 @@ TOPIC_PREFIX=svxreflector/testnode
sat.close()
receiver.close()
# ------------------------------------------------------------------
# Test X4: PAIRED inbound trunk audio reaches BOTH twins
# ------------------------------------------------------------------
def test_X4_paired_inbound_reaches_both_twins(self):
"""A client on refa transmits on a 262-prefixed TG. Clients on
ref1 AND ref2 must both receive UDP audio.
refa's TRUNK_IT_DE is PAIRED=1, so it picks a single sticky socket
(either ref1 or ref2) for every frame of the transmission. Without
the trunk->twin mirror, only the sticky-selected twin's clients
would hear the audio — the partner twin would be silent on the
same shared LOCAL_PREFIX.
Path under test (sticky lands on twin A; B is partner):
refa client UDP -> refa Reflector
-> TrunkLink::onLocalAudio (PAIRED sticky pick)
-> twinA TrunkLink::handleMsgPeerAudio
-> broadcastUdpMsg -> twinA receiver UDP socket
-> (NEW) forwardTrunkAudioToTwin
-> twinA m_twin_link->onLocalAudio
-> MsgPeerAudio over [TWIN]
-> twinB TwinLink::handleMsgPeerAudio
-> broadcastUdpMsg -> twinB receiver UDP socket
"""
tg = 26201 # prefix 262, owned by both twins
refa_client_port = T.twin_mapped_client_port("refa")
ref1_client_port = T.twin_mapped_client_port("ref1")
ref2_client_port = T.twin_mapped_client_port("ref2")
# Wait until the twin handshake is established so the trunk->twin
# mirror has somewhere to go. (Previous tests may have just torn
# ref2 down and back up.)
wait_until(
lambda: "TWIN: hello from partner" in docker_compose_output(
"logs", "reflector-ref1"),
timeout=20.0,
msg="twin link not re-established on ref1",
)
sender = ClientPeer()
recv1 = ClientPeer()
recv2 = ClientPeer()
try:
sender.connect(HOST, refa_client_port)
sender.authenticate(callsign=CLIENT_CALLSIGN,
password=CLIENT_PASSWORD)
sender.setup_udp(udp_port=refa_client_port)
recv1.connect(HOST, ref1_client_port)
recv1.authenticate(callsign="N0SEND", password=CLIENT_PASSWORD)
recv1.setup_udp(udp_port=ref1_client_port)
recv2.connect(HOST, ref2_client_port)
recv2.authenticate(callsign="N0THRD", password=CLIENT_PASSWORD)
recv2.setup_udp(udp_port=ref2_client_port)
sender.select_tg(tg)
recv1.select_tg(tg)
recv2.select_tg(tg)
time.sleep(0.5) # let TG selection + interest propagate
# Drain pre-test UDP noise
recv1.recv_udp_all(timeout=0.3)
recv2.recv_udp_all(timeout=0.3)
audio_payload = b"\xCA\xFE" * 80 # 160 bytes, distinctive
for _ in range(6):
sender.send_udp_audio(audio_payload)
time.sleep(0.02)
sender.send_udp_flush()
# Allow trunk -> twin hop a little extra time
msgs1 = recv1.recv_udp_all(timeout=3.0)
msgs2 = recv2.recv_udp_all(timeout=3.0)
audio1 = sum(1 for t, _ in msgs1 if t == UDP_AUDIO)
audio2 = sum(1 for t, _ in msgs2 if t == UDP_AUDIO)
flush1 = sum(1 for t, _ in msgs1 if t == UDP_FLUSH)
flush2 = sum(1 for t, _ in msgs2 if t == UDP_FLUSH)
self.assertGreater(
audio1, 0,
"ref1 client did not receive any UDP audio from refa "
"(sticky=ref1 broken, or trunk routing failed)")
self.assertGreater(
audio2, 0,
"ref2 client did not receive any UDP audio from refa "
"— the trunk->twin audio mirror is missing or broken")
# Flush must also reach both twins (forwardTrunkFlushToTwin).
self.assertGreater(
flush1, 0,
"ref1 client did not receive UDP flush from refa")
self.assertGreater(
flush2, 0,
"ref2 client did not receive UDP flush from refa "
"— the trunk->twin flush mirror is missing or broken")
finally:
sender.close()
recv1.close()
recv2.close()
def docker_compose_output(*args) -> str:
"""Return the stdout+stderr of a docker-compose command as text."""