I try to save an char* inside boost container but it fails. My char* is binary data on 2048 memory blocks. These binary data are a sound recorded with ALSA.
But when i save it in a vector of shared memory strings, it mutate somehow and i can't figure out how to fix it.
Edit and probably answer:
ALSA send void* buffers, so if i can create a shared memory vector of void* i may do the trick. So i need basically to create a vector of void* and each void* must be size fixed (in this case : 2048). I think that the boost::interprocess::basic_string
is the problem
End Edit
Here the full explanation :
I'm trying to listen from direct sound input with ALSA with a program and then use another program to write it to a file (or process anything on it)
I started with this question : Create a shared-memory vector of strings
Now i'm stuck, i don't know boost very well. I've created a github (https://github.com/Waxo/nodetest) with the full project. Alsa control with the method listen with callback just call a method with a (char* , int) prototype.
After building the project you can launch the ./nodetest
and ./nodetest arg
when ./nodetest
has said "Go"
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <iostream>
#include <atomic>
#include <future>
#include <iostream>
#include <alsa_control.h>
using std::cout;
using std::endl;
typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> MyShmString;
typedef boost::interprocess::allocator<MyShmString, boost::interprocess::managed_shared_memory::segment_manager> StringAllocator;
typedef boost::interprocess::vector<MyShmString, StringAllocator> MyShmStringVector;
class lambda_class {
public:
void lambda_callback(char *c, int rc) {
this->sample_count_ += rc;
this->output_file_.write(c, rc * 2);
}
lambda_class(std::string filename) {
this->filename_ = filename;
this->sample_count_ = 0;
this->output_file_.open(this->filename_, std::ios::binary);
write_header_wav(this->output_file_, 16000, 16, MONO, 10000);
}
~lambda_class() {
this->output_file_.close();
this->output_file_.open(this->filename_,
std::ios::binary | std::ios::in);
write_header_wav(this->output_file_, 16000, 16, MONO, this->sample_count_);
}
private:
std::string filename_;
int sample_count_;
std::ofstream output_file_;
lambda_class(const lambda_class &a) = delete;
};
class input_class {
public:
input_class() {
boost::interprocess::shared_memory_object::remove("MySharedMemory");
this->shm = new boost::interprocess::managed_shared_memory(boost::interprocess::create_only, "MySharedMemory",
1000000);
CharAllocator charallocator(this->shm->get_segment_manager());
StringAllocator stringallocator(this->shm->get_segment_manager());
this->myshmvector = shm->construct<MyShmStringVector>("myshmvector")(stringallocator);
};
~input_class() {
lambda_class *lc = new lambda_class("listener_vector.wav");
char *c = (char *) malloc(2048);
for (MyShmStringVector::iterator it = this->myshmvector->begin(); it != this->myshmvector->end(); it++) {
strcpy(c, it->c_str());
lc->lambda_callback(c, 2048);
}
delete lc;
boost::interprocess::shared_memory_object::remove("MySharedMemory");
this->shm->destroy_ptr(this->myshmvector);
}
void to_node(char *c, int rc) {
CharAllocator charallocator(this->shm->get_segment_manager());
StringAllocator stringallocator(this->shm->get_segment_manager());
MyShmString mystring(charallocator);
mystring = c;
this->myshmvector->insert(this->myshmvector->begin(), mystring);
}
private:
boost::interprocess::managed_shared_memory *shm;
MyShmStringVector *myshmvector;
};
void listener() {
lambda_class *ctc = new lambda_class("writer.wav");
char *c = (char *) malloc(2048);
boost::interprocess::managed_shared_memory segment(boost::interprocess::open_only, "MySharedMemory");
MyShmStringVector *myvector = segment.find<MyShmStringVector>("myshmvector").first;
for (MyShmStringVector::iterator it = myvector->begin(); it != myvector->end(); it++) {
strcpy(c, std::string(it->begin(), it->end()).c_str());
ctc->lambda_callback(c, 2048);
}
delete ctc;
return;
}
int main(int argc, char **argv) {
alsa_control *ac = new alsa_control(16000, 2048, 16, MONO);
if (argc == 1) {
input_class *ic = new input_class();
ac->listen_with_callback(std::bind(&input_class::to_node, ic, std::placeholders::_1, std::placeholders::_2),
"listener");
sleep(5);
ac->stop();
cout << "Go" << endl;
sleep(10);
delete ic;
delete ac;
} else {
auto th = std::async(std::launch::async, listener);
th.get();
}
return 0;
}
I'm just trying to use my sounds in multiple process, with a structure where i can use and share it (i will create and organiser for all my programs). The char* can have a fixed size, if i can use it, it's all good.
Edit :
My problem is that the sounds recorded :
- listener_vector.wav from the same process
- writer.wav from the other program
Are invalid, i think that the MyShmString the mutate them to invalid binary data.