116 lines
2.9 KiB
C++
116 lines
2.9 KiB
C++
// This file is part of JustMotion.
|
|
|
|
// JustMotion is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// JustMotion is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
#include "record_loop.h"
|
|
|
|
RecordLoop::RecordLoop(shared_t *sharedRes, QThread *thr, QObject *parent) : QProcess(parent)
|
|
{
|
|
checkTimer = 0;
|
|
shared = sharedRes;
|
|
|
|
connect(thr, &QThread::started, this, &RecordLoop::init);
|
|
connect(thr, &QThread::finished, this, &RecordLoop::deleteLater);
|
|
|
|
connect(this, &RecordLoop::readyReadStandardOutput, this, &RecordLoop::resetTime);
|
|
connect(this, &RecordLoop::readyReadStandardError, this, &RecordLoop::resetTime);
|
|
connect(this, &RecordLoop::started, this, &RecordLoop::resetTime);
|
|
|
|
moveToThread(thr);
|
|
}
|
|
|
|
RecordLoop::~RecordLoop()
|
|
{
|
|
terminate();
|
|
waitForFinished();
|
|
}
|
|
|
|
void RecordLoop::init()
|
|
{
|
|
checkTimer = new QTimer(this);
|
|
|
|
connect(checkTimer, &QTimer::timeout, this, &RecordLoop::restart);
|
|
|
|
checkTimer->setSingleShot(true);
|
|
checkTimer->setInterval(3000);
|
|
|
|
setupBuffDir(shared->buffPath);
|
|
restart();
|
|
}
|
|
|
|
QString RecordLoop::camCmdFromConf()
|
|
{
|
|
auto ret = "ffmpeg -hide_banner -y -i '" + shared->recordUri + "' -strftime 1 -strftime_mkdir 1 ";
|
|
|
|
if (shared->recordUri.contains("rtsp"))
|
|
{
|
|
ret += "-rtsp_transport udp ";
|
|
}
|
|
|
|
if (shared->vidCodec != "copy")
|
|
{
|
|
ret += "-vf fps=" + QString::number(shared->recFps) + ",scale=" + shared->recScale + " ";
|
|
}
|
|
|
|
ret += "-vcodec " + shared->vidCodec + " ";
|
|
ret += "-acodec " + shared->audCodec + " ";
|
|
ret += "-hls_time 3 -hls_list_size " + QString::number(shared->liveSecs / 3) + " -hls_flags delete_segments -hls_segment_filename vid/" + QString(STRFTIME_FMT) + shared->streamExt + " ";
|
|
ret += "pls.m3u8";
|
|
|
|
qInfo() << ret;
|
|
|
|
return ret;
|
|
}
|
|
|
|
QString RecordLoop::statusLine()
|
|
{
|
|
if (state() == QProcess::Running)
|
|
{
|
|
return "OK ";
|
|
}
|
|
else
|
|
{
|
|
return "FAIL";
|
|
}
|
|
}
|
|
|
|
void RecordLoop::resetTime()
|
|
{
|
|
checkTimer->start();
|
|
}
|
|
|
|
void RecordLoop::restart()
|
|
{
|
|
if (state() == QProcess::Running)
|
|
{
|
|
terminate();
|
|
waitForFinished();
|
|
}
|
|
|
|
auto cmdLine = camCmdFromConf();
|
|
auto args = parseArgs(cmdLine.toUtf8(), -1);
|
|
|
|
//qInfo() << "start recording command: " << cmdLine;
|
|
|
|
if (args.isEmpty())
|
|
{
|
|
qCritical() << "err: couldn't parse a program name";
|
|
}
|
|
else
|
|
{
|
|
setWorkingDirectory(shared->buffPath);
|
|
setProgram(args[0]);
|
|
setArguments(args.mid(1));
|
|
|
|
start();
|
|
}
|
|
}
|