Compare commits
3
Commits
30884ad0b5
...
1efdbe0771
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1efdbe0771 | ||
|
|
622335260f | ||
|
|
0e090168e8 |
No files matched your search
@@ -3,6 +3,8 @@
|
||||
#include "AudioDecoder.hpp"
|
||||
#include "Database.hpp"
|
||||
#include "NetworkClient.hpp"
|
||||
#include "PlaylistLoader.hpp"
|
||||
#include "QueueManager.hpp"
|
||||
#include "RpcTypes.hpp"
|
||||
#include "SubscriptionManager.hpp"
|
||||
#include "YouTubeClient.hpp"
|
||||
@@ -77,11 +79,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 +181,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 +222,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_;
|
||||
@@ -237,7 +239,8 @@ namespace Echoion::Core
|
||||
std::unique_ptr<SubscriptionManager> subscriptionManager_;
|
||||
|
||||
Api::BotConfig config_;
|
||||
std::vector<Api::QueueEntry> queue_;
|
||||
// Playback queue + shuffle state (driven under mutex_).
|
||||
QueueManager queueMgr_;
|
||||
std::optional<Api::QueueEntry> currentTrack_;
|
||||
std::atomic<bool> isPlaying_{false};
|
||||
std::atomic<bool> isPaused_{false};
|
||||
@@ -253,11 +256,7 @@ namespace Echoion::Core
|
||||
std::atomic<float> volume_{0.5f};
|
||||
std::atomic<bool> shuffle_{false};
|
||||
std::atomic<bool> repeat_{false};
|
||||
uint64_t nextQueueId_ = 1;
|
||||
|
||||
// Shuffle state - maintains shuffled order of queue indices
|
||||
std::vector<size_t> shuffledIndices_;
|
||||
size_t shuffleIndex_ = 0;
|
||||
void rebuildShuffledIndices();
|
||||
void reshuffleRemaining();
|
||||
|
||||
@@ -278,7 +277,6 @@ namespace Echoion::Core
|
||||
ClientInfoCallback clientInfoCallback_;
|
||||
UidToClientIdCallback uidToClientIdCallback_;
|
||||
NicknameCallback nicknameCallback_;
|
||||
std::mt19937 rng_{std::random_device{}()};
|
||||
|
||||
// Position tracking for periodic event updates
|
||||
std::chrono::steady_clock::time_point lastPositionUpdate_{};
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <TrackTypes.hpp>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
// Owns the playback queue and its shuffle state. NOT thread-safe by
|
||||
// design: the owning MusicManager serializes every access under its
|
||||
// playback mutex_, so the single-lock model of the real-time loop is
|
||||
// preserved. The containers are exposed by reference so the playback loop
|
||||
// keeps operating on them with identical semantics.
|
||||
class QueueManager
|
||||
{
|
||||
public:
|
||||
|
||||
using Entry = Api::QueueEntry;
|
||||
|
||||
[[nodiscard]] std::vector<Entry>& entries() noexcept
|
||||
{
|
||||
return entries_;
|
||||
}
|
||||
[[nodiscard]] const std::vector<Entry>& entries() const noexcept
|
||||
{
|
||||
return entries_;
|
||||
}
|
||||
[[nodiscard]] std::vector<std::size_t>& shuffledIndices() noexcept
|
||||
{
|
||||
return shuffledIndices_;
|
||||
}
|
||||
[[nodiscard]] std::size_t& shuffleIndex() noexcept
|
||||
{
|
||||
return shuffleIndex_;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t& nextQueueId() noexcept
|
||||
{
|
||||
return nextQueueId_;
|
||||
}
|
||||
[[nodiscard]] std::mt19937& rng() noexcept { return rng_; }
|
||||
|
||||
private:
|
||||
|
||||
std::vector<Entry> entries_;
|
||||
std::vector<std::size_t> shuffledIndices_;
|
||||
std::size_t shuffleIndex_ = 0;
|
||||
std::uint64_t nextQueueId_ = 1;
|
||||
std::mt19937 rng_{std::random_device{}()};
|
||||
};
|
||||
|
||||
} // namespace Echoion::Core
|
||||
+277
-387
File diff suppressed because it is too large.
Load diff
+123
-230
@@ -1,6 +1,7 @@
|
||||
#include "ConfigManager.hpp"
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include "echoion/core/MusicManager.hpp"
|
||||
#include "echoion/core/PlaylistLoader.hpp"
|
||||
#include <Events.hpp>
|
||||
#include <algorithm>
|
||||
#include <execution>
|
||||
@@ -42,6 +43,15 @@ namespace Echoion::Core
|
||||
|
||||
youTubeClient_ = std::make_unique<YouTubeClient>(networkClient_);
|
||||
|
||||
// Playlist loading runs off the audio path: it enqueues resolved
|
||||
// tracks via queueTrack and pokes the playback loop on the first one.
|
||||
playlistLoader_ = std::make_unique<PlaylistLoader>(
|
||||
youTubeClient_.get(), eventBus_,
|
||||
[this](const Api::TrackInfo& track, uint64_t clientId) {
|
||||
return queueTrack(track, clientId).has_value();
|
||||
},
|
||||
[this]() { requestPlaybackStart(); });
|
||||
|
||||
// Configure YouTube cookies if provided
|
||||
if (youtubeConfig.youtubeCookiesPath.has_value()) {
|
||||
youTubeClient_->setCookiesPath(*youtubeConfig.youtubeCookiesPath);
|
||||
@@ -127,10 +137,8 @@ namespace Echoion::Core
|
||||
playbackThread_.join();
|
||||
}
|
||||
|
||||
// Join playlist loading thread if still running (don't detach - unsafe)
|
||||
if (playlistLoadThread_.joinable()) {
|
||||
playlistLoadThread_.join();
|
||||
}
|
||||
// Stop playlist loading (the loader joins its background thread).
|
||||
playlistLoader_.reset();
|
||||
|
||||
// Join auto-start thread if still running
|
||||
if (playlistAutoStartThread_.joinable()) {
|
||||
@@ -202,7 +210,7 @@ namespace Echoion::Core
|
||||
|
||||
if (!isPlaying_ && !currentTrack_.has_value()) {
|
||||
currentTrack_ =
|
||||
Api::QueueEntry{.queueId = nextQueueId_++,
|
||||
Api::QueueEntry{.queueId = queueMgr_.nextQueueId()++,
|
||||
.track = *track,
|
||||
.addedAt = std::chrono::system_clock::now(),
|
||||
.addedBy = nickname,
|
||||
@@ -352,7 +360,7 @@ namespace Echoion::Core
|
||||
|
||||
if (needToStartThread ||
|
||||
(!playbackThreadRunning_ &&
|
||||
(!queue_.empty() || currentTrack_.has_value()))) {
|
||||
(!queueMgr_.entries().empty() || currentTrack_.has_value()))) {
|
||||
// Start the playback thread
|
||||
isPlaying_ = true;
|
||||
isPaused_ = false;
|
||||
@@ -386,7 +394,7 @@ namespace Echoion::Core
|
||||
isPlaying_ = false;
|
||||
isPaused_ = false;
|
||||
currentTrack_.reset();
|
||||
queue_.clear();
|
||||
queueMgr_.entries().clear();
|
||||
|
||||
if (audioDecoder_) {
|
||||
audioDecoder_->closeSource();
|
||||
@@ -414,7 +422,7 @@ namespace Echoion::Core
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
if (queue_.empty()) {
|
||||
if (queueMgr_.entries().empty()) {
|
||||
if (repeat_ && currentTrack_.has_value()) {
|
||||
continue;
|
||||
}
|
||||
@@ -426,7 +434,7 @@ namespace Echoion::Core
|
||||
|
||||
auto nextTrack = getNextTrack();
|
||||
currentTrack_ =
|
||||
Api::QueueEntry{.queueId = nextQueueId_++,
|
||||
Api::QueueEntry{.queueId = queueMgr_.nextQueueId()++,
|
||||
.track = nextTrack,
|
||||
.addedAt = std::chrono::system_clock::now(),
|
||||
.addedBy = currentTrack_->addedBy,
|
||||
@@ -535,13 +543,13 @@ namespace Echoion::Core
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(mutex_);
|
||||
|
||||
if (queue_.size() >= config_.maxQueueSize) {
|
||||
if (queueMgr_.entries().size() >= config_.maxQueueSize) {
|
||||
return Api::fail(
|
||||
Api::ErrorCode::InvalidArgument, "Queue is full");
|
||||
}
|
||||
|
||||
queueId = nextQueueId_++;
|
||||
queue_.push_back(
|
||||
queueId = queueMgr_.nextQueueId()++;
|
||||
queueMgr_.entries().push_back(
|
||||
Api::QueueEntry{.queueId = queueId,
|
||||
.track = track,
|
||||
.addedAt = std::chrono::system_clock::now(),
|
||||
@@ -552,15 +560,15 @@ namespace Echoion::Core
|
||||
// If shuffle is enabled, add the new track to shuffled indices
|
||||
if (shuffle_) {
|
||||
// Insert at a random position among the remaining tracks
|
||||
if (shuffleIndex_ < shuffledIndices_.size()) {
|
||||
if (queueMgr_.shuffleIndex() < queueMgr_.shuffledIndices().size()) {
|
||||
std::uniform_int_distribution<size_t> dist(
|
||||
shuffleIndex_, shuffledIndices_.size());
|
||||
size_t insertPos = dist(rng_);
|
||||
shuffledIndices_.insert(shuffledIndices_.begin() +
|
||||
queueMgr_.shuffleIndex(), queueMgr_.shuffledIndices().size());
|
||||
size_t insertPos = dist(queueMgr_.rng());
|
||||
queueMgr_.shuffledIndices().insert(queueMgr_.shuffledIndices().begin() +
|
||||
insertPos,
|
||||
queue_.size() - 1);
|
||||
queueMgr_.entries().size() - 1);
|
||||
} else {
|
||||
shuffledIndices_.push_back(queue_.size() - 1);
|
||||
queueMgr_.shuffledIndices().push_back(queueMgr_.entries().size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -572,7 +580,7 @@ namespace Echoion::Core
|
||||
// this was the first track
|
||||
shouldStart = !isPlaying_ && !isPaused_ &&
|
||||
!currentTrack_.has_value() &&
|
||||
hasOnlineSubscribers() && queue_.size() > 0;
|
||||
hasOnlineSubscribers() && queueMgr_.entries().size() > 0;
|
||||
needToStartThread =
|
||||
!playbackThread_.joinable() && !playbackThreadRunning_;
|
||||
|
||||
@@ -582,7 +590,7 @@ namespace Echoion::Core
|
||||
"thread_joinable={}, thread_running={}, queue_size={}",
|
||||
shouldStart, needToStartThread, isPlaying_.load(),
|
||||
playbackThread_.joinable(), playbackThreadRunning_.load(),
|
||||
queue_.size()));
|
||||
queueMgr_.entries().size()));
|
||||
}
|
||||
|
||||
if (shouldStart) {
|
||||
@@ -629,31 +637,31 @@ namespace Echoion::Core
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(mutex_);
|
||||
|
||||
if (position >= queue_.size()) {
|
||||
if (position >= queueMgr_.entries().size()) {
|
||||
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||
"Invalid position");
|
||||
}
|
||||
|
||||
auto removedTrack = queue_[position];
|
||||
queue_.erase(queue_.begin() + position);
|
||||
auto removedTrack = queueMgr_.entries()[position];
|
||||
queueMgr_.entries().erase(queueMgr_.entries().begin() + position);
|
||||
|
||||
// Update shuffled indices if shuffle is enabled
|
||||
if (shuffle_ && !shuffledIndices_.empty()) {
|
||||
if (shuffle_ && !queueMgr_.shuffledIndices().empty()) {
|
||||
// Remove the index from shuffledIndices
|
||||
auto it = std::find(shuffledIndices_.begin(),
|
||||
shuffledIndices_.end(), position);
|
||||
if (it != shuffledIndices_.end()) {
|
||||
auto it = std::find(queueMgr_.shuffledIndices().begin(),
|
||||
queueMgr_.shuffledIndices().end(), position);
|
||||
if (it != queueMgr_.shuffledIndices().end()) {
|
||||
size_t removedShuffleIndex =
|
||||
std::distance(shuffledIndices_.begin(), it);
|
||||
shuffledIndices_.erase(it);
|
||||
std::distance(queueMgr_.shuffledIndices().begin(), it);
|
||||
queueMgr_.shuffledIndices().erase(it);
|
||||
// If we removed an index before the current shuffle position,
|
||||
// decrement it
|
||||
if (removedShuffleIndex < shuffleIndex_) {
|
||||
shuffleIndex_--;
|
||||
if (removedShuffleIndex < queueMgr_.shuffleIndex()) {
|
||||
queueMgr_.shuffleIndex()--;
|
||||
}
|
||||
}
|
||||
// Decrement indices that were after the removed position
|
||||
for (auto& idx : shuffledIndices_) {
|
||||
for (auto& idx : queueMgr_.shuffledIndices()) {
|
||||
if (idx > position) {
|
||||
idx--;
|
||||
}
|
||||
@@ -667,7 +675,7 @@ namespace Echoion::Core
|
||||
.track = removedTrack.track,
|
||||
.queueId = removedTrack.queueId,
|
||||
.removedByClientId = 0,
|
||||
.queueLength = static_cast<uint32_t>(queue_.size())});
|
||||
.queueLength = static_cast<uint32_t>(queueMgr_.entries().size())});
|
||||
|
||||
return {};
|
||||
}
|
||||
@@ -675,13 +683,13 @@ namespace Echoion::Core
|
||||
Api::Result<void> MusicManager::clearQueue()
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(mutex_);
|
||||
uint32_t previousLength = static_cast<uint32_t>(queue_.size());
|
||||
queue_.clear();
|
||||
uint32_t previousLength = static_cast<uint32_t>(queueMgr_.entries().size());
|
||||
queueMgr_.entries().clear();
|
||||
|
||||
// Clear shuffle state
|
||||
if (shuffle_) {
|
||||
shuffledIndices_.clear();
|
||||
shuffleIndex_ = 0;
|
||||
queueMgr_.shuffledIndices().clear();
|
||||
queueMgr_.shuffleIndex() = 0;
|
||||
}
|
||||
|
||||
Void::Log::info("MusicManager", "{}", "Queue cleared");
|
||||
@@ -696,26 +704,26 @@ namespace Echoion::Core
|
||||
Api::Result<std::vector<Api::QueueEntry>> MusicManager::getQueue() const
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(mutex_);
|
||||
return queue_;
|
||||
return queueMgr_.entries();
|
||||
}
|
||||
|
||||
Api::Result<void> MusicManager::moveInQueue(uint32_t from, uint32_t to)
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(mutex_);
|
||||
|
||||
if (from >= queue_.size() || to >= queue_.size()) {
|
||||
if (from >= queueMgr_.entries().size() || to >= queueMgr_.entries().size()) {
|
||||
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||
"Invalid position");
|
||||
}
|
||||
|
||||
auto entry = queue_[from];
|
||||
queue_.erase(queue_.begin() + from);
|
||||
queue_.insert(queue_.begin() + to, entry);
|
||||
auto entry = queueMgr_.entries()[from];
|
||||
queueMgr_.entries().erase(queueMgr_.entries().begin() + from);
|
||||
queueMgr_.entries().insert(queueMgr_.entries().begin() + to, entry);
|
||||
|
||||
// Update shuffled indices if shuffle is enabled
|
||||
if (shuffle_ && !shuffledIndices_.empty()) {
|
||||
if (shuffle_ && !queueMgr_.shuffledIndices().empty()) {
|
||||
// Find and update indices that are affected by the move
|
||||
for (auto& idx : shuffledIndices_) {
|
||||
for (auto& idx : queueMgr_.shuffledIndices()) {
|
||||
if (from < to) {
|
||||
// Moving forward: indices in (from, to] decrement
|
||||
if (idx > from && idx <= to) {
|
||||
@@ -756,14 +764,14 @@ namespace Echoion::Core
|
||||
if (shuffle_) {
|
||||
// Build shuffled indices for current queue
|
||||
rebuildShuffledIndices();
|
||||
shuffleIndex_ = 0;
|
||||
queueMgr_.shuffleIndex() = 0;
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
std::format("Shuffle enabled - queue shuffled with {} tracks",
|
||||
shuffledIndices_.size()));
|
||||
queueMgr_.shuffledIndices().size()));
|
||||
} else {
|
||||
// Clear shuffle state
|
||||
shuffledIndices_.clear();
|
||||
shuffleIndex_ = 0;
|
||||
queueMgr_.shuffledIndices().clear();
|
||||
queueMgr_.shuffleIndex() = 0;
|
||||
Void::Log::info("MusicManager", "{}", "Shuffle disabled");
|
||||
}
|
||||
|
||||
@@ -776,37 +784,37 @@ namespace Echoion::Core
|
||||
|
||||
void MusicManager::rebuildShuffledIndices()
|
||||
{
|
||||
shuffledIndices_.clear();
|
||||
shuffledIndices_.reserve(queue_.size());
|
||||
queueMgr_.shuffledIndices().clear();
|
||||
queueMgr_.shuffledIndices().reserve(queueMgr_.entries().size());
|
||||
|
||||
// Create indices 0 to queue_size-1
|
||||
for (size_t i = 0; i < queue_.size(); ++i) {
|
||||
shuffledIndices_.push_back(i);
|
||||
for (size_t i = 0; i < queueMgr_.entries().size(); ++i) {
|
||||
queueMgr_.shuffledIndices().push_back(i);
|
||||
}
|
||||
|
||||
// Shuffle the indices using Fisher-Yates algorithm
|
||||
std::shuffle(shuffledIndices_.begin(), shuffledIndices_.end(), rng_);
|
||||
shuffleIndex_ = 0;
|
||||
std::shuffle(queueMgr_.shuffledIndices().begin(), queueMgr_.shuffledIndices().end(), queueMgr_.rng());
|
||||
queueMgr_.shuffleIndex() = 0;
|
||||
|
||||
Void::Log::debug("MusicManager", "{}", std::format("Rebuilt shuffled indices: {} tracks",
|
||||
shuffledIndices_.size()));
|
||||
queueMgr_.shuffledIndices().size()));
|
||||
}
|
||||
|
||||
void MusicManager::reshuffleRemaining()
|
||||
{
|
||||
if (shuffledIndices_.empty() ||
|
||||
shuffleIndex_ >= shuffledIndices_.size()) {
|
||||
if (queueMgr_.shuffledIndices().empty() ||
|
||||
queueMgr_.shuffleIndex() >= queueMgr_.shuffledIndices().size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only reshuffle the remaining indices (from shuffleIndex_ onwards)
|
||||
// Only reshuffle the remaining indices (from queueMgr_.shuffleIndex() onwards)
|
||||
// This preserves the already-played tracks order but reshuffles what's
|
||||
// left
|
||||
std::shuffle(shuffledIndices_.begin() + shuffleIndex_,
|
||||
shuffledIndices_.end(), rng_);
|
||||
std::shuffle(queueMgr_.shuffledIndices().begin() + queueMgr_.shuffleIndex(),
|
||||
queueMgr_.shuffledIndices().end(), queueMgr_.rng());
|
||||
|
||||
Void::Log::debug("MusicManager", "{}", std::format("Reshuffled remaining {} tracks",
|
||||
shuffledIndices_.size() - shuffleIndex_));
|
||||
queueMgr_.shuffledIndices().size() - queueMgr_.shuffleIndex()));
|
||||
}
|
||||
|
||||
Api::Result<bool> MusicManager::toggleRepeat()
|
||||
@@ -825,145 +833,30 @@ namespace Echoion::Core
|
||||
MusicManager::loadPlaylist(const std::string& playlistIdOrUrl,
|
||||
uint64_t clientId)
|
||||
{
|
||||
if (!youTubeClient_) {
|
||||
if (!playlistLoader_) {
|
||||
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||
"YouTube client not available");
|
||||
"Playlist loader not available");
|
||||
}
|
||||
return playlistLoader_->load(playlistIdOrUrl, clientId);
|
||||
}
|
||||
|
||||
// Check if already loading a playlist
|
||||
if (playlistLoading_.load()) {
|
||||
return Api::fail(Api::ErrorCode::AlreadyExists,
|
||||
"Already loading a playlist, please wait");
|
||||
void MusicManager::requestPlaybackStart()
|
||||
{
|
||||
// Start playback in a background thread so streaming isn't blocked.
|
||||
if (playlistAutoStartThread_.joinable()) {
|
||||
playlistAutoStartThread_.join();
|
||||
}
|
||||
playlistAutoStartThread_ = std::thread([this]() {
|
||||
// Small delay to ensure the queue is updated.
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
// Extract playlist ID if a full URL was provided
|
||||
std::string playlistId = playlistIdOrUrl;
|
||||
if (YouTubeClient::isYouTubeUrl(playlistIdOrUrl)) {
|
||||
auto extractedId =
|
||||
youTubeClient_->extractPlaylistId(playlistIdOrUrl);
|
||||
if (extractedId) {
|
||||
playlistId = *extractedId;
|
||||
Void::Log::debug("MusicManager", "{}", std::format(
|
||||
"Extracted playlist ID '{}' from URL", playlistId));
|
||||
} else {
|
||||
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||
"Could not extract playlist ID from URL");
|
||||
std::shared_lock<std::shared_mutex> lock(mutex_);
|
||||
if (!isPlaying_ && !queueMgr_.entries().empty()) {
|
||||
cv_.notify_one();
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
"Playback started from playlist");
|
||||
}
|
||||
}
|
||||
|
||||
// Join previous thread if any (don't detach - unsafe)
|
||||
if (playlistLoadThread_.joinable()) {
|
||||
playlistLoadThread_.join();
|
||||
}
|
||||
|
||||
// Reset loading state
|
||||
playlistLoading_ = true;
|
||||
playlistLoadedCount_ = 0;
|
||||
playlistTotalCount_ = 0;
|
||||
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
std::format("Starting async playlist load: {}", playlistId));
|
||||
|
||||
// Publish playlist load started event
|
||||
emit(
|
||||
Api::Events::PlaylistLoadStarted{.playlistId = playlistId,
|
||||
.requestedByClientId = clientId});
|
||||
|
||||
// Start playlist loading in background thread
|
||||
playlistLoadThread_ = std::thread([this, playlistId, clientId]() {
|
||||
std::atomic<uint32_t> added{0};
|
||||
std::atomic<bool> firstTrackAdded{false};
|
||||
std::mutex startPlaybackMutex;
|
||||
|
||||
// Use streaming playlist loading - tracks are added as soon as they
|
||||
// are resolved
|
||||
youTubeClient_->getPlaylistStreaming(
|
||||
playlistId,
|
||||
// Track callback - called for each track as it is resolved
|
||||
[this, &added, &firstTrackAdded, &startPlaybackMutex, clientId,
|
||||
playlistId](Api::TrackInfo&& track) {
|
||||
playlistLoadedCount_.fetch_add(1);
|
||||
|
||||
bool isFirst = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(startPlaybackMutex);
|
||||
isFirst = !firstTrackAdded.load();
|
||||
if (isFirst) {
|
||||
firstTrackAdded = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Add track to queue
|
||||
auto result = queueTrack(track, clientId);
|
||||
if (result) {
|
||||
added.fetch_add(1);
|
||||
|
||||
// Publish playlist track loaded event
|
||||
emit(
|
||||
Api::Events::PlaylistTrackLoaded{
|
||||
.playlistId = playlistId,
|
||||
.track = track,
|
||||
.loadedCount = static_cast<uint32_t>(
|
||||
playlistLoadedCount_.load()),
|
||||
.totalCount = static_cast<uint32_t>(
|
||||
playlistTotalCount_.load())});
|
||||
|
||||
// Start playback immediately on first track
|
||||
if (isFirst) {
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
std::format("First track resolved: {} - {}. "
|
||||
"Starting playback...",
|
||||
track.title, track.artist));
|
||||
|
||||
// Start playback in background thread to not block
|
||||
// streaming
|
||||
// Join any previous auto-start thread first
|
||||
if (playlistAutoStartThread_.joinable()) {
|
||||
playlistAutoStartThread_.join();
|
||||
}
|
||||
playlistAutoStartThread_ = std::thread([this]() {
|
||||
// Small delay to ensure queue is updated
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds(100));
|
||||
|
||||
std::shared_lock<std::shared_mutex> lock(
|
||||
mutex_);
|
||||
if (!isPlaying_ && !queue_.empty()) {
|
||||
// Trigger playback start
|
||||
cv_.notify_one();
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
"Playback started from playlist");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
// On playlist info callback - called when total track count is
|
||||
// known
|
||||
[this, playlistId](size_t totalTracks) {
|
||||
playlistTotalCount_ = totalTracks;
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
std::format("Playlist '{}' has {} tracks, resolving...",
|
||||
playlistId, totalTracks));
|
||||
},
|
||||
// On complete callback
|
||||
[this, &added, playlistId]() {
|
||||
Void::Log::info("MusicManager", "{}", std::format(
|
||||
"Playlist streaming complete. Added {} tracks",
|
||||
added.load()));
|
||||
playlistLoading_ = false;
|
||||
|
||||
// Publish playlist load completed event
|
||||
emit(
|
||||
Api::Events::PlaylistLoadCompleted{
|
||||
.playlistId = playlistId,
|
||||
.totalTracksLoaded = added.load(),
|
||||
.success = true,
|
||||
.error = std::nullopt});
|
||||
});
|
||||
}); // End of background thread
|
||||
|
||||
return {}; // Return immediately, loading continues in background
|
||||
});
|
||||
}
|
||||
|
||||
Api::Result<std::vector<Api::PlaylistInfo>>
|
||||
@@ -1021,7 +914,7 @@ namespace Echoion::Core
|
||||
|
||||
Api::PlaybackState state;
|
||||
state.currentTrack = currentTrack_;
|
||||
state.queue = queue_;
|
||||
state.queue = queueMgr_.entries();
|
||||
state.isPlaying = isPlaying_;
|
||||
state.isPaused = isPaused_;
|
||||
state.position = getPlaybackPosition();
|
||||
@@ -1118,7 +1011,7 @@ namespace Echoion::Core
|
||||
// Build playback state directly (don't call getPlaybackState to avoid
|
||||
// double-lock)
|
||||
state.playback.currentTrack = currentTrack_;
|
||||
state.playback.queue = queue_;
|
||||
state.playback.queue = queueMgr_.entries();
|
||||
state.playback.isPlaying = isPlaying_;
|
||||
state.playback.isPaused = isPaused_;
|
||||
// Calculate position directly using time-based tracking
|
||||
@@ -1136,7 +1029,7 @@ namespace Echoion::Core
|
||||
state.playback.shuffle = shuffle_;
|
||||
state.playback.lastUpdate = std::chrono::system_clock::now();
|
||||
|
||||
state.queue = queue_;
|
||||
state.queue = queueMgr_.entries();
|
||||
|
||||
if (currentTrack_ && currentTrack_->track.streamUrl) {
|
||||
state.currentStreamUrl = currentTrack_->track.streamUrl;
|
||||
@@ -1152,13 +1045,13 @@ namespace Echoion::Core
|
||||
MusicManager::restoreFromHotReload(const Api::HotReloadState& state)
|
||||
{
|
||||
config_ = state.config;
|
||||
queue_ = state.queue;
|
||||
queueMgr_.entries() = state.queue;
|
||||
volume_ = state.playback.volume;
|
||||
shuffle_ = state.playback.shuffle;
|
||||
repeat_ = state.playback.repeat;
|
||||
|
||||
// Rebuild shuffle state if shuffle is enabled
|
||||
if (shuffle_ && !queue_.empty()) {
|
||||
if (shuffle_ && !queueMgr_.entries().empty()) {
|
||||
rebuildShuffledIndices();
|
||||
}
|
||||
|
||||
@@ -1221,7 +1114,7 @@ namespace Echoion::Core
|
||||
state.playback.currentTrack.has_value(),
|
||||
state.currentStreamUrl.has_value()));
|
||||
}
|
||||
if (!currentTrack_.has_value() && !queue_.empty() &&
|
||||
if (!currentTrack_.has_value() && !queueMgr_.entries().empty() &&
|
||||
hasOnlineSubscribers()) {
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
"Playback not restored but queue has items, starting "
|
||||
@@ -1249,7 +1142,7 @@ namespace Echoion::Core
|
||||
std::jthread(&MusicManager::playbackThread, this);
|
||||
}
|
||||
cv_.notify_one();
|
||||
} else if (state.playback.isPlaying && !queue_.empty()) {
|
||||
} else if (state.playback.isPlaying && !queueMgr_.entries().empty()) {
|
||||
isPaused_ = true;
|
||||
isPlaying_ = true;
|
||||
currentTrack_.reset();
|
||||
@@ -1312,13 +1205,13 @@ namespace Echoion::Core
|
||||
std::unique_lock<std::shared_mutex> lock(mutex_);
|
||||
cv_.wait(lock, [this] {
|
||||
return !isPlaying_ || !isPaused_ ||
|
||||
(hasOnlineSubscribers() && !queue_.empty() &&
|
||||
(hasOnlineSubscribers() && !queueMgr_.entries().empty() &&
|
||||
!currentTrack_.has_value());
|
||||
});
|
||||
|
||||
// Auto-start if we have ONLINE subscribers and queue but no
|
||||
// current track
|
||||
if (hasOnlineSubscribers() && !queue_.empty() &&
|
||||
if (hasOnlineSubscribers() && !queueMgr_.entries().empty() &&
|
||||
!currentTrack_.has_value() && !wasPausedForNoSubscribers_) {
|
||||
if (!isPlaying_) {
|
||||
isPlaying_ = true;
|
||||
@@ -1336,8 +1229,8 @@ namespace Echoion::Core
|
||||
}
|
||||
}
|
||||
// Start first track from queue
|
||||
auto firstTrack = queue_.front();
|
||||
queue_.erase(queue_.begin());
|
||||
auto firstTrack = queueMgr_.entries().front();
|
||||
queueMgr_.entries().erase(queueMgr_.entries().begin());
|
||||
|
||||
auto streamUrl = getStreamUrl(firstTrack.track);
|
||||
if (streamUrl) {
|
||||
@@ -1628,7 +1521,7 @@ namespace Echoion::Core
|
||||
Void::Log::debug("MusicManager", "{}", std::format(
|
||||
"Playback thread: isPlaying_={}, queue_size={}, "
|
||||
"currentTrack.has_value={}",
|
||||
isPlaying_.load(), queue_.size(),
|
||||
isPlaying_.load(), queueMgr_.entries().size(),
|
||||
currentTrack_.has_value()));
|
||||
if (!isPlaying_) {
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
@@ -1741,7 +1634,7 @@ namespace Echoion::Core
|
||||
return;
|
||||
}
|
||||
|
||||
if (queue_.empty()) {
|
||||
if (queueMgr_.entries().empty()) {
|
||||
// Queue is empty - stop playback but keep thread running for next
|
||||
// track
|
||||
isPlaying_ = false;
|
||||
@@ -1757,7 +1650,7 @@ namespace Echoion::Core
|
||||
nextTrack.title, nextTrack.artist));
|
||||
|
||||
currentTrack_ =
|
||||
Api::QueueEntry{.queueId = nextQueueId_++,
|
||||
Api::QueueEntry{.queueId = queueMgr_.nextQueueId()++,
|
||||
.track = nextTrack,
|
||||
.addedAt = std::chrono::system_clock::now(),
|
||||
.addedBy = currentTrack_->addedBy,
|
||||
@@ -1811,41 +1704,41 @@ namespace Echoion::Core
|
||||
Void::Log::error("MusicManager", "{}", std::format("Failed to open next track: {}",
|
||||
result.error().message));
|
||||
// Remove the failed track and try next
|
||||
queue_.erase(queue_.begin());
|
||||
queueMgr_.entries().erase(queueMgr_.entries().begin());
|
||||
onTrackFinished();
|
||||
}
|
||||
} else {
|
||||
Void::Log::error("MusicManager", "{}", "Failed to get stream URL for next track");
|
||||
queue_.erase(queue_.begin());
|
||||
queueMgr_.entries().erase(queueMgr_.entries().begin());
|
||||
onTrackFinished();
|
||||
}
|
||||
}
|
||||
|
||||
Api::TrackInfo MusicManager::getNextTrack()
|
||||
{
|
||||
if (queue_.empty()) {
|
||||
if (queueMgr_.entries().empty()) {
|
||||
return Api::TrackInfo{};
|
||||
}
|
||||
|
||||
if (shuffle_ && !shuffledIndices_.empty()) {
|
||||
if (shuffle_ && !queueMgr_.shuffledIndices().empty()) {
|
||||
// Use shuffled order
|
||||
size_t queueIndex = shuffledIndices_[shuffleIndex_];
|
||||
auto track = queue_[queueIndex].track;
|
||||
size_t queueIndex = queueMgr_.shuffledIndices()[queueMgr_.shuffleIndex()];
|
||||
auto track = queueMgr_.entries()[queueIndex].track;
|
||||
|
||||
// Remove the track from queue and rebuild shuffled indices
|
||||
queue_.erase(queue_.begin() + queueIndex);
|
||||
shuffleIndex_++;
|
||||
queueMgr_.entries().erase(queueMgr_.entries().begin() + queueIndex);
|
||||
queueMgr_.shuffleIndex()++;
|
||||
|
||||
// If we've played all tracks, rebuild for next cycle
|
||||
if (shuffleIndex_ >= shuffledIndices_.size()) {
|
||||
if (queueMgr_.shuffleIndex() >= queueMgr_.shuffledIndices().size()) {
|
||||
rebuildShuffledIndices();
|
||||
} else {
|
||||
// Rebuild indices to account for the removed track
|
||||
// Decrement indices that were after the removed track
|
||||
for (size_t i = shuffleIndex_; i < shuffledIndices_.size();
|
||||
for (size_t i = queueMgr_.shuffleIndex(); i < queueMgr_.shuffledIndices().size();
|
||||
++i) {
|
||||
if (shuffledIndices_[i] > queueIndex) {
|
||||
shuffledIndices_[i]--;
|
||||
if (queueMgr_.shuffledIndices()[i] > queueIndex) {
|
||||
queueMgr_.shuffledIndices()[i]--;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1853,14 +1746,14 @@ namespace Echoion::Core
|
||||
return track;
|
||||
}
|
||||
|
||||
auto track = queue_.front().track;
|
||||
queue_.erase(queue_.begin());
|
||||
auto track = queueMgr_.entries().front().track;
|
||||
queueMgr_.entries().erase(queueMgr_.entries().begin());
|
||||
return track;
|
||||
}
|
||||
|
||||
Api::Result<void> MusicManager::shuffleQueue()
|
||||
{
|
||||
std::shuffle(queue_.begin(), queue_.end(), rng_);
|
||||
std::shuffle(queueMgr_.entries().begin(), queueMgr_.entries().end(), queueMgr_.rng());
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -2181,7 +2074,7 @@ Subscriptions are persistent across server reconnects.)";
|
||||
}
|
||||
// If we're not playing but have tracks in queue, auto-start
|
||||
else if (!isPlaying_ && !currentTrack_.has_value() &&
|
||||
!queue_.empty()) {
|
||||
!queueMgr_.entries().empty()) {
|
||||
Void::Log::info("MusicManager", "{}", "Online subscriber connected with tracks in "
|
||||
"queue, auto-starting");
|
||||
// Notify playback thread to start
|
||||
@@ -2307,7 +2200,7 @@ Subscriptions are persistent across server reconnects.)";
|
||||
std::shared_lock<std::shared_mutex> lock(mutex_);
|
||||
|
||||
// Don't prefetch if not playing, no current track, or queue is empty
|
||||
if (!isPlaying_ || !currentTrack_.has_value() || queue_.empty()) {
|
||||
if (!isPlaying_ || !currentTrack_.has_value() || queueMgr_.entries().empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2375,16 +2268,16 @@ Subscriptions are persistent across server reconnects.)";
|
||||
Api::TrackInfo nextTrack;
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(mutex_);
|
||||
if (queue_.empty()) {
|
||||
if (queueMgr_.entries().empty()) {
|
||||
prefetchState_.isPrefetching = false;
|
||||
return;
|
||||
}
|
||||
// Use shuffled order if shuffle is enabled
|
||||
if (shuffle_ && !shuffledIndices_.empty() &&
|
||||
shuffleIndex_ < shuffledIndices_.size()) {
|
||||
nextTrack = queue_[shuffledIndices_[shuffleIndex_]].track;
|
||||
if (shuffle_ && !queueMgr_.shuffledIndices().empty() &&
|
||||
queueMgr_.shuffleIndex() < queueMgr_.shuffledIndices().size()) {
|
||||
nextTrack = queueMgr_.entries()[queueMgr_.shuffledIndices()[queueMgr_.shuffleIndex()]].track;
|
||||
} else {
|
||||
nextTrack = queue_[0].track;
|
||||
nextTrack = queueMgr_.entries()[0].track;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
#include "echoion/core/PlaylistLoader.hpp"
|
||||
#include "echoion/core/YouTubeClient.hpp"
|
||||
#include <Events.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <format>
|
||||
#include <mutex>
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
Api::Result<void> PlaylistLoader::load(const std::string& playlistIdOrUrl,
|
||||
uint64_t clientId)
|
||||
{
|
||||
if (!yt_) {
|
||||
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||
"YouTube client not available");
|
||||
}
|
||||
|
||||
if (loading_.load()) {
|
||||
return Api::fail(Api::ErrorCode::AlreadyExists,
|
||||
"Already loading a playlist, please wait");
|
||||
}
|
||||
|
||||
// Extract playlist ID if a full URL was provided
|
||||
std::string playlistId = playlistIdOrUrl;
|
||||
if (YouTubeClient::isYouTubeUrl(playlistIdOrUrl)) {
|
||||
auto extractedId = yt_->extractPlaylistId(playlistIdOrUrl);
|
||||
if (extractedId) {
|
||||
playlistId = *extractedId;
|
||||
Void::Log::debug(
|
||||
"PlaylistLoader", "{}",
|
||||
std::format("Extracted playlist ID '{}' from URL",
|
||||
playlistId));
|
||||
} else {
|
||||
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||
"Could not extract playlist ID from URL");
|
||||
}
|
||||
}
|
||||
|
||||
// Join previous load thread if any (don't detach - unsafe)
|
||||
if (loadThread_.joinable()) {
|
||||
loadThread_.join();
|
||||
}
|
||||
|
||||
loading_ = true;
|
||||
loadedCount_ = 0;
|
||||
totalCount_ = 0;
|
||||
|
||||
Void::Log::info("PlaylistLoader", "{}",
|
||||
std::format("Starting async playlist load: {}",
|
||||
playlistId));
|
||||
|
||||
emit(Api::Events::PlaylistLoadStarted{.playlistId = playlistId,
|
||||
.requestedByClientId = clientId});
|
||||
|
||||
loadThread_ = std::thread([this, playlistId, clientId]() {
|
||||
std::atomic<uint32_t> added{0};
|
||||
std::atomic<bool> firstTrackAdded{false};
|
||||
std::mutex firstMutex;
|
||||
|
||||
yt_->getPlaylistStreaming(
|
||||
playlistId,
|
||||
// Track callback — called for each track as it resolves.
|
||||
[this, &added, &firstTrackAdded, &firstMutex, clientId,
|
||||
playlistId](Api::TrackInfo&& track) {
|
||||
loadedCount_.fetch_add(1);
|
||||
|
||||
bool isFirst = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(firstMutex);
|
||||
isFirst = !firstTrackAdded.load();
|
||||
if (isFirst) {
|
||||
firstTrackAdded = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (enqueue_(track, clientId)) {
|
||||
added.fetch_add(1);
|
||||
|
||||
emit(Api::Events::PlaylistTrackLoaded{
|
||||
.playlistId = playlistId,
|
||||
.track = track,
|
||||
.loadedCount =
|
||||
static_cast<uint32_t>(loadedCount_.load()),
|
||||
.totalCount =
|
||||
static_cast<uint32_t>(totalCount_.load())});
|
||||
|
||||
// Start playback immediately on the first track.
|
||||
if (isFirst) {
|
||||
Void::Log::info(
|
||||
"PlaylistLoader", "{}",
|
||||
std::format("First track resolved: {} - {}. "
|
||||
"Starting playback...",
|
||||
track.title, track.artist));
|
||||
if (requestStart_) {
|
||||
requestStart_();
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
// Total-track-count callback.
|
||||
[this, playlistId](size_t totalTracks) {
|
||||
totalCount_ = totalTracks;
|
||||
Void::Log::info(
|
||||
"PlaylistLoader", "{}",
|
||||
std::format("Playlist '{}' has {} tracks, resolving...",
|
||||
playlistId, totalTracks));
|
||||
},
|
||||
// Completion callback.
|
||||
[this, &added, playlistId]() {
|
||||
Void::Log::info(
|
||||
"PlaylistLoader", "{}",
|
||||
std::format(
|
||||
"Playlist streaming complete. Added {} tracks",
|
||||
added.load()));
|
||||
loading_ = false;
|
||||
|
||||
emit(Api::Events::PlaylistLoadCompleted{
|
||||
.playlistId = playlistId,
|
||||
.totalTracksLoaded = added.load(),
|
||||
.success = true,
|
||||
.error = std::nullopt});
|
||||
});
|
||||
});
|
||||
|
||||
return {}; // Loading continues in the background.
|
||||
}
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user