#pragma once #include #include #include #include #include #include #include #include // 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 initialize(const Api::HwAccelConfig& hwConfig = {}); void shutdown(); // Open audio source (URL or file path) Api::Result openSource(const std::string& url); void closeSource(); // Decode audio - returns zero-copy pooled buffer [[nodiscard]] Api::Result decodeFrame(); // Seek to position Api::Result seek(std::chrono::milliseconds position); // Get audio info [[nodiscard]] Api::Result 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& 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& outChunk, bool blocking = false); // Check if decode is finished [[nodiscard]] bool isFinished() const { return eof_.load(); } private: void decodeThread(); Api::Result readPacket(); Api::Result convertFrame(); void cleanupFfmpeg(); // Hardware acceleration setup Api::Result initializeHardwareAccel(const Api::HwAccelConfig& config); void cleanupHardwareAccel(); // Frame conversion (returns int16 for direct playback) Api::Result 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_ = 192000; uint32_t outputChannels_ = 2; // Threading std::jthread decodeThread_; mutable std::mutex mutex_; std::condition_variable cv_; std::queue decodedQueue_; static constexpr size_t MAX_QUEUE_SIZE = 50; // S16 audio queue (like reference) - avoids float conversion std::deque> int16Queue_; static constexpr size_t MAX_INT16_QUEUE_SIZE = 100; // State std::atomic initialized_{false}; std::atomic decoding_{false}; std::atomic eof_{false}; std::atomic shouldExit_{false}; // For FFmpeg interrupt callback std::chrono::milliseconds currentPosition_{0}; std::string currentUrl_; std::atomic streamSampleRate_{48000}; std::optional proxyConfig_; // Statistics mutable std::mutex statsMutex_; Stats stats_; }; } // namespace Echoion::Core