main.cpp 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * MIT License
 *
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <map>
#include <signal.h>
#include <iostream>
#include "Util/MD5.h"
#include "Util/File.h"
#include "Util/logger.h"
#include "Util/SSLBox.h"
#include "Util/onceToken.h"
#include "Util/CMD.h"
#include "Network/TcpServer.h"
#include "Poller/EventPoller.h"
#include "Common/config.h"
#include "Rtsp/UDPServer.h"
#include "Rtsp/RtspSession.h"
41
#include "Rtp/RtpSession.h"
42 43 44
#include "Rtmp/RtmpSession.h"
#include "Shell/ShellSession.h"
#include "Http/WebSocketSession.h"
45
#include "Rtp/UdpRecver.h"
46
#include "WebApi.h"
47
#include "WebHook.h"
48

xiongziliang committed
49 50 51 52
#if !defined(_WIN32)
#include "System.h"
#endif//!defined(_WIN32)

53 54 55 56 57 58 59 60
using namespace std;
using namespace toolkit;
using namespace mediakit;

namespace mediakit {
////////////HTTP配置///////////
namespace Http {
#define HTTP_FIELD "http."
61 62
const string kPort = HTTP_FIELD"port";
const string kSSLPort = HTTP_FIELD"sslport";
63
onceToken token1([](){
64 65
    mINI::Instance()[kPort] = 80;
    mINI::Instance()[kSSLPort] = 443;
66 67 68 69 70 71
},nullptr);
}//namespace Http

////////////SHELL配置///////////
namespace Shell {
#define SHELL_FIELD "shell."
72
const string kPort = SHELL_FIELD"port";
73
onceToken token1([](){
74
    mINI::Instance()[kPort] = 9000;
75 76 77 78 79 80
},nullptr);
} //namespace Shell

////////////RTSP服务器配置///////////
namespace Rtsp {
#define RTSP_FIELD "rtsp."
81 82
const string kPort = RTSP_FIELD"port";
const string kSSLPort = RTSP_FIELD"sslport";
83
onceToken token1([](){
84 85
    mINI::Instance()[kPort] = 554;
    mINI::Instance()[kSSLPort] = 332;
86 87 88 89 90 91 92
},nullptr);

} //namespace Rtsp

////////////RTMP服务器配置///////////
namespace Rtmp {
#define RTMP_FIELD "rtmp."
93
const string kPort = RTMP_FIELD"port";
94
onceToken token1([](){
95
    mINI::Instance()[kPort] = 1935;
96 97
},nullptr);
} //namespace RTMP
98 99 100 101 102 103 104 105 106 107

////////////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

108 109 110 111 112 113 114 115
}  // namespace mediakit


class CMD_main : public CMD {
public:
    CMD_main() {
        _parser.reset(new OptionParser(nullptr));

xiongziliang committed
116
#if !defined(_WIN32)
117 118 119 120 121 122 123
        (*_parser) << Option('d',/*该选项简称,如果是\x00则说明无简称*/
                             "daemon",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
                             Option::ArgNone,/*该选项后面必须跟值*/
                             nullptr,/*该选项默认值*/
                             false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
                             "是否以Daemon方式启动",/*该选项说明文字*/
                             nullptr);
xiongziliang committed
124
#endif//!defined(_WIN32)
125 126 127 128 129 130 131 132 133

        (*_parser) << Option('l',/*该选项简称,如果是\x00则说明无简称*/
                             "level",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
                             Option::ArgRequired,/*该选项后面必须跟值*/
                             to_string(LTrace).data(),/*该选项默认值*/
                             false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
                             "日志等级,LTrace~LError(0~4)",/*该选项说明文字*/
                             nullptr);

134 135 136 137 138
        (*_parser) << Option('m',/*该选项简称,如果是\x00则说明无简称*/
                             "max_day",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
                             Option::ArgRequired,/*该选项后面必须跟值*/
                             "7",/*该选项默认值*/
                             false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
xiongziliang committed
139
                             "日志最多保存天数",/*该选项说明文字*/
140 141
                             nullptr);

142 143 144 145 146 147 148 149 150 151 152 153 154
        (*_parser) << Option('c',/*该选项简称,如果是\x00则说明无简称*/
                             "config",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
                             Option::ArgRequired,/*该选项后面必须跟值*/
                             (exeDir() + "config.ini").data(),/*该选项默认值*/
                             false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
                             "配置文件路径",/*该选项说明文字*/
                             nullptr);

        (*_parser) << Option('s',/*该选项简称,如果是\x00则说明无简称*/
                             "ssl",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
                             Option::ArgRequired,/*该选项后面必须跟值*/
                             (exeDir() + "ssl.p12").data(),/*该选项默认值*/
                             false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
155
                             "ssl证书文件或文件夹,支持p12/pem类型",/*该选项说明文字*/
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                             nullptr);

        (*_parser) << Option('t',/*该选项简称,如果是\x00则说明无简称*/
                             "threads",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
                             Option::ArgRequired,/*该选项后面必须跟值*/
                             to_string(thread::hardware_concurrency()).data(),/*该选项默认值*/
                             false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
                             "启动事件触发线程数",/*该选项说明文字*/
                             nullptr);
    }

