From 9b37d69bfd881d95eb4ac45f487a2caf2c4a45fd Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Tue, 2 Dec 2025 11:12:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0python=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 3 + .gitmodules_github | 5 +- 3rdpart/CMakeLists.txt | 12 +++- 3rdpart/pybind11 | 1 + CMakeLists.txt | 4 ++ python/mk_logger.py | 27 +++++++ python/mk_plugin.py | 21 ++++++ server/WebHook.cpp | 10 +++ server/main.cpp | 19 +++++ server/pyinvoker.cpp | 155 +++++++++++++++++++++++++++++++++++++++++ server/pyinvoker.h | 46 ++++++++++++ src/Common/config.h | 20 +++--- 12 files changed, 311 insertions(+), 12 deletions(-) create mode 160000 3rdpart/pybind11 create mode 100644 python/mk_logger.py create mode 100644 python/mk_plugin.py create mode 100644 server/pyinvoker.cpp create mode 100644 server/pyinvoker.h diff --git a/.gitmodules b/.gitmodules index c6211ba1..af38aadf 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "www/webassist"] path = www/webassist url = https://gitee.com/victor1002/zlm_webassist +[submodule "3rdpart/pybind11"] + path = 3rdpart/pybind11 + url = https://gitee.com/mirrors/pybind11.git diff --git a/.gitmodules_github b/.gitmodules_github index 87b576ee..f3b18b57 100644 --- a/.gitmodules_github +++ b/.gitmodules_github @@ -9,4 +9,7 @@ url = https://github.com/open-source-parsers/jsoncpp.git [submodule "www/webassist"] path = www/webassist - url = https://github.com/1002victor/zlm_webassist \ No newline at end of file + url = https://github.com/1002victor/zlm_webassist +[submodule "3rdpart/pybind11"] + path = 3rdpart/pybind11 + url = https://github.com/pybind/pybind11.git \ No newline at end of file diff --git a/3rdpart/CMakeLists.txt b/3rdpart/CMakeLists.txt index 845984f6..8a5a534d 100644 --- a/3rdpart/CMakeLists.txt +++ b/3rdpart/CMakeLists.txt @@ -120,4 +120,14 @@ add_subdirectory(ZLToolKit) # 添加库别名 add_library(ZLMediaKit::ToolKit ALIAS ZLToolKit) # 添加依赖 -update_cached_list(MK_LINK_LIBRARIES ZLMediaKit::ToolKit) \ No newline at end of file +update_cached_list(MK_LINK_LIBRARIES ZLMediaKit::ToolKit) + +############################################################################## + +if (ENABLE_PYTHON) + # ============ pybind11 lib ============ + add_subdirectory(pybind11) + update_cached_list(MK_LINK_LIBRARIES pybind11::embed) + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/pybind11/include) + update_cached_list(MK_COMPILE_DEFINITIONS ENABLE_PYTHON) +endif () \ No newline at end of file diff --git a/3rdpart/pybind11 b/3rdpart/pybind11 new file mode 160000 index 00000000..ed5057de --- /dev/null +++ b/3rdpart/pybind11 @@ -0,0 +1 @@ +Subproject commit ed5057ded698e305210269dafa57574ecf964483 diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c9dceec..b72e1fe4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ option(USE_SOLUTION_FOLDERS "Enable solution dir supported" ON) option(ENABLE_OBJCOPY "Enable use objcopy to generate debug info file" ON) # 编译静态库 option(BUILD_SHARED_LIBS "Build shared instead of static" OFF) +option(ENABLE_PYTHON "Enable python plugin" OFF) ############################################################################## # 设置socket默认缓冲区大小为256k.如果设置为0则不设置socket的默认缓冲区大小,使用系统内核默认值(设置为0仅对linux有效) @@ -569,6 +570,9 @@ endif () file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/www" DESTINATION ${EXECUTABLE_OUTPUT_PATH}) file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/conf/config.ini" DESTINATION ${EXECUTABLE_OUTPUT_PATH}) file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/default.pem" DESTINATION ${EXECUTABLE_OUTPUT_PATH}) +if (ENABLE_PYTHON) + file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/python" DESTINATION ${EXECUTABLE_OUTPUT_PATH}) +endif () # 拷贝VideoStack 无视频流时默认填充的背景图片 # Copy the default background image used by VideoStack when there is no video stream diff --git a/python/mk_logger.py b/python/mk_logger.py new file mode 100644 index 00000000..a0b32328 --- /dev/null +++ b/python/mk_logger.py @@ -0,0 +1,27 @@ +import inspect + +try: + import mk_loader + USE_PLUGIN_LOGGER = True +except ImportError: + USE_PLUGIN_LOGGER = False + +def _do_log(level: int, *args): + frame_info = inspect.stack()[2] + filename = frame_info.filename + lineno = frame_info.lineno + funcname = frame_info.function + + # 把所有参数转成字符串后用空格拼接 + msg = " ".join(str(arg) for arg in args) + + if USE_PLUGIN_LOGGER: + mk_loader.log(level, filename, lineno, funcname, msg) + else: + print(f"[{filename}:{lineno}] {funcname} | {msg}") + +def log_trace(*args): _do_log(0, *args) +def log_debug(*args): _do_log(1, *args) +def log_info(*args): _do_log(2, *args) +def log_warn(*args): _do_log(3, *args) +def log_error(*args): _do_log(4, *args) diff --git a/python/mk_plugin.py b/python/mk_plugin.py new file mode 100644 index 00000000..d7b45032 --- /dev/null +++ b/python/mk_plugin.py @@ -0,0 +1,21 @@ +import mk_logger +import mk_loader + +def on_start(): + mk_logger.log_info("on_start") + + +def on_exit(): + mk_logger.log_info("on_exit") + + +def on_publish(type: str, info, invoker, sender) -> bool: + mk_logger.log_info(f"on_publish: {type}") + # opt 控制转协议,请参考配置文件[protocol]下字段 + opt = { + "enable_rtmp": "1" + } + # 响应推流鉴权结果 + mk_loader.mk_publish_auth_invoker_do(invoker, "", opt); + # 返回True代表此事件被python拦截 + return True diff --git a/server/WebHook.cpp b/server/WebHook.cpp index 5475df8f..236cd1c7 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -21,6 +21,10 @@ #include "WebHook.h" #include "WebApi.h" +#if defined(ENABLE_PYTHON) +#include "pyinvoker.h" +#endif + using namespace std; using namespace Json; using namespace toolkit; @@ -358,6 +362,12 @@ void installWebHook() { GET_CONFIG(bool, hook_enable, Hook::kEnable); NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastMediaPublish, [](BroadcastMediaPublishArgs) { +#if defined(ENABLE_PYTHON) + if (PythonInvoker::Instance().on_publish(type, args, invoker, sender)) { + return; + } +#endif + GET_CONFIG(string, hook_publish, Hook::kOnPublish); if (!hook_enable || hook_publish.empty()) { invoker("", ProtocolOption()); diff --git a/server/main.cpp b/server/main.cpp index 3591dcff..a6013bc9 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -43,6 +43,10 @@ #include "ZLMVersion.h" #endif +#if defined(ENABLE_PYTHON) +#include "pyinvoker.h" +#endif + #include "System.h" using namespace std; @@ -107,6 +111,14 @@ onceToken token1([](){ },nullptr); } //namespace RtpProxy +namespace Python { +#define Python_FIELD "python." +const string kPlugin = Python_FIELD"plugin"; +onceToken token1([](){ + mINI::Instance()[kPlugin] = "mk_plugin"; +},nullptr); +} //namespace RtpProxy + } // namespace mediakit @@ -494,6 +506,13 @@ int start_main(int argc,char *argv[]) { g_reload_certificates(); }); #endif + +#if defined(ENABLE_PYTHON) + auto py_plugin = mINI::Instance()[Python::kPlugin]; + if (!py_plugin.empty()) { + PythonInvoker::Instance().load(py_plugin); + } +#endif sem.wait(); } unInstallWebApi(); diff --git a/server/pyinvoker.cpp b/server/pyinvoker.cpp new file mode 100644 index 00000000..f40b4c31 --- /dev/null +++ b/server/pyinvoker.cpp @@ -0,0 +1,155 @@ +#if defined(ENABLE_PYTHON) + +#include "pyinvoker.h" +#include +#include +#include +#include +#include + +using namespace toolkit; +using namespace mediakit; + +template +auto to_python(const T &obj) -> typename std::enable_if::value, py::capsule>::type { + static auto name_str = toolkit::demangle(typeid(T).name()); + auto p = new toolkit::Any(std::make_shared(obj)); + return py::capsule(p, name_str.data(), [](PyObject *capsule) { + auto p = reinterpret_cast(PyCapsule_GetPointer(capsule, name_str.data())); + delete p; + TraceL << "delete " << name_str << "(" << p << ")"; + }); +} + +template +auto to_python(const T &obj) -> typename std::enable_if::value, py::capsule>::type { + static auto name_str = toolkit::demangle(typeid(T).name()); + auto p = new toolkit::Any(std::shared_ptr(const_cast(&obj), [](T *) {})); + return py::capsule(p, name_str.data(), [](PyObject *capsule) { + auto p = reinterpret_cast(PyCapsule_GetPointer(capsule, name_str.data())); + delete p; + TraceL << "unref " << name_str << "(" << p << ")"; + }); +} + +template +T &to_native(const py::capsule &cap) { + static auto name_str = toolkit::demangle(typeid(T).name()); + if (std::string(cap.name()) != name_str) { + throw std::runtime_error("Invalid capsule name!"); + } + auto any = reinterpret_cast(cap.get_pointer()); + return any->get(); +} + +mINI to_native(const py::dict &opt) { + mINI ret; + for (auto &item : opt) { + // 转换为字符串(允许 int/float/bool 等) + ret.emplace(py::str(item.first).cast(), py::str(item.second).cast()); + } + return ret; +} + +PYBIND11_EMBEDDED_MODULE(mk_loader, m) { + m.def("log", [](int lev, const char *file, int line, const char *func, const char *content) { + py::gil_scoped_release release; + LoggerWrapper::printLog(::toolkit::getLogger(), lev, file, func, line, content); + }); + m.def("mk_publish_auth_invoker_do", [](const py::capsule &cap, const std::string &err, const py::dict &opt) { + ProtocolOption option; + option.load(to_native(opt)); + // 执行c++代码时释放gil锁 + py::gil_scoped_release release; + auto &invoker = to_native(cap); + invoker(err, option); + }); +} + +namespace mediakit { + +inline bool set_env(const char *name, const char *value) { +#if defined(_WIN32) + std::string env_str = std::string(name) + "=" + value; + return _putenv(env_str.c_str()) == 0; +#else + return setenv(name, value, 1) == 0; // overwrite = 1 +#endif +} + +bool set_python_path() { + const char *env_var = std::getenv("PYTHONPATH"); + if (env_var && *env_var) { + PrintI("PYTHONPATH is already set to: %s", env_var); + return false; + } + auto default_path = exeDir() + "/python"; + // 1 表示覆盖已存在的值 + if (!set_env("PYTHONPATH", default_path.data())) { + PrintW("Failed to set PYTHONPATH"); + return false; + } + PrintI("PYTHONPATH was not set. Set to default: %s", default_path.data()); + return true; +} + +PythonInvoker &PythonInvoker::Instance() { + static std::shared_ptr instance(new PythonInvoker); + return *instance; +} + +PythonInvoker::PythonInvoker() { + // 确保日志一直可用 + _logger = Logger::Instance().shared_from_this(); + set_python_path(); // 确保 PYTHONPATH 在第一次调用时设置 + _interpreter = new py::scoped_interpreter; + _rel = new py::gil_scoped_release; +} + +PythonInvoker::~PythonInvoker() { + { + py::gil_scoped_acquire gil; // 加锁 + if (_on_exit) { + _on_exit(); + } + _on_exit = py::object(); + _on_publish = py::object(); + _module = py::module(); + } + + delete _rel; + delete _interpreter; +} + +void PythonInvoker::load(const std::string &module_name) { + try { + py::gil_scoped_acquire gil; // 加锁 + _module = py::module::import(module_name.c_str()); + if (hasattr(_module, "on_start")) { + py::object on_start = _module.attr("on_start"); + if (on_start) { + on_start(); + } + } + if (hasattr(_module, "on_exit")) { + _on_exit = _module.attr("on_exit"); + } + if (hasattr(_module, "on_publish")) { + _on_publish = _module.attr("on_publish"); + } + } catch (py::error_already_set &e) { + PrintE("Python exception:%s", e.what()); + } +} + +bool PythonInvoker::on_publish(BroadcastMediaPublishArgs) { + py::gil_scoped_acquire gil; // 确保在 Python 调用期间持有 GIL + if (!_on_publish) { + return false; + } + return _on_publish(getOriginTypeString(type), to_python(args), to_python(invoker), to_python(sender)).cast(); +} + +} // namespace mediakit + +#endif diff --git a/server/pyinvoker.h b/server/pyinvoker.h new file mode 100644 index 00000000..04f266b7 --- /dev/null +++ b/server/pyinvoker.h @@ -0,0 +1,46 @@ + +#ifndef PYINVOKER_H +#define PYINVOKER_H + +#if defined(ENABLE_PYTHON) + +#include +#include +#include +#include +#include "Util/logger.h" +#include "Common/config.h" +#include "Common/MediaSource.h" + +namespace py = pybind11; + +namespace mediakit { + +class PythonInvoker : public std::enable_shared_from_this{ +public: + ~PythonInvoker(); + + static PythonInvoker& Instance(); + + void load(const std::string &module_name); + bool on_publish(BroadcastMediaPublishArgs); + +private: + PythonInvoker(); + +private: + py::gil_scoped_release *_rel; + py::scoped_interpreter *_interpreter; + std::shared_ptr _logger; + py::module _module; + + // 程序退出 + py::object _on_exit; + // 推流鉴权 + py::object _on_publish; +}; + +} // namespace mediakit + +#endif +#endif // PYINVOKER_H \ No newline at end of file diff --git a/src/Common/config.h b/src/Common/config.h index c6c0b2db..a26757cf 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -55,31 +55,31 @@ extern const std::string kBroadcastRecordTs; // 收到http api请求广播 [AUTO-TRANSLATED:c72e7c3f] // Broadcast for receiving http api request extern const std::string kBroadcastHttpRequest; -#define BroadcastHttpRequestArgs const Parser &parser, const HttpSession::HttpResponseInvoker &invoker, bool &consumed, SockInfo &sender +#define BroadcastHttpRequestArgs const Parser &parser, const HttpSession::HttpResponseInvoker &invoker, bool &consumed, toolkit::SockInfo &sender // 在http文件服务器中,收到http访问文件或目录的广播,通过该事件控制访问http目录的权限 [AUTO-TRANSLATED:2de426b4] // In the http file server, broadcast for receiving http access to files or directories. Control access permissions to the http directory through this event. extern const std::string kBroadcastHttpAccess; -#define BroadcastHttpAccessArgs const Parser &parser, const std::string &path, const bool &is_dir, const HttpSession::HttpAccessPathInvoker &invoker, SockInfo &sender +#define BroadcastHttpAccessArgs const Parser &parser, const std::string &path, const bool &is_dir, const HttpSession::HttpAccessPathInvoker &invoker, toolkit::SockInfo &sender // 在http文件服务器中,收到http访问文件或目录前的广播,通过该事件可以控制http url到文件路径的映射 [AUTO-TRANSLATED:0294d0c5] // In the http file server, broadcast before receiving http access to files or directories. Control the mapping from http url to file path through this event. // 在该事件中通过自行覆盖path参数,可以做到譬如根据虚拟主机或者app选择不同http根目录的目的 [AUTO-TRANSLATED:1bea3efb] // By overriding the path parameter in this event, you can achieve the purpose of selecting different http root directories based on virtual hosts or apps. extern const std::string kBroadcastHttpBeforeAccess; -#define BroadcastHttpBeforeAccessArgs const Parser &parser, std::string &path, SockInfo &sender +#define BroadcastHttpBeforeAccessArgs const Parser &parser, std::string &path, toolkit::SockInfo &sender // 该流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm.如果该事件不监听则不认证 [AUTO-TRANSLATED:5f436d8f] // Does this stream need authentication? If yes, call invoker and pass in realm, otherwise pass in an empty realm. If this event is not listened to, no authentication will be performed. extern const std::string kBroadcastOnGetRtspRealm; -#define BroadcastOnGetRtspRealmArgs const MediaInfo &args, const RtspSession::onGetRealm &invoker, SockInfo &sender +#define BroadcastOnGetRtspRealmArgs const MediaInfo &args, const RtspSession::onGetRealm &invoker, toolkit::SockInfo &sender // 请求认证用户密码事件,user_name为用户名,must_no_encrypt如果为true,则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败 [AUTO-TRANSLATED:22b6dfcc] // Request authentication user password event, user_name is the username, must_no_encrypt if true, then the plaintext password must be provided (because it is base64 authentication method at this time), otherwise it will lead to authentication failure. // 获取到密码后请调用invoker并输入对应类型的密码和密码类型,invoker执行时会匹配密码 [AUTO-TRANSLATED:8c57fd43] // After getting the password, please call invoker and input the corresponding type of password and password type. The invoker will match the password when executing. extern const std::string kBroadcastOnRtspAuth; -#define BroadcastOnRtspAuthArgs const MediaInfo &args, const std::string &realm, const std::string &user_name, const bool &must_no_encrypt, const RtspSession::onAuth &invoker, SockInfo &sender +#define BroadcastOnRtspAuthArgs const MediaInfo &args, const std::string &realm, const std::string &user_name, const bool &must_no_encrypt, const RtspSession::onAuth &invoker, toolkit::SockInfo &sender // 推流鉴权结果回调对象 [AUTO-TRANSLATED:7e508ed1] // Push stream authentication result callback object @@ -90,7 +90,7 @@ using PublishAuthInvoker = std::function; // 播放rtsp/rtmp/http-flv事件广播,通过该事件控制播放鉴权 [AUTO-TRANSLATED:eddd7014] // Broadcast for playing rtsp/rtmp/http-flv events. Control playback authentication through this event. extern const std::string kBroadcastMediaPlayed; -#define BroadcastMediaPlayedArgs const MediaInfo &args, const Broadcast::AuthInvoker &invoker, SockInfo &sender +#define BroadcastMediaPlayedArgs const MediaInfo &args, const Broadcast::AuthInvoker &invoker, toolkit::SockInfo &sender // shell登录鉴权 [AUTO-TRANSLATED:26b135d4] // Shell login authentication extern const std::string kBroadcastShellLogin; -#define BroadcastShellLoginArgs const std::string &user_name, const std::string &passwd, const Broadcast::AuthInvoker &invoker, SockInfo &sender +#define BroadcastShellLoginArgs const std::string &user_name, const std::string &passwd, const Broadcast::AuthInvoker &invoker, toolkit::SockInfo &sender // 停止rtsp/rtmp/http-flv会话后流量汇报事件广播 [AUTO-TRANSLATED:69df61d8] // Broadcast for traffic reporting event after stopping rtsp/rtmp/http-flv session extern const std::string kBroadcastFlowReport; -#define BroadcastFlowReportArgs const MediaInfo &args, const uint64_t &totalBytes, const uint64_t &totalDuration, const bool &isPlayer, SockInfo &sender +#define BroadcastFlowReportArgs const MediaInfo &args, const uint64_t &totalBytes, const uint64_t &totalDuration, const bool &isPlayer, toolkit::SockInfo &sender // 未找到流后会广播该事件,请在监听该事件后去拉流或其他方式产生流,这样就能按需拉流了 [AUTO-TRANSLATED:0c00171d] // This event will be broadcast after the stream is not found. Please pull the stream or other methods to generate the stream after listening to this event, so that you can pull the stream on demand. extern const std::string kBroadcastNotFoundStream; -#define BroadcastNotFoundStreamArgs const MediaInfo &args, SockInfo &sender, const std::function &closePlayer +#define BroadcastNotFoundStreamArgs const MediaInfo &args, toolkit::SockInfo &sender, const std::function &closePlayer // 某个流无人消费时触发,目的为了实现无人观看时主动断开拉流等业务逻辑 [AUTO-TRANSLATED:3c45f002] // Triggered when a stream is not consumed by anyone. The purpose is to achieve business logic such as actively disconnecting the pull stream when no one is watching.