From 5886a38a9f33ca4563947783aa3491e273f00aaa Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 24 Dec 2019 13:56:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=8B=E4=BB=B6c=20api?= =?UTF-8?q?=E7=9A=84=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/{mk_event.h => events.h} | 72 ++++++-- .../{event_objects.h => events_objects.h} | 4 +- api/include/mediakit.h | 2 +- api/source/events.cpp | 162 ++++++++++++++++++ .../{event_objects.cpp => events_objects.cpp} | 4 +- api/source/mk_event.cpp | 27 --- 6 files changed, 222 insertions(+), 49 deletions(-) rename api/include/{mk_event.h => events.h} (63%) rename api/include/{event_objects.h => events_objects.h} (99%) create mode 100644 api/source/events.cpp rename api/source/{event_objects.cpp => events_objects.cpp} (99%) delete mode 100644 api/source/mk_event.cpp diff --git a/api/include/mk_event.h b/api/include/events.h similarity index 63% rename from api/include/mk_event.h rename to api/include/events.h index 303af64f..32ee86a2 100644 --- a/api/include/mk_event.h +++ b/api/include/events.h @@ -24,11 +24,11 @@ * SOFTWARE. */ -#ifndef MK_EVENT_H -#define MK_EVENT_H +#ifndef MK_EVENTS_H +#define MK_EVENTS_H #include "common.h" -#include "event_objects.h" +#include "events_objects.h" #ifdef __cplusplus extern "C" { #endif @@ -39,7 +39,8 @@ typedef struct{ * @param regist 注册为1,注销为0 * @param sender 该MediaSource对象 */ - typedef void (*on_mk_media_changed)(int regist,mk_media_source sender); + void (*on_mk_media_changed)(int regist, + const mk_media_source sender); /** * 收到rtsp/rtmp推流事件广播,通过该事件控制推流鉴权 @@ -48,7 +49,9 @@ typedef struct{ * @param invoker 执行invoker返回鉴权结果 * @param sender 该tcp客户端相关信息 */ - typedef void (*on_mk_media_publish)(mk_media_info url_info,mk_publish_auth_invoker invoker,mk_tcp_session sender); + void (*on_mk_media_publish)(const mk_media_info url_info, + const mk_publish_auth_invoker invoker, + const mk_tcp_session sender); /** * 播放rtsp/rtmp/http-flv/hls事件广播,通过该事件控制播放鉴权 @@ -57,20 +60,23 @@ typedef struct{ * @param invoker 执行invoker返回鉴权结果 * @param sender 播放客户端相关信息 */ - typedef void (*on_mk_media_play)(mk_media_info url_info,mk_auth_invoker invoker,mk_tcp_session sender); + void (*on_mk_media_play)(const mk_media_info url_info, + const mk_auth_invoker invoker, + const mk_tcp_session sender); /** * 未找到流后会广播该事件,请在监听该事件后去拉流或其他方式产生流,这样就能按需拉流了 * @param url_info 播放url相关信息 * @param sender 播放客户端相关信息 */ - typedef void (*on_mk_media_not_found)(mk_media_info url_info,mk_tcp_session sender); + void (*on_mk_media_not_found)(const mk_media_info url_info, + const mk_tcp_session sender); /** * 某个流无人消费时触发,目的为了实现无人观看时主动断开拉流等业务逻辑 * @param sender 该MediaSource对象 */ - typedef void (*on_mk_media_no_reader)(mk_media_source sender); + void (*on_mk_media_no_reader)(const mk_media_source sender); /** * 收到http api请求广播(包括GET/POST) @@ -79,7 +85,10 @@ typedef struct{ * @param consumed 置1则说明我们要处理该事件 * @param sender http客户端相关信息 */ - typedef void (*on_mk_http_request)(mk_parser parser, mk_http_response_invoker invoker, int *consumed,mk_tcp_session sender); + void (*on_mk_http_request)(const mk_parser parser, + const mk_http_response_invoker invoker, + int *consumed, + const mk_tcp_session sender); /** * 在http文件服务器中,收到http访问文件或目录的广播,通过该事件控制访问http目录的权限 @@ -90,7 +99,12 @@ typedef struct{ * @param invoker 执行invoker返回本次访问文件的结果 * @param sender http客户端相关信息 */ - typedef void (*on_mk_http_access)(mk_parser parser, mk_media_info url_info, const char *path,int is_dir, mk_http_access_path_invoker invoker,mk_tcp_session sender); + void (*on_mk_http_access)(const mk_parser parser, + const mk_media_info url_info, + const char *path, + int is_dir, + const mk_http_access_path_invoker invoker, + mk_tcp_session sender); /** * 在http文件服务器中,收到http访问文件或目录前的广播,通过该事件可以控制http url到文件路径的映射 @@ -100,7 +114,10 @@ typedef struct{ * @param path 文件绝对路径,覆盖之可以重定向到其他文件 * @param sender http客户端相关信息 */ - typedef void (*on_mk_http_before_access)(mk_parser parser, mk_media_info url_info,char *path, mk_tcp_session sender); + void (*on_mk_http_before_access)(const mk_parser parser, + const mk_media_info url_info, + char *path, + const mk_tcp_session sender); /** * 该rtsp流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm @@ -108,7 +125,9 @@ typedef struct{ * @param invoker 执行invoker返回是否需要rtsp专属认证 * @param sender rtsp客户端相关信息 */ - typedef void (*on_mk_rtsp_get_realm)(mk_media_info url_info, mk_rtsp_get_realm_invoker invoker, mk_tcp_session sender); + void (*on_mk_rtsp_get_realm)(const mk_media_info url_info, + const mk_rtsp_get_realm_invoker invoker, + const mk_tcp_session sender); /** * 请求认证用户密码事件,user_name为用户名,must_no_encrypt如果为true,则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败 @@ -120,17 +139,25 @@ typedef struct{ * @param invoker 执行invoker返回rtsp专属认证的密码 * @param sender rtsp客户端信息 */ - typedef void (*on_mk_rtsp_auth)(mk_media_info url_info,const char *realm,const char *user_name, int must_no_encrypt, mk_rtsp_auth_invoker invoker, mk_tcp_session sender); + void (*on_mk_rtsp_auth)(const mk_media_info url_info, + const char *realm, + const char *user_name, + int must_no_encrypt, + const mk_rtsp_auth_invoker invoker, + const mk_tcp_session sender); /** * 录制mp4分片文件成功后广播 */ - typedef void (*on_mk_record_mp4)(mk_mp4_info mp4); + void (*on_mk_record_mp4)(const mk_mp4_info mp4); /** * shell登录鉴权 */ - typedef void (*on_mk_shell_login_mp4)(const char *user_name, const char *passwd,mk_auth_invoker invoker,mk_tcp_session sender); + void (*on_mk_shell_login)(const char *user_name, + const char *passwd, + const mk_auth_invoker invoker, + const mk_tcp_session sender); /** * 停止rtsp/rtmp/http-flv会话后流量汇报事件广播 @@ -139,12 +166,23 @@ typedef struct{ * @param total_seconds 本次tcp会话时长,单位秒 * @param is_player 客户端是否为播放器 */ - typedef void (*on_mk_flow_report)(mk_media_info url_info,uint64_t total_bytes,uint64_t total_seconds,int is_player,mk_tcp_session sender); + void (*on_mk_flow_report)(const mk_media_info url_info, + uint64_t total_bytes, + uint64_t total_seconds, + int is_player, + const mk_tcp_session sender); } mk_events; +/** + * 监听ZLMediaKit里面的事件 + * @param events 各个事件的结构体,这个对象在内部会再拷贝一次,可以设置为null以便取消监听 + */ +API_EXPORT void API_CALL mk_events_listen(const mk_events *events); + + #ifdef __cplusplus } #endif -#endif //MK_EVENT_H +#endif //MK_EVENTS_H diff --git a/api/include/event_objects.h b/api/include/events_objects.h similarity index 99% rename from api/include/event_objects.h rename to api/include/events_objects.h index be30b787..aca46ed2 100644 --- a/api/include/event_objects.h +++ b/api/include/events_objects.h @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2019 xiongziliang <771730766@qq.com> @@ -33,7 +33,7 @@ extern "C" { ///////////////////////////////////////////MP4Info///////////////////////////////////////////// //MP4Info对象的C映射 -typedef void *mk_mp4_info; +typedef void* mk_mp4_info; //MP4Info::ui64StartedTime API_EXPORT uint64_t API_CALL mk_mp4_info_get_start_time(const mk_mp4_info ctx); //MP4Info::ui64TimeLen diff --git a/api/include/mediakit.h b/api/include/mediakit.h index 5886f9a5..3d38b9e8 100755 --- a/api/include/mediakit.h +++ b/api/include/mediakit.h @@ -34,6 +34,6 @@ #include "flvrecorder.h" #include "player.h" #include "pusher.h" -#include "mk_event.h" +#include "events.h" #endif /* MK_API_H_ */ diff --git a/api/source/events.cpp b/api/source/events.cpp new file mode 100644 index 00000000..781899fd --- /dev/null +++ b/api/source/events.cpp @@ -0,0 +1,162 @@ +/* + * MIT License + * + * Copyright (c) 2019 xiongziliang <771730766@qq.com> + * + * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "events.h" +#include "Common/config.h" +#include "Common/MediaSource.h" +#include "Http/HttpSession.h" +#include "Rtsp/RtspSession.h" +#include "Record/MP4Recorder.h" +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){ + if(events){ + memcpy(&s_events,events, sizeof(s_events)); + }else{ + memset(&s_events,0,sizeof(s_events)); + } + + static onceToken tokne([]{ + 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, + (mk_tcp_session)&sender); + 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, + (mk_media_info)&args, + path.c_str(), + is_dir, + (mk_http_access_path_invoker)&invoker, + (mk_tcp_session)&sender); + } + }); + + 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, + (mk_media_info) &args, + path_c, + (mk_tcp_session) &sender); + 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, + (mk_tcp_session) &sender); + } + }); + + 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, + (mk_tcp_session) &sender); + } + }); + + 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, + (mk_tcp_session) &sender); + } + }); + + 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, + (mk_tcp_session) &sender); + } + }); + + 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, + (mk_tcp_session) &sender); + } + }); + + 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, + (mk_tcp_session) &sender); + } + }); + + 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, + (mk_tcp_session) &sender); + } + }); + + 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); + } + }); + }); + +} diff --git a/api/source/event_objects.cpp b/api/source/events_objects.cpp similarity index 99% rename from api/source/event_objects.cpp rename to api/source/events_objects.cpp index 15f165db..7fe24418 100644 --- a/api/source/event_objects.cpp +++ b/api/source/events_objects.cpp @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2019 xiongziliang <771730766@qq.com> @@ -25,7 +25,7 @@ */ #include -#include "event_objects.h" +#include "events_objects.h" #include "Common/config.h" #include "Record/MP4Recorder.h" #include "Network/TcpSession.h" diff --git a/api/source/mk_event.cpp b/api/source/mk_event.cpp deleted file mode 100644 index 3f124b15..00000000 --- a/api/source/mk_event.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2019 xiongziliang <771730766@qq.com> - * - * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "mk_event.h"