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