1 #ifndef sharedmem_HEADER_GUARD
2 #define sharedmem_HEADER_GUARD
42 #include <semaphore.h>
51 static std::mutex test_mutex;
103 virtual void clientClose() = 0;
108 virtual void put(std::vector<uint8_t> &inp_payload,
void *meta_) = 0;
109 virtual void put(uint8_t* buf_,
void* meta_) = 0;
148 virtual void clientClose();
154 virtual void put(std::vector<uint8_t> &inp_payload,
void* meta_);
155 virtual void put(uint8_t* buf_,
void* meta_);
165 void put(std::vector<uint8_t> &inp_payload);
179 long int mstimestamp;
198 virtual void clientClose();
203 virtual void put(std::vector<uint8_t> &inp_payload,
void* meta_);
204 virtual void put(uint8_t* buf_,
void* meta_);
283 void serverPush(std::vector<uint8_t> &inp_payload,
void* meta);
301 PyObject *getBufferListPy();
316 void serverPush(std::vector<uint8_t> &inp_payload);
319 bool clientPull(
int &index_out,
int &size_out);
349 bool clientPull(
int &index_out,
int &size_out);
359 bool serverPushPyRGB(PyObject *po, SlotNumber slot,
long int mstimestamp);
386 ShmemFrameFilter(
const char* name,
int n_cells, std::size_t n_bytes,
int mstimeout=0);
413 RGBShmemFrameFilter(
const char* name,
int n_cells,
int width,
int height,
int mstimeout=0);
433 #define server_init(CLASSNAME, TYPENAME)\
434 void CLASSNAME::serverInit() {\
436 shm_unlink(payload_name.c_str());\
437 shm_unlink(meta_name.c_str());\
438 fd = shm_open(payload_name.c_str(),O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0600);\
439 fd_= shm_open(meta_name.c_str(), O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0600);\
440 if (fd == -1 or fd_==-1) {\
441 perror("valkka_core: sharedmem.cpp: SharedMemSegment::serverInit: shm_open failed");\
445 r = ftruncate(fd, n_bytes);\
447 r_= ftruncate(fd_, sizeof(TYPENAME));\
448 if (r != 0 or r_ !=0) {\
449 perror("valkka_core: sharedmem.cpp: SharedMemSegment::serverInit: ftruncate failed");\
452 ptr = mmap(0, n_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);\
453 ptr_= mmap(0, sizeof(TYPENAME), PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);\
454 if (ptr == MAP_FAILED or ptr_ == MAP_FAILED) {\
455 perror("valkka_core: sharedmem.cpp: SharedMemSegment::serverInit: mmap failed");\
458 payload = (uint8_t*) ptr;\
459 meta = (TYPENAME*) ptr_;\
467 #define client_init(CLASSNAME, TYPENAME) \
468 bool CLASSNAME::clientInit() { \
470 fd = shm_open(payload_name.c_str(), O_RDONLY, 0400);\
471 fd_= shm_open(meta_name.c_str(), O_RDONLY, 0400);\
472 if (fd == -1 or fd_==-1) {\
478 ptr =mmap(0, n_bytes, PROT_READ, MAP_SHARED, fd, 0);\
479 ptr_ =mmap(0, sizeof(TYPENAME), PROT_READ, MAP_SHARED, fd_, 0);\
480 if (ptr == MAP_FAILED or ptr_ == MAP_FAILED) {\
481 std::cout << "valkka_core: sharedmem.cpp: SharedMemSegment::clientInit: mmap failed" << std::endl;\
486 payload = (uint8_t*) ptr;\
487 meta = (TYPENAME*) ptr_;\
496 #define server_close(CLASSNAME, TYPENAME) \
497 void CLASSNAME::serverClose() { \
498 if (munmap(ptr, n_bytes)!=0 or munmap(ptr_, sizeof(TYPENAME)!=0)) { \
499 perror("valkka_core: sharedmem.cpp: CLASSNAME::serverClose: munmap failed"); \
502 if (shm_unlink(payload_name.c_str())!=0 or shm_unlink(meta_name.c_str())!=0) { \
503 perror("valkka_core: sharedmem.cpp: CLASSNAME::serverClose: shm_unlink failed"); \
508 #define client_close(CLASSNAME, TYPENAME) \
509 void CLASSNAME::clientClose() { \
510 if (munmap(ptr, n_bytes)!=0 or munmap(ptr_, sizeof(TYPENAME)!=0)) { \
511 perror("valkka_core: sharedmem.cpp: CLASSNAME::clientClose: munmap failed"); \
517 #define copy_meta_from(CLASSNAME, TYPENAME) \
518 void CLASSNAME::copyMetaFrom(void *meta_) { \
519 *meta = *((TYPENAME*)(meta_)); \
522 #define copy_meta_to(CLASSNAME, TYPENAME) \
523 void CLASSNAME::copyMetaTo(void *meta_) { \
524 *((TYPENAME*)(meta_)) = *meta; \
Decoded YUV frame in a non-planar format (thus "NP")
Definition: frame.h:437
A file descriptor for running select and poll with shmem ring buffers.
Definition: sharedmem.h:56
The mother class of all frame filters! FrameFilters are used to create "filter chains".
Definition: framefilter.h:44
Frame: An abstract queueable class.
Definition: frame.h:112
A Shmem segment describing an RGB24 frame.
Definition: sharedmem.h:188
virtual void serverInit()
Uses shmem_open with write rights. used by the constructor if is_server=true. Init with correct metad...
virtual void serverClose()
Erases the shmem segment. used by the constructor if is_server=true.
virtual bool clientInit()
Uses shmem_open with read-only rights. Init with correct metadata serialization.
virtual void copyMetaTo(void *meta_)
Dereference metadata pointer correctly and copy the contents from this memory segment's metadata.
virtual void putAVRGBFrame(AVRGBFrame *f)
Copy from AVFrame->data directly. Only metadata used: payload size.
Definition: sharedmem.cpp:220
virtual void put(std::vector< uint8_t > &inp_payload, void *meta_)
typecast void to std::size_t
Definition: sharedmem.cpp:200
virtual std::size_t getSize()
Client: return metadata = the size of the payload (not the maximum size). Uses SharedMemSegment::getM...
Definition: sharedmem.cpp:238
virtual void copyMetaFrom(void *meta_)
Dereference metadata pointer correctly and copy the contents into this memory segment's metadata.
Like ShmemFrameFilter.
Definition: sharedmem.h:408
virtual void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: sharedmem.cpp:908
RGBShmemFrameFilter(const char *name, int n_cells, int width, int height, int mstimeout=0)
Default constructor.
Definition: sharedmem.cpp:899
Interprocess shared memory ring buffer synchronized with posix semaphores.
Definition: sharedmem.h:226
void clearEventFd()
Clear event file descriptor.
Definition: sharedmem.cpp:365
bool getClientState()
Are the shmem segments available for client?
Definition: sharedmem.cpp:438
sem_t * flagsema
Posix semaphore objects (semaphore counter, semaphore for the overflow flag)
Definition: sharedmem.h:251
int n_bytes
Parameters defining the shmem ring buffer (number, size)
Definition: sharedmem.h:246
uint8_t ** cache
One can expose a cache instead as numpy arrays - but this requires an additional copy step - enable w...
Definition: sharedmem.h:260
struct timespec ts
Timespec for semaphore timeouts.
Definition: sharedmem.h:255
bool clientPullThread(int &index_out, void *meta)
multithreading version: releases GIL
Definition: sharedmem.cpp:607
void clearFlag()
Client: call this after handling the ring-buffer overflow.
Definition: sharedmem.cpp:401
void setFlag()
Server: call this to indicate a ring-buffer overflow.
Definition: sharedmem.cpp:380
void zero()
Force reset. Semaphore value is set to 0.
Definition: sharedmem.cpp:420
int getFlagValue()
Used by SharedMemoryRingBuffer::flagIsSet()
Definition: sharedmem.cpp:412
virtual ~SharedMemRingBufferBase()
Default destructor.
Definition: sharedmem.cpp:289
int mstimeout
Semaphore timeout in milliseconds.
Definition: sharedmem.h:247
SharedMemRingBufferBase(const char *name, int n_cells, std::size_t n_bytes, int mstimeout=0, bool is_server=false)
Default constructor.
Definition: sharedmem.cpp:245
std::string flagsema_name
Name to identify the posix semaphore used for the overflow flag.
Definition: sharedmem.h:253
bool clientPull(int &index_out, void *meta)
Returns the index of SharedMemoryRingBuffer::shmems that was just written.
Definition: sharedmem.cpp:511
void setEventFd()
Set event file descriptor.
Definition: sharedmem.cpp:346
int fd
A file descriptor for poll and select.
Definition: sharedmem.h:256
std::vector< SharedMemSegment * > shmems
Shared memory segments - can be exposed as numpy arrays.
Definition: sharedmem.h:259
bool is_server
Are we on the server side or not?
Definition: sharedmem.h:248
void serverPush(std::vector< uint8_t > &inp_payload, void *meta)
Copies payload to ring buffer.
Definition: sharedmem.cpp:451
int getValue()
Returns the current index (next to be read) of the shmem buffer.
Definition: sharedmem.cpp:430
bool flagIsSet()
Client: call this to see if there has been a ring-buffer overflow.
Definition: sharedmem.cpp:391
std::string sema_name
Name to identify the posix semaphore counter.
Definition: sharedmem.h:252
void clientUseFd(EventFd &event_fd)
Activate the file descriptor api for usage with select and poll.
Definition: sharedmem.cpp:342
int index
The index of cell that has just been written. Remember: server and client see their own copies of thi...
Definition: sharedmem.h:254
void serverUseFd(EventFd &event_fd)
Activate the file descriptor api for usage with select and poll.
Definition: sharedmem.cpp:314
SharedMemRingBuffer for AVRGBFrame.
Definition: sharedmem.h:328
SharedMemRingBufferRGB(const char *name, int n_cells, int width, int height, int mstimeout=0, bool is_server=false)
Default ctor.
Definition: sharedmem.cpp:700
virtual ~SharedMemRingBufferRGB()
Default destructor.
Definition: sharedmem.cpp:719
bool clientPull(int &index_out, int &size_out)
Legacy support.
Definition: sharedmem.cpp:818
bool clientPullFrameThread(int &index_out, RGB24Meta &meta)
For multithreading (instead of multiprocessing) applications: releases python GIL.
Definition: sharedmem.cpp:836
PyObject * clientPullPy()
Returns a python tuple of metadata (index, width, height, slot, timestamp)
Definition: sharedmem.cpp:841
bool clientPullFrame(int &index_out, RGB24Meta &meta)
Pulls payload and extended metadata.
Definition: sharedmem.cpp:827
Definition: sharedmem.h:308
bool serverPushPy(PyObject *po)
Push a numpy array.
Definition: sharedmem.cpp:649
virtual ~SharedMemRingBuffer()
Default destructor.
Definition: sharedmem.cpp:635
Handles a shared memory segment with metadata (the segment size)
Definition: sharedmem.h:85
virtual void copyMetaTo(void *meta_)=0
Dereference metadata pointer correctly and copy the contents from this memory segment's metadata.
virtual void serverClose()=0
Erases the shmem segment. used by the constructor if is_server=true.
uint8_t * payload
Pointer to payload.
Definition: sharedmem.h:123
virtual void copyMetaFrom(void *meta_)=0
Dereference metadata pointer correctly and copy the contents into this memory segment's metadata.
std::string payload_name
Name to identify the posix memory mapped file.
Definition: sharedmem.h:116
bool client_state
Was the shmem acquisition succesfull?
Definition: sharedmem.h:120
virtual bool clientInit()=0
Client: Uses shmem_open with read-only rights. Init with correct metadata serialization.
virtual void put(std::vector< uint8_t > &inp_payload, void *meta_)=0
Server: copy byte chunk into payload accompanied with metadata. Corrent typecast in child class metho...
virtual std::size_t getSize()=0
Client: return size of payload.
virtual void serverInit()=0
Server: Uses shmem_open with write rights. used by the constructor if is_server=true....
std::string meta_name
Name to identify the posix memory mapped file for the metadata.
Definition: sharedmem.h:117
std::string name
Name to identify the posix objects.
Definition: sharedmem.h:115
bool getClientState()
Was the shmem acquisition succesfull?
Definition: sharedmem.cpp:115
virtual ~SharedMemSegment()
Default destructor.
Definition: sharedmem.cpp:110
void close_()
Must be called before destruction. Releases correct amount of metadata bytes (depending on the subcla...
Definition: sharedmem.cpp:98
std::size_t n_bytes
Maximum size of the payload (this much is reserved)
Definition: sharedmem.h:125
void init()
Must be called after construction. Reserves shmem payload and correct metadata (depending on the subc...
Definition: sharedmem.cpp:82
void * ptr_
Raw pointers to shared memory.
Definition: sharedmem.h:119
SharedMemSegment(const char *name, std::size_t n_bytes, bool is_server=false)
Default constructor.
Definition: sharedmem.cpp:76
bool is_server
Client or server process?
Definition: sharedmem.h:118
This FrameFilter writes frames into a SharedMemRingBuffer.
Definition: sharedmem.h:373
ShmemFrameFilter(const char *name, int n_cells, std::size_t n_bytes, int mstimeout=0)
Default constructor.
Definition: sharedmem.cpp:878
virtual void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: sharedmem.cpp:886
Shared mem segment with simple metadata : just the payload length.
Definition: sharedmem.h:138
virtual void copyMetaTo(void *meta_)
Dereference metadata pointer correctly and copy the contents from this memory segment's metadata.
virtual void copyMetaFrom(void *meta_)
Dereference metadata pointer correctly and copy the contents into this memory segment's metadata.
virtual void serverClose()
Erases the shmem segment. used by the constructor if is_server=true.
virtual void putAVRGBFrame(AVRGBFrame *f)
Copy from AVFrame->data directly. Only metadata used: payload size.
Definition: sharedmem.cpp:171
virtual void put(std::vector< uint8_t > &inp_payload, void *meta_)
typecast void to std::size_t
Definition: sharedmem.cpp:150
virtual void serverInit()
Uses shmem_open with write rights. used by the constructor if is_server=true. Init with correct metad...
virtual bool clientInit()
Uses shmem_open with read-only rights. Init with correct metadata serialization.
virtual std::size_t getSize()
Client: return metadata = the size of the payload (not the maximum size). Uses SharedMemSegment::getM...
Definition: sharedmem.cpp:145
List of common header files.
Definition of FrameFilter and derived classes for various purposes.