QT +FFMPEG4.3 拉取 RTMP/http-flv 流播放 AVFrame转Qimage

devtools/2025/2/9 1:52:56/

QT +FFMPEG4.3 拉取 RTMP/http-flv 流播放

Cc_Video_thread.h
#ifndef CC_VIDEO_THREAD_H
#define CC_VIDEO_THREAD_H#include <QThread>
#include <QAtomicInt>
#include <QImage>#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>#include <libavutil/frame.h>
#include <libavutil/mem.h>
#include <libavutil/dict.h>
#include <libavutil/imgutils.h>#ifdef __cplusplus
}
#endifclass Cc_Video_thread : public QThread
{Q_OBJECT
public:Cc_Video_thread();~Cc_Video_thread();void run() override;/*** @brief open stream and decode* @param addr*/int open(const QString &addr);/*** @brief exit*/void exit();void packer_to_qimage();/*** @brief custom_interrupt_callback* @return*/static int custom_interrupt_callback(void *){//        LOG(ERROR) <<"[ERROR]:OUT TIME..."<<std::endl;return 0;};signals:void sendimage(QImage img);private:QAtomicInt exit_state_ = 1; // thread exit stateQAtomicInt save_state_ = 1; //QAtomicInt open_state_ = -1;AVCodec* codec_;AVPacket* packet_;AVStream * video_st;AVFrame* yuv_frame_;AVFrame* pFrameRGB;AVCodecContext* codecContext;AVFormatContext* format_context_;AVCodecParameters *codecParam;SwsContext* y2r_sws_context_;int video_stream_index_ = 0;int audio_stream_index_ = 0;int video_frame_size = 0 ;int audio_frame_size = 0;int video_frame_count = 0;int audio_frame_count = 0;uint8_t *m_OutBuffer;
};#endif // CC_VIDEO_THREAD_H
Cc_Video_thread.cpp
#include "cc_video_thread.h"#include <string>#include "glog/logging.h"Cc_Video_thread::Cc_Video_thread()
{start();
}Cc_Video_thread::~Cc_Video_thread()
{exit_state_ = 0;av_packet_free(&packet_);av_frame_free(&pFrameRGB);av_frame_free(&yuv_frame_);avformat_close_input(&format_context_);
}int Cc_Video_thread::open(const QString &addr)
{if(open_state_ == 1){return -1;}if(addr.isEmpty()){LOG(ERROR) << "[ERROR] addr is empty... ";return -1;}// init ffmpeg and open streamLOG(INFO) << avcodec_configuration() << std::endl;format_context_ = avformat_alloc_context();format_context_->interrupt_callback.callback=custom_interrupt_callback;format_context_->interrupt_callback.opaque=this;codecContext=avcodec_alloc_context3(nullptr);packet_  = av_packet_alloc();yuv_frame_=av_frame_alloc();pFrameRGB = av_frame_alloc();avformat_network_init(); // init net workAVDictionary *options = nullptr;av_dict_set(&options,"rtsp_transport", "tcp", 0);av_dict_set(&options,"stimeout","10000",0);// 设置“buffer_size”缓存容量av_dict_set(&options, "buffer_size", "1024000", 0);int ret = avformat_open_input(&format_context_,addr.toStdString().c_str(),NULL,&options);if(ret< 0 ){LOG(ERROR) <<"[ERROR]:avformat_open_input FAIL..."<<std::endl;return ret;}//从媒体文件中读包进而获取流消息if(avformat_find_stream_info(format_context_,nullptr)<0){LOG(ERROR) <<"[ERROR]:avformat_find_stream_info FAIL..."<<std::endl;return -1;}//打印av_dump_format(format_context_,0,addr.toStdString().c_str(),0);for(unsigned int i=0;i<format_context_->nb_streams;i++){video_st=format_context_->streams[i];//筛选视频流和音频流if(video_st->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){video_stream_index_=i;}if(video_st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO){audio_stream_index_=i;}}codecParam=format_context_->streams[video_stream_index_]->codecpar;   //获取编解码器的参数集codec_= const_cast<AVCodec*>(avcodec_find_decoder(codecParam->codec_id));   //获取编解码器if(NULL == codec_){LOG(ERROR)<<"获取编解码器 fail";return -1;}codecContext=avcodec_alloc_context3(nullptr);                               //获取编解码上下文avcodec_parameters_to_context(codecContext,codecParam);if( avcodec_open2(codecContext,codec_,nullptr)!=0){avcodec_free_context(&codecContext);LOG(ERROR)<<"Error : can`t open codec";return -1;}//构造一个格式转换上下文y2r_sws_context_=sws_getContext(codecParam->width,codecParam->height,(AVPixelFormat)codecParam->format,codecParam->width,codecParam->height,AV_PIX_FMT_RGB32,SWS_BICUBIC,NULL,NULL,NULL);int bytes = av_image_get_buffer_size(AV_PIX_FMT_RGB32, codecContext->width, codecContext->height,4);m_OutBuffer = (uint8_t *)av_malloc(bytes * sizeof(uint8_t));avpicture_fill((AVPicture *)pFrameRGB, m_OutBuffer, AV_PIX_FMT_RGB32, codecContext->width, codecContext->height);open_state_ = 1;return 1;
}void Cc_Video_thread::run()
{while(true){if(exit_state_ != 1){break;}if(open_state_ == 1){ //6.读取数据包int ret=av_read_frame(format_context_,packet_);if(ret<0)break;char output[1024];if(packet_->stream_index==video_stream_index_){video_frame_size+=packet_->size;memset(output,0,1024);sprintf(output,"recv %5d video frame %5d-%5d\n", ++video_frame_count, packet_->size, video_frame_size);LOG(INFO)  << output;ret =avcodec_send_packet(codecContext,packet_);//送packet中H264数据给解码器码器进行解码,解码好的YUV数据放在pInCodecCtx,if(ret!=0){LOG(ERROR)<<"send packet error code is " <<ret;break;}av_packet_unref(packet_);ret = avcodec_receive_frame(codecContext,yuv_frame_);//把解码好的YUV数据放到pFrame中if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)break;else if (ret < 0) {LOG(ERROR)<<"Error during decoding\n";break;}ret = sws_scale(y2r_sws_context_,yuv_frame_->data,yuv_frame_->linesize,0,codecParam->height,pFrameRGB->data,pFrameRGB->linesize);if(ret <= 0){LOG(ERROR)  << "ERROR to rgb....";}// 转换到QImageQImage tmmImage((uchar *)m_OutBuffer, codecContext->width, codecContext->height, QImage::Format_RGB32);emit sendimage(tmmImage.copy());}if(packet_->stream_index==audio_stream_index_){audio_frame_size+=packet_->size;memset(output,0,1024);sprintf(output,"recv %5d audio frame %5d-%5d\n", ++audio_frame_count, packet_->size, audio_frame_size);LOG(INFO)  << output;}}if(open_state_ != 1){LOG_EVERY_N(INFO, 100) << "thread id is :"<< currentThreadId() <<"open_state_:" <<open_state_<<" run...";msleep(100);}}
}void Cc_Video_thread::exit()
{open_state_ = -1;exit_state_ = 0;
}


http://www.ppmy.cn/devtools/157229.html

相关文章

C基础寒假练习(8)

一、终端输入10个学生成绩&#xff0c;使用冒泡排序对学生成绩从低到高排序 #include <stdio.h> int main(int argc, const char *argv[]) {int arr[10]; // 定义一个长度为10的整型数组&#xff0c;用于存储学生成绩int len sizeof(arr) / sizeof(arr[0]); // 计算数组…

DeepSeek 开源模型全解析(2024.1.1–2025.2.6)

目录 一、通用大语言模型&#xff1a;DeepSeek-V3 系列 137 二、推理优化模型&#xff1a;DeepSeek-R1 系列 811 三、多模态模型&#xff1a;Janus 系列 10 四、生态整合与部署建议 五、总结与展望 以下为 DeepSeek 在 2024 年 1 月至 2025 年 2 月期间发布的开源模型及其…

第十八章 视图

目录 一、概述 二、语法 2.1. 创建视图 2.2. 查询视图 2.3. 修改视图 2.4. 删除视图 2.5. 示例 三、检查选项 3.1. CASCADED&#xff08;级联&#xff09; 3.2. LOCAL&#xff08;本地&#xff09; 四、视图的更新 五、视图作用 5.1. 简单 5.2. 安全 5.3. 数据独…

数据库课程设计基于Java+MySQL+JDBC+JavaSwing的停车场管理系统源代码+数据库,进出车辆登记,车位管理

&#x1f697;停车场管理系统 运用技术 Java语言MySQL数据库JDBCSwing窗口交互 实现效果 用户登录&#xff1a;输入账号密码&#xff0c;验证通过方可进入&#xff0c;否则给出错误提示&#xff0c;拒绝访问 用户注册&#xff1a;提供用户注册功能&#xff0c;输入用户名&am…

鸿蒙 Next 开发实践:使用 WebView 适配移动端网站

在移动应用开发中&#xff0c;有时我们需要将已有的移动端网站嵌入到原生应用中&#xff0c;以实现快速开发和功能扩展。鸿蒙 Next 提供了强大的 WebView 组件&#xff0c;可以轻松实现这一目标。本文将通过一个简单的示例&#xff0c;展示如何在鸿蒙 Next 应用中使用 WebView …

DeepSeek-R1:开源机器人智能控制系统的革命性突破

目录 引言 一、DeepSeek-R1 的概述 1.1 什么是 DeepSeek-R1&#xff1f; 1.2 DeepSeek-R1 的定位 二、DeepSeek-R1 的核心特性 2.1 实时控制能力 2.2 多传感器融合 2.3 路径规划与导航 2.4 人工智能集成 2.5 开源与模块化设计 2.6 跨平台支持 三、DeepSeek-R1 的技术…

开源安全一站式构建!开启企业开源治理新篇章

在如今信息技术日新月异、飞速发展的数字化时代&#xff0c;开源技术如同一股强劲的东风&#xff0c;为企业创新注入了源源不断的活力&#xff0c;然而&#xff0c;正如一枚硬币有正反两面&#xff0c;开源技术的广泛应用亦伴随着不容忽视的挑战。安全风险如影随形&#xff0c;…

Unity3D开发之2019.4.5f1版本IPointerClickHandler Bug

实际代码测试ui物体挂载的脚本里&#xff1a; 如果实现IPointerDownHandler和IPointerClickHandler接口&#xff0c;则会触发OnPointerClick和OnPointerDown函数调用。如果只实现IPointerClickHandler接口&#xff0c;则不会触发OnPointerClick函数调用。如果只实现IPointerDo…