解决4GAi长文本播放卡顿问题,还需多多压测 2042/3 2042/head 2043/target/1
authorluozheng <1732036298@qq.com>
Mon, 4 Aug 2025 10:02:23 +0000 (18:02 +0800)
committerluozheng <1732036298@qq.com>
Mon, 4 Aug 2025 10:02:23 +0000 (18:02 +0800)
code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp
code/sdk/application/wearable/service/ble_service/bleservice/bleService.c
code/sdk/application/wearable/service/ble_service/bleservice/bleService.h

index afec342..3f811e4 100644 (file)
@@ -1635,9 +1635,8 @@ namespace OHOS
         }
         if (get_bk_websocket_tts_tick() == 0) // 代表可以发录音文本
         {
-            if (AiVoiceModel::GetInstance().GetInit_4gai_task())
+            if (AiVoiceModel::GetInstance().GetInit_4gai_task() && !get_ai_osal_msg_queue_is_full())
             {
-                LOS_Msleep(200);
                 AiVoiceModel::GetInstance().ProcessTextByPunctuation();
             }
         }
index cae2b19..2ac1d6a 100644 (file)
@@ -2571,7 +2571,32 @@ void AI_data_assembly(uint8_t type,uint8_t *buf,uint8_t aitype)
 \r
 static uint8_t g_blesrv_mail_flag=0;\r
 \r
+#define OSAL_INVALID_MSG_NUM 0xFFFFFFFF\r
+#define QUEUE_FULL_THRESHOLD 0.8f  // 80% 阈值\r
+// 判断AI消息队列是否接近满(超过阈值)\r
+int get_ai_osal_msg_queue_is_full(void)\r
+{\r
+    // 检查队列有效性\r
+    if (g_ble_Ai_record_queue == 0) {\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "invalid queue id");\r
+        return -1;  // 队列无效\r
+    }\r
+\r
+    // 获取当前消息数\r
+    unsigned int current_num = osal_msg_queue_get_msg_num(g_ble_Ai_record_queue);\r
+    if (current_num ==  OSAL_INVALID_MSG_NUM) {\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get msg num failed");\r
+        return -1;  // 获取失败\r
+    }\r
 \r
+    // 计算占比,超过阈值则视为“即将满”\r
+    float ratio = (float)current_num / BLESRV_APP_BOARD_MSG_COUNT;\r
+    if (ratio >= QUEUE_FULL_THRESHOLD) {\r
+        return 1;  // 达到或超过阈值(接近满)\r
+    } else {\r
+        return 0;  // 未达阈值(未满)\r
+    }\r
+}\r
 \r
 int send_msg_data_into_Ai_msg_queue(void *buff,uint16_t len,uint8_t type)\r
 {\r
@@ -2603,10 +2628,22 @@ int send_msg_data_into_Ai_msg_queue(void *buff,uint16_t len,uint8_t type)
         }\r
         memcpy_s((void *)(blesrv_mail.msg_data),len,buff,len);\r
 \r
+        const static int QUEUE_RETRY_DELAY_MS =  50;      // 重试间隔(ms)\r
+        // 检查队列是否已满(提前判断,减少失败概率)\r
+        if (osal_msg_queue_is_full(g_ble_Ai_record_queue)) {\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "queue is full");\r
+            osal_msleep(QUEUE_RETRY_DELAY_MS);  // 等待队列消费\r
+        }\r
         ret  = osal_msg_queue_write_copy(g_ble_Ai_record_queue, (void *)&blesrv_mail, sendlen , 0);\r
         if (ret != OSAL_SUCCESS)\r
         {\r
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"len<=singlebuflen Error blesrv Check send failed! err=0x%X %d! \r\n", ret,send_msg_data_into_Ai_msg_queue_count);\r
+            osal_msleep(QUEUE_RETRY_DELAY_MS);  // 等待队列消费\r
+            ret  = osal_msg_queue_write_copy(g_ble_Ai_record_queue, (void *)&blesrv_mail, sendlen , 0);\r
+            if (ret != OSAL_SUCCESS)\r
+            {\r
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"retry len<=singlebuflen Error blesrv Check send failed! err=0x%X %d! \r\n", ret,send_msg_data_into_Ai_msg_queue_count);    \r
+            }\r
         }\r
         return OSAL_SUCCESS;\r
         \r
index bf8666e..ed17e73 100644 (file)
@@ -113,6 +113,8 @@ void AI_data_assembly(uint8_t type,uint8_t *buf,uint8_t aitype);
 
 void set_AI_interval(uint8_t data);
 
+int get_ai_osal_msg_queue_is_full(void);
+
 #ifdef __cplusplus
 }
 #endif