25#ifndef __BTK_MEDIAPLAYER_H__
26#define __BTK_MEDIAPLAYER_H__
30 #include <libavformat/avformat.h>
31 #include <libswscale/swscale.h>
32 #include <libswresample/swresample.h>
33 #include <libavutil/opt.h>
36#include <opencv2/opencv.hpp>
37#include <boost/thread.hpp>
38#include <boost/array.hpp>
39#include <boost/circular_buffer.hpp>
46#define MAX_OUT_BUFFER 262144
98 static uint8_t* FLUSH_DATA;
101 static uint8_t* QUIT_DATA;
104 std::queue<AVPacket*> mQueue;
106 boost::condition_variable mCond;
112 PacketQueue(
size_t capacity);
113 virtual ~PacketQueue();
128 bool Push(AVPacket* packet);
132 void Flush(AVPacket* packet);
152 int64_t mInitialTime;
172 void Reset(int64_t initialTime);
189 static int64_t GetSysTime();
197 std::unique_ptr<char> data;
202 AudioFrame(
const AudioFrame&);
203 AudioFrame(AudioFrame&&);
204 AudioFrame& operator=(AudioFrame&&);
209 static std::list<MediaPlayer*> players;
212 static bool globallyPaused;
216 AVIOContext* mIoContext;
217 AVFormatContext* mContext;
219 AVCodecContext* mVideoCtx;
220 AVCodecContext* mAudioCtx;
227 int mFinalW, mFinalH;
229 int mSourceW, mSourceH;
233 boost::mutex mSizeMutex;
251 bool mSeekInProgressAudio;
252 bool mSeekInProgressVideo;
253 bool mSeekCompletedAudio;
258 cv::Mat mFrameBuffer;
259 cv::Mat mFrameRetrieveBuffer;
261 SwsContext* mScalerCtx;
262 AVFrame* mVideoBuffer;
265 boost::thread mOpenThread;
268 boost::thread mAudioThread;
269 boost::mutex mAudioMutex;
270 PacketQueue mAudioPktQ;
271 boost::circular_buffer<AudioFrame> mAudioFrameQ;
274 SwrContext* mResampleCtx;
275 uint64_t mOutChannelLayout;
276 AVSampleFormat mOutSampleFormat;
279 PacketQueue mVideoPktQ;
281 boost::thread mDecodingThread;
282 boost::condition_variable mPauseCond;
285 boost::thread mVideoThread;
286 boost::mutex mVideoMutex;
319 void Open(std::string path);
371 void GetSize(
int& width,
int& height);
383 void SetSize(
int width,
int height);
394 void Play(
long startPosition = 0);
423 bool GetFrame(ObjBitmap* scolBitmap);
455 std::vector<std::string>
ListStreams(AVMediaType type);
461 static int FileRead(
void* player, uint8_t* buf,
int buf_size);
462 static int FileWrite(
void* player, uint8_t* buf,
int buf_size);
463 static int64_t FileSeek(
void* player, int64_t offset,
int whence);
471 int SetStream(AVMediaType type,
int index = -1);
478 int GetActualStreamIndex(
int streamIndex, AVMediaType streamType);
480 long GetStreamDuration(
int index);
488 void SendQuitPacket();
490 void OpenFileThread(std::string path);
491 void OpenUrlThread(std::string url,
bool close);
493 void DecodingThread();
497 void ComputeSize(
int sourceW,
int sourceH);
504 void SetAudioFormat(uint64_t channelLayout, AVSampleFormat format,
int sampleRate);
505 void AllocResampler(uint64_t outChannelLayout, AVSampleFormat outSampleFormat,
int outSampleRate, AVCodecContext* context);
507 int64_t SynchronizeVideo(AVFrame *srcFrame, int64_t pts);
511 void SynchronizeAudio(int64_t pts);