MythTV  master
audiooutput.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstdlib>
3 
4 using namespace std;
5 
6 // Qt utils: to parse audio list
7 #include <QFile>
8 #include <QDateTime>
9 #include <QDir>
10 
11 #include "mythconfig.h"
12 #include "audiooutput.h"
13 #include "mythmiscutil.h"
14 #include "compat.h"
15 
16 #include "audiooutputnull.h"
17 #ifdef _WIN32
18 #include "audiooutputdx.h"
19 #include "audiooutputwin.h"
20 #endif
21 #ifdef USING_OSS
22 #include "audiooutputoss.h"
23 #endif
24 #ifdef USING_ALSA
25 #include "audiooutputalsa.h"
26 #endif
27 #if CONFIG_DARWIN
28 #include "audiooutputca.h"
29 #endif
30 #ifdef USING_JACK
31 #include "audiooutputjack.h"
32 #endif
33 #ifdef USING_PULSEOUTPUT
34 #include "audiooutputpulse.h"
35 #endif
36 #ifdef USING_PULSE
37 #include "audiopulsehandler.h"
38 #endif
39 #ifdef Q_OS_ANDROID
40 #include "audiooutputopensles.h"
41 #include "audiooutputaudiotrack.h"
42 #endif
43 
44 extern "C" {
45 #include "libavcodec/avcodec.h" // to get codec id
46 }
47 #include "audioconvert.h"
48 
49 #define LOC QString("AO: ")
50 
52 {
53 #ifdef USING_PULSE
55 #endif
56 }
57 
59  const QString &main_device, const QString &passthru_device,
60  AudioFormat format, int channels, AVCodecID codec, int samplerate,
61  AudioOutputSource source, bool set_initial_vol, bool passthru,
62  int upmixer_startup, AudioOutputSettings *custom)
63 {
64  AudioSettings settings(
65  main_device, passthru_device, format, channels, codec, samplerate,
66  source, set_initial_vol, passthru, upmixer_startup, custom);
67 
68  return OpenAudio(settings);
69 }
70 
72  const QString &main_device, const QString &passthru_device,
73  bool willsuspendpa)
74 {
75  AudioSettings settings(main_device, passthru_device);
76 
77  return OpenAudio(settings, willsuspendpa);
78 }
79 
81  bool willsuspendpa)
82 {
83  QString &main_device = settings.m_main_device;
84  AudioOutput *ret = nullptr;
85 
86  // Don't suspend Pulse if unnecessary. This can save 100mS
87  if (settings.m_format == FORMAT_NONE || settings.m_channels <= 0)
88  willsuspendpa = false;
89 
90 #ifdef USING_PULSE
91  bool pulsestatus = false;
92 #else
93  {
94  static bool warned = false;
95  if (!warned && IsPulseAudioRunning())
96  {
97  warned = true;
98  LOG(VB_GENERAL, LOG_WARNING,
99  "WARNING: ***Pulse Audio is running***");
100  }
101  }
102 #endif
103 
104  settings.FixPassThrough();
105 
106  if (main_device.startsWith("PulseAudio:"))
107  {
108 #ifdef USING_PULSEOUTPUT
109  return new AudioOutputPulseAudio(settings);
110 #else
111  LOG(VB_GENERAL, LOG_ERR, "Audio output device is set to PulseAudio "
112  "but PulseAudio support is not compiled in!");
113  return nullptr;
114 #endif
115  }
116  if (main_device.startsWith("NULL"))
117  {
118  return new AudioOutputNULL(settings);
119  }
120 
121 #ifdef USING_PULSE
122  if (willsuspendpa)
123  {
124  bool ispulse = false;
125 #ifdef USING_ALSA
126  // Check if using ALSA, that the device doesn't contain the word
127  // "pulse" in its hint
128  if (main_device.startsWith("ALSA:"))
129  {
130  QString device_name = main_device;
131 
132  device_name.remove(0, 5);
133  QMap<QString, QString> *alsadevs =
135  if (!alsadevs->empty() && alsadevs->contains(device_name))
136  {
137  if (alsadevs->value(device_name).contains("pulse",
138  Qt::CaseInsensitive))
139  {
140  ispulse = true;
141  }
142  }
143  delete alsadevs;
144  }
145 #endif
146  if (main_device.contains("pulse", Qt::CaseInsensitive))
147  {
148  ispulse = true;
149  }
150  if (!ispulse)
151  {
153  }
154  }
155 #else // USING_PULSE
156  // Quiet warning error when not compiling with pulseaudio
157  Q_UNUSED(willsuspendpa);
158 #endif
159 
160  if (main_device.startsWith("ALSA:"))
161  {
162 #ifdef USING_ALSA
163  settings.TrimDeviceType();
164  ret = new AudioOutputALSA(settings);
165 #else
166  LOG(VB_GENERAL, LOG_ERR, "Audio output device is set to an ALSA device "
167  "but ALSA support is not compiled in!");
168 #endif
169  }
170  else if (main_device.startsWith("JACK:"))
171  {
172 #ifdef USING_JACK
173  settings.TrimDeviceType();
174  ret = new AudioOutputJACK(settings);
175 #else
176  LOG(VB_GENERAL, LOG_ERR, "Audio output device is set to a JACK device "
177  "but JACK support is not compiled in!");
178 #endif
179  }
180  else if (main_device.startsWith("DirectX:"))
181  {
182 #ifdef _WIN32
183  ret = new AudioOutputDX(settings);
184 #else
185  LOG(VB_GENERAL, LOG_ERR, "Audio output device is set to DirectX device "
186  "but DirectX support is not compiled in!");
187 #endif
188  }
189  else if (main_device.startsWith("Windows:"))
190  {
191 #ifdef _WIN32
192  ret = new AudioOutputWin(settings);
193 #else
194  LOG(VB_GENERAL, LOG_ERR, "Audio output device is set to a Windows "
195  "device but Windows support is not compiled "
196  "in!");
197 #endif
198  }
199  else if (main_device.startsWith("OpenSLES:"))
200  {
201 #ifdef Q_OS_ANDROID
202  ret = new AudioOutputOpenSLES(settings);
203 #else
204  LOG(VB_GENERAL, LOG_ERR, "Audio output device is set to a OpenSLES "
205  "device but Android support is not compiled "
206  "in!");
207 #endif
208  }
209  else if (main_device.startsWith("AudioTrack:"))
210  {
211 #ifdef Q_OS_ANDROID
212  ret = new AudioOutputAudioTrack(settings);
213 #else
214  LOG(VB_GENERAL, LOG_ERR, "Audio output device is set to AudioTrack "
215  "device but Android support is not compiled "
216  "in!");
217 #endif
218  }
219 #if defined(USING_OSS)
220  else
221  ret = new AudioOutputOSS(settings);
222 #elif CONFIG_DARWIN
223  else
224  ret = new AudioOutputCA(settings);
225 #endif
226 
227  if (!ret)
228  {
229  LOG(VB_GENERAL, LOG_CRIT, "No useable audio output driver found.");
230  LOG(VB_GENERAL, LOG_ERR, "Don't disable OSS support unless you're "
231  "not running on Linux.");
232 #ifdef USING_PULSE
233  if (pulsestatus)
235 #endif
236  return nullptr;
237  }
238 #ifdef USING_PULSE
239  ret->m_pulsewassuspended = pulsestatus;
240 #endif
241  return ret;
242 }
243 
245 {
246 #ifdef USING_PULSE
247  if (m_pulsewassuspended)
249 #endif
250  av_frame_free(&m_frame);
251 }
252 
253 void AudioOutput::SetStretchFactor(float /*factor*/)
254 {
255 }
256 
258 {
259  return new AudioOutputSettings;
260 }
261 
263 {
264  return new AudioOutputSettings;
265 }
266 
267 bool AudioOutput::CanPassthrough(int /*samplerate*/,
268  int /*channels*/,
269  AVCodecID /*codec*/,
270  int /*profile*/) const
271 {
272  return false;
273 }
274 
275 // TODO: get rid of this if possible... need to see what uses GetError() and
276 // GetWarning() and why. These would give more useful logs as macros
277 void AudioOutput::Error(const QString &msg)
278 {
279  m_lastError = msg;
280  LOG(VB_GENERAL, LOG_ERR, "AudioOutput Error: " + m_lastError);
281 }
282 
283 void AudioOutput::SilentError(const QString &msg)
284 {
285  m_lastError = msg;
286 }
287 
288 void AudioOutput::Warn(const QString &msg)
289 {
290  m_lastWarn = msg;
291  LOG(VB_GENERAL, LOG_WARNING, "AudioOutput Warning: " + m_lastWarn);
292 }
293 
295 {
296  m_lastError.clear();
297 }
298 
300 {
301  m_lastWarn.clear();
302 }
303 
305  QString &name, QString &desc, bool willsuspendpa)
306 {
307  AudioOutputSettings aosettings(true);
308 
309  AudioOutput *ao = OpenAudio(name, QString(), willsuspendpa);
310  if (ao)
311  {
312  aosettings = *(ao->GetOutputSettingsCleaned());
313  delete ao;
314  }
315  if (aosettings.IsInvalid())
316  {
317  if (!willsuspendpa)
318  return nullptr;
319  QString msg = tr("Invalid or unuseable audio device");
320  return new AudioOutput::AudioDeviceConfig(name, msg);
321  }
322 
323  QString capabilities = desc;
324  int max_channels = aosettings.BestSupportedChannelsELD();
325  if (aosettings.hasELD())
326  {
327  if (aosettings.getELD().isValid())
328  {
329  capabilities += tr(" (%1 connected to %2)")
330  .arg(aosettings.getELD().product_name().simplified())
331  .arg(aosettings.getELD().connection_name());
332  }
333  else
334  {
335  capabilities += tr(" (No connection detected)");
336  }
337  }
338 
339  QString speakers;
340  switch (max_channels)
341  {
342  case 6:
343  speakers = "5.1";
344  break;
345  case 8:
346  speakers = "7.1";
347  break;
348  default:
349  speakers = "2.0";
350  break;
351  }
352 
353  capabilities += tr("\nDevice supports up to %1")
354  .arg(speakers);
355  if (aosettings.canPassthrough() >= 0)
356  {
357  if (aosettings.hasELD() && aosettings.getELD().isValid())
358  {
359  // We have an ELD, show actual reported capabilities
360  capabilities += " (" + aosettings.getELD().codecs_desc() + ")";
361  }
362  else
363  {
364  // build capabilities string, in a similar fashion as reported
365  // by ELD
366  int mask = 0;
367  mask |=
368  (aosettings.canLPCM() << 0) |
369  (aosettings.canAC3() << 1) |
370  (aosettings.canDTS() << 2);
371  // cppcheck-suppress variableScope
372  static const char *s_typeNames[] = { "LPCM", "AC3", "DTS" };
373 
374  if (mask != 0)
375  {
376  capabilities += QObject::tr(" (guessing: ");
377  bool found_one = false;
378  for (unsigned int i = 0; i < 3; i++)
379  {
380  if ((mask & (1 << i)) != 0)
381  {
382  if (found_one)
383  capabilities += ", ";
384  capabilities += s_typeNames[i];
385  found_one = true;
386  }
387  }
388  capabilities += QString(")");
389  }
390  }
391  }
392  LOG(VB_AUDIO, LOG_INFO, QString("Found %1 (%2)")
393  .arg(name).arg(capabilities));
394  auto adc = new AudioOutput::AudioDeviceConfig(name, capabilities);
395  adc->m_settings = aosettings;
396  return adc;
397 }
398 
399 #ifdef USING_OSS
400 static void fillSelectionsFromDir(const QDir &dir,
401  AudioOutput::ADCVect *list)
402 {
403  QFileInfoList il = dir.entryInfoList();
404  for (QFileInfoList::Iterator it = il.begin();
405  it != il.end(); ++it )
406  {
407  QFileInfo &fi = *it;
408  QString name = fi.absoluteFilePath();
409  QString desc = AudioOutput::tr("OSS device");
412  if (!adc)
413  continue;
414  list->append(*adc);
415  delete adc;
416  }
417 }
418 #endif
419 
421 {
422  auto *list = new ADCVect;
423 
424 #ifdef USING_PULSE
426 #endif
427 
428 #ifdef USING_ALSA
429  QMap<QString, QString> *alsadevs = AudioOutputALSA::GetDevices("pcm");
430 
431  if (!alsadevs->empty())
432  {
433  for (QMap<QString, QString>::const_iterator i = alsadevs->begin();
434  i != alsadevs->end(); ++i)
435  {
436  const QString& key = i.key();
437  QString desc = i.value();
438  QString devname = QString("ALSA:%1").arg(key);
439 
440  auto adc = GetAudioDeviceConfig(devname, desc);
441  if (!adc)
442  continue;
443  list->append(*adc);
444  delete adc;
445  }
446  }
447  delete alsadevs;
448 #endif
449 #ifdef USING_OSS
450  {
451  QDir dev("/dev", "dsp*", QDir::Name, QDir::System);
452  fillSelectionsFromDir(dev, list);
453  dev.setNameFilters(QStringList("adsp*"));
454  fillSelectionsFromDir(dev, list);
455 
456  dev.setPath("/dev/sound");
457  if (dev.exists())
458  {
459  dev.setNameFilters(QStringList("dsp*"));
460  fillSelectionsFromDir(dev, list);
461  dev.setNameFilters(QStringList("adsp*"));
462  fillSelectionsFromDir(dev, list);
463  }
464  }
465 #endif
466 #ifdef USING_JACK
467  {
468  QString name = "JACK:";
469  QString desc = tr("Use JACK default sound server.");
470  auto adc = GetAudioDeviceConfig(name, desc);
471  if (adc)
472  {
473  list->append(*adc);
474  delete adc;
475  }
476  }
477 #endif
478 #if CONFIG_DARWIN
479 
480  {
481  QMap<QString, QString> *devs = AudioOutputCA::GetDevices(nullptr);
482  if (!devs->empty())
483  {
484  for (QMap<QString, QString>::const_iterator i = devs->begin();
485  i != devs->end(); ++i)
486  {
487  QString key = i.key();
488  QString desc = i.value();
489  QString devname = QString("CoreAudio:%1").arg(key);
490 
491  auto adc = GetAudioDeviceConfig(devname, desc);
492  if (!adc)
493  continue;
494  list->append(*adc);
495  delete adc;
496  }
497  }
498  delete devs;
499  QString name = "CoreAudio:Default Output Device";
500  QString desc = tr("CoreAudio default output");
501  auto adc = GetAudioDeviceConfig(name, desc);
502  if (adc)
503  {
504  list->append(*adc);
505  delete adc;
506  }
507  }
508 #endif
509 #ifdef _WIN32
510  {
511  QString name = "Windows:";
512  QString desc = "Windows default output";
513  auto adc = GetAudioDeviceConfig(name, desc);
514  if (adc)
515  {
516  list->append(*adc);
517  delete adc;
518  }
519 
520  QMap<int, QString> *dxdevs = AudioOutputDX::GetDXDevices();
521 
522  if (!dxdevs->empty())
523  {
524  for (QMap<int, QString>::const_iterator i = dxdevs->begin();
525  i != dxdevs->end(); ++i)
526  {
527  QString devdesc = i.value();
528  QString devname = QString("DirectX:%1").arg(devdesc);
529 
530  adc = GetAudioDeviceConfig(devname, devdesc);
531  if (!adc)
532  continue;
533  list->append(*adc);
534  delete adc;
535  }
536  }
537  delete dxdevs;
538  }
539 #endif
540 
541 #ifdef USING_PULSE
542  if (pasuspended)
544 #endif
545 
546 #ifdef USING_PULSEOUTPUT
547  {
548  QString name = "PulseAudio:default";
549  QString desc = tr("PulseAudio default sound server.");
550  auto adc = GetAudioDeviceConfig(name, desc);
551  if (adc)
552  {
553  list->append(*adc);
554  delete adc;
555  }
556  }
557 #endif
558 
559 #ifdef ANDROID
560  {
561  QString name = "OpenSLES:";
562  QString desc = tr("OpenSLES default output. Stereo support only.");
563  auto adc = GetAudioDeviceConfig(name, desc);
564  if (adc)
565  {
566  list->append(*adc);
567  delete adc;
568  }
569  }
570  {
571  QString name = "AudioTrack:";
572  QString desc = tr("Android AudioTrack output. Supports surround sound.");
573  auto adc = GetAudioDeviceConfig(name, desc);
574  if (adc)
575  {
576  list->append(*adc);
577  delete adc;
578  }
579  }
580 #endif
581 
582  QString name = "NULL";
583  QString desc = "NULL device";
584  auto adc = GetAudioDeviceConfig(name, desc);
585  if (adc)
586  {
587  list->append(*adc);
588  delete adc;
589  }
590  return list;
591 }
592 
600 int AudioOutput::DecodeAudio(AVCodecContext *ctx,
601  uint8_t *buffer, int &data_size,
602  const AVPacket *pkt)
603 {
604  bool got_frame = false;
605  char error[AV_ERROR_MAX_STRING_SIZE];
606 
607  data_size = 0;
608  if (!m_frame)
609  {
610  if (!(m_frame = av_frame_alloc()))
611  {
612  return AVERROR(ENOMEM);
613  }
614  }
615  else
616  {
617  av_frame_unref(m_frame);
618  }
619 
620 // SUGGESTION
621 // Now that avcodec_decode_audio4 is deprecated and replaced
622 // by 2 calls (receive frame and send packet), this could be optimized
623 // into separate routines or separate threads.
624 // Also now that it always consumes a whole buffer some code
625 // in the caller may be able to be optimized.
626  int ret = avcodec_receive_frame(ctx,m_frame);
627  if (ret == 0)
628  got_frame = true;
629  if (ret == AVERROR(EAGAIN))
630  ret = 0;
631  if (ret == 0)
632  ret = avcodec_send_packet(ctx, pkt);
633  if (ret == AVERROR(EAGAIN))
634  ret = 0;
635  else if (ret < 0)
636  {
637  LOG(VB_AUDIO, LOG_ERR, LOC +
638  QString("audio decode error: %1 (%2)")
639  .arg(av_make_error_string(error, sizeof(error), ret))
640  .arg(got_frame));
641  return ret;
642  }
643  else
644  ret = pkt->size;
645 
646  if (!got_frame)
647  {
648  LOG(VB_AUDIO, LOG_DEBUG, LOC +
649  QString("audio decode, no frame decoded (%1)").arg(ret));
650  return ret;
651  }
652 
653  auto format = (AVSampleFormat)m_frame->format;
654  AudioFormat fmt =
655  AudioOutputSettings::AVSampleFormatToFormat(format, ctx->bits_per_raw_sample);
656 
657  data_size = m_frame->nb_samples * m_frame->channels * av_get_bytes_per_sample(format);
658 
659  // May need to convert audio to S16
660  AudioConvert converter(fmt, CanProcess(fmt) ? fmt : FORMAT_S16);
661  uint8_t* src = nullptr;
662 
663  if (av_sample_fmt_is_planar(format))
664  {
665  src = buffer;
666  converter.InterleaveSamples(m_frame->channels,
667  src,
668  (const uint8_t **)m_frame->extended_data,
669  data_size);
670  }
671  else
672  {
673  // data is already compacted...
674  src = m_frame->extended_data[0];
675  }
676 
677  uint8_t* transit = buffer;
678 
679  if (!CanProcess(fmt) &&
680  av_get_bytes_per_sample(ctx->sample_fmt) < AudioOutputSettings::SampleSize(converter.Out()))
681  {
682  // this conversion can't be done in place
683  transit = (uint8_t*)av_malloc(data_size * av_get_bytes_per_sample(ctx->sample_fmt)
684  / AudioOutputSettings::SampleSize(converter.Out()));
685  if (!transit)
686  {
687  LOG(VB_AUDIO, LOG_ERR, LOC +
688  QString("audio decode, out of memory"));
689  data_size = 0;
690  return ret;
691  }
692  }
693  if (!CanProcess(fmt) || src != transit)
694  {
695  data_size = converter.Process(transit, src, data_size, true);
696  }
697  if (transit != buffer)
698  {
699  av_free(transit);
700  }
701  return ret;
702 }
QString m_main_device
Definition: audiosettings.h:65
void Warn(const QString &msg)
QVector< AudioDeviceConfig > ADCVect
Definition: audiooutput.h:45
virtual AudioOutputSettings * GetOutputSettingsUsers(bool digital=true)
void Error(const QString &msg)
bool IsInvalid()
return true if class instance is marked invalid.
QString codecs_desc()
Definition: eldutils.cpp:494
static void error(const char *str,...)
Definition: vbi.c:42
static AudioOutput * OpenAudio(const QString &main_device, const QString &passthru_device, AudioFormat format, int channels, AVCodecID codec, int samplerate, AudioOutputSource source, bool set_initial_vol, bool passthru, int upmixer_startup=0, AudioOutputSettings *custom=nullptr)
Definition: audiooutput.cpp:58
static QMap< QString, QString > * GetDevices(const char *type)
AudioFormat m_format
Definition: audiosettings.h:67
QString connection_name()
Definition: eldutils.cpp:462
virtual bool CanPassthrough(int samplerate, int channels, AVCodecID codec, int profile) const
static QMap< QString, QString > * GetDevices(const char *type=nullptr)
int BestSupportedChannelsELD()
Reports best supported channel number, restricted to ELD range.
int Process(void *out, const void *in, int bytes, bool noclip=false)
Process Parameters: out : destination buffer where converted samples will be copied in : source buffe...
bool m_pulsewassuspended
Definition: audiooutput.h:203
virtual AudioOutputSettings * GetOutputSettingsCleaned(bool digital=true)
#define LOC
Definition: audiooutput.cpp:49
void TrimDeviceType(void)
QString product_name()
Definition: eldutils.cpp:457
static QMap< int, QString > * GetDXDevices(void)
static int SampleSize(AudioFormat format)
AudioFormat Out(void)
Definition: audioconvert.h:49
ELD & getELD(void)
retrieve ELD data
void InterleaveSamples(int channels, uint8_t *output, const uint8_t *const *input, int data_size)
bool IsPulseAudioRunning(void)
Is A/V Sync destruction daemon is running on this host?
virtual void SetStretchFactor(float factor)
virtual int DecodeAudio(AVCodecContext *ctx, uint8_t *buffer, int &data_size, const AVPacket *pkt)
Utility routine.
void * av_malloc(unsigned int size)
static AudioDeviceConfig * GetAudioDeviceConfig(QString &name, QString &desc, bool willsuspendpa=false)
static bool Suspend(enum PulseAction action)
AudioOutputSource
Definition: audiosettings.h:17
void SilentError(const QString &msg)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
~AudioOutput() override
bool canLPCM()
return true if device supports multichannels PCM (deprecated, see canFeature())
static void Cleanup(void)
Definition: audiooutput.cpp:51
void ClearWarning(void)
bool isValid()
Definition: eldutils.cpp:427
Implements Core Audio (Mac OS X Hardware Abstraction Layer) output.
Definition: audiooutputca.h:13
void av_free(void *ptr)
void ClearError(void)
bool hasELD()
get the ELD flag
static AudioFormat AVSampleFormatToFormat(AVSampleFormat format, int bits=0)
Return AVSampleFormat closest equivalent to AudioFormat.
bool canDTS()
return true if device can or may support DTS (deprecated, see canFeature())
static ADCVect * GetOutputList(void)
bool canAC3()
return true if device can or may support AC3 (deprecated, see canFeature())
static void fillSelectionsFromDir(const QDir &dir, AudioOutput::ADCVect *list)
void FixPassThrough(void)