Quit if no client when connection ends

This commit is contained in:
Patrick Nicolas
2024-06-03 20:59:06 +02:00
parent a8c99ea4c1
commit d935eabf14
7 changed files with 126 additions and 83 deletions
+6 -3
View File
@@ -35,7 +35,7 @@ static void avahi_set_bool_callback(AvahiWatch * w, int fd, AvahiWatchEvent even
*flag = true;
}
std::unique_ptr<xrt::drivers::wivrn::TCP> accept_connection(int watch_fd)
std::unique_ptr<xrt::drivers::wivrn::TCP> accept_connection(int watch_fd, std::function<bool()> quit)
{
char protocol_string[17];
sprintf(protocol_string, "%016lx", xrt::drivers::wivrn::protocol_version);
@@ -55,8 +55,11 @@ std::unique_ptr<xrt::drivers::wivrn::TCP> accept_connection(int watch_fd)
AvahiWatch * watch_listener = publisher.watch_new(listener.get_fd(), AVAHI_WATCH_IN, &avahi_set_bool_callback, &client_connected);
AvahiWatch * watch_user = publisher.watch_new(watch_fd, AVAHI_WATCH_IN, &avahi_set_bool_callback, &fd_triggered);
while (publisher.iterate() && !client_connected && !fd_triggered)
;
while (not(client_connected or fd_triggered or (quit and quit())))
{
if (not publisher.iterate(quit ? 100 : -1))
break;
}
publisher.watch_free(watch_listener);
publisher.watch_free(watch_user);
+3 -1
View File
@@ -19,6 +19,8 @@
#pragma once
#include <functional>
#include <memory>
#include <wivrn_sockets.h>
std::unique_ptr<xrt::drivers::wivrn::TCP> accept_connection(int watch_fd);
std::unique_ptr<xrt::drivers::wivrn::TCP> accept_connection(int watch_fd, std::function<bool()> quit = std::function<bool()>());
+1 -19
View File
@@ -21,6 +21,7 @@
#include "encoder/video_encoder.h"
#include "main/comp_compositor.h"
#include "math/m_space.h"
#include "utils/scoped_lock.h"
#include "xrt_cast.h"
#include <condition_variable>
#include <list>
@@ -89,25 +90,6 @@ static void destroy_images(struct wivrn_comp_target * cn)
target_fini_semaphores(cn);
}
namespace
{
class scoped_lock
{
os_mutex & mutex;
public:
scoped_lock(os_mutex & mutex) :
mutex(mutex)
{
os_mutex_lock(&mutex);
}
~scoped_lock()
{
os_mutex_unlock(&mutex);
}
};
}; // namespace
struct encoder_thread_param
{
wivrn_comp_target * cn;
+70 -54
View File
@@ -24,7 +24,9 @@
#include "main/comp_target.h"
#include "util/u_builders.h"
#include "util/u_logging.h"
#include "util/u_system.h"
#include "util/u_system_helpers.h"
#include "utils/scoped_lock.h"
#include "audio/audio_setup.h"
#include "wivrn_comp_target.h"
@@ -85,13 +87,13 @@ void xrt::drivers::wivrn::max_accumulator::send(wivrn_connection & connection)
next_sample += std::chrono::seconds(1);
}
xrt::drivers::wivrn::wivrn_session::wivrn_session(xrt::drivers::wivrn::TCP && tcp, xrt_session_event_sink & sink) :
connection(std::move(tcp)), event_sink(sink)
xrt::drivers::wivrn::wivrn_session::wivrn_session(xrt::drivers::wivrn::TCP && tcp, u_system & system) :
connection(std::move(tcp)), xrt_system(system)
{
}
xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wivrn::TCP && tcp,
xrt_session_event_sink & event_sink,
u_system & system,
xrt_system_devices ** out_xsysd,
xrt_space_overseer ** out_xspovrs,
xrt_system_compositor ** out_xsysc)
@@ -100,7 +102,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
std::optional<xrt::drivers::wivrn::from_headset::packets> control;
try
{
self = std::shared_ptr<wivrn_session>(new wivrn_session(std::move(tcp), event_sink));
self = std::shared_ptr<wivrn_session>(new wivrn_session(std::move(tcp), system));
while (not(control = self->connection.poll_control(-1)))
{
// FIXME: timeout
@@ -164,7 +166,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
}
u_builder_create_space_overseer_legacy(
&event_sink,
&self->xrt_system.broadcast,
self->hmd.get(),
self->left_hand.get(),
self->right_hand.get(),
@@ -285,55 +287,7 @@ void wivrn_session::run(std::weak_ptr<wivrn_session> weak_self)
U_LOG_E("Exception in network thread: %s", e.what());
auto self = weak_self.lock();
if (self)
{
xrt_session_event event{
.state = {
.type = XRT_SESSION_EVENT_STATE_CHANGE,
.visible = false,
.focused = false,
},
};
auto result = xrt_session_event_sink_push(&self->event_sink, &event);
if (result != XRT_SUCCESS)
{
U_LOG_W("Failed to notify session state change");
}
U_LOG_I("Waiting for new connection");
auto tcp = accept_connection(0 /*stdin*/);
if (not tcp)
{
self->quit = true;
exit(0);
}
try
{
self->offset_est.reset();
self->connection.reset(std::move(*tcp));
std::optional<xrt::drivers::wivrn::from_headset::packets> control;
while (not(control = self->connection.poll_control(-1)))
{
// FIXME: timeout
}
const auto & info = std::get<from_headset::headset_info_packet>(*control);
// FIXME: ensure new client is compatible
self->comp_target->reset_encoders();
if (self->audio_handle)
self->send_control(self->audio_handle->description());
event.state.visible = true;
event.state.focused = true;
result = xrt_session_event_sink_push(&self->event_sink, &event);
if (result != XRT_SUCCESS)
{
U_LOG_W("Failed to notify session state change");
}
}
catch (const std::exception & e)
{
U_LOG_E("Reconnection failed: %s", e.what());
}
}
self->reconnect();
}
}
}
@@ -351,3 +305,65 @@ void wivrn_session::dump_time(const std::string & event, uint64_t frame, uint64_
feedback_csv << std::quoted(event) << "," << frame << "," << time << "," << (int)stream << extra << std::endl;
}
}
static bool quit_if_no_client(u_system & xrt_system)
{
scoped_lock lock(xrt_system.sessions.mutex);
if (xrt_system.sessions.count == 0)
{
U_LOG_I("No OpenXR client connected, exiting");
exit(0);
}
return false;
}
void wivrn_session::reconnect()
{
// Notify clients about disconnected status
xrt_session_event event{
.state = {
.type = XRT_SESSION_EVENT_STATE_CHANGE,
.visible = false,
.focused = false,
},
};
auto result = xrt_session_event_sink_push(&xrt_system.broadcast, &event);
if (result != XRT_SUCCESS)
{
U_LOG_W("Failed to notify session state change");
}
U_LOG_I("Waiting for new connection");
auto tcp = accept_connection(0 /*stdin*/, [this]() { return quit_if_no_client(xrt_system); });
if (not tcp)
exit(0);
try
{
offset_est.reset();
connection.reset(std::move(*tcp));
std::optional<xrt::drivers::wivrn::from_headset::packets> control;
while (not(control = connection.poll_control(-1)))
{
// FIXME: timeout
}
const auto & info = std::get<from_headset::headset_info_packet>(*control);
// FIXME: ensure new client is compatible
comp_target->reset_encoders();
if (audio_handle)
send_control(audio_handle->description());
event.state.visible = true;
event.state.focused = true;
result = xrt_session_event_sink_push(&xrt_system.broadcast, &event);
if (result != XRT_SUCCESS)
{
U_LOG_W("Failed to notify session state change");
}
}
catch (const std::exception & e)
{
U_LOG_E("Reconnection failed: %s", e.what());
}
}
+7 -5
View File
@@ -22,7 +22,7 @@
#include "clock_offset.h"
#include "wivrn_connection.h"
#include "wivrn_packets.h"
#include "xrt/xrt_system.h"
#include "xrt/xrt_results.h"
#include <atomic>
#include <fstream>
#include <memory>
@@ -33,7 +33,7 @@ class wivrn_hmd;
class wivrn_controller;
struct audio_device;
struct xrt_session_event_sink;
struct u_system;
struct xrt_system_devices;
struct xrt_space_overseer;
struct xrt_system_compositor;
@@ -67,7 +67,8 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
{
friend wivrn_comp_target_factory;
wivrn_connection connection;
xrt_session_event_sink & event_sink;
u_system & xrt_system;
std::atomic<bool> quit = false;
std::thread thread;
@@ -87,11 +88,11 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
std::shared_ptr<audio_device> audio_handle;
wivrn_session(TCP && tcp, xrt_session_event_sink &);
wivrn_session(TCP && tcp, u_system &);
public:
static xrt_result_t create_session(TCP && tcp,
xrt_session_event_sink & event_sink,
u_system & system,
xrt_system_devices ** out_xsysd,
xrt_space_overseer ** out_xspovrs,
xrt_system_compositor ** out_xsysc);
@@ -131,6 +132,7 @@ public:
private:
static void run(std::weak_ptr<wivrn_session>);
void reconnect();
};
} // namespace xrt::drivers::wivrn
+1 -1
View File
@@ -43,7 +43,7 @@ wivrn_instance_create_system(struct xrt_instance * xinst,
struct xrt_system_compositor * xsysc = NULL;
auto res = xrt::drivers::wivrn::wivrn_session::create_session(
std::move(*tcp),
u_sys->broadcast,
*u_sys,
out_xsysd,
out_xspovrs,
out_xsysc);
+38
View File
@@ -0,0 +1,38 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022-2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022-2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "os/os_threading.h"
class scoped_lock
{
os_mutex & mutex;
public:
scoped_lock(os_mutex & mutex) :
mutex(mutex)
{
os_mutex_lock(&mutex);
}
~scoped_lock()
{
os_mutex_unlock(&mutex);
}
};