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
|
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2010-05-03 20:52:35 +02:00
|
|
|
#include <gst/gst.h>
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QStringList>
|
2010-05-06 18:28:19 +02:00
|
|
|
#include <QEvent>
|
2010-05-03 20:52:35 +02:00
|
|
|
#include <QMetaType>
|
|
|
|
|
2010-08-29 16:25:33 +02:00
|
|
|
#include "core/song.h"
|
|
|
|
|
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
|
|
|
};
|
2010-08-22 02:27:14 +02:00
|
|
|
Q_DECLARE_METATYPE(TranscoderPreset);
|
2010-05-03 20:52:35 +02:00
|
|
|
|
|
|
|
class Transcoder : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
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();
|
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
|
|
|
|
2010-08-22 02:27:14 +02:00
|
|
|
void AddJob(const QString& input, const TranscoderPreset& preset,
|
2010-05-03 20:52:35 +02:00
|
|
|
const QString& output = QString());
|
2014-11-13 22:31:49 +01:00
|
|
|
void AddTemporaryJob(const QString& input, const TranscoderPreset& preset);
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-08-30 13:36:40 +02:00
|
|
|
QMap<QString, float> GetProgress() const;
|
|
|
|
int QueuedJobsCount() const { return queued_jobs_.count(); }
|
|
|
|
|
2010-05-03 20:52:35 +02:00
|
|
|
public slots:
|
|
|
|
void Start();
|
2010-05-06 18:28:19 +02:00
|
|
|
void Cancel();
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
signals:
|
2014-11-13 22:31:49 +01:00
|
|
|
void JobComplete(const QString& 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 {
|
|
|
|
QString input;
|
|
|
|
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.
|
|
|
|
struct JobState {
|
2010-05-06 18:28:19 +02:00
|
|
|
JobState(const Job& job, Transcoder* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: job_(job),
|
|
|
|
parent_(parent),
|
2014-02-21 17:24:49 +01:00
|
|
|
pipeline_(nullptr),
|
2014-11-14 23:50:59 +01:00
|
|
|
convert_element_(nullptr) {}
|
2011-11-29 17:57:11 +01:00
|
|
|
~JobState();
|
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
|
|
|
|
|
|
|
Job job_;
|
|
|
|
Transcoder* parent_;
|
2011-11-29 17:57:11 +01:00
|
|
|
GstElement* pipeline_;
|
2010-05-06 18:28:19 +02:00
|
|
|
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
|
|
|
|
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_;
|
2010-05-03 20:52:35 +02:00
|
|
|
};
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // TRANSCODER_H
|