650:ai接口适配 2243/1 2243/head 2244/target/1
authorcheng <chengyf@zhihuishixun.com>
Mon, 15 Sep 2025 06:57:13 +0000 (14:57 +0800)
committercheng <chengyf@zhihuishixun.com>
Mon, 15 Sep 2025 06:57:13 +0000 (14:57 +0800)
code/sdk/application/wearable/fd230/nativeapp/nativeui/aivoice/include/AiVoiceModel.h
code/sdk/application/wearable/fd230/nativeapp/nativeui/aivoice/include/AiVoiceModelC.h
code/sdk/application/wearable/fd230/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp
code/sdk/application/wearable/fd230/nativeapp/nativeui/aivoice/src/AiVoicePresenter.cpp
code/sdk/application/wearable/fd230/nativeapp/nativeui/aivoice/src/AiVoiceView.cpp
code/sdk/zhihui/ai/media/ai_media_service_ext.cpp
code/sdk/zhihui/ai_old/zhihui_ai_server.cpp
code/sdk/zhihui/ai_old/zhihui_ws.cpp

index 529c518..c3c98a1 100644 (file)
@@ -150,8 +150,6 @@ namespace OHOS
         bool GetInit_4gai_task(void);
         void SetSelect4gorbt(int state);
         int GetSelect4gorbt(void);
-        
-        void cancel_tts_play(void);
 
         void ProcessTextByPunctuation();
         std::string GetAccumulatedText(void);
index fcaeee7..d90540f 100644 (file)
@@ -10,8 +10,6 @@ extern "C"
 #include <stdint.h>
 
     uint8_t getselect4gorbtai(void);
-    uint8_t get_clear_queue_stream(void);
-    void set_clear_queue_stream(uint8_t);
     void ble_AI_init(void);
 
 #ifdef __cplusplus
index 7f8bde1..1fa67f3 100644 (file)
@@ -34,6 +34,7 @@
 #include "custom_runtime.h"
 #include "voice_call_volte_sample_wrapper.h"
 #include <los_base.h>
+#include <websocket_server_manager.h>
 namespace OHOS
 {
     // 声明定时器变量
@@ -183,10 +184,9 @@ namespace OHOS
                 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetSelect4gorbt() == AI_SELECT_4G");
                 if (GetInit_4gai_task())
                 {
-#if AI_VOICE_ENABLE
-                    uapi_UI_start_asr(GetCurUsing4GAiModelType());
-#endif
-                                }
+                    // uapi_UI_start_asr(GetCurUsing4GAiModelType());
+                    uapi_start_asr_StartTranscription();
+                }
                 else
                 {
                     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "init_4gai_task fail!!");
@@ -411,7 +411,7 @@ namespace OHOS
         {
             return false;
         }
-        
+
         if (!AddUserQuestionToChatItems())
         {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddUserQuestionToChatItems fail!!");
@@ -853,7 +853,7 @@ namespace OHOS
             {
                 if (view_->GetAiVoiceScrollView(AIVOICE_RETURN_RESULT_TEXT_VIEWID) && view_->GetAiVoiceScrollView(AIVOICE_RETURN_RESULT_TEXT_VIEWID)->IsVisible())
                 {
-                    start_ai4g_tts_play_timer(); // 开启一个定时器,用于播放tts音频
+                    // start_ai4g_tts_play_timer(); // 开启一个定时器,用于播放tts音频
                     start_update_ai_answer_timer();
                 }
             }
@@ -942,10 +942,7 @@ namespace OHOS
             }
             else if (AiVoiceModel::GetInstance().GetSelect4gorbt() == AI_SELECT_4G)
             {
-#if AI_VOICE_ENABLE
-                LOS_Msleep(20);
-                startAudioCaptureTask();
-#endif
+                // uapi_start_asr_StartTranscription();录音同步移动到开启asr了,所有这里不调用
             }
 #else
             std::string audioPath = AiVoiceModel::GetInstance().GetAIAudioPath();
