audio-drain/drain/EndPointWrite.cpp

84 lines
3.1 KiB
C++
Raw Normal View History

/** @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-27 21:26:03 +01:00
#undef __class__
#define __class__ "EndPointWrite"
2015-02-05 19:10:53 +01:00
drain::EndPointWrite::EndPointWrite() :
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-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();
m_needProcess = true;
}
2015-02-24 22:20:11 +01:00
bool drain::EndPointWrite::process(std11::chrono::system_clock::time_point& _time,
void* _input,
size_t _inputNbChunk,
void*& _output,
size_t& _outputNbChunk){
2015-02-05 19:10:53 +01:00
drain::AutoLogInOut tmpLog("EndPointWrite");
//DRAIN_INFO(" nb Sample in buffer : " << m_tmpData.size());
if (m_function != nullptr) {
if (m_tmpData.size() <= 20000) {
m_function(_time, _inputNbChunk, m_output.getFormat(), m_output.getFrequency(), m_output.getMap());
}
}
// resize output buffer:
//DRAIN_INFO(" resize : " << (int32_t)m_formatSize << "*" << (int32_t)_inputNbChunk << "*" << (int32_t)m_outputMap.size());
m_outputData.resize(m_formatSize*_inputNbChunk*m_output.getMap().size(), 0);
// 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);
// check if data in the tmpBuffer
if (m_tmpData.size() == 0) {
DRAIN_WARNING("No data in the user buffer (write null data ... " << _outputNbChunk << " chunks)");
// just send no data ...
return true;
}
DRAIN_INFO("Write " << _outputNbChunk << " chunks");
// check if we have enought data:
int32_t nbChunkToCopy = std::min(_inputNbChunk, m_tmpData.size()/(m_output.getMap().size()*m_formatSize));
2015-01-27 21:26:03 +01:00
DRAIN_INFO(" " << nbChunkToCopy << " chunks ==> " << nbChunkToCopy*m_output.getMap().size()*m_formatSize << " Byte sizeBuffer=" << m_tmpData.size());
// 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);
//DRAIN_INFO(" nb Sample in buffer : " << m_tmpData.size());
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);
DRAIN_INFO("[ASYNC] Write data : " << _nbChunk << " chunks"
<< " ==> " << _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);
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++);
}
}