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
+190
-81
No files matched your search
+60
-64
@@ -233,30 +233,54 @@ namespace Echoion::Core
|
||||
}
|
||||
|
||||
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();
|
||||
if (!openResult && networkConfig.proxy.has_value() &&
|
||||
networkConfig.proxy->enabled()) {
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
"Direct open failed, trying download through proxy...");
|
||||
auto downloadedPath = downloadAudioForPlayback(*streamUrl);
|
||||
if (downloadedPath) {
|
||||
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: {}",
|
||||
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() &&
|
||||
networkConfig.proxy->enabled()) {
|
||||
Void::Log::info("MusicManager", "{}",
|
||||
"Direct open failed, trying download via "
|
||||
"yt-dlp through proxy...");
|
||||
openResult = downloadAndOpen();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2128,55 +2152,27 @@ Subscriptions are persistent across server reconnects.)";
|
||||
}
|
||||
|
||||
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,
|
||||
"NetworkClient not available");
|
||||
"YouTube client not available");
|
||||
}
|
||||
|
||||
// Create temp file path
|
||||
auto cacheDir =
|
||||
std::filesystem::temp_directory_path() / "echoion_audio";
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(cacheDir, ec);
|
||||
|
||||
auto timestamp =
|
||||
std::chrono::system_clock::now().time_since_epoch().count();
|
||||
auto tempPath = cacheDir / std::format("audio_{}.tmp", timestamp);
|
||||
|
||||
// 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));
|
||||
// yt-dlp re-downloads through the configured proxy (it handles SOCKS5,
|
||||
// which ffmpeg cannot) and writes the audio to disk, sidestepping both
|
||||
// the IP-locked stream URL and any in-memory body limit. Re-download by
|
||||
// the original video id/URL, not the (IP-locked) CDN stream URL.
|
||||
const std::string idOrUrl =
|
||||
!track.originalUrl.empty() ? track.originalUrl : track.id;
|
||||
if (idOrUrl.empty()) {
|
||||
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||
"Track has no id/URL to download");
|
||||
}
|
||||
|
||||
if (response->status != 200) {
|
||||
return Api::fail(
|
||||
Api::ErrorCode::NetworkError,
|
||||
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();
|
||||
const auto cacheDir =
|
||||
(std::filesystem::temp_directory_path() / "echoion_audio").string();
|
||||
return youTubeClient_->downloadAudioToFile(idOrUrl, cacheDir);
|
||||
}
|
||||
|
||||
void MusicManager::cleanupTempFile(const std::string& filepath)
|
||||
|
||||
Reference in new issue
Block a user