DavidLiu 3 months ago
parent
commit
1b2e30917b
1 changed files with 22 additions and 15 deletions
  1. 22 15
      src/core/voip/bot.py

+ 22 - 15
src/core/voip/bot.py

@@ -51,7 +51,7 @@ class UserStatus(Enum):
 
 
 class MyAudioMediaPort(pj.AudioMediaPort):
-    def __init__(self, call, aud_med=None, asr=None):
+    def __init__(self, call, aud_media=None, asr=None):
         pj.AudioMediaPort.__init__(self)
         # # 打开一个 .pcm 文件来保存音频流(可选:保存为 .wav)
         # self.wav = wave.open(f"{recording_file}", "wb")
@@ -60,7 +60,7 @@ class MyAudioMediaPort(pj.AudioMediaPort):
         # self.wav.setframerate(16000)
         print("MyAudioMediaPort::come in, debugger")
         self.call = call
-        self.aud_med = aud_med
+        self.aud_media = aud_media
         self.asr = asr
 
         self.user_asr_texts = []
@@ -180,9 +180,9 @@ class MyCall(pj.Call):
         self.user_part = user_part
         self.call_id = call_id
         self.kwargs = kwargs
-        self.aud_med = None
+        self.aud_media = None
         self.audio_port = None
-        self.player = None
+        self.audio_player = None
         self.asr = None
         self.session_id = kwargs.get('variable_sip_h_P-LIBRA-CallId')
         self.device_id = kwargs.get('variable_sip_h_P-LIBRA-DeviceId')
@@ -280,7 +280,7 @@ class MyCall(pj.Call):
 
         if call_info.state == pj.PJSIP_INV_STATE_DISCONNECTED:
             self.logger.info("通话结束:%s", self.user_part)
-            self.call_end()
+            self.release()
 
 
     def onCallMediaState(self, prm):
@@ -290,7 +290,7 @@ class MyCall(pj.Call):
             if media.type == pj.PJMEDIA_TYPE_AUDIO and \
                (media.status == pj.PJSUA_CALL_MEDIA_ACTIVE):
                 self.logger.info("Call Media state 111: %s", call_info.stateText)
-                self.aud_med = self.getAudioMedia(media.index)
+                self.aud_media = self.getAudioMedia(media.index)
                 try:
                     # 建立双向通道
                     self.receive_user_speaker()
@@ -299,9 +299,9 @@ class MyCall(pj.Call):
                     traceback.print_exc()
 
     def receive_user_speaker(self):
-        self.audio_port = MyAudioMediaPort(self, self.aud_med, self.asr)
+        self.audio_port = MyAudioMediaPort(self, self.aud_media, self.asr)
         self.audio_port.createPort("Incoming Call Port", build_audio_format())
-        self.aud_med.startTransmit(self.audio_port)
+        self.aud_media.startTransmit(self.audio_port)
 
     def send_bot_speaker(self, player_file):
         if not player_file :
@@ -310,10 +310,10 @@ class MyCall(pj.Call):
         self.player_complete_dict[player_id] = False
         # print('self.player_complete_dict[player_id]D:', player_id, player_file, self.player_complete_dict[player_id])
         self.logger.info(f"[DEBUG] Sending bot speaker, player_file: {player_file}, player_id: {player_id}")
-        self.player = MyAudioMediaPlayer(player_id, self.aud_med, on_complete=self.on_media_player_complete)
-        # self.player.createPlayer(player_file[0], pj.PJMEDIA_FILE_NO_LOOP)
-        self.player.createPlaylist(player_file, f'my_hello_playlist{player_id}', pj.PJMEDIA_FILE_NO_LOOP)
-        self.player.startTransmit(self.aud_med)
+        self.audio_player = MyAudioMediaPlayer(player_id, self.aud_media, on_complete=self.on_media_player_complete)
+        # self.audio_player.createPlayer(player_file[0], pj.PJMEDIA_FILE_NO_LOOP)
+        self.audio_player.createPlaylist(player_file, f'my_hello_playlist{player_id}', pj.PJMEDIA_FILE_NO_LOOP)
+        self.audio_player.startTransmit(self.aud_media)
 
     def on_receiver_asr_result(self, message, *args):
         # 判断是否播放完成 否则不记录用户说的内容
@@ -367,11 +367,18 @@ class MyCall(pj.Call):
             registry.BOT_CALL_DURATION.labels(self.taskId).observe(latency)
             registry.BOT_INTERACTION_ROUNDS.labels(self.taskId).observe(self.inter_action_total)
 
-    def call_end(self):
+    def release(self):
+        # self.hangup()
         if self.audio_port:
+            # self.audio_port.stopTransmit()
             self.audio_port = None  # 或调用相关销毁方法
-        if self.player:
-            self.player = None  # 或调用播放器停止方法
+        if self.audio_player:
+            # self.audio_player.stopTransmit()
+            self.audio_player = None  # 或调用播放器停止方法
+        if self.aud_media:
+            # self.audio_media.stopTransmit()
+            self.aud_media = None
+
         self.asr.close()
         # 远程挂机之后要将分机号回收
         self.agent.release(self.user_part)