From b8f521105e333a62ead53d2babb3ef4b0cd22e98 Mon Sep 17 00:00:00 2001
From: luoqing <luoqing@kylinsec.com.cn>
Date: Mon, 11 Jul 2022 14:25:28 +0800
Subject: [PATCH] feature(audio):fix unplugging headphones causes crash
 problems
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 修复拔出耳机导致崩溃问题
---
 plugins/audio/src/plugin/input-page.cpp   | 23 ++++++++++++++---------
 plugins/audio/src/plugin/output-page.cpp  |  7 +++----
 plugins/audio/src/plugin/volume-scale.cpp |  4 ----
 3 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/plugins/audio/src/plugin/input-page.cpp b/plugins/audio/src/plugin/input-page.cpp
index 3f4c5e3..66fa996 100644
--- a/plugins/audio/src/plugin/input-page.cpp
+++ b/plugins/audio/src/plugin/input-page.cpp
@@ -280,15 +280,15 @@ void InputPage::initConnet()
     connect(m_defaultSource, &AudioDeviceInterface::active_portChanged, [=](const QString &value) {
         handleActivePortChanged(value);
     });
+
+    //Fix:SourceAdded 和 SourceDelete没有被激发
     connect(m_audioInterface, &AudioInterface::SourceAdded, [this](uint index) {
         handleSourceAdded(index);
     });
     connect(m_audioInterface, &AudioInterface::SourceDelete, [this](uint index) {
         handleSourceDelete(index);
     });
-    connect(m_audioInterface, &AudioInterface::DefaultSourceChange, [this](uint index) {
-        handleDefaultSourceChanged(index);
-    });
+    connect(m_audioInterface, &AudioInterface::DefaultSourceChange, this,&InputPage::handleDefaultSourceChanged,Qt::QueuedConnection);
 }
 
 void InputPage::handleActivePortChanged(const QString &value)
@@ -315,29 +315,34 @@ void InputPage::handleVolumeChanged(double value)
     ui->volumeSetting->blockSignals(false);
 }
 
+/*
+ * TODO:
+ * 1、处理快速拔插设备
+ * 2、设备插入后是否需要等待设备准备好
+ * */
+
 void InputPage::handleDefaultSourceChanged(int index)
 {
     KLOG_DEBUG() << "DefaultSourceChanged:" << index;
-
     //delete and restart init defaultSource
-    delete m_defaultSource;
+    m_defaultSource->deleteLater();
     m_defaultSource = nullptr;
+
     ui->inputDevices->clear();
     initInputDevice();
     initInputSettins();
 
     ui->volumeScale->setPercent(0);
+
     if(m_audioInfo != nullptr)
     {
         m_audioInfo->stop();
-        delete m_audioInfo;
+        m_audioInfo->deleteLater();
         m_audioInfo = nullptr;
     }
     if(m_audioInput != nullptr)
     {
-        m_audioInput->stop();
-        m_audioInput->disconnect(this);
-        delete m_audioInput;
+        m_audioInput->deleteLater();
         m_audioInput = nullptr;
     }
     if(isValidPort)
diff --git a/plugins/audio/src/plugin/output-page.cpp b/plugins/audio/src/plugin/output-page.cpp
index 3040e29..768d613 100644
--- a/plugins/audio/src/plugin/output-page.cpp
+++ b/plugins/audio/src/plugin/output-page.cpp
@@ -160,9 +160,8 @@ void OutputPage::initConnect()
     connect(m_audioInterface, &AudioInterface::SinkDelete, [this](uint index) {
         handleSinkDelete(index);
     });
-    connect(m_audioInterface, &AudioInterface::DefaultSinkChange, [this](uint index) {
-        handleDefaultSinkChanged(index);
-    });
+    connect(m_audioInterface, &AudioInterface::DefaultSinkChange, this,&OutputPage::handleDefaultSinkChanged,Qt::QueuedConnection);
+
 
     connect(ui->outputDevices, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [=](int index) {
         QString namePort = ui->outputDevices->itemData(index, Qt::UserRole).toString();
@@ -211,7 +210,7 @@ void OutputPage::handleDefaultSinkChanged(int index)
 {
     KLOG_DEBUG() << "DefaultSinkChanged";
     //delete and restart init defaultSource
-    delete m_defaultSink;
+    m_defaultSink->deleteLater();
     m_defaultSink = nullptr;
     ui->outputDevices->clear();
 
diff --git a/plugins/audio/src/plugin/volume-scale.cpp b/plugins/audio/src/plugin/volume-scale.cpp
index e86311f..71c9f19 100644
--- a/plugins/audio/src/plugin/volume-scale.cpp
+++ b/plugins/audio/src/plugin/volume-scale.cpp
@@ -43,12 +43,9 @@ void VolumeScale::drawTicks(QPainter *painter)
     int posX = 0;
     int posY = 0;
     int length = geometry().width();
-    KLOG_DEBUG() << "geometry().width():" <<geometry().width();
 
     int ticksCount = length / TICK_SPACE;
 
-    KLOG_DEBUG() << "ticksCount:  " << ticksCount;
-
     int percentTicks = m_percent * ticksCount;
 
     for (int i = 0; i < ticksCount; i++) {
@@ -62,7 +59,6 @@ void VolumeScale::drawTicks(QPainter *painter)
         }
         painter->drawRect(posX,10,TICK_WIDTH,TICK_HEIGHT);
         posX += TICK_SPACE;
-        KLOG_DEBUG() << "drawRect:" << posX << ":" << i;
     }
 }
 void VolumeScale::setPercent(qreal percent)
-- 
2.33.0