@@ -975,12 +972,9 @@ namespace OHOS
     void AiVoiceModel::StopListenAndRecordingUserQuestion(void)
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "停止录音");
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "停止录音:%d", GetSelect4gorbt());
         if (GetSelect4gorbt() == AI_SELECT_4G) // 选的是4G需要关闭asr
         {
-            // uapi_stop_asr();
-            // stop_AudioCaptureProcess_AI();
-            // uapi_UI_stop_asr();
+            uapi_stop_asr_StopTranscription();
         }
         else if (GetSelect4gorbt() == AI_SELECT_BT)
         {
@@ -1043,9 +1037,14 @@ namespace OHOS
             AiVoiceModel::GetInstance().AIVoice_StopTimer();
             AiVoiceModel::GetInstance().SetErrnote("连接超时,请重新开始对话");
             std::string str = AiVoiceModel::GetInstance().GetErrnote();
-#if AI_VOICE_ENABLE
-            dinit_ai_task(); // 所有服务都删除重新连一下服务器
-#endif
+
+            if (AiVoiceModel::GetInstance().GetSelect4gorbt() == AI_SELECT_4G)
+            {
+                // dinit_ai_task(); // 所有服务都删除重新连一下服务器
+                stop_all_websocket_servers();
+                destroy_all_websocket_servers();
+            }
+
             SendMsgToUIService(UI_AI_JUMP_TO_RESPONDERR_VIEW, 0, (void *)str.c_str(), str.length());
         }
         else
@@ -1247,10 +1246,8 @@ namespace OHOS
             AiVoicePresenter *p_AiVoicePresenter = AiVoicePresenter::GetInstance();
             if (AiVoicePresenter::GetInstance() && AivoiceListeningView::GetInstance())
             {
-#if AI_VOICE_ENABLE
-                uapi_UI_stop_asr();
-#endif
-                            AiVoicePresenter::GetInstance()->ChangeViewAndLogicOperator(AIVOICE_LISTENING_VIEWID, AIVOICE_THINKING_VIEWID); // 切换到思考中界面
+                // uapi_UI_stop_asr();
+                AiVoicePresenter::GetInstance()->ChangeViewAndLogicOperator(AIVOICE_LISTENING_VIEWID, AIVOICE_THINKING_VIEWID); // 切换到思考中界面
             }
         }
         else
@@ -1335,9 +1332,12 @@ namespace OHOS
                 if ((view_->GetAiVoiceScrollView(AIVOICE_AIMODE_CONNECTING) != nullptr) &&
                     (view_->GetAiVoiceScrollView(AIVOICE_AIMODE_CONNECTING)->IsVisible()))
                 {
-#if AI_VOICE_ENABLE
-                    dinit_ai_task(); // 所有服务都删除重新连一下服务器
-#endif
+                    if (AiVoiceModel::GetInstance().GetSelect4gorbt() == AI_SELECT_4G)
+                    {
+                        // dinit_ai_task(); // 所有服务都删除重新连一下服务器
+                        stop_all_websocket_servers();
+                        destroy_all_websocket_servers();
+                    }
                     p_AiVoicePresenter->ChangeViewAndLogicOperator(AIVOICE_AIMODE_CONNECTING, AIVOICE_AIMODEL_CONNECT_FAILED_VIEWID);
                 }
             }
@@ -1350,10 +1350,9 @@ namespace OHOS
                 if (AiVoiceModel::GetInstance().GetInit_4gai_task())
                 {
                     uint8_t state = 0;
-#if AI_VOICE_ENABLE
-                    state = get_asr_is_ready_start();
-#endif
-                                        if (state)
+                    // state = get_asr_is_ready_start();
+                    state = get_asr_is_ready_start_old();
+                    if (state)
                     {
                         bool state = true;
                         SendMsgToUIService(UI_AI_JUMP_TO_LISTENING_VIEW, 0, &state, sizeof(bool));
@@ -1440,9 +1439,9 @@ namespace OHOS
                 if ((view_->GetAiVoiceScrollView(AIVOICE_4GSERVICECON_VIEW) != nullptr) &&
                     (view_->GetAiVoiceScrollView(AIVOICE_4GSERVICECON_VIEW)->IsVisible()))
                 {
-#if AI_VOICE_ENABLE
-                    dinit_ai_task(); // 所有服务都删除重新连一下服务器
-#endif
+                    // dinit_ai_task(); // 所有服务都删除重新连一下服务器
+                    stop_all_websocket_servers();
+                    destroy_all_websocket_servers();
                     p_AiVoicePresenter->ChangeViewAndLogicOperator(AIVOICE_4GSERVICECON_VIEW, AIVOICE_AIMODEL_CONNECT_FAILED_VIEWID);
                 }
             }
@@ -1451,20 +1450,12 @@ namespace OHOS
         {
             if (view_ != nullptr && p_AiVoicePresenter != nullptr)
             {
-#if AI_VOICE_ENABLE
-                if (uapi_get_server_state_is_end())
-                {
-                    init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType());
-                    return;
-                }
-
                 if (AiVoiceModel::GetInstance().GetInit_4gai_task())
                 {
                     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "4gAi服务初始化成功");
                     AiVoiceModel::GetInstance().stop_ai4g_connect_timer();
                     p_AiVoicePresenter->ChangeViewAndLogicOperator(AIVOICE_4GSERVICECON_VIEW, AIVOICE_AIMODE_CONNECTING);
                 }
-#endif
             }
         }
         return;
