2020-04-04 20:30:09 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-05-20 11:22:59 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-05-20 11:22:59 +08:00
|
|
|
|
*
|
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-05-20 11:22:59 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "Util/File.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/SSLBox.h"
|
|
|
|
|
#include "Util/onceToken.h"
|
|
|
|
|
#include "Util/CMD.h"
|
|
|
|
|
#include "Network/TcpServer.h"
|
2021-12-28 21:04:53 +08:00
|
|
|
|
#include "Network/UdpServer.h"
|
2019-05-20 11:22:59 +08:00
|
|
|
|
#include "Poller/EventPoller.h"
|
|
|
|
|
#include "Common/config.h"
|
|
|
|
|
#include "Rtsp/RtspSession.h"
|
|
|
|
|
#include "Rtmp/RtmpSession.h"
|
|
|
|
|
#include "Shell/ShellSession.h"
|
|
|
|
|
#include "Http/WebSocketSession.h"
|
2020-07-02 22:23:43 +08:00
|
|
|
|
#include "Rtp/RtpServer.h"
|
2019-06-06 18:28:33 +08:00
|
|
|
|
#include "WebApi.h"
|
2019-06-24 14:58:56 +08:00
|
|
|
|
#include "WebHook.h"
|
2021-09-08 18:00:55 +08:00
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_WEBRTC)
|
|
|
|
|
#include "../webrtc/WebRtcTransport.h"
|
|
|
|
|
#include "../webrtc/WebRtcSession.h"
|
|
|
|
|
#endif
|
2019-05-20 11:22:59 +08:00
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
#if defined(ENABLE_SRT)
|
|
|
|
|
#include "../srt/SrtSession.hpp"
|
|
|
|
|
#include "../srt/SrtTransport.hpp"
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-12-29 11:36:06 +08:00
|
|
|
|
#if defined(ENABLE_VERSION)
|
2022-05-26 20:30:43 +08:00
|
|
|
|
#include "version.h"
|
2020-12-29 11:36:06 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2019-06-15 17:07:10 +08:00
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
|
#include "System.h"
|
|
|
|
|
#endif//!defined(_WIN32)
|
|
|
|
|
|
2019-05-20 11:22:59 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
using namespace mediakit;
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
////////////HTTP配置///////////
|
|
|
|
|
namespace Http {
|
|
|
|
|
#define HTTP_FIELD "http."
|
2019-06-19 10:32:54 +08:00
|
|
|
|
const string kPort = HTTP_FIELD"port";
|
|
|
|
|
const string kSSLPort = HTTP_FIELD"sslport";
|
2019-05-20 11:22:59 +08:00
|
|
|
|
onceToken token1([](){
|
2019-12-06 11:54:10 +08:00
|
|
|
|
mINI::Instance()[kPort] = 80;
|
|
|
|
|
mINI::Instance()[kSSLPort] = 443;
|
2019-05-20 11:22:59 +08:00
|
|
|
|
},nullptr);
|
|
|
|
|
}//namespace Http
|
|
|
|
|
|
|
|
|
|
////////////SHELL配置///////////
|
|
|
|
|
namespace Shell {
|
|
|
|
|
#define SHELL_FIELD "shell."
|
2019-06-19 10:32:54 +08:00
|
|
|
|
const string kPort = SHELL_FIELD"port";
|
2019-05-20 11:22:59 +08:00
|
|
|
|
onceToken token1([](){
|
2019-12-06 11:54:10 +08:00
|
|
|
|
mINI::Instance()[kPort] = 9000;
|
2019-05-20 11:22:59 +08:00
|
|
|
|
},nullptr);
|
|
|
|
|
} //namespace Shell
|
|
|
|
|
|
|
|
|
|
////////////RTSP服务器配置///////////
|
|
|
|
|
namespace Rtsp {
|
|
|
|
|
#define RTSP_FIELD "rtsp."
|
2019-06-19 10:32:54 +08:00
|
|
|
|
const string kPort = RTSP_FIELD"port";
|
|
|
|
|
const string kSSLPort = RTSP_FIELD"sslport";
|
2019-05-20 11:22:59 +08:00
|
|
|
|
onceToken token1([](){
|
2019-12-06 11:54:10 +08:00
|
|
|
|
mINI::Instance()[kPort] = 554;
|
|
|
|
|
mINI::Instance()[kSSLPort] = 332;
|
2019-05-20 11:22:59 +08:00
|
|
|
|
},nullptr);
|
|
|
|
|
|
|
|
|
|
} //namespace Rtsp
|
|
|
|
|
|
|
|
|
|
////////////RTMP服务器配置///////////
|
|
|
|
|
namespace Rtmp {
|
|
|
|
|
#define RTMP_FIELD "rtmp."
|
2019-06-19 10:32:54 +08:00
|
|
|
|
const string kPort = RTMP_FIELD"port";
|
2020-04-29 11:25:15 +08:00
|
|
|
|
const string kSSLPort = RTMP_FIELD"sslport";
|
2019-05-20 11:22:59 +08:00
|
|
|
|
onceToken token1([](){
|
2019-12-06 11:54:10 +08:00
|
|
|
|
mINI::Instance()[kPort] = 1935;
|
2020-04-29 11:25:15 +08:00
|
|
|
|
mINI::Instance()[kSSLPort] = 19350;
|
2019-05-20 11:22:59 +08:00
|
|
|
|
},nullptr);
|
|
|
|
|
} //namespace RTMP
|
2019-12-06 11:54:10 +08:00
|
|
|
|
|
|
|
|
|
////////////Rtp代理相关配置///////////
|
|
|
|
|
namespace RtpProxy {
|
|
|
|
|
#define RTP_PROXY_FIELD "rtp_proxy."
|
|
|
|
|
const string kPort = RTP_PROXY_FIELD"port";
|
|
|
|
|
onceToken token1([](){
|
|
|
|
|
mINI::Instance()[kPort] = 10000;
|
|
|
|
|
},nullptr);
|
|
|
|
|
} //namespace RtpProxy
|
|
|
|
|
|
2023-07-14 20:15:11 +08:00
|
|
|
|
// RTC配置项目
|
|
|
|
|
namespace Rtc {
|
|
|
|
|
#define RTC_FIELD "rtc."
|
|
|
|
|
const string kPort = RTC_FIELD "port";
|
|
|
|
|
const string kTcpPort = RTC_FIELD "tcpPort";
|
|
|
|
|
onceToken token1([]() {
|
|
|
|
|
mINI::Instance()[kPort] = 8000;
|
|
|
|
|
mINI::Instance()[kTcpPort] = 8000;
|
|
|
|
|
},nullptr);
|
|
|
|
|
} // namespace RTC
|
|
|
|
|
|
2019-05-20 11:22:59 +08:00
|
|
|
|
} // namespace mediakit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CMD_main : public CMD {
|
|
|
|
|
public:
|
|
|
|
|
CMD_main() {
|
2023-04-01 23:54:11 +08:00
|
|
|
|
_parser = std::make_shared<OptionParser>(nullptr);
|
2019-05-20 11:22:59 +08:00
|
|
|
|
|
2019-06-15 17:07:10 +08:00
|
|
|
|
#if !defined(_WIN32)
|
2019-05-20 11:22:59 +08:00
|
|
|
|
(*_parser) << Option('d',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"daemon",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgNone,/*该选项后面必须跟值*/
|
|
|
|
|
nullptr,/*该选项默认值*/
|
|
|
|
|
false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"是否以Daemon方式启动",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
2019-06-15 17:07:10 +08:00
|
|
|
|
#endif//!defined(_WIN32)
|
2019-05-20 11:22:59 +08:00
|
|
|
|
|
|
|
|
|
(*_parser) << Option('l',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"level",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
2023-04-22 22:03:30 +08:00
|
|
|
|
to_string(LDebug).data(),/*该选项默认值*/
|
2019-05-20 11:22:59 +08:00
|
|
|
|
false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"日志等级,LTrace~LError(0~4)",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
2019-08-06 22:51:40 +08:00
|
|
|
|
(*_parser) << Option('m',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"max_day",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
"7",/*该选项默认值*/
|
|
|
|
|
false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
2019-08-06 22:56:09 +08:00
|
|
|
|
"日志最多保存天数",/*该选项说明文字*/
|
2019-08-06 22:51:40 +08:00
|
|
|
|
nullptr);
|
|
|
|
|
|
2019-05-20 11:22:59 +08:00
|
|
|
|
(*_parser) << Option('c',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"config",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
(exeDir() + "config.ini").data(),/*该选项默认值*/
|
|
|
|
|
false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"配置文件路径",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('s',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"ssl",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
2021-09-13 21:16:22 +08:00
|
|
|
|
(exeDir() + "default.pem").data(),/*该选项默认值*/
|
2019-05-20 11:22:59 +08:00
|
|
|
|
false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
2019-11-08 15:20:54 +08:00
|
|
|
|
"ssl证书文件或文件夹,支持p12/pem类型",/*该选项说明文字*/
|
2019-05-20 11:22:59 +08:00
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('t',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"threads",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
to_string(thread::hardware_concurrency()).data(),/*该选项默认值*/
|
|
|
|
|
false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"启动事件触发线程数",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
2020-12-29 11:36:06 +08:00
|
|
|
|
|
2023-04-22 22:10:35 +08:00
|
|
|
|
(*_parser) << Option(0,/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"affinity",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
to_string(1).data(),/*该选项默认值*/
|
|
|
|
|
false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"是否启动cpu亲和性设置",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
2020-12-29 11:36:06 +08:00
|
|
|
|
#if defined(ENABLE_VERSION)
|
|
|
|
|
(*_parser) << Option('v', "version", Option::ArgNone, nullptr, false, "显示版本号",
|
|
|
|
|
[](const std::shared_ptr<ostream> &stream, const string &arg) -> bool {
|
|
|
|
|
//版本信息
|
2021-06-29 11:16:05 +08:00
|
|
|
|
*stream << "编译日期: " << BUILD_TIME << std::endl;
|
2022-11-13 00:13:02 +08:00
|
|
|
|
*stream << "代码日期: " << COMMIT_TIME << std::endl;
|
2021-09-30 11:27:42 +08:00
|
|
|
|
*stream << "当前git分支: " << BRANCH_NAME << std::endl;
|
2021-06-29 11:16:05 +08:00
|
|
|
|
*stream << "当前git hash值: " << COMMIT_HASH << std::endl;
|
2020-12-29 11:36:06 +08:00
|
|
|
|
throw ExitException();
|
|
|
|
|
});
|
|
|
|
|
#endif
|
2023-06-26 10:49:25 +08:00
|
|
|
|
(*_parser) << Option(0,/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"log-slice",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
"100",/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"最大保存日志切片个数",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option(0,/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"log-size",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
"256",/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"单个日志切片最大容量,单位MB",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option(0,/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"log-dir",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
(exeDir() + "log/").data(),/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"日志保存文件夹路径",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
2019-05-20 11:22:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 11:36:06 +08:00
|
|
|
|
~CMD_main() override{}
|
|
|
|
|
const char *description() const override{
|
2019-05-20 11:22:59 +08:00
|
|
|
|
return "主程序命令参数";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-04 09:21:11 +08:00
|
|
|
|
//全局变量,在WebApi中用于保存配置文件用
|
|
|
|
|
string g_ini_file;
|
2019-09-24 15:21:20 +08:00
|
|
|
|
|
|
|
|
|
int start_main(int argc,char *argv[]) {
|
2019-05-20 16:26:04 +08:00
|
|
|
|
{
|
|
|
|
|
CMD_main cmd_main;
|
|
|
|
|
try {
|
|
|
|
|
cmd_main.operator()(argc, argv);
|
2021-01-17 18:31:50 +08:00
|
|
|
|
} catch (ExitException &) {
|
2020-12-29 11:36:06 +08:00
|
|
|
|
return 0;
|
2019-05-20 16:26:04 +08:00
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
cout << ex.what() << endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-05-20 11:22:59 +08:00
|
|
|
|
|
2019-05-20 16:26:04 +08:00
|
|
|
|
bool bDaemon = cmd_main.hasKey("daemon");
|
|
|
|
|
LogLevel logLevel = (LogLevel) cmd_main["level"].as<int>();
|
|
|
|
|
logLevel = MIN(MAX(logLevel, LTrace), LError);
|
2019-11-04 09:21:11 +08:00
|
|
|
|
g_ini_file = cmd_main["config"];
|
2019-05-20 16:26:04 +08:00
|
|
|
|
string ssl_file = cmd_main["ssl"];
|
|
|
|
|
int threads = cmd_main["threads"];
|
2023-04-22 22:10:35 +08:00
|
|
|
|
bool affinity = cmd_main["affinity"];
|
2019-05-20 11:22:59 +08:00
|
|
|
|
|
2019-05-20 16:26:04 +08:00
|
|
|
|
//设置日志
|
|
|
|
|
Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel", logLevel));
|
2023-04-23 14:30:41 +08:00
|
|
|
|
#if !defined(ANDROID)
|
2023-06-26 10:49:25 +08:00
|
|
|
|
auto fileChannel = std::make_shared<FileChannel>("FileChannel", cmd_main["log-dir"], logLevel);
|
2023-04-22 22:10:35 +08:00
|
|
|
|
// 日志最多保存天数
|
2019-08-06 22:51:40 +08:00
|
|
|
|
fileChannel->setMaxDay(cmd_main["max_day"]);
|
2023-06-26 10:49:25 +08:00
|
|
|
|
fileChannel->setFileMaxCount(cmd_main["log-slice"]);
|
2023-06-28 14:55:39 +08:00
|
|
|
|
fileChannel->setFileMaxSize(cmd_main["log-size"]);
|
2019-08-06 22:51:40 +08:00
|
|
|
|
Logger::Instance().add(fileChannel);
|
2023-04-22 22:10:35 +08:00
|
|
|
|
#endif // !defined(ANDROID)
|
2019-05-20 11:22:59 +08:00
|
|
|
|
|
2019-06-15 17:07:10 +08:00
|
|
|
|
#if !defined(_WIN32)
|
2019-11-29 11:27:54 +08:00
|
|
|
|
pid_t pid = getpid();
|
2022-04-09 21:02:54 +08:00
|
|
|
|
bool kill_parent_if_failed = true;
|
2019-05-20 16:26:04 +08:00
|
|
|
|
if (bDaemon) {
|
|
|
|
|
//启动守护进程
|
2022-04-09 21:02:54 +08:00
|
|
|
|
System::startDaemon(kill_parent_if_failed);
|
2019-05-20 16:26:04 +08:00
|
|
|
|
}
|
2019-05-20 16:34:46 +08:00
|
|
|
|
//开启崩溃捕获等
|
|
|
|
|
System::systemSetup();
|
2019-06-15 17:07:10 +08:00
|
|
|
|
#endif//!defined(_WIN32)
|
2019-05-20 16:34:46 +08:00
|
|
|
|
|
2019-05-20 16:26:04 +08:00
|
|
|
|
//启动异步日志线程
|
|
|
|
|
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
2022-11-06 00:38:14 +08:00
|
|
|
|
|
|
|
|
|
InfoL << kServerName;
|
|
|
|
|
|
2019-05-20 16:26:04 +08:00
|
|
|
|
//加载配置文件,如果配置文件不存在就创建一个
|
2019-11-04 09:21:11 +08:00
|
|
|
|
loadIniConfig(g_ini_file.data());
|
2019-05-20 16:26:04 +08:00
|
|
|
|
|
2021-09-08 18:00:55 +08:00
|
|
|
|
if (!File::is_dir(ssl_file.data())) {
|
2019-11-08 15:20:54 +08:00
|
|
|
|
//不是文件夹,加载证书,证书包含公钥和私钥
|
|
|
|
|
SSL_Initor::Instance().loadCertificate(ssl_file.data());
|
2021-09-08 18:00:55 +08:00
|
|
|
|
} else {
|
2019-11-08 15:20:54 +08:00
|
|
|
|
//加载文件夹下的所有证书
|
2021-09-08 18:00:55 +08:00
|
|
|
|
File::scanDir(ssl_file, [](const string &path, bool isDir) {
|
|
|
|
|
if (!isDir) {
|
2019-11-08 15:26:46 +08:00
|
|
|
|
//最后的一个证书会当做默认证书(客户端ssl握手时未指定主机)
|
2019-11-08 15:20:54 +08:00
|
|
|
|
SSL_Initor::Instance().loadCertificate(path.data());
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-05-20 16:26:04 +08:00
|
|
|
|
|
|
|
|
|
uint16_t shellPort = mINI::Instance()[Shell::kPort];
|
|
|
|
|
uint16_t rtspPort = mINI::Instance()[Rtsp::kPort];
|
|
|
|
|
uint16_t rtspsPort = mINI::Instance()[Rtsp::kSSLPort];
|
|
|
|
|
uint16_t rtmpPort = mINI::Instance()[Rtmp::kPort];
|
2020-04-29 11:25:15 +08:00
|
|
|
|
uint16_t rtmpsPort = mINI::Instance()[Rtmp::kSSLPort];
|
2019-05-20 16:26:04 +08:00
|
|
|
|
uint16_t httpPort = mINI::Instance()[Http::kPort];
|
|
|
|
|
uint16_t httpsPort = mINI::Instance()[Http::kSSLPort];
|
2020-04-29 11:25:15 +08:00
|
|
|
|
uint16_t rtpPort = mINI::Instance()[RtpProxy::kPort];
|
2019-05-20 16:26:04 +08:00
|
|
|
|
|
2023-04-22 22:10:35 +08:00
|
|
|
|
//设置poller线程数和cpu亲和性,该函数必须在使用ZLToolKit网络相关对象之前调用才能生效
|
|
|
|
|
//如果需要调用getSnap和addFFmpegSource接口,可以关闭cpu亲和性
|
|
|
|
|
|
2019-05-20 16:26:04 +08:00
|
|
|
|
EventPollerPool::setPoolSize(threads);
|
2023-04-22 22:10:35 +08:00
|
|
|
|
EventPollerPool::enableCpuAffinity(affinity);
|
2019-05-20 16:26:04 +08:00
|
|
|
|
|
|
|
|
|
//简单的telnet服务器,可用于服务器调试,但是不能使用23端口,否则telnet上了莫名其妙的现象
|
|
|
|
|
//测试方法:telnet 127.0.0.1 9000
|
2021-11-28 21:19:08 +08:00
|
|
|
|
auto shellSrv = std::make_shared<TcpServer>();
|
2020-04-29 11:25:15 +08:00
|
|
|
|
|
|
|
|
|
//rtsp[s]服务器, 可用于诸如亚马逊echo show这样的设备访问
|
2023-04-22 21:59:26 +08:00
|
|
|
|
auto rtspSrv = std::make_shared<TcpServer>();
|
|
|
|
|
auto rtspSSLSrv = std::make_shared<TcpServer>();
|
2020-04-29 11:25:15 +08:00
|
|
|
|
|
|
|
|
|
//rtmp[s]服务器
|
2023-04-22 21:59:26 +08:00
|
|
|
|
auto rtmpSrv = std::make_shared<TcpServer>();
|
|
|
|
|
auto rtmpsSrv = std::make_shared<TcpServer>();
|
2020-04-29 11:25:15 +08:00
|
|
|
|
|
|
|
|
|
//http[s]服务器
|
2023-04-22 21:59:26 +08:00
|
|
|
|
auto httpSrv = std::make_shared<TcpServer>();
|
|
|
|
|
auto httpsSrv = std::make_shared<TcpServer>();
|
2019-11-29 11:27:54 +08:00
|
|
|
|
|
2019-12-06 11:54:10 +08:00
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2020-04-29 11:25:15 +08:00
|
|
|
|
//GB28181 rtp推流端口,支持UDP/TCP
|
2021-11-28 21:19:08 +08:00
|
|
|
|
auto rtpServer = std::make_shared<RtpServer>();
|
2019-12-06 11:54:10 +08:00
|
|
|
|
#endif//defined(ENABLE_RTPPROXY)
|
|
|
|
|
|
2021-09-08 18:00:55 +08:00
|
|
|
|
#if defined(ENABLE_WEBRTC)
|
2022-11-18 22:52:57 +08:00
|
|
|
|
auto rtcSrv_tcp = std::make_shared<TcpServer>();
|
2021-09-08 18:00:55 +08:00
|
|
|
|
//webrtc udp服务器
|
2022-11-18 22:52:57 +08:00
|
|
|
|
auto rtcSrv_udp = std::make_shared<UdpServer>();
|
|
|
|
|
rtcSrv_udp->setOnCreateSocket([](const EventPoller::Ptr &poller, const Buffer::Ptr &buf, struct sockaddr *, int) {
|
2021-09-10 18:37:32 +08:00
|
|
|
|
if (!buf) {
|
|
|
|
|
return Socket::createSocket(poller, false);
|
|
|
|
|
}
|
2021-10-16 10:29:00 +08:00
|
|
|
|
auto new_poller = WebRtcSession::queryPoller(buf);
|
2021-09-10 18:37:32 +08:00
|
|
|
|
if (!new_poller) {
|
|
|
|
|
//该数据对应的webrtc对象未找到,丢弃之
|
|
|
|
|
return Socket::Ptr();
|
|
|
|
|
}
|
|
|
|
|
return Socket::createSocket(new_poller, false);
|
|
|
|
|
});
|
2022-09-18 21:03:05 +08:00
|
|
|
|
uint16_t rtcPort = mINI::Instance()[Rtc::kPort];
|
2022-11-19 01:51:53 +08:00
|
|
|
|
uint16_t rtcTcpPort = mINI::Instance()[Rtc::kTcpPort];
|
2021-09-08 18:00:55 +08:00
|
|
|
|
#endif//defined(ENABLE_WEBRTC)
|
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_SRT)
|
|
|
|
|
auto srtSrv = std::make_shared<UdpServer>();
|
|
|
|
|
srtSrv->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) {
|
|
|
|
|
//握手第一阶段
|
|
|
|
|
return Socket::createSocket(poller, false);
|
|
|
|
|
}
|
|
|
|
|
return Socket::createSocket(new_poller, false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
uint16_t srtPort = mINI::Instance()[SRT::kPort];
|
|
|
|
|
#endif //defined(ENABLE_SRT)
|
|
|
|
|
|
2019-11-29 11:27:54 +08:00
|
|
|
|
try {
|
|
|
|
|
//rtsp服务器,端口默认554
|
2021-09-08 18:00:55 +08:00
|
|
|
|
if (rtspPort) { rtspSrv->start<RtspSession>(rtspPort); }
|
2019-11-29 11:27:54 +08:00
|
|
|
|
//rtsps服务器,端口默认322
|
2021-09-08 18:00:55 +08:00
|
|
|
|
if (rtspsPort) { rtspSSLSrv->start<RtspSessionWithSSL>(rtspsPort); }
|
2020-04-29 11:25:15 +08:00
|
|
|
|
|
2019-11-29 11:27:54 +08:00
|
|
|
|
//rtmp服务器,端口默认1935
|
2021-09-08 18:00:55 +08:00
|
|
|
|
if (rtmpPort) { rtmpSrv->start<RtmpSession>(rtmpPort); }
|
2020-04-29 11:25:15 +08:00
|
|
|
|
//rtmps服务器,端口默认19350
|
2021-09-08 18:00:55 +08:00
|
|
|
|
if (rtmpsPort) { rtmpsSrv->start<RtmpSessionWithSSL>(rtmpsPort); }
|
2020-04-29 11:25:15 +08:00
|
|
|
|
|
2019-11-29 11:27:54 +08:00
|
|
|
|
//http服务器,端口默认80
|
2021-09-08 18:00:55 +08:00
|
|
|
|
if (httpPort) { httpSrv->start<HttpSession>(httpPort); }
|
2019-11-29 11:27:54 +08:00
|
|
|
|
//https服务器,端口默认443
|
2021-09-08 18:00:55 +08:00
|
|
|
|
if (httpsPort) { httpsSrv->start<HttpsSession>(httpsPort); }
|
2020-04-29 11:25:15 +08:00
|
|
|
|
|
2019-11-29 11:27:54 +08:00
|
|
|
|
//telnet远程调试服务器
|
2021-09-08 18:00:55 +08:00
|
|
|
|
if (shellPort) { shellSrv->start<ShellSession>(shellPort); }
|
2019-12-06 11:54:10 +08:00
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2020-07-02 22:23:43 +08:00
|
|
|
|
//创建rtp服务器
|
2021-09-08 18:00:55 +08:00
|
|
|
|
if (rtpPort) { rtpServer->start(rtpPort); }
|
2019-12-06 11:54:10 +08:00
|
|
|
|
#endif//defined(ENABLE_RTPPROXY)
|
|
|
|
|
|
2021-09-08 18:00:55 +08:00
|
|
|
|
#if defined(ENABLE_WEBRTC)
|
|
|
|
|
//webrtc udp服务器
|
2022-11-19 01:51:53 +08:00
|
|
|
|
if (rtcPort) { rtcSrv_udp->start<WebRtcSession>(rtcPort);}
|
|
|
|
|
|
|
|
|
|
if (rtcTcpPort) { rtcSrv_tcp->start<WebRtcSession>(rtcTcpPort);}
|
|
|
|
|
|
2021-09-08 18:00:55 +08:00
|
|
|
|
#endif//defined(ENABLE_WEBRTC)
|
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
#if defined(ENABLE_SRT)
|
|
|
|
|
// srt udp服务器
|
2022-09-22 21:18:34 +08:00
|
|
|
|
if(srtPort) { srtSrv->start<SRT::SrtSession>(srtPort); }
|
2022-06-03 13:25:32 +08:00
|
|
|
|
#endif//defined(ENABLE_SRT)
|
|
|
|
|
|
2021-09-08 18:00:55 +08:00
|
|
|
|
} catch (std::exception &ex) {
|
2023-06-10 11:04:52 +08:00
|
|
|
|
WarnL << "端口占用或无权限:" << ex.what();
|
|
|
|
|
ErrorL << "程序启动失败,请修改配置文件中端口号后重试!";
|
2019-11-29 11:27:54 +08:00
|
|
|
|
sleep(1);
|
|
|
|
|
#if !defined(_WIN32)
|
2022-04-09 21:02:54 +08:00
|
|
|
|
if (pid != getpid() && kill_parent_if_failed) {
|
|
|
|
|
//杀掉守护进程
|
2022-04-09 20:57:00 +08:00
|
|
|
|
kill(pid, SIGINT);
|
2019-11-29 11:27:54 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-05-20 16:26:04 +08:00
|
|
|
|
|
|
|
|
|
installWebApi();
|
|
|
|
|
InfoL << "已启动http api 接口";
|
|
|
|
|
installWebHook();
|
|
|
|
|
InfoL << "已启动http hook 接口";
|
|
|
|
|
|
|
|
|
|
//设置退出信号处理函数
|
|
|
|
|
static semaphore sem;
|
|
|
|
|
signal(SIGINT, [](int) {
|
|
|
|
|
InfoL << "SIGINT:exit";
|
2023-06-28 11:36:41 +08:00
|
|
|
|
signal(SIGINT, SIG_IGN); // 设置退出信号
|
2019-05-20 16:26:04 +08:00
|
|
|
|
sem.post();
|
2023-06-28 11:36:41 +08:00
|
|
|
|
}); // 设置退出信号
|
2019-06-15 17:07:10 +08:00
|
|
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
2019-11-04 09:21:11 +08:00
|
|
|
|
signal(SIGHUP, [](int) { mediakit::loadIniConfig(g_ini_file.data()); });
|
2019-06-15 17:07:10 +08:00
|
|
|
|
#endif
|
2019-05-20 16:26:04 +08:00
|
|
|
|
sem.wait();
|
|
|
|
|
}
|
2019-05-20 17:03:04 +08:00
|
|
|
|
unInstallWebApi();
|
|
|
|
|
unInstallWebHook();
|
2023-06-28 11:36:41 +08:00
|
|
|
|
onProcessExited();
|
|
|
|
|
|
2019-05-28 09:50:05 +08:00
|
|
|
|
//休眠1秒再退出,防止资源释放顺序错误
|
2019-05-20 17:03:04 +08:00
|
|
|
|
InfoL << "程序退出中,请等待...";
|
2019-05-28 09:50:05 +08:00
|
|
|
|
sleep(1);
|
2019-05-20 17:03:04 +08:00
|
|
|
|
InfoL << "程序退出完毕!";
|
2020-03-20 11:51:24 +08:00
|
|
|
|
return 0;
|
2019-05-20 11:22:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-24 15:21:20 +08:00
|
|
|
|
#ifndef DISABLE_MAIN
|
|
|
|
|
int main(int argc,char *argv[]) {
|
|
|
|
|
return start_main(argc,argv);
|
|
|
|
|
}
|
|
|
|
|
#endif //DISABLE_MAIN
|
|
|
|
|
|
|
|
|
|
|