Skip to content

Commit a1ab682

Browse files
committed
fix: fix sound card port priority ordering issue
Add Priority field to PriorityPort struct to store port priority value from pulseaudio Implement insertByPriority method that inserts ports in correct order based on their priority values within the same sound card Fix the issue where newly added ports were always inserted at the head of the queue regardless of their actual priority This resolves BUG-340227 where sound card port changes caused incorrect priority ordering Log: Fixed sound card port priority ordering when ports are updated Influence: 1. Test Bluetooth device connection with both A2DP and HSP profiles 2. Verify that higher priority ports (like A2DP) are selected before lower priority ports 3. Test multiple ports from the same sound card with different priority values 4. Verify port insertion order when ports have equal priority 5. Test mixed scenarios with ports from different sound cards fix: 修复声卡端口优先级排序问题 在 PriorityPort 结构中添加 Priority 字段存储来自 pulseaudio 的端口优先 级值 实现 insertByPriority 方法,根据同一声卡内端口的优先级值进行正确排序 修复新添加端口无论实际优先级如何总是插入队列头部的问题 解决 BUG-340227 中声卡端口变化导致优先级排序不正确的问题 Log: 修复声卡端口更新时的优先级排序问题 PMS: BUG-340227 Influence: 1. 测试同时支持 A2DP 和 HSP 配置文件的蓝牙设备连接 2. 验证高优先级端口(如 A2DP)优先于低优先级端口被选择 3. 测试同一声卡多个端口具有不同优先级值的情况 4. 验证端口优先级相等时的插入顺序 5. 测试不同声卡端口混合场景
1 parent 383914f commit a1ab682

3 files changed

Lines changed: 389 additions & 3 deletions

File tree

audio1/priority_policy.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ type Position struct {
9191
type PriorityPort struct {
9292
CardName string
9393
PortName string
94-
PortType int // 部分声卡需要在Property里判断类型,只有CardName和PortName不足以用来判断PortType,因此添加此项
94+
PortType int // 部分声卡需要在Property里判断类型,只有CardName和PortName不足以用来判断PortType,因此添加此项
95+
Priority uint32 // 端口权重,用于排序
9596
}
9697

9798
// 端口实例优先级列表
@@ -187,8 +188,44 @@ func (pp *PriorityPolicy) InsertPort(card *pulse.Card, port *pulse.CardPortInfo)
187188
CardName: card.Name,
188189
PortName: port.Name,
189190
PortType: tp,
191+
Priority: port.Priority,
190192
}
191-
// 新端口添加到队列最前
193+
194+
pp.insertByPriority(newPort, tp)
195+
}
196+
197+
// 为了解决pms: BUG-340227, 声卡端口会变化的情况
198+
// insertByPriority 按 priority 插入端口,如果队列中已存在该声卡的端口,
199+
// 遍历队列找到最后一个权重大于新端口的同声卡端口,插入到其后面,否则插入到队头
200+
func (pp *PriorityPolicy) insertByPriority(newPort *PriorityPort, tp int) {
201+
insertAfterIndex := -1 // 记录最后一个权重大于新端口的同声卡端口位置
202+
203+
// 遍历队列
204+
for i, existingPort := range pp.Ports[tp] {
205+
// 如果是相同声卡
206+
if existingPort.CardName == newPort.CardName {
207+
// 如果该端口权重大于(不能等于)新端口,记录位置
208+
if existingPort.Priority > newPort.Priority {
209+
insertAfterIndex = i
210+
}
211+
}
212+
}
213+
214+
// 如果找到了权重大于新端口的同声卡端口,插入到该位置之后
215+
if insertAfterIndex != -1 {
216+
insertIndex := insertAfterIndex + 1
217+
// 如果是插入到末尾
218+
if insertIndex >= len(pp.Ports[tp]) {
219+
pp.Ports[tp] = append(pp.Ports[tp], newPort)
220+
} else {
221+
// 插入到中间位置
222+
pp.Ports[tp] = append(pp.Ports[tp][:insertIndex],
223+
append([]*PriorityPort{newPort}, pp.Ports[tp][insertIndex:]...)...)
224+
}
225+
return
226+
}
227+
228+
// 否则插入到队头
192229
pp.Ports[tp] = append([]*PriorityPort{newPort}, pp.Ports[tp]...)
193230
}
194231

@@ -237,6 +274,7 @@ func (pp *PriorityPolicy) SetTheFirstPort(cardName string, portName string, avai
237274
CardName: p.CardName,
238275
PortName: p.PortName,
239276
PortType: p.PortType,
277+
Priority: p.Priority,
240278
}
241279
targetType = tp
242280
// 从列表中删除这个端口

0 commit comments

Comments
 (0)