1. 增加猫眼镜头切换IRCUT; 2. 优化连续多次打开视频获取视频流导致的内存问题

This commit is contained in:
2024-09-02 18:37:24 +08:00
parent 09c5843358
commit 15eb213578
19 changed files with 153 additions and 181 deletions

View File

@@ -1,10 +1,11 @@
// MainWidget.cpp
#include "MainWidget.h"
void onThreadFinished(QThread* thread, ClientHandler* handler)
void onThreadFinished(QThread* thread, ClientHandler* handler, DataHandler* dataHandler)
{
qDebug() << "Thread finished. Deleting handler and thread.";
handler->deleteLater();
dataHandler->deleteLater();
thread->deleteLater();
}
@@ -34,44 +35,11 @@ MainWidget::MainWidget(QWidget* parent) :
licenseHwInfoEdit = new QTextEdit(this);
UuidHwInfoEdit = new QTextEdit(this);
// 创建 mDNS 服务的 Server 和 Hostname 对象
QMdnsEngine::Server myserver;
QMdnsEngine::Hostname hostname(&myserver);
// 创建服务提供者Provider
QMdnsEngine::Provider provider(&myserver, &hostname);
// 定义要广播的服务
QMdnsEngine::Service service;
service.setName("Test Service");
service.setType("_test._tcp"); // 服务类型,包括 _tcp 或 _udp
service.setPort(12345); // 服务使用的端口号
// 启动服务广播
provider.update(service);
qDebug() << "mDNS service started. Broadcasting service:"
<< service.name() << service.type() << "on port" << service.port();
setupUI();
// 打印线程池状态信息
setupTimerForThreadPoolInfo();
server = new QTcpServer(this);
/*
// 创建 DataHandler 对象并连接信号
DataHandler* dataHandler = new DataHandler(leftLens_imageLabel, rightLens_imageLabel, videoLabel, licenseHwInfoEdit, &devInfoLineEdits,
frontBoardOneClickTest, frontBoardTest, frontBoardFuncConfig,
frontBoardDevInfoJson, frontBoardLicenseJson,
backBoardOneClickTest, backBoardTest, backBoardFuncConfig, backBoardDevInfoJson,
backBoardUuidJson, getPicJson, getVideoJson, this);
connect(this, &MainWidget::openFocusWindowRequested, dataHandler, &DataHandler::handleOpenFocusWindow);
connect(dataHandler, &DataHandler::statusUpdated, this, &MainWidget::onStatusUpdated);
*/
connect(server, &QTcpServer::newConnection, this, [this]() {
// 检查是否有挂起的连接
while (server->hasPendingConnections()) {
@@ -124,13 +92,21 @@ MainWidget::MainWidget(QWidget* parent) :
backBoardOneClickTest, backBoardTest, backBoardFuncConfig, backBoardDevInfoJson,
backBoardUuidJson, getPicJson, getVideoJson, clientId, isBackBoardOrAllBoard, nullptr);
// 将 ClientHandler 移到线程池中的线程
// 将 ClientHandler 移到线程池中的线程
handler->moveToThread(thread);
// 当线程结束时删除 handler
connect(thread, &QThread::finished, this, [=]() {
onThreadFinished(thread, handler);
});
// 创建 DataHandler 对象并连接信号
DataHandler* dataHandler = new DataHandler(leftLens_imageLabel, rightLens_imageLabel, videoLabel,
VideoResolutionEdit,
licenseHwInfoEdit, &devInfoLineEdits,
frontBoardOneClickTest, frontBoardTest, frontBoardFuncConfig,
frontBoardDevInfoJson, frontBoardLicenseJson,
backBoardOneClickTest, backBoardTest, backBoardFuncConfig, backBoardDevInfoJson,
backBoardUuidJson, getPicJson, getVideoJson, nullptr);
qDebug() << "dataHandler->moveToThread(thread)";
// 将 DataHandler 移到线程池中的线程
dataHandler->moveToThread(thread);
// 将sendData信号连接到主线程中的槽上
connect(handler, &ClientHandler::sendData, this, [socket](const QByteArray& data, int isBoardType) {
@@ -160,9 +136,6 @@ MainWidget::MainWidget(QWidget* parent) :
this->onStartTimeout(clientId, timeout);
});
// 启动新的线程
thread->start();
clients.append(handler);
clients_1[clientId] = handler;
clientThreads[clientId] = thread;
@@ -171,22 +144,23 @@ MainWidget::MainWidget(QWidget* parent) :
connect(handler, &ClientHandler::allItemsProcessed, this, &MainWidget::onAllItemsProcessed);
connect(handler, &ClientHandler::selectClientDisconnected, this, &MainWidget::onDisconnectClient);
#if 1
// 创建 DataHandler 对象并连接信号
DataHandler* dataHandler = new DataHandler(leftLens_imageLabel, rightLens_imageLabel, videoLabel, licenseHwInfoEdit, &devInfoLineEdits,
frontBoardOneClickTest, frontBoardTest, frontBoardFuncConfig,
frontBoardDevInfoJson, frontBoardLicenseJson,
backBoardOneClickTest, backBoardTest, backBoardFuncConfig, backBoardDevInfoJson,
backBoardUuidJson, getPicJson, getVideoJson, this);
dataHandlers[clientId] = dataHandler;
connect(this, &MainWidget::openFocusWindowRequested, dataHandler, &DataHandler::handleOpenFocusWindow);
connect(handler, &ClientHandler::dataReceived, dataHandler, &DataHandler::handleData);
connect(dataHandler, &DataHandler::statusUpdated, this, &MainWidget::onStatusUpdated);
#else
connect(handler, &ClientHandler::dataReceived, dataHandler, &DataHandler::handleData);
#endif
connect(handler, &ClientHandler::dataReceived, dataHandler, &DataHandler::handleData);
connect(dataHandler, &DataHandler::statusUpdated, this, &MainWidget::onStatusUpdated);
// 当线程结束时删除 handler
connect(thread, &QThread::finished, this, [=]() {
onThreadFinished(thread, handler, dataHandler);
});
// 启动新的线程
thread->start();
connect(handler, &ClientHandler::startReadTimer, this, &MainWidget::startClientReadTimer);
connect(handler, &ClientHandler::stopReadTimer, this, &MainWidget::stopClientReadTimer);
//handler->getClientId() == handler->preVideoClientId
qDebug() << "---- handler->getClientId():" << handler->getClientId();
qDebug() << "---- handler->preVideoClientId:" << handler->preVideoClientId;
connect(this, &MainWidget::openFocusWindowRequested, dataHandler, &DataHandler::handleOpenFocusWindow);
// 创建和管理定时器
QTimer* readTimer = new QTimer(this);
@@ -924,13 +898,20 @@ void MainWidget::onLicenseButtonClicked()
QString dataStr = "";
if (jsonObject["lable"].toString() == "get_license") {
QString hwInfoText = licenseHwInfoEdit->toPlainText();
if (hwInfoText.count("0x") != PIX_HARDWARE_INFO_BYTES) {
isRequestSuccessful = 0;
//licenseHwInfoEdit->setPlainText("您使用了License去获取License这是不允许的!!!");
LicenseConfirmWindow dialog("您使用了License去获取License这是不允许的!!!");
dialog.exec();
return;
}
dataStr = hwInfoText.remove(","); // 去掉所有的逗号
QString sendToHttpServerDataStr = dataStr.replace("0x", "").replace(" ", ""); // 去掉0x和空格
#if 1
qDebug() << "sendToHttpServerDataStr:" << sendToHttpServerDataStr;
FactoryToolSendGetLicenseToHttpServer(sendToHttpServerDataStr);
while (isRequestSuccessful == 0) {
QCoreApplication::processEvents(); // 防止阻塞UI线程
QCoreApplication::processEvents(); // 防止阻塞线程
}
qDebug() << "isRequestSuccessful:" << isRequestSuccessful;
if (isRequestSuccessful == 2) {
@@ -960,18 +941,18 @@ void MainWidget::onLicenseButtonClicked()
qDebug() << "License Key set in licenseHwInfoEdit:" << licenseKey;
return;
#endif
QByteArray dataByteArray = QByteArray::fromHex(dataStr.toUtf8());
QByteArray dataByteArray = QByteArray::fromHex(dataStr.toUtf8());
memcpy(hardware_info, dataByteArray.data(), qMin(dataByteArray.size(), PIX_HARDWARE_INFO_BYTES));
//unsigned char hardware_info[PIX_HARDWARE_INFO_BYTES] = { 0x46,0x0b,0x5d,0x11,0x58,0x17,0x4d,0x5e,0x55,0x5c,0x51,0x4a,0x5a,0x07,0x59,0x4c,0x5f,0x45,0x5b,0x5f,0x5a,0x45,0x1c,0x5a,0x45,0x43,0x44,0x47,0x51,0x5e,0x44,0x30 };
LicenseConfirmWindow dialog("你确定要获取此授权吗?\n请确认你的hw_info是否正确");
if (dialog.exec() == QDialog::Accepted) {
#if !MANUAL_UPLOAD_LICENSE
if (!(licenseGenerate(hardware_info, license_info))) {
qDebug() << "从U盘获取License失败" ;
qDebug() << "从U盘获取License失败";
isRequestSuccessful = 0;
licenseHwInfoEdit->setPlainText("从U盘获取License失败,请检查U盘是否插入电脑!!!");
return;
}
}
QString licenseInfoHex = QByteArray(reinterpret_cast<char*>(license_info), PIX_LICENCE_BYTES).toHex().toUpper();
#else
//unsigned char license_info_1[PIX_LICENCE_BYTES] = { 0x07, 0xe8, 0xf3, 0x80, 0xa8, 0x07, 0x72, 0xa1, 0x17, 0xfe, 0xda, 0x67, 0xbd, 0x4a, 0x5a, 0xb5, 0xbb, 0x8b, 0x2d, 0xb2, 0xbf, 0x89, 0x74, 0xe5, 0xb0, 0x99, 0x70, 0x74, 0x3c, 0x6f, 0xf8, 0x82, 0x79, 0xab, 0x31, 0x9c, 0xdf, 0xe8, 0x9e, 0x75, 0x8f, 0x42, 0xb3, 0xcf, 0x00, 0x60, 0xa0, 0x38, 0xa4, 0xb8, 0xbe, 0xa6, 0x5d, 0x9f, 0x8b, 0x41, 0xf3, 0x0a, 0x69, 0xf6, 0x50, 0x94, 0x3f, 0xd0, 0xa5, 0xee, 0x88, 0x20, 0x93, 0x9a, 0x1c, 0xe9, 0x64, 0xd3, 0xaf, 0x9f, 0xc7, 0x66, 0x00, 0x7d, 0x7d, 0x68, 0xf1, 0xa4, 0xe1, 0x58, 0x00, 0x1d, 0x03, 0x0d, 0x40, 0x08, 0xa4, 0xcc, 0x0b, 0xd8, 0x19, 0x70, 0x9a, 0x83, 0x81, 0xbf, 0x27, 0x35, 0xb8, 0xec, 0x59, 0xa8, 0xd0, 0x03, 0xdb, 0xf6, 0xcf, 0x83, 0xaa, 0x0e, 0xfc, 0x95, 0x29, 0x77, 0xec, 0x89, 0xc5, 0x79, 0x10, 0x40, 0xd8, 0xbb };
@@ -995,7 +976,7 @@ void MainWidget::onLicenseButtonClicked()
printf("\n");
licenseHwInfoEdit->setPlainText(licenseInfoStr);
isRequestSuccessful = 0;
}
}
}
else {
#if 0
@@ -1211,6 +1192,9 @@ void MainWidget::onSendGetPicClicked()
button->setStyleSheet("background-color: green;");
if (lastClickedGetPicCamIndex != -1 && lastClickedGetPicCamIndex != itemIndex) {
getPicButtons[lastClickedGetPicCamIndex]->setStyleSheet("");
if (lastClickedGetPicDevIndex != -1) {
getPicButtons[lastClickedGetPicDevIndex]->setStyleSheet("");
}
}
lastClickedGetPicCamIndex = itemIndex;
}
@@ -1254,26 +1238,27 @@ void MainWidget::onSendGetVideoClicked()
QPushButton* button = qobject_cast<QPushButton*>(sender());
int itemIndex = button->property("getVideoIndex").toInt();
if (itemIndex < 2) {
if (itemIndex < 4) {
button->setStyleSheet("background-color: green;");
if (lastClickedGetVideoCamIndex != -1 && lastClickedGetVideoCamIndex != itemIndex) {
getVideoButtons[lastClickedGetVideoCamIndex]->setStyleSheet("");
if (lastClickedGetVideoDevIndex != -1) {
getVideoButtons[lastClickedGetVideoDevIndex - 2]->setStyleSheet("");
}
}
lastClickedGetVideoCamIndex = itemIndex;
}
else {
if (lastClickedGetVideoCamIndex == -1) {
QListWidgetItem* listItem = new QListWidgetItem(QString("Please select IR or RGB lens to get video!!!"), statusListWidget);
QListWidgetItem* listItem = new QListWidgetItem(QString("Please select lens to get video!!!"), statusListWidget);
listItem->setBackground(Qt::red);
}
else {
if (itemIndex - 6 >= connectedClientsCount) {
QListWidgetItem* listItem = new QListWidgetItem(QString("This device is not connected !!!").arg(itemIndex - 4), statusListWidget);
if (itemIndex - 8 >= connectedClientsCount) {
QListWidgetItem* listItem = new QListWidgetItem(QString("This device is not connected !!!").arg(itemIndex - 6), statusListWidget);
listItem->setBackground(Qt::red);
}
else {
//qDebug() << "lastClickedGetVideoDevIndex:" << lastClickedGetVideoDevIndex;
//qDebug() << "itemIndex:" << itemIndex;
button->setStyleSheet("background-color: green;");
if (lastClickedGetVideoDevIndex != -1 && lastClickedGetVideoDevIndex != itemIndex) {
//qDebug() << "itemIndex:" << itemIndex;
@@ -1283,11 +1268,11 @@ void MainWidget::onSendGetVideoClicked()
//QMutexLocker locker(&mutex);
for (ClientHandler* handler : clients) {
if (handler->getClientId() == handler->controlClientId) {
handler->sendGetVideoItem(itemIndex - 5, lastClickedGetVideoCamIndex);
handler->sendGetVideoItem(itemIndex - 7, lastClickedGetVideoCamIndex);
break;
}
}
getVideoButtons[2]->setEnabled(true);
getVideoButtons[FOCUS_WINDOWS_BUTTON]->setEnabled(true);
}
}
}
@@ -1303,9 +1288,15 @@ void MainWidget::onOpenFocusWindowClicked()
QPushButton* button = qobject_cast<QPushButton*>(sender());
if (button) {
int itemIndex = button->property("getVideoIndex").toInt();
//qDebug() << "New Button clicked with itemIndex:" << itemIndex;
if (itemIndex == 2) {
emit openFocusWindowRequested(itemIndex); // 发送信号
qDebug() << "New Button clicked with itemIndex:" << itemIndex;
if (itemIndex == FOCUS_WINDOWS_BUTTON) {
for (ClientHandler* handler : clients) {
if (handler->getClientId() == handler->preVideoClientId) {
emit openFocusWindowRequested(itemIndex); // 发送信号
}
}
}
}
}