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

600 lines
15 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/>.
*/
#include "config.h"
2009-12-24 20:16:07 +01:00
#include "player.h"
#include "engines/enginebase.h"
#include "playlist/playlist.h"
#include "playlist/playlistitem.h"
#include "playlist/playlistmanager.h"
#include "radio/lastfmservice.h"
2010-05-31 23:24:54 +02:00
#ifdef HAVE_GSTREAMER
# include "engines/gstengine.h"
2010-05-31 23:24:54 +02:00
#endif
#ifdef HAVE_LIBVLC
# include "engines/vlcengine.h"
2010-05-31 23:24:54 +02:00
#endif
#ifdef HAVE_LIBXINE
# include "engines/xine-engine.h"
2010-05-31 23:24:54 +02:00
#endif
#ifdef HAVE_QT_PHONON
# include "engines/phononengine.h"
#endif
2010-03-24 22:07:16 +01:00
#ifdef Q_WS_X11
# include "core/mpris_player.h"
# include "core/mpris_tracklist.h"
2010-03-24 22:07:16 +01:00
# 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-04-15 00:36:28 +02:00
using boost::shared_ptr;
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
Player::Player(PlaylistManager* playlists, LastFMService* lastfm,
Engine::Type engine, QObject* parent)
2009-12-24 20:16:07 +01:00
: QObject(parent),
playlists_(playlists),
2009-12-29 20:22:02 +01:00
lastfm_(lastfm),
engine_(CreateEngine(engine)),
stream_change_type_(Engine::First)
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-05-31 22:24:05 +02:00
connect(engine_.get(), 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
}
2010-05-28 21:51:51 +02:00
Player::~Player() {
}
EngineBase* Player::CreateEngine(Engine::Type engine) {
switch(engine) {
2010-05-31 23:24:54 +02:00
#ifdef HAVE_GSTREAMER
2010-05-31 22:59:13 +02:00
case Engine::Type_GStreamer:
return new GstEngine();
break;
2010-05-31 23:24:54 +02:00
#endif
#ifdef HAVE_LIBVLC
2010-05-31 22:59:13 +02:00
case Engine::Type_VLC:
return new VlcEngine();
break;
2010-05-31 23:24:54 +02:00
#endif
#ifdef HAVE_LIBXINE
2010-05-31 22:59:13 +02:00
case Engine::Type_Xine:
return new XineEngine();
break;
2010-05-31 23:24:54 +02:00
#endif
#ifdef HAVE_QT_PHONON
2010-05-31 22:59:13 +02:00
case Engine::Type_QtPhonon:
return new PhononEngine();
break;
#endif
default:
qFatal("Selected engine not compiled in");
break;
}
/* NOT REACHED */
return NULL;
}
2010-02-03 22:48:00 +01:00
void Player::Init() {
2010-04-12 01:24:03 +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
2010-05-31 22:24:05 +02:00
connect(engine_.get(), SIGNAL(StateChanged(Engine::State)), SLOT(EngineStateChanged(Engine::State)));
connect(engine_.get(), SIGNAL(TrackAboutToEnd()), SLOT(TrackAboutToEnd()));
connect(engine_.get(), SIGNAL(TrackEnded()), SLOT(TrackEnded()));
connect(engine_.get(), SIGNAL(MetaData(Engine::SimpleMetaBundle)),
2010-02-03 23:20:31 +01:00
SLOT(EngineMetadataReceived(Engine::SimpleMetaBundle)));
2010-02-03 22:48:00 +01:00
2010-04-12 01:24:03 +02:00
engine_->SetVolume(settings_.value("volume", 50).toInt());
2009-12-24 20:16:07 +01:00
}
void Player::ReloadSettings() {
2010-04-12 01:03:39 +02:00
engine_->ReloadSettings();
}
void Player::HandleSpecialLoad(const PlaylistItem::SpecialLoadResult &result) {
switch (result.type_) {
case PlaylistItem::SpecialLoadResult::NoMoreTracks:
loading_async_ = QUrl();
NextItem(Engine::Auto);
break;
case PlaylistItem::SpecialLoadResult::TrackAvailable: {
// Might've been an async load, so check we're still on the same item
int current_index = playlists_->active()->current_index();
if (current_index == -1)
return;
shared_ptr<PlaylistItem> item = playlists_->active()->item_at(current_index);
if (!item || item->Url() != result.original_url_)
return;
engine_->Play(result.media_url_, stream_change_type_);
current_item_ = item;
loading_async_ = QUrl();
break;
}
case PlaylistItem::SpecialLoadResult::WillLoadAsynchronously:
// We'll get called again later with either NoMoreTracks or TrackAvailable
loading_async_ = result.original_url_;
break;
}
}
void Player::Next() {
NextInternal(Engine::Manual);
}
void Player::NextInternal(Engine::TrackChangeType change) {
if (playlists_->active()->current_item() &&
playlists_->active()->current_item()->options() & PlaylistItem::ContainsMultipleTracks) {
// The next track is already being loaded
if (playlists_->active()->current_item()->Url() == loading_async_)
return;
stream_change_type_ = change;
HandleSpecialLoad(playlists_->active()->current_item()->LoadNext());
2009-12-29 21:48:50 +01:00
return;
}
NextItem(change);
}
void Player::NextItem(Engine::TrackChangeType change) {
int i = playlists_->active()->next_index();
playlists_->active()->set_current_index(i);
2009-12-24 20:16:07 +01:00
if (i == -1) {
emit PlaylistFinished();
2009-12-24 20:16:07 +01:00
Stop();
return;
}
PlayAt(i, change, false);
2009-12-24 20:16:07 +01:00
}
void Player::TrackEnded() {
if (playlists_->active()->stop_after_current()) {
Stop();
return;
}
NextInternal(Engine::Auto);
}
2009-12-24 20:16:07 +01:00
void Player::PlayPause() {
switch (engine_->state()) {
case Engine::Paused:
2010-04-12 01:24:03 +02:00
engine_->Unpause();
2009-12-24 20:16:07 +01:00
break;
2010-05-27 15:17:28 +02:00
case Engine::Playing: {
// We really shouldn't pause last.fm streams
2010-05-27 15:17:28 +02:00
// Stopping seems like a reasonable thing to do (especially on mac where there
// is no media key for stop).
if (current_item_->options() & PlaylistItem::PauseDisabled) {
engine_->Stop();
} else {
engine_->Pause();
}
2009-12-24 20:16:07 +01:00
break;
2010-05-27 15:17:28 +02:00
}
2009-12-24 20:16:07 +01:00
case Engine::Empty:
case Engine::Idle: {
playlists_->SetActivePlaylist(playlists_->current_id());
if (playlists_->active()->rowCount() == 0)
break;
int i = playlists_->active()->current_index();
if (i == -1) i = playlists_->active()->last_played_index();
if (i == -1) i = 0;
PlayAt(i, Engine::First, true);
2009-12-24 20:16:07 +01:00
break;
}
}
}
void Player::Stop() {
2010-04-12 01:24:03 +02:00
engine_->Stop();
playlists_->active()->set_current_index(-1);
current_item_.reset();
2009-12-24 20:16:07 +01:00
}
void Player::Previous() {
int i = playlists_->active()->previous_index();
playlists_->active()->set_current_index(i);
2009-12-24 20:16:07 +01:00
if (i == -1) {
Stop();
return;
}
PlayAt(i, Engine::Manual, 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) {
int old_volume = engine_->volume();
2010-03-24 23:31:34 +01:00
int volume = qBound(0, value, 100);
settings_.setValue("volume", volume);
2010-04-12 01:24:03 +02:00
engine_->SetVolume(volume);
if (volume != old_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, Engine::TrackChangeType change, bool reshuffle) {
if (reshuffle)
playlists_->active()->set_current_index(-1);
playlists_->active()->set_current_index(index);
2009-12-26 22:35:45 +01:00
current_item_ = playlists_->active()->item_at(index);
2009-12-26 22:35:45 +01:00
if (current_item_->options() & PlaylistItem::SpecialPlayBehaviour) {
// It's already loading
if (current_item_->Url() == loading_async_)
return;
HandleSpecialLoad(current_item_->StartLoading());
}
2009-12-29 20:22:02 +01:00
else {
loading_async_ = QUrl();
engine_->Play(current_item_->Url(), change);
2009-12-29 21:48:50 +01:00
if (lastfm_->IsScrobblingEnabled())
lastfm_->NowPlaying(current_item_->Metadata());
2009-12-29 20:22:02 +01:00
}
emit CapsChange(GetCaps());
2009-12-26 22:35:45 +01:00
}
void Player::CurrentMetadataChanged(const Song &metadata) {
lastfm_->NowPlaying(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) {
int msec = qBound(0, seconds * 1000, int(engine_->length()));
engine_->Seek(msec);
// If we seek the track we don't want to submit it to last.fm
playlists_->active()->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) {
shared_ptr<PlaylistItem> item = playlists_->active()->current_item();
2010-02-03 23:20:31 +01:00
if (item == NULL)
return;
Song song = item->Metadata();
song.MergeFromSimpleMetaBundle(bundle);
2010-02-03 23:20:31 +01:00
2010-02-04 00:56:41 +01:00
// Ignore useless metadata
if (song.title().isEmpty() && song.artist().isEmpty())
return;
playlists_->active()->SetStreamMetadata(item->Url(), song);
2010-02-03 23:20:31 +01:00
}
int Player::GetCaps() const {
int caps = CAN_HAS_TRACKLIST;
if (current_item_) { caps |= CAN_PROVIDE_METADATA; }
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_->Metadata().filetype() != Song::Type_Stream) {
caps |= CAN_SEEK;
}
if (playlists_->active()->next_index() != -1 ||
playlists_->active()->current_item_options() & PlaylistItem::ContainsMultipleTracks) {
caps |= CAN_GO_NEXT;
}
if (playlists_->active()->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;
}
status.random = playlists_->sequence()->shuffle_mode() == PlaylistSequence::Shuffle_Off ? 0 : 1;
PlaylistSequence::RepeatMode repeat_mode = playlists_->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;
const Song& song = item.Metadata();
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;
}
QVariantMap Player::GetMetadata() const {
shared_ptr<PlaylistItem> item = playlists_->active()->current_item();
if (item) {
return GetMetadata(*item);
}
return QVariantMap();
}
QVariantMap Player::GetMetadata(int track) const {
if (track >= playlists_->active()->rowCount() || track < 0) {
return QVariantMap();
}
const PlaylistItem& item = *(playlists_->active()->item_at(track));
return GetMetadata(item);
}
void Player::Mute() {
SetVolume(0);
}
void Player::Pause() {
switch (GetState()) {
case Engine::Playing:
2010-04-12 01:24:03 +02:00
engine_->Pause();
break;
case Engine::Paused:
engine_->Unpause();
break;
default:
return;
}
}
void Player::Play() {
switch (GetState()) {
case Engine::Playing:
Seek(0);
break;
case Engine::Paused:
2010-04-12 01:24:03 +02:00
engine_->Unpause();
break;
default:
PlayPause();
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) {
playlists_->sequence()->SetRepeatMode(
enable ? PlaylistSequence::Repeat_Track : PlaylistSequence::Repeat_Off);
}
void Player::ShowOSD() {
if (current_item_)
emit ForceShowOSD(current_item_->Metadata());
}
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 = playlists_->active()->InsertPaths(list, play_now ? playlists_->active()->current_index() + 1 : -1);
} else {
index = playlists_->active()->InsertStreamUrls(list, play_now ? playlists_->active()->current_index() + 1: -1);
}
if (index.isValid()) {
if (play_now) {
PlayAt(index.row(), Engine::First, true);
}
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) {
playlists_->active()->removeRows(index, 1);
}
int Player::GetCurrentTrack() const {
return playlists_->active()->current_index();
}
int Player::GetLength() const {
return playlists_->active()->rowCount();
}
void Player::SetLoop(bool enable) {
playlists_->active()->sequence()->SetRepeatMode(
enable ? PlaylistSequence::Repeat_Playlist : PlaylistSequence::Repeat_Off);
}
void Player::SetRandom(bool enable) {
playlists_->active()->sequence()->SetShuffleMode(
enable ? PlaylistSequence::Shuffle_All : PlaylistSequence::Shuffle_Off);
}
void Player::PlayTrack(int index) {
PlayAt(index, Engine::Manual, true);
}
void Player::PlaylistChanged() {
emit TrackListChange(GetLength());
}
2010-04-21 15:55:30 +02:00
void Player::TrackAboutToEnd() {
if (engine_->is_autocrossfade_enabled()) {
// Crossfade is on, so just start playing the next track. The current one
// will fade out, and the new one will fade in
NextInternal(Engine::Auto);
2010-04-21 15:55:30 +02:00
} else {
// Crossfade is off, so start preloading the next track so we don't get a
// gap between songs.
if (current_item_->options() & PlaylistItem::ContainsMultipleTracks)
2010-04-21 15:55:30 +02:00
return;
if (playlists_->active()->next_index() == -1)
return;
2010-04-21 15:55:30 +02:00
shared_ptr<PlaylistItem> item = playlists_->active()->item_at(
playlists_->active()->next_index());
2010-04-21 15:55:30 +02:00
if (!item)
return;
QUrl url = item->Url();
// Get the actual track URL rather than the stream URL.
if (item->options() & PlaylistItem::ContainsMultipleTracks) {
PlaylistItem::SpecialLoadResult result = item->LoadNext();
switch (result.type_) {
case PlaylistItem::SpecialLoadResult::NoMoreTracks:
return;
case PlaylistItem::SpecialLoadResult::WillLoadAsynchronously:
loading_async_ = item->Url();
return;
case PlaylistItem::SpecialLoadResult::TrackAvailable:
url = result.media_url_;
break;
}
}
engine_->StartPreloading(url);
2010-04-21 15:55:30 +02:00
}
}