Implement reload commands; fix SOCKS5 playback; API->yt-dlp search fallback
F1 (reload commands): Core registers itself as Api::ICoreInterface in the service registry; !reloadconfig and !reload resolve it via ctx.service and call Core::reloadConfig() (which reloads config.json from disk), replacing the old placeholder replies. F2 (SOCKS5 playback): ffmpeg can't use a SOCKS5 proxy, so it connected directly to the IP-locked googlevideo URL and timed out; the fallback then buffered the whole (705 MB) body in memory and tripped Void::Net's body limit. New YouTubeClient::downloadAudioToFile() re-downloads the audio with yt-dlp (which handles SOCKS5 + writes to disk) by video id and ffmpeg opens the local file. MusicManager::downloadAudioForPlayback now uses it, and the play-open path short-circuits straight to the download when the proxy is SOCKS5 (skipping the doomed ~15 s ffmpeg attempt); HTTP/no-proxy keeps stream-first-then-download. F3 (API fallback): YouTubeClient::search() now falls back to yt-dlp when the Data API path errors (bad/expired key, quota, network), mirroring what fetchPlaylistItems already did. Build green; runtime behaviour to be verified on the live bot. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
1efdbe0771
commit
499143050b
8 files changed
+187
-78
No files matched your search
@@ -125,7 +125,7 @@ namespace Echoion::Core
|
|||||||
// Additional methods for internal use (not in interface)
|
// Additional methods for internal use (not in interface)
|
||||||
void setAudioDataCallback(
|
void setAudioDataCallback(
|
||||||
const MusicManager::AudioDataCallback& callback);
|
const MusicManager::AudioDataCallback& callback);
|
||||||
Api::Result<void> reloadConfig();
|
Api::Result<void> reloadConfig() override;
|
||||||
|
|
||||||
// Internal implementations using full types
|
// Internal implementations using full types
|
||||||
Api::Result<void> playTrack(const Api::TrackInfo& track);
|
Api::Result<void> playTrack(const Api::TrackInfo& track);
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ namespace Echoion::Core
|
|||||||
|
|
||||||
// Download audio through proxy for playback
|
// Download audio through proxy for playback
|
||||||
[[nodiscard]] Api::Result<std::string>
|
[[nodiscard]] Api::Result<std::string>
|
||||||
downloadAudioForPlayback(const std::string& url);
|
downloadAudioForPlayback(const Api::TrackInfo& track);
|
||||||
void cleanupTempFile(const std::string& filepath);
|
void cleanupTempFile(const std::string& filepath);
|
||||||
|
|
||||||
// Prefetch next track for seamless transitions
|
// Prefetch next track for seamless transitions
|
||||||
|
|||||||
@@ -101,6 +101,14 @@ namespace Echoion::Core
|
|||||||
[[nodiscard]] Api::Result<std::string>
|
[[nodiscard]] Api::Result<std::string>
|
||||||
getAudioStreamUrl(std::string_view videoId);
|
getAudioStreamUrl(std::string_view videoId);
|
||||||
|
|
||||||
|
// Download the best audio to a file via yt-dlp (used when the
|
||||||
|
// stream URL can't be opened directly — e.g. behind a SOCKS5 proxy
|
||||||
|
// that ffmpeg can't use). yt-dlp applies the proxy + cookies and
|
||||||
|
// writes to disk; returns the resulting file path.
|
||||||
|
[[nodiscard]] Api::Result<std::string>
|
||||||
|
downloadAudioToFile(std::string_view videoIdOrUrl,
|
||||||
|
const std::string& outDir);
|
||||||
|
|
||||||
// Configure cookie options for yt-dlp
|
// Configure cookie options for yt-dlp
|
||||||
void setCookiesPath(const std::string& path);
|
void setCookiesPath(const std::string& path);
|
||||||
void setCookiesFromBrowser(const std::string& browser);
|
void setCookiesFromBrowser(const std::string& browser);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Command.hpp"
|
#include "Command.hpp"
|
||||||
|
#include <CoreInterface.hpp>
|
||||||
#include <Void/Log/Log.hpp>
|
#include <Void/Log/Log.hpp>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
|
||||||
@@ -13,11 +14,24 @@ namespace Echoion::Core
|
|||||||
|
|
||||||
void execute(const CommandContext& ctx) override
|
void execute(const CommandContext& ctx) override
|
||||||
{
|
{
|
||||||
// This is a placeholder - actual reload is handled by the
|
Void::Log::info("ReloadCommand", "{}",
|
||||||
// plugin
|
"Reload requested by user");
|
||||||
Void::Log::info("ReloadCommand", "{}", "Reload requested by user");
|
|
||||||
ctx.reply("Reload is handled automatically by the plugin. Use "
|
// Reload configuration from disk. A full core/plugin reload is
|
||||||
"the TS3 plugin menu to reload the core.");
|
// driven from the TS plugin menu (that lives in the plugin).
|
||||||
|
if (auto core = ctx.service<Api::ICoreInterface>()) {
|
||||||
|
auto result = core->reloadConfig();
|
||||||
|
if (result) {
|
||||||
|
ctx.reply("✅ Configuration reloaded. For a full core "
|
||||||
|
"reload, use the TS plugin menu.");
|
||||||
|
} else {
|
||||||
|
ctx.reply(std::string("❌ Config reload failed: ") +
|
||||||
|
result.error().message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ctx.reply("Reload is handled by the plugin. Use the TS "
|
||||||
|
"plugin menu to reload the core.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view getDescription() const override
|
std::string_view getDescription() const override
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Command.hpp"
|
#include "Command.hpp"
|
||||||
|
#include <CoreInterface.hpp>
|
||||||
#include <Void/Log/Log.hpp>
|
#include <Void/Log/Log.hpp>
|
||||||
#include "echoion/core/Core.hpp" // For Core::reloadConfig
|
|
||||||
#include <format>
|
#include <format>
|
||||||
|
|
||||||
namespace Echoion::Core
|
namespace Echoion::Core
|
||||||
@@ -15,15 +15,19 @@ namespace Echoion::Core
|
|||||||
void execute(const CommandContext& ctx) override
|
void execute(const CommandContext& ctx) override
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// Note: ctx.core is not directly accessible, we need to use
|
auto core = ctx.service<Api::ICoreInterface>();
|
||||||
// the MusicManager's core reference For now, reply with
|
if (!core) {
|
||||||
// instructions
|
ctx.reply("Config reload is unavailable right now.");
|
||||||
ctx.reply(
|
return;
|
||||||
"Config reload: Please use !reload command or restart "
|
}
|
||||||
"the bot for config changes to take effect.");
|
|
||||||
|
|
||||||
// TODO: Add core reference to CommandContext so commands
|
auto result = core->reloadConfig();
|
||||||
// can call Core methods
|
if (result) {
|
||||||
|
ctx.reply("✅ Configuration reloaded from disk.");
|
||||||
|
} else {
|
||||||
|
ctx.reply(std::string("❌ Failed to reload config: ") +
|
||||||
|
result.error().message);
|
||||||
|
}
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
Void::Log::error("ReloadConfigCommand", "{}", std::format(
|
Void::Log::error("ReloadConfigCommand", "{}", std::format(
|
||||||
"ReloadConfig command exception: {}", e.what()));
|
"ReloadConfig command exception: {}", e.what()));
|
||||||
|
|||||||
@@ -353,6 +353,10 @@ namespace Echoion::Core
|
|||||||
registry_.provide<Api::ConfigManager>(
|
registry_.provide<Api::ConfigManager>(
|
||||||
alias(&configManager_));
|
alias(&configManager_));
|
||||||
registry_.provide<Void::EventBus::EventBus>(alias(&eventBus_));
|
registry_.provide<Void::EventBus::EventBus>(alias(&eventBus_));
|
||||||
|
// Expose Core itself so commands can reach Core operations
|
||||||
|
// (e.g. reloadConfig) through the registry.
|
||||||
|
registry_.provide<Api::ICoreInterface>(
|
||||||
|
alias(static_cast<Api::ICoreInterface*>(this)));
|
||||||
if (networkClient_) {
|
if (networkClient_) {
|
||||||
registry_.provide<Api::INetworkClient>(networkClient_);
|
registry_.provide<Api::INetworkClient>(networkClient_);
|
||||||
}
|
}
|
||||||
|
|||||||
+58
-62
@@ -233,30 +233,54 @@ namespace Echoion::Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
Void::Log::debug("MusicManager", "{}", "Opening audio source...");
|
Void::Log::debug("MusicManager", "{}", "Opening audio source...");
|
||||||
auto openResult = audioDecoder_->openSource(*streamUrl);
|
|
||||||
|
|
||||||
// If direct open fails and we have a proxy, try downloading through
|
|
||||||
// proxy
|
|
||||||
const auto& networkConfig = configManager_->getNetwork();
|
const auto& networkConfig = configManager_->getNetwork();
|
||||||
|
const bool socks5Proxy =
|
||||||
|
networkConfig.proxy.has_value() &&
|
||||||
|
networkConfig.proxy->enabled() &&
|
||||||
|
networkConfig.proxy->kind ==
|
||||||
|
Void::Net::ProxyConfig::Kind::Socks5;
|
||||||
|
|
||||||
|
// Download the audio via yt-dlp and open the resulting local file.
|
||||||
|
auto downloadAndOpen = [&]() -> Api::Result<void> {
|
||||||
|
auto downloadedPath =
|
||||||
|
downloadAudioForPlayback(currentTrack_->track);
|
||||||
|
if (!downloadedPath) {
|
||||||
|
Void::Log::warn(
|
||||||
|
"MusicManager", "{}",
|
||||||
|
std::format("Failed to download audio: {}",
|
||||||
|
downloadedPath.error().message));
|
||||||
|
return std::unexpected(downloadedPath.error());
|
||||||
|
}
|
||||||
|
Void::Log::info("MusicManager", "{}",
|
||||||
|
std::format("Downloaded audio to: {}",
|
||||||
|
*downloadedPath));
|
||||||
|
auto opened = audioDecoder_->openSource(*downloadedPath);
|
||||||
|
if (opened) {
|
||||||
|
std::unique_lock<std::shared_mutex> lock(tempFilesMutex_);
|
||||||
|
tempFiles_.insert(*downloadedPath);
|
||||||
|
}
|
||||||
|
return opened;
|
||||||
|
};
|
||||||
|
|
||||||
|
Api::Result<void> openResult;
|
||||||
|
if (socks5Proxy) {
|
||||||
|
// ffmpeg can't use a SOCKS5 proxy for the IP-locked stream URL;
|
||||||
|
// go straight to the yt-dlp download (which can).
|
||||||
|
Void::Log::info(
|
||||||
|
"MusicManager", "{}",
|
||||||
|
"SOCKS5 proxy configured: downloading audio via yt-dlp...");
|
||||||
|
openResult = downloadAndOpen();
|
||||||
|
} else {
|
||||||
|
openResult = audioDecoder_->openSource(*streamUrl);
|
||||||
|
// HTTP/no proxy: if a direct open fails but a proxy is set,
|
||||||
|
// fall back to a yt-dlp download through it.
|
||||||
if (!openResult && networkConfig.proxy.has_value() &&
|
if (!openResult && networkConfig.proxy.has_value() &&
|
||||||
networkConfig.proxy->enabled()) {
|
networkConfig.proxy->enabled()) {
|
||||||
Void::Log::info("MusicManager", "{}",
|
Void::Log::info("MusicManager", "{}",
|
||||||
"Direct open failed, trying download through proxy...");
|
"Direct open failed, trying download via "
|
||||||
auto downloadedPath = downloadAudioForPlayback(*streamUrl);
|
"yt-dlp through proxy...");
|
||||||
if (downloadedPath) {
|
openResult = downloadAndOpen();
|
||||||
Void::Log::info("MusicManager", "{}", std::format("Downloaded audio to: {}",
|
|
||||||
*downloadedPath));
|
|
||||||
openResult = audioDecoder_->openSource(*downloadedPath);
|
|
||||||
if (openResult) {
|
|
||||||
// Track temp file for cleanup
|
|
||||||
std::unique_lock<std::shared_mutex> lock(
|
|
||||||
tempFilesMutex_);
|
|
||||||
tempFiles_.insert(*downloadedPath);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Void::Log::warn("MusicManager", "{}",
|
|
||||||
std::format("Failed to download through proxy: {}",
|
|
||||||
downloadedPath.error().message));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2128,55 +2152,27 @@ Subscriptions are persistent across server reconnects.)";
|
|||||||
}
|
}
|
||||||
|
|
||||||
Api::Result<std::string>
|
Api::Result<std::string>
|
||||||
MusicManager::downloadAudioForPlayback(const std::string& url)
|
MusicManager::downloadAudioForPlayback(const Api::TrackInfo& track)
|
||||||
{
|
{
|
||||||
if (!networkClient_) {
|
if (!youTubeClient_) {
|
||||||
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||||
"NetworkClient not available");
|
"YouTube client not available");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create temp file path
|
// yt-dlp re-downloads through the configured proxy (it handles SOCKS5,
|
||||||
auto cacheDir =
|
// which ffmpeg cannot) and writes the audio to disk, sidestepping both
|
||||||
std::filesystem::temp_directory_path() / "echoion_audio";
|
// the IP-locked stream URL and any in-memory body limit. Re-download by
|
||||||
std::error_code ec;
|
// the original video id/URL, not the (IP-locked) CDN stream URL.
|
||||||
std::filesystem::create_directories(cacheDir, ec);
|
const std::string idOrUrl =
|
||||||
|
!track.originalUrl.empty() ? track.originalUrl : track.id;
|
||||||
auto timestamp =
|
if (idOrUrl.empty()) {
|
||||||
std::chrono::system_clock::now().time_since_epoch().count();
|
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||||
auto tempPath = cacheDir / std::format("audio_{}.tmp", timestamp);
|
"Track has no id/URL to download");
|
||||||
|
|
||||||
// Download the file
|
|
||||||
Void::Log::info("MusicManager", "{}",
|
|
||||||
std::format("Downloading audio from: {}...", url.substr(0, 60)));
|
|
||||||
|
|
||||||
auto response = networkClient_->get(url);
|
|
||||||
if (!response) {
|
|
||||||
return Api::fail(Api::ErrorCode::NetworkError,
|
|
||||||
std::format("Failed to download audio: {}",
|
|
||||||
response.error().message));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response->status != 200) {
|
const auto cacheDir =
|
||||||
return Api::fail(
|
(std::filesystem::temp_directory_path() / "echoion_audio").string();
|
||||||
Api::ErrorCode::NetworkError,
|
return youTubeClient_->downloadAudioToFile(idOrUrl, cacheDir);
|
||||||
std::format("HTTP error {}: {}", response->status,
|
|
||||||
response->body.substr(0, 100)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write to temp file
|
|
||||||
std::ofstream file(tempPath, std::ios::binary);
|
|
||||||
if (!file) {
|
|
||||||
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
|
||||||
"Failed to create temp file");
|
|
||||||
}
|
|
||||||
|
|
||||||
file.write(response->body.data(), response->body.size());
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
Void::Log::info("MusicManager", "{}", std::format("Downloaded {} bytes to {}",
|
|
||||||
response->body.size(), tempPath.string()));
|
|
||||||
|
|
||||||
return tempPath.string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MusicManager::cleanupTempFile(const std::string& filepath)
|
void MusicManager::cleanupTempFile(const std::string& filepath)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
#include <filesystem>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
@@ -226,6 +227,77 @@ namespace Echoion::Core
|
|||||||
Api::ErrorCode::NotFound, "Failed to get audio URL from yt-dlp");
|
Api::ErrorCode::NotFound, "Failed to get audio URL from yt-dlp");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Api::Result<std::string>
|
||||||
|
YouTubeClient::downloadAudioToFile(std::string_view videoIdOrUrl,
|
||||||
|
const std::string& outDir)
|
||||||
|
{
|
||||||
|
if (!initialized_ || !ytDlpManager_->isAvailable()) {
|
||||||
|
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||||
|
"yt-dlp not available");
|
||||||
|
}
|
||||||
|
if (videoIdOrUrl.empty()) {
|
||||||
|
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||||
|
"Video ID/URL is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
std::filesystem::create_directories(outDir, ec);
|
||||||
|
|
||||||
|
const std::string id(videoIdOrUrl);
|
||||||
|
const std::string outTemplate = outDir + "/%(id)s.%(ext)s";
|
||||||
|
|
||||||
|
auto startTime = std::chrono::steady_clock::now();
|
||||||
|
Void::Log::info(
|
||||||
|
"YouTubeClient", "{}",
|
||||||
|
std::format("Downloading audio via yt-dlp: {}", id));
|
||||||
|
|
||||||
|
// yt-dlp downloads through the configured proxy + cookies and writes to
|
||||||
|
// disk; --print after_move:filepath emits the final path on stdout.
|
||||||
|
auto future = ytDlpManager_->executeAsync(
|
||||||
|
{id, "-f", "bestaudio/251/140", "-o", outTemplate, "--no-playlist",
|
||||||
|
"--no-warnings", "--print", "after_move:filepath"});
|
||||||
|
|
||||||
|
auto waitStatus = future.wait_for(std::chrono::seconds(180));
|
||||||
|
if (waitStatus == std::future_status::timeout) {
|
||||||
|
return Api::fail(Api::ErrorCode::NetworkError,
|
||||||
|
"yt-dlp audio download timed out");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ytdlpResult = future.get();
|
||||||
|
if (!ytdlpResult.success) {
|
||||||
|
return Api::fail(Api::ErrorCode::NetworkError,
|
||||||
|
"yt-dlp audio download failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// The emitted path is the last non-empty stdout line.
|
||||||
|
std::string path;
|
||||||
|
{
|
||||||
|
std::istringstream stream(ytdlpResult.output);
|
||||||
|
for (std::string line; std::getline(stream, line);) {
|
||||||
|
while (!line.empty() &&
|
||||||
|
(line.back() == '\r' || line.back() == '\n')) {
|
||||||
|
line.pop_back();
|
||||||
|
}
|
||||||
|
if (!line.empty()) {
|
||||||
|
path = line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path.empty() || !std::filesystem::exists(path)) {
|
||||||
|
return Api::fail(Api::ErrorCode::NotFound,
|
||||||
|
"yt-dlp reported no downloaded file");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
|
std::chrono::steady_clock::now() - startTime)
|
||||||
|
.count();
|
||||||
|
Void::Log::info("YouTubeClient", "{}",
|
||||||
|
std::format("Downloaded audio to {} in {} ms", path,
|
||||||
|
elapsed));
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
Api::Result<std::string>
|
Api::Result<std::string>
|
||||||
YouTubeClient::getStreamUrl(std::string_view videoId)
|
YouTubeClient::getStreamUrl(std::string_view videoId)
|
||||||
{
|
{
|
||||||
@@ -368,6 +440,17 @@ namespace Echoion::Core
|
|||||||
Void::Log::info("YouTubeClient", "{}",
|
Void::Log::info("YouTubeClient", "{}",
|
||||||
std::format("Searching YouTube via API: '{}'", query));
|
std::format("Searching YouTube via API: '{}'", query));
|
||||||
result = searchWithApi(query, pageToken);
|
result = searchWithApi(query, pageToken);
|
||||||
|
|
||||||
|
// Fall back to yt-dlp if the API path failed (bad/expired key,
|
||||||
|
// quota exceeded, network error, etc.).
|
||||||
|
if (!result) {
|
||||||
|
Void::Log::warn(
|
||||||
|
"YouTubeClient", "{}",
|
||||||
|
std::format("YouTube API search failed ({}), falling back "
|
||||||
|
"to yt-dlp",
|
||||||
|
result.error().message));
|
||||||
|
result = searchWithYtDlp(query, pageToken);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Fallback to yt-dlp if no API key
|
// Fallback to yt-dlp if no API key
|
||||||
Void::Log::warn("YouTubeClient", "{}", "No YouTube API key configured, falling back "
|
Void::Log::warn("YouTubeClient", "{}", "No YouTube API key configured, falling back "
|
||||||
|
|||||||
Reference in new issue
Block a user