Skip to content

Commit 0233a35

Browse files
committed
fix: fix failure to change audio input device
In ReduceNoise mode, dde audio can not get the master device on echo-cancel-source. Log: pms: BUG-324445
1 parent 07e04d2 commit 0233a35

4 files changed

Lines changed: 97 additions & 27 deletions

File tree

audio1/audio.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ package audio
77
import (
88
"errors"
99
"fmt"
10-
"github.com/linuxdeepin/go-lib/strv"
1110
"math"
1211
"os"
12+
"regexp"
1313
"sort"
1414
"strings"
1515
"sync"
1616
"time"
1717

18+
"github.com/linuxdeepin/go-lib/strv"
19+
1820
dbus "github.com/godbus/dbus/v5"
1921
"github.com/linuxdeepin/dde-daemon/common/dsync"
2022
notifications "github.com/linuxdeepin/go-dbus-factory/session/org.freedesktop.notifications"
@@ -1653,6 +1655,10 @@ func (a *Audio) refreshBluetoothOpts() {
16531655
}
16541656

16551657
func (a *Audio) updateDefaultSink(sinkName string) {
1658+
if a.defaultSink.Name == sinkName {
1659+
logger.Warningf("defaultSink %s is the same as sinkName %s", a.defaultSink.Name, sinkName)
1660+
return
1661+
}
16561662
sinkInfo := a.getSinkInfoByName(sinkName)
16571663

16581664
if sinkInfo == nil {
@@ -1663,11 +1669,16 @@ func (a *Audio) updateDefaultSink(sinkName string) {
16631669
logger.Debugf("updateDefaultSink #%d %s", sinkInfo.Index, sinkName)
16641670
a.moveSinkInputsToSink(sinkInfo.Index)
16651671
if !isPhysicalDevice(sinkName) {
1666-
sinkInfo = a.getSinkInfoByName(sinkInfo.PropList["device.master_device"])
1667-
if sinkInfo == nil {
1672+
master := a.getMasterNameFromVirtualDevice(sinkName)
1673+
if master == "" {
16681674
logger.Warning("failed to get virtual device sinkInfo for name:", sinkName)
16691675
return
16701676
}
1677+
sinkInfo = a.getSinkInfoByName(master)
1678+
if sinkInfo == nil {
1679+
logger.Warning("failed to get virtual device sinkInfo for name:", master)
1680+
return
1681+
}
16711682
}
16721683
a.mu.Lock()
16731684
sink, ok := a.sinks[sinkInfo.Index]
@@ -1748,6 +1759,10 @@ func (a *Audio) updateSinks(index uint32) (sink *Sink) {
17481759
}
17491760

17501761
func (a *Audio) updateDefaultSource(sourceName string) {
1762+
if a.defaultSource.Name == sourceName {
1763+
logger.Warningf("defaultSource %s is the same as sourceName %s", a.defaultSource.Name, sourceName)
1764+
return
1765+
}
17511766
sourceInfo := a.getSourceInfoByName(sourceName)
17521767
if sourceInfo == nil {
17531768
logger.Warning("failed to get sourceInfo for name:", sourceName)
@@ -1757,7 +1772,12 @@ func (a *Audio) updateDefaultSource(sourceName string) {
17571772
logger.Debugf("updateDefaultSource #%d %s", sourceInfo.Index, sourceName)
17581773

17591774
if !isPhysicalDevice(sourceName) {
1760-
sourceInfo = a.getSourceInfoByName(sourceInfo.Proplist["device.master_device"])
1775+
master := a.getMasterNameFromVirtualDevice(sourceName)
1776+
if master == "" {
1777+
logger.Error("failed to get virtual device for name:", sourceName)
1778+
return
1779+
}
1780+
sourceInfo = a.getSourceInfoByName(master)
17611781
if sourceInfo == nil {
17621782
logger.Warning("failed to get virtual device sourceInfo for name:", sourceName)
17631783
return
@@ -1858,6 +1878,20 @@ func (a *Audio) getDefaultSinkName() string {
18581878
return v
18591879
}
18601880

1881+
func (a *Audio) getMasterNameFromVirtualDevice(device string) string {
1882+
modules := a.ctx.GetModuleList()
1883+
for _, module := range modules {
1884+
if strings.Contains(module.Name, "echo-cancel") {
1885+
re := regexp.MustCompile(`source_master=([^\s]+)`)
1886+
match := re.FindStringSubmatch(module.Argument)
1887+
if len(match) > 1 {
1888+
return match[1]
1889+
}
1890+
}
1891+
}
1892+
return ""
1893+
}
1894+
18611895
func (a *Audio) getSinkInfoByName(sinkName string) *pulse.Sink {
18621896
for _, sinkInfo := range a.ctx.GetSinkList() {
18631897
if sinkInfo.Name == sinkName {

audio1/audio_config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ func (a *Audio) setReduceNoise(enable bool) error {
9393
var err error
9494
var out []byte
9595
if enable {
96-
out, err = exec.Command("/bin/sh", "/usr/share/dde-daemon/audio/echoCancelEnable.sh").CombinedOutput()
96+
sourceName := a.getDefaultSourceName()
97+
logger.Debugf("echoCancelEnable.sh --source_master=%s", sourceName)
98+
out, err = exec.Command("/usr/share/dde-daemon/audio/echoCancelEnable.sh", "--source_master="+sourceName).CombinedOutput()
9799
if err != nil {
98100
logger.Warningf("failed to enable reduce noise %v %s", err, out)
99101
}

audio1/source.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,20 @@ func newSource(sourceInfo *pulse.Source, audio *Audio) *Source {
4545
service: audio.service,
4646
}
4747
if !isPhysicalDevice(sourceInfo.Name) {
48-
masterSourceInfo := audio.getSourceInfoByName(sourceInfo.Proplist["device.master_device"])
49-
if masterSourceInfo == nil {
50-
logger.Warningf("cannot get master source for %s", sourceInfo.Name)
48+
master := audio.getMasterNameFromVirtualDevice(sourceInfo.Name)
49+
if master != "" {
50+
51+
masterSourceInfo := audio.getSourceInfoByName(master)
52+
if masterSourceInfo == nil {
53+
logger.Warningf("cannot get master source for %s", sourceInfo.Name)
54+
} else {
55+
sourceInfo.Card = masterSourceInfo.Card
56+
sourceInfo.Ports = masterSourceInfo.Ports
57+
sourceInfo.ActivePort = masterSourceInfo.ActivePort
58+
logger.Debugf("create reducing noise source on %s", masterSourceInfo.Name)
59+
}
5160
} else {
52-
sourceInfo.Card = masterSourceInfo.Card
53-
sourceInfo.Ports = masterSourceInfo.Ports
54-
sourceInfo.ActivePort = masterSourceInfo.ActivePort
55-
logger.Debugf("create reducing noise source on %s", masterSourceInfo.Name)
61+
logger.Warningf("cannot get master source for %s", sourceInfo.Name)
5662
}
5763
}
5864

@@ -256,14 +262,19 @@ func (*Source) GetInterfaceName() string {
256262
func (s *Source) update(sourceInfo *pulse.Source) {
257263
// 如果是虚拟通道,则将card和ports等设为对应主通道的值,这是为了能够正常使用降噪的训通道
258264
if !isPhysicalDevice(sourceInfo.Name) {
259-
masterSourceInfo := s.audio.getSourceInfoByName(sourceInfo.Proplist["device.master_device"])
260-
if masterSourceInfo == nil {
261-
logger.Warningf("cannot get master source for %s", sourceInfo.Name)
265+
master := s.audio.getMasterNameFromVirtualDevice(sourceInfo.Name)
266+
if master != "" {
267+
masterSourceInfo := s.audio.getSourceInfoByName(master)
268+
if masterSourceInfo == nil {
269+
logger.Warningf("cannot get master source for %s", sourceInfo.Name)
270+
} else {
271+
sourceInfo.Card = masterSourceInfo.Card
272+
sourceInfo.Ports = masterSourceInfo.Ports
273+
sourceInfo.ActivePort = masterSourceInfo.ActivePort
274+
logger.Debugf("create reducing noise source on %s", masterSourceInfo.Name)
275+
}
262276
} else {
263-
sourceInfo.Card = masterSourceInfo.Card
264-
sourceInfo.Ports = masterSourceInfo.Ports
265-
sourceInfo.ActivePort = masterSourceInfo.ActivePort
266-
logger.Debugf("create reducing noise source on %s", masterSourceInfo.Name)
277+
logger.Warningf("cannot get master source for %s", sourceInfo.Name)
267278
}
268279
}
269280

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
#!/bin/bash
2-
aecArgs="$*"
3-
# If no "aec_args" are passed on to the script, use this "aec_args" as default:
4-
[ -z "$aecArgs" ] && aecArgs="analog_gain_control=0 digital_gain_control=1"
2+
3+
# 如果没有传递 aecArgs,使用默认值
54
newSourceName="echo-cancel-source"
5+
aecArgs="analog_gain_control=0 digital_gain_control=1"
6+
sourceMaster=""
7+
8+
# 解析参数
9+
while [[ $# -gt 0 ]]; do
10+
case $1 in
11+
--aec_args=*)
12+
aecArgs="${1#*=}"
13+
shift
14+
;;
15+
--source_master=*)
16+
sourceMaster="${1#*=}"
17+
shift
18+
;;
19+
*)
20+
shift
21+
;;
22+
esac
23+
done
24+
25+
moduleArgs="source_name=$newSourceName"
26+
if [ -n "$sourceMaster" ]; then
27+
moduleArgs="$moduleArgs source_master=$sourceMaster"
28+
fi
629

730
# Reload "module-echo-cancel"
8-
echo Reload \"module-echo-cancel\" with \"aec_args=$aecArgs\"
31+
echo Reload \"module-echo-cancel\" with \"aec_args=$aecArgs\" $moduleArgs
932
pactl unload-module module-echo-cancel 2>/dev/null
10-
if pactl load-module module-echo-cancel use_master_format=1 aec_method=webrtc aec_args=\"$aecArgs\" source_name=$newSourceName; then
11-
# Set a new default source and sink, if module-echo-cancel has loaded successfully.
12-
pactl set-default-source $newSourceName
13-
fi
33+
34+
if pactl load-module module-echo-cancel use_master_format=1 aec_method=webrtc aec_args=\"$aecArgs\" $moduleArgs; then
35+
pactl set-default-source $newSourceName
36+
fi

0 commit comments

Comments
 (0)