This commit is contained in:
wangjiyu 2025-04-15 15:28:47 +08:00
parent f2299b38db
commit 16962bf871
1 changed files with 5 additions and 60 deletions

View File

@ -66,7 +66,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
// 1. 获取GIL
std::cout << "[1] 获取GIL锁..." << std::endl;
//PyGILState_STATE gstate = PyGILState_Ensure();
PyGILState_STATE gstate = PyGILState_Ensure();
try {
// 2. 输入参数校验
@ -81,42 +81,6 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
const size_t data_size = audioFrame.dataCount * sizeof(int16_t);
/*
// 3. 创建共享内存
std::cout << "[3] 创建共享内存..." << std::endl;
char shm_name[32];
snprintf(shm_name, sizeof(shm_name), "/audio_shm_%d", getpid());
int fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666);
if (fd == -1) {
std::cout << "[ERROR] shm_open失败: " << strerror(errno) << std::endl;
throw std::runtime_error("Failed to create shared memory");
}
std::cout << " 共享内存fd: " << fd << " 名称: " << shm_name << std::endl;
// 4. 设置共享内存大小
std::cout << "[4] 设置共享内存大小..." << std::endl;
if (ftruncate(fd, data_size) == -1) {
close(fd);
std::cout << "[ERROR] ftruncate失败: " << strerror(errno) << std::endl;
throw std::runtime_error("Failed to resize shared memory");
}
std::cout << " 内存大小: " << data_size << " bytes" << std::endl;
// 5. 内存映射
std::cout << "[5] 内存映射..." << std::endl;
void* ptr = mmap(NULL, data_size, PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
close(fd);
std::cout << "[ERROR] mmap失败: " << strerror(errno) << std::endl;
throw std::runtime_error("Failed to map shared memory");
}
std::cout << " 映射地址: " << ptr << std::endl;
// 6. 拷贝数据到共享内存
std::cout << "[6] 拷贝音频数据到共享内存..." << std::endl;
memcpy(ptr, audioFrame.data, data_size);
*/
std::cout << "step1" << std::endl;
namespace py = boost::python;
namespace np = boost::python::numpy;
@ -145,17 +109,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cout << " pyCallback_ repr: " << PyUnicode_AsUTF8(repr) << std::endl;
Py_DECREF(repr); // 必须手动释放
}
/*
// 传递共享内存信息
pyCallback_(
py::str(shm_name), // 共享内存名称
data_size, // 数据大小
audioFrame.dataCount,
audioFrame.sampleRate,
audioFrame.numChannels,
audioFrame.channelIndex
);
*/
pyCallback_(
audioArray, // numpy 数组
data_size, // 数据大小
@ -197,11 +151,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
Py_DECREF(pyCallback_.ptr());
} catch (...) {
std::cout << "[ERROR] 回调执行失败" << std::endl;
/*
munmap(ptr, data_size);
close(fd);
shm_unlink(shm_name);
*/
Py_DECREF(pyCallback_.ptr());
throw;
}
@ -212,19 +162,14 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
// 8. 释放资源
std::cout << "[8] 释放共享内存资源..." << std::endl;
/*
munmap(ptr, data_size);
close(fd);
shm_unlink(shm_name);
*/
std::cout << "[9] 释放GIL..." << std::endl;
//PyGILState_Release(gstate);
PyGILState_Release(gstate);
std::cout << "=== 音频处理完成 ===" << std::endl;
} catch (const std::exception& e) {
std::cout << "[EXCEPTION] 异常捕获: " << e.what() << std::endl;
//PyGILState_Release(gstate);
PyGILState_Release(gstate);
std::cerr << "Audio process error: " << e.what() << std::endl;
}
}