Commit dee378b7 authored by Max Kellermann's avatar Max Kellermann

player/Thread: make SEEK (partially) non-blocking

When the decoder is still starting up while we handle a SEEK, finish the "player SEEK" immediately and re-enter the player loop, being able to handle commands (and even cancel the pending seek). This is the first part in a series of patches to solve the "blocking input blocks decoder, blocks player, blocks the main thread" problem. There are many other blocking code locations left, and the main thread isn't non-blocking either because it waits for "seeking" to become false.
parent f76262ef
...@@ -256,6 +256,11 @@ PlayerControl::SeekLocked(std::unique_ptr<DetachedSong> song, SongTime t) ...@@ -256,6 +256,11 @@ PlayerControl::SeekLocked(std::unique_ptr<DetachedSong> song, SongTime t)
assert(next_song == nullptr); assert(next_song == nullptr);
/* the SEEK command is asynchronous; until completion, the
"seeking" flag is set */
while (seeking)
ClientWait();
if (error_type != PlayerError::NONE) { if (error_type != PlayerError::NONE) {
assert(error); assert(error);
std::rethrow_exception(error); std::rethrow_exception(error);
......
...@@ -54,7 +54,9 @@ enum class PlayerCommand : uint8_t { ...@@ -54,7 +54,9 @@ enum class PlayerCommand : uint8_t {
/** /**
* Seek to a certain position in the specified song. This * Seek to a certain position in the specified song. This
* command can also be used to change the current song or * command can also be used to change the current song or
* start playback. * start playback. It "finishes" immediately, but
* PlayerControl::seeking will be set until seeking really
* completes (or fails).
*/ */
SEEK, SEEK,
...@@ -177,6 +179,11 @@ struct PlayerControl final : AudioOutputClient { ...@@ -177,6 +179,11 @@ struct PlayerControl final : AudioOutputClient {
ReplayGainMode replay_gain_mode = ReplayGainMode::OFF; ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;
/** /**
* Is the player currently busy with the SEEK command?
*/
bool seeking = false;
/**
* If this flag is set, then the player will be auto-paused at * If this flag is set, then the player will be auto-paused at
* the end of the song, before the next song starts to play. * the end of the song, before the next song starts to play.
* *
......
...@@ -147,6 +147,16 @@ class Player { ...@@ -147,6 +147,16 @@ class Player {
*/ */
SongTime elapsed_time = SongTime::zero(); SongTime elapsed_time = SongTime::zero();
/**
* If this is positive, then we need to ask the decoder to
* seek after it has completed startup. This is needed if the
* decoder is in the middle of startup while the player
* receives another seek command.
*
* This is only valid while #decoder_starting is true.
*/
SongTime pending_seek;
PeriodClock throttle_silence_log; PeriodClock throttle_silence_log;
public: public:
...@@ -280,6 +290,11 @@ private: ...@@ -280,6 +290,11 @@ private:
*/ */
bool SeekDecoder() noexcept; bool SeekDecoder() noexcept;
void CancelPendingSeek() noexcept {
pending_seek = SongTime::zero();
pc.seeking = false;
}
/** /**
* Check if the decoder has reported an error, and forward it * Check if the decoder has reported an error, and forward it
* to PlayerControl::SetError(). * to PlayerControl::SetError().
...@@ -432,6 +447,7 @@ Player::ActivateDecoder() noexcept ...@@ -432,6 +447,7 @@ Player::ActivateDecoder() noexcept
/* set the "starting" flag, which will be cleared by /* set the "starting" flag, which will be cleared by
CheckDecoderStartup() */ CheckDecoderStartup() */
decoder_starting = true; decoder_starting = true;
pending_seek = SongTime::zero();
/* update PlayerControl's song information */ /* update PlayerControl's song information */
pc.total_time = song->GetDuration(); pc.total_time = song->GetDuration();
...@@ -528,6 +544,19 @@ Player::CheckDecoderStartup() noexcept ...@@ -528,6 +544,19 @@ Player::CheckDecoderStartup() noexcept
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
if (pending_seek > SongTime::zero()) {
assert(pc.seeking);
bool success = SeekDecoder(pending_seek);
pc.seeking = false;
pc.ClientSignal();
if (!success)
return false;
/* re-fill the buffer after seeking */
buffering = true;
}
if (!paused && !OpenOutput()) { if (!paused && !OpenOutput()) {
FormatError(player_domain, FormatError(player_domain,
"problems opening audio device " "problems opening audio device "
...@@ -619,6 +648,8 @@ Player::SeekDecoder() noexcept ...@@ -619,6 +648,8 @@ Player::SeekDecoder() noexcept
{ {
assert(pc.next_song != nullptr); assert(pc.next_song != nullptr);
CancelPendingSeek();
{ {
const ScopeUnlock unlock(pc.mutex); const ScopeUnlock unlock(pc.mutex);
pc.outputs.Cancel(); pc.outputs.Cancel();
...@@ -650,18 +681,22 @@ Player::SeekDecoder() noexcept ...@@ -650,18 +681,22 @@ Player::SeekDecoder() noexcept
pc.next_song.reset(); pc.next_song.reset();
queued = false; queued = false;
/* wait for the decoder to complete initialization if (decoder_starting) {
(just in case that happens to be still in /* wait for the decoder to complete
progress) */ initialization; postpone the SEEK
command */
if (!WaitDecoderStartup())
return false;
/* send the SEEK command */
if (!SeekDecoder(pc.seek_time)) { pending_seek = pc.seek_time;
pc.seeking = true;
pc.CommandFinished(); pc.CommandFinished();
return false; return true;
} else {
/* send the SEEK command */
if (!SeekDecoder(pc.seek_time)) {
pc.CommandFinished();
return false;
}
} }
} }
...@@ -1114,6 +1149,7 @@ Player::Run() noexcept ...@@ -1114,6 +1149,7 @@ Player::Run() noexcept
} }
} }
CancelPendingSeek();
StopDecoder(); StopDecoder();
ClearAndDeletePipe(); ClearAndDeletePipe();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment