Add support for authentication and encryption
This commit is contained in:
committed by
Patrick Nicolas
parent
447b77c35c
commit
c6e773fd22
@@ -127,14 +127,12 @@ int32_t wivrn::android::audio::microphone_data_cb(AAudioStream * stream, void *
|
||||
|
||||
size_t frame_size = AAudioStream_getChannelCount(stream) * sizeof(uint16_t);
|
||||
|
||||
wivrn::audio_data packet{
|
||||
.timestamp = self->instance.now(),
|
||||
.payload = std::span(audio_data, frame_size * num_frames),
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
self->session.send_control(packet);
|
||||
self->session.send_control(wivrn::audio_data{
|
||||
.timestamp = self->instance.now(),
|
||||
.payload = std::span(audio_data, frame_size * num_frames),
|
||||
});
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
|
||||
+80
-6
@@ -31,13 +31,17 @@
|
||||
#include "stream.h"
|
||||
#include "utils/overloaded.h"
|
||||
#include "wivrn_client.h"
|
||||
#include "wivrn_sockets.h"
|
||||
#include "xr/passthrough.h"
|
||||
#include "xr/space.h"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
|
||||
#include "wivrn_discover.h"
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <glm/matrix.hpp>
|
||||
#include <magic_enum.hpp>
|
||||
@@ -45,6 +49,7 @@
|
||||
#include <simdjson.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <string>
|
||||
#include <sys/poll.h>
|
||||
#include <utils/ranges.h>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
@@ -53,6 +58,8 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
static bool force_autoconnect = false;
|
||||
|
||||
scenes::lobby::~lobby()
|
||||
@@ -174,6 +181,19 @@ scenes::lobby::lobby()
|
||||
spdlog::info("Composition layer color scale/bias NOT supported");
|
||||
|
||||
keyboard.set_layout(application::get_config().virtual_keyboard_layout);
|
||||
|
||||
const auto keypair_path = application::get_config_path() / "private_key.pem";
|
||||
try
|
||||
{
|
||||
std::ifstream f{keypair_path};
|
||||
keypair = crypto::key::from_private_key(std::string{(std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()});
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
keypair = crypto::key::generate_x448_keypair();
|
||||
std::ofstream{keypair_path} << keypair.private_key();
|
||||
spdlog::info("Generated X448 keypair");
|
||||
}
|
||||
}
|
||||
|
||||
static std::string ip_address_to_string(const in_addr & addr)
|
||||
@@ -190,7 +210,7 @@ static std::string ip_address_to_string(const in6_addr & addr)
|
||||
return buf;
|
||||
}
|
||||
|
||||
std::unique_ptr<wivrn_session> connect_to_session(wivrn_discover::service service, bool manual_connection)
|
||||
std::unique_ptr<wivrn_session> scenes::lobby::connect_to_session(wivrn_discover::service service, bool manual_connection)
|
||||
{
|
||||
if (!manual_connection)
|
||||
{
|
||||
@@ -244,22 +264,76 @@ std::unique_ptr<wivrn_session> connect_to_session(wivrn_discover::service servic
|
||||
}
|
||||
|
||||
std::string error;
|
||||
for (const auto & address: service.addresses)
|
||||
for (const std::variant<in_addr, in6_addr> & address: service.addresses)
|
||||
{
|
||||
std::string address_string = std::visit([](auto & address) {
|
||||
return ip_address_to_string(address);
|
||||
},
|
||||
address);
|
||||
|
||||
struct connection_cancelled
|
||||
{};
|
||||
|
||||
try
|
||||
{
|
||||
spdlog::debug("Trying address {}", address_string);
|
||||
spdlog::debug("Connection to {}", address_string);
|
||||
|
||||
return std::visit([port = service.port, tcp_only = service.tcp_only](auto & address) {
|
||||
return std::make_unique<wivrn_session>(address, port, tcp_only);
|
||||
return std::visit([this, port = service.port, tcp_only = service.tcp_only](auto & address) {
|
||||
return std::make_unique<wivrn_session>(address, port, tcp_only, keypair, [&](int fd) {
|
||||
auto request = pin_request.lock();
|
||||
request->pin_requested = true;
|
||||
request->pin_cancelled = false;
|
||||
request->pin = "";
|
||||
pin_buffer = "";
|
||||
|
||||
while (not request.wait_for(500ms, [&]() { return request->pin != "" or request->pin_cancelled; }))
|
||||
{
|
||||
pollfd fds{};
|
||||
fds.events = POLLRDHUP;
|
||||
fds.fd = fd;
|
||||
|
||||
int r = ::poll(&fds, 1, 0);
|
||||
|
||||
if (r < 0)
|
||||
{
|
||||
request->pin_requested = false;
|
||||
throw std::system_error(errno, std::system_category());
|
||||
}
|
||||
|
||||
if (fds.revents & (POLLHUP | POLLERR))
|
||||
{
|
||||
request->pin_requested = false;
|
||||
throw std::runtime_error("Error on control socket");
|
||||
}
|
||||
|
||||
if (fds.revents & POLLRDHUP)
|
||||
{
|
||||
request->pin_requested = false;
|
||||
throw socket_shutdown{};
|
||||
}
|
||||
}
|
||||
|
||||
request->pin_requested = false;
|
||||
|
||||
if (request->pin_cancelled)
|
||||
throw connection_cancelled{};
|
||||
|
||||
return request->pin;
|
||||
});
|
||||
},
|
||||
address);
|
||||
}
|
||||
catch (connection_cancelled)
|
||||
{
|
||||
spdlog::info("Connection cancelled");
|
||||
return nullptr;
|
||||
}
|
||||
catch (handshake_error & e)
|
||||
{
|
||||
spdlog::warn("Error during handshake to {} ({}): {}", service.hostname, address_string, e.what());
|
||||
std::string txt = fmt::format(_F("Cannot connect to {} ({}): {}"), service.hostname, address_string, e.what());
|
||||
throw std::runtime_error(txt);
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
spdlog::warn("Cannot connect to {} ({}): {}", service.hostname, address_string, e.what());
|
||||
@@ -376,7 +450,7 @@ void scenes::lobby::connect(const configuration::server_data & data)
|
||||
async_error.reset();
|
||||
|
||||
async_session = utils::async<std::unique_ptr<wivrn_session>, std::string>(
|
||||
[](auto token, wivrn_discover::service service, bool manual) {
|
||||
[this](auto token, wivrn_discover::service service, bool manual) {
|
||||
token.set_progress(_("Waiting for connection"));
|
||||
return connect_to_session(service, manual);
|
||||
},
|
||||
|
||||
@@ -21,16 +21,19 @@
|
||||
|
||||
#include "asset.h"
|
||||
#include "configuration.h"
|
||||
#include "crypto.h"
|
||||
#include "render/scene_data.h"
|
||||
#include "scene.h"
|
||||
#include "scenes/hand_model.h"
|
||||
#include "scenes/lobby_keyboard.h"
|
||||
#include "utils/thread_safe.h"
|
||||
#include "wifi_lock.h"
|
||||
#include "wivrn_config.h"
|
||||
#include "wivrn_discover.h"
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
#include "xr/system.h"
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
@@ -130,6 +133,16 @@ class lobby : public scene_impl<lobby>
|
||||
|
||||
virtual_keyboard keyboard;
|
||||
|
||||
struct pin_request_data
|
||||
{
|
||||
bool pin_requested = false;
|
||||
bool pin_cancelled = false;
|
||||
std::string pin;
|
||||
};
|
||||
|
||||
thread_safe_notifyable<pin_request_data> pin_request;
|
||||
std::string pin_buffer;
|
||||
|
||||
void draw_features_status(XrTime predicted_display_time);
|
||||
void gui_connecting();
|
||||
void gui_server_list();
|
||||
@@ -145,11 +158,14 @@ class lobby : public scene_impl<lobby>
|
||||
void vibrate_on_hover();
|
||||
|
||||
void connect(const configuration::server_data & data);
|
||||
std::unique_ptr<wivrn_session> connect_to_session(wivrn_discover::service service, bool manual_connection);
|
||||
|
||||
std::optional<glm::vec3> check_recenter_gesture(const std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> & joints);
|
||||
std::optional<glm::vec3> check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position);
|
||||
std::optional<glm::vec3> check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation);
|
||||
|
||||
crypto::key keypair;
|
||||
|
||||
public:
|
||||
virtual ~lobby();
|
||||
lobby();
|
||||
|
||||
+91
-48
@@ -31,9 +31,11 @@
|
||||
#include "stream.h"
|
||||
#include "version.h"
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <ranges>
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
#include <string>
|
||||
#include <utils/strings.h>
|
||||
|
||||
#include "IconsFontAwesome6.h"
|
||||
@@ -165,56 +167,97 @@ void scenes::lobby::gui_connecting()
|
||||
{
|
||||
using constants::style::button_size;
|
||||
|
||||
std::string status;
|
||||
if (next_scene)
|
||||
ImVec2 size{600, 0};
|
||||
|
||||
auto pin_request = this->pin_request.lock();
|
||||
|
||||
std::string button_label = _("Cancel");
|
||||
|
||||
if (pin_request->pin_requested)
|
||||
{
|
||||
if (next_scene->current_state() == scenes::stream::state::stalled)
|
||||
status = _("Video stream interrupted");
|
||||
else if (server_name == "")
|
||||
status = fmt::format(_F("Connection ready\nStart a VR application on your computer"));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {30, 30});
|
||||
ImGui::PushFont(imgui_ctx->large_font);
|
||||
ImGui::Text("%s", _S("Enter the PIN"));
|
||||
if (ImGui::IsWindowAppearing())
|
||||
ImGui::SetKeyboardFocusHere();
|
||||
InputText("##PIN", pin_buffer, size, ImGuiInputTextFlags_CharsDecimal);
|
||||
ImGui::PopFont();
|
||||
|
||||
ImGui::BeginDisabled(pin_buffer.size() != 6);
|
||||
if (ImGui::Button("OK", button_size))
|
||||
{
|
||||
pin_request->pin = pin_buffer;
|
||||
pin_request->pin_requested = false;
|
||||
pin_request.notify_one();
|
||||
}
|
||||
vibrate_on_hover();
|
||||
ImGui::EndDisabled();
|
||||
|
||||
ImGui::SameLine();
|
||||
ImGui::Dummy({size.x - 2 * button_size.x - 2 * ImGui::GetStyle().ItemSpacing.x, 0});
|
||||
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button(button_label.c_str(), button_size))
|
||||
{
|
||||
async_session.cancel();
|
||||
next_scene.reset();
|
||||
pin_request->pin_cancelled = true;
|
||||
pin_request.notify_one();
|
||||
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
vibrate_on_hover();
|
||||
ImGui::PopStyleVar(); // ItemSpacing
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string status;
|
||||
if (next_scene)
|
||||
{
|
||||
if (next_scene->current_state() == scenes::stream::state::stalled)
|
||||
status = _("Video stream interrupted");
|
||||
else if (server_name == "")
|
||||
status = fmt::format(_F("Connection ready\nStart a VR application on your computer"));
|
||||
else
|
||||
status = fmt::format(_F("Connection ready\nStart a VR application on {}"), server_name);
|
||||
}
|
||||
else if (async_session.valid())
|
||||
status = async_session.get_progress();
|
||||
else if (async_error)
|
||||
status = *async_error;
|
||||
else
|
||||
status = fmt::format(_F("Connection ready\nStart a VR application on {}"), server_name);
|
||||
{
|
||||
ImGui::CloseCurrentPopup();
|
||||
return;
|
||||
}
|
||||
|
||||
if (async_error)
|
||||
button_label = _("Close");
|
||||
|
||||
ImGui::Dummy({1000, 1});
|
||||
|
||||
ImGui::PushFont(imgui_ctx->large_font);
|
||||
if (server_name == "")
|
||||
CenterTextH(fmt::format(_F("Connection")));
|
||||
else
|
||||
CenterTextH(fmt::format(_F("Connection to {}"), server_name));
|
||||
ImGui::PopFont();
|
||||
|
||||
// ImGui::TextWrapped("%s", status.first.c_str());
|
||||
ImGui::Text("%s", status.c_str());
|
||||
|
||||
ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x - button_size.x - ImGui::GetStyle().WindowPadding.x);
|
||||
|
||||
if (ImGui::Button(button_label.c_str(), button_size))
|
||||
{
|
||||
async_session.cancel();
|
||||
next_scene.reset();
|
||||
pin_request->pin_cancelled = true;
|
||||
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
vibrate_on_hover();
|
||||
}
|
||||
else if (async_session.valid())
|
||||
status = async_session.get_progress();
|
||||
else if (async_error)
|
||||
status = *async_error;
|
||||
else
|
||||
{
|
||||
ImGui::CloseCurrentPopup();
|
||||
return;
|
||||
}
|
||||
|
||||
std::string button_label;
|
||||
|
||||
if (async_error)
|
||||
button_label = _("Close");
|
||||
else
|
||||
button_label = _("Cancel");
|
||||
|
||||
ImGui::Dummy({1000, 1});
|
||||
|
||||
ImGui::PushFont(imgui_ctx->large_font);
|
||||
if (server_name == "")
|
||||
CenterTextH(fmt::format(_F("Connection")));
|
||||
else
|
||||
CenterTextH(fmt::format(_F("Connection to {}"), server_name));
|
||||
ImGui::PopFont();
|
||||
|
||||
// ImGui::TextWrapped("%s", status.first.c_str());
|
||||
ImGui::Text("%s", status.c_str());
|
||||
|
||||
ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x - button_size.x - ImGui::GetStyle().WindowPadding.x);
|
||||
|
||||
if (ImGui::Button(button_label.c_str(), button_size))
|
||||
{
|
||||
async_session.cancel();
|
||||
|
||||
next_scene.reset();
|
||||
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
vibrate_on_hover();
|
||||
}
|
||||
|
||||
void scenes::lobby::gui_new_server()
|
||||
@@ -457,7 +500,7 @@ void scenes::lobby::gui_server_list()
|
||||
|
||||
const auto & popup_layer = imgui_ctx->layers()[1];
|
||||
const glm::vec2 popup_layer_center = popup_layer.vp_origin + popup_layer.vp_size / 2;
|
||||
ImGui::SetNextWindowPos({popup_layer_center.x, popup_layer_center.y}, ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f));
|
||||
ImGui::SetNextWindowPos({popup_layer_center.x, popup_layer_center.y}, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, constants::style::window_padding);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, constants::style::window_rounding);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, constants::style::window_border_size);
|
||||
|
||||
+52
-46
@@ -79,64 +79,65 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
|
||||
std::shared_ptr<stream> self{new stream};
|
||||
self->network_session = std::move(network_session);
|
||||
|
||||
from_headset::headset_info_packet info{};
|
||||
self->network_session->send_control([&]() {
|
||||
from_headset::headset_info_packet info{};
|
||||
|
||||
auto view = self->system.view_configuration_views(self->viewconfig)[0];
|
||||
view = override_view(view, guess_model());
|
||||
auto view = self->system.view_configuration_views(self->viewconfig)[0];
|
||||
view = override_view(view, guess_model());
|
||||
|
||||
auto resolution_scale = application::get_config().resolution_scale;
|
||||
auto resolution_scale = application::get_config().resolution_scale;
|
||||
|
||||
view.recommendedImageRectWidth *= resolution_scale;
|
||||
view.recommendedImageRectHeight *= resolution_scale;
|
||||
view.recommendedImageRectWidth *= resolution_scale;
|
||||
view.recommendedImageRectHeight *= resolution_scale;
|
||||
|
||||
info.recommended_eye_width = view.recommendedImageRectWidth;
|
||||
info.recommended_eye_height = view.recommendedImageRectHeight;
|
||||
info.recommended_eye_width = view.recommendedImageRectWidth;
|
||||
info.recommended_eye_height = view.recommendedImageRectHeight;
|
||||
|
||||
auto [flags, views] = self->session.locate_views(
|
||||
XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO,
|
||||
self->instance.now(),
|
||||
application::space(xr::spaces::view));
|
||||
auto [flags, views] = self->session.locate_views(
|
||||
XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO,
|
||||
self->instance.now(),
|
||||
application::space(xr::spaces::view));
|
||||
|
||||
assert(views.size() == info.fov.size());
|
||||
assert(views.size() == info.fov.size());
|
||||
|
||||
for (auto [i, j]: std::views::zip(views, info.fov))
|
||||
{
|
||||
j = i.fov;
|
||||
}
|
||||
for (auto [i, j]: std::views::zip(views, info.fov))
|
||||
{
|
||||
j = i.fov;
|
||||
}
|
||||
|
||||
const auto & config = application::get_config();
|
||||
const auto & config = application::get_config();
|
||||
|
||||
if (self->instance.has_extension(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME))
|
||||
{
|
||||
info.available_refresh_rates = self->session.get_refresh_rates();
|
||||
if (std::ranges::find(info.available_refresh_rates, config.preferred_refresh_rate) != info.available_refresh_rates.end())
|
||||
info.preferred_refresh_rate = config.preferred_refresh_rate;
|
||||
else
|
||||
info.preferred_refresh_rate = self->session.get_current_refresh_rate();
|
||||
}
|
||||
if (self->instance.has_extension(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME))
|
||||
{
|
||||
info.available_refresh_rates = self->session.get_refresh_rates();
|
||||
if (std::ranges::find(info.available_refresh_rates, config.preferred_refresh_rate) != info.available_refresh_rates.end())
|
||||
info.preferred_refresh_rate = config.preferred_refresh_rate;
|
||||
else
|
||||
info.preferred_refresh_rate = self->session.get_current_refresh_rate();
|
||||
}
|
||||
|
||||
if (info.preferred_refresh_rate == 0)
|
||||
{
|
||||
spdlog::warn("Unable to detect preferred refresh rate, using {}", guessed_fps);
|
||||
info.preferred_refresh_rate = guessed_fps;
|
||||
}
|
||||
if (info.preferred_refresh_rate == 0)
|
||||
{
|
||||
spdlog::warn("Unable to detect preferred refresh rate, using {}", guessed_fps);
|
||||
info.preferred_refresh_rate = guessed_fps;
|
||||
}
|
||||
|
||||
if (info.available_refresh_rates.empty())
|
||||
spdlog::warn("Unable to detect refresh rates");
|
||||
if (info.available_refresh_rates.empty())
|
||||
spdlog::warn("Unable to detect refresh rates");
|
||||
|
||||
info.hand_tracking = config.check_feature(feature::hand_tracking);
|
||||
info.eye_gaze = config.check_feature(feature::eye_gaze);
|
||||
info.face_tracking2_fb = config.check_feature(feature::face_tracking);
|
||||
info.palm_pose = application::space(xr::spaces::palm_left) or application::space(xr::spaces::palm_right);
|
||||
info.passthrough = self->system.passthrough_supported() != xr::system::passthrough_type::no_passthrough;
|
||||
info.hand_tracking = config.check_feature(feature::hand_tracking);
|
||||
info.eye_gaze = config.check_feature(feature::eye_gaze);
|
||||
info.face_tracking2_fb = config.check_feature(feature::face_tracking);
|
||||
info.palm_pose = application::space(xr::spaces::palm_left) or application::space(xr::spaces::palm_right);
|
||||
info.passthrough = self->system.passthrough_supported() != xr::system::passthrough_type::no_passthrough;
|
||||
|
||||
audio::get_audio_description(info);
|
||||
if (not(config.check_feature(feature::microphone)))
|
||||
info.microphone = {};
|
||||
audio::get_audio_description(info);
|
||||
if (not(config.check_feature(feature::microphone)))
|
||||
info.microphone = {};
|
||||
|
||||
info.supported_codecs = decoder_impl::supported_codecs();
|
||||
|
||||
self->network_session->send_control(info);
|
||||
info.supported_codecs = decoder_impl::supported_codecs();
|
||||
return info;
|
||||
}());
|
||||
|
||||
self->network_thread = utils::named_thread("network_thread", &stream::process_packets, self.get());
|
||||
|
||||
@@ -835,21 +836,26 @@ void scenes::stream::render(const XrFrameState & frame_state)
|
||||
|
||||
// Network operations may be blocking, do them once everything was submitted
|
||||
{
|
||||
// Keep a copy of the feedback packets as they can be modified if they're encrypted
|
||||
std::vector<from_headset::feedback> feedbacks;
|
||||
std::vector<serialization_packet> packets;
|
||||
|
||||
feedbacks.reserve(current_blit_handles.size());
|
||||
packets.reserve(current_blit_handles.size());
|
||||
|
||||
for (const auto & handle: current_blit_handles)
|
||||
{
|
||||
if (handle)
|
||||
{
|
||||
auto & packet = packets.emplace_back();
|
||||
wivrn_session::control_socket_t::serialize(packet, handle->feedback);
|
||||
wivrn_session::control_socket_t::serialize(packet, feedbacks.emplace_back(handle->feedback));
|
||||
}
|
||||
}
|
||||
if (not packets.empty())
|
||||
{
|
||||
try
|
||||
{
|
||||
network_session->send_control(std::span(packets));
|
||||
network_session->send_control(std::move(packets));
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
|
||||
@@ -142,6 +142,7 @@ public:
|
||||
void on_focused() override;
|
||||
void on_unfocused() override;
|
||||
|
||||
void operator()(to_headset::crypto_handshake &&) {};
|
||||
void operator()(to_headset::handshake &&) {};
|
||||
void operator()(to_headset::video_stream_data_shard &&);
|
||||
void operator()(to_headset::haptics &&);
|
||||
|
||||
@@ -60,7 +60,7 @@ void scenes::stream::read_actions()
|
||||
}
|
||||
try
|
||||
{
|
||||
network_session->send_stream(inputs);
|
||||
network_session->send_stream(std::move(inputs));
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
|
||||
@@ -71,10 +71,10 @@ void scenes::stream::operator()(to_headset::video_stream_description && desc)
|
||||
|
||||
void scenes::stream::operator()(to_headset::timesync_query && query)
|
||||
{
|
||||
from_headset::timesync_response response{};
|
||||
response.query = query.query;
|
||||
response.response = instance.now();
|
||||
network_session->send_stream(response);
|
||||
network_session->send_stream(from_headset::timesync_response{
|
||||
.query = query.query,
|
||||
.response = instance.now(),
|
||||
});
|
||||
}
|
||||
|
||||
void scenes::stream::operator()(audio_data && data)
|
||||
@@ -87,7 +87,7 @@ void scenes::stream::send_feedback(const wivrn::from_headset::feedback & feedbac
|
||||
{
|
||||
try
|
||||
{
|
||||
network_session->send_control(feedback);
|
||||
network_session->send_control(wivrn::from_headset::feedback{feedback});
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
|
||||
@@ -337,7 +337,7 @@ void scenes::stream::tracking()
|
||||
wivrn_session::stream_socket_t::serialize(packets.emplace_back(), i);
|
||||
}
|
||||
|
||||
network_session->send_stream(std::span(packets));
|
||||
network_session->send_stream(std::move(packets));
|
||||
|
||||
t0 += tracking_period;
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 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
|
||||
|
||||
namespace utils
|
||||
{
|
||||
template <class... Ts>
|
||||
struct overloaded : Ts...
|
||||
{
|
||||
using Ts::operator()...;
|
||||
};
|
||||
template <class... Ts>
|
||||
overloaded(Ts...) -> overloaded<Ts...>;
|
||||
} // namespace utils
|
||||
+84
-43
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include "wivrn_client.h"
|
||||
#include "secrets.h"
|
||||
#include "spdlog/common.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include <arpa/inet.h>
|
||||
@@ -39,6 +40,14 @@
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
const char * handshake_error::what() const noexcept
|
||||
{
|
||||
return message.c_str();
|
||||
}
|
||||
|
||||
handshake_error::handshake_error(std::string_view message) :
|
||||
message("Handshake error: " + std::string{message}) {}
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T>
|
||||
@@ -49,82 +58,114 @@ void init_stream(T & stream)
|
||||
} // namespace
|
||||
|
||||
template <typename T>
|
||||
void wivrn_session::handshake(T address, bool tcp_only)
|
||||
void wivrn_session::handshake(T address, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter)
|
||||
{
|
||||
// FIXME this comment
|
||||
// Wait for handshake on control socket,
|
||||
// then send ours on stream or control socket,
|
||||
// finally wait for second server handshake
|
||||
|
||||
pollfd fds{};
|
||||
fds.events = POLLIN;
|
||||
fds.fd = control.get_fd();
|
||||
|
||||
auto timeout = std::chrono::steady_clock::now() + 5s;
|
||||
auto receive = [&](std::optional<std::chrono::seconds> timeout = std::nullopt) {
|
||||
std::chrono::steady_clock::time_point timeout_abs{};
|
||||
if (timeout)
|
||||
timeout_abs = std::chrono::steady_clock::now() + *timeout;
|
||||
|
||||
// Loop because TCP socket may return partial data
|
||||
while (true)
|
||||
{
|
||||
int r = ::poll(&fds, 1, 5000);
|
||||
if (r < 0)
|
||||
throw std::system_error(errno, std::system_category());
|
||||
|
||||
if (r > 0 && (fds.revents & POLLIN))
|
||||
while (true)
|
||||
{
|
||||
auto packet = control.receive();
|
||||
if (not packet)
|
||||
continue;
|
||||
try
|
||||
{
|
||||
auto h = std::get<to_headset::handshake>(*packet);
|
||||
if (h.stream_port > 0 && !tcp_only)
|
||||
{
|
||||
stream = decltype(stream)();
|
||||
stream.connect(address, h.stream_port);
|
||||
init_stream(stream);
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
spdlog::error("Error when expecting handshake: {}", e.what());
|
||||
throw std::runtime_error("Invalid handshake received");
|
||||
}
|
||||
}
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout_abs - std::chrono::steady_clock::now());
|
||||
int r = ::poll(&fds, 1, std::max<int>(ms.count(), 100));
|
||||
if (r < 0)
|
||||
throw std::system_error(errno, std::system_category());
|
||||
|
||||
if (std::chrono::steady_clock::now() >= timeout)
|
||||
throw std::runtime_error("No handshake received");
|
||||
if (r > 0 && (fds.revents & POLLIN))
|
||||
{
|
||||
auto packet = control.receive();
|
||||
if (not packet)
|
||||
continue;
|
||||
|
||||
return std::move(*packet);
|
||||
}
|
||||
|
||||
if (std::chrono::steady_clock::now() >= timeout_abs)
|
||||
throw std::runtime_error("Timeout");
|
||||
}
|
||||
};
|
||||
|
||||
send_control(from_headset::crypto_handshake{.public_key = headset_keypair.public_key()});
|
||||
|
||||
to_headset::crypto_handshake crypto_handshake = std::get<to_headset::crypto_handshake>(receive(10s));
|
||||
|
||||
crypto::key server_key = crypto::key::from_public_key(crypto_handshake.public_key);
|
||||
|
||||
const std::string pin = crypto_handshake.pin_required ? pin_enter(control.get_fd()) : "000000";
|
||||
|
||||
spdlog::info("Using pin \"{}\"", pin);
|
||||
|
||||
secrets s{headset_keypair, server_key, pin};
|
||||
control.set_aes_key_and_ivs(s.control_key, s.control_iv_to_headset, s.control_iv_from_headset);
|
||||
|
||||
// Confirm that encryption is set up
|
||||
send_control(from_headset::crypto_handshake{});
|
||||
|
||||
to_headset::handshake h{std::get<to_headset::handshake>(receive(10s))};
|
||||
if (h.stream_port > 0 && !tcp_only)
|
||||
{
|
||||
stream = decltype(stream)();
|
||||
|
||||
stream.set_aes_key_and_ivs(s.stream_key, s.stream_iv_header_to_headset, s.stream_iv_header_from_headset);
|
||||
stream.connect(address, h.stream_port);
|
||||
init_stream(stream);
|
||||
}
|
||||
|
||||
// may be on control socket if forced TCP
|
||||
send_stream(from_headset::handshake{});
|
||||
|
||||
// Wait for second handshake
|
||||
auto timeout = std::chrono::steady_clock::now() + 10s;
|
||||
while (true)
|
||||
{
|
||||
if (poll(
|
||||
[](const auto && packet) { return std::is_same_v<std::remove_cvref_t<decltype(packet)>, to_headset::handshake>; },
|
||||
std::chrono::milliseconds(100)))
|
||||
if (poll([](const auto && packet) { return std::is_same_v<std::remove_cvref_t<decltype(packet)>, to_headset::handshake>; }, 100ms))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (std::chrono::steady_clock::now() >= timeout)
|
||||
throw std::runtime_error("Failed to establish connection");
|
||||
|
||||
// If using stream socket, the handshake might be lost
|
||||
if (stream)
|
||||
{
|
||||
stream.send(from_headset::handshake{});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wivrn_session::wivrn_session(in6_addr address, int port, bool tcp_only) :
|
||||
wivrn_session::wivrn_session(in6_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter) :
|
||||
control(address, port), stream(-1), address(address)
|
||||
{
|
||||
char buffer[100];
|
||||
spdlog::info("Connection to {}:{}", inet_ntop(AF_INET6, &address, buffer, sizeof(buffer)), port);
|
||||
handshake(address, tcp_only);
|
||||
try
|
||||
{
|
||||
handshake(address, tcp_only, headset_keypair, pin_enter);
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
throw handshake_error{e.what()};
|
||||
}
|
||||
}
|
||||
|
||||
wivrn_session::wivrn_session(in_addr address, int port, bool tcp_only) :
|
||||
wivrn_session::wivrn_session(in_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter) :
|
||||
control(address, port), stream(-1), address(address)
|
||||
{
|
||||
char buffer[100];
|
||||
spdlog::info("Connection to {}:{}", inet_ntop(AF_INET, &address, buffer, sizeof(buffer)), port);
|
||||
handshake(address, tcp_only);
|
||||
try
|
||||
{
|
||||
handshake(address, tcp_only, headset_keypair, pin_enter);
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
throw handshake_error{e.what()};
|
||||
}
|
||||
}
|
||||
|
||||
+14
-15
@@ -19,12 +19,23 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "crypto.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include "wivrn_sockets.h"
|
||||
#include <chrono>
|
||||
#include <poll.h>
|
||||
|
||||
using namespace wivrn;
|
||||
|
||||
class handshake_error : public std::exception
|
||||
{
|
||||
std::string message;
|
||||
|
||||
public:
|
||||
const char * what() const noexcept override;
|
||||
handshake_error(std::string_view message);
|
||||
};
|
||||
|
||||
class wivrn_session
|
||||
{
|
||||
public:
|
||||
@@ -36,13 +47,13 @@ private:
|
||||
stream_socket_t stream;
|
||||
|
||||
template <typename T>
|
||||
void handshake(T address, bool tcp_only);
|
||||
void handshake(T address, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
|
||||
|
||||
public:
|
||||
std::variant<in_addr, in6_addr> address;
|
||||
|
||||
wivrn_session(in6_addr address, int port, bool tcp_only);
|
||||
wivrn_session(in_addr address, int port, bool tcp_only);
|
||||
wivrn_session(in6_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
|
||||
wivrn_session(in_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
|
||||
wivrn_session(const wivrn_session &) = delete;
|
||||
wivrn_session & operator=(const wivrn_session &) = delete;
|
||||
|
||||
@@ -52,12 +63,6 @@ public:
|
||||
control.send(std::forward<T>(packet));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void serialize_control(serialization_packet & p, const T & data)
|
||||
{
|
||||
control.serialize<T>(p, data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void send_stream(T && packet)
|
||||
{
|
||||
@@ -67,12 +72,6 @@ public:
|
||||
control.send(std::forward<T>(packet));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void serialize_stream(serialization_packet & p, const T & data)
|
||||
{
|
||||
stream.serialize<T>(p, data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int poll(T && visitor, std::chrono::milliseconds timeout)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user