From 4e0919d13b1e6250f7b9e1c6d43103e20f2a1123 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 24 Dec 2019 16:02:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9B=E5=AE=8C=E6=88=90=E7=9A=84c?= =?UTF-8?q?=20api=E4=BA=8B=E4=BB=B6=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- api/include/events.h | 93 ++++++----- api/tests/server.cpp | 387 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 429 insertions(+), 53 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index 4d88a395..77c2a6d9 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit 4d88a395a25e9265c579aa930acdf60796bd4e4c +Subproject commit 77c2a6d9f94ab6e7e40bc1f223eb206d2542cb72 diff --git a/api/include/events.h b/api/include/events.h index 32ee86a2..26a9f5c2 100644 --- a/api/include/events.h +++ b/api/include/events.h @@ -29,19 +29,20 @@ #include "common.h" #include "events_objects.h" + #ifdef __cplusplus extern "C" { #endif -typedef struct{ +typedef struct { /** * 注册或反注册MediaSource事件广播 * @param regist 注册为1,注销为0 * @param sender 该MediaSource对象 */ - void (*on_mk_media_changed)(int regist, - const mk_media_source sender); - + void (API_CALL *on_mk_media_changed)(int regist, + const mk_media_source sender); + /** * 收到rtsp/rtmp推流事件广播,通过该事件控制推流鉴权 * @see mk_publish_auth_invoker_do @@ -49,9 +50,9 @@ typedef struct{ * @param invoker 执行invoker返回鉴权结果 * @param sender 该tcp客户端相关信息 */ - void (*on_mk_media_publish)(const mk_media_info url_info, - const mk_publish_auth_invoker invoker, - const mk_tcp_session sender); + void (API_CALL *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事件广播,通过该事件控制播放鉴权 @@ -60,23 +61,23 @@ typedef struct{ * @param invoker 执行invoker返回鉴权结果 * @param sender 播放客户端相关信息 */ - void (*on_mk_media_play)(const mk_media_info url_info, - const mk_auth_invoker invoker, - const mk_tcp_session sender); + void (API_CALL *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 播放客户端相关信息 */ - void (*on_mk_media_not_found)(const mk_media_info url_info, - const mk_tcp_session sender); + void (API_CALL *on_mk_media_not_found)(const mk_media_info url_info, + const mk_tcp_session sender); /** * 某个流无人消费时触发,目的为了实现无人观看时主动断开拉流等业务逻辑 * @param sender 该MediaSource对象 */ - void (*on_mk_media_no_reader)(const mk_media_source sender); + void (API_CALL *on_mk_media_no_reader)(const mk_media_source sender); /** * 收到http api请求广播(包括GET/POST) @@ -85,10 +86,10 @@ typedef struct{ * @param consumed 置1则说明我们要处理该事件 * @param sender http客户端相关信息 */ - void (*on_mk_http_request)(const mk_parser parser, - const mk_http_response_invoker invoker, - int *consumed, - const mk_tcp_session sender); + void (API_CALL *on_mk_http_request)(const mk_parser parser, + const mk_http_response_invoker invoker, + int *consumed, + const mk_tcp_session sender); /** * 在http文件服务器中,收到http访问文件或目录的广播,通过该事件控制访问http目录的权限 @@ -99,12 +100,12 @@ typedef struct{ * @param invoker 执行invoker返回本次访问文件的结果 * @param sender http客户端相关信息 */ - 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); + void (API_CALL *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到文件路径的映射 @@ -114,10 +115,10 @@ typedef struct{ * @param path 文件绝对路径,覆盖之可以重定向到其他文件 * @param sender http客户端相关信息 */ - void (*on_mk_http_before_access)(const mk_parser parser, - const mk_media_info url_info, - char *path, - const mk_tcp_session sender); + void (API_CALL *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 @@ -125,9 +126,9 @@ typedef struct{ * @param invoker 执行invoker返回是否需要rtsp专属认证 * @param sender rtsp客户端相关信息 */ - void (*on_mk_rtsp_get_realm)(const mk_media_info url_info, - const mk_rtsp_get_realm_invoker invoker, - const mk_tcp_session sender); + void (API_CALL *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认证方式),否则会导致认证失败 @@ -139,25 +140,25 @@ typedef struct{ * @param invoker 执行invoker返回rtsp专属认证的密码 * @param sender rtsp客户端信息 */ - 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); + void (API_CALL *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分片文件成功后广播 */ - void (*on_mk_record_mp4)(const mk_mp4_info mp4); + void (API_CALL *on_mk_record_mp4)(const mk_mp4_info mp4); /** * shell登录鉴权 */ - void (*on_mk_shell_login)(const char *user_name, - const char *passwd, - const mk_auth_invoker invoker, - const mk_tcp_session sender); + void (API_CALL *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会话后流量汇报事件广播 @@ -166,11 +167,11 @@ typedef struct{ * @param total_seconds 本次tcp会话时长,单位秒 * @param is_player 客户端是否为播放器 */ - 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); + void (API_CALL *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; diff --git a/api/tests/server.cpp b/api/tests/server.cpp index 514f4941..38acf606 100644 --- a/api/tests/server.cpp +++ b/api/tests/server.cpp @@ -27,22 +27,397 @@ #include #include #include "mediakit.h" + #ifdef _WIN32 #include "windows.h" #else + #include "unistd.h" + #endif using namespace std; -int main(int argc,char *argv[]){ - mk_env_init1(0,0,1, (string(argv[0]) + ".ini").data(),0, nullptr, nullptr); - mk_http_server_start(80,false); - mk_rtsp_server_start(554,false); - mk_rtmp_server_start(1935,false); + +#define LOG_LEV 4 + +/** + * 注册或反注册MediaSource事件广播 + * @param regist 注册为1,注销为0 + * @param sender 该MediaSource对象 + */ +void API_CALL on_mk_media_changed(int regist, + const mk_media_source sender) { + log_printf(LOG_LEV,"%d %s/%s/%s/%s",(int)regist, + mk_media_source_get_schema(sender), + mk_media_source_get_vhost(sender), + mk_media_source_get_app(sender), + mk_media_source_get_stream(sender)); + +} + +/** + * 收到rtsp/rtmp推流事件广播,通过该事件控制推流鉴权 + * @see mk_publish_auth_invoker_do + * @param url_info 推流url相关信息 + * @param invoker 执行invoker返回鉴权结果 + * @param sender 该tcp客户端相关信息 + */ +void API_CALL on_mk_media_publish(const mk_media_info url_info, + const mk_publish_auth_invoker invoker, + const mk_tcp_session sender) { + log_printf(LOG_LEV, + "client info, local: %s:%d, peer: %s:%d\n" + "%s/%s/%s/%s, url params: %s", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + mk_media_info_get_schema(url_info), + mk_media_info_get_vhost(url_info), + mk_media_info_get_app(url_info), + mk_media_info_get_stream(url_info), + mk_media_info_get_params(url_info)); + + //允许推流,并且允许转rtxp/hls/mp4 + mk_publish_auth_invoker_do(invoker, nullptr, 1, 1, true); +} + +/** + * 播放rtsp/rtmp/http-flv/hls事件广播,通过该事件控制播放鉴权 + * @see mk_auth_invoker_do + * @param url_info 播放url相关信息 + * @param invoker 执行invoker返回鉴权结果 + * @param sender 播放客户端相关信息 + */ +void API_CALL on_mk_media_play(const mk_media_info url_info, + const mk_auth_invoker invoker, + const mk_tcp_session sender) { + + log_printf(LOG_LEV, + "client info, local: %s:%d, peer: %s:%d\n" + "%s/%s/%s/%s, url params: %s", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + mk_media_info_get_schema(url_info), + mk_media_info_get_vhost(url_info), + mk_media_info_get_app(url_info), + mk_media_info_get_stream(url_info), + mk_media_info_get_params(url_info)); + + //允许播放 + mk_auth_invoker_do(invoker, nullptr); +} + +/** + * 未找到流后会广播该事件,请在监听该事件后去拉流或其他方式产生流,这样就能按需拉流了 + * @param url_info 播放url相关信息 + * @param sender 播放客户端相关信息 + */ +void API_CALL on_mk_media_not_found(const mk_media_info url_info, + const mk_tcp_session sender) { + log_printf(LOG_LEV, + "client info, local: %s:%d, peer: %s:%d\n" + "%s/%s/%s/%s, url params: %s", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + mk_media_info_get_schema(url_info), + mk_media_info_get_vhost(url_info), + mk_media_info_get_app(url_info), + mk_media_info_get_stream(url_info), + mk_media_info_get_params(url_info)); +} + +/** + * 某个流无人消费时触发,目的为了实现无人观看时主动断开拉流等业务逻辑 + * @param sender 该MediaSource对象 + */ +void API_CALL on_mk_media_no_reader(const mk_media_source sender) { + log_printf(LOG_LEV, + "%s/%s/%s/%s", + mk_media_source_get_schema(sender), + mk_media_source_get_vhost(sender), + mk_media_source_get_app(sender), + mk_media_source_get_stream(sender)); +} + +/** + * 收到http api请求广播(包括GET/POST) + * @param parser http请求内容对象 + * @param invoker 执行该invoker返回http回复 + * @param consumed 置1则说明我们要处理该事件 + * @param sender http客户端相关信息 + */ +//测试url : http://127.0.0.1/api/test +void API_CALL on_mk_http_request(const mk_parser parser, + const mk_http_response_invoker invoker, + int *consumed, + const mk_tcp_session sender) { + + log_printf(LOG_LEV, + "client info, local: %s:%d, peer: %s:%d\n" + "%s %s?%s %s\n" + "User-Agent: %s\n" + "%s", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + mk_parser_get_method(parser), + mk_parser_get_url(parser), + mk_parser_get_url_params(parser), + mk_parser_get_tail(parser), + mk_parser_get_header(parser, "User-Agent"), + mk_parser_get_content(parser)); + + const char *url = mk_parser_get_url(parser); + if(strcasecmp(url,"/api/test") != 0){ + *consumed = false; + return; + } + + //只拦截api: /api/test + *consumed = true; + const char *response_header[] = {"Content-Type","text/html",NULL}; + const char *content = + "" + "" + "hello world" + "" + "" + "

hello world


" + "
""ZLMediaKit-4.0
" + "" + ""; + mk_http_response_invoker_do(invoker,"200 OK",response_header,content); +} + +/** + * 在http文件服务器中,收到http访问文件或目录的广播,通过该事件控制访问http目录的权限 + * @param parser http请求内容对象 + * @param url_info 请求url相关信息 + * @param path 文件绝对路径 + * @param is_dir path是否为文件夹 + * @param invoker 执行invoker返回本次访问文件的结果 + * @param sender http客户端相关信息 + */ +void API_CALL 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) { + + log_printf(LOG_LEV, + "client info, local: %s:%d, peer: %s:%d, path: %s ,is_dir: %d\n" + "%s %s?%s %s\n" + "User-Agent: %s\n" + "%s", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + path,(int)is_dir, + mk_parser_get_method(parser), + mk_parser_get_url(parser), + mk_parser_get_url_params(parser), + mk_parser_get_tail(parser), + mk_parser_get_header(parser,"User-Agent"), + mk_parser_get_content(parser)); + + //有访问权限,每次访问文件都需要鉴权 + mk_http_access_path_invoker_do(invoker, nullptr, nullptr, 0); +} + +/** + * 在http文件服务器中,收到http访问文件或目录前的广播,通过该事件可以控制http url到文件路径的映射 + * 在该事件中通过自行覆盖path参数,可以做到譬如根据虚拟主机或者app选择不同http根目录的目的 + * @param parser http请求内容对象 + * @param url_info 请求url相关信息 + * @param path 文件绝对路径,覆盖之可以重定向到其他文件 + * @param sender http客户端相关信息 + */ +void API_CALL on_mk_http_before_access(const mk_parser parser, + const mk_media_info url_info, + char *path, + const mk_tcp_session sender) { + log_printf(LOG_LEV, + "client info, local: %s:%d, peer: %s:%d, path: %s\n" + "%s %s?%s %s\n" + "User-Agent: %s\n" + "%s", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + path, + mk_parser_get_method(parser), + mk_parser_get_url(parser), + mk_parser_get_url_params(parser), + mk_parser_get_tail(parser), + mk_parser_get_header(parser, "User-Agent"), + mk_parser_get_content(parser)); + //覆盖path的值可以重定向文件 +} + +/** + * 该rtsp流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm + * @param url_info 请求rtsp url相关信息 + * @param invoker 执行invoker返回是否需要rtsp专属认证 + * @param sender rtsp客户端相关信息 + */ +void API_CALL on_mk_rtsp_get_realm(const mk_media_info url_info, + const mk_rtsp_get_realm_invoker invoker, + const mk_tcp_session sender) { + log_printf(LOG_LEV, + "client info, local: %s:%d, peer: %s:%d\n" + "%s/%s/%s/%s, url params: %s", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + mk_media_info_get_schema(url_info), + mk_media_info_get_vhost(url_info), + mk_media_info_get_app(url_info), + mk_media_info_get_stream(url_info), + mk_media_info_get_params(url_info)); + + //rtsp播放默认鉴权 + mk_rtsp_get_realm_invoker_do(invoker, "zlmediakit"); +} + +/** + * 请求认证用户密码事件,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客户端信息 + */ +void API_CALL 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) { + + log_printf(LOG_LEV, + "client info, local: %s:%d, peer: %s:%d\n" + "%s/%s/%s/%s, url params: %s\n" + "realm: %s, user_name: %s, must_no_encrypt: %d", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + mk_media_info_get_schema(url_info), + mk_media_info_get_vhost(url_info), + mk_media_info_get_app(url_info), + mk_media_info_get_stream(url_info), + mk_media_info_get_params(url_info), + realm,user_name,(int)must_no_encrypt); + + //rtsp播放用户名跟密码一致 + mk_rtsp_auth_invoker_do(invoker,false,user_name); +} + +/** + * 录制mp4分片文件成功后广播 + */ +void API_CALL on_mk_record_mp4(const mk_mp4_info mp4) { + log_printf(LOG_LEV, + "\nstart_time: %d\n" + "time_len: %d\n" + "file_size: %d\n" + "file_path: %s\n" + "file_name: %s\n" + "folder: %s\n" + "url: %s\n" + "vhost: %s\n" + "app: %s\n" + "stream: %s\n", + mk_mp4_info_get_start_time(mp4), + mk_mp4_info_get_time_len(mp4), + mk_mp4_info_get_file_size(mp4), + mk_mp4_info_get_file_path(mp4), + mk_mp4_info_get_file_name(mp4), + mk_mp4_info_get_folder(mp4), + mk_mp4_info_get_url(mp4), + mk_mp4_info_get_vhost(mp4), + mk_mp4_info_get_app(mp4), + mk_mp4_info_get_stream(mp4)); +} + +/** + * shell登录鉴权 + */ +void API_CALL on_mk_shell_login(const char *user_name, + const char *passwd, + const mk_auth_invoker invoker, + const mk_tcp_session sender) { + log_printf(LOG_LEV,"client info, local: %s:%d, peer: %s:%d\n" + "user_name: %s, passwd: %s", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + user_name, passwd); + //允许登录shell + mk_auth_invoker_do(invoker, nullptr); +} + +/** + * 停止rtsp/rtmp/http-flv会话后流量汇报事件广播 + * @param url_info 播放url相关信息 + * @param total_bytes 耗费上下行总流量,单位字节数 + * @param total_seconds 本次tcp会话时长,单位秒 + * @param is_player 客户端是否为播放器 + */ +void API_CALL 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) { + log_printf(LOG_LEV,"client info, local: %s:%d, peer: %s:%d\n" + "total_bytes: %d, total_seconds: %d, is_player: %d", + mk_tcp_session_local_ip(sender), + mk_tcp_session_local_port(sender), + mk_tcp_session_peer_ip(sender), + mk_tcp_session_peer_port(sender), + (int)total_bytes, (int)total_seconds, (int)is_player); +} + +int main(int argc, char *argv[]) { + mk_env_init1(0, 0, 1, (string(argv[0]) + ".ini").data(), 0, nullptr, nullptr); + mk_http_server_start(80, false); + mk_rtsp_server_start(554, false); + mk_rtmp_server_start(1935, false); mk_rtp_server_start(10000); + + mk_events events = { + .on_mk_media_changed = on_mk_media_changed, + .on_mk_media_publish = on_mk_media_publish, + .on_mk_media_play = on_mk_media_play, + .on_mk_media_not_found = on_mk_media_not_found, + .on_mk_media_no_reader = on_mk_media_no_reader, + .on_mk_http_request = on_mk_http_request, + .on_mk_http_access = on_mk_http_access, + .on_mk_http_before_access = on_mk_http_before_access, + .on_mk_rtsp_get_realm = on_mk_rtsp_get_realm, + .on_mk_rtsp_auth = on_mk_rtsp_auth, + .on_mk_record_mp4 = on_mk_record_mp4, + .on_mk_shell_login = on_mk_shell_login, + .on_mk_flow_report = on_mk_flow_report + }; + mk_events_listen(&events); + static bool flag = true; signal(SIGINT, [](int) { flag = false; });// 设置退出信号 - while (flag){ + while (flag) { #ifdef _WIN32 Sleep(1000); #else