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 (AudioOutput::ADCVect::const_iterator it = m_outputlist->begin();
142  it != m_outputlist->end(); ++it)
143  {
144  if (it->m_name == current)
145  {
146  found = true;
147  break;
148  }
149  }
150  if (!found)
151  {
154  if (adc->m_settings.IsInvalid())
155  {
156  LOG(VB_GENERAL, LOG_ERR, QString("Audio device %1 isn't usable")
157  .arg(current));
158  }
159  else
160  {
161  // only insert the device if it is valid
162  m_outputlist->insert(0, *adc);
163  }
164  delete adc;
165  }
166  }
167  for (AudioOutput::ADCVect::const_iterator it = m_outputlist->begin();
168  it != m_outputlist->end(); ++it)
169  {
170  QString name = it->m_name;
172  output->SetData(name);
173  }
174  if (found)
175  {
176  m_audioDeviceButtonList->SetValueByData(qVariantFromValue(current));
177  }
178 
179  m_maxspeakers = gCoreContext->GetNumSetting("MaxChannels", 2);
181 
182  // Update list for default audio device
184 
185  connect(m_ac3Check,
186  SIGNAL(valueChanged()), SLOT(UpdateCapabilitiesAC3()));
187  connect(m_audioDeviceButtonList,
188  SIGNAL(itemSelected(MythUIButtonListItem*)),
190 }
191 
193 {
194  QString out = m_audioDeviceButtonList->GetItemCurrent()->GetText();
195  int max_speakers = 8;
196  int realmax_speakers = 8;
197 
198  AudioOutputSettings settings;
199 
200  for (AudioOutput::ADCVect::const_iterator it = m_outputlist->begin();
201  it != m_outputlist->end(); ++it)
202  {
203  if (it->m_name == out)
204  {
205  settings = it->m_settings;
206  break;
207  }
208  }
209 
210  realmax_speakers = max_speakers = settings.BestSupportedChannels();
211 
212  bool bAC3 = settings.canFeature(FEATURE_AC3);
213  bool bDTS = settings.canFeature(FEATURE_DTS);
214  bool bLPCM = settings.canFeature(FEATURE_LPCM);
215  bool bEAC3 = settings.canFeature(FEATURE_EAC3);
216  bool bTRUEHD = settings.canFeature(FEATURE_TRUEHD);
217  bool bDTSHD = settings.canFeature(FEATURE_DTSHD);
218 
219  bAC3 ? m_ac3Check->Show() : m_ac3Check->Hide();
220  bDTS ? m_dtsCheck->Show() : m_dtsCheck->Hide();
221  bEAC3 ? m_eac3Check->Show() : m_eac3Check->Hide();
222  bTRUEHD ? m_truehdCheck->Show() : m_truehdCheck->Hide();
223  bDTSHD ? m_dtshdCheck->Show() : m_dtshdCheck->Hide();
224 
227 
228  if (max_speakers > 2 && !bLPCM)
229  max_speakers = 2;
230  if (max_speakers == 2 && bAC3)
231  {
232  max_speakers = 6;
233  if (AC3)
234  {
235  restore = true;
236  }
237  }
238 
239  int cur_speakers = m_maxspeakers;
240 
241  if (m_speakerNumberButtonList->GetItemCurrent() != nullptr)
242  {
244  .value<int>();
245  }
246  if (cur_speakers > m_maxspeakers)
247  {
248  m_maxspeakers = cur_speakers;
249  }
250  if (restore)
251  {
252  cur_speakers = m_maxspeakers;
253  }
254 
255  if (cur_speakers > max_speakers)
256  {
257  LOG(VB_AUDIO, LOG_INFO, QString("Reset device %1").arg(out));
258  cur_speakers = max_speakers;
259  }
260 
261  // Remove everything and re-add available channels
263  for (int i = 1; i <= max_speakers; i++)
264  {
265  if (settings.IsSupportedChannels(i))
266  {
267  switch (i)
268  {
269  case 2:
270  {
271  auto *stereo =
273  QObject::tr("Stereo"));
274  stereo->SetData(2);
275  break;
276  }
277  case 6:
278  {
279  auto *sixchan =
281  QObject::tr("5.1 Channel Audio"));
282  sixchan->SetData(6);
283  break;
284  }
285  case 8:
286  {
287  auto *eightchan =
289  QObject::tr("7.1 Channel Audio"));
290  eightchan->SetData(8);
291  break;
292  }
293  default:
294  continue;
295  }
296  }
297  }
298  m_speakerNumberButtonList->SetValueByData(qVariantFromValue(cur_speakers));
299 
300  // Return values is used by audio test
301  // where we mainly are interested by the number of channels
302  // if we support AC3 and/or LPCM
303  settings.SetBestSupportedChannels(cur_speakers);
304  settings.setFeature(bAC3, FEATURE_AC3);
305  settings.setFeature(bDTS, FEATURE_DTS);
306  settings.setFeature(bLPCM && realmax_speakers > 2, FEATURE_LPCM);
307 
308  return settings;
309 }
310 
312  MythUIButtonListItem* item)
313 {
314  bool restore = false;
315  if (item)
316  {
317  restore = item->GetText() != m_lastAudioDevice;
318  m_lastAudioDevice = item->GetText();
319  }
320  return UpdateCapabilities(restore);
321 }
322 
324 {
325  return UpdateCapabilities(false, true);
326 }
327 
329 {
330  if (m_testThread)
331  {
332  toggleSpeakers();
333  }
334 
335  save();
336 
338  auto *sw = new VideoSetupWizard(mainStack, m_generalScreen,
339  this, "videosetupwizard");
340 
341  if (sw->Create())
342  {
343  mainStack->AddScreen(sw);
344  }
345  else
346  delete sw;
347 }
348 
350 {
351  // reset advanced audio config to default values
352  gCoreContext->SaveBoolSetting("StereoPCM", false);
353  gCoreContext->SaveBoolSetting("Audio48kOverride", false);
354  gCoreContext->SaveBoolSetting("HBRPassthru", true);
355  gCoreContext->SaveBoolSetting("PassThruDeviceOverride", false);
356  gCoreContext->SaveSetting("PassThruOutputDevice", QString());
357 
359  .value<int>();
360  gCoreContext->SaveSetting("MaxChannels", channels);
361 
362  QString device =
364  gCoreContext->SaveSetting("AudioOutputDevice", device);
365 
366  bool ac3State = (m_ac3Check->GetCheckState() == MythUIStateType::Full);
367  gCoreContext->SaveBoolSetting("AC3PassThru", ac3State);
368 
369  bool dtsState = (m_dtsCheck->GetCheckState() == MythUIStateType::Full);
370  gCoreContext->SaveBoolSetting("DTSPassThru", dtsState);
371 
372  bool eac3State = (m_eac3Check->GetCheckState() == MythUIStateType::Full);
373  gCoreContext->SaveBoolSetting("EAC3PassThru", eac3State);
374 
375  bool truehdState = (m_truehdCheck->GetCheckState() == MythUIStateType::Full);
376  gCoreContext->SaveBoolSetting("TrueHDPassThru", truehdState);
377 
378  bool dtshdState = (m_dtshdCheck->GetCheckState() == MythUIStateType::Full);
379  gCoreContext->SaveBoolSetting("DTSHDPassThru", dtshdState);
380 }
381 
383 {
384  Close();
385 }
386 
387 bool AudioSetupWizard::keyPressEvent(QKeyEvent *event)
388 {
389  if (GetFocusWidget()->keyPressEvent(event))
390  return true;
391 
392  bool handled = false;
393 
395  handled = true;
396 
397  return handled;
398 }
399 
401 {
402  if (m_testThread)
403  {
404  m_testThread->cancel();
405  m_testThread->wait();
406  delete m_testThread;
407  m_testThread = nullptr;
408  m_testSpeakerButton->SetText(tr("Test Speakers"));
409  return;
410  }
411 
412  AudioOutputSettings settings = UpdateCapabilities(false);
413  QString out = m_audioDeviceButtonList->GetItemCurrent()->GetText();
415  .value<int> ();
416 
417  m_testThread =
418  new AudioTestThread(this, out, out, channels, settings, false);
419  if (!m_testThread->result().isEmpty())
420  {
421  QString msg = QObject::tr("Audio device is invalid or not useable.");
422  ShowOkPopup(msg);
423  delete m_testThread;
424  m_testThread = nullptr;
425  }
426  else
427  {
428  m_testThread->start();
429  m_testSpeakerButton->SetText(tr("Stop Speaker Test"));
430  }
431 }
void start(QThread::Priority=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
void Load(void) override
Load data which will ultimately be displayed on-screen or used to determine what appears on-screen (S...
void Show(void)
MythUIStateType::StateType GetCheckState() const
AudioTestThread * m_testThread
QVector< AudioOutput::AudioDeviceConfig > * m_outputlist
bool IsInvalid()
return true if class instance is marked invalid.
bool IsSupportedChannels(int channels)
MythConfirmationDialog * ShowOkPopup(const QString &message, QObject *parent, const char *slot, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
Definition: element.h:83
void SaveSetting(const QString &key, int newValue)
MythScreenType * m_generalScreen
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:32
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...
bool canFeature(DigitalFeature arg)
return DigitalFeature mask.
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
int GetNumSetting(const QString &key, int defaultval=0)
MythUIButton * m_testSpeakerButton
bool keyPressEvent(QKeyEvent *) override
Key event handler.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
bool Create(void) override
MythUIButton * m_prevButton
bool keyPressEvent(QKeyEvent *) override
Key event handler.
MythUIButtonList * m_audioDeviceButtonList
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:132
MythUICheckBox * m_truehdCheck
MythUIButtonListItem * GetItemCurrent() const
void SetValueByData(const QVariant &data)
#define output