/** @file * @author Edouard DUPIN * @copyright 2015, Edouard DUPIN, all right reserved * @license APACHE v2.0 (see license file) */ #include "debug.h" #include "Interface.h" #include "io/Node.h" #include #include #include #include #include #include #undef __class__ #define __class__ "Interface" airtio::Interface::Interface(void) : m_node(nullptr), m_freq(8000), m_map(), m_format(airtalgo::format_int16), m_name(""), m_volume(0.0f) { } bool airtio::Interface::init(const std::string& _name, float _freq, const std::vector& _map, airtalgo::format _format, const std::shared_ptr& _node) { m_name = _name; m_node = _node; m_freq = _freq; m_map = _map; m_format = _format; m_process = std::make_shared(); m_volume = 0.0f; // Create convertion interface if (m_node->isInput() == true) { // TODO : Set an auto update of IO if (m_map != m_node->getMap()) { std::shared_ptr algo = airtalgo::ChannelReorder::create(); algo->setInputFormat(airtalgo::IOFormatInterface(m_node->getMap(), m_node->getFormat(), m_node->getFrequency())); algo->setOutputFormat(airtalgo::IOFormatInterface(m_map, m_node->getFormat(), m_node->getFrequency())); m_process->pushBack(algo); AIRTIO_INFO("convert " << m_node->getMap() << " -> " << m_map); } if (m_freq != m_node->getFrequency()) { std::shared_ptr algo = airtalgo::Resampler::create(); algo->setInputFormat(airtalgo::IOFormatInterface(m_map, m_node->getFormat(), m_node->getFrequency())); algo->setOutputFormat(airtalgo::IOFormatInterface(m_map, m_node->getFormat(), m_freq)); m_process->pushBack(algo); AIRTIO_INFO("convert " << m_node->getFrequency() << " -> " << m_freq); } if (m_format != m_node->getFormat()) { std::shared_ptr algo = airtalgo::FormatUpdate::create(); algo->setInputFormat(airtalgo::IOFormatInterface(m_map, m_node->getFormat(), m_freq)); algo->setOutputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); m_process->pushBack(algo); AIRTIO_INFO("convert " << m_node->getFormat() << " -> " << m_format); } // by default we add a read node if (true) { std::shared_ptr algo = airtalgo::EndPointRead::create(); algo->setInputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); algo->setOutputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); m_process->pushBack(algo); AIRTIO_INFO("add default read node ..."); } } else { // by default we add a write node: if (true) { std::shared_ptr algo = airtalgo::EndPointWrite::create(); algo->setInputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); algo->setOutputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); m_process->pushBack(algo); AIRTIO_INFO("add default write node ..."); } // TODO : Set an auto update of IO if (m_format != m_node->getFormat()) { std::shared_ptr algo = airtalgo::FormatUpdate::create(); algo->setInputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); algo->setOutputFormat(airtalgo::IOFormatInterface(m_map, m_node->getFormat(), m_freq)); m_process->pushBack(algo); AIRTIO_INFO("convert " << m_format << " -> " << m_node->getFormat()); } if (m_freq != m_node->getFrequency()) { std::shared_ptr algo = airtalgo::Resampler::create(); algo->setInputFormat(airtalgo::IOFormatInterface(m_map, m_node->getFormat(), m_freq)); algo->setOutputFormat(airtalgo::IOFormatInterface(m_map, m_node->getFormat(), m_node->getFrequency())); m_process->pushBack(algo); AIRTIO_INFO("convert " << m_freq << " -> " << m_node->getFrequency()); } if (m_map != m_node->getMap()) { std::shared_ptr algo = airtalgo::ChannelReorder::create(); algo->setInputFormat(airtalgo::IOFormatInterface(m_map, m_node->getFormat(), m_node->getFrequency())); algo->setOutputFormat(airtalgo::IOFormatInterface(m_node->getMap(), m_node->getFormat(), m_node->getFrequency())); m_process->pushBack(algo); AIRTIO_INFO("convert " << m_map << " -> " << m_node->getMap()); } } //m_node->interfaceAdd(shared_from_this()); return true; } std::shared_ptr airtio::Interface::create(const std::string& _name, float _freq, const std::vector& _map, airtalgo::format _format, const std::shared_ptr& _node) { std::shared_ptr out = std::shared_ptr(new airtio::Interface()); out->init(_name, _freq, _map, _format, _node); return out; } airtio::Interface::~Interface() { //stop(true, true); std::unique_lock lock(m_mutex); //m_node->interfaceRemove(shared_from_this()); m_process.reset(); } void airtio::Interface::setOutputCallback(size_t _chunkSize, airtalgo::needDataFunction _function) { std::unique_lock lock(m_mutex); m_process->removeIfFirst(); std::shared_ptr algo = airtalgo::EndPointCallback::create(_function); AIRTIO_INFO("set property: " << m_map << " " << m_format << " " << m_freq); algo->setInputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); algo->setOutputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); m_process->pushFront(algo); } void airtio::Interface::setInputCallback(size_t _chunkSize, airtalgo::haveNewDataFunction _function) { std::unique_lock lock(m_mutex); m_process->removeIfLast(); std::shared_ptr algo = airtalgo::EndPointCallback::create(_function); algo->setInputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); algo->setOutputFormat(airtalgo::IOFormatInterface(m_map, m_format, m_freq)); m_process->pushBack(algo); } void airtio::Interface::setWriteCallback(airtalgo::needDataFunctionWrite _function) { std::unique_lock lock(m_mutex); std::shared_ptr algo = m_process->get(0); if (algo == nullptr) { return; } algo->setCallback(_function); } void airtio::Interface::start(const std::chrono::system_clock::time_point& _time) { std::unique_lock lock(m_mutex); AIRTIO_DEBUG("start [BEGIN]"); m_node->interfaceAdd(shared_from_this()); AIRTIO_DEBUG("start [ END ]"); } void airtio::Interface::stop(bool _fast, bool _abort) { std::unique_lock lock(m_mutex); AIRTIO_DEBUG("stop [BEGIN]"); m_node->interfaceRemove(shared_from_this()); AIRTIO_DEBUG("stop [ END]"); } void airtio::Interface::abort() { std::unique_lock lock(m_mutex); AIRTIO_DEBUG("abort [BEGIN]"); // TODO :... AIRTIO_DEBUG("abort [ END ]"); } bool airtio::Interface::setParameter(const std::string& _filter, const std::string& _parameter, const std::string& _value) { AIRTIO_DEBUG("setParameter [BEGIN] : '" << _filter << "':'" << _parameter << "':'" << _value << "'"); bool out = false; if ( _filter == "volume" && _parameter != "FLOW") { AIRTIO_ERROR("Interface is not allowed to modify '" << _parameter << "' Volume just allowed to modify 'FLOW' volume"); } AIRTIO_TODO(" IMPLEMENT"); AIRTIO_DEBUG("setParameter [ END ] : '" << out << "'"); return out; } std::string airtio::Interface::getParameter(const std::string& _filter, const std::string& _parameter) const { AIRTIO_DEBUG("getParameter [BEGIN] : '" << _filter << "':'" << _parameter << "'"); std::string out; AIRTIO_TODO(" IMPLEMENT"); AIRTIO_DEBUG("getParameter [ END ] : '" << out << "'"); return out; } std::string airtio::Interface::getParameterProperty(const std::string& _filter, const std::string& _parameter) const { AIRTIO_DEBUG("getParameterProperty [BEGIN] : '" << _filter << "':'" << _parameter << "'"); std::string out; AIRTIO_TODO(" IMPLEMENT"); AIRTIO_DEBUG("getParameterProperty [ END ] : '" << out << "'"); return out; } void airtio::Interface::write(const void* _value, size_t _nbChunk) { std::unique_lock lock(m_mutex); std::shared_ptr algo = m_process->get(0); if (algo == nullptr) { return; } algo->write(_value, _nbChunk); } #if 0 // TODO : add API aCCess mutex for Read and write... std::vector airtio::Interface::read(size_t _nbChunk) { // TODO :... std::vector data; /* data.resize(_nbChunk*m_map.size(), 0); m_mutex.lock(); int32_t nbChunkBuffer = m_circularBuffer.size() / m_map.size(); m_mutex.unlock(); while (nbChunkBuffer < _nbChunk) { usleep(1000); nbChunkBuffer = m_circularBuffer.size() / m_map.size(); } m_mutex.lock(); for (size_t iii = 0; iii lock(m_mutex); // TODO :... } size_t airtio::Interface::size() const { std::unique_lock lock(m_mutex); // TODO :... return 0; } void airtio::Interface::setBufferSize(size_t _nbChunk) { std::unique_lock lock(m_mutex); // TODO :... } void airtio::Interface::setBufferSize(const std::chrono::duration& _time) { std::unique_lock lock(m_mutex); // TODO :... } void airtio::Interface::clearInternalBuffer() { std::unique_lock lock(m_mutex); // TODO :... } std::chrono::system_clock::time_point airtio::Interface::getCurrentTime() const { std::unique_lock lock(m_mutex); // TODO :... return std::chrono::system_clock::time_point(); return std::chrono::system_clock::now(); } void airtio::Interface::systemNewInputData(std::chrono::system_clock::time_point _time, void* _data, size_t _nbChunk) { std::unique_lock lockProcess(m_mutex); m_process->push(_time, _data, _nbChunk); } void airtio::Interface::systemNeedOutputData(std::chrono::system_clock::time_point _time, void* _data, size_t _nbChunk, size_t _chunkSize) { std::unique_lock lockProcess(m_mutex); m_process->pull(_time, _data, _nbChunk, _chunkSize); } bool airtio::Interface::systemSetVolume(const std::string& _parameter, const std::string& _value);