player.h 7.4 KB

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