nx_metadata_sdk 1.0 Metadata SDK

device_agent.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 <set>
6
7 #include <nx/sdk/analytics/helpers/consuming_device_agent.h>
8 #include <nx/sdk/analytics/i_object_metadata_packet.h>
9
10 #include <nx/kit/json.h>
11
12 namespace nx {
13 namespace vms_server_plugins {
14 namespace analytics {
15 namespace stub {
16 namespace object_streamer {
17
18 class DeviceAgent: public nx::sdk::analytics::ConsumingDeviceAgent
19 {
20 public:
21 DeviceAgent(const nx::sdk::IDeviceInfo* deviceInfo, std::string pluginHomeDir);
22 virtual ~DeviceAgent() override;
23
24 protected:
25 virtual std::string manifestString() const override;
26
27 virtual bool pushCompressedVideoFrame(
28 const nx::sdk::analytics::ICompressedVideoPacket* videoFrame) override;
29
30 virtual void doSetNeededMetadataTypes(
31 nx::sdk::Result<void>* outValue,
32 const nx::sdk::analytics::IMetadataTypes* neededMetadataTypes) override;
33
34 virtual nx::sdk::Result<const nx::sdk::ISettingsResponse*> settingsReceived() override;
35
36 private:
37 struct Object
38 {
39 std::string typeId;
40 nx::sdk::Uuid trackId;
41 nx::sdk::analytics::Rect boundingBox;
42 std::map<std::string, std::string> attributes;
43 int frameNumberToGenerateObject = 0;
44 int64_t timestampUs = -1;
45 };
46
47 enum class Issue
48 {
49 objectStreamIsNotAValidJson,
50 objectStreamIsNotAJsonArray,
51 objectItemIsNotAJsonObject,
52 trackIdIsNotAString,
53 trackIdIsNotAUuid,
54 typeIdIsNotAString,
55 frameNumberIsNotANumber,
56 boundingBoxIsNotAJsonObject,
57 topLeftXIsNotANumber,
58 topLeftYIsNotANumber,
59 widthIsNotANumber,
60 heightIsNotANumber,
61 objectIsOutOfBounds,
62 attributesFieldIsNotAJsonObject,
63 attributeValueIsNotAString,
64 timestampIsNotANumber,
65 };
66
67 struct Issues
68 {
69 std::set<Issue> errors;
70 std::set<Issue> warnings;
71 };
72
73 struct StreamInfo
74 {
75 std::map<int, std::vector<Object>> objectsByFrameNumber;
76 std::set<std::string> objectTypeIds;
77 };
78
79 private:
80 static StreamInfo parseObjectStreamFile(const std::string& filePath, Issues* outIssues);
81
82 static bool parseCommonFields(
83 const nx::kit::Json& objectDescription,
84 Object* outObject,
85 Issues* outIssues);
86
87 static bool parseBoundingBox(
88 const nx::kit::Json& objectDescription,
89 nx::sdk::analytics::Rect* outBoundingBox,
90 Issues* outIssues);
91
92 static bool parseAttributes(
93 const nx::kit::Json& objectDescription,
94 std::map<std::string, std::string>* outAttributes,
95 Issues* outIssues);
96
97 static bool parseTimestamp(
98 const nx::kit::Json& objectDescription,
99 int64_t* outTimestamp,
100 Issues* outIssues);
101
102 static std::string issueToString(Issue issue);
103
104 static std::string makePluginDiagnosticEventDescription(const std::set<Issue>& issues);
105
106 private:
107 std::vector<nx::sdk::Ptr<nx::sdk::analytics::IObjectMetadataPacket>> generateObjects(
108 int frameNumber,
109 int64_t frameTimestampUs,
110 int64_t durationUs);
111
112 sdk::Ptr<sdk::ISettingsResponse> makeSettingsResponse(
113 const std::string& manifestFilePath,
114 const std::string& streamFilePath) const;
115
116 void reportIssues(const Issues& issues);
117
118 private:
119 StreamInfo m_streamInfo;
120 std::set<std::string> m_disabledObjectTypeIds;
121 int m_frameNumber = 0;
122 int m_maxFrameNumber = 0;
123 int64_t m_lastFrameTimestampUs = -1;
124 std::string m_pluginHomeDir;
125 bool m_isInitialSettings = true;
126 };
127
128 } // namespace object_streamer
129 } // namespace stub
130 } // namespace analytics
131 } // namespace vms_server_plugins
132 } // namespace nx
nx::vms_server_plugins::analytics::stub::object_streamer::DeviceAgent::settingsReceived
virtual nx::sdk::Result< const nx::sdk::ISettingsResponse * > settingsReceived() override
Definition: device_agent.cpp:159
nx::kit::detail::json11::Json
Definition: json.cpp:80
nx::sdk::analytics::ICompressedVideoPacket
Definition: i_compressed_video_packet.h:34
nx::sdk::Ptr
Definition: ptr.h:18
nx::vms_server_plugins::analytics::stub::object_streamer::DeviceAgent
Definition: device_agent.h:18
json.h
nx::sdk::analytics::ConsumingDeviceAgent
Definition: consuming_device_agent.h:38
nx::sdk::analytics::IMetadataTypes
Definition: i_metadata_types.h:13
nx::sdk::Result< void >
Definition: result.h:81
nx::sdk::Uuid
Definition: uuid.h:22
nx::sdk::IDeviceInfo
Definition: i_device_info.h:14
nx::sdk::Result
Definition: result.h:47
nx
Definition: apple_utils.h:6
nx::vms_server_plugins::analytics::stub::object_streamer::DeviceAgent::pushCompressedVideoFrame
virtual bool pushCompressedVideoFrame(const nx::sdk::analytics::ICompressedVideoPacket *videoFrame) override
Definition: device_agent.cpp:86
nx::sdk::analytics::Rect
Definition: rect.h:11