DetStreamPipe.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "DetStreamPipe.h"
  2. #include "SysUtils.h"
  3. #define ASYNC_CHECK_DELAY_MS 20
  4. #define DET_WAIT_MSECOND 100
  5. NAMESPACE_MAS_BEGIN
  6. DetStreamPipe::DetStreamPipe(TZ_INT rscID,
  7. TZ_INT detId, TZ_INT intype, const ExcepInfo &einfo)
  8. : IStreamPipe(rscID, detId, EMPT_DET, einfo),
  9. m_iType(intype),
  10. m_detLib(nullptr),
  11. m_detector(nullptr) {}
  12. DetStreamPipe::~DetStreamPipe()
  13. {
  14. this->Dispose();
  15. }
  16. TZ_INT DetStreamPipe::Initialize(SPtr<DetectorLib> & detLib, const std::string & initParam)
  17. {
  18. m_detLib = detLib;
  19. m_detLib->GetDetLibKey(m_detKey);
  20. m_detector = m_detLib->BuildDetector();
  21. if (!m_detector)
  22. {
  23. TZLogWarn("%s BuildDetector return nullptr!!!", m_detKey.c_str());
  24. return MEC_NULL_OBJ;
  25. }
  26. auto irst = m_detector->Initialize(initParam);
  27. if (irst) return irst;
  28. this->Start();
  29. return MEC_OK;
  30. }
  31. TZ_INT DetStreamPipe::Dispose()
  32. {
  33. this->StopAndWait();
  34. m_detLib->DestroyDetector(m_detector);
  35. m_detector = nullptr;
  36. return MEC_OK;
  37. }
  38. std::tuple<TZ_BOOL, TZ_BOOL> DetStreamPipe::streamArrived(SPtr<StreamInfo> streamInfo)
  39. {
  40. if (m_detSema.Count() > 3000)
  41. {
  42. TZLogInfo("sema is more than 3000!!!");
  43. return std::make_tuple(FALSE, FALSE);
  44. }
  45. m_lock.Lock();
  46. m_cacheInfo.push_back(streamInfo);
  47. m_lock.Unlock();
  48. m_detSema.Signal();
  49. return std::make_tuple(FALSE, FALSE);
  50. }
  51. TZ_INT DetStreamPipe::TurnOnGPU()
  52. {
  53. return m_detector->TurnOnGPU();
  54. }
  55. TZ_INT DetStreamPipe::TurnOffGPU()
  56. {
  57. return m_detector->TurnOffGPU();
  58. }
  59. TZ_INT DetStreamPipe::SetDetectCfg(const std::string param)
  60. {
  61. return m_detector->SetDetectCfg(param);
  62. }
  63. void DetStreamPipe::Entry()
  64. {
  65. TZ_INT irst;
  66. while (!this->IsStop())
  67. {
  68. if (!m_detSema.Wait(DET_WAIT_MSECOND)) continue;
  69. m_lock.Lock();
  70. if (!m_cacheInfo.size())
  71. {
  72. m_lock.Unlock();
  73. continue;
  74. }
  75. SPtr<StreamInfo> info = m_cacheInfo.front();
  76. m_cacheInfo.pop_front();
  77. m_lock.Unlock();
  78. irst = m_detector->DoDetect(info, this->detCallback, this);
  79. if (irst)
  80. {
  81. TZLogWarn("Pipe %llu DoDetect failed %d!!", this->GetHandle(), irst);
  82. }
  83. //TZLogInfo("after %s-------------", m_detKey.c_str());
  84. }
  85. }
  86. TZ_INT DetStreamPipe::detCallback(SPtr<StreamInfo> & streamInfo, void * ctx)
  87. {
  88. DetStreamPipe * self = (DetStreamPipe *)ctx;
  89. self->DeliverStream(streamInfo);
  90. return MEC_OK;
  91. }
  92. NAMESPACE_MAS_END