player.h 9.8 KB

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