You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ovpCBoxAlgorithmUDPStimcodeSender.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \file CBoxAlgorithmUDPStimcodeSender.cpp
  4. /// \brief Functions of Class UDP-StimCode-Sender
  5. /// \author Tobias Baumann (TH Nuernberg).
  6. /// \version 1.2.
  7. /// \date Mon Oct 04 12:43:53 2021.
  8. /// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
  9. ///
  10. ///-------------------------------------------------------------------------------------------------
  11. //includes
  12. #include "ovpCBoxAlgorithmUDPStimcodeSender.h"
  13. using namespace OpenViBE;
  14. using namespace /*OpenViBE::*/Kernel;
  15. using namespace /*OpenViBE::*/Plugins;
  16. using namespace /*OpenViBE::Plugins::*/Tactilebci;
  17. bool CBoxAlgorithmUDPStimcodeSender::initialize()
  18. {
  19. m_StimDecoder.initialize(*this, 0);
  20. m_StimEncoder.initialize(*this, 0);
  21. // retrieve box settings
  22. m_IP = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 0);
  23. m_Port = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1);
  24. m_RowBase = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 2);
  25. m_nTactilos = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 3);
  26. //set m_nTactilos to 2 if lower than 2
  27. if(m_nTactilos < 2)
  28. {
  29. m_nTactilos = 2;
  30. }
  31. //set m_nTactilos to MAX if greater than MAX_TACTILOS
  32. if(m_nTactilos > MAX_TACTILOS)
  33. {
  34. m_nTactilos = MAX_TACTILOS;
  35. }
  36. // connect UDP socket
  37. boost::asio::ip::udp::endpoint m_Endpoint(boost::asio::ip::address::from_string(m_IP), m_Port);
  38. m_Socket.connect(m_Endpoint);
  39. return true;
  40. }
  41. /*******************************************************************************/
  42. bool CBoxAlgorithmUDPStimcodeSender::uninitialize()
  43. {
  44. m_StimDecoder.uninitialize();
  45. m_StimEncoder.uninitialize();
  46. return true;
  47. }
  48. /*******************************************************************************/
  49. bool CBoxAlgorithmUDPStimcodeSender::processInput(const size_t index)
  50. {
  51. // some pre-processing code if needed...
  52. // ready to process !
  53. getBoxAlgorithmContext()->markAlgorithmAsReadyToProcess();
  54. return true;
  55. }
  56. /*******************************************************************************/
  57. bool CBoxAlgorithmUDPStimcodeSender::process()
  58. {
  59. // the static box context describes the box inputs, outputs, settings structures
  60. const IBox& staticBoxContext = this->getStaticBoxContext();
  61. // the dynamic box context describes the current state of the box inputs and outputs (i.e. the chunks)
  62. IBoxIO& boxContext = this->getDynamicBoxContext();
  63. uint64_t StimulationID = 0;
  64. uint64_t ChunkStartTime = 0;
  65. uint64_t ChunkEndTime = 0;
  66. uint64_t Size = 0;
  67. const uint8_t* Buffer = nullptr;
  68. //iterate over all chunk on input 0
  69. for (uint64_t i = 0; i < boxContext.getInputChunkCount(0); ++i)
  70. {
  71. // decode the chunk i
  72. m_StimDecoder.decode(i);
  73. if(m_StimDecoder.isBufferReceived())
  74. {
  75. //check received stimulations
  76. IStimulationSet* StimSet = m_StimDecoder.getOutputStimulationSet();
  77. for(uint64_t j=0; j<StimSet->getStimulationCount(); j++)
  78. {
  79. StimulationID = StimSet->getStimulationIdentifier(j);
  80. if(StimulationID >= m_RowBase && StimulationID < (m_RowBase + m_nTactilos))
  81. {
  82. this->getLogManager() << LogLevel_Debug << "send udp : [StimulusCode " << (StimulationID-m_RowBase+1) << "]\n";
  83. m_Socket.send(boost::asio::buffer(std::to_string(StimulationID-m_RowBase+1)));
  84. }
  85. }
  86. }
  87. // forward input chunks
  88. boxContext.getInputChunk(0, i, ChunkStartTime, ChunkEndTime, Size, Buffer);
  89. boxContext.appendOutputChunkData(0, Buffer, Size);
  90. boxContext.markOutputAsReadyToSend(0, ChunkStartTime, ChunkEndTime);
  91. boxContext.markInputAsDeprecated(0, i);
  92. }
  93. return true;
  94. }