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"
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-02-21 22:55:03 +01:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
# include "phononengine.h"
|
|
|
|
#else
|
|
|
|
# include "xine-engine.h"
|
|
|
|
#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
|
|
|
|
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),
|
2010-02-03 15:21:53 +01:00
|
|
|
current_item_options_(PlaylistItem::Default),
|
2010-02-21 21:49:38 +01:00
|
|
|
engine_(NULL),
|
2010-02-03 22:48:00 +01:00
|
|
|
init_engine_watcher_(new QFutureWatcher<bool>(this))
|
2009-12-24 20:16:07 +01:00
|
|
|
{
|
2010-02-21 22:55:03 +01:00
|
|
|
#ifdef Q_OS_WIN32
|
2010-02-21 21:49:38 +01:00
|
|
|
engine_ = new PhononEngine;
|
2010-02-21 22:55:03 +01:00
|
|
|
#else
|
|
|
|
engine_ = new XineEngine;
|
|
|
|
#endif
|
2010-02-21 21:49:38 +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(init_engine_watcher_, SIGNAL(finished()), SLOT(EngineInitFinished()));
|
|
|
|
connect(engine_, SIGNAL(error(QString)), SIGNAL(Error(QString)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::Init() {
|
|
|
|
init_engine_ = QtConcurrent::run(boost::bind(&EngineBase::init, engine_));
|
|
|
|
init_engine_watcher_->setFuture(init_engine_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::EngineInitFinished() {
|
|
|
|
if (init_engine_.result() == false) {
|
|
|
|
qFatal("Error initialising audio engine");
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
connect(engine_, SIGNAL(stateChanged(Engine::State)), SLOT(EngineStateChanged(Engine::State)));
|
2009-12-26 23:59:11 +01:00
|
|
|
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
|
|
|
|
2010-02-27 17:47:31 +01:00
|
|
|
engine_->setVolume(settings_.value("volume", 50).toInt());
|
|
|
|
|
2010-02-03 22:48:00 +01:00
|
|
|
emit InitFinished();
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2010-02-03 17:51:56 +01:00
|
|
|
void Player::ReloadSettings() {
|
2010-02-03 22:48:00 +01:00
|
|
|
if (!init_engine_.isFinished())
|
|
|
|
return;
|
|
|
|
|
2010-02-03 17:51:56 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-02-04 00:12:21 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-12-26 23:59:11 +01:00
|
|
|
void Player::TrackEnded() {
|
2009-12-29 20:57:33 +01:00
|
|
|
int i = playlist_->current_index();
|
2009-12-27 00:43:38 +01:00
|
|
|
if (i == -1 || playlist_->stop_after_current()) {
|
2009-12-26 23:59:11 +01:00
|
|
|
Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-29 21:48:50 +01:00
|
|
|
Next();
|
2009-12-26 23:59:11 +01:00
|
|
|
}
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
void Player::PlayPause() {
|
2010-02-03 22:48:00 +01:00
|
|
|
if (!init_engine_.isFinished())
|
|
|
|
return;
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
switch (engine_->state()) {
|
|
|
|
case Engine::Paused:
|
|
|
|
qDebug() << "Unpausing";
|
|
|
|
engine_->unpause();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Engine::Playing:
|
2010-01-17 16:48:31 +01:00
|
|
|
// We really shouldn't pause last.fm streams
|
2010-02-03 15:21:53 +01:00
|
|
|
if (current_item_options_ & PlaylistItem::PauseDisabled)
|
2010-01-17 16:48:31 +01:00
|
|
|
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);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::Stop() {
|
2010-02-03 22:48:00 +01:00
|
|
|
if (!init_engine_.isFinished())
|
|
|
|
return;
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
qDebug() << "Stopping";
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::SetVolume(int value) {
|
|
|
|
settings_.setValue("volume", value);
|
|
|
|
engine_->setVolume(value);
|
2010-02-03 23:05:39 +01:00
|
|
|
emit VolumeChanged(value);
|
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) {
|
2010-02-03 22:48:00 +01:00
|
|
|
if (!init_engine_.isFinished())
|
|
|
|
return;
|
|
|
|
|
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);
|
2010-02-03 15:21:53 +01:00
|
|
|
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
|
|
|
}
|
2009-12-26 22:35:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::StreamReady(const QUrl& original_url, const QUrl& media_url) {
|
2010-02-03 22:48:00 +01:00
|
|
|
if (!init_engine_.isFinished())
|
|
|
|
return;
|
|
|
|
|
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();
|
2010-03-08 00:28:40 +01:00
|
|
|
current_item_options_ = item->options();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::CurrentMetadataChanged(const Song &metadata) {
|
|
|
|
lastfm_->NowPlaying(metadata);
|
|
|
|
current_item_ = metadata;
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
2010-01-15 18:12:47 +01:00
|
|
|
|
|
|
|
void Player::Seek(int seconds) {
|
2010-02-03 22:48:00 +01:00
|
|
|
if (!init_engine_.isFinished())
|
|
|
|
return;
|
|
|
|
|
2010-01-15 18:12:47 +01:00
|
|
|
engine_->seek(seconds * 1000);
|
|
|
|
}
|
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);
|
|
|
|
}
|