diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index a77116b9..7036ef10 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -28,7 +28,6 @@ #include "spdlog/spdlog.h" #include "utils/named_thread.h" #include "utils/ranges.h" -#include "utils/sync_queue.h" #include "vk/pipeline.h" #include "vk/shader.h" #include "wifi_lock.h" @@ -78,7 +77,6 @@ static const std::array supported_formats = std::shared_ptr scenes::stream::create(std::unique_ptr network_session) { std::shared_ptr self{new stream}; - spdlog::info("decoder_mutex.native_handle() = {}", (void *)self->decoder_mutex.native_handle()); self->network_session = std::move(network_session); from_headset::headset_info_packet info{}; @@ -126,8 +124,6 @@ std::shared_ptr scenes::stream::create(std::unique_ptrupdate_local_floor(self->instance.now()); self->network_thread = utils::named_thread("network_thread", &stream::process_packets, self.get()); - self->video_thread = utils::named_thread("video_thread", &stream::video, self.get()); - self->command_buffer = std::move(self->device.allocateCommandBuffers({ .commandPool = *self->commandpool, .level = vk::CommandBufferLevel::ePrimary, @@ -224,9 +220,6 @@ scenes::stream::~stream() { exit(); - if (video_thread.joinable()) - video_thread.join(); - if (tracking_thread && tracking_thread->joinable()) tracking_thread->join(); @@ -242,7 +235,7 @@ void scenes::stream::push_blit_handle(shard_accumulator * decoder, std::shared_p return; { - std::unique_lock lock(decoder_mutex); + std::shared_lock lock(decoder_mutex); for (auto & i: decoders) { if (i.decoder.get() == decoder) @@ -347,6 +340,7 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render) if (exiting) application::pop_scene(); + std::shared_lock lock(decoder_mutex); if (decoders.empty()) should_render = false; @@ -531,7 +525,6 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render) std::array pose{}; std::array fov{}; { - std::unique_lock lock(decoder_mutex); // Search for frame with desired display time on all decoders // If no such frame exists, use the latest frame for each decoder auto common_frame = accumulator_images::common_frame(decoders, predicted_display_time); @@ -727,14 +720,12 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render) void scenes::stream::exit() { exiting = true; - shard_queue.close(); } void scenes::stream::setup(const to_headset::video_stream_description & description) { std::unique_lock lock(decoder_mutex); - // FIXME: stop video thread decoders.clear(); if (description.items.empty()) @@ -910,38 +901,6 @@ void scenes::stream::setup_reprojection_swapchain() reprojector.emplace(device, physical_device, images, swapchain_images, extent, swapchains[0].format(), *video_stream_description); } -void scenes::stream::video() -{ -#ifdef __ANDROID__ - application::instance().setup_jni(); -#endif - - while (not exiting) - { - try - { - auto shard = shard_queue.pop(); - - if (shard.stream_item_idx >= decoders.size()) - { - // We don't know (yet?) about this stream, ignore packet - return; - } - auto idx = shard.stream_item_idx; - decoders[idx].decoder->push_shard(std::move(shard)); - } - catch (utils::sync_queue_closed &) - { - break; - } - catch (std::exception & e) - { - spdlog::error("Exception in video thread: {}", e.what()); - exit(); - } - } -} - scene::meta & scenes::stream::get_meta_scene() { static meta m{ diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 04308402..bf848de6 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -24,10 +24,10 @@ #include "render/imgui_impl.h" #include "scene.h" #include "stream_reprojection.h" -#include "utils/sync_queue.h" #include "wivrn_client.h" #include "wivrn_packets.h" #include +#include #include #include @@ -79,11 +79,8 @@ private: xr::space local_floor = nullptr; std::atomic tracking_prediction_offset; std::optional tracking_thread; - std::thread video_thread; - utils::sync_queue shard_queue; - - std::mutex decoder_mutex; + std::shared_mutex decoder_mutex; std::optional video_stream_description; std::vector decoders; // Locked by decoder_mutex vk::raii::DescriptorPool blit_descriptor_pool = nullptr; @@ -157,7 +154,6 @@ public: private: void process_packets(); void tracking(); - void video(); void read_actions(); void setup(const to_headset::video_stream_description &); diff --git a/client/scenes/stream_network.cpp b/client/scenes/stream_network.cpp index ce098de8..cef386ae 100644 --- a/client/scenes/stream_network.cpp +++ b/client/scenes/stream_network.cpp @@ -44,7 +44,14 @@ void scenes::stream::process_packets() void scenes::stream::operator()(to_headset::video_stream_data_shard && shard) { - shard_queue.push(std::move(shard)); + std::shared_lock lock(decoder_mutex); + if (shard.stream_item_idx >= decoders.size()) + { + // We don't know (yet?) about this stream, ignore packet + return; + } + auto idx = shard.stream_item_idx; + decoders[idx].decoder->push_shard(std::move(shard)); } void scenes::stream::operator()(to_headset::audio_stream_description && desc)