Skip to content

Commit

Permalink
Merge pull request #8455 from tcanabrava/remove_settings_receiver
Browse files Browse the repository at this point in the history
Remove QGCApplication and VideoSettings from the VideoReceiver
  • Loading branch information
dogmaphobic authored Mar 4, 2020
2 parents 7cb08b7 + 63623b0 commit 5322602
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/QGCApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class QGCApplication : public QApplication
void reportMissingParameter(int componentId, const QString& name);

/// Show a non-modal message to the user
void showMessage(const QString& message);
Q_SLOT void showMessage(const QString& message);

/// @return true: Fake ui into showing mobile interface
bool fakeMobile(void) const { return _fakeMobile; }
Expand Down
2 changes: 1 addition & 1 deletion src/Settings/VideoSettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,5 @@ bool VideoSettings::streamConfigured(void)

void VideoSettings::_configChanged(QVariant)
{
emit streamConfiguredChanged();
emit streamConfiguredChanged(streamConfigured());
}
2 changes: 1 addition & 1 deletion src/Settings/VideoSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class VideoSettings : public SettingsGroup
static const char* videoSourceMPEGTS;

signals:
void streamConfiguredChanged ();
void streamConfiguredChanged (bool configured);

private slots:
void _configChanged (QVariant value);
Expand Down
100 changes: 100 additions & 0 deletions src/VideoStreaming/VideoManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ VideoManager::setToolbox(QGCToolbox *toolbox)
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
qmlRegisterUncreatableType<VideoManager> ("QGroundControl.VideoManager", 1, 0, "VideoManager", "Reference only");
qmlRegisterUncreatableType<VideoReceiver>("QGroundControl", 1, 0, "VideoReceiver","Reference only");

// TODO: Those connections should be Per Video, not per VideoManager.
_videoSettings = toolbox->settingsManager()->videoSettings();
QString videoSource = _videoSettings->videoSource()->rawValue().toString();
connect(_videoSettings->videoSource(), &Fact::rawValueChanged, this, &VideoManager::_videoSourceChanged);
Expand All @@ -72,7 +74,56 @@ VideoManager::setToolbox(QGCToolbox *toolbox)
emit isGStreamerChanged();
qCDebug(VideoManagerLog) << "New Video Source:" << videoSource;
_videoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
_videoReceiver->setUnittestMode(qgcApp()->runningUnitTests());
_thermalVideoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
_thermalVideoReceiver->setUnittestMode(qgcApp()->runningUnitTests());
_videoReceiver->moveToThread(qgcApp()->thread());
_thermalVideoReceiver->moveToThread(qgcApp()->thread());

// Those connects are temporary: In a perfect world those connections are going to be done on the Qml
// but because currently the videoReceiver is created in the C++ world, this is easier.
// The fact returning a QVariant is a quite annoying to use proper signal / slot connection.
_updateSettings();

auto appSettings = toolbox->settingsManager()->appSettings();
for (auto *videoReceiver : { _videoReceiver, _thermalVideoReceiver}) {
// First, Setup the current values from the settings.
videoReceiver->setRtspTimeout(_videoSettings->rtspTimeout()->rawValue().toInt());
videoReceiver->setStreamEnabled(_videoSettings->streamEnabled()->rawValue().toBool());
videoReceiver->setRecordingFormatId(_videoSettings->recordingFormat()->rawValue().toInt());
videoReceiver->setStreamConfigured(_videoSettings->streamConfigured());

connect(_videoSettings->rtspTimeout(), &Fact::rawValueChanged,
videoReceiver, [videoReceiver](const QVariant &value) {
videoReceiver->setRtspTimeout(value.toInt());
}
);

connect(_videoSettings->streamEnabled(), &Fact::rawValueChanged,
videoReceiver, [videoReceiver](const QVariant &value) {
videoReceiver->setStreamEnabled(value.toBool());
}
);

connect(_videoSettings->recordingFormat(), &Fact::rawValueChanged,
videoReceiver, [videoReceiver](const QVariant &value) {
videoReceiver->setRecordingFormatId(value.toInt());
}
);

// Why some options are facts while others aren't?
connect(_videoSettings, &VideoSettings::streamConfiguredChanged, videoReceiver, &VideoReceiver::setStreamConfigured);

// Fix those.
// connect(appSettings, &Fact::rawValueChanged, videoReceiver, &VideoReceiver::setVideoPath);
// connect(appSettings->videoSavePath(), &Fact::rawValueChanged, videoReceiver, &VideoReceiver::setImagePath);

// Connect the video receiver with the rest of the app.
connect(videoReceiver, &VideoReceiver::restartTimeout, this, &VideoManager::restartVideo);
connect(videoReceiver, &VideoReceiver::sendMessage, qgcApp(), &QGCApplication::showMessage);
connect(videoReceiver, &VideoReceiver::beforeRecording, this, &VideoManager::cleanupOldVideos);
}

_updateSettings();
if(isGStreamer()) {
startVideo();
Expand All @@ -84,6 +135,55 @@ VideoManager::setToolbox(QGCToolbox *toolbox)
#endif
}

QStringList VideoManager::videoMuxes()
{
return {"matroskamux", "qtmux", "mp4mux"};
}

QStringList VideoManager::videoExtensions()
{
return {"mkv", "mov", "mp4"};
}

void VideoManager::cleanupOldVideos()
{
#if defined(QGC_GST_STREAMING)
//-- Only perform cleanup if storage limit is enabled
if(!_videoSettings->enableStorageLimit()->rawValue().toBool()) {
return;
}
QString savePath = qgcApp()->toolbox()->settingsManager()->appSettings()->videoSavePath();
QDir videoDir = QDir(savePath);
videoDir.setFilter(QDir::Files | QDir::Readable | QDir::NoSymLinks | QDir::Writable);
videoDir.setSorting(QDir::Time);

QStringList nameFilters;
for(const QString& extension : videoExtensions()) {
nameFilters << QString("*.") + extension;
}
videoDir.setNameFilters(nameFilters);
//-- get the list of videos stored
QFileInfoList vidList = videoDir.entryInfoList();
if(!vidList.isEmpty()) {
uint64_t total = 0;
//-- Settings are stored using MB
uint64_t maxSize = _videoSettings->maxVideoSize()->rawValue().toUInt() * 1024 * 1024;
//-- Compute total used storage
for(int i = 0; i < vidList.size(); i++) {
total += vidList[i].size();
}
//-- Remove old movies until max size is satisfied.
while(total >= maxSize && !vidList.isEmpty()) {
total -= vidList.last().size();
qCDebug(VideoReceiverLog) << "Removing old video file:" << vidList.last().filePath();
QFile file (vidList.last().filePath());
file.remove();
vidList.removeLast();
}
}
#endif
}

//-----------------------------------------------------------------------------
void
VideoManager::startVideo()
Expand Down
5 changes: 5 additions & 0 deletions src/VideoStreaming/VideoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class VideoManager : public QGCTool
virtual VideoReceiver* videoReceiver () { return _videoReceiver; }
virtual VideoReceiver* thermalVideoReceiver () { return _thermalVideoReceiver; }

QStringList videoExtensions();
QStringList videoMuxes();

#if defined(QGC_DISABLE_UVC)
virtual bool uvcEnabled () { return false; }
#else
Expand All @@ -82,6 +85,8 @@ class VideoManager : public QGCTool
Q_INVOKABLE void startVideo ();
Q_INVOKABLE void stopVideo ();

void cleanupOldVideos();

signals:
void hasVideoChanged ();
void isGStreamerChanged ();
Expand Down
Loading

0 comments on commit 5322602

Please sign in to comment.