mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
Merge pull request #2818 from xinnan-tech/fix_wakeup_audioRate
修复设备播放音频时打断后使用唤醒词时流控器未重置造成的死锁
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import time
|
||||
import json
|
||||
import uuid
|
||||
import random
|
||||
import asyncio
|
||||
from core.utils.dialogue import Message
|
||||
@@ -105,6 +106,9 @@ async def checkWakeupWords(conn, text):
|
||||
# 播放唤醒词回复
|
||||
conn.client_abort = False
|
||||
|
||||
# 将唤醒词回复视为新会话,生成新的 sentence_id,确保流控器重置
|
||||
conn.sentence_id = str(uuid.uuid4().hex)
|
||||
|
||||
conn.logger.bind(tag=TAG).info(f"播放唤醒词回复: {response.get('text')}")
|
||||
await sendAudioMessage(conn, SentenceType.FIRST, opus_packets, response.get("text"))
|
||||
await sendAudioMessage(conn, SentenceType.LAST, [], None)
|
||||
|
||||
@@ -133,12 +133,27 @@ def _get_or_create_rate_controller(conn, frame_duration, is_single_packet):
|
||||
Returns:
|
||||
(rate_controller, flow_control)
|
||||
"""
|
||||
# 判断是否需要重置:单包模式且 sentence_id 变化,或者控制器不存在
|
||||
need_reset = (
|
||||
is_single_packet
|
||||
and getattr(conn, "audio_flow_control", {}).get("sentence_id")
|
||||
!= conn.sentence_id
|
||||
) or not hasattr(conn, "audio_rate_controller")
|
||||
# 检查是否需要重置控制器
|
||||
need_reset = False
|
||||
|
||||
if not hasattr(conn, "audio_rate_controller"):
|
||||
# 控制器不存在,需要创建
|
||||
need_reset = True
|
||||
else:
|
||||
rate_controller = conn.audio_rate_controller
|
||||
|
||||
# 后台发送任务已停止, 则需要重置
|
||||
if (
|
||||
not rate_controller.pending_send_task
|
||||
or rate_controller.pending_send_task.done()
|
||||
):
|
||||
need_reset = True
|
||||
# 当sentence_id 变化,需要重置
|
||||
elif (
|
||||
getattr(conn, "audio_flow_control", {}).get("sentence_id")
|
||||
!= conn.sentence_id
|
||||
):
|
||||
need_reset = True
|
||||
|
||||
if need_reset:
|
||||
# 创建或获取 rate_controller
|
||||
|
||||
Reference in New Issue
Block a user