@@ -1649,7 +1640,7 @@ namespace OHOS
         else
         {
             // if(AiVoiceModel::GetInstance().GetUserQuestionStr() !=)
-            // SendMsgToUIService(UI_UPDATE_USER_QUESTION_STR, 0, nullptr, 0); 
+            // SendMsgToUIService(UI_UPDATE_USER_QUESTION_STR, 0, nullptr, 0);
 
             SendMsgToUIService(UI_UPDATE_AI_ANSWER_STR, 0, nullptr, 0);
         }
@@ -1821,23 +1812,7 @@ namespace OHOS
     }
     bool AiVoiceModel::GetInit_4gai_task(void)
     {
-        uint8_t asr_state = 0;
-        uint8_t chat_state = 0;
-        uint8_t tts_state = 0;
-#if AI_VOICE_ENABLE
-        asr_state = get_ai_asr_service_init_flag();
-        chat_state = get_ai_chat_service_init_flag();
-        tts_state = get_ai_tts_server_init_flag();
-#endif
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "asr_state:%d,chat_state:%d,tts_state:%d", asr_state, chat_state, tts_state);
-        if ((asr_state == 1) && (chat_state == 1) && (tts_state == 1))
-        {
-            init_4gai_task = true;
-        }
-        else
-        {
-            init_4gai_task = false;
-        }
+        init_4gai_task = are_all_services_ready();
         return init_4gai_task;
     }
 
@@ -1849,13 +1824,6 @@ namespace OHOS
     {
         return select4gorbt;
     }
-    // 打断tts播放
-    void AiVoiceModel::cancel_tts_play(void)
-    {
-#if AI_VOICE_ENABLE
-        tts_server_deinit();
-#endif
-    }
 
     // 是否需要跳转到结果视图
     bool AiVoiceModel::is_need_to_jump_result_view(void)
@@ -1973,7 +1941,6 @@ namespace OHOS
 #endif
         return textId;
     }
-
 }
 
 #ifdef __cplusplus
@@ -1991,46 +1958,31 @@ extern "C"
         return 0;
     }
 
-    static uint8_t clear_queue_stream = 0;
-    uint8_t get_clear_queue_stream(void)
-    {
-        return clear_queue_stream;
-    }
+#ifdef BLE_INSTRUCT
 
-    // 清空队列中可能存在的音频数据
-    void set_clear_queue_stream(uint8_t state)
-    {
-        clear_queue_stream = state;
-    }
-
-
-    #ifdef BLE_INSTRUCT
-
-    #include "bleEvent.h"
-    #include "bleInstruct.h"
+#include "bleEvent.h"
+#include "bleInstruct.h"
 
     void ble_set_ai_Question_str_(char *buf)
     {
         OHOS::AiVoiceModel::GetInstance().SetUserQuestionStr(std::string(buf));
     }
 
-
-    void ble_set_ai_Answer_str_(char *buf,uint8_t type)
+    void ble_set_ai_Answer_str_(char *buf, uint8_t type)
     {
-        OHOS::AiVoiceModel::GetInstance().SetAiAnswerStrStr(std::string(buf),type);
+        OHOS::AiVoiceModel::GetInstance().SetAiAnswerStrStr(std::string(buf), type);
     }
 
     extern bleInstruct_t_ bleInstruct_t;
 
     void ble_AI_init(void)
     {
-        bleInstruct_t.ble_set_ai_Question_str= ble_set_ai_Question_str_;
+        bleInstruct_t.ble_set_ai_Question_str = ble_set_ai_Question_str_;
 
-        bleInstruct_t.ble_set_ai_Answer_str=ble_set_ai_Answer_str_;
+        bleInstruct_t.ble_set_ai_Answer_str = ble_set_ai_Answer_str_;
     }
 
-    #endif
-
+#endif
 
 #ifdef __cplusplus
 #if __cplusplus
index b64dcfb..289f519 100644 (file)
@@ -28,6 +28,7 @@
 #include "NativeRegisterManager.h"
 #include "aivoice/include/AiVoiceSelectView.h"
 #include "graphic_service.h"
+#include "websocket_server_manager.h"
 namespace OHOS
 {
 
@@ -122,10 +123,12 @@ namespace OHOS
         model->stop_update_ai_answer_timer();
         model->stop_uapi_stop_tts_timer();
         remove_4GAI_vote(); // 移除4g投票
-#if AI_VOICE_ENABLE
-        dinit_ai_task();
-        ai_cleanup_resources();
-#endif
+
+        stop_all_websocket_servers();
+        destroy_all_websocket_servers();
+        
+        // dinit_ai_task();
+        // ai_cleanup_resources();
 
         uapi_ble_connect_param_update(BLE_CONNECT_INTERVAL_SLOW);
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoicePresenter::OnStop end");
@@ -495,7 +498,11 @@ namespace OHOS
                 }
                 if (!model->GetInit_4gai_task())
                 {
+                    /* -------------------------------------------------------------------------- */
                     // init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType());
+                    init_all_websocket_servers();
+                    start_all_websocket_servers(); // 连接服务器
+                    /* -------------------------------------------------------------------------- */
                     model->start_ai4g_connect_timer();
                     ChangeViewAndLogicOperator(AIVOICE_STARTCHAT_VIEWID, AIVOICE_4GSERVICECON_VIEW); // LOADING
                 }
@@ -520,22 +527,7 @@ namespace OHOS
         }
         else if (viewId == FINISH_SPEAKING_BUTTON_VIEW_ID) // 如果点击的是说话完毕按钮
         {
-            if (model->GetSelect4gorbt() == AI_SELECT_4G)
-            {
-#if AI_VOICE_ENABLE
-                uapi_UI_stop_asr();
-#endif
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "FINISH_SPEAKING_BUTTON_VIEW_ID:(%d,%d)", model->is_need_to_jump_result_view(), model->getentryTextViewState());
-                if (!model->is_need_to_jump_result_view() && !model->getentryTextViewState()) // 没有数据就跳转到思考中界面
-                {
-                    GraphicService::GetInstance()->PostGraphicEvent(
-                        std::bind(&OHOS::AiVoicePresenter::JumpThinkingView, OHOS::AiVoicePresenter::GetInstance()));
-                }
-            }
-            else if (model->GetSelect4gorbt() == AI_SELECT_BT)
-            {
-                ChangeViewAndLogicOperator(AIVOICE_LISTENING_VIEWID, AIVOICE_THINKING_VIEWID);
-            }
+            ChangeViewAndLogicOperator(AIVOICE_LISTENING_VIEWID, AIVOICE_THINKING_VIEWID);
             return true;
         }
         else if (viewId == AIVOICE_SELECT_VIEW_BUTTON4G_VIEW_ID) // 如果点击的是4G连接方式
@@ -550,6 +542,8 @@ namespace OHOS
             if (!model->GetInit_4gai_task())
             {
                 // init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType());
+                init_all_websocket_servers();
+                start_all_websocket_servers(); // 连接服务器
                 model->start_ai4g_connect_timer();
                 ChangeViewAndLogicOperator(model->GetCurView(), AIVOICE_4GSERVICECON_VIEW); // LOADING
             }
@@ -582,10 +576,6 @@ namespace OHOS
                 {
                     AiVoiceModel::GetInstance().stop_ai4g_tts_play_timer();
                     AiVoiceModel::GetInstance().stop_uapi_stop_tts_timer();
-                    // usleep(50000);
-                    set_clear_queue_stream(1);
-                    LOS_Msleep(100);
-                    // uapi_UI_start_asr(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType());
                     ChangeViewAndLogicOperator(AIVOICE_RETURN_RESULT_TEXT_VIEWID, AIVOICE_AIMODE_CONNECTING);
                     return true;
                 }
