#include <string.h>
#include <stdio.h>
#include <stdarg.h>
+#include <sys/stat.h>
+#include <unistd.h>
#include "cJSON.h" // Added for JSON parsing
#include "custom_utils.h"
#include "soc_osal.h"
#endif
// 功能开关宏定义
#define ENABLE_BINARY_DATA_INTERRUPT_CHECK 1 // 启用二进制数据间隔检查
+#define ENABLE_AUDIO_FILE_WRITE 0 // 启用音频文件写入功能(根据chat_id生成文件)
// 主线程循环间隔时长(毫秒)
#define LN_WS_MAIN_THREAD_INTERVAL_MS 500
static int ln_ws_audio_buffer_add(const uint8_t *audio_data, uint32_t audio_size, bool finished);
static void ln_ws_audio_buffer_flush(void);
static int ln_ws_send_audio_data_direct(const uint8_t *audio_data, uint32_t audio_size, bool finished);
+static int ln_ws_write_audio_to_file(const uint8_t *audio_data, uint32_t audio_size, const char *chat_id);
const char* ln_ws_get_connection_state_string(void);
/* Base64缓存管理函数 */
char message_id[128];
generate_message_id(message_id, sizeof(message_id), g_audio_chat_ctx.chat_id, g_audio_chat_ctx.message_counter);
+#if ENABLE_AUDIO_FILE_WRITE
+ // 写入本地文件(根据chat_id生成文件,已有文件不生成但追加写入数据)
+ if (audio_data != NULL && audio_size > 0) {
+ ln_ws_write_audio_to_file(audio_data, audio_size, g_audio_chat_ctx.chat_id);
+ }
+#endif
+
// Base64编码音频数据(如果是结束消息,则使用空字符串)
char *audio_base64 = NULL;
if (audio_size == 0) {
LN_WS_INFO("Audio buffer flush completed\n");
}
+/**
+ * @brief 将音频数据写入本地文件(根据chat_id生成文件,已有文件不生成但追加写入数据)
+ * @param audio_data 音频数据
+ * @param audio_size 数据大小
+ * @param chat_id 聊天ID
+ * @return 0成功,-1失败
+ */
+static int ln_ws_write_audio_to_file(const uint8_t *audio_data, uint32_t audio_size, const char *chat_id)
+{
+#if ENABLE_AUDIO_FILE_WRITE
+ if (audio_data == NULL || audio_size == 0 || chat_id == NULL) {
+ return -1;
+ }
+
+ char file_path[128];
+ snprintf(file_path, sizeof(file_path), "/user/audio_%s.MP3", chat_id);
+
+ // 检查文件是否存在
+ struct stat file_stat;
+ const char *mode;
+ if (stat(file_path, &file_stat) != 0) {
+ // 文件不存在,创建新文件
+ mode = "wb";
+ } else {
+ // 文件已存在,追加写入
+ mode = "ab";
+ }
+
+ FILE *fp = fopen(file_path, mode);
+ if (fp != NULL) {
+ size_t written = fwrite(audio_data, 1, audio_size, fp);
+ fclose(fp);
+ if (written == audio_size) {
+ LN_WS_DEBUG("Audio data written to file: %s, size: %u, mode: %s\n",
+ file_path, audio_size, mode);
+ return 0;
+ } else {
+ LN_WS_WARN("Failed to write all audio data to file: %s, written: %zu, expected: %u\n",
+ file_path, written, audio_size);
+ return -1;
+ }
+ } else {
+ LN_WS_WARN("Failed to open audio file: %s, mode: %s\n", file_path, mode);
+ return -1;
+ }
+#else
+ // 功能未启用,直接返回成功
+ (void)audio_data;
+ (void)audio_size;
+ (void)chat_id;
+ return 0;
+#endif
+}
+
/**
* 直接发送音频数据(跳过状态检查)
* @param audio_data 音频数据
LN_WS_ERROR("Invalid audio data parameters\n");
return -1;
}
+
+
// 如果聊天未开始,生成新的聊天ID
if (!g_audio_chat_ctx.is_active) {
reset_chat_id_match();
LN_WS_DEBUG("New chat started: %s\n", g_audio_chat_ctx.chat_id);
}
+
+
+
+
// 增加消息计数器
g_audio_chat_ctx.message_counter++;
char message_id[128];
generate_message_id(message_id, sizeof(message_id), g_audio_chat_ctx.chat_id, g_audio_chat_ctx.message_counter);
+#if ENABLE_AUDIO_FILE_WRITE
+ // 写入本地文件(根据chat_id生成文件,已有文件不生成但追加写入数据)
+ if (audio_data != NULL && audio_size > 0) {
+ ln_ws_write_audio_to_file(audio_data, audio_size, g_audio_chat_ctx.chat_id);
+ }
+#endif
+
// Base64编码音频数据(如果是结束消息,则使用空字符串)
char *audio_base64 = NULL;
if (audio_size == 0) {