适配ZLToolKit代码,简化程序退出流程代码

This commit is contained in:
xiongziliang 2018-12-28 16:47:50 +08:00
parent 315cd180d4
commit cc7844a994
11 changed files with 337 additions and 387 deletions

@ -1 +1 @@
Subproject commit d3a1bd9d8a3162d0237954034d85f1313008cab2
Subproject commit 98d824bdc6604b4e761657922c913ea46bd90223

View File

@ -26,17 +26,13 @@
#include "UDPServer.h"
#include "Util/TimeTicker.h"
#include "Util/onceToken.h"
using namespace toolkit;
namespace mediakit {
UDPServer &UDPServer::Instance() {
static UDPServer *instance(new UDPServer());
return *instance;
}
void UDPServer::Destory() {
delete &UDPServer::Instance();
}
INSTANCE_IMP(UDPServer);
UDPServer::UDPServer() {
}

View File

@ -27,8 +27,9 @@
#ifndef RTSP_UDPSERVER_H_
#define RTSP_UDPSERVER_H_
#include <mutex>
#include <stdint.h>
#include <mutex>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include "Util/util.h"
@ -40,18 +41,23 @@ using namespace toolkit;
namespace mediakit {
class UDPServer {
class UDPServer : public std::enable_shared_from_this<UDPServer> {
public:
typedef function< bool(int, const Buffer::Ptr &, struct sockaddr *)> onRecvData;
UDPServer();
virtual ~UDPServer();
~UDPServer();
static UDPServer &Instance();
static void Destory();
/**
*
* @deprecated
*/
static void Destory(){};
Socket::Ptr getSock(const char *strLocalIp, int iTrackIndex,uint16_t iLocalPort = 0);
void listenPeer(const char *strPeerIp, void *pSelf, const onRecvData &cb);
void stopListenPeer(const char *strPeerIp, void *pSelf);
private:
UDPServer();
void onRcvData(int iTrackId, const Buffer::Ptr &pBuf,struct sockaddr *pPeerAddr);
void onErr(const string &strKey,const SockException &err);
unordered_map<string, Socket::Ptr> _mapUpdSock;

View File

@ -142,7 +142,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
return false;
}
WarnL << "不支持的rtp类型:" << nal.type << " " << rtppack->sequence;
WarnL << "不支持的rtp类型:" << (int)nal.type << " " << rtppack->sequence;
return false;
// 29 FU-B 单NAL单元B模式
// 24 STAP-A 单一时间的组合包

View File

@ -41,9 +41,10 @@ using namespace mediakit;
int main(int argc, char *argv[]) {
//设置退出信号处理函数
signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); });
signal(SIGINT, [](int) { EventPollerPool::Instance().shutdown(); });
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
if (argc != 5) {
@ -55,7 +56,6 @@ int main(int argc, char *argv[]) {
return 0;
}
{
list<MediaPlayer::Ptr> playerList;
auto playerCnt = atoi(argv[1]);//启动的播放器个数
atomic_int alivePlayerCnt(0);
@ -80,17 +80,9 @@ int main(int argc, char *argv[]) {
InfoL << "存活播放器个数:" << alivePlayerCnt.load();
return true;
});
EventPoller::Instance().runLoop();
AsyncTaskThread::Instance().CancelTask(0);
}
static onceToken token(nullptr, []() {
WorkThreadPool::Instance();
UDPServer::Destory();
EventPoller::Destory();
AsyncTaskThread::Destory();
Logger::Destory();
});
EventPollerPool::Instance().wait();
AsyncTaskThread::Instance().CancelTask(0);
return 0;
}

View File

@ -102,10 +102,11 @@ static onceToken s_token([](){
int main(int argc,char *argv[]){
//设置退出信号处理函数
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
signal(SIGINT, [](int){EventPollerPool::Instance().shutdown();});
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
//加载配置文件,如果配置文件不存在就创建一个
loadIniConfig();
@ -132,14 +133,7 @@ int main(int argc,char *argv[]){
InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1" << endl;
EventPoller::Instance().runLoop();
static onceToken s_token(nullptr,[]() {
//TcpServer用到了WorkThreadPool
WorkThreadPool::Destory();
EventPoller::Destory();
Logger::Destory();
});
EventPollerPool::Instance().wait();
return 0;
}

View File

@ -41,11 +41,11 @@ using namespace mediakit;
int main(int argc, char *argv[]) {
//设置退出信号处理函数
signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); });
signal(SIGINT, [](int) { EventPollerPool::Instance().shutdown(); });
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
{
///////////////////////////////http downloader///////////////////////
//下载器map
map<string, HttpDownloader::Ptr> downloaderMap;
@ -183,12 +183,7 @@ int main(int argc, char *argv[]) {
});
//事件轮询
EventPoller::Instance().runLoop();
}
//程序开始退出
EventPoller::Destory();
AsyncTaskThread::Destory();
Logger::Destory();
EventPollerPool::Instance().wait();
return 0;
}

