IPlayerController.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. #ifndef IPLAYERCONTROLLER_H
  2. #define IPLAYERCONTROLLER_H
  3. #include <string>
  4. #include <memory>
  5. #include <functional>
  6. #include <chrono>
  7. #include <vector>
  8. #include <map>
  9. namespace AVPlayer2 {
  10. namespace Core {
  11. /**
  12. * 播放器状态枚举
  13. */
  14. enum class PlayerState {
  15. Stopped, // 停止状态
  16. Loading, // 加载中
  17. Loaded, // 已加载
  18. Playing, // 播放中
  19. Paused, // 暂停
  20. Seeking, // 跳转中
  21. Buffering, // 缓冲中
  22. Error // 错误状态
  23. };
  24. /**
  25. * 播放模式枚举
  26. */
  27. enum class PlayMode {
  28. Normal, // 正常播放
  29. Loop, // 循环播放
  30. Shuffle, // 随机播放
  31. RepeatOne // 单曲循环
  32. };
  33. /**
  34. * 缩放模式枚举
  35. */
  36. enum class ScaleMode {
  37. Fit, // 适应窗口
  38. Fill, // 填充窗口
  39. Stretch, // 拉伸
  40. Original, // 原始大小
  41. Custom // 自定义
  42. };
  43. } // namespace Core
  44. namespace Types {
  45. /**
  46. * 播放器配置
  47. */
  48. struct PlayerConfig {
  49. bool enableHardwareDecoding; // 启用硬件解码
  50. bool enableAudioOutput; // 启用音频输出
  51. bool enableVideoOutput; // 启用视频输出
  52. bool enableSubtitles; // 启用字幕
  53. int maxVideoFrameBuffer; // 最大视频帧缓冲
  54. int maxAudioFrameBuffer; // 最大音频帧缓冲
  55. double seekAccuracy; // 跳转精度(秒)
  56. bool autoPlay; // 自动播放
  57. double defaultVolume; // 默认音量(0.0-1.0)
  58. Core::PlayMode defaultPlayMode; // 默认播放模式
  59. Core::ScaleMode defaultScaleMode; // 默认缩放模式
  60. std::string userAgent; // 用户代理
  61. int networkTimeout; // 网络超时(毫秒)
  62. PlayerConfig() {
  63. enableHardwareDecoding = true;
  64. enableAudioOutput = true;
  65. enableVideoOutput = true;
  66. enableSubtitles = true;
  67. maxVideoFrameBuffer = 10;
  68. maxAudioFrameBuffer = 20;
  69. seekAccuracy = 1.0;
  70. autoPlay = false;
  71. defaultVolume = 1.0;
  72. defaultPlayMode = Core::PlayMode::Normal;
  73. defaultScaleMode = Core::ScaleMode::Fit;
  74. userAgent = "AVPlayer2/1.0";
  75. networkTimeout = 30000;
  76. }
  77. };
  78. /**
  79. * 播放器统计信息
  80. */
  81. struct PlayerStats {
  82. std::chrono::microseconds totalDuration; // 总时长
  83. std::chrono::microseconds currentPosition; // 当前位置
  84. double playbackSpeed; // 播放速度
  85. int videoFrameRate; // 视频帧率
  86. int audioSampleRate; // 音频采样率
  87. int videoBitrate; // 视频比特率
  88. int audioBitrate; // 音频比特率
  89. int droppedFrames; // 丢弃帧数
  90. int renderedFrames; // 渲染帧数
  91. double bufferHealth; // 缓冲健康度(0.0-1.0)
  92. std::string videoCodec; // 视频编解码器
  93. std::string audioCodec; // 音频编解码器
  94. PlayerStats() {
  95. totalDuration = std::chrono::microseconds(0);
  96. currentPosition = std::chrono::microseconds(0);
  97. playbackSpeed = 1.0;
  98. videoFrameRate = 0;
  99. audioSampleRate = 0;
  100. videoBitrate = 0;
  101. audioBitrate = 0;
  102. droppedFrames = 0;
  103. renderedFrames = 0;
  104. bufferHealth = 0.0;
  105. }
  106. };
  107. /**
  108. * 播放列表项
  109. */
  110. struct PlaylistItem {
  111. std::string id; // 项目ID
  112. std::string url; // 媒体URL
  113. std::string title; // 标题
  114. std::string artist; // 艺术家
  115. std::string album; // 专辑
  116. std::chrono::microseconds duration; // 时长
  117. std::map<std::string, std::string> metadata; // 元数据
  118. PlaylistItem() {
  119. duration = std::chrono::microseconds(0);
  120. }
  121. };
  122. /**
  123. * 字幕信息
  124. */
  125. struct SubtitleTrack {
  126. int index; // 轨道索引
  127. std::string language; // 语言
  128. std::string title; // 标题
  129. std::string codec; // 编解码器
  130. bool isDefault; // 是否默认
  131. bool isForced; // 是否强制
  132. SubtitleTrack() {
  133. index = -1;
  134. isDefault = false;
  135. isForced = false;
  136. }
  137. };
  138. /**
  139. * 音频轨道信息
  140. */
  141. struct AudioTrack {
  142. int index; // 轨道索引
  143. std::string language; // 语言
  144. std::string title; // 标题
  145. std::string codec; // 编解码器
  146. int channels; // 声道数
  147. int sampleRate; // 采样率
  148. int bitrate; // 比特率
  149. bool isDefault; // 是否默认
  150. AudioTrack() {
  151. index = -1;
  152. channels = 0;
  153. sampleRate = 0;
  154. bitrate = 0;
  155. isDefault = false;
  156. }
  157. };
  158. } // namespace Types
  159. namespace Core {
  160. /**
  161. * 播放器控制器抽象接口
  162. * 提供完整的媒体播放控制功能
  163. */
  164. class IPlayerController {
  165. public:
  166. virtual ~IPlayerController() = default;
  167. /**
  168. * 初始化播放器
  169. * @param config 播放器配置
  170. * @return 是否初始化成功
  171. */
  172. virtual bool initialize(const Types::PlayerConfig& config) = 0;
  173. /**
  174. * 释放播放器资源
  175. */
  176. virtual void release() = 0;
  177. // ========== 媒体加载 ==========
  178. /**
  179. * 加载媒体文件
  180. * @param url 媒体文件URL或路径
  181. * @return 是否开始加载
  182. */
  183. virtual bool loadMedia(const std::string& url) = 0;
  184. /**
  185. * 卸载当前媒体
  186. */
  187. virtual void unloadMedia() = 0;
  188. // ========== 播放控制 ==========
  189. /**
  190. * 开始播放
  191. * @return 是否开始播放
  192. */
  193. virtual bool play() = 0;
  194. /**
  195. * 暂停播放
  196. */
  197. virtual void pause() = 0;
  198. /**
  199. * 停止播放
  200. */
  201. virtual void stop() = 0;
  202. /**
  203. * 跳转到指定位置
  204. * @param position 目标位置(微秒)
  205. * @return 是否开始跳转
  206. */
  207. virtual bool seekTo(std::chrono::microseconds position) = 0;
  208. /**
  209. * 相对跳转
  210. * @param offset 偏移量(微秒,正数向前,负数向后)
  211. * @return 是否开始跳转
  212. */
  213. virtual bool seekBy(std::chrono::microseconds offset) = 0;
  214. // ========== 播放参数 ==========
  215. /**
  216. * 设置播放速度
  217. * @param speed 播放速度(1.0为正常速度)
  218. */
  219. virtual void setPlaybackSpeed(double speed) = 0;
  220. /**
  221. * 获取播放速度
  222. * @return 当前播放速度
  223. */
  224. virtual double getPlaybackSpeed() const = 0;
  225. /**
  226. * 设置音量
  227. * @param volume 音量(0.0-1.0)
  228. */
  229. virtual void setVolume(double volume) = 0;
  230. /**
  231. * 获取音量
  232. * @return 当前音量
  233. */
  234. virtual double getVolume() const = 0;
  235. /**
  236. * 设置静音
  237. * @param muted 是否静音
  238. */
  239. virtual void setMuted(bool muted) = 0;
  240. /**
  241. * 获取静音状态
  242. * @return 是否静音
  243. */
  244. virtual bool isMuted() const = 0;
  245. // ========== 状态查询 ==========
  246. /**
  247. * 获取播放器状态
  248. * @return 当前状态
  249. */
  250. virtual PlayerState getState() const = 0;
  251. /**
  252. * 获取当前播放位置
  253. * @return 当前位置(微秒)
  254. */
  255. virtual std::chrono::microseconds getCurrentPosition() const = 0;
  256. /**
  257. * 获取媒体总时长
  258. * @return 总时长(微秒)
  259. */
  260. virtual std::chrono::microseconds getDuration() const = 0;
  261. /**
  262. * 获取缓冲进度
  263. * @return 缓冲进度(0.0-1.0)
  264. */
  265. virtual double getBufferProgress() const = 0;
  266. /**
  267. * 是否可以播放
  268. * @return 是否可以播放
  269. */
  270. virtual bool canPlay() const = 0;
  271. /**
  272. * 是否可以暂停
  273. * @return 是否可以暂停
  274. */
  275. virtual bool canPause() const = 0;
  276. /**
  277. * 是否可以跳转
  278. * @return 是否可以跳转
  279. */
  280. virtual bool canSeek() const = 0;
  281. // ========== 轨道管理 ==========
  282. /**
  283. * 获取音频轨道列表
  284. * @return 音频轨道列表
  285. */
  286. virtual std::vector<Types::AudioTrack> getAudioTracks() const = 0;
  287. /**
  288. * 获取字幕轨道列表
  289. * @return 字幕轨道列表
  290. */
  291. virtual std::vector<Types::SubtitleTrack> getSubtitleTracks() const = 0;
  292. /**
  293. * 选择音频轨道
  294. * @param index 轨道索引
  295. * @return 是否选择成功
  296. */
  297. virtual bool selectAudioTrack(int index) = 0;
  298. /**
  299. * 选择字幕轨道
  300. * @param index 轨道索引(-1表示关闭字幕)
  301. * @return 是否选择成功
  302. */
  303. virtual bool selectSubtitleTrack(int index) = 0;
  304. /**
  305. * 获取当前音频轨道索引
  306. * @return 当前音频轨道索引
  307. */
  308. virtual int getCurrentAudioTrack() const = 0;
  309. /**
  310. * 获取当前字幕轨道索引
  311. * @return 当前字幕轨道索引
  312. */
  313. virtual int getCurrentSubtitleTrack() const = 0;
  314. // ========== 播放列表 ==========
  315. /**
  316. * 设置播放模式
  317. * @param mode 播放模式
  318. */
  319. virtual void setPlayMode(PlayMode mode) = 0;
  320. /**
  321. * 获取播放模式
  322. * @return 当前播放模式
  323. */
  324. virtual PlayMode getPlayMode() const = 0;
  325. /**
  326. * 添加播放列表项
  327. * @param item 播放列表项
  328. * @return 是否添加成功
  329. */
  330. virtual bool addPlaylistItem(const Types::PlaylistItem& item) = 0;
  331. /**
  332. * 移除播放列表项
  333. * @param index 项目索引
  334. * @return 是否移除成功
  335. */
  336. virtual bool removePlaylistItem(int index) = 0;
  337. /**
  338. * 清空播放列表
  339. */
  340. virtual void clearPlaylist() = 0;
  341. /**
  342. * 获取播放列表
  343. * @return 播放列表
  344. */
  345. virtual std::vector<Types::PlaylistItem> getPlaylist() const = 0;
  346. /**
  347. * 播放指定项目
  348. * @param index 项目索引
  349. * @return 是否开始播放
  350. */
  351. virtual bool playItem(int index) = 0;
  352. /**
  353. * 播放下一项
  354. * @return 是否开始播放
  355. */
  356. virtual bool playNext() = 0;
  357. /**
  358. * 播放上一项
  359. * @return 是否开始播放
  360. */
  361. virtual bool playPrevious() = 0;
  362. /**
  363. * 获取当前播放项索引
  364. * @return 当前播放项索引
  365. */
  366. virtual int getCurrentPlaylistIndex() const = 0;
  367. // ========== 视频控制 ==========
  368. /**
  369. * 设置视频缩放模式
  370. * @param mode 缩放模式
  371. */
  372. virtual void setScaleMode(ScaleMode mode) = 0;
  373. /**
  374. * 获取视频缩放模式
  375. * @return 当前缩放模式
  376. */
  377. virtual ScaleMode getScaleMode() const = 0;
  378. /**
  379. * 设置视频旋转角度
  380. * @param angle 旋转角度(度)
  381. */
  382. virtual void setRotation(int angle) = 0;
  383. /**
  384. * 获取视频旋转角度
  385. * @return 当前旋转角度
  386. */
  387. virtual int getRotation() const = 0;
  388. // ========== 统计信息 ==========
  389. /**
  390. * 获取播放器统计信息
  391. * @return 统计信息
  392. */
  393. virtual Types::PlayerStats getStats() const = 0;
  394. /**
  395. * 重置统计信息
  396. */
  397. virtual void resetStats() = 0;
  398. // ========== 回调设置 ==========
  399. /**
  400. * 设置状态变化回调
  401. * @param callback 状态变化回调函数
  402. */
  403. virtual void setStateChangedCallback(
  404. std::function<void(PlayerState, PlayerState)> callback) = 0;
  405. /**
  406. * 设置位置变化回调
  407. * @param callback 位置变化回调函数
  408. */
  409. virtual void setPositionChangedCallback(
  410. std::function<void(std::chrono::microseconds)> callback) = 0;
  411. /**
  412. * 设置时长变化回调
  413. * @param callback 时长变化回调函数
  414. */
  415. virtual void setDurationChangedCallback(
  416. std::function<void(std::chrono::microseconds)> callback) = 0;
  417. /**
  418. * 设置缓冲进度回调
  419. * @param callback 缓冲进度回调函数
  420. */
  421. virtual void setBufferProgressCallback(
  422. std::function<void(double)> callback) = 0;
  423. /**
  424. * 设置媒体加载完成回调
  425. * @param callback 媒体加载完成回调函数
  426. */
  427. virtual void setMediaLoadedCallback(
  428. std::function<void(const std::string&)> callback) = 0;
  429. /**
  430. * 设置播放完成回调
  431. * @param callback 播放完成回调函数
  432. */
  433. virtual void setPlaybackFinishedCallback(
  434. std::function<void()> callback) = 0;
  435. /**
  436. * 设置错误回调
  437. * @param callback 错误回调函数
  438. */
  439. virtual void setErrorCallback(
  440. std::function<void(const std::string&, const std::string&)> callback) = 0;
  441. };
  442. /**
  443. * 播放器控制器工厂接口
  444. */
  445. class IPlayerControllerFactory {
  446. public:
  447. virtual ~IPlayerControllerFactory() = default;
  448. /**
  449. * 创建播放器控制器实例
  450. * @param name 控制器名称
  451. * @return 播放器控制器智能指针
  452. */
  453. virtual std::unique_ptr<IPlayerController> createPlayerController(const std::string& name = "") = 0;
  454. /**
  455. * 获取默认播放器配置
  456. * @return 默认配置
  457. */
  458. virtual Types::PlayerConfig getDefaultConfig() = 0;
  459. /**
  460. * 获取支持的媒体格式列表
  461. * @return 支持的格式列表
  462. */
  463. virtual std::vector<std::string> getSupportedFormats() = 0;
  464. /**
  465. * 检查是否支持指定格式
  466. * @param format 媒体格式
  467. * @return 是否支持
  468. */
  469. virtual bool isFormatSupported(const std::string& format) = 0;
  470. };
  471. } // namespace Core
  472. } // namespace AVPlayer2
  473. #endif // IPLAYERCONTROLLER_H