Qt实战--视频引擎接口类HVideoPlayer

HVideoPlayer

接口功能

  • 设置媒体源HMedia
  • 开始start、停止stop、暂停pause、继续resume
  • 视频帧HFrame的push、pop

定义代码

#ifndef HVIDEOPLAYER_H
#define HVIDEOPLAYER_H

#include "hmedia.h"
#include "hframe.h"

class HVideoPlayer
{
public:
    HVideoPlayer(){}
    virtual ~HVideoPlayer() {}

    virtual int start() = 0;
    virtual int stop() = 0;
    virtual int pause() = 0;
    virtual int resume() = 0;

    void set_media(HMedia& media){
        this->media = media;
    }

    FrameStats get_frame_stats(){
        return frame_buf.frame_stats;
    }

    FrameInfo get_frame_info(){
        return frame_buf.frame_info;
    }

    void set_frame_cache(int cache){
        frame_buf.setCache(cache);
    }

    int push_frame(HFrame* pFrame){
        return frame_buf.push(pFrame);
    }

    int pop_frame(HFrame* pFrame){
        return frame_buf.pop(pFrame);
    }

protected:
    HMedia      media;
    HFrameBuf   frame_buf;
};

#endif // HVIDEOPLAYER_H

HMedia

#ifndef HMEDIA_H
#define HMEDIA_H

#include <string>

enum MEDIA_TYPE{
    MEDIA_TYPE_FILE = 0,
    MEDIA_TYPE_NETWORK,
    MEDIA_TYPE_CAPTURE,

    MEDIA_TYPE_NONE
};

#define DEFAULT_MEDIA_TYPE  MEDIA_TYPE_CAPTURE

typedef struct media_s{
    MEDIA_TYPE  type;
    int         index; // for camera index
    std::string src;
    std::string descr;

    media_s(){
        type = DEFAULT_MEDIA_TYPE;
        index = 0;
    }
}HMedia;

#endif // HMEDIA_H

HFrame

typedef struct hframe_s{
    hbuf_t buf;
    int w;
    int h;
    int type;
    int bpp;
    uint64 ts;
    void* userdata;
}HFrame;

HFrameBuf

class HFrameBuf : public HRingBuf{
public:
    enum CacheFullPolicy{
        SQUEEZE,
        DISCARD,       
    }policy;
    
    HFrameBuf() : HRingBuf() {
        cache_num = DEFAULT_FRAME_CACHENUM;
        policy = SQUEEZE;
    }

    void setCache(int num) {cache_num = num;}
    void setPolicy(CacheFullPolicy policy) {this->policy = policy;}

    int push(HFrame* pFrame);
    int pop(HFrame* pFrame);

    int         cache_num;
    FrameStats  frame_stats;
    FrameInfo   frame_info;
    std::deque<HFrame> frames;
};

HFrameBuf帧缓存类继承自环形缓存类HRingBuf,由缓存的帧数决定环形缓存区的大小;
push接口从环形缓存区分配内存,从HFrame拷贝数据,并记录在双端队列std::deque<HFrame> frames;中;
pop接口从双端队列std::deque<HFrame> frames;中取出一帧,拷贝数据至HFrame;

HBuf、HRingBuf

关于缓存类HBuf和环形缓存类HRingBuf的定义实现请自行研究Github源码,
你将体会到C内存管理的灵活和高效之处。

交互图

我们用graphviz简单画下HVideoPlayer视频播放引擎类和HVideoWidget视频播放控件类的交互过程:

HVideoPlayer

下一节我们给出基于OpenCV中VideoCapture类实现的播放引擎实现类HVideoCapture

发布了128 篇原创文章 · 获赞 142 · 访问量 28万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览