vdr-plugin-softhddevice-drm-gles 1.7.3-98a2ef7
cSoftHdAudio Class Reference

Audio Interface. More...

#include <audio.h>

Public Member Functions

 cSoftHdAudio (cSoftHdDevice *)
 Create a new audio context.
 
int LazyInit (void)
 Initialize audio output module (alsa)
 
void Exit (void)
 Cleanup audio output module (alsa)
 
int Setup (AVRational, int, int, bool)
 Alsa setup wrapper.
 
void SetPaused (bool)
 Set audio playback pause state.
 
bool IsPaused (void)
 
void Filter (AVFrame *, AVCodecContext *)
 Send audio frame to filter and enqueue it.
 
void EnqueueSpdif (const uint16_t *, int, int64_t pts)
 Enqueue prepared spdif bursts in audio output queue.
 
bool IsBufferFull (void)
 
bool IsBufferEmpty (void)
 
void FlushBuffers (void)
 Flush audio buffers.
 
int GetUsedRingbufferBytes (void)
 Get used bytes in audio ringbuffer.
 
int GetUsedRingbufferMs (void)
 Get used ms in audio ringbuffer.
 
int64_t GetHardwareOutputPtsMs (void)
 Get the hardware output PTS in milliseconds.
 
int64_t GetHardwareOutputDelayMs (void)
 Get the hardware delay in milliseconds.
 
int64_t GetHardwareOutputPtsTimebaseUnits (void)
 Get the hardware output PTS in timebase units.
 
bool HasInputPts (void)
 
int64_t GetInputPtsMs (void)
 
int64_t GetOutputPtsMs (void)
 Get the output PTS of the ringbuffer.
 
int GetAvResyncBorderMs (void)
 
void SetTrickSpeed (double, bool, bool)
 Set the trickspeed.
 
void AdvanceTrickSpeedPts (int64_t)
 Advance the pts in trickspeed mode and delay "playback".
 
int64_t GetTrickSpeedPtsTimebaseUnits (void)
 
void ResetTrickSpeedPts (void)
 
bool IsForwardTrickSpeed (void)
 
bool IsSlowTrickSpeed (void)
 
void SetVolume (int)
 Set mixer volume (0-1000)
 
void SetSoftvol (bool softVolume)
 
void SetNormalize (bool, int)
 Set normalize volume parameters.
 
void SetCompression (bool, int)
 Set volume compression parameters.
 
void SetEqualizer (bool, int[18])
 Set equalizer bands.
 
void SetStereoDescent (int)
 Set stereo loudness descent.
 
void SetPassthroughMask (int mask)
 
void SetAutoAES (bool appendAes)
 
void SetTimebase (AVRational timebase)
 
void SetDownmix (int downMix)
 
int GetPassthroughMask (void) const
 
void DropSamplesOlderThanPtsMs (int64_t)
 Drop samples older than the given PTS.
 
void ClockDriftCompensation (void)
 Calculate clock drift compensation.
 
void ResetHwDelayBaseline (void)
 Reset the hw delay baseline.
 
void SetHwDelayBaseline (void)
 Set the hw delay baseline.
 

Protected Member Functions

virtual void Action (void)
 Audio thread loop, started with Start().
 

Private Member Functions

void Enqueue (const uint16_t *, int, int64_t)
 Send audio data to ringbuffer.
 
void EnqueueFrame (AVFrame *)
 Place samples in audio output queue.
 
bool SendAudio (int)
 Write regular audio data from the ringbuffer to the hardware.
 
bool SendPause (void)
 Write pause to passthrough device.
 
void RebuildPauseBurst (int)
 Rebuild the pause spdif burst with the size of the last recognized normal spdif audio if size changed.
 
void Stop (void)
 Stop the thread.
 
void FlushAlsaBuffers (void)
 Flush alsa buffers.
 
void DropAlsaBuffers (void)
 Drop alsa buffers.
 
bool CyclicCall (void)
 Cyclic audio playback call.
 
void ProcessEvents (void)
 Process queued events and forward them to event receiver.
 
int InitFilter (AVCodecContext *)
 Init audio filters.
 
AVFrame * FilterGetFrame (void)
 Get frame from filter sink.
 
int CheckForFilterReady (AVCodecContext *)
 Check if the filter has changed and is ready, init the filter if needed.
 
std::string BuildChannelMapFilter (const AVChannelLayout &)
 Build the "|"-separated mappings list for the channelmap filter.
 
int64_t GetOutputPtsMsInternal (void)
 

Private Attributes

cSoftHdDevice * m_pDevice
 pointer to device
 
cSoftHdConfig * m_pConfig
 pointer to config
 
cAlsaDevice m_alsa
 alsa device
 
cBufferFillLevelLowPassFilter m_fillLevel
 low pass filter for the buffer fill level
 
cPidController m_pidController {3, 0.005, 0, 1000}
 PID controller for clock drift compensation with tuning values coming from educated guesses.
 
std::chrono::steady_clock::time_point m_lastPidInvocation
 last time the PID controller was invoked
 
int m_packetCounter = 0
 packet counter for logging
 
bool m_initialized = false
 class initialized
 
std::mutex m_mutex
 mutex for thread safety
 
std::mutex m_pauseMutex
 mutex for a safe thread pausing
 
std::mutex m_queueMutex
 mutex for queue safety
 
std::vector< Event > m_eventQueue
 event queue for incoming events
 
