ZLMediaKit/api/source/mk_common.cpp

375 lines
12 KiB
C++
Raw Permalink Normal View History

2019-12-18 11:47:49 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2019-12-17 18:45:31 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2019-12-17 18:45:31 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* 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 "Util/File.h"
2019-12-17 18:45:31 +08:00
#include "Network/TcpServer.h"
2022-05-25 15:38:32 +08:00
#include "Network/UdpServer.h"
2019-12-17 18:45:31 +08:00
#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/RtpServer.h"
static RtpServer::Ptr rtpServer;
2019-12-18 14:53:42 +08:00
#endif
#ifdef ENABLE_WEBRTC
#include "../webrtc/WebRtcSession.h"
2022-12-02 14:43:06 +08:00
static UdpServer::Ptr rtcServer_udp;
static TcpServer::Ptr rtcServer_tcp;
#endif
2022-09-22 21:18:34 +08:00
#if defined(ENABLE_SRT)
#include "../srt/SrtSession.hpp"
2022-12-02 14:43:06 +08:00
static UdpServer::Ptr srtServer;
2022-09-22 21:18:34 +08:00
#endif
2019-12-17 18:45:31 +08:00
//////////////////////////environment init///////////////////////////
2021-08-30 20:43:03 +08:00
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);
2021-08-30 20:43:03 +08:00
mk_env_init1(cfg->thread_num,
2019-12-20 11:04:18 +08:00
cfg->log_level,
2021-08-30 20:43:03 +08:00
cfg->log_mask,
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,
2021-08-30 20:43:03 +08:00
cfg->ssl_pwd);
2019-12-20 11:04:18 +08:00
}
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);
2022-09-22 21:18:34 +08:00
shell_server = nullptr;
2020-01-14 11:21:21 +08:00
#ifdef ENABLE_RTPPROXY
rtpServer = nullptr;
2022-09-22 21:18:34 +08:00
#endif
#ifdef ENABLE_WEBRTC
rtcServer_udp = nullptr;
rtcServer_tcp = nullptr;
2022-09-22 21:18:34 +08:00
#endif
#ifdef ENABLE_SRT
srtServer = 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
}
API_EXPORT void API_CALL mk_env_init2(int thread_num,
2020-04-22 09:51:04 +08:00
int log_level,
2021-08-30 20:43:03 +08:00
int log_mask,
2020-04-22 09:51:04 +08:00
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) {
// 确保只初始化一次 [AUTO-TRANSLATED:e4b32b0f]
// Ensure initialization only happens once
2019-12-17 18:45:31 +08:00
static onceToken token([&]() {
2021-08-30 20:43:03 +08:00
if (log_mask & LOG_CONSOLE) {
// 控制台日志 [AUTO-TRANSLATED:5c00e83f]
// Console log
Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel", (LogLevel) log_level));
}
2021-08-30 20:43:03 +08:00
if (log_mask & LOG_CALLBACK) {
// 广播日志 [AUTO-TRANSLATED:67556df8]
// Broadcast log
2021-08-30 20:43:03 +08:00
Logger::Instance().add(std::make_shared<EventChannel>("EventChannel", (LogLevel) log_level));
}
if (log_mask & LOG_FILE) {
// 日志文件 [AUTO-TRANSLATED:afacc934]
// Log file
2021-08-30 20:43:03 +08:00
auto channel = std::make_shared<FileChannel>("FileChannel",
log_file_path ? File::absolutePath("", log_file_path) :
2021-08-30 20:43:03 +08:00
exeDir() + "log/", (LogLevel) log_level);
channel->setMaxDay(log_file_days ? log_file_days : 1);
2020-04-22 09:51:04 +08:00
Logger::Instance().add(channel);
}
// 异步日志线程 [AUTO-TRANSLATED:1cc193a1]
// Asynchronous log thread
2019-12-18 13:54:55 +08:00
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
// 设置线程数 [AUTO-TRANSLATED:22ec5cc9]
// Set thread count
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]) {
// 设置配置文件 [AUTO-TRANSLATED:2216856d]
// Set configuration file
2019-12-20 11:04:18 +08:00
if (ini_is_path) {
try {
2019-12-23 15:31:35 +08:00
mINI::Instance().parseFile(ini);
} catch (std::exception &) {
2019-12-23 15:31:35 +08:00
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]) {
// 设置ssl证书 [AUTO-TRANSLATED:e441027c]
// Set SSL certificate
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
}
});
}
2021-09-28 09:52:29 +08:00
API_EXPORT void API_CALL mk_set_log(int file_max_size, int file_max_count) {
auto channel = dynamic_pointer_cast<FileChannel>(Logger::Instance().get("FileChannel"));
if (channel) {
channel->setFileMaxSize(file_max_size);
channel->setFileMaxCount(file_max_count);
}
}
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;
// 广播配置文件热加载 [AUTO-TRANSLATED:7ae561f3]
// Broadcast configuration file hot reload
NOTICE_EMIT(BroadcastReloadConfigArgs, Broadcast::kBroadcastReloadConfig);
2019-12-17 18:45:31 +08:00
}
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<SessionWithSSL<HttpSession> >(port);
2019-12-17 18:45:31 +08:00
} else{
http_server[ssl]->start<HttpSession>(port);
}
return http_server[ssl]->getPort();
} catch (std::exception &ex) {
http_server[ssl] = nullptr;
2019-12-17 18:45:31 +08:00
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<SessionWithSSL<RtspSession> >(port);
2019-12-17 18:45:31 +08:00
}else{
rtsp_server[ssl]->start<RtspSession>(port);
}
return rtsp_server[ssl]->getPort();
} catch (std::exception &ex) {
rtsp_server[ssl] = nullptr;
2019-12-17 18:45:31 +08:00
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<SessionWithSSL<RtmpSession> >(port);
2019-12-17 18:45:31 +08:00
}else{
rtmp_server[ssl]->start<RtmpSession>(port);
}
return rtmp_server[ssl]->getPort();
} catch (std::exception &ex) {
rtmp_server[ssl] = nullptr;
2019-12-17 18:45:31 +08:00
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 服务器 [AUTO-TRANSLATED:480fda83]
// Create RTP server
rtpServer = std::make_shared<RtpServer>();
rtpServer->start(port);
return rtpServer->getPort();
2019-12-18 14:53:42 +08:00
} catch (std::exception &ex) {
rtpServer = nullptr;
2019-12-18 14:53:42 +08:00
WarnL << ex.what();
return 0;
}
#else
WarnL << "未启用该功能!";
return 0;
#endif
}
API_EXPORT uint16_t API_CALL mk_rtc_server_start(uint16_t port) {
#ifdef ENABLE_WEBRTC
try {
// 创建rtc udp服务器 [AUTO-TRANSLATED:9287972e]
// Create RTC UDP server
rtcServer_udp = std::make_shared<UdpServer>();
rtcServer_udp->setOnCreateSocket([](const EventPoller::Ptr &poller, const Buffer::Ptr &buf, struct sockaddr *, int) {
if (!buf) {
return Socket::createSocket(poller, false);
}
auto new_poller = WebRtcSession::queryPoller(buf);
if (!new_poller) {
// 该数据对应的webrtc对象未找到丢弃之 [AUTO-TRANSLATED:d401f8cb]
// The WebRTC object corresponding to this data was not found, discard it
return Socket::Ptr();
}
return Socket::createSocket(new_poller, false);
});
rtcServer_udp->start<WebRtcSession>(port);
// 创建rtc tcp服务器 [AUTO-TRANSLATED:1eefd92f]
// Create RTC TCP server
rtcServer_tcp = std::make_shared<TcpServer>();
rtcServer_tcp->start<WebRtcSession>(rtcServer_udp->getPort());
return rtcServer_udp->getPort();
} catch (std::exception &ex) {
rtcServer_udp = nullptr;
rtcServer_tcp = nullptr;
WarnL << ex.what();
return 0;
}
#else
2022-10-06 12:33:48 +08:00
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
return 0;
#endif
}
2022-10-06 12:59:12 +08:00
#ifdef ENABLE_WEBRTC
2022-10-06 12:33:48 +08:00
class WebRtcArgsUrl : public mediakit::WebRtcArgs {
public:
WebRtcArgsUrl(std::string url) { _url = std::move(url); }
toolkit::variant operator[](const std::string &key) const override {
if (key == "url") {
return _url;
}
return "";
}
private:
std::string _url;
};
2022-10-06 12:59:12 +08:00
#endif
2022-10-06 12:33:48 +08:00
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type,
const char *offer, const char *url) {
mk_webrtc_get_answer_sdp2(user_data, nullptr, cb, type, offer, url);
}
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp2(void *user_data, on_user_data_free user_data_free, on_mk_webrtc_get_answer_sdp cb, const char *type,
2022-10-06 12:33:48 +08:00
const char *offer, const char *url) {
#ifdef ENABLE_WEBRTC
assert(type && offer && url && cb);
auto session = std::make_shared<HttpSession>(Socket::createSocket());
std::string offer_str = offer;
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
2023-12-09 16:23:51 +08:00
auto args = std::make_shared<WebRtcArgsUrl>(url);
2024-03-23 22:52:40 +08:00
WebRtcPluginManager::Instance().negotiateSdp(*session, type, *args, [offer_str, session, ptr, cb](const WebRtcInterface &exchanger) mutable {
auto &handler = const_cast<WebRtcInterface &>(exchanger);
2022-10-06 12:33:48 +08:00
try {
auto sdp_answer = handler.getAnswerSdp(offer_str);
cb(ptr.get(), sdp_answer.data(), nullptr);
2022-10-06 12:33:48 +08:00
} catch (std::exception &ex) {
cb(ptr.get(), nullptr, ex.what());
2022-10-06 12:33:48 +08:00
}
});
#else
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
#endif
}
2022-09-22 21:18:34 +08:00
API_EXPORT uint16_t API_CALL mk_srt_server_start(uint16_t port) {
#ifdef ENABLE_SRT
try {
srtServer = std::make_shared<UdpServer>();
srtServer->setOnCreateSocket([](const EventPoller::Ptr &poller, const Buffer::Ptr &buf, struct sockaddr *, int) {
if (!buf) {
return Socket::createSocket(poller, false);
}
auto new_poller = SRT::SrtSession::queryPoller(buf);
if (!new_poller) {
// 握手第一阶段 [AUTO-TRANSLATED:6b3abcd4]
// Handshake stage one
2022-09-22 21:18:34 +08:00
return Socket::createSocket(poller, false);
}
return Socket::createSocket(new_poller, false);
});
srtServer->start<SRT::SrtSession>(port);
return srtServer->getPort();
} catch (std::exception &ex) {
srtServer = nullptr;
2022-09-22 21:18:34 +08:00
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 = nullptr;
2019-12-24 16:09:09 +08:00
WarnL << ex.what();
return 0;
}
}