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 "audiooutput.h"
14 #include "mythlogging.h"
15 #include "mthread.h"
16 
17 #include "samplerate.h"
18 
19 #define VBAUDIO(str) LOG(VB_AUDIO, LOG_INFO, LOC + (str))
20 #define VBAUDIOTS(str) LOG(VB_AUDIO | VB_TIMESTAMP, LOG_INFO, LOC + (str))
21 #define VBGENERAL(str) LOG(VB_GENERAL, LOG_INFO, LOC + (str))
22 #define VBERROR(str) LOG(VB_GENERAL, LOG_ERR, LOC + (str))
23 #define VBWARN(str) LOG(VB_GENERAL, LOG_WARNING, LOC + (str))
24 #define VBERRENO(str) Error(LOC + (str) + ": " + ENO)
25 #define VBERRNOCONST(str) LOG(VB_GENERAL, LOG_ERR, LOC + (str) + ": " + ENO)
26 
27 namespace soundtouch {
28 class SoundTouch;
29 };
30 class FreeSurround;
32 struct AVCodecContext;
33 
35 {
36 public:
37  AsyncLooseLock() = default;
38  void Clear() { m_head = m_tail = 0; }
39  void Ref() { m_head++; }
40  bool TestAndDeref() { bool r = false; if ((r=(m_head != m_tail))) m_tail++; return r; }
41 private:
42  int m_head {0};
43  int m_tail {0};
44 };
45 
46 // Forward declaration of SPDIF encoder
47 class SPDIFEncoder;
48 
49 class AudioOutputBase : public AudioOutput, public MThread
50 {
51  public:
52  static const char *quality_string(int q);
53  explicit AudioOutputBase(const AudioSettings &settings);
54  ~AudioOutputBase() override;
55 
56  AudioOutputSettings* GetOutputSettingsCleaned(bool digital = true) override; // AudioOutput
57  AudioOutputSettings* GetOutputSettingsUsers(bool digital = false) override; // AudioOutput
58 
59  // reconfigure sound out for new params
60  void Reconfigure(const AudioSettings &settings) override; // AudioOutput
61 
62  // dsprate is in 100 * samples/second
63  void SetEffDsp(int dsprate) override; // AudioOutput
64 
65  // timestretch
66  void SetStretchFactor(float factor) override; // AudioOutput
67  float GetStretchFactor(void) const override; // AudioOutput
68  int GetChannels(void) const override { return m_channels; } // AudioOutput
69  AudioFormat GetFormat(void) const override { return m_format; }; // AudioOutput
70  int GetBytesPerFrame(void) const override // AudioOutput
71  { return m_sourceBytesPerFrame; }
72 
73  bool CanPassthrough(int samplerate, int channels,
74  AVCodecID codec, int profile) const override; // AudioOutput
75  bool CanDownmix(void) const override { return true; } // AudioOutput
76  bool IsUpmixing(void) override; // AudioOutput
77  bool ToggleUpmix(void) override; // AudioOutput
78  bool CanUpmix(void) override; // AudioOutput
79  bool CanProcess(AudioFormat /*fmt*/) override { return true; } // AudioOutput
80  uint32_t CanProcess(void) override // AudioOutput
81  {
82  // we support all codec
83  return ~(((~0ULL) >> FORMAT_FLT) << FORMAT_FLT);
84  }
85 
86  void Reset(void) override; // AudioOutput
87 
88  void SetSWVolume(int new_volume, bool save) override; // VolumeBase
89  int GetSWVolume(void) override; // VolumeBase
90 
91  // timecode is in milliseconds.
92  bool AddFrames(void *buffer, int frames, int64_t timecode) override; // AudioOutput
93  bool AddData(void *buffer, int len, int64_t timecode, int frames) override; // AudioOutput
94  bool NeedDecodingBeforePassthrough() const override { return false; }; // AudioOutput
95  int64_t LengthLastData(void) const override { return m_lengthLastData; } // AudioOutput
96 
97  void SetTimecode(int64_t timecode) override; // AudioOutput
98  bool IsPaused(void) const override { return m_actuallyPaused; } // AudioOutput
99  void Pause(bool paused) override; // AudioOutput
100  void PauseUntilBuffered(void) override; // AudioOutput
101 
102  // Wait for all data to finish playing
103  void Drain(void) override; // AudioOutput
104 
105  int64_t GetAudiotime(void) override; // AudioOutput
106  int64_t GetAudioBufferedTime(void) override; // AudioOutput
107 
108  // Send output events showing current progress
109  virtual void Status(void);
110 
111  void SetSourceBitrate(int rate) override; // AudioOutput
112 
113  void GetBufferStatus(uint &fill, uint &total) override; // AudioOutput
114 
115  // Only really used by the AudioOutputNULL object
116  void bufferOutputData(bool y) override // AudioOutput
117  { m_bufferOutputDataForUse = y; }
118  int readOutputData(unsigned char *read_buffer, int max_length) override; // AudioOutput
119 
120  static const uint kAudioSRCInputSize = 16384;
121 
123  // In other words, divisible by 96.
124  static const uint kAudioRingBufferSize = 10239936U;
125 
126  protected:
127  // Following function must be called from subclass constructor
128  void InitSettings(const AudioSettings &settings);
129 
130  // You need to implement the following functions
131  virtual bool OpenDevice(void) = 0;
132  virtual void CloseDevice(void) = 0;
133  virtual void WriteAudio(unsigned char *aubuf, int size) = 0;
138  virtual int GetBufferedOnSoundcard(void) const = 0;
139  // Default implementation only supports 2ch s16le at 48kHz
140  virtual AudioOutputSettings* GetOutputSettings(bool /*digital*/)
141  { return new AudioOutputSettings; }
142  // You need to call this from any implementation in the dtor.
143  void KillAudio(void);
144 
145  // The following functions may be overridden, but don't need to be
146  virtual bool StartOutputThread(void);
147  virtual void StopOutputThread(void);
148 
149  int GetAudioData(uchar *buffer, int buf_size, bool full_buffer,
150  volatile uint *local_raud = nullptr);
151 
152  void OutputAudioLoop(void);
153 
154  void run() override; // MThread
155 
156  int CheckFreeSpace(int &frames);
157 
158  inline int audiolen() const; // number of valid bytes in audio buffer
159  int audiofree() const; // number of free bytes in audio buffer
160  int audioready() const; // number of bytes ready to be written
161 
162  void SetStretchFactorLocked(float factor);
163 
164  // For audiooutputca
165  int GetBaseAudBufTimeCode() const { return m_audbufTimecode; }
166 
167  bool usesSpdif() const { return m_usesSpdif; }
168 
169  protected:
170  // Basic details about the audio stream
171  int m_channels {-1};
172  AVCodecID m_codec {AV_CODEC_ID_NONE};
177  int m_sampleRate {-1};
178  int m_effDsp {0}; // from the recorded stream (NuppelVideo)
179  int m_fragmentSize {0};
181 
182  QString m_mainDevice;
184  bool m_discreteDigital {false};
185 
186  bool m_passthru {false};
187  bool m_enc {false};
188  bool m_reEnc {false};
189 
190  float m_stretchFactor {1.0F};
191  int m_effStretchFactor {100000}; // scaled to 100000 ase ffdsp is
193 
194  bool m_killAudio {false};
195 
196  bool m_pauseAudio {false};
197  bool m_actuallyPaused {false};
198  bool m_wasPaused {false};
199  bool m_unpauseWhenReady {false};
201  bool m_bufferOutputDataForUse {false}; // used by AudioOutputNULL
202 
204  int m_maxChannels {0};
205  enum
206  {
211  };
213  long m_sourceBitRate {-1};
215 
216  private:
217  bool SetupPassthrough(AVCodecID codec, int codec_profile,
218  int &samplerate_tmp, int &channels_tmp);
219  AudioOutputSettings* OutputSettings(bool digital = true);
220  int CopyWithUpmix(char *buffer, int frames, uint &org_waud);
221  void SetAudiotime(int frames, int64_t timecode);
226  bool m_needResampler {false};
227  SRC_STATE *m_srcCtx {nullptr};
228  soundtouch::SoundTouch *m_pSoundStretch {nullptr};
231 
234  bool m_upmixDefault {false};
235  bool m_needsUpmix {false};
236  bool m_needsDownmix {false};
238  float m_oldStretchFactor {1.0F};
239  int m_volume {80};
241 
242  bool m_processing {false};
243 
244  int64_t m_framesBuffered {0};
245 
246  bool m_audioThreadExists {false};
247 
253 
258  QMutex m_avsyncLock;
259 
263  int64_t m_audioTime {0};
264 
268  volatile uint m_raud {0}; // read position
269  volatile uint m_waud {0}; // write position
273  int64_t m_audbufTimecode {0};
275 
276  QMutex m_killAudioLock {QMutex::NonRecursive};
277 
278  long m_currentSeconds {-1};
279 
280  float *m_srcIn;
281 
282  // All actual buffers
283  SRC_DATA m_srcData {};
287  float *m_srcOut {nullptr};
295  bool m_configureSucceeded {false};
296  int64_t m_lengthLastData {0};
297 
298  // SPDIF Encoder for digital passthrough
299  bool m_usesSpdif {true};
301 
302  // Flag indicating if SetStretchFactor enabled audio float processing
303  bool m_forcedProcessing {false};
304  int m_previousBpf {0};
305 };
306 
307 #endif
virtual void WriteAudio(unsigned char *aubuf, int size)=0
int64_t m_audbufTimecode
timecode of audio most recently placed into buffer
virtual void CloseDevice(void)=0
AsyncLooseLock()=default
int64_t m_lengthLastData
static const uint kAudioRingBufferSize
Audio Buffer Size – should be divisible by 32,24,16,12,10,8,6,4,2..
void bufferOutputData(bool y) override
int64_t m_framesBuffered
void InitSettings(const AudioSettings &settings)
virtual bool OpenDevice(void)=0
bool IsPaused(void) const override
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
QMutex m_audioBufLock
Writes to the audiobuffer, reconfigures and audiobuffer resets can only take place while holding this...
void SetStretchFactor(float factor) override
Set the timestretch factor.
AudioOutputSettings * m_outputSettings
volatile uint m_raud
Audio circular buffer.
virtual int GetBufferedOnSoundcard(void) const =0
Return the size in bytes of frames currently in the audio buffer adjusted with the audio playback lat...
virtual void StopOutputThread(void)
bool ToggleUpmix(void) override
Toggle between stereo and upmixed 5.1 if the source material is stereo.
bool AddData(void *buffer, int len, int64_t timecode, int frames) override
Add data to the audiobuffer and perform any required processing.
int GetAudioData(uchar *buffer, int buf_size, bool full_buffer, volatile uint *local_raud=nullptr)
Copy frames from the audiobuffer into the buffer provided.
int64_t GetAudioBufferedTime(void) override
Get the difference in timecode between the samples that are about to become audible and the samples m...
bool NeedDecodingBeforePassthrough() const override
int64_t GetAudiotime(void) override
Calculate the timecode of the samples that are about to become audible.
int audiofree() const
Get the free space in the audiobuffer in bytes.
AudioFormat m_format
int GetBaseAudBufTimeCode() const
AudioOutputBase(const AudioSettings &settings)
volatile uint m_waud
void OutputAudioLoop(void)
Run in the output thread, write frames to the output device as they become available and there's spac...
void Reset(void) override
Reset the audiobuffer, timecode and mythmusic visualisation.
bool IsUpmixing(void) override
Source is currently being upmixed.
void SetSWVolume(int new_volume, bool save) override
Set the volume for software volume control.
bool CanUpmix(void) override
Upmixing of the current source is available if requested.
SPDIFEncoder * m_spdifEnc
int audiolen() const
Get the number of bytes in the audiobuffer.
float m_srcInBuf[kAudioSRCInputSize+16]
void SetSourceBitrate(int rate) override
Set the bitrate of the source material, reported in periodic OutputEvents.
AudioOutputSettings * OutputSettings(bool digital=true)
bool CanProcess(AudioFormat) override
void SetTimecode(int64_t timecode) override
Set the timecode of the samples most recently added to the audiobuffer.
bool usesSpdif() const
uchar m_audioBuffer[kAudioRingBufferSize]
main audio buffer
int64_t LengthLastData(void) const override
int GetSWVolume(void) override
Get the volume for software volume control.
AudioOutputSettings * m_outputSettingsDigital
virtual void Status(void)
Report status via an OutputEvent.
unsigned int uint
Definition: compat.h:140
AudioOutputSettings * GetOutputSettingsUsers(bool digital=false) override
Returns capabilities supported by the audio device amended to take into account the digital audio opt...
AudioFormat GetFormat(void) const override
int GetBytesPerFrame(void) 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.
bool SetupPassthrough(AVCodecID codec, int codec_profile, int &samplerate_tmp, int &channels_tmp)
void SetAudiotime(int frames, int64_t timecode)
Set the timecode of the top of the ringbuffer Exclude all other processing elements as they dont vary...
FreeSurround * m_upmixer
AudioOutputSource
Definition: audiosettings.h:21
static const uint kAudioSRCInputSize
AsyncLooseLock m_resetActive
void PauseUntilBuffered(void) override
virtual bool StartOutputThread(void)
AudioOutputSettings * m_outputSettingsRaw
AudioOutputSource m_source
SRC_STATE * m_srcCtx
AudioOutputSettings * GetOutputSettingsCleaned(bool digital=true) override
Returns capabilities supported by the audio device amended to take into account the digital audio opt...
AudioFormat m_outputFormat
void Pause(bool paused) override
void KillAudio(void)
Kill the output thread and cleanup.
void Reconfigure(const AudioSettings &settings) override
(Re)Configure AudioOutputBase
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, int64_t timecode) override
Add frames to the audiobuffer and perform any required processing.
int CopyWithUpmix(char *buffer, int frames, uint &org_waud)
Copy frames into the audiobuffer, upmixing en route if necessary.
soundtouch::SoundTouch * m_pSoundStretch
QString m_passthruDevice
virtual AudioOutputSettings * GetOutputSettings(bool)
void SetEffDsp(int dsprate) override
Set the effective DSP rate.
~AudioOutputBase() override
Destructor.
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.
const char * frames[3]
Definition: element.c:46
void Drain(void) override
Block until all available frames have been written to the device.
static const char * quality_string(int q)
AudioOutputSettings * m_outputSettingsDigitalRaw
AudioOutputDigitalEncoder * m_encoder
void run() override
Main routine for the output thread.
float GetStretchFactor(void) const override
Get the timetretch factor.
int audioready() const
Get the scaled number of bytes in the audiobuffer, i.e.
uint32_t CanProcess(void) override
int GetChannels(void) const override
void SetStretchFactorLocked(float factor)
Set the timestretch factor.
int64_t m_audioTime
timecode of audio leaving the soundcard (same units as timecodes)
QMutex m_avsyncLock
must hold avsync_lock to read or write 'audiotime' and 'audiotime_updated'
int readOutputData(unsigned char *read_buffer, int max_length) override
bool CanDownmix(void) const override