Files
faumaray d146f11f02 Release 1.0.0
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-04 01:49:28 +03:00

87 lines
2.8 KiB
C++

#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