Extract PlaylistLoader from MusicManager
Move the async, streaming playlist-load orchestration (the background load thread, loading/loaded/total counters, and the getPlaylistStreaming wiring) into a PlaylistLoader class. It enqueues resolved tracks via an injected callback (-> queueTrack) and asks the owner to start playback on the first track via requestPlaybackStart() — the single seam back to the audio loop, which still owns the auto-start thread + cv_ poke. loadPlaylist() now just delegates. Behavior preserved; build green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
0e090168e8
commit
622335260f
4 files changed
+243
-145
No files matched your search
@@ -3,6 +3,7 @@
|
||||
#include "AudioDecoder.hpp"
|
||||
#include "Database.hpp"
|
||||
#include "NetworkClient.hpp"
|
||||
#include "PlaylistLoader.hpp"
|
||||
#include "RpcTypes.hpp"
|
||||
#include "SubscriptionManager.hpp"
|
||||
#include "YouTubeClient.hpp"
|
||||
@@ -77,11 +78,11 @@ namespace Echoion::Core
|
||||
uint64_t clientId);
|
||||
[[nodiscard]] bool isPlaylistLoading() const
|
||||
{
|
||||
return playlistLoading_.load();
|
||||
return playlistLoader_ && playlistLoader_->isLoading();
|
||||
}
|
||||
[[nodiscard]] size_t getPlaylistLoadProgress() const
|
||||
{
|
||||
return playlistLoadedCount_.load();
|
||||
return playlistLoader_ ? playlistLoader_->loadedCount() : 0;
|
||||
}
|
||||
[[nodiscard]] Api::Result<std::vector<Api::PlaylistInfo>>
|
||||
getPlaylists() const;
|
||||
@@ -179,6 +180,8 @@ namespace Echoion::Core
|
||||
}
|
||||
|
||||
void playbackThread();
|
||||
// Auto-start the playback loop when a playlist's first track lands.
|
||||
void requestPlaybackStart();
|
||||
[[nodiscard]] Api::Result<Api::TrackInfo>
|
||||
resolveQuery(const std::string& query);
|
||||
[[nodiscard]] Api::Result<std::string>
|
||||
@@ -218,13 +221,11 @@ namespace Echoion::Core
|
||||
std::optional<std::string> prefetchedUrl;
|
||||
} prefetchState_;
|
||||
|
||||
// Async playlist loading state
|
||||
std::thread playlistLoadThread_;
|
||||
std::atomic<bool> playlistLoading_{false};
|
||||
std::atomic<size_t> playlistLoadedCount_{0};
|
||||
std::atomic<size_t> playlistTotalCount_{0};
|
||||
// Async playlist loading (background thread owned by the loader).
|
||||
std::unique_ptr<PlaylistLoader> playlistLoader_;
|
||||
|
||||
// Auto-start thread for playlist (must be joined, not detached)
|
||||
// Auto-start thread: pokes the playback loop when a playlist's
|
||||
// first track lands. Owned here because it touches the audio loop.
|
||||
std::thread playlistAutoStartThread_;
|
||||
|
||||
std::shared_ptr<NetworkClient> networkClient_;
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include <TrackTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <Void/Async/EventBus.hpp>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
class YouTubeClient;
|
||||
|
||||
// Drives async, streaming playlist loading off the audio path. Owns the
|
||||
// background load thread; resolved tracks are handed to the owner's queue
|
||||
// via the enqueue callback, and the owner is asked to start playback on the
|
||||
// first track via requestStart. Event publishing goes through the bus.
|
||||
class PlaylistLoader
|
||||
{
|
||||
public:
|
||||
|
||||
// Returns true if the track was accepted into the queue.
|
||||
using EnqueueFn =
|
||||
std::function<bool(const Api::TrackInfo&, uint64_t)>;
|
||||
using PlaybackStartFn = std::function<void()>;
|
||||
|
||||
PlaylistLoader(YouTubeClient* yt, Void::EventBus::EventBus* bus,
|
||||
EnqueueFn enqueue, PlaybackStartFn requestStart)
|
||||
: yt_(yt), bus_(bus), enqueue_(std::move(enqueue)),
|
||||
requestStart_(std::move(requestStart))
|
||||
{
|
||||
}
|
||||
|
||||
~PlaylistLoader()
|
||||
{
|
||||
if (loadThread_.joinable()) {
|
||||
loadThread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
PlaylistLoader(const PlaylistLoader&) = delete;
|
||||
PlaylistLoader& operator=(const PlaylistLoader&) = delete;
|
||||
|
||||
[[nodiscard]] Api::Result<void>
|
||||
load(const std::string& playlistIdOrUrl, uint64_t clientId);
|
||||
|
||||
[[nodiscard]] bool isLoading() const { return loading_.load(); }
|
||||
[[nodiscard]] std::size_t loadedCount() const
|
||||
{
|
||||
return loadedCount_.load();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
template <typename T> void emit(T event)
|
||||
{
|
||||
if (bus_) {
|
||||
bus_->publish<T>(std::move(event));
|
||||
}
|
||||
}
|
||||
|
||||
YouTubeClient* yt_;
|
||||
Void::EventBus::EventBus* bus_;
|
||||
EnqueueFn enqueue_;
|
||||
PlaybackStartFn requestStart_;
|
||||
|
||||
std::thread loadThread_;
|
||||
std::atomic<bool> loading_{false};
|
||||
std::atomic<std::size_t> loadedCount_{0};
|
||||
std::atomic<std::size_t> totalCount_{0};
|
||||
};
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user