ZLMediaKit/tests/test_server.cpp

129 lines
3.8 KiB
C++
Raw Normal View History

2017-04-05 09:39:16 +08:00
//============================================================================
// Name : main.cpp
// Author : 熊子良
// Version :
//============================================================================
2017-08-09 18:39:30 +08:00
#include <map>
2017-04-05 09:39:16 +08:00
#include <signal.h>
#include <iostream>
2017-08-09 18:39:30 +08:00
#include "Common/config.h"
2017-04-05 09:39:16 +08:00
#include "Rtsp/UDPServer.h"
#include "Rtsp/RtspSession.h"
#include "Rtmp/RtmpSession.h"
#include "Http/HttpSession.h"
2017-05-03 00:12:51 +08:00
#ifdef ENABLE_OPENSSL
2017-04-19 17:47:07 +08:00
#include "Util/SSLBox.h"
2017-05-03 00:12:51 +08:00
#include "Http/HttpsSession.h"
#endif//ENABLE_OPENSSL
2017-08-09 18:39:30 +08:00
#include "Util/File.h"
2017-04-25 11:35:41 +08:00
#include "Util/logger.h"
#include "Util/onceToken.h"
#include "Network/TcpServer.h"
#include "Poller/EventPoller.h"
#include "Thread/WorkThreadPool.h"
2017-05-02 23:43:10 +08:00
#include "Device/PlayerProxy.h"
2017-08-09 18:39:30 +08:00
#if !defined(_WIN32)
2017-05-05 21:29:44 +08:00
#include "Shell/ShellSession.h"
2017-08-09 18:39:30 +08:00
using namespace ZL::Shell;
#endif // !defined(_WIN32)
2017-04-05 09:39:16 +08:00
using namespace std;
using namespace ZL::Util;
using namespace ZL::Http;
using namespace ZL::Rtsp;
using namespace ZL::Rtmp;
using namespace ZL::Thread;
using namespace ZL::Network;
2017-05-02 23:43:10 +08:00
using namespace ZL::DEV;
2017-04-05 09:39:16 +08:00
void programExit(int arg) {
EventPoller::Instance().shutdown();
}
int main(int argc,char *argv[]){
2017-08-09 18:39:30 +08:00
// setExePath(argv[0]);
2017-04-05 09:39:16 +08:00
signal(SIGINT, programExit);
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
2017-05-09 16:52:31 +08:00
Config::loaIniConfig();
2017-08-09 18:39:30 +08:00
DebugL << exePath();
2017-05-02 23:43:10 +08:00
//support rtmp and rtsp url
//just support H264+AAC
auto urlList = {"rtmp://live.hkstv.hk.lxdns.com/live/hks",
2017-08-03 13:55:46 +08:00
"rtsp://admin:jzan123456@192.168.0.122/"};
2017-05-02 23:43:10 +08:00
map<string , PlayerProxy::Ptr> proxyMap;
int i=0;
for(auto url : urlList){
2017-05-05 21:29:44 +08:00
//PlayerProxy构造函数前两个参数分别为应用名app,流idstreamId
2017-08-09 18:39:30 +08:00
//比如说应用为live流id为0那么直播地址为:
2017-05-05 21:29:44 +08:00
//http://127.0.0.1/live/0/hls.m3u8
//rtsp://127.0.0.1/live/0
//rtmp://127.0.0.1/live/0
2017-08-09 18:39:30 +08:00
//录像地址为:
2017-05-05 21:29:44 +08:00
//http://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
//rtsp://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
//rtmp://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
2017-06-03 10:52:49 +08:00
PlayerProxy::Ptr player(new PlayerProxy("live",to_string(i++).data()));
2017-08-03 13:55:46 +08:00
(*player)[PlayerProxy::kAliveSecond] = 10;//录制10秒
2017-05-02 23:43:10 +08:00
player->play(url);
proxyMap.emplace(string(url),player);
}
2017-05-03 00:12:51 +08:00
#ifdef ENABLE_OPENSSL
2017-05-02 23:43:10 +08:00
//请把证书"test_server.pem"放置在本程序可执行程序同目录下
try{
SSL_Initor::Instance().loadServerPem((exePath() + ".pem").data());
}catch(...){
FatalL << "请把证书:" << (exeName() + ".pem") << "放置在本程序可执行程序同目录下:" << exeDir() << endl;
return 0;
}
2017-05-03 00:12:51 +08:00
#endif //ENABLE_OPENSSL
2017-04-05 09:39:16 +08:00
TcpServer<RtspSession>::Ptr rtspSrv(new TcpServer<RtspSession>());
TcpServer<RtmpSession>::Ptr rtmpSrv(new TcpServer<RtmpSession>());
TcpServer<HttpSession>::Ptr httpSrv(new TcpServer<HttpSession>());
2017-08-09 18:39:30 +08:00
2017-05-03 00:12:51 +08:00
2017-04-05 09:39:16 +08:00
rtspSrv->start(mINI::Instance()[Config::Rtsp::kPort]);
rtmpSrv->start(mINI::Instance()[Config::Rtmp::kPort]);
httpSrv->start(mINI::Instance()[Config::Http::kPort]);
2017-05-03 00:12:51 +08:00
2017-08-09 18:39:30 +08:00
#if !defined(_WIN32)
2017-05-05 21:29:44 +08:00
//简单的telnet服务器可用于服务器调试但是不能使用23端口
//测试方法:telnet 127.0.0.1 8023
//输入用户名和密码登录(user:test,pwd:123456)输入help命令查看帮助
2017-08-09 18:39:30 +08:00
TcpServer<ShellSession>::Ptr shellSrv(new TcpServer<ShellSession>());
2017-05-05 21:29:44 +08:00
ShellSession::addUser("test","123456");
shellSrv->start(8023);
2017-08-09 18:39:30 +08:00
#endif // !defined(_WIN32)
2017-05-03 00:12:51 +08:00
#ifdef ENABLE_OPENSSL
TcpServer<HttpsSession>::Ptr httpsSrv(new TcpServer<HttpsSession>());
2017-04-19 17:47:07 +08:00
httpsSrv->start(mINI::Instance()[Config::Http::kSSLPort]);
2017-05-03 00:12:51 +08:00
#endif //ENABLE_OPENSSL
2017-04-05 09:39:16 +08:00
EventPoller::Instance().runLoop();
2017-05-05 21:29:44 +08:00
proxyMap.clear();
2017-04-05 09:39:16 +08:00
rtspSrv.reset();
rtmpSrv.reset();
httpSrv.reset();
2017-08-09 18:39:30 +08:00
#if !defined(_WIN32)
2017-05-05 21:29:44 +08:00
shellSrv.reset();
2017-08-09 18:39:30 +08:00
#endif // !defined(_WIN32)
2017-05-03 00:12:51 +08:00
#ifdef ENABLE_OPENSSL
2017-04-19 17:47:07 +08:00
httpsSrv.reset();
2017-05-03 00:12:51 +08:00
#endif //ENABLE_OPENSSL
2017-05-02 23:43:10 +08:00
UDPServer::Destory();
WorkThreadPool::Destory();
EventPoller::Destory();
Logger::Destory();
2017-04-05 09:39:16 +08:00
return 0;
}