mk_common.cpp 6.55 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
 * MIT License
 *
 * Copyright (c) 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.
 */

27
#include "mk_common.h"
28 29 30 31 32 33 34 35 36 37
#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"
xiongziliang committed
38
#include "Shell/ShellSession.h"
39 40 41 42 43 44 45
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];
xiongziliang committed
46
static TcpServer::Ptr shell_server;
47

48 49 50 51
#ifdef ENABLE_RTPPROXY
#include "Rtp/UdpRecver.h"
#include "Rtp/RtpSession.h"
static std::shared_ptr<UdpRecver> udpRtpServer;
xiongziliang committed
52
static TcpServer::Ptr tcpRtpServer;
53 54
#endif

55
//////////////////////////environment init///////////////////////////
xiongziliang committed
56
API_EXPORT void API_CALL mk_env_init(const mk_config *cfg) {
xiongziliang committed
57
    assert(cfg);
58 59 60 61 62 63 64 65 66
    mk_env_init1(cfg->thread_num,
                 cfg->log_level,
                 cfg->ini_is_path,
                 cfg->ini,
                 cfg->ssl_is_path,
                 cfg->ssl,
                 cfg->ssl_pwd);
}

67
extern void stopAllTcpServer();
68

69 70 71 72
API_EXPORT void API_CALL mk_stop_all_server(){
    CLEAR_ARR(rtsp_server);
    CLEAR_ARR(rtmp_server);
    CLEAR_ARR(http_server);
xiongziliang committed
73
#ifdef ENABLE_RTPPROXY
74 75
    udpRtpServer = nullptr;
    tcpRtpServer = nullptr;
xiongziliang committed
76
#endif
77
    stopAllTcpServer();
78 79
}

80 81 82 83 84 85 86 87
API_EXPORT void API_CALL mk_env_init1(  int thread_num,
                                        int log_level,
                                        int ini_is_path,
                                        const char *ini,
                                        int ssl_is_path,
                                        const char *ssl,
                                        const char *ssl_pwd) {

88
    static onceToken token([&]() {
89
        Logger::Instance().add(std::make_shared<ConsoleChannel>("console", (LogLevel) log_level));
90 91
        Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());

92 93
        EventPollerPool::setPoolSize(thread_num);
        WorkThreadPool::setPoolSize(thread_num);
94

95
        if (ini && ini[0]) {
96
            //设置配置文件
97
            if (ini_is_path) {
98 99 100 101 102 103
                try{
                    mINI::Instance().parseFile(ini);
                }catch (std::exception &ex) {
                    InfoL << "dump ini file to:" << ini;
                    mINI::Instance().dumpFile(ini);
                }
104
            } else {
105
                mINI::Instance().parse(ini);
106 107 108
            }
        }

109
        if (ssl && ssl[0]) {
110
            //设置ssl证书
111
            SSL_Initor::Instance().loadCertificate(ssl, true, ssl_pwd ? ssl_pwd : "", ssl_is_path);
112 113 114 115 116
        }
    });
}

API_EXPORT void API_CALL mk_set_option(const char *key, const char *val) {
xiongziliang committed
117
    assert(key && val);
118 119 120 121 122 123 124 125 126 127
    if (mINI::Instance().find(key) == mINI::Instance().end()) {
        WarnL << "key:" << key << " not existed!";
        return;
    }
    mINI::Instance()[key] = val;
}

API_EXPORT uint16_t API_CALL mk_http_server_start(uint16_t port, int ssl) {
    ssl = MAX(0,MIN(ssl,1));
    try {
xiongziliang committed
128
        http_server[ssl] = std::make_shared<TcpServer>();
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        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 {
xiongziliang committed
145
        rtsp_server[ssl] = std::make_shared<TcpServer>();
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        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 {
xiongziliang committed
162
        rtmp_server[ssl] = std::make_shared<TcpServer>();
163 164 165 166 167 168 169 170 171 172 173 174 175
        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;
    }
}

176 177 178 179
API_EXPORT uint16_t API_CALL mk_rtp_server_start(uint16_t port){
#ifdef ENABLE_RTPPROXY
    try {
        //创建rtp tcp服务器
180
        tcpRtpServer = std::make_shared<TcpServer>();
181
        tcpRtpServer->start<RtpSession>(port);
182 183

        //创建rtp udp服务器
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        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
}

xiongziliang committed
200 201 202 203 204 205 206 207 208 209 210
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;
    }
}