Initial commit
This commit is contained in:
commit
f6c5bcca39
52 files changed
+55449
No files matched your search
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
extern "C" {
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavutil/opt.h>
|
||||
#include <libswresample/swresample.h>
|
||||
}
|
||||
enum class DecodeStatus { Idle, Processing, Completed, Failed, Cancelled };
|
||||
class AudioDecoder {
|
||||
public:
|
||||
struct AudioData {
|
||||
std::vector<short> buffer;
|
||||
int samples = 0;
|
||||
int channels = 0;
|
||||
int sampleRate = 0;
|
||||
};
|
||||
std::deque<std::vector<short>> m_chunkQueue;
|
||||
std::mutex m_queueMtx;
|
||||
std::condition_variable m_cv;
|
||||
std::atomic<bool> m_isDecodingFinished{false};
|
||||
AudioDecoder();
|
||||
~AudioDecoder();
|
||||
void StartDecode(const std::string &url, int targetSampleRate);
|
||||
void Cancel() { m_cancelRequested = true; }
|
||||
DecodeStatus GetStatus() const { return m_status; }
|
||||
AudioData GetResult() { return std::move(m_result); }
|
||||
bool GetNextChunk(std::vector<short> &outChunk, bool blocking = false);
|
||||
bool IsFinished() const { return m_finished.load(); }
|
||||
|
||||
private:
|
||||
void Log(const std::string &msg) {
|
||||
std::lock_guard<std::mutex> lock(m_logMtx);
|
||||
std::cout << "[AudioDecoder] " << msg << std::endl;
|
||||
}
|
||||
void Cleanup();
|
||||
void InternalDecode(std::string url, int targetSampleRate);
|
||||
std::atomic<DecodeStatus> m_status{DecodeStatus::Idle};
|
||||
std::atomic<bool> m_cancelRequested{false};
|
||||
std::jthread m_thread;
|
||||
std::mutex m_logMtx;
|
||||
AudioData m_result;
|
||||
std::atomic<bool> m_finished{false};
|
||||
AVFormatContext *m_formatCtx = nullptr;
|
||||
AVCodecContext *m_codecCtx = nullptr;
|
||||
SwrContext *m_swrCtx = nullptr;
|
||||
};
|
||||
@@ -0,0 +1,118 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <singleton_atomic.hpp>
|
||||
#include <string>
|
||||
#include <ts6bot/Proxy.hpp>
|
||||
|
||||
#include <glaze/glaze.hpp>
|
||||
|
||||
namespace ts6bot {
|
||||
|
||||
enum class LogLevel { Critical, Error, Warning, Info, Trace };
|
||||
|
||||
struct ServerQueryConfig {
|
||||
std::string host{"127.0.0.1"};
|
||||
uint16_t port{
|
||||
10022}; // ServerQuery SSH default :contentReference[oaicite:6]{index=6}
|
||||
std::string username{"serveradmin"};
|
||||
std::string password{};
|
||||
int virtual_server_id{1};
|
||||
std::string nickname{"ts6bot"};
|
||||
};
|
||||
|
||||
struct AfkConfig {
|
||||
bool enabled{true};
|
||||
int idle_seconds{600};
|
||||
std::uint64_t afk_channel_id{0};
|
||||
int poll_interval_ms{5000};
|
||||
};
|
||||
|
||||
struct MusicConfig {
|
||||
bool enabled{true};
|
||||
std::string youtubeApiToken;
|
||||
};
|
||||
|
||||
struct ProxyConfiguration : public ProxyConfig {
|
||||
bool enabled{false};
|
||||
};
|
||||
|
||||
struct Config {
|
||||
std::string nickname;
|
||||
std::string host;
|
||||
std::string channel;
|
||||
std::string password;
|
||||
LogLevel loggingLevel;
|
||||
AfkConfig afk{};
|
||||
MusicConfig music{};
|
||||
ProxyConfiguration proxy{};
|
||||
};
|
||||
|
||||
class Configuration : public SingletonAtomic<Configuration> {
|
||||
public:
|
||||
const Config getConfig() const noexcept { return m_config; }
|
||||
|
||||
Configuration(std::string path) {
|
||||
auto config = Config();
|
||||
std::string buffer;
|
||||
auto ec = glz::read_file_json(config, path, buffer);
|
||||
if (ec) {
|
||||
std::string descriptive_error = glz::format_error(ec, buffer);
|
||||
std::cerr << "Not found configuration in " << path << " using default"
|
||||
<< std::endl;
|
||||
std::cerr << descriptive_error << std::endl;
|
||||
std::cerr << ec.ec << std::endl;
|
||||
m_config = Config();
|
||||
}
|
||||
this->m_config = config;
|
||||
}
|
||||
|
||||
Config m_config{};
|
||||
};
|
||||
|
||||
// Glaze meta (explicit, stable)
|
||||
|
||||
} // namespace ts6bot
|
||||
template <> struct glz::meta<ProxyType> {
|
||||
using enum ProxyType;
|
||||
static constexpr auto value = enumerate(HTTP, SOCKS5, SOCKS5_HOSTNAME);
|
||||
};
|
||||
template <> struct glz::meta<ts6bot::LogLevel> {
|
||||
using enum ts6bot::LogLevel;
|
||||
static constexpr auto value =
|
||||
enumerate(Critical, Error, Warning, Info, Trace);
|
||||
};
|
||||
template <> struct glz::meta<ts6bot::ServerQueryConfig> {
|
||||
using T = ts6bot::ServerQueryConfig;
|
||||
static constexpr auto value =
|
||||
object("host", &T::host, "port", &T::port, "username", &T::username,
|
||||
"password", &T::password, "virtual_server_id",
|
||||
&T::virtual_server_id, "nickname", &T::nickname);
|
||||
};
|
||||
|
||||
template <> struct glz::meta<ts6bot::AfkConfig> {
|
||||
using T = ts6bot::AfkConfig;
|
||||
static constexpr auto value =
|
||||
object("enabled", &T::enabled, "idle_seconds", &T::idle_seconds,
|
||||
"afk_channel_id", &T::afk_channel_id, "poll_interval_ms",
|
||||
&T::poll_interval_ms);
|
||||
};
|
||||
|
||||
template <> struct glz::meta<ts6bot::MusicConfig> {
|
||||
using T = ts6bot::MusicConfig;
|
||||
static constexpr auto value =
|
||||
object("enabled", &T::enabled, "youtubeApiToken", &T::youtubeApiToken);
|
||||
};
|
||||
template <> struct glz::meta<ts6bot::ProxyConfiguration> {
|
||||
using T = ts6bot::ProxyConfiguration;
|
||||
static constexpr auto value =
|
||||
object("url", &T::url, "username", &T::username, "password", &T::password,
|
||||
"type", &T::type, "enabled", &T::enabled);
|
||||
};
|
||||
template <> struct glz::meta<ts6bot::Config> {
|
||||
using T = ts6bot::Config;
|
||||
static constexpr auto value =
|
||||
object("nickname", &T::nickname, "host", &T::host, "channel", &T::channel,
|
||||
"password", &T::password, "loggingLevel", &T::loggingLevel, "afk",
|
||||
&T::afk, "music", &T::music, "proxy", &T::proxy);
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ts6bot::crypto {
|
||||
|
||||
// RFC 7636: length 43..128, chars [A-Z a-z 0-9 - . _ ~]
|
||||
std::string GenerateCodeVerifier(std::size_t length = 64);
|
||||
|
||||
// Random state value (base64url, no padding).
|
||||
std::string GenerateState(std::size_t bytes = 16);
|
||||
|
||||
std::vector<std::uint8_t> Sha256(std::span<const std::uint8_t> data);
|
||||
std::vector<std::uint8_t> Sha256(std::span<const char> data);
|
||||
|
||||
// Base64url encode without padding.
|
||||
std::string Base64UrlEncode(std::span<const std::uint8_t> data);
|
||||
|
||||
} // namespace ts6bot::crypto
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <string_view>
|
||||
|
||||
#include "quill/Backend.h"
|
||||
#include "quill/Frontend.h"
|
||||
#include "quill/LogMacros.h"
|
||||
#include "quill/Logger.h"
|
||||
#include "quill/sinks/FileSink.h"
|
||||
#include "quill/sinks/ConsoleSink.h"
|
||||
|
||||
namespace ts6bot {
|
||||
|
||||
class Log {
|
||||
public:
|
||||
static void init(std::string_view file_path);
|
||||
static quill::Logger* logger();
|
||||
|
||||
private:
|
||||
static quill::Logger* g_logger;
|
||||
};
|
||||
|
||||
} // namespace ts6bot
|
||||
@@ -0,0 +1,100 @@
|
||||
#include <optional>
|
||||
|
||||
#include "PluginDefinitions.hpp"
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Youtube/Client.hpp"
|
||||
#include <atomic>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#include <singleton_atomic.hpp>
|
||||
#include <string>
|
||||
#include <teamspeak/public_definitions.h>
|
||||
#include <thread>
|
||||
#include <ts3_functions.h>
|
||||
#include <ts6bot/AudioDecoder.hpp>
|
||||
#include <quill/Logger.h>
|
||||
#include <quill/LogMacros.h>
|
||||
#include <vector>
|
||||
class MusicManager {
|
||||
public:
|
||||
explicit MusicManager(std::shared_ptr<TS3Functions> functions, quill::Logger* logger, std::uint64_t serverConnectionHandlerID = 0);
|
||||
~MusicManager();
|
||||
|
||||
struct Music {
|
||||
std::string name;
|
||||
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
|
||||
void subscribe(anyID id);
|
||||
void moveSubscriberToOffline(anyID id);
|
||||
void moveSubscriberToOnline(anyID id);
|
||||
void addSubscriberToOnline(anyID id);
|
||||
void addSubscriberToOffline(anyID id);
|
||||
void 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& req, std::optional<definitions::Streams> streams);
|
||||
void checkQueueAndPlay();
|
||||
|
||||
// Youtube / Search
|
||||
std::vector<definitions::SearchResultItem> search(std::string name, int maxResults = 5) const;
|
||||
void playUrl(std::string url, const std::string &requester = "");
|
||||
|
||||
// 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();
|
||||
|
||||
quill::Logger* m_logger;
|
||||
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;
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
enum class ProxyType {
|
||||
HTTP,
|
||||
SOCKS5,
|
||||
SOCKS5_HOSTNAME // Use this to let the proxy resolve the DNS
|
||||
};
|
||||
struct ProxyConfig {
|
||||
std::string url;
|
||||
ProxyType type = ProxyType::SOCKS5_HOSTNAME;
|
||||
std::string username = "";
|
||||
std::string password = "";
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
|
||||
class SignalShutdown {
|
||||
public:
|
||||
explicit SignalShutdown(std::function<void()> onShutdown);
|
||||
~SignalShutdown();
|
||||
|
||||
SignalShutdown(const SignalShutdown&) = delete;
|
||||
SignalShutdown& operator=(const SignalShutdown&) = delete;
|
||||
|
||||
private:
|
||||
std::function<void()> m_OnShutdown;
|
||||
std::thread m_Thread;
|
||||
bool m_Running = true;
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace ts6bot::url {
|
||||
|
||||
// Percent-encode a string for use as a query parameter value.
|
||||
std::string EncodeComponent(const std::string& s);
|
||||
|
||||
struct HttpUrl {
|
||||
std::string host;
|
||||
uint16_t port{};
|
||||
std::string path; // begins with '/'
|
||||
};
|
||||
|
||||
// Minimal parser for redirect URIs.
|
||||
// Supports: http://<host>:<port>/<path>
|
||||
// Throws std::runtime_error on invalid input.
|
||||
HttpUrl ParseHttpUrl(const std::string& httpUrl);
|
||||
|
||||
} // namespace ts6bot::url
|
||||
@@ -0,0 +1,114 @@
|
||||
#pragma once
|
||||
|
||||
#include "Spotify/Client.hpp"
|
||||
#include <gst/gst.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <print>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
struct GstObjectDeleter {
|
||||
void operator()(gpointer ptr) const noexcept {
|
||||
if (ptr)
|
||||
gst_object_unref(ptr);
|
||||
}
|
||||
};
|
||||
|
||||
struct GstMessageDeleter {
|
||||
void operator()(GstMessage *msg) const noexcept {
|
||||
if (msg)
|
||||
gst_message_unref(msg);
|
||||
}
|
||||
};
|
||||
|
||||
class VirtualMicrophone {
|
||||
public:
|
||||
// micName: what apps will see/select (e.g. "StudioMic")
|
||||
explicit VirtualMicrophone(std::string micName, bool createNew);
|
||||
~VirtualMicrophone() noexcept;
|
||||
|
||||
// Play Spotify track via gstspotify spotifyaudiosrc and route into the
|
||||
// virtual microphone.
|
||||
void PlayUri(const std::string &trackUri, const std::string &token,
|
||||
const ProxyConfig &proxy);
|
||||
void PlayUri(const std::string &trackUri,
|
||||
const std::optional<ProxyConfig> proxy);
|
||||
// Playback control
|
||||
void Pause();
|
||||
void Resume();
|
||||
void StopPlayback(); // stops pipeline (does not destroy devices)
|
||||
void WaitUntilFinished(); // waits EOS or ERROR; throws on ERROR
|
||||
|
||||
// Device control (normally you don't call these directly)
|
||||
void DestroyDevices() noexcept;
|
||||
|
||||
// For debugging / integration
|
||||
[[nodiscard]] const std::string &MicName() const noexcept {
|
||||
return m_MicName;
|
||||
}
|
||||
[[nodiscard]] const std::string &BusSinkName() const noexcept {
|
||||
return m_BusSinkName;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_MicName; // virtual mic node name (Audio/Source/Virtual)
|
||||
std::string m_BusSinkName; // bus sink name (Audio/Sink)
|
||||
|
||||
// Pulse/PipeWire module ids
|
||||
uint32_t m_ModBusSink = 0;
|
||||
uint32_t m_ModVirtualMic = 0;
|
||||
|
||||
// pw-link ports we connected (for disconnection)
|
||||
std::optional<std::string> m_LinkOutFL;
|
||||
std::optional<std::string> m_LinkOutFR;
|
||||
std::optional<std::string> m_LinkInFL;
|
||||
std::optional<std::string> m_LinkInFR;
|
||||
|
||||
// GStreamer pipeline
|
||||
std::unique_ptr<GstElement, GstObjectDeleter> m_Pipeline;
|
||||
std::jthread m_BusThread;
|
||||
|
||||
// completion signalling
|
||||
std::mutex m_Mtx;
|
||||
std::condition_variable m_Cv;
|
||||
bool m_Finished = true;
|
||||
bool m_HadError = false;
|
||||
std::string m_LastError;
|
||||
|
||||
private:
|
||||
// startup behavior
|
||||
void CleanupStaleDevicesByName();
|
||||
|
||||
// create routing: bus sink + virtual mic + pw-link
|
||||
void CreateDevices();
|
||||
void CreateLinks();
|
||||
|
||||
// pipeline lifecycle
|
||||
void BuildAndStartPipeline(const std::string &pipelineDesc);
|
||||
void WatchBus(std::stop_token st);
|
||||
|
||||
// utilities
|
||||
static std::string RunCommandCapture(const std::string &cmd);
|
||||
static int RunCommandNoCapture(const std::string &cmd);
|
||||
static uint32_t ParseFirstUint(std::string_view s);
|
||||
|
||||
static std::vector<std::string> SplitLines(const std::string &s);
|
||||
static bool Contains(std::string_view hay, std::string_view needle);
|
||||
|
||||
// pw-link helpers
|
||||
static std::vector<std::string> PwLinkListOutputs();
|
||||
static std::vector<std::string> PwLinkListInputs();
|
||||
static std::optional<std::string>
|
||||
FindPortPrefix(const std::vector<std::string> &list, std::string_view prefix,
|
||||
std::string_view suffix);
|
||||
};
|
||||
Reference in new issue
Block a user