/* * 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. */ #include "common.h" #include #include #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" 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]; #ifdef ENABLE_RTPPROXY #include "Rtp/UdpRecver.h" #include "Rtp/RtpSession.h" static std::shared_ptr udpRtpServer; static TcpServer::Ptr tcpRtpServer(new TcpServer()); #endif //////////////////////////environment init/////////////////////////// API_EXPORT void API_CALL mk_env_init(const config *cfg) { assert(cfg); static onceToken token([&]() { Logger::Instance().add(std::make_shared("console", (LogLevel) cfg->log_level)); Logger::Instance().setWriter(std::make_shared()); EventPollerPool::setPoolSize(cfg->thread_num); WorkThreadPool::setPoolSize(cfg->thread_num); if (cfg->ini) { //设置配置文件 if (cfg->ini_is_path) { mINI::Instance().parseFile(cfg->ini); } else { mINI::Instance().parse(cfg->ini); } } if (cfg->ssl) { //设置ssl证书 SSL_Initor::Instance().loadCertificate(cfg->ssl, true, cfg->ssl_pwd ? cfg->ssl_pwd : "", cfg->ssl_is_path); } }); } API_EXPORT void API_CALL mk_set_option(const char *key, const char *val) { assert(key && val); 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 { http_server[ssl] = std::make_shared(); if(ssl){ http_server[ssl]->start >(port); } else{ http_server[ssl]->start(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 { rtsp_server[ssl] = std::make_shared(); if(ssl){ rtsp_server[ssl]->start >(port); }else{ rtsp_server[ssl]->start(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 { rtmp_server[ssl] = std::make_shared(); if(ssl){ rtmp_server[ssl]->start >(port); }else{ rtmp_server[ssl]->start(port); } return rtmp_server[ssl]->getPort(); } catch (std::exception &ex) { rtmp_server[ssl].reset(); WarnL << ex.what(); return 0; } } API_EXPORT uint16_t API_CALL mk_rtp_server_start(uint16_t port){ #ifdef ENABLE_RTPPROXY try { //创建rtp tcp服务器 tcpRtpServer = std::make_shared(); tcpRtpServer->start(port); //创建rtp udp服务器 auto ret = tcpRtpServer->getPort(); udpRtpServer = std::make_shared(); udpRtpServer->initSock(port); return ret; } catch (std::exception &ex) { tcpRtpServer.reset(); udpRtpServer.reset(); WarnL << ex.what(); return 0; } #else WarnL << "未启用该功能!"; return 0; #endif } API_EXPORT void API_CALL mk_log_printf(int level, const char *file, const char *function, int line, const char *fmt, ...) { assert(file && function && fmt); LogContextCapturer info(Logger::Instance(), (LogLevel) level, file, function, line); va_list pArg; va_start(pArg, fmt); char buf[4096]; int n = vsprintf(buf, fmt, pArg); buf[n] = '\0'; va_end(pArg); info << buf; }