inference.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * @Description: 推理模块
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-10-13 09:37:51
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2021-10-19 13:51:36
  8. */
  9. #pragma once
  10. #include <iostream>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <gst/gst.h>
  14. #include <glib.h>
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include <string.h>
  18. #include <sys/time.h>
  19. #include "gstnvdsmeta.h"
  20. #include "gst-nvmessage.h"
  21. #include "DataSource.h"
  22. // ZLMediaKit
  23. #include "Util/logger.h"
  24. #include "Util/NoticeCenter.h"
  25. #include "Poller/EventPoller.h"
  26. #include "Player/PlayerProxy.h"
  27. #include "Rtmp/RtmpPusher.h"
  28. #include "Common/config.h"
  29. #include "Pusher/MediaPusher.h"
  30. #include "Extension/Frame.h"
  31. #include "Util/SqlPool.h"
  32. #include "Network/TcpClient.h"
  33. #include "Poller/Timer.h"
  34. #include "TCPClient.h"
  35. #include <mutex>
  36. #include <list>
  37. using namespace std;
  38. namespace MIVA{
  39. // 基于Deepstream
  40. class Inference
  41. {
  42. private:
  43. // Deepstream
  44. GMainLoop *loop = NULL;
  45. GstElement *pipeline = NULL,*streammux =NULL, *sink = NULL, *pgie = NULL,
  46. *queue1, *queue2, *queue3, *queue4, *queue5, *nvvidconv = NULL,
  47. *nvosd = NULL, *tiler = NULL;
  48. GstElement *transform = NULL;
  49. GstBus *bus = NULL;
  50. guint bus_watch_id;
  51. GstPad *tiler_src_pad = NULL;
  52. guint tiler_rows, tiler_columns;
  53. guint pgie_batch_size;
  54. std::vector<DataSource> m_DataList;
  55. public:
  56. /**
  57. * @description: 创建对象
  58. * @param {*}
  59. * @return {*} 智能指针
  60. * @author: lishengyin
  61. */
  62. static std::shared_ptr<Inference> CreateNew();
  63. Inference();
  64. ~Inference();
  65. public:
  66. /**
  67. * @description: 推理模块初始化
  68. * @param {vector<DataSource>} DataList 数据源集合
  69. * @return {*}
  70. * @author: lishengyin
  71. */
  72. int32_t Init(std::vector<DataSource> DataList);
  73. /**
  74. * @description: 释放数据
  75. * @param {*}
  76. * @return {*}
  77. * @author: lishengyin
  78. */
  79. void Destory();
  80. /**
  81. * @description: 准备任务
  82. * @param {*}
  83. * @return {*}
  84. * @author: lishengyin
  85. */
  86. void ReadyTask();
  87. /**
  88. * @description: 启动任务
  89. * @param {*}
  90. * @return {*}
  91. * @author: lishengyin
  92. */
  93. void StartTask();
  94. /**
  95. * @description: 暂停任务
  96. * @param {*}
  97. * @return {*}
  98. * @author: lishengyin
  99. */
  100. void PauseTask();
  101. /**
  102. * @description: 停止任务
  103. * @param {*}
  104. * @return {*}
  105. * @author: lishengyin
  106. */
  107. void StopTask();
  108. /**
  109. * @description: 重启任务
  110. * @param {*}
  111. * @return {*}
  112. * @author: lishengyin
  113. */
  114. void RestartTask();
  115. private:
  116. /**
  117. * @description: 获取推理结果
  118. * @param {GstPad *} pad
  119. * @param {GstPadProbeInfo *} info
  120. * @param {gpointer} u_data
  121. * @return {*}
  122. * @author: lishengyin
  123. */
  124. static GstPadProbeReturn tiler_src_pad_buffer_probe(GstPad * pad, GstPadProbeInfo * info, gpointer u_data);
  125. /**
  126. * @description: 监听bus
  127. * @param {GstBus *} bus
  128. * @param {GstMessage *} msg
  129. * @param {gpointer} data
  130. * @return {*}
  131. * @author: lishengyin
  132. */
  133. static gboolean bus_call (GstBus * bus, GstMessage * msg, gpointer data);
  134. /**
  135. * @description: 解码模块监听child_added
  136. * @param {*}
  137. * @return {*}
  138. * @author: lishengyin
  139. */
  140. static void decodebin_child_added (GstChildProxy * child_proxy, GObject * object,
  141. gchar * name, gpointer user_data);
  142. /**
  143. * @description: 创建解码bin
  144. * @param {guint} index
  145. * @param {gchar *} filename
  146. * @return {*}
  147. * @author: lishengyin
  148. */
  149. static GstElement * create_uridecode_bin (guint index, gchar * filename);
  150. /**
  151. * @description: 解码模块监听pad-added
  152. * @param {GstElement *} decodebin
  153. * @param {GstPad *} pad
  154. * @param {gpointer} data
  155. * @return {*}
  156. * @author: lishengyin
  157. */
  158. static void cb_newpad (GstElement * decodebin, GstPad * pad, gpointer data);
  159. /**
  160. * @description: 添加数据源
  161. * @param {int} source_Id 数据源ID
  162. * @param {string} uri 数据源Url
  163. * @return {*} 是否添加成功
  164. * @author: lishengyin
  165. */
  166. gboolean add_sources (int source_Id, std::string uri);
  167. /**
  168. * @description: 删除数据源
  169. * @param {gint} source_id
  170. * @return {*}
  171. * @author: lishengyin
  172. */
  173. void stop_release_source (gint source_id);
  174. };
  175. }