121 lines
3.5 KiB
C++
121 lines
3.5 KiB
C++
#include <optional>
|
|
|
|
#include "PluginDefinitions.hpp"
|
|
|
|
#pragma once
|
|
|
|
#include "Youtube/Client.hpp"
|
|
#include <atomic>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <queue>
|
|
#include <set>
|
|
#include <string>
|
|
#include <teamspeak/public_definitions.h>
|
|
#include <thread>
|
|
#include <ts3_functions.h>
|
|
#include <ts6bot/AudioDecoder.hpp>
|
|
#include <vector>
|
|
class MusicManager
|
|
{
|
|
public:
|
|
|
|
explicit MusicManager(std::shared_ptr<TS3Functions> functions,
|
|
std::uint64_t serverConnectionHandlerID = 0);
|
|
~MusicManager();
|
|
|
|
struct Music {
|
|
std::string name;
|
|
std::string shortUrl;
|
|
std::string length;
|
|
std::string videoId;
|
|
std::string requestedBy;
|
|
YouTube::Result<definitions::Streams> streams;
|
|
};
|
|
|
|
// Playback Control
|
|
void stop();
|
|
void skip();
|
|
void forceSkip();
|
|
void setStopAfterCurrent(bool val) { m_stopAfterCurrent = val; }
|
|
bool isIdle() const { return m_isIdle; }
|
|
std::uint64_t getServerConnectionHandlerID() const
|
|
{
|
|
return m_serverConnectionHandlerID;
|
|
}
|
|
void setServerConnectionHandlerID(std::uint64_t id)
|
|
{
|
|
m_serverConnectionHandlerID = id;
|
|
}
|
|
// Subscription (Whisper) Management
|
|
bool subscribe(anyID id);
|
|
void moveSubscriberToOffline(anyID id);
|
|
void moveSubscriberToOnline(anyID id);
|
|
bool addSubscriberToOnline(anyID id);
|
|
bool addSubscriberToOffline(anyID id);
|
|
bool unSubscribe(anyID id);
|
|
std::set<uint64_t> getOnlineSubscribers() const;
|
|
std::set<uint64_t> getOfflineSubscribers() const;
|
|
// Queue Management
|
|
std::vector<Music> getQueue() const;
|
|
std::optional<Music> getCurrent() const { return m_current; }
|
|
void removeFromQueue(std::size_t index);
|
|
void removeFromQueue(const std::string& name);
|
|
void addToQueueDirect(const std::string& vid,
|
|
const std::string& title,
|
|
const std::string& shortUrl,
|
|
const std::string& length,
|
|
const std::string& req,
|
|
std::optional<definitions::Streams>
|
|
streams);
|
|
void checkQueueAndPlay();
|
|
|
|
// Youtube / Search
|
|
std::vector<definitions::SearchResultItem>
|
|
search(std::string name, int maxResults = 5) const;
|
|
bool playUrl(std::string url, const std::string& requester = "");
|
|
bool playPlaylist(const std::string& playlistId,
|
|
const std::string& requester);
|
|
// Helper to extract playlist ID from URL
|
|
std::optional<std::string> extractPlaylistId(std::string_view url);
|
|
// Selection Logic
|
|
// Stores the results for a specific user to select later
|
|
void storePendingSearch(
|
|
anyID clientId,
|
|
const std::vector<definitions::SearchResultItem>& results);
|
|
bool selectSearchResult(anyID clientId,
|
|
int index,
|
|
const std::string& requesterName);
|
|
|
|
private:
|
|
|
|
void loop(std::stop_token st);
|
|
static std::string formatDuration(const std::string& isoDuration);
|
|
void updateWhisperList();
|
|
|
|
std::shared_ptr<TS3Functions> m_functions{nullptr};
|
|
std::unique_ptr<AudioDecoder> m_microphone{nullptr};
|
|
std::unique_ptr<YouTube::Client> m_client{nullptr};
|
|
|
|
std::atomic<bool> m_stopAfterCurrent{false};
|
|
std::atomic<bool> m_isIdle{true};
|
|
|
|
std::deque<Music> m_queue{};
|
|
mutable std::mutex m_queueMtx;
|
|
std::condition_variable m_cv;
|
|
|
|
std::set<anyID> m_onlineSubscribers;
|
|
std::set<anyID> m_offlineSubscribers;
|
|
// Playback Control
|
|
std::atomic_bool m_skipRequested{false};
|
|
|
|
std::atomic<float> m_volume{0.5f}; // Default 50%
|
|
std::uint64_t m_serverConnectionHandlerID = 0;
|
|
std::optional<Music> m_current;
|
|
std::mutex m_subMtx;
|
|
std::map<anyID, std::vector<definitions::SearchResultItem>>
|
|
m_pendingSearches;
|
|
std::mutex m_pendingMtx;
|
|
std::jthread m_worker;
|
|
};
|