From dfc2f1f8cbfeb65da0fa61fefff63087f3160234 Mon Sep 17 00:00:00 2001 From: ugfuglio Date: Fri, 30 Oct 2026 12:28:42 -0400 Subject: [PATCH] fix(stream): isolate audio and video UDP receive endpoints Signed-off-by: ugfuglio --- src/stream.cpp | 65 ++++++++++++++++++++++++++++++++++++-- tests/unit/test_stream.cpp | 13 ++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/stream.cpp b/src/stream.cpp index 2f2db0c2f..21c5c2b2d 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -1445,7 +1445,8 @@ namespace stream { auto &io = ctx.io_context; - udp::endpoint peer; + // Pending audio and video receives must not write into the same endpoint. + std::array peers; std::array, 2> buf; std::array, 2> recv_func; @@ -1478,6 +1479,7 @@ namespace stream { auto recv_func_init = [&](udp::socket &sock, int buf_elem, std::map &peer_to_session) { recv_func[buf_elem] = [&, buf_elem](const boost::system::error_code &ec, size_t bytes) { + auto &peer = peers[buf_elem]; auto fg = util::fail_guard([&sock, &buf, &peer, &recv_func, &buf_elem]() { sock.async_receive_from(asio::buffer(buf[buf_elem]), peer, 0, recv_func[buf_elem]); }); @@ -1520,14 +1522,71 @@ namespace stream { recv_func_init(video_sock, 0, peer_to_video_session); recv_func_init(audio_sock, 1, peer_to_audio_session); - video_sock.async_receive_from(asio::buffer(buf[0]), peer, 0, recv_func[0]); - audio_sock.async_receive_from(asio::buffer(buf[1]), peer, 0, recv_func[1]); + video_sock.async_receive_from(asio::buffer(buf[0]), peers[0], 0, recv_func[0]); + audio_sock.async_receive_from(asio::buffer(buf[1]), peers[1], 0, recv_func[1]); while (!broadcast_shutdown_event->peek()) { io.run(); } } +#ifdef SUNSHINE_TESTS + /** + * @brief Exercise production ping routing with concurrent loopback audio and video receives. + * @param session_id_pings Use session-ID pings instead of legacy address-based pings. + * @return True when every ping retains its sending channel's endpoint and payload. + */ + bool test_receive_ping_endpoints(bool session_id_pings) { + broadcast_ctx_t ctx; + const auto loopback = asio::ip::address_v4::loopback(); + ctx.video_sock.open(udp::v4()); + ctx.audio_sock.open(udp::v4()); + ctx.video_sock.bind(udp::endpoint {loopback, 0}); + ctx.audio_sock.bind(udp::endpoint {loopback, 0}); + ctx.message_queue_queue = std::make_shared(4); + + udp::socket video_sender {ctx.io_context, udp::endpoint {loopback, 0}}; + udp::socket audio_sender {ctx.io_context, udp::endpoint {loopback, 0}}; + const std::array expected {video_sender.local_endpoint(), audio_sender.local_endpoint()}; + const std::string token {"abcdefghijklmnop"}; + const auto packet = session_id_pings ? token + std::string(4, '\0') : "PING"s; + std::array queues; + for (std::size_t channel = 0; channel < queues.size(); ++channel) { + queues[channel] = std::make_shared(4); + const auto id = session_id_pings ? av_session_id_t {token} : av_session_id_t {asio::ip::address {loopback}}; + ctx.message_queue_queue->raise(static_cast(channel), id, queues[channel]); + } + + // Queued packets exercise both initial receives and callback rearming before the next handler runs. + for (int packet_index = 0; packet_index < 2; ++packet_index) { + video_sender.send_to(asio::buffer(packet), ctx.video_sock.local_endpoint()); + audio_sender.send_to(asio::buffer(packet), ctx.audio_sock.local_endpoint()); + } + + auto shutdown = mail::man->event(mail::broadcast_shutdown); + shutdown->reset(); + auto reset_shutdown = util::fail_guard([&shutdown]() { + shutdown->reset(); + }); + bool matched = true; + std::jthread observer {[&]() { + for (std::size_t channel = 0; channel < queues.size(); ++channel) { + for (int packet_index = 0; packet_index < 2; ++packet_index) { + auto received = queues[channel]->pop(1s); + if (!received || received->first != expected[channel] || received->second != packet) { + matched = false; + } + } + } + shutdown->raise(true); + ctx.io_context.stop(); + }}; + recvThread(ctx); + observer.join(); + return matched; + } +#endif + /** * @brief Run the broadcast video sender thread. * diff --git a/tests/unit/test_stream.cpp b/tests/unit/test_stream.cpp index e94ceb052..12f1d5387 100644 --- a/tests/unit/test_stream.cpp +++ b/tests/unit/test_stream.cpp @@ -22,8 +22,21 @@ namespace stream { std::vector concat_and_insert(uint64_t insert_size, uint64_t slice_size, const std::string_view &data1, const std::string_view &data2); std::optional> parse_control_packet(const ENetPacket &packet); + bool test_receive_ping_endpoints(bool session_id_pings); } // namespace stream +TEST(StreamPingTests, KeepsLegacyAudioAndVideoSenderEndpointsSeparate) { + for (int attempt = 0; attempt < 20; ++attempt) { + ASSERT_TRUE(stream::test_receive_ping_endpoints(false)) << "Attempt " << attempt; + } +} + +TEST(StreamPingTests, KeepsSessionIdAudioAndVideoSenderEndpointsSeparate) { + for (int attempt = 0; attempt < 20; ++attempt) { + ASSERT_TRUE(stream::test_receive_ping_endpoints(true)) << "Attempt " << attempt; + } +} + TEST(ConcatAndInsertTests, ConcatNoInsertionTest) { std::array b1 = {'a', 'b'}; std::array b2 = {'c', 'd', 'e'};