player.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include "idatastruct.h"
  6. #ifdef __cplusplus
  7. extern "C"
  8. {
  9. #endif // __cplusplus
  10. #include <libavcodec/avcodec.h>
  11. #include <libavformat/avformat.h>
  12. #include <libswscale/swscale.h>
  13. #include <libswresample/swresample.h>
  14. #include <libavutil/frame.h>
  15. #include <libavutil/time.h>
  16. #include <libavutil/imgutils.h>
  17. #include <libavutil/mem.h>
  18. #if defined(_WIN32)
  19. #include <SDL.h>
  20. #include <SDL_video.h>
  21. #include <SDL_render.h>
  22. #include <SDL_rect.h>
  23. #include <SDL_mutex.h>
  24. #else
  25. #include <SDL2/SDL.h>
  26. #include <SDL2/SDL_video.h>
  27. #include <SDL2/SDL_render.h>
  28. #include <SDL2/SDL_rect.h>
  29. #include <SDL2/SDL_mutex.h>
  30. #endif
  31. #ifdef __cplusplus
  32. }
  33. #endif // __cplusplus
  34. /* no AV sync correction is done if below the minimum AV sync threshold */
  35. #define AV_SYNC_THRESHOLD_MIN 0.04
  36. /* AV sync correction is done if above the maximum AV sync threshold */
  37. #define AV_SYNC_THRESHOLD_MAX 0.1
  38. /* If a frame duration is longer than this, it will not be duplicated to compensate AV sync */
  39. #define AV_SYNC_FRAMEDUP_THRESHOLD 0.1
  40. /* no AV correction is done if too big error */
  41. #define AV_NOSYNC_THRESHOLD 10.0
  42. /* polls for possible required screen refresh at least this often, should be less than 1/fps */
  43. #define REFRESH_RATE 0.01
  44. #define SDL_AUDIO_BUFFER_SIZE 1024
  45. #define MAX_AUDIO_FRAME_SIZE 192000
  46. #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
  47. #define MIN_FRAMES 25
  48. /* Minimum SDL audio buffer size, in samples. */
  49. #define SDL_AUDIO_MIN_BUFFER_SIZE 512
  50. /* Calculate actual buffer size keeping in mind not cause too frequent audio callbacks */
  51. #define SDL_AUDIO_MAX_CALLBACKS_PER_SEC 30
  52. #define VIDEO_PICTURE_QUEUE_SIZE 3
  53. #define SUBPICTURE_QUEUE_SIZE 16
  54. #define SAMPLE_QUEUE_SIZE 9
  55. #define FRAME_QUEUE_SIZE FFMAX(SAMPLE_QUEUE_SIZE, FFMAX(VIDEO_PICTURE_QUEUE_SIZE, SUBPICTURE_QUEUE_SIZE))
  56. #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
  57. #ifndef RVC_MAX_DISPLAYNUM
  58. #define RVC_MAX_DISPLAYNUM 5
  59. #endif
  60. typedef struct play_clock_s{
  61. double pts; // 当前帧(待播放)显示时间戳,播放后,当前帧变成上一帧
  62. double pts_drift; // 当前帧显示时间戳与当前系统时钟时间的差值
  63. double last_updated; // 当前时钟(如视频时钟)最后一次更新时间,也可称当前时钟时间
  64. double speed; // 时钟速度控制,用于控制播放速度
  65. int serial; // 播放序列,所谓播放序列就是一段连续的播放动作,一个seek操作会启动一段新的播放序列
  66. int paused; // 暂停标志
  67. int *queue_serial; // 指向packet_serial
  68. }play_clock_t;
  69. typedef struct audio_param_s{
  70. int freq;
  71. int channels;
  72. int64_t channel_layout;
  73. enum AVSampleFormat fmt;
  74. int frame_size;
  75. int bytes_per_sec;
  76. }audio_param_t;
  77. typedef struct sdl_video_s{
  78. SDL_Window *window;
  79. SDL_Renderer *renderer;
  80. SDL_Texture *texture;
  81. SDL_Rect rect;
  82. }sdl_video_t;
  83. typedef struct packet_queue_s{
  84. AVPacketList *first_pkt, *last_pkt;
  85. int nb_packets; // 队列中packet的数量
  86. int size; // 队列所占内存空间大小
  87. int64_t duration; // 队列中所有packet总的播放时长
  88. int abort_flag;
  89. int serial; // 播放序列,所谓播放序列就是一段连续的播放动作,一个seek操作会启动一段新的播放序列
  90. SDL_mutex *mutex;
  91. SDL_cond *cond;
  92. }packet_queue_t;
  93. /* Common struct for handling all types of decoded data and allocated render buffers. */
  94. typedef struct frame_s{
  95. AVFrame *frame;
  96. int serial;
  97. double pts; /* presentation timestamp for the frame */
  98. double duration; /* estimated duration of the frame */
  99. int64_t pos; // frame对应的packet在输入文件中的地址偏移
  100. int width;
  101. int height;
  102. int format;
  103. AVRational sar;
  104. int uploaded;
  105. int flip_v;
  106. }frame_t;
  107. typedef struct frame_queue_s{
  108. frame_t queue[FRAME_QUEUE_SIZE];
  109. int rindex; // 读索引。待播放时读取此帧进行播放,播放后此帧成为上一帧
  110. int windex; // 写索引
  111. int size; // 总帧数
  112. int max_size; // 队列可存储最大帧数
  113. int keep_last;
  114. int rindex_shown; // 当前是否有帧在显示
  115. SDL_mutex *frame_mutex;
  116. SDL_cond *frame_cond;
  117. packet_queue_t *pktq; // 指向对应的packet_queue
  118. }frame_queue_t;
  119. typedef struct play_media_callback_s {
  120. void (*cb_play_media_finished)(void* user_data);
  121. void* user_data;
  122. }play_media_callback_t;
  123. typedef enum eMediaType_s {
  124. eAudio_Type,
  125. eVideo_Type,
  126. eImage_Type
  127. }eMediaType_t;
  128. typedef enum eWindType_s {
  129. eVideoSize_Type,
  130. eFullScreen_Type,
  131. eSpecified_Type
  132. }eWindType_t;
  133. typedef struct player_stat_s{
  134. AVFormatContext *p_fmt_ctx[MAX_FILECOUNT];
  135. AVStream *p_audio_stream[MAX_FILECOUNT];
  136. AVStream *p_video_stream[MAX_FILECOUNT];
  137. AVCodecContext *p_acodec_ctx[MAX_FILECOUNT];
  138. AVCodecContext *p_vcodec_ctx[MAX_FILECOUNT];
  139. int audio_idx[MAX_FILECOUNT];
  140. int video_idx[MAX_FILECOUNT];
  141. sdl_video_t sdl_video;
  142. play_clock_t audio_clk; // 音频时钟
  143. play_clock_t video_clk; // 视频时钟
  144. double frame_timer;
  145. packet_queue_t audio_pkt_queue;
  146. packet_queue_t video_pkt_queue;
  147. frame_queue_t audio_frm_queue;
  148. frame_queue_t video_frm_queue;
  149. struct SwsContext *img_convert_ctx[MAX_FILECOUNT];
  150. struct SwrContext *audio_swr_ctx;
  151. AVFrame *p_frm_yuv[MAX_FILECOUNT];
  152. audio_param_t audio_param_src;
  153. audio_param_t audio_param_tgt;
  154. int audio_hw_buf_size; // SDL音频缓冲区大小(单位字节)
  155. uint8_t *p_audio_frm; // 指向待播放的一帧音频数据,指向的数据区将被拷入SDL音频缓冲区。若经过重采样则指向audio_frm_rwr,否则指向frame中的音频
  156. uint8_t *audio_frm_rwr; // 音频重采样的输出缓冲区
  157. unsigned int audio_frm_size; // 待播放的一帧音频数据(audio_buf指向)的大小
  158. unsigned int audio_frm_rwr_size; // 申请到的音频缓冲区audio_frm_rwr的实际尺寸
  159. int audio_cp_index; // 当前音频帧中已拷入SDL音频缓冲区的位置索引(指向第一个待拷贝字节)
  160. int audio_write_buf_size; // 当前音频帧中尚未拷入SDL音频缓冲区的数据量,audio_frm_size = audio_cp_index + audio_write_buf_size
  161. double audio_clock;
  162. int audio_clock_serial;
  163. int abort_request;
  164. int paused;
  165. int step;
  166. bool bread_finished;
  167. SDL_cond *continue_read_thread;
  168. SDL_Thread *read_tid; // demux解复用线程
  169. play_media_callback_t* prvc_cb; // 播放状态回调函数
  170. char* piconpath; // icon图标路径
  171. eMediaType_t eMType; // 媒体类型
  172. eWindType_t eWindType; // 视频框大小类型
  173. volatile uint8_t uVolume; // 音量大小1-128
  174. CMediaHostApi* rvc_hostapi;
  175. char strPlayLists[MAX_FILECOUNT][MAX_PATH]; //播放列表,全路径
  176. uint uFilesCount; //播放文件数
  177. volatile int index; //当前播放文件索引
  178. bool bCirclePlay;
  179. bool bvice_monitor;
  180. int iDisplayCx;
  181. int iDisplayCy;
  182. int iDisplayWidth;
  183. int iDisplayHeight;
  184. int (*on_audio_volume)(int* audiodata, void* user_data);
  185. int (*on_audio_play_finished)(void* user_data);
  186. void* user_data;
  187. }player_stat_t;
  188. typedef struct rvc_media_player_param_s{
  189. char* p_input_file;
  190. eMediaType_t eType;
  191. eWindType_t eWindType;
  192. int idisplaycx;
  193. int idisplaycy;
  194. int idisplaywidth;
  195. int idisplayheight;
  196. bool bvicemonitor;
  197. char strPlayLists[MAX_FILECOUNT][MAX_PATH]; //播放列表,全路径
  198. uint uFilesCount; //播放文件数
  199. play_media_callback_t* cb;
  200. }rvc_media_player_param_t;
  201. double get_clock(play_clock_t *c);
  202. void set_clock_at(play_clock_t *c, double pts, int serial, double time);
  203. void set_clock(play_clock_t *c, double pts, int serial);
  204. class CMediaPlayer
  205. {
  206. public:
  207. CMediaPlayer(CMediaHostApi* pHostApi);
  208. ~CMediaPlayer();
  209. int InitParam(rvc_media_player_param_t* pMedia_Player);
  210. int Initialize_Player_Stat(rvc_media_player_param_t* pMedia_Player);
  211. int SetVolume(uint8_t uVolume);
  212. bool GetPlayingFlag();
  213. void StartMediaPlay();
  214. int StopMediaPlay();
  215. int ExitMediaPlayingThread();
  216. int64_t GetMediaPlayingThreadId();
  217. int GetViceVideoDisplayInfo(int* icx, int* icy, int* iwidth, int* iheight);
  218. uint8_t GetVolume();
  219. bool SetFinishedFlag();
  220. private:
  221. player_stat_t* m_player_stat;
  222. bool m_bplaying;
  223. uint8_t m_uvolume;
  224. CMediaHostApi* m_hostapi;
  225. char* m_piconpath; // ico图标路径
  226. };