mirror of
https://gitee.com/xia-chu/ZLMediaKit.git
synced 2026-05-19 00:07:49 +08:00
on_flow_report回调到python层
This commit is contained in:
parent
c496dbf51e
commit
566761b47e
@ -8,7 +8,7 @@ def on_exit():
|
|||||||
mk_logger.log_info("on_exit")
|
mk_logger.log_info("on_exit")
|
||||||
|
|
||||||
def on_publish(type: str, args: dict, invoker, sender: dict) -> bool:
|
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 控制转协议,请参考配置文件[protocol]下字段
|
||||||
opt = {
|
opt = {
|
||||||
"enable_rtmp": "1"
|
"enable_rtmp": "1"
|
||||||
@ -19,8 +19,13 @@ def on_publish(type: str, args: dict, invoker, sender: dict) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def on_play(args: dict, invoker, sender: dict) -> bool:
|
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, "");
|
mk_loader.auth_invoker_do(invoker, "");
|
||||||
# 返回True代表此事件被python拦截
|
# 返回True代表此事件被python拦截
|
||||||
return True
|
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
|
||||||
|
|||||||
@ -417,6 +417,11 @@ void installWebHook() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastFlowReport, [](BroadcastFlowReportArgs) {
|
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);
|
GET_CONFIG(string, hook_flowreport, Hook::kOnFlowReport);
|
||||||
if (!hook_enable || hook_flowreport.empty()) {
|
if (!hook_enable || hook_flowreport.empty()) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -73,7 +73,7 @@ T &to_native(const py::capsule &cap) {
|
|||||||
if (std::string(cap.name()) != name_str) {
|
if (std::string(cap.name()) != name_str) {
|
||||||
throw std::runtime_error("Invalid capsule name!");
|
throw std::runtime_error("Invalid capsule name!");
|
||||||
}
|
}
|
||||||
auto any = reinterpret_cast<toolkit::Any *>(cap.get_pointer());
|
auto any = static_cast<toolkit::Any *>(cap.get_pointer());
|
||||||
return any->get<T>();
|
return any->get<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,12 +181,15 @@ void PythonInvoker::load(const std::string &module_name) {
|
|||||||
if (hasattr(_module, "on_play")) {
|
if (hasattr(_module, "on_play")) {
|
||||||
_on_play = _module.attr("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) {
|
} catch (py::error_already_set &e) {
|
||||||
PrintE("Python exception:%s", e.what());
|
PrintE("Python exception:%s", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PythonInvoker::on_publish(BroadcastMediaPublishArgs) {
|
bool PythonInvoker::on_publish(BroadcastMediaPublishArgs) const {
|
||||||
py::gil_scoped_acquire gil; // 确保在 Python 调用期间持有 GIL
|
py::gil_scoped_acquire gil; // 确保在 Python 调用期间持有 GIL
|
||||||
if (!_on_publish) {
|
if (!_on_publish) {
|
||||||
return false;
|
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>();
|
return _on_publish(getOriginTypeString(type), to_python(args), to_python(invoker), to_python(sender)).cast<bool>();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PythonInvoker::on_play(BroadcastMediaPlayedArgs) {
|
bool PythonInvoker::on_play(BroadcastMediaPlayedArgs) const {
|
||||||
py::gil_scoped_acquire gil; // 确保在 Python 调用期间持有 GIL
|
py::gil_scoped_acquire gil; // 确保在 Python 调用期间持有 GIL
|
||||||
if (!_on_play) {
|
if (!_on_play) {
|
||||||
return false;
|
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>();
|
return _on_play(to_python(args), to_python(invoker), to_python(sender)).cast<bool>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<bool>();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace mediakit
|
} // namespace mediakit
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -23,8 +23,9 @@ public:
|
|||||||
static PythonInvoker& Instance();
|
static PythonInvoker& Instance();
|
||||||
|
|
||||||
void load(const std::string &module_name);
|
void load(const std::string &module_name);
|
||||||
bool on_publish(BroadcastMediaPublishArgs);
|
bool on_publish(BroadcastMediaPublishArgs) const;
|
||||||
bool on_play(BroadcastMediaPlayedArgs);
|
bool on_play(BroadcastMediaPlayedArgs) const;
|
||||||
|
bool on_flow_report(BroadcastFlowReportArgs) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PythonInvoker();
|
PythonInvoker();
|
||||||
@ -41,6 +42,8 @@ private:
|
|||||||
py::object _on_publish;
|
py::object _on_publish;
|
||||||
// 播放鉴权
|
// 播放鉴权
|
||||||
py::object _on_play;
|
py::object _on_play;
|
||||||
|
// 流量汇报接口
|
||||||
|
py::object _on_flow_report;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mediakit
|
} // namespace mediakit
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user