std::atomic< double > m_pitchPpm = 0
 pitch adjustment in ppm. Positive values are faster
 
int m_pitchAdjustFrameCounter = 0
 counter for pitch adjustment frames
 
int m_volume = 0
 current volume (0 .. 1000)
 
int m_stereoDescent
 volume descent for stereo
 
AVRational m_timebase
 AVCodecContext pkts_timebase.
 
int64_t m_inputPts = AV_NOPTS_VALUE
 pts clock (last pts in ringbuffer)
 
std::atomic< bool > m_paused = true
 audio is paused
 
std::atomic< int64_t > m_trickSpeedPts = AV_NOPTS_VALUE
 pts of the last received (undecoded) packet during audio-only trickspeed
 
std::atomic< double > m_trickspeedFactor = 0
 current trickspeed factor for AdvanceTrickSpeedPts()
 
std::atomic< bool > m_trickspeed = false
 true, if trickspeed is active
 
std::atomic< bool > m_forwardTrickspeed = true
 current trickspeed direction
 
bool m_softVolume
 flag to use soft volume
 
int m_spdifBurstSize = 0
 size of the current spdif burst
 
std::vector< uint16_t > m_pauseBurst
 holds the burst data itself
 
int m_hwBaseline = 0
 saves the hw delay (pause bursts) once a real audio frame to correctly do the AV-Sync
 
bool m_firstRealAudioReceived = false
 false, as long as no real audio was sent - used to trigger the baseline set
 
cAudioProcessor m_audioProcessor
 
bool m_useNormalizer
 flag to use volume normalize
 
bool m_useCompressor
 flag to use compress volume
 
bool m_useEqualizer
 flag to use equalizer
 
const char * m_pMixerDevice = nullptr
 mixer device name (not used)
 
const char * m_pMixerChannel
 mixer channel name
 
int m_filterChanged = 0
 filter has changed
 
int m_filterReady = 0
 filter is ready
 
AVFilterGraph * m_pFilterGraph = nullptr
 
AVFilterContext * m_pBuffersrcCtx
 
AVFilterContext * m_pBuffersinkCtx
 
cSoftHdRingbuffer m_pRingbuffer {RINGBUFFER_SIZE}
 sample ring buffer
 

Static Private Attributes

static constexpr int AUDIO_MIN_BUFFER_FREE = 3072 * 8 * 8
 Minimum free space in audio buffer 8 packets for 8 channels.
 
static constexpr int AV_SYNC_BORDER_MS = 5000
 absolute max a/v difference in ms which should trigger a resync
 
static constexpr int BYTES_PER_SAMPLE = 2
 number of bytes per sample
 
static constexpr int64_t PTS_WRAP = 1LL << 33
 wraparound mod for a 33-bit PTS
 
static constexpr double MAX_TRICKSPEED_STEP_DELAY_MS = 3000
 upper bound for a single AdvanceTrickSpeedPts(), in case of large/ erratic pts jumps
 
static constexpr unsigned RINGBUFFER_SIZE = 3 * 5 * 7 * 8 * 2 * 1000
 default ring buffer size ~2s 8ch 16bit (3 * 5 * 7 * 8)
 

Detailed Description

Audio Interface.

Definition at line 51 of file audio.h.

Constructor & Destructor Documentation

◆ cSoftHdAudio()

Member Function Documentation

◆ Action()

void cSoftHdAudio::Action ( void  )
protectedvirtual

Audio thread loop, started with Start().

Tries to periodically send frames to the hardware and checks for events (underruns)

Definition at line 1079 of file audio.cpp.

References CyclicCall(), LOGDEBUG, and ProcessEvents().

◆ AdvanceTrickSpeedPts()

void cSoftHdAudio::AdvanceTrickSpeedPts ( int64_t  pts)

Advance the pts in trickspeed mode and delay "playback".

In normal playback, we retrieve the current audio pts by a calculation like: PTS = InputPTS - bufferered data - hardware delay This will work, if we deal with contigous data and ascending pts. In trickspeed (except slow forward), the data which comes from VDR is not contigous. So we can't use the above logic to calculate the current audio pts.

As a consequence we need to do some tricks here: The current pts will immediately be set as soon as the packet arrives (and has a valid pts of course).

We also have to handle the duration, a frame has to be displayed in the device. In case of playback containing video data, the render thread does this for us. In audio-only playback we do the following now: Calculate the difference in ms between the last two audio packets and use the trickspeed factor to calculate the delay. This will cause PlayAudio() to sleep exactly as long as needed. Because we have nothing to sync and don't hear anything anyway, it's enough to keep the pts moving for a correct trickspeed handling process bar display.

Parameters
ptsnext audio input pts from

Definition at line 820 of file audio.cpp.

References AV_NOPTS_VALUE, IsForwardTrickSpeed(), IsSlowTrickSpeed(), L_TRICK, LOGDEBUG2, m_alsa, m_timebase, m_trickspeedFactor, m_trickSpeedPts, MAX_TRICKSPEED_STEP_DELAY_MS, and cAlsaDevice::PtsToMs().

Referenced by cSoftHdDevice::PlayAudio().

◆ CheckForFilterReady()

int cSoftHdAudio::CheckForFilterReady ( AVCodecContext *  ctx)
private

Check if the filter has changed and is ready, init the filter if needed.

Parameters
ctxAVCodec audio decoding context
Return values
1error, init failed
0filter initiated

