nx_server_plugin_sdk 1.0 Server Plugin 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 <atomic>
6 #include <condition_variable>
7 #include <deque>
8 #include <memory>
9 #include <string>
10 #include <thread>
11 #include <vector>
12
13 #include <nx/sdk/analytics/helpers/consuming_device_agent.h>
14 #include <nx/sdk/analytics/helpers/object_metadata_packet.h>
15 #include <nx/sdk/analytics/helpers/pixel_format.h>
16 #include <nx/sdk/helpers/uuid_helper.h>
17
18 #include "engine.h"
19 #include "stub_analytics_plugin_special_objects_ini.h"
20
21 namespace nx {
22 namespace vms_server_plugins {
23 namespace analytics {
24 namespace stub {
25 namespace special_objects {
26
27 const std::string kBlinkingObjectType = "nx.stub.blinkingObject";
28 const std::string kFixedObjectType = "nx.stub.fixedObject";
29 const std::string kPointObjectType = "nx.stub.pointObject";
30 const std::string kCounterObjectType = "nx.stub.counter";
31 const std::string kEmptyNameObjectType = "nx.stub.emptyNameObject";
32
33 class DeviceAgent: public nx::sdk::analytics::ConsumingDeviceAgent
34 {
35 public:
36 DeviceAgent(Engine* engine, const nx::sdk::IDeviceInfo* deviceInfo);
37 virtual ~DeviceAgent() override;
38
39 protected:
40 virtual std::string manifestString() const override;
41
42 virtual nx::sdk::Result<const nx::sdk::ISettingsResponse*> settingsReceived() override;
43
44 virtual bool pushCompressedVideoFrame(
45 nx::sdk::Ptr<const nx::sdk::analytics::ICompressedVideoPacket> videoFrame) override;
46
47 virtual bool pullMetadataPackets(
48 std::vector<nx::sdk::Ptr<nx::sdk::analytics::IMetadataPacket>>* metadataPackets) override;
49
50 private:
51 std::vector<nx::sdk::Ptr<nx::sdk::analytics::IMetadataPacket>> cookSomeObjects();
52
53 nx::sdk::Ptr<nx::sdk::analytics::IObjectMetadata> cookBlinkingObjectIfNeeded(
54 int64_t metadataTimestampUs);
55
56 void addBlinkingObjectIfNeeded(
57 int64_t metadataTimestampUs,
58 std::vector<nx::sdk::Ptr<nx::sdk::analytics::IMetadataPacket>>* metadataPackets,
59 nx::sdk::Ptr<nx::sdk::analytics::ObjectMetadataPacket> objectMetadataPacket);
60
61 void addFixedObjectIfNeeded(
62 nx::sdk::Ptr<nx::sdk::analytics::ObjectMetadataPacket> objectMetadataPacket);
63
64 void addCounterIfNeeded(
65 nx::sdk::Ptr<nx::sdk::analytics::ObjectMetadataPacket> objectMetadataPacket);
66
67 void addPointIfNeeded(
68 nx::sdk::Ptr<nx::sdk::analytics::ObjectMetadataPacket> objectMetadataPacket);
69
70 void addEmptyNameObjectIfNeeded(
71 nx::sdk::Ptr<nx::sdk::analytics::ObjectMetadataPacket> objectMetadataPacket);
72
73 void processVideoFrame(
74 nx::sdk::Ptr<const nx::sdk::analytics::IDataPacket> videoFrame,
75 const char* func);
76
77 void processCustomMetadataPacket(
78 nx::sdk::Ptr<const nx::sdk::analytics::ICustomMetadataPacket> customMetadataPacket,
79 const char* func);
80
81 void cleanUpTimestampQueue();
82
83 private:
84 Engine* const m_engine;
85
86 int m_frameCounter = 0;
87
88 std::deque<int64_t> m_frameTimestampUsQueue;
89 int64_t m_lastVideoFrameTimestampUs = 0;
90 int64_t m_lastBlinkingObjectTimestampUs = 0;
91
92 struct DeviceAgentSettings
93 {
94 bool needToGenerateObjects() const
95 {
96 return generateFixedObject
97 || generateCounter
98 || generatePoint
99 || blinkingObjectPeriodMs.load() != std::chrono::milliseconds::zero();
100 }
101
102 std::atomic<bool> generateFixedObject{false};
103
104 std::mutex fixedObjectColorMutex;
105 std::string fixedObjectColor;
106
107 std::atomic<bool> generateCounter{false};
108 std::atomic<bool> generatePoint{false};
109 std::atomic<bool> generateEmptyNameObject{false};
110
111 std::atomic<std::chrono::milliseconds> blinkingObjectPeriodMs{
112 std::chrono::milliseconds::zero()};
113
114 std::atomic<bool> blinkingObjectInDedicatedPacket{false};
115
116 std::atomic<int> generateObjectsEveryNFrames{1};
117
118 std::atomic<std::chrono::milliseconds> additionalFrameProcessingDelayMs{
119 std::chrono::milliseconds::zero()};
120
121 std::atomic<std::chrono::milliseconds> overallMetadataDelayMs{
122 std::chrono::milliseconds::zero()};
123
124 std::atomic<float> counterBoundingBoxSideSize{0};
125 std::atomic<float> counterBoundingBoxXOffset{0};
126 std::atomic<float> counterBoundingBoxYOffset{0};
127 };
128
129 DeviceAgentSettings m_deviceAgentSettings;
130
131 std::mutex m_objectGenerationMutex;
132 int m_counterObjectAttributeValue = 0;
133
134 const sdk::Uuid m_blinkingObjectTrackId = sdk::UuidHelper::randomUuid();
135 const sdk::Uuid m_fixedObjectTrackId = sdk::UuidHelper::randomUuid();
136 const sdk::Uuid m_counterObjectTrackId = sdk::UuidHelper::randomUuid();
137 };
138
139 } // namespace special_objects
140 } // namespace stub
141 } // namespace analytics
142 } // namespace vms_server_plugins
143 } // namespace nx
nx::vms_server_plugins::analytics::stub::special_objects::DeviceAgent::manifestString
virtual std::string manifestString() const override
Definition: device_agent.cpp:59
nx::vms_server_plugins::analytics::stub::special_objects::Engine
Definition: engine.h:18
nx::sdk::Ptr
Definition: ptr.h:18
nx::sdk::analytics::ConsumingDeviceAgent
Definition: consuming_device_agent.h:36
nx::vms_server_plugins::analytics::stub::special_objects::DeviceAgent
Definition: device_agent.h:33
nx::sdk::Uuid
Definition: uuid.h:22
nx::sdk::IDeviceInfo
Definition: i_device_info.h:13
nx::sdk::Result
Definition: result.h:52
nx
Definition: apple_utils.h:6
nx::vms_server_plugins::analytics::stub::special_objects::DeviceAgent::pushCompressedVideoFrame
virtual bool pushCompressedVideoFrame(nx::sdk::Ptr< const nx::sdk::analytics::ICompressedVideoPacket > videoFrame) override
Definition: device_agent.cpp:191
nx::vms_server_plugins::analytics::stub::special_objects::DeviceAgent::settingsReceived
virtual nx::sdk::Result< const nx::sdk::ISettingsResponse * > settingsReceived() override
Definition: device_agent.cpp:91