1 parent
df99b6ea7f
commit
d146f11f02
63 files changed
+8331
-2709
No files matched your search
@@ -5,11 +5,11 @@
|
||||
#include <Types.hpp>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <queue>
|
||||
#include <span>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
// Forward declarations for FFmpeg
|
||||
extern "C" {
|
||||
@@ -18,12 +18,15 @@ struct AVCodecContext;
|
||||
struct AVFrame;
|
||||
struct AVPacket;
|
||||
struct SwrContext;
|
||||
struct AVBufferRef;
|
||||
}
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
// FFmpeg-backed streaming decoder. openSource() spawns a decode jthread
|
||||
// that resamples to interleaved S16 at the configured output format and
|
||||
// fills int16Queue_ (bounded, backpressured); the playback side drains it
|
||||
// chunk-by-chunk via getNextChunk().
|
||||
class AudioDecoder
|
||||
{
|
||||
public:
|
||||
@@ -31,18 +34,13 @@ namespace Echoion::Core
|
||||
AudioDecoder();
|
||||
~AudioDecoder();
|
||||
|
||||
// Initialize decoder with optional hardware acceleration
|
||||
Api::Result<void>
|
||||
initialize(const Api::HwAccelConfig& hwConfig = {});
|
||||
Api::Result<void> initialize();
|
||||
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);
|
||||
|
||||
@@ -50,50 +48,19 @@ namespace Echoion::Core
|
||||
[[nodiscard]] Api::Result<Api::AudioStreamInfo>
|
||||
getStreamInfo() const;
|
||||
|
||||
// Get current position
|
||||
// Get current decode position (runs ahead of playback by up to
|
||||
// the queued chunk depth; use MusicManager for playback 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<Void::Net::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)
|
||||
// Get next S16 chunk. Swaps the caller's buffer for the queued
|
||||
// one, so the caller's previous storage is recycled into the
|
||||
// decode free-list — no per-chunk copy or allocation.
|
||||
bool getNextChunk(std::vector<int16_t>& outChunk,
|
||||
bool blocking = false);
|
||||
|
||||
@@ -103,18 +70,12 @@ namespace Echoion::Core
|
||||
private:
|
||||
|
||||
void decodeThread();
|
||||
Api::Result<void> readPacket();
|
||||
Api::Result<Api::DecodedAudio> convertFrame();
|
||||
// Borrow a chunk buffer of `samples` length from the recycle
|
||||
// free-list (reusing pooled storage), or allocate one if the
|
||||
// free-list is empty.
|
||||
std::vector<int16_t> acquireChunkBuffer(size_t samples);
|
||||
void cleanupFfmpeg();
|
||||
|
||||
// Hardware acceleration setup
|
||||
Api::Result<void>
|
||||
initializeHardwareAccel(const Api::HwAccelConfig& config);
|
||||
void cleanupHardwareAccel();
|
||||
|
||||
// Frame conversion (returns int16 for direct playback)
|
||||
Api::Result<Api::DecodedAudio> convertFramePooled();
|
||||
|
||||
// FFmpeg contexts
|
||||
AVFormatContext* formatContext_ = nullptr;
|
||||
AVCodecContext* codecContext_ = nullptr;
|
||||
@@ -123,13 +84,6 @@ namespace Echoion::Core
|
||||
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_;
|
||||
@@ -142,28 +96,30 @@ namespace Echoion::Core
|
||||
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;
|
||||
|
||||
// Free-list of recycled chunk buffers. getNextChunk() swaps the
|
||||
// caller's previous buffer in here and decodeThread() reuses that
|
||||
// storage instead of allocating a fresh vector per frame.
|
||||
// Guarded by mutex_ (same as int16Queue_).
|
||||
std::vector<std::vector<int16_t>> recycledChunks_;
|
||||
|
||||
// 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};
|
||||
// Decode position in ms; written by the decode thread per frame,
|
||||
// read cross-thread by getPosition() (prefetch decisions).
|
||||
std::atomic<int64_t> currentPositionMs_{0};
|
||||
std::string currentUrl_;
|
||||
|
||||
std::atomic<uint32_t> streamSampleRate_{48000};
|
||||
std::optional<Void::Net::ProxyConfig> proxyConfig_;
|
||||
|
||||
// Statistics
|
||||
mutable std::mutex statsMutex_;
|
||||
Stats stats_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user