2010-06-15 15:24:17 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-06-15 15:24:17 +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/>.
|
|
|
|
*/
|
|
|
|
|
2011-08-10 17:23:32 +02:00
|
|
|
#include "config.h"
|
2010-06-15 15:24:17 +02:00
|
|
|
#include "songloader.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2010-06-15 15:24:17 +02:00
|
|
|
#include "core/song.h"
|
2010-08-31 21:45:33 +02:00
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "library/sqlrow.h"
|
2010-06-15 15:24:17 +02:00
|
|
|
#include "playlistparsers/parserbase.h"
|
2011-01-04 00:36:10 +01:00
|
|
|
#include "playlistparsers/cueparser.h"
|
2010-06-15 15:24:17 +02:00
|
|
|
#include "playlistparsers/playlistparser.h"
|
2011-07-15 15:27:50 +02:00
|
|
|
#include "internet/fixlastfm.h"
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QDirIterator>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QTimer>
|
2011-06-10 01:08:43 +02:00
|
|
|
#include <QUrl>
|
2010-06-15 16:24:17 +02:00
|
|
|
#include <QtConcurrentRun>
|
2010-06-15 15:24:17 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2011-08-10 17:23:32 +02:00
|
|
|
#ifdef HAVE_AUDIOCD
|
|
|
|
# include <gst/cdda/gstcddabasesrc.h>
|
|
|
|
#endif
|
|
|
|
|
2011-06-10 01:08:43 +02:00
|
|
|
|
2010-06-23 13:47:54 +02:00
|
|
|
QSet<QString> SongLoader::sRawUriSchemes;
|
2010-06-23 13:51:13 +02:00
|
|
|
const int SongLoader::kDefaultTimeout = 5000;
|
2010-06-23 13:47:54 +02:00
|
|
|
|
2010-08-31 23:24:57 +02:00
|
|
|
SongLoader::SongLoader(LibraryBackendInterface* library, QObject *parent)
|
2010-06-15 15:24:17 +02:00
|
|
|
: QObject(parent),
|
|
|
|
timeout_timer_(new QTimer(this)),
|
2010-12-11 11:35:07 +01:00
|
|
|
playlist_parser_(new PlaylistParser(library, this)),
|
2011-01-04 00:36:10 +01:00
|
|
|
cue_parser_(new CueParser(library, this)),
|
2010-06-23 13:51:13 +02:00
|
|
|
timeout_(kDefaultTimeout),
|
2010-06-15 15:24:17 +02:00
|
|
|
state_(WaitingForType),
|
|
|
|
success_(false),
|
2010-08-31 21:45:33 +02:00
|
|
|
parser_(NULL),
|
|
|
|
library_(library)
|
2010-06-15 15:24:17 +02:00
|
|
|
{
|
2010-06-23 13:47:54 +02:00
|
|
|
if (sRawUriSchemes.isEmpty()) {
|
|
|
|
sRawUriSchemes << "udp" << "mms" << "mmsh" << "mmst" << "mmsu" << "rtsp"
|
|
|
|
<< "rtspu" << "rtspt" << "rtsph";
|
|
|
|
}
|
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
timeout_timer_->setSingleShot(true);
|
2010-06-26 15:20:08 +02:00
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
connect(timeout_timer_, SIGNAL(timeout()), SLOT(Timeout()));
|
|
|
|
}
|
|
|
|
|
2010-06-15 20:24:08 +02:00
|
|
|
SongLoader::~SongLoader() {
|
|
|
|
if (pipeline_) {
|
|
|
|
state_ = Finished;
|
|
|
|
gst_element_set_state(pipeline_.get(), GST_STATE_NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 13:51:13 +02:00
|
|
|
SongLoader::Result SongLoader::Load(const QUrl& url) {
|
2010-06-15 15:24:17 +02:00
|
|
|
url_ = url;
|
|
|
|
|
|
|
|
if (url_.scheme() == "file") {
|
2010-08-31 21:45:33 +02:00
|
|
|
return LoadLocal(url_.toLocalFile());
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
2010-06-23 13:47:54 +02:00
|
|
|
if (sRawUriSchemes.contains(url_.scheme())) {
|
|
|
|
// The URI scheme indicates that it can't possibly be a playlist, so add
|
|
|
|
// it as a raw stream.
|
|
|
|
AddAsRawStream();
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2010-06-23 13:51:13 +02:00
|
|
|
timeout_timer_->start(timeout_);
|
2010-06-15 15:24:17 +02:00
|
|
|
return LoadRemote();
|
|
|
|
}
|
|
|
|
|
2011-04-16 16:04:15 +02:00
|
|
|
SongLoader::Result SongLoader::LoadLocalPartial(const QString& filename) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Fast Loading local file" << filename;
|
2011-04-16 16:04:15 +02:00
|
|
|
// First check to see if it's a directory - if so we can load all the songs
|
|
|
|
// inside right away.
|
|
|
|
if (QFileInfo(filename).isDir()) {
|
|
|
|
LoadLocalDirectory(filename);
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
Song song;
|
|
|
|
song.InitFromFilePartial(filename);
|
|
|
|
if (song.is_valid())
|
|
|
|
songs_ << song;
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2011-06-10 01:08:43 +02:00
|
|
|
SongLoader::Result SongLoader::LoadAudioCD() {
|
2011-08-10 17:23:32 +02:00
|
|
|
#ifdef HAVE_AUDIOCD
|
2011-06-10 01:08:43 +02:00
|
|
|
// Create gstreamer cdda element
|
2011-06-15 21:28:25 +02:00
|
|
|
GstElement* cdda = gst_element_make_from_uri (GST_URI_SRC, "cdda://", NULL);
|
2011-06-10 01:08:43 +02:00
|
|
|
if (cdda == NULL) {
|
|
|
|
qLog(Error) << "Error while creating CDDA GstElement";
|
2011-06-15 01:38:43 +02:00
|
|
|
return Error;
|
2011-06-10 01:08:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change the element's state to ready and paused, to be able to query it
|
2011-06-15 21:28:25 +02:00
|
|
|
if (gst_element_set_state(cdda, GST_STATE_READY) == GST_STATE_CHANGE_FAILURE ||
|
|
|
|
gst_element_set_state(cdda, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
|
2011-06-10 01:08:43 +02:00
|
|
|
qLog(Error) << "Error while changing CDDA GstElement's state";
|
2011-06-15 21:28:25 +02:00
|
|
|
gst_element_set_state(cdda, GST_STATE_NULL);
|
2011-06-15 01:38:43 +02:00
|
|
|
gst_object_unref(GST_OBJECT(cdda));
|
|
|
|
return Error;
|
2011-06-10 01:08:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get number of tracks
|
|
|
|
GstFormat fmt = gst_format_get_by_nick ("track");
|
|
|
|
GstFormat out_fmt = fmt;
|
2011-06-15 01:38:43 +02:00
|
|
|
gint64 num_tracks = 0;
|
2011-06-10 01:08:43 +02:00
|
|
|
if (!gst_element_query_duration (cdda, &out_fmt, &num_tracks) || out_fmt != fmt) {
|
|
|
|
qLog(Error) << "Error while querying cdda GstElement";
|
2011-06-15 01:38:43 +02:00
|
|
|
gst_object_unref(GST_OBJECT(cdda));
|
|
|
|
return Error;
|
2011-06-10 01:08:43 +02:00
|
|
|
}
|
|
|
|
|
2011-06-15 01:38:43 +02:00
|
|
|
for (int track_number = 1; track_number <= num_tracks; track_number++) {
|
2011-06-10 01:08:43 +02:00
|
|
|
// Init song
|
|
|
|
Song song;
|
2011-06-15 01:38:43 +02:00
|
|
|
guint64 duration = 0;
|
2011-06-10 11:19:30 +02:00
|
|
|
// quint64 == ulonglong and guint64 == ulong, therefore we must cast
|
2011-06-15 01:38:43 +02:00
|
|
|
if (gst_tag_list_get_uint64 (GST_CDDA_BASE_SRC(cdda)->tracks[track_number-1].tags,
|
|
|
|
GST_TAG_DURATION, &duration)) {
|
2011-06-10 11:37:50 +02:00
|
|
|
song.set_length_nanosec((quint64)duration);
|
2011-06-10 01:08:43 +02:00
|
|
|
}
|
|
|
|
song.set_valid(true);
|
|
|
|
song.set_filetype(Song::Type_Cdda);
|
|
|
|
song.set_url(QUrl(QString("cdda://%1").arg(track_number)));
|
|
|
|
song.set_title(QString("Track %1").arg(track_number));
|
|
|
|
song.set_track(track_number);
|
|
|
|
songs_ << song;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate MusicBrainz DiscId
|
|
|
|
gst_tag_register_musicbrainz_tags();
|
|
|
|
GstElement *pipe = gst_pipeline_new ("pipeline");
|
|
|
|
gst_bin_add (GST_BIN (pipe), cdda);
|
|
|
|
gst_element_set_state (pipe, GST_STATE_READY);
|
|
|
|
gst_element_set_state (pipe, GST_STATE_PAUSED);
|
|
|
|
GstMessage *msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe),
|
|
|
|
GST_CLOCK_TIME_NONE,
|
|
|
|
GST_MESSAGE_TAG);
|
|
|
|
GstTagList *tags = NULL;
|
|
|
|
gst_message_parse_tag (msg, &tags);
|
|
|
|
char *string_mb = NULL;
|
|
|
|
if (gst_tag_list_get_string (tags, GST_TAG_CDDA_MUSICBRAINZ_DISCID, &string_mb)) {
|
2011-06-15 01:38:43 +02:00
|
|
|
QString musicbrainz_discid(string_mb);
|
|
|
|
qLog(Info) << "MusicBrainz discid: " << musicbrainz_discid;
|
|
|
|
|
|
|
|
MusicBrainzClient *musicbrainz_client = new MusicBrainzClient(this);
|
|
|
|
connect(musicbrainz_client,
|
|
|
|
SIGNAL(Finished(const QString&, const QString&, MusicBrainzClient::ResultList)),
|
|
|
|
SLOT(AudioCDTagsLoaded(const QString&, const QString&, MusicBrainzClient::ResultList)));
|
|
|
|
musicbrainz_client->StartDiscIdRequest(musicbrainz_discid);
|
|
|
|
g_free(string_mb);
|
2011-06-10 01:08:43 +02:00
|
|
|
}
|
2011-06-15 01:38:43 +02:00
|
|
|
|
|
|
|
// Clean all the Gstreamer objects we have used: we don't need them anymore
|
|
|
|
gst_object_unref(GST_OBJECT(cdda));
|
|
|
|
gst_element_set_state (pipe, GST_STATE_NULL);
|
|
|
|
gst_object_unref(GST_OBJECT(pipe));
|
|
|
|
gst_object_unref(GST_OBJECT(msg));
|
|
|
|
gst_object_unref(GST_OBJECT(tags));
|
2011-06-10 01:08:43 +02:00
|
|
|
|
2011-08-10 17:23:32 +02:00
|
|
|
return Success;
|
|
|
|
#else // HAVE_AUDIOCD
|
|
|
|
return Error;
|
|
|
|
#endif
|
2011-06-10 01:08:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoader::AudioCDTagsLoaded(const QString& artist, const QString& album,
|
2011-06-15 01:38:43 +02:00
|
|
|
const MusicBrainzClient::ResultList& results) {
|
2011-06-10 01:08:43 +02:00
|
|
|
// Remove previously added songs metadata, because there are not needed
|
|
|
|
// and that we are going to fill it with new (more complete) ones
|
|
|
|
songs_.clear();
|
|
|
|
int track_number = 1;
|
|
|
|
foreach (const MusicBrainzClient::Result& ret, results) {
|
|
|
|
Song song;
|
|
|
|
song.set_artist(artist);
|
|
|
|
song.set_album(album);
|
|
|
|
song.set_title(ret.title_);
|
|
|
|
song.set_length_nanosec(ret.duration_msec_ * kNsecPerMsec);
|
|
|
|
song.set_track(track_number);
|
2011-06-15 01:38:43 +02:00
|
|
|
// We need to set url: that's how playlist will find the correct item to update
|
2011-06-10 01:08:43 +02:00
|
|
|
song.set_url(QUrl(QString("cdda://%1").arg(track_number++)));
|
|
|
|
songs_ << song;
|
|
|
|
}
|
|
|
|
emit LoadFinished(true);
|
|
|
|
}
|
|
|
|
|
2010-10-16 14:37:33 +02:00
|
|
|
SongLoader::Result SongLoader::LoadLocal(const QString& filename, bool block,
|
|
|
|
bool ignore_playlists) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Loading local file" << filename;
|
2010-06-15 20:24:08 +02:00
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
// First check to see if it's a directory - if so we can load all the songs
|
|
|
|
// inside right away.
|
|
|
|
if (QFileInfo(filename).isDir()) {
|
2010-08-31 21:45:33 +02:00
|
|
|
if (!block) {
|
2010-09-18 13:50:20 +02:00
|
|
|
QtConcurrent::run(this, &SongLoader::LoadLocalDirectoryAndEmit, filename);
|
2010-08-31 21:45:33 +02:00
|
|
|
return WillLoadAsync;
|
|
|
|
} else {
|
|
|
|
LoadLocalDirectory(filename);
|
|
|
|
return Success;
|
|
|
|
}
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// It's a local file, so check if it looks like a playlist.
|
|
|
|
// Read the first few bytes.
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
return Error;
|
|
|
|
QByteArray data(file.read(PlaylistParser::kMagicSize));
|
|
|
|
|
2011-02-27 13:14:32 +01:00
|
|
|
ParserBase* parser = playlist_parser_->ParserForMagic(data);
|
2010-07-24 16:09:27 +02:00
|
|
|
if (!parser) {
|
|
|
|
// Check the file extension as well, maybe the magic failed, or it was a
|
|
|
|
// basic M3U file which is just a plain list of filenames.
|
|
|
|
parser = playlist_parser_->ParserForExtension(QFileInfo(filename).suffix());
|
|
|
|
}
|
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
if (parser) {
|
2010-10-16 14:37:33 +02:00
|
|
|
if (ignore_playlists) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Skipping" << parser->name() << "playlist while loading directory";
|
2010-10-16 14:37:33 +02:00
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Parsing using" << parser->name();
|
2010-06-15 20:24:08 +02:00
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
// It's a playlist!
|
2010-08-31 21:45:33 +02:00
|
|
|
if (!block) {
|
2010-09-18 13:50:20 +02:00
|
|
|
QtConcurrent::run(this, &SongLoader::LoadPlaylistAndEmit, parser, filename);
|
2010-08-31 21:45:33 +02:00
|
|
|
return WillLoadAsync;
|
|
|
|
} else {
|
|
|
|
LoadPlaylist(parser, filename);
|
|
|
|
return Success;
|
|
|
|
}
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
2010-07-24 16:09:27 +02:00
|
|
|
// Not a playlist, so just assume it's a song
|
2011-08-18 22:43:52 +02:00
|
|
|
QUrl url = QUrl::fromLocalFile(filename);
|
2011-01-04 00:36:10 +01:00
|
|
|
|
2010-08-31 21:45:33 +02:00
|
|
|
LibraryQuery query;
|
|
|
|
query.SetColumnSpec("%songs_table.ROWID, " + Song::kColumnSpec);
|
2011-08-27 23:51:30 +02:00
|
|
|
query.AddWhere("filename", url.toEncoded());
|
2011-01-04 00:36:10 +01:00
|
|
|
|
|
|
|
SongList song_list;
|
|
|
|
|
2010-08-31 21:45:33 +02:00
|
|
|
if (library_->ExecQuery(&query) && query.Next()) {
|
2011-01-04 00:36:10 +01:00
|
|
|
// we may have many results when the file has many sections
|
|
|
|
do {
|
|
|
|
Song song;
|
2011-06-17 22:00:10 +02:00
|
|
|
song.InitFromQuery(query, true);
|
2011-01-04 00:36:10 +01:00
|
|
|
|
|
|
|
song_list << song;
|
|
|
|
} while(query.Next());
|
2010-08-31 21:45:33 +02:00
|
|
|
} else {
|
2011-01-04 00:36:10 +01:00
|
|
|
QString matching_cue = filename.section('.', 0, -2) + ".cue";
|
|
|
|
|
|
|
|
// it's a cue - create virtual tracks
|
|
|
|
if(QFile::exists(matching_cue)) {
|
|
|
|
QFile cue(matching_cue);
|
|
|
|
cue.open(QIODevice::ReadOnly);
|
|
|
|
|
2011-01-12 00:09:59 +01:00
|
|
|
song_list = cue_parser_->Load(&cue, matching_cue, QDir(filename.section('/', 0, -2)));
|
2011-01-04 00:36:10 +01:00
|
|
|
|
|
|
|
// it's a normal media file
|
|
|
|
} else {
|
|
|
|
Song song;
|
|
|
|
song.InitFromFile(filename, -1);
|
|
|
|
|
|
|
|
song_list << song;
|
|
|
|
|
|
|
|
}
|
2010-08-31 21:45:33 +02:00
|
|
|
}
|
2011-04-16 16:04:15 +02:00
|
|
|
foreach (const Song& song, song_list) {
|
2011-01-04 00:36:10 +01:00
|
|
|
if (song.is_valid())
|
|
|
|
songs_ << song;
|
|
|
|
}
|
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2011-04-16 16:04:15 +02:00
|
|
|
void SongLoader::EffectiveSongsLoad() {
|
|
|
|
for (int i = 0; i < songs_.size(); i++) {
|
|
|
|
Song& song = songs_[i];
|
|
|
|
|
2011-08-27 23:29:35 +02:00
|
|
|
if (song.filetype() != Song::Type_Unknown) {
|
|
|
|
// Maybe we loaded the metadata already, for example from a cuesheet.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-04-16 16:04:15 +02:00
|
|
|
LibraryQuery query;
|
|
|
|
query.SetColumnSpec("%songs_table.ROWID, " + Song::kColumnSpec);
|
2011-08-19 22:37:59 +02:00
|
|
|
query.AddWhere("filename", song.url().toEncoded());
|
2011-04-16 16:04:15 +02:00
|
|
|
|
|
|
|
if (library_->ExecQuery(&query) && query.Next()) {
|
|
|
|
// we may have many results when the file has many sections
|
|
|
|
do {
|
2011-06-17 22:00:10 +02:00
|
|
|
song.InitFromQuery(query, true);
|
2011-04-16 16:04:15 +02:00
|
|
|
} while(query.Next());
|
|
|
|
} else {
|
|
|
|
// it's a normal media file
|
2011-08-18 22:43:52 +02:00
|
|
|
QString filename = song.url().toLocalFile();
|
2011-04-16 16:04:15 +02:00
|
|
|
song.InitFromFile(filename, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-18 13:50:20 +02:00
|
|
|
void SongLoader::LoadPlaylistAndEmit(ParserBase* parser, const QString& filename) {
|
|
|
|
LoadPlaylist(parser, filename);
|
|
|
|
emit LoadFinished(true);
|
|
|
|
}
|
|
|
|
|
2010-08-03 16:59:18 +02:00
|
|
|
void SongLoader::LoadPlaylist(ParserBase* parser, const QString& filename) {
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::ReadOnly);
|
2011-02-03 23:21:15 +01:00
|
|
|
songs_ = parser->Load(&file, filename, QFileInfo(filename).path());
|
2010-08-03 16:59:18 +02:00
|
|
|
}
|
|
|
|
|
2010-06-15 20:24:08 +02:00
|
|
|
static bool CompareSongs(const Song& left, const Song& right) {
|
2010-06-15 20:34:15 +02:00
|
|
|
// Order by artist, album, disc, track
|
2010-06-15 20:24:08 +02:00
|
|
|
if (left.artist() < right.artist()) return true;
|
2010-12-18 19:47:44 +01:00
|
|
|
if (left.artist() > right.artist()) return false;
|
2010-06-15 20:24:08 +02:00
|
|
|
if (left.album() < right.album()) return true;
|
2010-12-18 19:47:44 +01:00
|
|
|
if (left.album() > right.album()) return false;
|
2010-06-15 20:24:08 +02:00
|
|
|
if (left.disc() < right.disc()) return true;
|
2010-12-18 19:47:44 +01:00
|
|
|
if (left.disc() > right.disc()) return false;
|
|
|
|
if (left.track() < right.track()) return true;
|
|
|
|
if (left.track() > right.track()) return false;
|
2011-04-28 14:27:53 +02:00
|
|
|
return left.url() < right.url();
|
2010-06-15 20:24:08 +02:00
|
|
|
}
|
|
|
|
|
2010-09-18 13:50:20 +02:00
|
|
|
void SongLoader::LoadLocalDirectoryAndEmit(const QString& filename) {
|
|
|
|
LoadLocalDirectory(filename);
|
|
|
|
emit LoadFinished(true);
|
|
|
|
}
|
|
|
|
|
2010-06-15 16:24:17 +02:00
|
|
|
void SongLoader::LoadLocalDirectory(const QString& filename) {
|
2010-06-15 15:24:17 +02:00
|
|
|
QDirIterator it(filename, QDir::Files | QDir::NoDotAndDotDot | QDir::Readable,
|
|
|
|
QDirIterator::Subdirectories);
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
2011-04-16 16:04:15 +02:00
|
|
|
LoadLocalPartial(it.next());
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
2010-06-15 20:24:08 +02:00
|
|
|
qStableSort(songs_.begin(), songs_.end(), CompareSongs);
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
2010-06-26 15:20:08 +02:00
|
|
|
void SongLoader::AddAsRawStream() {
|
|
|
|
Song song;
|
|
|
|
song.set_valid(true);
|
|
|
|
song.set_filetype(Song::Type_Stream);
|
2011-04-28 14:27:53 +02:00
|
|
|
song.set_url(url_);
|
2010-06-26 15:20:08 +02:00
|
|
|
song.set_title(url_.toString());
|
|
|
|
songs_ << song;
|
|
|
|
}
|
|
|
|
|
2010-06-26 17:09:32 +02:00
|
|
|
void SongLoader::Timeout() {
|
|
|
|
state_ = Finished;
|
|
|
|
success_ = false;
|
|
|
|
StopTypefind();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoader::StopTypefind() {
|
|
|
|
// Destroy the pipeline
|
|
|
|
if (pipeline_) {
|
|
|
|
gst_element_set_state(pipeline_.get(), GST_STATE_NULL);
|
|
|
|
pipeline_.reset();
|
|
|
|
}
|
|
|
|
timeout_timer_->stop();
|
|
|
|
|
|
|
|
if (success_ && parser_) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Parsing" << url_ << "with" << parser_->name();
|
2010-06-26 17:09:32 +02:00
|
|
|
|
|
|
|
// Parse the playlist
|
|
|
|
QBuffer buf(&buffer_);
|
|
|
|
buf.open(QIODevice::ReadOnly);
|
|
|
|
songs_ = parser_->Load(&buf);
|
|
|
|
} else if (success_) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Loading" << url_ << "as raw stream";
|
2010-06-26 17:09:32 +02:00
|
|
|
|
|
|
|
// It wasn't a playlist - just put the URL in as a stream
|
|
|
|
AddAsRawStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
emit LoadFinished(success_);
|
|
|
|
}
|
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
SongLoader::Result SongLoader::LoadRemote() {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Loading remote file" << url_;
|
2010-06-15 20:24:08 +02:00
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
// It's not a local file so we have to fetch it to see what it is. We use
|
|
|
|
// gstreamer to do this since it handles funky URLs for us (http://, ssh://,
|
|
|
|
// etc) and also has typefinder plugins.
|
|
|
|
// First we wait for typefinder to tell us what it is. If it's not text/plain
|
|
|
|
// or text/uri-list assume it's a song and return success.
|
|
|
|
// Otherwise wait to get 512 bytes of data and do magic on it - if the magic
|
2010-06-15 15:28:08 +02:00
|
|
|
// fails then we don't know what it is so return failure.
|
2010-06-15 15:24:17 +02:00
|
|
|
// If the magic succeeds then we know for sure it's a playlist - so read the
|
|
|
|
// rest of the file, parse the playlist and return success.
|
|
|
|
|
|
|
|
// Create the pipeline - it gets unreffed if it goes out of scope
|
|
|
|
boost::shared_ptr<GstElement> pipeline(
|
|
|
|
gst_pipeline_new(NULL), boost::bind(&gst_object_unref, _1));
|
|
|
|
|
|
|
|
// Create the source element automatically based on the URL
|
|
|
|
GstElement* source = gst_element_make_from_uri(
|
|
|
|
GST_URI_SRC, url_.toEncoded().constData(), NULL);
|
|
|
|
if (!source) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Couldn't create gstreamer source element for" << url_.toString();
|
2010-06-15 15:24:17 +02:00
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the other elements and link them up
|
|
|
|
GstElement* typefind = gst_element_factory_make("typefind", NULL);
|
|
|
|
GstElement* fakesink = gst_element_factory_make("fakesink", NULL);
|
|
|
|
|
|
|
|
gst_bin_add_many(GST_BIN(pipeline.get()), source, typefind, fakesink, NULL);
|
|
|
|
gst_element_link_many(source, typefind, fakesink, NULL);
|
|
|
|
|
|
|
|
// Connect callbacks
|
|
|
|
GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline.get()));
|
|
|
|
g_signal_connect(typefind, "have-type", G_CALLBACK(TypeFound), this);
|
|
|
|
gst_bus_set_sync_handler(bus, BusCallbackSync, this);
|
|
|
|
gst_bus_add_watch(bus, BusCallback, this);
|
|
|
|
|
|
|
|
// Add a probe to the sink so we can capture the data if it's a playlist
|
|
|
|
GstPad* pad = gst_element_get_pad(fakesink, "sink");
|
|
|
|
gst_pad_add_buffer_probe(pad, G_CALLBACK(DataReady), this);
|
|
|
|
gst_object_unref(pad);
|
|
|
|
|
|
|
|
// Start "playing"
|
|
|
|
gst_element_set_state(pipeline.get(), GST_STATE_PLAYING);
|
|
|
|
pipeline_ = pipeline;
|
|
|
|
return WillLoadAsync;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoader::TypeFound(GstElement*, uint, GstCaps* caps, void* self) {
|
|
|
|
SongLoader* instance = static_cast<SongLoader*>(self);
|
|
|
|
|
|
|
|
if (instance->state_ != WaitingForType)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check the mimetype
|
2010-06-15 16:52:42 +02:00
|
|
|
instance->mime_type_ = gst_structure_get_name(gst_caps_get_structure(caps, 0));
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Mime type is" << instance->mime_type_;
|
2010-06-15 16:52:42 +02:00
|
|
|
if (instance->mime_type_ == "text/plain" ||
|
2010-11-26 16:16:48 +01:00
|
|
|
instance->mime_type_ == "text/uri-list" ||
|
|
|
|
instance->mime_type_ == "application/xml") {
|
2010-06-15 15:24:17 +02:00
|
|
|
// Yeah it might be a playlist, let's get some data and have a better look
|
|
|
|
instance->state_ = WaitingForMagic;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nope, not a playlist - we're done
|
|
|
|
instance->StopTypefindAsync(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoader::DataReady(GstPad *, GstBuffer *buf, void *self) {
|
|
|
|
SongLoader* instance = static_cast<SongLoader*>(self);
|
|
|
|
|
|
|
|
if (instance->state_ == Finished)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Append the data to the buffer
|
|
|
|
instance->buffer_.append(reinterpret_cast<const char*>(GST_BUFFER_DATA(buf)),
|
|
|
|
GST_BUFFER_SIZE(buf));
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Received total" << instance->buffer_.size() << "bytes";
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
if (instance->state_ == WaitingForMagic &&
|
|
|
|
instance->buffer_.size() >= PlaylistParser::kMagicSize) {
|
|
|
|
// Got enough that we can test the magic
|
|
|
|
instance->MagicReady();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean SongLoader::BusCallback(GstBus*, GstMessage* msg, gpointer self) {
|
|
|
|
SongLoader* instance = reinterpret_cast<SongLoader*>(self);
|
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE(msg)) {
|
|
|
|
case GST_MESSAGE_ERROR:
|
|
|
|
instance->ErrorMessageReceived(msg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-06-16 00:32:20 +02:00
|
|
|
return FALSE;
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GstBusSyncReply SongLoader::BusCallbackSync(GstBus*, GstMessage* msg, gpointer self) {
|
|
|
|
SongLoader* instance = reinterpret_cast<SongLoader*>(self);
|
|
|
|
switch (GST_MESSAGE_TYPE(msg)) {
|
|
|
|
case GST_MESSAGE_EOS:
|
|
|
|
instance->EndOfStreamReached();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_MESSAGE_ERROR:
|
|
|
|
instance->ErrorMessageReceived(msg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return GST_BUS_PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoader::ErrorMessageReceived(GstMessage* msg) {
|
|
|
|
if (state_ == Finished)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GError* error;
|
|
|
|
gchar* debugs;
|
|
|
|
|
|
|
|
gst_message_parse_error(msg, &error, &debugs);
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Error) << error->message;
|
|
|
|
qLog(Error) << debugs;
|
2010-06-15 15:24:17 +02:00
|
|
|
|
2010-07-10 20:39:41 +02:00
|
|
|
QString message_str = error->message;
|
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
g_error_free(error);
|
|
|
|
free(debugs);
|
|
|
|
|
2010-07-10 20:39:41 +02:00
|
|
|
if (state_ == WaitingForType &&
|
|
|
|
message_str == "Could not determine type of stream.") {
|
|
|
|
// Don't give up - assume it's a playlist and see if one of our parsers can
|
|
|
|
// read it.
|
|
|
|
state_ = WaitingForMagic;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
StopTypefindAsync(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoader::EndOfStreamReached() {
|
|
|
|
switch (state_) {
|
|
|
|
case Finished:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WaitingForMagic:
|
|
|
|
// Do the magic on the data we have already
|
|
|
|
MagicReady();
|
|
|
|
if (state_ == Finished)
|
|
|
|
break;
|
|
|
|
// It looks like a playlist, so parse it
|
|
|
|
|
|
|
|
// fallthrough
|
|
|
|
case WaitingForData:
|
|
|
|
// It's a playlist and we've got all the data - finish and parse it
|
|
|
|
StopTypefindAsync(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WaitingForType:
|
|
|
|
StopTypefindAsync(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoader::MagicReady() {
|
2011-02-27 13:14:32 +01:00
|
|
|
parser_ = playlist_parser_->ParserForMagic(buffer_, mime_type_);
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
if (!parser_) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << url_.toString() << "is text, but not a recognised playlist";
|
2010-06-15 15:24:17 +02:00
|
|
|
// It doesn't look like a playlist, so just finish
|
|
|
|
StopTypefindAsync(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is a playlist - we'll get more data and parse the whole thing in
|
|
|
|
// EndOfStreamReached
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Debug) << "Magic says" << parser_->name();
|
2010-10-25 14:19:36 +02:00
|
|
|
if (parser_->name() == "ASX/INI" && url_.scheme() == "http") {
|
2010-10-25 14:14:28 +02:00
|
|
|
// This is actually a weird MS-WMSP stream. Changing the protocol to MMS from
|
|
|
|
// HTTP makes it playable.
|
|
|
|
parser_ = NULL;
|
|
|
|
url_.setScheme("mms");
|
|
|
|
StopTypefindAsync(true);
|
|
|
|
}
|
2010-06-15 15:24:17 +02:00
|
|
|
state_ = WaitingForData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoader::StopTypefindAsync(bool success) {
|
|
|
|
state_ = Finished;
|
|
|
|
success_ = success;
|
|
|
|
|
|
|
|
metaObject()->invokeMethod(this, "StopTypefind", Qt::QueuedConnection);
|
|
|
|
}
|