update:优化代码

This commit is contained in:
hrz
2025-09-10 21:40:25 +08:00
parent c93cb3bb52
commit 5bd78cac1a
4 changed files with 133 additions and 132 deletions
@@ -30,6 +30,28 @@ async def sendAudioMessage(conn, sentenceType, audios, text):
await conn.close()
async def _send_to_mqtt_gateway(conn, opus_packet, timestamp, sequence):
"""
发送带16字节头部的opus数据包给mqtt_gateway
Args:
conn: 连接对象
opus_packet: opus数据包
timestamp: 时间戳
sequence: 序列号
"""
# 为opus数据包添加16字节头部
header = bytearray(16)
header[0] = 1 # type
header[2:4] = len(opus_packet).to_bytes(2, "big") # payload length
header[4:8] = sequence.to_bytes(4, "big") # sequence
header[8:12] = timestamp.to_bytes(4, "big") # 时间戳
header[12:16] = len(opus_packet).to_bytes(4, "big") # opus长度
# 发送包含头部的完整数据包
complete_packet = bytes(header) + opus_packet
await conn.websocket.send(complete_packet)
# 播放音频
async def sendAudio(conn, audios, frame_duration=60):
"""
@@ -42,9 +64,6 @@ async def sendAudio(conn, audios, frame_duration=60):
"""
if audios is None or len(audios) == 0:
return
# 检查是否需要处理头部(只有当websocket URL以"?from=mqtt"为结尾时才处理头部)
request_path = conn.websocket.request.path
need_header = request_path.endswith("?from=mqtt")
if isinstance(audios, bytes):
if conn.client_abort:
@@ -71,19 +90,19 @@ async def sendAudio(conn, audios, frame_duration=60):
if delay > 0:
await asyncio.sleep(delay)
if need_header:
# 为opus数据包添加16字节头部
timestamp = int((flow_control["start_time"] + flow_control["packet_count"] * frame_duration / 1000) * 1000) % (2**32)
header = bytearray(16)
header[0] = 1 # type
header[2:4] = len(audios).to_bytes(2, 'big') # payload length
header[4:8] = flow_control["sequence"].to_bytes(4, 'big') # connection id/sequence
header[8:12] = timestamp.to_bytes(4, 'big') # 时间戳
header[12:16] = len(audios).to_bytes(4, 'big') # opus长度
# 发送包含头部的完整数据包
complete_packet = bytes(header) + audios
await conn.websocket.send(complete_packet)
if conn.conn_from_mqtt_gateway:
# 计算时间戳
timestamp = int(
(
flow_control["start_time"]
+ flow_control["packet_count"] * frame_duration / 1000
)
* 1000
) % (2**32)
# 调用通用函数发送带头部的数据包
await _send_to_mqtt_gateway(
conn, audios, timestamp, flow_control["sequence"]
)
else:
# 直接发送opus数据包,不添加头部
await conn.websocket.send(audios)
@@ -97,30 +116,21 @@ async def sendAudio(conn, audios, frame_duration=60):
start_time = time.perf_counter()
play_position = 0
# 检查是否需要添加头部(只有当websocket URL以"?from=mqtt"为结尾时才添加头部)
request_path = conn.websocket.request.path
need_header = request_path.endswith("?from=mqtt")
# 执行预缓冲
pre_buffer_frames = min(3, len(audios))
for i in range(pre_buffer_frames):
if need_header:
# 为预缓冲包添加头部
timestamp = int((start_time + i * frame_duration / 1000) * 1000) % (2**32)
header = bytearray(16)
header[0] = 1 # type
header[2:4] = len(audios[i]).to_bytes(2, 'big') # payload length
header[4:8] = i.to_bytes(4, 'big') # sequence
header[8:12] = timestamp.to_bytes(4, 'big') # 时间戳
header[12:16] = len(audios[i]).to_bytes(4, 'big') # opus长度
complete_packet = bytes(header) + audios[i]
await conn.websocket.send(complete_packet)
if conn.conn_from_mqtt_gateway:
# 计算时间戳
timestamp = int((start_time + i * frame_duration / 1000) * 1000) % (
2**32
)
# 调用通用函数发送带头部的数据包
await _send_to_mqtt_gateway(conn, audios[i], timestamp, i)
else:
# 直接发送预缓冲包,不添加头部
await conn.websocket.send(audios[i])
remaining_audios = audios[pre_buffer_frames:]
# 播放剩余音频帧
for i, opus_packet in enumerate(remaining_audios):
if conn.client_abort:
@@ -135,25 +145,19 @@ async def sendAudio(conn, audios, frame_duration=60):
delay = expected_time - current_time
if delay > 0:
await asyncio.sleep(delay)
if need_header:
# 为opus数据包添加16字节头部 (timestamp at offset 8, length at offset 12)
timestamp = int((start_time + play_position / 1000) * 1000) % (2**32) # 使用播放位置计算时间戳
if conn.conn_from_mqtt_gateway:
# 计算时间戳和序列号
timestamp = int((start_time + play_position / 1000) * 1000) % (
2**32
) # 使用播放位置计算时间戳
sequence = pre_buffer_frames + i # 确保序列号连续
header = bytearray(16)
header[0] = 1 # type
header[2:4] = len(opus_packet).to_bytes(2, 'big') # payload length
header[4:8] = sequence.to_bytes(4, 'big') # sequence
header[8:12] = timestamp.to_bytes(4, 'big') # 时间戳在第8-11字节
header[12:16] = len(opus_packet).to_bytes(4, 'big') # opus长度在第12-15字节
# 发送包含头部的完整数据包
complete_packet = bytes(header) + opus_packet
await conn.websocket.send(complete_packet)
# 调用通用函数发送带头部的数据包
await _send_to_mqtt_gateway(conn, opus_packet, timestamp, sequence)
else:
# 直接发送opus数据包,不添加头部
await conn.websocket.send(opus_packet)
play_position += frame_duration