int VideoDecodeModule::Open(std::string strUrl)
{
AVFormatContext *pFormatCtx = nullptr;
AVCodec *pCodec = nullptr;
AVCodecContext* pCodecCtx = nullptr;
AVDictionary *opt = nullptr;
std::string decodeName = "";
AVBufferRef* pBufferRef = nullptr;
int ret = 0;
int videoStream = -1;
char errorbuf[1024] = { 0 };
av_dict_set(&opt, "buffer_size", "1024000", 0); // 缓冲区大小 单位字节 解决高画质模糊的问题
av_dict_set(&opt, "max_delay", "100", 0); // 最大延时 单位微妙
av_dict_set(&opt, "stimeout", "3000000", 0); // 设置超时断开连接时间 3s 单位微妙
av_dict_set(&opt, "rtsp_transport", "tcp", 0); // 以tcp方式打开,如果以udp方式打开将tcp替换为udp
av_dict_set(&opt, "fflags", "nobuffer", 0);
av_dict_set(&opt, "rtbufsize", "6", 0);
av_dict_set(&opt, "start_time_realtime", 0, 0);
if ((ret = avformat_open_input(&pFormatCtx, strUrl.data(), nullptr, &opt)) != 0)
{
av_strerror(ret, errorbuf, sizeof(errorbuf));
return -1;
}
m_spFormatContext = std::shared_ptr<AVFormatContext>(pFormatCtx, [](AVFormatContext* ctx){
avformat_close_input(&ctx);
});
/*if ((ret = avformat_find_stream_info(pFormatCtx, nullptr)) < 0)
{
av_strerror(ret, errorbuf, sizeof(errorbuf));
return -1;
}*/
if ((ret = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, 0)) < 0)
{
av_strerror(ret, errorbuf, sizeof(errorbuf));
return -1;
}
videoStream = ret;
m_video = pFormatCtx->streams[videoStream];
if (m_decodeType == DT_CPU)
ffmpeg解码播放
于 2024-06-04 15:00:13 首次发布