HoviTron Video Pipeline
JsonStreamer.h
1/* ----------------------
2* Copyright 2023 Université Libre de Bruxelles(ULB), Universidad Politécnica de Madrid(UPM), CREAL, Deutsches Zentrum für Luft - und Raumfahrt(DLR)
3
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at < http://www.apache.org/licenses/LICENSE-2.0%3E
7
8* Unless required by applicable law or agreed to in writing, software
9* distributed under the License is distributed on an "AS IS" BASIS,
10* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11* See the License for the specific language governing permissionsand
12* limitations under the License.
13---------------------- */
14
15
16#pragma once
17
18#include <filesystem>
19#include <span>
20#include <fstream>
21#include <vector>
22#include <optional>
23#include <thread>
24#include <mutex>
25#include <atomic>
26
27#include <glm/vec2.hpp>
28
29#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
30#include <vulkan/vulkan.hpp>
31
32#define HVT_FUNCTION_PROTOTYPES
33
34#ifdef WIN32
35#define HVT_ENTRY_ATTR __declspec(dllexport)
36#endif
37#include "../RVSVulkan/SourcesVulkan/HvtStreamingAPI.h"
38
39#include "../RVSVulkan/RVSConfig/include/Config.h"
40
41
42enum class HvtHandleType {
43 CONTEXT,
44 SEMAPHORE
45};
46
47template<typename OurType, typename HvtHandle, HvtHandleType htype>
48struct Handle{
49 Handle() : type(htype) {}
50 static constexpr HvtHandleType staticType = htype;
51
52 static OurType* check(HvtHandle hvtHandle) {
53 Handle* h = reinterpret_cast<Handle*>(hvtHandle);
54 if(h && staticType == h->type) {
55 return static_cast<OurType*>(h);
56 } else {
57 throw HvtResult::HVT_ERROR_INVALID_HANDLE;
58 }
59 }
60
61 static OurType* opt_check(HvtHandle hvtHandle) {
62 if(!hvtHandle) {
63 return nullptr;
64 }
65 return check(hvtHandle);
66 }
67
68 HvtHandle to_handle(){
69 return reinterpret_cast<HvtHandle>(this);
70 }
71 private:
72 HvtHandleType type;
73};
74
75struct Semaphore : public Handle<Semaphore, HvtSemaphore, HvtHandleType::SEMAPHORE>
76{
77 vk::UniqueSemaphore sem;
78};
79
80// Implementation class
81class JsonStreamer : public Handle<JsonStreamer, HvtStreamingContext, HvtHandleType::CONTEXT>
82{
83 using Clock = std::chrono::steady_clock;
84 static constexpr auto numSlots = 3;
85 public:
86 JsonStreamer(std::span<const uint8_t, VK_UUID_SIZE> uuid);
87 void enumerateStreamsParameters(uint32_t* streamsCount, HvtRGBDStreamParameters* parameters) const;
88 void importStreamImages(const HvtStreamImagesExportInfo& exportInfos);
89 Semaphore* importSemaphore(const HvtSemaphoreExportInfo& exportInfos);
90 void destroySemaphore(Semaphore* sem) const;
91 void startStreaming();
92 void acquireStreamsFrames(const HvtAcquireStreamFramesInfo& infos);
93 void releaseStreamsFrames(Semaphore* waitSem);
94 void stopStreaming();
96 private:
97 void initVk(std::span<const uint8_t, VK_UUID_SIZE> uuid);
98 uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
99 void initSettings();
100 void initVkResources();
101
102 template<typename Closure>
103 void oneTimeSubmit(Closure&& func, vk::SubmitInfo submitInfo = vk::SubmitInfo({},{},{},{}));
104
105 struct ImageSlot{
106 vk::UniqueImage image;
107 vk::UniqueDeviceMemory memory;
108 };
109
110 struct ReadStream{
111 std::ifstream colorFile;
112 std::ifstream depthFile;
113 size_t colorFrameStride;
114 size_t colorFrameSize;
115 size_t depthFrameStride;
116 size_t depthFrameSize;
117
118 glm::ivec2 resolution;
119 HvtProjectionType projectionType;
120 HvtIntrinsics intrinsics;
121 HvtExtrinsics extrinsics;
122
123 float anear; float afar;
124
125 vk::Format colorFormat;
126 vk::Format depthFormat;
127 vk::UniqueDeviceMemory stagingMemory;
128 vk::UniqueBuffer stagingBuffer;
129 char* stagingMapping = nullptr;
130
131 std::array<ImageSlot, numSlots> colorSlots;
132 std::array<ImageSlot, numSlots> depthSlots;
133 bool importedColor = false;
134 bool importedDepth = false;
135
136 Clock::duration framePeriod;
137 int frameCount = 1;
138 int streamedFrame = -1;
139
140 int frameIndex(Clock::duration time) const;
141 bool nextFrameReady(Clock::duration time) const;
142 };
143
144 void streamingLoop();
145 void uploadFrame(vk::CommandBuffer cmd, int streamId, ReadStream &stream, int frame);
146 void swapStreamingToPending();
147 void swapPendingToReading();
148
149 std::optional<rvs::Config> config;
150
151 std::vector<ReadStream> readStreams;
152
153 vk::UniqueInstance instance;
154 vk::PhysicalDevice phyDevice = VK_NULL_HANDLE;
155 vk::UniqueDevice device;
156 uint32_t queueFamilyIndex;
157 vk::Queue queue;
158 vk::Queue syncQueue;
159 std::mutex queueMutex;
160 //vk::UniqueCommandPool commandPool;
161
162 std::array<Semaphore*, numSlots> usageWaitSemaphores{};
163 uint32_t slotReadingIndex = 0;
164 uint32_t slotPendingIndex = 1;
165 uint32_t slotStreamingIndex = 2;
166 std::atomic_bool newDataInPending = false;
167 std::mutex indicesMutex;
169 std::atomic_bool running = false;
170 std::thread streamingThread;
171};
Parameters for query of the current frames infos.
Description of an RGBD stream.
Export info for images of a stream.
Union of possible intrinsics types data.