ZLMediaKit/api/source/mk_events.cpp

166 lines
7.0 KiB
C++
Raw Normal View History

2019-12-24 13:56:53 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2019-12-24 13:56:53 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2019-12-24 13:56:53 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* 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 {
GET_CONFIG(bool, toHls, General::kPublishToHls);
GET_CONFIG(bool, toMP4, General::kPublishToMP4);
invoker("", toHls, toMP4);
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) {
s_events.on_mk_media_not_found((mk_media_info) &args,
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::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());
}
});
2019-12-24 13:56:53 +08:00
});
}