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

251 lines
5.6 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"
2009-12-24 20:16:07 +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),
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
{
#ifdef Q_OS_WIN32
2010-02-21 21:49:38 +01:00
engine_ = new PhononEngine;
#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)));
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());
2010-02-03 22:48:00 +01:00
emit InitFinished();
2009-12-24 20:16:07 +01:00
}
void Player::ReloadSettings() {
2010-02-03 22:48:00 +01:00
if (!init_engine_.isFinished())
return;
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);
}
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() {
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:
// 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);
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);
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();
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);
}