OrangePi3588Media/include/utils/shared_state.h
2026-01-13 21:14:25 +08:00

64 lines
1.5 KiB
C++

#pragma once
#include <cstdint>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
#include "frame/frame.h"
// SharedState is a process-wide, cross-plugin state store.
// It is implemented in a shared library so all dynamically loaded node plugins
// see the same singleton instance.
namespace rk3588 {
#if defined(_WIN32)
#if defined(RK3588_BUILD_SHARED_STATE)
#define RK3588_SHARED_STATE_API __declspec(dllexport)
#else
#define RK3588_SHARED_STATE_API __declspec(dllimport)
#endif
#else
#define RK3588_SHARED_STATE_API
#endif
struct TrackedObject {
int cls_id = -1;
int track_id = -1;
float score = 0.0f;
Rect bbox{}; // coordinates in snapshot image space (img_w/img_h)
bool confirmed = false;
};
struct TargetsSnapshot {
uint64_t update_steady_us = 0;
int img_w = 0;
int img_h = 0;
std::string model_name;
std::vector<TrackedObject> objects;
};
RK3588_SHARED_STATE_API uint64_t NowSteadyUs();
class RK3588_SHARED_STATE_API SharedState {
public:
static SharedState& Instance();
void SetTargets(std::string key, TargetsSnapshot snap);
std::shared_ptr<const TargetsSnapshot> GetTargets(const std::string& key) const;
void ClearTargets(const std::string& key);
private:
SharedState() = default;
SharedState(const SharedState&) = delete;
SharedState& operator=(const SharedState&) = delete;
mutable std::mutex mu_;
std::unordered_map<std::string, std::shared_ptr<const TargetsSnapshot>> targets_;
};
} // namespace rk3588