ZLMediaKit/api/source/mk_events.cpp

173 lines
7.4 KiB
C++
Raw Permalink Normal View History

2019-12-24 13:56:53 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2019-12-24 13:56:53 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2019-12-24 13:56:53 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2019-12-24 13:56:53 +08:00
*/
#include "mk_events.h"
2019-12-24 13:56:53 +08:00
#include "Common/config.h"
#include "Common/MediaSource.h"
#include "Http/HttpSession.h"
#include "Rtsp/RtspSession.h"
#include "Record/MP4Recorder.h"
using namespace toolkit;
2019-12-24 13:56:53 +08:00
using namespace mediakit;
static void* s_tag;
static mk_events s_events = {0};
API_EXPORT void API_CALL mk_events_listen(const mk_events *events){
2021-08-30 20:43:03 +08:00
if (events) {
memcpy(&s_events, events, sizeof(s_events));
} else {
memset(&s_events, 0, sizeof(s_events));
2019-12-24 13:56:53 +08:00
}
2021-08-30 20:43:03 +08:00
static onceToken token([]{
2019-12-24 13:56:53 +08:00
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastMediaChanged,[](BroadcastMediaChangedArgs){
if(s_events.on_mk_media_changed){
s_events.on_mk_media_changed(bRegist,
(mk_media_source)&sender);
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastRecordMP4,[](BroadcastRecordMP4Args){
if(s_events.on_mk_record_mp4){
s_events.on_mk_record_mp4((mk_mp4_info)&info);
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastHttpRequest,[](BroadcastHttpRequestArgs){
if(s_events.on_mk_http_request){
int consumed_int = consumed;
s_events.on_mk_http_request((mk_parser)&parser,
(mk_http_response_invoker)&invoker,
&consumed_int,
2020-04-23 22:04:59 +08:00
(mk_sock_info)&sender);
2019-12-24 13:56:53 +08:00
consumed = consumed_int;
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastHttpAccess,[](BroadcastHttpAccessArgs){
if(s_events.on_mk_http_access){
s_events.on_mk_http_access((mk_parser)&parser,
path.c_str(),
is_dir,
(mk_http_access_path_invoker)&invoker,
2020-04-23 22:04:59 +08:00
(mk_sock_info)&sender);
2019-12-24 18:28:39 +08:00
} else{
invoker("","",0);
2019-12-24 13:56:53 +08:00
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastHttpBeforeAccess,[](BroadcastHttpBeforeAccessArgs){
if(s_events.on_mk_http_before_access){
char path_c[4 * 1024] = {0};
strcpy(path_c,path.c_str());
s_events.on_mk_http_before_access((mk_parser) &parser,
path_c,
2020-04-23 22:04:59 +08:00
(mk_sock_info) &sender);
2019-12-24 13:56:53 +08:00
path = path_c;
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastOnGetRtspRealm,[](BroadcastOnGetRtspRealmArgs){
if (s_events.on_mk_rtsp_get_realm) {
s_events.on_mk_rtsp_get_realm((mk_media_info) &args,
(mk_rtsp_get_realm_invoker) &invoker,
2020-04-23 22:04:59 +08:00
(mk_sock_info) &sender);
2019-12-24 18:28:39 +08:00
}else{
invoker("");
2019-12-24 13:56:53 +08:00
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastOnRtspAuth,[](BroadcastOnRtspAuthArgs){
if (s_events.on_mk_rtsp_auth) {
s_events.on_mk_rtsp_auth((mk_media_info) &args,
realm.c_str(),
user_name.c_str(),
must_no_encrypt,
(mk_rtsp_auth_invoker) &invoker,
2020-04-23 22:04:59 +08:00
(mk_sock_info) &sender);
2019-12-24 13:56:53 +08:00
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastMediaPublish,[](BroadcastMediaPublishArgs){
if (s_events.on_mk_media_publish) {
s_events.on_mk_media_publish((mk_media_info) &args,
(mk_publish_auth_invoker) &invoker,
2020-04-23 22:04:59 +08:00
(mk_sock_info) &sender);
2020-09-12 19:09:56 +08:00
} else {
invoker("", ProtocolOption());
2019-12-24 13:56:53 +08:00
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastMediaPlayed,[](BroadcastMediaPlayedArgs){
if (s_events.on_mk_media_play) {
s_events.on_mk_media_play((mk_media_info) &args,
(mk_auth_invoker) &invoker,
2020-04-23 22:04:59 +08:00
(mk_sock_info) &sender);
2019-12-24 18:28:39 +08:00
}else{
invoker("");
2019-12-24 13:56:53 +08:00
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastShellLogin,[](BroadcastShellLoginArgs){
if (s_events.on_mk_shell_login) {
s_events.on_mk_shell_login(user_name.c_str(),
passwd.c_str(),
(mk_auth_invoker) &invoker,
2020-04-23 22:04:59 +08:00
(mk_sock_info) &sender);
2019-12-24 18:28:39 +08:00
}else{
invoker("");
2019-12-24 13:56:53 +08:00
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastFlowReport,[](BroadcastFlowReportArgs){
if (s_events.on_mk_flow_report) {
s_events.on_mk_flow_report((mk_media_info) &args,
totalBytes,
totalDuration,
isPlayer,
2020-04-23 22:04:59 +08:00
(mk_sock_info)&sender);
2019-12-24 13:56:53 +08:00
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastNotFoundStream,[](BroadcastNotFoundStreamArgs){
if (s_events.on_mk_media_not_found) {
if (s_events.on_mk_media_not_found((mk_media_info) &args,
(mk_sock_info) &sender)) {
closePlayer();
}
2019-12-24 13:56:53 +08:00
}
});
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastStreamNoneReader,[](BroadcastStreamNoneReaderArgs){
if (s_events.on_mk_media_no_reader) {
s_events.on_mk_media_no_reader((mk_media_source) &sender);
}
});
2021-08-30 20:43:03 +08:00
NoticeCenter::Instance().addListener(&s_tag, EventChannel::kBroadcastLogEvent,[](BroadcastLogEventArgs){
if (s_events.on_mk_log) {
2021-08-30 20:43:03 +08:00
auto log = ctx->str();
s_events.on_mk_log((int) ctx->_level, ctx->_file.data(), ctx->_line, ctx->_function.data(), log.data());
}
});
NoticeCenter::Instance().addListener(&s_tag, Broadcast::kBroadcastSendRtpStopped,[](BroadcastSendRtpStoppedArgs){
if (s_events.on_mk_media_send_rtp_stop) {
s_events.on_mk_media_send_rtp_stop(sender.getMediaTuple().vhost.c_str(), sender.getMediaTuple().app.c_str(),
sender.getMediaTuple().stream.c_str(), ssrc.c_str(), ex.getErrCode(), ex.what());
}
});
2019-12-24 13:56:53 +08:00
});
}