5#pragma comment(lib, "swresample.lib")
12#include <QWaitCondition>
18#include <libavcodec/avcodec.h>
19#include <libavformat/avformat.h>
20#include <libswresample/swresample.h>
44 explicit AudioDecoder(
const QByteArray &audio_data, QObject *parent =
nullptr);
121 void Seek(
double position);
152 void audioInfo(
int sample_rate,
int channels,
double duration);
194 static int ReadPacket(
void *opaque, uint8_t *buf,
int buf_size);
205 static int64_t
SeekPacket(
void *opaque, int64_t offset,
int whence);
void Reset()
Resets the playback position to the beginning (0 seconds). Seeks the FFmpeg format context,...
Definition audio_decoder.cpp:297
QMutex mutex_
Mutex protecting access to shared state variables from different threads.
Definition audio_decoder.h:235
bool stopped_
Flag indicating if the thread should stop execution.
Definition audio_decoder.h:239
QByteArray audio_data_
The raw audio data provided in the constructor.
Definition audio_decoder.h:208
QAudioOutput * audio_output_
Qt Multimedia object for audio output.
Definition audio_decoder.h:228
int64_t seek_position_
Target timestamp for the pending seek operation (in stream timebase).
Definition audio_decoder.h:245
QWaitCondition wait_condition_
Condition variable used to pause/resume the decoding thread.
Definition audio_decoder.h:237
void Seek(double position)
Seeks to a specific position in the audio stream. The position is specified in seconds....
Definition audio_decoder.cpp:274
AVCodecContext * audio_codec_context_
FFmpeg context for the audio codec.
Definition audio_decoder.h:217
bool reached_end_of_stream_
Flag indicating if the end of the audio stream has been reached.
Definition audio_decoder.h:249
bool Initialize()
Initializes the FFmpeg components and QAudioOutput for playback. Opens the input stream using the cus...
Definition audio_decoder.cpp:133
void run() override
The main function executed by the QThread. Contains the loop that reads packets, decodes frames,...
Definition audio_decoder.cpp:319
void error(const QString &message)
Emitted when an error occurs during initialization or decoding.
void playbackFinished()
Emitted when the decoder reaches the end of the audio stream.
float GetVolume() const
Gets the current playback volume. This operation is thread-safe.
Definition audio_decoder.cpp:128
AVFormatContext * format_context_
FFmpeg context for handling the container format.
Definition audio_decoder.h:213
QIODevice * audio_device_
Qt I/O device associated with audio_output_, used for writing PCM data.
Definition audio_decoder.h:230
AudioDecoder(const QByteArray &audio_data, QObject *parent=nullptr)
Constructs an AudioDecoder object. Initializes internal state, allocates memory for FFmpeg's custom I...
Definition audio_decoder.cpp:9
static int64_t SeekPacket(void *opaque, int64_t offset, int whence)
Custom seek function for FFmpeg's AVIOContext. Adjusts the internal read_position_ based on the offse...
Definition audio_decoder.cpp:457
void DecodeAudioFrame(const AVFrame *audio_frame) const
Decodes a single audio frame using the FFmpeg resampler and writes the result to the audio device.
Definition audio_decoder.cpp:410
~AudioDecoder() override
Destructor. Stops the decoding thread, waits for it to finish, and releases all allocated resources.
Definition audio_decoder.cpp:43
bool Reinitialize()
Reinitializes the decoder after it has been stopped or encountered an error. Releases existing resour...
Definition audio_decoder.cpp:84
void audioInfo(int sample_rate, int channels, double duration)
Emitted after successful initialization, providing audio stream details.
double current_position_
Current playback position in seconds.
Definition audio_decoder.h:247
AVIOContext * io_context_
FFmpeg context for custom I/O operations (reading from memory).
Definition audio_decoder.h:223
int audio_stream_
Index of the audio stream within the format context.
Definition audio_decoder.h:215
void positionChanged(double position)
Emitted periodically during playback to indicate the current position.
bool IsDecoderRunning() const
Checks if the decoding thread is currently running.
Definition audio_decoder.h:135
bool seeking_
Flag indicating if a seek operation is pending.
Definition audio_decoder.h:243
AVFrame * audio_frame_
FFmpeg frame structure to hold decoded audio data.
Definition audio_decoder.h:221
bool IsPaused() const
Checks if the playback is currently paused. This operation is thread-safe.
Definition audio_decoder.cpp:269
void Pause()
Toggles the paused state of the playback. If paused, playback stops; if unpaused, playback resumes....
Definition audio_decoder.cpp:260
bool initialized_
Flag indicating if the Initialize() method has completed successfully.
Definition audio_decoder.h:251
void Stop()
Stops the decoding thread and playback gracefully. Sets the stopped_ flag and wakes the thread if it'...
Definition audio_decoder.cpp:253
QAudioFormat audio_format_
Qt audio format description (PCM S16LE stereo 44.1kHz).
Definition audio_decoder.h:232
int read_position_
Current read position within audio_data_ for the custom I/O context.
Definition audio_decoder.h:210
void ReleaseResources()
Releases all FFmpeg and Qt Multimedia resources. Stops audio output, frees contexts (format,...
Definition audio_decoder.cpp:49
bool paused_
Flag indicating if playback is currently paused.
Definition audio_decoder.h:241
SwrContext * swr_context_
FFmpeg context for audio resampling (format/rate/channel conversion).
Definition audio_decoder.h:219
void SetVolume(float volume) const
Sets the playback volume. The volume is a linear factor between 0.0 (silent) and 1....
Definition audio_decoder.cpp:121
unsigned char * io_buffer_
Buffer used by the custom I/O context to hold audio_data_.
Definition audio_decoder.h:225
static int ReadPacket(void *opaque, uint8_t *buf, int buf_size)
Custom read function for FFmpeg's AVIOContext. Reads data from the internal audio_data_ QByteArray bu...
Definition audio_decoder.cpp:443