Definition at line 647 of file audio.cpp.

References InitFilter(), L_SOUND, LOGDEBUG2, m_filterChanged, m_filterReady, and m_pFilterGraph.

Referenced by Filter().

◆ ClockDriftCompensation()

void cSoftHdAudio::ClockDriftCompensation ( void  )

Calculate clock drift compensation.

Uses a PID controller to adjust the playback pitch based on the audio buffer fill level. This keeps the buffer level constant and compensates for clock drift between the sender and the audio hardware.

Also updates the low-pass filter for the buffer fill level.

Definition at line 1274 of file audio.cpp.

References cAlsaDevice::FramesToMsDouble(), cAlsaDevice::GetAvailableBufferFrames(), cBufferFillLevelLowPassFilter::GetBufferFillLevelFramesAvg(), cAlsaDevice::GetBufferSizeFrames(), cPidController::GetDTerm(), cPidController::GetITerm(), cPidController::GetPTerm(), cPidController::GetTargetValue(), cAlsaDevice::IsPassthroughActive(), cBufferFillLevelLowPassFilter::IsSettled(), L_SOUND, LOGDEBUG2, m_alsa, m_fillLevel, m_lastPidInvocation, m_packetCounter, m_pidController, m_pitchPpm, cPidController::SetTargetValue(), cPidController::Update(), and cBufferFillLevelLowPassFilter::UpdateAvgBufferFillLevel().

Referenced by cSoftHdDevice::PlayAudio().

◆ CyclicCall()

bool cSoftHdAudio::CyclicCall ( void  )
private

Cyclic audio playback call.

Handles audio output to ALSA, writing samples from the ring buffer to the hardware when space is available.

If passthrough is enabled, the thread continues sending data (pause bursts) even if audio playback is paused. This prevents, that the AV-Receiver looses the lock and may switch to PCM instead.

Returns
true if data was written or the next write should be scheduled immediately

Definition at line 1117 of file audio.cpp.

References AUDIO, DropSamplesOlderThanPtsMs(), cAlsaDevice::FramesToBytes(), cAlsaDevice::GetAvailableBufferFrames(), GetOutputPtsMs(), cAlsaDevice::HandleError(), cSoftHdDevice::IsAudioOnlyPlayback(), cSoftHdDevice::IsDraining(), IsForwardTrickSpeed(), cAlsaDevice::IsPassthroughActive(), IsSlowTrickSpeed(), L_TRICK, LOGDEBUG2, m_alsa, m_eventQueue, m_mutex, m_paused, m_pauseMutex, m_pDevice, m_queueMutex, m_spdifBurstSize, m_trickspeed, m_trickspeedFactor, SendAudio(), SendPause(), and cAlsaDevice::WaitUntilReady().

Referenced by Action().

◆ DropAlsaBuffers()

void cSoftHdAudio::DropAlsaBuffers ( void  )
private

◆ DropSamplesOlderThanPtsMs()

void cSoftHdAudio::DropSamplesOlderThanPtsMs ( int64_t  ptsMs)

Drop samples older than the given PTS.

Removes audio samples from the ringbuffer that have a presentation timestamp older than the specified ptsMs.

Parameters
ptsMspresentation timestamp in milliseconds - samples older than this will be dropped

Definition at line 420 of file audio.cpp.

References cAlsaDevice::FramesToBytes(), GetOutputPtsMsInternal(), HasInputPts(), L_AV_SYNC, LOGDEBUG2, m_alsa, m_fillLevel, m_mutex, m_pidController, m_pRingbuffer, cAlsaDevice::MsToFrames(), cSoftHdRingbuffer::ReadAdvance(), cBufferFillLevelLowPassFilter::Reset(), cPidController::Reset(), Timestamp2StringMs(), cSoftHdRingbuffer::UsedBytes(), and cBufferFillLevelLowPassFilter::WroteFrames().

Referenced by cSoftHdDevice::CheckAudioPlaybackStartConditions(), CyclicCall(), cVideoRender::DisplayFrame(), cSoftHdDevice::ResumeFromPause(), and cSoftHdDevice::SchedulePlaybackStart().

◆ Enqueue()

◆ EnqueueFrame()

void cSoftHdAudio::EnqueueFrame ( AVFrame *  frame)
private

Place samples in audio output queue.

Parameters
frameaudio frame

Definition at line 452 of file audio.cpp.

References BYTES_PER_SAMPLE, cAudioProcessor::Compress(), Enqueue(), m_audioProcessor, m_useCompressor, m_useNormalizer, and cAudioProcessor::Normalize().

Referenced by Filter().

◆ EnqueueSpdif()

void cSoftHdAudio::EnqueueSpdif ( const uint16_t *  buffer,
int  count,
int64_t  pts 
)

Enqueue prepared spdif bursts in audio output queue.

Wrapper for Enqueue(), but builds a new pause burst if necessary

Parameters
bufferdata buffer
countnumber of bytes in data buffer
ptspts of the buffer

Definition at line 510 of file audio.cpp.

References Enqueue(), m_pauseMutex, and RebuildPauseBurst().

Referenced by cAudioDecoder::Passthrough().

◆ Exit()

void cSoftHdAudio::Exit ( void  )

Cleanup audio output module (alsa)

This currently also stops the audio thread.

Todo:
Move stopping the thread to AlsaExit()

