Skip to content

Commit

Permalink
feat: add audio server switch support
Browse files Browse the repository at this point in the history
add audio server switch support

Issue:linuxdeepin/developer-center#5819
Log: add audio server switch support
  • Loading branch information
chenchongbiao committed Nov 15, 2023
1 parent 16fdf3e commit 659b707
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 78 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ option(DISABLE_AUTHENTICATION "disable build authentication plugins" OFF)
option(DISABLE_UPDATE "disable build update plugins" OFF)
option(DISABLE_LANGUAGE "disable lanugage settings in control center" OFF)
option(USE_DEEPIN_ZONE "enable special timezone file on deepin" OFF)
option(DISABLE_SOUND_ADVANCED "disable sound advanced settings" OFF)
set(DEEPIN_TIME_ZONE_PATH "/usr/share/dde/zoneinfo/zone1970.tab" CACHE STRING "deepin timezone path")

# asan 自己有内存泄露,暂不使用
Expand Down Expand Up @@ -808,6 +809,10 @@ if (BUILD_PLUGIN)
${Sound_SRCS}
)

if (DISABLE_SOUND_ADVANCED)
target_compile_definitions(${Sound_Name} PUBLIC -DDCC_DISABLE_SOUND_ADVANCED)
endif()

set(Sound_Includes
src/plugin-sound/operation
src/plugin-sound/window
Expand Down
17 changes: 17 additions & 0 deletions src/plugin-sound/operation/sounddbusproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ void SoundDBusProxy::setPausePlayer(bool value)
m_audioInter->setProperty("PausePlayer", QVariant::fromValue(value));
}

QString SoundDBusProxy::audioServer()
{
return qvariant_cast<QString>(m_audioInter->property("CurrentAudioServer"));
}

void SoundDBusProxy::SetAudioServer(const QString &in0)
{
QList<QVariant> argumentList;
argumentList << QVariant::fromValue(in0);
m_audioInter->asyncCallWithArgumentList(QStringLiteral("SetCurrentAudioServer"), argumentList);
}

bool SoundDBusProxy::audioServerState()
{
return qvariant_cast<bool>(m_audioInter->property("AudioServerState"));
}

void SoundDBusProxy::SetPortEnabled(uint in0, const QString &in1, bool in2)
{
QList<QVariant> argumentList;
Expand Down
11 changes: 10 additions & 1 deletion src/plugin-sound/operation/sounddbusproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class SoundDBusProxy : public QObject
void SetPort(uint in0, const QString &in1, int in2);

void SetBluetoothAudioMode(const QString &in0);

// SoundEffect
void GetSoundEnabledMap();
void EnableSound(const QString &name, bool enabled, QObject *receiver, const char *member, const char *errorSlot);
Expand Down Expand Up @@ -76,6 +75,14 @@ class SoundDBusProxy : public QObject
bool pausePlayer();
void setPausePlayer(bool value);

Q_PROPERTY(QString CurrentAudioServer READ audioServer WRITE SetAudioServer NOTIFY CurrentAudioServerChanged)
QString audioServer();
void SetAudioServer(const QString &in0);

// 音频切换的状态
Q_PROPERTY(bool AudioServerState READ audioServerState NOTIFY AudioServerStateChanged)
bool audioServerState();

Q_PROPERTY(QString BluetoothAudioMode READ bluetoothAudioMode NOTIFY BluetoothAudioModeChanged)
QString bluetoothAudioMode();
Q_PROPERTY(QStringList BluetoothAudioModeOpts READ bluetoothAudioModeOpts NOTIFY BluetoothAudioModeOptsChanged)
Expand Down Expand Up @@ -141,6 +148,8 @@ class SoundDBusProxy : public QObject
void SinkInputsChanged(const QList<QDBusObjectPath> &value) const;
void SinksChanged(const QList<QDBusObjectPath> &value) const;
void SourcesChanged(const QList<QDBusObjectPath> &value) const;
void CurrentAudioServerChanged(const QString &value) const;
void AudioServerStateChanged(const bool state) const;

// SoundEffect SIGNALS
void EnabledChanged(bool value) const;
Expand Down
62 changes: 40 additions & 22 deletions src/plugin-sound/operation/soundmodel.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
//
//SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-License-Identifier: GPL-3.0-or-later
#include "soundmodel.h"

#include <DIconButton>
#include <DSysInfo>
#include <DToolButton>

#include <QDebug>
#include <QDBusInterface>
#include <QDBusReply>
#include <QStandardItemModel>
#include <QDebug>
#include <QHBoxLayout>
#include <DToolButton>
#include <QLoggingCategory>
#include <QStandardItemModel>

Q_LOGGING_CATEGORY(DdcSoundModel, "dcc-sound-model")

Expand Down Expand Up @@ -44,7 +44,7 @@ SoundLabel::SoundLabel(QWidget *parent)
, m_mute(false)
, m_btn(new DTK_WIDGET_NAMESPACE::DToolButton(this))
{
QHBoxLayout* layout = new QHBoxLayout(this);
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_btn);

Expand Down Expand Up @@ -120,7 +120,7 @@ SoundModel::SoundModel(QObject *parent)
{ tr("Error"), DDesktopServices::SSE_Error },
};

