feat(M2): UDP voice/media plane -- SFU relay, Opus, AEAD, jitter buffer

Adds the full voice pipeline: 14-byte binary frame header, ChaCha20-Poly1305
AEAD keyed from the TLS exporter, libopus encode/decode with FEC/PLC/DTX,
an adaptive per-ssrc jitter buffer, a miniaudio capture/playback engine, an
APM passthrough stub, and the UdpBinding/StreamAnnounce signaling chain
wired through ConnSession/SessionRegistry into a new server-side SFU
(MediaRelay) that decrypts and re-encrypts frames per channel member.

Exit criterion verified: test_m2_voice — two headless clients relay 50
encrypted Opus frames through the server; ctest --preset m1-dev is 9/9
green. Also corrects protocol.md's UdpBinding diagram, which described the
UDP-side binding packet as AEAD-sealed when it is in fact a plaintext
bootstrap frame (separate from the TCP/TLS UdpBinding ack).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-16 01:31:14 +02:00
parent 63f457fc54
commit 694494a5be
29 changed files with 2548 additions and 86 deletions

View File

@@ -40,6 +40,9 @@ struct TcpChannelCallbacks {
std::function<void(std::vector<uint8_t>)> on_frame; // one decoded frame payload
std::function<void(std::error_code)> on_error;
std::function<void()> on_disconnected;
// Called (on the handshake thread) right after TLS succeeds, before reads begin.
// Use to export keying material while the handshake context is still fresh.
std::function<void(voicecat::crypto::TlsContext&)> on_tls_ready;
};
// ── Client-side: owns an io_context + dedicated net thread ──────────────────
@@ -166,12 +169,44 @@ class TcpAcceptor {
};
// ── UDP media channel (M2) ───────────────────────────────────────────────────
// Thin async UDP socket. send_to() is thread-safe. Recv callbacks fire on the
// io_context's thread (same thread that runs the io_context::run() loop).
class UdpMediaChannel {
public:
bool bound() const { return bound_; }
using FrameCallback =
std::function<void(const uint8_t*, size_t, asio::ip::udp::endpoint)>;
UdpMediaChannel() = default;
~UdpMediaChannel() { close(); }
UdpMediaChannel(const UdpMediaChannel&) = delete;
UdpMediaChannel& operator=(const UdpMediaChannel&) = delete;
// Bind to 0.0.0.0:port (0 = OS-assigned). Must be called before start_recv/send_to.
bool bind(asio::io_context& io, uint16_t port = 0);
// Begin the async recv loop. cb is called on the io_context thread.
void start_recv(FrameCallback cb);
// Thread-safe fire-and-forget send. Copies data into a heap buffer.
void send_to(const uint8_t* data, size_t len, asio::ip::udp::endpoint dst);
// Cancel all async ops and close the socket. Safe to call from any thread.
void close();
asio::ip::udp::endpoint local_endpoint() const;
bool bound() const { return bound_.load(std::memory_order_acquire); }
private:
bool bound_ = false;
void do_recv();
// Socket + recv state live here; only accessed from the io_context thread after bind().
std::unique_ptr<asio::ip::udp::socket> socket_;
asio::ip::udp::endpoint sender_ep_;
std::array<uint8_t, 1500> recv_buf_{};
FrameCallback frame_cb_;
std::atomic<bool> bound_{false};
std::atomic<bool> closed_{false};
};
} // namespace voicecat::net