ZLMediaKit/api/source/mk_common.cpp

219 lines
6.5 KiB
C++
Raw Normal View History

2019-12-18 11:47:49 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2019-12-17 18:45:31 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
2020-04-04 20:30:09 +08:00
* 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.
2019-12-17 18:45:31 +08:00
*/
#include "mk_common.h"
2019-12-17 18:45:31 +08:00
#include <stdarg.h>
#include <unordered_map>
#include "Util/logger.h"
#include "Util/SSLBox.h"
#include "Network/TcpServer.h"
#include "Thread/WorkThreadPool.h"
#include "Rtsp/RtspSession.h"
#include "Rtmp/RtmpSession.h"
#include "Http/HttpSession.h"
2019-12-24 16:09:09 +08:00
#include "Shell/ShellSession.h"
2019-12-17 18:45:31 +08:00
using namespace std;
using namespace toolkit;
using namespace mediakit;
static TcpServer::Ptr rtsp_server[2];
static TcpServer::Ptr rtmp_server[2];
static TcpServer::Ptr http_server[2];
2019-12-24 16:09:09 +08:00
static TcpServer::Ptr shell_server;
2019-12-17 18:45:31 +08:00
2019-12-18 14:53:42 +08:00
#ifdef ENABLE_RTPPROXY
#include "Rtp/UdpRecver.h"
#include "Rtp/RtpSession.h"
static std::shared_ptr<UdpRecver> udpRtpServer;
2019-12-23 14:01:00 +08:00
static TcpServer::Ptr tcpRtpServer;
2019-12-18 14:53:42 +08:00
#endif
2019-12-17 18:45:31 +08:00
//////////////////////////environment init///////////////////////////
2019-12-19 16:45:32 +08:00
API_EXPORT void API_CALL mk_env_init(const mk_config *cfg) {
2019-12-18 14:43:32 +08:00
assert(cfg);
2019-12-20 11:04:18 +08:00
mk_env_init1(cfg->thread_num,
cfg->log_level,
2020-04-22 09:51:04 +08:00
cfg->log_file_path,
cfg->log_file_days,
2019-12-20 11:04:18 +08:00
cfg->ini_is_path,
cfg->ini,
cfg->ssl_is_path,
cfg->ssl,
cfg->ssl_pwd);
}
2019-12-26 21:22:19 +08:00
extern void stopAllTcpServer();
2019-12-23 14:20:49 +08:00
API_EXPORT void API_CALL mk_stop_all_server(){
CLEAR_ARR(rtsp_server);
CLEAR_ARR(rtmp_server);
CLEAR_ARR(http_server);
2020-01-14 11:21:21 +08:00
#ifdef ENABLE_RTPPROXY
2019-12-23 14:20:49 +08:00
udpRtpServer = nullptr;
tcpRtpServer = nullptr;
2020-01-14 11:21:21 +08:00
#endif
2019-12-26 21:22:19 +08:00
stopAllTcpServer();
2019-12-23 14:20:49 +08:00
}
2020-04-22 09:51:04 +08:00
API_EXPORT void API_CALL mk_env_init1(int thread_num,
int log_level,
const char *log_file_path,
int log_file_days,
int ini_is_path,
const char *ini,
int ssl_is_path,
const char *ssl,
const char *ssl_pwd) {
//确保只初始化一次
2019-12-17 18:45:31 +08:00
static onceToken token([&]() {
2020-04-22 09:51:04 +08:00
//控制台日志
2019-12-20 11:04:18 +08:00
Logger::Instance().add(std::make_shared<ConsoleChannel>("console", (LogLevel) log_level));
2020-04-22 09:51:04 +08:00
if(log_file_path && log_file_days){
//日志文件
auto channel = std::make_shared<FileChannel>("FileChannel", File::absolutePath(log_file_path, ""), (LogLevel) log_level);
Logger::Instance().add(channel);
}
//异步日志线程
2019-12-18 13:54:55 +08:00
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
2020-04-22 09:51:04 +08:00
//设置线程数
2019-12-20 11:04:18 +08:00
EventPollerPool::setPoolSize(thread_num);
WorkThreadPool::setPoolSize(thread_num);
2019-12-17 18:45:31 +08:00
2019-12-20 11:04:18 +08:00
if (ini && ini[0]) {
2019-12-17 18:45:31 +08:00
//设置配置文件
2019-12-20 11:04:18 +08:00
if (ini_is_path) {
2019-12-23 15:31:35 +08:00
try{
mINI::Instance().parseFile(ini);
}catch (std::exception &ex) {
InfoL << "dump ini file to:" << ini;
mINI::Instance().dumpFile(ini);
}
2019-12-17 18:45:31 +08:00
} else {
2019-12-20 11:04:18 +08:00
mINI::Instance().parse(ini);
2019-12-17 18:45:31 +08:00
}
}
2019-12-20 11:04:18 +08:00
if (ssl && ssl[0]) {
2019-12-17 18:45:31 +08:00
//设置ssl证书
2019-12-20 11:04:18 +08:00
SSL_Initor::Instance().loadCertificate(ssl, true, ssl_pwd ? ssl_pwd : "", ssl_is_path);
2019-12-17 18:45:31 +08:00
}
});
}
API_EXPORT void API_CALL mk_set_option(const char *key, const char *val) {
2019-12-18 14:43:32 +08:00
assert(key && val);
2019-12-17 18:45:31 +08:00
if (mINI::Instance().find(key) == mINI::Instance().end()) {
WarnL << "key:" << key << " not existed!";
return;
}
mINI::Instance()[key] = val;
}
API_EXPORT const char * API_CALL mk_get_option(const char *key)
{
assert(key);
if (mINI::Instance().find(key) == mINI::Instance().end()) {
WarnL << "key:" << key << " not existed!";
return nullptr;
}
return mINI::Instance()[key].data();
}
2019-12-17 18:45:31 +08:00
API_EXPORT uint16_t API_CALL mk_http_server_start(uint16_t port, int ssl) {
ssl = MAX(0,MIN(ssl,1));
try {
2019-12-18 14:56:24 +08:00
http_server[ssl] = std::make_shared<TcpServer>();
2019-12-17 18:45:31 +08:00
if(ssl){
http_server[ssl]->start<TcpSessionWithSSL<HttpSession> >(port);
} else{
http_server[ssl]->start<HttpSession>(port);
}
return http_server[ssl]->getPort();
} catch (std::exception &ex) {
http_server[ssl].reset();
WarnL << ex.what();
return 0;
}
}
API_EXPORT uint16_t API_CALL mk_rtsp_server_start(uint16_t port, int ssl) {
ssl = MAX(0,MIN(ssl,1));
try {
2019-12-18 14:56:24 +08:00
rtsp_server[ssl] = std::make_shared<TcpServer>();
2019-12-17 18:45:31 +08:00
if(ssl){
rtsp_server[ssl]->start<TcpSessionWithSSL<RtspSession> >(port);
}else{
rtsp_server[ssl]->start<RtspSession>(port);
}
return rtsp_server[ssl]->getPort();
} catch (std::exception &ex) {
rtsp_server[ssl].reset();
WarnL << ex.what();
return 0;
}
}
API_EXPORT uint16_t API_CALL mk_rtmp_server_start(uint16_t port, int ssl) {
ssl = MAX(0,MIN(ssl,1));
try {
2019-12-18 14:56:24 +08:00
rtmp_server[ssl] = std::make_shared<TcpServer>();
2019-12-17 18:45:31 +08:00
if(ssl){
rtmp_server[ssl]->start<TcpSessionWithSSL<RtmpSession> >(port);
}else{
rtmp_server[ssl]->start<RtmpSession>(port);
}
return rtmp_server[ssl]->getPort();
} catch (std::exception &ex) {
rtmp_server[ssl].reset();
WarnL << ex.what();
return 0;
}
}
2019-12-18 14:53:42 +08:00
API_EXPORT uint16_t API_CALL mk_rtp_server_start(uint16_t port){
#ifdef ENABLE_RTPPROXY
try {
//创建rtp tcp服务器
2019-12-18 14:54:47 +08:00
tcpRtpServer = std::make_shared<TcpServer>();
2019-12-18 14:53:42 +08:00
tcpRtpServer->start<RtpSession>(port);
2019-12-18 14:54:47 +08:00
//创建rtp udp服务器
2019-12-18 14:53:42 +08:00
auto ret = tcpRtpServer->getPort();
udpRtpServer = std::make_shared<UdpRecver>();
udpRtpServer->initSock(port);
return ret;
} catch (std::exception &ex) {
tcpRtpServer.reset();
udpRtpServer.reset();
WarnL << ex.what();
return 0;
}
#else
WarnL << "未启用该功能!";
return 0;
#endif
}
2019-12-24 16:09:09 +08:00
API_EXPORT uint16_t API_CALL mk_shell_server_start(uint16_t port){
try {
shell_server = std::make_shared<TcpServer>();
shell_server->start<ShellSession>(port);
return shell_server->getPort();
} catch (std::exception &ex) {
shell_server.reset();
WarnL << ex.what();
return 0;
}
}