2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2009-2012, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2010-2011, 2014, Arnaud Bienner <arnaud.bienner@gmail.com>
|
|
|
|
Copyright 2010-2012, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2011, Paweł Bara <keirangtp@gmail.com>
|
|
|
|
Copyright 2011, Andrea Decorte <adecorte@gmail.com>
|
|
|
|
Copyright 2012, Anand <anandtp@live.in>
|
|
|
|
Copyright 2012, Arash Abedinzadeh <arash.abedinzadeh@gmail.com>
|
|
|
|
Copyright 2013, Andreas <asfa194@gmail.com>
|
|
|
|
Copyright 2013, Kevin Cox <kevincox.ca@gmail.com>
|
|
|
|
Copyright 2014, Mark Furneaux <mark@romaco.ca>
|
2014-11-02 19:40:52 +01:00
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
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"
|
2014-02-06 14:48:00 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2015-01-08 04:10:35 +01:00
|
|
|
#include <QSettings>
|
2014-02-06 14:48:00 +01:00
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
#include <QtDebug>
|
|
|
|
#include <QtConcurrentRun>
|
|
|
|
|
|
|
|
#include "config.h"
|
2012-02-12 14:41:50 +01:00
|
|
|
#include "core/application.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2011-04-28 17:10:28 +02:00
|
|
|
#include "core/urlhandler.h"
|
2010-04-15 14:39:34 +02:00
|
|
|
#include "engines/enginebase.h"
|
2010-12-26 14:01:35 +01:00
|
|
|
#include "engines/gstengine.h"
|
2010-12-25 14:37:45 +01:00
|
|
|
#include "library/librarybackend.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "playlist/playlist.h"
|
2010-05-20 23:21:55 +02:00
|
|
|
#include "playlist/playlistitem.h"
|
|
|
|
#include "playlist/playlistmanager.h"
|
2010-04-15 14:39:34 +02:00
|
|
|
|
2010-12-18 18:28:02 +01:00
|
|
|
#ifdef HAVE_LIBLASTFM
|
2014-12-18 23:35:21 +01:00
|
|
|
#include "internet/lastfm/lastfmservice.h"
|
2010-12-18 18:28:02 +01:00
|
|
|
#endif
|
2010-03-24 22:07:16 +01:00
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
using std::shared_ptr;
|
2010-03-24 21:58:17 +01:00
|
|
|
|
2015-01-06 05:33:47 +01:00
|
|
|
const char* Player::kSettingsGroup = "Player";
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
Player::Player(Application* app, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: PlayerInterface(parent),
|
|
|
|
app_(app),
|
|
|
|
lastfm_(nullptr),
|
|
|
|
engine_(new GstEngine(app_->task_manager())),
|
|
|
|
stream_change_type_(Engine::First),
|
|
|
|
last_state_(Engine::Empty),
|
|
|
|
nb_errors_received_(0),
|
2015-01-06 05:33:47 +01:00
|
|
|
volume_before_mute_(50),
|
2015-01-08 04:10:35 +01:00
|
|
|
last_pressed_previous_(QDateTime::currentDateTime()),
|
|
|
|
menu_previousmode_(PreviousBehaviour_DontRestart) {
|
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)));
|
2011-03-10 19:01:35 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(engine_.get(), SIGNAL(ValidSongRequested(QUrl)),
|
|
|
|
SLOT(ValidSongRequested(QUrl)));
|
|
|
|
connect(engine_.get(), SIGNAL(InvalidSongRequested(QUrl)),
|
|
|
|
SLOT(InvalidSongRequested(QUrl)));
|
2010-02-03 22:48:00 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
Player::~Player() {}
|
2010-05-28 21:51:51 +02:00
|
|
|
|
2010-02-03 22:48:00 +01:00
|
|
|
void Player::Init() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!engine_->Init()) qFatal("Error initialising audio engine");
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(engine_.get(), SIGNAL(StateChanged(Engine::State)),
|
|
|
|
SLOT(EngineStateChanged(Engine::State)));
|
2010-05-31 22:24:05 +02:00
|
|
|
connect(engine_.get(), SIGNAL(TrackAboutToEnd()), SLOT(TrackAboutToEnd()));
|
|
|
|
connect(engine_.get(), SIGNAL(TrackEnded()), SLOT(TrackEnded()));
|
|
|
|
connect(engine_.get(), SIGNAL(MetaData(Engine::SimpleMetaBundle)),
|
2014-02-07 16:34:20 +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());
|
2011-04-28 17:10:28 +02:00
|
|
|
|
2015-01-08 04:10:35 +01:00
|
|
|
ReloadSettings();
|
|
|
|
|
2011-04-28 17:10:28 +02:00
|
|
|
#ifdef HAVE_LIBLASTFM
|
2014-03-27 18:55:58 +01:00
|
|
|
lastfm_ = app_->scrobbler();
|
2011-04-28 17:10:28 +02:00
|
|
|
#endif
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2015-01-06 05:33:47 +01:00
|
|
|
void Player::ReloadSettings() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
menu_previousmode_ = PreviousBehaviour(
|
|
|
|
s.value("menu_previousmode", PreviousBehaviour_DontRestart).toInt());
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
engine_->ReloadSettings();
|
|
|
|
}
|
2010-02-03 17:51:56 +01:00
|
|
|
|
2011-07-21 01:22:20 +02:00
|
|
|
void Player::HandleLoadResult(const UrlHandler::LoadResult& result) {
|
2010-05-18 22:43:10 +02:00
|
|
|
switch (result.type_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case UrlHandler::LoadResult::NoMoreTracks:
|
|
|
|
qLog(Debug) << "URL handler for" << result.original_url_
|
|
|
|
<< "said no more tracks";
|
2010-05-18 22:43:10 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
loading_async_ = QUrl();
|
|
|
|
NextItem(stream_change_type_);
|
|
|
|
break;
|
2010-05-18 22:43:10 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case UrlHandler::LoadResult::TrackAvailable: {
|
|
|
|
// Might've been an async load, so check we're still on the same item
|
|
|
|
int current_index = app_->playlist_manager()->active()->current_row();
|
|
|
|
if (current_index == -1) return;
|
|
|
|
|
|
|
|
shared_ptr<PlaylistItem> item =
|
|
|
|
app_->playlist_manager()->active()->item_at(current_index);
|
|
|
|
if (!item || item->Url() != result.original_url_) return;
|
|
|
|
|
|
|
|
qLog(Debug) << "URL handler for" << result.original_url_ << "returned"
|
|
|
|
<< result.media_url_;
|
|
|
|
|
|
|
|
// If there was no length info in song's metadata, use the one provided by
|
|
|
|
// URL handler, if there is one
|
|
|
|
if (item->Metadata().length_nanosec() <= 0 &&
|
|
|
|
result.length_nanosec_ != -1) {
|
|
|
|
Song song = item->Metadata();
|
|
|
|
song.set_length_nanosec(result.length_nanosec_);
|
|
|
|
item->SetTemporaryMetadata(song);
|
|
|
|
app_->playlist_manager()->active()->InformOfCurrentSongChange();
|
|
|
|
}
|
|
|
|
engine_->Play(
|
|
|
|
result.media_url_, stream_change_type_, item->Metadata().has_cue(),
|
|
|
|
item->Metadata().beginning_nanosec(), item->Metadata().end_nanosec());
|
2011-04-28 17:10:28 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
current_item_ = item;
|
|
|
|
loading_async_ = QUrl();
|
|
|
|
break;
|
2011-10-02 12:05:56 +02:00
|
|
|
}
|
2010-05-18 22:43:10 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case UrlHandler::LoadResult::WillLoadAsynchronously:
|
|
|
|
qLog(Debug) << "URL handler for" << result.original_url_
|
|
|
|
<< "is loading asynchronously";
|
2011-04-28 17:10:28 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
// We'll get called again later with either NoMoreTracks or TrackAvailable
|
|
|
|
loading_async_ = result.original_url_;
|
|
|
|
break;
|
2010-05-18 22:43:10 +02:00
|
|
|
}
|
2010-04-12 03:59:21 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void Player::Next() { NextInternal(Engine::Manual); }
|
2010-04-30 01:30:24 +02:00
|
|
|
|
2011-03-13 19:37:46 +01:00
|
|
|
void Player::NextInternal(Engine::TrackChangeFlags change) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (HandleStopAfter()) return;
|
2010-08-26 21:29:55 +02:00
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
if (app_->playlist_manager()->active()->current_item()) {
|
|
|
|
const QUrl url = app_->playlist_manager()->active()->current_item()->Url();
|
2010-05-18 22:43:10 +02:00
|
|
|
|
2011-04-28 17:10:28 +02:00
|
|
|
if (url_handlers_.contains(url.scheme())) {
|
|
|
|
// The next track is already being loaded
|
2014-02-07 16:34:20 +01:00
|
|
|
if (url == loading_async_) return;
|
2011-04-28 17:10:28 +02:00
|
|
|
|
|
|
|
stream_change_type_ = change;
|
|
|
|
HandleLoadResult(url_handlers_[url.scheme()]->LoadNext(url));
|
|
|
|
return;
|
|
|
|
}
|
2009-12-29 21:48:50 +01:00
|
|
|
}
|
|
|
|
|
2010-05-08 19:39:12 +02:00
|
|
|
NextItem(change);
|
2010-02-04 00:12:21 +01:00
|
|
|
}
|
|
|
|
|
2011-03-13 19:37:46 +01:00
|
|
|
void Player::NextItem(Engine::TrackChangeFlags change) {
|
2014-02-04 22:08:32 +01:00
|
|
|
Playlist* active_playlist = app_->playlist_manager()->active();
|
|
|
|
|
|
|
|
// If we received too many errors in auto change, with repeat enabled, we stop
|
|
|
|
if (change == Engine::Auto) {
|
|
|
|
const PlaylistSequence::RepeatMode repeat_mode =
|
|
|
|
active_playlist->sequence()->repeat_mode();
|
|
|
|
if (repeat_mode != PlaylistSequence::Repeat_Off) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if ((repeat_mode == PlaylistSequence::Repeat_Track &&
|
|
|
|
nb_errors_received_ >= 3) ||
|
|
|
|
(nb_errors_received_ >=
|
|
|
|
app_->playlist_manager()->active()->proxy()->rowCount())) {
|
2014-02-04 22:08:32 +01:00
|
|
|
// We received too many "Error" state changes: probably looping over a
|
|
|
|
// playlist which contains only unavailable elements: stop now.
|
|
|
|
nb_errors_received_ = 0;
|
|
|
|
Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-27 22:02:37 +01:00
|
|
|
// Manual track changes override "Repeat track"
|
|
|
|
const bool ignore_repeat_track = change & Engine::Manual;
|
|
|
|
|
2014-02-04 22:08:32 +01:00
|
|
|
int i = active_playlist->next_row(ignore_repeat_track);
|
2009-12-24 20:16:07 +01:00
|
|
|
if (i == -1) {
|
2012-02-12 14:41:50 +01:00
|
|
|
app_->playlist_manager()->active()->set_current_row(i);
|
2010-04-19 15:01:57 +02:00
|
|
|
emit PlaylistFinished();
|
2009-12-24 20:16:07 +01:00
|
|
|
Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-30 01:30:24 +02:00
|
|
|
PlayAt(i, change, false);
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 00:31:15 +02:00
|
|
|
bool Player::HandleStopAfter() {
|
2012-02-12 14:41:50 +01:00
|
|
|
if (app_->playlist_manager()->active()->stop_after_current()) {
|
2011-09-28 00:31:15 +02:00
|
|
|
// Find what the next track would've been, and mark that one as current
|
|
|
|
// so it plays next time the user presses Play.
|
2012-02-12 14:41:50 +01:00
|
|
|
const int next_row = app_->playlist_manager()->active()->next_row();
|
2011-09-28 00:31:15 +02:00
|
|
|
if (next_row != -1) {
|
2014-04-26 04:45:25 +02:00
|
|
|
app_->playlist_manager()->active()->set_current_row(next_row, true);
|
2011-09-28 00:31:15 +02:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:30:31 +02:00
|
|
|
app_->playlist_manager()->active()->StopAfter(-1);
|
|
|
|
|
2014-04-26 06:58:08 +02:00
|
|
|
Stop(true);
|
2011-09-28 00:31:15 +02:00
|
|
|
return true;
|
2009-12-26 23:59:11 +01:00
|
|
|
}
|
2011-09-28 00:31:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::TrackEnded() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (HandleStopAfter()) return;
|
2009-12-26 23:59:11 +01:00
|
|
|
|
2010-12-25 14:37:45 +01:00
|
|
|
if (current_item_ && current_item_->IsLocalLibraryItem() &&
|
2011-04-17 16:11:37 +02:00
|
|
|
current_item_->Metadata().id() != -1 &&
|
2012-02-12 14:41:50 +01:00
|
|
|
!app_->playlist_manager()->active()->have_incremented_playcount() &&
|
2014-02-07 16:34:20 +01:00
|
|
|
app_->playlist_manager()->active()->get_lastfm_status() !=
|
|
|
|
Playlist::LastFM_Seeked) {
|
2010-12-25 14:37:45 +01:00
|
|
|
// The track finished before its scrobble point (30 seconds), so increment
|
|
|
|
// the play count now.
|
2012-02-12 14:41:50 +01:00
|
|
|
app_->playlist_manager()->library_backend()->IncrementPlayCountAsync(
|
2010-12-25 14:37:45 +01:00
|
|
|
current_item_->Metadata().id());
|
|
|
|
}
|
|
|
|
|
2010-04-30 01:30:24 +02:00
|
|
|
NextInternal(Engine::Auto);
|
2009-12-26 23:59:11 +01:00
|
|
|
}
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
void Player::PlayPause() {
|
|
|
|
switch (engine_->state()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case Engine::Paused:
|
|
|
|
engine_->Unpause();
|
|
|
|
break;
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case Engine::Playing: {
|
|
|
|
// We really shouldn't pause last.fm streams
|
|
|
|
// 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) {
|
|
|
|
Stop();
|
|
|
|
} else {
|
|
|
|
engine_->Pause();
|
|
|
|
}
|
2010-04-12 02:40:03 +02:00
|
|
|
break;
|
2014-02-07 16:34:20 +01:00
|
|
|
}
|
2010-04-12 02:40:03 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case Engine::Empty:
|
|
|
|
case Engine::Error:
|
|
|
|
case Engine::Idle: {
|
|
|
|
app_->playlist_manager()->SetActivePlaylist(
|
|
|
|
app_->playlist_manager()->current_id());
|
|
|
|
if (app_->playlist_manager()->active()->rowCount() == 0) break;
|
2010-04-12 02:40:03 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int i = app_->playlist_manager()->active()->current_row();
|
|
|
|
if (i == -1) i = app_->playlist_manager()->active()->last_played_row();
|
|
|
|
if (i == -1) i = 0;
|
|
|
|
|
|
|
|
PlayAt(i, Engine::First, true);
|
|
|
|
break;
|
|
|
|
}
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:27:07 +02:00
|
|
|
void Player::RestartOrPrevious() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (engine_->position_nanosec() < 8 * kNsecPerSec) return Previous();
|
2013-05-29 21:27:07 +02:00
|
|
|
|
|
|
|
SeekTo(0);
|
|
|
|
}
|
|
|
|
|
2014-04-26 06:58:08 +02:00
|
|
|
void Player::Stop(bool stop_after) {
|
|
|
|
engine_->Stop(stop_after);
|
2012-02-12 14:41:50 +01:00
|
|
|
app_->playlist_manager()->active()->set_current_row(-1);
|
2010-05-20 23:21:55 +02:00
|
|
|
current_item_.reset();
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2013-07-20 19:38:37 +02:00
|
|
|
void Player::StopAfterCurrent() {
|
2014-02-07 16:34:20 +01:00
|
|
|
app_->playlist_manager()->active()->StopAfter(
|
|
|
|
app_->playlist_manager()->active()->current_row());
|
2013-07-20 19:38:37 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void Player::Previous() { PreviousItem(Engine::Manual); }
|
2012-08-26 14:10:38 +02:00
|
|
|
|
|
|
|
void Player::PreviousItem(Engine::TrackChangeFlags change) {
|
|
|
|
const bool ignore_repeat_track = change & Engine::Manual;
|
|
|
|
|
2015-01-06 05:33:47 +01:00
|
|
|
if (menu_previousmode_ == PreviousBehaviour_Restart) {
|
|
|
|
// Check if it has been over two seconds since previous button was pressed
|
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
if (last_pressed_previous_.isValid() &&
|
|
|
|
last_pressed_previous_.secsTo(now) >= 2) {
|
|
|
|
last_pressed_previous_ = now;
|
|
|
|
PlayAt(app_->playlist_manager()->active()->current_row(), change, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last_pressed_previous_ = now;
|
|
|
|
}
|
|
|
|
|
2012-08-26 14:10:38 +02:00
|
|
|
int i = app_->playlist_manager()->active()->previous_row(ignore_repeat_track);
|
2012-02-12 14:41:50 +01:00
|
|
|
app_->playlist_manager()->active()->set_current_row(i);
|
2009-12-24 20:16:07 +01:00
|
|
|
if (i == -1) {
|
|
|
|
Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-26 14:10:38 +02:00
|
|
|
PlayAt(i, change, false);
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::EngineStateChanged(Engine::State state) {
|
2014-02-04 22:08:32 +01:00
|
|
|
if (Engine::Error == state) {
|
|
|
|
nb_errors_received_++;
|
|
|
|
} else {
|
|
|
|
nb_errors_received_ = 0;
|
|
|
|
}
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
switch (state) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case Engine::Paused:
|
|
|
|
emit Paused();
|
|
|
|
break;
|
|
|
|
case Engine::Playing:
|
|
|
|
emit Playing();
|
|
|
|
break;
|
2014-02-04 22:08:32 +01:00
|
|
|
case Engine::Error:
|
2009-12-24 20:16:07 +01:00
|
|
|
case Engine::Empty:
|
2014-02-07 16:34:20 +01:00
|
|
|
case Engine::Idle:
|
|
|
|
emit Stopped();
|
|
|
|
break;
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
2010-12-11 14:38:51 +01:00
|
|
|
last_state_ = state;
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::SetVolume(int value) {
|
2010-04-13 01:35:47 +02:00
|
|
|
int old_volume = engine_->volume();
|
|
|
|
|
2010-03-24 23:31:34 +01:00
|
|
|
int volume = qBound(0, value, 100);
|
2010-03-24 23:29:17 +01:00
|
|
|
settings_.setValue("volume", volume);
|
2010-04-12 01:24:03 +02:00
|
|
|
engine_->SetVolume(volume);
|
2010-04-13 01:35:47 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (volume != old_volume) {
|
2010-04-13 01:35:47 +02:00
|
|
|
emit VolumeChanged(volume);
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int Player::GetVolume() const { return engine_->volume(); }
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void Player::PlayAt(int index, Engine::TrackChangeFlags change,
|
|
|
|
bool reshuffle) {
|
|
|
|
if (change == Engine::Manual &&
|
|
|
|
engine_->position_nanosec() != engine_->length_nanosec()) {
|
2010-12-10 01:11:38 +01:00
|
|
|
emit TrackSkipped(current_item_);
|
2011-10-02 12:05:56 +02:00
|
|
|
const QUrl& url = current_item_->Url();
|
|
|
|
if (url_handlers_.contains(url.scheme())) {
|
|
|
|
url_handlers_[url.scheme()]->TrackSkipped();
|
|
|
|
}
|
2010-12-10 01:11:38 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (current_item_ && app_->playlist_manager()->active()->has_item_at(index) &&
|
2012-05-07 12:26:13 +02:00
|
|
|
current_item_->Metadata().IsOnSameAlbum(
|
2014-02-07 16:34:20 +01:00
|
|
|
app_->playlist_manager()->active()->item_at(index)->Metadata())) {
|
2011-03-13 19:37:46 +01:00
|
|
|
change |= Engine::SameAlbum;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (reshuffle) app_->playlist_manager()->active()->ReshuffleIndices();
|
2012-02-12 14:41:50 +01:00
|
|
|
app_->playlist_manager()->active()->set_current_row(index);
|
2009-12-26 22:35:45 +01:00
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
if (app_->playlist_manager()->active()->current_row() == -1) {
|
2011-11-27 23:24:38 +01:00
|
|
|
// Maybe index didn't exist in the playlist.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
current_item_ = app_->playlist_manager()->active()->current_item();
|
2011-04-28 17:10:28 +02:00
|
|
|
const QUrl url = current_item_->Url();
|
2009-12-26 22:35:45 +01:00
|
|
|
|
2011-04-28 17:10:28 +02:00
|
|
|
if (url_handlers_.contains(url.scheme())) {
|
2010-05-18 22:43:10 +02:00
|
|
|
// It's already loading
|
2014-02-07 16:34:20 +01:00
|
|
|
if (url == loading_async_) return;
|
2010-05-18 22:43:10 +02:00
|
|
|
|
2010-06-12 22:22:58 +02:00
|
|
|
stream_change_type_ = change;
|
2011-04-28 17:10:28 +02:00
|
|
|
HandleLoadResult(url_handlers_[url.scheme()]->StartLoading(url));
|
|
|
|
} else {
|
2010-05-18 22:43:10 +02:00
|
|
|
loading_async_ = QUrl();
|
2011-01-02 19:53:45 +01:00
|
|
|
engine_->Play(current_item_->Url(), change,
|
2011-03-29 00:11:07 +02:00
|
|
|
current_item_->Metadata().has_cue(),
|
2011-02-13 19:34:30 +01:00
|
|
|
current_item_->Metadata().beginning_nanosec(),
|
|
|
|
current_item_->Metadata().end_nanosec());
|
2009-12-29 21:48:50 +01:00
|
|
|
|
2010-12-18 18:28:02 +01:00
|
|
|
#ifdef HAVE_LIBLASTFM
|
2009-12-29 21:48:50 +01:00
|
|
|
if (lastfm_->IsScrobblingEnabled())
|
2010-05-20 23:21:55 +02:00
|
|
|
lastfm_->NowPlaying(current_item_->Metadata());
|
2010-12-18 18:28:02 +01:00
|
|
|
#endif
|
2009-12-29 20:22:02 +01:00
|
|
|
}
|
2009-12-26 22:35:45 +01:00
|
|
|
}
|
|
|
|
|
2010-11-21 16:13:26 +01:00
|
|
|
void Player::CurrentMetadataChanged(const Song& metadata) {
|
2011-03-10 19:01:35 +01:00
|
|
|
// those things might have changed (especially when a previously invalid
|
|
|
|
// song was reloaded) so we push the latest version into Engine
|
2011-03-12 21:20:13 +01:00
|
|
|
engine_->RefreshMarkers(metadata.beginning_nanosec(), metadata.end_nanosec());
|
2011-03-10 19:01:35 +01:00
|
|
|
|
2010-12-18 18:28:02 +01:00
|
|
|
#ifdef HAVE_LIBLASTFM
|
2010-03-08 00:28:40 +01:00
|
|
|
lastfm_->NowPlaying(metadata);
|
2010-12-18 18:28:02 +01:00
|
|
|
#endif
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
2010-01-15 18:12:47 +01:00
|
|
|
|
2011-02-13 19:36:29 +01:00
|
|
|
void Player::SeekTo(int seconds) {
|
2012-07-10 15:59:35 +02:00
|
|
|
const qint64 length_nanosec = engine_->length_nanosec();
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-07-10 15:59:35 +02:00
|
|
|
// If the length is 0 then either there is no song playing, or the song isn't
|
|
|
|
// seekable.
|
2012-07-15 13:17:44 +02:00
|
|
|
if (length_nanosec <= 0) {
|
2012-07-10 15:59:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
|
|
|
|
const qint64 nanosec =
|
|
|
|
qBound(0ll, qint64(seconds) * kNsecPerSec, length_nanosec);
|
2011-02-13 19:29:27 +01:00
|
|
|
engine_->Seek(nanosec);
|
2010-03-24 15:21:26 +01:00
|
|
|
|
|
|
|
// If we seek the track we don't want to submit it to last.fm
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Info) << "Track seeked to" << nanosec << "ns - not scrobbling";
|
2014-02-07 16:34:20 +01:00
|
|
|
if (app_->playlist_manager()->active()->get_lastfm_status() ==
|
|
|
|
Playlist::LastFM_New) {
|
|
|
|
app_->playlist_manager()->active()->set_lastfm_status(
|
|
|
|
Playlist::LastFM_Seeked);
|
2011-04-07 18:25:52 +02:00
|
|
|
}
|
2011-01-06 22:08:11 +01:00
|
|
|
|
2011-02-13 19:29:27 +01:00
|
|
|
emit Seeked(nanosec / 1000);
|
2010-01-15 18:12:47 +01:00
|
|
|
}
|
2010-02-03 23:20:31 +01:00
|
|
|
|
2011-01-19 17:05:16 +01:00
|
|
|
void Player::SeekForward() {
|
2011-03-21 16:15:17 +01:00
|
|
|
SeekTo(engine()->position_nanosec() / kNsecPerSec + 10);
|
2011-01-19 17:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::SeekBackward() {
|
2011-03-21 16:15:17 +01:00
|
|
|
SeekTo(engine()->position_nanosec() / kNsecPerSec - 10);
|
2011-01-19 17:05:16 +01:00
|
|
|
}
|
|
|
|
|
2010-02-03 23:20:31 +01:00
|
|
|
void Player::EngineMetadataReceived(const Engine::SimpleMetaBundle& bundle) {
|
2012-02-12 14:41:50 +01:00
|
|
|
PlaylistItemPtr item = app_->playlist_manager()->active()->current_item();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!item) return;
|
2010-02-03 23:20:31 +01:00
|
|
|
|
2010-07-10 21:51:34 +02:00
|
|
|
Engine::SimpleMetaBundle bundle_copy = bundle;
|
|
|
|
|
|
|
|
// Maybe the metadata is from icycast and has "Artist - Title" shoved
|
|
|
|
// together in the title field.
|
2011-07-26 16:56:19 +02:00
|
|
|
const int dash_pos = bundle_copy.title.indexOf('-');
|
2010-07-10 21:51:34 +02:00
|
|
|
if (dash_pos != -1 && bundle_copy.artist.isEmpty()) {
|
2011-07-26 16:56:19 +02:00
|
|
|
// Split on " - " if it exists, otherwise split on "-".
|
|
|
|
const int space_dash_pos = bundle_copy.title.indexOf(" - ");
|
|
|
|
if (space_dash_pos != -1) {
|
|
|
|
bundle_copy.artist = bundle_copy.title.left(space_dash_pos).trimmed();
|
2014-02-07 16:34:20 +01:00
|
|
|
bundle_copy.title = bundle_copy.title.mid(space_dash_pos + 3).trimmed();
|
2011-07-26 16:56:19 +02:00
|
|
|
} else {
|
|
|
|
bundle_copy.artist = bundle_copy.title.left(dash_pos).trimmed();
|
2014-02-07 16:34:20 +01:00
|
|
|
bundle_copy.title = bundle_copy.title.mid(dash_pos + 1).trimmed();
|
2011-07-26 16:56:19 +02:00
|
|
|
}
|
2010-07-15 14:59:14 +02:00
|
|
|
}
|
|
|
|
|
2010-04-21 16:04:40 +02:00
|
|
|
Song song = item->Metadata();
|
2010-07-10 21:51:34 +02:00
|
|
|
song.MergeFromSimpleMetaBundle(bundle_copy);
|
2010-02-03 23:20:31 +01:00
|
|
|
|
2010-02-04 00:56:41 +01:00
|
|
|
// Ignore useless metadata
|
2014-02-07 16:34:20 +01:00
|
|
|
if (song.title().isEmpty() && song.artist().isEmpty()) return;
|
2010-02-04 00:56:41 +01:00
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
app_->playlist_manager()->active()->SetStreamMetadata(item->Url(), song);
|
2010-02-03 23:20:31 +01:00
|
|
|
}
|
2010-03-24 21:58:17 +01:00
|
|
|
|
2010-11-21 16:13:26 +01:00
|
|
|
PlaylistItemPtr Player::GetItemAt(int pos) const {
|
2012-02-12 14:41:50 +01:00
|
|
|
if (pos < 0 || pos >= app_->playlist_manager()->active()->rowCount())
|
2010-11-21 16:13:26 +01:00
|
|
|
return PlaylistItemPtr();
|
2012-02-12 14:41:50 +01:00
|
|
|
return app_->playlist_manager()->active()->item_at(pos);
|
2010-03-24 21:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::Mute() {
|
2010-07-14 00:22:04 +02:00
|
|
|
const int current_volume = engine_->volume();
|
|
|
|
|
|
|
|
if (current_volume == 0) {
|
|
|
|
SetVolume(volume_before_mute_);
|
|
|
|
} else {
|
|
|
|
volume_before_mute_ = current_volume;
|
|
|
|
SetVolume(0);
|
|
|
|
}
|
2010-03-24 21:58:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void Player::Pause() { engine_->Pause(); }
|
2010-03-24 21:58:17 +01:00
|
|
|
|
|
|
|
void Player::Play() {
|
|
|
|
switch (GetState()) {
|
|
|
|
case Engine::Playing:
|
2011-02-13 19:36:29 +01:00
|
|
|
SeekTo(0);
|
2010-03-24 21:58:17 +01:00
|
|
|
break;
|
|
|
|
case Engine::Paused:
|
2010-04-12 01:24:03 +02:00
|
|
|
engine_->Unpause();
|
2010-03-24 21:58:17 +01:00
|
|
|
break;
|
|
|
|
default:
|
2010-04-13 22:22:29 +02:00
|
|
|
PlayPause();
|
2010-03-24 21:58:17 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::ShowOSD() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (current_item_) emit ForceShowOSD(current_item_->Metadata(), false);
|
2011-06-05 10:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::TogglePrettyOSD() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (current_item_) emit ForceShowOSD(current_item_->Metadata(), true);
|
2010-03-24 21:58:17 +01:00
|
|
|
}
|
|
|
|
|
2010-04-21 15:55:30 +02:00
|
|
|
void Player::TrackAboutToEnd() {
|
2011-08-28 03:59:19 +02:00
|
|
|
// If the current track was from a URL handler then it might have special
|
|
|
|
// behaviour to queue up a subsequent track. We don't want to preload (and
|
|
|
|
// scrobble) the next item in the playlist if it's just going to be stopped
|
|
|
|
// again immediately after.
|
2012-02-12 14:41:50 +01:00
|
|
|
if (app_->playlist_manager()->active()->current_item()) {
|
|
|
|
const QUrl url = app_->playlist_manager()->active()->current_item()->Url();
|
2011-10-02 12:05:56 +02:00
|
|
|
if (url_handlers_.contains(url.scheme())) {
|
|
|
|
url_handlers_[url.scheme()]->TrackAboutToEnd();
|
2011-08-28 03:59:19 +02:00
|
|
|
return;
|
2011-10-02 12:05:56 +02:00
|
|
|
}
|
2011-08-28 03:59:19 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const bool has_next_row =
|
|
|
|
app_->playlist_manager()->active()->next_row() != -1;
|
2011-03-14 21:04:33 +01:00
|
|
|
PlaylistItemPtr next_item;
|
|
|
|
|
|
|
|
if (has_next_row) {
|
2014-02-07 16:34:20 +01:00
|
|
|
next_item = app_->playlist_manager()->active()->item_at(
|
|
|
|
app_->playlist_manager()->active()->next_row());
|
2011-03-14 21:04:33 +01:00
|
|
|
}
|
2011-03-13 19:37:46 +01:00
|
|
|
|
2011-03-13 19:37:39 +01:00
|
|
|
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
|
2010-12-07 21:29:13 +01:00
|
|
|
|
|
|
|
// But, if there's no next track and we don't want to fade out, then do
|
|
|
|
// nothing and just let the track finish to completion.
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!engine_->is_fadeout_enabled() && !has_next_row) return;
|
2010-12-07 21:29:13 +01:00
|
|
|
|
2011-03-13 19:37:46 +01:00
|
|
|
// If the next track is on the same album (or same cue file), and the
|
|
|
|
// user doesn't want to crossfade between tracks on the same album, then
|
|
|
|
// don't do this automatic crossfading.
|
2014-02-07 16:34:20 +01:00
|
|
|
if (engine_->crossfade_same_album() || !has_next_row || !next_item ||
|
2011-03-13 19:37:46 +01:00
|
|
|
!current_item_->Metadata().IsOnSameAlbum(next_item->Metadata())) {
|
|
|
|
TrackEnded();
|
2011-03-13 19:37:39 +01:00
|
|
|
return;
|
2011-03-13 19:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-13 19:37:39 +01:00
|
|
|
|
2011-03-13 19:37:46 +01:00
|
|
|
// Crossfade is off, so start preloading the next track so we don't get a
|
|
|
|
// gap between songs.
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!has_next_row || !next_item) return;
|
2010-05-18 22:43:10 +02:00
|
|
|
|
2011-03-13 19:37:46 +01:00
|
|
|
QUrl url = next_item->Url();
|
2010-05-18 22:43:10 +02:00
|
|
|
|
2011-03-13 19:37:46 +01:00
|
|
|
// Get the actual track URL rather than the stream URL.
|
2011-04-28 17:10:28 +02:00
|
|
|
if (url_handlers_.contains(url.scheme())) {
|
2011-07-21 01:22:20 +02:00
|
|
|
UrlHandler::LoadResult result = url_handlers_[url.scheme()]->LoadNext(url);
|
2011-03-13 19:37:46 +01:00
|
|
|
switch (result.type_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case UrlHandler::LoadResult::NoMoreTracks:
|
|
|
|
return;
|
2010-05-19 15:08:52 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case UrlHandler::LoadResult::WillLoadAsynchronously:
|
|
|
|
loading_async_ = url;
|
|
|
|
return;
|
2011-03-13 19:37:46 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case UrlHandler::LoadResult::TrackAvailable:
|
|
|
|
url = result.media_url_;
|
|
|
|
break;
|
2010-05-18 16:30:55 +02:00
|
|
|
}
|
2010-04-21 15:55:30 +02:00
|
|
|
}
|
2011-03-29 00:11:07 +02:00
|
|
|
engine_->StartPreloading(url, next_item->Metadata().has_cue(),
|
|
|
|
next_item->Metadata().beginning_nanosec(),
|
2011-03-13 19:37:46 +01:00
|
|
|
next_item->Metadata().end_nanosec());
|
2010-04-21 15:55:30 +02:00
|
|
|
}
|
2011-03-10 19:01:35 +01:00
|
|
|
|
|
|
|
void Player::ValidSongRequested(const QUrl& url) {
|
|
|
|
emit SongChangeRequestProcessed(url, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::InvalidSongRequested(const QUrl& url) {
|
|
|
|
// first send the notification to others...
|
|
|
|
emit SongChangeRequestProcessed(url, false);
|
|
|
|
// ... and now when our listeners have completed their processing of the
|
|
|
|
// current item we can change the current item by skipping to the next song
|
|
|
|
NextItem(Engine::Auto);
|
|
|
|
}
|
2011-04-28 17:10:28 +02:00
|
|
|
|
2011-04-28 19:50:45 +02:00
|
|
|
void Player::RegisterUrlHandler(UrlHandler* handler) {
|
2011-04-28 17:10:28 +02:00
|
|
|
const QString scheme = handler->scheme();
|
|
|
|
|
|
|
|
if (url_handlers_.contains(scheme)) {
|
|
|
|
qLog(Warning) << "Tried to register a URL handler for" << scheme
|
|
|
|
<< "but one was already registered";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qLog(Info) << "Registered URL handler for" << scheme;
|
|
|
|
url_handlers_.insert(scheme, handler);
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(handler, SIGNAL(destroyed(QObject*)),
|
|
|
|
SLOT(UrlHandlerDestroyed(QObject*)));
|
2011-07-21 01:22:20 +02:00
|
|
|
connect(handler, SIGNAL(AsyncLoadComplete(UrlHandler::LoadResult)),
|
|
|
|
SLOT(HandleLoadResult(UrlHandler::LoadResult)));
|
2011-04-28 17:10:28 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 19:50:45 +02:00
|
|
|
void Player::UnregisterUrlHandler(UrlHandler* handler) {
|
2011-04-28 17:10:28 +02:00
|
|
|
const QString scheme = url_handlers_.key(handler);
|
|
|
|
if (scheme.isEmpty()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Warning) << "Tried to unregister a URL handler for"
|
|
|
|
<< handler->scheme() << "that wasn't registered";
|
2011-04-28 17:10:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-28 19:50:45 +02:00
|
|
|
qLog(Info) << "Unregistered URL handler for" << scheme;
|
2011-04-28 17:10:28 +02:00
|
|
|
url_handlers_.remove(scheme);
|
2014-02-07 16:34:20 +01:00
|
|
|
disconnect(handler, SIGNAL(destroyed(QObject*)), this,
|
|
|
|
SLOT(UrlHandlerDestroyed(QObject*)));
|
|
|
|
disconnect(handler, SIGNAL(AsyncLoadComplete(UrlHandler::LoadResult)), this,
|
|
|
|
SLOT(HandleLoadResult(UrlHandler::LoadResult)));
|
2011-04-28 17:10:28 +02:00
|
|
|
}
|
|
|
|
|
2012-01-04 12:50:19 +01:00
|
|
|
const UrlHandler* Player::HandlerForUrl(const QUrl& url) const {
|
2014-02-07 16:34:20 +01:00
|
|
|
QMap<QString, UrlHandler*>::const_iterator it =
|
|
|
|
url_handlers_.constFind(url.scheme());
|
2012-01-04 12:50:19 +01:00
|
|
|
if (it == url_handlers_.constEnd()) {
|
2014-02-06 16:49:49 +01:00
|
|
|
return nullptr;
|
2012-01-04 12:50:19 +01:00
|
|
|
}
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
2011-04-28 17:10:28 +02:00
|
|
|
void Player::UrlHandlerDestroyed(QObject* object) {
|
|
|
|
UrlHandler* handler = static_cast<UrlHandler*>(object);
|
|
|
|
const QString scheme = url_handlers_.key(handler);
|
|
|
|
if (!scheme.isEmpty()) {
|
|
|
|
url_handlers_.remove(scheme);
|
|
|
|
}
|
|
|
|
}
|