Fix playback after restoring queue on boot

custom
jacqueline 12 months ago
parent 265049c519
commit 690c64c151
  1. 14
      src/tangara/audio/audio_fsm.cpp
  2. 16
      src/tangara/audio/stream_cues.cpp
  3. 3
      src/tangara/audio/stream_cues.hpp

@ -191,6 +191,11 @@ void AudioState::react(const internal::StreamStarted& ev) {
if (!sIsPaused && !is_in_state<states::Playback>()) { if (!sIsPaused && !is_in_state<states::Playback>()) {
transit<states::Playback>(); transit<states::Playback>();
} else {
// Make sure everyone knows we've got a track ready to go, even if we're
// not playing it yet. This mostly matters when restoring the queue from
// disk after booting.
emitPlaybackUpdate(true);
} }
} }
@ -485,12 +490,13 @@ void Playback::react(const system_fsm::SdStateChanged& ev) {
void Playback::react(const internal::StreamHeartbeat& ev) { void Playback::react(const internal::StreamHeartbeat& ev) {
sStreamCues.update(sOutput->samplesUsed()); sStreamCues.update(sOutput->samplesUsed());
auto current = sStreamCues.current();
if (!current.first) { if (sStreamCues.hasStream()) {
transit<Standby>();
} else {
emitPlaybackUpdate(false); emitPlaybackUpdate(false);
} else {
// Finished the current stream, and there's nothing upcoming. We must be
// finished.
transit<Standby>();
} }
} }

@ -32,10 +32,14 @@ auto StreamCues::update(uint32_t sample) -> void {
auto StreamCues::addCue(std::shared_ptr<TrackInfo> track, uint32_t sample) auto StreamCues::addCue(std::shared_ptr<TrackInfo> track, uint32_t sample)
-> void { -> void {
upcoming_.push_back(Cue{ if (sample == now_) {
.track = track, current_ = {track, now_};
.start_at = sample, } else {
}); upcoming_.push_back(Cue{
.track = track,
.start_at = sample,
});
}
} }
auto StreamCues::current() -> std::pair<std::shared_ptr<TrackInfo>, uint32_t> { auto StreamCues::current() -> std::pair<std::shared_ptr<TrackInfo>, uint32_t> {
@ -54,4 +58,8 @@ auto StreamCues::current() -> std::pair<std::shared_ptr<TrackInfo>, uint32_t> {
return {current_->track, duration}; return {current_->track, duration};
} }
auto StreamCues::hasStream() -> bool {
return current_ || !upcoming_.empty();
}
} // namespace audio } // namespace audio

@ -26,9 +26,12 @@ class StreamCues {
/* Updates the current track given the new most recently played sample. */ /* Updates the current track given the new most recently played sample. */
auto update(uint32_t sample) -> void; auto update(uint32_t sample) -> void;
/* Returns the current track, and how long it has been playing for. */ /* Returns the current track, and how long it has been playing for. */
auto current() -> std::pair<std::shared_ptr<TrackInfo>, uint32_t>; auto current() -> std::pair<std::shared_ptr<TrackInfo>, uint32_t>;
auto hasStream() -> bool;
auto addCue(std::shared_ptr<TrackInfo>, uint32_t start_at) -> void; auto addCue(std::shared_ptr<TrackInfo>, uint32_t start_at) -> void;
private: private:

Loading…
Cancel
Save