nx_server_plugin_sdk 1.0 Server Plugin SDK

data_manager.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
5 #include <atomic>
6 #include <chrono>
7 #include <condition_variable>
8 #include <mutex>
9 #include <optional>
10 #include <queue>
11 #include <string>
12 #include <thread>
13 #include <vector>
14
15 #include <camera/camera_plugin.h>
16 #include <nx/kit/json.h>
17 #include <nx/sdk/cloud_storage/helpers/data.h>
18 #include <nx/sdk/cloud_storage/helpers/device_archive.h>
19 #include <nx/sdk/cloud_storage/i_device_agent.h>
20 #include <nx/sdk/cloud_storage/i_media_data_packet.h>
21 #include <nx/sdk/i_device_info.h>
22
23 namespace nx::vms_server_plugins::cloud_storage::stub {
24
25 class FileImpl;
26
27 class File
28 {
29 public:
30 File(const std::string& path);
31 ~File();
32
33 template<typename Container>
34 Container readAll() const;
35
36 template<typename Container>
37 void writeAll(const Container& data, bool replaceContents);
38
39 private:
40 std::unique_ptr<FileImpl> m_fileImpl;
41 };
42
43 struct MediaFileHeader;
44
45 class WritableMediaFile
46 {
47 public:
48 WritableMediaFile(
49 const std::shared_ptr<std::mutex>& mutex,
50 const std::string& path,
51 std::chrono::milliseconds startTime,
52 const nx::sdk::IList<nx::sdk::cloud_storage::ICodecInfo>* codecList,
53 const char* opaqueMetadata);
54
55 ~WritableMediaFile();
56
57 void writeMediaPacket(const nx::sdk::cloud_storage::IMediaDataPacket* packet);
58 void close(std::chrono::milliseconds duration);
59 int size() const;
60
61 private:
62 std::shared_ptr<std::mutex> m_mutex;
63 std::unique_ptr<File> m_file;
64 const std::string m_path;
65 std::chrono::milliseconds m_startTime{};
66 std::vector<uint8_t> m_buffer;
67 };
68
69 class ReadableMediaFile
70 {
71 public:
72 ReadableMediaFile(const std::string& path);
73 ~ReadableMediaFile();
74
75 std::vector<nx::sdk::cloud_storage::CodecInfoData> codecInfo() const;
76 std::string opaqueMetadata() const;
77 std::optional<nx::sdk::cloud_storage::MediaPacketData> getNextPacket() const;
78 std::string name() const;
79 void seek(
80 int64_t timestamp,
81 bool findKeyFrame,
82 int64_t* selectedPosition);
83
84 int size() const;
85
86 private:
87 std::vector<uint8_t> m_fileData;
88 std::unique_ptr<MediaFileHeader> m_header;
89 int m_begin = 0;
90 mutable int m_current = 0;
91 std::string m_name;
92 };
93
94 struct DeviceArchiveIndex
95 {
96 std::map<int, std::vector<nx::sdk::cloud_storage::MediaChunk>> chunksPerStream;
97 nx::sdk::cloud_storage::DeviceDescription deviceDescription;
98 };
99
100 struct ArchiveIndex
101 {
102 std::vector<DeviceArchiveIndex> deviceArchiveIndexList;
103
104 void sort();
105 };
106
107 class DataManager
108 {
109 public:
110 using SaveHandler =
111 std::function<void(nx::sdk::cloud_storage::MetadataType, nx::sdk::ErrorCode)>;
112
113 DataManager(const std::string& workDir);
114 ~DataManager();
115
116 void setSaveHandler(SaveHandler handler);
117 nx::sdk::ErrorCode saveBookmark(const nx::sdk::cloud_storage::Bookmark& data);
118 void deleteBookmark(const char* id);
119 std::string queryBookmarks(const nx::sdk::cloud_storage::BookmarkFilter& filter) const;
120
121 nx::sdk::ErrorCode saveMotion(const nx::sdk::cloud_storage::Motion& data);
122 std::string queryMotion(const nx::sdk::cloud_storage::MotionFilter& filter) const;
123
124 nx::sdk::ErrorCode saveObjectTrack(const nx::sdk::cloud_storage::ObjectTrack& data);
125 std::string queryAnalytics(const nx::sdk::cloud_storage::AnalyticsFilter& filter) const;
126 std::string queryAnalyticsPeriods(const nx::sdk::cloud_storage::AnalyticsFilter& filter) const;
127
128 void addDevice(const nx::sdk::cloud_storage::DeviceDescription& deviceDescription);
129 std::unique_ptr<WritableMediaFile> writableMediaFile(
130 const std::string& deviceId,
131 int streamIndex,
132 std::chrono::milliseconds timestamp,
133 const nx::sdk::IList<nx::sdk::cloud_storage::ICodecInfo>* codecList,
134 const char* opaqueMetadata) const;
135
136 std::unique_ptr<ReadableMediaFile> readableMediaFile(
137 const std::string& bucketUrl,
138 const std::string& deviceId,
139 int streamIndex,
140 int64_t startTimeMs,
141 int64_t durationMs) const;
142
143 ArchiveIndex getArchive(std::optional<std::chrono::system_clock::time_point> startTime) const;
144 void saveTrackImage(
145 const char* data,
146 nx::sdk::cloud_storage::TrackImageType type);
147 std::string fetchTrackImage(const char* objectTrackId, nx::sdk::cloud_storage::TrackImageType type);
148
149 void run();
150 void flushMetadata(nx::sdk::cloud_storage::MetadataType type);
151
152 std::pair<std::string, int> bucketUrlAndId() const;
153
154 private:
155 std::string m_workDir;
156 std::shared_ptr<std::mutex> m_mutex{std::make_shared<std::mutex>()};
157 std::vector<nx::sdk::cloud_storage::Bookmark> m_cloudBookmarks;
158 std::vector<nx::sdk::cloud_storage::Motion> m_cloudMotion;
159 std::vector<nx::sdk::cloud_storage::ObjectTrack> m_cloudAnalytics;
160 std::queue<nx::sdk::cloud_storage::Bookmark> m_bookmarksToSend;
161 std::queue<nx::sdk::cloud_storage::Motion> m_motionToSend;
162 std::queue<nx::sdk::cloud_storage::ObjectTrack> m_analyticsToSend;
163 SaveHandler m_saveHandler;
164 std::thread m_workThread;
165 std::atomic<bool> m_needToStop{false};
166 std::condition_variable m_cond;
167 bool m_needToFlushBookmarks{false};
168 bool m_needToFlushMotion{false};
169 bool m_needToFlushAnalytics{false};
170 std::pair<std::string, int> m_bucketUrlToId;
171
172 private:
173 std::string devicePath(const std::string& deviceId) const;
174
175 template<typename Data>
176 std::queue<Data>* dataQueue();
177
178 template<typename Data>
179 std::vector<Data>* cloudData();
180
181 template<typename Data>
182 bool* needToFlush();
183
184 template<typename Data>
185 nx::sdk::cloud_storage::MetadataType metadataType() const;
186
187 template<typename Data>
188 void processQueue(std::queue<Data>& dataQueue, std::unique_lock<std::mutex>& lock);
189
190 template<typename Data>
191 nx::sdk::ErrorCode saveMetadataImpl(const Data& data);
192
193 template<typename Data>
194 void saveObject(const Data& data, const std::string& fileName);
195
196 template<typename Data>
197 std::vector<Data> allObjects(const std::string& fileName) const;
198
199 template<typename Data>
200 std::vector<Data> allObjectsNoLock(const std::string& fileName) const;
201
202 template<typename Data>
203 std::optional<Data> oneObject(const std::string& fileName) const;
204
205 template<typename DataList>
206 void flushToFile(const DataList& data, const std::string& fileName);
207
208 template<typename Data>
209 std::vector<Data> load(const std::string& fileName) const;
210
211 std::vector<nx::kit::Json> loadListNoLock(const std::string& fileName) const;
212
213 std::string mediaFilePath(
214 const std::string& bucketUrl,
215 const std::string& deviceId,
216 int streamIndex,
217 std::chrono::milliseconds timestamp) const;
218
219 std::string mediaFilePath(
220 const std::string& bucketUrl,
221 const std::string& deviceId,
222 int streamIndex,
223 int64_t startTimeMs,
224 int64_t durationMs) const;
225 };
226
227 } //namespace nx::vms_server_plugins::cloud_storage::stub
nx::vms_server_plugins::cloud_storage::stub::DataManager
Definition: data_manager.h:107
nx::sdk::cloud_storage::Motion
Definition: data.h:339
nx::vms_server_plugins::cloud_storage::stub::File
Definition: data_manager.h:27
nx::sdk::cloud_storage::IMediaDataPacket
Definition: i_media_data_packet.h:17
nx::vms_server_plugins::cloud_storage::stub::ArchiveIndex
Definition: data_manager.h:100
nx::vms_server_plugins::cloud_storage::stub
Definition: data_manager.cpp:36
nx::sdk::cloud_storage::Bookmark
Definition: data.h:243
json.h
nx::vms_server_plugins::cloud_storage::stub::DeviceArchiveIndex
Definition: data_manager.h:94
nx::vms_server_plugins::cloud_storage::stub::MediaFileHeader
Definition: data_manager.cpp:619
nx::vms_server_plugins::cloud_storage::stub::ReadableMediaFile
Definition: data_manager.h:69
nx::sdk::cloud_storage::ObjectTrack
Definition: data.h:569
nx::sdk::cloud_storage::MotionFilter
Definition: data.h:410
nx::sdk::cloud_storage::AnalyticsFilter
Definition: data.h:766
nx::sdk::cloud_storage::DeviceDescription
Definition: data.h:209
nx::sdk::cloud_storage::BookmarkFilter
Definition: data.h:284
nx::sdk::IList< nx::sdk::cloud_storage::ICodecInfo >
nx::vms_server_plugins::cloud_storage::stub::WritableMediaFile
Definition: data_manager.h:45