queue/Playlist: seek methods return bool/Error instead of PlaylistResult

This commit is contained in:
Max Kellermann 2015-11-11 19:32:32 +01:00
parent 0f4f04eaa4
commit 5e93c05095
5 changed files with 78 additions and 48 deletions

View File

@ -142,17 +142,19 @@ struct Partition final : private PlayerListener, private MixerListener {
return playlist.PlayPrevious(pc); return playlist.PlayPrevious(pc);
} }
PlaylistResult SeekSongPosition(unsigned song_position, bool SeekSongPosition(unsigned song_position,
SongTime seek_time) { SongTime seek_time, Error &error) {
return playlist.SeekSongPosition(pc, song_position, seek_time); return playlist.SeekSongPosition(pc, song_position, seek_time,
error);
} }
PlaylistResult SeekSongId(unsigned song_id, SongTime seek_time) { bool SeekSongId(unsigned song_id, SongTime seek_time, Error &error) {
return playlist.SeekSongId(pc, song_id, seek_time); return playlist.SeekSongId(pc, song_id, seek_time, error);
} }
PlaylistResult SeekCurrent(SignedSongTime seek_time, bool relative) { bool SeekCurrent(SignedSongTime seek_time, bool relative,
return playlist.SeekCurrent(pc, seek_time, relative); Error &error) {
return playlist.SeekCurrent(pc, seek_time, relative, error);
} }
void SetRepeat(bool new_value) { void SetRepeat(bool new_value) {

View File

@ -296,9 +296,10 @@ handle_seek(Client &client, Request args, Response &r)
if (!args.Parse(0, song, r) || !args.Parse(1, seek_time, r)) if (!args.Parse(0, song, r) || !args.Parse(1, seek_time, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = Error error;
client.partition.SeekSongPosition(song, seek_time); return client.partition.SeekSongPosition(song, seek_time, error)
return print_playlist_result(r, result); ? CommandResult::OK
: print_error(r, error);
} }
CommandResult CommandResult
@ -311,9 +312,10 @@ handle_seekid(Client &client, Request args, Response &r)
if (!args.Parse(1, seek_time, r)) if (!args.Parse(1, seek_time, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = Error error;
client.partition.SeekSongId(id, seek_time); return client.partition.SeekSongId(id, seek_time, error)
return print_playlist_result(r, result); ? CommandResult::OK
: print_error(r, error);
} }
CommandResult CommandResult
@ -325,9 +327,10 @@ handle_seekcur(Client &client, Request args, Response &r)
if (!ParseCommandArg(r, seek_time, p)) if (!ParseCommandArg(r, seek_time, p))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = Error error;
client.partition.SeekCurrent(seek_time, relative); return client.partition.SeekCurrent(seek_time, relative, error)
return print_playlist_result(r, result); ? CommandResult::OK
: print_error(r, error);
} }
CommandResult CommandResult

View File

