2011-08-05 02:15:16 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, 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 <QMutexLocker>
|
2014-10-26 00:28:03 +02:00
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <gst/tag/tag.h>
|
|
|
|
#include <gst/audio/gstaudiocdsrc.h>
|
2011-08-05 02:15:16 +02:00
|
|
|
|
|
|
|
#include "core/logging.h"
|
2011-11-28 14:51:35 +01:00
|
|
|
#include "core/timeconstants.h"
|
2011-08-05 02:15:16 +02:00
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "library/librarymodel.h"
|
|
|
|
|
|
|
|
#include "cddadevice.h"
|
|
|
|
|
|
|
|
CddaDevice::CddaDevice(const QUrl& url, DeviceLister* lister,
|
2014-02-07 16:34:20 +01:00
|
|
|
const QString& unique_id, DeviceManager* manager,
|
|
|
|
Application* app, int database_id, bool first_time)
|
|
|
|
: ConnectedDevice(url, lister, unique_id, manager, app, database_id,
|
|
|
|
first_time),
|
|
|
|
cdda_(nullptr),
|
|
|
|
cdio_(nullptr) {}
|
|
|
|
|
|
|
|
CddaDevice::~CddaDevice() {
|
|
|
|
if (cdio_) cdio_destroy(cdio_);
|
2011-08-05 02:15:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CddaDevice::Init() {
|
|
|
|
QMutexLocker locker(&mutex_init_);
|
2014-02-07 16:34:20 +01:00
|
|
|
song_count_ = 0; // Reset song count, in case it was already set
|
|
|
|
cdio_ = cdio_open(url_.path().toLocal8Bit().constData(), DRIVER_DEVICE);
|
2014-02-06 16:49:49 +01:00
|
|
|
if (cdio_ == nullptr) {
|
2011-08-05 02:15:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Create gstreamer cdda element
|
2014-10-26 00:28:03 +02:00
|
|
|
GError* error = nullptr;
|
|
|
|
cdda_ = gst_element_make_from_uri(GST_URI_SRC, "cdda://", nullptr, &error);
|
|
|
|
if (error) {
|
|
|
|
qLog(Error) << error->code << error->message;
|
|
|
|
}
|
2014-02-06 16:49:49 +01:00
|
|
|
if (cdda_ == nullptr) {
|
2011-08-05 02:15:16 +02:00
|
|
|
model_->Reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-26 00:28:03 +02:00
|
|
|
g_object_set(cdda_, "device", g_strdup(url_.path().toLocal8Bit().constData()),
|
|
|
|
nullptr);
|
|
|
|
if (g_object_class_find_property (G_OBJECT_GET_CLASS (cdda_), "paranoia-mode")) {
|
|
|
|
g_object_set (cdda_, "paranoia-mode", 0, NULL);
|
|
|
|
}
|
2011-08-10 00:49:36 +02:00
|
|
|
|
2011-08-05 02:15:16 +02:00
|
|
|
// Change the element's state to ready and paused, to be able to query it
|
2014-02-07 16:34:20 +01: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-08-05 02:15:16 +02:00
|
|
|
model_->Reset();
|
|
|
|
gst_element_set_state(cdda_, GST_STATE_NULL);
|
|
|
|
gst_object_unref(GST_OBJECT(cdda_));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get number of tracks
|
2014-02-07 16:34:20 +01:00
|
|
|
GstFormat fmt = gst_format_get_by_nick("track");
|
2011-08-05 02:15:16 +02:00
|
|
|
GstFormat out_fmt = fmt;
|
|
|
|
gint64 num_tracks = 0;
|
2014-10-26 00:28:03 +02:00
|
|
|
if (!gst_element_query_duration(cdda_, out_fmt, &num_tracks) ||
|
2014-02-07 16:34:20 +01:00
|
|
|
out_fmt != fmt) {
|
2011-08-05 02:15:16 +02:00
|
|
|
qLog(Error) << "Error while querying cdda GstElement";
|
|
|
|
model_->Reset();
|
|
|
|
gst_object_unref(GST_OBJECT(cdda_));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SongList songs;
|
|
|
|
for (int track_number = 1; track_number <= num_tracks; track_number++) {
|
|
|
|
// Init song
|
|
|
|
Song song;
|
|
|
|
song.set_id(track_number);
|
|
|
|
song.set_valid(true);
|
|
|
|
song.set_filetype(Song::Type_Cdda);
|
2014-02-07 16:34:20 +01:00
|
|
|
song.set_url(
|
|
|
|
QUrl(QString("cdda://%1/%2").arg(url_.path()).arg(track_number)));
|
2011-08-05 02:15:16 +02:00
|
|
|
song.set_title(QString("Track %1").arg(track_number));
|
|
|
|
song.set_track(track_number);
|
|
|
|
songs << song;
|
|
|
|
}
|
|
|
|
song_count_ = num_tracks;
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(this, SIGNAL(SongsDiscovered(const SongList&)), model_,
|
|
|
|
SLOT(SongsDiscovered(const SongList&)));
|
2014-05-24 13:33:32 +02:00
|
|
|
// Reset the model before emitting the SongsDiscovered signal. This
|
|
|
|
// ensures that the model is updated properly even when a disc that
|
|
|
|
// doesn't exist in MusicBrainz is inserted.
|
|
|
|
model_->Reset();
|
2011-08-05 02:15:16 +02:00
|
|
|
emit SongsDiscovered(songs);
|
|
|
|
|
2014-10-26 00:28:03 +02:00
|
|
|
|
2011-08-05 02:15:16 +02:00
|
|
|
gst_tag_register_musicbrainz_tags();
|
2014-10-26 00:28:03 +02:00
|
|
|
|
|
|
|
GstElement* pipeline = gst_pipeline_new("pipeline");
|
|
|
|
GstElement* sink = gst_element_factory_make ("fakesink", NULL);
|
|
|
|
gst_bin_add_many (GST_BIN (pipeline), cdda_, sink, NULL);
|
|
|
|
gst_element_link (cdda_, sink);
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_READY);
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_PAUSED);
|
|
|
|
|
|
|
|
// Get TOC and TAG messages
|
|
|
|
GstMessage* msg = nullptr;
|
|
|
|
GstMessage* msg_toc = nullptr;
|
|
|
|
GstMessage* msg_tag = nullptr;
|
|
|
|
while ((msg = gst_bus_timed_pop_filtered(GST_ELEMENT_BUS(pipeline),
|
|
|
|
GST_SECOND, (GstMessageType)(GST_MESSAGE_TOC | GST_MESSAGE_TAG)))) {
|
|
|
|
if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_TOC) {
|
|
|
|
if (msg_toc) gst_message_unref(msg_toc); // Shouldn't happen, but just in case
|
|
|
|
msg_toc = msg;
|
|
|
|
} else if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_TAG) {
|
|
|
|
if (msg_tag) gst_message_unref(msg_tag);
|
|
|
|
msg_tag = msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle TOC message: get tracks duration
|
|
|
|
if (msg_toc) {
|
|
|
|
GstToc* toc;
|
|
|
|
gst_message_parse_toc (msg_toc, &toc, nullptr);
|
|
|
|
if (toc) {
|
|
|
|
GList* entries = gst_toc_get_entries(toc);
|
|
|
|
if (entries && songs.size() <= g_list_length (entries)) {
|
|
|
|
int i = 0;
|
|
|
|
for (GList* node = entries; node != nullptr; node = node->next) {
|
|
|
|
GstTocEntry *entry = static_cast<GstTocEntry*>(node->data);
|
|
|
|
quint64 duration = 0;
|
|
|
|
gint64 start, stop;
|
|
|
|
if (gst_toc_entry_get_start_stop_times (entry, &start, &stop))
|
|
|
|
duration = stop - start;
|
|
|
|
songs[i++].set_length_nanosec(duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_message_unref(msg_toc);
|
|
|
|
}
|
|
|
|
// Update songs with duration
|
|
|
|
model_->Reset();
|
|
|
|
emit SongsDiscovered(songs);
|
|
|
|
song_count_ = songs.size();
|
|
|
|
|
|
|
|
// Handle TAG message: generate MusicBrainz DiscId
|
|
|
|
if (msg_tag) {
|
|
|
|
GstTagList* tags = nullptr;
|
|
|
|
gst_message_parse_tag(msg_tag, &tags);
|
|
|
|
char* string_mb = nullptr;
|
|
|
|
if (gst_tag_list_get_string(tags, GST_TAG_CDDA_MUSICBRAINZ_DISCID,
|
|
|
|
&string_mb)) {
|
|
|
|
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);
|
|
|
|
gst_message_unref(msg_tag);
|
|
|
|
gst_tag_list_free(tags);
|
|
|
|
}
|
2011-08-05 02:15:16 +02:00
|
|
|
}
|
2011-08-09 21:30:28 +02:00
|
|
|
|
2014-10-26 00:28:03 +02:00
|
|
|
gst_element_set_state(pipeline, GST_STATE_NULL);
|
2011-08-15 18:08:09 +02:00
|
|
|
// This will also cause cdda_ to be unref'd.
|
2014-10-26 00:28:03 +02:00
|
|
|
gst_object_unref(pipeline);
|
2011-08-05 02:15:16 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void CddaDevice::AudioCDTagsLoaded(
|
|
|
|
const QString& artist, const QString& album,
|
|
|
|
const MusicBrainzClient::ResultList& results) {
|
|
|
|
MusicBrainzClient* musicbrainz_client =
|
|
|
|
qobject_cast<MusicBrainzClient*>(sender());
|
2011-08-05 02:15:16 +02:00
|
|
|
musicbrainz_client->deleteLater();
|
|
|
|
SongList songs;
|
|
|
|
int track_number = 1;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (results.size() == 0) return;
|
2011-08-05 02:15:16 +02:00
|
|
|
model_->Reset();
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const MusicBrainzClient::Result& ret : results) {
|
2011-08-05 02:15:16 +02:00
|
|
|
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);
|
2012-01-07 22:51:02 +01:00
|
|
|
song.set_year(ret.year_);
|
|
|
|
song.set_id(track_number);
|
2014-02-07 16:34:20 +01:00
|
|
|
// We need to set url: that's how playlist will find the correct item to
|
|
|
|
// update
|
|
|
|
song.set_url(
|
|
|
|
QUrl(QString("cdda://%1/%2").arg(unique_id()).arg(track_number++)));
|
2011-08-05 02:15:16 +02:00
|
|
|
songs << song;
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(this, SIGNAL(SongsDiscovered(const SongList&)), model_,
|
|
|
|
SLOT(SongsDiscovered(const SongList&)));
|
2011-08-05 02:15:16 +02:00
|
|
|
emit SongsDiscovered(songs);
|
2012-11-18 22:14:06 +01:00
|
|
|
song_count_ = songs.size();
|
2011-08-05 02:15:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CddaDevice::Refresh() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if ((cdio_ && cdda_) && /* already init... */
|
|
|
|
cdio_get_media_changed(cdio_) !=
|
|
|
|
1 /* ...and hasn't change since last time */) {
|
2011-08-05 02:15:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check if mutex is already token (i.e. init is already taking place)
|
|
|
|
if (!mutex_init_.tryLock()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mutex_init_.unlock();
|
|
|
|
Init();
|
|
|
|
}
|