Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-06-25 01:58:56 +03:00
1 parent 583e4e752d
commit 1f26927ad6
2 files changed
+59 -40

No files matched your search

+58 -39
View File
@@ -1,5 +1,5 @@
#include <Void/Log/Log.hpp>
#include "echoion/core/AudioDecoder.hpp" #include "echoion/core/AudioDecoder.hpp"
#include <Void/Log/Log.hpp>
extern "C" { extern "C" {
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
@@ -41,7 +41,8 @@ namespace Echoion::Core
if (hwConfig.isEnabled()) { if (hwConfig.isEnabled()) {
auto result = initializeHardwareAccel(hwConfig); auto result = initializeHardwareAccel(hwConfig);
if (!result) { if (!result) {
Void::Log::warn("AudioDecoder", "{}", Void::Log::warn(
"AudioDecoder", "{}",
std::format("Hardware acceleration init failed: {}, " std::format("Hardware acceleration init failed: {}, "
"falling back to software", "falling back to software",
result.error().message)); result.error().message));
@@ -76,7 +77,7 @@ namespace Echoion::Core
break; break;
default: default:
return Api::fail(Api::ErrorCode::InvalidArgument, return Api::fail(Api::ErrorCode::InvalidArgument,
"Unsupported hardware acceleration type"); "Unsupported hardware acceleration type");
} }
if (av_hwdevice_find_type_by_name(av_hwdevice_get_type_name(hwType)) == if (av_hwdevice_find_type_by_name(av_hwdevice_get_type_name(hwType)) ==
@@ -87,8 +88,9 @@ namespace Echoion::Core
} }
hwAccelType_ = config.type; hwAccelType_ = config.type;
Void::Log::info("AudioDecoder", "{}", std::format("Hardware acceleration configured: {}", Void::Log::info("AudioDecoder", "{}",
av_hwdevice_get_type_name(hwType))); std::format("Hardware acceleration configured: {}",
av_hwdevice_get_type_name(hwType)));
return {}; return {};
} }
@@ -121,8 +123,8 @@ namespace Echoion::Core
Api::Result<void> AudioDecoder::openSource(const std::string& url) Api::Result<void> AudioDecoder::openSource(const std::string& url)
{ {
if (!initialized_) { if (!initialized_) {
return Api::fail( return Api::fail(Api::ErrorCode::ServiceUnavailable,
Api::ErrorCode::ServiceUnavailable, "Decoder not initialized"); "Decoder not initialized");
} }
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
@@ -160,7 +162,7 @@ namespace Echoion::Core
// protocol handler, causing proxy bypass. // protocol handler, causing proxy bypass.
if (proxyConfig_->has_auth()) { if (proxyConfig_->has_auth()) {
proxyUrl = std::format( proxyUrl = std::format(
"socks5://{}:{}@{}:{}", *proxyConfig_->username, "socks5h://{}:{}@{}:{}", *proxyConfig_->username,
*proxyConfig_->password, proxyConfig_->host, *proxyConfig_->password, proxyConfig_->host,
proxyConfig_->port); proxyConfig_->port);
} else { } else {
@@ -180,19 +182,25 @@ namespace Echoion::Core
} }
av_dict_set(&options, "proxy", proxyUrl.c_str(), 0); av_dict_set(&options, "proxy", proxyUrl.c_str(), 0);
Void::Log::info("AudioDecoder", "{}", std::format("Using proxy for ffmpeg: {}:{}",
proxyConfig_->host, av_dict_set(
proxyConfig_->port)); &options, "protocol_whitelist",
"file,http,https,tls,tcp,udp,crypto,httpproxy,socks,socks5", 0);
Void::Log::info("AudioDecoder", "{}",
std::format("Using proxy for ffmpeg: {}:{}",
proxyConfig_->host,
proxyConfig_->port));
} }
Void::Log::debug("AudioDecoder", "{}", Void::Log::debug(
"AudioDecoder", "{}",
std::format("Opening audio source: {}", url.substr(0, 80))); std::format("Opening audio source: {}", url.substr(0, 80)));
// Allocate format context first (needed for interrupt callback) // Allocate format context first (needed for interrupt callback)
formatContext_ = avformat_alloc_context(); formatContext_ = avformat_alloc_context();
if (!formatContext_) { if (!formatContext_) {
return Api::fail(Api::ErrorCode::AudioError, return Api::fail(Api::ErrorCode::AudioError,
"Failed to allocate format context"); "Failed to allocate format context");
} }
// Set up interrupt callback to allow graceful shutdown during blocking // Set up interrupt callback to allow graceful shutdown during blocking
@@ -214,8 +222,10 @@ namespace Echoion::Core
if (ret < 0) { if (ret < 0) {
char errbuf[256]; char errbuf[256];
av_strerror(ret, errbuf, sizeof(errbuf)); av_strerror(ret, errbuf, sizeof(errbuf));
Void::Log::error("AudioDecoder", "{}", std::format( Void::Log::error(
"FFmpeg failed to open input (error {}): {}", ret, errbuf)); "AudioDecoder", "{}",
std::format("FFmpeg failed to open input (error {}): {}", ret,
errbuf));
return Api::fail( return Api::fail(
Api::ErrorCode::AudioError, Api::ErrorCode::AudioError,
std::format( std::format(
@@ -228,7 +238,8 @@ namespace Echoion::Core
if (ret < 0) { if (ret < 0) {
char errbuf[256]; char errbuf[256];
av_strerror(ret, errbuf, sizeof(errbuf)); av_strerror(ret, errbuf, sizeof(errbuf));
Void::Log::error("AudioDecoder", "{}", Void::Log::error(
"AudioDecoder", "{}",
std::format("FFmpeg failed to find stream info (error {}): {}", std::format("FFmpeg failed to find stream info (error {}): {}",
ret, errbuf)); ret, errbuf));
avformat_close_input(&formatContext_); avformat_close_input(&formatContext_);
@@ -245,14 +256,14 @@ namespace Echoion::Core
if (audioStreamIndex_ == -1) { if (audioStreamIndex_ == -1) {
avformat_close_input(&formatContext_); avformat_close_input(&formatContext_);
return Api::fail(Api::ErrorCode::AudioError, return Api::fail(Api::ErrorCode::AudioError,
"No audio stream found"); "No audio stream found");
} }
codecContext_ = avcodec_alloc_context3(decoder); codecContext_ = avcodec_alloc_context3(decoder);
if (!codecContext_) { if (!codecContext_) {
avformat_close_input(&formatContext_); avformat_close_input(&formatContext_);
return Api::fail(Api::ErrorCode::AudioError, return Api::fail(Api::ErrorCode::AudioError,
"Failed to allocate codec context"); "Failed to allocate codec context");
} }
codecContext_->thread_count = 0; // Auto-detect CPU cores codecContext_->thread_count = 0; // Auto-detect CPU cores
codecContext_->thread_type = FF_THREAD_FRAME | FF_THREAD_SLICE; codecContext_->thread_type = FF_THREAD_FRAME | FF_THREAD_SLICE;
@@ -262,15 +273,16 @@ namespace Echoion::Core
if (ret < 0) { if (ret < 0) {
avcodec_free_context(&codecContext_); avcodec_free_context(&codecContext_);
avformat_close_input(&formatContext_); avformat_close_input(&formatContext_);
return Api::fail( return Api::fail(Api::ErrorCode::AudioError,
Api::ErrorCode::AudioError, "Failed to copy codec parameters"); "Failed to copy codec parameters");
} }
ret = avcodec_open2(codecContext_, decoder, nullptr); ret = avcodec_open2(codecContext_, decoder, nullptr);
if (ret < 0) { if (ret < 0) {
avcodec_free_context(&codecContext_); avcodec_free_context(&codecContext_);
avformat_close_input(&formatContext_); avformat_close_input(&formatContext_);
return Api::fail(Api::ErrorCode::AudioError, "Failed to open codec"); return Api::fail(Api::ErrorCode::AudioError,
"Failed to open codec");
} }
frame_ = av_frame_alloc(); frame_ = av_frame_alloc();
@@ -278,8 +290,8 @@ namespace Echoion::Core
if (!frame_ || !packet_) { if (!frame_ || !packet_) {
cleanupFfmpeg(); cleanupFfmpeg();
return Api::fail( return Api::fail(Api::ErrorCode::AudioError,
Api::ErrorCode::AudioError, "Failed to allocate frame/packet"); "Failed to allocate frame/packet");
} }
streamInfo_.sampleRate = codecContext_->sample_rate; streamInfo_.sampleRate = codecContext_->sample_rate;
@@ -334,8 +346,8 @@ namespace Echoion::Core
} }
if (ret < 0 || swr_init(swrContext_) < 0) { if (ret < 0 || swr_init(swrContext_) < 0) {
cleanupFfmpeg(); cleanupFfmpeg();
return Api::fail( return Api::fail(Api::ErrorCode::AudioError,
Api::ErrorCode::AudioError, "Failed to initialize resampler"); "Failed to initialize resampler");
} }
// Allocate output buffer for S16 samples (like reference) // Allocate output buffer for S16 samples (like reference)
@@ -348,10 +360,12 @@ namespace Echoion::Core
shouldExit_ = false; shouldExit_ = false;
decodeThread_ = std::jthread(&AudioDecoder::decodeThread, this); decodeThread_ = std::jthread(&AudioDecoder::decodeThread, this);
Void::Log::info("AudioDecoder", "{}", std::format( Void::Log::info(
"Audio source opened: {} -> {} Hz, {} ch, in={} out=S16", url, "AudioDecoder", "{}",
outputSampleRate_, outputChannels_, std::format(
av_get_sample_fmt_name(codecContext_->sample_fmt))); "Audio source opened: {} -> {} Hz, {} ch, in={} out=S16", url,
outputSampleRate_, outputChannels_,
av_get_sample_fmt_name(codecContext_->sample_fmt)));
return {}; return {};
} }
@@ -389,9 +403,10 @@ namespace Echoion::Core
std::lock_guard<std::mutex> statsLock(statsMutex_); std::lock_guard<std::mutex> statsLock(statsMutex_);
stats_.framesDecoded++; stats_.framesDecoded++;
Void::Log::debug("AudioDecoder", "{}", std::format("Frame consumed: {} samples, {} ms", Void::Log::debug("AudioDecoder", "{}",
audio.view.samplesPerChannel() * 2, std::format("Frame consumed: {} samples, {} ms",
audio.timestamp.count())); audio.view.samplesPerChannel() * 2,
audio.timestamp.count()));
return audio; return audio;
} }
@@ -424,9 +439,11 @@ namespace Echoion::Core
cv_.notify_one(); cv_.notify_one();
if (callCount <= 10 || callCount % 1000 == 0) { if (callCount <= 10 || callCount % 1000 == 0) {
Void::Log::debug("AudioDecoder", "{}", std::format( Void::Log::debug(
"getNextChunk {}: returned {} samples, queue left: {}", "AudioDecoder", "{}",
callCount, outChunk.size(), int16Queue_.size())); std::format(
"getNextChunk {}: returned {} samples, queue left: {}",
callCount, outChunk.size(), int16Queue_.size()));
} }
std::lock_guard<std::mutex> statsLock(statsMutex_); std::lock_guard<std::mutex> statsLock(statsMutex_);
@@ -571,7 +588,8 @@ namespace Echoion::Core
framesProduced++; framesProduced++;
if (framesProduced <= 5) { if (framesProduced <= 5) {
Void::Log::debug("AudioDecoder", "{}", Void::Log::debug(
"AudioDecoder", "{}",
std::format("Decode produced frame {}: {} samples", std::format("Decode produced frame {}: {} samples",
framesProduced, chunk.size())); framesProduced, chunk.size()));
} }
@@ -611,8 +629,9 @@ namespace Echoion::Core
eof_ = true; eof_ = true;
cv_.notify_all(); cv_.notify_all();
Void::Log::info("AudioDecoder", "{}", std::format("Decode finished. Packets: {}, Frames: {}", Void::Log::info("AudioDecoder", "{}",
packetsRead, framesProduced)); std::format("Decode finished. Packets: {}, Frames: {}",
packetsRead, framesProduced));
} }
Api::Result<void> AudioDecoder::readPacket() Api::Result<void> AudioDecoder::readPacket()
@@ -628,7 +647,7 @@ namespace Echoion::Core
char errbuf[256]; char errbuf[256];
av_strerror(ret, errbuf, sizeof(errbuf)); av_strerror(ret, errbuf, sizeof(errbuf));
return Api::fail(Api::ErrorCode::AudioError, return Api::fail(Api::ErrorCode::AudioError,
std::format("Error reading frame: {}", errbuf)); std::format("Error reading frame: {}", errbuf));
} }
if (packet_->stream_index != audioStreamIndex_) { if (packet_->stream_index != audioStreamIndex_) {
+1 -1
View File
@@ -512,7 +512,7 @@ namespace Echoion::Core
}); });
// Wait with timeout // Wait with timeout
auto waitStatus = responseFuture.wait_for(std::chrono::seconds(10)); auto waitStatus = responseFuture.wait_for(std::chrono::seconds(20));
if (waitStatus == std::future_status::timeout) { if (waitStatus == std::future_status::timeout) {
Void::Log::warn("YouTubeClient", "{}", "YouTube API search timed out after 10s"); Void::Log::warn("YouTubeClient", "{}", "YouTube API search timed out after 10s");