Release 1.0.0

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-04 01:49:28 +03:00
1 parent df99b6ea7f
commit d146f11f02
63 files changed
+8331 -2709

No files matched your search

@@ -0,0 +1,34 @@
#pragma once
#include "TrackSource.hpp"
namespace Echoion::Core
{
// Pass-through for URLs that already point at an audio stream (radio
// streams, .mp3/.ogg/.opus/.m3u8 files, icecast mounts). No network work
// at resolve time — ffmpeg opens the URL directly.
class DirectUrlSource : public ITrackSource
{
public:
[[nodiscard]] std::string_view name() const override
{
return "direct-url";
}
[[nodiscard]] bool canHandle(const std::string& query) const override;
[[nodiscard]] bool isAvailable() const override { return true; }
[[nodiscard]] bool supportsSearch() const override
{
return false;
}
Api::Result<Api::SearchResult>
search(std::string_view query) override;
Api::Result<Api::TrackInfo>
resolve(const std::string& urlOrId) override;
Api::Result<StreamHandle>
getStream(const Api::TrackInfo& track) override;
};
} // namespace Echoion::Core
@@ -0,0 +1,86 @@
#pragma once
#include "../NetworkClient.hpp"
#include "../SignatureCipher.hpp"
#include "TrackSource.hpp"
#include <atomic>
#include <memory>
#include <mutex>
namespace Echoion::Core
{
// Native YouTube resolution through the innertube player API — one HTTP
// POST instead of a yt-dlp subprocess, so it is by far the fastest
// YouTube path. Requests go through the configured proxy (NetworkClient
// applies it); youtubei is ISP-blocked here, so with requireProxy=true
// (default) the source reports unavailable when no proxy is set and the
// router skips it.
//
// Client strategy: the IOS client historically returns direct
// adaptiveFormats[].url with no signature deciphering. When it yields
// nothing playable, the WEB client + SignatureCipher (quickjs solver)
// deciphers signatureCipher/n-param challenges.
class InnertubeSource : public ITrackSource
{
public:
// endpointOverride points tests at a StubHttpServer; production
// passes nothing and talks to https://www.youtube.com.
explicit InnertubeSource(std::shared_ptr<NetworkClient> network,
std::string endpointOverride = "");
void configure(bool enabled, bool requireProxy);
[[nodiscard]] std::string_view name() const override
{
return "innertube";
}
[[nodiscard]] bool canHandle(const std::string& query) const override;
[[nodiscard]] bool isAvailable() const override;
[[nodiscard]] bool supportsSearch() const override
{
return false; // text search runs through the yt-dlp sources
}
Api::Result<Api::SearchResult>
search(std::string_view query) override;
Api::Result<Api::TrackInfo>
resolve(const std::string& urlOrId) override;
Api::Result<StreamHandle>
getStream(const Api::TrackInfo& track) override;
private:
struct PlayerResponse {
std::string title;
std::string author;
int64_t lengthSeconds = 0;
std::string thumbnailUrl;
std::string streamUrl; // direct url when present
std::string signatureCipher; // needs deciphering
};
// POST youtubei/v1/player with the given client context; parses
// the best audio format out of the response.
Api::Result<PlayerResponse> callPlayer(const std::string& videoId,
bool webClient);
// WEB fallback: fetch the watch page + player JS, feed the
// cipher, decipher the signatureCipher/n-param challenge.
Api::Result<std::string>
decipherWithWebPlayer(const std::string& videoId,
const std::string& signatureCipher);
std::shared_ptr<NetworkClient> network_;
std::string endpoint_;
std::atomic<bool> enabled_{true};
std::atomic<bool> requireProxy_{true};
// Lazy cipher state (only the WEB fallback needs it).
std::mutex cipherMutex_;
std::unique_ptr<SignatureCipher> cipher_;
std::string loadedPlayerJsUrl_;
};
} // namespace Echoion::Core
@@ -0,0 +1,74 @@
#pragma once
#include "TrackSource.hpp"
#include <chrono>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
namespace Echoion::Core
{
// Ordered chain of track sources with per-source failure cooldown and a
// stream-URL cache. Resolution walks the configured order and takes the
// first source that canHandle() the query, is available, and succeeds;
// failures put a source on cooldown (skipped unless it is the last
// candidate) and fall through to the next.
class SourceRouter
{
public:
void setSources(std::vector<std::shared_ptr<ITrackSource>> sources);
// Reorder/enable per config; names not found are ignored, sources
// not named are dropped from the chain.
void setOrder(const std::vector<std::string>& order);
void setFailureCooldown(std::chrono::milliseconds cooldown);
// Hedged getStream: when the first candidate hasn't answered
// within this delay, race the second one (first success wins).
// 0 disables hedging.
void setHedgeDelay(std::chrono::milliseconds delay);
// Text search across the first searching source that succeeds.
[[nodiscard]] Api::Result<Api::SearchResult>
search(std::string_view query);
// URL/id/text -> track metadata.
[[nodiscard]] Api::Result<Api::TrackInfo>
resolve(const std::string& query);
// Track -> playable stream handle (cached).
[[nodiscard]] Api::Result<StreamHandle>
getStream(const Api::TrackInfo& track);
// A cached/served handle failed to open: evict it and demote the
// producing source so the next resolve tries elsewhere.
void invalidate(const std::string& trackId);
private:
struct CachedStream {
StreamHandle handle;
std::chrono::steady_clock::time_point expiry;
};
struct SourceState {
std::shared_ptr<ITrackSource> source;
std::chrono::steady_clock::time_point cooldownUntil{};
};
[[nodiscard]] std::vector<SourceState*>
candidatesFor(const std::string& query, bool needSearch);
void reportFailure(SourceState& state);
mutable std::mutex mutex_;
std::vector<std::shared_ptr<ITrackSource>> allSources_;
std::vector<SourceState> chain_;
std::chrono::milliseconds cooldown_{std::chrono::minutes(5)};
std::chrono::milliseconds hedgeDelay_{std::chrono::milliseconds(300)};
std::unordered_map<std::string, CachedStream> streamCache_;
};
} // namespace Echoion::Core
@@ -0,0 +1,57 @@
#pragma once
#include <TrackTypes.hpp>
#include <Types.hpp>
#include <string>
#include <string_view>
namespace Echoion::Core
{
// A playable handle produced by a source: either an http(s) URL the
// decoder can stream, or a local file path (downloaded audio) the caller
// must delete after playback.
struct StreamHandle {
std::string url;
bool isLocalFile = false;
std::string sourceName; // for logging / failure attribution
};
// One catalog/stream backend in the resolution chain. Implementations:
// InnertubeSource (native YouTube player API), YandexMusicSource (native
// Yandex Music), DirectUrlSource (raw audio URLs), YtDlpSource (direct
// and proxied yt-dlp).
class ITrackSource
{
public:
virtual ~ITrackSource() = default;
[[nodiscard]] virtual std::string_view name() const = 0;
// Can this source resolve the query at all? URLs are matched by
// host/scheme; bare text search is handled only by
// YouTube-catalog sources.
[[nodiscard]] virtual bool
canHandle(const std::string& query) const = 0;
// Health gate — sources with no healthy instances report false
// and are skipped by the router.
[[nodiscard]] virtual bool isAvailable() const = 0;
// Does this source implement text search?
[[nodiscard]] virtual bool supportsSearch() const = 0;
virtual Api::Result<Api::SearchResult>
search(std::string_view query) = 0;
// Resolve metadata for a URL or id this source canHandle().
virtual Api::Result<Api::TrackInfo>
resolve(const std::string& urlOrId) = 0;
// Produce a playable handle for a resolved track.
virtual Api::Result<StreamHandle>
getStream(const Api::TrackInfo& track) = 0;
};
} // namespace Echoion::Core
@@ -0,0 +1,79 @@
#pragma once
#include "../NetworkClient.hpp"
#include "TrackSource.hpp"
#include <atomic>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <vector>
namespace Echoion::Core
{
// Native Yandex Music source (unofficial api.music.yandex.net). Full
// tracks require a Yandex OAuth token (sources.yandexToken) from an
// account with Plus; without one this source reports unavailable and
// yandex URLs keep routing to ytdlp-direct.
//
// Yandex is reachable directly in this deployment's region and may
// geo-block foreign proxy exits, so every request deliberately bypasses
// the configured proxy (NetworkClient::getDirect).
class YandexMusicSource : public ITrackSource
{
public:
// endpointOverride points tests at a StubHttpServer (it replaces
// both the API base and the download-info hosts).
explicit YandexMusicSource(std::shared_ptr<NetworkClient> network,
std::string endpointOverride = "");
void configure(bool enabled, std::string token);
[[nodiscard]] std::string_view name() const override
{
return "yandex";
}
[[nodiscard]] bool canHandle(const std::string& query) const override;
[[nodiscard]] bool isAvailable() const override;
[[nodiscard]] bool supportsSearch() const override
{
return false;
}
Api::Result<Api::SearchResult>
search(std::string_view query) override;
Api::Result<Api::TrackInfo>
resolve(const std::string& urlOrId) override;
Api::Result<StreamHandle>
getStream(const Api::TrackInfo& track) override;
// Album/playlist track listing for queue imports.
[[nodiscard]] bool isCollectionUrl(const std::string& url) const;
Api::Result<std::vector<Api::TrackInfo>>
playlistTracks(const std::string& url);
// Related/similar tracks for radio autoplay. trackId is the bare
// Yandex numeric id (no "yandex:" prefix).
Api::Result<std::vector<Api::TrackInfo>>
related(const std::string& trackId, size_t limit = 10);
// Extract the bare Yandex track id from a music.yandex.* URL, if it
// is a single-track URL. Used to seed radio from history.
[[nodiscard]] std::optional<std::string>
trackIdFromUrl(const std::string& url) const;
private:
Api::Result<std::string> apiGet(const std::string& path);
[[nodiscard]] std::string tokenSnapshot() const;
std::shared_ptr<NetworkClient> network_;
std::string endpoint_;
std::atomic<bool> enabled_{true};
mutable std::mutex tokenMutex_;
std::string token_;
};
} // namespace Echoion::Core
@@ -0,0 +1,58 @@
#pragma once
#include "../YouTubeClient.hpp"
#include "TrackSource.hpp"
#include <memory>
namespace Echoion::Core
{
// yt-dlp-backed source, in two router entries over the same
// YouTubeClient:
// - Direct ("ytdlp-direct", useProxy=false): non-YouTube extractor URLs
// (SoundCloud, Bandcamp, VK, Yandex, radio pages) reachable without a
// proxy. Never handles bare text search.
// - Proxied ("ytdlp-proxied", useProxy=true): last-resort YouTube
// resolution + text search through the configured proxy; also owns
// the SOCKS5 download-to-file path.
class YtDlpSource : public ITrackSource
{
public:
enum class Mode { Direct, Proxied };
YtDlpSource(std::shared_ptr<YouTubeClient> client, Mode mode,
std::string tempDir);
[[nodiscard]] std::string_view name() const override
{
return mode_ == Mode::Direct ? "ytdlp-direct"
: "ytdlp-proxied";
}
[[nodiscard]] bool canHandle(const std::string& query) const override;
[[nodiscard]] bool isAvailable() const override;
[[nodiscard]] bool supportsSearch() const override
{
return mode_ == Mode::Proxied;
}
Api::Result<Api::SearchResult>
search(std::string_view query) override;
Api::Result<Api::TrackInfo>
resolve(const std::string& urlOrId) override;
Api::Result<StreamHandle>
getStream(const Api::TrackInfo& track) override;
// Force the download-to-file path on the next getStream (set by
// the router's preferDownload retry).
Api::Result<StreamHandle>
getStreamViaDownload(const Api::TrackInfo& track);
private:
std::shared_ptr<YouTubeClient> client_;
Mode mode_;
std::string tempDir_;
};
} // namespace Echoion::Core