2012-05-25 18:18:07 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "moodbarpipeline.h"
|
|
|
|
|
2012-05-27 21:05:01 +02:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QThread>
|
2012-05-29 00:18:36 +02:00
|
|
|
#include <QUrl>
|
2012-05-27 21:05:01 +02:00
|
|
|
|
2012-06-08 14:27:38 +02:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/signalchecker.h"
|
2014-09-21 11:35:21 +02:00
|
|
|
#include "core/timeconstants.h"
|
2013-05-04 14:48:11 +02:00
|
|
|
#include "core/utilities.h"
|
2014-09-21 11:35:21 +02:00
|
|
|
#include "moodbar/moodbarbuilder.h"
|
|
|
|
|
|
|
|
#include "gst/moodbar/gstfastspectrum.h"
|
2012-06-08 14:27:38 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
bool MoodbarPipeline::sIsAvailable = false;
|
2014-09-21 11:35:21 +02:00
|
|
|
const int MoodbarPipeline::kBands = 128;
|
2012-05-25 18:18:07 +02:00
|
|
|
|
2012-05-29 00:33:37 +02:00
|
|
|
MoodbarPipeline::MoodbarPipeline(const QUrl& local_filename)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(nullptr),
|
|
|
|
local_filename_(local_filename),
|
|
|
|
pipeline_(nullptr),
|
|
|
|
convert_element_(nullptr),
|
|
|
|
success_(false) {}
|
2012-05-25 18:18:07 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
MoodbarPipeline::~MoodbarPipeline() { Cleanup(); }
|
2012-05-25 18:18:07 +02:00
|
|
|
|
|
|
|
bool MoodbarPipeline::IsAvailable() {
|
|
|
|
if (!sIsAvailable) {
|
|
|
|
GstElementFactory* factory = gst_element_factory_find("fftwspectrum");
|
|
|
|
if (!factory) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gst_object_unref(factory);
|
2012-05-29 00:18:36 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
sIsAvailable = true;
|
|
|
|
}
|
2012-05-29 00:18:36 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
return sIsAvailable;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstElement* MoodbarPipeline::CreateElement(const QString& factory_name) {
|
2014-02-07 16:34:20 +01:00
|
|
|
GstElement* ret =
|
|
|
|
gst_element_factory_make(factory_name.toAscii().constData(), nullptr);
|
2012-05-29 00:18:36 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
if (ret) {
|
|
|
|
gst_bin_add(GST_BIN(pipeline_), ret);
|
|
|
|
} else {
|
|
|
|
qLog(Warning) << "Unable to create gstreamer element" << factory_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-27 21:05:01 +02:00
|
|
|
void MoodbarPipeline::Start() {
|
|
|
|
Q_ASSERT(QThread::currentThread() != qApp->thread());
|
|
|
|
|
2013-05-04 14:48:11 +02:00
|
|
|
Utilities::SetThreadIOPriority(Utilities::IOPRIO_CLASS_IDLE);
|
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
if (pipeline_) {
|
2012-05-27 21:05:01 +02:00
|
|
|
return;
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|
2012-05-29 00:18:36 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
pipeline_ = gst_pipeline_new("moodbar-pipeline");
|
2012-05-29 00:18:36 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GstElement* decodebin = CreateElement("uridecodebin");
|
|
|
|
convert_element_ = CreateElement("audioconvert");
|
2014-09-21 11:35:21 +02:00
|
|
|
GstElement* spectrum = CreateElement("fastspectrum");
|
|
|
|
GstElement* fakesink = CreateElement("fakesink");
|
2012-05-29 00:18:36 +02:00
|
|
|
|
2014-09-21 11:35:21 +02:00
|
|
|
if (!decodebin || !convert_element_ || !spectrum || !fakesink) {
|
2014-02-06 16:49:49 +01:00
|
|
|
pipeline_ = nullptr;
|
2012-05-27 19:53:57 +02:00
|
|
|
emit Finished(false);
|
2012-05-27 21:05:01 +02:00
|
|
|
return;
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|
2012-05-27 17:46:16 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// Join them together
|
2014-09-21 11:35:21 +02:00
|
|
|
if (!gst_element_link(convert_element_, spectrum) ||
|
|
|
|
!gst_element_link(spectrum, fakesink)) {
|
|
|
|
qLog(Error) << "Failed to link elements";
|
|
|
|
pipeline_ = nullptr;
|
|
|
|
emit Finished(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
builder_.reset(new MoodbarBuilder);
|
2012-05-29 00:18:36 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// Set properties
|
2014-10-15 21:57:57 +02:00
|
|
|
g_object_set(decodebin, "uri", local_filename_.toEncoded().constData(),
|
2014-02-07 16:34:20 +01:00
|
|
|
nullptr);
|
2014-10-15 21:57:57 +02:00
|
|
|
g_object_set(spectrum, "bands", kBands, nullptr);
|
2014-09-21 11:35:21 +02:00
|
|
|
|
|
|
|
GstFastSpectrum* fast_spectrum = GST_FASTSPECTRUM(spectrum);
|
2014-10-15 21:57:57 +02:00
|
|
|
fast_spectrum->output_callback = [this](
|
|
|
|
double* magnitudes, int size) { builder_->AddFrame(magnitudes, size); };
|
2012-05-29 00:18:36 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// Connect signals
|
2012-06-08 15:13:23 +02:00
|
|
|
CHECKED_GCONNECT(decodebin, "pad-added", &NewPadCallback, this);
|
2014-09-21 11:35:21 +02:00
|
|
|
GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline_));
|
|
|
|
gst_bus_set_sync_handler(bus, BusCallbackSync, this, nullptr);
|
|
|
|
gst_object_unref(bus);
|
2012-05-29 00:18:36 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// Start playing
|
|
|
|
gst_element_set_state(pipeline_, GST_STATE_PLAYING);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoodbarPipeline::ReportError(GstMessage* msg) {
|
|
|
|
GError* error;
|
|
|
|
gchar* debugs;
|
|
|
|
|
|
|
|
gst_message_parse_error(msg, &error, &debugs);
|
|
|
|
QString message = QString::fromLocal8Bit(error->message);
|
|
|
|
|
|
|
|
g_error_free(error);
|
|
|
|
free(debugs);
|
|
|
|
|
|
|
|
qLog(Error) << "Error processing" << local_filename_ << ":" << message;
|
|
|
|
}
|
|
|
|
|
2012-05-29 00:18:36 +02:00
|
|
|
void MoodbarPipeline::NewPadCallback(GstElement*, GstPad* pad, gpointer data) {
|
2012-05-25 18:18:07 +02:00
|
|
|
MoodbarPipeline* self = reinterpret_cast<MoodbarPipeline*>(data);
|
2014-02-07 16:34:20 +01:00
|
|
|
GstPad* const audiopad =
|
|
|
|
gst_element_get_static_pad(self->convert_element_, "sink");
|
2012-05-25 18:18:07 +02:00
|
|
|
|
|
|
|
if (GST_PAD_IS_LINKED(audiopad)) {
|
|
|
|
qLog(Warning) << "audiopad is already linked, unlinking old pad";
|
|
|
|
gst_pad_unlink(audiopad, GST_PAD_PEER(audiopad));
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_pad_link(pad, audiopad);
|
|
|
|
gst_object_unref(audiopad);
|
|
|
|
|
2014-09-21 11:35:21 +02:00
|
|
|
int rate = 0;
|
|
|
|
GstCaps* caps = gst_pad_get_current_caps(pad);
|
|
|
|
GstStructure* structure = gst_caps_get_structure(caps, 0);
|
|
|
|
gst_structure_get_int(structure, "rate", &rate);
|
|
|
|
gst_caps_unref(caps);
|
2013-01-29 13:19:26 +01:00
|
|
|
|
2014-09-21 11:35:21 +02:00
|
|
|
self->builder_->Init(kBands, rate);
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GstBusSyncReply MoodbarPipeline::BusCallbackSync(GstBus*, GstMessage* msg,
|
|
|
|
gpointer data) {
|
2012-05-25 18:18:07 +02:00
|
|
|
MoodbarPipeline* self = reinterpret_cast<MoodbarPipeline*>(data);
|
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE(msg)) {
|
|
|
|
case GST_MESSAGE_EOS:
|
|
|
|
self->Stop(true);
|
|
|
|
break;
|
2013-01-29 13:19:26 +01:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
case GST_MESSAGE_ERROR:
|
|
|
|
self->ReportError(msg);
|
|
|
|
self->Stop(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return GST_BUS_PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoodbarPipeline::Stop(bool success) {
|
|
|
|
success_ = success;
|
2014-10-06 15:40:10 +02:00
|
|
|
if (builder_ != nullptr) {
|
|
|
|
data_ = builder_->Finish(1000);
|
|
|
|
builder_.reset();
|
|
|
|
}
|
2014-09-21 11:35:21 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
emit Finished(success);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoodbarPipeline::Cleanup() {
|
2012-05-27 22:33:17 +02:00
|
|
|
Q_ASSERT(QThread::currentThread() == thread());
|
|
|
|
Q_ASSERT(QThread::currentThread() != qApp->thread());
|
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
if (pipeline_) {
|
2014-12-22 22:13:44 +01:00
|
|
|
GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline_));
|
|
|
|
gst_bus_set_sync_handler(bus, nullptr, nullptr, nullptr);
|
|
|
|
gst_object_unref(bus);
|
|
|
|
|
2012-05-27 17:46:16 +02:00
|
|
|
gst_element_set_state(pipeline_, GST_STATE_NULL);
|
2012-05-25 18:18:07 +02:00
|
|
|
gst_object_unref(pipeline_);
|
2014-02-06 16:49:49 +01:00
|
|
|
pipeline_ = nullptr;
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|
|
|
|
}
|