Definition at line 1035 of file audio.cpp.

References cAlsaDevice::Exit(), L_SOUND, LOGDEBUG2, m_alsa, m_initialized, m_pFilterGraph, and Stop().

Referenced by cSoftHdDevice::EnterState().

◆ Filter()

void cSoftHdAudio::Filter ( AVFrame *  inframe,
AVCodecContext *  ctx 
)

Send audio frame to filter and enqueue it.

Parameters
inframeincoming audio frame to be filtered
ctxAVCodec audio decoding context
Return values
1error, send again
0running

Definition at line 680 of file audio.cpp.

References CheckForFilterReady(), EnqueueFrame(), FilterGetFrame(), L_SOUND, LOGDEBUG2, LOGERROR, m_filterChanged, and m_pBuffersrcCtx.

Referenced by cAudioDecoder::DecodePCM().

◆ FilterGetFrame()

AVFrame * cSoftHdAudio::FilterGetFrame ( void  )
private

Get frame from filter sink.

Returns
pointer to AVFrame if success, NULL otherwise

Definition at line 614 of file audio.cpp.

References LOGERROR, and m_pBuffersinkCtx.

Referenced by Filter().

◆ FlushAlsaBuffers()

void cSoftHdAudio::FlushAlsaBuffers ( void  )
private

◆ FlushBuffers()

void cSoftHdAudio::FlushBuffers ( void  )

◆ GetAvResyncBorderMs()

int cSoftHdAudio::GetAvResyncBorderMs ( void  )
inline

Definition at line 74 of file audio.h.

References AV_SYNC_BORDER_MS.

Referenced by cVideoRender::FrameDropNecessary().

◆ GetHardwareOutputDelayMs()

int64_t cSoftHdAudio::GetHardwareOutputDelayMs ( void  )

Get the hardware delay in milliseconds.

Returns
delay in milliseconds, or AV_NOPTS_VALUE if not available

Definition at line 892 of file audio.cpp.

References AV_NOPTS_VALUE, cAlsaDevice::FramesToMs(), cAlsaDevice::GetHwDelayFrames(), cAlsaDevice::IsRunning(), m_alsa, m_inputPts, and m_mutex.

Referenced by cVideoRender::LogDroppedDuped().

◆ GetHardwareOutputPtsMs()

int64_t cSoftHdAudio::GetHardwareOutputPtsMs ( void  )

Get the hardware output PTS in milliseconds.

Calculates the presentation timestamp of audio currently being output by the hardware by accounting for ALSA/kernel buffer delays. This represents the PTS of the audio that is actually being played right now.

Returns
PTS in milliseconds, or AV_NOPTS_VALUE if not available

Definition at line 866 of file audio.cpp.

References AV_NOPTS_VALUE, cAlsaDevice::FramesToMs(), cAlsaDevice::GetHwDelayFrames(), GetOutputPtsMsInternal(), cAlsaDevice::IsRunning(), m_alsa, m_hwBaseline, m_inputPts, m_mutex, m_timebase, PTS_WRAP, and cAlsaDevice::PtsToMs().

Referenced by cSoftHdPlayer::Action(), cVideoRender::DisplayFrame(), GetHardwareOutputPtsTimebaseUnits(), and cSoftHdPlayer::Play().

◆ GetHardwareOutputPtsTimebaseUnits()

int64_t cSoftHdAudio::GetHardwareOutputPtsTimebaseUnits ( void  )

Get the hardware output PTS in timebase units.

Returns
presentation timestamp in timebase units

Definition at line 909 of file audio.cpp.

References AV_NOPTS_VALUE, GetHardwareOutputPtsMs(), m_alsa, m_timebase, and cAlsaDevice::MsToPts().

Referenced by cSoftHdDevice::GetSTC().

◆ GetInputPtsMs()

◆ GetOutputPtsMs()

int64_t cSoftHdAudio::GetOutputPtsMs ( void  )

Get the output PTS of the ringbuffer.

Calculates the presentation timestamp of the next audio sample that will be output from the ringbuffer. This is the input PTS minus the duration of audio currently buffered in the ringbuffer.

Note: This does not account for ALSA/kernel buffer delays. For the actual hardware output PTS, use GetHardwareOutputPtsMs() instead.

Returns
PTS in milliseconds

Definition at line 775 of file audio.cpp.

References GetOutputPtsMsInternal(), and m_mutex.

Referenced by cSoftHdDevice::CheckAudioPlaybackStartConditions(), cSoftHdDevice::CheckPlaybackStartConditions(), CyclicCall(), cSoftHdDevice::GetFirstAudioPtsMsToPlay(), cSoftHdDevice::GetFirstVideoPtsMsToPlay(), cSoftHdDevice::PausePlayback(), cSoftHdDevice::ResumeFromPause(), and cSoftHdDevice::SchedulePlaybackStart().

◆ GetOutputPtsMsInternal()

◆ GetPassthroughMask()

int cSoftHdAudio::GetPassthroughMask ( void  ) const
inline

Definition at line 95 of file audio.h.

References cAlsaDevice::GetPassthroughMask(), and m_alsa.

◆ GetTrickSpeedPtsTimebaseUnits()

int64_t cSoftHdAudio::GetTrickSpeedPtsTimebaseUnits ( void  )
inline

Definition at line 78 of file audio.h.

References m_trickSpeedPts.

Referenced by cSoftHdDevice::GetSTC().

