RtpOneRoadThread.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "RtpOneRoadThread.h"
  2. RTPOneRoadThread::RTPOneRoadThread(RecordThreadInfo_t& threadInfo)
  3. : BaseRecordThread(threadInfo),
  4. m_eventLoop()
  5. {
  6. m_logger = spdlog::get("RTPServer");
  7. if(m_logger == nullptr)
  8. {
  9. fmt::print("RTPServer 日志记录器未初始化,请先初始化日志记录器");
  10. return;
  11. }
  12. m_logBase = fmt::format("录音通道: {}:{}", static_cast<int>(m_threadInfo.cardRoadInfo.strSoundCardName.toStdString(),
  13. m_threadInfo.cardRoadInfo.roadInfo.nRoadNum));
  14. m_isRunning.store(true); // 设置为运行状态
  15. }
  16. RTPOneRoadThread::~RTPOneRoadThread()
  17. {
  18. }
  19. /* 发送数据的线程函数 */
  20. void RTPOneRoadThread::ThreadTask()
  21. {
  22. if(!initData())
  23. {
  24. SPDLOG_LOGGER_ERROR(m_logger, "{} 初始化数据失败", m_logBase);
  25. return;
  26. }
  27. m_sendTimer.setTimerType(Qt::PreciseTimer);
  28. m_sendTimer.setSingleShot(false); // 设置为循环定时器
  29. m_sendTimer.setInterval(10); // 每10毫秒发送一次数据
  30. QEventLoop::connect(&m_sendTimer, &QTimer::timeout, this, &RTPOneRoadThread::do_timerSendData);
  31. m_sendTimer.start();
  32. /* 将线程控制权交给Qt的事件循环 */
  33. m_eventLoop.exec();
  34. /* 清空数据 */
  35. clearData();
  36. }
  37. /**
  38. * @brief 停止线程
  39. */
  40. void RTPOneRoadThread::stopThread()
  41. {
  42. if(m_sendTimer.isActive())
  43. {
  44. m_sendTimer.stop();
  45. }
  46. while(m_eventLoop.isRunning())
  47. {
  48. m_eventLoop.quit();
  49. m_eventLoop.processEvents(QEventLoop::AllEvents, 100);
  50. }
  51. }
  52. /* 设置数据 */
  53. bool RTPOneRoadThread::setData(const AudioSrcData& srcData)
  54. {
  55. /* UDP会话列表是空的,就不接收数据了 */
  56. if(!m_isRecvData.load())
  57. {
  58. return true;
  59. }
  60. /* 如果数据列表1被占用,就写入数据列表2 */
  61. AudioSrcData* data = new AudioSrcData(srcData);
  62. if(data == nullptr)
  63. {
  64. return false;
  65. }
  66. m_ringQueue.push(data);
  67. return false;
  68. }
  69. /* 添加一个UDP会话 */
  70. bool RTPOneRoadThread::addUdpSession(const RtpSendClientInfo_t& udpSession)
  71. {
  72. /* 检查是否已有相同的会话,这里只需要检查IP和端口即可,会话ID和名称在这里没什么用 */
  73. for(const auto& session : m_listUdpSockets)
  74. {
  75. if(session.clientIP == udpSession.clientIP && session.clientPort == udpSession.clientPort)
  76. {
  77. SPDLOG_LOGGER_DEBUG(m_logger, "{} 已存在相同的UDP会话: {}:{}", m_logBase, udpSession.clientIP.toStdString(), udpSession.clientPort);
  78. return true;
  79. }
  80. }
  81. /* 创建新的UDP套接字 */
  82. QUdpSocket* udpSocket = new QUdpSocket();
  83. /* 绑定本地IP和端口 */
  84. if(!udpSocket->bind(QHostAddress::Any, udpSession.localPort))
  85. {
  86. SPDLOG_LOGGER_ERROR(m_logger, "{} 创建UDP套接字失败: {}:{}", m_logBase, udpSession.clientIP.toStdString(), udpSession.clientPort);
  87. delete udpSocket;
  88. return false;
  89. }
  90. SPDLOG_LOGGER_INFO(m_logger, "{} 添加UDP会话: {}:{}", m_logBase, udpSession.clientIP.toStdString(), udpSession.clientPort);
  91. // 将新的会话添加到列表中
  92. RtpSendClientInfo_t newSession = udpSession;
  93. newSession.udpSocket = udpSocket;
  94. m_lockUdpSockets.lock();
  95. m_listUdpSockets.append(newSession);
  96. m_lockUdpSockets.unlock();
  97. m_isRecvData.store(true); // 设置为接收数据状态
  98. return true;
  99. }
  100. /* 删除一个会话 */
  101. bool RTPOneRoadThread::removeUdpSession(const RtpSendClientInfo_t& udpSession)
  102. {
  103. std::lock_guard<QMutex> lock(m_lockUdpSockets);
  104. for(auto it = m_listUdpSockets.begin(); it != m_listUdpSockets.end(); ++it)
  105. {
  106. if(it->clientIP == udpSession.clientIP && it->clientPort == udpSession.clientPort)
  107. {
  108. if(it->udpSocket != nullptr)
  109. {
  110. it->udpSocket->close();
  111. delete it->udpSocket;
  112. it->udpSocket = nullptr;
  113. }
  114. SPDLOG_LOGGER_INFO(m_logger, "{} 删除UDP会话: {}:{}", m_logBase, it->clientIP.toStdString(), it->clientPort);
  115. // 从列表中删除该会话
  116. m_listUdpSockets.erase(it);
  117. break;
  118. }
  119. }
  120. /* 如果列表为空,就不再接收数据 */
  121. if(m_listUdpSockets.isEmpty())
  122. {
  123. m_isRecvData.store(false);
  124. SPDLOG_LOGGER_DEBUG(m_logger, "{} UDP会话列表为空,不再接收音频原始数据", m_logBase);
  125. /* 清空环形队列 */
  126. while(m_ringQueue.QueueSize() > 0)
  127. {
  128. auto data = m_ringQueue.front_pop_noBlock();
  129. if(data != nullptr)
  130. {
  131. delete data;
  132. data = nullptr;
  133. }
  134. }
  135. }
  136. return true;
  137. }
  138. /* 初始化数据 */
  139. bool RTPOneRoadThread::initData()
  140. {
  141. m_listUdpSockets.clear();
  142. m_isRecvData.store(false);
  143. m_ringQueue.setQueueCapacity(512);
  144. m_ringQueue.setDefaultValue(nullptr);
  145. return true;
  146. }
  147. /* 清除数据*/
  148. void RTPOneRoadThread::clearData()
  149. {
  150. m_isRecvData.store(false); // 设置为不接收数据状态
  151. /* 清空UDP会话列表 */
  152. std::lock_guard<QMutex> lock(m_lockUdpSockets);
  153. for(auto& session : m_listUdpSockets)
  154. {
  155. if(session.udpSocket != nullptr)
  156. {
  157. session.udpSocket->close();
  158. delete session.udpSocket;
  159. session.udpSocket = nullptr;
  160. }
  161. }
  162. m_listUdpSockets.clear();
  163. /* 清空环形队列 */
  164. while(m_ringQueue.QueueSize() > 0)
  165. {
  166. auto data = m_ringQueue.front_pop_noBlock();
  167. if(data != nullptr)
  168. {
  169. delete data;
  170. data = nullptr;
  171. }
  172. }
  173. }
  174. /**
  175. * @brief 发送数据,先获取一个缓冲区的数据,获取到后就遍历所有的UDP会话,将这个缓冲区所有的数据都发送出去
  176. * 然后紧接着获取第二个缓冲区的数据,也一起发送出去
  177. *
  178. * @return true
  179. * @return false
  180. */
  181. bool RTPOneRoadThread::sendData()
  182. {
  183. while(m_ringQueue.QueueSize() > 0 && m_isRunning.load())
  184. {
  185. auto data = m_ringQueue.front_pop_noBlock();
  186. if(data == nullptr)
  187. {
  188. continue;
  189. }
  190. if(data->isEmpty())
  191. {
  192. delete data;
  193. continue;
  194. }
  195. std::lock_guard<QMutex> lock(m_lockUdpSockets);
  196. /* 遍历所有的UDP会话,发送数据 */
  197. for(const auto& session : m_listUdpSockets)
  198. {
  199. if(session.udpSocket == nullptr || !session.udpSocket->isValid())
  200. {
  201. SPDLOG_LOGGER_WARN(m_logger, "{} 无效的UDP套接字: {}:{}", m_logBase, session.clientIP.toStdString(), session.clientPort);
  202. continue;
  203. }
  204. qint64 bytesSent = session.udpSocket->writeDatagram(data->pData, data->dataSize,
  205. QHostAddress(session.clientIP), session.clientPort);
  206. if(bytesSent == -1)
  207. {
  208. SPDLOG_LOGGER_ERROR(m_logger, "{} 发送数据失败: {}:{}", m_logBase, session.clientIP.toStdString(), session.clientPort);
  209. }
  210. else
  211. {
  212. SPDLOG_LOGGER_DEBUG(m_logger, "{} 发送数据成功: {}:{} 大小: {}", m_logBase, session.clientIP.toStdString(), session.clientPort, bytesSent);
  213. }
  214. }
  215. /* 发送完数据后,删除这个数据 */
  216. delete data;
  217. data = nullptr;
  218. }
  219. return true;
  220. }
  221. /* 定时发送数据 */
  222. void RTPOneRoadThread::do_timerSendData()
  223. {
  224. if(!m_isRecvData)
  225. {
  226. return;
  227. }
  228. sendData();
  229. }