mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-13 11:41:44 +08:00
整理代码,添加注释
This commit is contained in:
@@ -27,7 +27,6 @@
|
||||
#include <signal.h>
|
||||
#include <iostream>
|
||||
#include "Util/logger.h"
|
||||
#include "Util/onceToken.h"
|
||||
#include "Util/NoticeCenter.h"
|
||||
#include "Poller/EventPoller.h"
|
||||
#include "Device/PlayerProxy.h"
|
||||
@@ -41,57 +40,79 @@ using namespace ZL::Thread;
|
||||
using namespace ZL::Network;
|
||||
using namespace ZL::DEV;
|
||||
|
||||
//推流器,保持强引用
|
||||
RtmpPusher::Ptr pusher;
|
||||
//声明函数
|
||||
void rePushDelay(const string &app,const string &stream,const string &url);
|
||||
|
||||
//创建推流器并开始推流
|
||||
void createPusher(const string &app,const string &stream,const string &url){
|
||||
//创建推流器并绑定一个RtmpMediaSource
|
||||
pusher.reset(new RtmpPusher(app.data(), stream.data()));
|
||||
//设置推流中断处理逻辑
|
||||
pusher->setOnShutdown([app,stream, url](const SockException &ex) {
|
||||
WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
|
||||
//重试
|
||||
rePushDelay(app,stream, url);
|
||||
});
|
||||
//设置发布结果处理逻辑
|
||||
pusher->setOnPublished([app,stream, url](const SockException &ex) {
|
||||
if (ex) {
|
||||
WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
|
||||
//如果发布失败,就重试
|
||||
rePushDelay(app,stream, url);
|
||||
}else {
|
||||
InfoL << "Publish success,Please play with player:" << url;
|
||||
}
|
||||
});
|
||||
pusher->publish(url.data());
|
||||
}
|
||||
|
||||
//推流失败或断开延迟2秒后重试推流
|
||||
void rePushDelay(const string &app,const string &stream,const string &url){
|
||||
//上次延时两秒的任务可能还没执行,所以我们要先取消上次任务
|
||||
AsyncTaskThread::Instance().CancelTask(0);
|
||||
//2秒后执行重新推流的任务
|
||||
AsyncTaskThread::Instance().DoTaskDelay(0, 2000, [app, stream,url]() {
|
||||
InfoL << "Re-Publishing...";
|
||||
//重新推流
|
||||
createPusher(app,stream,url);
|
||||
//此任务不重复
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
|
||||
int domain(int argc, const char *argv[]) {
|
||||
//设置退出信号处理函数
|
||||
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
|
||||
//设置日志
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
|
||||
PlayerProxy::Ptr player(new PlayerProxy("app", "stream"));
|
||||
//拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
|
||||
//你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请研读MediaReader代码)
|
||||
player->play(argv[1]);
|
||||
string playUrl = argv[1];
|
||||
string pushUrl = argv[2];
|
||||
|
||||
RtmpPusher::Ptr pusher;
|
||||
//监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发。
|
||||
NoticeCenter::Instance().addListener(nullptr, Config::Broadcast::kBroadcastRtmpSrcRegisted,
|
||||
[&pusher, argv](BroadcastRtmpSrcRegistedArgs) {
|
||||
//拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
|
||||
//你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请查看test_rtmpPusherMp4.cpp代码)
|
||||
PlayerProxy::Ptr player(new PlayerProxy("app", "stream"));
|
||||
player->play(playUrl.data());
|
||||
|
||||
//监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发
|
||||
NoticeCenter::Instance().addListener(nullptr, Config::Broadcast::kBroadcastRtmpSrcRegisted, [pushUrl](BroadcastRtmpSrcRegistedArgs) {
|
||||
//媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源
|
||||
const_cast<RtmpPusher::Ptr &>(pusher).reset(new RtmpPusher(app, stream));
|
||||
string appTmp(app), streamTmp(stream);
|
||||
|
||||
pusher->setOnShutdown([appTmp,streamTmp, argv](const SockException &ex) {
|
||||
WarnL << "已断开与服务器连接(Server connection is closed):" << ex.getErrCode() << " " << ex.what();
|
||||
AsyncTaskThread::Instance().CancelTask(0);
|
||||
AsyncTaskThread::Instance().DoTaskDelay(0, 2000, [appTmp, streamTmp, argv]() {
|
||||
InfoL << "正在重新发布(Re-Publish Steam)...";
|
||||
NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastRtmpSrcRegisted, appTmp.data(), streamTmp.data());
|
||||
return false;
|
||||
});
|
||||
});
|
||||
|
||||
pusher->setOnPublished([appTmp, streamTmp,argv](const SockException &ex) {
|
||||
if (ex) {
|
||||
WarnL << "发布失败(Publish fail):" << ex.getErrCode() << " " << ex.what();
|
||||
AsyncTaskThread::Instance().CancelTask(0);
|
||||
AsyncTaskThread::Instance().DoTaskDelay(0, 2000, [appTmp, streamTmp, argv]() {
|
||||
InfoL << "正在重新发布(Re-Publish Steam)...";
|
||||
NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastRtmpSrcRegisted, appTmp.data(), streamTmp.data());
|
||||
return false;
|
||||
});
|
||||
}else {
|
||||
InfoL << "发布成功,请用播放器打开(Publish success,Please use play with player):" << argv[2];
|
||||
}
|
||||
});
|
||||
|
||||
//开始推流
|
||||
pusher->publish(argv[2]);
|
||||
createPusher(app,stream,pushUrl);
|
||||
});
|
||||
|
||||
//事件轮询
|
||||
EventPoller::Instance().runLoop();
|
||||
//删除事件监听
|
||||
NoticeCenter::Instance().delListener(nullptr);
|
||||
//销毁代理播放器、推流器
|
||||
player.reset();
|
||||
pusher.reset();
|
||||
|
||||
//清理程序
|
||||
EventPoller::Destory();
|
||||
Logger::Destory();
|
||||
return 0;
|
||||
@@ -101,7 +122,7 @@ int domain(int argc, const char *argv[]) {
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
const char *argList[] = {argv[0],"rtmp://live.hkstv.hk.lxdns.com/live/hks","rtmp://jizan.iok.la/live/test"};
|
||||
return domain(argc,argList);
|
||||
return domain(3,argList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user