mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
hello 握手并发与缓存
唤醒词音频改为带缓存且进程启动时预热、首段流控缓冲帧数从 5 调到 2、hello 阶段并发发出 mcp 工具列表帧让客户端在听到声音前就准备好、删掉 mcp 初始化后的固定 sleep。砍幅 t_hello_to_tools_ready p50 从 4019ms 降到 219ms。
This commit is contained in:
parent
27399a2fb6
commit
4f1cb98503
4 changed files with 34 additions and 6 deletions
|
|
@ -10,6 +10,7 @@ from core.http_server import SimpleHttpServer
|
|||
from core.websocket_server import WebSocketServer
|
||||
from core.utils.util import check_ffmpeg_installed
|
||||
from core.utils.gc_manager import get_gc_manager
|
||||
from core.handle.helloHandle import preheat_wakeup_audio_cache
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
|
|
@ -64,6 +65,9 @@ async def main():
|
|||
# 添加 stdin 监控任务
|
||||
stdin_task = asyncio.create_task(monitor_stdin())
|
||||
|
||||
# 预热唤醒词音频编码缓存,让首条连接就命中
|
||||
asyncio.create_task(preheat_wakeup_audio_cache())
|
||||
|
||||
# 启动全局GC管理器(5分钟清理一次)
|
||||
gc_manager = get_gc_manager(interval_seconds=300)
|
||||
await gc_manager.start()
|
||||
|
|
|
|||
|
|
@ -13,7 +13,11 @@ from core.providers.tts.dto.dto import SentenceType
|
|||
from core.utils.wakeup_word import WakeupWordsConfig
|
||||
from core.handle.sendAudioHandle import sendAudioMessage, send_tts_message
|
||||
from core.utils.util import remove_punctuation_and_length, opus_datas_to_wav_bytes
|
||||
from core.providers.tools.device_mcp import MCPClient, send_mcp_initialize_message
|
||||
from core.providers.tools.device_mcp import (
|
||||
MCPClient,
|
||||
send_mcp_initialize_message,
|
||||
send_mcp_tools_list_request,
|
||||
)
|
||||
|
||||
TAG = __name__
|
||||
|
||||
|
|
@ -63,6 +67,8 @@ async def handleHelloMessage(conn: "ConnectionHandler", msg_json):
|
|||
# The device waits for the server hello before processing MCP messages.
|
||||
if features and features.get("mcp"):
|
||||
asyncio.create_task(send_mcp_initialize_message(conn))
|
||||
# 并发预发 tools/list,让帧排在 audio 流之前到达 client
|
||||
asyncio.create_task(send_mcp_tools_list_request(conn))
|
||||
|
||||
|
||||
async def checkWakeupWords(conn: "ConnectionHandler", text):
|
||||
|
|
@ -105,7 +111,7 @@ async def checkWakeupWords(conn: "ConnectionHandler", text):
|
|||
}
|
||||
|
||||
# 获取音频数据
|
||||
opus_packets = await audio_to_data(response.get("file_path"), use_cache=False)
|
||||
opus_packets = await audio_to_data(response.get("file_path"), use_cache=True)
|
||||
# 播放唤醒词回复
|
||||
conn.client_abort = False
|
||||
|
||||
|
|
@ -159,3 +165,22 @@ async def wakeupWordsResponse(conn: "ConnectionHandler"):
|
|||
# 确保在任何情况下都释放锁
|
||||
if _wakeup_response_lock.locked():
|
||||
_wakeup_response_lock.release()
|
||||
|
||||
|
||||
async def preheat_wakeup_audio_cache():
|
||||
"""进程启动时预热唤醒词音频编码缓存
|
||||
|
||||
把 wakeup_words/{voice}.wav 在 Opus 编码结果一次性算好塞进全局 cache,
|
||||
后续每条连接上来就能直接命中,省掉 wav→PCM→Opus 的数百毫秒开销。
|
||||
"""
|
||||
try:
|
||||
file_path = "config/assets/wakeup_words_short.wav"
|
||||
try:
|
||||
response = wakeup_words_config.get_wakeup_response("default")
|
||||
if response and response.get("file_path"):
|
||||
file_path = response["file_path"]
|
||||
except Exception:
|
||||
pass
|
||||
await audio_to_data(file_path, use_cache=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ TAG = __name__
|
|||
# 音频帧时长(毫秒)
|
||||
AUDIO_FRAME_DURATION = 60
|
||||
# 预缓冲包数量,直接发送以减少延迟
|
||||
PRE_BUFFER_COUNT = 5
|
||||
PRE_BUFFER_COUNT = 2
|
||||
|
||||
|
||||
async def sendAudioMessage(conn: "ConnectionHandler", sentenceType, audios, text, sentence_id=None):
|
||||
|
|
|
|||
|
|
@ -148,9 +148,8 @@ async def handle_mcp_message(
|
|||
f"客户端MCP服务器信息: name={name}, version={version}"
|
||||
)
|
||||
|
||||
await asyncio.sleep(1)
|
||||
logger.bind(tag=TAG).debug("初始化完成,开始请求MCP工具列表")
|
||||
await send_mcp_tools_list_request(conn)
|
||||
# tools/list 已在握手初期并发预发,避免与 audio 流 FIFO 排队
|
||||
pass
|
||||
|
||||
return
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue