Skip to content

Commit fef0661

Browse files
committed
refactor: improve audio port insertion logic
1. Changed port insertion order from reverse to sequential to maintain natural priority order 2. Enhanced InsertPort method to accept all card ports for better clustering and ordering 3. Implemented weight-based ordering within card clusters using port order mapping 4. Simplified insertion logic by removing complex priority comparison algorithm 5. Updated test cases to reflect new clustering behavior and insertion strategy Log: Improved audio device port management for better user experience Influence: 1. Test audio device switching between different ports 2. Verify Bluetooth device port ordering (A2DP vs HSP) 3. Check multi-card scenarios with mixed port types 4. Test port priority handling when devices are connected/disconnected 5. Verify stability when refreshing audio ports 6. Test edge cases with equal priority ports refactor: 改进音频端口插入逻辑 1. 将端口插入顺序从倒序改为顺序以保持自然优先级顺序 2. 增强 InsertPort 方法以接受所有卡端口,实现更好的集群和排序 3. 使用端口顺序映射实现卡集群内基于权重的排序 4. 通过移除复杂的优先级比较算法简化插入逻辑 5. 更新测试用例以反映新的集群行为和插入策略 Log: 改进音频设备端口管理以提升用户体验 Influence: 1. 测试不同端口间的音频设备切换 2. 验证蓝牙设备端口排序(A2DP vs HSP) 3. 检查多声卡混合端口类型的场景 4. 测试设备连接/断开时的端口优先级处理 5. 验证刷新音频端口时的稳定性 6. 测试优先级相等端口的边缘情况 PMS: BUG-353339 Change-Id: I8db970fc8bc5e6d54055e4757f1cb60015b2ddcf
1 parent 31e53ee commit fef0661

3 files changed

Lines changed: 105 additions & 373 deletions

File tree

audio1/priority_manager.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ func (pm *PriorityManager) refreshPorts(cards CardList) {
239239
}
240240
}
241241

242-
// 插入该声卡的新端口(倒序插入以保持优先级顺序)
242+
// 插入该声卡的新端口
243243
// 排序后:[默认端口, 高优先级, 中优先级, 低优先级]
244-
// 倒序插入到队首:低默认端口
245-
// 最终队列:[默认端口, 高优先级, 中优先级, 低优先级] 在队首
246-
for i := len(newSinkPorts) - 1; i >= 0; i-- {
247-
pm.Output.InsertPort(card.core, &newSinkPorts[i])
244+
// 依次插入:默认端口
245+
// 插入逻辑会保证它们在队列中也是这个顺序,并且尽量靠近该声卡已有的端口
246+
for i := 0; i < len(newSinkPorts); i++ {
247+
pm.Output.InsertPort(card.core, &newSinkPorts[i], card.Ports)
248248
}
249-
for i := len(newSourcePorts) - 1; i >= 0; i-- {
250-
pm.Input.InsertPort(card.core, &newSourcePorts[i])
249+
for i := 0; i < len(newSourcePorts); i++ {
250+
pm.Input.InsertPort(card.core, &newSourcePorts[i], card.Ports)
251251
}
252252
}
253253

audio1/priority_policy.go

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -177,57 +177,51 @@ func (pp *PriorityPolicy) FindPort(cardName string, portName string) *Position {
177177
return &Position{tp: PortTypeInvalid, index: -1}
178178
}
179179

180-
func (pp *PriorityPolicy) InsertPort(card *pulse.Card, port *pulse.CardPortInfo) {
181-
pos := pp.FindPort(card.Name, port.Name)
182-
if pos.tp != PortTypeInvalid {
183-
// 已存在,无需插入
180+
func (pp *PriorityPolicy) InsertPort(card *pulse.Card, port *pulse.CardPortInfo, allCardPorts pulse.CardPortInfos) {
181+
if pos := pp.FindPort(card.Name, port.Name); pos.tp != PortTypeInvalid {
184182
return
185183
}
184+
186185
tp := DetectPortType(card, port)
187186
newPort := &PriorityPort{
188-
CardName: card.Name,
189-
PortName: port.Name,
190-
PortType: tp,
191-
Priority: port.Priority,
187+
CardName: card.Name, PortName: port.Name,
188+
PortType: tp, Priority: port.Priority,
192189
}
190+
queue := pp.Ports[tp]
193191

194-
// 新增设备插入到该类型队列的最前面
195-
pp.Ports[tp] = append([]*PriorityPort{newPort}, pp.Ports[tp]...)
196-
}
192+
// 1. 构建权重 Map,显式处理“未记录端口”的权重
193+
orderMap := make(map[string]int, len(allCardPorts))
194+
for i, p := range allCardPorts {
195+
orderMap[p.Name] = i
196+
}
197+
myWeight, ok := orderMap[port.Name]
198+
if !ok {
199+
// 防御性处理:如果端口名不在声卡列表中,默认排在该卡集群的末尾
200+
myWeight = len(allCardPorts)
201+
}
197202

198-
// 为了解决pms: BUG-340227, 声卡端口会变化的情况
199-
// insertByPriority 按 priority 插入端口,如果队列中已存在该声卡的端口,
200-
// 遍历队列找到最后一个权重大于新端口的同声卡端口,插入到其后面,否则插入到队头
201-
func (pp *PriorityPolicy) insertByPriority(newPort *PriorityPort, tp int) {
202-
insertAfterIndex := -1 // 记录最后一个权重大于新端口的同声卡端口位置
203-
204-
// 遍历队列
205-
for i, existingPort := range pp.Ports[tp] {
206-
// 如果是相同声卡
207-
if existingPort.CardName == newPort.CardName {
208-
// 如果该端口权重大于(不能等于)新端口,记录位置
209-
if existingPort.Priority > newPort.Priority {
210-
insertAfterIndex = i
203+
// 2. 寻找插入点
204+
insertIdx := 0 // 默认为新卡置顶逻辑
205+
foundSameCard := false
206+
for i, p := range queue {
207+
if p.CardName == card.Name {
208+
if !foundSameCard {
209+
foundSameCard = true
210+
insertIdx = i // 找到集群后,默认插在集群首位
211+
}
212+
// 在声卡集群内部,依据 orderMap 权重进行排序
213+
if otherWeight, ok := orderMap[p.PortName]; ok && myWeight > otherWeight {
214+
insertIdx = i + 1
211215
}
212216
}
213217
}
214218

215-
// 如果找到了权重大于新端口的同声卡端口,插入到该位置之后
216-
if insertAfterIndex != -1 {
217-
insertIndex := insertAfterIndex + 1
218-
// 如果是插入到末尾
219-
if insertIndex >= len(pp.Ports[tp]) {
220-
pp.Ports[tp] = append(pp.Ports[tp], newPort)
221-
} else {
222-
// 插入到中间位置
223-
pp.Ports[tp] = append(pp.Ports[tp][:insertIndex],
224-
append([]*PriorityPort{newPort}, pp.Ports[tp][insertIndex:]...)...)
225-
}
226-
return
219+
// 3. 执行切片插入
220+
if insertIdx >= len(queue) {
221+
pp.Ports[tp] = append(queue, newPort)
222+
} else {
223+
pp.Ports[tp] = append(queue[:insertIdx], append([]*PriorityPort{newPort}, queue[insertIdx:]...)...)
227224
}
228-
229-
// 否则插入到队头
230-
pp.Ports[tp] = append([]*PriorityPort{newPort}, pp.Ports[tp]...)
231225
}
232226

233227
func (pp *PriorityPolicy) GetTheFirstPort() (*PriorityPort, *Position) {

0 commit comments

Comments
 (0)