修复因没有按照帧结构发送数据导致服务端有坏数据问题
authorhcj <1224667075@qq.com>
Sat, 18 Apr 2026 08:39:59 +0000 (16:39 +0800)
committerhcj <1224667075@qq.com>
Sat, 18 Apr 2026 08:39:59 +0000 (16:39 +0800)
code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c

index 93b82f3..73ec355 100644 (file)
@@ -250,7 +250,7 @@ static uint32_t g_audio_timeout_ticks = 1500; // 音频超时tick数(默认1.5
 uint32_t g_last_binary_data_tick = 0;  // 最后一次接收二进制数据的tick
 
 // 音频数据最近缓存(环形缓冲区,仅保留最近数据)
-#define AUDIO_CACHE_BUFFER_SIZE  (1024*3)  // 8KB缓存
+#define AUDIO_CACHE_BUFFER_SIZE  (72*6*5)  // 8KB缓存
 static uint8_t g_audio_cache_buffer[AUDIO_CACHE_BUFFER_SIZE];
 static uint32_t g_audio_cache_pos = 0;  // 当前写入位置
 static uint32_t g_audio_cache_total = 0;  // 总写入数据量
@@ -291,7 +291,7 @@ static void ln_ws_reset_audio_cache(void);
 static uint32_t ln_ws_copy_from_ring_cache(uint32_t start_pos, uint32_t length, uint8_t* output);
 static int ln_ws_send_cached_audio_data(void);
 
-
+#define TIME_TICKS_TEST 0
 
 /* 握手成功回调:标记连接就绪 */
 static void ln_ws_on_open(int client)
@@ -347,7 +347,10 @@ static void ln_ws_message_callback(const char *raw_data, uint32_t timestamp)
         LN_WS_WARN("Invalid message\n");
     }
     LN_WS_DEBUG("on_message: %s\n", buf);
-
+#if TIME_TICKS_TEST
+    uint32_t current_tick = osKernelGetTickCount();
+    LN_WS_DEBUG("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx on_message current_tick: %u\n", current_tick);
+#endif
     free(buf);
 }
 
@@ -613,6 +616,12 @@ static void ln_ws_audio_write_callback(uint32_t size)
 {
     LN_WS_DEBUG("Audio data written successfully: %u bytes\n", size);
 
+#if TIME_TICKS_TEST
+    uint32_t current_tick = osKernelGetTickCount();
+    LN_WS_DEBUG("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx current_tick: %u\n", current_tick);
+#endif
+
+
     // 更新最后一次音频写入tick
     g_last_audio_write_tick = osKernelGetTickCount();
     g_audio_playback_active = true;
@@ -723,7 +732,14 @@ int ln_ws_send_audio_data(const uint8_t *audio_data, uint32_t audio_size, bool f
             LN_WS_ERROR("Failed to allocate memory for base64 encoding\n");
             return -1;
         }
-        
+        memset(audio_base64, 0, base64_size);
+        // LN_WS_DEBUG("audio_data before encode: \n");
+        // for(int i = 0; i < 32; i++)
+        // {
+        //     printf(" %02X",audio_data[i]);
+        // }
+        // LN_WS_DEBUG("audio_data end\n");
+
         size_t output_len = 0;
         int ret_encode = mbedtls_base64_encode((unsigned char*)audio_base64, base64_size - 1, &output_len, 
                                               audio_data, audio_size);
@@ -1153,7 +1169,11 @@ static void AIVoiceDataCallback_ln(const uint8_t* data, uint32_t size, void* use
 static void ai_binary_message_handler(const uint8_t *raw_data, uint32_t timestamp, uint32_t data_len)
 {
     LN_WS_INFO("Received binary message at %u, length: %d bytes\n", timestamp, data_len);
-    
+
+#if TIME_TICKS_TEST
+    uint32_t current_tick = osKernelGetTickCount();
+    LN_WS_DEBUG("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ai_binary_message_handler current_tick: %u\n", current_tick);
+#endif
     // 收到二进制消息时,清除 ping 超时检测
     if (g_ping_timeout_active) {
         g_ping_timeout_active = false;
@@ -4093,7 +4113,7 @@ static int ln_ws_send_cached_audio_data(void)
     LN_WS_INFO("Sending cached audio data: %u bytes (wrapped=%d, start_pos=%u)\n", 
                total_size, g_audio_cache_wrapped, start_pos);
     
-    #define MAX_SEND_CHUNK_SIZE 512  // 每次最多发送4KB
+    #define MAX_SEND_CHUNK_SIZE 72*6  // 每次最多发送4KB
     
     int ret = 0;
     uint32_t remaining = total_size;