From 40c232695051ec6fd8e4064a39c590b8e2316eec Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 23 Dec 2019 18:50:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84c=E6=8E=A5=E5=8F=A3=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/event_objects.h | 235 ++++++++++++++++++++++++ api/include/mediakit.h | 1 + api/include/mk_event.h | 150 ++++++++++++++++ api/source/event_objects.cpp | 333 +++++++++++++++++++++++++++++++++++ api/source/mk_event.cpp | 27 +++ src/Record/MP4Recorder.h | 7 +- 6 files changed, 749 insertions(+), 4 deletions(-) create mode 100644 api/include/event_objects.h create mode 100644 api/include/mk_event.h create mode 100644 api/source/event_objects.cpp create mode 100644 api/source/mk_event.cpp diff --git a/api/include/event_objects.h b/api/include/event_objects.h new file mode 100644 index 00000000..be30b787 --- /dev/null +++ b/api/include/event_objects.h @@ -0,0 +1,235 @@ +/* + * 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. + */ + +#ifndef MK_EVENT_OBJECTS_H +#define MK_EVENT_OBJECTS_H +#include "common.h" +#ifdef __cplusplus +extern "C" { +#endif + +///////////////////////////////////////////MP4Info///////////////////////////////////////////// +//MP4Info对象的C映射 +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 +API_EXPORT uint64_t API_CALL mk_mp4_info_get_time_len(const mk_mp4_info ctx); +//MP4Info::ui64FileSize +API_EXPORT uint64_t API_CALL mk_mp4_info_get_file_size(const mk_mp4_info ctx); +//MP4Info::strFilePath +API_EXPORT const char* API_CALL mk_mp4_info_get_file_path(const mk_mp4_info ctx); +//MP4Info::strFileName +API_EXPORT const char* API_CALL mk_mp4_info_get_file_name(const mk_mp4_info ctx); +//MP4Info::strFolder +API_EXPORT const char* API_CALL mk_mp4_info_get_folder(const mk_mp4_info ctx); +//MP4Info::strUrl +API_EXPORT const char* API_CALL mk_mp4_info_get_url(const mk_mp4_info ctx); +//MP4Info::strVhost +API_EXPORT const char* API_CALL mk_mp4_info_get_vhost(const mk_mp4_info ctx); +//MP4Info::strAppName +API_EXPORT const char* API_CALL mk_mp4_info_get_app(const mk_mp4_info ctx); +//MP4Info::strStreamId +API_EXPORT const char* API_CALL mk_mp4_info_get_stream(const mk_mp4_info ctx); + +///////////////////////////////////////////Parser///////////////////////////////////////////// +//Parser对象的C映射 +typedef void* mk_parser; +//Parser::Method(),获取命令字,譬如GET/POST +API_EXPORT const char* API_CALL mk_parser_get_method(const mk_parser ctx); +//Parser::Url(),获取HTTP的访问url(不包括?后面的参数) +API_EXPORT const char* API_CALL mk_parser_get_url(const mk_parser ctx); +//Parser::FullUrl(),包括?后面的参数 +API_EXPORT const char* API_CALL mk_parser_get_full_url(const mk_parser ctx); +//Parser::Params(),?后面的参数字符串 +API_EXPORT const char* API_CALL mk_parser_get_url_params(const mk_parser ctx); +//Parser::getUrlArgs()["key"],获取?后面的参数中的特定参数 +API_EXPORT const char* API_CALL mk_parser_get_url_param(const mk_parser ctx,const char *key); +//Parser::Tail(),获取协议相关信息,譬如 HTTP/1.1 +API_EXPORT const char* API_CALL mk_parser_get_tail(const mk_parser ctx); +//Parser::getValues()["key"],获取HTTP头中特定字段 +API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const char *key); +//Parser::Content(),获取HTTP body +API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx); + +///////////////////////////////////////////MediaInfo///////////////////////////////////////////// +//MediaInfo对象的C映射 +typedef void* mk_media_info; +//MediaInfo::_param_strs +API_EXPORT const char* API_CALL mk_media_info_get_params(const mk_media_info ctx); +//MediaInfo["key"] +API_EXPORT const char* API_CALL mk_media_info_get_param(const mk_media_info ctx,const char *key); +//MediaInfo::_schema +API_EXPORT const char* API_CALL mk_media_info_get_schema(const mk_media_info ctx); +//MediaInfo::_vhost +API_EXPORT const char* API_CALL mk_media_info_get_vhost(const mk_media_info ctx); +//MediaInfo::_app +API_EXPORT const char* API_CALL mk_media_info_get_app(const mk_media_info ctx); +//MediaInfo::_streamid +API_EXPORT const char* API_CALL mk_media_info_get_stream(const mk_media_info ctx); + + +///////////////////////////////////////////MediaSource///////////////////////////////////////////// +//MediaSource对象的C映射 +typedef void* mk_media_source; +//查找MediaSource的回调函数 +typedef void(API_CALL *on_mk_media_source_find_cb)(void *user_data, const mk_media_source ctx); + +//MediaSource::getSchema() +API_EXPORT const char* API_CALL mk_media_source_get_schema(const mk_media_source ctx); +//MediaSource::getVhost() +API_EXPORT const char* API_CALL mk_media_source_get_vhost(const mk_media_source ctx); +//MediaSource::getApp() +API_EXPORT const char* API_CALL mk_media_source_get_app(const mk_media_source ctx); +//MediaSource::getId() +API_EXPORT const char* API_CALL mk_media_source_get_stream(const mk_media_source ctx); +//MediaSource::readerCount() +API_EXPORT int API_CALL mk_media_source_get_reader_count(const mk_media_source ctx); +//MediaSource::close() +API_EXPORT int API_CALL mk_media_source_close(const mk_media_source ctx,int force); +//MediaSource::seekTo() +API_EXPORT int API_CALL mk_media_source_seek_to(const mk_media_source ctx,uint32_t stamp); + +//MediaSource::find() +API_EXPORT void API_CALL mk_media_source_find(const char *schema, + const char *vhost, + const char *app, + const char *stream, + void *user_data, + on_mk_media_source_find_cb cb); +//MediaSource::for_each_media() +API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb); + +///////////////////////////////////////////TcpSession///////////////////////////////////////////// +//TcpSession对象的C映射 +typedef void* mk_tcp_session; +//TcpSession::safeShutdown() +API_EXPORT void API_CALL mk_tcp_session_shutdown(const mk_tcp_session ctx,int err,const char *err_msg); +//TcpSession::get_peer_ip() +API_EXPORT const char* API_CALL mk_tcp_session_peer_ip(const mk_tcp_session ctx); +//TcpSession::get_local_ip() +API_EXPORT const char* API_CALL mk_tcp_session_local_ip(const mk_tcp_session ctx); +//TcpSession::get_peer_port() +API_EXPORT uint16_t API_CALL mk_tcp_session_peer_port(const mk_tcp_session ctx); +//TcpSession::get_local_port() +API_EXPORT uint16_t API_CALL mk_tcp_session_local_port(const mk_tcp_session ctx); + +///////////////////////////////////////////HttpResponseInvoker///////////////////////////////////////////// +//HttpSession::HttpResponseInvoker对象的C映射 +typedef void* mk_http_response_invoker; + +/** + * HttpSession::HttpResponseInvoker(const string &codeOut, const StrCaseMap &headerOut, const string &body); + * @param response_code 譬如200 OK + * @param response_header 返回的http头,譬如 {"Content-Type","text/html",NULL} 必须以NULL结尾 + * @param response_content 返回的content部分,譬如一个网页内容 + */ +API_EXPORT void API_CALL mk_http_response_invoker_do(const mk_http_response_invoker ctx, + const char *response_code, + const char *response_header[], + const char *response_content); +/** + * HttpSession::HttpResponseInvoker(const StrCaseMap &requestHeader,const StrCaseMap &responseHeader,const string &filePath); + * @param request_parser 请求事件中的mk_parser对象,用于提取其中http头中的Range字段 + * @param response_header 返回的http头,譬如 {"Content-Type","text/html",NULL} 必须以NULL结尾 + * @param response_file_path 返回的content部分,譬如/path/to/html/file + */ +API_EXPORT void API_CALL mk_http_response_invoker_do_file(const mk_http_response_invoker ctx, + const mk_parser request_parser, + const char *response_header[], + const char *response_file_path); + + +///////////////////////////////////////////HttpAccessPathInvoker///////////////////////////////////////////// +//HttpSession::HttpAccessPathInvoker对象的C映射 +typedef void* mk_http_access_path_invoker; +/** + * HttpSession::HttpAccessPathInvoker(const string &errMsg,const string &accessPath, int cookieLifeSecond); + * @param err_msg 如果为空,则代表鉴权通过,否则为错误提示,可以为null + * @param access_path 运行或禁止访问的根目录,可以为null + * @param cookie_life_second 鉴权cookie有效期 + **/ +API_EXPORT void API_CALL mk_http_access_path_invoker_do(const mk_http_access_path_invoker ctx, + const char *err_msg, + const char *access_path, + int cookie_life_second); + +///////////////////////////////////////////RtspSession::onGetRealm///////////////////////////////////////////// +//RtspSession::onGetRealm对象的C映射 +typedef void* mk_rtsp_get_realm_invoker; +/** + * 执行RtspSession::onGetRealm + * @param realm 该rtsp流是否需要开启rtsp专属鉴权,至null或空字符串则不鉴权 + */ +API_EXPORT void API_CALL mk_rtsp_get_realm_invoker_do(const mk_rtsp_get_realm_invoker ctx, + const char *realm); + +///////////////////////////////////////////RtspSession::onAuth///////////////////////////////////////////// +//RtspSession::onAuth对象的C映射 +typedef void* mk_rtsp_auth_invoker; + +/** + * 执行RtspSession::onAuth + * @param encrypted 为true是则表明是md5加密的密码,否则是明文密码, 在请求明文密码时如果提供md5密码者则会导致认证失败 + * @param pwd_or_md5 明文密码或者md5加密的密码 + */ +API_EXPORT void API_CALL mk_rtsp_auth_invoker_do(const mk_rtsp_auth_invoker ctx, + int encrypted, + const char *pwd_or_md5); + + +///////////////////////////////////////////Broadcast::PublishAuthInvoker///////////////////////////////////////////// +//Broadcast::PublishAuthInvoker对象的C映射 +typedef void* mk_publish_auth_invoker; + +/** + * 执行Broadcast::PublishAuthInvoker + * @param err_msg 为空或null则代表鉴权成功 + * @param enable_rtxp rtmp推流时是否运行转rtsp;rtsp推流时,是否允许转rtmp + * @param enable_hls 是否允许转换hls + * @param enable_mp4 是否运行MP4录制 + */ +API_EXPORT void API_CALL mk_publish_auth_invoker_do(const mk_publish_auth_invoker ctx, + const char *err_msg, + int enable_rtxp, + int enable_hls, + int enable_mp4); + +///////////////////////////////////////////Broadcast::AuthInvoker///////////////////////////////////////////// +//Broadcast::AuthInvoker对象的C映射 +typedef void* mk_auth_invoker; + +/** + * 执行Broadcast::AuthInvoker + * @param err_msg 为空或null则代表鉴权成功 + */ +API_EXPORT void API_CALL mk_auth_invoker_do(const mk_auth_invoker ctx, const char *err_msg); + + +#ifdef __cplusplus +} +#endif +#endif //MK_EVENT_OBJECTS_H diff --git a/api/include/mediakit.h b/api/include/mediakit.h index 2cbcc442..5886f9a5 100755 --- a/api/include/mediakit.h +++ b/api/include/mediakit.h @@ -34,5 +34,6 @@ #include "flvrecorder.h" #include "player.h" #include "pusher.h" +#include "mk_event.h" #endif /* MK_API_H_ */ diff --git a/api/include/mk_event.h b/api/include/mk_event.h new file mode 100644 index 00000000..303af64f --- /dev/null +++ b/api/include/mk_event.h @@ -0,0 +1,150 @@ +/* + * 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. + */ + +#ifndef MK_EVENT_H +#define MK_EVENT_H + +#include "common.h" +#include "event_objects.h" +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct{ + /** + * 注册或反注册MediaSource事件广播 + * @param regist 注册为1,注销为0 + * @param sender 该MediaSource对象 + */ + typedef void (*on_mk_media_changed)(int regist,mk_media_source sender); + + /** + * 收到rtsp/rtmp推流事件广播,通过该事件控制推流鉴权 + * @see mk_publish_auth_invoker_do + * @param url_info 推流url相关信息 + * @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); + + /** + * 播放rtsp/rtmp/http-flv/hls事件广播,通过该事件控制播放鉴权 + * @see mk_auth_invoker_do + * @param url_info 播放url相关信息 + * @param invoker 执行invoker返回鉴权结果 + * @param sender 播放客户端相关信息 + */ + typedef void (*on_mk_media_play)(mk_media_info url_info,mk_auth_invoker invoker,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); + + /** + * 某个流无人消费时触发,目的为了实现无人观看时主动断开拉流等业务逻辑 + * @param sender 该MediaSource对象 + */ + typedef void (*on_mk_media_no_reader)(mk_media_source sender); + + /** + * 收到http api请求广播(包括GET/POST) + * @param parser http请求内容对象 + * @param invoker 执行该invoker返回http回复 + * @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); + + /** + * 在http文件服务器中,收到http访问文件或目录的广播,通过该事件控制访问http目录的权限 + * @param parser http请求内容对象 + * @param url_info 请求url相关信息 + * @param path 文件绝对路径 + * @param is_dir path是否为文件夹 + * @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); + + /** + * 在http文件服务器中,收到http访问文件或目录前的广播,通过该事件可以控制http url到文件路径的映射 + * 在该事件中通过自行覆盖path参数,可以做到譬如根据虚拟主机或者app选择不同http根目录的目的 + * @param parser http请求内容对象 + * @param url_info 请求url相关信息 + * @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); + + /** + * 该rtsp流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm + * @param url_info 请求rtsp url相关信息 + * @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); + + /** + * 请求认证用户密码事件,user_name为用户名,must_no_encrypt如果为true,则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败 + * 获取到密码后请调用invoker并输入对应类型的密码和密码类型,invoker执行时会匹配密码 + * @param url_info 请求rtsp url相关信息 + * @param realm rtsp认证realm + * @param user_name rtsp认证用户名 + * @param must_no_encrypt 如果为true,则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败 + * @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); + + /** + * 录制mp4分片文件成功后广播 + */ + typedef void (*on_mk_record_mp4)(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); + + /** + * 停止rtsp/rtmp/http-flv会话后流量汇报事件广播 + * @param url_info 播放url相关信息 + * @param total_bytes 耗费上下行总流量,单位字节数 + * @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); +} mk_events; + + +#ifdef __cplusplus +} +#endif +#endif //MK_EVENT_H + diff --git a/api/source/event_objects.cpp b/api/source/event_objects.cpp new file mode 100644 index 00000000..15f165db --- /dev/null +++ b/api/source/event_objects.cpp @@ -0,0 +1,333 @@ +/* + * 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 +#include "event_objects.h" +#include "Common/config.h" +#include "Record/MP4Recorder.h" +#include "Network/TcpSession.h" +#include "Http/HttpSession.h" +#include "Rtsp/RtspSession.h" +using namespace mediakit; + +///////////////////////////////////////////MP4Info///////////////////////////////////////////// +API_EXPORT uint64_t API_CALL mk_mp4_info_get_start_time(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->ui64StartedTime; +} + +API_EXPORT uint64_t API_CALL mk_mp4_info_get_time_len(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->ui64TimeLen; +} + +API_EXPORT uint64_t API_CALL mk_mp4_info_get_file_size(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->ui64FileSize; +} + +API_EXPORT const char* API_CALL mk_mp4_info_get_file_path(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->strFilePath.c_str(); +} + +API_EXPORT const char* API_CALL mk_mp4_info_get_file_name(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->strFileName.c_str(); +} + +API_EXPORT const char* API_CALL mk_mp4_info_get_folder(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->strFolder.c_str(); +} + +API_EXPORT const char* API_CALL mk_mp4_info_get_url(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->strUrl.c_str(); +} + +API_EXPORT const char* API_CALL mk_mp4_info_get_vhost(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->strVhost.c_str(); +} + +API_EXPORT const char* API_CALL mk_mp4_info_get_app(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->strAppName.c_str(); +} + +API_EXPORT const char* API_CALL mk_mp4_info_get_stream(const mk_mp4_info ctx){ + assert(ctx); + MP4Info *info = (MP4Info *)ctx; + return info->strStreamId.c_str(); +} + +///////////////////////////////////////////Parser///////////////////////////////////////////// +API_EXPORT const char* API_CALL mk_parser_get_method(const mk_parser ctx){ + assert(ctx); + Parser *parser = (Parser *)ctx; + return parser->Method().c_str(); +} +API_EXPORT const char* API_CALL mk_parser_get_url(const mk_parser ctx){ + assert(ctx); + Parser *parser = (Parser *)ctx; + return parser->Url().c_str(); +} +API_EXPORT const char* API_CALL mk_parser_get_full_url(const mk_parser ctx){ + assert(ctx); + Parser *parser = (Parser *)ctx; + return parser->FullUrl().c_str(); +} +API_EXPORT const char* API_CALL mk_parser_get_url_params(const mk_parser ctx){ + assert(ctx); + Parser *parser = (Parser *)ctx; + return parser->Params().c_str(); +} +API_EXPORT const char* API_CALL mk_parser_get_url_param(const mk_parser ctx,const char *key){ + assert(ctx && key); + Parser *parser = (Parser *)ctx; + return parser->getUrlArgs()[key].c_str(); +} +API_EXPORT const char* API_CALL mk_parser_get_tail(const mk_parser ctx){ + assert(ctx); + Parser *parser = (Parser *)ctx; + return parser->Tail().c_str(); +} +API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const char *key){ + assert(ctx && key); + Parser *parser = (Parser *)ctx; + return parser->getValues()[key].c_str(); +} +API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx){ + assert(ctx); + Parser *parser = (Parser *)ctx; + return parser->Content().c_str(); +} + +///////////////////////////////////////////MediaInfo///////////////////////////////////////////// +API_EXPORT const char* API_CALL mk_media_info_get_params(const mk_media_info ctx){ + assert(ctx); + MediaInfo *info = (MediaInfo *)ctx; + return info->_param_strs.c_str(); +} +API_EXPORT const char* API_CALL mk_media_info_get_param(const mk_media_info ctx,const char *key){ + assert(ctx && key); + MediaInfo *info = (MediaInfo *)ctx; + return info->_params[key].c_str(); +} +API_EXPORT const char* API_CALL mk_media_info_get_schema(const mk_media_info ctx){ + assert(ctx); + MediaInfo *info = (MediaInfo *)ctx; + return info->_schema.c_str(); +} +API_EXPORT const char* API_CALL mk_media_info_get_vhost(const mk_media_info ctx){ + assert(ctx); + MediaInfo *info = (MediaInfo *)ctx; + return info->_vhost.c_str(); +} +API_EXPORT const char* API_CALL mk_media_info_get_app(const mk_media_info ctx){ + assert(ctx); + MediaInfo *info = (MediaInfo *)ctx; + return info->_app.c_str(); +} +API_EXPORT const char* API_CALL mk_media_info_get_stream(const mk_media_info ctx){ + assert(ctx); + MediaInfo *info = (MediaInfo *)ctx; + return info->_streamid.c_str(); +} + +///////////////////////////////////////////MediaSource///////////////////////////////////////////// +API_EXPORT const char* API_CALL mk_media_source_get_schema(const mk_media_source ctx){ + assert(ctx); + MediaSource *src = (MediaSource *)ctx; + return src->getSchema().c_str(); +} +API_EXPORT const char* API_CALL mk_media_source_get_vhost(const mk_media_source ctx){ + assert(ctx); + MediaSource *src = (MediaSource *)ctx; + return src->getVhost().c_str(); +} +API_EXPORT const char* API_CALL mk_media_source_get_app(const mk_media_source ctx){ + assert(ctx); + MediaSource *src = (MediaSource *)ctx; + return src->getApp().c_str(); +} +API_EXPORT const char* API_CALL mk_media_source_get_stream(const mk_media_source ctx){ + assert(ctx); + MediaSource *src = (MediaSource *)ctx; + return src->getId().c_str(); +} +API_EXPORT int API_CALL mk_media_source_get_reader_count(const mk_media_source ctx){ + assert(ctx); + MediaSource *src = (MediaSource *)ctx; + return src->readerCount(); +} +API_EXPORT int API_CALL mk_media_source_close(const mk_media_source ctx,int force){ + assert(ctx); + MediaSource *src = (MediaSource *)ctx; + return src->close(force); +} +API_EXPORT int API_CALL mk_media_source_seek_to(const mk_media_source ctx,uint32_t stamp){ + assert(ctx); + MediaSource *src = (MediaSource *)ctx; + return src->seekTo(stamp); +} + +API_EXPORT void API_CALL mk_media_source_find(const char *schema, + const char *vhost, + const char *app, + const char *stream, + void *user_data, + on_mk_media_source_find_cb cb) { + assert(schema && vhost && app && stream && cb); + auto src = MediaSource::find(schema, vhost, app, stream); + cb(user_data, src.get()); +} + +API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb){ + assert(cb); + MediaSource::for_each_media([&](const MediaSource::Ptr &src){ + cb(user_data,src.get()); + }); +} + +///////////////////////////////////////////TcpSession///////////////////////////////////////////// +API_EXPORT void API_CALL mk_tcp_session_shutdown(const mk_tcp_session ctx,int err,const char *err_msg){ + assert(ctx); + TcpSession *session = (TcpSession *)ctx; + session->safeShutdown(SockException((ErrCode)err,err_msg)); +} +API_EXPORT const char* API_CALL mk_tcp_session_peer_ip(const mk_tcp_session ctx){ + assert(ctx); + TcpSession *session = (TcpSession *)ctx; + return session->get_peer_ip().c_str(); +} +API_EXPORT const char* API_CALL mk_tcp_session_local_ip(const mk_tcp_session ctx){ + assert(ctx); + TcpSession *session = (TcpSession *)ctx; + return session->get_local_ip().c_str(); +} +API_EXPORT uint16_t API_CALL mk_tcp_session_peer_port(const mk_tcp_session ctx){ + assert(ctx); + TcpSession *session = (TcpSession *)ctx; + return session->get_peer_port(); +} +API_EXPORT uint16_t API_CALL mk_tcp_session_local_port(const mk_tcp_session ctx){ + assert(ctx); + TcpSession *session = (TcpSession *)ctx; + return session->get_local_port(); +} + +///////////////////////////////////////////HttpResponseInvoker///////////////////////////////////////////// +static StrCaseMap get_http_header( const char *response_header[]){ + StrCaseMap header; + for (int i = 0; response_header[i] != NULL;) { + auto key = response_header[i]; + auto value = response_header[i + 1]; + if (key && value) { + i += 2; + header[key] = value; + continue; + } + break; + } + return std::move(header); +} +API_EXPORT void API_CALL mk_http_response_invoker_do(const mk_http_response_invoker ctx, + const char *response_code, + const char *response_header[], + const char *response_content){ + assert(ctx && response_code && response_header && response_content); + auto header = get_http_header(response_header); + HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx; + (*invoker)(response_code,header,response_content); +} + +API_EXPORT void API_CALL mk_http_response_invoker_do_file(const mk_http_response_invoker ctx, + const mk_parser request_parser, + const char *response_header[], + const char *response_file_path){ + assert(ctx && request_parser && response_header && response_file_path); + auto header = get_http_header(response_header); + HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx; + (*invoker).responseFile(((Parser*)(request_parser))->getValues(),header,response_file_path); +} + +///////////////////////////////////////////HttpAccessPathInvoker///////////////////////////////////////////// +API_EXPORT void API_CALL mk_http_access_path_invoker_do(const mk_http_access_path_invoker ctx, + const char *err_msg, + const char *access_path, + int cookie_life_second){ + assert(ctx); + HttpSession::HttpAccessPathInvoker *invoer = (HttpSession::HttpAccessPathInvoker *)ctx; + (*invoer)(err_msg ? err_msg : "", + access_path? access_path : "", + cookie_life_second); +} +///////////////////////////////////////////RtspSession::onGetRealm///////////////////////////////////////////// +API_EXPORT void API_CALL mk_rtsp_get_realm_invoker_do(const mk_rtsp_get_realm_invoker ctx, + const char *realm){ + assert(ctx); + RtspSession::onGetRealm *invoker = (RtspSession::onGetRealm *)ctx; + (*invoker)(realm ? realm : ""); +} + +///////////////////////////////////////////RtspSession::onAuth///////////////////////////////////////////// +API_EXPORT void API_CALL mk_rtsp_auth_invoker_do(const mk_rtsp_auth_invoker ctx, + int encrypted, + const char *pwd_or_md5){ + assert(ctx); + RtspSession::onAuth *invoker = (RtspSession::onAuth *)ctx; + (*invoker)(encrypted, pwd_or_md5 ? pwd_or_md5 : ""); +} + +///////////////////////////////////////////Broadcast::PublishAuthInvoker///////////////////////////////////////////// +API_EXPORT void API_CALL mk_publish_auth_invoker_do(const mk_publish_auth_invoker ctx, + const char *err_msg, + int enable_rtxp, + int enable_hls, + int enable_mp4){ + assert(ctx); + Broadcast::PublishAuthInvoker *invoker = (Broadcast::PublishAuthInvoker *)ctx; + (*invoker)(err_msg ? err_msg : "", enable_rtxp, enable_hls, enable_mp4); +} + +///////////////////////////////////////////Broadcast::AuthInvoker///////////////////////////////////////////// +API_EXPORT void API_CALL mk_auth_invoker_do(const mk_auth_invoker ctx, const char *err_msg){ + assert(ctx); + Broadcast::AuthInvoker *invoker = (Broadcast::AuthInvoker *)ctx; + (*invoker)(err_msg ? err_msg : ""); +} + diff --git a/api/source/mk_event.cpp b/api/source/mk_event.cpp new file mode 100644 index 00000000..3f124b15 --- /dev/null +++ b/api/source/mk_event.cpp @@ -0,0 +1,27 @@ +/* + * 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" diff --git a/src/Record/MP4Recorder.h b/src/Record/MP4Recorder.h index 3204fb51..838f2507 100644 --- a/src/Record/MP4Recorder.h +++ b/src/Record/MP4Recorder.h @@ -27,8 +27,6 @@ #ifndef MP4MAKER_H_ #define MP4MAKER_H_ -#ifdef ENABLE_MP4RECORD - #include #include #include "Player/PlayerBase.h" @@ -56,6 +54,7 @@ public: string strVhost;//vhost }; +#ifdef ENABLE_MP4RECORD class MP4Recorder : public MediaSinkInterface{ public: typedef std::shared_ptr Ptr; @@ -95,8 +94,8 @@ private: list _tracks; }; -} /* namespace mediakit */ - #endif ///ENABLE_MP4RECORD +} /* namespace mediakit */ + #endif /* MP4MAKER_H_ */