From b6215897df7a42213f3b0549499dd9c0436e9ab8 Mon Sep 17 00:00:00 2001 From: zc Date: Mon, 4 Mar 2024 21:47:32 -0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=86=E9=A2=91=E5=BD=95?= =?UTF-8?q?=E5=88=B6=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E5=BD=95=E5=88=B6=E8=B5=B7=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Channel.cpp | 50 ++++++++---------------------------------- Channel.h | 5 +---- CheckStorageThread.cpp | 9 ++++---- Constant.h | 1 - TcpController.cpp | 24 +++++++++++++++----- TcpController.h | 2 ++ Tool.cpp | 18 ++++++++------- main.cpp | 3 ++- 8 files changed, 46 insertions(+), 66 deletions(-) diff --git a/Channel.cpp b/Channel.cpp index 7217bb7..1636a7f 100755 --- a/Channel.cpp +++ b/Channel.cpp @@ -26,8 +26,6 @@ Channel::Channel(QObject* parent) audioEncoder = nullptr; record = nullptr; rtsp = nullptr; - snap = Link::create("EncodeV"); - overlay = Link::create("Overlay"); file = nullptr; videoDecoder = nullptr; audioDecoder = nullptr; @@ -76,37 +74,7 @@ void Channel::init() dataVi["width"] = 1920; dataVi["height"] = 1080; videoInput->start(dataVi); - - // 测试水印 - QVariantMap dataOver; - QVariantList lays; - QVariantMap lay; - lay["type"] = "time"; - lay["enable"] = true; - lay["font"] = "/link/res/font.ttf"; - lay["content"] = "yyyy年MM月dd日 hh:mm:ss"; - lay["x"] = 0.1; - lay["y"] = 0.5; - lay["scale"] = 2; - lay["color"] = "#ffffff"; - lay["alpha"] = 1; - lays << lay; - dataOver["lays"] = lays; - overlay->start(dataOver); - videoInput->linkV(overlay)->linkV(videoOutput); - - // videoInput->linkV(videoOutput); - - // 截图 - QVariantMap dataSnap; - dataSnap["codec"] = "jpeg"; - dataSnap["snap"] = true; - dataSnap["width"] = 1920; - dataSnap["height"] = 1080; - snap->start(dataSnap); - videoInput->linkV(snap); - // 等待1秒,防止截图失败 - QThread::sleep(1); + videoInput->linkV(videoOutput); // 视频编码 videoEncoder = Link::create("EncodeV"); @@ -120,7 +88,8 @@ void Channel::init() record = Link::create("Mux"); QVariantMap dataMp4; dataMp4["format"] = "mp4"; - dataMp4["lowLatency"] = true; + // dataMp4["lowLatency"] = true; + // dataMp4["filecache"] = 20480000; record->setData(dataMp4); videoInput->linkV(videoEncoder)->linkV(record); audioInput->linkA(audioEncoder)->linkA(record); @@ -163,13 +132,12 @@ void Channel::init() */ void Channel::onTimeout() { - QString curTime = QDateTime::currentDateTime().toString("yyyy-MM-dd_hh:mm:ss"); + while (int secs = QTime::currentTime().second() % 60 != 0) { + } + QString curTime = QDateTime::currentDateTime().toString("yyyyMMddhhmm"); QString path = QString("%1/%2/%3.mp4").arg(Constant::VideoPath).arg(channelName).arg(curTime); // 重新分片 record->invoke("segment", path); - - // path = QString("%1/%2/%3.jpg").arg(Constant::SnapPath).arg(channelName).arg(curTime); - // snap->invoke("snapSync", path); } /** @@ -181,8 +149,10 @@ void Channel::startRecord() Log::error("there are no disk mounted"); return; } + while (int secs = QTime::currentTime().second() % 60 != 0) { + } - QString curTime = QDateTime::currentDateTime().toString("yyyy-MM-dd_hh:mm:ss"); + QString curTime = QDateTime::currentDateTime().toString("yyyyMMddhhmm"); QVariantMap dataRecord; QString path = QString("%1/%2/%3.mp4").arg(Constant::VideoPath).arg(channelName).arg(curTime); dataRecord["path"] = path; @@ -191,8 +161,6 @@ void Channel::startRecord() Log::info("{} start recording...", channelName.toStdString()); timer->start(); - // path = QString("%1/%2/%3.jpg").arg(Constant::SnapPath).arg(channelName).arg(curTime); - // snap->invoke("snapSync", path); emit showRecordState(true); } diff --git a/Channel.h b/Channel.h index c7830f9..c796126 100755 --- a/Channel.h +++ b/Channel.h @@ -29,12 +29,9 @@ public: LinkObject* audioEncoder; QVariantMap audioEncoderParams; - LinkObject* overlay; - LinkObject* record; - LinkObject* snap; // 单个视频时长 - int duration = 1 * 60 * 1000; + int duration = 10 * 60 * 1000; bool isRecord = false; LinkObject* file; diff --git a/CheckStorageThread.cpp b/CheckStorageThread.cpp index 30537bf..949493b 100755 --- a/CheckStorageThread.cpp +++ b/CheckStorageThread.cpp @@ -20,7 +20,7 @@ CheckStorageThread::CheckStorageThread() } /** - * @brief check available storage each 10 min + * @brief 10分钟检查一次外接硬盘的可用空间 */ void CheckStorageThread::run() { @@ -28,12 +28,11 @@ void CheckStorageThread::run() int64_t available = Tool::getAvailableStorage(Constant::MountedPath); if (available < THRESHOLD) { Log::info("there are not enough storage, then remove some files..."); - // get the file list + // 获取文件列表 QStringList fileList = Tool::getFileList(QString("%1/%2").arg(Constant::VideoPath).arg(Constant::MainChannel)); if (!fileList.isEmpty()) { - // remove the first video file QString filename = fileList.first(); - // wait until file is not playing + // 判断文件是否再回放,如果在回放就阻塞等待 mutex.lock(); if (filename == curFilename) { Log::info("{} is playing, wait for play end and remove the files...", filename.toStdString()); @@ -41,7 +40,7 @@ void CheckStorageThread::run() Log::info("check thread end wait..."); } mutex.unlock(); - // start remove file + // 删除文件 QString path = QString("%1/%2/%3").arg(Constant::VideoPath).arg(Constant::MainChannel).arg(filename); bool ret = Tool::removeFile(path); if (!ret) { diff --git a/Constant.h b/Constant.h index df284b7..7ea433e 100755 --- a/Constant.h +++ b/Constant.h @@ -19,7 +19,6 @@ public: static constexpr char* NetConfigPath = "/opt/RecordControlApplication/configuration/net.json"; static constexpr char* NetScriptPath = "/opt/RecordControlApplication/scripts/setNetwork.sh"; static constexpr char* VideoPath = "/root/usb/videos"; - static constexpr char* SnapPath = "/root/usb/snap"; static constexpr char* MountedPath = "/root/usb"; static constexpr char* ErrorImagePath = "/opt/RecordControlApplication/images/error.jpeg"; static constexpr char* EmptyImagePath = "/opt/RecordControlApplication/images/empty.jpeg"; diff --git a/TcpController.cpp b/TcpController.cpp index 4f80317..217760a 100755 --- a/TcpController.cpp +++ b/TcpController.cpp @@ -22,6 +22,7 @@ TcpController::TcpController(QObject* parent) routes.insert("get_name", std::bind(&TcpController::getName, this, std::placeholders::_1)); routes.insert("set_record_mode", std::bind(&TcpController::setRecordMode, this, std::placeholders::_1)); routes.insert("set_playback_mode", std::bind(&TcpController::setPlaybackMode, this, std::placeholders::_1)); + routes.insert("reoot", std::bind(&TcpController::reboot, this, std::placeholders::_1)); } Routes TcpController::getRoutes() @@ -214,16 +215,10 @@ void TcpController::deleteFile(QTcpSocket* socket) QString snapName = fileName.split(".")[0] + ".jpg"; QString filePath = QString("%1/%2/%3").arg(Constant::VideoPath).arg(interface).arg(fileName); - QString snapPath = QString("%1/%2/%3").arg(Constant::SnapPath).arg(interface).arg(fileName); QFile video(filePath); if (video.exists()) { video.remove(); Log::info("remove video {}", filePath.toStdString()); - QFile image(snapPath); - if (image.exists()) { - image.remove(); - Log::info("remove image: {}", snapPath.toStdString()); - } socket->write("url=delete_file\r\nstatus=success"); } else { Log::error("error, file: {} dont exist", filePath.toStdString()); @@ -288,3 +283,20 @@ void TcpController::setPlaybackMode(QTcpSocket* socket) Json::saveFile(cfg, Constant::ConfigurationPath); socket->write("url=set_playback_mode\r\nstatus=success"); } + +/** + * @brief 重启程序 + * @param socket + */ +void TcpController::reboot(QTcpSocket* socket) +{ + // 先停止所有的录制 + for (Channel* chn : channelList) { + chn->stopRecord(); + } + socket->write("url=reboot\r\nstatus=success"); + socket->flush(); + + // 退出程序,等待后台运行的shell脚本重启程序 + exit(0); +} \ No newline at end of file diff --git a/TcpController.h b/TcpController.h index 94c8b54..1811f11 100755 --- a/TcpController.h +++ b/TcpController.h @@ -31,6 +31,8 @@ private: void setRecordMode(QTcpSocket* socket); void setPlaybackMode(QTcpSocket* socket); + void reboot(QTcpSocket* socket); + inline QVariantMap parseParams(QTcpSocket* socket); }; diff --git a/Tool.cpp b/Tool.cpp index 9e5df7e..2c37bf5 100755 --- a/Tool.cpp +++ b/Tool.cpp @@ -13,7 +13,7 @@ Tool::Tool() } /** - * @brief get file list of + * @brief 获取文件列表 */ QStringList Tool::getFileList(QString path) { @@ -33,7 +33,7 @@ QStringList Tool::getFileList(QString path) } /** - * @brief remove file or dir + * @brief 删除文件或文件夹 */ bool Tool::removeFile(QString path) { @@ -58,7 +58,8 @@ bool Tool::removeFile(QString path) } /** - * @brief call linux shell tool + * @brief 另外开启一个进程,使用linux命令行工具 + * @param path 命令 */ #ifdef Q_OS_UNIX QString Tool::writeCom(QString path) @@ -67,10 +68,10 @@ QString Tool::writeCom(QString path) QStringList argList; argList << "-c" << path; proc.start("/bin/sh", argList); - // wait process start + // 等待进程启动 proc.waitForFinished(); proc.waitForReadyRead(); - // read data from console + // 从控制台读取数据 QByteArray procOutput = proc.readAll(); proc.close(); return QString(procOutput); @@ -78,14 +79,15 @@ QString Tool::writeCom(QString path) #endif /** - * get available storage of disk + * @brief 获取磁盘的可用空间 + * @param mountedPath 磁盘挂载位置 */ int64_t Tool::getAvailableStorage(QString mountedPath) { #ifdef Q_OS_UNIX - // get the available size of disk + // 使用linux命令获取磁盘挂载信息 QString mountInfo = writeCom(QString("df %1").arg(mountedPath)); - // parse the string like + // 解析挂载信息获取可用磁盘大侠 // "Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/sda 487110880 45112 462630056 0% /root/usb\n" QString diskInfo = mountInfo.split("\n")[1]; QStringList list = diskInfo.split(" "); diff --git a/main.cpp b/main.cpp index aab614f..3615fe5 100755 --- a/main.cpp +++ b/main.cpp @@ -154,7 +154,8 @@ int main(int argc, char* argv[]) // 硬盘检测线程,检测硬盘容量 CheckStorageThread* thread = new CheckStorageThread(); - // thread->start(); + thread->start(); + Log::info("start storage check thread..."); return a.exec(); }