captureConfig.streamType = AUDIO_STREAM_VOICE_ASSISTANT;
int ret = g_stream_in_uplink->Init(captureConfig);
-
+ if (ret != 0) {
+ MEDIA_ERR_LOG("audio stream init failed with error %d", ret);
+ return;
+ }
// AI_data_assembly(1,start_data, Cur_AI_Model);
ret = g_stream_in_uplink->Start();
if (ret != 0) {
- MEDIA_ERR_LOG("audio stream out downink play failed");
- return ;
-
+ MEDIA_ERR_LOG("audio stream start failed with error %d", ret);
+ return;
}
MEDIA_ERR_LOG("g_stream_in_uplink->Start");
uint8_t* data_buf=(uint8_t *)malloc(ai_size);
if(data_buf == NULL)
{
- MEDIA_ERR_LOG("malloc");
+ MEDIA_ERR_LOG("malloc failed for size %d", ai_size);
return ;
}
+
+ // 初始化内存并验证分配
+ if (memset_s(data_buf, ai_size, 0, ai_size) != 0) {
+ MEDIA_ERR_LOG("memset_s failed");
+ free(data_buf);
+ return;
+ }
+
+ // 验证内存访问
+ if (data_buf[0] != 0 || data_buf[ai_size-1] != 0) {
+ MEDIA_ERR_LOG("Memory verification failed");
+ free(data_buf);
+ return;
+ }
AudioCaptureTask_is_run=1;
while (!g_sample_needStop) {
LOS_Msleep(10);
+
+ // 检查数据缓冲区是否有效
+ if (data_buf == NULL) {
+ MEDIA_ERR_LOG("data_buf is NULL, breaking loop");
+ break;
+ }
+
CapturerBuffer capBuf = {};
capBuf.data = data_buf;
capBuf.size = ai_size;
capBuf.frameCount = 1;
+
+ // 添加错误处理
+ if (g_stream_in_uplink == nullptr) {
+ MEDIA_ERR_LOG("g_stream_in_uplink is nullptr");
+ break;
+ }
+
int32_t readlen = g_stream_in_uplink->ObtainBuffer(capBuf, true);
- if(readlen>0)
+ if(readlen > 0 && readlen <= ai_size)
{
- MEDIA_ERR_LOG("readlen %d",readlen);
- send_msg_data_into_Ai_msg_queue((void *)data_buf,(uint16_t)readlen,0);
-
- record_size+=readlen;
-
- fwrite(data_buf,1,readlen,fp);
- #if TTS_TEST
- if(record_size>=1024*80)
- {
- g_sample_needStop=1;
+ MEDIA_ERR_LOG("readlen %d",readlen);
+
+ // 检查内存边界
+ if (readlen <= ai_size) {
+ send_msg_data_into_Ai_msg_queue((void *)data_buf,(uint16_t)readlen,0);
+ record_size+=readlen;
+
+ if (fp != NULL) {
+ fwrite(data_buf,1,readlen,fp);
+ }
+
+ #if TTS_TEST
+ if(record_size>=1024*80)
+ {
+ g_sample_needStop=1;
+ }
+ #else
+ if(record_size>=1024*90)
+ {
+ g_sample_needStop=1;
+ }
+ #endif
+
+ // 安全的内存清零
+ if (data_buf != NULL) {
+ memset_s(data_buf,ai_size,0,ai_size);
+ }
+ } else {
+ MEDIA_ERR_LOG("readlen %d exceeds buffer size %d", readlen, ai_size);
}
- #else
- if(record_size>=1024*90)
- {
- g_sample_needStop=1;
+ } else if (readlen < 0) {
+ MEDIA_ERR_LOG("ObtainBuffer failed with error %d", readlen);
+ // 增加错误处理和重试逻辑
+ static uint32_t error_count = 0;
+ error_count++;
+
+ if (error_count > 5) { // 减少错误阈值
+ MEDIA_ERR_LOG("Too many ObtainBuffer errors, stopping audio capture");
+ g_sample_needStop = 1;
+ break;
+ }
+
+ // 增加延迟避免频繁重试
+ LOS_Msleep(200); // 增加延迟时间
+
+ // 尝试重新初始化音频流
+ if (error_count % 3 == 0) { // 更频繁的重试
+ MEDIA_ERR_LOG("Attempting to reinitialize audio stream");
+ if (g_stream_in_uplink != nullptr) {
+ g_stream_in_uplink->Stop();
+ LOS_Msleep(100); // 增加停止时间
+ int ret = g_stream_in_uplink->Start();
+ if (ret != 0) {
+ MEDIA_ERR_LOG("Audio stream restart failed with error %d", ret);
+ g_sample_needStop = 1;
+ break;
+ }
+ }
}
- #endif
- memset_s(data_buf,ai_size,0,ai_size);
}
// 蓝牙接口发送实时录音数据
}
uint8_t start_data[19]={0};
AI_data_assembly(0,start_data,Cur_AI_Model);
- fclose(fp);
+
+ if (fp != NULL) {
+ fclose(fp);
+ fp = NULL;
+ }
+
send_msg_data_into_Ai_msg_queue((void *)start_data,19,2);
-
-
- free(data_buf);
- data_buf = NULL;
+ // 安全释放内存
+ if (data_buf != NULL) {
+ free(data_buf);
+ data_buf = NULL;
+ }
MEDIA_ERR_LOG("AI_audio_size %d",AI_audio_size);
ret = g_stream_in_uplink->Stop();