Skip to content

Commit

Permalink
cleaned NEXT on last track
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe44 committed Oct 6, 2023
1 parent eae302b commit bf3f95f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
5 changes: 3 additions & 2 deletions cspot/include/TrackPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct TrackReference;
class TrackPlayer : bell::Task {
public:
// Callback types
typedef std::function<void(std::shared_ptr<QueuedTrack>)> TrackLoadedCallback;
typedef std::function<void(std::shared_ptr<QueuedTrack>, bool)> TrackLoadedCallback;
typedef std::function<size_t(uint8_t*, size_t, std::string_view)>
DataCallback;
typedef std::function<void()> EOFCallback;
Expand All @@ -49,7 +49,7 @@ class TrackPlayer : bell::Task {

// CDNTrackStream::TrackInfo getCurrentTrackInfo();
void seekMs(size_t ms);
void resetState();
void resetState(bool paused = false);

// Vorbis codec callbacks
size_t _vorbisRead(void* ptr, size_t size, size_t nmemb);
Expand Down Expand Up @@ -89,6 +89,7 @@ class TrackPlayer : bell::Task {
std::atomic<bool> pendingReset = false;
std::atomic<bool> inFuture = false;
std::atomic<size_t> pendingSeekPositionMs = 0;
std::atomic<bool> startPaused = false;

std::mutex runningMutex;

Expand Down
33 changes: 11 additions & 22 deletions cspot/src/SpircHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ SpircHandler::SpircHandler(std::shared_ptr<cspot::Context> ctx) {
}
};

auto trackLoadedCallback = [this](std::shared_ptr<QueuedTrack> track) {
playbackState->setPlaybackState(PlaybackState::State::Playing);
auto trackLoadedCallback = [this](std::shared_ptr<QueuedTrack> track, bool paused = false) {
playbackState->setPlaybackState(paused ? PlaybackState::State::Paused : PlaybackState::State::Playing);
playbackState->updatePositionMs(track->requestedPosition);

this->notify();

CSPOT_LOG(info, "WHARE %d", paused);
// Send playback start event, unpause
sendEvent(EventType::PLAYBACK_START, (int)track->requestedPosition);
sendEvent(EventType::PLAY_PAUSE, false);
sendEvent(EventType::PLAY_PAUSE, paused);
};

this->ctx = ctx;
Expand Down Expand Up @@ -142,7 +142,6 @@ void SpircHandler::handleFrame(std::vector<uint8_t>& data) {
notify();

sendEvent(EventType::SEEK, (int)playbackState->remoteFrame.position);
//sendEvent(EventType::FLUSH);
break;
}
case MessageType_kMessageTypeVolume:
Expand Down Expand Up @@ -201,8 +200,8 @@ void SpircHandler::handleFrame(std::vector<uint8_t>& data) {
false);
this->notify();

trackPlayer->resetState();
sendEvent(EventType::FLUSH);
trackPlayer->resetState();
break;
}
case MessageType_kMessageTypeShuffle: {
Expand All @@ -229,24 +228,14 @@ void SpircHandler::notify() {
this->sendCmd(MessageType_kMessageTypeNotify);
}

bool SpircHandler::skipSong(TrackQueue::SkipDirection dir) {
if (trackQueue->skipTrack(dir)) {
// flush first to clean sink
sendEvent(EventType::FLUSH);

// Reset track state
trackPlayer->resetState();
bool SpircHandler::skipSong(TrackQueue::SkipDirection dir) {
bool skipped = trackQueue->skipTrack(dir);

return true;
} else {
// can't skip, just pause where we are
sendEvent(EventType::PLAY_PAUSE, true);

playbackState->setPlaybackState(PlaybackState::State::Paused);
notify();
// Reset track state
trackPlayer->resetState(!skipped);

return false;
}
// send NEXT or PREV event only when successful
return skipped;
}

bool SpircHandler::nextSong() {
Expand Down
6 changes: 4 additions & 2 deletions cspot/src/TrackPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ void TrackPlayer::stop() {
std::scoped_lock lock(runningMutex);
}

void TrackPlayer::resetState() {
void TrackPlayer::resetState(bool paused) {
// Mark for reset
this->pendingReset = true;
this->currentSongPlaying = false;
this->startPaused = paused;

std::scoped_lock lock(dataOutMutex);

Expand Down Expand Up @@ -184,7 +185,8 @@ void TrackPlayer::runTask() {
}

if (trackOffset == 0 && pendingSeekPositionMs == 0) {
this->trackLoaded(track);
this->trackLoaded(track, startPaused);
startPaused = false;
}

int32_t r =
Expand Down
3 changes: 2 additions & 1 deletion targets/cli/CliPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ CliPlayer::CliPlayer(std::unique_ptr<AudioSink> sink,
this->centralAudioBuffer->clearBuffer();
break;
case cspot::SpircHandler::EventType::PLAYBACK_START:
this->isPaused = true;
this->centralAudioBuffer->clearBuffer();
break;
default:
Expand All @@ -90,7 +91,7 @@ void CliPlayer::runTask() {

if (this->pauseRequested) {
this->pauseRequested = false;
std::cout << "Pause requsted!" << std::endl;
std::cout << "Pause requested!" << std::endl;
#ifndef BELL_DISABLE_CODECS
auto effect = std::make_unique<bell::BellDSP::FadeEffect>(
44100 / 2, false, [this]() { this->isPaused = true; });
Expand Down

0 comments on commit bf3f95f

Please sign in to comment.