◆ GetUsedRingbufferBytes()

int cSoftHdAudio::GetUsedRingbufferBytes ( void  )

Get used bytes in audio ringbuffer.

Definition at line 746 of file audio.cpp.

References m_mutex, m_pRingbuffer, and cSoftHdRingbuffer::UsedBytes().

Referenced by cVideoRender::LogDroppedDuped().

◆ GetUsedRingbufferMs()

int cSoftHdAudio::GetUsedRingbufferMs ( void  )

Get used ms in audio ringbuffer.

Definition at line 756 of file audio.cpp.

References cAlsaDevice::BytesToFrames(), cAlsaDevice::FramesToMs(), m_alsa, m_mutex, m_pRingbuffer, and cSoftHdRingbuffer::UsedBytes().

Referenced by cVideoRender::LogDroppedDuped().

◆ HasInputPts()

◆ InitFilter()

int cSoftHdAudio::InitFilter ( AVCodecContext *  audioCtx)
private

Init audio filters.

The following alsa filters are set:

  • abuffer
  • channelmap
  • superequalizer
  • aformat
  • abuffersink
Return values
0everything ok
1didn't support channels, downmix set -> scrap this frame, test next
-1something gone wrong

Definition at line 203 of file audio.cpp.

References BuildChannelMapFilter(), cAlsaDevice::GetDownmix(), cAudioProcessor::GetEqualizerOptions(), cAlsaDevice::GetHwNumChannels(), cAlsaDevice::GetHwSampleRate(), L_SOUND, LOGDEBUG2, LOGERROR, LOGWARNING, m_alsa, m_audioProcessor, m_filterChanged, m_filterReady, m_pBuffersinkCtx, m_pBuffersrcCtx, m_pFilterGraph, m_useEqualizer, and Setup().

Referenced by CheckForFilterReady().

◆ IsBufferEmpty()

bool cSoftHdAudio::IsBufferEmpty ( void  )
inline

◆ IsBufferFull()

bool cSoftHdAudio::IsBufferFull ( void  )
inline

◆ IsForwardTrickSpeed()

bool cSoftHdAudio::IsForwardTrickSpeed ( void  )
inline

◆ IsPaused()

bool cSoftHdAudio::IsPaused ( void  )
inline

◆ IsSlowTrickSpeed()

bool cSoftHdAudio::IsSlowTrickSpeed ( void  )
inline

◆ LazyInit()

int cSoftHdAudio::LazyInit ( void  )

Initialize audio output module (alsa)

The init is done lazily as soon as there is a STOP->PLAY state change or the mediaplayer wants to play video or audio.

This function can safely be called anytime, because it does nothing, if the init has already be done.

Return values
0on success, or if audio is already initialized
-1on error

Definition at line 1013 of file audio.cpp.

References cAlsaDevice::Init(), LOGERROR, m_alsa, and m_initialized.

Referenced by cSoftHdDevice::InitAudio(), cSoftHdDevice::PlayAudioPkts(), and cSoftHdDevice::PlayVideoPkts().

◆ ProcessEvents()

void cSoftHdAudio::ProcessEvents ( void  )
private

Process queued events and forward them to event receiver.

Definition at line 1256 of file audio.cpp.

References m_eventQueue, m_pDevice, m_queueMutex, and cSoftHdDevice::TriggerEvent().

Referenced by Action().

◆ RebuildPauseBurst()

void cSoftHdAudio::RebuildPauseBurst ( int  size)
private

Rebuild the pause spdif burst with the size of the last recognized normal spdif audio if size changed.

Parameters
sizespdif burst size in bytes

Definition at line 478 of file audio.cpp.

References L_SOUND, LOGDEBUG2, m_pauseBurst, and m_spdifBurstSize.

Referenced by EnqueueSpdif().

◆ ResetHwDelayBaseline()

void cSoftHdAudio::ResetHwDelayBaseline ( void  )

◆ ResetTrickSpeedPts()

void cSoftHdAudio::ResetTrickSpeedPts ( void  )
inline

Definition at line 79 of file audio.h.

References AV_NOPTS_VALUE, and m_trickSpeedPts.

Referenced by cSoftHdDevice::EnterState().

◆ SendAudio()

bool cSoftHdAudio::SendAudio ( int  freeAlsaBufferFrames)
private

Write regular audio data from the ringbuffer to the hardware.

Parameters
freeAlsaBufferFramesnumber of frames that can be written to the hardware
Return values
trueif data was written or the write should be scheduled again immediately
falseif no data was written

Definition at line 1180 of file audio.cpp.

References cAudioProcessor::Amplify(), cAlsaDevice::BytesToFrames(), cAlsaDevice::CheckWrittenFrames(), cAlsaDevice::FramesToBytes(), cSoftHdRingbuffer::GetReadPointer(), cAlsaDevice::IsPassthroughActive(), m_alsa, m_audioProcessor, m_fillLevel, m_pRingbuffer, m_softVolume, m_volume, cSoftHdRingbuffer::ReadAdvance(), cAlsaDevice::Write(), and cBufferFillLevelLowPassFilter::WroteFrames().

Referenced by CyclicCall().

◆ SendPause()

bool cSoftHdAudio::SendPause ( void  )
private

Write pause to passthrough device.

Returns
true if a complete burst was written, false otherwise

Definition at line 1214 of file audio.cpp.

