Files

150 lines
4.6 KiB
C++

#pragma once
#include "NetworkClient.hpp"
#include <TrackTypes.hpp>
#include <Types.hpp>
#include <Void/Async/ThreadPool.hpp>
#include <YouTubeTypes.hpp>
#include <atomic>
#include <filesystem>
#include <future>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
namespace Echoion::Core
{
struct YtDlpResult {
std::string output;
int exitCode;
bool success;
std::chrono::milliseconds executionTime;
};
class YtDlpManager
{
public:
explicit YtDlpManager(std::shared_ptr<NetworkClient> networkClient);
~YtDlpManager();
// Initialize - checks for yt-dlp and downloads if needed
Api::Result<void> initialize();
// Gate the periodic (7-day) background self-update. Configured
// via features.enableAutoUpdateCheck; set BEFORE initialize().
void setAutoUpdateEnabled(bool enabled)
{
autoUpdateEnabled_ = enabled;
}
// Get the path to the yt-dlp executable
[[nodiscard]] std::filesystem::path getExecutablePath() const;
// Check if yt-dlp is available
[[nodiscard]] bool isAvailable() const;
// Get yt-dlp version
[[nodiscard]] std::string getVersion() const;
// Force update yt-dlp to latest version; returns the verified
// version string of the freshly installed binary.
Api::Result<std::string> update();
// Execute yt-dlp with given arguments and return output
// (synchronous)
[[nodiscard]] std::string
execute(const std::vector<std::string>& args) const;
// Execute yt-dlp asynchronously and return a future
[[nodiscard]] std::future<YtDlpResult>
executeAsync(const std::vector<std::string>& args);
// Set cookie options for YouTube authentication
void setCookiesPath(const std::string& path);
void setCookiesFromBrowser(const std::string& browser);
// Get cookie arguments for yt-dlp command
[[nodiscard]] std::vector<std::string> getCookieArgs() const;
// Set proxy configuration for yt-dlp
void setProxyConfig(const std::optional<Void::Net::ProxyConfig>& proxy);
[[nodiscard]] std::optional<Void::Net::ProxyConfig>
getProxyConfig() const;
// Configure thread pool size for async operations (default: 4)
void setThreadPoolSize(size_t size);
[[nodiscard]] size_t getThreadPoolSize() const;
private:
// Get the storage directory for yt-dlp
[[nodiscard]] std::filesystem::path getStorageDir() const;
// Get the executable name based on platform
[[nodiscard]] std::string getExecutableName() const;
// Download yt-dlp from official source
Api::Result<void> downloadYtDlp();
// Make the given file executable (Unix only)
Api::Result<void> makeExecutable(const std::filesystem::path& path);
// Check if update is needed (older than 7 days)
[[nodiscard]] bool shouldUpdate() const;
// Get download URL based on platform
[[nodiscard]] std::string getDownloadUrl() const;
// Internal execute implementation
[[nodiscard]] YtDlpResult
executeInternal(const std::vector<std::string>& args) const;
// Get proxy arguments for yt-dlp command
[[nodiscard]] std::vector<std::string> getProxyArgs() const;
// Thread pool for async operations
void startThreadPool();
void stopThreadPool();
std::shared_ptr<NetworkClient> networkClient_;
std::filesystem::path executablePath_;
std::atomic<bool> initialized_{false};
std::atomic<bool> autoUpdateEnabled_{true};
// Background download/verify (joined in the destructor).
std::jthread initThread_;
mutable std::mutex mutex_;
// Cookie configuration for YouTube authentication
std::optional<std::string> cookiesPath_;
std::optional<std::string> cookiesFromBrowser_;
// Proxy configuration
std::optional<Void::Net::ProxyConfig> proxyConfig_;
mutable std::mutex proxyMutex_;
// Thread pool for async operations (Void worker pool; recreated
// on resize). Guarded by mutex_ for start/stop/submit.
size_t threadPoolSize_ = 4;
std::unique_ptr<Void::Concurrency::ThreadPool> pool_;
// yt-dlp download URLs
static constexpr std::string_view YTDLP_DOWNLOAD_LINUX_X86_64 =
"https://github.com/yt-dlp/yt-dlp/releases/latest/download/"
"yt-dlp_linux";
static constexpr std::string_view YTDLP_DOWNLOAD_LINUX_AARCH64 =
"https://github.com/yt-dlp/yt-dlp/releases/latest/download/"
"yt-dlp_linux_aarch64";
static constexpr std::string_view YTDLP_DOWNLOAD_WINDOWS =
"https://github.com/yt-dlp/yt-dlp/releases/latest/download/"
"yt-dlp.exe";
static constexpr std::string_view YTDLP_DOWNLOAD_MACOS =
"https://github.com/yt-dlp/yt-dlp/releases/latest/download/"
"yt-dlp_macos";
};
} // namespace Echoion::Core