mirror of
https://github.com/LizardByte/Sunshine.git
synced 2026-12-01 01:29:38 +00:00
fix(stream): isolate audio and video UDP receive endpoints
Signed-off-by: ugfuglio <se7en.brc@gmail.com>
This commit is contained in:
1 parent
4dadc7d40c
commit
dfc2f1f8cb
2 files changed
+75
-3
No files matched your search
+62
-3
@@ -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<udp::endpoint, 2> peers;
|
||||
|
||||
std::array<std::array<char, 2048>, 2> buf;
|
||||
std::array<std::function<void(const boost::system::error_code, size_t)>, 2> recv_func;
|
||||
@@ -1478,6 +1479,7 @@ namespace stream {
|
||||
|
||||
auto recv_func_init = [&](udp::socket &sock, int buf_elem, std::map<av_session_id_t, message_queue_t> &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<message_queue_queue_t::element_type>(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<message_queue_t, 2> queues;
|
||||
for (std::size_t channel = 0; channel < queues.size(); ++channel) {
|
||||
queues[channel] = std::make_shared<message_queue_t::element_type>(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<socket_e>(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<bool>(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.
|
||||
*
|
||||
|
||||
@@ -22,8 +22,21 @@
|
||||
namespace stream {
|
||||
std::vector<uint8_t> concat_and_insert(uint64_t insert_size, uint64_t slice_size, const std::string_view &data1, const std::string_view &data2);
|
||||
std::optional<std::pair<std::uint16_t, std::string_view>> 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<char, 2> b1 = {'a', 'b'};
|
||||
std::array<char, 3> b2 = {'c', 'd', 'e'};
|
||||
|
||||
Reference in new issue
Block a user