debug
This commit is contained in:
parent
498932385e
commit
281bb13a73
|
@ -10,6 +10,24 @@ void RTCContext::onConsumer(uint32_t msgId, const char* roomId, const char* peer
|
|||
std::cout << "RTCContext::onConsumer():" << consumerInfo.roomId << "," << consumerInfo.displayName << "," << consumerInfo.channelIndex;
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
isOnConsumer_ = true;
|
||||
std::cout << "registerSoundLevelListener" << std::endl;
|
||||
int16_t ret1 = rtcEngine_->registerSoundLevelListener(mrtc::TYPE_AUDIO_SOURCE_CUSTOM, consumerInfo.roomId,
|
||||
peerId, consumerInfo.channelIndex, this);
|
||||
if (0 != ret1)
|
||||
{
|
||||
std::cout << "RTCContext::instance().registerSoundLevelListener() inUser failed, ret:" << ret1;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "muteAudio" << std::endl;
|
||||
int16_t ret2 = rtcEngine_->muteAudio(consumerInfo.roomId, peerId, mrtc::TYPE_AUDIO_SOURCE_CUSTOM, false, consumerInfo.channelIndex);
|
||||
if (0 != ret2)
|
||||
{
|
||||
std::cout << "RTCContext::instance().muteAudio() failed, ret:" << ret2;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "init recv succ" << std::endl;
|
||||
//std::cout << "RTCContext::onConsumer()" << std::endl;
|
||||
}
|
||||
void RTCContext::onRender(const char* roomId, const char* peerId,
|
||||
|
@ -36,242 +54,7 @@ void RTCContext::onSoundLevelUpdate(const char* roomId, const char* peerId, uint
|
|||
{
|
||||
std::cout << "RTCContext::onSoundLevelUpdate()" << std::endl;
|
||||
}
|
||||
/*
|
||||
void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
|
||||
mrtc::MRTCAudioFrame& audioFrame,
|
||||
mrtc::MRTCAudioSourceType audioSourceType)
|
||||
{
|
||||
namespace py = boost::python;
|
||||
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
|
||||
try {
|
||||
std::cout << "-----------------------------------" << std::endl;
|
||||
std::cout << "dataCount:" << audioFrame.dataCount << std::endl;
|
||||
std::cout << "dataCount value: " << audioFrame.dataCount
|
||||
<< " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl;
|
||||
|
||||
|
||||
std::cout << "onAudioProcess, numpyApi_:" << numpyApi_[93] << std::endl;
|
||||
if (!numpyApi_ || !numpyApi_[93]) { // 93是PyArray_SimpleNew的偏移量
|
||||
std::cout << "numpyApi_ is null in onAudioProcess" << std::endl;
|
||||
} else {
|
||||
std::cout << "numpyApi_ is not null in onAudioProcess:" << numpyApi_[93] << std::endl;
|
||||
}
|
||||
//auto numpyApi = RTCContext::numpy_api();
|
||||
std::cout << "step1" << std::endl;
|
||||
if (!numpyApi_) {
|
||||
PyGILState_Release(gstate);
|
||||
throw std::runtime_error("NumPy C-API not initialized. Call import_array() in module init");
|
||||
}
|
||||
std::cout << "step2" << std::endl;
|
||||
|
||||
using PyArray_SimpleNew_t = PyObject*(*)(int, npy_intp*, int);
|
||||
|
||||
void* func_ptr = numpyApi_[93];
|
||||
std::cout << "Raw function pointer: " << func_ptr << std::endl;
|
||||
|
||||
auto ptmp = (PyObject*(*)(int, npy_intp*, int))numpyApi_[93];
|
||||
std::cout << "ptmp is:" << ptmp << std::endl;
|
||||
std::cout << "Pointer sizes:\n"
|
||||
<< "void*: " << sizeof(void*) << "\n"
|
||||
<< "FunctionPtr: " << sizeof(PyObject*(*)(int, npy_intp*, int)) << std::endl;
|
||||
|
||||
// 2. 使用memcpy避免编译器优化问题
|
||||
PyArray_SimpleNew_t PyArray_SimpleNew;
|
||||
static_assert(sizeof(func_ptr) == sizeof(PyArray_SimpleNew),
|
||||
"Pointer size mismatch");
|
||||
std::cout << "step3" << std::endl;
|
||||
memcpy(&PyArray_SimpleNew, &func_ptr, sizeof(func_ptr));
|
||||
|
||||
//auto PyArray_SimpleNew = reinterpret_cast<PyArray_SimpleNew_t>(numpyApi_[93]);
|
||||
std::cout << "step4, PyArray_SimpleNew:" << PyArray_SimpleNew << std::endl;
|
||||
|
||||
// 3. 严格校验输入数据
|
||||
if (!audioFrame.data || audioFrame.dataCount <= 0) {
|
||||
PyGILState_Release(gstate);
|
||||
throw std::invalid_argument("Invalid audio frame data");
|
||||
}
|
||||
std::cout << "step5" << std::endl;
|
||||
|
||||
// 4. 安全创建维度数组(带边界检查)
|
||||
if (audioFrame.dataCount > std::numeric_limits<npy_intp>::max()) {
|
||||
PyGILState_Release(gstate);
|
||||
throw std::overflow_error("Audio frame size exceeds maximum limit");
|
||||
}
|
||||
std::cout << "step6" << std::endl;
|
||||
npy_intp dims[1] = {static_cast<npy_intp>(audioFrame.dataCount)};
|
||||
|
||||
std::cout << "step7" << std::endl;
|
||||
// 5. 创建NumPy数组(带内存保护)
|
||||
PyObject* pyArray = nullptr;
|
||||
pyArray = PyArray_SimpleNew(1, dims, NPY_INT16);
|
||||
std::cout << "step8" << std::endl;
|
||||
if (!pyArray) {
|
||||
PyGILState_Release(gstate);
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
std::cout << "step9" << std::endl;
|
||||
|
||||
// 6. 安全拷贝数据(带对齐检查)
|
||||
if (reinterpret_cast<uintptr_t>(audioFrame.data) % alignof(int16_t) != 0) {
|
||||
Py_DECREF(pyArray);
|
||||
PyGILState_Release(gstate);
|
||||
throw std::runtime_error("Unaligned audio data pointer");
|
||||
}
|
||||
std::cout << "step10" << std::endl;
|
||||
std::memcpy(PyArray_DATA(reinterpret_cast<PyArrayObject*>(pyArray)),
|
||||
audioFrame.data,
|
||||
audioFrame.dataCount * sizeof(int16_t));
|
||||
|
||||
std::cout << "step11" << std::endl;
|
||||
// 7. 执行回调(带引用计数保护)
|
||||
if (!pyCallback_.is_none()) {
|
||||
try {
|
||||
pyCallback_(
|
||||
py::handle<>(pyArray), // 自动管理引用
|
||||
audioFrame.dataCount,
|
||||
audioFrame.sampleRate,
|
||||
audioFrame.numChannels,
|
||||
audioFrame.channelIndex
|
||||
);
|
||||
} catch (...) {
|
||||
Py_DECREF(pyArray);
|
||||
throw; // 重新抛出异常
|
||||
}
|
||||
}
|
||||
std::cout << "step12" << std::endl;
|
||||
|
||||
// 8. 释放资源
|
||||
Py_DECREF(pyArray);
|
||||
std::cout << "step13" << std::endl;
|
||||
PyGILState_Release(gstate);
|
||||
std::cout << "step14" << std::endl;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Audio process error: " << e.what() << std::endl;
|
||||
PyErr_Print();
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
*/
|
||||
/*
|
||||
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");
|
||||
}
|
||||
|
||||
if (audioFrame.dataCount > std::numeric_limits<npy_intp>::max()) {
|
||||
std::cout << "[ERROR] 数据长度超过最大值" << std::endl;
|
||||
throw std::overflow_error("Audio frame size exceeds maximum limit");
|
||||
}
|
||||
|
||||
// 3. 准备数组维度
|
||||
std::cout << "[3] 准备数组维度..." << std::endl;
|
||||
npy_intp dims[1] = {static_cast<npy_intp>(audioFrame.dataCount)};
|
||||
std::cout << " 维度设置完成: [" << dims[0] << "]" << std::endl;
|
||||
|
||||
// 4. 检查NumPy API状态
|
||||
std::cout << "[4] 检查NumPy API状态..." << std::endl;
|
||||
std::cout << " numpyApi_ 地址: " << numpyApi_ << std::endl;
|
||||
if (!numpyApi_) {
|
||||
throw std::runtime_error("NumPy C-API not initialized");
|
||||
}
|
||||
|
||||
// 5. 获取PyArray_SimpleNew函数
|
||||
std::cout << "[5] 获取PyArray_SimpleNew函数..." << std::endl;
|
||||
using PyArray_SimpleNew_t = PyObject*(*)(int, npy_intp*, int);
|
||||
PyArray_SimpleNew_t PyArray_SimpleNew =
|
||||
reinterpret_cast<PyArray_SimpleNew_t>(numpyApi_[93]);
|
||||
std::cout << " 函数地址: " << (void*)PyArray_SimpleNew << std::endl;
|
||||
|
||||
std::cout << "[5.1] 验证函数指针..." << std::endl;
|
||||
void* func_ptr = numpyApi_[93];
|
||||
if (reinterpret_cast<uintptr_t>(func_ptr) < 0x1000) { // 检查是否为合法地址
|
||||
std::cerr << "非法函数指针: " << func_ptr << std::endl;
|
||||
throw std::runtime_error("Invalid PyArray_SimpleNew pointer");
|
||||
}
|
||||
// 6. 创建NumPy数组
|
||||
std::cout << "[6] 创建NumPy数组..." << std::endl;
|
||||
PyObject* pyArray = PyArray_SimpleNew(1, dims, NPY_INT16);
|
||||
std::cout << " 数组地址: " << pyArray << std::endl;
|
||||
|
||||
if (!pyArray) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
|
||||
// 7. 检查内存对齐
|
||||
std::cout << "[7] 检查内存对齐..." << std::endl;
|
||||
std::cout << " 音频数据地址: " << (void*)audioFrame.data
|
||||
<< " 对齐要求: " << alignof(int16_t) << std::endl;
|
||||
|
||||
if (reinterpret_cast<uintptr_t>(audioFrame.data) % alignof(int16_t) != 0) {
|
||||
Py_DECREF(pyArray);
|
||||
throw std::runtime_error("Unaligned audio data pointer");
|
||||
}
|
||||
|
||||
// 8. 拷贝数据
|
||||
std::cout << "[8] 拷贝音频数据..." << std::endl;
|
||||
std::cout << " 目标地址: " << PyArray_DATA((PyArrayObject*)pyArray)
|
||||
<< " 字节数: " << audioFrame.dataCount * sizeof(int16_t) << std::endl;
|
||||
|
||||
std::memcpy(PyArray_DATA((PyArrayObject*)pyArray),
|
||||
audioFrame.data,
|
||||
audioFrame.dataCount * sizeof(int16_t));
|
||||
|
||||
// 9. 执行回调
|
||||
if (!pyCallback_.is_none()) {
|
||||
std::cout << "[9] 准备执行Python回调..." << std::endl;
|
||||
try {
|
||||
pyCallback_(
|
||||
py::handle<>(pyArray),
|
||||
audioFrame.dataCount,
|
||||
audioFrame.sampleRate,
|
||||
audioFrame.numChannels,
|
||||
audioFrame.channelIndex
|
||||
);
|
||||
std::cout << " 回调执行成功" << std::endl;
|
||||
} catch (...) {
|
||||
std::cout << "[ERROR] 回调执行失败" << std::endl;
|
||||
Py_DECREF(pyArray);
|
||||
throw;
|
||||
}
|
||||
} else {
|
||||
std::cout << "[9] 无回调函数设置" << std::endl;
|
||||
}
|
||||
|
||||
// 10. 释放资源
|
||||
std::cout << "[10] 释放资源..." << std::endl;
|
||||
Py_DECREF(pyArray);
|
||||
std::cout << "[11] 释放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);
|
||||
PyErr_Print();
|
||||
std::cerr << "Audio process error: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
*/
|
||||
void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
|
||||
mrtc::MRTCAudioFrame& audioFrame,
|
||||
mrtc::MRTCAudioSourceType audioSourceType)
|
||||
|
@ -519,6 +302,7 @@ bool RTCContext::initRecv(const char* destRoomId, const char* srcUserId, const i
|
|||
} else {
|
||||
std::cout << "numpyApi_ is not null in initRecv" << std::endl;
|
||||
}
|
||||
/*
|
||||
while (!isOnConsumer_)
|
||||
{
|
||||
std::cout << "wait for OnConsumer" << std::endl;
|
||||
|
@ -542,6 +326,7 @@ bool RTCContext::initRecv(const char* destRoomId, const char* srcUserId, const i
|
|||
}
|
||||
|
||||
std::cout << "init recv succ" << std::endl;
|
||||
*/
|
||||
return true;
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue