File indexing completed on 2024-12-01 04:28:35

0001 /*
0002     SPDX-FileCopyrightText: 2013-2021 Meltytech LLC
0003     SPDX-FileCopyrightText: 2021 Jean-Baptiste Mardelle <jb@kdenlive.org>
0004 
0005     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "abstracttask.h"
0009 #include "audio/audioStreamInfo.h"
0010 #include "bin/projectclip.h"
0011 #include "bin/projectitemmodel.h"
0012 #include "core.h"
0013 #include "kdenlivesettings.h"
0014 
0015 #ifdef Q_OS_UNIX
0016 // on Unix systems we can use setpriority() to make proxy-rendering tasks lower
0017 // priority
0018 #include "sys/resource.h"
0019 #endif
0020 
0021 #ifdef Q_OS_WIN
0022 // on Windows we can use SetPriorityClass() instead
0023 #include <windows.h>
0024 #endif
0025 
0026 #include <QElapsedTimer>
0027 #include <QFile>
0028 #include <QImage>
0029 #include <QList>
0030 #include <QProcess>
0031 #include <QRgb>
0032 #include <QString>
0033 #include <QThreadPool>
0034 #include <QTime>
0035 #include <QVariantList>
0036 
0037 AbstractTask::AbstractTask(const ObjectId &owner, JOBTYPE type, QObject *object)
0038     : QRunnable()
0039     , m_owner(owner)
0040     , m_object(object)
0041     , m_progress(0)
0042     , m_isCanceled(false)
0043     , m_softDelete(false)
0044     , m_isForce(false)
0045     , m_running(false)
0046     , m_type(type)
0047 {
0048     setAutoDelete(false);
0049     m_uuid = QUuid::createUuid();
0050     switch (type) {
0051     case AbstractTask::LOADJOB:
0052         m_priority = 10;
0053         break;
0054     case AbstractTask::TRANSCODEJOB:
0055     case AbstractTask::PROXYJOB:
0056         m_priority = 8;
0057         break;
0058     case AbstractTask::FILTERCLIPJOB:
0059     case AbstractTask::STABILIZEJOB:
0060     case AbstractTask::ANALYSECLIPJOB:
0061     case AbstractTask::SPEEDJOB:
0062         m_priority = 5;
0063         break;
0064     default:
0065         m_priority = 5;
0066         break;
0067     }
0068 }
0069 
0070 bool AbstractTask::isCanceled() const
0071 {
0072     return m_isCanceled;
0073 }
0074 
0075 void AbstractTask::cancelJob(bool softDelete)
0076 {
0077     if (m_isCanceled.testAndSetAcquire(0, 1)) {
0078         if (softDelete) {
0079             m_softDelete.testAndSetAcquire(0, 1);
0080         }
0081         qDebug() << "====== SETTING TASK CANCELED: " << m_isCanceled << ", TYPE: " << m_type;
0082         Q_EMIT jobCanceled();
0083     }
0084 }
0085 
0086 const ObjectId AbstractTask::ownerId() const
0087 {
0088     return m_owner;
0089 }
0090 
0091 AbstractTask::~AbstractTask() {}
0092 
0093 bool AbstractTask::operator==(const AbstractTask &b)
0094 {
0095     return m_owner == b.ownerId();
0096 }
0097 
0098 void AbstractTask::run()
0099 {
0100     qDebug() << "============0\n\nABSTRACT TASKSTARTRING\n\n==================";
0101 }
0102 
0103 // Background tasks should not slow down the main UI too much. Unless the user
0104 // has opted out, lower the priority of proxy and transcode tasks.
0105 void AbstractTask::setPreferredPriority(qint64 pid)
0106 {
0107     if (!KdenliveSettings::nice_tasks()) {
0108         qDebug() << "Not changing process priority for PID" << pid;
0109         return;
0110     }
0111 #ifdef Q_OS_UNIX
0112     qDebug() << "Lowering task priority for PID" << pid;
0113     int err = setpriority(PRIO_PROCESS, pid, 10);
0114     if (err != 0) {
0115         qWarning() << "Failed to lower task priority for PID" << pid << " - error" << strerror(errno);
0116     }
0117 #endif
0118 #ifdef Q_OS_WIN
0119     HANDLE processHandle = OpenProcess(PROCESS_SET_INFORMATION, FALSE, pid);
0120     if (processHandle) {
0121         qDebug() << "Lowering priority for PID" << pid;
0122         SetPriorityClass(processHandle, BELOW_NORMAL_PRIORITY_CLASS);
0123         CloseHandle(processHandle);
0124     } else {
0125         qWarning() << "Could not get HANDLE for PID" << pid;
0126     }
0127 #endif
0128 }
0129 
0130 AbstractTaskDone::~AbstractTaskDone() {
0131     pCore->taskManager.taskDone(m_cid, m_task);
0132 }