1 Star 0 Fork 72

玉念聿辉/SimpleMediaServer(流媒体)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.cpp 18.74 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
#ifdef ENABLE_RTSP
#include "Rtsp/RtspServer.h"
#include "Rtsp/RtspClient.h"
#endif
#if defined(ENABLE_HTTP) || defined(ENABLE_API) || defined(ENABLE_HLS)
#include "Http/HttpServer.h"
#include "Http/HttpClientApi.h"
#endif
#if defined(ENABLE_GB28181) || defined(ENABLE_EHOME2) || defined(ENABLE_EHOME5)
#include "GB28181/GB28181Server.h"
#endif
#ifdef ENABLE_EHOME2
#include "Ehome2/Ehome2Server.h"
#endif
#ifdef ENABLE_EHOME5
#include "Ehome5/Ehome5Server.h"
#endif
#ifdef ENABLE_RTMP
#include "Rtmp/RtmpServer.h"
#include "Rtmp/RtmpClient.h"
#endif
#ifdef ENABLE_WEBRTC
#include "Webrtc/WebrtcServer.h"
#include "Webrtc/WebrtcClient.h"
#endif
#ifdef ENABLE_SRT
#include "Srt/SrtServer.h"
#endif
#ifdef ENABLE_JT1078
#include "JT1078/JT1078Server.h"
#endif
#if defined(ENABLE_GB28181) || defined(ENABLE_EHOME2) || defined(ENABLE_EHOME5) || defined(ENABLE_RTSP) || defined(ENABLE_WEBRTC) || defined(ENABLE_RTP)
#include "Rtp/RtpServer.h"
#endif
#ifdef ENABLE_OPENSSL
#include "Ssl/TlsContext.h"
#endif
#ifdef ENABLE_HOOK
#include "Hook/MediaHook.h"
#endif
#ifdef ENABLE_RECORD
#include "Record/RecordReader.h"
#endif
#ifdef ENABLE_API
#include "Api/HttpApi.h"
#include "Api/RtmpApi.h"
#include "Api/RtspApi.h"
#include "Api/GB28181Api.h"
#include "Api/WebrtcApi.h"
#include "Api/HttpStreamApi.h"
#include "Api/RecordApi.h"
#include "Api/TestApi.h"
#include "Api/JT1078Api.h"
#include "Api/FfmpegApi.h"
#include "Api/HookApi.h"
#include "Api/RtpApi.h"
#include "Api/WebsocketApi.h"
#include "Api/SrtApi.h"
#include "Api/VodApi.h"
#endif
#include "Codec/AacTrack.h"
#include "Codec/AV1Track.h"
#include "Codec/Mp3Track.h"
#include "Codec/G711Track.h"
#include "Codec/AdpcmaTrack.h"
#include "Codec/OpusTrack.h"
#include "Codec/H264Track.h"
#include "Codec/H265Track.h"
#include "Codec/H266Track.h"
#include "Codec/VP8Track.h"
#include "Codec/VP9Track.h"
#include "Codec/AacFrame.h"
#include "Codec/AV1Frame.h"
#include "Codec/H264Frame.h"
#include "Codec/H265Frame.h"
#include "Codec/H266Frame.h"
#include "Codec/VP8Frame.h"
#include "Codec/VP9Frame.h"
#include "Log/Logger.h"
#include "EventLoopPool.h"
#include "WorkPoller/WorkLoopPool.h"
#include "Common/Config.h"
#include "Util/Thread.h"
#include "Common/Heartbeat.h"
#include <unistd.h>
#include <string.h>
#include <sys/resource.h>
using namespace std;
// static unordered_map<string, int> options {
// "-c" : 1
// };
void setFileLimits()
{
struct rlimit limitOld, limitNew;
if (getrlimit(RLIMIT_NOFILE, &limitOld)==0) {
limitNew.rlim_cur = limitNew.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_NOFILE, &limitNew)!=0) {
limitNew.rlim_cur = limitNew.rlim_max = limitOld.rlim_max;
setrlimit(RLIMIT_NOFILE, &limitNew);
}
logInfo << "文件最大描述符个数设置为:" << limitNew.rlim_cur;
}
}
void setCoreLimits()
{
struct rlimit limitOld, limitNew;
if (getrlimit(RLIMIT_CORE, &limitOld)==0) {
limitNew.rlim_cur = limitNew.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &limitNew)!=0) {
limitNew.rlim_cur = limitNew.rlim_max = limitOld.rlim_max;
setrlimit(RLIMIT_CORE, &limitNew);
}
logInfo << "core文件大小设置为:" << limitNew.rlim_cur;
}
}
int main(int argc, char** argv)
{
Thread::setThreadName("SMS-main");
string configPath = "./server.json";
for (int i = 0; i < argc; ++i) {
if (!strcmp(argv[i], "-c")) {
configPath = argv[++i];
}
}
// 多线程开启epoll
EventLoopPool::instance()->init(0, true, true);
WorkLoopPool::instance()->init(0, true, true);
#ifdef ENABLE_SRT
SrtEventLoopPool::instance()->init(1, true, true);
#endif
// config
Config::instance()->load(configPath);
auto configJson = Config::instance()->getConfig();
int consoleLog = Config::instance()->get("Log", "console");
if (consoleLog) {
Logger::instance()->addChannel(std::make_shared<ConsoleChannel>("ConsoleChannel", LTrace));
}
string prefixname = Config::instance()->get("Log", "prefixname", "", "", "SimpleMediaServer");
auto fileChannel = std::make_shared<FileChannel>("FileChannel", Path::exeDir() + "log/", prefixname, LTrace);
//日志最多保存天数
static int ilogmaxday = Config::instance()->getAndListen([fileChannel](const json &config){
ilogmaxday = Config::instance()->get("Log", "maxDay");
fileChannel->setMaxDay(ilogmaxday);
}, "Log", "maxDay");
fileChannel->setMaxDay(ilogmaxday);
//配置日志最大保留个数和单个日志切片大小
static int ilogmaxsize = Config::instance()->getAndListen([fileChannel](const json &config){
ilogmaxsize = Config::instance()->get("Log", "maxSize");
fileChannel->setFileMaxSize(ilogmaxsize);
}, "Log", "maxSize");
fileChannel->setFileMaxSize(ilogmaxsize);
static int ilogmaxcount = Config::instance()->getAndListen([fileChannel](const json &config){
ilogmaxcount = Config::instance()->get("Log", "maxCount");
fileChannel->setFileMaxCount(ilogmaxcount);
}, "Log", "maxCount");
fileChannel->setFileMaxCount(ilogmaxcount);
Logger::instance()->addChannel(fileChannel);
Logger::instance()->setWriter(std::make_shared<AsyncLogWriter>());
static int logLevel = Config::instance()->getAndListen([](const json &config){
logLevel = config["Log"]["logLevel"];
Logger::instance()->setLevel((LogLevel)logLevel);
}, "Log", "logLevel");
Logger::instance()->setLevel((LogLevel)logLevel);
setFileLimits();
setCoreLimits();
#ifdef ENABLE_OPENSSL
auto sslKey = Config::instance()->get("Ssl", "key");
auto sslCrt = Config::instance()->get("Ssl", "cert");
TlsContext::setKeyFile(sslKey, sslCrt);
#endif
#ifdef ENABLE_HOOK
MediaHook::instance()->init();
HookManager::instance()->setOnHookReportByHttp(HttpClientApi::reportByHttp);
#endif
#ifdef ENABLE_RECORD
RecordReader::init();
#endif
#ifdef ENABLE_API
#if defined(ENABLE_HTTP) || defined(ENABLE_API) || defined(ENABLE_HLS)
HttpApi::initApi();
WebsocketApi::initApi();
HttpStreamApi::initApi();
#endif
#ifdef ENABLE_HOOK
HookApi::initApi();
#endif
#ifdef ENABLE_RTMP
RtmpApi::initApi();
#endif
#ifdef ENABLE_RTSP
RtspApi::initApi();
#endif
#ifdef ENABLE_GB28181
GB28181Api::initApi();
#endif
#ifdef ENABLE_RTP
RtpApi::initApi();
#endif
#ifdef ENABLE_WEBRTC
WebrtcApi::initApi();
#endif
#ifdef ENABLE_RECORD
RecordApi::initApi();
#endif
#ifdef ENABLE_API
TestApi::initApi();
#endif
#ifdef ENABLE_JT1078
JT1078Api::initApi();
#endif
#ifdef ENABLE_FFMPEG
FfmpegApi::initApi();
#endif
#ifdef ENABLE_SRT
SrtApi::initApi();
#endif
VodApi::initApi();
#endif
AacTrack::registerTrackInfo();
AV1Track::registerTrackInfo();
Mp3Track::registerTrackInfo();
G711aTrack::registerTrackInfo();
G711uTrack::registerTrackInfo();
AdpcmaTrack::registerTrackInfo();
OpusTrack::registerTrackInfo();
H264Track::registerTrackInfo();
H265Track::registerTrackInfo();
H266Track::registerTrackInfo();
VP8Track::registerTrackInfo();
VP9Track::registerTrackInfo();
AacFrame::registerFrame();
AV1Frame::registerFrame();
H264Frame::registerFrame();
H265Frame::registerFrame();
H266Frame::registerFrame();
VP8Frame::registerFrame();
VP9Frame::registerFrame();
Heartbeat::Ptr beat = make_shared<Heartbeat>();
beat->startAsync();
#ifdef ENABLE_RTSP
// 开启RTSP SERVER
// 参数需要改成从配置读取
RtspClient::init();
auto rtspConfigVec = configJson["Rtsp"]["Server"];
for (auto server : rtspConfigVec.items()) {
string serverId = server.key();
auto rtspConfig = server.value();
if (!rtspConfig.is_object()) {
continue;
}
const string ip = rtspConfig["ip"];
int port = rtspConfig["port"];
int sslPort = rtspConfig["sslPort"];
int count = rtspConfig["threads"];
logInfo << "start rtsp server, port: " << port;
if (port) {
RtspServer::instance()->start(ip, port, count);
}
logInfo << "start rtsps server, sslPort: " << sslPort;
if (sslPort) {
RtspServer::instance()->start(ip, sslPort, count, true);
}
}
#endif
#ifdef ENABLE_RTMP
RtmpClient::init();
auto rtmpConfigVec = configJson["Rtmp"]["Server"];
for (auto server : rtmpConfigVec.items()) {
string serverId = server.key();
auto rtmpConfig = server.value();
if (!rtmpConfig.is_object()) {
continue;
}
const string ip = rtmpConfig["ip"];
int port = rtmpConfig["port"];
int count = rtmpConfig["threads"];
logInfo << "start rtmp server, port: " << port;
if (port) {
RtmpServer::instance()->start(ip, port, count);
}
// logInfo << "start rtsps server, sslPort: " << sslPort;
// if (sslPort) {
// RtspServer::instance()->start(ip, sslPort, count);
// }
}
#endif
#if defined(ENABLE_GB28181) || defined(ENABLE_EHOME2) || defined(ENABLE_EHOME5)
auto GB28181ConfigVec = configJson["GB28181"]["Server"];
for (auto server : GB28181ConfigVec.items()) {
string serverId = server.key();
auto GB28181Config = server.value();
if (!GB28181Config.is_object()) {
continue;
}
const string ip = GB28181Config["ip"];
int port = GB28181Config["port"];
int count = GB28181Config["threads"];
int sockType = GB28181Config["sockType"];
logInfo << "start gb28181 server, port: " << port;
if (port) {
GB28181Server::instance()->start(ip, port, count, sockType);
}
// logInfo << "start rtsps server, sslPort: " << sslPort;
// if (sslPort) {
// RtspServer::instance()->start(ip, sslPort, count);
// }
}
#endif
#if defined(ENABLE_GB28181) || defined(ENABLE_EHOME2) || defined(ENABLE_EHOME5) || defined(ENABLE_RTSP) || defined(ENABLE_WEBRTC) || defined(ENABLE_RTP)
auto rtpConfigVec = configJson["Rtp"]["Server"];
for (auto server : rtpConfigVec.items()) {
string serverId = server.key();
auto rtpConfig = server.value();
if (!rtpConfig.is_object()) {
continue;
}
const string ip = rtpConfig["ip"];
int port = rtpConfig["port"];
int count = rtpConfig["threads"];
int sockType = rtpConfig["sockType"];
logInfo << "start rtp server, port: " << port;
if (port) {
RtpServer::instance()->start(ip, port, count, sockType);
}
// logInfo << "start rtsps server, sslPort: " << sslPort;
// if (sslPort) {
// RtspServer::instance()->start(ip, sslPort, count);
// }
}
#endif
#ifdef ENABLE_EHOME2
auto ehome2ConfigVec = configJson["Ehome2"]["Server"];
for (auto server : ehome2ConfigVec.items()) {
string serverId = server.key();
auto ehomeConfig = server.value();
if (!ehomeConfig.is_object()) {
continue;
}
const string ip = ehomeConfig["ip"];
int port = ehomeConfig["port"];
int count = ehomeConfig["threads"];
int sockType = ehomeConfig["sockType"];
logInfo << "start ehome2 server, port: " << port;
if (port) {
Ehome2Server::instance()->start(ip, port, count, sockType);
}
// logInfo << "start rtsps server, sslPort: " << sslPort;
// if (sslPort) {
// RtspServer::instance()->start(ip, sslPort, count);
// }
}
#endif
#ifdef ENABLE_EHOME5
auto ehome5ConfigVec = configJson["Ehome5"]["Server"];
for (auto server : ehome5ConfigVec.items()) {
string serverId = server.key();
auto ehomeConfig = server.value();
if (!ehomeConfig.is_object()) {
continue;
}
const string ip = ehomeConfig["ip"];
int port = ehomeConfig["port"];
int count = ehomeConfig["threads"];
int sockType = ehomeConfig["sockType"];
logInfo << "start ehome5 server, port: " << port;
if (port) {
Ehome5Server::instance()->start(ip, port, count, sockType);
}
// logInfo << "start rtsps server, sslPort: " << sslPort;
// if (sslPort) {
// RtspServer::instance()->start(ip, sslPort, count);
// }
}
#endif
#ifdef ENABLE_JT1078
auto jt1078ConfigVec = configJson["JT1078"]["Server"];
int maxPort = jt1078ConfigVec.value("portMax", 0);
int minPort = jt1078ConfigVec.value("portMin", 0);
JT1078Server::instance()->setPortRange(minPort, maxPort);
for (auto server : jt1078ConfigVec.items()) {
string serverId = server.key();
auto jt1078Config = server.value();
if (!jt1078Config.is_object()) {
continue;
}
const string ip = jt1078Config["ip"];
int port = jt1078Config["port"];
int count = jt1078Config["threads"];
int isTalk = jt1078Config.value("isTalk", 0);
if (isTalk) {
logInfo << "start jt1078 talk server, port: " << port;
} else {
logInfo << "start jt1078 server, port: " << port;
}
if (port) {
JT1078Server::instance()->start(ip, port, count, isTalk);
}
// logInfo << "start 1078 server, sslPort: " << sslPort;
// if (sslPort) {
// JT1078Server::instance()->start(ip, sslPort, count, true);
// }
}
#endif
#if defined(ENABLE_HTTP) || defined(ENABLE_API) || defined(ENABLE_HLS)
auto websocketConfigVec = configJson["Websocket"]["Server"];
for (auto server : websocketConfigVec.items()) {
string serverId = server.key();
auto websocketConfig = server.value();
if (!websocketConfig.is_object()) {
continue;
}
const string ip = websocketConfig["ip"];
int port = websocketConfig["port"];
int sslPort = websocketConfig["sslPort"];
int count = websocketConfig["threads"];
logInfo << "start websocket server, port: " << port;
if (port) {
HttpServer::instance()->start(ip, port, count, false, true);
}
logInfo << "start websockets server, sslPort: " << sslPort;
if (sslPort) {
HttpServer::instance()->start(ip, sslPort, count, true, true);
}
}
// http server放到最后,等其他协议的api加到httpApi中的mapApi里,避免多线程使用mapApi
auto httpApiConfigVec = configJson["Http"]["Api"];
for (auto server : httpApiConfigVec.items()) {
string serverId = server.key();
auto httpApiConfig = server.value();
if (!httpApiConfig.is_object()) {
continue;
}
const string ip = httpApiConfig["ip"];
int port = httpApiConfig["port"];
int sslPort = httpApiConfig["sslPort"];
int count = httpApiConfig["threads"];
logInfo << "start http api, port: " << port;
if (port) {
HttpServer::instance()->start(ip, port, count);
}
logInfo << "start https api, sslPort: " << sslPort;
if (sslPort) {
HttpServer::instance()->start(ip, sslPort, count, true);
}
}
auto httpServerConfigVec = configJson["Http"]["Server"];
for (auto server : httpServerConfigVec.items()) {
string serverId = server.key();
auto httpServerConfig = server.value();
if (!httpServerConfig.is_object()) {
continue;
}
const string ip = httpServerConfig["ip"];
int port = httpServerConfig["port"];
int sslPort = httpServerConfig["sslPort"];
int count = httpServerConfig["threads"];
logInfo << "start http server, port: " << port;
if (port) {
HttpServer::instance()->start(ip, port, count);
}
logInfo << "start https server, sslPort: " << sslPort;
if (sslPort) {
HttpServer::instance()->start(ip, sslPort, count, true);
}
}
#endif
#ifdef ENABLE_WEBRTC
WebrtcClient::init();
auto WebrtcConfigVec = configJson["Webrtc"]["Server"];
for (auto server : WebrtcConfigVec.items()) {
string serverId = server.key();
auto WebrtcConfig = server.value();
if (!WebrtcConfig.is_object()) {
continue;
}
const string ip = WebrtcConfig["ip"];
int port = WebrtcConfig["port"];
int count = WebrtcConfig["threads"];
int sockType = WebrtcConfig["sockType"];
logInfo << "start webrtc server, port: " << port;
if (port) {
WebrtcServer::instance()->start(ip, port, count, sockType);
// RtcServer::Instance().Start(EventLoopPool::instance()->getLoopByCircle(), port, "0.0.0.0");
}
// logInfo << "start rtsps server, sslPort: " << sslPort;
// if (sslPort) {
// RtspServer::instance()->start(ip, sslPort, count);
// }
}
#endif
#ifdef ENABLE_SRT
SrtSocket::initSrt();
auto srtConfigVec = configJson["Srt"]["Server"];
for (auto server : srtConfigVec.items()) {
string serverId = server.key();
auto srtConfig = server.value();
if (!srtConfig.is_object()) {
continue;
}
const string ip = srtConfig["ip"];
int port = srtConfig["port"];
int count = srtConfig["threads"];
logInfo << "start srt server, port: " << port;
if (port) {
SrtServer::instance()->start(ip, port, count, 0);
// RtcServer::Instance().Start(EventLoopPool::instance()->getLoopByCircle(), port, "0.0.0.0");
}
// logInfo << "start rtsps server, sslPort: " << sslPort;
// if (sslPort) {
// RtspServer::instance()->start(ip, sslPort, count);
// }
}
#endif
// auto loop = EventLoopPool::instance()->getLoopByCircle();
// TcpClient::Ptr client = make_shared<TcpClient>(loop);
// loop->async([client]() {
// client->create("");
// client->connect("127.0.0.1", 9000);
// }, true);
while (true) {
// TODO 可做一些巡检工作
EventLoopPool::instance()->for_each_loop([](const EventLoop::Ptr &loop){
int lastWaitDuration, lastRunDuration, curWaitDuration, curRunDuration;
loop->getLoad(lastWaitDuration, lastRunDuration, curWaitDuration, curRunDuration);
if (curRunDuration > 1000 || lastRunDuration > 1000) {
logWarn << "thread: looper-" << loop->getEpollFd()
<< ", lastWaitDuration: " << lastWaitDuration
<< ", lastRunDuration: " << lastRunDuration
<< ", curWaitDuration: " << curWaitDuration
<< ", curRunDuration: " << curRunDuration;
}
});
sleep(5);
}
#ifdef ENABLE_SRT
SrtSocket::uninitSrt();
#endif
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/yunianvh/simple-media-server.git
git@gitee.com:yunianvh/simple-media-server.git
yunianvh
simple-media-server
SimpleMediaServer(流媒体)
master

搜索帮助