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  const auto *ao = std::find_if(m_outputlist->cbegin(), m_outputlist->cend(), samename);
193  if (ao != m_outputlist->cend())
194  settings = ao->m_settings;
195 
196  realmax_speakers = max_speakers = settings.BestSupportedChannels();
197 
198  bool bAC3 = settings.canFeature(FEATURE_AC3);
199  bool bDTS = settings.canFeature(FEATURE_DTS);
200  bool bLPCM = settings.canFeature(FEATURE_LPCM);
201  bool bEAC3 = settings.canFeature(FEATURE_EAC3);
202  bool bTRUEHD = settings.canFeature(FEATURE_TRUEHD);
203  bool bDTSHD = settings.canFeature(FEATURE_DTSHD);
204 
205  bAC3 ? m_ac3Check->Show() : m_ac3Check->Hide();
206  bDTS ? m_dtsCheck->Show() : m_dtsCheck->Hide();
207  bEAC3 ? m_eac3Check->Show() : m_eac3Check->Hide();
208  bTRUEHD ? m_truehdCheck->Show() : m_truehdCheck->Hide();
209  bDTSHD ? m_dtshdCheck->Show() : m_dtshdCheck->Hide();
210 
213 
214  if (max_speakers > 2 && !bLPCM)
215  max_speakers = 2;
216  if (max_speakers == 2 && bAC3)
217  {
218  max_speakers = 6;
219  if (AC3)
220  {
221  restore = true;
222  }
223  }
224 
225  int cur_speakers = m_maxspeakers;
226 
227  if (m_speakerNumberButtonList->GetItemCurrent() != nullptr)
228  {
230  .value<int>();
231  }
232  if (cur_speakers > m_maxspeakers)
233  {
234  m_maxspeakers = cur_speakers;
235  }
236  if (restore)
237  {
238  cur_speakers = m_maxspeakers;
239  }
240 
241  if (cur_speakers > max_speakers)
242  {
243  LOG(VB_AUDIO, LOG_INFO, QString("Reset device %1").arg(out));
244  cur_speakers = max_speakers;
245  }
246 
247  // Remove everything and re-add available channels
249  for (int i = 1; i <= max_speakers; i++)
250  {
251  if (settings.IsSupportedChannels(i))
252  {
253  switch (i)
254  {
255  case 2:
256  {
257  auto *stereo =
259  QObject::tr("Stereo"));
260  stereo->SetData(2);
261  break;
262  }
263  case 6:
264  {
265  auto *sixchan =
267  QObject::tr("5.1 Channel Audio"));
268  sixchan->SetData(6);
269  break;
270  }
271  case 8:
272  {
273  auto *eightchan =
275  QObject::tr("7.1 Channel Audio"));
276  eightchan->SetData(8);
277  break;
278  }
279  default:
280  continue;
281  }
282  }
283  }
284  m_speakerNumberButtonList->SetValueByData(QVariant::fromValue(cur_speakers));
285 
286  // Return values is used by audio test
287  // where we mainly are interested by the number of channels
288  // if we support AC3 and/or LPCM
289  settings.SetBestSupportedChannels(cur_speakers);
290  settings.setFeature(bAC3, FEATURE_AC3);
291  settings.setFeature(bDTS, FEATURE_DTS);
292  settings.setFeature(bLPCM && realmax_speakers > 2, FEATURE_LPCM);
293 
294  return settings;
295 }
296 
298  MythUIButtonListItem* item)
299 {
300  bool restore = false;
301  if (item)
302  {
303  restore = item->GetText() != m_lastAudioDevice;
304  m_lastAudioDevice = item->GetText();
305  }
306  return UpdateCapabilities(restore);
307 }
308 
310 {
311  return UpdateCapabilities(false, true);
312 }
313 
315 {
316  if (m_testThread)
317  {
318  toggleSpeakers();
319  }
320 
321  save();
322 
324  auto *sw = new VideoSetupWizard(mainStack, m_generalScreen,
325  this, "videosetupwizard");
326 
327  if (sw->Create())
328  {
329  mainStack->AddScreen(sw);
330  }
331  else
332  delete sw;
333 }
334 
336 {
337  // reset advanced audio config to default values
338  gCoreContext->SaveBoolSetting("StereoPCM", false);
339  gCoreContext->SaveBoolSetting("Audio48kOverride", false);
340  gCoreContext->SaveBoolSetting("HBRPassthru", true);
341  gCoreContext->SaveBoolSetting("PassThruDeviceOverride", false);
342  gCoreContext->SaveSetting("PassThruOutputDevice", QString());
343 
345  .value<int>();
346  gCoreContext->SaveSetting("MaxChannels", channels);
347 
348  QString device =
350  gCoreContext->SaveSetting("AudioOutputDevice", device);
351 
352  bool ac3State = (m_ac3Check->GetCheckState() == MythUIStateType::Full);
353  gCoreContext->SaveBoolSetting("AC3PassThru", ac3State);
354 
355  bool dtsState = (m_dtsCheck->GetCheckState() == MythUIStateType::Full);
356  gCoreContext->SaveBoolSetting("DTSPassThru", dtsState);
357 
358  bool eac3State = (m_eac3Check->GetCheckState() == MythUIStateType::Full);
359  gCoreContext->SaveBoolSetting("EAC3PassThru", eac3State);
360 
361  bool truehdState = (m_truehdCheck->GetCheckState() == MythUIStateType::Full);
362  gCoreContext->SaveBoolSetting("TrueHDPassThru", truehdState);
363 
364  bool dtshdState = (m_dtshdCheck->GetCheckState() == MythUIStateType::Full);
365  gCoreContext->SaveBoolSetting("DTSHDPassThru", dtshdState);
366 }
367 
369 {
370  Close();
371 }
372 
373 bool AudioSetupWizard::keyPressEvent(QKeyEvent *event)
374 {
375  if (GetFocusWidget()->keyPressEvent(event))
376  return true;
377 
378  bool handled = false;
379 
381  handled = true;
382 
383  return handled;
384 }
385 
387 {
388  if (m_testThread)
389  {
390  m_testThread->cancel();
391  m_testThread->wait();
392  delete m_testThread;
393  m_testThread = nullptr;
394  m_testSpeakerButton->SetText(tr("Test Speakers"));
395  return;
396  }
397 
398  AudioOutputSettings settings = UpdateCapabilities(false);
399  QString out = m_audioDeviceButtonList->GetItemCurrent()->GetText();
401  .value<int> ();
402 
403  m_testThread =
404  new AudioTestThread(this, out, out, channels, settings, false);
405  if (!m_testThread->result().isEmpty())
406  {
407  QString msg = QObject::tr("Audio device is invalid or not useable.");
408  ShowOkPopup(msg);
409  delete m_testThread;
410  m_testThread = nullptr;
411  }
412  else
413  {
414  m_testThread->start();
415  m_testSpeakerButton->SetText(tr("Stop Speaker Test"));
416  }
417 }
MythUIButton::Clicked
void Clicked()
MythScreenType::LoadInBackground
void LoadInBackground(const QString &message="")
Definition: mythscreentype.cpp:302
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:324
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:288
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:402
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:314
MythScreenStack
Definition: mythscreenstack.h:16
arg
arg(title).arg(filename).arg(doDelete))
mythdbcon.h
FEATURE_TRUEHD
@ FEATURE_TRUEHD
Definition: audiooutputsettings.h:41
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
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:28
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:335
VideoSetupWizard
Definition: setupwizard_video.h:20
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:229
MythUIType::SetHelpText
void SetHelpText(const QString &text)
Definition: mythuitype.h:172
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:222
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:148
AudioOutput::AudioDeviceConfig::m_settings
AudioOutputSettings m_settings
Definition: audiooutput.h:35
AudioSetupWizard::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: setupwizard_audio.cpp:373
MythUIButton
A single button widget.
Definition: mythuibutton.h:22
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:3580
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:16
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:933
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:3305
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:414
AC3
@ AC3
Definition: element.h:84
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:50
AudioOutput::AudioDeviceConfig::m_name
QString m_name
Definition: audiooutput.h:33
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:108
AudioSetupWizard::slotPrevious
void slotPrevious(void)
Definition: setupwizard_audio.cpp:368
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:902
MThread::wait
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:305
AudioSetupWizard::UpdateCapabilitiesAC3
AudioOutputSettings UpdateCapabilitiesAC3(void)
Definition: setupwizard_audio.cpp:309
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:152
AudioSetupWizard::toggleSpeakers
void toggleSpeakers(void)
Definition: setupwizard_audio.cpp:386
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:52
ShowOkPopup
MythConfirmationDialog * ShowOkPopup(const QString &message, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
Definition: mythdialogbox.cpp:557
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:156
AudioOutput::GetOutputList
static ADCVect * GetOutputList(void)
Definition: audiooutput.cpp:416
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:919