RecordControlApplication/Channel.cpp

441 lines
12 KiB
C++
Raw Permalink Normal View History

2024-01-18 15:41:43 +08:00
#include "Channel.h"
2024-03-04 16:22:40 +08:00
#include "Constant.h"
2024-08-12 11:26:42 +08:00
#include "DatabaseManager.h"
#include "Json.h"
2024-03-04 16:22:40 +08:00
#include "Log.h"
#include "Tool.h"
#include <QCoreApplication>
2024-03-04 16:22:40 +08:00
#include <QFileInfo>
2024-01-18 15:41:43 +08:00
#include <QProcess>
#include <QTime>
#include <chrono>
2024-01-18 15:41:43 +08:00
2024-08-12 11:26:42 +08:00
#include <QElapsedTimer>
LinkObject* Channel::lineIn = nullptr;
LinkObject* Channel::lineOut = nullptr;
2024-01-18 15:41:43 +08:00
LinkObject* Channel::rtspServer = nullptr;
2024-08-12 11:26:42 +08:00
LinkObject* Channel::resample = nullptr;
2024-01-18 15:41:43 +08:00
extern LinkObject* vo;
extern LinkObject* vo1;
2024-08-12 11:26:42 +08:00
extern DatabaseManager* db;
2024-01-18 15:41:43 +08:00
Channel::Channel(QObject* parent)
: QObject(parent)
{
videoInput = nullptr;
videoOutput = nullptr;
2024-08-12 11:26:42 +08:00
audioInput = nullptr;
audioOutput = nullptr;
2024-01-19 09:04:21 +08:00
videoEncoder = nullptr;
2024-01-18 15:41:43 +08:00
audioEncoder = nullptr;
record = nullptr;
rtsp = nullptr;
2024-08-12 11:26:42 +08:00
inputFile = nullptr;
2024-03-04 16:22:40 +08:00
videoDecoder = nullptr;
audioDecoder = nullptr;
image = Link::create("InputImage");
2024-01-18 15:41:43 +08:00
2024-08-12 11:26:42 +08:00
if (lineIn == nullptr) {
lineIn = Link::create("InputAlsa");
2024-01-18 15:41:43 +08:00
QVariantMap dataIn;
dataIn["path"] = "hw:0,0";
dataIn["channels"] = 2;
2024-08-12 11:26:42 +08:00
lineIn->start(dataIn);
}
if (resample = nullptr) {
resample = Link::create("Resample");
resample->start();
lineIn->linkA(resample);
2024-01-18 15:41:43 +08:00
}
2024-08-12 11:26:42 +08:00
if (lineOut == nullptr) {
lineOut = Link::create("OutputAlsa");
2024-01-18 15:41:43 +08:00
QVariantMap dataOut;
dataOut["path"] = "hw:0,0";
2024-08-12 11:26:42 +08:00
lineOut->start(dataOut);
2024-01-18 15:41:43 +08:00
}
if (rtspServer == nullptr) {
rtspServer = Link::create("Rtsp");
rtspServer->start();
}
}
/**
2024-03-04 16:22:40 +08:00
* @brief
2024-01-18 15:41:43 +08:00
*/
void Channel::init()
{
if (channelName.isEmpty()) {
2024-03-04 16:22:40 +08:00
Log::error("channel name is empty!");
2024-01-18 15:41:43 +08:00
return;
}
2024-08-12 11:26:42 +08:00
// 通道音频输出
audioOutput = Link::create("OutputAo");
QVariantMap dataVo;
2024-03-04 16:22:40 +08:00
if (channelName == Constant::MainChannel) {
2024-01-18 15:41:43 +08:00
videoOutput = vo;
2024-08-12 11:26:42 +08:00
dataVo["interface"] = "HDMI-OUT0";
2024-01-18 15:41:43 +08:00
} else {
videoOutput = vo1;
2024-08-12 11:26:42 +08:00
dataVo["interface"] = "HDMI-OUT1";
2024-01-18 15:41:43 +08:00
}
2024-08-12 11:26:42 +08:00
loadOverlayConfig();
// 水印,用于显示录制状态
overlay = Link::create("Overlay");
overlay->start(norecordOverlay);
overlay->linkV(videoOutput);
2024-01-18 15:41:43 +08:00
2024-03-04 16:22:40 +08:00
// 视频输入
2024-01-18 15:41:43 +08:00
videoInput = Link::create("InputVi");
QVariantMap dataVi;
dataVi["interface"] = channelName;
2024-03-04 16:22:40 +08:00
dataVi["width"] = 1920;
dataVi["height"] = 1080;
2024-01-18 15:41:43 +08:00
videoInput->start(dataVi);
2024-08-12 11:26:42 +08:00
videoInput->linkV(overlay)->linkV(videoOutput);
2024-01-18 15:41:43 +08:00
2024-08-12 11:26:42 +08:00
// 通道音频输入
audioInput = Link::create("InputAi");
QVariantMap dataAi;
dataAi["interface"] = channelName;
dataAi["channels"] = 2;
audioInput->start(dataAi);
audioInput->linkA(audioOutput);
// 音量
gain = Link::create("Gain");
gain->start();
volume = Link::create("Volume");
volume->start();
gain->linkA(volume);
lineOut->linkA(gain);
2024-01-18 15:41:43 +08:00
2024-03-04 16:22:40 +08:00
// 音频编码
2024-01-18 15:41:43 +08:00
audioEncoder = Link::create("EncodeA");
audioEncoder->start(audioEncoderParams);
2024-08-12 11:26:42 +08:00
// 视频编码
videoEncoder = Link::create("EncodeV");
videoEncoder->start(videoEncoderParams);
2024-03-04 16:22:40 +08:00
// 录制
2024-01-18 15:41:43 +08:00
record = Link::create("Mux");
QVariantMap dataMp4;
dataMp4["format"] = "mp4";
2024-08-12 11:26:42 +08:00
dataMp4["filecache"] = 20480000;
dataMp4["lowLatency"] = true;
2024-08-12 11:26:42 +08:00
dataMp4["thread"] = true;
dataMp4["segmentDuration"] = duration;
2024-01-18 15:41:43 +08:00
record->setData(dataMp4);
videoInput->linkV(videoEncoder)->linkV(record);
2024-08-12 11:26:42 +08:00
audioInput->linkV(audioEncoder)->linkV(record);
resample->linkA(audioEncoder)->linkA(record);
connect(record, SIGNAL(newEvent(QString, QVariant)), this, SLOT(onNewEvent(QString, QVariant)));
// 测试执行时间用时18-20ms
// connect(record, &LinkObject::newEvent, [=](QString msg, QVariant data) {
// Tool::getCostTime(
// [=] {
// onNewEvent(msg, data);
// },
// "onNewEvent");
// });
2024-03-04 16:22:40 +08:00
// rstp流
2024-01-18 15:41:43 +08:00
rtsp = Link::create("Mux");
QVariantMap dataRtsp;
dataRtsp["path"] = QString("mem://%1").arg(pushCode);
dataRtsp["format"] = "rtsp";
rtsp->start(dataRtsp);
videoInput->linkV(videoEncoder)->linkV(rtsp)->linkV(rtspServer);
2024-08-12 11:26:42 +08:00
audioInput->linkA(audioEncoder)->linkV(rtsp)->linkV(rtspServer);
resample->linkA(audioEncoder)->linkA(rtsp)->linkA(rtspServer);
2024-03-04 16:22:40 +08:00
// 播放文件
2024-08-12 11:26:42 +08:00
inputFile = Link::create("InputFile");
connect(inputFile, &LinkObject::newEvent, [=](QString type, QVariant msg) {
2024-03-04 16:22:40 +08:00
if (type == "EOF") {
Log::info("{} one video playback end", channelName.toStdString());
emit playEnd();
}
});
// 音频解码只在HDMI-OUT0输出音频
audioDecoder = Link::create("DecodeA");
audioDecoder->start();
if (channelName == Constant::MainChannel) {
2024-08-12 11:26:42 +08:00
inputFile->linkA(audioDecoder)->linkA(lineOut);
2024-03-04 16:22:40 +08:00
} else {
2024-08-12 11:26:42 +08:00
inputFile->linkA(audioDecoder);
2024-03-04 16:22:40 +08:00
}
// 视频解码
videoDecoder = Link::create("DecodeV");
videoDecoder->start();
2024-08-12 11:26:42 +08:00
inputFile->linkV(videoDecoder)->linkV(videoOutput);
2024-01-18 15:41:43 +08:00
}
/**
2024-03-04 16:22:40 +08:00
* @brief
2024-01-18 15:41:43 +08:00
*/
void Channel::startRecord()
{
2024-08-12 11:26:42 +08:00
QElapsedTimer mstimer;
mstimer.start();
// 记录本次录制开始时间以及当前视频录制的开始时间
QString curTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
startTime = curTime;
currentTime = curTime;
2024-01-18 15:41:43 +08:00
QVariantMap dataRecord;
2024-08-12 11:26:42 +08:00
QString path = QString("%1/%2/%3_%d.mp4").arg(Constant::VideoPath).arg(channelName).arg(curTime);
2024-03-04 16:22:40 +08:00
dataRecord["path"] = path;
record->start(dataRecord);
isRecord = true;
Log::info("{} start recording...", channelName.toStdString());
2024-08-12 11:26:42 +08:00
Log::info("open done {}", path.toStdString());
// 显示录制状态水印
overlay->setData(recordOverlay);
float time = (double)mstimer.nsecsElapsed() / (double)1000000;
qDebug() << channelName << "startRecord cast time:" << time << "ms";
}
/**
* @brief
* @param msg
* @param data
*/
void Channel::onNewEvent(QString msg, QVariant data)
{
if (msg == "newSegment") {
int id = data.toInt();
// 将上一次的文件信息存入数据库中
DatabaseManager::File file;
file.channel = channelName == Constant::MainChannel
? DatabaseManager::MainChannel
: DatabaseManager::SecondaryChannel;
// 设置当前视频的录制时间信息
file.year = currentTime.mid(0, 4);
file.month = currentTime.mid(4, 2);
file.day = currentTime.mid(6, 2);
file.time = currentTime.mid(8, 6);
// 设置当前视频的文件名格式本次录制开始时间_第几次分片
file.filename = QString("%1_%2.mp4").arg(startTime).arg(id - 1);
if (db->insert(file)) {
Log::info("insert one record into database success, name: {}, channel: {}",
file.filename.toStdString(),
channelName.toStdString());
} else {
Log::error("insert one record into database failed, name: {}, channel: {}",
file.filename.toStdString(),
channelName.toStdString());
}
// 更新当前录制录制视频的时间
currentTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
}
2024-01-18 15:41:43 +08:00
}
/**
2024-03-04 16:22:40 +08:00
* @brief
2024-01-18 15:41:43 +08:00
*/
void Channel::stopRecord()
{
2024-03-04 16:22:40 +08:00
Log::info("{} stop recording...", channelName.toStdString());
2024-01-18 15:41:43 +08:00
record->stop(true);
2024-08-12 11:26:42 +08:00
// 将录制文件的信息存入数据库
DatabaseManager::File file;
file.channel = channelName == Constant::MainChannel
? DatabaseManager::MainChannel
: DatabaseManager::SecondaryChannel;
file.year = startTime.mid(0, 4);
file.month = startTime.mid(4, 2);
file.day = startTime.mid(6, 2);
file.time = startTime.mid(8, 6);
overlay->setData(norecordOverlay);
2024-01-18 15:41:43 +08:00
}
/**
2024-03-04 16:22:40 +08:00
* @brief
* @param path
* @return /
2024-01-18 15:41:43 +08:00
*/
2024-03-04 16:22:40 +08:00
bool Channel::startPlayback(QString path)
2024-01-18 15:41:43 +08:00
{
2024-03-04 16:22:40 +08:00
QFileInfo info(path);
if (!info.exists()) {
Log::error("cannot open video {} , video file does not exist", path.toStdString());
videoInput->unLinkV(videoOutput);
QVariantMap dataImage;
dataImage["path"] = Constant::EmptyImagePath;
image->start(dataImage);
image->linkV(videoOutput);
state = Error;
return false;
}
2024-01-18 15:41:43 +08:00
2024-03-04 16:22:40 +08:00
// 开始回放
2024-01-18 15:41:43 +08:00
QVariantMap dataFile;
dataFile["path"] = path;
2024-08-12 11:26:42 +08:00
dataFile["sync"] = true;
inputFile->start(dataFile);
2024-01-18 15:41:43 +08:00
2024-03-04 16:22:40 +08:00
// 判断视频是否损坏,如果损坏则输出提示图片
2024-08-12 11:26:42 +08:00
int duration = inputFile->invoke("getDuration", path).toInt();
2024-03-04 16:22:40 +08:00
if (duration == 0) {
Log::error("cannot open video {}, video file was corrupted", path.toStdString());
2024-08-12 11:26:42 +08:00
inputFile->stop();
2024-03-04 16:22:40 +08:00
videoInput->unLinkV(videoOutput);
QVariantMap dataImage;
dataImage["path"] = Constant::ErrorImagePath;
image->start(dataImage);
image->linkV(videoOutput);
state = Error;
return false;
}
2024-01-18 15:41:43 +08:00
2024-08-12 11:26:42 +08:00
// 断开视频信号输出,启动回放输出
overlay->unLinkV(videoOutput);
2024-03-04 16:22:40 +08:00
videoDecoder->linkV(videoOutput);
2024-08-12 11:26:42 +08:00
// 断开音频信号输出,启动回放输出
audioInput->unLinkA(audioOutput);
audioDecoder->linkA(lineOut);
2024-03-04 16:22:40 +08:00
playbackDuration = duration;
state = Playback;
return true;
}
2024-01-19 09:04:21 +08:00
/**
2024-03-04 16:22:40 +08:00
* @brief
2024-01-19 09:04:21 +08:00
*/
void Channel::startPlayLive()
{
2024-03-04 16:22:40 +08:00
if (state == Playback) {
videoDecoder->unLinkV(videoOutput);
2024-08-12 11:26:42 +08:00
audioDecoder->unLinkA(audioOutput);
inputFile->stop(true);
2024-03-04 16:22:40 +08:00
} else if (state == Error) {
image->unLinkV(videoOutput);
image->stop(true);
}
2024-08-12 11:26:42 +08:00
// 打开视频和音频输出
overlay->linkV(videoOutput);
audioInput->linkA(audioOutput);
// 关闭外部音频输出
audioDecoder->unLinkA(lineOut);
2024-03-04 16:22:40 +08:00
state = Stop;
2024-01-19 09:04:21 +08:00
}
2024-01-18 15:41:43 +08:00
/**
2024-03-04 16:22:40 +08:00
* @brief 退10s
2024-01-18 15:41:43 +08:00
*/
void Channel::back()
{
2024-03-04 16:22:40 +08:00
Log::info("{} back 10s", channelName.toStdString());
2024-08-12 11:26:42 +08:00
int curPos = inputFile->invoke("getPosition").toInt();
curPos -= 10 * 1000;
2024-08-12 11:26:42 +08:00
inputFile->invoke("seek", curPos);
2024-01-18 15:41:43 +08:00
}
/**
2024-03-04 16:22:40 +08:00
* @brief 10s
2024-01-18 15:41:43 +08:00
*/
void Channel::forward()
{
2024-03-04 16:22:40 +08:00
Log::info("{} forward 10s", channelName.toStdString());
2024-08-12 11:26:42 +08:00
int curPos = inputFile->invoke("getPosition").toInt();
curPos += 10 * 1000;
2024-08-12 11:26:42 +08:00
inputFile->invoke("seek", curPos);
2024-01-18 15:41:43 +08:00
}
/**
2024-03-04 16:22:40 +08:00
* @brief
2024-01-18 15:41:43 +08:00
*/
void Channel::togglePause()
{
2024-03-04 16:22:40 +08:00
if (state == Stop || state == Error)
return;
if (state == Playback)
state = Pause;
else
state = Playback;
2024-08-12 11:26:42 +08:00
inputFile->invoke("pause", state == Pause);
2024-01-18 15:41:43 +08:00
}
/**
* @brief
2024-01-18 15:41:43 +08:00
*/
void Channel::showFinishPromot()
2024-01-18 15:41:43 +08:00
{
videoInput->unLinkV(videoDecoder);
QVariantMap dataImage;
dataImage["path"] = Constant::FinishImagePath;
image->start(dataImage);
image->linkV(videoOutput);
state = Finish;
2024-08-12 11:26:42 +08:00
}
/**
* @brief
* @return L左声道音量R右声道音量
*/
QVariantMap Channel::getVolume()
{
QVariantMap result;
QVariantMap data = volume->invoke("getVolume").toMap();
result["L"] = data["max"].toInt();
if (data["avg"].toInt() < 15)
result["L"] = 0;
result["R"] = data["max2"].toInt();
if (data["avg2"].toInt() < 15)
result["R"] = 0;
return result;
}
/**
* @brief
*/
void Channel::volumeUp()
{
if (curGain < maxGian)
curGain += 6;
QVariantMap data;
data["gain"] = curGain;
gain->setData(data);
}
/**
* @brief
*/
void Channel::volumeDown()
{
if (curGain > minGain)
curGain -= 6;
QVariantMap data;
data["gain"] = curGain;
gain->setData(data);
}
/**
* @brief
*/
void Channel::loadOverlayConfig()
{
auto loadFromJson = [](const QString& path) {
QVariantMap dataOver;
QVariantList list = Json::loadFile(path).toList();
QVariantList list2;
for (int i = 0; i < list.count(); i++) {
QVariantMap map = list[i].toMap();
list2 << map;
}
dataOver["lays"] = list2;
return dataOver;
};
recordOverlay = loadFromJson(Constant::RecordOverlay);
norecordOverlay = loadFromJson(Constant::NoRecordOverlay);
}