Files
screen_cast/tests/network/test_signaling.cpp
T
fegger 7be8d59d07 feat(network): implement Phase 6 LAN discovery and session signaling
Add mDNS/DNS-SD discovery and JSON session negotiation so two peers on
a LAN connect without hard-coded addresses.

- Discovery (Avahi threaded-poll client): the receiver announces
  _screencast._tcp with its signaling port; senders browse and resolve
  peers. Strict lock ordering (poll lock before state mutex) keeps the
  callbacks deadlock-free; name collisions rename via
  avahi_alternative_service_name.
- Signaling: one JSON object per newline-terminated TCP line. The
  receiver hosts a server (port 5005) and answers session offers with
  its RTP port; senders connect, offer, and stream to the negotiated
  endpoint. WebSocket was deferred: no WS library is installed, the
  skill permits plain TCP, and the wire format is transport-agnostic.
- Dual-stack transports: this machine resolves its own services over
  IPv6, so getaddrinfo now runs AF_UNSPEC and listeners bind IPv6 with
  IPV6_V6ONLY=0 (IPv4 fallback), covering UDP and TCP alike. The
  signaling client shutdown now uses shutdown() so a reader blocked in
  recv() cannot hang the join (a plain close() does not wake it).
- CLI: --discover lists receivers (deduped to one entry per host);
  --send auto-disovers when exactly one receiver is found; --peer
  targets a receiver directly; --signaling-port overrides the default.

Validation: meson test 5/5 (new signaling round-trip test), valgrind
clean. End-to-end over loopback: --discover finds the announced
receiver, a probe negotiated a session and streamed 60 frames over
both IPv4 and IPv6, and the receiver reported stream started. mDNS
resolution was verified against avahi-browse as an independent
reference.
2026-09-07 12:16:34 +02:00

102 lines
3.3 KiB
C++

// Phase 6 signaling test: a client offers a session to a server over a real
// localhost TCP connection and receives the server's answer.
#include "screencast/network/signaling.h"
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <future>
#include <memory>
#include <string>
#include <thread>
namespace {
[[noreturn]] void fail(const char* what) {
std::fprintf(stderr, "test_signaling: FAIL: %s\n", what);
std::abort();
}
void check(bool condition, const char* what) {
if (!condition) {
fail(what);
}
}
void check_result(const char* what, const sc::NetworkResult<std::unique_ptr<sc::SignalingChannel>>& result) {
if (sc::is_network_error(result)) {
std::fprintf(stderr, "test_signaling: FAIL: %s: %s\n", what, sc::network_error(result).message.c_str());
std::abort();
}
}
} // namespace
int main() {
// Bind the server to the first free port in a small range.
std::unique_ptr<sc::SignalingChannel> server;
uint16_t server_port = 0;
for (uint16_t port = 45940; port < 45960; ++port) {
auto server_result = sc::SignalingFactory::create_server(port);
if (!sc::is_network_error(server_result)) {
server = std::move(sc::network_value(server_result));
server_port = port;
break;
}
}
check(server != nullptr, "server listens");
check(server_port != 0, "server port");
constexpr uint16_t advertised_rtp_port = 45999;
// The receiver side of the handshake: reply to offers with an answer.
server->on_message([&](const sc::SignalingMessage& message) {
const sc::SessionOffer* offer = std::get_if<sc::SessionOffer>(&message);
if (offer == nullptr) {
return;
}
sc::SessionAnswer answer;
answer.session_id = offer->session_id;
answer.rtp_endpoint = sc::Endpoint{"", advertised_rtp_port};
server->send(answer);
});
// The sender side: connect, offer, and wait for the answer.
auto client_result = sc::SignalingFactory::create_client();
check_result("client create", client_result);
std::unique_ptr<sc::SignalingChannel> client = std::move(sc::network_value(client_result));
check(client->connect(sc::Endpoint{"127.0.0.1", server_port}), "client connect");
std::promise<sc::SessionAnswer> answer_promise;
auto answer_future = answer_promise.get_future();
std::atomic<bool> answered{false};
client->on_message([&](const sc::SignalingMessage& message) {
if (const sc::SessionAnswer* answer = std::get_if<sc::SessionAnswer>(&message)) {
if (!answered.exchange(true)) {
answer_promise.set_value(*answer);
}
}
});
sc::SessionOffer offer;
offer.session_id = "test-session-0001";
offer.codec_name = "h264";
offer.frame_rate_num = 25;
offer.frame_rate_den = 1;
client->send(offer);
check(answer_future.wait_for(std::chrono::seconds(5)) == std::future_status::ready, "answer received");
const sc::SessionAnswer answer = answer_future.get();
check(answer.session_id == "test-session-0001", "session id echoes");
check(answer.rtp_endpoint.port == advertised_rtp_port, "rtp port in answer");
client->disconnect();
server->disconnect();
std::puts("test_signaling: all checks passed");
return 0;
}