2015-01-25 22:10:49 +01:00
|
|
|
/** @file
|
|
|
|
* @author Edouard DUPIN
|
|
|
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
|
|
|
* @license APACHE v2.0 (see license file)
|
|
|
|
*/
|
|
|
|
#include "debug.h"
|
2015-02-05 19:10:53 +01:00
|
|
|
#include <drain/EndPointWrite.h>
|
2015-01-25 22:10:49 +01:00
|
|
|
|
2015-01-27 21:26:03 +01:00
|
|
|
|
|
|
|
#undef __class__
|
|
|
|
#define __class__ "EndPointWrite"
|
|
|
|
|
2015-02-05 19:10:53 +01:00
|
|
|
drain::EndPointWrite::EndPointWrite() :
|
2015-01-25 22:10:49 +01:00
|
|
|
m_function(nullptr) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-05 19:10:53 +01:00
|
|
|
void drain::EndPointWrite::init() {
|
|
|
|
drain::EndPoint::init();
|
2015-02-01 22:22:42 +01:00
|
|
|
m_type = "EndPoint";
|
2015-01-29 21:46:01 +01:00
|
|
|
}
|
2015-01-25 22:10:49 +01:00
|
|
|
|
2015-02-24 22:20:11 +01:00
|
|
|
std11::shared_ptr<drain::EndPointWrite> drain::EndPointWrite::create() {
|
|
|
|
std11::shared_ptr<drain::EndPointWrite> tmp(new drain::EndPointWrite());
|
2015-01-29 21:46:01 +01:00
|
|
|
tmp->init();
|
|
|
|
return tmp;
|
2015-01-28 22:07:11 +01:00
|
|
|
}
|
|
|
|
|
2015-02-05 19:10:53 +01:00
|
|
|
void drain::EndPointWrite::configurationChange() {
|
|
|
|
drain::EndPoint::configurationChange();
|
2015-01-25 22:10:49 +01:00
|
|
|
m_needProcess = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-24 22:20:11 +01:00
|
|
|
bool drain::EndPointWrite::process(std11::chrono::system_clock::time_point& _time,
|
2015-02-17 21:08:15 +01:00
|
|
|
void* _input,
|
|
|
|
size_t _inputNbChunk,
|
|
|
|
void*& _output,
|
|
|
|
size_t& _outputNbChunk){
|
2015-02-05 19:10:53 +01:00
|
|
|
drain::AutoLogInOut tmpLog("EndPointWrite");
|
2015-02-05 21:33:12 +01:00
|
|
|
//DRAIN_INFO(" nb Sample in buffer : " << m_tmpData.size());
|
2015-01-25 22:10:49 +01:00
|
|
|
if (m_function != nullptr) {
|
|
|
|
if (m_tmpData.size() <= 20000) {
|
2015-02-17 21:08:15 +01:00
|
|
|
m_function(_time, _inputNbChunk, m_output.getFormat(), m_output.getFrequency(), m_output.getMap());
|
2015-01-25 22:10:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// resize output buffer:
|
2015-02-05 21:33:12 +01:00
|
|
|
//DRAIN_INFO(" resize : " << (int32_t)m_formatSize << "*" << (int32_t)_inputNbChunk << "*" << (int32_t)m_outputMap.size());
|
2015-01-27 22:45:30 +01:00
|
|
|
m_outputData.resize(m_formatSize*_inputNbChunk*m_output.getMap().size(), 0);
|
2015-01-25 22:10:49 +01:00
|
|
|
// set output pointer:
|
|
|
|
_outputNbChunk = m_outputData.size()/(m_formatSize*m_output.getMap().size());
|
|
|
|
_output = &m_outputData[0];
|
2015-02-24 22:20:11 +01:00
|
|
|
std11::unique_lock<std11::mutex> lock(m_mutex);
|
2015-01-25 22:10:49 +01:00
|
|
|
// check if data in the tmpBuffer
|
|
|
|
if (m_tmpData.size() == 0) {
|
2015-02-05 21:33:12 +01:00
|
|
|
DRAIN_WARNING("No data in the user buffer (write null data ... " << _outputNbChunk << " chunks)");
|
2015-01-25 22:10:49 +01:00
|
|
|
// just send no data ...
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-05 21:33:12 +01:00
|
|
|
DRAIN_INFO("Write " << _outputNbChunk << " chunks");
|
2015-01-25 22:10:49 +01:00
|
|
|
// check if we have enought data:
|
2015-01-27 22:45:30 +01:00
|
|
|
int32_t nbChunkToCopy = std::min(_inputNbChunk, m_tmpData.size()/(m_output.getMap().size()*m_formatSize));
|
2015-01-27 21:26:03 +01:00
|
|
|
|
2015-02-05 21:33:12 +01:00
|
|
|
DRAIN_INFO(" " << nbChunkToCopy << " chunks ==> " << nbChunkToCopy*m_output.getMap().size()*m_formatSize << " Byte sizeBuffer=" << m_tmpData.size());
|
2015-01-25 22:10:49 +01:00
|
|
|
// copy data to the output:
|
|
|
|
memcpy(_output, &m_tmpData[0], nbChunkToCopy*m_output.getMap().size()*m_formatSize);
|
|
|
|
// remove old data:
|
2015-01-27 21:26:03 +01:00
|
|
|
m_tmpData.erase(m_tmpData.begin(), m_tmpData.begin() + nbChunkToCopy*m_output.getMap().size()*m_formatSize);
|
2015-02-05 21:33:12 +01:00
|
|
|
//DRAIN_INFO(" nb Sample in buffer : " << m_tmpData.size());
|
2015-01-25 22:10:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-05 19:10:53 +01:00
|
|
|
void drain::EndPointWrite::write(const void* _value, size_t _nbChunk) {
|
2015-02-24 22:20:11 +01:00
|
|
|
std11::unique_lock<std11::mutex> lock(m_mutex);
|
2015-02-05 21:33:12 +01:00
|
|
|
DRAIN_INFO("[ASYNC] Write data : " << _nbChunk << " chunks"
|
2015-01-27 22:45:30 +01:00
|
|
|
<< " ==> " << _nbChunk*m_output.getMap().size() << " samples"
|
2015-02-02 21:48:57 +01:00
|
|
|
<< " formatSize=" << int32_t(m_formatSize)
|
|
|
|
<< " bufferSize=" << m_tmpData.size());
|
2015-01-27 21:26:03 +01:00
|
|
|
const int8_t* value = static_cast<const int8_t*>(_value);
|
2015-01-27 22:45:30 +01:00
|
|
|
for (size_t iii=0; iii<_nbChunk*m_formatSize*m_output.getMap().size(); ++iii) {
|
2015-01-27 21:26:03 +01:00
|
|
|
m_tmpData.push_back(*value++);
|
2015-01-25 22:10:49 +01:00
|
|
|
}
|
|
|
|
}
|