@@ -601,11 +591,8 @@ namespace OHOS
                 {
                     AiVoiceModel::GetInstance().stop_ai4g_tts_play_timer();
                     AiVoiceModel::GetInstance().stop_uapi_stop_tts_timer();
-                    set_clear_queue_stream(1);
-                    LOS_Msleep(100);
-#if AI_VOICE_ENABLE
-                    dinit_ai_task();
-#endif
+                    stop_all_websocket_servers();
+                    destroy_all_websocket_servers();
                 }
             }
             ChangeViewAndLogicOperator(it->second.first, it->second.second);
@@ -626,10 +613,9 @@ namespace OHOS
             {
                 if (model->GetSelect4gorbt() == AI_SELECT_4G)
                 {
-#if AI_VOICE_ENABLE
-                    dinit_ai_task();
-#endif
-                    LOS_Msleep(50);
+                    // dinit_ai_task();
+                    stop_all_websocket_servers();
+                    destroy_all_websocket_servers();
                 }
             }
             ChangeViewAndLogicOperator(it->second.first, it->second.second);
@@ -673,7 +659,7 @@ namespace OHOS
         {
             if (scrollView == view_->GetAiVoiceScrollView(AIVOICE_STARTCHAT_VIEWID))
             {
-                //开始对话界面,右滑切换界面
+                // 开始对话界面,右滑切换界面
                 DragLeftToRightChangeAIModel();
             }
             else if (scrollView == view_->GetAiVoiceScrollView(AIVOICE_ALERT_APPBLUETOOTH_AND_4GNETWORK_VIEWID))
@@ -728,7 +714,7 @@ namespace OHOS
         }
         else if (model->GetAiModelTextId() == STR_AIMODES_CHATGPT && event.GetDragDirection() == DragEvent::DIRECTION_BOTTOM_TO_TOP) /*上滑*/
         {
-            //如果选择的模型是在chatgpt
+            // 如果选择的模型是在chatgpt
             if (model->IsExistOpenAIAPIKeys() == true)
             {
                 ChangeViewAndLogicOperator(AIVOICE_STARTCHAT_VIEWID, AIVOICE_SETAPIKEYS); /*开始对话界面->API Keys set界面*/
index 00ffa54..e715afb 100644 (file)
@@ -11,6 +11,7 @@
 #include "wearable_log.h"
 #include "ui_resource_string.h"
 #include "ConfigModel.h"
+#include "uiservice/include/ui_service.h"
 namespace OHOS
 {
 
@@ -531,8 +532,6 @@ namespace OHOS
         return;
     }
 
-
-
     /*切换到开始对话Ai界面*/
     bool AiVoiceView::ChangeAiVoiceStartChatView(int16_t thisView)
     {
@@ -721,6 +720,10 @@ namespace OHOS
             AiVoiceScrollView[thisView] = scrollview;
             AddViewToRootContainer(AiVoiceScrollView[thisView]);
         }
+        if (!AiVoiceModel::GetInstance().GetUserQuestionStr().empty())
+        {
+            SendMsgToUIService(UI_UPDATE_USER_QUESTION_STR, 0, nullptr, 0);
+        }
         return true;
     }
 
index 4150296..f1adfc5 100644 (file)
@@ -342,7 +342,7 @@ static int32_t CreateAIVoiceInputThread(AIVoiceContext *context)
     }
     
     // 保存线程句柄
-    context->taskHandle = (void*)task_cfg.task_handle;
+    // context->taskHandle = (void*)task_cfg.task_handle;
     
     MEDIA_INFO_LOG("AI voice input thread created successfully");
     return 0;
index 609d489..ddc8d9b 100644 (file)
@@ -102,7 +102,6 @@ extern void bluetooth_get_local_addr(unsigned char *addr, int len);
 extern int active_md5_buf(char *input, char *output);
 extern void uapi_cat1_keep_wakeup(uint8_t state);
 extern void set_AIStreamend(uint8_t state);
-extern void set_clear_queue_stream(uint8_t state);
 extern void startAudioCaptureTask(void);
 extern void stop_AudioCaptureProcess_AI(void);
 extern void ble_set_ai_Question_str(char *str);
index 0f69010..bdfee90 100644 (file)
@@ -1386,8 +1386,8 @@ int  receive_websocket_message(WebSocketClient *client, unsigned char *buf,int l
     
     // 超时检查函数
     auto check_timeout = [&]() -> bool {
-        uint32_t current_time = (uint32_t)osKernelGetTickCount();
-        return (current_time - start_time) >= timeout_ms;
+        // uint32_t current_time = (uint32_t)osKernelGetTickCount();
+        return 0;
     };
     // while(1)
     // {