Files
EchoionCore/include/echoion/core/AudioDecoder.hpp
T
2026-02-01 23:04:49 +03:00

146 lines
4.4 KiB
C++

#pragma once
#include <echoion/api.hpp>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <atomic>
#include <span>
#include <optional>
// Forward declarations for FFmpeg
extern "C" {
struct AVFormatContext;
struct AVCodecContext;
struct AVFrame;
struct AVPacket;
struct SwrContext;
struct AVBufferRef;
}
namespace Echoion::Core {
class AudioDecoder {
public:
AudioDecoder();
~AudioDecoder();
// Initialize decoder with optional hardware acceleration
Api::Result<void> initialize(const Api::HwAccelConfig& hwConfig = {});
void shutdown();
// Open audio source (URL or file path)
Api::Result<void> openSource(const std::string& url);
void closeSource();
// Decode audio - returns zero-copy pooled buffer
[[nodiscard]] Api::Result<Api::DecodedAudio> decodeFrame();
// Seek to position
Api::Result<void> seek(std::chrono::milliseconds position);
// Get audio info
[[nodiscard]] Api::Result<Api::AudioStreamInfo> getStreamInfo() const;
// Get current position
[[nodiscard]] std::chrono::milliseconds getPosition() const;
// Check if decoder has more data
[[nodiscard]] bool hasMoreData() const;
// Set output format
void setOutputFormat(uint32_t sampleRate, uint32_t channels);
// Set proxy configuration for network requests
void setProxyConfig(const std::optional<Api::ProxyConfig>& proxy);
// Hardware acceleration
[[nodiscard]] bool isHardwareAccelerated() const { return hwAccelEnabled_; }
[[nodiscard]] Api::HwAccelType getHwAccelType() const { return hwAccelType_; }
// Enable/disable zero-copy mode
void setZeroCopyEnabled(bool enabled) { zeroCopyEnabled_ = enabled; }
[[nodiscard]] bool isZeroCopyEnabled() const { return zeroCopyEnabled_; }
// Get decoder statistics
struct Stats {
uint64_t framesDecoded = 0;
uint64_t framesDropped = 0;
uint64_t bytesDecoded = 0;
std::chrono::milliseconds totalDecodeTime{0};
double averageDecodeTimeMs = 0.0;
bool hwAccelerationActive = false;
};
[[nodiscard]] Stats getStats() const;
// Get next S16 chunk (like reference implementation)
bool getNextChunk(std::vector<int16_t>& outChunk, bool blocking = false);
// Check if decode is finished
[[nodiscard]] bool isFinished() const { return eof_.load(); }
private:
void decodeThread();
Api::Result<void> readPacket();
Api::Result<Api::DecodedAudio> convertFrame();
void cleanupFfmpeg();
// Hardware acceleration setup
Api::Result<void> initializeHardwareAccel(const Api::HwAccelConfig& config);
void cleanupHardwareAccel();
// Zero-copy frame conversion
Api::Result<Api::DecodedAudio> convertFrameZeroCopy();
Api::Result<Api::DecodedAudio> convertFramePooled();
// FFmpeg contexts
AVFormatContext* formatContext_ = nullptr;
AVCodecContext* codecContext_ = nullptr;
AVFrame* frame_ = nullptr;
AVPacket* packet_ = nullptr;
SwrContext* swrContext_ = nullptr;
uint8_t* outputBuffer_ = nullptr; // For S16 output like reference
// Hardware acceleration
AVBufferRef* hwDeviceContext_ = nullptr;
AVBufferRef* hwFramesContext_ = nullptr;
bool hwAccelEnabled_ = false;
Api::HwAccelType hwAccelType_ = Api::HwAccelType::None;
bool zeroCopyEnabled_ = true;
// Stream info
int audioStreamIndex_ = -1;
Api::AudioStreamInfo streamInfo_;
// Output format
uint32_t outputSampleRate_ = 48000;
uint32_t outputChannels_ = 2;
// Threading
std::jthread decodeThread_;
mutable std::mutex mutex_;
std::condition_variable cv_;
std::queue<Api::DecodedAudio> decodedQueue_;
static constexpr size_t MAX_QUEUE_SIZE = 50;
// S16 audio queue (like reference) - avoids float conversion
std::deque<std::vector<int16_t>> int16Queue_;
static constexpr size_t MAX_INT16_QUEUE_SIZE = 100;
// State
std::atomic<bool> initialized_{false};
std::atomic<bool> decoding_{false};
std::atomic<bool> eof_{false};
std::atomic<bool> shouldExit_{false}; // For FFmpeg interrupt callback
std::chrono::milliseconds currentPosition_{0};
std::string currentUrl_;
std::optional<Api::ProxyConfig> proxyConfig_;
// Statistics
mutable std::mutex statsMutex_;
Stats stats_;
};
} // namespace Echoion::Core