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.

ovasCPluginExternalStimulations.cpp 7.4KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "ovasCPluginExternalStimulations.h"
  2. #include <boost/interprocess/ipc/message_queue.hpp>
  3. #include <vector>
  4. #include <ctime>
  5. #include <system/ovCTime.h>
  6. #include "../ovasCSettingsHelper.h"
  7. #include "../ovasCSettingsHelperOperators.h"
  8. namespace OpenViBE {
  9. namespace AcquisitionServer {
  10. namespace Plugins {
  11. CPluginExternalStimulations::CPluginExternalStimulations(const Kernel::IKernelContext& ctx)
  12. : IAcquisitionServerPlugin(ctx, CString("AcquisitionServer_Plugin_ExternalStimulations")), m_ExternalStimulationsQueueName("openvibeExternalStimulations")
  13. {
  14. m_kernelCtx.getLogManager() << Kernel::LogLevel_Info << "Loading plugin: ExternalStimulations (deprecated)\n";
  15. m_settings.add("EnableExternalStimulations", &m_IsExternalStimulationsEnabled);
  16. m_settings.add("ExternalStimulationQueueName", &m_ExternalStimulationsQueueName);
  17. m_settings.load();
  18. }
  19. // Hooks
  20. bool CPluginExternalStimulations::startHook(const std::vector<CString>& /*selectedChannelNames*/, const size_t /*sampling*/, const size_t /*nChannel*/, const size_t /*nSamplePerSentBlock*/)
  21. {
  22. if (m_IsExternalStimulationsEnabled)
  23. {
  24. ftime(&m_CTStartTime);
  25. m_IsESThreadRunning = true;
  26. m_ESthreadPtr.reset(new std::thread(std::bind(&CPluginExternalStimulations::readExternalStimulations, this)));
  27. m_kernelCtx.getLogManager() << Kernel::LogLevel_Info << "External stimulations (deprecated) activated...\n";
  28. }
  29. m_ExternalStimulations.clear();
  30. m_DebugExternalStimulationsSent = 0;
  31. m_DebugCurrentReadIPCStimulations = 0;
  32. m_DebugStimulationsLost = 0;
  33. m_DebugStimulationsReceivedEarlier = 0;
  34. m_DebugStimulationsReceivedLate = 0;
  35. m_DebugStimulationsReceivedWrongSize = 0;
  36. m_DebugStimulationsBuffered = 0;
  37. return true;
  38. }
  39. void CPluginExternalStimulations::loopHook(std::deque<std::vector<float>>& /* vPendingBuffer */, CStimulationSet& stimulationSet, const uint64_t start, const uint64_t end, const uint64_t /* sampleTime */)
  40. {
  41. if (m_IsExternalStimulationsEnabled)
  42. {
  43. //m_kernelCtx.getLogManager() << Kernel::LogLevel_Error << "Checking for external stimulations:" << p << "\n";
  44. addExternalStimulations(&stimulationSet, m_kernelCtx.getLogManager(), start, end);
  45. }
  46. }
  47. void CPluginExternalStimulations::stopHook()
  48. {
  49. if (m_IsExternalStimulationsEnabled)
  50. {
  51. m_IsESThreadRunning = false;
  52. if (m_ESthreadPtr) { m_ESthreadPtr->join(); }
  53. else { m_kernelCtx.getLogManager() << Kernel::LogLevel_Warning << "Warning: External Stims plugin stopHook() tried to join a NULL thread\n"; }
  54. }
  55. //software tagging diagnosting
  56. m_kernelCtx.getLogManager() << Kernel::LogLevel_Debug << " Total external ones received through IPC: " << m_DebugCurrentReadIPCStimulations << "\n";
  57. m_kernelCtx.getLogManager() << Kernel::LogLevel_Debug << " Sent to Designer: " << m_DebugExternalStimulationsSent << "\n";
  58. m_kernelCtx.getLogManager() << Kernel::LogLevel_Debug << " Lost because of invalid timestamp: " << m_DebugStimulationsLost << "\n";
  59. m_kernelCtx.getLogManager() << Kernel::LogLevel_Debug << " Stimulations that came earlier: " << m_DebugStimulationsReceivedEarlier << "\n";
  60. m_kernelCtx.getLogManager() << Kernel::LogLevel_Debug << " Stimulations that came later: " << m_DebugStimulationsReceivedLate << "\n";
  61. m_kernelCtx.getLogManager() << Kernel::LogLevel_Debug << " Stimulations that had wrong size: " << m_DebugStimulationsReceivedWrongSize << "\n";
  62. m_kernelCtx.getLogManager() << Kernel::LogLevel_Debug << " Buffered: " << m_DebugStimulationsBuffered << "\n";
  63. //end software tagging diagnosting
  64. }
  65. // Plugin specific methods
  66. void CPluginExternalStimulations::readExternalStimulations()
  67. {
  68. using namespace boost::interprocess;
  69. //std::cout << "Creating External Stimulations thread" << std::endl;
  70. //std::cout << "Queue Name : " << m_ExternalStimulationsQueueName << std::endl;
  71. //char mq_name[255];
  72. //std::strcpy(mq_name, m_ExternalStimulationsQueueName.toASCIIString());
  73. const int chunkLength = 3;
  74. const int pauseTime = 5;
  75. uint32_t priority;
  76. size_t recvdSize;
  77. uint64_t chunk[chunkLength];
  78. while (m_IsESThreadRunning)
  79. {
  80. bool success;
  81. try
  82. {
  83. //Open a message queue.
  84. message_queue mq(open_only //only open
  85. , m_ExternalStimulationsQueueName.toASCIIString() //name
  86. //,mq_name //name
  87. );
  88. success = mq.try_receive(&chunk, sizeof(chunk), recvdSize, priority);
  89. }
  90. catch (interprocess_exception& /* ex */)
  91. {
  92. //m_IsESThreadRunning = false;
  93. //m_kernelCtx.getLogManager() << Kernel::LogLevel_Error << "Problem with message queue in external stimulations:" << ex.what() << "\n";
  94. System::Time::sleep(pauseTime);
  95. continue;
  96. }
  97. if (!success)
  98. {
  99. System::Time::sleep(pauseTime);
  100. continue;
  101. }
  102. m_DebugCurrentReadIPCStimulations++;
  103. if (recvdSize != sizeof(chunk))
  104. {
  105. //m_kernelCtx.getLogManager() << Kernel::LogLevel_Error << "Problem with type of received data when reqding external stimulation!\n";
  106. m_DebugStimulationsReceivedWrongSize++;
  107. }
  108. else
  109. {
  110. //m_kernelCtx.getLogManager() << Kernel::LogLevel_Warning << "received\n";
  111. SExternalStimulation stim;
  112. stim.identifier = chunk[1];
  113. const uint64_t receivedTime = chunk[2];
  114. //1. calculate time
  115. const uint64_t ctStartTimeMs = (m_CTStartTime.time * 1000 + m_CTStartTime.millitm);
  116. const int64_t timeTest = receivedTime - ctStartTimeMs;
  117. if (timeTest < 0)
  118. {
  119. m_DebugStimulationsLost++;
  120. //m_kernelCtx.getLogManager() << Kernel::LogLevel_Warning << "AS: external stimulation time is invalid, probably stimulation is before reference point, total invalid so far: " << m_FlashesLost << "\n";
  121. System::Time::sleep(pauseTime);
  122. continue; //we skip this stimulation
  123. }
  124. //2. Convert to OpenVibe time
  125. const uint64_t ctEventTime = receivedTime - ctStartTimeMs;
  126. const double time = double(ctEventTime) / double(1000);
  127. const uint64_t ovTime = CTime(time).time();
  128. stim.timestamp = ovTime;
  129. //3. Store, the main thread will process it
  130. {
  131. //lock
  132. std::lock_guard<std::mutex> lock(m_es_mutex);
  133. m_ExternalStimulations.push_back(stim);
  134. m_DebugStimulationsBuffered++;
  135. m_esAvailable.notify_one();
  136. //unlock
  137. }
  138. System::Time::sleep(pauseTime);
  139. }
  140. }
  141. }
  142. void CPluginExternalStimulations::addExternalStimulations(CStimulationSet* ss, Kernel::ILogManager& /*logm*/, const uint64_t start, const uint64_t /*end*/)
  143. {
  144. const uint64_t durationMs = 40;
  145. {
  146. //lock
  147. std::lock_guard<std::mutex> lock(m_es_mutex);
  148. for (auto i = m_ExternalStimulations.begin(); i != m_ExternalStimulations.end(); ++i)
  149. {
  150. // if time is current or any time in the future - send it (AS will buffer it)
  151. if (i->timestamp >= start)
  152. {
  153. //flashes_in_this_time_chunk++;
  154. //logm << Kernel::LogLevel_Error << "Stimulation added." << "\n";
  155. ss->appendStimulation(i->identifier, i->timestamp, durationMs);
  156. }
  157. else
  158. {
  159. //the stimulation is coming too late - after the current block being processed
  160. //we correct the timestamp to the current block and we send it
  161. m_DebugStimulationsReceivedLate++;
  162. ss->appendStimulation(i->identifier, start, durationMs);
  163. }
  164. m_DebugExternalStimulationsSent++;
  165. }
  166. // Since we processed all stimulations, we can clear the queue
  167. m_ExternalStimulations.clear();
  168. m_esAvailable.notify_one();
  169. //unlock
  170. }
  171. }
  172. bool CPluginExternalStimulations::setExternalStimulationsEnabled(const bool active)
  173. {
  174. m_IsExternalStimulationsEnabled = active;
  175. return true;
  176. }
  177. } // namespace Plugins
  178. } // namespace AcquisitionServer
  179. } // namespace OpenViBE