    virtual ~CMD_main() {}
    virtual const char *description() const {
        return "主程序命令参数";
    }
};

xiongziliang committed
173
#if !defined(_WIN32)
174 175 176
static void inline listen_shell_input(){
    cout << "> 欢迎进入命令模式,你可以输入\"help\"命令获取帮助" << endl;
    cout << "> " << std::flush;
xiongziliang committed
177 178 179

    signal(SIGTTOU,SIG_IGN);
    signal(SIGTTIN,SIG_IGN);
xiongziliang committed
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    SockUtil::setNoBlocked(STDIN_FILENO);
    auto oninput = [](int event) {
        if (event & Event_Read) {
            char buf[1024];
            int n = read(STDIN_FILENO, buf, sizeof(buf));
            if (n > 0) {
                buf[n] = '\0';
                try {
                    CMDRegister::Instance()(buf);
                    cout << "> " << std::flush;
                } catch (ExitException &ex) {
                    InfoL << "ExitException";
                    kill(getpid(), SIGINT);
                } catch (std::exception &ex) {
                    cout << ex.what() << endl;
                }
            } else {
                DebugL << get_uv_errmsg();
                EventPollerPool::Instance().getFirstPoller()->delEvent(STDIN_FILENO);
            }
        }

        if (event & Event_Error) {
            WarnL << "Event_Error";
            EventPollerPool::Instance().getFirstPoller()->delEvent(STDIN_FILENO);
        }
    };
    EventPollerPool::Instance().getFirstPoller()->addEvent(STDIN_FILENO, Event_Read | Event_Error | Event_LT,oninput);
}
xiongziliang committed
210 211
#endif//!defined(_WIN32)

212 213
//全局变量,在WebApi中用于保存配置文件用
string g_ini_file;
214 215

