mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-16 13:05:59 +08:00
全面更新整理c sdk
This commit is contained in:
6
api/tests/CMakeLists.sample
Normal file
6
api/tests/CMakeLists.sample
Normal file
@@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.1.3)
|
||||
project(player)
|
||||
include_directories("../../include")
|
||||
link_directories("../../so")
|
||||
add_executable(player "./player_opencv.c")
|
||||
target_link_libraries(player mk_api ssl crypto avcodec swscale swresample avutil x264 openh264 x265)
|
||||
@@ -22,14 +22,17 @@ static void s_on_exit(int sig) {
|
||||
exit_flag = 1;
|
||||
}
|
||||
|
||||
static void on_h264_frame(void *user_data, mk_h264_splitter splitter, const char *frame, int size) {
|
||||
static void on_h264_frame(void *user_data, mk_h264_splitter splitter, const char *data, int size) {
|
||||
#ifdef _WIN32
|
||||
Sleep(40);
|
||||
#else
|
||||
usleep(40 * 1000);
|
||||
#endif
|
||||
mk_media media = (mk_media) user_data;
|
||||
mk_media_input_h264(media, frame, size, 0, 0);
|
||||
static int dts = 0;
|
||||
mk_frame frame = mk_frame_create(MKCodecH264, dts, dts, data, size, NULL, NULL);
|
||||
dts += 40;
|
||||
mk_media_input_frame((mk_media) user_data, frame);
|
||||
mk_frame_unref(frame);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@@ -60,7 +63,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
mk_media media = mk_media_create("__defaultVhost__", "live", "test", 0, 0, 0);
|
||||
//h264的codec
|
||||
mk_media_init_video(media, 0, 0, 0, 0);
|
||||
mk_media_init_video(media, 0, 0, 0, 0, 2 * 104 * 1024);
|
||||
mk_media_init_complete(media);
|
||||
|
||||
//创建h264分帧器
|
||||
|
||||
61
api/tests/httpclient.c
Normal file
61
api/tests/httpclient.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
||||
*
|
||||
* Use of this source code is governed by MIT license that can be found in the
|
||||
* LICENSE file in the root of the source tree. All contributing project authors
|
||||
* may be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "mk_mediakit.h"
|
||||
|
||||
typedef struct {
|
||||
mk_sem sem;
|
||||
mk_http_requester requester;
|
||||
} Context;
|
||||
|
||||
static API_CALL void on_requester_complete(void *user_data, int code, const char *err_msg){
|
||||
Context *ctx = (Context *)user_data;
|
||||
log_debug("code: %d %s", code, err_msg);
|
||||
size_t res_len = 0;
|
||||
log_debug("response: %s %s", mk_http_requester_get_response_status(ctx->requester),
|
||||
mk_http_requester_get_response_body(ctx->requester, &res_len));
|
||||
mk_sem_post(ctx->sem, 1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
mk_config config = {
|
||||
.ini = NULL,
|
||||
.ini_is_path = 0,
|
||||
.log_level = 0,
|
||||
.log_mask = LOG_CONSOLE,
|
||||
.ssl = NULL,
|
||||
.ssl_is_path = 1,
|
||||
.ssl_pwd = NULL,
|
||||
.thread_num = 0
|
||||
};
|
||||
mk_env_init(&config);
|
||||
|
||||
mk_http_requester requester = mk_http_requester_create();
|
||||
mk_http_requester_set_method(requester, "POST");
|
||||
|
||||
mk_http_body body = mk_http_body_from_string("tn=monline_7_dg&ie=utf-8&wd=test", 0);
|
||||
mk_http_requester_set_body(requester, body);
|
||||
mk_http_body_release(body);
|
||||
|
||||
mk_sem sem = mk_sem_create();
|
||||
|
||||
Context ctx = {.requester = requester, .sem = sem};
|
||||
|
||||
mk_http_requester_set_cb(requester, on_requester_complete, &ctx);
|
||||
mk_http_requester_start(requester, "http://www.baidu.com/baidu", 10);
|
||||
|
||||
//等待http请求完毕
|
||||
mk_sem_wait(sem);
|
||||
|
||||
mk_sem_release(sem);
|
||||
mk_http_requester_release(requester);
|
||||
return 0;
|
||||
}
|
||||
108
api/tests/player_opencv.c
Normal file
108
api/tests/player_opencv.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
||||
*
|
||||
* Use of this source code is governed by MIT license that can be found in the
|
||||
* LICENSE file in the root of the source tree. All contributing project authors
|
||||
* may be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "mk_mediakit.h"
|
||||
|
||||
typedef struct {
|
||||
mk_player player;
|
||||
mk_decoder video_decoder;
|
||||
mk_swscale swscale;
|
||||
} Context;
|
||||
|
||||
void API_CALL on_track_frame_out(void *user_data, mk_frame frame) {
|
||||
Context *ctx = (Context *) user_data;
|
||||
mk_decoder_decode(ctx->video_decoder, frame, 1, 1);
|
||||
}
|
||||
|
||||
void API_CALL on_frame_decode(void *user_data, mk_frame_pix frame) {
|
||||
Context *ctx = (Context *) user_data;
|
||||
int w = mk_get_av_frame_width(mk_frame_pix_get_av_frame(frame));
|
||||
int h = mk_get_av_frame_height(mk_frame_pix_get_av_frame(frame));
|
||||
|
||||
#if 1
|
||||
uint8_t *brg24 = malloc(w * h * 3);
|
||||
mk_swscale_input_frame(ctx->swscale, frame, brg24);
|
||||
free(brg24);
|
||||
#else
|
||||
//todo 此处转换为opencv对象
|
||||
cv::Mat *mat = new cv::Mat();
|
||||
mat->create(h, w, CV_8UC3);
|
||||
mk_swscale_input_frame(ctx->swscale, frame, (uint8_t *) mat->data);
|
||||
#endif
|
||||
log_trace("decode frame output, pts:%d, w:%d, h:%d", mk_get_av_frame_dts(mk_frame_pix_get_av_frame(frame)), w, h);
|
||||
}
|
||||
|
||||
void API_CALL on_mk_play_event_func(void *user_data, int err_code, const char *err_msg, mk_track tracks[],
|
||||
int track_count) {
|
||||
Context *ctx = (Context *) user_data;
|
||||
if (err_code == 0) {
|
||||
//success
|
||||
log_debug("play success!");
|
||||
for (int i = 0; i < track_count; ++i) {
|
||||
if (mk_track_is_video(tracks[i])) {
|
||||
log_info("got video track: %s", mk_track_codec_name(tracks[i]));
|
||||
ctx->video_decoder = mk_decoder_create(tracks[i], 0);
|
||||
mk_decoder_set_cb(ctx->video_decoder, on_frame_decode, user_data);
|
||||
//监听track数据回调
|
||||
mk_track_add_delegate(tracks[i], on_track_frame_out, user_data);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log_warn("play failed: %d %s", err_code, err_msg);
|
||||
}
|
||||
}
|
||||
|
||||
void API_CALL on_mk_shutdown_func(void *user_data, int err_code, const char *err_msg, mk_track tracks[], int track_count) {
|
||||
log_warn("play interrupted: %d %s", err_code, err_msg);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
mk_config config = {
|
||||
.ini = NULL,
|
||||
.ini_is_path = 0,
|
||||
.log_level = 0,
|
||||
.log_mask = LOG_CONSOLE,
|
||||
.ssl = NULL,
|
||||
.ssl_is_path = 1,
|
||||
.ssl_pwd = NULL,
|
||||
.thread_num = 0
|
||||
};
|
||||
mk_env_init(&config);
|
||||
if (argc != 2) {
|
||||
printf("Usage: ./player url\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
Context ctx;
|
||||
memset(&ctx, 0, sizeof(Context));
|
||||
|
||||
ctx.player = mk_player_create();
|
||||
ctx.swscale = mk_swscale_create(3, 0, 0);
|
||||
mk_player_set_on_result(ctx.player, on_mk_play_event_func, &ctx);
|
||||
mk_player_set_on_shutdown(ctx.player, on_mk_shutdown_func, &ctx);
|
||||
mk_player_play(ctx.player, argv[1]);
|
||||
|
||||
log_info("enter any key to exit");
|
||||
getchar();
|
||||
|
||||
if (ctx.player) {
|
||||
mk_player_release(ctx.player);
|
||||
}
|
||||
if (ctx.video_decoder) {
|
||||
mk_decoder_release(ctx.video_decoder, 1);
|
||||
}
|
||||
if (ctx.swscale) {
|
||||
mk_swscale_release(ctx.swscale);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -8,16 +8,9 @@
|
||||
* may be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include "mk_mediakit.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "windows.h"
|
||||
#else
|
||||
#include "unistd.h"
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
mk_player player;
|
||||
mk_media media;
|
||||
@@ -84,7 +77,12 @@ void API_CALL on_mk_media_source_regist_func(void *user_data, mk_media_source se
|
||||
}
|
||||
}
|
||||
|
||||
void API_CALL on_mk_play_event_func(void *user_data, int err_code, const char *err_msg) {
|
||||
void API_CALL on_track_frame_out(void *user_data, mk_frame frame) {
|
||||
Context *ctx = (Context *) user_data;
|
||||
mk_media_input_frame(ctx->media, frame);
|
||||
}
|
||||
|
||||
void API_CALL on_mk_play_event_func(void *user_data, int err_code, const char *err_msg, mk_track tracks[], int track_count) {
|
||||
Context *ctx = (Context *) user_data;
|
||||
release_media(&(ctx->media));
|
||||
release_pusher(&(ctx->pusher));
|
||||
@@ -92,21 +90,9 @@ void API_CALL on_mk_play_event_func(void *user_data, int err_code, const char *e
|
||||
//success
|
||||
log_debug("play success!");
|
||||
ctx->media = mk_media_create("__defaultVhost__", "live", "test", 0, 0, 0);
|
||||
|
||||
int video_codec = mk_player_video_codec_id(ctx->player);
|
||||
int audio_codec = mk_player_audio_codec_id(ctx->player);
|
||||
if(video_codec != -1){
|
||||
mk_media_init_video(ctx->media, video_codec,
|
||||
mk_player_video_width(ctx->player),
|
||||
mk_player_video_height(ctx->player),
|
||||
mk_player_video_fps(ctx->player));
|
||||
}
|
||||
|
||||
if(audio_codec != -1){
|
||||
mk_media_init_audio(ctx->media,audio_codec,
|
||||
mk_player_audio_samplerate(ctx->player),
|
||||
mk_player_audio_channel(ctx->player),
|
||||
mk_player_audio_bit(ctx->player));
|
||||
for (int i = 0; i < track_count; ++i) {
|
||||
mk_media_init_track(ctx->media, tracks[i]);
|
||||
mk_track_add_delegate(tracks[i], on_track_frame_out, user_data);
|
||||
}
|
||||
mk_media_init_complete(ctx->media);
|
||||
mk_media_set_on_regist(ctx->media, on_mk_media_source_regist_func, ctx);
|
||||
@@ -116,49 +102,15 @@ void API_CALL on_mk_play_event_func(void *user_data, int err_code, const char *e
|
||||
}
|
||||
}
|
||||
|
||||
void API_CALL on_mk_play_data_func(void *user_data,int track_type, int codec_id,void *data,size_t len, uint32_t dts,uint32_t pts){
|
||||
Context *ctx = (Context *) user_data;
|
||||
switch (codec_id) {
|
||||
case 0 : {
|
||||
//h264
|
||||
mk_media_input_h264(ctx->media, data, (int)len, dts, pts);
|
||||
break;
|
||||
}
|
||||
case 1 : {
|
||||
//h265
|
||||
mk_media_input_h265(ctx->media, data, (int)len, dts, pts);
|
||||
break;
|
||||
}
|
||||
case 2 : {
|
||||
//aac, aac头7个字节
|
||||
mk_media_input_aac(ctx->media, (uint8_t*)data + 7, (int)len, dts, data);
|
||||
break;
|
||||
}
|
||||
case 3 : //g711a
|
||||
case 4 : //g711u
|
||||
case 5 : //opus
|
||||
mk_media_input_audio(ctx->media, data, (int) len, dts);
|
||||
break;
|
||||
|
||||
default: {
|
||||
log_warn("unknown codec: %d", codec_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void context_start(Context *ctx, const char *url_pull, const char *url_push){
|
||||
release_player(&(ctx->player));
|
||||
ctx->player = mk_player_create();
|
||||
mk_player_set_on_result(ctx->player, on_mk_play_event_func, ctx);
|
||||
mk_player_set_on_shutdown(ctx->player, on_mk_play_event_func, ctx);
|
||||
mk_player_set_on_data(ctx->player, on_mk_play_data_func, ctx);
|
||||
mk_player_play(ctx->player, url_pull);
|
||||
strcpy(ctx->push_url, url_push);
|
||||
}
|
||||
|
||||
//create_player("http://hls.weathertv.cn/tslslive/qCFIfHB/hls/live_sd.m3u8");
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
mk_config config = {
|
||||
.ini = NULL,
|
||||
@@ -172,6 +124,11 @@ int main(int argc, char *argv[]){
|
||||
};
|
||||
mk_env_init(&config);
|
||||
|
||||
if (argc != 3) {
|
||||
printf("Usage: ./pusher.c pull_url push_url\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//可以通过
|
||||
//rtmp://127.0.0.1/live/test
|
||||
//rtsp://127.0.0.1/live/test
|
||||
@@ -179,20 +136,14 @@ int main(int argc, char *argv[]){
|
||||
mk_rtsp_server_start(554, 0);
|
||||
mk_rtmp_server_start(1935, 0);
|
||||
|
||||
Context *ctx = (Context *)malloc(sizeof(Context));
|
||||
Context *ctx = (Context *) malloc(sizeof(Context));
|
||||
memset(ctx, 0, sizeof(Context));
|
||||
|
||||
//推流给自己测试,当然也可以推流给其他服务器测试
|
||||
context_start(ctx, "http://hls.weathertv.cn/tslslive/qCFIfHB/hls/live_sd.m3u8", "rtsp://127.0.0.1/live/rtsp_push");
|
||||
context_start(ctx, argv[1], argv[2]);
|
||||
|
||||
int i = 10 * 60;
|
||||
while(--i){
|
||||
#ifdef _WIN32
|
||||
Sleep(1000);
|
||||
#else
|
||||
sleep(1);
|
||||
#endif
|
||||
}
|
||||
log_info("enter any key to exit");
|
||||
getchar();
|
||||
|
||||
release_context(&ctx);
|
||||
return 0;
|
||||
|
||||
@@ -8,18 +8,8 @@
|
||||
* may be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include "mk_mediakit.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "windows.h"
|
||||
#else
|
||||
|
||||
#include "unistd.h"
|
||||
|
||||
#endif
|
||||
|
||||
#define LOG_LEV 4
|
||||
|
||||
/**
|
||||
@@ -396,10 +386,6 @@ void API_CALL on_mk_flow_report(const mk_media_info url_info,
|
||||
(int)mk_sock_info_peer_port(sender));
|
||||
}
|
||||
|
||||
static int flag = 1;
|
||||
static void s_on_exit(int sig){
|
||||
flag = 0;
|
||||
}
|
||||
int main(int argc, char *argv[]) {
|
||||
char *ini_path = mk_util_get_exe_dir("c_api.ini");
|
||||
char *ssl_path = mk_util_get_exe_dir("ssl.p12");
|
||||
@@ -444,17 +430,11 @@ int main(int argc, char *argv[]) {
|
||||
.on_mk_flow_report = on_mk_flow_report
|
||||
};
|
||||
mk_events_listen(&events);
|
||||
|
||||
log_info("media server %s", "stared!");
|
||||
|
||||
signal(SIGINT, s_on_exit );// 设置退出信号
|
||||
while (flag) {
|
||||
#ifdef _WIN32
|
||||
Sleep(1000);
|
||||
#else
|
||||
sleep(1);
|
||||
#endif
|
||||
}
|
||||
log_info("enter any key to exit");
|
||||
getchar();
|
||||
|
||||
mk_stop_all_server();
|
||||
return 0;
|
||||
}
|
||||
@@ -8,26 +8,15 @@
|
||||
* may be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "mk_mediakit.h"
|
||||
#ifdef _WIN32
|
||||
#include "windows.h"
|
||||
#else
|
||||
#include "unistd.h"
|
||||
#endif
|
||||
|
||||
#define LOG_LEV 4
|
||||
//修改此宏,可以选择协议类型
|
||||
#define TCP_TYPE mk_type_ws
|
||||
|
||||
static int flag = 1;
|
||||
static void s_on_exit(int sig){
|
||||
flag = 0;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
typedef struct {
|
||||
mk_tcp_session _session;
|
||||
@@ -52,11 +41,15 @@ void API_CALL on_mk_tcp_session_create(uint16_t server_port,mk_tcp_session sessi
|
||||
* @param data 数据指针
|
||||
* @param len 数据长度
|
||||
*/
|
||||
void API_CALL on_mk_tcp_session_data(uint16_t server_port,mk_tcp_session session,const char *data,size_t len){
|
||||
void API_CALL on_mk_tcp_session_data(uint16_t server_port,mk_tcp_session session, mk_buffer buffer){
|
||||
char ip[64];
|
||||
log_printf(LOG_LEV,"from %s %d, data[%d]: %s",mk_tcp_session_peer_ip(session,ip),(int)mk_tcp_session_peer_port(session), len,data);
|
||||
log_printf(LOG_LEV,"from %s %d, data[%d]: %s",
|
||||
mk_tcp_session_peer_ip(session,ip),
|
||||
(int)mk_tcp_session_peer_port(session),
|
||||
mk_buffer_get_size(buffer),
|
||||
mk_buffer_get_data(buffer));
|
||||
mk_tcp_session_send(session,"echo:",0);
|
||||
mk_tcp_session_send(session,data,len);
|
||||
mk_tcp_session_send_buffer(session, buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,8 +122,8 @@ void API_CALL on_mk_tcp_client_disconnect(mk_tcp_client client,int code,const ch
|
||||
* @param data 数据指针
|
||||
* @param len 数据长度
|
||||
*/
|
||||
void API_CALL on_mk_tcp_client_data(mk_tcp_client client,const char *data,size_t len){
|
||||
log_printf(LOG_LEV,"data[%d]:%s",len,data);
|
||||
void API_CALL on_mk_tcp_client_data(mk_tcp_client client, mk_buffer buffer){
|
||||
log_printf(LOG_LEV, "data[%d]:%s", mk_buffer_get_size(buffer), mk_buffer_get_data(buffer));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,14 +196,9 @@ int main(int argc, char *argv[]) {
|
||||
test_server();
|
||||
test_client();
|
||||
|
||||
signal(SIGINT, s_on_exit );// 设置退出信号
|
||||
while (flag) {
|
||||
#ifdef _WIN32
|
||||
Sleep(1000);
|
||||
#else
|
||||
sleep(1);
|
||||
#endif
|
||||
}
|
||||
log_info("enter any key to exit");
|
||||
getchar();
|
||||
|
||||
mk_stop_all_server();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user