StreamDef.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #include "MemPool.h"
  2. #include "MesgDispatcher.h"
  3. #include "StreamDef.h"
  4. #include "SysUtils.h"
  5. NAMESPACE_MAS_BEGIN
  6. #define PIPE_CHECK_DELAY_SECONDS (2)
  7. ExcepInfo::ExcepInfo(
  8. const std::string & name, TZ_INT threshold) :
  9. Name(name), Threshold(threshold), OutputState(EMPS_NO_DATA),
  10. Lastest((TIME_POINT)ZERO_POINT)
  11. {
  12. }
  13. ExcepInfo::ExcepInfo(const ExcepInfo & einfo) :
  14. Name(einfo.Name), Threshold(einfo.Threshold),
  15. OutputState(einfo.OutputState.load()),
  16. Lastest(einfo.Lastest.load())
  17. {
  18. }
  19. ExcepInfo::~ExcepInfo()
  20. {
  21. }
  22. Media::Media(TZ_INT Length) : Length(Length),
  23. DataType(-1), Height(-1), Width(-1), Mem(nullptr)
  24. {
  25. Mem = MEMPOOL->AllocAvailMem(Length);
  26. }
  27. Media::Media(const Media & origin) :
  28. Length(origin.Length), DataType(origin.DataType),
  29. Height(origin.Height), Width(origin.Width), Mem(origin.Mem)
  30. {
  31. }
  32. Media::~Media()
  33. {
  34. if (Mem)
  35. {
  36. MEMPOOL->ReturnFreeMem(this->Mem, this->Length);
  37. Mem = nullptr;
  38. }
  39. }
  40. StreamInfo::StreamInfo() :
  41. m_hd((TZ_HANDLE)this), m_seq(0), m_deliverType(EMPT_MEDIA),
  42. m_mediaRsc(nullptr), m_straProducing(nullptr)
  43. {
  44. }
  45. StreamInfo::StreamInfo(TZ_HANDLE hd) :
  46. m_hd(hd), m_seq(0), m_deliverType(EMPT_MEDIA),
  47. m_mediaRsc(nullptr), m_straProducing(nullptr)
  48. {
  49. }
  50. StreamInfo::StreamInfo(const StreamInfo & origin) :
  51. m_hd(origin.m_hd), m_seq(origin.m_seq),
  52. m_deliverType(origin.m_deliverType.load()),
  53. m_mediaRsc(origin.m_mediaRsc),
  54. m_straProducing(nullptr)
  55. {
  56. }
  57. StreamInfo::StreamInfo(SPtr<StreamInfo> & origin) :
  58. m_hd(origin->m_hd), m_seq(origin->m_seq),
  59. m_deliverType(origin->m_deliverType.load()),
  60. m_mediaRsc(origin->m_mediaRsc),
  61. m_straProducing(nullptr)
  62. {
  63. }
  64. StreamInfo::~StreamInfo()
  65. {
  66. }
  67. TZ_INT StreamInfo::AddDetRst(const std::string & detKey,
  68. const std::string & detRst, SPtr<Media> & Media, const DrawInfo & draw)
  69. {
  70. auto prst =
  71. std::make_shared<DetProducing>(detKey, detRst, Media, draw);
  72. std::unique_lock<std::mutex> lock(m_detLock);
  73. m_detProducings.emplace(detKey, prst);
  74. return MEC_OK;
  75. }
  76. TZ_INT StreamInfo::SetStraRst(const std::string & straKey,
  77. const StraRst & straRst, SPtr<Media> & Media)
  78. {
  79. if (m_straProducing)
  80. {
  81. TZLogWarn("stra priducing isn't nullptr!!!");
  82. return MEC_FAILED;
  83. }
  84. m_straProducing =
  85. std::make_shared<StraProducing>(straKey, straRst, Media);
  86. return MEC_OK;
  87. }
  88. TZ_INT StreamInfo::GetDeliverType()
  89. {
  90. return m_deliverType.load();
  91. }
  92. void StreamInfo::SetDeliverType(TZ_INT type)
  93. {
  94. m_deliverType.store(type);
  95. }
  96. TZ_INT StreamInfo::SetMediaRsc(SPtr<Media> & mediaRsc)
  97. {
  98. if (m_mediaRsc)
  99. {
  100. TZLogWarn("media rsc isn't nullptr!!!");
  101. return MEC_FAILED;
  102. }
  103. m_mediaRsc = mediaRsc;
  104. return MEC_OK;
  105. }
  106. TZ_INT StreamInfo::SetSeq(TZ_Uint32 seq)
  107. {
  108. m_seq = seq;
  109. return MEC_OK;
  110. }
  111. TZ_HANDLE StreamInfo::GetHandle()
  112. {
  113. return m_hd;
  114. }
  115. TZ_Uint32 StreamInfo::GetSeq()
  116. {
  117. return m_seq;
  118. }
  119. SPtr<Media> & StreamInfo::GetMediaRsc()
  120. {
  121. return m_mediaRsc;
  122. }
  123. SPtr<StraProducing> & StreamInfo::GetStraProducing()
  124. {
  125. return m_straProducing;
  126. }
  127. std::unordered_map<std::string, SPtr<DetProducing>> & StreamInfo::GetAllDetRst()
  128. {
  129. return m_detProducings;
  130. }
  131. IStreamPipe::IStreamPipe(
  132. TZ_INT srcId, int64_t selfId, TZ_INT type,
  133. const ExcepInfo & einfo) :
  134. m_inited(FALSE), m_type(type),
  135. m_srcId(srcId), m_selfId(selfId),
  136. m_switch(FALSE), m_einfo(einfo)
  137. {
  138. m_handle = (TZ_HANDLE)this;
  139. }
  140. IStreamPipe::~IStreamPipe()
  141. {
  142. this->Dispose();
  143. }
  144. TZ_INT IStreamPipe::Initialize()
  145. {
  146. if (m_inited)
  147. {
  148. TZLogInfo("pipe %llu type %d has inited...", m_handle, m_type);
  149. return MEC_OK;
  150. }
  151. PIPESTATECHECKER->RegisterPipe(shared_from_this());
  152. auto irst = this->pipeInitialize();
  153. if (irst) return irst;
  154. m_switch.store(TRUE);
  155. m_inited = TRUE;
  156. TZLogInfo("pipe %llu type %d init succeed~~~", m_handle, m_type);
  157. return MEC_OK;
  158. }
  159. TZ_INT IStreamPipe::Dispose()
  160. {
  161. if (!m_inited)
  162. {
  163. TZLogInfo("pipe %llu type %d doesn't init...", m_handle, m_type);
  164. return MEC_OK;
  165. }
  166. m_switch.store(FALSE);
  167. PIPESTATECHECKER->RemovePipe(m_handle);
  168. this->pipeDispose();
  169. m_inited = FALSE;
  170. TZLogInfo("pipe %llu type %d dispose succeed~~~", m_handle, m_type);
  171. return MEC_OK;
  172. }
  173. TZ_INT IStreamPipe::AddStreamPipe(const SPtr<IStreamPipe> & pipe)
  174. {
  175. m_nextLock.lock();
  176. m_next[pipe->m_handle] = pipe;
  177. m_nextLock.unlock();
  178. TZLogInfo("add pipe %llu in %llu~~~",
  179. pipe->m_handle, this->m_handle);
  180. return MEC_OK;
  181. }
  182. TZ_INT IStreamPipe::RemoveStreamPipe(TZ_HANDLE hd)
  183. {
  184. std::unique_lock<std::mutex> lock(m_nextLock);
  185. auto iter = m_next.find(hd);
  186. if (m_next.end() == iter)
  187. {
  188. TZLogWarn("pipe %llu does not in pipe %llu!!!", hd, m_handle);
  189. return m_next.size();
  190. }
  191. m_next.erase(iter);
  192. return m_next.size();
  193. }
  194. TZ_INT IStreamPipe::RemoveStreamPipeBySelfId(int64_t selfId)
  195. {
  196. std::unique_lock<std::mutex> lock(m_nextLock);
  197. auto iter = std::find_if(m_next.begin(), m_next.end(),
  198. [selfId](std::pair<TZ_HANDLE, SPtr<IStreamPipe>> mem)
  199. {
  200. return mem.second->GetSelfId() == selfId;
  201. });
  202. if (m_next.end() == iter)
  203. {
  204. TZLogWarn("pipe %llu can't find selfId %lld pipe!!!", m_handle, selfId);
  205. return MEC_OK;
  206. }
  207. m_next.erase(iter);
  208. TZLogInfo("pipe %llu remove selfId %lld pipe~~~", m_handle, selfId);
  209. return MEC_OK;
  210. }
  211. void IStreamPipe::Switch(TZ_BOOL flag)
  212. {
  213. m_switch.store(flag);
  214. }
  215. void IStreamPipe::DeliverStream(
  216. SPtr<StreamInfo> & streamInfo, TZ_BOOL cpFlag)
  217. {
  218. m_nextLock.lock();
  219. std::map<TZ_HANDLE, SPtr<IStreamPipe>> next(m_next);
  220. m_nextLock.unlock();
  221. streamInfo->SetDeliverType(m_type);
  222. for (auto & n : next)
  223. {
  224. if (cpFlag)
  225. {
  226. auto stream = std::make_shared<StreamInfo>(streamInfo);
  227. n.second->streamArrivedBySwitch(stream);
  228. continue;
  229. }
  230. n.second->streamArrivedBySwitch(streamInfo);
  231. }
  232. this->updateExcepTime();
  233. }
  234. void IStreamPipe::updateExcepTime()
  235. {
  236. m_einfo.Lastest.store(TIME_NOW);
  237. }
  238. TZ_BOOL IStreamPipe::streamFilter(SPtr<StreamInfo> & streamInfo)
  239. {
  240. return TRUE;
  241. }
  242. TZ_INT IStreamPipe::pipeInitialize()
  243. {
  244. return MEC_OK;
  245. }
  246. TZ_INT IStreamPipe::pipeDispose()
  247. {
  248. return MEC_OK;
  249. }
  250. void IStreamPipe::streamArrivedBySwitch(
  251. SPtr<StreamInfo> & streamInfo)
  252. {
  253. if (!m_switch.load()) return;
  254. if (!this->streamFilter(streamInfo)) return;
  255. TZ_BOOL autoDeliver = FALSE, cpFlag = FALSE;
  256. std::tie(autoDeliver, cpFlag) = this->streamArrived(streamInfo);
  257. if (!autoDeliver) return;
  258. this->DeliverStream(streamInfo, cpFlag);
  259. }
  260. TZ_INT IStreamPipe::GetType()
  261. {
  262. return m_type;
  263. }
  264. int64_t IStreamPipe::GetSelfId()
  265. {
  266. return m_selfId;
  267. }
  268. TZ_INT IStreamPipe::GetSrcId()
  269. {
  270. return m_srcId;
  271. }
  272. TZ_HANDLE IStreamPipe::GetHandle()
  273. {
  274. return m_handle;
  275. }
  276. ExcepInfo IStreamPipe::GetExcepInfo()
  277. {
  278. return m_einfo;
  279. }
  280. PipeStateChecker * PipeStateChecker::_ins = nullptr;
  281. tzc::Mutex PipeStateChecker::_insLock;
  282. PipeStateChecker::PipeStateChecker()
  283. {
  284. this->Start();
  285. }
  286. PipeStateChecker::~PipeStateChecker()
  287. {
  288. this->StopAndWait();
  289. m_pipeMapLock.lock();
  290. m_pipes.clear();
  291. m_pipeMapLock.unlock();
  292. }
  293. PipeStateChecker * PipeStateChecker::Instance()
  294. {
  295. if (!_ins)
  296. {
  297. _insLock.Lock();
  298. if (!_ins)
  299. {
  300. _ins = new PipeStateChecker();
  301. }
  302. _insLock.Unlock();
  303. }
  304. return _ins;
  305. }
  306. void PipeStateChecker::DestoryInstance()
  307. {
  308. _insLock.Lock();
  309. TZ_delete(_ins);
  310. _insLock.Unlock();
  311. }
  312. TZ_INT PipeStateChecker::RegisterPipe(const SPtr<IStreamPipe> & pipe)
  313. {
  314. std::unique_lock<std::mutex> lock(m_pipeMapLock);
  315. if (m_pipes.end() != m_pipes.find(pipe->m_handle))
  316. {
  317. TZLogWarn("pipe %llu registered!!!", pipe->m_handle);
  318. return MEC_DUPLICATED;
  319. }
  320. m_pipes[pipe->m_handle] = pipe;
  321. TZLogInfo("register pipe %llu~~~", pipe->m_handle);
  322. return MEC_OK;
  323. }
  324. TZ_INT PipeStateChecker::RemovePipe(TZ_HANDLE handle)
  325. {
  326. std::unique_lock<std::mutex> lock(m_pipeMapLock);
  327. if (m_pipes.end() == m_pipes.find(handle))
  328. {
  329. TZLogWarn("pipe %llu does not in checker!!!", handle);
  330. return MEC_NULL_OBJ;
  331. }
  332. m_pipes.erase(handle);
  333. TZLogInfo("rm pipe %llu~~~", handle);
  334. return MEC_OK;
  335. }
  336. void PipeStateChecker::Entry()
  337. {
  338. while (!this->IsStop())
  339. {
  340. m_pipeMapLock.lock();
  341. std::map<TZ_HANDLE, WPtr<IStreamPipe>> pipes(m_pipes);
  342. m_pipeMapLock.unlock();
  343. for (auto iter = pipes.begin(); iter != pipes.end(); ++iter)
  344. {
  345. TZ_HANDLE handle = (*iter).first;
  346. auto pipe = (*iter).second.lock();
  347. if (!pipe)
  348. {
  349. m_pipeMapLock.lock();
  350. m_pipes.erase(handle);
  351. TZLogInfo("erase pipe %llu~~~", handle);
  352. m_pipeMapLock.unlock();
  353. continue;
  354. }
  355. ExcepInfo & einfo = pipe->m_einfo;
  356. TIME_POINT now = TIME_NOW;
  357. if (std::chrono::duration_cast<std::chrono::milliseconds>(
  358. now - einfo.Lastest.load()).count() < einfo.Threshold)
  359. {
  360. einfo.OutputState = EMPS_OK;
  361. continue;
  362. }
  363. if (einfo.Lastest.load() == (TIME_POINT)ZERO_POINT)
  364. {
  365. einfo.OutputState = EMPS_NO_DATA;
  366. }
  367. else
  368. {
  369. einfo.OutputState = EMPS_TIMEOUT;
  370. }
  371. }
  372. tzc::SysUtils::DelaySeconds(PIPE_CHECK_DELAY_SECONDS);
  373. }
  374. }
  375. NAMESPACE_MAS_END