blob: 6ff3fc7d0d5f7ee5d48061897a33a783ba9be0c8 [file] [log] [blame]
Tim Yao10e5d9b2020-12-30 17:38:10 -08001#ifndef __IPCBUFFER_H
2#define __IPCBUFFER_H
3
Tim Yao6017d492023-08-07 15:05:53 -07004#include <time.h>
Tim Yao10e5d9b2020-12-30 17:38:10 -08005#include <boost/interprocess/managed_shared_memory.hpp>
6
7using namespace boost::interprocess;
8
9class IpcBuffer
10{
11public:
12 IpcBuffer(const char *name, size_t capacity);
13 ~IpcBuffer();
14
15 size_t size() const { return size_; }
16 size_t capacity() const { return capacity_; }
17 size_t write(const uint8_t *data, size_t bytes);
18 size_t read(uint8_t *data, size_t bytes);
19 void write_nb(const uint8_t *data, size_t bytes);
20 void get_write_position(uint64_t& time, uint64_t& position);
21 void reset();
22 uint8_t *start_ptr();
Tim Yao6017d492023-08-07 15:05:53 -070023 uint64_t wp() { return wr_position_; }
Tim Yao10e5d9b2020-12-30 17:38:10 -080024 const char *name();
25
Tim Yao6017d492023-08-07 15:05:53 -070026 uint32_t getUnderrun() { return underrun_; }
27 void incUnderrun() { underrun_++; }
28
29 void addSilence(size_t count) { silence_inserted_ += count; }
30 size_t getSilenceInserted() { return silence_inserted_; }
31
32 void setMeta(uint64_t meta_64, uint32_t meta_32);
33 void getMeta(struct timespec *ts, uint64_t *meta_64, uint32_t *meta_32);
34
Tim Yao10e5d9b2020-12-30 17:38:10 -080035private:
36 // Note: members can be access from different process
37 // only keep information which are common to all processes
38 // addresses must be mapped during runtime
39 size_t begin_index_, end_index_, size_, capacity_;
40 managed_shared_memory::handle_t handle_;
41 std::string name_;
42 bool blocking_;
Tim Yao6017d492023-08-07 15:05:53 -070043 interprocess_mutex mutex_;
Tim Yao10e5d9b2020-12-30 17:38:10 -080044 uint64_t wr_position_;
45 uint64_t wr_time_;
Tim Yao6017d492023-08-07 15:05:53 -070046 uint32_t underrun_;
47 size_t silence_inserted_;
48 struct timespec meta_ts_;
49 uint64_t meta_64_;
50 uint32_t meta_32_;
Tim Yao10e5d9b2020-12-30 17:38:10 -080051};
52
53#endif