refactored import function for greater robustness

The import function was written early on in the rewrite as a multithreaded
background task that was considered somewhat flawed. While it worked for the
most part, there were possibilities of race conditions that could have
potentially been fatal, particularly since media could theoretically be
deleted while the import/probe tasks were running in the background.

With the save/load functions coming in, it became even more complicated as
projects may include metadata about the footage that can't be implemented
easily when the footage is imported/probed in the background. Making importing
a modal task fixes all of these issues, it's still done in a background thread
to not hang the GUI thread, but the GUI thread can be briefly "paused" in a
user friendly way so that all these functions can be safer.
This commit is contained in:
itsmattkc
2020-01-15 15:19:14 +11:00
parent 16904c976f
commit 75a9291cd2
24 changed files with 233 additions and 382 deletions
-3
View File
@@ -14,9 +14,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
add_subdirectory(import)
add_subdirectory(probe)
set(OLIVE_SOURCES
${OLIVE_SOURCES}
task/task.h
-22
View File
@@ -1,22 +0,0 @@
# Olive - Non-Linear Video Editor
# Copyright (C) 2019 Olive Team
#
# This program 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.
#
# This program 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(OLIVE_SOURCES
${OLIVE_SOURCES}
task/import/import.h
task/import/import.cpp
PARENT_SCOPE
)
-160
View File
@@ -1,160 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program 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.
This program 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "import.h"
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
// FIXME: Only used for test code
#include "panel/panelmanager.h"
#include "panel/project/project.h"
// End test code
#include "core.h"
#include "project/item/footage/footage.h"
#include "task/probe/probe.h"
#include "task/taskmanager.h"
ImportTask::ImportTask(ProjectViewModel *model, Folder *parent, const QStringList &urls) :
model_(model),
urls_(urls),
parent_(parent),
command_(nullptr)
{
set_text(tr("Importing %1 files").arg(urls.size()));
}
bool ImportTask::Action()
{
parent_->LockDeletes();
command_ = new QUndoCommand();
Import(urls_, parent_, command_);
// If this task was cancelled, we won't bother pushing an undo command (we don't end up with anything undoable since
// the undo command executes the final import anyway)
if (cancelled()) {
delete command_;
command_ = nullptr;
parent_->UnlockDeletes();
}
return true;
}
bool ImportTask::Epilogue()
{
if (command_ != nullptr) {
Core::instance()->undo_stack()->push(command_);
}
parent_->UnlockDeletes();
return true;
}
void ImportTask::Import(const QStringList &files, Folder *folder, QUndoCommand *parent_command)
{
for (int i=0;i<files.size();i++) {
// Stop here if the Task has been cancelled
if (cancelled()) {
break;
}
const QString& url = files.at(i);
QFileInfo file_info(url);
// Check if this file is a diretory
if (file_info.isDir()) {
// Use QDir to get a list of all the files in the directory
QDir dir(url);
// QDir::entryList only returns filenames, we can use entryInfoList() to get full paths
QFileInfoList entry_list = dir.entryInfoList();
// Only proceed if the empty actually has files in it
if (!entry_list.isEmpty()) {
// Create a folder corresponding to the directory
ItemPtr f = std::make_shared<Folder>();
f->set_name(file_info.fileName());
// Create undoable command that adds the items to the model
new ProjectViewModel::AddItemCommand(model_,
folder,
f,
parent_command);
// Convert QFileInfoList into QStringList
QStringList full_urls;
foreach (QFileInfo info, entry_list) {
if (info.fileName() != ".." && info.fileName() != ".") {
full_urls.append(info.absoluteFilePath());
}
}
// Recursively follow this path
Import(full_urls, static_cast<Folder*>(f.get()), parent_command);
}
} else {
FootagePtr f = std::make_shared<Footage>();
// FIXME: Is it possible for a file to go missing between the Import dialog and here?
// And what is the behavior/result of that?
f->set_filename(url);
f->set_name(file_info.fileName());
f->set_timestamp(file_info.lastModified());
// Create undoable command that adds the items to the model
new ProjectViewModel::AddItemCommand(model_,
folder,
f,
parent_command);
// Create ProbeTask to analyze this media
TaskPtr pt = std::make_shared<ProbeTask>(f);
// The task won't work unless it's in the main thread and we're definitely not
// FIXME: Should Tasks check what thread they're in and move themselves to the main thread?
pt->moveToThread(qApp->thread());
// Queue task in task manager
new TaskManager::AddTaskCommand(pt, parent_command);
//olive::task_manager.AddTask(pt);
}
emit ProgressChanged(i * 100 / files.size());
}
}
-56
View File
@@ -1,56 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program 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.
This program 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef IMPORT_H
#define IMPORT_H
#include "project/projectviewmodel.h"
#include "project/item/folder/folder.h"
#include "task/task.h"
/**
* @brief The ImportTask class
*
* A background task to create Footage objects from a list of URLs, and then create ProbeTasks for each of them.
*
* Using this Task is the best way to import media into a project since it will run in the background/multithreaded
* without pausing the main thread.
*/
class ImportTask : public Task
{
Q_OBJECT
public:
ImportTask(ProjectViewModel* model, Folder *parent, const QStringList& urls);
virtual bool Action() override;
virtual bool Epilogue() override;
private:
void Import(const QStringList& files, Folder* folder, QUndoCommand* parent_command);
ProjectViewModel* model_;
QStringList urls_;
Folder* parent_;
QUndoCommand* command_;
};
#endif // IMPORT_H
-22
View File
@@ -1,22 +0,0 @@
# Olive - Non-Linear Video Editor
# Copyright (C) 2019 Olive Team
#
# This program 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.
#
# This program 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(OLIVE_SOURCES
${OLIVE_SOURCES}
task/probe/probe.h
task/probe/probe.cpp
PARENT_SCOPE
)
-44
View File
@@ -1,44 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program 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.
This program 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "probe.h"
#include <QFileInfo>
#include "codec/decoder.h"
ProbeTask::ProbeTask(FootagePtr footage) :
footage_(footage)
{
QString base_filename = QFileInfo(footage_->filename()).fileName();
set_text(tr("Probing \"%1\"").arg(base_filename));
}
bool ProbeTask::Action()
{
footage_->LockDeletes();
Decoder::ProbeMedia(footage_.get());
footage_->UnlockDeletes();
return true;
}
-49
View File
@@ -1,49 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program 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.
This program 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef PROBE_H
#define PROBE_H
#include "project/item/footage/footage.h"
#include "task/task.h"
/**
* @brief The ProbeTask class
*
* A background task for probing a certain Footage file for its metadata and determining if we have a viable decoder
* for it.
*
* Currently this function just calls olive::ProbeMedia() which will call Footage::Clear(), clearing the Footage of
* any previous metadata before passing it through the available decoders until it finds one that can parse it.
* The ProbeTask mostly functions as a background/multithreaded wrapper for this functionality.
*/
class ProbeTask : public Task
{
Q_OBJECT
public:
ProbeTask(FootagePtr footage);
virtual bool Action() override;
private:
FootagePtr footage_;
};
#endif // PROBE_H