优化ai_server: 2087/1 2087/head
author孙春阳 <snickersun@163.com>
Sat, 16 Aug 2025 00:24:23 +0000 (08:24 +0800)
committer孙春阳 <snickersun@163.com>
Sat, 16 Aug 2025 00:24:23 +0000 (08:24 +0800)
1、asr服务保持运行ai对话期间不中断
2、修复queue semaphonre ,tcp manager 清除不净
3、增大录音发送缓存,优化声音识别

code/sdk/application/wearable/service/ble_service/bleservice/bleService.c
code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp

index 1b2097d..12d13d4 100644 (file)
@@ -2617,7 +2617,7 @@ int send_msg_data_into_Ai_msg_queue(void *buff,uint16_t len,uint8_t type)
     // uint32_t singlebuflen= ble_nus_max_data_len-20;
     uint32_t singlebuflen = 0;
     if(getselect4gorbtai() == 1) {
-        singlebuflen = 288*2;//4G
+        singlebuflen = 288*4;//4G
     } else {
         singlebuflen = ble_nus_max_data_len-20;//BT
     }
index 2d81265..a7d3224 100644 (file)
@@ -250,6 +250,7 @@ static bool wait_for_tcp_connection(WebSocketClient* client, const char* client_
 extern bool read_dev_token(char *data_token);
 
 errcode_t ai_system_init(void);
+void ai_system_deinit(void);
 void ai_cleanup_resources(void);
 uint32_t ai_get_memory_usage(void);
 uint32_t ai_get_memory_usage_percent(void);
@@ -386,8 +387,12 @@ static int ws_queue_init(ws_message_queue_t* queue)
     /* 创建信号量 */
     queue->semaphore = osSemaphoreNew(WS_QUEUE_SIZE, 0, NULL);
     if (!queue->semaphore) {
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create semaphore for ws queue");
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create semaphore for ws queue (size=%d)", WS_QUEUE_SIZE);
+        
+    
+        
         osMutexDelete(queue->mutex);
+        queue->mutex = NULL;
         return -1;
     }
     
@@ -530,6 +535,14 @@ static int ws_queues_init_all(void)
 {
     int ret = 0;
     
+    // 检查是否已经初始化,避免重复初始化
+    if (chat_msg_queue.initialized || asr_msg_queue.initialized || tts_msg_queue.initialized) {
+        WEARABLE_LOGW(WEARABLE_LOG_MODULE_APP, "WebSocket queues already initialized, deinitializing first");
+        ws_queue_deinit(&chat_msg_queue);
+        ws_queue_deinit(&asr_msg_queue);
+        ws_queue_deinit(&tts_msg_queue);
+    }
+    
     ret |= ws_queue_init(&chat_msg_queue);
     ret |= ws_queue_init(&asr_msg_queue);
     ret |= ws_queue_init(&tts_msg_queue);
@@ -614,6 +627,7 @@ void uapi_UI_start_asr(uint8_t type)
     asr_need_stop=0;
 
     asr_server_init();
+    uapi_start_asr();
 
     if(chat_client==NULL)
     {
@@ -931,13 +945,14 @@ void bk_websocket_demo(void *data) {
     uint32_t start_time = (uint32_t)osKernelGetTickCount();
     uint32_t retry_count = 0;
     
-    // 初始化互斥锁
+    // 检查互斥锁是否已存在,避免重复创建
     if (!g_ai_mutex) {
         g_ai_mutex = osMutexNew(NULL);
         if (!g_ai_mutex) {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create AI mutex");
             return;
         }
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI mutex created in bk_websocket_demo");
     }
     
     AI_LOCK();
@@ -1434,7 +1449,7 @@ bool aer_result_analysis(char *buf)
      if(strcmp(event_str,"TranscriptionCompleted")==0) //asr 结束
     {
          cJSON_Delete(root);
-               return false;
+               return true; // 返回true asr不断连
     }
     if(strcmp(event_str,"Failed")==0)
     {
@@ -1486,7 +1501,7 @@ void bk_websocket_asr(void *data) {
     
     WebSocketMessageType type;
     bool state = true;
-    
+ASR_SATRT:
     AI_LOCK();
     is_bk_websocket_asr = 1;
     AI_UNLOCK();
@@ -1556,7 +1571,7 @@ void bk_websocket_asr(void *data) {
                     break;
                 }
                 
-msleep(1000);
+                msleep(1000);
                 continue;
             } else {
                                 WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "ASR WebSocket connection established");
@@ -1639,7 +1654,7 @@ WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "等待ASR WebSocket握手完成... (%d m
         // timeout.tv_usec = (AI_RECEIVE_TIMEOUT_MS % 1000) * 1000;
         // lwip_setsockopt(asr_client->sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
 
-        uapi_start_asr();
+        // uapi_start_asr();
 
         uint32_t last_activity = (uint32_t)osKernelGetTickCount();
         
@@ -1699,7 +1714,10 @@ WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "等待ASR WebSocket握手完成... (%d m
             
             msleep(10); // 避免过度占用CPU
         }
-
+        if (disconnect_server == 0) {
+            WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "Reconnecting ASR client...");
+            goto ASR_SATRT;
+        }
         AI_LOCK();
         set_ai_asr_service_init_flag(0);
         AI_UNLOCK();
@@ -2435,7 +2453,12 @@ bool init_ai_task(uint8_t type)
             ai_cleanup_resources();
             return false;
         }
-
+        // 初始化ASR服务器
+        if (asr_server_init() != ERRCODE_SUCC) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize asr server");
+            ai_cleanup_resources();
+            return false;
+        }
         WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI task initialized successfully, type: %d", type);
     }
 
