RecordControlApplication/main.cpp
2024-08-11 20:26:42 -07:00

231 lines
6.1 KiB
C++
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Channel.h"
#include "CheckStorageThread.h"
#include "Constant.h"
#include "DatabaseManager.h"
#include "Json.h"
#include "Link.h"
#include "Log.h"
#include "SerialPortTool.h"
#include "TcpServer.h"
#include "Tool.h"
#include "Widget.h"
#include <QApplication>
#include <cstring>
#include <stdlib.h>
#include <unistd.h>
// Tcp服务器
TcpServer* server;
// 串口工具
SerialPortTool* serialPortTool;
// 数据库管理
DatabaseManager* db;
// 视频输出通道
LinkObject* vo;
LinkObject* vo1;
// 通道列表
QList<Channel*> channelList;
// 录制模式
Constant::RecordMode recordMode;
// 回放模式
Constant::PlaybackMode playbackMode;
QString mainChannelProtocol;
QString secondaryChannelProtocol;
/**
* @brief 加载配置文件
*/
bool loadConfiguration(QString path)
{
QFileInfo fileInfo(path);
if (!fileInfo.exists()) {
Log::critical("config.json does not exist, exit");
return false;
}
QVariantMap config = Json::loadFile(path).toMap();
recordMode = Constant::RecordMode(config["recordMode"].toInt());
playbackMode = Constant::PlaybackMode(config["playbackMode"].toInt());
QVariantList list = config["interface"].toList();
// 初始化通道
for (int i = 0; i < list.count(); i++) {
Channel* chn = new Channel();
QVariantMap cfg = list.at(i).toMap();
chn->channelName = cfg["name"].toString();
if (chn->channelName == Constant::MainChannel) {
mainChannelProtocol = cfg["protocol"].toString();
} else if (chn->channelName == Constant::SecondaryChannel) {
secondaryChannelProtocol = cfg["protocol"].toString();
}
QVariantMap encV = cfg["encV"].toMap();
chn->videoEncoderParams = encV;
QVariantMap encA = cfg["encA"].toMap();
chn->audioEncoderParams = encA;
chn->pushCode = cfg["pushCode"].toString();
chn->init();
channelList.push_back(chn);
}
return true;
}
/**
* @brief 开始录制
*/
void startRecord()
{
// 判断是否挂载了磁盘
QString info = Tool::writeCom(QString("df %1").arg(Constant::MountedPath));
if (!info.contains(Constant::MountedPath)) {
Log::error("there is no disk mounted");
serialPortTool->onLoopOn();
QThread::msleep(100);
return;
}
// 一路录制,只录制主通道
if (recordMode == Constant::OneChannelRecord) {
for (Channel* chn : channelList) {
if (chn->channelName == Constant::MainChannel) {
chn->startRecord();
// QThread::msleep(100);
serialPortTool->onRecordStart();
QThread::msleep(100);
// serialPortTool->onLoopOff();
}
}
}
// 两路录制
else if (recordMode == Constant::TwoChannelRecord) {
for (Channel* chn : channelList) {
chn->startRecord();
serialPortTool->onRecordStart();
QThread::msleep(100);
// serialPortTool->onLoopOff();
// QThread::msleep(100);
}
}
// 无录制
else {
// 打开环路灯
serialPortTool->onLoopOn();
QThread::msleep(100);
}
}
void setChannelProtocol()
{
if (mainChannelProtocol == "HDMI") {
serialPortTool->onMainChannelHDMI();
} else if (mainChannelProtocol == "VGA") {
serialPortTool->onMainChannelVGA();
}
QThread::msleep(100);
if (secondaryChannelProtocol == "HDMI") {
serialPortTool->onSecondaryChannelHDMI();
} else if (secondaryChannelProtocol == "VGA") {
serialPortTool->onSecondaryChannelVGA();
}
QThread::msleep(100);
}
int main(int argc, char* argv[])
{
// std::atexit(resetLight);
// 初始化日志
Log::init();
// 初始化Link编码系统
if (!Link::init()) {
Log::error("Link init failed, exit");
return 0;
}
// 在初始化QApplication之前初始化视频输出和linuxfb
// 否则不能显示qt的ui
// HDMI-OUT0视频输出只将ui输出在0口
vo = Link::create("OutputVo");
QVariantMap dataVo;
dataVo["ui"] = true;
dataVo["type"] = "hdmi";
vo->start(dataVo);
// HDMI-OUT1视频输出
vo1 = Link::create("OutputVo");
QVariantMap dataVo1;
dataVo1["ui"] = false;
dataVo1["type"] = "bt1120";
vo1->start(dataVo1);
/**
* 后续可能需要进行修改**************************
*/
// HDMI-OUT1口特殊设置
static int lastNorm = 0;
int norm = 0;
int ddr = 0;
// 获取到设置的输出分辨率
QString str = "1080P60";
if (str == "1080P60")
norm = 9;
else if (str == "1080P50")
norm = 10;
else if (str == "1080P30")
norm = 12;
else if (str == "720P60")
norm = 5;
else if (str == "720P50")
norm = 6;
else if (str == "3840x2160_30") {
norm = 14;
ddr = 1;
}
if (norm != lastNorm) {
lastNorm = norm;
QString cmd = "rmmod hi_lt8618sx_lp.ko";
system(cmd.toLatin1().data());
cmd = cmd.sprintf("insmod /ko/extdrv/hi_lt8618sx_lp.ko norm=%d USE_DDRCLK=%d", norm, ddr);
system(cmd.toLatin1().data());
}
QApplication a(argc, argv);
// 初始化配置文件, 需要在QApplication之后
if (!loadConfiguration(Constant::ConfigurationPath)) {
Log::error("load configuration failed, exit...");
return 0;
}
// 打开串口
serialPortTool = new SerialPortTool();
serialPortTool->open();
// setChannelProtocol();
serialPortTool->onPowerOn(); // 打开电源灯
QThread::msleep(100);
// 开启Tcp服务
server = new TcpServer();
server->listen();
// qt界面
Widget w;
w.show();
// 硬盘检测线程,检测硬盘容量
CheckStorageThread* thread = new CheckStorageThread();
thread->start();
QObject::connect(thread, SIGNAL(diskWillFull()), serialPortTool, SLOT(onDiskWillFull()));
QObject::connect(thread, SIGNAL(diskNotFull()), serialPortTool, SLOT(onDiskNotFull()));
Log::info("start storage check thread...");
// 开始录制
startRecord();
return a.exec();
}