@ -280,16 +280,19 @@ public:
void PlayPrevious(PlayerControl &pc); void PlayPrevious(PlayerControl &pc);
PlaylistResult SeekSongOrder(PlayerControl &pc, bool SeekSongOrder(PlayerControl &pc,
unsigned song_order, unsigned song_order,
SongTime seek_time); SongTime seek_time,
Error &error);
PlaylistResult SeekSongPosition(PlayerControl &pc, bool SeekSongPosition(PlayerControl &pc,
unsigned song_position, unsigned sonag_position,
SongTime seek_time); SongTime seek_time,
Error &error);
PlaylistResult SeekSongId(PlayerControl &pc, bool SeekSongId(PlayerControl &pc,
unsigned song_id, SongTime seek_time); unsigned song_id, SongTime seek_time,
Error &error);
/** /**
* Seek within the current song. Fails if MPD is not currently * Seek within the current song. Fails if MPD is not currently
@ -299,8 +302,9 @@ public:
* @param relative if true, then the specified time is relative to the * @param relative if true, then the specified time is relative to the
* current position * current position
*/ */
PlaylistResult SeekCurrent(PlayerControl &pc, bool SeekCurrent(PlayerControl &pc,
SignedSongTime seek_time, bool relative); SignedSongTime seek_time, bool relative,
Error &error);
bool GetRepeat() const { bool GetRepeat() const {
return queue.repeat; return queue.repeat;

View File

@ -189,8 +189,9 @@ playlist::PlayPrevious(PlayerControl &pc)
PlayOrder(pc, order); PlayOrder(pc, order);
} }
PlaylistResult bool
playlist::SeekSongOrder(PlayerControl &pc, unsigned i, SongTime seek_time) playlist::SeekSongOrder(PlayerControl &pc, unsigned i, SongTime seek_time,
Error &error)
{ {
assert(queue.IsValidOrder(i)); assert(queue.IsValidOrder(i));
@ -213,52 +214,71 @@ playlist::SeekSongOrder(PlayerControl &pc, unsigned i, SongTime seek_time)
if (!pc.LockSeek(new DetachedSong(queue.GetOrder(i)), seek_time)) { if (!pc.LockSeek(new DetachedSong(queue.GetOrder(i)), seek_time)) {
UpdateQueuedSong(pc, queued_song); UpdateQueuedSong(pc, queued_song);
return PlaylistResult::NOT_PLAYING; // TODO: fix error code
error.Set(playlist_domain, int(PlaylistResult::NOT_PLAYING),
"Decoder failed to seek");
return false;
} }
queued = -1; queued = -1;
UpdateQueuedSong(pc, nullptr); UpdateQueuedSong(pc, nullptr);
return PlaylistResult::SUCCESS; return true;
} }
PlaylistResult bool
playlist::SeekSongPosition(PlayerControl &pc, unsigned song, playlist::SeekSongPosition(PlayerControl &pc, unsigned song,
SongTime seek_time) SongTime seek_time,
Error &error)
{ {
if (!queue.IsValidPosition(song)) if (!queue.IsValidPosition(song)) {
return PlaylistResult::BAD_RANGE; error.Set(playlist_domain, int(PlaylistResult::BAD_RANGE),
"Bad range");
return false;
}
unsigned i = queue.random unsigned i = queue.random
? queue.PositionToOrder(song) ? queue.PositionToOrder(song)
: song; : song;
return SeekSongOrder(pc, i, seek_time); return SeekSongOrder(pc, i, seek_time, error);
} }
PlaylistResult bool
playlist::SeekSongId(PlayerControl &pc, unsigned id, SongTime seek_time) playlist::SeekSongId(PlayerControl &pc, unsigned id, SongTime seek_time,
Error &error)
{ {
int song = queue.IdToPosition(id); int song = queue.IdToPosition(id);
if (song < 0) if (song < 0) {
return PlaylistResult::NO_SUCH_SONG; error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_SONG),
"No such song");
return false;
}
return SeekSongPosition(pc, song, seek_time); return SeekSongPosition(pc, song, seek_time, error);
} }
PlaylistResult bool
playlist::SeekCurrent(PlayerControl &pc, playlist::SeekCurrent(PlayerControl &pc,
SignedSongTime seek_time, bool relative) SignedSongTime seek_time, bool relative,
Error &error)
{ {
if (!playing) if (!playing) {
return PlaylistResult::NOT_PLAYING; error.Set(playlist_domain, int(PlaylistResult::NOT_PLAYING),
"Not playing");
return false;
}
if (relative) { if (relative) {
const auto status = pc.LockGetStatus(); const auto status = pc.LockGetStatus();
if (status.state != PlayerState::PLAY && if (status.state != PlayerState::PLAY &&
status.state != PlayerState::PAUSE) status.state != PlayerState::PAUSE) {
return PlaylistResult::NOT_PLAYING; error.Set(playlist_domain,
int(PlaylistResult::NOT_PLAYING),
"Not playing");
return false;
}
seek_time += status.elapsed_time; seek_time += status.elapsed_time;
if (seek_time.IsNegative()) if (seek_time.IsNegative())
@ -268,5 +288,5 @@ playlist::SeekCurrent(PlayerControl &pc,
if (seek_time.IsNegative()) if (seek_time.IsNegative())
seek_time = SignedSongTime::zero(); seek_time = SignedSongTime::zero();
return SeekSongOrder(pc, current, SongTime(seek_time)); return SeekSongOrder(pc, current, SongTime(seek_time), error);
} }

View File

@ -198,7 +198,8 @@ playlist_state_restore(const char *line, TextFile &file,
else if (seek_time.count() == 0) else if (seek_time.count() == 0)
playlist.PlayPosition(pc, current); playlist.PlayPosition(pc, current);
else else
playlist.SeekSongPosition(pc, current, seek_time); playlist.SeekSongPosition(pc, current, seek_time,
IgnoreError());
if (state == PlayerState::PAUSE) if (state == PlayerState::PAUSE)
pc.LockPause(); pc.LockPause();