MythTV  master
setupwizard_audio.cpp
Go to the documentation of this file.
1 // qt
2 #include <QString>
3 #include <QVariant>
4 
5 // myth
6 #include <mythcontext.h>
7 #include <mythdbcon.h>
8 #include <mythdirs.h>
9 #include <mythprogressdialog.h>
10 
11 #include <audiooutpututil.h>
12 
13 #include "audiogeneralsettings.h"
14 #include "setupwizard_general.h"
15 #include "setupwizard_audio.h"
16 #include "setupwizard_video.h"
17 
19 {
20  // Load the theme for this screen
21  bool foundtheme = LoadWindowFromXML("config-ui.xml", "audiowizard", this);
22  if (!foundtheme)
23  return false;
24 
26  dynamic_cast<MythUIButtonList *> (GetChild("audiodevices"));
28  dynamic_cast<MythUIButtonList *> (GetChild("speakers"));
29 
30  m_dtsCheck = dynamic_cast<MythUICheckBox *> (GetChild("dtscheck"));
31  m_ac3Check = dynamic_cast<MythUICheckBox *> (GetChild("ac3check"));
32  m_eac3Check = dynamic_cast<MythUICheckBox *> (GetChild("eac3check"));
33  m_truehdCheck = dynamic_cast<MythUICheckBox *> (GetChild("truehdcheck"));
34  m_dtshdCheck = dynamic_cast<MythUICheckBox *> (GetChild("dtshdcheck"));
35 
37  dynamic_cast<MythUIButton *> (GetChild("testspeakers"));
38 
39  m_nextButton = dynamic_cast<MythUIButton *> (GetChild("next"));
40  m_prevButton = dynamic_cast<MythUIButton *> (GetChild("previous"));
41 
45  {
46  LOG(VB_GENERAL, LOG_ERR, "Theme is missing critical theme elements.");
47  return false;
48  }
49 
50  // Pre-set the widgets to their database values
51  // Buttonlists are set in load()
52 
53  int dtsSetting = gCoreContext->GetNumSetting("DTSPassThru", 0);
54  if (dtsSetting == 1)
56 
57  int ac3Setting = gCoreContext->GetNumSetting("AC3PassThru", 0);
58  if (ac3Setting == 1)
60 
61  int eac3Setting = gCoreContext->GetNumSetting("EAC3PassThru", 0);
62  if (eac3Setting == 1)
64 
65  int truehdSetting = gCoreContext->GetNumSetting("TrueHDPassThru", 0);
66  if (truehdSetting == 1)
68 
69  int dtshdSetting = gCoreContext->GetNumSetting("DTSHDPassThru", 0);
70  if (dtshdSetting == 1)
72 
73  // Help Text
74 
75  // Buttonlists
76  m_audioDeviceButtonList->SetHelpText( tr("Select from one of the "
77  "audio devices detected on your system. When "
78  "satisfied, you can test audio before moving "
79  "on. If you fail to configure audio, video "
80  "playback may fail as well.") );
81  m_speakerNumberButtonList->SetHelpText( tr("Select the number of speakers you "
82  "have.") );
83 
84  // Checkboxes
85  m_dtsCheck->SetHelpText( tr("Select this checkbox if your receiver "
86  "is capable of playing DTS.") );
87  m_ac3Check->SetHelpText( tr("Select this checkbox if your receiver "
88  "is capable of playing AC-3 (Dolby Digital).") );
89  m_eac3Check->SetHelpText( tr("Select this checkbox if your receiver "
90  "is capable of playing E-AC-3 (Dolby Digital Plus).") );
91  m_truehdCheck->SetHelpText( tr("Select this checkbox if your receiver "
92  "is capable of playing TrueHD.") );
93  m_dtshdCheck->SetHelpText( tr("Select this checkbox if your receiver "
94  "is capable of playing DTS-HD.") );
95 
96  // Buttons
97  m_testSpeakerButton->SetHelpText( tr("Test your audio settings by playing "
98  "noise through each speaker.") );
99  m_nextButton->SetHelpText( tr("Save these changes and move on to the next "
100  "configuration step.") );
101  m_prevButton->SetHelpText( tr("Return to the previous configuration step.") );
102 
103  // I hate to SetText but it's the only way to make it reliably bi-modal
104  m_testSpeakerButton->SetText(tr("Test Speakers"));
105 
109 
110  QString message = tr("Discovering audio devices...");
111  LoadInBackground(message);
112 
113  BuildFocusList();
114 
115  return true;
116 }
117 
119 {
120  if (m_testThread)
121  {
122  m_testThread->cancel();
123  m_testThread->wait();
124  delete m_testThread;
125  }
126  delete m_outputlist;
127 }
128 
130 {
132 }
133 
135 {
136  QString current = gCoreContext->GetSetting(QString("AudioOutputDevice"));
137  bool found = false;
138 
139  if (!current.isEmpty())
140  {
141  auto samename = [current](const auto & ao){ return ao.m_name == current; };
142  found = std::any_of(m_outputlist->cbegin(), m_outputlist->cend(), samename);
143  if (!found)
144  {
147  if (adc->m_settings.IsInvalid())
148  {
149  LOG(VB_GENERAL, LOG_ERR, QString("Audio device %1 isn't usable")
150  .arg(current));
151  }
152  else
153  {
154  // only insert the device if it is valid
155  m_outputlist->insert(0, *adc);
156  }
157  delete adc;
158  }
159  }
160  for (const auto & ao : qAsConst(*m_outputlist))
161  {
162  QString name = ao.m_name;
164  output->SetData(name);
165  }
166  if (found)
167  {
168  m_audioDeviceButtonList->SetValueByData(QVariant::fromValue(current));
169  }
170 
171  m_maxspeakers = gCoreContext->GetNumSetting("MaxChannels", 2);
173 
174  // Update list for default audio device
176 
180  this, qOverload<MythUIButtonListItem*>(&AudioSetupWizard::UpdateCapabilities));
181 }
182 
184 {
185  QString out = m_audioDeviceButtonList->GetItemCurrent()->GetText();
186  int max_speakers = 8;
187  int realmax_speakers = 8;
188 
189  AudioOutputSettings settings;
190 
191  auto samename = [out](const auto & ao){ return ao.m_name == out; };
192  // NOLINTNEXTLINE(readability-qualified-auto) // qt6
193  const auto ao = std::find_if(m_outputlist->cbegin(), m_outputlist->cend(), samename);
194  if (ao != m_outputlist->cend())
195  settings = ao->m_settings;
196 
197  realmax_speakers = max_speakers = settings.BestSupportedChannels();
198 
199  bool bAC3 = settings.canFeature(FEATURE_AC3);
200  bool bDTS = settings.canFeature(FEATURE_DTS);
201  bool bLPCM = settings.canFeature(FEATURE_LPCM);
202  bool bEAC3 = settings.canFeature(FEATURE_EAC3);
203  bool bTRUEHD = settings.canFeature(FEATURE_TRUEHD);
204  bool bDTSHD = settings.canFeature(FEATURE_DTSHD);
205 
206  bAC3 ? m_ac3Check->Show() : m_ac3Check->Hide();
207  bDTS ? m_dtsCheck->Show() : m_dtsCheck->Hide();
208  bEAC3 ? m_eac3Check->Show() : m_eac3Check->Hide();
209  bTRUEHD ? m_truehdCheck->Show() : m_truehdCheck->Hide();
210  bDTSHD ? m_dtshdCheck->Show() : m_dtshdCheck->Hide();
211 
214 
215  if (max_speakers > 2 && !bLPCM)
216  max_speakers = 2;
217  if (max_speakers == 2 && bAC3)
218  {
219  max_speakers = 6;
220  if (AC3)
221  {
222  restore = true;
223  }
224  }
225 
226  int cur_speakers = m_maxspeakers;
227 
228  if (m_speakerNumberButtonList->GetItemCurrent() != nullptr)
229  {
231  .value<int>();
232  }
233  if (cur_speakers > m_maxspeakers)
234  {
235  m_maxspeakers = cur_speakers;
236  }
237  if (restore)
238  {
239  cur_speakers = m_maxspeakers;
240  }
241 
242  if (cur_speakers > max_speakers)
243  {
244  LOG(VB_AUDIO, LOG_INFO, QString("Reset device %1").arg(out));
245  cur_speakers = max_speakers;
246  }
247 
248  // Remove everything and re-add available channels
250  for (int i = 1; i <= max_speakers; i++)
251  {
252  if (settings.IsSupportedChannels(i))
253  {
254  switch (i)
255  {
256  case 2:
257  {
258  auto *stereo =
260  QObject::tr("Stereo"));
261  stereo->SetData(2);
262  break;
263  }
264  case 6:
265  {
266  auto *sixchan =
268  QObject::tr("5.1 Channel Audio"));
269  sixchan->SetData(6);
270  break;
271  }
272  case 8:
273  {
274  auto *eightchan =
276  QObject::tr("7.1 Channel Audio"));
277  eightchan->SetData(8);
278  break;
279  }
280  default:
281  continue;
282  }
283  }
284  }
285  m_speakerNumberButtonList->SetValueByData(QVariant::fromValue(cur_speakers));
286 
287  // Return values is used by audio test
288  // where we mainly are interested by the number of channels
289  // if we support AC3 and/or LPCM
290  settings.SetBestSupportedChannels(cur_speakers);
291  settings.setFeature(bAC3, FEATURE_AC3);
292  settings.setFeature(bDTS, FEATURE_DTS);
293  settings.setFeature(bLPCM && realmax_speakers > 2, FEATURE_LPCM);
294 
295  return settings;
296 }
297 
299  MythUIButtonListItem* item)
300 {
301  bool restore = false;
302  if (item)
303  {
304  restore = item->GetText() != m_lastAudioDevice;
305  m_lastAudioDevice = item->GetText();
306  }
307  return UpdateCapabilities(restore);
308 }
309 
311 {
312  return UpdateCapabilities(false, true);
313 }
314 
316 {
317  if (m_testThread)
318  {
319  toggleSpeakers();
320  }
321 
322  save();
323 
325  auto *sw = new VideoSetupWizard(mainStack, m_generalScreen,
326  this, "videosetupwizard");
327 
328  if (sw->Create())
329  {
330  mainStack->AddScreen(sw);
331  }
332  else
333  delete sw;
334 }
335 
337 {
338  // reset advanced audio config to default values
339  gCoreContext->SaveBoolSetting("StereoPCM", false);
340  gCoreContext->SaveBoolSetting("Audio48kOverride", false);
341  gCoreContext->SaveBoolSetting("HBRPassthru", true);
342  gCoreContext->SaveBoolSetting("PassThruDeviceOverride", false);
343  gCoreContext->SaveSetting("PassThruOutputDevice", QString());
344 
346  .value<int>();
347  gCoreContext->SaveSetting("MaxChannels", channels);
348 
349  QString device =
351  gCoreContext->SaveSetting("AudioOutputDevice", device);
352 
353  bool ac3State = (m_ac3Check->GetCheckState() == MythUIStateType::Full);
354  gCoreContext->SaveBoolSetting("AC3PassThru", ac3State);
355 
356  bool dtsState = (m_dtsCheck->GetCheckState() == MythUIStateType::Full);
357  gCoreContext->SaveBoolSetting("DTSPassThru", dtsState);
358 
359  bool eac3State = (m_eac3Check->GetCheckState() == MythUIStateType::Full);
360  gCoreContext->SaveBoolSetting("EAC3PassThru", eac3State);
361 
362  bool truehdState = (m_truehdCheck->GetCheckState() == MythUIStateType::Full);
363  gCoreContext->SaveBoolSetting("TrueHDPassThru", truehdState);
364 
365  bool dtshdState = (m_dtshdCheck->GetCheckState() == MythUIStateType::Full);
366  gCoreContext->SaveBoolSetting("DTSHDPassThru", dtshdState);
367 }
368 
370 {
371  Close();
372 }
373 
374 bool AudioSetupWizard::keyPressEvent(QKeyEvent *event)
375 {
376  if (GetFocusWidget()->keyPressEvent(event))
377  return true;
378 
379  bool handled = false;
380 
382  handled = true;
383 
384  return handled;
385 }
386 
388 {
389  if (m_testThread)
390  {
391  m_testThread->cancel();
392  m_testThread->wait();
393  delete m_testThread;
394  m_testThread = nullptr;
395  m_testSpeakerButton->SetText(tr("Test Speakers"));
396  return;
397  }
398 
399  AudioOutputSettings settings = UpdateCapabilities(false);
400  QString out = m_audioDeviceButtonList->GetItemCurrent()->GetText();
402  .value<int> ();
403 
404  m_testThread =
405  new AudioTestThread(this, out, out, channels, settings, false);
406  if (!m_testThread->result().isEmpty())
407  {
408  QString msg = QObject::tr("Audio device is invalid or not useable.");
409  ShowOkPopup(msg);
410  delete m_testThread;
411  m_testThread = nullptr;
412  }
413  else
414  {
415  m_testThread->start();
416  m_testSpeakerButton->SetText(tr("Stop Speaker Test"));
417  }
418 }
MythUIButton::Clicked
void Clicked()
MythScreenType::LoadInBackground
void LoadInBackground(const QString &message="")
Definition: mythscreentype.cpp:285
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:317
AudioOutputSettings::IsSupportedChannels
bool IsSupportedChannels(int channels)
Definition: audiooutputsettings.cpp:243
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1590
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:286
AudioSetupWizard::m_testSpeakerButton
MythUIButton * m_testSpeakerButton
Definition: setupwizard_audio.h:48
AudioSetupWizard::m_prevButton
MythUIButton * m_prevButton
Definition: setupwizard_audio.h:51
MythUIButtonList::SetValueByData
void SetValueByData(const QVariant &data)
Definition: mythuibuttonlist.cpp:1542
AudioSetupWizard::Init
void Init(void) override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
Definition: setupwizard_audio.cpp:134
AudioSetupWizard::m_audioDeviceButtonList
MythUIButtonList * m_audioDeviceButtonList
Definition: setupwizard_audio.h:39
audiooutpututil.h
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:385
MThread::wait
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:303
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:131
FEATURE_AC3
@ FEATURE_AC3
Definition: audiooutputsettings.h:37
AudioTestThread::cancel
void cancel()
Definition: audiogeneralsettings.cpp:639
MythUIButtonList::itemSelected
void itemSelected(MythUIButtonListItem *item)
setupwizard_general.h
AudioSetupWizard::slotNext
void slotNext(void)
Definition: setupwizard_audio.cpp:315
MythScreenStack
Definition: mythscreenstack.h:16
mythdbcon.h
FEATURE_TRUEHD
@ FEATURE_TRUEHD
Definition: audiooutputsettings.h:41
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
AC3
@ AC3
Definition: element.h:84
AudioSetupWizard::UpdateCapabilities
AudioOutputSettings UpdateCapabilities(bool restore=true, bool AC3=false)
Definition: setupwizard_audio.cpp:183
AudioSetupWizard::Create
bool Create(void) override
Definition: setupwizard_audio.cpp:18
FEATURE_EAC3
@ FEATURE_EAC3
Definition: audiooutputsettings.h:40
mythdirs.h
setupwizard_video.h
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
mythprogressdialog.h
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:112
MythUIButtonListItem
Definition: mythuibuttonlist.h:27
MythUICheckBox::valueChanged
void valueChanged()
MythUIType::Show
void Show(void)
Definition: mythuitype.cpp:1125
AudioSetupWizard::m_ac3Check
MythUICheckBox * m_ac3Check
Definition: setupwizard_audio.h:43
AudioSetupWizard::save
void save(void)
Definition: setupwizard_audio.cpp:336
VideoSetupWizard
Definition: setupwizard_video.h:19
AudioSetupWizard::m_lastAudioDevice
QString m_lastAudioDevice
Definition: setupwizard_audio.h:53
AudioOutputSettings::BestSupportedChannels
int BestSupportedChannels()
Definition: audiooutputsettings.cpp:253
FEATURE_DTS
@ FEATURE_DTS
Definition: audiooutputsettings.h:38
MythUIButton::SetText
void SetText(const QString &msg)
Definition: mythuibutton.cpp:227
MythUIType::SetHelpText
void SetHelpText(const QString &text)
Definition: mythuitype.h:172
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:205
AudioSetupWizard::Load
void Load(void) override
Load data which will ultimately be displayed on-screen or used to determine what appears on-screen (S...
Definition: setupwizard_audio.cpp:129
AudioTestThread
Definition: audiogeneralsettings.h:147
AudioOutput::AudioDeviceConfig::m_settings
AudioOutputSettings m_settings
Definition: audiooutput.h:36
AudioSetupWizard::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: setupwizard_audio.cpp:374
MythUIButton
A single button widget.
Definition: mythuibutton.h:21
FEATURE_LPCM
@ FEATURE_LPCM
Definition: audiooutputsettings.h:39
AudioSetupWizard::m_dtsCheck
MythUICheckBox * m_dtsCheck
Definition: setupwizard_audio.h:42
AudioOutput::GetAudioDeviceConfig
static AudioDeviceConfig * GetAudioDeviceConfig(QString &name, const QString &desc, bool willsuspendpa=false)
Definition: audiooutput.cpp:302
AudioSetupWizard::m_outputlist
QVector< AudioOutput::AudioDeviceConfig > * m_outputlist
Definition: setupwizard_audio.h:34
MythUIStateType::Full
@ Full
Definition: mythuistatetype.h:25
AudioOutputSettings::canFeature
bool canFeature(DigitalFeature arg) const
return DigitalFeature mask.
Definition: audiooutputsettings.h:89
audiogeneralsettings.h
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3582
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
MythUICheckBox
A checkbox widget supporting three check states - on,off,half and two conditions - selected and unsel...
Definition: mythuicheckbox.h:15
AudioSetupWizard::~AudioSetupWizard
~AudioSetupWizard() override
Definition: setupwizard_audio.cpp:118
setupwizard_audio.h
AudioOutputSettings::setFeature
void setFeature(DigitalFeature arg)
set the provided digital feature possible values are:
Definition: audiooutputsettings.h:126
AudioSetupWizard::m_generalScreen
MythScreenType * m_generalScreen
Definition: setupwizard_audio.h:37
MythCoreContext::GetNumSetting
int GetNumSetting(const QString &key, int defaultval=0)
Definition: mythcorecontext.cpp:937
AudioSetupWizard::m_truehdCheck
MythUICheckBox * m_truehdCheck
Definition: setupwizard_audio.h:45
AudioOutputSettings::IsInvalid
bool IsInvalid() const
return true if class instance is marked invalid.
Definition: audiooutputsettings.h:114
AudioSetupWizard::m_maxspeakers
int m_maxspeakers
Definition: setupwizard_audio.h:52
MythUIType::Hide
void Hide(void)
Definition: mythuitype.cpp:1120
AudioOutput::AudioDeviceConfig
Definition: audiooutput.h:31
MythUIButtonListItem::GetText
QString GetText(const QString &name="") const
Definition: mythuibuttonlist.cpp:3307
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:397
MythUICheckBox::SetCheckState
void SetCheckState(MythUIStateType::StateType state)
Definition: mythuicheckbox.cpp:66
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:692
AudioSetupWizard::m_testThread
AudioTestThread * m_testThread
Definition: setupwizard_audio.h:35
AudioSetupWizard::m_speakerNumberButtonList
MythUIButtonList * m_speakerNumberButtonList
Definition: setupwizard_audio.h:40
AudioOutputSettings
Definition: audiooutputsettings.h:49
AudioOutput::AudioDeviceConfig::m_name
QString m_name
Definition: audiooutput.h:34
mythcontext.h
AudioTestThread::result
QString result()
Definition: audiogeneralsettings.cpp:644
AudioSetupWizard::m_eac3Check
MythUICheckBox * m_eac3Check
Definition: setupwizard_audio.h:44
MythUIButtonList::Reset
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuibuttonlist.cpp:114
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
AudioSetupWizard::slotPrevious
void slotPrevious(void)
Definition: setupwizard_audio.cpp:369
AudioOutputSettings::SetBestSupportedChannels
void SetBestSupportedChannels(int channels)
Force set the greatest number of channels supported by the audio device.
Definition: audiooutputsettings.cpp:267
AudioSetupWizard::m_nextButton
MythUIButton * m_nextButton
Definition: setupwizard_audio.h:50
MythCoreContext::SaveSetting
void SaveSetting(const QString &key, int newValue)
Definition: mythcorecontext.cpp:906
AudioSetupWizard::UpdateCapabilitiesAC3
AudioOutputSettings UpdateCapabilitiesAC3(void)
Definition: setupwizard_audio.cpp:310
MythUICheckBox::GetCheckState
MythUIStateType::StateType GetCheckState() const
Definition: mythuicheckbox.cpp:98
MythUIButtonList
List widget, displays list items in a variety of themeable arrangements and can trigger signals when ...
Definition: mythuibuttonlist.h:151
AudioSetupWizard::toggleSpeakers
void toggleSpeakers(void)
Definition: setupwizard_audio.cpp:387
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:50
ShowOkPopup
MythConfirmationDialog * ShowOkPopup(const QString &message, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
Definition: mythdialogbox.cpp:558
FEATURE_DTSHD
@ FEATURE_DTSHD
Definition: audiooutputsettings.h:42
output
#define output
Definition: synaesthesia.cpp:220
AudioSetupWizard::m_dtshdCheck
MythUICheckBox * m_dtshdCheck
Definition: setupwizard_audio.h:46
MythCoreContext::SaveBoolSetting
void SaveBoolSetting(const QString &key, bool newValue)
Definition: mythcorecontext.h:162
AudioOutput::GetOutputList
static ADCVect * GetOutputList(void)
Definition: audiooutput.cpp:415
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:923