File indexing completed on 2022-12-06 14:19:31

0001 /*
0002 SPDX-FileCopyrightText: 2021 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003 This file is part of Kdenlive. See www.kdenlive.org.
0004 
0005 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 
0009 #pragma once
0010 
0011 #include "abstracttask.h"
0012 #include "definitions.h"
0013 
0014 #include <QAbstractListModel>
0015 #include <QFutureWatcher>
0016 #include <QObject>
0017 #include <QReadWriteLock>
0018 #include <QThreadPool>
0019 #include <map>
0020 #include <memory>
0021 #include <unordered_map>
0022 #include <vector>
0023 
0024 class AbstractTask;
0025 
0026 enum class TaskManagerStatus { NoJob, Pending, Running, Finished, Canceled };
0027 Q_DECLARE_METATYPE(TaskManagerStatus)
0028 
0029 /** @class TaskManager
0030     @brief This class is responsible for clip jobs management.
0031  */
0032 class TaskManager : public QObject
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     explicit TaskManager(QObject *parent);
0038     ~TaskManager() override;
0039 
0040     /** @brief Discard specific job type for a clip.
0041      *  @param owner the owner item for this task
0042      *  @param type The type of job that you want to abort, leave to NOJOBTYPE to abort all jobs
0043      */
0044     void discardJobs(const ObjectId &owner, AbstractTask::JOBTYPE type = AbstractTask::NOJOBTYPE, bool softDelete = false, const QVector<AbstractTask::JOBTYPE> exceptions = {});
0045 
0046     /** @brief Check if there is a pending / running job a clip.
0047      *  @param owner the owner item for this task
0048      *  @param type The type of job that you want to query
0049      */
0050     bool hasPendingJob(const ObjectId &owner, AbstractTask::JOBTYPE type = AbstractTask::NOJOBTYPE) const;
0051     
0052     TaskManagerStatus jobStatus(const ObjectId &owner) const;
0053 
0054     /** @brief return the progress of a given job on a given clip */
0055     int getJobProgressForClip(const ObjectId &owner) const;
0056     
0057     /** @brief Add a task in the list and push it on the thread pool */
0058     void startTask(int ownerId, AbstractTask *task);
0059 
0060     /** @brief Remove a finished task */
0061     void taskDone(int cid, AbstractTask *task);
0062     
0063     /** @brief Update the number of concurrent jobs allowed */
0064     void updateConcurrency();
0065 
0066     /** @brief We are aborting all tasks and don't want them to send any updates */
0067     bool isBlocked() const;
0068 
0069     /** @brief return the message of a given job on a given clip (message, detailed log)*/
0070     //QPair<QString, QString> getJobMessageForClip(int jobId, const QString &binId) const;
0071 
0072 public slots:
0073     /** @brief Discard all running jobs. */
0074     void slotCancelJobs(const QVector<AbstractTask::JOBTYPE> exceptions = {});
0075 
0076 private slots:
0077     /** @brief Update number of running jobs. */
0078     void updateJobCount();
0079 
0080 private:
0081     QThreadPool m_taskPool;
0082     QThreadPool m_transcodePool;
0083     std::unordered_map<int, std::vector<AbstractTask*> > m_taskList;
0084     mutable QReadWriteLock m_tasksListLock;
0085     bool m_blockUpdates;
0086 
0087 signals:
0088     void jobCount(int);
0089 };