|
@@ -320,6 +320,7 @@ class XunfeiAsr:
|
|
STATUS_LAST_FRAME = 2 # 最后一帧的标识
|
|
STATUS_LAST_FRAME = 2 # 最后一帧的标识
|
|
|
|
|
|
def __init__(self, tid, logger, message_receiver=None):
|
|
def __init__(self, tid, logger, message_receiver=None):
|
|
|
|
+ self.connected = False
|
|
self.__id = tid
|
|
self.__id = tid
|
|
self.logger = logger
|
|
self.logger = logger
|
|
self.message_receiver = message_receiver
|
|
self.message_receiver = message_receiver
|
|
@@ -388,6 +389,7 @@ class XunfeiAsr:
|
|
self.logger.info("xunfei.Asr.call_id:%s, ws_url:%s", self.__id, ws_url)
|
|
self.logger.info("xunfei.Asr.call_id:%s, ws_url:%s", self.__id, ws_url)
|
|
self.ws = websocket.WebSocketApp(ws_url, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close)
|
|
self.ws = websocket.WebSocketApp(ws_url, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close)
|
|
self.ws.on_open = self.on_open
|
|
self.ws.on_open = self.on_open
|
|
|
|
+ self.connected = False
|
|
self.ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
|
|
self.ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
|
|
time_cost = (datetime.now() - time1)
|
|
time_cost = (datetime.now() - time1)
|
|
self.logger.info(f"xunfei.Asr.started. call_id:{self.__id}, timeCost:{time_cost}, count:{count}")
|
|
self.logger.info(f"xunfei.Asr.started. call_id:{self.__id}, timeCost:{time_cost}, count:{count}")
|
|
@@ -402,10 +404,8 @@ class XunfeiAsr:
|
|
self.__th.start()
|
|
self.__th.start()
|
|
|
|
|
|
def send_audio(self, audio_data=None, first=False):
|
|
def send_audio(self, audio_data=None, first=False):
|
|
- self.logger.info('xunfei.Asr.send_audio:ws_is_None: call_id: %s, chunk:%s, %s', self.__id, self.ws.sock, self.ws.sock.connected)
|
|
|
|
- if not (self.ws.sock and self.ws.sock.connected):
|
|
|
|
- self.logger.info('xunfei.Asr.send_audio:ws_is_None: call_id: %s, chunk:%s, %s', self.__id, len(audio_data),
|
|
|
|
- audio_data)
|
|
|
|
|
|
+ if not self.connected:
|
|
|
|
+ self.logger.info('xunfei.Asr.send_audio:ws_is_None: call_id: %s, chunk:%s, %s', self.__id, len(audio_data), audio_data)
|
|
return
|
|
return
|
|
|
|
|
|
status = XunfeiAsr.STATUS_FIRST_FRAME if first else XunfeiAsr.STATUS_CONTINUE_FRAME # 音频的状态信息,标识音频是第一帧,还是中间帧、最后一帧
|
|
status = XunfeiAsr.STATUS_FIRST_FRAME if first else XunfeiAsr.STATUS_CONTINUE_FRAME # 音频的状态信息,标识音频是第一帧,还是中间帧、最后一帧
|
|
@@ -424,6 +424,7 @@ class XunfeiAsr:
|
|
"audio": str(base64.b64encode(buf), 'utf-8'),
|
|
"audio": str(base64.b64encode(buf), 'utf-8'),
|
|
"encoding": "raw"}}
|
|
"encoding": "raw"}}
|
|
d = json.dumps(d)
|
|
d = json.dumps(d)
|
|
|
|
+
|
|
self.ws.send(d)
|
|
self.ws.send(d)
|
|
# 中间帧处理
|
|
# 中间帧处理
|
|
elif status == XunfeiAsr.STATUS_CONTINUE_FRAME:
|
|
elif status == XunfeiAsr.STATUS_CONTINUE_FRAME:
|
|
@@ -448,6 +449,7 @@ class XunfeiAsr:
|
|
|
|
|
|
# 收到websocket连接建立的处理
|
|
# 收到websocket连接建立的处理
|
|
def on_open(self, ws):
|
|
def on_open(self, ws):
|
|
|
|
+ self.connected = True
|
|
self.logger.info("xunfei.Asr.open: call_id: %s", self.__id)
|
|
self.logger.info("xunfei.Asr.open: call_id: %s", self.__id)
|
|
# self.__event.set()
|
|
# self.__event.set()
|
|
|
|
|
|
@@ -480,6 +482,7 @@ class XunfeiAsr:
|
|
|
|
|
|
# 收到websocket关闭的处理
|
|
# 收到websocket关闭的处理
|
|
def on_close(self, ws, a, b):
|
|
def on_close(self, ws, a, b):
|
|
|
|
+ self.connected = False
|
|
self.logger.error("xunfei.Asr.recv::close, call_id:%s", self.__id)
|
|
self.logger.error("xunfei.Asr.recv::close, call_id:%s", self.__id)
|
|
# if not self.__event.is_set():
|
|
# if not self.__event.is_set():
|
|
# self.__event.set()
|
|
# self.__event.set()
|