Просмотр исходного кода

PlayerComponent: fallback to auto-selection if audio device not present

If a user has selected a device that's not available, or unplugged it at
any time, try to fallback to device auto-selection. This is probably
better than staying silent.
Vincent Lang 8 лет назад
Родитель
Сommit
419948d967
2 измененных файлов с 30 добавлено и 21 удалено
  1. 29 20
      src/player/PlayerComponent.cpp
  2. 1 1
      src/player/PlayerComponent.h

+ 29 - 20
src/player/PlayerComponent.cpp

@@ -54,7 +54,7 @@ PlayerComponent::PlayerComponent(QObject* parent)
   connect(&DisplayComponent::Get(), &DisplayComponent::refreshRateChanged, this, &PlayerComponent::onRefreshRateChange);
 
   m_reloadAudioTimer.setSingleShot(true);
-  connect(&m_reloadAudioTimer, &QTimer::timeout, this, &PlayerComponent::onReloadAudio);
+  connect(&m_reloadAudioTimer, &QTimer::timeout, this, &PlayerComponent::updateAudioDevice);
 }
 
 /////////////////////////////////////////////////////////////////////////////////////////
@@ -823,12 +823,6 @@ void PlayerComponent::setSubtitleDelay(qint64 milliseconds)
   mpv::qt::set_property(m_mpv, "sub-delay", milliseconds / 1000.0);
 }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-void PlayerComponent::onReloadAudio()
-{
-  mpv::qt::command(m_mpv, QStringList() << "ao-reload");
-}
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // This is called with the set of previous audio devices that were detected, and the set of current
 // audio devices. From this we guess whether we should reopen the audio device. If the user-selected
@@ -845,9 +839,9 @@ void PlayerComponent::checkCurrentAudioDevice(const QSet<QString>& old_devs, con
   QLOG_DEBUG() << "Audio devices added:" << added;
   QLOG_DEBUG() << "Audio device selected:" << userDevice;
 
-  if (!mpv::qt::get_property(m_mpv, "idle-active").toBool() && userDevice.length())
+  if (userDevice.length())
   {
-    if (added.contains(userDevice))
+    if (added.contains(userDevice) || removed.contains(userDevice))
     {
       // The timer is for debouncing the reload. Several change notifications could
       // come in quick succession. Also, it's possible that trying to open the
@@ -871,16 +865,19 @@ void PlayerComponent::updateAudioDeviceList()
     Q_ASSERT(d.type() == QVariant::Map);
     QVariantMap dmap = d.toMap();
 
-    devices.insert(dmap["name"].toString());
+    QString device = dmap["name"].toString();
+    QString description = dmap["description"].toString();
+
+    devices.insert(device);
+
+    if (userDevice == device)
+      userDeviceFound = true;
 
     QVariantMap entry;
-    entry["value"] = dmap["name"];
-    entry["title"] = dmap["description"];
+    entry["value"] = device;
+    entry["title"] = description;
 
     settingList << entry;
-
-    if (userDevice == dmap["name"])
-      userDeviceFound = true;
   }
 
   if (!userDeviceFound)
@@ -899,17 +896,27 @@ void PlayerComponent::updateAudioDeviceList()
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-void PlayerComponent::setAudioConfiguration()
+void PlayerComponent::updateAudioDevice()
 {
-  QStringList aoDefaults;
+  QString device = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "device").toString();
 
+  if (!m_audioDevices.contains(device))
+  {
+    device = "auto";
+    QLOG_WARN() << "Not using audio device" << device << "because it's not present.";
+  }
+
+  mpv::qt::set_property(m_mpv, "audio-device", device);
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+void PlayerComponent::setAudioConfiguration()
+{
   QString deviceType = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "devicetype").toString();
 
   mpv::qt::set_property(m_mpv, "audio-exclusive", SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "exclusive").toBool());
 
-  // set the audio device
-  QVariant device = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "device");
-  mpv::qt::set_property(m_mpv, "audio-device", device);
+  updateAudioDevice();
 
   QString resampleOpts = "";
   bool normalize = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "normalize").toBool();
@@ -974,6 +981,8 @@ void PlayerComponent::setAudioConfiguration()
     mpv::qt::command(m_mpv, QStringList() << "af" << "del" << "@ac3");
   }
 
+  QVariant device = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "device");
+
   // Make a informational log message.
   QString audioConfig = QString(QString("Audio Config - device: %1, ") +
                                         "channel layout: %2, " +

+ 1 - 1
src/player/PlayerComponent.h

@@ -152,8 +152,8 @@ private Q_SLOTS:
   void handleMpvEvents();
   void onRestoreDisplay();
   void onRefreshRateChange();
-  void onReloadAudio();
   void onCodecsLoadingDone(CodecsFetcher* sender);
+  void updateAudioDevice();
 
 Q_SIGNALS:
   // The following signals correspond to the State enum above.