thread_manager.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #pragma once
  2. #include <thread>
  3. #include <atomic>
  4. #include <memory>
  5. #include <functional>
  6. #include <mutex>
  7. #include "../base/types.h"
  8. extern "C" {
  9. #include <libavformat/avformat.h>
  10. #include <libavcodec/avcodec.h>
  11. #include <libavcodec/bsf.h>
  12. }
  13. namespace av {
  14. // 前向声明codec命名空间中的类
  15. namespace codec {
  16. class VideoDecoder;
  17. class AudioDecoder;
  18. }
  19. namespace player {
  20. }
  21. namespace utils {
  22. class Synchronizer;
  23. }
  24. namespace utils {
  25. class PacketQueue;
  26. class FrameQueue;
  27. }
  28. namespace player {
  29. // 使用codec命名空间中的解码器
  30. using VideoDecoder = av::codec::VideoDecoder;
  31. using AudioDecoder = av::codec::AudioDecoder;
  32. // 帧输出回调函数类型定义
  33. using FrameOutputCallback = std::function<void(const AVFramePtr&)>;
  34. /**
  35. * @brief 线程基类
  36. */
  37. class ThreadBase {
  38. public:
  39. ThreadBase(const std::string& name);
  40. virtual ~ThreadBase();
  41. // 禁用拷贝
  42. ThreadBase(const ThreadBase&) = delete;
  43. ThreadBase& operator=(const ThreadBase&) = delete;
  44. /**
  45. * @brief 启动线程
  46. */
  47. bool start();
  48. /**
  49. * @brief 停止线程
  50. */
  51. void stop();
  52. /**
  53. * @brief 等待线程结束
  54. */
  55. void join();
  56. /**
  57. * @brief 检查线程是否正在运行
  58. */
  59. bool isRunning() const;
  60. /**
  61. * @brief 获取线程名称
  62. */
  63. const std::string& getName() const { return m_name; }
  64. protected:
  65. /**
  66. * @brief 线程主函数,子类需要实现
  67. */
  68. virtual void run() = 0;
  69. /**
  70. * @brief 检查是否应该停止
  71. */
  72. bool shouldStop() const { return m_shouldStop.load(); }
  73. private:
  74. std::string m_name;
  75. std::unique_ptr<std::thread> m_thread;
  76. std::atomic<bool> m_running{false};
  77. std::atomic<bool> m_shouldStop{false};
  78. void threadEntry();
  79. };
  80. /**
  81. * @brief 读取线程,负责从媒体文件读取数据包
  82. */
  83. class ReadThread : public ThreadBase {
  84. public:
  85. ReadThread(AVFormatContext* formatContext,
  86. av::utils::PacketQueue* packetQueue,
  87. int videoStreamIndex = -1,
  88. int audioStreamIndex = -1);
  89. ~ReadThread() override = default;
  90. /**
  91. * @brief 设置跳转目标
  92. */
  93. void seek(int64_t timestamp);
  94. protected:
  95. void run() override;
  96. private:
  97. AVFormatContext* m_formatContext;
  98. av::utils::PacketQueue* m_packetQueue;
  99. int m_videoStreamIndex;
  100. int m_audioStreamIndex;
  101. std::atomic<int64_t> m_seekTarget{-1};
  102. std::atomic<bool> m_seeking{false};
  103. bool readPacket();
  104. void handleSeek();
  105. };
  106. /**
  107. * @brief 视频解码线程
  108. */
  109. class VideoDecodeThread : public ThreadBase {
  110. public:
  111. VideoDecodeThread(av::utils::PacketQueue* packetQueue,
  112. av::utils::FrameQueue* frameQueue,
  113. VideoDecoder* decoder,
  114. av::utils::Synchronizer* synchronizer,
  115. int streamIndex,
  116. AVCodecParameters* codecParams = nullptr,
  117. AVRational timeBase = {1, 1000000});
  118. ~VideoDecodeThread() override;
  119. /**
  120. * @brief 设置帧输出回调函数
  121. */
  122. void setFrameOutputCallback(FrameOutputCallback callback);
  123. protected:
  124. void run() override;
  125. private:
  126. av::utils::PacketQueue* m_packetQueue;
  127. av::utils::FrameQueue* m_frameQueue;
  128. VideoDecoder* m_decoder;
  129. av::utils::Synchronizer* m_synchronizer;
  130. int m_streamIndex;
  131. AVBSFContext* m_bsfContext;
  132. FrameOutputCallback m_frameOutputCallback;
  133. AVRational m_timeBase;
  134. bool decodeFrame();
  135. bool initBitStreamFilter(AVCodecParameters* codecParams);
  136. void cleanupBitStreamFilter();
  137. };
  138. /**
  139. * @brief 音频解码线程
  140. */
  141. class AudioDecodeThread : public ThreadBase {
  142. public:
  143. AudioDecodeThread(av::utils::PacketQueue* packetQueue,
  144. av::utils::FrameQueue* frameQueue,
  145. AudioDecoder* decoder,
  146. av::utils::Synchronizer* synchronizer,
  147. int streamIndex,
  148. AVRational timeBase = {1, 1000000});
  149. ~AudioDecodeThread() override = default;
  150. /**
  151. * @brief 设置帧输出回调函数
  152. */
  153. void setFrameOutputCallback(FrameOutputCallback callback);
  154. protected:
  155. void run() override;
  156. private:
  157. av::utils::PacketQueue* m_packetQueue;
  158. av::utils::FrameQueue* m_frameQueue;
  159. AudioDecoder* m_decoder;
  160. av::utils::Synchronizer* m_synchronizer;
  161. int m_streamIndex;
  162. FrameOutputCallback m_frameOutputCallback;
  163. AVRational m_timeBase;
  164. bool decodeFrame();
  165. };
  166. /**
  167. * @brief 线程管理器,统一管理所有播放相关线程
  168. */
  169. class ThreadManager {
  170. public:
  171. ThreadManager();
  172. ~ThreadManager();
  173. // 禁用拷贝
  174. ThreadManager(const ThreadManager&) = delete;
  175. ThreadManager& operator=(const ThreadManager&) = delete;
  176. /**
  177. * @brief 创建读取线程
  178. */
  179. bool createReadThread(AVFormatContext* formatContext,
  180. av::utils::PacketQueue* packetQueue,
  181. int videoStreamIndex = -1,
  182. int audioStreamIndex = -1);
  183. /**
  184. * @brief 创建视频解码线程
  185. */
  186. bool createVideoDecodeThread(av::utils::PacketQueue* packetQueue,
  187. av::utils::FrameQueue* frameQueue,
  188. VideoDecoder* decoder,
  189. av::utils::Synchronizer* synchronizer,
  190. int streamIndex,
  191. AVCodecParameters* codecParams = nullptr,
  192. AVRational timeBase = {1, 1000000});
  193. /**
  194. * @brief 创建音频解码线程
  195. */
  196. bool createAudioDecodeThread(av::utils::PacketQueue* packetQueue,
  197. av::utils::FrameQueue* frameQueue,
  198. AudioDecoder* decoder,
  199. av::utils::Synchronizer* synchronizer,
  200. int streamIndex,
  201. AVRational timeBase = {1, 1000000});
  202. /**
  203. * @brief 启动所有线程
  204. */
  205. bool startAll();
  206. /**
  207. * @brief 停止所有线程
  208. */
  209. void stopAll();
  210. /**
  211. * @brief 等待所有线程结束
  212. */
  213. void joinAll();
  214. /**
  215. * @brief 检查是否有线程正在运行
  216. */
  217. bool hasRunningThreads() const;
  218. /**
  219. * @brief 获取读取线程
  220. */
  221. ReadThread* getReadThread() const { return m_readThread.get(); }
  222. /**
  223. * @brief 获取视频解码线程
  224. */
  225. VideoDecodeThread* getVideoDecodeThread() const { return m_videoDecodeThread.get(); }
  226. /**
  227. * @brief 获取音频解码线程
  228. */
  229. AudioDecodeThread* getAudioDecodeThread() const { return m_audioDecodeThread.get(); }
  230. private:
  231. std::unique_ptr<ReadThread> m_readThread;
  232. std::unique_ptr<VideoDecodeThread> m_videoDecodeThread;
  233. std::unique_ptr<AudioDecodeThread> m_audioDecodeThread;
  234. };
  235. /**
  236. * @brief 线程安全的回调函数包装器
  237. */
  238. template<typename Func>
  239. class ThreadSafeCallback {
  240. public:
  241. ThreadSafeCallback(Func&& func) : m_func(std::forward<Func>(func)) {}
  242. template<typename... Args>
  243. auto operator()(Args&&... args) -> decltype(m_func(std::forward<Args>(args)...)) {
  244. std::lock_guard<std::mutex> lock(m_mutex);
  245. return m_func(std::forward<Args>(args)...);
  246. }
  247. private:
  248. Func m_func;
  249. mutable std::mutex m_mutex;
  250. };
  251. /**
  252. * @brief 创建线程安全的回调函数
  253. */
  254. template<typename Func>
  255. auto makeThreadSafeCallback(Func&& func) {
  256. return ThreadSafeCallback<Func>(std::forward<Func>(func));
  257. }
  258. } // namespace player
  259. } // namespace av