雷鸟:解决使用4gai的音频流播放不了问题
authorluozheng <1732036298@qq.com>
Tue, 23 Sep 2025 10:47:22 +0000 (18:47 +0800)
committerluozheng <1732036298@qq.com>
Tue, 23 Sep 2025 10:47:22 +0000 (18:47 +0800)
code/sdk/zhihui/ai/media/ai_media_service_ext.cpp
code/sdk/zhihui/leiniao/imsl_ln/ln_api.c

index 2d2a31b..8f2c445 100644 (file)
@@ -45,8 +45,8 @@ using namespace::Audio;
 const int32_t NUMBER_BASE = 10;
 const int32_t CHANNEL_COUNT = 2;
 const int32_t SAMPLERATE_16000 = 16000;
-const uint32_t AI_VOICE_BUFFER_SIZE = 0x800;  // 2KB buffer for AI voice
-const uint32_t AI_VOICE_QUEUE_SIZE = 10;      // 队列大小
+const uint32_t AI_VOICE_BUFFER_SIZE = 0x1000;  // 2KB buffer for AI voice
+const uint32_t AI_VOICE_QUEUE_SIZE = 20;      // 队列大小
 
 // 定义音频数据回调函数类型
 
@@ -840,6 +840,7 @@ static void ExampleAIVoiceDataCallback(const uint8_t* data, uint32_t size, void*
  * @param size 音频数据大小
  * @return 0 成功,-1 失败
  */
+#ifdef BOARD_LEINIAO
 int32_t AI_WriteAudioData(const uint8_t* data, uint32_t size)
 {
     if (data == nullptr || size == 0) {
@@ -871,14 +872,53 @@ int32_t AI_WriteAudioData(const uint8_t* data, uint32_t size)
     if (ret != 0) {
         // MEDIA_ERR_LOG("Failed to write audio data to output stream, ret=%d", ret);
         // return -1;
-        usleep(1000*500);
+        usleep(30*size);
         goto AI_WRITE;
     }
     
     // MEDIA_INFO_LOG("Successfully wrote %u bytes of audio data to output stream", size);
     return 0;
 }
+#else
+int32_t AI_WriteAudioData(const uint8_t* data, uint32_t size)
+{
+    if (data == nullptr || size == 0) {
+        MEDIA_ERR_LOG("Invalid audio data parameters");
+        return -1;
+    }
+    AI_WRITE:
+    if(g_aiVoiceContext->interruptFlag)
+    return -1;
+    // 获取全局互斥锁(自动初始化)
+    MediaMutexHandle mutex = GetGlobalMutex();
+    if (mutex == nullptr) {
+        MEDIA_ERR_LOG("Failed to get global mutex");
+        return -1;
+    }
+    
+    // 检查AI语音上下文是否存在
+    MediaMutexLock(mutex);
+    if (g_aiVoiceContext == nullptr) {
+        MEDIA_ERR_LOG("AI voice context is null, please start AI voice first");
+        MediaMutexUnLock(mutex);
+        return -1;
+    }
+    MediaMutexUnLock(g_aiVoiceContextMutex);
 
+    // 写入音频数据到输出流
+    int32_t ret = AudioStreamOutStreamWrite(data, size);
+    usleep(50*size);
+    if (ret != 0) {
+        // MEDIA_ERR_LOG("Failed to write audio data to output stream, ret=%d", ret);
+        // return -1;
+        usleep(1000*500);
+        goto AI_WRITE;
+    }
+    
+    // MEDIA_INFO_LOG("Successfully wrote %u bytes of audio data to output stream", size);
+    return 0;
+}
+#endif
 /**
  * @brief 检查AI语音服务是否正在运行
  * @return true 正在运行,false 未运行
index 38523c3..92d25ff 100644 (file)
@@ -745,7 +745,24 @@ static uint32_t ln_download_mp3_file_advanced(const char *audio_url, const char
                 }
                 total_written += written;
                 #endif
-                AI_SendAudioDataToQueue((const uint8_t*)client_data.responseBuf, client_data.contentBlockLen);
+                ret = AI_SendAudioDataToQueue((const uint8_t*)client_data.responseBuf, client_data.contentBlockLen);
+                if (ret != 0)
+                {
+                    printf("download mp3 file advanced: write failed1, ret=%d\n", ret);
+                    // 写失败重新写
+                    LOS_Msleep(100);
+                    ret = AI_SendAudioDataToQueue((const uint8_t*)client_data.responseBuf, (uint32_t)client_data.contentBlockLen);
+                    if (ret != 0)
+                    {
+                        printf("download mp3 file advanced: write failed2, ret=%d\n", ret);
+                        LOS_Msleep(100);
+                        ret = AI_SendAudioDataToQueue((const uint8_t*)client_data.responseBuf, (uint32_t)client_data.contentBlockLen);
+                        if (ret != 0)
+                        {
+                            printf("download mp3 file advanced: write failed3, ret=%d\n", ret);
+                        }
+                    }
+                }
             }
             else
             {
@@ -1428,12 +1445,12 @@ void ln_ai_record_start(void)
 
 void ln_ai_record_stop(void) 
 {
+    g_is_ai_record_start = 0;
     if (g_aiCtx_fp) 
     {
         fclose(g_aiCtx_fp);
         g_aiCtx_fp = NULL;
     }
-    g_is_ai_record_start = 0;
 }
 
 // 雷鸟http服务初始化