diff --git a/FFmpegDemo/FFmpegDemo.assets/VideoPlayHW-tuya.png b/FFmpegDemo/FFmpegDemo.assets/VideoPlayHW-tuya.png index db6a0892f0fd5d4fe65b6c534c36fe585b243fb8..ff9515d631021ae0edc0f8fa3ff4c18229800c00 100644 Binary files a/FFmpegDemo/FFmpegDemo.assets/VideoPlayHW-tuya.png and b/FFmpegDemo/FFmpegDemo.assets/VideoPlayHW-tuya.png differ diff --git a/FFmpegDemo/FFmpegDemo.assets/image-20221015204308041.png b/FFmpegDemo/FFmpegDemo.assets/image-20221015204308041.png new file mode 100644 index 0000000000000000000000000000000000000000..4d873236e6c8c79196f55a327654eacd7c1ee625 Binary files /dev/null and b/FFmpegDemo/FFmpegDemo.assets/image-20221015204308041.png differ diff --git a/FFmpegDemo/FFmpegDemo.assets/image-20221017232820037.png b/FFmpegDemo/FFmpegDemo.assets/image-20221017232820037.png new file mode 100644 index 0000000000000000000000000000000000000000..e1cf3826105cb0acbaadbc5d405f2bcc3d6ad54e Binary files /dev/null and b/FFmpegDemo/FFmpegDemo.assets/image-20221017232820037.png differ diff --git a/FFmpegDemo/FFmpegDemo.md b/FFmpegDemo/FFmpegDemo.md index a653ee393fd26692265cc27925c981f81ccff1fb..971fbc20bf209ae8423618c3038f92d1357a35fe 100644 --- a/FFmpegDemo/FFmpegDemo.md +++ b/FFmpegDemo/FFmpegDemo.md @@ -6,10 +6,12 @@ ## 1、说明 -| 类名 | 功能 | -| ----------- | ---------------------------------------------- | -| VideoPlay | 使用ffmpeg音视频库【软解码】实现的视频播放器; | -| VideoPlayHW | 使用ffmpeg音视频库【硬解码】实现的视频播放器; | +| 类名 | 功能 | +| ------------ | ------------------------------------------------------------ | +| VideoPlay | 使用ffmpeg音视频库【软解码】实现的视频播放器; | +| VideoPlayGL1 | 使用ffmpeg音视频库【软解码 + OpenGL显示RGB图像】实现的视频播放器; | +| VideoPlayGL2 | 使用ffmpeg音视频库【软解码 + OpenGL显示YUV图像】实现的视频播放器; | +| VideoPlayHW | 使用ffmpeg音视频库【硬解码】实现的视频播放器; | @@ -34,7 +36,44 @@ ![VideoPlay-tuya](FFmpegDemo.assets/VideoPlay-tuya.gif) -### 1.2 VideoPlayHW + + +### 1.2 VideoPlayGL1 + +> 1. 使用ffmpeg音视频库【软解码】实现的视频播放器; +> 2. 支持打开本地视频文件(如mp4、mov、avi等)、网络视频流(rtsp、rtmp、http等); +> 3. 支持视频匀速播放; +> 4. 采用【OpenGL显示RGB】图像,支持自适应窗口缩放,支持使用QOpenGLWidget、QOpenGLWindow显 +> 5. 视频播放支持实时开始/关闭、暂停/继续播放; +> 6. 视频解码、线程控制、显示各部分功能分离,低耦合度。 +> 7. 采用最新的5.1.2版本ffmpeg库进行开发,超详细注释信息,将所有踩过的坑、解决办法、注意事项都得很写清楚。 +> + +* 下图中使用OpenGL显示RGB图像CPU占用率是使用QPainter显示的一半,由于我使用的是非常老的笔记本的集显测试,所以GPU占用率比较高。 + +![image-20221015204308041](FFmpegDemo.assets/image-20221015204308041.png) + + + +### 1.3 VideoPlayGL2 + +> 1. 使用ffmpeg音视频库【软解码】实现的视频播放器; +> 2. 支持打开本地视频文件(如mp4、mov、avi等)、网络视频流(rtsp、rtmp、http等); +> 3. 支持视频匀速播放; +> 4. 采用【OpenGL显示YUV】图像,支持自适应窗口缩放,支持使用QOpenGLWidget、QOpenGLWindow显示; +> 5. 将YUV转RGB的步骤由CPU转换改为使用GPU转换,降低CPU占用率; +> 6. 视频播放支持实时开始/关闭、暂停/继续播放; +> 7. 视频解码、线程控制、显示各部分功能分离,低耦合度。 +> 8. 采用最新的5.1.2版本ffmpeg库进行开发,超详细注释信息,将所有踩过的坑、解决办法、注意事项都得很写清楚。 +> + +* 下图中使用OpenGL显示YUV图像CPU占用率是使用QPainter显示的1/3左右,由于我使用的是非常老的笔记本的集显测试,所以GPU占用率比较高。 + +![image-20221017232820037](FFmpegDemo.assets/image-20221017232820037.png) + + + +### 1.4 VideoPlayHW > 1. 使用ffmpeg音视频库【硬解码】实现的视频播放器,采用GPU解码, 大幅降低对CPU的暂用率; > 2. 支持打开本地视频文件(如mp4、mov、avi等)、网络视频流(rtsp、rtmp、http等); diff --git a/FFmpegDemo/FFmpegDemo.pro b/FFmpegDemo/FFmpegDemo.pro index a53eb892bde60ee41ef9602eff7bdc316b026284..25c81c9ee51290f72ab21e444db31f8937b16b18 100644 --- a/FFmpegDemo/FFmpegDemo.pro +++ b/FFmpegDemo/FFmpegDemo.pro @@ -11,4 +11,6 @@ TEMPLATE = subdirs SUBDIRS += VideoPlay # 使用软解码实现的视频播放器 +SUBDIRS += VideoPlayGL1 # 使用软解码实现的视频播放器 使用OpenGL显示RGB图像 +SUBDIRS += VideoPlayGL2 # 使用软解码实现的视频播放器 使用OpenGL显示YUV图像 SUBDIRS += VideoPlayHW # 使用硬件解码实现的视频播放器 diff --git a/FFmpegDemo/VideoPlay/widget.cpp b/FFmpegDemo/VideoPlay/widget.cpp index fe31aada0720997c5a93996f8d4c8247c0a00db8..a7409f418c384213b9c71e485389245b853ce563 100644 --- a/FFmpegDemo/VideoPlay/widget.cpp +++ b/FFmpegDemo/VideoPlay/widget.cpp @@ -87,6 +87,6 @@ void Widget::on_playState(ReadThread::PlayState state) { ui->but_open->setText("开始播放"); ui->but_pause->setText("暂停"); - this->setWindowTitle(QString("Qt+ffmpeg视频播放Demo V%1").arg(APP_VERSION)); + this->setWindowTitle(QString("Qt+ffmpeg视频播放(软解码)Demo V%1").arg(APP_VERSION)); } } diff --git a/FFmpegDemo/VideoPlayGL1/VideoPlay/VideoPlay.pri b/FFmpegDemo/VideoPlayGL1/VideoPlay/VideoPlay.pri new file mode 100644 index 0000000000000000000000000000000000000000..a1396dcab6ecacbbc5444ada65b5bbb2412fbd2e --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/VideoPlay/VideoPlay.pri @@ -0,0 +1,31 @@ +#--------------------------------------------------------------------------------------- +# @功能: ffmpeg读取视频图像模块 +# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit(也支持其它编译器) +# @Qt IDE: D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator +# +# @开发者 mhf +# @邮箱 1603291350@qq.com +# @时间 2022-09-15 14:27:25 +# @备注 +#--------------------------------------------------------------------------------------- + +# 加载库,ffmpeg n5.1.2版本 +win32{ +LIBS += -LE:/lib/ffmpeg5-1-2/lib/ -lavcodec -lavfilter -lavformat -lswscale -lavutil -lswresample -lavdevice +INCLUDEPATH += E:/lib/ffmpeg5-1-2/include +DEPENDPATH += E:/lib/ffmpeg5-1-2/include +} + +unix:!macx{ +LIBS += -L/home/mhf/lib/ffmpeg/ffmpeg-5-1-2/lib -lavcodec -lavfilter -lavformat -lswscale -lavutil -lswresample -lavdevice +INCLUDEPATH += /home/mhf/lib/ffmpeg/ffmpeg-5-1-2/include +DEPENDPATH += /home/mhf/lib/ffmpeg/ffmpeg-5-1-2/include +} + +HEADERS += \ + $$PWD/readthread.h \ + $$PWD/videodecode.h + +SOURCES += \ + $$PWD/readthread.cpp \ + $$PWD/videodecode.cpp diff --git a/FFmpegDemo/VideoPlayGL1/VideoPlay/readthread.cpp b/FFmpegDemo/VideoPlayGL1/VideoPlay/readthread.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b7ed79826f7733c26e42db17069417623d3d871 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/VideoPlay/readthread.cpp @@ -0,0 +1,125 @@ +#include "readthread.h" +#include "videodecode.h" + +#include +#include +#include +#include +#include + +ReadThread::ReadThread(QObject *parent) : QThread(parent) +{ + m_videoDecode = new VideoDecode(); + + qRegisterMetaType("PlayState"); // 注册自定义枚举类型,否则信号槽无法发送 +} + +ReadThread::~ReadThread() +{ + if(m_videoDecode) + { + delete m_videoDecode; + } +} + +/** + * @brief 传入播放的视频地址并开启线程 + * @param url + */ +void ReadThread::open(const QString &url) +{ + if(!this->isRunning()) + { + m_url = url; + emit this->start(); + } +} + +/** + * @brief 控制暂停、继续 + * @param flag true:暂停 fasle:继续 + */ +void ReadThread::pause(bool flag) +{ + m_pause = flag; +} + +/** + * @brief 关闭播放 + */ +void ReadThread::close() +{ + m_play = false; + m_pause = false; +} + +/** + * @brief 返回当前播放的地址 + * @return + */ +const QString &ReadThread::url() +{ + return m_url; +} + +/** + * @brief 非阻塞延时 + * @param msec 延时毫秒 + */ +void sleepMsec(int msec) +{ + if(msec <= 0) return; + QEventLoop loop; //定义一个新的事件循环 + QTimer::singleShot(msec, &loop, SLOT(quit()));//创建单次定时器,槽函数为事件循环的退出函数 + loop.exec(); //事件循环开始执行,程序会卡在这里,直到定时时间到,本循环被退出 +} + +void ReadThread::run() +{ + bool ret = m_videoDecode->open(m_url); // 打开网络流时会比较慢,如果放到Ui线程会卡 + if(ret) + { + m_play = true; + m_etime1.start(); + m_etime2.start(); + emit playState(play); + } + else + { + qWarning() << "打开失败!"; + } + // 循环读取视频图像 + while (m_play) + { + // 暂停 + while (m_pause) + { + sleepMsec(200); + } + + QImage image = m_videoDecode->read(); // 读取视频图像 + if(!image.isNull()) + { + // 1倍速播放 +#if 0 + sleepMsec(int(m_decodeVideo->pts() - m_etime1.elapsed())); // 不支持后退 +#else + sleepMsec(int(m_videoDecode->pts() - m_etime2.elapsed())); // 支持后退 +#endif + emit updateImage(image); + } + else + { + // 当前读取到无效图像时判断是否读取完成 + if(m_videoDecode->isEnd()) + { + break; + } + sleepMsec(1); // 这里不能使用QThread::msleep()延时,否则会很不稳定 + } + } + + qDebug() << "播放结束!"; + m_videoDecode->close(); + emit playState(end); +} diff --git a/FFmpegDemo/VideoPlayGL1/VideoPlay/readthread.h b/FFmpegDemo/VideoPlayGL1/VideoPlay/readthread.h new file mode 100644 index 0000000000000000000000000000000000000000..b785c14af92de76892859dd3080fbfb023ffc537 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/VideoPlay/readthread.h @@ -0,0 +1,54 @@ +/****************************************************************************** + * @文件名 readthread.h + * @功能 读取视频图像数据线程,在线程中解码视频 + * + * @开发者 mhf + * @邮箱 1603291350@qq.com + * @时间 2022/09/15 + * @备注 + *****************************************************************************/ +#ifndef READTHREAD_H +#define READTHREAD_H + +#include +#include +#include + +class VideoDecode; +class PlayImage; + +class ReadThread : public QThread +{ + Q_OBJECT +public: + enum PlayState // 视频播放状态 + { + play, + end + }; +public: + explicit ReadThread(QObject *parent = nullptr); + ~ReadThread() override; + + void open(const QString& url = QString()); // 打开视频 + void pause(bool flag); // 暂停视频 + void close(); // 关闭视频 + const QString& url(); // 获取打开的视频地址 + +protected: + void run() override; + +signals: + void updateImage(const QImage& image); // 将读取到的视频图像发送出去 + void playState(PlayState state); // 视频播放状态发送改变时触发 + +private: + VideoDecode* m_videoDecode = nullptr; // 视频解码类 + QString m_url; // 打开的视频地址 + bool m_play = false; // 播放控制 + bool m_pause = false; // 暂停控制 + QElapsedTimer m_etime1; // 控制视频播放速度(更精确,但不支持视频后退) + QTime m_etime2; // 控制视频播放速度(支持视频后退) +}; + +#endif // READTHREAD_H diff --git a/FFmpegDemo/VideoPlayGL1/VideoPlay/videodecode.cpp b/FFmpegDemo/VideoPlayGL1/VideoPlay/videodecode.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea3d4376ec58f250b0fcbb559ed07575bc283d36 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/VideoPlay/videodecode.cpp @@ -0,0 +1,393 @@ +#include "videodecode.h" +#include +#include +#include +#include + + +extern "C" { // 用C规则编译指定的代码 +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/imgutils.h" + +} + +#define ERROR_LEN 1024 // 异常信息数组长度 +#define PRINT_LOG 1 + +VideoDecode::VideoDecode() +{ +// initFFmpeg(); // 5.1.2版本不需要调用了 + + m_error = new char[ERROR_LEN]; +} + +VideoDecode::~VideoDecode() +{ + close(); +} + +/** + * @brief 初始化ffmpeg库(整个程序中只需加载一次) + * 旧版本的ffmpeg需要注册各种文件格式、解复用器、对网络库进行全局初始化。 + * 在新版本的ffmpeg中纷纷弃用了,不需要注册了 + */ +void VideoDecode::initFFmpeg() +{ + static bool isFirst = true; + static QMutex mutex; + QMutexLocker locker(&mutex); + if(isFirst) + { + // av_register_all(); // 已经从源码中删除 + /** + * 初始化网络库,用于打开网络流媒体,此函数仅用于解决旧GnuTLS或OpenSSL库的线程安全问题。 + * 一旦删除对旧GnuTLS和OpenSSL库的支持,此函数将被弃用,并且此函数不再有任何用途。 + */ + avformat_network_init(); + isFirst = false; + } +} + +/** + * @brief 打开媒体文件,或者流媒体,例如rtmp、strp、http + * @param url 视频地址 + * @return true:成功 false:失败 + */ +bool VideoDecode::open(const QString &url) +{ + if(url.isNull()) return false; + + AVDictionary* dict = nullptr; + av_dict_set(&dict, "rtsp_transport", "tcp", 0); // 设置rtsp流使用tcp打开,如果打开失败错误信息为【Error number -135 occurred】可以切换(UDP、tcp、udp_multicast、http),比如vlc推流就需要使用udp打开 + av_dict_set(&dict, "max_delay", "3", 0); // 设置最大复用或解复用延迟(以微秒为单位)。当通过【UDP】 接收数据时,解复用器尝试重新排序接收到的数据包(因为它们可能无序到达,或者数据包可能完全丢失)。这可以通过将最大解复用延迟设置为零(通过max_delayAVFormatContext 字段)来禁用。 + av_dict_set(&dict, "timeout", "1000000", 0); // 以微秒为单位设置套接字 TCP I/O 超时,如果等待时间过短,也可能会还没连接就返回了。 + + // 打开输入流并返回解封装上下文 + int ret = avformat_open_input(&m_formatContext, // 返回解封装上下文 + url.toStdString().data(), // 打开视频地址 + nullptr, // 如果非null,此参数强制使用特定的输入格式。自动选择解封装器(文件格式) + &dict); // 参数设置 + // 释放参数字典 + if(dict) + { + av_dict_free(&dict); + } + // 打开视频失败 + if(ret < 0) + { + showError(ret); + free(); + return false; + } + + // 读取媒体文件的数据包以获取流信息。 + ret = avformat_find_stream_info(m_formatContext, nullptr); + if(ret < 0) + { + showError(ret); + free(); + return false; + } + m_totalTime = m_formatContext->duration / (AV_TIME_BASE / 1000); // 计算视频总时长(毫秒) +#if PRINT_LOG + qDebug() << QString("视频总时长:%1 ms,[%2]").arg(m_totalTime).arg(QTime::fromMSecsSinceStartOfDay(int(m_totalTime)).toString("HH:mm:ss zzz")); +#endif + + // 通过AVMediaType枚举查询视频流ID(也可以通过遍历查找),最后一个参数无用 + m_videoIndex = av_find_best_stream(m_formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0); + if(m_videoIndex < 0) + { + showError(m_videoIndex); + free(); + return false; + } + + AVStream* videoStream = m_formatContext->streams[m_videoIndex]; // 通过查询到的索引获取视频流 + + // 获取视频图像分辨率(AVStream中的AVCodecContext在新版本中弃用,改为使用AVCodecParameters) + m_size.setWidth(videoStream->codecpar->width); + m_size.setHeight(videoStream->codecpar->height); + m_frameRate = rationalToDouble(&videoStream->avg_frame_rate); // 视频帧率 + + // 通过解码器ID获取视频解码器(新版本返回值必须使用const) + const AVCodec* codec = avcodec_find_decoder(videoStream->codecpar->codec_id); + m_totalFrames = videoStream->nb_frames; + +#if PRINT_LOG + qDebug() << QString("分辨率:[w:%1,h:%2] 帧率:%3 总帧数:%4 解码器:%5") + .arg(m_size.width()).arg(m_size.height()).arg(m_frameRate).arg(m_totalFrames).arg(codec->name); +#endif + + // 分配AVCodecContext并将其字段设置为默认值。 + m_codecContext = avcodec_alloc_context3(codec); + if(!m_codecContext) + { +#if PRINT_LOG + qWarning() << "创建视频解码器上下文失败!"; +#endif + free(); + return false; + } + + // 使用视频流的codecpar为解码器上下文赋值 + ret = avcodec_parameters_to_context(m_codecContext, videoStream->codecpar); + if(ret < 0) + { + showError(ret); + free(); + return false; + } + + m_codecContext->flags2 |= AV_CODEC_FLAG2_FAST; // 允许不符合规范的加速技巧。 + m_codecContext->thread_count = 8; // 使用8线程解码 + + // 初始化解码器上下文,如果之前avcodec_alloc_context3传入了解码器,这里设置NULL就可以 + ret = avcodec_open2(m_codecContext, nullptr, nullptr); + if(ret < 0) + { + showError(ret); + free(); + return false; + } + + // 分配AVPacket并将其字段设置为默认值。 + m_packet = av_packet_alloc(); + if(!m_packet) + { +#if PRINT_LOG + qWarning() << "av_packet_alloc() Error!"; +#endif + free(); + return false; + } + // 分配AVFrame并将其字段设置为默认值。 + m_frame = av_frame_alloc(); + if(!m_frame) + { +#if PRINT_LOG + qWarning() << "av_frame_alloc() Error!"; +#endif + free(); + return false; + } + + // 分配图像空间 + int size = av_image_get_buffer_size(AV_PIX_FMT_RGBA, m_size.width(), m_size.height(), 4); + /** + * 【注意:】这里可以多分配一些,否则如果只是安装size分配,大部分视频图像数据拷贝没有问题, + * 但是少部分视频图像在使用sws_scale()拷贝时会超出数组长度,在使用使用msvc debug模式时delete[] m_buffer会报错(HEAP CORRUPTION DETECTED: after Normal block(#32215) at 0x000001AC442830370.CRT delected that the application wrote to memory after end of heap buffer) + * 特别是这个视频流http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4 + */ + m_buffer = new uchar[size + 1000]; // 这里多分配1000个字节就基本不会出现拷贝超出的情况了,反正不缺这点内存 +// m_image = new QImage(m_buffer, m_size.width(), m_size.height(), QImage::Format_RGBA8888); // 这种方式分配内存大部分情况下也可以,但是因为存在拷贝超出数组的情况,delete时也会报错 + m_end = false; + return true; +} + +/** + * @brief + * @return + */ +QImage VideoDecode::read() +{ + // 如果没有打开则返回 + if(!m_formatContext) + { + return QImage(); + } + + // 读取下一帧数据 + int readRet = av_read_frame(m_formatContext, m_packet); + if(readRet < 0) + { + avcodec_send_packet(m_codecContext, m_packet); // 读取完成后向解码器中传如空AVPacket,否则无法读取出最后几帧 + } + else + { + if(m_packet->stream_index == m_videoIndex) // 如果是图像数据则进行解码 + { + // 计算当前帧时间(毫秒) +#if 1 // 方法一:适用于所有场景,但是存在一定误差 + m_packet->pts = qRound64(m_packet->pts * (1000 * rationalToDouble(&m_formatContext->streams[m_videoIndex]->time_base))); + m_packet->dts = qRound64(m_packet->dts * (1000 * rationalToDouble(&m_formatContext->streams[m_videoIndex]->time_base))); +#else // 方法二:适用于播放本地视频文件,计算每一帧时间较准,但是由于网络视频流无法获取总帧数,所以无法适用 + m_obtainFrames++; + m_packet->pts = qRound64(m_obtainFrames * (qreal(m_totalTime) / m_totalFrames)); +#endif + // 将读取到的原始数据包传入解码器 + int ret = avcodec_send_packet(m_codecContext, m_packet); + if(ret < 0) + { + showError(ret); + } + } + } + av_packet_unref(m_packet); // 释放数据包,引用计数-1,为0时释放空间 + + int ret = avcodec_receive_frame(m_codecContext, m_frame); + if(ret < 0) + { + av_frame_unref(m_frame); + if(readRet < 0) + { + m_end = true; // 当无法读取到AVPacket并且解码器中也没有数据时表示读取完成 + } + return QImage(); + } + + m_pts = m_frame->pts; + + // 为什么图像转换上下文要放在这里初始化呢,是因为m_frame->format,如果使用硬件解码,解码出来的图像格式和m_codecContext->pix_fmt的图像格式不一样,就会导致无法转换为QImage + if(!m_swsContext) + { + // 获取缓存的图像转换上下文。首先校验参数是否一致,如果校验不通过就释放资源;然后判断上下文是否存在,如果存在直接复用,如不存在进行分配、初始化操作 + m_swsContext = sws_getCachedContext(m_swsContext, + m_frame->width, // 输入图像的宽度 + m_frame->height, // 输入图像的高度 + (AVPixelFormat)m_frame->format, // 输入图像的像素格式 + m_size.width(), // 输出图像的宽度 + m_size.height(), // 输出图像的高度 + AV_PIX_FMT_RGBA, // 输出图像的像素格式 + SWS_BILINEAR, // 选择缩放算法(只有当输入输出图像大小不同时有效),一般选择SWS_FAST_BILINEAR + nullptr, // 输入图像的滤波器信息, 若不需要传NULL + nullptr, // 输出图像的滤波器信息, 若不需要传NULL + nullptr); // 特定缩放算法需要的参数(?),默认为NULL + if(!m_swsContext) + { +#if PRINT_LOG + qWarning() << "sws_getCachedContext() Error!"; +#endif + free(); + return QImage(); + } + } + + // AVFrame转QImage + uchar* data[] = {m_buffer}; + int lines[4]; + av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, m_frame->width); // 使用像素格式pix_fmt和宽度填充图像的平面线条大小。 + ret = sws_scale(m_swsContext, // 缩放上下文 + m_frame->data, // 原图像数组 + m_frame->linesize, // 包含源图像每个平面步幅的数组 + 0, // 开始位置 + m_frame->height, // 行数 + data, // 目标图像数组 + lines); // 包含目标图像每个平面的步幅的数组 + QImage image(m_buffer, m_frame->width, m_frame->height, QImage::Format_RGBA8888); + av_frame_unref(m_frame); + + return image; +} + +/** + * @brief 关闭视频播放并释放内存 + */ +void VideoDecode::close() +{ + clear(); + free(); + + m_totalTime = 0; + m_videoIndex = 0; + m_totalFrames = 0; + m_obtainFrames = 0; + m_pts = 0; + m_frameRate = 0; + m_size = QSize(0, 0); +} + +/** + * @brief 视频是否读取完成 + * @return + */ +bool VideoDecode::isEnd() +{ + return m_end; +} + +/** + * @brief 返回当前帧图像播放时间 + * @return + */ +const qint64 &VideoDecode::pts() +{ + return m_pts; +} + +/** + * @brief 显示ffmpeg函数调用异常信息 + * @param err + */ +void VideoDecode::showError(int err) +{ +#if PRINT_LOG + memset(m_error, 0, ERROR_LEN); // 将数组置零 + av_strerror(err, m_error, ERROR_LEN); + qWarning() << "DecodeVideo Error:" << m_error; +#else + Q_UNUSED(err) +#endif +} + +/** + * @brief 将AVRational转换为double,用于计算帧率 + * @param rational + * @return + */ +qreal VideoDecode::rationalToDouble(AVRational* rational) +{ + qreal frameRate = (rational->den == 0) ? 0 : (qreal(rational->num) / rational->den); + return frameRate; +} + +/** + * @brief 清空读取缓冲 + */ +void VideoDecode::clear() +{ + // 因为avformat_flush不会刷新AVIOContext (s->pb)。如果有必要,在调用此函数之前调用avio_flush(s->pb)。 + if(m_formatContext && m_formatContext->pb) + { + avio_flush(m_formatContext->pb); + } + if(m_formatContext) + { + avformat_flush(m_formatContext); // 清理读取缓冲 + } +} + +void VideoDecode::free() +{ + // 释放上下文swsContext。 + if(m_swsContext) + { + sws_freeContext(m_swsContext); + m_swsContext = nullptr; // sws_freeContext不会把上下文置NULL + } + // 释放编解码器上下文和与之相关的所有内容,并将NULL写入提供的指针 + if(m_codecContext) + { + avcodec_free_context(&m_codecContext); + } + // 关闭并失败m_formatContext,并将指针置为null + if(m_formatContext) + { + avformat_close_input(&m_formatContext); + } + if(m_packet) + { + av_packet_free(&m_packet); + } + if(m_frame) + { + av_frame_free(&m_frame); + } + if(m_buffer) + { + delete [] m_buffer; + m_buffer = nullptr; + } +} diff --git a/FFmpegDemo/VideoPlayGL1/VideoPlay/videodecode.h b/FFmpegDemo/VideoPlayGL1/VideoPlay/videodecode.h new file mode 100644 index 0000000000000000000000000000000000000000..7da0f6f9cc22e96277200d4225eea62e9fc4106d --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/VideoPlay/videodecode.h @@ -0,0 +1,62 @@ +/****************************************************************************** + * @文件名 videodecode.h + * @功能 视频解码类,在这个类中调用ffmpeg打开视频进行解码 + * + * @开发者 mhf + * @邮箱 1603291350@qq.com + * @时间 2022/09/15 + * @备注 + *****************************************************************************/ +#ifndef VIDEODECODE_H +#define VIDEODECODE_H + +#include +#include + +struct AVFormatContext; +struct AVCodecContext; +struct AVRational; +struct AVPacket; +struct AVFrame; +struct SwsContext; +struct AVBufferRef; +class QImage; + +class VideoDecode +{ +public: + VideoDecode(); + ~VideoDecode(); + + bool open(const QString& url = QString()); // 打开媒体文件,或者流媒体rtmp、strp、http + QImage read(); // 读取视频图像 + void close(); // 关闭 + bool isEnd(); // 是否读取完成 + const qint64& pts(); // 获取当前帧显示时间 + +private: + void initFFmpeg(); // 初始化ffmpeg库(整个程序中只需加载一次) + void showError(int err); // 显示ffmpeg执行错误时的错误信息 + qreal rationalToDouble(AVRational* rational); // 将AVRational转换为double + void clear(); // 清空读取缓冲 + void free(); // 释放 + +private: + AVFormatContext* m_formatContext = nullptr; // 解封装上下文 + AVCodecContext* m_codecContext = nullptr; // 解码器上下文 + SwsContext* m_swsContext = nullptr; // 图像转换上下文 + AVPacket* m_packet = nullptr; // 数据包 + AVFrame* m_frame = nullptr; // 解码后的视频帧 + int m_videoIndex = 0; // 视频流索引 + qint64 m_totalTime = 0; // 视频总时长 + qint64 m_totalFrames = 0; // 视频总帧数 + qint64 m_obtainFrames = 0; // 视频当前获取到的帧数 + qint64 m_pts = 0; // 图像帧的显示时间 + qreal m_frameRate = 0; // 视频帧率 + QSize m_size; // 视频分辨率大小 + char* m_error = nullptr; // 保存异常信息 + bool m_end = false; // 视频读取完成 + uchar* m_buffer = nullptr; // YUV图像需要转换位RGBA图像,这里保存转换后的图形数据 +}; + +#endif // VIDEODECODE_H diff --git a/FFmpegDemo/VideoPlayGL1/VideoPlayGL1.pro b/FFmpegDemo/VideoPlayGL1/VideoPlayGL1.pro new file mode 100644 index 0000000000000000000000000000000000000000..8d7b3b22ac345bba1166043dfd604ee7bc35bb8d --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/VideoPlayGL1.pro @@ -0,0 +1,65 @@ +#--------------------------------------------------------------------------------------- +# @功能: 使用ffmpeg音视频库【软解码】实现的视频播放器; +# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit 32bit(也支持其它编译器) +# @Qt IDE: D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator +# +# @开发者 mhf +# @邮箱 1603291350@qq.com +# @时间 2022-09-14 14:13:52 +# @备注 1、使用ffmpeg音视频库【软解码】实现的视频播放器; +# 2、支持打开本地视频文件(如mp4、mov、avi等)、网络视频流(rtsp、rtmp、http等); +# 3、支持视频匀速播放; +# 4、采用【OpenGL显示RGB】图像,支持自适应窗口缩放,支持使用QOpenGLWidget、QOpenGLWindow显示; +# 5、视频播放支持实时开始/关闭、暂停/继续播放; +# 6、视频解码、线程控制、显示各部分功能分离,低耦合度。 +# 7、采用最新的5.1.2版本ffmpeg库进行开发,超详细注释信息,将所有踩过的坑、解决办法、注意事项都得很写清楚。 +#--------------------------------------------------------------------------------------- +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +CONFIG += c++11 +DEFINES += QT_DEPRECATED_WARNINGS + +SOURCES += \ + playimage.cpp \ + main.cpp \ + widget.cpp + +HEADERS += \ + playimage.h \ + widget.h + +FORMS += \ + widget.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +# ffmpeg读取视频图像模块 +include(./VideoPlay/VideoPlay.pri) +INCLUDEPATH += ./VideoPlay + +# 定义程序版本号 +VERSION = 1.0.0 +DEFINES += APP_VERSION=\\\"$$VERSION\\\" +TARGET = VideoPlayGL1 + +contains(QT_ARCH, i386){ # 使用32位编译器 +DESTDIR = $$PWD/../bin # 程序输出路径 +}else{ +DESTDIR = $$PWD/../bin64 # 使用64位编译器 +} +# msvc 编译器使用utf-8编码 +msvc { +QMAKE_CFLAGS += /utf-8 +QMAKE_CXXFLAGS += /utf-8 +} + +DISTFILES += \ + fragment.fsh \ + vertex.vsh + +RESOURCES += \ + rc.qrc diff --git a/FFmpegDemo/VideoPlayGL1/fragment.fsh b/FFmpegDemo/VideoPlayGL1/fragment.fsh new file mode 100644 index 0000000000000000000000000000000000000000..22136bbd07721e4495aca3edca0524102829fbc0 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/fragment.fsh @@ -0,0 +1,8 @@ +#version 330 core +out vec4 FragColor; +in vec2 TexCord; // 纹理坐标 +uniform sampler2D texture; // 纹理采样器 +void main() +{ + FragColor = texture2D(texture, TexCord); // 采样纹理函数 +} diff --git a/FFmpegDemo/VideoPlayGL1/main.cpp b/FFmpegDemo/VideoPlayGL1/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0a4ec26478f6b9aba3e1747ec464ea0c26dd5b9 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + return a.exec(); +} diff --git a/FFmpegDemo/VideoPlayGL1/playimage.cpp b/FFmpegDemo/VideoPlayGL1/playimage.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5f6a873d87436a5391cd97631f65296fea91f2a --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/playimage.cpp @@ -0,0 +1,158 @@ +#include "playimage.h" + + +#if USE_WINDOW +PlayImage::PlayImage(QOpenGLWindow::UpdateBehavior updateBehavior, QWindow *parent):QOpenGLWindow(updateBehavior, parent) +{ +} +#else +PlayImage::PlayImage(QWidget *parent, Qt::WindowFlags f): QOpenGLWidget(parent, f) +{ + +} +#endif + + +PlayImage::~PlayImage() +{ + if(!isValid()) return; // 如果控件和OpenGL资源(如上下文)已成功初始化,则返回true。 + this->makeCurrent(); // 通过将相应的上下文设置为当前上下文并在该上下文中绑定帧缓冲区对象,为呈现此小部件的OpenGL内容做准备。 + // 释放纹理 + if(m_texture) + { + m_texture->destroy(); + delete m_texture; + } + this->doneCurrent(); // 释放上下文 + // 释放 + glDeleteBuffers(1, &VBO); + glDeleteBuffers(1, &EBO); + glDeleteVertexArrays(1, &VAO); +} + +/** + * @brief 传入Qimage图片显示 + * @param image + */ +void PlayImage::updateImage(const QImage& image) +{ + if(image.isNull()) return; + + m_size = image.size(); + if(!m_texture) + { + m_texture = new QOpenGLTexture(image.mirrored()); + resizeGL(this->width(), this->height()); + } + else + { + m_texture->destroy(); + m_texture->setData(image.mirrored()); + } + this->update(); +} +// 三个顶点坐标XYZ,VAO、VBO数据播放,范围时[-1 ~ 1]直接 +static GLfloat vertices[] = { // 前三列点坐标,后两列为纹理坐标 + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 右上角 + 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // 右下 + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // 左下 + -1.0f, 1.0f, 0.0f, 0.0f, 1.0f // 左上 +}; +static GLuint indices[] = { + 0, 1, 3, + 1, 2, 3 +}; +void PlayImage::initializeGL() +{ + initializeOpenGLFunctions(); + + m_program = new QOpenGLShaderProgram(this); + m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vertex.vsh"); + m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fragment.fsh"); + m_program->link(); + + // 返回属性名称在此着色器程序的参数列表中的位置。如果名称不是此着色器程序的有效属性,则返回-1。 + GLuint posAttr = GLuint(m_program->attributeLocation("aPos")); + GLuint texCord = GLuint(m_program->attributeLocation("aTexCord")); + + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glGenBuffers(1, &EBO); // 创建一个EBO + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + + + // 为当前绑定到的缓冲区对象创建一个新的数据存储target。任何预先存在的数据存储都将被删除。 + glBufferData(GL_ARRAY_BUFFER, // 为VBO缓冲绑定顶点数据 + sizeof (vertices), // 数组字节大小 + vertices, // 需要绑定的数组 + GL_STATIC_DRAW); // 指定数据存储的预期使用模式,GL_STATIC_DRAW: 数据几乎不会改变 + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // 将顶点索引数组传入EBO缓存 + // 设置顶点坐标数据 + glVertexAttribPointer(posAttr, // 指定要修改的通用顶点属性的索引 + 3, // 指定每个通用顶点属性的组件数(如vec3:3,vec4:4) + GL_FLOAT, // 指定数组中每个组件的数据类型(数组中一行有几个数) + GL_FALSE, // 指定在访问定点数据值时是否应规范化 ( GL_TRUE) 或直接转换为定点值 ( GL_FALSE),如果vertices里面单个数超过-1或者1可以选择GL_TRUE + 5 * sizeof(GLfloat), // 指定连续通用顶点属性之间的字节偏移量。 + nullptr); // 指定当前绑定到目标的缓冲区的数据存储中数组中第一个通用顶点属性的第一个组件的偏移量。初始值为0 (一个数组从第几个字节开始读) + // 启用通用顶点属性数组 + glEnableVertexAttribArray(posAttr); // 属性索引是从调用glGetAttribLocation接收的,或者传递给glBindAttribLocation。 + + // 设置纹理坐标数据 + glVertexAttribPointer(texCord, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), reinterpret_cast(3 * sizeof (GLfloat))); // 指定当前绑定到目标的缓冲区的数据存储中数组中第一个通用顶点属性的第一个组件的偏移量。初始值为0 (一个数组从第几个字节开始读) + // 启用通用顶点属性数组 + glEnableVertexAttribArray(texCord); // 属性索引是从调用glGetAttribLocation接收的,或者传递给glBindAttribLocation。 + + // 释放 + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); // 设置为零以破坏现有的顶点数组对象绑定 + + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // 指定颜色缓冲区的清除值(背景色) +} + +void PlayImage::resizeGL(int w, int h) +{ + if(m_size.width() < 0 || m_size.height() < 0) return; + + // 计算需要显示图片的窗口大小,用于实现长宽等比自适应显示 + if((double(w) / h) < (double(m_size.width()) / m_size.height())) + { + m_zoomSize.setWidth(w); + m_zoomSize.setHeight(((double(w) / m_size.width()) * m_size.height())); // 这里不使用QRect,使用QRect第一次设置时有误差bug + } + else + { + m_zoomSize.setHeight(h); + m_zoomSize.setWidth((double(h) / m_size.height()) * m_size.width()); + } + m_pos.setX(double(w - m_zoomSize.width()) / 2); + m_pos.setY(double(h - m_zoomSize.height()) / 2); + this->update(QRect(0, 0, w, h)); +} + +void PlayImage::paintGL() +{ + glClear(GL_COLOR_BUFFER_BIT); // 将窗口的位平面区域(背景)设置为先前由glClearColor、glClearDepth和选择的值 + glViewport(m_pos.x(), m_pos.y(), m_zoomSize.width(), m_zoomSize.height()); // 设置视图大小实现图片自适应 + + m_program->bind(); // 绑定着色器 + if(m_texture) + { + m_texture->bind(); + } + + glBindVertexArray(VAO); // 绑定VAO + + glDrawElements(GL_TRIANGLES, // 绘制的图元类型 + 6, // 指定要渲染的元素数(点数) + GL_UNSIGNED_INT, // 指定索引中值的类型(indices) + nullptr); // 指定当前绑定到GL_ELEMENT_array_buffer目标的缓冲区的数据存储中数组中第一个索引的偏移量。 + glBindVertexArray(0); + if(m_texture) + { + m_texture->release(); + } + m_program->release(); +} diff --git a/FFmpegDemo/VideoPlayGL1/playimage.h b/FFmpegDemo/VideoPlayGL1/playimage.h new file mode 100644 index 0000000000000000000000000000000000000000..ec73ce2cf948b72d06680212c7d360ca1656f07f --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/playimage.h @@ -0,0 +1,57 @@ +/****************************************************************************** + * @文件名 playimage.h + * @功能 使用OpenGL实现RGB图像的绘制,可通过USE_WINDOW宏切换使用QOpenGLWindow还是QOpenGLWidget + * + * @开发者 mhf + * @邮箱 1603291350@qq.com + * @时间 2022/10/14 + * @备注 + *****************************************************************************/ +#ifndef PLAYIMAGE_H +#define PLAYIMAGE_H + +#include +#include +#include +#include + +#define USE_WINDOW 0 // 1:使用QOpenGLWindow显示, 0:使用QOpenGLWidget显示 + +#if USE_WINDOW +#include +class PlayImage : public QOpenGLWindow, public QOpenGLFunctions_3_3_Core +#else +#include +class PlayImage : public QOpenGLWidget, public QOpenGLFunctions_3_3_Core +#endif +{ + Q_OBJECT +public: +#if USE_WINDOW + explicit PlayImage(UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = nullptr); +#else + explicit PlayImage(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); +#endif + ~PlayImage() override; + + void updateImage(const QImage& image); + + +protected: + void initializeGL() override; // 初始化gl + void resizeGL(int w, int h) override; // 窗口尺寸变化 + void paintGL() override; // 刷新显示 + +private: + QOpenGLShaderProgram* m_program = nullptr; + QOpenGLTexture* m_texture = nullptr; + + GLuint VBO = 0; // 顶点缓冲对象,负责将数据从内存放到缓存,一个VBO可以用于多个VAO + GLuint VAO = 0; // 顶点数组对象,任何随后的顶点属性调用都会储存在这个VAO中,一个VAO可以有多个VBO + GLuint EBO = 0; // 元素缓冲对象,它存储 OpenGL 用来决定要绘制哪些顶点的索引 + QSize m_size; + QSizeF m_zoomSize; + QPointF m_pos; +}; + +#endif // PLAYIMAGE_H diff --git a/FFmpegDemo/VideoPlayGL1/rc.qrc b/FFmpegDemo/VideoPlayGL1/rc.qrc new file mode 100644 index 0000000000000000000000000000000000000000..28293086375e5700d3257397d189b3c908a60025 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/rc.qrc @@ -0,0 +1,6 @@ + + + fragment.fsh + vertex.vsh + + diff --git a/FFmpegDemo/VideoPlayGL1/vertex.vsh b/FFmpegDemo/VideoPlayGL1/vertex.vsh new file mode 100644 index 0000000000000000000000000000000000000000..afde74c24cb6f34b301ba10564df18bc7fe5a8a2 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/vertex.vsh @@ -0,0 +1,9 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCord; +out vec2 TexCord; // 纹理坐标 +void main() +{ + gl_Position = vec4(aPos, 1.0); + TexCord = aTexCord; +} diff --git a/FFmpegDemo/VideoPlayGL1/widget.cpp b/FFmpegDemo/VideoPlayGL1/widget.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5655c1b38a3f1822cc38bde388b6216cc14d4b46 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/widget.cpp @@ -0,0 +1,101 @@ +#include "widget.h" +#include "ui_widget.h" + +#include + +Widget::Widget(QWidget *parent) + : QWidget(parent) + , ui(new Ui::Widget) +{ + ui->setupUi(this); + this->setWindowTitle(QString("Qt+ffmpeg视频播放(软解码 + OpenGL显示RGB)Demo V%1").arg(APP_VERSION)); + + + // 使用QOpenGLWindow绘制 + playImage = new PlayImage; +#if USE_WINDOW + ui->verticalLayout->addWidget(QWidget::createWindowContainer(playImage)); // 这一步加载速度要比OpenGLWidget慢一点 +#else + ui->verticalLayout->addWidget(playImage); +#endif + + m_readThread = new ReadThread(); + connect(m_readThread, &ReadThread::updateImage, playImage, &PlayImage::updateImage); + connect(m_readThread, &ReadThread::playState, this, &Widget::on_playState); +} + +Widget::~Widget() +{ + // 释放视频读取线程 + if(m_readThread) + { + m_readThread->close(); + m_readThread->wait(); + delete m_readThread; + } + delete ui; +} + +/** + * @brief 获取本地视频路径 + */ +void Widget::on_but_file_clicked() +{ + QString strName = QFileDialog::getOpenFileName(this, "选择播放视频~!", "/", "视频 (*.mp4 *.m4v *.mov *.avi *.flv);; 其它(*)"); + if(strName.isEmpty()) + { + return; + } + ui->com_url->setCurrentText(strName); +} + +/** + * @brief 视频播放/停止 + */ +void Widget::on_but_open_clicked() +{ + if(ui->but_open->text() == "开始播放") + { + m_readThread->open(ui->com_url->currentText()); + } + else + { + m_readThread->close(); + } +} + +/** + * @brief 视频暂停/继续 + */ +void Widget::on_but_pause_clicked() +{ + if(ui->but_pause->text() == "暂停") + { + m_readThread->pause(true); + ui->but_pause->setText("继续"); + } + else + { + m_readThread->pause(false); + ui->but_pause->setText("暂停"); + } +} + +/** + * @brief 根据视频播放状态切换界面设置 + * @param state + */ +void Widget::on_playState(ReadThread::PlayState state) +{ + if(state == ReadThread::play) + { + this->setWindowTitle(QString("正在播放:%1").arg(m_readThread->url())); + ui->but_open->setText("停止播放"); + } + else + { + ui->but_open->setText("开始播放"); + ui->but_pause->setText("暂停"); + this->setWindowTitle(QString("Qt+ffmpeg视频播放(软解码 + OpenGL显示RGB)Demo V%1").arg(APP_VERSION)); + } +} diff --git a/FFmpegDemo/VideoPlayGL1/widget.h b/FFmpegDemo/VideoPlayGL1/widget.h new file mode 100644 index 0000000000000000000000000000000000000000..9ed58d1edec61ebb08c86594944e76039b9e5067 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/widget.h @@ -0,0 +1,35 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include +#include "readthread.h" +#include "playimage.h" + +QT_BEGIN_NAMESPACE +namespace Ui { class Widget; } +QT_END_NAMESPACE + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + +private slots: + void on_but_file_clicked(); + + void on_but_open_clicked(); + + void on_but_pause_clicked(); + + void on_playState(ReadThread::PlayState state); + +private: + Ui::Widget *ui; + + PlayImage* playImage = nullptr; + ReadThread* m_readThread = nullptr; +}; +#endif // WIDGET_H diff --git a/FFmpegDemo/VideoPlayGL1/widget.ui b/FFmpegDemo/VideoPlayGL1/widget.ui new file mode 100644 index 0000000000000000000000000000000000000000..a0091763fb2c311c8aa44a8288d6781624e6ed22 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL1/widget.ui @@ -0,0 +1,137 @@ + + + Widget + + + + 0 + 0 + 800 + 600 + + + + Widget + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + true + + + QComboBox::InsertAtBottom + + + QComboBox::AdjustToMinimumContentsLengthWithIcon + + + false + + + + rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 + + + + + rtmp://ns8.indexforce.com/home/mystream + + + + + rtmp://58.200.131.2:1935/livetv/cctv1 + + + + + http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4 + + + + + http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8 + + + + + http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4 + + + + + http://vjs.zencdn.net/v/oceans.mp4 + + + + + https://media.w3.org/2010/05/sintel/trailer.mp4 + + + + + https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv + + + + + + + + + 0 + 0 + + + + 选择 + + + + + + + + 0 + 0 + + + + 开始播放 + + + + + + + + 0 + 0 + + + + 暂停 + + + + + + + + + + diff --git a/FFmpegDemo/VideoPlayGL2/VideoPlay/VideoPlay.pri b/FFmpegDemo/VideoPlayGL2/VideoPlay/VideoPlay.pri new file mode 100644 index 0000000000000000000000000000000000000000..a1396dcab6ecacbbc5444ada65b5bbb2412fbd2e --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/VideoPlay/VideoPlay.pri @@ -0,0 +1,31 @@ +#--------------------------------------------------------------------------------------- +# @功能: ffmpeg读取视频图像模块 +# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit(也支持其它编译器) +# @Qt IDE: D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator +# +# @开发者 mhf +# @邮箱 1603291350@qq.com +# @时间 2022-09-15 14:27:25 +# @备注 +#--------------------------------------------------------------------------------------- + +# 加载库,ffmpeg n5.1.2版本 +win32{ +LIBS += -LE:/lib/ffmpeg5-1-2/lib/ -lavcodec -lavfilter -lavformat -lswscale -lavutil -lswresample -lavdevice +INCLUDEPATH += E:/lib/ffmpeg5-1-2/include +DEPENDPATH += E:/lib/ffmpeg5-1-2/include +} + +unix:!macx{ +LIBS += -L/home/mhf/lib/ffmpeg/ffmpeg-5-1-2/lib -lavcodec -lavfilter -lavformat -lswscale -lavutil -lswresample -lavdevice +INCLUDEPATH += /home/mhf/lib/ffmpeg/ffmpeg-5-1-2/include +DEPENDPATH += /home/mhf/lib/ffmpeg/ffmpeg-5-1-2/include +} + +HEADERS += \ + $$PWD/readthread.h \ + $$PWD/videodecode.h + +SOURCES += \ + $$PWD/readthread.cpp \ + $$PWD/videodecode.cpp diff --git a/FFmpegDemo/VideoPlayGL2/VideoPlay/readthread.cpp b/FFmpegDemo/VideoPlayGL2/VideoPlay/readthread.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7a218cfc9752ce751b1aeb516e1461cde3db037 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/VideoPlay/readthread.cpp @@ -0,0 +1,136 @@ +#include "readthread.h" +#include "videodecode.h" + +#include + +#include +#include +#include +#include +#include + +extern "C" { // 用C规则编译指定的代码 +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/imgutils.h" +} + +ReadThread::ReadThread(QObject *parent) : QThread(parent) +{ + m_videoDecode = new VideoDecode(); + + // 注册自定义枚举类型,否则信号槽无法发送 + qRegisterMetaType("PlayState"); +} + +ReadThread::~ReadThread() +{ + if(m_videoDecode) + { + delete m_videoDecode; + } +} + +/** + * @brief 传入播放的视频地址并开启线程 + * @param url + */ +void ReadThread::open(const QString &url) +{ + if(!this->isRunning()) + { + m_url = url; + emit this->start(); + } +} + +/** + * @brief 控制暂停、继续 + * @param flag true:暂停 fasle:继续 + */ +void ReadThread::pause(bool flag) +{ + m_pause = flag; +} + +/** + * @brief 关闭播放 + */ +void ReadThread::close() +{ + m_play = false; + m_pause = false; +} + +/** + * @brief 返回当前播放的地址 + * @return + */ +const QString &ReadThread::url() +{ + return m_url; +} + +/** + * @brief 非阻塞延时 + * @param msec 延时毫秒 + */ +void sleepMsec(int msec) +{ + if(msec <= 0) return; + QEventLoop loop; //定义一个新的事件循环 + QTimer::singleShot(msec, &loop, SLOT(quit()));//创建单次定时器,槽函数为事件循环的退出函数 + loop.exec(); //事件循环开始执行,程序会卡在这里,直到定时时间到,本循环被退出 +} + +void ReadThread::run() +{ + bool ret = m_videoDecode->open(m_url); // 打开网络流时会比较慢,如果放到Ui线程会卡 + if(ret) + { + m_play = true; + m_etime1.start(); + m_etime2.start(); + emit playState(play); + } + else + { + qWarning() << "打开失败!"; + } + // 循环读取视频图像 + while (m_play) + { + // 暂停 + while (m_pause) + { + sleepMsec(200); + } + + AVFrame* frame = m_videoDecode->read(); // 读取视频图像 + if(frame) + { + // 1倍速播放 +#if 0 + sleepMsec(int(m_decodeVideo->pts() - m_etime1.elapsed())); // 不支持后退 +#else + sleepMsec(int(m_videoDecode->pts() - m_etime2.elapsed())); // 支持后退 +#endif + emit repaint(frame); + } + else + { + // 当前读取到无效图像时判断是否读取完成 + if(m_videoDecode->isEnd()) + { + break; + } + sleepMsec(1); // 这里不能使用QThread::msleep()延时,否则会很不稳定 + } + } + + qDebug() << "播放结束!"; + m_videoDecode->close(); + emit playState(end); +} diff --git a/FFmpegDemo/VideoPlayGL2/VideoPlay/readthread.h b/FFmpegDemo/VideoPlayGL2/VideoPlay/readthread.h new file mode 100644 index 0000000000000000000000000000000000000000..66eb5be3c91ebb6e26afc1dfdf97344f494f8427 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/VideoPlay/readthread.h @@ -0,0 +1,55 @@ +/****************************************************************************** + * @文件名 readthread.h + * @功能 读取视频图像数据线程,在线程中解码视频 + * + * @开发者 mhf + * @邮箱 1603291350@qq.com + * @时间 2022/09/15 + * @备注 + *****************************************************************************/ +#ifndef READTHREAD_H +#define READTHREAD_H + +#include +#include +#include + +class VideoDecode; +class PlayImage; +struct AVFrame; + +class ReadThread : public QThread +{ + Q_OBJECT +public: + enum PlayState // 视频播放状态 + { + play, + end + }; +public: + explicit ReadThread(QObject *parent = nullptr); + ~ReadThread() override; + + void open(const QString& url = QString()); // 打开视频 + void pause(bool flag); // 暂停视频 + void close(); // 关闭视频 + const QString& url(); // 获取打开的视频地址 + +protected: + void run() override; + +signals: + void repaint(AVFrame* frame); // 重绘 + void playState(PlayState state); // 视频播放状态发送改变时触发 + +private: + VideoDecode* m_videoDecode = nullptr; // 视频解码类 + QString m_url; // 打开的视频地址 + bool m_play = false; // 播放控制 + bool m_pause = false; // 暂停控制 + QElapsedTimer m_etime1; // 控制视频播放速度(更精确,但不支持视频后退) + QTime m_etime2; // 控制视频播放速度(支持视频后退) +}; + +#endif // READTHREAD_H diff --git a/FFmpegDemo/VideoPlayGL2/VideoPlay/videodecode.cpp b/FFmpegDemo/VideoPlayGL2/VideoPlay/videodecode.cpp new file mode 100644 index 0000000000000000000000000000000000000000..542431384527a0194908c0c53e7dfe5301c59f2c --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/VideoPlay/videodecode.cpp @@ -0,0 +1,354 @@ +#include "videodecode.h" +#include +#include +#include +#include + + +extern "C" { // 用C规则编译指定的代码 +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/imgutils.h" +} + +#define ERROR_LEN 1024 // 异常信息数组长度 +#define PRINT_LOG 1 + +VideoDecode::VideoDecode() +{ +// initFFmpeg(); // 5.1.2版本不需要调用了 + + m_error = new char[ERROR_LEN]; +} + +VideoDecode::~VideoDecode() +{ + close(); +} + +/** + * @brief 初始化ffmpeg库(整个程序中只需加载一次) + * 旧版本的ffmpeg需要注册各种文件格式、解复用器、对网络库进行全局初始化。 + * 在新版本的ffmpeg中纷纷弃用了,不需要注册了 + */ +void VideoDecode::initFFmpeg() +{ + static bool isFirst = true; + static QMutex mutex; + QMutexLocker locker(&mutex); + if(isFirst) + { + // av_register_all(); // 已经从源码中删除 + /** + * 初始化网络库,用于打开网络流媒体,此函数仅用于解决旧GnuTLS或OpenSSL库的线程安全问题。 + * 一旦删除对旧GnuTLS和OpenSSL库的支持,此函数将被弃用,并且此函数不再有任何用途。 + */ + avformat_network_init(); + isFirst = false; + } +} + +/** + * @brief 打开媒体文件,或者流媒体,例如rtmp、strp、http + * @param url 视频地址 + * @return true:成功 false:失败 + */ +bool VideoDecode::open(const QString &url) +{ + if(url.isNull()) return false; + + AVDictionary* dict = nullptr; + av_dict_set(&dict, "rtsp_transport", "tcp", 0); // 设置rtsp流使用tcp打开,如果打开失败错误信息为【Error number -135 occurred】可以切换(UDP、tcp、udp_multicast、http),比如vlc推流就需要使用udp打开 + av_dict_set(&dict, "max_delay", "3", 0); // 设置最大复用或解复用延迟(以微秒为单位)。当通过【UDP】 接收数据时,解复用器尝试重新排序接收到的数据包(因为它们可能无序到达,或者数据包可能完全丢失)。这可以通过将最大解复用延迟设置为零(通过max_delayAVFormatContext 字段)来禁用。 + av_dict_set(&dict, "timeout", "1000000", 0); // 以微秒为单位设置套接字 TCP I/O 超时,如果等待时间过短,也可能会还没连接就返回了。 + + // 打开输入流并返回解封装上下文 + int ret = avformat_open_input(&m_formatContext, // 返回解封装上下文 + url.toStdString().data(), // 打开视频地址 + nullptr, // 如果非null,此参数强制使用特定的输入格式。自动选择解封装器(文件格式) + &dict); // 参数设置 + // 释放参数字典 + if(dict) + { + av_dict_free(&dict); + } + // 打开视频失败 + if(ret < 0) + { + showError(ret); + free(); + return false; + } + + // 读取媒体文件的数据包以获取流信息。 + ret = avformat_find_stream_info(m_formatContext, nullptr); + if(ret < 0) + { + showError(ret); + free(); + return false; + } + m_totalTime = m_formatContext->duration / (AV_TIME_BASE / 1000); // 计算视频总时长(毫秒) +#if PRINT_LOG + qDebug() << QString("视频总时长:%1 ms,[%2]").arg(m_totalTime).arg(QTime::fromMSecsSinceStartOfDay(int(m_totalTime)).toString("HH:mm:ss zzz")); +#endif + + // 通过AVMediaType枚举查询视频流ID(也可以通过遍历查找),最后一个参数无用 + m_videoIndex = av_find_best_stream(m_formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0); + if(m_videoIndex < 0) + { + showError(m_videoIndex); + free(); + return false; + } + + AVStream* videoStream = m_formatContext->streams[m_videoIndex]; // 通过查询到的索引获取视频流 + + // 获取视频图像分辨率(AVStream中的AVCodecContext在新版本中弃用,改为使用AVCodecParameters) + m_size.setWidth(videoStream->codecpar->width); + m_size.setHeight(videoStream->codecpar->height); + m_frameRate = rationalToDouble(&videoStream->avg_frame_rate); // 视频帧率 + + // 通过解码器ID获取视频解码器(新版本返回值必须使用const) + const AVCodec* codec = avcodec_find_decoder(videoStream->codecpar->codec_id); + m_totalFrames = videoStream->nb_frames; + +#if PRINT_LOG + qDebug() << QString("分辨率:[w:%1,h:%2] 帧率:%3 总帧数:%4 解码器:%5") + .arg(m_size.width()).arg(m_size.height()).arg(m_frameRate).arg(m_totalFrames).arg(codec->name); +#endif + + // 分配AVCodecContext并将其字段设置为默认值。 + m_codecContext = avcodec_alloc_context3(codec); + if(!m_codecContext) + { +#if PRINT_LOG + qWarning() << "创建视频解码器上下文失败!"; +#endif + free(); + return false; + } + + // 使用视频流的codecpar为解码器上下文赋值 + ret = avcodec_parameters_to_context(m_codecContext, videoStream->codecpar); + if(ret < 0) + { + showError(ret); + free(); + return false; + } + + m_codecContext->flags2 |= AV_CODEC_FLAG2_FAST; // 允许不符合规范的加速技巧。 + m_codecContext->thread_count = 8; // 使用8线程解码 + + // 初始化解码器上下文,如果之前avcodec_alloc_context3传入了解码器,这里设置NULL就可以 + ret = avcodec_open2(m_codecContext, nullptr, nullptr); + if(ret < 0) + { + showError(ret); + free(); + return false; + } + + // 分配AVPacket并将其字段设置为默认值。 + m_packet = av_packet_alloc(); + if(!m_packet) + { +#if PRINT_LOG + qWarning() << "av_packet_alloc() Error!"; +#endif + free(); + return false; + } + // 分配AVFrame并将其字段设置为默认值。 + m_frame = av_frame_alloc(); + if(!m_frame) + { +#if PRINT_LOG + qWarning() << "av_frame_alloc() Error!"; +#endif + free(); + return false; + } + + // 分配图像空间 + int size = av_image_get_buffer_size(AV_PIX_FMT_RGBA, m_size.width(), m_size.height(), 4); + /** + * 【注意:】这里可以多分配一些,否则如果只是安装size分配,大部分视频图像数据拷贝没有问题, + * 但是少部分视频图像在使用sws_scale()拷贝时会超出数组长度,在使用使用msvc debug模式时delete[] m_buffer会报错(HEAP CORRUPTION DETECTED: after Normal block(#32215) at 0x000001AC442830370.CRT delected that the application wrote to memory after end of heap buffer) + * 特别是这个视频流http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4 + */ + m_buffer = new uchar[size + 1000]; // 这里多分配1000个字节就基本不会出现拷贝超出的情况了,反正不缺这点内存 +// m_image = new QImage(m_buffer, m_size.width(), m_size.height(), QImage::Format_RGBA8888); // 这种方式分配内存大部分情况下也可以,但是因为存在拷贝超出数组的情况,delete时也会报错 + m_end = false; + return true; +} + +/** + * @brief + * @return + */ +AVFrame* VideoDecode::read() +{ + // 如果没有打开则返回 + if(!m_formatContext) + { + return nullptr; + } + + // 读取下一帧数据 + int readRet = av_read_frame(m_formatContext, m_packet); + if(readRet < 0) + { + avcodec_send_packet(m_codecContext, m_packet); // 读取完成后向解码器中传如空AVPacket,否则无法读取出最后几帧 + } + else + { + if(m_packet->stream_index == m_videoIndex) // 如果是图像数据则进行解码 + { + // 计算当前帧时间(毫秒) +#if 1 // 方法一:适用于所有场景,但是存在一定误差 + m_packet->pts = qRound64(m_packet->pts * (1000 * rationalToDouble(&m_formatContext->streams[m_videoIndex]->time_base))); + m_packet->dts = qRound64(m_packet->dts * (1000 * rationalToDouble(&m_formatContext->streams[m_videoIndex]->time_base))); +#else // 方法二:适用于播放本地视频文件,计算每一帧时间较准,但是由于网络视频流无法获取总帧数,所以无法适用 + m_obtainFrames++; + m_packet->pts = qRound64(m_obtainFrames * (qreal(m_totalTime) / m_totalFrames)); +#endif + // 将读取到的原始数据包传入解码器 + int ret = avcodec_send_packet(m_codecContext, m_packet); + if(ret < 0) + { + showError(ret); + } + } + } + av_packet_unref(m_packet); // 释放数据包,引用计数-1,为0时释放空间 + + av_frame_unref(m_frame); + int ret = avcodec_receive_frame(m_codecContext, m_frame); + if(ret < 0) + { + av_frame_unref(m_frame); + if(readRet < 0) + { + m_end = true; // 当无法读取到AVPacket并且解码器中也没有数据时表示读取完成 + } + return nullptr; + } + + m_pts = m_frame->pts; + + return m_frame; +} + +/** + * @brief 关闭视频播放并释放内存 + */ +void VideoDecode::close() +{ + clear(); + free(); + + m_totalTime = 0; + m_videoIndex = 0; + m_totalFrames = 0; + m_obtainFrames = 0; + m_pts = 0; + m_frameRate = 0; + m_size = QSize(0, 0); +} + +/** + * @brief 视频是否读取完成 + * @return + */ +bool VideoDecode::isEnd() +{ + return m_end; +} + +/** + * @brief 返回当前帧图像播放时间 + * @return + */ +const qint64 &VideoDecode::pts() +{ + return m_pts; +} + +/** + * @brief 显示ffmpeg函数调用异常信息 + * @param err + */ +void VideoDecode::showError(int err) +{ +#if PRINT_LOG + memset(m_error, 0, ERROR_LEN); // 将数组置零 + av_strerror(err, m_error, ERROR_LEN); + qWarning() << "DecodeVideo Error:" << m_error; +#else + Q_UNUSED(err) +#endif +} + +/** + * @brief 将AVRational转换为double,用于计算帧率 + * @param rational + * @return + */ +qreal VideoDecode::rationalToDouble(AVRational* rational) +{ + qreal frameRate = (rational->den == 0) ? 0 : (qreal(rational->num) / rational->den); + return frameRate; +} + +/** + * @brief 清空读取缓冲 + */ +void VideoDecode::clear() +{ + // 因为avformat_flush不会刷新AVIOContext (s->pb)。如果有必要,在调用此函数之前调用avio_flush(s->pb)。 + if(m_formatContext && m_formatContext->pb) + { + avio_flush(m_formatContext->pb); + } + if(m_formatContext) + { + avformat_flush(m_formatContext); // 清理读取缓冲 + } +} + +void VideoDecode::free() +{ + // 释放上下文swsContext。 + if(m_swsContext) + { + sws_freeContext(m_swsContext); + m_swsContext = nullptr; // sws_freeContext不会把上下文置NULL + } + // 释放编解码器上下文和与之相关的所有内容,并将NULL写入提供的指针 + if(m_codecContext) + { + avcodec_free_context(&m_codecContext); + } + // 关闭并失败m_formatContext,并将指针置为null + if(m_formatContext) + { + avformat_close_input(&m_formatContext); + } + if(m_packet) + { + av_packet_free(&m_packet); + } + if(m_frame) + { + av_frame_free(&m_frame); + } + if(m_buffer) + { + delete [] m_buffer; + m_buffer = nullptr; + } +} diff --git a/FFmpegDemo/VideoPlayGL2/VideoPlay/videodecode.h b/FFmpegDemo/VideoPlayGL2/VideoPlay/videodecode.h new file mode 100644 index 0000000000000000000000000000000000000000..e3ec1af97180bdb8fba7739402c76e0d33bab8a8 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/VideoPlay/videodecode.h @@ -0,0 +1,62 @@ +/****************************************************************************** + * @文件名 videodecode.h + * @功能 视频解码类,在这个类中调用ffmpeg打开视频进行解码 + * + * @开发者 mhf + * @邮箱 1603291350@qq.com + * @时间 2022/09/15 + * @备注 + *****************************************************************************/ +#ifndef VIDEODECODE_H +#define VIDEODECODE_H + +#include +#include + +struct AVFormatContext; +struct AVCodecContext; +struct AVRational; +struct AVPacket; +struct AVFrame; +struct SwsContext; +struct AVBufferRef; +class QImage; + +class VideoDecode +{ +public: + VideoDecode(); + ~VideoDecode(); + + bool open(const QString& url = QString()); // 打开媒体文件,或者流媒体rtmp、strp、http + AVFrame* read(); // 读取视频图像 + void close(); // 关闭 + bool isEnd(); // 是否读取完成 + const qint64& pts(); // 获取当前帧显示时间 + +private: + void initFFmpeg(); // 初始化ffmpeg库(整个程序中只需加载一次) + void showError(int err); // 显示ffmpeg执行错误时的错误信息 + qreal rationalToDouble(AVRational* rational); // 将AVRational转换为double + void clear(); // 清空读取缓冲 + void free(); // 释放 + +private: + AVFormatContext* m_formatContext = nullptr; // 解封装上下文 + AVCodecContext* m_codecContext = nullptr; // 解码器上下文 + SwsContext* m_swsContext = nullptr; // 图像转换上下文 + AVPacket* m_packet = nullptr; // 数据包 + AVFrame* m_frame = nullptr; // 解码后的视频帧 + int m_videoIndex = 0; // 视频流索引 + qint64 m_totalTime = 0; // 视频总时长 + qint64 m_totalFrames = 0; // 视频总帧数 + qint64 m_obtainFrames = 0; // 视频当前获取到的帧数 + qint64 m_pts = 0; // 图像帧的显示时间 + qreal m_frameRate = 0; // 视频帧率 + QSize m_size; // 视频分辨率大小 + char* m_error = nullptr; // 保存异常信息 + bool m_end = false; // 视频读取完成 + uchar* m_buffer = nullptr; // YUV图像需要转换位RGBA图像,这里保存转换后的图形数据 +}; + +#endif // VIDEODECODE_H diff --git a/FFmpegDemo/VideoPlayGL2/VideoPlayGL2.pro b/FFmpegDemo/VideoPlayGL2/VideoPlayGL2.pro new file mode 100644 index 0000000000000000000000000000000000000000..de8624af090987c271e53b68e15a3b298b7fe1cf --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/VideoPlayGL2.pro @@ -0,0 +1,66 @@ +#--------------------------------------------------------------------------------------- +# @功能: 使用ffmpeg音视频库【软解码】实现的视频播放器; +# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit 32bit(也支持其它编译器) +# @Qt IDE: D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator +# +# @开发者 mhf +# @邮箱 1603291350@qq.com +# @时间 2022-09-14 14:13:52 +# @备注 1、使用ffmpeg音视频库【软解码】实现的视频播放器; +# 2、支持打开本地视频文件(如mp4、mov、avi等)、网络视频流(rtsp、rtmp、http等); +# 3、支持视频匀速播放; +# 4、采用【OpenGL显示YUV】图像,支持自适应窗口缩放,支持使用QOpenGLWidget、QOpenGLWindow显示; +# 5、将YUV转RGB的步骤由CPU转换改为使用GPU转换,降低CPU占用率; +# 6、视频播放支持实时开始/关闭、暂停/继续播放; +# 7、视频解码、线程控制、显示各部分功能分离,低耦合度。 +# 8、采用最新的5.1.2版本ffmpeg库进行开发,超详细注释信息,将所有踩过的坑、解决办法、注意事项都得很写清楚。 +#--------------------------------------------------------------------------------------- +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +CONFIG += c++11 +DEFINES += QT_DEPRECATED_WARNINGS + +SOURCES += \ + playimage.cpp \ + main.cpp \ + widget.cpp + +HEADERS += \ + playimage.h \ + widget.h + +FORMS += \ + widget.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +# ffmpeg读取视频图像模块 +include(./VideoPlay/VideoPlay.pri) +INCLUDEPATH += ./VideoPlay + +# 定义程序版本号 +VERSION = 1.0.0 +DEFINES += APP_VERSION=\\\"$$VERSION\\\" +TARGET = VideoPlayGL2 + +contains(QT_ARCH, i386){ # 使用32位编译器 +DESTDIR = $$PWD/../bin # 程序输出路径 +}else{ +DESTDIR = $$PWD/../bin64 # 使用64位编译器 +} +# msvc 编译器使用utf-8编码 +msvc { +QMAKE_CFLAGS += /utf-8 +QMAKE_CXXFLAGS += /utf-8 +} + +DISTFILES += \ + fragment.fsh \ + vertex.vsh + +RESOURCES += \ + rc.qrc diff --git a/FFmpegDemo/VideoPlayGL2/fragment.fsh b/FFmpegDemo/VideoPlayGL2/fragment.fsh new file mode 100644 index 0000000000000000000000000000000000000000..c80fbe4d5f7f44a75e9b5cdb5d579986d5a1d6f0 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/fragment.fsh @@ -0,0 +1,22 @@ +#version 330 core +in vec2 TexCord; // 纹理坐标 +uniform sampler2D texture; // 纹理采样器 + +uniform sampler2D tex_y; +uniform sampler2D tex_u; +uniform sampler2D tex_v; + +void main() +{ + vec3 yuv; + vec3 rgb; + + yuv.x = texture2D(tex_y, TexCord).r; + yuv.y = texture2D(tex_u, TexCord).r-0.5; + yuv.z = texture2D(tex_v, TexCord).r-0.5; + + rgb = mat3(1.0, 1.0, 1.0, + 0.0, -0.39465, 2.03211, + 1.13983, -0.58060, 0.0) * yuv; + gl_FragColor = vec4(rgb, 1.0); +} diff --git a/FFmpegDemo/VideoPlayGL2/main.cpp b/FFmpegDemo/VideoPlayGL2/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0a4ec26478f6b9aba3e1747ec464ea0c26dd5b9 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + return a.exec(); +} diff --git a/FFmpegDemo/VideoPlayGL2/playimage.cpp b/FFmpegDemo/VideoPlayGL2/playimage.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64894329ba212e10f7defe1154ff6444b0483e47 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/playimage.cpp @@ -0,0 +1,235 @@ +#include "playimage.h" + +extern "C" { // 用C规则编译指定的代码 +#include "libavcodec/avcodec.h" + +} + +#if USE_WINDOW +PlayImage::PlayImage(QOpenGLWindow::UpdateBehavior updateBehavior, QWindow *parent):QOpenGLWindow(updateBehavior, parent) +{ + // 初始化视图大小,由于Shader里面有YUV转RGB的代码,会初始化显示为绿色,这里通过将视图大小设置为0避免显示绿色背景 + m_pos = QPointF(0, 0); + m_zoomSize = QSize(0, 0); +} +#else +PlayImage::PlayImage(QWidget *parent, Qt::WindowFlags f): QOpenGLWidget(parent, f) +{ + // 初始化视图大小,由于Shader里面有YUV转RGB的代码,会初始化显示为绿色,这里通过将视图大小设置为0避免显示绿色背景 + m_pos = QPointF(0, 0); + m_zoomSize = QSize(0, 0); +} +#endif + + +PlayImage::~PlayImage() +{ + if(!isValid()) return; // 如果控件和OpenGL资源(如上下文)已成功初始化,则返回true。 + this->makeCurrent(); // 通过将相应的上下文设置为当前上下文并在该上下文中绑定帧缓冲区对象,为呈现此小部件的OpenGL内容做准备。 + // 释放纹理 + if(m_texY) + { + m_texY->destroy(); + delete m_texY; + } + if(m_texU) + { + m_texU->destroy(); + delete m_texU; + } + if(m_texV) + { + m_texV->destroy(); + delete m_texV; + } + this->doneCurrent(); // 释放上下文 + // 释放 + glDeleteBuffers(1, &VBO); + glDeleteBuffers(1, &EBO); + glDeleteVertexArrays(1, &VAO); +} + +void PlayImage::repaint(AVFrame *frame) +{ + if(!frame) return; + + // 当切换显示的视频后,如果分辨率不同则需要重新创建纹理,否则会崩溃 + if(frame->width != m_size.width() || frame->height != m_size.height()) + { + if(m_texY && m_texU && m_texV) + { + m_texY->destroy(); + m_texU->destroy(); + m_texV->destroy(); + delete m_texY; + delete m_texU; + delete m_texV; + m_texY = nullptr; + m_texU = nullptr; + m_texV = nullptr; + } + } + if(!m_texY) // 初始化纹理 + { + // 创建2D纹理 + m_texY = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_texU = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_texV = new QOpenGLTexture(QOpenGLTexture::Target2D); + + // 设置纹理大小 + m_texY->setSize(frame->width, frame->height); + m_texU->setSize(frame->width / 2, frame->height / 2); + m_texV->setSize(frame->width / 2, frame->height / 2); + + // 设置放大、缩小过滤器 + m_texY->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Linear); + m_texU->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Linear); + m_texV->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Linear); + + // 设置图像格式 + m_texY->setFormat(QOpenGLTexture::R8_UNorm); + m_texU->setFormat(QOpenGLTexture::R8_UNorm); + m_texV->setFormat(QOpenGLTexture::R8_UNorm); + + // 分配内存 + m_texY->allocateStorage(); + m_texU->allocateStorage(); + m_texV->allocateStorage(); + + // 记录图像分辨率 + m_size.setWidth(frame->width); + m_size.setHeight(frame->height); + resizeGL(this->width(), this->height()); + } + m_options.setImageHeight(frame->height); + m_options.setRowLength(frame->linesize[0]); + m_texY->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, static_cast(frame->data[0]), &m_options); // 设置图像数据 Y + m_options.setRowLength(frame->linesize[1]); + m_texU->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, static_cast(frame->data[1]), &m_options); // 设置图像数据 U + m_options.setRowLength(frame->linesize[2]); + m_texV->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, static_cast(frame->data[2]), &m_options); // 设置图像数据 V + + av_frame_unref(frame); // 取消引用帧引用的所有缓冲区并重置帧字段。 + + this->update(); +} + +// 三个顶点坐标XYZ,VAO、VBO数据播放,范围时[-1 ~ 1]直接 +static GLfloat vertices[] = { // 前三列点坐标,后两列为纹理坐标 + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 右上角 + 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // 右下 + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // 左下 + -1.0f, 1.0f, 0.0f, 0.0f, 1.0f // 左上 +}; +static GLuint indices[] = { + 0, 1, 3, + 1, 2, 3 +}; +void PlayImage::initializeGL() +{ + initializeOpenGLFunctions(); + + // 加载shader脚本程序 + m_program = new QOpenGLShaderProgram(this); + m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vertex.vsh"); + m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fragment.fsh"); + m_program->link(); + + // 绑定YUV 变量值 + m_program->bind(); + m_program->setUniformValue("tex_y", 0); + m_program->setUniformValue("tex_u", 1); + m_program->setUniformValue("tex_v", 2); + + // 返回属性名称在此着色器程序的参数列表中的位置。如果名称不是此着色器程序的有效属性,则返回-1。 + GLuint posAttr = GLuint(m_program->attributeLocation("aPos")); + GLuint texCord = GLuint(m_program->attributeLocation("aTexCord")); + + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glGenBuffers(1, &EBO); // 创建一个EBO + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + + + // 为当前绑定到的缓冲区对象创建一个新的数据存储target。任何预先存在的数据存储都将被删除。 + glBufferData(GL_ARRAY_BUFFER, // 为VBO缓冲绑定顶点数据 + sizeof (vertices), // 数组字节大小 + vertices, // 需要绑定的数组 + GL_STATIC_DRAW); // 指定数据存储的预期使用模式,GL_STATIC_DRAW: 数据几乎不会改变 + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // 将顶点索引数组传入EBO缓存 + // 设置顶点坐标数据 + glVertexAttribPointer(posAttr, // 指定要修改的通用顶点属性的索引 + 3, // 指定每个通用顶点属性的组件数(如vec3:3,vec4:4) + GL_FLOAT, // 指定数组中每个组件的数据类型(数组中一行有几个数) + GL_FALSE, // 指定在访问定点数据值时是否应规范化 ( GL_TRUE) 或直接转换为定点值 ( GL_FALSE),如果vertices里面单个数超过-1或者1可以选择GL_TRUE + 5 * sizeof(GLfloat), // 指定连续通用顶点属性之间的字节偏移量。 + nullptr); // 指定当前绑定到目标的缓冲区的数据存储中数组中第一个通用顶点属性的第一个组件的偏移量。初始值为0 (一个数组从第几个字节开始读) + // 启用通用顶点属性数组 + glEnableVertexAttribArray(posAttr); // 属性索引是从调用glGetAttribLocation接收的,或者传递给glBindAttribLocation。 + + // 设置纹理坐标数据 + glVertexAttribPointer(texCord, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), reinterpret_cast(3 * sizeof (GLfloat))); // 指定当前绑定到目标的缓冲区的数据存储中数组中第一个通用顶点属性的第一个组件的偏移量。初始值为0 (一个数组从第几个字节开始读) + // 启用通用顶点属性数组 + glEnableVertexAttribArray(texCord); // 属性索引是从调用glGetAttribLocation接收的,或者传递给glBindAttribLocation。 + + // 释放 + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); // 设置为零以破坏现有的顶点数组对象绑定 + + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // 指定颜色缓冲区的清除值(背景色) + + +} + +void PlayImage::resizeGL(int w, int h) +{ + if(m_size.width() < 0 || m_size.height() < 0) return; + + // 计算需要显示图片的窗口大小,用于实现长宽等比自适应显示 + if((double(w) / h) < (double(m_size.width()) / m_size.height())) + { + m_zoomSize.setWidth(w); + m_zoomSize.setHeight(((double(w) / m_size.width()) * m_size.height())); // 这里不使用QRect,使用QRect第一次设置时有误差bug + } + else + { + m_zoomSize.setHeight(h); + m_zoomSize.setWidth((double(h) / m_size.height()) * m_size.width()); + } + m_pos.setX(double(w - m_zoomSize.width()) / 2); + m_pos.setY(double(h - m_zoomSize.height()) / 2); + this->update(QRect(0, 0, w, h)); +} + +void PlayImage::paintGL() +{ + glClear(GL_COLOR_BUFFER_BIT); // 将窗口的位平面区域(背景)设置为先前由glClearColor、glClearDepth和选择的值 + glViewport(m_pos.x(), m_pos.y(), m_zoomSize.width(), m_zoomSize.height()); // 设置视图大小实现图片自适应 + + m_program->bind(); // 绑定着色器 + // 绑定纹理 + if(m_texY && m_texU && m_texV) + { + m_texY->bind(0); + m_texU->bind(1); + m_texV->bind(2); + } + + glBindVertexArray(VAO); // 绑定VAO + + glDrawElements(GL_TRIANGLES, // 绘制的图元类型 + 6, // 指定要渲染的元素数(点数) + GL_UNSIGNED_INT, // 指定索引中值的类型(indices) + nullptr); // 指定当前绑定到GL_ELEMENT_array_buffer目标的缓冲区的数据存储中数组中第一个索引的偏移量。 + glBindVertexArray(0); + if(m_texY && m_texU && m_texV) + { + m_texY->release(); + m_texU->release(); + m_texV->release(); + } + m_program->release(); +} diff --git a/FFmpegDemo/VideoPlayGL2/playimage.h b/FFmpegDemo/VideoPlayGL2/playimage.h new file mode 100644 index 0000000000000000000000000000000000000000..da5455972f7040ce8c9ecf03020850660d79d3f3 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/playimage.h @@ -0,0 +1,63 @@ +/****************************************************************************** + * @文件名 playimage.h + * @功能 使用OpenGL实现YUV图像的绘制,可通过USE_WINDOW宏切换使用QOpenGLWindow还是QOpenGLWidget + * + * @开发者 mhf + * @邮箱 1603291350@qq.com + * @时间 2022/10/14 + * @备注 + *****************************************************************************/ +#ifndef PLAYIMAGE_H +#define PLAYIMAGE_H + +#include +#include +#include +#include +#include + +struct AVFrame; + +#define USE_WINDOW 0 // 1:使用QOpenGLWindow显示, 0:使用QOpenGLWidget显示 + +#if USE_WINDOW +#include +class PlayImage : public QOpenGLWindow, public QOpenGLFunctions_3_3_Core +#else +#include +class PlayImage : public QOpenGLWidget, public QOpenGLFunctions_3_3_Core +#endif +{ + Q_OBJECT +public: +#if USE_WINDOW + explicit PlayImage(UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = nullptr); +#else + explicit PlayImage(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); +#endif + ~PlayImage() override; + + void repaint(AVFrame* frame); // 重绘 + + +protected: + void initializeGL() override; // 初始化gl + void resizeGL(int w, int h) override; // 窗口尺寸变化 + void paintGL() override; // 刷新显示 + +private: + QOpenGLShaderProgram* m_program = nullptr; + QOpenGLTexture* m_texY = nullptr; + QOpenGLTexture* m_texU = nullptr; + QOpenGLTexture* m_texV = nullptr; + QOpenGLPixelTransferOptions m_options; + + GLuint VBO = 0; // 顶点缓冲对象,负责将数据从内存放到缓存,一个VBO可以用于多个VAO + GLuint VAO = 0; // 顶点数组对象,任何随后的顶点属性调用都会储存在这个VAO中,一个VAO可以有多个VBO + GLuint EBO = 0; // 元素缓冲对象,它存储 OpenGL 用来决定要绘制哪些顶点的索引 + QSize m_size; + QSizeF m_zoomSize; + QPointF m_pos; +}; + +#endif // PLAYIMAGE_H diff --git a/FFmpegDemo/VideoPlayGL2/rc.qrc b/FFmpegDemo/VideoPlayGL2/rc.qrc new file mode 100644 index 0000000000000000000000000000000000000000..28293086375e5700d3257397d189b3c908a60025 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/rc.qrc @@ -0,0 +1,6 @@ + + + fragment.fsh + vertex.vsh + + diff --git a/FFmpegDemo/VideoPlayGL2/vertex.vsh b/FFmpegDemo/VideoPlayGL2/vertex.vsh new file mode 100644 index 0000000000000000000000000000000000000000..fb7430e2ae354c8ea9808721ed4a9599562b6354 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/vertex.vsh @@ -0,0 +1,9 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCord; +out vec2 TexCord; // 纹理坐标 +void main() +{ + gl_Position = vec4(aPos.x, -aPos.y, aPos.z, 1.0); // 图像坐标和OpenGL坐标Y轴相反, + TexCord = aTexCord; +} diff --git a/FFmpegDemo/VideoPlayGL2/widget.cpp b/FFmpegDemo/VideoPlayGL2/widget.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3dfdb60a99c8375ee002c945f93677fe3606ec85 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/widget.cpp @@ -0,0 +1,106 @@ +#include "widget.h" +#include "ui_widget.h" + +#include + +extern "C" { // 用C规则编译指定的代码 +#include "libavcodec/avcodec.h" +} +Q_DECLARE_METATYPE(AVFrame) //注册结构体,否则无法通过信号传递AVFrame + +Widget::Widget(QWidget *parent) + : QWidget(parent) + , ui(new Ui::Widget) +{ + ui->setupUi(this); + this->setWindowTitle(QString("Qt+ffmpeg视频播放(软解码 + OpenGL显示YUV)Demo V%1").arg(APP_VERSION)); + + + // 使用QOpenGLWindow绘制 + playImage = new PlayImage; +#if USE_WINDOW + ui->verticalLayout->addWidget(QWidget::createWindowContainer(playImage)); // 这一步加载速度要比OpenGLWidget慢一点 +#else + ui->verticalLayout->addWidget(playImage); +#endif + + m_readThread = new ReadThread(); + connect(m_readThread, &ReadThread::repaint, playImage, &PlayImage::repaint, Qt::BlockingQueuedConnection); + connect(m_readThread, &ReadThread::playState, this, &Widget::on_playState); +} + +Widget::~Widget() +{ + // 释放视频读取线程 + if(m_readThread) + { + m_readThread->close(); + m_readThread->wait(); + delete m_readThread; + } + delete ui; +} + +/** + * @brief 获取本地视频路径 + */ +void Widget::on_but_file_clicked() +{ + QString strName = QFileDialog::getOpenFileName(this, "选择播放视频~!", "/", "视频 (*.mp4 *.m4v *.mov *.avi *.flv);; 其它(*)"); + if(strName.isEmpty()) + { + return; + } + ui->com_url->setCurrentText(strName); +} + +/** + * @brief 视频播放/停止 + */ +void Widget::on_but_open_clicked() +{ + if(ui->but_open->text() == "开始播放") + { + m_readThread->open(ui->com_url->currentText()); + } + else + { + m_readThread->close(); + } +} + +/** + * @brief 视频暂停/继续 + */ +void Widget::on_but_pause_clicked() +{ + if(ui->but_pause->text() == "暂停") + { + m_readThread->pause(true); + ui->but_pause->setText("继续"); + } + else + { + m_readThread->pause(false); + ui->but_pause->setText("暂停"); + } +} + +/** + * @brief 根据视频播放状态切换界面设置 + * @param state + */ +void Widget::on_playState(ReadThread::PlayState state) +{ + if(state == ReadThread::play) + { + this->setWindowTitle(QString("正在播放:%1").arg(m_readThread->url())); + ui->but_open->setText("停止播放"); + } + else + { + ui->but_open->setText("开始播放"); + ui->but_pause->setText("暂停"); + this->setWindowTitle(QString("Qt+ffmpeg视频播放(软解码 + OpenGL显示YUV)Demo V%1").arg(APP_VERSION)); + } +} diff --git a/FFmpegDemo/VideoPlayGL2/widget.h b/FFmpegDemo/VideoPlayGL2/widget.h new file mode 100644 index 0000000000000000000000000000000000000000..9ed58d1edec61ebb08c86594944e76039b9e5067 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/widget.h @@ -0,0 +1,35 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include +#include "readthread.h" +#include "playimage.h" + +QT_BEGIN_NAMESPACE +namespace Ui { class Widget; } +QT_END_NAMESPACE + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + +private slots: + void on_but_file_clicked(); + + void on_but_open_clicked(); + + void on_but_pause_clicked(); + + void on_playState(ReadThread::PlayState state); + +private: + Ui::Widget *ui; + + PlayImage* playImage = nullptr; + ReadThread* m_readThread = nullptr; +}; +#endif // WIDGET_H diff --git a/FFmpegDemo/VideoPlayGL2/widget.ui b/FFmpegDemo/VideoPlayGL2/widget.ui new file mode 100644 index 0000000000000000000000000000000000000000..a0091763fb2c311c8aa44a8288d6781624e6ed22 --- /dev/null +++ b/FFmpegDemo/VideoPlayGL2/widget.ui @@ -0,0 +1,137 @@ + + + Widget + + + + 0 + 0 + 800 + 600 + + + + Widget + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + true + + + QComboBox::InsertAtBottom + + + QComboBox::AdjustToMinimumContentsLengthWithIcon + + + false + + + + rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 + + + + + rtmp://ns8.indexforce.com/home/mystream + + + + + rtmp://58.200.131.2:1935/livetv/cctv1 + + + + + http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4 + + + + + http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8 + + + + + http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4 + + + + + http://vjs.zencdn.net/v/oceans.mp4 + + + + + https://media.w3.org/2010/05/sintel/trailer.mp4 + + + + + https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv + + + + + + + + + 0 + 0 + + + + 选择 + + + + + + + + 0 + 0 + + + + 开始播放 + + + + + + + + 0 + 0 + + + + 暂停 + + + + + + + + + + diff --git a/FFmpegDemo/VideoPlayHW/widget.cpp b/FFmpegDemo/VideoPlayHW/widget.cpp index 1aa489f745c6302f46c721723a0452fa05a17a2f..890452fd291fcff4526f0f024dd533dab56c344f 100644 --- a/FFmpegDemo/VideoPlayHW/widget.cpp +++ b/FFmpegDemo/VideoPlayHW/widget.cpp @@ -116,6 +116,6 @@ void Widget::on_playState(ReadThread::PlayState state) { ui->but_open->setText("开始播放"); ui->but_pause->setText("暂停"); - this->setWindowTitle(QString("Qt+ffmpeg视频播放Demo V%1").arg(APP_VERSION)); + this->setWindowTitle(QString("Qt+ffmpeg视频播放(硬解码)Demo V%1").arg(APP_VERSION)); } } diff --git a/QSqlDemo/QSql.assets/SignIn-tuya.gif b/QSqlDemo/QSql.assets/SignIn-tuya.gif new file mode 100644 index 0000000000000000000000000000000000000000..2f7641482bb5daa9062e24a9cfd55f4ad20afcc7 Binary files /dev/null and b/QSqlDemo/QSql.assets/SignIn-tuya.gif differ diff --git a/QSqlDemo/QSql.assets/TableModel2-tuya.gif b/QSqlDemo/QSql.assets/TableModel2-tuya.gif new file mode 100644 index 0000000000000000000000000000000000000000..434080a2897db140468f5361a647578d970ff6ea Binary files /dev/null and b/QSqlDemo/QSql.assets/TableModel2-tuya.gif differ diff --git a/QSqlDemo/QSql.md b/QSqlDemo/QSql.md index f2cdc2aec85d7a778650c05b11440aa760cd5b72..c8f5c4dbc2c90a7f87065c05c8bd495be405d481 100644 --- a/QSqlDemo/QSql.md +++ b/QSqlDemo/QSql.md @@ -9,9 +9,11 @@ | 工程 | 功能 | | -------------------- | ------------------------------------------------------------ | | TableModel | 展示了使用QSqlTableModel来显示数据库信息 | +| TableModel2 | 在TableModel的基础上实现创建空白数据行、自增key | | RelationalTableModel | 展示了使用QSqlRelationalTableModel来使用数据库[外键] | | SqlWidgetMapper | 将数据库内容通过QDataWidgetMapper将模型数据映射到小部件 | | CachedTable | 通过QSqlTableModel显示和修改数据,通过按键保存修改内容和恢复修改内容 | +| SignIn | 使用QSqlite数据库实现用户登录、后台管理用户功能 | @@ -21,9 +23,21 @@ ![image-20220518112255014](QSql.assets/image-20220518112255014.png) + + +### 1.2 TableModel2 + +> 这个Demo中展示了如何使用具有表视图的专用 SQL 表模型(QSqlTableModel)来编辑数据库中的信息 +> 1、通过按键新建 空白数据行; +> 2、使用自增Key; +> 3、通过按键更新数据; +> 4、判断表是否存在,不存在则创建。 + +![TableModel2-tuya](QSql.assets/TableModel2-tuya.gif) + ### 1.2 RelationalTableModel -![RelationalTableModel](C:/Users/mhf/Desktop/RelationalTableModel.gif) +![RelationalTableModel](QSql.assets/RelationalTableModel.gif) ### 1.3 SqlWidgetMapper @@ -31,4 +45,22 @@ ### 1.4 CachedTable -![CachedTable](C:/Users/mhf/Desktop/CachedTable.gif) +![CachedTable](QSql.assets/CachedTable.gif) + + + +### 1.5 SignIn + +> 使用QSqlite数据库实现用户登录、后台管理用户功能 +> 1、通过按键新建 空白数据行; +> 2、使用自增Key; +> 3、通过按键更新数据; +> 4、判断表是否存在,不存在则创建。 +> 5、用户登录功能,默认创建超级管理员账号root +> 6、支持用户后台管理,通过后台创建、修改、删除用户 +> 7、用户分为超级管理员、普通管理员、普通用户三个等级; +> 8、超级管理员有所有权限,可创建、修改、删除普通管理员、普通用户; +> 9、普通管理员可创建、修改、删除普通用户,可新建、修改、查询数据; +> 10、普通用户不可修改用户信息,不可新建、修改数据库信息,只可查询。 + +![SignIn-tuya](QSql.assets/SignIn-tuya.gif) diff --git a/QSqlDemo/QSqlDemo.pro b/QSqlDemo/QSqlDemo.pro index 1caffb542a47a2c724b3e256dcfdfe667cd6ce09..1c889cc7313457d1a16705757efdbbb3756cdf97 100644 --- a/QSqlDemo/QSqlDemo.pro +++ b/QSqlDemo/QSqlDemo.pro @@ -5,4 +5,5 @@ SUBDIRS += \ RelationalTableModel \ # 本示例展示了如何使用QSqlRelationalTableModel(关系表模型)来可视化数据库中[外键]的使用。 SqlWidgetMapper \ # 通过QDataWidgetMapper将数据库数据映射到小部件 TableModel \ # 这个Demo中展示了如何使用具有表视图的专用 SQL 表模型(QSqlTableModel)来编辑数据库中的信息。 - TableModel2 # 这个Demo中展示了如何使用具有表视图的专用 SQL 表模型(QSqlTableModel)来编辑数据库中的信息,实现创建空白数据行、自增key + TableModel2\ # 这个Demo中展示了如何使用具有表视图的专用 SQL 表模型(QSqlTableModel)来编辑数据库中的信息,实现创建空白数据行、自增key + SignIn # 使用QSqlite数据库实现用户登录、后台管理用户功能 diff --git a/QSqlDemo/SignIn/SignIn.pro b/QSqlDemo/SignIn/SignIn.pro new file mode 100644 index 0000000000000000000000000000000000000000..06cc357e8977fadde58aa182e6ddaa95aa319e5b --- /dev/null +++ b/QSqlDemo/SignIn/SignIn.pro @@ -0,0 +1,63 @@ +#--------------------------------------------------------------------------------------- +# @功能: 使用QSqlite数据库实现用户登录、后台管理用户功能 +# 1、通过按键新建 空白数据行; +# 2、使用自增Key; +# 3、通过按键更新数据; +# 4、判断表是否存在,不存在则创建。 +# 5、用户登录功能,默认创建超级管理员账号root +# 6、支持用户后台管理,通过后台创建、修改、删除用户 +# 7、用户分为超级管理员、普通管理员、普通用户三个等级; +# 8、超级管理员有所有权限,可创建、修改、删除普通管理员、普通用户; +# 9、普通管理员可创建、修改、删除普通用户,可新建、修改、查询数据; +# 10、普通用户不可修改用户信息,不可新建、修改数据库信息,只可查询。 +# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit(也支持其它编译器) +# @Qt IDE: D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator +# +# @开发者 mhf +# @邮箱 1603291350@qq.com +# @时间 2022-10-07 11:29:40 +# @备注 +#--------------------------------------------------------------------------------------- +QT += core gui sql + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 +DEFINES += QT_DEPRECATED_WARNINGS + +SOURCES += \ + dialog.cpp \ + main.cpp \ + userbackstage.cpp \ + widget.cpp + +HEADERS += \ + dialog.h \ + userbackstage.h \ + widget.h + +FORMS += \ + dialog.ui \ + userbackstage.ui \ + widget.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +# 定义程序版本号 +VERSION = 1.0.0 +DEFINES += APP_VERSION=\\\"$$VERSION\\\" + +contains(QT_ARCH, i386){ # 使用32位编译器 +DESTDIR = $$PWD/../bin # 程序输出路径 +}else{ +DESTDIR = $$PWD/../bin64 # 使用64位编译器 +} + +# msvc 编译器使用utf-8编码 +msvc { +QMAKE_CFLAGS += /utf-8 +QMAKE_CXXFLAGS += /utf-8 +} diff --git a/QSqlDemo/SignIn/dialog.cpp b/QSqlDemo/SignIn/dialog.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3a92c982c858106cb5e4ef1fcc7681547858afb --- /dev/null +++ b/QSqlDemo/SignIn/dialog.cpp @@ -0,0 +1,157 @@ +#include "dialog.h" +#include "ui_dialog.h" + +#include +#include +#include + + + +bool Dialog::m_loggedIn = false; // 登录状态 +QString Dialog::m_userName; // 登录的用户名 +UserBackstage::UserType Dialog::m_userType; // 用户类型 + +Dialog::Dialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::Dialog) +{ + ui->setupUi(this); + + this->setWindowTitle("登录"); + + m_db = QSqlDatabase::addDatabase("QSQLITE"); // 使用数据库驱动(Qsqlite)和默认连接名称(qt_sql_default_connection)添加一个数据库 + m_db.setDatabaseName("SignIn.db"); // 使用文件数据库(可生成数据库文件,数据一直有效) + + if(!m_db.open()) // 打开数据库 + { + QMessageBox::critical(nullptr, "Error", "打开数据库失败!"); + return ; + } + + // 创建一个用于保存用户信息的表 + if(!isTableExists("User")) + { + QSqlQuery query; + + // 创建一个表person,包含id、firstname、lastname三个字段 + bool ret = query.exec("create table User (" + "id integer primary key," // 索引(自增key) + "userName varchar(20)," // 用户名 + "password varchar(20)," // 密码 + "type int)"); // 用户类型 + if(!ret) + { + qDebug() << "创建表失败:"; + } + else + { + QSqlQuery query; + query.prepare("insert into User(userName, password, type)" + "values (:userName, :password, :type)"); + query.bindValue(":userName", "root"); + query.bindValue(":password", "123456"); + query.bindValue(":type", int(UserBackstage::Root)); + query.exec(); + } + } +} + +Dialog::~Dialog() +{ + if(m_db.isOpen()) + { + m_db.close(); // 关闭数据库 + } + delete ui; +} + +/** + * @brief 返回登录状态 + * @return true登录 false未登录 + */ +bool Dialog::loggedIn() +{ + return m_loggedIn; +} + +/** + * @brief 返回登录的用户名 + * @return 如果没有则为空 + */ +QString Dialog::userName() +{ + return m_userName; +} + +/** + * @brief 返回登录的用户类型 + * @return + */ +UserBackstage::UserType Dialog::type() +{ + return m_userType; +} + +/** + * @brief 判断表是否存在 + * @param table + * @return + */ +bool Dialog::isTableExists(const QString &table) +{ + QSqlQuery query; + QString sql = QString("select * from sqlite_master where name = '%1';").arg(table); // 查询sqlite_master表中是否存在表名 + if(query.exec(sql)) + { + return query.next(); + } + return false; +} + +/** + * @brief 登录 + */ +void Dialog::on_but_signIn_clicked() +{ + QString userName = ui->line_user->text().trimmed(); + QString password = ui->line_password->text().trimmed(); + if(userName.isEmpty()) + { + QMessageBox::about(this, "注意!", "用户名不能为空"); + return; + } + if(password.isEmpty()) + { + QMessageBox::about(this, "注意!", "用户密码不能为空"); + return; + } + + // 从数据库中查询用户和密码,完成登录功能 + QSqlQuery query; + QString sql = QString("select * from User where userName = '%1';").arg(userName); // 查询用户 + if(query.exec(sql)) + { + if(query.next()) // true则用户存在 + { + if(password == query.value("password").toString()) // 密码相等 + { + m_userName = userName; + m_userType = UserBackstage::UserType(query.value("type").toInt()); + m_loggedIn = true; + this->close(); + } + else + { + QMessageBox::about(this, "注意", "输入密码错误!"); + } + } + else + { + QMessageBox::about(this, "注意", "用户不存在!"); + } + } + else + { + QMessageBox::about(this, "注意", "sql指令执行失败!"); + } +} diff --git a/QSqlDemo/SignIn/dialog.h b/QSqlDemo/SignIn/dialog.h new file mode 100644 index 0000000000000000000000000000000000000000..9c9e74858dedfd77dfe61e1222de7ac225f58f6a --- /dev/null +++ b/QSqlDemo/SignIn/dialog.h @@ -0,0 +1,36 @@ +#ifndef DIALOG_H +#define DIALOG_H + +#include +#include +#include "userbackstage.h" + +namespace Ui { +class Dialog; +} + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + explicit Dialog(QWidget *parent = nullptr); + ~Dialog(); + + static bool loggedIn(); + static QString userName(); + static UserBackstage::UserType type(); + +private slots: + bool isTableExists(const QString& table); + void on_but_signIn_clicked(); + +private: + Ui::Dialog *ui; + QSqlDatabase m_db; + static bool m_loggedIn; // 登录状态 + static QString m_userName; // 登录的用户名 + static UserBackstage::UserType m_userType; // 用户类型 +}; + +#endif // DIALOG_H diff --git a/QSqlDemo/SignIn/dialog.ui b/QSqlDemo/SignIn/dialog.ui new file mode 100644 index 0000000000000000000000000000000000000000..9b13a60f08754237564bb9c3fa7e19641a5319c1 --- /dev/null +++ b/QSqlDemo/SignIn/dialog.ui @@ -0,0 +1,140 @@ + + + Dialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + Qt::Vertical + + + + 20 + 74 + + + + + + + + Qt::Horizontal + + + + 50 + 20 + + + + + + + + 20 + + + + + + + + 200 + 30 + + + + + + + QLineEdit::Password + + + false + + + + + + + + 200 + 30 + + + + + + + + 用户: + + + + + + + 密码: + + + + + + + + + + 0 + 35 + + + + 登录 + + + + + + + + + Qt::Horizontal + + + + 50 + 20 + + + + + + + + Qt::Vertical + + + + 20 + 65 + + + + + + + + + diff --git a/QSqlDemo/SignIn/main.cpp b/QSqlDemo/SignIn/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f38132eedbe7d94437834646e1a9ec72241b638 --- /dev/null +++ b/QSqlDemo/SignIn/main.cpp @@ -0,0 +1,20 @@ +#include "widget.h" +#include "dialog.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + // 打开登录界面 + Dialog signIn; + signIn.exec(); + + if(!signIn.loggedIn()) // 登录失败直接退出 + { + return 0; + } + // 登录成功打开主界面 + Widget w; + w.show(); + return a.exec(); +} diff --git a/QSqlDemo/SignIn/userbackstage.cpp b/QSqlDemo/SignIn/userbackstage.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf6c30aae083efc27dc775baab6513e2f313c9b7 --- /dev/null +++ b/QSqlDemo/SignIn/userbackstage.cpp @@ -0,0 +1,136 @@ +#include "userbackstage.h" +#include "ui_userbackstage.h" +#include +#include +#include + +UserBackstage::UserBackstage(QWidget *parent) : + QDialog(parent), + ui(new Ui::UserBackstage) +{ + ui->setupUi(this); + + m_model = new QSqlTableModel(this); + m_model->setTable("User"); // 设置需要显示的数据库表 + ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); + m_model->setHeaderData(0, Qt::Horizontal, "ID"); + m_model->setHeaderData(1, Qt::Horizontal, "用户名"); + m_model->setHeaderData(2, Qt::Horizontal, "密码"); + m_model->setHeaderData(3, Qt::Horizontal, "用户类型"); + ui->tableView->setModel(m_model); + + m_model->select(); // 获取数据库中的数据 + ui->tableView->resizeColumnsToContents(); // 根据表格中的内容自动调整列宽 +} + +UserBackstage::~UserBackstage() +{ + delete ui; +} + +/** + * @brief 设置登录的用户类型 + * @param type + */ +void UserBackstage::setUserType(UserBackstage::UserType type) +{ + m_userType = type; + + // 添加用户类型 + ui->com_type->clear(); + switch (type) + { + case User: + { + break; + } + case Admin: + { + ui->com_type->addItem("普通用户", User); + m_model->setFilter(QString("type = '%1'").arg(int(User))); // 只显示普通用户的信息 + break; + } + case Root: + { + ui->com_type->addItem("普通用户", User); + ui->com_type->addItem("管理员", Admin); + break; + } + } +} + +/** + * @brief 设置用户信息 + */ +void UserBackstage::on_but_set_clicked() +{ + QString userName = ui->line_user->text().trimmed(); + QString password = ui->line_password->text().trimmed(); + UserType userType = UserType(ui->com_type->currentIndex()); + if(userName.isEmpty()) + { + QMessageBox::about(this, "注意!", "用户名不能为空"); + return; + } + if(password.isEmpty()) + { + QMessageBox::about(this, "注意!", "用户密码不能为空"); + return; + } + + QSqlQuery query; + QString sql = QString("select * from User where userName = '%1';").arg(userName); // 查询用户 + if(query.exec(sql)) + { + if(query.next()) // true则用户存在则更新数据 + { + int type = query.value("type").toInt(); + if(type >= m_userType) // 如果修改的用户等级超过登录用户等级,则修改失败 + { + return; + } + sql = QString("update User set password='%1', type=%2 where userName = '%3';") + .arg(password) + .arg(int(userType)) + .arg(userName); + query.exec(sql); + } + else // 用户不存在则插入数据 + { + query.prepare("insert into User(userName, password, type)" + "values (:userName, :password, :type)"); + query.bindValue(":userName", userName); + query.bindValue(":password", password); + query.bindValue(":type", int(userType)); + query.exec(); + } + m_model->select(); // 获取数据库中的数据 + } + else + { + qDebug() << "指令执行失败"; + } +} + +/** + * @brief 删除用户信息 + */ +void UserBackstage::on_but_delete_clicked() +{ + QString userName = ui->line_user->text().trimmed(); + if(userName.isEmpty()) + { + QMessageBox::about(this, "注意!", "用户名不能为空"); + return; + } + QSqlQuery query; + QString sql = QString("delete from User where userName = '%1';").arg(userName); // 查询用户 + if(query.exec(sql)) + { + m_model->select(); // 获取数据库中的数据 + } + else + { + qDebug() << "指令执行失败"; + } +} diff --git a/QSqlDemo/SignIn/userbackstage.h b/QSqlDemo/SignIn/userbackstage.h new file mode 100644 index 0000000000000000000000000000000000000000..75b6ab1c69f1b23269e41e5fca5a139111782930 --- /dev/null +++ b/QSqlDemo/SignIn/userbackstage.h @@ -0,0 +1,37 @@ +#ifndef USERBACKSTAGE_H +#define USERBACKSTAGE_H + +#include +#include + +namespace Ui { +class UserBackstage; +} + +class UserBackstage : public QDialog +{ + Q_OBJECT +public: + enum UserType { + User, // 普通用户 + Admin, // 普通管理员 + Root // 超级管理员(唯一) + }; +public: + explicit UserBackstage(QWidget *parent = nullptr); + ~UserBackstage(); + + void setUserType(UserType type); + +private slots: + void on_but_set_clicked(); + + void on_but_delete_clicked(); + +private: + Ui::UserBackstage *ui; + QSqlTableModel* m_model = nullptr; // 创建一个 单个数据库表的可编辑数据模型 + UserType m_userType; +}; + +#endif // USERBACKSTAGE_H diff --git a/QSqlDemo/SignIn/userbackstage.ui b/QSqlDemo/SignIn/userbackstage.ui new file mode 100644 index 0000000000000000000000000000000000000000..618fc2284b7a101293d2d28741442a2d1bda573b --- /dev/null +++ b/QSqlDemo/SignIn/userbackstage.ui @@ -0,0 +1,125 @@ + + + UserBackstage + + + + 0 + 0 + 582 + 404 + + + + Dialog + + + + + + + + + 6 + + + + + + + 用户名: + + + + + + + + 0 + 0 + + + + + + + + 密码: + + + + + + + + 0 + 0 + + + + + + + + 类型: + + + + + + + + 0 + 0 + + + + + + + + + + + 0 + 0 + + + + 设置用户 + + + + + + + + 0 + 0 + + + + 删除用户 + + + + + + + + + Qt::Vertical + + + + 20 + 189 + + + + + + + + + diff --git a/QSqlDemo/SignIn/widget.cpp b/QSqlDemo/SignIn/widget.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd627a1832abd2df8b07917820b1c0ebfa862c13 --- /dev/null +++ b/QSqlDemo/SignIn/widget.cpp @@ -0,0 +1,159 @@ +#include "widget.h" +#include "ui_widget.h" +#include "dialog.h" +#include "userbackstage.h" +#include +#include +#include + +Widget::Widget(QWidget *parent) + : QWidget(parent) + , ui(new Ui::Widget) +{ + ui->setupUi(this); + + QString user; + switch (Dialog::type()) + { + case UserBackstage::User: + { + ui->but_user->setVisible(false); // 隐藏用户管理功能 + ui->but_add->setVisible(false); // 隐藏数据新建功能 + ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); // 普通用户禁用修改数据功能 + user = QString("普通用户:%1").arg(Dialog::userName()); + break; + } + case UserBackstage::Admin: + { + user = QString("普通管理员:%1").arg(Dialog::userName()); + break; + } + case UserBackstage::Root: + { + user = QString("超级管理员:%1").arg(Dialog::userName()); + break; + } + } + this->setWindowTitle(QString("QSql-用户登录Demo - V%1 %2").arg(APP_VERSION).arg(user)); +} + +Widget::~Widget() +{ + delete ui; +} + + +void Widget::on_but_connect_clicked() +{ + if(ui->but_connect->text() == "关闭数据库") + { + ui->but_connect->setText("连接数据库"); + + m_db.close(); + + } + else + { + + m_db = QSqlDatabase::addDatabase("QSQLITE"); // 使用数据库驱动(Qsqlite)和默认连接名称(qt_sql_default_connection)添加一个数据库 + qDebug() << QSqlDatabase::defaultConnection; // 打印默认数据库连接名称 + m_db.setDatabaseName("SignIn.db"); // 使用文件数据库(可生成数据库文件,数据一直有效) + + if(!m_db.open()) // 打开数据库 + { + QMessageBox::critical(nullptr, "Error", "打开数据库失败!"); + return ; + } + + // 如果表不存在则创建表 + if(!isTableExists("person")) + { + QSqlQuery query; + + // 创建一个表person,包含id、firstname、lastname三个字段 + bool ret = query.exec("create table person (" + "id integer primary key," // 索引(自增key),使用integer默认为自增, int不能设置主键自增 + "firstname varchar(20)," // 名 + "lastname varchar(20))"); // 姓 + if(!ret) + { + qDebug() << "创建表失败:"; + } + } + + initModel(); + + ui->but_connect->setText("关闭数据库"); + } +} + +/** + * @brief 判断表是否存在 + * @param table 表名称 + * @return true存在 false不存在 + */ +bool Widget::isTableExists(const QString &table) +{ + QSqlQuery query; + QString sql = QString("select * from sqlite_master where name = '%1';").arg(table); // 查询sqlite_master表中是否存在表名 + if(query.exec(sql)) + { + return query.next(); + } + return false; +} + +/** + * @brief SQL 表模型(QSqlTableModel)来编辑数据库中的信息 + */ +void Widget::initModel() +{ + if(m_model) + { + m_model->clear(); + delete m_model; + m_model = nullptr; + } + m_model = new QSqlTableModel(this, m_db); + m_model->setTable("person"); // 设置需要显示的数据库表 +#if 1 + m_model->setEditStrategy(QSqlTableModel::OnFieldChange); // 在界面上修改后数据立刻保存到数据库 +#else + m_model->setEditStrategy(QSqlTableModel::OnManualSubmit); // 将将编辑数据库中值的策略设置为[在调用 submitAll() 或 revertAll() 之前,所有更改都将缓存在模型中(即在界面上修改数据后不会立刻存入数据库)] +#endif + m_model->setHeaderData(0, Qt::Horizontal, "ID"); + m_model->setHeaderData(1, Qt::Horizontal, "名称"); + m_model->setHeaderData(2, Qt::Horizontal, "姓氏"); + ui->tableView->setModel(m_model); +} + +/** + * @brief 添加空白数据行 + */ +void Widget::on_but_add_clicked() +{ + QSqlQuery query; + query.prepare("insert into person(firstname, lastname)" // 写入数据时不需写入id字段,实现自增 + "values (:firstname, :lastname)"); + query.bindValue(":firstname", ""); + query.bindValue(":lastname", ""); + query.exec(); + m_model->select(); // 获取数据库中的数据 +} + +/** + * @brief 查询数据库更新界面 + */ +void Widget::on_but_read_clicked() +{ + if(!m_model) return; + m_model->select(); // 获取数据库中的数据 + ui->tableView->resizeColumnsToContents(); // 根据表格中的内容自动调整列宽 +} + +void Widget::on_but_user_clicked() +{ + UserBackstage user; + user.setUserType(Dialog::type()); + user.exec(); +} diff --git a/QSqlDemo/SignIn/widget.h b/QSqlDemo/SignIn/widget.h new file mode 100644 index 0000000000000000000000000000000000000000..bfae62dae4074c6832c32a74b277868f0c0207f4 --- /dev/null +++ b/QSqlDemo/SignIn/widget.h @@ -0,0 +1,36 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class Widget; } +QT_END_NAMESPACE + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + +private slots: + void initModel(); + bool isTableExists(const QString& table); + void on_but_connect_clicked(); + + void on_but_add_clicked(); + + void on_but_read_clicked(); + + void on_but_user_clicked(); + +private: + Ui::Widget *ui; + QSqlTableModel* m_model = nullptr; // 创建一个 单个数据库表的可编辑数据模型 + QSqlDatabase m_db; +}; +#endif // WIDGET_H diff --git a/QSqlDemo/SignIn/widget.ui b/QSqlDemo/SignIn/widget.ui new file mode 100644 index 0000000000000000000000000000000000000000..d2e649419c33be2ec4d6306ccb6908242af28ac4 --- /dev/null +++ b/QSqlDemo/SignIn/widget.ui @@ -0,0 +1,52 @@ + + + Widget + + + + 0 + 0 + 522 + 402 + + + + Widget + + + + + + 新增数据行 + + + + + + + 连接数据库 + + + + + + + 读取数据 + + + + + + + 用户管理 + + + + + + + + + + +