From 566761b47e12cf3aba439b63d80ec07052c3d305 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Tue, 2 Dec 2025 20:33:21 +0800 Subject: [PATCH] =?UTF-8?q?on=5Fflow=5Freport=E5=9B=9E=E8=B0=83=E5=88=B0py?= =?UTF-8?q?thon=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/mk_plugin.py | 9 +++++++-- server/WebHook.cpp | 5 +++++ server/pyinvoker.cpp | 17 ++++++++++++++--- server/pyinvoker.h | 7 +++++-- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/python/mk_plugin.py b/python/mk_plugin.py index 8d4970b5..4e471f6f 100644 --- a/python/mk_plugin.py +++ b/python/mk_plugin.py @@ -8,7 +8,7 @@ def on_exit(): mk_logger.log_info("on_exit") def on_publish(type: str, args: dict, invoker, sender: dict) -> bool: - mk_logger.log_info(f"on_publish, args: {type}, args: {args}, sender: {sender}") + mk_logger.log_info(f"args: {type}, args: {args}, sender: {sender}") # opt 控制转协议,请参考配置文件[protocol]下字段 opt = { "enable_rtmp": "1" @@ -19,8 +19,13 @@ def on_publish(type: str, args: dict, invoker, sender: dict) -> bool: return True def on_play(args: dict, invoker, sender: dict) -> bool: - mk_logger.log_info(f"on_play, args: {args}, sender: {sender}") + mk_logger.log_info(f"args: {args}, sender: {sender}") # 响应播放鉴权结果 mk_loader.auth_invoker_do(invoker, ""); # 返回True代表此事件被python拦截 return True + +def on_flow_report(args: dict, totalBytes: int, totalDuration: int, isPlayer: bool, sender: dict) -> bool: + mk_logger.log_info(f"args: {args}, totalBytes: {totalBytes}, totalDuration: {totalDuration}, isPlayer: {isPlayer}, sender: {sender}") + # 返回True代表此事件被python拦截 + return True diff --git a/server/WebHook.cpp b/server/WebHook.cpp index 516cd48c..d2498ffd 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -417,6 +417,11 @@ void installWebHook() { }); NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastFlowReport, [](BroadcastFlowReportArgs) { +#if defined(ENABLE_PYTHON) + if (PythonInvoker::Instance().on_flow_report(args, totalBytes, totalDuration, isPlayer, sender)) { + return; + } +#endif GET_CONFIG(string, hook_flowreport, Hook::kOnFlowReport); if (!hook_enable || hook_flowreport.empty()) { return; diff --git a/server/pyinvoker.cpp b/server/pyinvoker.cpp index 82662050..831a473c 100644 --- a/server/pyinvoker.cpp +++ b/server/pyinvoker.cpp @@ -73,7 +73,7 @@ T &to_native(const py::capsule &cap) { if (std::string(cap.name()) != name_str) { throw std::runtime_error("Invalid capsule name!"); } - auto any = reinterpret_cast(cap.get_pointer()); + auto any = static_cast(cap.get_pointer()); return any->get(); } @@ -181,12 +181,15 @@ void PythonInvoker::load(const std::string &module_name) { if (hasattr(_module, "on_play")) { _on_play = _module.attr("on_play"); } + if (hasattr(_module, "on_flow_report")) { + _on_flow_report = _module.attr("on_flow_report"); + } } catch (py::error_already_set &e) { PrintE("Python exception:%s", e.what()); } } -bool PythonInvoker::on_publish(BroadcastMediaPublishArgs) { +bool PythonInvoker::on_publish(BroadcastMediaPublishArgs) const { py::gil_scoped_acquire gil; // 确保在 Python 调用期间持有 GIL if (!_on_publish) { return false; @@ -194,7 +197,7 @@ bool PythonInvoker::on_publish(BroadcastMediaPublishArgs) { return _on_publish(getOriginTypeString(type), to_python(args), to_python(invoker), to_python(sender)).cast(); } -bool PythonInvoker::on_play(BroadcastMediaPlayedArgs) { +bool PythonInvoker::on_play(BroadcastMediaPlayedArgs) const { py::gil_scoped_acquire gil; // 确保在 Python 调用期间持有 GIL if (!_on_play) { return false; @@ -202,6 +205,14 @@ bool PythonInvoker::on_play(BroadcastMediaPlayedArgs) { return _on_play(to_python(args), to_python(invoker), to_python(sender)).cast(); } +bool PythonInvoker::on_flow_report(BroadcastFlowReportArgs) const { + py::gil_scoped_acquire gil; // 确保在 Python 调用期间持有 GIL + if (!_on_flow_report) { + return false; + } + return _on_flow_report(to_python(args), totalBytes, totalDuration, isPlayer, to_python(sender)).cast(); +} + } // namespace mediakit #endif diff --git a/server/pyinvoker.h b/server/pyinvoker.h index 185fba7e..b8d47e3e 100644 --- a/server/pyinvoker.h +++ b/server/pyinvoker.h @@ -23,8 +23,9 @@ public: static PythonInvoker& Instance(); void load(const std::string &module_name); - bool on_publish(BroadcastMediaPublishArgs); - bool on_play(BroadcastMediaPlayedArgs); + bool on_publish(BroadcastMediaPublishArgs) const; + bool on_play(BroadcastMediaPlayedArgs) const; + bool on_flow_report(BroadcastFlowReportArgs) const; private: PythonInvoker(); @@ -41,6 +42,8 @@ private: py::object _on_publish; // 播放鉴权 py::object _on_play; + // 流量汇报接口 + py::object _on_flow_report; }; } // namespace mediakit