int start_main(int argc,char *argv[]) {
xiongziliang committed
216 217 218 219 220 221 222 223
    {
        CMD_main cmd_main;
        try {
            cmd_main.operator()(argc, argv);
        } catch (std::exception &ex) {
            cout << ex.what() << endl;
            return -1;
        }
224

xiongziliang committed
225 226 227
        bool bDaemon = cmd_main.hasKey("daemon");
        LogLevel logLevel = (LogLevel) cmd_main["level"].as<int>();
        logLevel = MIN(MAX(logLevel, LTrace), LError);
228
        g_ini_file = cmd_main["config"];
xiongziliang committed
229 230
        string ssl_file = cmd_main["ssl"];
        int threads = cmd_main["threads"];
231

xiongziliang committed
232 233
        //设置日志
        Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel", logLevel));
234
#ifndef ANDROID
235 236 237 238
        auto fileChannel = std::make_shared<FileChannel>("FileChannel", exeDir() + "log/", logLevel);
        //日志最多保存天数
        fileChannel->setMaxDay(cmd_main["max_day"]);
        Logger::Instance().add(fileChannel);
239
#endif//
240

xiongziliang committed
241
#if !defined(_WIN32)
242
        pid_t pid = getpid();
xiongziliang committed
243 244 245 246
        if (bDaemon) {
            //启动守护进程
            System::startDaemon();
        }
xiongziliang committed
247 248
        //开启崩溃捕获等
        System::systemSetup();
xiongziliang committed
249
#endif//!defined(_WIN32)
xiongziliang committed
250

xiongziliang committed
251 252 253
        //启动异步日志线程
        Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
        //加载配置文件,如果配置文件不存在就创建一个
254
        loadIniConfig(g_ini_file.data());
xiongziliang committed
255

256 257 258 259 260 261 262
        if(!File::is_dir(ssl_file.data())){
            //不是文件夹,加载证书,证书包含公钥和私钥
            SSL_Initor::Instance().loadCertificate(ssl_file.data());
        }else{
            //加载文件夹下的所有证书
            File::scanDir(ssl_file,[](const string &path, bool isDir){
                if(!isDir){
xiongziliang committed
263
                    //最后的一个证书会当做默认证书(客户端ssl握手时未指定主机)
264 265 266 267 268
                    SSL_Initor::Instance().loadCertificate(path.data());
                }
                return true;
            });
        }
xiongziliang committed
269 270 271 272 273 274 275

        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];
        uint16_t httpPort = mINI::Instance()[Http::kPort];
        uint16_t httpsPort = mINI::Instance()[Http::kSSLPort];
276
        uint16_t rtp_proxy = mINI::Instance()[RtpProxy::kPort];
xiongziliang committed
277

xiongziliang committed
278
        //设置poller线程数,该函数必须在使用ZLToolKit网络相关对象之前调用才能生效
xiongziliang committed
279 280 281 282 283 284 285 286 287 288 289 290
        EventPollerPool::setPoolSize(threads);

        //简单的telnet服务器,可用于服务器调试,但是不能使用23端口,否则telnet上了莫名其妙的现象
        //测试方法:telnet 127.0.0.1 9000
        TcpServer::Ptr shellSrv(new TcpServer());
        TcpServer::Ptr rtspSrv(new TcpServer());
        TcpServer::Ptr rtmpSrv(new TcpServer());
        TcpServer::Ptr httpSrv(new TcpServer());
        //如果支持ssl,还可以开启https服务器
        TcpServer::Ptr httpsSrv(new TcpServer());
        //支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
        TcpServer::Ptr rtspSSLSrv(new TcpServer());
291

292 293 294 295 296
#if defined(ENABLE_RTPPROXY)
        UdpRecver recver;
        TcpServer::Ptr tcpRtpServer(new TcpServer());
#endif//defined(ENABLE_RTPPROXY)

297 298 299 300 301 302 303 304 305 306 307 308 309
        try {
            //rtsp服务器,端口默认554
            rtspSrv->start<RtspSession>(rtspPort);//默认554
            //rtsps服务器,端口默认322
            rtspSSLSrv->start<RtspSessionWithSSL>(rtspsPort);
            //rtmp服务器,端口默认1935
            rtmpSrv->start<RtmpSession>(rtmpPort);
            //http服务器,端口默认80
            httpSrv->start<HttpSession>(httpPort);
            //https服务器,端口默认443
            httpsSrv->start<HttpsSession>(httpsPort);
            //telnet远程调试服务器
            shellSrv->start<ShellSession>(shellPort);
310 311 312 313 314 315 316 317

#if defined(ENABLE_RTPPROXY)
            //创建rtp udp服务器
            recver.initSock(rtp_proxy);
            //创建rtp tcp服务器
            tcpRtpServer->start<RtpSession>(rtp_proxy);
#endif//defined(ENABLE_RTPPROXY)

318 319 320 321 322 323 324 325 326 327 328
        }catch (std::exception &ex){
            WarnL << "端口占用或无权限:" << ex.what() << endl;
            ErrorL << "程序启动失败,请修改配置文件中端口号后重试!" << endl;
            sleep(1);
#if !defined(_WIN32)
            if(pid != getpid()){
                kill(pid,SIGINT);
            }
#endif
            return -1;
        }
xiongziliang committed
329 330 331 332 333 334

        installWebApi();
        InfoL << "已启动http api 接口";
        installWebHook();
        InfoL << "已启动http hook 接口";

335
#if !defined(_WIN32) && !defined(ANDROID)
xiongziliang committed
336 337 338 339
        if (!bDaemon) {
            //交互式shell输入
            listen_shell_input();
        }
xiongziliang committed
340
#endif
341

xiongziliang committed
342 343 344 345
        //设置退出信号处理函数
        static semaphore sem;
        signal(SIGINT, [](int) {
            InfoL << "SIGINT:exit";
346
            signal(SIGINT, SIG_IGN);// 设置退出信号
xiongziliang committed
347 348
            sem.post();
        });// 设置退出信号
xiongziliang committed
349 350

#if !defined(_WIN32)
351
        signal(SIGHUP, [](int) { mediakit::loadIniConfig(g_ini_file.data()); });
xiongziliang committed
352
#endif
xiongziliang committed
353 354
        sem.wait();
    }
355 356
    unInstallWebApi();
    unInstallWebHook();
357
    Recorder::stopAll();
xiongziliang committed
358
    //休眠1秒再退出,防止资源释放顺序错误
359
    InfoL << "程序退出中,请等待...";
xiongziliang committed
360
    sleep(1);
361
    InfoL << "程序退出完毕!";
362 363 364
	return 0;
}

365 366 367 368 369 370 371
#ifndef DISABLE_MAIN
int main(int argc,char *argv[]) {
    return start_main(argc,argv);
}
#endif //DISABLE_MAIN