/* * 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 "utils/sync_queue.h" #include "wivrn_packets.h" #include #include #include #include #include #include #include #include #include #include #include #include #define DEUGLIFY(x) \ struct x##_deleter \ { \ void operator()(x * ptr) \ { \ x##_delete(ptr); \ } \ }; \ using x##_ptr = std::unique_ptr; DEUGLIFY(AImageReader) DEUGLIFY(AMediaCodec) class shard_accumulator; namespace scenes { class stream; } namespace wivrn::android { class decoder { public: struct pipeline_context; struct blit_target { vk::Image image; vk::ImageView image_view; vk::Offset2D offset; vk::Extent2D extent; std::shared_ptr framebuffer; std::shared_ptr pipeline; }; struct mapped_hardware_buffer; struct blit_handle { xrt::drivers::wivrn::from_headset::feedback feedback; xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t view_info; std::shared_ptr vk_data; AImage * aimage{}; ~blit_handle(); }; private: xrt::drivers::wivrn::to_headset::video_stream_description::item description; float fps; vk::raii::Device& device; AImageReader_ptr image_reader; AMediaCodec_ptr media_codec; std::weak_ptr weak_scene; shard_accumulator * accumulator; PFN_vkGetAndroidHardwareBufferPropertiesANDROID vkGetAndroidHardwareBufferPropertiesANDROID; void on_image_available(AImageReader * reader); static void on_image_available(void * context, AImageReader * reader); utils::sync_queue input_buffers; utils::sync_queue output_buffers; utils::sync_queue> frame_infos; std::thread output_releaser; static void on_media_error(AMediaCodec *, void * userdata, media_status_t error, int32_t actionCode, const char * detail); static void on_media_format_changed(AMediaCodec *, void * userdata, AMediaFormat *); static void on_media_input_available(AMediaCodec *, void * userdata, int32_t index); static void on_media_output_available(AMediaCodec *, void * userdata, int32_t index, AMediaCodecBufferInfo * bufferInfo); void push_nals(std::span> data, int64_t timestamp, uint32_t flags); std::vector blit_targets; std::shared_ptr pipeline; std::mutex hbm_mutex; std::unordered_map> hardware_buffer_map; vk::raii::RenderPass renderpass = nullptr; std::shared_ptr map_hardware_buffer(AImage *); public: decoder(vk::raii::Device& device, vk::raii::PhysicalDevice& physical_device, const xrt::drivers::wivrn::to_headset::video_stream_description::item & description, float fps, uint8_t stream_index, std::weak_ptr scene, shard_accumulator * accumulator); decoder(const decoder &) = delete; decoder(decoder &&) = delete; ~decoder(); void push_data(std::span> data, uint64_t frame_index, bool partial); void frame_completed(xrt::drivers::wivrn::from_headset::feedback &, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t & view_info); const auto & desc() const { return description; } static const vk::ImageLayout framebuffer_expected_layout = vk::ImageLayout::eColorAttachmentOptimal; static const vk::ImageUsageFlagBits framebuffer_usage = vk::ImageUsageFlagBits::eColorAttachment; void set_blit_targets(std::vector targets, vk::Format format); void blit(vk::raii::CommandBuffer& command_buffer, blit_handle & handle, std::span target_indices); void blit_finished(blit_handle & handle); }; } // namespace wivrn::android