This commit is contained in:
wangjiyu 2025-04-11 08:42:29 +08:00
parent 66788d2f20
commit ea0e051c96
1 changed files with 103 additions and 1 deletions

View File

@ -155,7 +155,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
exit(0);
}
*/
/*
void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
mrtc::MRTCAudioFrame& audioFrame,
mrtc::MRTCAudioSourceType audioSourceType)
@ -271,6 +271,108 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cerr << "Audio process error: " << e.what() << std::endl;
}
}
*/
void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
mrtc::MRTCAudioFrame& audioFrame,
mrtc::MRTCAudioSourceType audioSourceType)
{
namespace py = boost::python;
std::cout << "=== 开始音频处理(共享内存版) ===" << std::endl;
// 1. 获取GIL
std::cout << "[1] 获取GIL锁..." << std::endl;
PyGILState_STATE gstate = PyGILState_Ensure();
try {
// 2. 输入参数校验
std::cout << "[2] 检查输入参数..." << std::endl;
std::cout << " dataCount: " << audioFrame.dataCount
<< " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl;
if (!audioFrame.data || audioFrame.dataCount <= 0) {
std::cout << "[ERROR] 无效音频数据指针或长度" << std::endl;
throw std::invalid_argument("Invalid audio frame data");
}
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 << " 数据拷贝完成" << std::endl;
// 7. 执行回调
if (!pyCallback_.is_none()) {
std::cout << "[7] 准备执行Python回调..." << std::endl;
try {
// 传递共享内存信息
pyCallback_(
py::str(shm_name), // 共享内存名称
data_size, // 数据大小
audioFrame.dataCount,
audioFrame.sampleRate,
audioFrame.numChannels,
audioFrame.channelIndex
);
std::cout << " 回调执行成功" << std::endl;
} catch (...) {
std::cout << "[ERROR] 回调执行失败" << std::endl;
munmap(ptr, data_size);
close(fd);
shm_unlink(shm_name);
throw;
}
} else {
std::cout << "[7] 无回调函数设置" << std::endl;
}
// 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);
std::cout << "=== 音频处理完成 ===" << std::endl;
} catch (const std::exception& e) {
std::cout << "[EXCEPTION] 异常捕获: " << e.what() << std::endl;
PyGILState_Release(gstate);
std::cerr << "Audio process error: " << e.what() << std::endl;
}
}
void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info)
{