1- // SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
1+ // SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
22//
33// SPDX-License-Identifier: GPL-3.0-or-later
44
@@ -115,57 +115,6 @@ func (a *Audio) isCardIdValid(cardId uint32) bool {
115115 return false
116116}
117117
118- func (a * Audio ) needAutoSwitchInputPort () bool {
119- // 不支持自动切换端口
120- if ! a .canAutoSwitchPort () {
121- return false
122- }
123-
124- firstPort , _ := GetPriorityManager ().GetTheFirstPort (pulse .DirectionSource )
125-
126- // 没有可用端口
127- if firstPort == nil || firstPort .PortType == PortTypeInvalid {
128- logger .Debug ("no input port" )
129- return false
130- }
131-
132- // 检查当前profile是否是配置文件中设置的profile
133- card , err := a .cards .getByName (firstPort .CardName )
134- if err != nil {
135- logger .Warning (err )
136- return false
137- }
138- cp := card .core .ActiveProfile
139- port , err := card .Ports .Get (firstPort .PortName , pulse .DirectionSource )
140- if err != nil {
141- logger .Warning (err )
142- return false
143- }
144-
145- // 输入端口不应该主动切换配置文件,会导致输入端口不可用或者发生变化
146- // 如果当前端口和当前配置文件匹配,不需要切换端口
147- if port .Profiles .Exists (cp .Name ) {
148- return false
149- }
150-
151- // 当前端口就是优先级最高的端口
152- var currentCardName , currentPortName string
153- if a .defaultSource != nil {
154- currentCardName = a .getCardNameById (a .defaultSource .Card )
155- currentPortName = a .defaultSource .ActivePort .Name
156- }
157-
158- if currentCardName == firstPort .CardName && currentPortName == firstPort .PortName {
159- logger .Debugf ("current input<%s,%s> is already the first port" ,
160- currentCardName , currentPortName )
161- return false
162- }
163-
164- logger .Debugf ("will auto switch from input<%s,%s> to input<%s,%s>" ,
165- currentCardName , currentPortName , firstPort .CardName , firstPort .PortName )
166- return true
167- }
168-
169118func (a * Audio ) checkAutoSwitchOutputPort () (auto bool , cardId uint32 , portName string ) {
170119 // 不支持自动切换端口
171120 if ! a .canAutoSwitchPort () {
@@ -181,13 +130,28 @@ func (a *Audio) checkAutoSwitchOutputPort() (auto bool, cardId uint32, portName
181130 currentPortName = a .defaultSink .ActivePort .Name
182131 }
183132 }
184- prefer , pos := GetPriorityManager ().GetTheFirstPort (pulse .DirectionSink )
185- if pos .tp != PortTypeInvalid {
186- logger .Debug ("loop prefer port:" , * prefer )
133+ var prefer * PriorityPort
134+ var pos * Position
135+ for {
136+ prefer , pos = GetPriorityManager ().LoopAvaiablePort (pulse .DirectionSink , pos )
137+ if prefer == nil || pos == nil || pos .tp == PortTypeInvalid {
138+ break
139+ }
140+ logger .Debugf ("loop prefer output port: %+v" , prefer )
187141 card , err := a .cards .getByName (prefer .CardName )
188142 if err != nil {
189143 logger .Warning (err )
190- return
144+ continue
145+ }
146+ // 配置同步可能有滞后性,需要查询声卡和端口是否存在
147+ var pc * pulse.Card
148+ if pc , err = a .ctx .GetCard (card .Id ); err != nil {
149+ logger .Warning (err )
150+ continue
151+ }
152+ if _ , err = pc .Ports .Get (prefer .PortName , pulse .DirectionSink ); err != nil {
153+ logger .Warning (err )
154+ continue
191155 }
192156 mode := GetConfigKeeper ().GetMode (card , prefer .PortName )
193157 if currentCardName != prefer .CardName ||
@@ -196,11 +160,11 @@ func (a *Audio) checkAutoSwitchOutputPort() (auto bool, cardId uint32, portName
196160 logger .Debugf ("will auto switch from output<%s,%s> to output<%s,%s>" ,
197161 currentCardName , currentPortName , prefer .CardName , prefer .PortName )
198162 return true , card .Id , prefer .PortName
163+ } else {
164+ return false , 0 , ""
199165 }
200- } else {
201- return true , 0 , ""
202166 }
203- return
167+ return true , 0 , ""
204168}
205169
206170func (a * Audio ) autoSwitchOutputPort () bool {
@@ -209,10 +173,10 @@ func (a *Audio) autoSwitchOutputPort() bool {
209173 if cardId == 0 || portName == "" {
210174 if ! strings .Contains (a .ctx .GetDefaultSink (), "null-sink" ) {
211175 a .LoadNullSinkModule ()
212- logger .Info ("no prefer port, set default sink to" , nullSinkName )
176+ logger .Info ("no prefer output port, set default sink to" , nullSinkName )
213177 a .ctx .SetDefaultSink (nullSinkName )
214178 } else {
215- logger .Info ("no prefer port, default sink is null-sink already" )
179+ logger .Info ("no prefer output port, default sink is null-sink already" )
216180 }
217181 return true
218182 } else {
@@ -239,20 +203,32 @@ func (a *Audio) checkAutoSwitchInputPort() (auto bool, cardId uint32, portName s
239203 currentCardName = a .getCardNameById (a .defaultSource .Card )
240204 currentPortName = a .defaultSource .ActivePort .Name
241205 }
242- prefer , pos := GetPriorityManager ().GetTheFirstPort (pulse .DirectionSource )
243- for pos .tp != PortTypeInvalid {
244- logger .Debug ("loop prefer port:" , * prefer )
206+
207+ var prefer * PriorityPort
208+ var pos * Position
209+ for {
210+ prefer , pos = GetPriorityManager ().LoopAvaiablePort (pulse .DirectionSource , pos )
211+ if prefer == nil || pos == nil || pos .tp == PortTypeInvalid {
212+ break
213+ }
214+ logger .Debugf ("loop prefer input port: %+v" , prefer )
245215 card , err := a .cards .getByName (prefer .CardName )
246216 if err != nil {
247217 logger .Warning (err )
248- return
218+ continue
249219 }
250- port , err := card .Ports .Get (prefer .PortName , pulse .DirectionSource )
220+ // 配置同步可能有滞后性,需要查询声卡和端口是否存在
221+ var pc * pulse.Card
222+ if pc , err = a .ctx .GetCard (card .Id ); err != nil {
223+ logger .Warning (err )
224+ continue
225+ }
226+ port , err := pc .Ports .Get (prefer .PortName , pulse .DirectionSource )
251227 if err != nil {
252228 logger .Warning (err )
253- return
229+ continue
254230 }
255- if port .Profiles .Exists (card .ActiveProfile .Name ) {
231+ if card . ActiveProfile != nil && port .Profiles .Exists (card .ActiveProfile .Name ) {
256232 if currentCardName != prefer .CardName ||
257233 currentPortName != prefer .PortName {
258234 logger .Debugf ("will auto switch from input<%s,%s> to input<%s,%s>" ,
@@ -262,7 +238,6 @@ func (a *Audio) checkAutoSwitchInputPort() (auto bool, cardId uint32, portName s
262238 return false , 0 , ""
263239 }
264240 }
265- prefer , pos = GetPriorityManager ().GetNextPort (pulse .DirectionSource , pos )
266241 }
267242 return true , 0 , ""
268243}
@@ -273,10 +248,10 @@ func (a *Audio) autoSwitchInputPort() bool {
273248 if cardId == 0 || portName == "" {
274249 if ! strings .Contains (a .ctx .GetDefaultSource (), "null-sink" ) {
275250 a .LoadNullSinkModule ()
276- logger .Info ("no prefer port, set default source to" , nullSinkName )
251+ logger .Info ("no prefer input port, set default source to" , nullSinkName )
277252 a .ctx .SetDefaultSource (nullSinkName + ".monitor" )
278253 } else {
279- logger .Info ("no prefer port, default source is null-sink already" )
254+ logger .Info ("no prefer input port, default source is null-sink already" )
280255 }
281256 return true
282257 } else {
@@ -291,51 +266,6 @@ func (a *Audio) autoSwitchInputPort() bool {
291266 return true
292267}
293268
294- func (a * Audio ) needAutoSwitchOutputPort () bool {
295- // 不支持自动切换端口
296- if ! a .canAutoSwitchPort () {
297- return false
298- }
299- logger .Debug ("check need auto switch output" )
300- firstPort , _ := GetPriorityManager ().GetTheFirstPort (pulse .DirectionSink )
301-
302- // 没有可用端口
303- if firstPort == nil || firstPort .PortType == PortTypeInvalid {
304- logger .Debug ("no output port" )
305- return false
306- }
307-
308- // 检查当前profile是否是配置文件中设置的profile
309- card , err := a .cards .getByName (firstPort .CardName )
310- if err != nil {
311- logger .Warning (err )
312- return false
313- }
314- cp := card .core .ActiveProfile
315- profile := GetConfigKeeper ().GetMode (card , firstPort .PortName )
316- if profile != "" && cp .Name != profile {
317- logger .Warningf ("output profile not match, current: %s, prefer: %s" , cp .Name , profile )
318- return true
319- }
320-
321- var currentCardName , currentPortName string
322- if a .defaultSink != nil {
323- currentCardName = a .getCardNameById (a .defaultSink .Card )
324- currentPortName = a .defaultSink .ActivePort .Name
325- }
326-
327- // 当前端口就是优先级最高的端口
328- if currentCardName == firstPort .CardName && currentPortName == firstPort .PortName {
329- logger .Debugf ("current output<%s,%s> is already the first" ,
330- currentCardName , currentPortName )
331- return false
332- }
333-
334- logger .Debugf ("will auto switch from output<%s,%s> to output<%s,%s>" ,
335- currentCardName , currentPortName , firstPort .CardName , firstPort .PortName )
336- return true
337- }
338-
339269// 自动切换端口,至少要保证声卡的profile是配置文件中设置的profile
340270// 如果不是,可能还在切换中,等待一下
341271func (a * Audio ) autoSwitchPort () {
0 commit comments