设为首页 加入收藏

TOP

FFmpeg4.0笔记:封装ffmpeg的解封装功能类CDemux(一)
2019-07-06 16:10:40 】 浏览:121
Tags:FFmpeg4.0 笔记 封装 ffmpeg 功能 CDemux

Github

https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff

CDemux.h

/*******************************************************************
*  Copyright(c) 2019
*  All rights reserved.
*
*  文件名称:    CDemux.h
*  简要描述:    解封装
*
*  作者:  gongluck
*  说明:
*
*******************************************************************/

#ifndef __CDEMUX_H__
#define __CDEMUX_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>

#ifdef __cplusplus
}
#endif

#include <string>
#include <mutex>
#include <thread>

class CDemux
{
public:
    virtual ~CDemux();
    // 状态
    enum STATUS { STOP, DEMUXING };
    // 状态通知回调声明
    typedef void (*DemuxStatusCallback)(STATUS status, const std::string& err, void* param);
    // 解封装帧回调声明
    typedef void (*DemuxPacketCallback)(const AVPacket* packet, int64_t timestamp, void* param);

    // 设置输入
    bool set_input(const std::string& input, std::string& err);
    // 获取输入
    const std::string& get_input(std::string& err);

    // 设置解封装帧回调 
    bool set_demux_callback(DemuxPacketCallback cb, void* param, std::string& err);
    // 设置解封装状态变化回调
    bool set_demux_status_callback(DemuxStatusCallback cb, void* param, std::string& err);

    // 打开输入
    bool openinput(std::string& err);
    // 开始解封装
    bool begindemux(std::string& err);
    // 停止解封装
    bool stopdemux(std::string& err);

    // 获取流索引
    int get_steam_index(AVMediaType type, std::string& err);
    // 获取流参数
    const AVCodecParameters* get_steam_par(int index, std::string& err);

    // 跳转到指定秒
    bool seek(int64_t timestamp, int index, int flags, std::string& err);

    // 启用设备采集
    bool device_register_all(std::string& err);
    // 设置输入格式
    bool set_input_format(const std::string& fmt, std::string& err);
    // 设置附加参数
    bool set_dic_opt(const std::string& key, const std::string& value, std::string& err);
    // 清理设置
    bool free_opt(std::string& err);

    // 设置bsf名称,影响回调的packet数据能否直接播放
    bool set_bsf_name(const std::string& bsf, std::string& err);

private:
    // 解封装线程
    bool demuxthread();

private:
    STATUS status_ = STOP;
    std::recursive_mutex mutex_;

    std::string input_;
    std::thread demuxth_;

    DemuxStatusCallback demuxstatuscb_ = nullptr;
    void* demuxstatuscbparam_ = nullptr;
    DemuxPacketCallback demuxpacketcb_ = nullptr;
    void* demuxpacketcbparam_ = nullptr;

    //ffmpeg
    AVFormatContext* fmtctx_ = nullptr;
    AVInputFormat* fmt_ = nullptr;
    AVDictionary* dic_ = nullptr;
    std::string bsfname_;
};

#endif//__CDEMUX_H__

CDemux.cpp

/*******************************************************************
*  Copyright(c) 2019
*  All rights reserved.
*
*  文件名称:    CDemux.cpp
*  简要描述:    解封装
*
*  作者:  gongluck
*  说明:
*
*******************************************************************/

#include "common.h"
#include "CDemux.h"

CDemux::~CDemux()
{
    std::string err;
    stopdemux(err);
    free_opt(err);
}

bool CDemux::set_input(const std::string& input, std::string& err)
{
    LOCK();
    CHECKSTOP(err);
    err = "opt succeed.";

    if (input.empty())
    {
        err = "input is empty.";
        return false;
    }
    else
    {
        input_ = input;
        return true;
    }
}

const std::string& CDemux::get_input(std::string& err)
{
    LOCK();
    err = "opt succeed.&quo
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇QDomDocument 读取和编辑xml文件 下一篇洛古最简单50题解(11-20)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目