if(IsServerSystem) {
if (IsServerSystem) {
m_soundEffectMapBattery.removeOne({ tr("Wake up"), DDesktopServices::SSE_WakeUp });
m_soundEffectMapPower.removeOne({ tr("Wake up"), DDesktopServices::SSE_WakeUp });
}
Expand Down Expand Up @@ -166,7 +166,6 @@ void SoundModel::setPausePlayer(bool pausePlayer)
}
}


void SoundModel::setMicrophoneOn(bool microphoneOn)
{
if (microphoneOn != m_microphoneOn) {
Expand Down Expand Up @@ -215,8 +214,7 @@ void SoundModel::addPort(Port *port)

if (port->direction() == Port::Out) {
m_outputPorts.append(port);
}
else {
} else {
m_inputPorts.append(port);
}

Expand All @@ -234,8 +232,7 @@ void SoundModel::removePort(const QString &portId, const uint &cardId)

if (port->direction() == Port::Out) {
m_outputPorts.removeOne(port);
}
else {
} else {
m_inputPorts.removeOne(port);
}
port->deleteLater();
Expand All @@ -249,7 +246,7 @@ bool SoundModel::containsPort(const Port *port)

Port *SoundModel::findPort(const QString &portId, const uint &cardId) const
{
auto res = std::find_if(m_ports.cbegin(), m_ports.end(), [=] (const Port *data)->bool {
auto res = std::find_if(m_ports.cbegin(), m_ports.end(), [=](const Port *data) -> bool {
return ((data->id() == portId) && (data->cardId() == cardId));
});

Expand All @@ -275,8 +272,8 @@ void SoundModel::setSpeakerVolume(double speakerVolume)

void SoundModel::setMaxUIVolume(double value)
{
double val = qRound(value * 10) / 10.0;
if (!qFuzzyCompare(val, m_maxUIVolume)) {
double val = qRound(value * 10) / 10.0;
if (!qFuzzyCompare(val, m_maxUIVolume)) {
m_maxUIVolume = val;
Q_EMIT maxUIVolumeChanged(val);
}
Expand Down Expand Up @@ -329,7 +326,7 @@ SoundEffectList SoundModel::soundEffectMap() const

void SoundModel::setEffectData(DDesktopServices::SystemSoundEffect effect, const bool enable)
{
if(m_soundEffectData[effect] == enable)
if (m_soundEffectData[effect] == enable)
return;

m_soundEffectData[effect] = enable;
Expand All @@ -344,14 +341,16 @@ bool SoundModel::queryEffectData(DDesktopServices::SystemSoundEffect effect)

void SoundModel::setEnableSoundEffect(bool enableSoundEffect)
{
if (m_enableSoundEffect == enableSoundEffect) return;
if (m_enableSoundEffect == enableSoundEffect)
return;

m_enableSoundEffect = enableSoundEffect;

Q_EMIT enableSoundEffectChanged(enableSoundEffect);
}

void SoundModel::updateSoundEffectPath(DDesktopServices::SystemSoundEffect effect, const QString &path)
void SoundModel::updateSoundEffectPath(DDesktopServices::SystemSoundEffect effect,
const QString &path)
{
m_soundEffectPaths[effect] = path;
}
Expand All @@ -366,7 +365,8 @@ const QString SoundModel::getNameByEffectType(DDesktopServices::SystemSoundEffec
return SOUND_EFFECT_MAP.value(effect);
}

DDesktopServices::SystemSoundEffect SoundModel::getEffectTypeByGsettingName(const QString &name) {
DDesktopServices::SystemSoundEffect SoundModel::getEffectTypeByGsettingName(const QString &name)
{
return SOUND_EFFECT_MAP.key(name);
}

Expand All @@ -380,8 +380,10 @@ bool SoundModel::isLaptop() const
return m_isLaptop;
}

void SoundModel::setIsLaptop(bool isLaptop) {
if (isLaptop == m_isLaptop) return;
void SoundModel::setIsLaptop(bool isLaptop)
{
if (isLaptop == m_isLaptop)
return;

m_isLaptop = isLaptop;

Expand All @@ -395,7 +397,7 @@ bool SoundModel::isIncreaseVolume() const

void SoundModel::setIncreaseVolume(bool value)
{
if(m_increaseVolume != value) {
if (m_increaseVolume != value) {
m_increaseVolume = value;
Q_EMIT increaseVolumeChanged(value);
}
Expand Down Expand Up @@ -426,6 +428,22 @@ void SoundModel::setWaitSoundReceiptTime(const int receiptTime)
}
}

void SoundModel::setAudioServerChangedState(const bool state)
{
if (m_audioServerStatus != state) {
m_audioServerStatus = state;
Q_EMIT onSetAudioServerFinish(state);
}
}

void SoundModel::setAudioServer(const QString &audioServer)
{
if (m_audioServer != audioServer) {
m_audioServer = audioServer;
Q_EMIT curAudioServerChanged(audioServer);
}
}

void Port::setId(const QString &id)
{
if (id != m_id) {
Expand Down
15 changes: 15 additions & 0 deletions src/plugin-sound/operation/soundmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ class SoundModel : public QObject
inline int currentWaitSoundReceiptTime() { return m_waitSoundReceiptTime; }
void setWaitSoundReceiptTime(const int receiptTime);

// 设置音频框架
inline QString audioServer() const { return m_audioServer; }
void setAudioServer(const QString &serverName);

// 音频框架切换的状态
inline bool audioServerChangedState() const { return m_audioServerStatus; }
void setAudioServerChangedState(const bool state);

Q_SIGNALS:
void speakerOnChanged(bool speakerOn) const;
void microphoneOnChanged(bool microphoneOn) const;
Expand Down Expand Up @@ -217,6 +225,11 @@ class SoundModel : public QObject
//声音输出设备是否可见
void outputDevicesVisibleChanged(QString name, bool flag);

// 音频框架设置完成
void onSetAudioServerFinish(bool value);
// 当前音频框架切换的信号
void curAudioServerChanged(const QString &audioFrame);

#ifndef DCC_DISABLE_FEEDBACK
void microphoneFeedbackChanged(double microphoneFeedback) const;
#endif
Expand All @@ -228,6 +241,8 @@ class SoundModel : public QObject
void isLaptopChanged(bool isLaptop);

private:
QString m_audioServer; // 当前使用音频框架
bool m_audioServerStatus{true}; // 设置音频时的状态
bool m_speakerOn;
bool m_microphoneOn;
bool m_enableSoundEffect;
Expand Down
11 changes: 11 additions & 0 deletions src/plugin-sound/operation/soundworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ void SoundWorker::initConnect()

connect(m_pingTimer, &QTimer::timeout, [this] { if (m_soundDBusInter) m_soundDBusInter->Tick(); });
connect(m_soundDBusInter, &SoundDBusProxy::HasBatteryChanged, m_model, &SoundModel::setIsLaptop);

connect(m_soundDBusInter, &SoundDBusProxy::CurrentAudioServerChanged, m_model, &SoundModel::setAudioServer);
connect(m_soundDBusInter, &SoundDBusProxy::AudioServerStateChanged, m_model, &SoundModel::setAudioServerChangedState);
}

void SoundWorker::activate()
Expand All @@ -66,6 +69,8 @@ void SoundWorker::activate()
m_model->setCurrentBluetoothAudioMode(m_soundDBusInter->bluetoothAudioMode());
m_model->setEnableSoundEffect(m_soundDBusInter->enabled());
m_model->setWaitSoundReceiptTime(m_waitSoundPortReceipt);
m_model->setAudioServer(m_soundDBusInter->audioServer());
m_model->setAudioServerChangedState(m_soundDBusInter->audioServerState());

m_pingTimer->start();
m_soundDBusInter->blockSignals(false);
Expand All @@ -88,6 +93,12 @@ void SoundWorker::refreshSoundEffect()
m_soundDBusInter->GetSoundEnabledMap();
}

void SoundWorker::setAudioServer(const QString &value)
{
m_soundDBusInter->SetAudioServer(value);
m_model->setAudioServer(value);
}

void SoundWorker::switchSpeaker(bool on)
{
m_soundDBusInter->SetMuteSink(!on);
Expand Down
2 changes: 2 additions & 0 deletions src/plugin-sound/operation/soundworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public Q_SLOTS:
void setBluetoothMode(const QString &mode);
void refreshSoundEffect();

void setAudioServer(const QString &value);

private Q_SLOTS:
void defaultSinkChanged(const QDBusObjectPath &path);
void defaultSourceChanged(const QDBusObjectPath &path);
Expand Down
Loading

0 comments on commit 659b707

Please sign in to comment.