StreamDef.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #ifndef __MAS_PIPE_STATE_CHECKER_H
  2. #define __MAS_PIPE_STATE_CHECKER_H
  3. #include "MAS_AlgoDef.h"
  4. #include "ComDef.h"
  5. #include "Locks.h"
  6. #include <mutex>
  7. #include <unordered_map>
  8. NAMESPACE_MAS_BEGIN
  9. #define PIPESTATECHECKER PipeStateChecker::Instance()
  10. typedef enum __MAS_MEDIA_ENPACK {
  11. EMMP_UNKNOWN,
  12. EMMP_RTP,
  13. EMMP_FRAME,
  14. } EN_MAS_MEDIA_ENPACK;
  15. typedef enum __MAS_MEDIA_ENCODE {
  16. EMME_UNKNOWN,
  17. EMME_H264,
  18. EMME_H265,
  19. EMME_JPEG,
  20. EMME_PNG,
  21. EMME_YUV,
  22. EMME_RGB,
  23. } EN_MAS_MEDIA_ENCODE;
  24. typedef enum __MAS_MEDIA_FRAME_TYPE {
  25. EMMFT_UNKNOWN,
  26. EMMFT_I,
  27. EMMFT_P,
  28. EMMFT_B,
  29. EMMFT_OTHER,
  30. } EN_MEDIA_FRAME_TYPE;
  31. struct ExcepInfo {
  32. ExcepInfo(const std::string & name, TZ_INT threshold);
  33. ExcepInfo(const ExcepInfo & einfo);
  34. ~ExcepInfo();
  35. TZ_INT Threshold; //阈值, 多少毫秒没有收到帧数据就进行报错
  36. std::string Name; //界面展示name
  37. std::atomic<TZ_INT> OutputState;
  38. std::atomic<TIME_POINT> Lastest;
  39. };
  40. struct Media {
  41. Media(TZ_INT Length);
  42. Media(const Media & origin);
  43. ~Media();
  44. TZ_INT Length;
  45. TZ_INT DataType;
  46. TZ_INT Height;
  47. TZ_INT Width;
  48. void * Mem;
  49. };
  50. struct StraProducing {
  51. StraProducing(const std::string & key,
  52. const StraRst & rst, SPtr<Media> & media) :
  53. Key(key), Result(rst), StraMedia(media) {}
  54. ~StraProducing() {}
  55. std::string Key;
  56. StraRst Result;
  57. SPtr<Media> StraMedia;
  58. };
  59. struct DetProducing {
  60. DetProducing(const std::string & key,
  61. const std::string & rst, SPtr<Media> & media,
  62. const DrawInfo & draw) :
  63. Key(key), Result(rst), DetMedia(media), Draw(draw) {}
  64. ~DetProducing() {}
  65. std::string Key;
  66. std::string Result;
  67. SPtr<Media> DetMedia;
  68. DrawInfo Draw;
  69. };
  70. class StreamInfo {
  71. public:
  72. StreamInfo();
  73. StreamInfo(TZ_HANDLE hd);
  74. StreamInfo(const StreamInfo & origin);
  75. StreamInfo(SPtr<StreamInfo> & origin);
  76. virtual ~StreamInfo();
  77. TZ_INT SetMediaRsc(SPtr<Media> & mediaRsc);
  78. TZ_INT SetStraRst(const std::string & straKey,
  79. const StraRst & straRst, SPtr<Media> & Media);
  80. TZ_INT GetDeliverType();
  81. void SetDeliverType(TZ_INT type);
  82. TZ_INT AddDetRst(const std::string & detKey,
  83. const std::string & detRst, SPtr<Media> & Media,
  84. const DrawInfo & draw);
  85. TZ_HANDLE GetHandle();
  86. TZ_INT SetSeq(TZ_Uint32 seq);
  87. TZ_Uint32 GetSeq();
  88. SPtr<Media> & GetMediaRsc();
  89. SPtr<StraProducing> & GetStraProducing();
  90. std::unordered_map<std::string, SPtr<DetProducing>> & GetAllDetRst();
  91. private:
  92. TZ_HANDLE m_hd;
  93. TZ_Uint32 m_seq;
  94. std::atomic_int m_deliverType;
  95. SPtr<Media> m_mediaRsc;
  96. SPtr<StraProducing> m_straProducing;
  97. std::mutex m_detLock;
  98. std::unordered_map<std::string, SPtr<DetProducing>> m_detProducings;
  99. };
  100. class IStreamPipe : public std::enable_shared_from_this<IStreamPipe> {
  101. friend class PipeStateChecker;
  102. public:
  103. // PlayPipe : srcId 媒体资源id, selfId 设备id
  104. // DetectorPipe : srcId 媒体资源id, selfId 检测单元id
  105. // StrategyPipe : srcId 场景id, selfId 判定策略id
  106. // WSSStreamPipe : srcId 媒体资源id selfId wss的sessId
  107. // ScenePipe: srcId 场景id selfId -
  108. IStreamPipe(TZ_INT srcId, int64_t selfId,
  109. TZ_INT type, const ExcepInfo & einfo);
  110. virtual ~IStreamPipe();
  111. TZ_INT Initialize();
  112. TZ_INT Dispose();
  113. TZ_INT AddStreamPipe(const SPtr<IStreamPipe> & pipe);
  114. TZ_INT RemoveStreamPipe(TZ_HANDLE hd);
  115. TZ_INT RemoveStreamPipeBySelfId(int64_t selfId);
  116. void Switch(TZ_BOOL flag);
  117. void DeliverStream(SPtr<StreamInfo> & streamInfo, TZ_BOOL cyFlag = FALSE);
  118. TZ_INT GetType();
  119. TZ_INT GetSrcId();
  120. int64_t GetSelfId();
  121. TZ_HANDLE GetHandle();
  122. ExcepInfo GetExcepInfo();
  123. protected:
  124. void updateExcepTime();
  125. virtual TZ_BOOL streamFilter(SPtr<StreamInfo> & streamInfo);
  126. virtual TZ_INT pipeInitialize();
  127. virtual TZ_INT pipeDispose();
  128. // pos0: auto deliver or not
  129. // pos1: copy or not
  130. virtual std::tuple<TZ_BOOL, TZ_BOOL> streamArrived(
  131. SPtr<StreamInfo> & stream) = 0;
  132. private:
  133. void streamArrivedBySwitch(SPtr<StreamInfo> & streamInfo);
  134. protected:
  135. TZ_BOOL m_inited;
  136. private:
  137. TZ_HANDLE m_handle;
  138. TZ_INT m_type; // EN_MAS_PIPE_TYPE
  139. TZ_INT m_srcId;
  140. int64_t m_selfId;
  141. std::atomic_bool m_switch;
  142. std::mutex m_nextLock;
  143. std::map<TZ_HANDLE, SPtr<IStreamPipe>> m_next;
  144. ExcepInfo m_einfo;
  145. };
  146. class PipeStateChecker : public tzc::OSThread {
  147. public:
  148. static PipeStateChecker * Instance();
  149. static void DestoryInstance();
  150. public:
  151. TZ_INT RegisterPipe(const SPtr<IStreamPipe> & pipe);
  152. TZ_INT RemovePipe(TZ_HANDLE handle);
  153. private:
  154. PipeStateChecker();
  155. virtual ~PipeStateChecker();
  156. virtual void Entry();
  157. private:
  158. std::mutex m_pipeMapLock;
  159. std::map<TZ_HANDLE, WPtr<IStreamPipe>> m_pipes;
  160. static PipeStateChecker * _ins;
  161. static tzc::Mutex _insLock;
  162. };
  163. NAMESPACE_MAS_END
  164. #endif