mpd/src/Playlist.cxx

347 lines
7.6 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.
*/
#include "config.h"
2013-01-07 10:55:05 +01:00
#include "Playlist.hxx"
#include "PlayerControl.hxx"
#include "song.h"
2013-01-09 08:36:52 +01:00
#include "Idle.hxx"
#include <glib.h>
#include <assert.h>
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "playlist"
void
playlist::FullIncrementVersions()
{
queue.ModifyAll();
idle_add(IDLE_PLAYLIST);
}
void
2013-01-07 10:55:05 +01:00
playlist::TagChanged()
{
2013-01-07 10:55:05 +01:00
if (!playing)
return;
2013-01-07 10:55:05 +01:00
assert(current >= 0);
2013-01-07 10:55:05 +01:00
queue.ModifyAtOrder(current);
idle_add(IDLE_PLAYLIST);
}
/**
* Queue a song, addressed by its order number.
*/
static void
playlist_queue_song_order(struct playlist *playlist, struct player_control *pc,
unsigned order)
{
char *uri;
assert(playlist->queue.IsValidOrder(order));
playlist->queued = order;
struct song *song =
song_dup_detached(playlist->queue.GetOrder(order));
uri = song_get_uri(song);
g_debug("queue song %i:\"%s\"", playlist->queued, uri);
g_free(uri);
pc_enqueue_song(pc, song);
}
/**
* Called if the player thread has started playing the "queued" song.
*/
static void
playlist_song_started(struct playlist *playlist, struct player_control *pc)
{
assert(pc->next_song == NULL);
assert(playlist->queued >= -1);
/* queued song has started: copy queued to current,
and notify the clients */
int current = playlist->current;
playlist->current = playlist->queued;
playlist->queued = -1;
if(playlist->queue.consume)
2013-01-07 10:55:05 +01:00
playlist->DeleteOrder(*pc, current);
idle_add(IDLE_PLAYER);
}
const struct song *
2013-01-07 10:55:05 +01:00
playlist::GetQueuedSong() const
{
2013-01-07 10:55:05 +01:00
return playing && queued >= 0
? queue.GetOrder(queued)
: nullptr;
}
void
2013-01-07 10:55:05 +01:00
playlist::UpdateQueuedSong(player_control &pc, const song *prev)
{
2013-01-07 10:55:05 +01:00
if (!playing)
return;
2013-01-07 10:55:05 +01:00
assert(!queue.IsEmpty());
assert((queued < 0) == (prev == NULL));
2013-01-07 10:55:05 +01:00
const int next_order = current >= 0
? queue.GetNextOrder(current)
: 0;
2013-01-07 10:55:05 +01:00
if (next_order == 0 && queue.random && !queue.single) {
/* shuffle the song order again, so we get a different
order each time the playlist is played
completely */
2013-01-07 10:55:05 +01:00
const unsigned current_position =
queue.OrderToPosition(current);
2013-01-07 10:55:05 +01:00
queue.ShuffleOrder();
2013-01-07 10:55:05 +01:00
/* make sure that the current still points to
the current song, after the song order has been
shuffled */
2013-01-07 10:55:05 +01:00
current = queue.PositionToOrder(current_position);
}
2013-01-07 10:55:05 +01:00
const struct song *const next_song = next_order >= 0
? queue.GetOrder(next_order)
: nullptr;
if (prev != NULL && next_song != prev) {
/* clear the currently queued song */
2013-01-07 10:55:05 +01:00
pc_cancel(&pc);
queued = -1;
}
if (next_order >= 0) {
if (next_song != prev)
2013-01-07 10:55:05 +01:00
playlist_queue_song_order(this, &pc, next_order);
else
2013-01-07 10:55:05 +01:00
queued = next_order;
}
}
void
2013-01-07 10:55:05 +01:00
playlist::PlayOrder(player_control &pc, int order)
{
2013-01-07 10:55:05 +01:00
playing = true;
queued = -1;
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
2013-01-07 10:55:05 +01:00
struct song *song = song_dup_detached(queue.GetOrder(order));
2013-01-07 10:55:05 +01:00
char *uri = song_get_uri(song);
g_debug("play %i:\"%s\"", order, uri);
g_free(uri);
2013-01-07 10:55:05 +01:00
pc_play(&pc, song);
current = order;
}
static void
playlist_resume_playback(struct playlist *playlist, struct player_control *pc);
void
2013-01-07 10:55:05 +01:00
playlist::SyncWithPlayer(player_control &pc)
{
2013-01-07 10:55:05 +01:00
if (!playing)
/* this event has reached us out of sync: we aren't
playing anymore; ignore the event */
return;
2013-01-07 10:55:05 +01:00
player_lock(&pc);
const enum player_state pc_state = pc_get_state(&pc);
const song *pc_next_song = pc.next_song;
player_unlock(&pc);
if (pc_state == PLAYER_STATE_STOP)
/* the player thread has stopped: check if playback
should be restarted with the next song. That can
happen if the playlist isn't filling the queue fast
enough */
2013-01-07 10:55:05 +01:00
playlist_resume_playback(this, &pc);
else {
/* check if the player thread has already started
playing the queued song */
2013-01-07 10:55:05 +01:00
if (pc_next_song == nullptr && queued != -1)
playlist_song_started(this, &pc);
2013-01-07 10:55:05 +01:00
player_lock(&pc);
pc_next_song = pc.next_song;
player_unlock(&pc);
/* make sure the queued song is always set (if
possible) */
2013-01-07 10:55:05 +01:00
if (pc_next_song == nullptr && queued < 0)
UpdateQueuedSong(pc, nullptr);
}
}
/**
* The player has stopped for some reason. Check the error, and
* decide whether to re-start playback
*/
static void
playlist_resume_playback(struct playlist *playlist, struct player_control *pc)
{
enum player_error error;
assert(playlist->playing);
assert(pc_get_state(pc) == PLAYER_STATE_STOP);
error = pc_get_error_type(pc);
if (error == PLAYER_ERROR_NONE)
playlist->error_count = 0;
else
++playlist->error_count;
if ((playlist->stop_on_error && error != PLAYER_ERROR_NONE) ||
error == PLAYER_ERROR_OUTPUT ||
playlist->error_count >= playlist->queue.GetLength())
/* too many errors, or critical error: stop
playback */
2013-01-07 10:55:05 +01:00
playlist->Stop(*pc);
else
/* continue playback at the next song */
2013-01-07 10:55:05 +01:00
playlist->PlayNext(*pc);
}
void
2013-01-07 10:55:05 +01:00
playlist::SetRepeat(player_control &pc, bool status)
{
2013-01-07 10:55:05 +01:00
if (status == queue.repeat)
return;
2013-01-07 10:55:05 +01:00
queue.repeat = status;
2013-01-07 10:55:05 +01:00
pc_set_border_pause(&pc, queue.single && !queue.repeat);
/* if the last song is currently being played, the "next song"
might change when repeat mode is toggled */
2013-01-07 10:55:05 +01:00
UpdateQueuedSong(pc, GetQueuedSong());
idle_add(IDLE_OPTIONS);
}
static void
playlist_order(struct playlist *playlist)
{
if (playlist->current >= 0)
/* update playlist.current, order==position now */
playlist->current = playlist->queue.OrderToPosition(playlist->current);
playlist->queue.RestoreOrder();
}
void
2013-01-07 10:55:05 +01:00
playlist::SetSingle(player_control &pc, bool status)
{
2013-01-07 10:55:05 +01:00
if (status == queue.single)
return;
2013-01-07 10:55:05 +01:00
queue.single = status;
2013-01-07 10:55:05 +01:00
pc_set_border_pause(&pc, queue.single && !queue.repeat);
/* if the last song is currently being played, the "next song"
might change when single mode is toggled */
2013-01-07 10:55:05 +01:00
UpdateQueuedSong(pc, GetQueuedSong());
idle_add(IDLE_OPTIONS);
}
void
2013-01-07 10:55:05 +01:00
playlist::SetConsume(bool status)
{
2013-01-07 10:55:05 +01:00
if (status == queue.consume)
return;
2013-01-07 10:55:05 +01:00
queue.consume = status;
idle_add(IDLE_OPTIONS);
}
void
2013-01-07 10:55:05 +01:00
playlist::SetRandom(player_control &pc, bool status)
{
2013-01-07 10:55:05 +01:00
if (status == queue.random)
return;
2013-01-07 10:55:05 +01:00
const struct song *const queued_song = GetQueuedSong();
2013-01-07 10:55:05 +01:00
queue.random = status;
2013-01-07 10:55:05 +01:00
if (queue.random) {
/* shuffle the queue order, but preserve current */
2013-01-07 10:55:05 +01:00
const int current_position = GetCurrentPosition();
2013-01-07 10:55:05 +01:00
queue.ShuffleOrder();
if (current_position >= 0) {
/* make sure the current song is the first in
the order list, so the whole rest of the
playlist is played after that */
unsigned current_order =
2013-01-07 10:55:05 +01:00
queue.PositionToOrder(current_position);
queue.SwapOrders(0, current_order);
current = 0;
} else
2013-01-07 10:55:05 +01:00
current = -1;
} else
2013-01-07 10:55:05 +01:00
playlist_order(this);
2013-01-07 10:55:05 +01:00
UpdateQueuedSong(pc, queued_song);
idle_add(IDLE_OPTIONS);
}
int
2013-01-07 10:55:05 +01:00
playlist::GetCurrentPosition() const
{
2013-01-07 10:55:05 +01:00
return current >= 0
? queue.OrderToPosition(current)
: -1;
}
int
2013-01-07 10:55:05 +01:00
playlist::GetNextPosition() const
{
2013-01-07 10:55:05 +01:00
if (current < 0)
return -1;
2013-01-07 10:55:05 +01:00
if (queue.single && queue.repeat)
return queue.OrderToPosition(current);
else if (queue.IsValidOrder(current + 1))
return queue.OrderToPosition(current + 1);
else if (queue.repeat)
return queue.OrderToPosition(0);
2013-01-07 10:55:05 +01:00
return -1;
}