/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 Patrick Nicolas * * 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 . */ #pragma once #include "app_pacer.h" #include "clock_offset.h" #include "compositor/compositor.h" #include "inplace_vector.hpp" #include "tracking_control.h" #include "utils/thread_safe.h" #include "wivrn_android_face_tracker.h" #include "wivrn_connection.h" #include "wivrn_controller.h" #include "wivrn_eye_tracker.h" #include "wivrn_fb_face2_tracker.h" #include "wivrn_generic_tracker.h" #include "wivrn_hmd.h" #include "wivrn_htc_face_tracker.h" #include "wivrn_ipc.h" #include "wivrn_packets.h" #include "wivrn_uinput.h" #include "xrt/xrt_results.h" #include "xrt/xrt_system.h" #include #include #include #include #include #include struct b_system; struct ipc_server; struct xrt_space_overseer; struct xrt_system_compositor; union xrt_session_event; namespace wivrn { class wivrn_eye_tracker; class wivrn_android_face_tracker; class wivrn_fb_face2_tracker; class wivrn_htc_face_tracker; class wivrn_generic_tracker; struct audio_device; class wivrn_session : public xrt_system_devices { std::unique_ptr connection; from_headset::headset_info_packet headset_info; // run-time editable settings thread_safe settings; wivrn::compositor compositor; pacing_app_factory app_pacers; b_system & xrt_system; ipc_server * mnd_ipc_server; xrt_space_overseer * space_overseer; xrt_system_compositor * system_compositor; std::mutex roles_mutex; xrt_system_roles roles{ .generation_id = 1, .left = -1, .right = -1, .gamepad = -1, }; tracking_control control; wivrn_hmd hmd; wivrn_controller left_controller; int32_t left_controller_index; wivrn_controller right_controller; int32_t right_controller_index; wivrn_controller left_hand_interaction; int32_t left_hand_interaction_index; wivrn_controller right_hand_interaction; int32_t right_hand_interaction_index; std::optional eye_tracker; std::optional android_face_tracker; std::optional fb_face2_tracker; std::optional htc_face_tracker; beman::inplace_vector::inplace_vector generic_trackers; std::optional uinput_handler; clock_offset_estimator offset_est; std::atomic tracking_latency; // production to reception time std::mutex csv_mutex; std::ofstream feedback_csv; std::unique_ptr audio_handle; // when sessions shall be destroyed, key is client id, value is timestamp thread_safe> session_loss; std::jthread net_thread; std::jthread worker_thread; wivrn_session(std::unique_ptr connection, b_system &); public: using base_t = xrt_system_devices; ~wivrn_session(); static xrt_result_t create_session(std::unique_ptr connection, b_system & system, xrt_system_devices ** out_xsysd, xrt_space_overseer ** out_xspovrs, xrt_system_compositor ** out_xsysc); void start(ipc_server *); void stop(); bool request_stop(); void quit_if_no_client(); clock_offset get_offset(); bool connected(); const from_headset::headset_info_packet & get_info() const { return headset_info; }; float default_fps(); locked get_settings() { return settings.lock(); } wivrn_hmd & get_hmd() { return hmd; } void add_tracking_request(device_id, int64_t at_ns, int64_t produced_ns, int64_t now); void add_tracking_request(device_id, int64_t at_ns, int64_t produced_ns); void operator()(from_headset::crypto_handshake &&) {} void operator()(from_headset::pin_check_1 &&) {} void operator()(from_headset::pin_check_3 &&) {} void operator()(from_headset::headset_info_packet &&); void operator()(const from_headset::settings_changed &); void operator()(from_headset::handshake &&) {} void operator()(const from_headset::tracking &); void operator()(from_headset::derived_pose &&); void operator()(from_headset::hand_tracking &&); void operator()(from_headset::body_tracking &&); void operator()(from_headset::inputs &&); void operator()(from_headset::hid::input && e); void operator()(from_headset::timesync_response &&); void operator()(from_headset::feedback &&); void operator()(from_headset::battery &&); void operator()(from_headset::visibility_mask_changed &&); void operator()(from_headset::session_state_changed &&); void operator()(from_headset::user_presence_changed &&); void operator()(from_headset::refresh_rate_changed &&); void operator()(from_headset::stream_tab_changed &&); void operator()(from_headset::override_foveation_center &&); void operator()(from_headset::get_application_list &&); void operator()(const from_headset::start_app &); void operator()(const from_headset::get_running_applications &); void operator()(const from_headset::set_active_application &); void operator()(const from_headset::stop_application &); void operator()(audio_data &&); void operator()(to_monado::stop &&); void operator()(to_monado::disconnect &&); void operator()(to_monado::set_bitrate &&); void operator()(to_headset::stream_tab_change &&); bool has_stream() { return connection->has_stream(); } template void send_stream(T && packet) { connection->send_stream(std::forward(packet)); } template void send_control(T && packet) { connection->send_control(std::forward(packet)); } xrt_result_t push_event(const xrt_session_event &); void set_foveated_size(uint32_t width, uint32_t height); void dump_time(const std::string & event, uint64_t frame, int64_t time, uint8_t stream = -1, const char * extra = ""); private: void run_net(std::stop_token stop); void run_worker(std::stop_token stop); void reconnect(std::stop_token stop); void pause_session(); void resume_session(); void update_client_states(bool visible, bool focused); void poll_session_loss(); // checks if a headset is usable with this session std::pair> validate_headset_info(const from_headset::headset_info_packet & info); // xrt_system implementation xrt_result_t get_roles(xrt_system_roles * out_roles); xrt_result_t feature_inc(xrt_device_feature_type type); xrt_result_t feature_dec(xrt_device_feature_type type); void destroy(); }; } // namespace wivrn