1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_mapped_file.hpp> // use for Coliru
#include <boost/interprocess/containers/vector.hpp> // boost/containers/vector.hpp
#include <boost/interprocess/containers/string.hpp> // boost/containers/string.hpp
#include <iostream>
#include <sys/time.h>
#include <stdio.h>
// void_allocator;
namespace bip = boost::interprocess;
typedef unsigned char uchar;
//Typedefs of allocators and containers
typedef bip::managed_shared_memory::segment_manager segment_manager_t;
typedef bip::allocator<void, segment_manager_t> void_allocator;
typedef void_allocator::rebind<uchar>::other uchar_allocator;
typedef bip::vector<uchar, uchar_allocator> uchar_vector;
template <typename Alloc = std::allocator<uchar> >
struct BasicInData {
public:
BasicInData(Alloc alloc = {}) : image(alloc)
{ }
template <typename T>
BasicInData(double x, int sizeImg, uchar_vector& image, Alloc alloc = {}) :
x(x), sizeImg(sizeImg), image(alloc)
{ }
double x = 0;
int sizeImg = 0;
uchar_vector image;
};
using InData = BasicInData<>; // just heap allocated
namespace Shared {
using segment = bip::managed_shared_memory;
using segment_manager = segment::segment_manager;
template <typename T> using alloc = bip::allocator<T, segment_manager>;
template <typename T> using vector = bip::vector<T, alloc<T> >;
using InData = BasicInData<alloc<uchar> >; // shared memory version
vector<InData>& locate(segment& smt) {
auto* v = smt.find_or_construct<vector<InData> >("InDataVector")(smt.get_segment_manager());
assert(v);
return *v;
}
}
int main(int argc, char* argv[]) {
if(argc == 1){ //Parent process
// Remove shared memory on construction and destruction
bip::shared_memory_object::remove("MySharedMemory");
// Create a new segment with given name and size
struct timeval tv;
gettimeofday(&tv, NULL);
struct shm_remove
{
shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
~shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
}remover;
Shared::segment smt(bip::create_only,"MySharedMemory", 100000000);
auto &data = Shared::locate(smt);
//Shared::alloc bip::alloc_inst (data);
cv::Mat_<cv::Vec3b> mat;
cv::VideoCapture vcap(0);
Shared::InData id(smt.get_segment_manager());
if (!vcap.isOpened())
return -1;
char count = 0;
while (1) {
vcap >> mat;
printf ("count: %d \n", count); count++;
int image_size = mat.total() * mat.elemSize();
printf ("size: %d \n", image_size);
id.sizeImg = image_size;
printf ("id's sizeImg: %d \n", id.sizeImg * sizeof(uchar));
id.image.resize(image_size * sizeof(uchar));
int totalSize = image_size * sizeof(uchar);
printf ("count: %d \n", count);
memcpy(&id.image[0], mat.data, image_size * sizeof(uchar));
//Launch child process
gettimeofday(&tv, NULL);
double time = ((double)tv.tv_usec/1000000);
id.x = time;
data.push_back(id);
if((100000000 / count) <= (totalSize*20)){ printf("Getting video data done"); break; }
}
std::string s(argv[0]); s += " child";
if(0 != std::system(s.c_str()))
return 1;
// check child has destroyed the vector
if(smt.find<Shared::vector<InData>>("InDataVector").first)
return 1;
} else{
// Open the managed segment
bip::managed_shared_memory segment(bip::open_only, "MySharedMemory");
// Find the vector using c-string name
bip::vector<InData> *myvector = segment.find<bip::vector<InData>>("InDataVector").first;
// Use vector in reverse order
bip::vector<InData>::iterator it;
cv::Mat_<cv::Vec3b> im;
for(it = myvector->begin(); it !=myvector->end(); ++it){
im.resize(it->sizeImg);
memcpy(im.data, &it->image[0], it->sizeImg);
cv::imshow("window1", im);
}
segment.destroy<bip::vector<InData>>("InDataVector");
return 0;
}
return 0;
}
|