References cAlsaDevice::BytesToFrames(), cAlsaDevice::CheckWrittenFrames(), m_alsa, m_pauseBurst, m_spdifBurstSize, and cAlsaDevice::Write().

Referenced by CyclicCall().

◆ SetAutoAES()

void cSoftHdAudio::SetAutoAES ( bool  appendAes)
inline

Definition at line 92 of file audio.h.

References m_alsa, and cAlsaDevice::SetAutoAES().

Referenced by cMenuSetupSoft::Store().

◆ SetCompression()

void cSoftHdAudio::SetCompression ( bool  enable,
int  maxfac 
)

Set volume compression parameters.

Parameters
enabletrue, turn on compression
maxfacmax. factor of compression / 1000

Definition at line 971 of file audio.cpp.

References m_audioProcessor, m_useCompressor, and cAudioProcessor::SetCompressor().

Referenced by cSoftHdAudio(), and cMenuSetupSoft::Store().

◆ SetDownmix()

void cSoftHdAudio::SetDownmix ( int  downMix)
inline

Definition at line 94 of file audio.h.

References m_alsa, and cAlsaDevice::SetDownmix().

Referenced by cMenuSetupSoft::Store().

◆ SetEqualizer()

void cSoftHdAudio::SetEqualizer ( bool  enable,
int  band[18] 
)

Set equalizer bands.

Parameters
enableset using equalizer
bandsetting frequenz bands

Definition at line 983 of file audio.cpp.

References m_audioProcessor, m_filterChanged, m_useEqualizer, and cAudioProcessor::SetEqualizer().

Referenced by cSoftHdAudio(), and cMenuSetupSoft::Store().

◆ SetHwDelayBaseline()

void cSoftHdAudio::SetHwDelayBaseline ( void  )

◆ SetNormalize()

void cSoftHdAudio::SetNormalize ( bool  enable,
int  maxfac 
)

Set normalize volume parameters.

Parameters
enabletrue, turn on normalize
maxfacmax. factor of normalize / 1000

Definition at line 959 of file audio.cpp.

References m_audioProcessor, m_useNormalizer, and cAudioProcessor::SetNormalizer().

Referenced by cSoftHdAudio(), and cMenuSetupSoft::Store().

◆ SetPassthroughMask()

void cSoftHdAudio::SetPassthroughMask ( int  mask)
inline

Definition at line 91 of file audio.h.

References m_alsa, and cAlsaDevice::SetPassthroughMask().

Referenced by cSoftHdDevice::SetPassthroughMask().

◆ SetPaused()

void cSoftHdAudio::SetPaused ( bool  pause)

◆ SetSoftvol()

void cSoftHdAudio::SetSoftvol ( bool  softVolume)
inline

Definition at line 84 of file audio.h.

References m_softVolume.

Referenced by cMenuSetupSoft::Store().

◆ SetStereoDescent()

void cSoftHdAudio::SetStereoDescent ( int  delta)

Set stereo loudness descent.

Parameters
deltavalue (/1000) to reduce stereo volume

Definition at line 995 of file audio.cpp.

References m_stereoDescent, m_volume, and SetVolume().

Referenced by cSoftHdAudio(), and cMenuSetupSoft::Store().

◆ SetTimebase()

void cSoftHdAudio::SetTimebase ( AVRational  timebase)
inline

Definition at line 93 of file audio.h.

References m_timebase.

Referenced by cAudioDecoder::Passthrough().

◆ SetTrickSpeed()

void cSoftHdAudio::SetTrickSpeed ( double  speed,
bool  active,
bool  forward 
)

Set the trickspeed.

Parameters
speedtrickspeed factor
activetrue, if trickspeed is currently active
forwardtrue, if this is forward trickspeed

Definition at line 789 of file audio.cpp.

References m_forwardTrickspeed, m_trickspeed, and m_trickspeedFactor.

Referenced by cSoftHdDevice::LeaveState(), and cSoftHdDevice::SetTrickSpeed().

◆ Setup()

int cSoftHdAudio::Setup ( AVRational  timebase,
int  samplerate,
int  channels,
bool  passthrough 
)

Alsa setup wrapper.

only used for passthrough atm, setting up PCM goes via Filter()

Parameters
timebasecodec timebase
sampleratestream samplerate
channelsstream nb of channels
passthroughpassthrough enabled
Return values
0everything ok
-1something gone wrong in AlsaSetup
1no parameter change, no setup needed

Definition at line 583 of file audio.cpp.

References cSoftHdConfig::ConfigAudioDownmix, DropAlsaBuffers(), cAlsaDevice::GetDownmix(), cAlsaDevice::GetHwNumChannels(), cAlsaDevice::GetHwSampleRate(), cAlsaDevice::IsPassthroughActive(), LOGERROR, m_alsa, m_pConfig, m_timebase, cAlsaDevice::Setup(), and Stop().

Referenced by cAudioDecoder::CheckUpdateFormat(), and InitFilter().

◆ SetVolume()

void cSoftHdAudio::SetVolume ( int  volume)

◆ Stop()

void cSoftHdAudio::Stop ( void  )
private

Stop the thread.

Definition at line 1097 of file audio.cpp.

References LOGDEBUG.

Referenced by Exit(), and Setup().

Member Data Documentation

◆ AUDIO_MIN_BUFFER_FREE

constexpr int cSoftHdAudio::AUDIO_MIN_BUFFER_FREE = 3072 * 8 * 8
staticconstexprprivate

