Skip to content

Commit 509d83f

Browse files
add-uosdeepin-bot[bot]
authored andcommitted
feat(audio): disable system audio in virtual machines
Add virtual machine detection using systemd-detect-virt to automatically disable system audio recording when running in a VM environment. System audio is now unavailable in virtual machines due to driver compatibility issues. Add helper function to uncheck all audio recording options (microphone and system audio) for consistent UI state management. bug: https://pms.uniontech.com/bug-view-360853.html log: disable system audio in virtual machines
1 parent 8c92dd8 commit 509d83f

5 files changed

Lines changed: 53 additions & 7 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
Makefile
22
moc_*.o
33
moc_*.cpp
4+
moc_*.h
5+
qrc_*.cpp
46
*.o
7+
*.so
58
build/
69
*.qm
710
*.pro.user*
11+
deepin-screen-recorder
12+
deepin-pin-screenshots
13+
.qmake.stash

src/utils.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co.,Ltd.
2-
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2+
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
33
//
44
// SPDX-License-Identifier: GPL-3.0-or-later
55

@@ -241,6 +241,35 @@ void Utils::showCurrentSys()
241241
//qDebug() << "udpateVersion: " << DSysInfo::udpateVersion();
242242
//qDebug() << "majorVersion: " << DSysInfo::majorVersion();
243243
//qDebug() << "majorVersion: " << DSysInfo::majorVersion();
244+
qInfo() << "isVirtualMachine: " << isRunningInVirtualMachine();
245+
}
246+
247+
//Detect whether running in a virtual machine
248+
bool Utils::isRunningInVirtualMachine(bool forceVirtualMachine)
249+
{
250+
QProcess process;
251+
process.start("/usr/bin/systemd-detect-virt");
252+
process.waitForFinished();
253+
QString output = process.readAllStandardOutput().trimmed();
254+
255+
// Print the output of systemd-detect-virt command
256+
qInfo() << "systemd-detect-virt output: " << output;
257+
258+
// none: not a virtual machine
259+
// kvm, vmware, virtualbox, xen, qemu, etc.: virtual machine
260+
// empty output: also considered as not a virtual machine
261+
if (forceVirtualMachine) {
262+
qInfo() << "Force treat as virtual machine";
263+
return true;
264+
}
265+
266+
if (output.isEmpty() || output.toLower() == "none") {
267+
qInfo() << "Running on physical machine, not a virtual machine";
268+
return false;
269+
} else {
270+
qInfo() << "Running in a virtual machine, type:" << output;
271+
return true;
272+
}
244273
}
245274
void Utils::enableXGrabButton()
246275
{

src/utils.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co.,Ltd.
2-
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2+
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
33
//
44
// SPDX-License-Identifier: GPL-3.0-or-later
55

@@ -143,7 +143,14 @@ class Utils : public QObject
143143
* @brief showCurrentSys 显示
144144
*/
145145
static void showCurrentSys();
146-
146+
147+
/**
148+
* @brief isRunningInVirtualMachine Detect whether running in a virtual machine
149+
* @param forceVirtualMachine If true, force treat as virtual machine; if false, auto-detect
150+
* @return true: running in a virtual machine; false: not running in a virtual machine
151+
*/
152+
static bool isRunningInVirtualMachine(bool forceVirtualMachine = false);
153+
147154
/**
148155
* @brief 使能XGrabButton抓取所有的鼠标点击事件
149156
*/

src/utils/voicevolumewatcher.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co.,Ltd.
2-
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2+
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
33
//
44
// SPDX-License-Identifier: GPL-3.0-or-later
55

66
#include "voicevolumewatcher.h"
77
#include "audioutils.h"
8+
#include "utils.h"
89

910
#include <QDebug>
1011
#include <QThread>

src/widgets/subtoolwidget.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co.,Ltd.
2-
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2+
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
33
//
44
// SPDX-License-Identifier: GPL-3.0-or-later
55

@@ -1456,8 +1456,11 @@ void SubToolWidget::setSystemAudioEnable(bool status)
14561456
m_haveSystemAudio = status;
14571457
m_systemAudioAction->setEnabled(status);
14581458
m_systemAudioAction->setCheckable(status);
1459-
m_systemAudioAction->setChecked(!status);
1460-
m_systemAudioAction->trigger();
1459+
m_systemAudioAction->setChecked(!status && !Utils::isRunningInVirtualMachine());
1460+
if (Utils::isRunningInVirtualMachine())
1461+
emit m_systemAudioAction->triggered(false);
1462+
else
1463+
m_systemAudioAction->trigger();
14611464
}
14621465
// 当m_microphoneAction或m_systemAudioAction被点击或者程序主动调用trigg()时,会触发工具栏音频采集图标的改变及发射实际需要录制的音频
14631466
void SubToolWidget::onChangeAudioType(bool checked)

0 commit comments

Comments
 (0)