FFmpeg+Qt实现摄像头(rtsp)实时显示视频

由于项目需要实时显示摄像头的图像,就学习了FFmpeg的相关知识。
执行run()函数
打开tcp或udp地址
能否读取流
查找解码器
打开解码器
将解码后数据转换成RGB32
在Qwidget中显示
释放内存
结束程序

网络摄像机rtsp地址详解。

流程框架

FFmpeg+Qt实现摄像头(rtsp)实时显示视频_第1张图片

程序源代码

/*.CPP*/
/****************************************
 * File name:Camera_page
 * 爱捣蛋的小仙儿。
*****************************************/
#include "Camera_page.h"
#include 
#include
#include
#include 
#include 

extern "C"
{
    #include "libavcodec/avcodec.h"
    #include "libavformat/avformat.h"
    #include "libavutil/pixfmt.h"
    #include "libswscale/swscale.h"
    #include "libavutil/time.h"
}
using namespace std;
VideoPlayer::VideoPlayer()
{

}
VideoPlayer::~VideoPlayer()
{

}
/*线程*/
void VideoPlayer::startPlay()
{
 //调用 QThread 的start函数 将会自动执行下面的run函数 run函数是一个新的线程
    this->start();
}
/*线程*/
void VideoPlayer::run()
{
    /*定义结构体 调用其成员函数*/
    //输入数据缓存,视音频流个数 视音频流 文件名 时长 比特率 解封装等
    AVFormatContext *pFormatCtx;
    AVCodecContext *pCodecCtx;
    AVCodec *pCodec;/*存储解码器信息*/
    AVFrame *pFrame, *pFrameRGB;/*存储解码器信息*/
    AVPacket *packet;/*数据包*/
   static uint8_t *out_buffer;

    /*处理图片像素数据 图片像素格式转换 图片拉伸等 */
    static struct SwsContext *img_convert_ctx;
         /*视频流*/       /*图像*/
    int videoStream, i, numBytes;
      /*解码*/   /*解码成功*/
    int ret, got_picture;

    avformat_network_init();//初始化FFmpeg网络模块
    av_register_all();//初始化FFMPEG  调用了这个才能正常适用编码器和解码器(弃用函数)


    pFormatCtx = avformat_alloc_context();//初始化内存

    //AVDictionary是FFmpeg的键值对存储工具,FFmpeg经常使用AVDictionary设置/读取内部参数
    AVDictionary *avdic=NULL;
    char option_key[]="rtsp_transport";
    char  m_bTcp;
    av_dict_set(&avdic,option_key,m_bTcp ? "udp" : "tcp",0);
    char option_key2[]="stimeout";
    char option_value2[]="3000000";
    av_dict_set(&avdic, "buffer_size", "1024000", 0);                 //画质优化
    av_dict_set(&avdic,option_key2,option_value2,0);
    char url[]="rtsp://admin:[email protected]:8557/h264";/*网络摄像头的数据*/
    /*avformat_open_input函数*/
    //参数一:指向用户提供的AVFormatContext(由avformat_alloc_context分配)的指针。
    //参数二:要打开的流的url
    //参数三:fmt如果非空,则此参数强制使用特定的输入格式。否则将自动检测格式。
    //参数四:包含AVFormatContext和demuxer私有选项的字典。返回时,此参数将被销毁并替换为包含找不到的选项
    if (avformat_open_input(&pFormatCtx, url, NULL, &avdic) != 0)    //打开多媒体并获取信息
    {
        printf("can't open the file. \n");
        return;
    }
    if(avdic != NULL)
    {
        av_dict_free(&avdic);
    }

    //获取视频流信息
    /*avformat_find_stream_info函数*/
    //参数一:媒体文件上下文。
    //参数二:字典,一些配置选项。      /*媒体句柄*/
    if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
    {
        printf("Could't find stream infomation.\n");
        return;
    }
    videoStream = -1;/*无视频流*/
    //循环查找视频中包含的流信息,直到找到视频类型的流
    /* pFormatCtx函数*/
    //unsigned int nb_streams    当前的流数量
    //AVStream **streams;  指针数组 视频流和语音流*/
    for (i = 0; i < pFormatCtx->nb_streams; i++)
    {
        if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)//codec弃用函数
        {
            videoStream = i;
        }
    }
    //如果videoStream为-1 说明没有找到视频流
    if (videoStream == -1)
    {
        printf("Didn't find a video stream.\n");
        return;
    }

    //打印流信息
    //注意:最后一个参数填0,打印输入流;最后一个参数填1,打印输出流
    av_dump_format(pFormatCtx, 0, url,0);

    //查找解码器,获取指向视频流的编解码器上下文的指针
    pCodecCtx = pFormatCtx->streams[videoStream]->codec;
    //通过解封装之后从avstream结构体里获取CodecID(指定格式流)
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    
    //设置编码器参数(不同参数对视频编质量或大小的影响)
    pCodecCtx->bit_rate =0;   //初始化为0   比特率
    pCodecCtx->time_base.num=1;  //下面两行:一秒钟25帧
    pCodecCtx->time_base.den=25;
    pCodecCtx->frame_number=1;   //每包一个视频帧

    /*编码器如果等于NULL 编码器没有找到*/
    if (pCodec == NULL)
    {
        printf("Codec not found.\n");
        return;
    }

    //打开解码器
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
    {
        printf("Could not open codec.\n");
        return;
    }

    pFrame = av_frame_alloc();    //创建  存储解码器信息*/
    pFrameRGB = av_frame_alloc(); //创建  存储解码器信息*/

                                  //解码后的h264数据转换成RGB32
    img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
                 pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
                        AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);


    //图像的像素格式  图像的像素宽度  图像的像素高度(计算这个格式的图片,需要多少字节来存储)
    numBytes = avpicture_get_size(AV_PIX_FMT_RGB32, pCodecCtx->width,pCodecCtx->height);//(弃用函数)
    qDebug() << numBytes;        //需要多少字节来存储

     out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
     /*瓜分分配的空间*/
     //瓜分上一步分配到的buffer.
     av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, out_buffer, AV_PIX_FMT_RGB32, pCodecCtx->width, pCodecCtx->height, 1);


    int y_size = pCodecCtx->width * pCodecCtx->height;
    packet = (AVPacket *) malloc(sizeof(AVPacket)); //申请一个视频帧包的大小
    av_new_packet(packet, y_size); //分配packet的数据,为packet分配一个指定大小的内存

     int as = 0;
    while (1)
    {
        //av_read_frame
        //返回流的下一帧。此函数返回存储在文件中的内容,不对有效的帧进行验证。获取存储在文件中的帧中,
        //并为每个调用返回一个。不会的省略有效帧之间的无效数据,以便给解码器最大可用于解码的信息。
        //返回0是成功,小于0则是错误,大于0则是文件末尾,所以大于等于0是返回成功
        //每解码一个视频帧,需要先调用 av_read_frame()获得一帧视频的压缩数据,然后才能对该数据进行解码
        if (av_read_frame(pFormatCtx, packet) <  0)
        {
           qDebug("a == %d\n",++as);

           if(as == 4)
           {
           qDebug(" 连接异常结束\n");
          thread()->terminate();
          thread()->wait();
          this->terminate();
          this->wait();
           }
           continue;  
        }

          if(as != 0)
          {
              as = 0;
          }
        if (packet->stream_index == videoStream)
        {
           ret = avcodec_send_packet(pCodecCtx, packet);                    //发送数据到ffmepg,放到解码队列中
           got_picture = avcodec_receive_frame(pCodecCtx, pFrame);          //将成功的解码队列中取出1个frame

            if (ret < 0)
            {
                usleep(1000);
                printf("decode error.\n");
                continue;
            }

            if (!got_picture)
            {
                 //颜色空间转换,最后输出到out_buffer
                sws_scale(img_convert_ctx,(uint8_t const * const *) pFrame->data,
                        pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data,
                        pFrameRGB->linesize);//sws_scale库可以在一个函数里面同时实现:1.图像色彩空间转换;2.分辨率缩放;3.前后图像滤波处理。

                //把这个RGB数据 用QImage加载
                QImage tmpImg((uchar *)out_buffer,pCodecCtx->width,pCodecCtx->height,QImage::Format_RGB32);
                QImage image = tmpImg.copy(); //把图像复制一份 传递给界面显示
                emit sig_GetOneFrame(image);  //发送信号
             }
         }

        //释放一个包。
        av_free_packet(packet); //释放资源,否则内存会一直上升(弃用函数)
        av_packet_unref(packet);
        memset(out_buffer,0,sizeof(out_buffer));
    }
    av_free(out_buffer);
    av_free(pFrameRGB);
    avcodec_close(pCodecCtx);//关闭给定的avcodeContext并释放与之关联的所有数据
    if(NULL != pCodecCtx){
        avcodec_free_context(&pCodecCtx);
        avdic = NULL;
    }
    if(NULL != pFormatCtx){
    avformat_close_input(&pFormatCtx);//关闭打开的输入pFormatCtx。释放它和它的所有内容并设置为空。
    pFormatCtx = NULL;
    }

}

/*.h*/
/****************************************
 * File name:Camera_page.h
 * 爱捣蛋的小仙儿。
*****************************************/
#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H
#include 
#include 
extern "C"
{
#include "libavutil/imgutils.h"
}
class VlcInstance;
class VlcMedia;
class VlcMediaPlayer;

class VideoPlayer :public QThread
{
     Q_OBJECT
public:
    explicit VideoPlayer();
    ~VideoPlayer();
   const static uint32_t dwLastFrameRealtime = 0;
    void startPlay();//线程开始
    //int interrupt_cb(void *ctx);

signals:
    void sig_GetOneFrame(QImage); //每获取到一帧图像 就发送此信号

protected:
    void run();

private:
    QString mFileName;


    VlcInstance *_instance;
    VlcMedia *_media;
    VlcMediaPlayer *_player;
};

#endif // VIDEOPLAYER_H

源码:ffmpeg_Qt_camera.zip

希望看到此文章的小伙伴们,如果有什么错误,欢迎下方留言,博客肯定虚心接受并改正!大家一起共同进步。如果对你有所帮助,可以给博主一个小小的赞。 谢谢❥(^_-)

你可能感兴趣的:(FFmpeg,Linux,Qt,ffmpeg,qt,rtsp,linux)