@@ -2469,8 +2492,9 @@ bool dinit_ai_task(void)
         tts_server_deinit();
         
         // 清理AI系统资源
-        ai_cleanup_resources();
+        ai_cleanup_resources(); 
         
+       
         WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI task deinitialized successfully");
     }
 
@@ -2996,7 +3020,7 @@ void ai_cleanup_resources(void) {
     // }
     
     // 清理WebSocket消息队列
-    // ws_queues_deinit_all();
+    ws_queues_deinit_all();
     
     // 清理互斥锁
     if (g_ai_mutex) {
@@ -3016,7 +3040,7 @@ void ai_cleanup_resources(void) {
     AI_UNLOCK();
     
     // 最后清理TCP管理器
-    // tcp_simple_manager_deinit();
+    tcp_simple_manager_deinit();
     
     WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI resources cleaned up");
 }
@@ -3030,12 +3054,17 @@ errcode_t ai_system_init(void) {
         return ERRCODE_FAIL;
     }
     
-    // 创建互斥锁
-    g_ai_mutex = osMutexNew(NULL);
+    // 创建互斥锁(检查是否已存在)
     if (!g_ai_mutex) {
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create AI mutex");
-        tcp_simple_manager_deinit();  // 清理TCP管理器
-        return ERRCODE_FAIL;
+        g_ai_mutex = osMutexNew(NULL);
+        if (!g_ai_mutex) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create AI mutex");
+            // tcp_simple_manager_deinit();  // 清理TCP管理器
+            return ERRCODE_FAIL;
+        }
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI mutex created in ai_system_init");
+    } else {
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI mutex already exists, reusing");
     }
     
     // 初始化内存使用统计
@@ -3044,7 +3073,10 @@ errcode_t ai_system_init(void) {
     // 初始化WebSocket消息队列
     if (ws_queues_init_all() != 0) {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize all WebSocket queues");
-        osMutexDelete(g_ai_mutex);
+        if (g_ai_mutex) {
+            osMutexDelete(g_ai_mutex);
+            g_ai_mutex = NULL;
+        }
         tcp_simple_manager_deinit();  // 清理TCP管理器
         return ERRCODE_FAIL;
     }