提供完成的c api事件测试代码

This commit is contained in:
xiongziliang 2019-12-24 16:02:41 +08:00
parent 824eeee6f5
commit 4e0919d13b
3 changed files with 429 additions and 53 deletions

@ -1 +1 @@
Subproject commit 4d88a395a25e9265c579aa930acdf60796bd4e4c Subproject commit 77c2a6d9f94ab6e7e40bc1f223eb206d2542cb72

View File

@ -29,6 +29,7 @@
#include "common.h" #include "common.h"
#include "events_objects.h" #include "events_objects.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -39,7 +40,7 @@ typedef struct{
* @param regist 10 * @param regist 10
* @param sender MediaSource对象 * @param sender MediaSource对象
*/ */
void (*on_mk_media_changed)(int regist, void (API_CALL *on_mk_media_changed)(int regist,
const mk_media_source sender); const mk_media_source sender);
/** /**
@ -49,7 +50,7 @@ typedef struct{
* @param invoker invoker返回鉴权结果 * @param invoker invoker返回鉴权结果
* @param sender tcp客户端相关信息 * @param sender tcp客户端相关信息
*/ */
void (*on_mk_media_publish)(const mk_media_info url_info, void (API_CALL *on_mk_media_publish)(const mk_media_info url_info,
const mk_publish_auth_invoker invoker, const mk_publish_auth_invoker invoker,
const mk_tcp_session sender); const mk_tcp_session sender);
@ -60,7 +61,7 @@ typedef struct{
* @param invoker invoker返回鉴权结果 * @param invoker invoker返回鉴权结果
* @param sender * @param sender
*/ */
void (*on_mk_media_play)(const mk_media_info url_info, void (API_CALL *on_mk_media_play)(const mk_media_info url_info,
const mk_auth_invoker invoker, const mk_auth_invoker invoker,
const mk_tcp_session sender); const mk_tcp_session sender);
@ -69,14 +70,14 @@ typedef struct{
* @param url_info url相关信息 * @param url_info url相关信息
* @param sender * @param sender
*/ */
void (*on_mk_media_not_found)(const mk_media_info url_info, void (API_CALL *on_mk_media_not_found)(const mk_media_info url_info,
const mk_tcp_session sender); const mk_tcp_session sender);
/** /**
* *
* @param sender MediaSource对象 * @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) * http api请求广播(GET/POST)
@ -85,7 +86,7 @@ typedef struct{
* @param consumed 1 * @param consumed 1
* @param sender http客户端相关信息 * @param sender http客户端相关信息
*/ */
void (*on_mk_http_request)(const mk_parser parser, void (API_CALL *on_mk_http_request)(const mk_parser parser,
const mk_http_response_invoker invoker, const mk_http_response_invoker invoker,
int *consumed, int *consumed,
const mk_tcp_session sender); const mk_tcp_session sender);
@ -99,7 +100,7 @@ typedef struct{
* @param invoker invoker返回本次访问文件的结果 * @param invoker invoker返回本次访问文件的结果
* @param sender http客户端相关信息 * @param sender http客户端相关信息
*/ */
void (*on_mk_http_access)(const mk_parser parser, void (API_CALL *on_mk_http_access)(const mk_parser parser,
const mk_media_info url_info, const mk_media_info url_info,
const char *path, const char *path,
int is_dir, int is_dir,
@ -114,7 +115,7 @@ typedef struct{
* @param path , * @param path ,
* @param sender http客户端相关信息 * @param sender http客户端相关信息
*/ */
void (*on_mk_http_before_access)(const mk_parser parser, void (API_CALL *on_mk_http_before_access)(const mk_parser parser,
const mk_media_info url_info, const mk_media_info url_info,
char *path, char *path,
const mk_tcp_session sender); const mk_tcp_session sender);
@ -125,7 +126,7 @@ typedef struct{
* @param invoker invoker返回是否需要rtsp专属认证 * @param invoker invoker返回是否需要rtsp专属认证
* @param sender rtsp客户端相关信息 * @param sender rtsp客户端相关信息
*/ */
void (*on_mk_rtsp_get_realm)(const mk_media_info url_info, void (API_CALL *on_mk_rtsp_get_realm)(const mk_media_info url_info,
const mk_rtsp_get_realm_invoker invoker, const mk_rtsp_get_realm_invoker invoker,
const mk_tcp_session sender); const mk_tcp_session sender);
@ -139,7 +140,7 @@ typedef struct{
* @param invoker invoker返回rtsp专属认证的密码 * @param invoker invoker返回rtsp专属认证的密码
* @param sender rtsp客户端信息 * @param sender rtsp客户端信息
*/ */
void (*on_mk_rtsp_auth)(const mk_media_info url_info, void (API_CALL *on_mk_rtsp_auth)(const mk_media_info url_info,
const char *realm, const char *realm,
const char *user_name, const char *user_name,
int must_no_encrypt, int must_no_encrypt,
@ -149,12 +150,12 @@ typedef struct{
/** /**
* mp4分片文件成功后广播 * mp4分片文件成功后广播
*/ */
void (*on_mk_record_mp4)(const mk_mp4_info mp4); void (API_CALL *on_mk_record_mp4)(const mk_mp4_info mp4);
/** /**
* shell登录鉴权 * shell登录鉴权
*/ */
void (*on_mk_shell_login)(const char *user_name, void (API_CALL *on_mk_shell_login)(const char *user_name,
const char *passwd, const char *passwd,
const mk_auth_invoker invoker, const mk_auth_invoker invoker,
const mk_tcp_session sender); const mk_tcp_session sender);
@ -166,7 +167,7 @@ typedef struct{
* @param total_seconds tcp会话时长 * @param total_seconds tcp会话时长
* @param is_player * @param is_player
*/ */
void (*on_mk_flow_report)(const mk_media_info url_info, void (API_CALL *on_mk_flow_report)(const mk_media_info url_info,
uint64_t total_bytes, uint64_t total_bytes,
uint64_t total_seconds, uint64_t total_seconds,
int is_player, int is_player,

View File

@ -27,19 +27,394 @@
#include <csignal> #include <csignal>
#include <string> #include <string>
#include "mediakit.h" #include "mediakit.h"
#ifdef _WIN32 #ifdef _WIN32
#include "windows.h" #include "windows.h"
#else #else
#include "unistd.h" #include "unistd.h"
#endif #endif
using namespace std; using namespace std;
#define LOG_LEV 4
/**
* MediaSource事件广播
* @param regist 10
* @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 =
"<html>"
"<head>"
"<title>hello world</title>"
"</head>"
"<body bgcolor=\"white\">"
"<center><h1>hello world</h1></center><hr>"
"<center>""ZLMediaKit-4.0</center>"
"</body>"
"</html>";
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[]) { int main(int argc, char *argv[]) {
mk_env_init1(0, 0, 1, (string(argv[0]) + ".ini").data(), 0, nullptr, nullptr); mk_env_init1(0, 0, 1, (string(argv[0]) + ".ini").data(), 0, nullptr, nullptr);
mk_http_server_start(80, false); mk_http_server_start(80, false);
mk_rtsp_server_start(554, false); mk_rtsp_server_start(554, false);
mk_rtmp_server_start(1935, false); mk_rtmp_server_start(1935, false);
mk_rtp_server_start(10000); 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; static bool flag = true;
signal(SIGINT, [](int) { flag = false; });// 设置退出信号 signal(SIGINT, [](int) { flag = false; });// 设置退出信号
while (flag) { while (flag) {