Minimum free space in audio buffer 8 packets for 8 channels.

Definition at line 106 of file audio.h.

Referenced by IsBufferFull().

◆ AV_SYNC_BORDER_MS

constexpr int cSoftHdAudio::AV_SYNC_BORDER_MS = 5000
staticconstexprprivate

absolute max a/v difference in ms which should trigger a resync

Definition at line 107 of file audio.h.

Referenced by Enqueue(), and GetAvResyncBorderMs().

◆ BYTES_PER_SAMPLE

constexpr int cSoftHdAudio::BYTES_PER_SAMPLE = 2
staticconstexprprivate

number of bytes per sample

Definition at line 108 of file audio.h.

Referenced by EnqueueFrame().

◆ m_alsa

◆ m_audioProcessor

cAudioProcessor cSoftHdAudio::m_audioProcessor
private

◆ m_eventQueue

std::vector<Event> cSoftHdAudio::m_eventQueue
private

event queue for incoming events

Definition at line 125 of file audio.h.

Referenced by CyclicCall(), Enqueue(), and ProcessEvents().

◆ m_fillLevel

cBufferFillLevelLowPassFilter cSoftHdAudio::m_fillLevel
private

low pass filter for the buffer fill level

Definition at line 115 of file audio.h.

Referenced by ClockDriftCompensation(), DropSamplesOlderThanPtsMs(), Enqueue(), FlushBuffers(), and SendAudio().

◆ m_filterChanged

int cSoftHdAudio::m_filterChanged = 0
private

filter has changed

Definition at line 168 of file audio.h.

Referenced by CheckForFilterReady(), Filter(), FlushBuffers(), InitFilter(), and SetEqualizer().

◆ m_filterReady

int cSoftHdAudio::m_filterReady = 0
private

filter is ready

Definition at line 169 of file audio.h.

Referenced by CheckForFilterReady(), and InitFilter().

◆ m_firstRealAudioReceived

bool cSoftHdAudio::m_firstRealAudioReceived = false
private

false, as long as no real audio was sent - used to trigger the baseline set

Definition at line 144 of file audio.h.

Referenced by ResetHwDelayBaseline(), and SetHwDelayBaseline().

◆ m_forwardTrickspeed

std::atomic<bool> cSoftHdAudio::m_forwardTrickspeed = true
private

current trickspeed direction

Definition at line 138 of file audio.h.

Referenced by IsForwardTrickSpeed(), and SetTrickSpeed().

◆ m_hwBaseline

int cSoftHdAudio::m_hwBaseline = 0
private

saves the hw delay (pause bursts) once a real audio frame to correctly do the AV-Sync

Definition at line 143 of file audio.h.

Referenced by GetHardwareOutputPtsMs(), ResetHwDelayBaseline(), and SetHwDelayBaseline().

◆ m_initialized

bool cSoftHdAudio::m_initialized = false
private

class initialized

Definition at line 121 of file audio.h.

Referenced by Exit(), FlushBuffers(), and LazyInit().

◆ m_inputPts

int64_t cSoftHdAudio::m_inputPts = AV_NOPTS_VALUE
private

pts clock (last pts in ringbuffer)

Definition at line 133 of file audio.h.

Referenced by Enqueue(), FlushBuffers(), GetHardwareOutputDelayMs(), GetHardwareOutputPtsMs(), GetInputPtsMs(), GetOutputPtsMsInternal(), and HasInputPts().

◆ m_lastPidInvocation

std::chrono::steady_clock::time_point cSoftHdAudio::m_lastPidInvocation
private

last time the PID controller was invoked

Definition at line 117 of file audio.h.

Referenced by ClockDriftCompensation().

◆ m_mutex

◆ m_packetCounter

int cSoftHdAudio::m_packetCounter = 0
private

packet counter for logging

Definition at line 118 of file audio.h.

Referenced by ClockDriftCompensation().

◆ m_pauseBurst

std::vector<uint16_t> cSoftHdAudio::m_pauseBurst
private

holds the burst data itself

Definition at line 142 of file audio.h.

Referenced by RebuildPauseBurst(), and SendPause().

◆ m_paused

std::atomic<bool> cSoftHdAudio::m_paused = true
private

audio is paused

Definition at line 134 of file audio.h.

Referenced by CyclicCall(), IsPaused(), and SetPaused().

◆ m_pauseMutex

std::mutex cSoftHdAudio::m_pauseMutex
private

mutex for a safe thread pausing

Definition at line 123 of file audio.h.

Referenced by CyclicCall(), EnqueueSpdif(), and SetPaused().

◆ m_pBuffersinkCtx

AVFilterContext* cSoftHdAudio::m_pBuffersinkCtx
private

Definition at line 172 of file audio.h.

Referenced by FilterGetFrame(), and InitFilter().

◆ m_pBuffersrcCtx

AVFilterContext* cSoftHdAudio::m_pBuffersrcCtx
private

Definition at line 171 of file audio.h.

Referenced by Filter(), and InitFilter().

◆ m_pConfig

cSoftHdConfig* cSoftHdAudio::m_pConfig
private

pointer to config

Definition at line 113 of file audio.h.

Referenced by cSoftHdAudio(), and Setup().

◆ m_pDevice

cSoftHdDevice* cSoftHdAudio::m_pDevice
private

pointer to device

Definition at line 112 of file audio.h.

