mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-02-17 22:00:52 +08:00
新增on_get_rtsp_realm Python事件
This commit is contained in:
@@ -63,20 +63,20 @@ 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"args: {type}, args: {args}, sender: {sender}")
|
||||
mk_logger.log_info(f"type: {type}, args: {args}, sender: {sender}")
|
||||
# opt 控制转协议,请参考配置文件[protocol]下字段
|
||||
opt = {
|
||||
"enable_rtmp": "1"
|
||||
}
|
||||
# 响应推流鉴权结果
|
||||
mk_loader.publish_auth_invoker_do(invoker, "", opt);
|
||||
mk_loader.publish_auth_invoker_do(invoker, "", opt)
|
||||
# 返回True代表此事件被python拦截
|
||||
return True
|
||||
|
||||
def on_play(args: dict, invoker, sender: dict) -> bool:
|
||||
mk_logger.log_info(f"args: {args}, sender: {sender}")
|
||||
# 响应播放鉴权结果
|
||||
mk_loader.auth_invoker_do(invoker, "");
|
||||
mk_loader.play_auth_invoker_do(invoker, "")
|
||||
# 返回True代表此事件被python拦截
|
||||
return True
|
||||
|
||||
@@ -85,5 +85,22 @@ def on_flow_report(args: dict, totalBytes: int, totalDuration: int, isPlayer: bo
|
||||
# 返回True代表此事件被python拦截
|
||||
return True
|
||||
|
||||
def on_media_changed(is_register: bool, sender) -> bool:
|
||||
mk_logger.log_info(f"is_register: {is_register}, sender: {sender.getUrl()}")
|
||||
# 该事件在c++中也处理下
|
||||
return False
|
||||
|
||||
def on_player_proxy_failed(url, media_tuple, ex) -> bool:
|
||||
mk_logger.log_info(f"on_player_proxy_failed: {url}, {media_tuple.shortUrl()}, {ex.what()}")
|
||||
# 该事件在c++中也处理下
|
||||
return False
|
||||
|
||||
def on_get_rtsp_realm(args: dict, invoker, sender) -> bool:
|
||||
mk_logger.log_info(f"on_get_rtsp_realm, args: {args}, sender: {sender}")
|
||||
mk_loader.rtsp_get_realm_invoker_do(invoker, "zlmediakit")
|
||||
# 该事件在c++中也处理下
|
||||
return True
|
||||
|
||||
|
||||
def on_reload_config():
|
||||
mk_logger.log_info(f"on_reload_config")
|
||||
@@ -444,6 +444,11 @@ void installWebHook() {
|
||||
// 监听kBroadcastOnGetRtspRealm事件决定rtsp链接是否需要鉴权(传统的rtsp鉴权方案)才能访问 [AUTO-TRANSLATED:00dc9fa3]
|
||||
// Listen to the kBroadcastOnGetRtspRealm event to determine whether the rtsp link needs authentication (traditional rtsp authentication scheme) to access
|
||||
NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastOnGetRtspRealm, [](BroadcastOnGetRtspRealmArgs) {
|
||||
#if defined(ENABLE_PYTHON)
|
||||
if (PythonInvoker::Instance().on_get_rtsp_realm(args, invoker, sender)) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
GET_CONFIG(string, hook_rtsp_realm, Hook::kOnRtspRealm);
|
||||
if (!hook_enable || hook_rtsp_realm.empty()) {
|
||||
// 无需认证 [AUTO-TRANSLATED:77728e07]
|
||||
|
||||
@@ -74,7 +74,7 @@ py::dict to_python(const SockInfo &info) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::shared_ptr<T> to_python2(const T &t) {
|
||||
std::shared_ptr<T> to_python_ref(const T &t) {
|
||||
return std::shared_ptr<T>(const_cast<T *>(&t), py::nodelete());
|
||||
}
|
||||
|
||||
@@ -203,13 +203,20 @@ PYBIND11_EMBEDDED_MODULE(mk_loader, m) {
|
||||
invoker(err, option);
|
||||
});
|
||||
|
||||
m.def("auth_invoker_do", [](const py::capsule &cap, const std::string &err) {
|
||||
m.def("play_auth_invoker_do", [](const py::capsule &cap, const std::string &err) {
|
||||
// 执行c++代码时释放gil锁
|
||||
py::gil_scoped_release release;
|
||||
auto &invoker = to_native<Broadcast::AuthInvoker>(cap);
|
||||
invoker(err);
|
||||
});
|
||||
|
||||
m.def("rtsp_get_realm_invoker_do", [](const py::capsule &cap, const std::string &realm) {
|
||||
// 执行c++代码时释放gil锁
|
||||
py::gil_scoped_release release;
|
||||
auto &invoker = to_native<RtspSession::onGetRealm>(cap);
|
||||
invoker(realm);
|
||||
});
|
||||
|
||||
m.def("set_fastapi", [](const py::object &check_route, const py::object &submit_coro) {
|
||||
static void *fastapi_tag = nullptr;
|
||||
NoticeCenter::Instance().delListener(&fastapi_tag, Broadcast::kBroadcastHttpRequest);
|
||||
@@ -323,13 +330,14 @@ PythonInvoker::~PythonInvoker() {
|
||||
if (_on_exit) {
|
||||
_on_exit();
|
||||
}
|
||||
_on_exit = py::object();
|
||||
_on_publish = py::object();
|
||||
_on_play = py::object();
|
||||
_on_flow_report = py::object();
|
||||
_on_reload_config = py::object();
|
||||
_on_media_changed = py::object();
|
||||
_on_player_proxy_failed = py::object();
|
||||
_on_exit = py::function();
|
||||
_on_publish = py::function();
|
||||
_on_play = py::function();
|
||||
_on_flow_report = py::function();
|
||||
_on_reload_config = py::function();
|
||||
_on_media_changed = py::function();
|
||||
_on_player_proxy_failed = py::function();
|
||||
_on_get_rtsp_realm = py::function();
|
||||
_module = py::module();
|
||||
}
|
||||
delete _rel;
|
||||
@@ -352,6 +360,7 @@ void PythonInvoker::load(const std::string &module_name) {
|
||||
GET_FUNC(_module, on_reload_config);
|
||||
GET_FUNC(_module, on_media_changed);
|
||||
GET_FUNC(_module, on_player_proxy_failed);
|
||||
GET_FUNC(_module, on_get_rtsp_realm);
|
||||
|
||||
if (hasattr(_module, "on_start")) {
|
||||
py::object on_start = _module.attr("on_start");
|
||||
@@ -393,7 +402,7 @@ bool PythonInvoker::on_media_changed(BroadcastMediaChangedArgs) const {
|
||||
if (!_on_media_changed) {
|
||||
return false;
|
||||
}
|
||||
return _on_media_changed(bRegist, to_python2(sender)).cast<bool>();
|
||||
return _on_media_changed(bRegist, to_python_ref(sender)).cast<bool>();
|
||||
}
|
||||
|
||||
bool PythonInvoker::on_player_proxy_failed(BroadcastPlayerProxyFailedArgs) const {
|
||||
@@ -401,7 +410,15 @@ bool PythonInvoker::on_player_proxy_failed(BroadcastPlayerProxyFailedArgs) const
|
||||
if (!_on_player_proxy_failed) {
|
||||
return false;
|
||||
}
|
||||
return _on_player_proxy_failed(sender.getUrl(), to_python2(sender.getMediaTuple()), to_python2(ex)).cast<bool>();
|
||||
return _on_player_proxy_failed(sender.getUrl(), to_python_ref(sender.getMediaTuple()), to_python_ref(ex)).cast<bool>();
|
||||
}
|
||||
|
||||
bool PythonInvoker::on_get_rtsp_realm(BroadcastOnGetRtspRealmArgs) const {
|
||||
py::gil_scoped_acquire gil; // 确保在 Python 调用期间持有 GIL
|
||||
if (!_on_get_rtsp_realm) {
|
||||
return false;
|
||||
}
|
||||
return _on_get_rtsp_realm(to_python(args), to_python(invoker), to_python(sender)).cast<bool>();
|
||||
}
|
||||
|
||||
} // namespace mediakit
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "Common/config.h"
|
||||
#include "Common/MediaSource.h"
|
||||
#include "Player/PlayerProxy.h"
|
||||
#include "Rtsp/RtspSession.h"
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
@@ -30,6 +31,7 @@ public:
|
||||
bool on_flow_report(BroadcastFlowReportArgs) const;
|
||||
bool on_media_changed(BroadcastMediaChangedArgs) const;
|
||||
bool on_player_proxy_failed(BroadcastPlayerProxyFailedArgs) const;
|
||||
bool on_get_rtsp_realm(BroadcastOnGetRtspRealmArgs) const;
|
||||
|
||||
private:
|
||||
PythonInvoker();
|
||||
@@ -41,19 +43,22 @@ private:
|
||||
py::module _module;
|
||||
|
||||
// 程序退出
|
||||
py::object _on_exit;
|
||||
py::function _on_exit;
|
||||
// 推流鉴权
|
||||
py::object _on_publish;
|
||||
py::function _on_publish;
|
||||
// 播放鉴权
|
||||
py::object _on_play;
|
||||
py::function _on_play;
|
||||
// 流量汇报接口
|
||||
py::object _on_flow_report;
|
||||
py::function _on_flow_report;
|
||||
// 配置文件热更新回调
|
||||
py::object _on_reload_config;
|
||||
py::function _on_reload_config;
|
||||
// 媒体注册注销
|
||||
py::object _on_media_changed;
|
||||
py::function _on_media_changed;
|
||||
// 拉流代理失败
|
||||
py::object _on_player_proxy_failed;
|
||||
py::function _on_player_proxy_failed;
|
||||
// rtsp播放是否开启专属鉴权
|
||||
py::function _on_get_rtsp_realm;
|
||||
|
||||
};
|
||||
|
||||
} // namespace mediakit
|
||||
|
||||
Reference in New Issue
Block a user