159 lines
5.7 KiB
C++
159 lines
5.7 KiB
C++
|
// RecvDataHandler.cpp
|
||
|
#include "RecvDataHandler.h"
|
||
|
#include "../Network/ClientHandler.h"
|
||
|
|
||
|
DataHandler::DataHandler(QLabel* leftLens_imageLabel, QLabel* rightLens_imageLabel, QLabel* videoLabel, QObject* parent)
|
||
|
: QObject(parent),
|
||
|
leftLens_m_imageLabel(leftLens_imageLabel),
|
||
|
rightLens_m_imageLabel(rightLens_imageLabel),
|
||
|
videoLabel(videoLabel),
|
||
|
ffmpegDecoder(new FFmpegDecoder()), // 初始化FFmpeg解码器
|
||
|
buffer(new QByteArray())
|
||
|
{
|
||
|
ffmpegDecoder->initialize(); // 初始化解码器
|
||
|
clearAllRecvData();
|
||
|
}
|
||
|
|
||
|
DataHandler::~DataHandler()
|
||
|
{
|
||
|
delete ffmpegDecoder; // 释放解码器
|
||
|
delete buffer;
|
||
|
}
|
||
|
|
||
|
// 将十六进制字符串转换为 QByteArray
|
||
|
QByteArray DataHandler::hexStringToByteArray(const QString& hexString)
|
||
|
{
|
||
|
QByteArray byteArray;
|
||
|
for (int i = 0; i < hexString.length(); i += 2) {
|
||
|
byteArray.append(static_cast<char>(hexString.mid(i, 2).toUShort(nullptr, 16)));
|
||
|
}
|
||
|
return byteArray;
|
||
|
}
|
||
|
|
||
|
const QString filePath_1 = "add1.h264";
|
||
|
|
||
|
// 显示视频的函数
|
||
|
void DataHandler::showVideo(const QString& client, const QByteArray& valData)
|
||
|
{
|
||
|
static QFile file(filePath_1);
|
||
|
|
||
|
if (!file.isOpen()) {
|
||
|
if (QFile::exists(filePath_1)) {
|
||
|
QFile::remove(filePath_1);
|
||
|
}
|
||
|
if (!file.open(QIODevice::Append | QIODevice::WriteOnly)) {
|
||
|
qWarning() << "无法打开H264文件:" << file.errorString();
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//qDebug() << "==========valData.size:" << valData.size();
|
||
|
if (!valData.isEmpty()) {
|
||
|
file.write(valData);
|
||
|
file.flush(); // 刷新文件缓冲区,以确保数据写入磁盘
|
||
|
qWarning() << " ------------ write ------------" ;
|
||
|
}
|
||
|
//qint64 elapsed = timer.elapsed(); // 获取经过的时间(毫秒)
|
||
|
//qDebug() << "Time spent writing file:" << elapsed << "ms";
|
||
|
|
||
|
if (!start_run) {
|
||
|
start_run = 1;
|
||
|
ffmpegDecoder->decodeFile(filePath_1, videoLabel);
|
||
|
}
|
||
|
//ffmpegDecoder->decodeFile(filePath_1, videoLabel);
|
||
|
}
|
||
|
|
||
|
void DataHandler::clearAllRecvData() {
|
||
|
allRecvData = QByteArray();
|
||
|
remain = 0;
|
||
|
dataLen = 0;
|
||
|
}
|
||
|
|
||
|
void DataHandler::handleCmd(unsigned char msg_id, const QString& client, QByteArray actual_data)
|
||
|
{
|
||
|
switch (msg_id) {
|
||
|
case 0x19:
|
||
|
{
|
||
|
showPic(leftLens_m_imageLabel, rightLens_m_imageLabel, client, actual_data);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 0x11:
|
||
|
{
|
||
|
showVideo(client, actual_data);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
{}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 处理接收到的数据
|
||
|
void DataHandler::handleData(const QString& client, const QByteArray& recvData, unsigned char msg_id,
|
||
|
int currentRecvItemIndex, int currentRecvFuncItemIndex,
|
||
|
const QString& itemData, const QString& funcItemData)
|
||
|
{
|
||
|
//qDebug() << "Data received from" << client << ":" << recvData;
|
||
|
#if 0
|
||
|
从文件中读取YUV数据
|
||
|
QFile file("output_2.txt");
|
||
|
if (!file.open(QIODevice::ReadOnly)) {
|
||
|
qWarning() << "Failed to open file:";
|
||
|
file.close();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
QByteArray recvdata = file.readAll(); // 读取文件中的数据
|
||
|
file.close();
|
||
|
|
||
|
qDebug() << "read file data size:" << recvdata.size();
|
||
|
#endif
|
||
|
|
||
|
qDebug() << "---Received data size:" << recvData.size();
|
||
|
|
||
|
// 将接收到的数据追加到buffer
|
||
|
buffer->append(recvData);
|
||
|
|
||
|
while (buffer->size() >= 11) { // 至少需要11个字节来解析数据头
|
||
|
// 检查数据头
|
||
|
if (buffer->mid(0, 4) == QByteArray::fromHex("aa55aa55")) {
|
||
|
msg_id = static_cast<unsigned char>(buffer->at(4));
|
||
|
int dataSize = (static_cast<unsigned char>(buffer->at(9)) << 24) |
|
||
|
(static_cast<unsigned char>(buffer->at(8)) << 16) |
|
||
|
(static_cast<unsigned char>(buffer->at(7)) << 8) |
|
||
|
(static_cast<unsigned char>(buffer->at(6)));
|
||
|
// 第11字节为返回 OK/NG
|
||
|
bool success = (static_cast<unsigned char>(buffer->at(10)) == 0x00);
|
||
|
//int totalSize = 10 + dataSize + 1; // 数据头大小(10字节) + success + 实际数据
|
||
|
int totalSize = 10 + dataSize; // 数据头大小(10字节) + 实际数据大小
|
||
|
|
||
|
if (buffer->size() >= totalSize) {
|
||
|
// 去掉前面 11 字节
|
||
|
//QByteArray data = buffer->mid(11, dataSize);
|
||
|
QByteArray data = buffer->mid(10, dataSize);
|
||
|
QString hexString = QString::fromUtf8(data.toHex().data());
|
||
|
QByteArray actual_data = hexStringToByteArray(hexString);
|
||
|
buffer->remove(0, totalSize); // 移除已处理的数据
|
||
|
// 暂时设置 NG
|
||
|
// 同一个client仅当 msg_id 不连续为 0x11/0x21 或第一次处理时才执行 emit statusUpdated
|
||
|
if ((msg_id != 0x11 || clientLastMsgId.value(client, 0) != 0x11) &&
|
||
|
(msg_id != 0x21 || clientLastMsgId.value(client, 0) != 0x21)){
|
||
|
qDebug() << "Emitting statusUpdated for client:" << client << "with msg_id:" << msg_id;
|
||
|
emit statusUpdated(client, currentRecvItemIndex + 1, currentRecvFuncItemIndex + 1,
|
||
|
true, itemData, funcItemData);
|
||
|
}
|
||
|
clientLastMsgId[client] = msg_id;
|
||
|
handleCmd(msg_id, client, actual_data);
|
||
|
}
|
||
|
else {
|
||
|
break; // 数据还不完整,等待下一次接收
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
buffer->remove(0, 1); // 移除无效数据头
|
||
|
}
|
||
|
}
|
||
|
}
|