Clementine-audio-player-Mac.../src/player.cpp

478 lines
12 KiB
C++
Raw Normal View History

/* This file is part of Clementine.
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/>.
*/
2009-12-24 20:16:07 +01:00
#include "player.h"
#include "playlist.h"
2009-12-29 20:22:02 +01:00
#include "lastfmservice.h"
2010-04-06 18:57:02 +02:00
#include "engines/gstengine.h"
2010-03-24 22:07:16 +01:00
#ifdef Q_WS_X11
# include "mpris_player.h"
# include "mpris_tracklist.h"
# include <QDBusConnection>
#endif
2009-12-24 20:16:07 +01:00
#include <QtDebug>
2010-02-03 22:48:00 +01:00
#include <QtConcurrentRun>
#include <boost/bind.hpp>
2009-12-24 20:16:07 +01:00
2010-03-24 22:34:32 +01:00
#ifdef Q_WS_X11
QDBusArgument& operator<< (QDBusArgument& arg, const DBusStatus& status) {
arg.beginStructure();
2010-03-24 22:46:00 +01:00
arg << status.play;
arg << status.random;
arg << status.repeat;
arg << status.repeat_playlist;
arg.endStructure();
return arg;
}
const QDBusArgument& operator>> (const QDBusArgument& arg, DBusStatus& status) {
arg.beginStructure();
2010-03-24 22:46:00 +01:00
arg >> status.play;
arg >> status.random;
arg >> status.repeat;
arg >> status.repeat_playlist;
arg.endStructure();
return arg;
}
2010-03-24 22:34:32 +01:00
#endif
2009-12-29 20:22:02 +01:00
Player::Player(Playlist* playlist, LastFMService* lastfm, QObject* parent)
2009-12-24 20:16:07 +01:00
: QObject(parent),
playlist_(playlist),
2009-12-29 20:22:02 +01:00
lastfm_(lastfm),
current_item_options_(PlaylistItem::Default),
2010-04-06 18:57:02 +02:00
engine_(new GstEngine)
2009-12-24 20:16:07 +01:00
{
2010-02-03 22:48:00 +01:00
settings_.beginGroup("Player");
2010-02-03 23:05:39 +01:00
SetVolume(settings_.value("volume", 50).toInt());
2010-02-03 22:48:00 +01:00
connect(engine_, SIGNAL(error(QString)), SIGNAL(Error(QString)));
2010-03-24 22:07:16 +01:00
// MPRIS DBus interface.
#ifdef Q_WS_X11
MprisPlayer* mpris = new MprisPlayer(this);
// Hack so the next registerObject() doesn't override this one.
QDBusConnection::sessionBus().registerObject(
"/Player", mpris, QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals);
new MprisTrackList(this);
QDBusConnection::sessionBus().registerObject("/TrackList", this);
2010-03-24 22:07:16 +01:00
#endif
2010-02-03 22:48:00 +01:00
}
void Player::Init() {
2010-04-06 18:57:02 +02:00
if (!engine_->init())
2010-02-03 22:48:00 +01:00
qFatal("Error initialising audio engine");
2009-12-24 20:16:07 +01:00
connect(engine_, SIGNAL(stateChanged(Engine::State)), SLOT(EngineStateChanged(Engine::State)));
connect(engine_, SIGNAL(trackEnded()), SLOT(TrackEnded()));
2010-02-03 23:20:31 +01:00
connect(engine_, SIGNAL(metaData(Engine::SimpleMetaBundle)),
SLOT(EngineMetadataReceived(Engine::SimpleMetaBundle)));
2010-02-03 22:48:00 +01:00
engine_->setVolume(settings_.value("volume", 50).toInt());
2009-12-24 20:16:07 +01:00
}
void Player::ReloadSettings() {
engine_->reloadSettings();
}
2009-12-24 20:16:07 +01:00
void Player::Next() {
2009-12-29 21:48:50 +01:00
if (playlist_->current_item_options() & PlaylistItem::ContainsMultipleTracks) {
playlist_->current_item()->LoadNext();
return;
}
NextItem();
}
void Player::NextItem() {
2009-12-29 20:57:33 +01:00
int i = playlist_->next_index();
playlist_->set_current_index(i);
2009-12-24 20:16:07 +01:00
if (i == -1) {
Stop();
return;
}
PlayAt(i, false);
2009-12-24 20:16:07 +01:00
}
void Player::TrackEnded() {
2009-12-29 20:57:33 +01:00
int i = playlist_->current_index();
if (i == -1 || playlist_->stop_after_current()) {
Stop();
return;
}
2009-12-29 21:48:50 +01:00
Next();
}
2009-12-24 20:16:07 +01:00
void Player::PlayPause() {
switch (engine_->state()) {
case Engine::Paused:
qDebug() << "Unpausing";
engine_->unpause();
break;
case Engine::Playing:
// We really shouldn't pause last.fm streams
if (current_item_options_ & PlaylistItem::PauseDisabled)
break;
2009-12-24 20:16:07 +01:00
qDebug() << "Pausing";
engine_->pause();
break;
case Engine::Empty:
case Engine::Idle: {
2009-12-29 20:57:33 +01:00
int i = playlist_->current_index();
2009-12-24 20:16:07 +01:00
if (i == -1) {
if (playlist_->rowCount() == 0)
break;
i = 0;
}
PlayAt(i, false);
2009-12-24 20:16:07 +01:00
break;
}
}
}
void Player::Stop() {
engine_->stop();
2009-12-29 20:57:33 +01:00
playlist_->set_current_index(-1);
2009-12-24 20:16:07 +01:00
}
void Player::Previous() {
2009-12-29 20:57:33 +01:00
int i = playlist_->previous_index();
playlist_->set_current_index(i);
2009-12-24 20:16:07 +01:00
if (i == -1) {
Stop();
return;
}
PlayAt(i, false);
2009-12-24 20:16:07 +01:00
}
void Player::EngineStateChanged(Engine::State state) {
switch (state) {
case Engine::Paused: emit Paused(); break;
case Engine::Playing: emit Playing(); break;
case Engine::Empty:
case Engine::Idle: emit Stopped(); break;
}
emit StatusChange(GetStatus());
emit CapsChange(GetCaps());
2009-12-24 20:16:07 +01:00
}
void Player::SetVolume(int value) {
2010-03-24 23:31:34 +01:00
int volume = qBound(0, value, 100);
settings_.setValue("volume", volume);
engine_->setVolume(volume);
emit VolumeChanged(volume);
2009-12-24 20:16:07 +01:00
}
int Player::GetVolume() const {
return engine_->volume();
}
Engine::State Player::GetState() const {
return engine_->state();
}
void Player::PlayAt(int index, bool manual_change) {
if (manual_change)
playlist_->set_current_index(-1); // to reshuffle
2009-12-29 20:57:33 +01:00
playlist_->set_current_index(index);
2009-12-26 22:35:45 +01:00
PlaylistItem* item = playlist_->item_at(index);
current_item_options_ = item->options();
current_item_ = item->Metadata();
2009-12-26 22:35:45 +01:00
2009-12-29 17:12:08 +01:00
if (item->options() & PlaylistItem::SpecialPlayBehaviour)
item->StartLoading();
2009-12-29 20:22:02 +01:00
else {
2009-12-26 22:35:45 +01:00
engine_->play(item->Url());
2009-12-29 21:48:50 +01:00
if (lastfm_->IsScrobblingEnabled())
lastfm_->NowPlaying(item->Metadata());
2009-12-29 20:22:02 +01:00
}
emit CapsChange(GetCaps());
2009-12-26 22:35:45 +01:00
}
void Player::StreamReady(const QUrl& original_url, const QUrl& media_url) {
2009-12-29 20:57:33 +01:00
int current_index = playlist_->current_index();
2009-12-26 22:35:45 +01:00
if (current_index == -1)
return;
PlaylistItem* item = playlist_->item_at(current_index);
if (!item || item->Url() != original_url)
return;
engine_->play(media_url);
2009-12-29 21:48:50 +01:00
2010-02-04 18:24:01 +01:00
current_item_ = item->Metadata();
current_item_options_ = item->options();
}
void Player::CurrentMetadataChanged(const Song &metadata) {
lastfm_->NowPlaying(metadata);
current_item_ = metadata;
emit TrackChange(GetMetadata());
2009-12-24 20:16:07 +01:00
}
2010-01-15 18:12:47 +01:00
void Player::Seek(int seconds) {
engine_->seek(seconds * 1000);
// If we seek the track we don't want to submit it to last.fm
playlist_->set_scrobbled(true);
2010-01-15 18:12:47 +01:00
}
2010-02-03 23:20:31 +01:00
void Player::EngineMetadataReceived(const Engine::SimpleMetaBundle& bundle) {
PlaylistItem* item = playlist_->current_item();
if (item == NULL)
return;
Song song;
song.InitFromSimpleMetaBundle(bundle);
2010-02-04 00:56:41 +01:00
// Ignore useless metadata
if (song.title().isEmpty() && song.artist().isEmpty())
return;
2010-02-03 23:20:31 +01:00
playlist_->SetStreamMetadata(item->Url(), song);
}
int Player::GetCaps() const {
int caps = CAN_HAS_TRACKLIST;
if (current_item_.is_valid()) { caps |= CAN_PROVIDE_METADATA; }
2010-03-24 22:46:00 +01:00
if (GetState() == Engine::Playing && current_item_options_ & PlaylistItem::PauseDisabled) {
caps |= CAN_PAUSE;
}
if (GetState() == Engine::Paused) {
caps |= CAN_PLAY;
}
if (GetState() != Engine::Empty && current_item_.filetype() != Song::Type_Stream) {
caps |= CAN_SEEK;
}
if (playlist_->next_index() != -1 ||
playlist_->current_item_options() & PlaylistItem::ContainsMultipleTracks) {
caps |= CAN_GO_NEXT;
}
if (playlist_->previous_index() != -1) {
caps |= CAN_GO_PREV;
}
return caps;
}
DBusStatus Player::GetStatus() const {
DBusStatus status;
switch (GetState()) {
case Engine::Empty:
case Engine::Idle:
2010-03-24 22:46:00 +01:00
status.play = DBusStatus::Mpris_Stopped;
break;
case Engine::Playing:
2010-03-24 22:46:00 +01:00
status.play = DBusStatus::Mpris_Playing;
break;
case Engine::Paused:
2010-03-24 22:46:00 +01:00
status.play = DBusStatus::Mpris_Paused;
break;
}
2010-03-24 22:46:00 +01:00
status.random = playlist_->sequence()->shuffle_mode() == PlaylistSequence::Shuffle_Off ? 0 : 1;
PlaylistSequence::RepeatMode repeat_mode = playlist_->sequence()->repeat_mode();
2010-03-24 22:46:00 +01:00
status.repeat = repeat_mode == PlaylistSequence::Repeat_Track ? 1 : 0;
status.repeat_playlist = (repeat_mode == PlaylistSequence::Repeat_Album ||
repeat_mode == PlaylistSequence::Repeat_Playlist) ? 1 : 0;
return status;
}
namespace {
inline void AddMetadata(const QString& key, const QString& metadata, QVariantMap* map) {
if (!metadata.isEmpty()) {
(*map)[key] = metadata;
}
}
inline void AddMetadata(const QString& key, int metadata, QVariantMap* map) {
if (metadata > 0) {
(*map)[key] = metadata;
}
}
} // namespace
QVariantMap Player::GetMetadata(const PlaylistItem& item) const {
QVariantMap ret;
if (item.type() == PlaylistItem::Type_Song) {
const Song& song = item.Metadata();
if (song.is_valid()) {
AddMetadata("location", item.Url().toString(), &ret);
AddMetadata("title", song.PrettyTitle(), &ret);
AddMetadata("artist", song.artist(), &ret);
AddMetadata("album", song.album(), &ret);
AddMetadata("time", song.length(), &ret);
AddMetadata("tracknumber", song.track(), &ret);
}
return ret;
} else {
AddMetadata("location", item.Url().toString(), &ret);
const Song& song = item.Metadata();
AddMetadata("title", song.PrettyTitle(), &ret);
AddMetadata("artist", song.artist(), &ret);
AddMetadata("album", song.album(), &ret);
AddMetadata("time", song.length(), &ret);
AddMetadata("tracknumber", song.track(), &ret);
return ret;
}
}
QVariantMap Player::GetMetadata() const {
PlaylistItem* item = playlist_->current_item();
if (item) {
return GetMetadata(*item);
}
return QVariantMap();
}
QVariantMap Player::GetMetadata(int track) const {
if (track >= playlist_->rowCount() || track < 0) {
return QVariantMap();
}
const PlaylistItem& item = *(playlist_->item_at(track));
return GetMetadata(item);
}
void Player::Mute() {
SetVolume(0);
}
void Player::Pause() {
switch (GetState()) {
case Engine::Playing:
engine_->pause();
break;
case Engine::Paused:
engine_->pause();
break;
default:
return;
}
}
void Player::Play() {
switch (GetState()) {
case Engine::Playing:
Seek(0);
break;
case Engine::Paused:
engine_->unpause();
break;
default:
Next();
break;
}
}
void Player::Prev() {
Previous();
}
int Player::PositionGet() const {
return engine_->position();
}
void Player::PositionSet(int x) {
Seek(x / 1000);
}
void Player::Repeat(bool enable) {
playlist_->sequence()->SetRepeatMode(
enable ? PlaylistSequence::Repeat_Track : PlaylistSequence::Repeat_Off);
}
void Player::ShowOSD() {
emit ForceShowOSD(current_item_);
}
void Player::VolumeDown(int change) {
SetVolume(GetVolume() - change);
}
void Player::VolumeUp(int change) {
SetVolume(GetVolume() + change);
}
int Player::VolumeGet() const {
return GetVolume();
}
void Player::VolumeSet(int volume) {
SetVolume(volume);
}
int Player::AddTrack(const QString& track, bool play_now) {
QUrl url(track);
QList<QUrl> list;
list << url;
QModelIndex index;
if (url.scheme() == "file") {
index = playlist_->InsertPaths(list, play_now ? playlist_->current_index() + 1 : -1);
} else {
index = playlist_->InsertStreamUrls(list, play_now ? playlist_->current_index() + 1: -1);
}
if (index.isValid()) {
if (play_now) {
Next();
}
2010-03-24 22:46:00 +01:00
return 0; // Success.
}
2010-03-24 22:46:00 +01:00
return -1; // Anything else for failure.
}
void Player::DelTrack(int index) {
playlist_->removeRows(index, 1);
}
int Player::GetCurrentTrack() const {
return playlist_->current_index();
}
int Player::GetLength() const {
return playlist_->rowCount();
}
void Player::SetLoop(bool enable) {
playlist_->sequence()->SetRepeatMode(
enable ? PlaylistSequence::Repeat_Playlist : PlaylistSequence::Repeat_Off);
}
void Player::SetRandom(bool enable) {
playlist_->sequence()->SetShuffleMode(
enable ? PlaylistSequence::Shuffle_All : PlaylistSequence::Shuffle_Off);
}
void Player::PlayTrack(int index) {
PlayAt(index, true);
}
void Player::PlaylistChanged() {
emit TrackListChange(GetLength());
}