IBufferManager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #ifndef IBUFFERMANAGER_H
  2. #define IBUFFERMANAGER_H
  3. #include <memory>
  4. #include <functional>
  5. #include <chrono>
  6. #include <string>
  7. #include <vector>
  8. namespace AVPlayer2 {
  9. namespace Core {
  10. /**
  11. * 缓冲区状态枚举
  12. */
  13. enum class BufferState {
  14. Empty, // 空缓冲区
  15. Filling, // 填充中
  16. Ready, // 准备就绪
  17. Full, // 缓冲区满
  18. Draining, // 排空中
  19. Error // 错误状态
  20. };
  21. /**
  22. * 缓冲区类型枚举
  23. */
  24. enum class BufferType {
  25. Audio, // 音频缓冲区
  26. Video, // 视频缓冲区
  27. Subtitle, // 字幕缓冲区
  28. Packet, // 数据包缓冲区
  29. Frame // 帧缓冲区
  30. };
  31. /**
  32. * 缓冲策略枚举
  33. */
  34. enum class BufferStrategy {
  35. FixedSize, // 固定大小
  36. DynamicSize, // 动态大小
  37. TimeBased, // 基于时间
  38. AdaptiveSize // 自适应大小
  39. };
  40. } // namespace Core
  41. namespace Types {
  42. /**
  43. * 缓冲区配置
  44. */
  45. struct BufferConfig {
  46. Core::BufferType type; // 缓冲区类型
  47. Core::BufferStrategy strategy; // 缓冲策略
  48. size_t maxSize; // 最大大小(字节)
  49. size_t minSize; // 最小大小(字节)
  50. int maxCount; // 最大数量
  51. int minCount; // 最小数量
  52. std::chrono::milliseconds maxDuration; // 最大时长
  53. std::chrono::milliseconds minDuration; // 最小时长
  54. double lowWaterMark; // 低水位标记(0.0-1.0)
  55. double highWaterMark; // 高水位标记(0.0-1.0)
  56. bool enablePreallocation; // 启用预分配
  57. bool enableMemoryPool; // 启用内存池
  58. BufferConfig() {
  59. type = Core::BufferType::Audio;
  60. strategy = Core::BufferStrategy::FixedSize;
  61. maxSize = 1024 * 1024; // 1MB
  62. minSize = 64 * 1024; // 64KB
  63. maxCount = 100;
  64. minCount = 10;
  65. maxDuration = std::chrono::milliseconds(5000); // 5秒
  66. minDuration = std::chrono::milliseconds(1000); // 1秒
  67. lowWaterMark = 0.2;
  68. highWaterMark = 0.8;
  69. enablePreallocation = true;
  70. enableMemoryPool = true;
  71. }
  72. };
  73. /**
  74. * 缓冲区统计信息
  75. */
  76. struct BufferStats {
  77. Core::BufferType type; // 缓冲区类型
  78. Core::BufferState state; // 当前状态
  79. size_t currentSize; // 当前大小(字节)
  80. size_t maxSize; // 最大大小(字节)
  81. int currentCount; // 当前数量
  82. int maxCount; // 最大数量
  83. std::chrono::milliseconds currentDuration; // 当前时长
  84. std::chrono::milliseconds maxDuration; // 最大时长
  85. double fillLevel; // 填充级别(0.0-1.0)
  86. double utilizationRate; // 利用率(0.0-1.0)
  87. int underrunCount; // 下溢次数
  88. int overrunCount; // 上溢次数
  89. double averageInputRate; // 平均输入速率(字节/秒)
  90. double averageOutputRate; // 平均输出速率(字节/秒)
  91. std::chrono::microseconds lastInputTime; // 最后输入时间
  92. std::chrono::microseconds lastOutputTime; // 最后输出时间
  93. BufferStats() {
  94. type = Core::BufferType::Audio;
  95. state = Core::BufferState::Empty;
  96. currentSize = 0;
  97. maxSize = 0;
  98. currentCount = 0;
  99. maxCount = 0;
  100. currentDuration = std::chrono::milliseconds(0);
  101. maxDuration = std::chrono::milliseconds(0);
  102. fillLevel = 0.0;
  103. utilizationRate = 0.0;
  104. underrunCount = 0;
  105. overrunCount = 0;
  106. averageInputRate = 0.0;
  107. averageOutputRate = 0.0;
  108. lastInputTime = std::chrono::microseconds(0);
  109. lastOutputTime = std::chrono::microseconds(0);
  110. }
  111. };
  112. /**
  113. * 缓冲区项目信息
  114. */
  115. struct BufferItem {
  116. std::string id; // 项目ID
  117. std::shared_ptr<void> data; // 数据指针
  118. size_t size; // 数据大小
  119. std::chrono::microseconds timestamp; // 时间戳
  120. std::chrono::microseconds duration; // 持续时间
  121. int priority; // 优先级
  122. std::map<std::string, std::string> metadata; // 元数据
  123. BufferItem() {
  124. size = 0;
  125. timestamp = std::chrono::microseconds(0);
  126. duration = std::chrono::microseconds(0);
  127. priority = 0;
  128. }
  129. };
  130. /**
  131. * 内存池统计信息
  132. */
  133. struct MemoryPoolStats {
  134. size_t totalAllocated; // 总分配内存
  135. size_t totalUsed; // 总使用内存
  136. size_t totalFree; // 总空闲内存
  137. int allocationCount; // 分配次数
  138. int deallocationCount; // 释放次数
  139. int poolHitCount; // 池命中次数
  140. int poolMissCount; // 池未命中次数
  141. double fragmentationRatio; // 碎片率
  142. MemoryPoolStats() {
  143. totalAllocated = 0;
  144. totalUsed = 0;
  145. totalFree = 0;
  146. allocationCount = 0;
  147. deallocationCount = 0;
  148. poolHitCount = 0;
  149. poolMissCount = 0;
  150. fragmentationRatio = 0.0;
  151. }
  152. };
  153. } // namespace Types
  154. namespace Core {
  155. /**
  156. * 缓冲区管理器抽象接口
  157. * 提供统一的缓冲区管理功能
  158. */
  159. class IBufferManager {
  160. public:
  161. virtual ~IBufferManager() = default;
  162. /**
  163. * 初始化缓冲区管理器
  164. * @param configs 缓冲区配置列表
  165. * @return 是否初始化成功
  166. */
  167. virtual bool initialize(const std::vector<Types::BufferConfig>& configs) = 0;
  168. /**
  169. * 释放缓冲区管理器
  170. */
  171. virtual void release() = 0;
  172. /**
  173. * 启动缓冲区管理器
  174. * @return 是否启动成功
  175. */
  176. virtual bool start() = 0;
  177. /**
  178. * 停止缓冲区管理器
  179. */
  180. virtual void stop() = 0;
  181. /**
  182. * 重置所有缓冲区
  183. */
  184. virtual void reset() = 0;
  185. // ========== 缓冲区操作 ==========
  186. /**
  187. * 创建缓冲区
  188. * @param name 缓冲区名称
  189. * @param config 缓冲区配置
  190. * @return 是否创建成功
  191. */
  192. virtual bool createBuffer(const std::string& name, const Types::BufferConfig& config) = 0;
  193. /**
  194. * 销毁缓冲区
  195. * @param name 缓冲区名称
  196. * @return 是否销毁成功
  197. */
  198. virtual bool destroyBuffer(const std::string& name) = 0;
  199. /**
  200. * 清空缓冲区
  201. * @param name 缓冲区名称
  202. * @return 是否清空成功
  203. */
  204. virtual bool clearBuffer(const std::string& name) = 0;
  205. /**
  206. * 调整缓冲区大小
  207. * @param name 缓冲区名称
  208. * @param newConfig 新配置
  209. * @return 是否调整成功
  210. */
  211. virtual bool resizeBuffer(const std::string& name, const Types::BufferConfig& newConfig) = 0;
  212. // ========== 数据操作 ==========
  213. /**
  214. * 向缓冲区写入数据
  215. * @param name 缓冲区名称
  216. * @param item 缓冲区项目
  217. * @param blocking 是否阻塞等待
  218. * @return 是否写入成功
  219. */
  220. virtual bool writeData(const std::string& name, const Types::BufferItem& item, bool blocking = false) = 0;
  221. /**
  222. * 从缓冲区读取数据
  223. * @param name 缓冲区名称
  224. * @param item 输出的缓冲区项目
  225. * @param blocking 是否阻塞等待
  226. * @return 是否读取成功
  227. */
  228. virtual bool readData(const std::string& name, Types::BufferItem& item, bool blocking = false) = 0;
  229. /**
  230. * 预览缓冲区数据(不移除)
  231. * @param name 缓冲区名称
  232. * @param item 输出的缓冲区项目
  233. * @param index 预览索引(0为最前面)
  234. * @return 是否预览成功
  235. */
  236. virtual bool peekData(const std::string& name, Types::BufferItem& item, int index = 0) = 0;
  237. /**
  238. * 跳过指定数量的数据项
  239. * @param name 缓冲区名称
  240. * @param count 跳过数量
  241. * @return 实际跳过的数量
  242. */
  243. virtual int skipData(const std::string& name, int count) = 0;
  244. /**
  245. * 根据时间戳跳转
  246. * @param name 缓冲区名称
  247. * @param timestamp 目标时间戳
  248. * @return 是否跳转成功
  249. */
  250. virtual bool seekToTimestamp(const std::string& name, std::chrono::microseconds timestamp) = 0;
  251. // ========== 状态查询 ==========
  252. /**
  253. * 获取缓冲区状态
  254. * @param name 缓冲区名称
  255. * @return 缓冲区状态
  256. */
  257. virtual BufferState getBufferState(const std::string& name) = 0;
  258. /**
  259. * 获取缓冲区统计信息
  260. * @param name 缓冲区名称
  261. * @return 统计信息
  262. */
  263. virtual Types::BufferStats getBufferStats(const std::string& name) = 0;
  264. /**
  265. * 获取所有缓冲区统计信息
  266. * @return 所有缓冲区统计信息
  267. */
  268. virtual std::vector<Types::BufferStats> getAllBufferStats() = 0;
  269. /**
  270. * 检查缓冲区是否存在
  271. * @param name 缓冲区名称
  272. * @return 是否存在
  273. */
  274. virtual bool hasBuffer(const std::string& name) = 0;
  275. /**
  276. * 检查缓冲区是否为空
  277. * @param name 缓冲区名称
  278. * @return 是否为空
  279. */
  280. virtual bool isEmpty(const std::string& name) = 0;
  281. /**
  282. * 检查缓冲区是否已满
  283. * @param name 缓冲区名称
  284. * @return 是否已满
  285. */
  286. virtual bool isFull(const std::string& name) = 0;
  287. /**
  288. * 检查缓冲区是否达到低水位
  289. * @param name 缓冲区名称
  290. * @return 是否达到低水位
  291. */
  292. virtual bool isLowWaterMark(const std::string& name) = 0;
  293. /**
  294. * 检查缓冲区是否达到高水位
  295. * @param name 缓冲区名称
  296. * @return 是否达到高水位
  297. */
  298. virtual bool isHighWaterMark(const std::string& name) = 0;
  299. /**
  300. * 获取缓冲区可用空间
  301. * @param name 缓冲区名称
  302. * @return 可用空间(字节)
  303. */
  304. virtual size_t getAvailableSpace(const std::string& name) = 0;
  305. /**
  306. * 获取缓冲区已用空间
  307. * @param name 缓冲区名称
  308. * @return 已用空间(字节)
  309. */
  310. virtual size_t getUsedSpace(const std::string& name) = 0;
  311. /**
  312. * 获取缓冲区数据项数量
  313. * @param name 缓冲区名称
  314. * @return 数据项数量
  315. */
  316. virtual int getItemCount(const std::string& name) = 0;
  317. /**
  318. * 获取缓冲区时长
  319. * @param name 缓冲区名称
  320. * @return 缓冲区时长
  321. */
  322. virtual std::chrono::milliseconds getBufferDuration(const std::string& name) = 0;
  323. // ========== 内存池管理 ==========
  324. /**
  325. * 启用内存池
  326. * @param enable 是否启用
  327. */
  328. virtual void enableMemoryPool(bool enable) = 0;
  329. /**
  330. * 获取内存池统计信息
  331. * @return 内存池统计信息
  332. */
  333. virtual Types::MemoryPoolStats getMemoryPoolStats() = 0;
  334. /**
  335. * 清理内存池
  336. */
  337. virtual void cleanupMemoryPool() = 0;
  338. /**
  339. * 预分配内存
  340. * @param size 预分配大小(字节)
  341. * @param count 预分配数量
  342. * @return 是否预分配成功
  343. */
  344. virtual bool preallocateMemory(size_t size, int count) = 0;
  345. // ========== 回调设置 ==========
  346. /**
  347. * 设置缓冲区状态变化回调
  348. * @param callback 状态变化回调函数
  349. */
  350. virtual void setBufferStateChangedCallback(
  351. std::function<void(const std::string&, BufferState, BufferState)> callback) = 0;
  352. /**
  353. * 设置低水位回调
  354. * @param callback 低水位回调函数
  355. */
  356. virtual void setLowWaterMarkCallback(
  357. std::function<void(const std::string&)> callback) = 0;
  358. /**
  359. * 设置高水位回调
  360. * @param callback 高水位回调函数
  361. */
  362. virtual void setHighWaterMarkCallback(
  363. std::function<void(const std::string&)> callback) = 0;
  364. /**
  365. * 设置缓冲区满回调
  366. * @param callback 缓冲区满回调函数
  367. */
  368. virtual void setBufferFullCallback(
  369. std::function<void(const std::string&)> callback) = 0;
  370. /**
  371. * 设置缓冲区空回调
  372. * @param callback 缓冲区空回调函数
  373. */
  374. virtual void setBufferEmptyCallback(
  375. std::function<void(const std::string&)> callback) = 0;
  376. /**
  377. * 设置错误回调
  378. * @param callback 错误回调函数
  379. */
  380. virtual void setErrorCallback(
  381. std::function<void(const std::string&, const std::string&)> callback) = 0;
  382. };
  383. /**
  384. * 缓冲区管理器工厂接口
  385. */
  386. class IBufferManagerFactory {
  387. public:
  388. virtual ~IBufferManagerFactory() = default;
  389. /**
  390. * 创建缓冲区管理器实例
  391. * @param name 管理器名称
  392. * @return 缓冲区管理器智能指针
  393. */
  394. virtual std::unique_ptr<IBufferManager> createBufferManager(const std::string& name = "") = 0;
  395. /**
  396. * 获取默认缓冲区配置
  397. * @param type 缓冲区类型
  398. * @return 默认配置
  399. */
  400. virtual Types::BufferConfig getDefaultConfig(BufferType type) = 0;
  401. /**
  402. * 获取推荐的缓冲区大小
  403. * @param type 缓冲区类型
  404. * @return 推荐大小(字节)
  405. */
  406. virtual size_t getRecommendedBufferSize(BufferType type) = 0;
  407. };
  408. } // namespace Core
  409. } // namespace AVPlayer2
  410. #endif // IBUFFERMANAGER_H