diff --git a/CMakeLists.txt b/CMakeLists.txt index 3126a48..1224495 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,8 @@ add_executable(rtc_plugins rtc_plugins.cpp util/RTCContext.cpp util/numpyConfig.h - util/numpyStub.h) + util/numpyStub.h + util/GIL.h) add_library(rtc_plugins_lib SHARED rtc_plugins.cpp util/RTCContext.cpp) diff --git a/util/GIL.h b/util/GIL.h new file mode 100644 index 0000000..08c0a96 --- /dev/null +++ b/util/GIL.h @@ -0,0 +1,18 @@ +#include +struct PyLockGIL +{ + + PyLockGIL(): gstate(PyGILState_Ensure()) + { + } + + ~PyLockGIL() + { + PyGILState_Release(gstate); + } + + PyLockGIL(const PyLockGIL&) = delete; + PyLockGIL& operator=(const PyLockGIL&) = delete; + + PyGILState_STATE gstate; +}; \ No newline at end of file diff --git a/util/RTCContext.cpp b/util/RTCContext.cpp index 91e0e52..e5135b4 100644 --- a/util/RTCContext.cpp +++ b/util/RTCContext.cpp @@ -75,7 +75,8 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId, // 1. 获取GIL std::cout << "[1] 获取GIL锁..." << std::endl; #ifdef GIL - PyGILState_STATE gstate = PyGILState_Ensure(); + //PyGILState_STATE gstate = PyGILState_Ensure(); + PyLockGIL gil; #endif try { @@ -191,19 +192,19 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId, std::cout << "[9] 释放GIL..." << std::endl; #ifdef GIL - PyGILState_Release(gstate); + //PyGILState_Release(gstate); #endif std::cout << "=== 音频处理完成 ===" << std::endl; } catch (const std::exception& e) { std::cout << "[EXCEPTION] 异常捕获: " << e.what() << std::endl; #ifdef GIL - PyGILState_Release(gstate); + //PyGILState_Release(gstate); #endif std::cerr << "Audio process error: " << e.what() << std::endl; } #ifdef GIL - PyGILState_Release(gstate); + //PyGILState_Release(gstate); #endif } diff --git a/util/RTCContext.h b/util/RTCContext.h index e2c079e..b968927 100644 --- a/util/RTCContext.h +++ b/util/RTCContext.h @@ -3,6 +3,7 @@ //#include "numpyConfig.h" #include "numpyStub.h" +#include "GIL.h" #include "IMRTCEngine.hpp" #include "MRTCEngineDefine.hpp"