HoviTron Video Pipeline
opencvStreamer.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
42
43enum class InternalFormatClass {
44 YUV,
45 RGB
46};
47
48enum class HvtHandleType {
49 CONTEXT,
50 SEMAPHORE
51};
52
53template<typename OurType, typename HvtHandle, HvtHandleType htype>
54struct Handle {
55 Handle() : type(htype) {}
56 static constexpr HvtHandleType staticType = htype;
57
58 static OurType* check(HvtHandle hvtHandle) {
59 Handle* h = reinterpret_cast<Handle*>(hvtHandle);
60 if (h && staticType == h->type) {
61 return static_cast<OurType*>(h);
62 }
63 else {
64 throw HvtResult::HVT_ERROR_INVALID_HANDLE;
65 }
66 }
67
68 static OurType* opt_check(HvtHandle hvtHandle) {
69 if (!hvtHandle) {
70 return nullptr;
71 }
72 return check(hvtHandle);
73 }
74
75 HvtHandle to_handle() {
76 return reinterpret_cast<HvtHandle>(this);
77 }
78private:
79 HvtHandleType type;
80};
81
82struct Semaphore : public Handle<Semaphore, HvtSemaphore, HvtHandleType::SEMAPHORE>
83{
84 vk::UniqueSemaphore sem;
85};
86
87// Implementation class
88class opencvStreamer : public Handle<opencvStreamer, HvtStreamingContext, HvtHandleType::CONTEXT>
89{
90 using Clock = std::chrono::steady_clock;
91 static constexpr auto numSlots = 3;
92public:
93 opencvStreamer(std::span<const uint8_t, VK_UUID_SIZE> uuid);
94 void enumerateStreamsParameters(uint32_t* streamsCount, HvtRGBDStreamParameters* parameters) const;
95 void importStreamImages(const HvtStreamImagesExportInfo& exportInfos);
96 Semaphore* importSemaphore(const HvtSemaphoreExportInfo& exportInfos);
97 void destroySemaphore(Semaphore* sem) const;
98 void startStreaming();
99 void acquireStreamsFrames(const HvtAcquireStreamFramesInfo& infos);
100 void releaseStreamsFrames(Semaphore* waitSem);
101 void stopStreaming();
103private:
104 void initVk(std::span<const uint8_t, VK_UUID_SIZE> uuid);
105 uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
106 void initSettings();
107 void initVkResources();
108
109 template<typename Closure>
110 void oneTimeSubmit(Closure&& func, vk::SubmitInfo submitInfo = vk::SubmitInfo({}, {}, {}, {}));
111
112 struct ImageSlot {
113 vk::UniqueImage image;
114 vk::UniqueDeviceMemory memory;
115 };
116
117 struct ReadStream {
118 std::ifstream colorFile;
119 std::ifstream depthFile;
120 size_t colorFrameStride;
121 size_t colorFrameSize;
122 size_t depthFrameStride;
123 size_t depthFrameSize;
124
125 glm::ivec2 resolution;
126 HvtProjectionType projectionType;
127 HvtIntrinsics intrinsics;
128 HvtExtrinsics extrinsics;
129
130 float anear; float afar;
131
132 vk::Format colorFormat;
133 vk::Format depthFormat;
134 vk::UniqueDeviceMemory stagingMemory;
135 vk::UniqueBuffer stagingBuffer;
136 char* stagingMapping = nullptr;
137
138 std::array<ImageSlot, numSlots> colorSlots;
139 std::array<ImageSlot, numSlots> depthSlots;
140 bool importedColor = false;
141 bool importedDepth = false;
142
143 Clock::duration framePeriod;
144 int frameCount = 1;
145 int streamedFrame = -1;
146
147 std::string fileNameColor = "";
148 std::string fileNameDepth = "";
149
150 int frameIndex(Clock::duration time) const;
151 bool nextFrameReady(Clock::duration time) const;
152 };
153
154 void streamingLoop();
155 void uploadFrame(vk::CommandBuffer cmd, int streamId, ReadStream& stream, int frame);
156 void swapStreamingToPending();
157 void swapPendingToReading();
158
159 std::optional<rvs::Config> config;
160 InternalFormatClass internalFormat = InternalFormatClass::YUV;
161
162 std::vector<ReadStream> readStreams;
163
164 vk::UniqueInstance instance;
165 vk::PhysicalDevice phyDevice = VK_NULL_HANDLE;
166 vk::UniqueDevice device;
167 uint32_t queueFamilyIndex;
168 vk::Queue queue;
169 vk::Queue syncQueue;
170 std::mutex queueMutex;
171 //vk::UniqueCommandPool commandPool;
172
173 std::array<Semaphore*, numSlots> usageWaitSemaphores{};
174 uint32_t slotReadingIndex = 0;
175 uint32_t slotPendingIndex = 1;
176 uint32_t slotStreamingIndex = 2;
177 std::atomic_bool newDataInPending = false;
178 std::mutex indicesMutex;
180 std::atomic_bool running = false;
181 std::thread streamingThread;
182};
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.