Referenced by CyclicCall(), and ProcessEvents().

◆ m_pFilterGraph

AVFilterGraph* cSoftHdAudio::m_pFilterGraph = nullptr
private

Definition at line 170 of file audio.h.

Referenced by CheckForFilterReady(), Exit(), and InitFilter().

◆ m_pidController

cPidController cSoftHdAudio::m_pidController {3, 0.005, 0, 1000}
private

PID controller for clock drift compensation with tuning values coming from educated guesses.

Definition at line 116 of file audio.h.

Referenced by ClockDriftCompensation(), DropSamplesOlderThanPtsMs(), and FlushBuffers().

◆ m_pitchAdjustFrameCounter

int cSoftHdAudio::m_pitchAdjustFrameCounter = 0
private

counter for pitch adjustment frames

Definition at line 127 of file audio.h.

Referenced by Enqueue().

◆ m_pitchPpm

std::atomic<double> cSoftHdAudio::m_pitchPpm = 0
private

pitch adjustment in ppm. Positive values are faster

Definition at line 126 of file audio.h.

Referenced by ClockDriftCompensation(), and Enqueue().

◆ m_pMixerChannel

const char* cSoftHdAudio::m_pMixerChannel
private

mixer channel name

Definition at line 165 of file audio.h.

◆ m_pMixerDevice

const char* cSoftHdAudio::m_pMixerDevice = nullptr
private

mixer device name (not used)

Definition at line 164 of file audio.h.

◆ m_pRingbuffer

◆ m_queueMutex

std::mutex cSoftHdAudio::m_queueMutex
private

mutex for queue safety

Definition at line 124 of file audio.h.

Referenced by CyclicCall(), Enqueue(), and ProcessEvents().

◆ m_softVolume

bool cSoftHdAudio::m_softVolume
private

flag to use soft volume

Definition at line 140 of file audio.h.

Referenced by SendAudio(), SetSoftvol(), and SetVolume().

◆ m_spdifBurstSize

int cSoftHdAudio::m_spdifBurstSize = 0
private

size of the current spdif burst

Definition at line 141 of file audio.h.

Referenced by CyclicCall(), RebuildPauseBurst(), and SendPause().

◆ m_stereoDescent

int cSoftHdAudio::m_stereoDescent
private

volume descent for stereo

Definition at line 130 of file audio.h.

Referenced by SetStereoDescent(), and SetVolume().

◆ m_timebase

AVRational cSoftHdAudio::m_timebase
private

◆ m_trickspeed

std::atomic<bool> cSoftHdAudio::m_trickspeed = false
private

true, if trickspeed is active

Definition at line 137 of file audio.h.

Referenced by CyclicCall(), and SetTrickSpeed().

◆ m_trickspeedFactor

std::atomic<double> cSoftHdAudio::m_trickspeedFactor = 0
private

current trickspeed factor for AdvanceTrickSpeedPts()

Definition at line 136 of file audio.h.

Referenced by AdvanceTrickSpeedPts(), CyclicCall(), IsSlowTrickSpeed(), and SetTrickSpeed().

◆ m_trickSpeedPts

std::atomic<int64_t> cSoftHdAudio::m_trickSpeedPts = AV_NOPTS_VALUE
private

pts of the last received (undecoded) packet during audio-only trickspeed

Definition at line 135 of file audio.h.

Referenced by AdvanceTrickSpeedPts(), GetTrickSpeedPtsTimebaseUnits(), and ResetTrickSpeedPts().

◆ m_useCompressor

bool cSoftHdAudio::m_useCompressor
private

flag to use compress volume

Definition at line 160 of file audio.h.

Referenced by EnqueueFrame(), and SetCompression().

◆ m_useEqualizer

bool cSoftHdAudio::m_useEqualizer
private

flag to use equalizer

Definition at line 161 of file audio.h.

Referenced by InitFilter(), and SetEqualizer().

◆ m_useNormalizer

bool cSoftHdAudio::m_useNormalizer
private

flag to use volume normalize

Definition at line 159 of file audio.h.

Referenced by EnqueueFrame(), and SetNormalize().

◆ m_volume

int cSoftHdAudio::m_volume = 0
private

current volume (0 .. 1000)

Definition at line 129 of file audio.h.

Referenced by SendAudio(), SetStereoDescent(), and SetVolume().

◆ MAX_TRICKSPEED_STEP_DELAY_MS

constexpr double cSoftHdAudio::MAX_TRICKSPEED_STEP_DELAY_MS = 3000
staticconstexprprivate

upper bound for a single AdvanceTrickSpeedPts(), in case of large/ erratic pts jumps

Definition at line 110 of file audio.h.

Referenced by AdvanceTrickSpeedPts().

◆ PTS_WRAP

constexpr int64_t cSoftHdAudio::PTS_WRAP = 1LL << 33
staticconstexprprivate

wraparound mod for a 33-bit PTS

Definition at line 109 of file audio.h.

Referenced by GetHardwareOutputPtsMs(), and GetOutputPtsMsInternal().

◆ RINGBUFFER_SIZE

constexpr unsigned cSoftHdAudio::RINGBUFFER_SIZE = 3 * 5 * 7 * 8 * 2 * 1000
staticconstexprprivate

default ring buffer size ~2s 8ch 16bit (3 * 5 * 7 * 8)

Definition at line 179 of file audio.h.


The documentation for this class was generated from the following files: