1.更新MDNS广播的服务类型命名规范,防止出现多设备打开找不到MDNS的广播的问题 2.更新区分前后板的TAG

This commit is contained in:
2024-09-02 10:22:41 +08:00
parent e0c1e37191
commit 09c5843358
22 changed files with 501 additions and 304 deletions

View File

@@ -15,7 +15,7 @@ FFmpegDecoder::FFmpegDecoder(QObject* parent) :
{
av_log_set_level(AV_LOG_QUIET); // 设置日志级别为安静模式
avformat_network_init(); // 初始化网络
//qDebug() << "FFmpegDecoder created";
qDebug() << "FFmpegDecoder created";
}
FFmpegDecoder::~FFmpegDecoder()
@@ -31,16 +31,54 @@ FFmpegDecoder::~FFmpegDecoder()
qDebug() << "FFmpegDecoder destroyed";
}
void FFmpegDecoder::processVideo(int itemIndex)
{
#if 0
int width = 720;
int height = 1280;
#elif 1
int width = 640;
int height = 480;
#else
int width = 480;
int height = 640;
#endif
if (width * 16 == height * 9) {
FocusWindowDialog* dialog = new FocusWindowDialog(nullptr, QSize(540, 960));
dialog->exec();
}
else if (width * 9 == height * 16) {
FocusWindowDialog* dialog = new FocusWindowDialog(nullptr, QSize(960, 540));
dialog->exec();
}
else if (width * 4 == height * 3) {
FocusWindowDialog* dialog = new FocusWindowDialog(nullptr, QSize(480, 640));
dialog->exec();
}
else if (width * 3 == height * 4) {
FocusWindowDialog* dialog = new FocusWindowDialog(nullptr, QSize(640, 480));
dialog->exec();
}
}
void FFmpegDecoder::initialize()
{
// 初始化FFmpeg库
avformat_network_init();
}
void FFmpegDecoder::stopFFmpegDecoder()
{
mutex.lock();
abort = true;
condition.wakeOne(); // 唤醒等待的线程
mutex.unlock();
}
void FFmpegDecoder::decodeFile(const QString& filePath, QLabel* videoDisplayLabel)
{
QMutexLocker locker(&mutex);
this->filePath = filePath;
this->filePath = filePath;
this->videoLabel = videoDisplayLabel;
if (!isRunning()) {
qDebug() << "Starting decoder thread";
@@ -55,7 +93,7 @@ void FFmpegDecoder::run()
QFile file(filePath);
qint64 fileSize = 0;
while (true) {
while (!isInterruptionRequested()) {
mutex.lock();
while (!restart && !abort) {
condition.wait(&mutex);
@@ -65,14 +103,12 @@ void FFmpegDecoder::run()
qDebug() << "Decoder thread aborting";
break;
}
restart = false;
QLabel* currentVideoLabel = videoLabel;
QSize labelSize = currentVideoLabel->size();
mutex.unlock();
if (labelSize.width() <= 0 || labelSize.height() <= 0) {
// 自动调整 QLabel 大小
labelSize = QSize(800, 600); // 例如设置为默认大小
if (labelSize.width() < 740 || labelSize.height() < 357) {
labelSize = QSize(740, 357);
currentVideoLabel->setFixedSize(labelSize);
qDebug() << "Adjusting video label size to: Width =" << labelSize.width() << ", Height =" << labelSize.height();
}
@@ -88,14 +124,18 @@ void FFmpegDecoder::run()
file.close();
continue;
}
restart = false;
while (!abort) {
qint64 currentFileSize = file.size();
//qDebug() << "Decoder thread currentFileSize:" << currentFileSize;
//qDebug() << "Decoder thread fileSize:" << fileSize;
if (currentFileSize > fileSize) {
fileSize = currentFileSize;
file.seek(fileSize); // 设置文件读取位置到末尾
//qDebug() << "---------------1---------------";
// 读取并处理数据包
while (av_read_frame(formatContext, packet) >= 0) {
//qDebug() << "---------------2---------------";
if (packet->stream_index == videoStreamIndex) {
int ret = avcodec_send_packet(codecContext, packet);
if (ret < 0) {
@@ -103,6 +143,7 @@ void FFmpegDecoder::run()
av_packet_unref(packet);
continue;
}
//qDebug() << "---------------3---------------";
while (ret >= 0) {
ret = avcodec_receive_frame(codecContext, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
@@ -113,28 +154,31 @@ void FFmpegDecoder::run()
qWarning() << "Error during decoding";
break;
}
qDebug() << "H264 video resolution: Width =" << frame->width << ", Height =" << frame->height;
//qDebug() << "---------------4---------------";
QImage img = avFrameToQImage(frame);
//qDebug() << "---------------5---------------";
QImage scaledImage = img.scaled(labelSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
//qDebug() << "---------------6---------------";
currentVideoLabel->setPixmap(QPixmap::fromImage(scaledImage));
QThread::msleep(10); // Simulate 25 FPS frame rate
//qDebug() << "---------------7---------------";
}
}
av_packet_unref(packet);
//qDebug() << "---------------8---------------";
}
}
//qDebug() << "---------------9---------------";
mutex.lock();
if (restart) {
restart = false;
mutex.unlock();
break;
}
//qDebug() << "---------------10---------------";
mutex.unlock();
}
//qDebug() << "---------------11---------------";
cleanup();
file.close();
@@ -148,6 +192,10 @@ void FFmpegDecoder::run()
bool FFmpegDecoder::initializeFFmpeg(const QString& filePath)
{
if (!QFile::exists(filePath)) {
qWarning() << "FFmpeg File does not exist:" << filePath;
return false;
}
if (avformat_open_input(&formatContext, filePath.toStdString().c_str(), nullptr, nullptr) != 0) {
qWarning() << "Failed to open file with FFmpeg:" << filePath;
return false;
@@ -227,6 +275,8 @@ QImage FFmpegDecoder::avFrameToQImage(AVFrame* frame)
{
int width = frame->width;
int height = frame->height;
qDebug() << "H264 video resolution: Width =" << frame->width << ", Height =" << frame->height;
AVPixelFormat pixFmt = (AVPixelFormat)frame->format;
if (!swsContext) {

View File

@@ -14,8 +14,10 @@
#include <QWaitCondition>
#include <cstdint>
#include <vector>
#include <QDir>
#include "RingBuffer.h"
#include "FocusWindow.h"
extern "C" {
#include <libavcodec/avcodec.h>
@@ -38,9 +40,10 @@ public:
void initialize();
void decodeFile(const QString& filePath, QLabel* videoLabel);
void decodeSingleFrame(const QByteArray& data, QLabel* videoLabel); // 添加 videoLabel 参数
void FFmpegDecoder::processVideo(int itemIndex);
bool initializeFFmpeg(const QString& filePath);
void cleanup();
void stopFFmpegDecoder();
protected:
void run() override;
@@ -48,19 +51,19 @@ protected:
private:
QImage avFrameToQImage(AVFrame* frame);
QMutex mutex;
QWaitCondition condition;
QString filePath;
QLabel* videoLabel;
bool abort;
bool restart;
QMutex mutex;
QWaitCondition condition;
QString filePath;
QLabel* videoLabel;
bool abort;
bool restart;
AVFormatContext* formatContext;
AVCodecContext* codecContext;
AVFrame* frame;
AVPacket* packet;
SwsContext* swsContext;
int videoStreamIndex;
AVCodecContext* codecContext;
AVFrame* frame;
AVPacket* packet;
SwsContext* swsContext;
int videoStreamIndex;
RingBuffer* ringBuffer;
//QLabel* singleFrameLabel; // 用于解码单帧的标签

View File

@@ -0,0 +1,26 @@
#include <QDialog>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
class FocusWindowDialog : public QDialog
{
Q_OBJECT
public:
FocusWindowDialog(QWidget* parent = nullptr, const QSize& labelSize = QSize(480, 640)) : QDialog(parent)
{
QVBoxLayout* layout = new QVBoxLayout(this);
QLabel* label = new QLabel("1111 This is a new window opened by clicking the new button.", this);
label->setFixedSize(labelSize); // 设置QLabel的固定大小
layout->addWidget(label);
QPushButton* closeButton = new QPushButton("Close", this);
layout->addWidget(closeButton);
connect(closeButton, &QPushButton::clicked, this, &FocusWindowDialog::accept);
setLayout(layout);
setWindowTitle("SL100 视频播放窗口");
//resize(500, 700); // 设置对话框的大小
}
};