Reconnection support
This commit is contained in:
@@ -283,7 +283,7 @@ void pipewire_device::speaker_process(void * self_v)
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
U_LOG_W("Failed to send audio data: %s", e.what());
|
||||
U_LOG_D("Failed to send audio data: %s", e.what());
|
||||
}
|
||||
pw_stream_queue_buffer(self->speaker.get(), buffer);
|
||||
}
|
||||
|
||||
@@ -345,7 +345,7 @@ struct pulse_device : public audio_device
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
U_LOG_E("Error in audio thread: %s", e.what());
|
||||
U_LOG_D("Error in audio thread: %s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,16 @@
|
||||
|
||||
static const size_t num_samples = 100;
|
||||
|
||||
void clock_offset_estimator::reset()
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
sample_index = 0;
|
||||
samples.clear();
|
||||
offset = clock_offset();
|
||||
next_sample = {};
|
||||
sample_interval = std::chrono::milliseconds(10);
|
||||
}
|
||||
|
||||
void clock_offset_estimator::request_sample(wivrn_connection & connection)
|
||||
{
|
||||
if (std::chrono::steady_clock::now() < next_sample)
|
||||
|
||||
@@ -62,6 +62,7 @@ class clock_offset_estimator
|
||||
std::atomic<std::chrono::milliseconds> sample_interval = std::chrono::milliseconds(10);
|
||||
|
||||
public:
|
||||
void reset();
|
||||
void request_sample(wivrn_connection & connection);
|
||||
void add_sample(const xrt::drivers::wivrn::from_headset::timesync_response & sample);
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ static void create_encoders(wivrn_comp_target * cn, std::vector<encoder_settings
|
||||
assert(cn->encoder_threads.empty());
|
||||
assert(cn->wivrn_bundle);
|
||||
|
||||
to_headset::video_stream_description desc{};
|
||||
auto & desc = cn->desc;
|
||||
desc.width = cn->width;
|
||||
desc.height = cn->height;
|
||||
desc.fps = cn->fps;
|
||||
@@ -649,6 +649,15 @@ void wivrn_comp_target::on_feedback(const from_headset::feedback & feedback, con
|
||||
}
|
||||
}
|
||||
|
||||
void wivrn_comp_target::reset_encoders()
|
||||
{
|
||||
for (auto & encoder: encoders)
|
||||
{
|
||||
encoder->SyncNeeded();
|
||||
}
|
||||
cnx->send_control(desc);
|
||||
}
|
||||
|
||||
wivrn_comp_target::wivrn_comp_target(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx, struct comp_compositor * c, float fps) :
|
||||
comp_target{},
|
||||
pacer(U_TIME_1S_IN_NS / fps),
|
||||
|
||||
@@ -92,6 +92,7 @@ struct wivrn_comp_target : public comp_target
|
||||
os_thread_helper_destroy(&thread);
|
||||
}
|
||||
};
|
||||
to_headset::video_stream_description desc{};
|
||||
std::list<encoder_thread> encoder_threads;
|
||||
std::vector<std::shared_ptr<VideoEncoder>> encoders;
|
||||
|
||||
@@ -101,6 +102,7 @@ struct wivrn_comp_target : public comp_target
|
||||
~wivrn_comp_target();
|
||||
|
||||
void on_feedback(const from_headset::feedback &, const clock_offset &);
|
||||
void reset_encoders();
|
||||
};
|
||||
|
||||
} // namespace xrt::drivers::wivrn
|
||||
|
||||
@@ -28,6 +28,14 @@ using namespace std::chrono_literals;
|
||||
wivrn_connection::wivrn_connection(TCP && tcp) :
|
||||
control(std::move(tcp)), stream(-1)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
void wivrn_connection::init()
|
||||
{
|
||||
active = false;
|
||||
stream = -1;
|
||||
|
||||
sockaddr_in6 server_address;
|
||||
socklen_t len = sizeof(server_address);
|
||||
if (getsockname(control.get_fd(), (sockaddr *)&server_address, &len) < 0)
|
||||
@@ -125,6 +133,13 @@ wivrn_connection::wivrn_connection(TCP && tcp) :
|
||||
{
|
||||
U_LOG_I("Failed to set IP ToS to Expedited Forwarding: %s", e.what());
|
||||
}
|
||||
active = true;
|
||||
}
|
||||
|
||||
void wivrn_connection::reset(TCP && tcp)
|
||||
{
|
||||
control = std::move(tcp);
|
||||
init();
|
||||
}
|
||||
|
||||
std::optional<from_headset::packets> wivrn_connection::poll_control(int timeout)
|
||||
|
||||
@@ -30,21 +30,31 @@ class wivrn_connection
|
||||
{
|
||||
typed_socket<TCP, from_headset::packets, to_headset::packets> control;
|
||||
typed_socket<UDP, from_headset::packets, to_headset::packets> stream;
|
||||
std::atomic<bool> active = false;
|
||||
|
||||
void init();
|
||||
|
||||
public:
|
||||
wivrn_connection(TCP && tcp);
|
||||
wivrn_connection(const wivrn_connection &) = delete;
|
||||
wivrn_connection & operator=(const wivrn_connection &) = delete;
|
||||
|
||||
void deactivate()
|
||||
{
|
||||
active = false;
|
||||
}
|
||||
void reset(TCP && tcp);
|
||||
|
||||
template <typename T>
|
||||
void send_control(T && packet)
|
||||
{
|
||||
control.send(std::forward<T>(packet));
|
||||
if (active)
|
||||
control.send(std::forward<T>(packet));
|
||||
}
|
||||
template <typename T>
|
||||
void send_stream(T && packet)
|
||||
{
|
||||
if (stream)
|
||||
if (active and stream)
|
||||
stream.send(std::forward<T>(packet));
|
||||
else
|
||||
control.send(std::forward<T>(packet));
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "wivrn_session.h"
|
||||
|
||||
#include "accept_connection.h"
|
||||
#include "main/comp_main_interface.h"
|
||||
#include "main/comp_target.h"
|
||||
#include "util/u_builders.h"
|
||||
@@ -279,10 +280,35 @@ void wivrn_session::run(std::weak_ptr<wivrn_session> weak_self)
|
||||
auto self = weak_self.lock();
|
||||
if (self)
|
||||
{
|
||||
self->quit = true;
|
||||
exit(0);
|
||||
self->connection.deactivate();
|
||||
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());
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
U_LOG_E("Reconnection failed: %s", e.what());
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ extern "C"
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavutil/hwcontext.h>
|
||||
#include <libavutil/hwcontext_drm.h>
|
||||
#include <libavutil/opt.h>
|
||||
#include <libavutil/pixdesc.h>
|
||||
}
|
||||
|
||||
@@ -180,19 +181,21 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk, xrt::drivers::wivrn::en
|
||||
|
||||
AVDictionary * opts = nullptr;
|
||||
av_dict_set(&opts, "async_depth", "1", 0);
|
||||
for (auto option: settings.options)
|
||||
{
|
||||
av_dict_set(&opts, option.first.c_str(), option.second.c_str(), 0);
|
||||
}
|
||||
switch (settings.codec)
|
||||
{
|
||||
case Codec::h264:
|
||||
encoder_ctx->profile = FF_PROFILE_H264_MAIN;
|
||||
encoder_ctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
|
||||
av_dict_set(&opts, "coder", "cavlc", 0);
|
||||
av_dict_set(&opts, "rc_mode", "CBR", 0);
|
||||
break;
|
||||
case Codec::h265:
|
||||
encoder_ctx->profile = FF_PROFILE_HEVC_MAIN;
|
||||
break;
|
||||
}
|
||||
for (auto option: settings.options)
|
||||
{
|
||||
av_dict_set(&opts, option.first.c_str(), option.second.c_str(), 0);
|
||||
}
|
||||
|
||||
encoder_ctx->width = settings.video_width;
|
||||
encoder_ctx->height = settings.video_height;
|
||||
|
||||
@@ -166,7 +166,14 @@ void VideoEncoder::SendData(std::span<uint8_t> data, bool end_of_frame)
|
||||
}
|
||||
}
|
||||
shard.payload = {begin, next};
|
||||
cnx->send_stream(shard);
|
||||
try
|
||||
{
|
||||
cnx->send_stream(shard);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// Ignore network errors
|
||||
}
|
||||
++shard.shard_idx;
|
||||
shard.flags = 0;
|
||||
shard.view_info.reset();
|
||||
|
||||
Reference in New Issue
Block a user