add SharedMemoryWriter support
This commit is contained in:
@@ -47,6 +47,9 @@ private:
|
||||
std::string handleSetEHDRRatioMin(const std::string& value);
|
||||
std::string handleSetEHDRRatioMax(const std::string& value);
|
||||
std::string handleGetEHDRStatus();
|
||||
std::string handleEnableSharedMemory(const std::string& name, const std::string& buffer_size);
|
||||
std::string handleDisableSharedMemory();
|
||||
std::string handleGetSharedMemoryStatus();
|
||||
|
||||
// Helper functions
|
||||
static VX_IMAGE_FORMAT stringToFormat(const std::string& format);
|
||||
|
||||
62
include/vizionstreamer/SharedMemoryWriter.h
Normal file
62
include/vizionstreamer/SharedMemoryWriter.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* VizionStreamer - Shared Memory Writer Interface
|
||||
* Copyright (c) 2025 Maik Jurischka
|
||||
*
|
||||
* Licensed under CC BY-NC-SA 4.0
|
||||
* https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <atomic>
|
||||
|
||||
// Shared memory header structure for lock-free synchronization
|
||||
struct SharedMemoryHeader {
|
||||
uint32_t magic; // Magic number for validation (0x56495A4E = "VIZN")
|
||||
uint32_t width; // Frame width in pixels
|
||||
uint32_t height; // Frame height in pixels
|
||||
uint32_t format; // Format enum (matches VX_IMAGE_FORMAT)
|
||||
uint32_t data_size; // Actual frame data size in bytes
|
||||
uint64_t timestamp_ns; // Timestamp in nanoseconds
|
||||
uint32_t frame_sequence; // Monotonic frame counter
|
||||
std::atomic<uint32_t> write_sequence; // Lock-free synchronization counter
|
||||
char format_str[16]; // Format string ("YUY2", "MJPG", etc.)
|
||||
uint8_t reserved[72]; // Reserved for future expansion (padding to 128 bytes)
|
||||
};
|
||||
|
||||
static_assert(sizeof(SharedMemoryHeader) == 128, "SharedMemoryHeader must be exactly 128 bytes");
|
||||
|
||||
class SharedMemoryWriter {
|
||||
public:
|
||||
explicit SharedMemoryWriter(std::string name);
|
||||
~SharedMemoryWriter();
|
||||
|
||||
// Initialize shared memory region with given buffer size
|
||||
bool create(size_t frame_buffer_size);
|
||||
|
||||
// Write frame to shared memory with lock-free protocol
|
||||
bool writeFrame(const uint8_t* data, size_t size,
|
||||
int width, int height,
|
||||
const std::string& format,
|
||||
uint64_t timestamp_ns = 0);
|
||||
|
||||
// Cleanup shared memory
|
||||
void destroy();
|
||||
|
||||
// Status queries
|
||||
bool isCreated() const { return shm_fd_ >= 0; }
|
||||
std::string getName() const { return shm_name_; }
|
||||
size_t getBufferSize() const { return buffer_size_; }
|
||||
uint32_t getFrameCount() const { return frame_counter_; }
|
||||
|
||||
private:
|
||||
SharedMemoryHeader* getHeader();
|
||||
|
||||
std::string shm_name_; // Shared memory object name
|
||||
int shm_fd_; // File descriptor from shm_open
|
||||
void* shm_ptr_; // Mapped memory pointer
|
||||
size_t buffer_size_; // Total size (header + frame data)
|
||||
uint32_t frame_counter_; // Frame sequence counter
|
||||
};
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <vizionsdk/VizionSDK.h>
|
||||
#include "vizionstreamer/GStreamerPipeline.h"
|
||||
#include "vizionstreamer/SharedMemoryWriter.h"
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
@@ -29,11 +30,19 @@ public:
|
||||
|
||||
void setPipelineDescription(const std::string& pipeline);
|
||||
|
||||
// Shared memory output control
|
||||
bool enableSharedMemory(const std::string& name, size_t buffer_size);
|
||||
void disableSharedMemory();
|
||||
[[nodiscard]] bool isSharedMemoryEnabled() const { return shmWriter_ != nullptr && shmWriter_->isCreated(); }
|
||||
[[nodiscard]] std::string getSharedMemoryName() const { return shmWriter_ ? shmWriter_->getName() : ""; }
|
||||
[[nodiscard]] size_t getSharedMemorySize() const { return shmWriter_ ? shmWriter_->getBufferSize() : 0; }
|
||||
|
||||
private:
|
||||
void acquisitionLoop();
|
||||
|
||||
std::shared_ptr<VxCamera> camera_;
|
||||
std::unique_ptr<GStreamerPipeline> gstPipeline_;
|
||||
std::unique_ptr<SharedMemoryWriter> shmWriter_;
|
||||
std::unique_ptr<std::thread> acquisitionThread_;
|
||||
std::atomic<bool> running_;
|
||||
std::mutex mutex_;
|
||||
|
||||
Reference in New Issue
Block a user