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 
106  connect(m_testSpeakerButton, SIGNAL(Clicked()), this, SLOT(toggleSpeakers()));
107  connect(m_nextButton, SIGNAL(Clicked()), this, SLOT(slotNext()));
108  connect(m_prevButton, SIGNAL(Clicked()), this, SLOT(slotPrevious()));
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  for (const auto & ao : qAsConst(*m_outputlist))
142  {
143  if (ao.m_name == current)
144  {
145  found = true;
146  break;
147  }
148  }
149  if (!found)
150  {
153  if (adc->m_settings.IsInvalid())
154  {
155  LOG(VB_GENERAL, LOG_ERR, QString("Audio device %1 isn't usable")
156  .arg(current));
157  }
158  else
159  {
160  // only insert the device if it is valid
161  m_outputlist->insert(0, *adc);
162  }
163  delete adc;
164  }
165  }
166  for (const auto & ao : qAsConst(*m_outputlist))
167  {
168  QString name = ao.m_name;
170  output->SetData(name);
171  }
172  if (found)
173  {
174  m_audioDeviceButtonList->SetValueByData(QVariant::fromValue(current));
175  }
176 
177  m_maxspeakers = gCoreContext->GetNumSetting("MaxChannels", 2);
179 
180  // Update list for default audio device
182 
183  connect(m_ac3Check,
184  SIGNAL(valueChanged()), SLOT(UpdateCapabilitiesAC3()));
185  connect(m_audioDeviceButtonList,
186  SIGNAL(itemSelected(MythUIButtonListItem*)),
188 }
189 
191 {
192  QString out = m_audioDeviceButtonList->GetItemCurrent()->GetText();
193  int max_speakers = 8;
194  int realmax_speakers = 8;
195 
196  AudioOutputSettings settings;
197 
198  for (const auto & ao : qAsConst(*m_outputlist))
199  {
200  if (ao.m_name == out)
201  {
202  settings = ao.m_settings;
203  break;
204  }
205  }
206 
207  realmax_speakers = max_speakers = settings.BestSupportedChannels();
208 
209  bool bAC3 = settings.canFeature(FEATURE_AC3);
210  bool bDTS = settings.canFeature(FEATURE_DTS);
211  bool bLPCM = settings.canFeature(FEATURE_LPCM);
212  bool bEAC3 = settings.canFeature(FEATURE_EAC3);
213  bool bTRUEHD = settings.canFeature(FEATURE_TRUEHD);
214  bool bDTSHD = settings.canFeature(FEATURE_DTSHD);
215 
216  bAC3 ? m_ac3Check->Show() : m_ac3Check->Hide();
217  bDTS ? m_dtsCheck->Show() : m_dtsCheck->Hide();
218  bEAC3 ? m_eac3Check->Show() : m_eac3Check->Hide();
219  bTRUEHD ? m_truehdCheck->Show() : m_truehdCheck->Hide();
220  bDTSHD ? m_dtshdCheck->Show() : m_dtshdCheck->Hide();
221 
224 
225  if (max_speakers > 2 && !bLPCM)
226  max_speakers = 2;
227  if (max_speakers == 2 && bAC3)
228  {
229  max_speakers = 6;
230  if (AC3)
231  {
232  restore = true;
233  }
234  }
235 
236  int cur_speakers = m_maxspeakers;
237 
238  if (m_speakerNumberButtonList->GetItemCurrent() != nullptr)
239  {
241  .value<int>();
242  }
243  if (cur_speakers > m_maxspeakers)
244  {
245  m_maxspeakers = cur_speakers;
246  }
247  if (restore)
248  {
249  cur_speakers = m_maxspeakers;
250  }
251 
252  if (cur_speakers > max_speakers)
253  {
254  LOG(VB_AUDIO, LOG_INFO, QString("Reset device %1").arg(out));
255  cur_speakers = max_speakers;
256  }
257 
258  // Remove everything and re-add available channels
260  for (int i = 1; i <= max_speakers; i++)
261  {
262  if (settings.IsSupportedChannels(i))
263  {
264  switch (i)
265  {
266  case 2:
267  {
268  auto *stereo =
270  QObject::tr("Stereo"));
271  stereo->SetData(2);
272  break;
273  }
274  case 6:
275  {
276  auto *sixchan =
278  QObject::tr("5.1 Channel Audio"));
279  sixchan->SetData(6);
280  break;
281  }
282  case 8:
283  {
284  auto *eightchan =
286  QObject::tr("7.1 Channel Audio"));
287  eightchan->SetData(8);
288  break;
289  }
290  default:
291  continue;
292  }
293  }
294  }
295  m_speakerNumberButtonList->SetValueByData(QVariant::fromValue(cur_speakers));
296 
297  // Return values is used by audio test
298  // where we mainly are interested by the number of channels
299  // if we support AC3 and/or LPCM
300  settings.SetBestSupportedChannels(cur_speakers);
301  settings.setFeature(bAC3, FEATURE_AC3);
302  settings.setFeature(bDTS, FEATURE_DTS);
303  settings.setFeature(bLPCM && realmax_speakers > 2, FEATURE_LPCM);
304 
305  return settings;
306 }
307 
309  MythUIButtonListItem* item)
310 {
311  bool restore = false;
312  if (item)
313  {
314  restore = item->GetText() != m_lastAudioDevice;
315  m_lastAudioDevice = item->GetText();
316  }
317  return UpdateCapabilities(restore);
318 }
319 
321 {
322  return UpdateCapabilities(false, true);
323 }
324 
326 {
327  if (m_testThread)
328  {
329  toggleSpeakers();
330  }
331 
332  save();
333 
335  auto *sw = new VideoSetupWizard(mainStack, m_generalScreen,
336  this, "videosetupwizard");
337 
338  if (sw->Create())
339  {
340  mainStack->AddScreen(sw);
341  }
342  else
343  delete sw;
344 }
345 
347 {
348  // reset advanced audio config to default values
349  gCoreContext->SaveBoolSetting("StereoPCM", false);
350  gCoreContext->SaveBoolSetting("Audio48kOverride", false);
351  gCoreContext->SaveBoolSetting("HBRPassthru", true);
352  gCoreContext->SaveBoolSetting("PassThruDeviceOverride", false);
353  gCoreContext->SaveSetting("PassThruOutputDevice", QString());
354 
356  .value<int>();
357  gCoreContext->SaveSetting("MaxChannels", channels);
358 
359  QString device =
361  gCoreContext->SaveSetting("AudioOutputDevice", device);
362 
363  bool ac3State = (m_ac3Check->GetCheckState() == MythUIStateType::Full);
364  gCoreContext->SaveBoolSetting("AC3PassThru", ac3State);
365 
366  bool dtsState = (m_dtsCheck->GetCheckState() == MythUIStateType::Full);
367  gCoreContext->SaveBoolSetting("DTSPassThru", dtsState);
368 
369  bool eac3State = (m_eac3Check->GetCheckState() == MythUIStateType::Full);
370  gCoreContext->SaveBoolSetting("EAC3PassThru", eac3State);
371 
372  bool truehdState = (m_truehdCheck->GetCheckState() == MythUIStateType::Full);
373  gCoreContext->SaveBoolSetting("TrueHDPassThru", truehdState);
374 
375  bool dtshdState = (m_dtshdCheck->GetCheckState() == MythUIStateType::Full);
376  gCoreContext->SaveBoolSetting("DTSHDPassThru", dtshdState);
377 }
378 
380 {
381  Close();
382 }
383 
384 bool AudioSetupWizard::keyPressEvent(QKeyEvent *event)
385 {
386  if (GetFocusWidget()->keyPressEvent(event))
387  return true;
388 
389  bool handled = false;
390 
392  handled = true;
393 
394  return handled;
395 }
396 
398 {
399  if (m_testThread)
400  {
401  m_testThread->cancel();
402  m_testThread->wait();
403  delete m_testThread;
404  m_testThread = nullptr;
405  m_testSpeakerButton->SetText(tr("Test Speakers"));
406  return;
407  }
408 
409  AudioOutputSettings settings = UpdateCapabilities(false);
410  QString out = m_audioDeviceButtonList->GetItemCurrent()->GetText();
412  .value<int> ();
413 
414  m_testThread =
415  new AudioTestThread(this, out, out, channels, settings, false);
416  if (!m_testThread->result().isEmpty())
417  {
418  QString msg = QObject::tr("Audio device is invalid or not useable.");
419  ShowOkPopup(msg);
420  delete m_testThread;
421  m_testThread = nullptr;
422  }
423  else
424  {
425  m_testThread->start();
426  m_testSpeakerButton->SetText(tr("Stop Speaker Test"));
427  }
428 }
void Load(void) override
Load data which will ultimately be displayed on-screen or used to determine what appears on-screen (S...
~AudioSetupWizard() override
void Show(void)
MythUIStateType::StateType GetCheckState() const
AudioTestThread * m_testThread
QVector< AudioOutput::AudioDeviceConfig > * m_outputlist
bool IsSupportedChannels(int channels)
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
MythConfirmationDialog * ShowOkPopup(const QString &message, QObject *parent, const char *slot, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
bool canFeature(DigitalFeature arg) const
return DigitalFeature mask.
void SaveSetting(const QString &key, int newValue)
MythScreenType * m_generalScreen
Definition: element.h:83
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
void SetBestSupportedChannels(int channels)
Force set the greatest number of channels supported by the audio device.
MythUICheckBox * m_eac3Check
MythUIButton * m_nextButton
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythScreenStack * GetMainStack()
AudioOutputSettings m_settings
Definition: audiooutput.h:34
MythUICheckBox * m_dtshdCheck
void BuildFocusList(void)
AudioOutputSettings UpdateCapabilitiesAC3(void)
void Hide(void)
virtual void Close()
MythUIButtonList * m_speakerNumberButtonList
void LoadInBackground(const QString &message="")
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
void SetHelpText(const QString &text)
Definition: mythuitype.h:150
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QString GetSetting(const QString &key, const QString &defaultval="")
void setFeature(DigitalFeature arg)
set the provided digital feature possible values are:
void Init(void) override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
static AudioDeviceConfig * GetAudioDeviceConfig(QString &name, QString &desc, bool willsuspendpa=false)
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MythUIType * GetFocusWidget(void) const
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
MythMainWindow * GetMythMainWindow(void)
QString GetText(const QString &name="") const
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
int GetNumSetting(const QString &key, int defaultval=0)
MythUIButton * m_testSpeakerButton
bool Create(void) override
MythUIButton * m_prevButton
MythUIButtonList * m_audioDeviceButtonList
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
void SetCheckState(MythUIStateType::StateType state)
MythUICheckBox * m_ac3Check
void SaveBoolSetting(const QString &key, bool newValue)
MythUICheckBox * m_dtsCheck
AudioOutputSettings UpdateCapabilities(bool restore=true, bool AC3=false)
static ADCVect * GetOutputList(void)
void SetText(const QString &msg)
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:130
MythUICheckBox * m_truehdCheck
MythUIButtonListItem * GetItemCurrent() const
void SetValueByData(const QVariant &data)
#define output
bool IsInvalid() const
return true if class instance is marked invalid.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23