mpd/src/PlaylistControl.cxx

265 lines
5.7 KiB
C++
Raw Normal View History

/*
2013-01-04 20:50:00 +01:00
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* Functions for controlling playback on the playlist level.
*
*/
#include "config.h"
2013-01-07 10:55:05 +01:00
#include "Playlist.hxx"
#include "PlayerControl.hxx"
2013-07-28 13:25:12 +02:00
#include "Song.hxx"
2013-01-04 20:50:00 +01:00
#include <glib.h>
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "playlist"
void
2013-01-07 10:55:05 +01:00
playlist::Stop(player_control &pc)
{
2013-01-07 10:55:05 +01:00
if (!playing)
return;
2013-01-07 10:55:05 +01:00
assert(current >= 0);
g_debug("stop");
pc.Stop();
2013-01-07 10:55:05 +01:00
queued = -1;
playing = false;
2013-01-07 10:55:05 +01:00
if (queue.random) {
/* shuffle the playlist, so the next playback will
result in a new random order */
2013-01-07 10:55:05 +01:00
unsigned current_position = queue.OrderToPosition(current);
2013-01-07 10:55:05 +01:00
queue.ShuffleOrder();
/* make sure that "current" stays valid, and the next
"play" command plays the same song again */
2013-01-07 10:55:05 +01:00
current = queue.PositionToOrder(current_position);
}
}
enum playlist_result
2013-01-07 10:55:05 +01:00
playlist::PlayPosition(player_control &pc, int song)
{
pc.ClearError();
2013-01-07 10:55:05 +01:00
unsigned i = song;
if (song == -1) {
/* play any song ("current" song, or the first song */
2013-01-07 10:55:05 +01:00
if (queue.IsEmpty())
return PLAYLIST_RESULT_SUCCESS;
2013-01-07 10:55:05 +01:00
if (playing) {
/* already playing: unpause playback, just in
case it was paused, and return */
pc.SetPause(false);
return PLAYLIST_RESULT_SUCCESS;
}
/* select a song: "current" song, or the first one */
2013-01-07 10:55:05 +01:00
i = current >= 0
? current
: 0;
2013-01-07 10:55:05 +01:00
} else if (!queue.IsValidPosition(song))
return PLAYLIST_RESULT_BAD_RANGE;
2013-01-07 10:55:05 +01:00
if (queue.random) {
if (song >= 0)
/* "i" is currently the song position (which
would be equal to the order number in
no-random mode); convert it to a order
number, because random mode is enabled */
2013-01-07 10:55:05 +01:00
i = queue.PositionToOrder(song);
2013-01-07 10:55:05 +01:00
if (!playing)
current = 0;
/* swap the new song with the previous "current" one,
so playback continues as planned */
2013-01-07 10:55:05 +01:00
queue.SwapOrders(i, current);
i = current;
}
2013-01-07 10:55:05 +01:00
stop_on_error = false;
error_count = 0;
2013-01-07 10:55:05 +01:00
PlayOrder(pc, i);
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result
2013-01-07 10:55:05 +01:00
playlist::PlayId(player_control &pc, int id)
{
2013-01-07 10:55:05 +01:00
if (id == -1)
return PlayPosition(pc, id);
2013-01-07 10:55:05 +01:00
int song = queue.IdToPosition(id);
if (song < 0)
return PLAYLIST_RESULT_NO_SUCH_SONG;
2013-01-07 10:55:05 +01:00
return PlayPosition(pc, song);
}
void
2013-01-07 10:55:05 +01:00
playlist::PlayNext(player_control &pc)
{
2013-01-07 10:55:05 +01:00
if (!playing)
return;
2013-01-07 10:55:05 +01:00
assert(!queue.IsEmpty());
assert(queue.IsValidOrder(current));
2013-01-07 10:55:05 +01:00
const int old_current = current;
stop_on_error = false;
/* determine the next song from the queue's order list */
2013-01-07 10:55:05 +01:00
const int next_order = queue.GetNextOrder(current);
if (next_order < 0) {
/* no song after this one: stop playback */
2013-01-07 10:55:05 +01:00
Stop(pc);
/* reset "current song" */
2013-01-07 10:55:05 +01:00
current = -1;
}
else
{
2013-01-07 10:55:05 +01:00
if (next_order == 0 && queue.random) {
/* The queue told us that the next song is the first
song. This means we are in repeat mode. Shuffle
the queue order, so this time, the user hears the
songs in a different than before */
2013-01-07 10:55:05 +01:00
assert(queue.repeat);
2013-01-07 10:55:05 +01:00
queue.ShuffleOrder();
2013-01-07 10:55:05 +01:00
/* note that current and queued are
now invalid, but playlist_play_order() will
discard them anyway */
}
2013-01-07 10:55:05 +01:00
PlayOrder(pc, next_order);
}
/* Consume mode removes each played songs. */
2013-01-07 10:55:05 +01:00
if (queue.consume)
DeleteOrder(pc, old_current);
}
void
2013-01-07 10:55:05 +01:00
playlist::PlayPrevious(player_control &pc)
{
2013-01-07 10:55:05 +01:00
if (!playing)
return;
2013-01-07 10:55:05 +01:00
assert(!queue.IsEmpty());
2013-01-07 10:55:05 +01:00
int order;
if (current > 0) {
/* play the preceding song */
2013-01-07 10:55:05 +01:00
order = current - 1;
} else if (queue.repeat) {
/* play the last song in "repeat" mode */
2013-01-07 10:55:05 +01:00
order = queue.GetLength() - 1;
} else {
/* re-start playing the current song if it's
the first one */
2013-01-07 10:55:05 +01:00
order = current;
}
2013-01-07 10:55:05 +01:00
PlayOrder(pc, order);
}
enum playlist_result
2013-01-07 10:55:05 +01:00
playlist::SeekSongPosition(player_control &pc, unsigned song, float seek_time)
{
2013-01-07 10:55:05 +01:00
if (!queue.IsValidPosition(song))
return PLAYLIST_RESULT_BAD_RANGE;
2013-07-28 13:25:12 +02:00
const Song *queued_song = GetQueuedSong();
2013-01-07 10:55:05 +01:00
unsigned i = queue.random
? queue.PositionToOrder(song)
: song;
pc.ClearError();
2013-01-07 10:55:05 +01:00
stop_on_error = true;
error_count = 0;
2013-01-07 10:55:05 +01:00
if (!playing || (unsigned)current != i) {
/* seeking is not within the current song - prepare
song change */
2013-01-07 10:55:05 +01:00
playing = true;
current = i;
2013-01-07 10:55:05 +01:00
queued_song = nullptr;
}
2013-07-28 13:25:12 +02:00
Song *the_song = queue.GetOrder(i)->DupDetached();
if (!pc.Seek(the_song, seek_time)) {
2013-01-07 10:55:05 +01:00
UpdateQueuedSong(pc, queued_song);
return PLAYLIST_RESULT_NOT_PLAYING;
}
2013-01-07 10:55:05 +01:00
queued = -1;
UpdateQueuedSong(pc, NULL);
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result
2013-01-07 10:55:05 +01:00
playlist::SeekSongId(player_control &pc, unsigned id, float seek_time)
{
2013-01-07 10:55:05 +01:00
int song = queue.IdToPosition(id);
if (song < 0)
return PLAYLIST_RESULT_NO_SUCH_SONG;
2013-01-07 10:55:05 +01:00
return SeekSongPosition(pc, song, seek_time);
}
enum playlist_result
2013-01-07 10:55:05 +01:00
playlist::SeekCurrent(player_control &pc, float seek_time, bool relative)
{
2013-01-07 10:55:05 +01:00
if (!playing)
return PLAYLIST_RESULT_NOT_PLAYING;
if (relative) {
const auto status = pc.GetStatus();
if (status.state != PlayerState::PLAY &&
status.state != PlayerState::PAUSE)
return PLAYLIST_RESULT_NOT_PLAYING;
seek_time += (int)status.elapsed_time;
}
if (seek_time < 0)
seek_time = 0;
2013-01-07 10:55:05 +01:00
return SeekSongPosition(pc, current, seek_time);
}