2010-05-03 20:52:35 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-03 20:52:35 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TRANSCODER_H
|
|
|
|
#define TRANSCODER_H
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2010-05-06 18:28:19 +02:00
|
|
|
#include <QEvent>
|
2010-05-03 20:52:35 +02:00
|
|
|
#include <QMetaType>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <QObject>
|
|
|
|
#include <QStringList>
|
2021-05-16 14:03:36 +02:00
|
|
|
#include <QUrl>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <memory>
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-08-29 16:25:33 +02:00
|
|
|
#include "core/song.h"
|
2021-01-07 07:50:08 +01:00
|
|
|
#include "engines/gstpipelinebase.h"
|
2010-08-29 16:25:33 +02:00
|
|
|
|
2021-01-31 07:05:21 +01:00
|
|
|
struct SuitableElement;
|
|
|
|
|
2010-08-22 02:27:14 +02:00
|
|
|
struct TranscoderPreset {
|
2010-08-29 17:32:36 +02:00
|
|
|
TranscoderPreset() : type_(Song::Type_Unknown) {}
|
2014-02-07 16:34:20 +01:00
|
|
|
TranscoderPreset(Song::FileType type, const QString& name,
|
|
|
|
const QString& extension, const QString& codec_mimetype,
|
2010-08-22 02:27:14 +02:00
|
|
|
const QString& muxer_mimetype_ = QString());
|
|
|
|
|
2010-08-29 17:32:36 +02:00
|
|
|
Song::FileType type_;
|
2010-08-22 02:27:14 +02:00
|
|
|
QString name_;
|
|
|
|
QString extension_;
|
|
|
|
QString codec_mimetype_;
|
|
|
|
QString muxer_mimetype_;
|
2010-05-03 20:52:35 +02:00
|
|
|
};
|
2019-11-10 00:30:18 +01:00
|
|
|
Q_DECLARE_METATYPE(TranscoderPreset)
|
2010-05-03 20:52:35 +02:00
|
|
|
|
|
|
|
class Transcoder : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2021-02-05 08:31:24 +01:00
|
|
|
enum CodecType {
|
|
|
|
Codec_None = 0,
|
|
|
|
Codec_Flac = 1,
|
|
|
|
Codec_Mp4 = 2,
|
|
|
|
Codec_Mp3 = 3,
|
|
|
|
Codec_Vorbis = 4,
|
|
|
|
Codec_Speex = 5,
|
|
|
|
Codec_Opus = 6,
|
|
|
|
Codec_Wma = 7
|
|
|
|
};
|
|
|
|
|
2014-11-13 22:31:49 +01:00
|
|
|
Transcoder(QObject* parent = nullptr, const QString& settings_postfix = "");
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-08-29 16:25:33 +02:00
|
|
|
static TranscoderPreset PresetForFileType(Song::FileType type);
|
2010-08-29 17:32:36 +02:00
|
|
|
static QList<TranscoderPreset> GetAllPresets();
|
2021-02-05 08:31:24 +01:00
|
|
|
static QString MimeType(CodecType type);
|
2010-08-29 21:22:21 +02:00
|
|
|
static Song::FileType PickBestFormat(QList<Song::FileType> supported);
|
2010-08-29 16:25:33 +02:00
|
|
|
|
2010-05-06 18:28:19 +02:00
|
|
|
int max_threads() const { return max_threads_; }
|
|
|
|
void set_max_threads(int count) { max_threads_ = count; }
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2021-05-16 14:03:36 +02:00
|
|
|
void AddJob(const QUrl& input, const TranscoderPreset& preset,
|
2021-08-22 13:35:35 +02:00
|
|
|
const QString& output = QString(),
|
|
|
|
bool overwrite_existing = false);
|
2021-05-16 14:03:36 +02:00
|
|
|
void AddTemporaryJob(const QUrl& input, const TranscoderPreset& preset);
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2021-05-16 14:03:36 +02:00
|
|
|
QMap<QUrl, float> GetProgress() const;
|
2010-08-30 13:36:40 +02:00
|
|
|
int QueuedJobsCount() const { return queued_jobs_.count(); }
|
|
|
|
|
2021-01-24 09:18:03 +01:00
|
|
|
GstPipelineModel* model() { return model_; }
|
|
|
|
|
2010-05-03 20:52:35 +02:00
|
|
|
void Start();
|
2010-05-06 18:28:19 +02:00
|
|
|
void Cancel();
|
2021-01-27 07:41:56 +01:00
|
|
|
void DumpGraph(int id);
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2021-01-31 09:10:17 +01:00
|
|
|
static QString GetEncoderFactoryForMimeType(const QString& mime_type);
|
2020-09-18 16:15:19 +02:00
|
|
|
signals:
|
2021-05-16 14:03:36 +02:00
|
|
|
void JobComplete(const QUrl& input, const QString& output, bool success);
|
2010-05-08 17:36:12 +02:00
|
|
|
void LogLine(const QString& message);
|
2010-05-03 20:52:35 +02:00
|
|
|
void AllJobsComplete();
|
|
|
|
|
2010-05-06 18:28:19 +02:00
|
|
|
protected:
|
|
|
|
bool event(QEvent* e);
|
|
|
|
|
2010-05-03 20:52:35 +02:00
|
|
|
private:
|
2010-05-03 21:56:21 +02:00
|
|
|
// The description of a file to transcode - lives in the main thread.
|
2010-05-03 20:52:35 +02:00
|
|
|
struct Job {
|
2021-05-16 14:03:36 +02:00
|
|
|
QUrl input;
|
2010-05-03 20:52:35 +02:00
|
|
|
QString output;
|
2010-08-22 02:27:14 +02:00
|
|
|
TranscoderPreset preset;
|
2010-05-03 20:52:35 +02:00
|
|
|
};
|
|
|
|
|
2010-05-03 21:56:21 +02:00
|
|
|
// State held by a job and shared across gstreamer callbacks - lives in the
|
|
|
|
// job's thread.
|
2021-01-07 07:50:08 +01:00
|
|
|
class JobState : public GstPipelineBase {
|
|
|
|
public:
|
2010-05-06 18:28:19 +02:00
|
|
|
JobState(const Job& job, Transcoder* parent)
|
2021-01-13 07:49:02 +01:00
|
|
|
: GstPipelineBase("transcode"),
|
|
|
|
job_(job),
|
|
|
|
parent_(parent),
|
|
|
|
convert_element_(nullptr) {}
|
2010-05-06 18:28:19 +02:00
|
|
|
|
|
|
|
void PostFinished(bool success);
|
2010-05-08 17:36:12 +02:00
|
|
|
void ReportError(GstMessage* msg);
|
2010-05-06 18:28:19 +02:00
|
|
|
|
2021-01-07 07:50:08 +01:00
|
|
|
GstElement* Pipeline() { return pipeline_; }
|
2021-01-24 09:18:03 +01:00
|
|
|
QString GetDisplayName();
|
2021-01-07 07:50:08 +01:00
|
|
|
|
2010-05-06 18:28:19 +02:00
|
|
|
Job job_;
|
|
|
|
Transcoder* parent_;
|
|
|
|
GstElement* convert_element_;
|
2010-05-03 21:56:21 +02:00
|
|
|
};
|
|
|
|
|
2010-05-06 18:28:19 +02:00
|
|
|
// Event passed from a GStreamer callback to the Transcoder when a job
|
|
|
|
// finishes.
|
|
|
|
struct JobFinishedEvent : public QEvent {
|
|
|
|
JobFinishedEvent(JobState* state, bool success);
|
|
|
|
|
|
|
|
static int sEventType;
|
|
|
|
|
|
|
|
JobState* state_;
|
|
|
|
bool success_;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum StartJobStatus {
|
|
|
|
StartedSuccessfully,
|
|
|
|
FailedToStart,
|
|
|
|
NoMoreJobs,
|
|
|
|
AllThreadsBusy,
|
|
|
|
};
|
|
|
|
|
|
|
|
StartJobStatus MaybeStartNextJob();
|
|
|
|
bool StartJob(const Job& job);
|
2010-05-03 21:56:21 +02:00
|
|
|
|
2014-05-12 23:57:11 +02:00
|
|
|
GstElement* CreateElement(const QString& factory_name,
|
|
|
|
GstElement* bin = nullptr,
|
2010-05-08 17:36:12 +02:00
|
|
|
const QString& name = QString());
|
2010-08-22 02:27:14 +02:00
|
|
|
GstElement* CreateElementForMimeType(const QString& element_type,
|
|
|
|
const QString& mime_type,
|
2014-02-21 17:24:49 +01:00
|
|
|
GstElement* bin = nullptr);
|
2011-04-17 01:04:15 +02:00
|
|
|
void SetElementProperties(const QString& name, GObject* element);
|
2010-05-08 17:36:12 +02:00
|
|
|
|
2021-01-31 07:05:21 +01:00
|
|
|
static SuitableElement FindBestElementForMimeType(const QString& element_type,
|
|
|
|
const QString& mime_type);
|
|
|
|
|
2014-11-13 22:31:49 +01:00
|
|
|
static void NewPadCallback(GstElement*, GstPad* pad, gpointer data);
|
2014-02-07 16:34:20 +01:00
|
|
|
static GstBusSyncReply BusCallbackSync(GstBus*, GstMessage* msg,
|
|
|
|
gpointer data);
|
2010-05-03 20:52:35 +02:00
|
|
|
|
|
|
|
private:
|
2014-05-12 23:57:11 +02:00
|
|
|
typedef QList<std::shared_ptr<JobState>> JobStateList;
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-05-06 18:28:19 +02:00
|
|
|
int max_threads_;
|
|
|
|
QList<Job> queued_jobs_;
|
|
|
|
JobStateList current_jobs_;
|
2014-11-13 22:31:49 +01:00
|
|
|
QString settings_postfix_;
|
2021-01-24 09:18:03 +01:00
|
|
|
GstPipelineModel* model_;
|
2010-05-03 20:52:35 +02:00
|
|
|
};
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // TRANSCODER_H
|