av_player.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef AV_PLAYER_H
  2. #define AV_PLAYER_H
  3. #include <QObject>
  4. #include <QSharedPointer>
  5. #include <QSize>
  6. #include <atomic>
  7. #include "av_clock.h"
  8. #include "av_decoder.h"
  9. #include "sonic.h"
  10. extern "C" {
  11. #include <SDL.h>
  12. }
  13. #include "ffmpeg_compat.h"
  14. using AVTool::Decoder;
  15. class VideoFrame;
  16. typedef Decoder::MyFrame MyFrame;
  17. class AVPlayer : public QObject
  18. {
  19. Q_OBJECT
  20. friend void fillAStreamCallback(void* userdata, uint8_t* stream, int len);
  21. public:
  22. enum PlayState { AV_STOPPED, AV_PLAYING, AV_PAUSED };
  23. AVPlayer();
  24. ~AVPlayer();
  25. int play(const QString& url);
  26. void pause(bool isPause);
  27. void clearPlayer();
  28. AVTool::MediaInfo* detectMediaInfo(const QString& url);
  29. AVPlayer::PlayState playState();
  30. void setVFrameSize(const QSize& size)
  31. {
  32. m_imageWidth = size.width();
  33. m_imageHeight = size.height();
  34. }
  35. inline void setVolume(int volumePer)
  36. {
  37. m_volume = (volumePer * SDL_MIX_MAXVOLUME / 100) % (SDL_MIX_MAXVOLUME + 1);
  38. }
  39. inline void seekBy(int32_t time_s) { seekTo((int32_t) m_audioClock.getClock() + time_s); }
  40. inline void seekTo(int32_t time_s)
  41. {
  42. if (playState() == AV_STOPPED)
  43. return;
  44. if (time_s < 0)
  45. time_s = 0;
  46. m_decoder->seekTo(time_s, time_s - (int32_t) m_audioClock.getClock());
  47. }
  48. inline uint32_t avDuration() { return m_duration; }
  49. inline void setSpeed(float speed) { m_playSpeed = speed; }
  50. signals:
  51. void frameChanged(QSharedPointer<VideoFrame> frame);
  52. void AVTerminate();
  53. void AVPtsChanged(unsigned int pts);
  54. void AVDurationChanged(unsigned int duration);
  55. private:
  56. int initSDL();
  57. int initVideo();
  58. void videoCallback(std::shared_ptr<void> par);
  59. double computeTargetDelay(double delay);
  60. double vpDuration(MyFrame* lastFrame, MyFrame* curFrame);
  61. void displayImage(AVFrame* frame);
  62. void initAVClock();
  63. private:
  64. //解码器实例
  65. Decoder* m_decoder;
  66. AVFormatContext* m_fmtCtx;
  67. AVCodecParameters* m_audioCodecPar;
  68. SwrContext* m_swrCtx;
  69. uint8_t* m_audioBuf;
  70. sonicStream m_sonicStream;
  71. uint32_t m_audioBufSize;
  72. uint32_t m_audioBufIndex;
  73. uint32_t m_duration;
  74. uint32_t m_lastAudPts;
  75. enum AVSampleFormat m_targetSampleFmt;
  76. //记录音视频帧最新播放帧的时间戳,用于同步
  77. // double m_audioPts;
  78. // double m_videoPts;
  79. double m_frameTimer;
  80. std::atomic<float> m_playSpeed;
  81. //延时时间
  82. double m_delay;
  83. //音频播放时钟
  84. AVClock m_audioClock;
  85. //视频播放时钟
  86. AVClock m_videoClock;
  87. int m_targetChannels;
  88. int m_targetFreq;
  89. int m_targetChannelLayout;
  90. int m_targetNbSamples;
  91. int m_volume;
  92. //同步时钟初始化标志,音视频异步线程
  93. //谁先读到初始标志位就由谁初始化时钟
  94. int m_clockInitFlag;
  95. int m_audioIndex;
  96. int m_videoIndex;
  97. int m_imageWidth;
  98. int m_imageHeight;
  99. //终止标志
  100. std::atomic<int> m_exit;
  101. //记录暂停前的时间
  102. double m_pauseTime;
  103. //暂停标志
  104. std::atomic<int> m_pause;
  105. AVFrame* m_audioFrame;
  106. AVCodecParameters* m_videoCodecPar;
  107. enum AVPixelFormat m_dstPixFmt;
  108. int m_swsFlags;
  109. SwsContext* m_swsCtx;
  110. uint8_t* m_buffer;
  111. uint8_t* m_pixels[4];
  112. int m_pitch[4];
  113. };
  114. #endif