1 parent
583e4e752d
commit
1f26927ad6
2 files changed
+59
-40
No files matched your search
+58
-39
@@ -1,5 +1,5 @@
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include "echoion/core/AudioDecoder.hpp"
|
||||
#include <Void/Log/Log.hpp>
|
||||
extern "C" {
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavformat/avformat.h>
|
||||
@@ -41,7 +41,8 @@ namespace Echoion::Core
|
||||
if (hwConfig.isEnabled()) {
|
||||
auto result = initializeHardwareAccel(hwConfig);
|
||||
if (!result) {
|
||||
Void::Log::warn("AudioDecoder", "{}",
|
||||
Void::Log::warn(
|
||||
"AudioDecoder", "{}",
|
||||
std::format("Hardware acceleration init failed: {}, "
|
||||
"falling back to software",
|
||||
result.error().message));
|
||||
@@ -76,7 +77,7 @@ namespace Echoion::Core
|
||||
break;
|
||||
default:
|
||||
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)) ==
|
||||
@@ -87,8 +88,9 @@ namespace Echoion::Core
|
||||
}
|
||||
|
||||
hwAccelType_ = config.type;
|
||||
Void::Log::info("AudioDecoder", "{}", std::format("Hardware acceleration configured: {}",
|
||||
av_hwdevice_get_type_name(hwType)));
|
||||
Void::Log::info("AudioDecoder", "{}",
|
||||
std::format("Hardware acceleration configured: {}",
|
||||
av_hwdevice_get_type_name(hwType)));
|
||||
|
||||
return {};
|
||||
}
|
||||
@@ -121,8 +123,8 @@ namespace Echoion::Core
|
||||
Api::Result<void> AudioDecoder::openSource(const std::string& url)
|
||||
{
|
||||
if (!initialized_) {
|
||||
return Api::fail(
|
||||
Api::ErrorCode::ServiceUnavailable, "Decoder not initialized");
|
||||
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||
"Decoder not initialized");
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
@@ -160,7 +162,7 @@ namespace Echoion::Core
|
||||
// protocol handler, causing proxy bypass.
|
||||
if (proxyConfig_->has_auth()) {
|
||||
proxyUrl = std::format(
|
||||
"socks5://{}:{}@{}:{}", *proxyConfig_->username,
|
||||
"socks5h://{}:{}@{}:{}", *proxyConfig_->username,
|
||||
*proxyConfig_->password, proxyConfig_->host,
|
||||
proxyConfig_->port);
|
||||
} else {
|
||||
@@ -180,19 +182,25 @@ namespace Echoion::Core
|
||||
}
|
||||
|
||||
av_dict_set(&options, "proxy", proxyUrl.c_str(), 0);
|
||||
Void::Log::info("AudioDecoder", "{}", std::format("Using proxy for ffmpeg: {}:{}",
|
||||
proxyConfig_->host,
|
||||
proxyConfig_->port));
|
||||
|
||||
av_dict_set(
|
||||
&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)));
|
||||
|
||||
// Allocate format context first (needed for interrupt callback)
|
||||
formatContext_ = avformat_alloc_context();
|
||||
if (!formatContext_) {
|
||||
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
|
||||
@@ -214,8 +222,10 @@ namespace Echoion::Core
|
||||
if (ret < 0) {
|
||||
char errbuf[256];
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
Void::Log::error("AudioDecoder", "{}", std::format(
|
||||
"FFmpeg failed to open input (error {}): {}", ret, errbuf));
|
||||
Void::Log::error(
|
||||
"AudioDecoder", "{}",
|
||||
std::format("FFmpeg failed to open input (error {}): {}", ret,
|
||||
errbuf));
|
||||
return Api::fail(
|
||||
Api::ErrorCode::AudioError,
|
||||
std::format(
|
||||
@@ -228,7 +238,8 @@ namespace Echoion::Core
|
||||
if (ret < 0) {
|
||||
char errbuf[256];
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
Void::Log::error("AudioDecoder", "{}",
|
||||
Void::Log::error(
|
||||
"AudioDecoder", "{}",
|
||||
std::format("FFmpeg failed to find stream info (error {}): {}",
|
||||
ret, errbuf));
|
||||
avformat_close_input(&formatContext_);
|
||||
@@ -245,14 +256,14 @@ namespace Echoion::Core
|
||||
if (audioStreamIndex_ == -1) {
|
||||
avformat_close_input(&formatContext_);
|
||||
return Api::fail(Api::ErrorCode::AudioError,
|
||||
"No audio stream found");
|
||||
"No audio stream found");
|
||||
}
|
||||
|
||||
codecContext_ = avcodec_alloc_context3(decoder);
|
||||
if (!codecContext_) {
|
||||
avformat_close_input(&formatContext_);
|
||||
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_type = FF_THREAD_FRAME | FF_THREAD_SLICE;
|
||||
@@ -262,15 +273,16 @@ namespace Echoion::Core
|
||||
if (ret < 0) {
|
||||
avcodec_free_context(&codecContext_);
|
||||
avformat_close_input(&formatContext_);
|
||||
return Api::fail(
|
||||
Api::ErrorCode::AudioError, "Failed to copy codec parameters");
|
||||
return Api::fail(Api::ErrorCode::AudioError,
|
||||
"Failed to copy codec parameters");
|
||||
}
|
||||
|
||||
ret = avcodec_open2(codecContext_, decoder, nullptr);
|
||||
if (ret < 0) {
|
||||
avcodec_free_context(&codecContext_);
|
||||
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();
|
||||
@@ -278,8 +290,8 @@ namespace Echoion::Core
|
||||
|
||||
if (!frame_ || !packet_) {
|
||||
cleanupFfmpeg();
|
||||
return Api::fail(
|
||||
Api::ErrorCode::AudioError, "Failed to allocate frame/packet");
|
||||
return Api::fail(Api::ErrorCode::AudioError,
|
||||
"Failed to allocate frame/packet");
|
||||
}
|
||||
|
||||
streamInfo_.sampleRate = codecContext_->sample_rate;
|
||||
@@ -334,8 +346,8 @@ namespace Echoion::Core
|
||||
}
|
||||
if (ret < 0 || swr_init(swrContext_) < 0) {
|
||||
cleanupFfmpeg();
|
||||
return Api::fail(
|
||||
Api::ErrorCode::AudioError, "Failed to initialize resampler");
|
||||
return Api::fail(Api::ErrorCode::AudioError,
|
||||
"Failed to initialize resampler");
|
||||
}
|
||||
|
||||
// Allocate output buffer for S16 samples (like reference)
|
||||
@@ -348,10 +360,12 @@ namespace Echoion::Core
|
||||
shouldExit_ = false;
|
||||
decodeThread_ = std::jthread(&AudioDecoder::decodeThread, this);
|
||||
|
||||
Void::Log::info("AudioDecoder", "{}", std::format(
|
||||
"Audio source opened: {} -> {} Hz, {} ch, in={} out=S16", url,
|
||||
outputSampleRate_, outputChannels_,
|
||||
av_get_sample_fmt_name(codecContext_->sample_fmt)));
|
||||
Void::Log::info(
|
||||
"AudioDecoder", "{}",
|
||||
std::format(
|
||||
"Audio source opened: {} -> {} Hz, {} ch, in={} out=S16", url,
|
||||
outputSampleRate_, outputChannels_,
|
||||
av_get_sample_fmt_name(codecContext_->sample_fmt)));
|
||||
|
||||
return {};
|
||||
}
|
||||
@@ -389,9 +403,10 @@ namespace Echoion::Core
|
||||
std::lock_guard<std::mutex> statsLock(statsMutex_);
|
||||
stats_.framesDecoded++;
|
||||
|
||||
Void::Log::debug("AudioDecoder", "{}", std::format("Frame consumed: {} samples, {} ms",
|
||||
audio.view.samplesPerChannel() * 2,
|
||||
audio.timestamp.count()));
|
||||
Void::Log::debug("AudioDecoder", "{}",
|
||||
std::format("Frame consumed: {} samples, {} ms",
|
||||
audio.view.samplesPerChannel() * 2,
|
||||
audio.timestamp.count()));
|
||||
|
||||
return audio;
|
||||
}
|
||||
@@ -424,9 +439,11 @@ namespace Echoion::Core
|
||||
cv_.notify_one();
|
||||
|
||||
if (callCount <= 10 || callCount % 1000 == 0) {
|
||||
Void::Log::debug("AudioDecoder", "{}", std::format(
|
||||
"getNextChunk {}: returned {} samples, queue left: {}",
|
||||
callCount, outChunk.size(), int16Queue_.size()));
|
||||
Void::Log::debug(
|
||||
"AudioDecoder", "{}",
|
||||
std::format(
|
||||
"getNextChunk {}: returned {} samples, queue left: {}",
|
||||
callCount, outChunk.size(), int16Queue_.size()));
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> statsLock(statsMutex_);
|
||||
@@ -571,7 +588,8 @@ namespace Echoion::Core
|
||||
framesProduced++;
|
||||
|
||||
if (framesProduced <= 5) {
|
||||
Void::Log::debug("AudioDecoder", "{}",
|
||||
Void::Log::debug(
|
||||
"AudioDecoder", "{}",
|
||||
std::format("Decode produced frame {}: {} samples",
|
||||
framesProduced, chunk.size()));
|
||||
}
|
||||
@@ -611,8 +629,9 @@ namespace Echoion::Core
|
||||
eof_ = true;
|
||||
cv_.notify_all();
|
||||
|
||||
Void::Log::info("AudioDecoder", "{}", std::format("Decode finished. Packets: {}, Frames: {}",
|
||||
packetsRead, framesProduced));
|
||||
Void::Log::info("AudioDecoder", "{}",
|
||||
std::format("Decode finished. Packets: {}, Frames: {}",
|
||||
packetsRead, framesProduced));
|
||||
}
|
||||
|
||||
Api::Result<void> AudioDecoder::readPacket()
|
||||
@@ -628,7 +647,7 @@ namespace Echoion::Core
|
||||
char errbuf[256];
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
return Api::fail(Api::ErrorCode::AudioError,
|
||||
std::format("Error reading frame: {}", errbuf));
|
||||
std::format("Error reading frame: {}", errbuf));
|
||||
}
|
||||
|
||||
if (packet_->stream_index != audioStreamIndex_) {
|
||||
|
||||
Reference in new issue
Block a user