完成事件c api的开发

This commit is contained in:
xiongziliang 2019-12-24 13:56:53 +08:00
parent eb89a3e504
commit 5886a38a9f
6 changed files with 222 additions and 49 deletions

View File

@ -24,11 +24,11 @@
* SOFTWARE. * SOFTWARE.
*/ */
#ifndef MK_EVENT_H #ifndef MK_EVENTS_H
#define MK_EVENT_H #define MK_EVENTS_H
#include "common.h" #include "common.h"
#include "event_objects.h" #include "events_objects.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -39,7 +39,8 @@ typedef struct{
* @param regist 10 * @param regist 10
* @param sender MediaSource对象 * @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推流事件广播 * rtsp/rtmp推流事件广播
@ -48,7 +49,9 @@ typedef struct{
* @param invoker invoker返回鉴权结果 * @param invoker invoker返回鉴权结果
* @param sender tcp客户端相关信息 * @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事件广播 * rtsp/rtmp/http-flv/hls事件广播
@ -57,20 +60,23 @@ typedef struct{
* @param invoker invoker返回鉴权结果 * @param invoker invoker返回鉴权结果
* @param sender * @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 url_info url相关信息
* @param sender * @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对象 * @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) * http api请求广播(GET/POST)
@ -79,7 +85,10 @@ typedef struct{
* @param consumed 1 * @param consumed 1
* @param sender http客户端相关信息 * @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目录的权限 * http文件服务器中,http访问文件或目录的广播,访http目录的权限
@ -90,7 +99,12 @@ typedef struct{
* @param invoker invoker返回本次访问文件的结果 * @param invoker invoker返回本次访问文件的结果
* @param sender http客户端相关信息 * @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到文件路径的映射 * http文件服务器中,http访问文件或目录前的广播,http url到文件路径的映射
@ -100,7 +114,10 @@ typedef struct{
* @param path , * @param path ,
* @param sender http客户端相关信息 * @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 * rtsp流是否需要认证invoker并传入realm,realm
@ -108,7 +125,9 @@ typedef struct{
* @param invoker invoker返回是否需要rtsp专属认证 * @param invoker invoker返回是否需要rtsp专属认证
* @param sender 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认证方式), * user_name为用户名must_no_encrypt如果为true(base64认证方式),
@ -120,17 +139,25 @@ typedef struct{
* @param invoker invoker返回rtsp专属认证的密码 * @param invoker invoker返回rtsp专属认证的密码
* @param sender 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分片文件成功后广播 * mp4分片文件成功后广播
*/ */
typedef void (*on_mk_record_mp4)(mk_mp4_info mp4); void (*on_mk_record_mp4)(const mk_mp4_info mp4);
/** /**
* shell登录鉴权 * 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会话后流量汇报事件广播 * rtsp/rtmp/http-flv会话后流量汇报事件广播
@ -139,12 +166,23 @@ typedef struct{
* @param total_seconds tcp会话时长 * @param total_seconds tcp会话时长
* @param is_player * @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; } mk_events;
/**
* ZLMediaKit里面的事件
* @param events ,null以便取消监听
*/
API_EXPORT void API_CALL mk_events_listen(const mk_events *events);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif //MK_EVENT_H #endif //MK_EVENTS_H

View File

@ -1,4 +1,4 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2019 xiongziliang <771730766@qq.com> * Copyright (c) 2019 xiongziliang <771730766@qq.com>

View File

@ -34,6 +34,6 @@
#include "flvrecorder.h" #include "flvrecorder.h"
#include "player.h" #include "player.h"
#include "pusher.h" #include "pusher.h"
#include "mk_event.h" #include "events.h"
#endif /* MK_API_H_ */ #endif /* MK_API_H_ */

162
api/source/events.cpp Normal file
View File

@ -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);
}
});
});
}

View File

@ -1,4 +1,4 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2019 xiongziliang <771730766@qq.com> * Copyright (c) 2019 xiongziliang <771730766@qq.com>
@ -25,7 +25,7 @@
*/ */
#include <assert.h> #include <assert.h>
#include "event_objects.h" #include "events_objects.h"
#include "Common/config.h" #include "Common/config.h"
#include "Record/MP4Recorder.h" #include "Record/MP4Recorder.h"
#include "Network/TcpSession.h" #include "Network/TcpSession.h"

View File

@ -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"