MythTV master
audiooutputbase.h
Go to the documentation of this file.
1#ifndef AUDIOOUTPUTBASE
2#define AUDIOOUTPUTBASE
3
4// POSIX headers
5#include <sys/time.h> // for struct timeval
6
7// Qt headers
8#include <QString>
9#include <QMutex>
10#include <QWaitCondition>
11
12// MythTV headers
13#include "libmythbase/mthread.h"
14
15#include "audiooutput.h"
16#include "samplerate.h"
17
18namespace soundtouch {
19class SoundTouch;
20};
21class FreeSurround;
23struct AVCodecContext;
24
26{
27public:
28 AsyncLooseLock() = default;
29 void Clear() { m_head = m_tail = 0; }
30 void Ref() { m_head++; }
31 bool TestAndDeref() { bool r = (m_head != m_tail); if (r) m_tail++; return r; }
32private:
33 int m_head {0};
34 int m_tail {0};
35};
36
37// Forward declaration of SPDIF encoder
38class SPDIFEncoder;
39
40class AudioOutputBase : public AudioOutput, public MThread
41{
42 public:
43 static const char *quality_string(int q);
44 explicit AudioOutputBase(const AudioSettings &settings);
45 ~AudioOutputBase() override;
46
47 AudioOutputSettings* GetOutputSettingsCleaned(bool digital = true) override; // AudioOutput
48 AudioOutputSettings* GetOutputSettingsUsers(bool digital = false) override; // AudioOutput
49
50 // reconfigure sound out for new params
51 void Reconfigure(const AudioSettings &settings) override; // AudioOutput
52
53 // dsprate is in 100 * samples/second
54 void SetEffDsp(int dsprate) override; // AudioOutput
55
56 // timestretch
57 void SetStretchFactor(float factor) override; // AudioOutput
58 float GetStretchFactor(void) const override; // AudioOutput
59 int GetChannels(void) const override { return m_channels; } // AudioOutput
60 AudioFormat GetFormat(void) const override { return m_format; }; // AudioOutput
61 int GetBytesPerFrame(void) const override // AudioOutput
62 { return m_sourceBytesPerFrame; }
63
64 bool CanPassthrough(int samplerate, int channels,
65 AVCodecID codec, int profile) const override; // AudioOutput
66 bool CanDownmix(void) const override { return true; } // AudioOutput
67 bool IsUpmixing(void) override; // AudioOutput
68 bool ToggleUpmix(void) override; // AudioOutput
69 bool CanUpmix(void) override; // AudioOutput
70 bool CanProcess(AudioFormat /*fmt*/) override { return true; } // AudioOutput
71 uint32_t CanProcess(void) override // AudioOutput
72 {
73 // we support all codec
74 return ~(((~0ULL) >> FORMAT_FLT) << FORMAT_FLT);
75 }
76
77 void Reset(void) override; // AudioOutput
78
79 void SetSWVolume(int new_volume, bool save) override; // VolumeBase
80 int GetSWVolume(void) override; // VolumeBase
81
82 // timecode is in milliseconds.
83 bool AddFrames(void *buffer, int frames, std::chrono::milliseconds timecode) override; // AudioOutput
84 bool AddData(void *buffer, int len, std::chrono::milliseconds timecode, int frames) override; // AudioOutput
85 bool NeedDecodingBeforePassthrough() const override { return false; }; // AudioOutput
86 std::chrono::milliseconds LengthLastData(void) const override { return m_lengthLastData; } // AudioOutput
87
88 void SetTimecode(std::chrono::milliseconds timecode) override; // AudioOutput
89 bool IsPaused(void) const override { return m_actuallyPaused; } // AudioOutput
90 void Pause(bool paused) override; // AudioOutput
91 void PauseUntilBuffered(void) override; // AudioOutput
92
93 // Wait for all data to finish playing
94 void Drain(void) override; // AudioOutput
95
96 std::chrono::milliseconds GetAudiotime(void) override; // AudioOutput
97 std::chrono::milliseconds GetAudioBufferedTime(void) override; // AudioOutput
98
99 // Send output events showing current progress
100 virtual void Status(void);
101
102 void SetSourceBitrate(int rate) override; // AudioOutput
103
104 void GetBufferStatus(uint &fill, uint &total) override; // AudioOutput
105
106 // Only really used by the AudioOutputNULL object
107 void bufferOutputData(bool y) override // AudioOutput
109 int readOutputData(unsigned char *read_buffer, size_t max_length) override; // AudioOutput
110
111 static const uint kAudioSRCInputSize = 16384;
112
114 // In other words, divisible by 96.
115 static const uint kAudioRingBufferSize = 10239936U;
116
117 protected:
118 // Following function must be called from subclass constructor
119 void InitSettings(const AudioSettings &settings);
120
121 // You need to implement the following functions
122 virtual bool OpenDevice(void) = 0;
123 virtual void CloseDevice(void) = 0;
124 virtual void WriteAudio(unsigned char *aubuf, int size) = 0;
129 virtual int GetBufferedOnSoundcard(void) const = 0;
130 // Default implementation only supports 2ch s16le at 48kHz
131 virtual AudioOutputSettings* GetOutputSettings(bool /*digital*/)
132 { return new AudioOutputSettings; }
133 // You need to call this from any implementation in the dtor.
134 void KillAudio(void);
135
136 // The following functions may be overridden, but don't need to be
137 virtual bool StartOutputThread(void);
138 virtual void StopOutputThread(void);
139
140 int GetAudioData(uchar *buffer, int buf_size, bool full_buffer,
141 volatile uint *local_raud = nullptr);
142
143 void OutputAudioLoop(void);
144
145 void run() override; // MThread
146
147 int CheckFreeSpace(int &frames);
148
149 inline int audiolen() const; // number of valid bytes in audio buffer
150 int audiofree() const; // number of free bytes in audio buffer
151 int audioready() const; // number of bytes ready to be written
152
153 void SetStretchFactorLocked(float factor);
154
155 // For audiooutputca
156 std::chrono::milliseconds GetBaseAudBufTimeCode() const
157 { return m_audbufTimecode; }
158
159 bool usesSpdif() const { return m_usesSpdif; }
160
161 protected:
162 // Basic details about the audio stream
163 int m_channels {-1};
164 AVCodecID m_codec {AV_CODEC_ID_NONE};
169 int m_sampleRate {-1};
170 int m_effDsp {0}; // from the recorded stream (NuppelVideo)
173
176 bool m_discreteDigital {false};
177
178 bool m_passthru {false};
179 bool m_enc {false};
180 bool m_reEnc {false};
181
182 float m_stretchFactor {1.0F};
183 int m_effStretchFactor {100000}; // scaled to 100000 ase ffdsp is
185
186 bool m_killAudio {false};
187
188 bool m_pauseAudio {false};
189 bool m_actuallyPaused {false};
190 bool m_wasPaused {false};
191 bool m_unpauseWhenReady {false};
193 bool m_bufferOutputDataForUse {false}; // used by AudioOutputNULL
194
197 enum : std::int8_t
198 {
203 };
207
208 private:
209 bool SetupPassthrough(AVCodecID codec, int codec_profile,
210 int &samplerate_tmp, int &channels_tmp);
211 AudioOutputSettings* OutputSettings(bool digital = true);
212 int CopyWithUpmix(char *buffer, int frames, uint &org_waud);
213 void SetAudiotime(int frames, std::chrono::milliseconds timecode);
218 bool m_needResampler {false};
219 SRC_STATE *m_srcCtx {nullptr};
220 soundtouch::SoundTouch *m_pSoundStretch {nullptr};
223
226 bool m_upmixDefault {false};
227 bool m_needsUpmix {false};
228 bool m_needsDownmix {false};
230 float m_oldStretchFactor {1.0F};
231 int m_volume {80};
233
234 bool m_processing {false};
235
236 int64_t m_framesBuffered {0};
237
239
245
251
255 std::chrono::milliseconds m_audioTime {0ms};
256
260 volatile uint m_raud {0}; // read position
261 volatile uint m_waud {0}; // write position
265 std::chrono::milliseconds m_audbufTimecode {0ms};
267
269
270 std::chrono::seconds m_currentSeconds {-1s};
271
272 float *m_srcIn;
273
274 // All actual buffers
275 SRC_DATA m_srcData {};
276 [[maybe_unused]] uint m_memoryCorruptionTest0 {0xdeadbeef};
277 alignas(16) std::array<float,kAudioSRCInputSize> m_srcInBuf {};
278 [[maybe_unused]] uint m_memoryCorruptionTest1 {0xdeadbeef};;
279 float *m_srcOut {nullptr};
281 [[maybe_unused]] uint m_memoryCorruptionTest2 {0xdeadbeef};;
285 std::array<uchar,kAudioRingBufferSize> m_audioBuffer {0};
286 [[maybe_unused]] uint m_memoryCorruptionTest3 {0xdeadbeef};;
288 std::chrono::milliseconds m_lengthLastData {0ms};
289
290 // SPDIF Encoder for digital passthrough
291 bool m_usesSpdif {true};
293
294 // Flag indicating if SetStretchFactor enabled audio float processing
295 bool m_forcedProcessing {false};
297};
298
299#endif
@ FORMAT_NONE
@ FORMAT_FLT
AudioOutputSource
Definition: audiosettings.h:21
AsyncLooseLock()=default
void KillAudio(void)
Kill the output thread and cleanup.
virtual void StopOutputThread(void)
void Reconfigure(const AudioSettings &settings) override
(Re)Configure AudioOutputBase
bool IsUpmixing(void) override
Source is currently being upmixed.
QString m_passthruDevice
bool ToggleUpmix(void) override
Toggle between stereo and upmixed 5.1 if the source material is stereo.
AudioOutputSource m_source
void SetStretchFactor(float factor) override
Set the timestretch factor.
bool usesSpdif() const
std::chrono::seconds m_currentSeconds
AudioOutputSettings * GetOutputSettingsUsers(bool digital=false) override
Returns capabilities supported by the audio device amended to take into account the digital audio opt...
soundtouch::SoundTouch * m_pSoundStretch
AudioOutputBase(const AudioSettings &settings)
int GetBytesPerFrame(void) const override
virtual bool StartOutputThread(void)
void OutputAudioLoop(void)
Run in the output thread, write frames to the output device as they become available and there's spac...
void SetEffDsp(int dsprate) override
Set the effective DSP rate.
int GetSWVolume(void) override
Get the volume for software volume control.
int audiofree() const
Get the free space in the audiobuffer in bytes.
std::chrono::milliseconds m_lengthLastData
volatile uint m_waud
uint32_t CanProcess(void) override
AudioFormat m_outputFormat
bool IsPaused(void) const override
std::chrono::milliseconds m_audbufTimecode
timecode of audio most recently placed into buffer
int CheckFreeSpace(int &frames)
Check that there's enough space in the audiobuffer to write the provided number of frames.
bool AddFrames(void *buffer, int frames, std::chrono::milliseconds timecode) override
Add frames to the audiobuffer and perform any required processing.
QMutex m_audioBufLock
Writes to the audiobuffer, reconfigures and audiobuffer resets can only take place while holding this...
void SetTimecode(std::chrono::milliseconds timecode) override
Set the timecode of the samples most recently added to the audiobuffer.
bool CanUpmix(void) override
Upmixing of the current source is available if requested.
float GetStretchFactor(void) const override
Get the timetretch factor.
void InitSettings(const AudioSettings &settings)
AudioOutputSettings * m_outputSettingsRaw
AudioOutputDigitalEncoder * m_encoder
std::array< uchar, kAudioRingBufferSize > m_audioBuffer
main audio buffer
void SetStretchFactorLocked(float factor)
Set the timestretch factor.
virtual void WriteAudio(unsigned char *aubuf, int size)=0
bool CanPassthrough(int samplerate, int channels, AVCodecID codec, int profile) const override
Test if we can output digital audio and if sample rate is supported.
virtual AudioOutputSettings * GetOutputSettings(bool)
std::chrono::milliseconds GetBaseAudBufTimeCode() const
AudioFormat GetFormat(void) const override
void SetAudiotime(int frames, std::chrono::milliseconds timecode)
Set the timecode of the top of the ringbuffer Exclude all other processing elements as they dont vary...
std::chrono::milliseconds GetAudioBufferedTime(void) override
Get the difference in timecode between the samples that are about to become audible and the samples m...
virtual bool OpenDevice(void)=0
AudioOutputSettings * GetOutputSettingsCleaned(bool digital=true) override
Returns capabilities supported by the audio device amended to take into account the digital audio opt...
static const char * quality_string(int q)
SRC_STATE * m_srcCtx
AudioFormat m_format
bool AddData(void *buffer, int len, std::chrono::milliseconds timecode, int frames) override
Add data to the audiobuffer and perform any required processing.
std::chrono::milliseconds m_audioTime
timecode of audio leaving the soundcard (same units as timecodes)
virtual void Status(void)
Report status via an OutputEvent.
bool CanProcess(AudioFormat) override
int audiolen() const
Get the number of bytes in the audiobuffer.
static const uint kAudioSRCInputSize
std::array< float, kAudioSRCInputSize > m_srcInBuf
void Reset(void) override
Reset the audiobuffer, timecode and mythmusic visualisation.
void Pause(bool paused) override
int GetAudioData(uchar *buffer, int buf_size, bool full_buffer, volatile uint *local_raud=nullptr)
Copy frames from the audiobuffer into the buffer provided.
~AudioOutputBase() override
Destructor.
volatile uint m_raud
Audio circular buffer.
int GetChannels(void) const override
void Drain(void) override
Block until all available frames have been written to the device.
std::chrono::milliseconds GetAudiotime(void) override
Calculate the timecode of the samples that are about to become audible.
bool SetupPassthrough(AVCodecID codec, int codec_profile, int &samplerate_tmp, int &channels_tmp)
virtual int GetBufferedOnSoundcard(void) const =0
Return the size in bytes of frames currently in the audio buffer adjusted with the audio playback lat...
int readOutputData(unsigned char *read_buffer, size_t max_length) override
void SetSWVolume(int new_volume, bool save) override
Set the volume for software volume control.
bool CanDownmix(void) const override
virtual void CloseDevice(void)=0
AudioOutputSettings * m_outputSettingsDigital
void run() override
Main routine for the output thread.
SPDIFEncoder * m_spdifEnc
void bufferOutputData(bool y) override
FreeSurround * m_upmixer
bool NeedDecodingBeforePassthrough() const override
void GetBufferStatus(uint &fill, uint &total) override
Fill in the number of bytes in the audiobuffer and the total size of the audiobuffer.
void PauseUntilBuffered(void) override
int64_t m_framesBuffered
int CopyWithUpmix(char *buffer, int frames, uint &org_waud)
Copy frames into the audiobuffer, upmixing en route if necessary.
AudioOutputSettings * m_outputSettings
int audioready() const
Get the scaled number of bytes in the audiobuffer, i.e.
AudioOutputSettings * m_outputSettingsDigitalRaw
std::chrono::milliseconds LengthLastData(void) const override
AudioOutputSettings * OutputSettings(bool digital=true)
static const uint kAudioRingBufferSize
Audio Buffer Size – should be divisible by 32,24,16,12,10,8,6,4,2..
void SetSourceBitrate(int rate) override
Set the bitrate of the source material, reported in periodic OutputEvents.
QMutex m_avsyncLock
must hold avsync_lock to read or write 'audiotime' and 'audiotime_updated'
AsyncLooseLock m_resetActive
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
unsigned int uint
Definition: freesurround.h:24