View File

@ -43,7 +43,8 @@ int main(int argc, char *argv[]) {
//设置退出信号处理函数
signal(SIGINT, [](int) { SDLDisplayerHelper::Instance().shutdown(); });
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
if (argc != 3) {
ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n"
@ -54,7 +55,6 @@ int main(int argc, char *argv[]) {
}
{
MediaPlayer::Ptr player(new MediaPlayer());
weak_ptr<MediaPlayer> weakPlayer = player;
player->setOnPlayResult([weakPlayer](const SockException &ex) {
@ -93,12 +93,8 @@ int main(int argc, char *argv[]) {
});
(*player)[RtspPlayer::kRtpType] = atoi(argv[2]);
player->play(argv[1]);
SDLDisplayerHelper::Instance().runLoop();
}
UDPServer::Destory();
EventPoller::Destory();
AsyncTaskThread::Destory();
Logger::Destory();
return 0;
}

View File

@ -83,12 +83,11 @@ void rePushDelay(const string &app, const string &stream, const string &url) {
//这里才是真正执行main函数你可以把函数名(domain)改成main然后就可以输入自定义url了
int domain(const string &playUrl, const string &pushUrl) {
//设置退出信号处理函数
signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); });
signal(SIGINT, [](int) { EventPollerPool::Instance().shutdown(); });
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
{
//拉一个流生成一个RtmpMediaSource源的名称是"app/stream"
//你也可以以其他方式生成RtmpMediaSource比如说MP4文件请查看test_rtmpPusherMp4.cpp代码
PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "app", "stream"));
@ -102,18 +101,8 @@ int domain(const string &playUrl, const string &pushUrl) {
createPusher(app, stream, pushUrl);
}
});
//事件轮询
EventPoller::Instance().runLoop();
pusher.reset();
}
//删除事件监听
NoticeCenter::Instance().delListener(nullptr);
//清理程序
EventPoller::Destory();
AsyncTaskThread::Destory();
Logger::Destory();
EventPollerPool::Instance().wait();
return 0;
}

View File

@ -92,9 +92,9 @@ void rePushDelay(const string &app,const string &stream,const string &url){
//这里才是真正执行main函数你可以把函数名(domain)改成main然后就可以输入自定义url了
int domain(const string & filePath,const string & pushUrl){
//设置退出信号处理函数
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
signal(SIGINT, [](int){EventPollerPool::Instance().shutdown();});
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
//录像应用名称默认为record
@ -104,16 +104,7 @@ int domain(const string & filePath,const string & pushUrl){
createPusher(appName,filePath,pushUrl);
//开始事件轮询
EventPoller::Instance().runLoop();
//删除事件监听
NoticeCenter::Instance().delListener(nullptr);
//销毁推流器
pusher.reset();
//程序清理
EventPoller::Destory();
AsyncTaskThread::Destory();
Logger::Destory();
EventPollerPool::Instance().wait();
return 0;
}

View File

@ -229,15 +229,16 @@ static onceToken s_token([](){
int main(int argc,char *argv[]) {
//设置退出信号处理函数
signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); });
signal(SIGINT, [](int) { EventPollerPool::Instance().shutdown(); });
signal(SIGHUP, [](int) { loadIniConfig(); });
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().add(std::make_shared<FileChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
//加载配置文件,如果配置文件不存在就创建一个
loadIniConfig();
{
//这里是拉流地址支持rtmp/rtsp协议负载必须是H264+AAC
//如果是其他不识别的音视频将会被忽略(譬如说h264+adpcm转发后会去除音频)
auto urlList = {"rtmp://live.hkstv.hk.lxdns.com/live/hks1",
@ -353,17 +354,7 @@ int main(int argc,char *argv[]) {
}
});
EventPoller::Instance().runLoop();
}//设置作用域,作用域结束后会销毁临时变量;省去手动注销服务器
//rtsp服务器用到udp端口分配器了
UDPServer::Destory();
//TcpServer用到了WorkThreadPool
WorkThreadPool::Destory();
EventPoller::Destory();
AsyncTaskThread::Destory();
Logger::Destory();
EventPollerPool::Instance().wait();
return 0;
}