Decodes and plays audio streams using FFmpeg and SDL. More...
#include <audio_decoder.h>
Public Member Functions | |
AudioDecoder (FFmpegPlayer *player) | |
Constructs an AudioDecoder instance. | |
~AudioDecoder () | |
Destroys the AudioDecoder instance and cleans up resources. | |
bool | setup (AVFormatContext *formatContext) |
Sets up the audio decoder with a format context. | |
void | play () |
Starts audio playback. | |
void | stop () |
Stops audio playback. | |
void | update () |
Updates the audio buffer by decoding frames. | |
bool | decodeAudioFrame () |
Decodes the next audio frame. | |
void | fillAudioStream (Uint8 *stream, int len) |
Fills the SDL audio stream buffer with decoded audio. | |
void | setVolume (float volume) |
Sets the audio volume. | |
void | setMute (bool mute) |
Toggles the mute state. | |
void | flush () |
Flushes the decoder buffers. | |
Static Public Member Functions | |
static void | SDLAudioCallback (void *userdata, Uint8 *stream, int len) |
SDL audio callback function. | |
Friends | |
class | FFmpegPlayer |
Decodes and plays audio streams using FFmpeg and SDL.
This class manages the decoding of audio streams from an AVFormatContext, resamples audio to a compatible format, and buffers it for playback via SDL audio callbacks. It works with FFmpegPlayer and handles volume and mute settings.
AudioDecoder::AudioDecoder | ( | FFmpegPlayer * | player | ) |
Constructs an AudioDecoder instance.
Initializes the decoder with a reference to the parent FFmpegPlayer.
player | Pointer to the FFmpegPlayer instance. |
AudioDecoder::~AudioDecoder | ( | ) |
Destroys the AudioDecoder instance and cleans up resources.
Ensures all allocated FFmpeg and SDL resources are properly freed.
bool AudioDecoder::decodeAudioFrame | ( | ) |
Decodes the next audio frame.
Attempts to read and decode an audio packet into a frame.
void AudioDecoder::fillAudioStream | ( | Uint8 * | stream, |
int | len | ||
) |
Fills the SDL audio stream buffer with decoded audio.
Applies volume and mute settings to the audio data.
stream | Pointer to the audio buffer. |
len | Length of the audio buffer in bytes. |
void AudioDecoder::flush | ( | ) |
Flushes the decoder buffers.
Clears any pending frames in the codec context.
void AudioDecoder::play | ( | ) |
Starts audio playback.
Unpauses the SDL audio device to begin playback.
|
static |
SDL audio callback function.
Static callback invoked by SDL to fill the audio buffer.
userdata | Pointer to the AudioDecoder instance. |
stream | Pointer to the audio buffer. |
len | Length of the audio buffer in bytes. |
void AudioDecoder::setMute | ( | bool | mute | ) |
Toggles the mute state.
mute | True to mute, false to unmute. |
bool AudioDecoder::setup | ( | AVFormatContext * | formatContext | ) |
Sets up the audio decoder with a format context.
Configures the decoder with the audio stream and initializes SDL audio device.
formatContext | Pointer to the AVFormatContext containing the audio stream. |
void AudioDecoder::setVolume | ( | float | volume | ) |
Sets the audio volume.
Applies a logarithmic scale to the input volume (0-100) and updates the current volume.
volume | Volume level (0-100). |
void AudioDecoder::stop | ( | ) |
Stops audio playback.
Pauses the SDL audio device and flushes the audio FIFO.
void AudioDecoder::update | ( | ) |
Updates the audio buffer by decoding frames.
Ensures the audio FIFO has sufficient samples for continuous playback.