mirror of
https://gitee.com/xia-chu/ZLMediaKit.git
synced 2026-05-06 19: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")
|
||||
|
||||
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
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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<toolkit::Any *>(cap.get_pointer());
|
||||
auto any = static_cast<toolkit::Any *>(cap.get_pointer());
|
||||
return any->get<T>();
|
||||
}
|
||||
|
||||
@ -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>();
|
||||
}
|
||||
|
||||
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>();
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
#endif
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user