mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
整理代码,添加注释
This commit is contained in:
parent
e6bdfbce5c
commit
c367df73a5
BIN
src/.DS_Store
vendored
BIN
src/.DS_Store
vendored
Binary file not shown.
@ -37,17 +37,24 @@ namespace Rtmp {
|
||||
|
||||
unordered_map<string, RtmpPusher::rtmpCMDHandle> RtmpPusher::g_mapCmd;
|
||||
RtmpPusher::RtmpPusher(const char *strApp,const char *strStream) {
|
||||
static onceToken token([]() {
|
||||
g_mapCmd.emplace("_error",&RtmpPusher::onCmd_result);
|
||||
g_mapCmd.emplace("_result",&RtmpPusher::onCmd_result);
|
||||
g_mapCmd.emplace("onStatus",&RtmpPusher::onCmd_onStatus);
|
||||
}, []() {});
|
||||
auto src = RtmpMediaSource::find(strApp,strStream);
|
||||
if (!src) {
|
||||
auto strErr = StrPrinter << "media source:" << strApp << "/" << strStream << "not found!" << endl;
|
||||
throw std::runtime_error(strErr);
|
||||
}
|
||||
m_pMediaSrc = src;
|
||||
init(src);
|
||||
}
|
||||
RtmpPusher::RtmpPusher(const RtmpMediaSource::Ptr &src){
|
||||
init(src);
|
||||
}
|
||||
|
||||
void RtmpPusher::init(const RtmpMediaSource::Ptr &src){
|
||||
static onceToken token([]() {
|
||||
g_mapCmd.emplace("_error",&RtmpPusher::onCmd_result);
|
||||
g_mapCmd.emplace("_result",&RtmpPusher::onCmd_result);
|
||||
g_mapCmd.emplace("onStatus",&RtmpPusher::onCmd_onStatus);
|
||||
}, []() {});
|
||||
m_pMediaSrc=src;
|
||||
}
|
||||
|
||||
RtmpPusher::~RtmpPusher() {
|
||||
|
@ -39,6 +39,7 @@ public:
|
||||
typedef std::shared_ptr<RtmpPusher> Ptr;
|
||||
typedef std::function<void(const SockException &ex)> Event;
|
||||
RtmpPusher(const char *strApp,const char *strStream);
|
||||
RtmpPusher(const RtmpMediaSource::Ptr &src);
|
||||
virtual ~RtmpPusher();
|
||||
|
||||
void publish(const char* strUrl);
|
||||
@ -65,6 +66,7 @@ protected:
|
||||
send(pcRawData, iSize);
|
||||
}
|
||||
private:
|
||||
void init(const RtmpMediaSource::Ptr &src);
|
||||
void onShutdown(const SockException &ex) {
|
||||
m_pPublishTimer.reset();
|
||||
if(m_onShutdown){
|
||||
|
@ -42,14 +42,13 @@ using namespace ZL::Rtsp;
|
||||
using namespace ZL::Thread;
|
||||
using namespace ZL::Network;
|
||||
|
||||
void programExit(int arg) {
|
||||
EventPoller::Instance().shutdown();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
//设置退出信号处理函数
|
||||
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
|
||||
//设置日志
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
//Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
signal(SIGINT, programExit);
|
||||
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
|
||||
if(argc != 5){
|
||||
FatalL << "\r\n测试方法:./test_benchmark player_count play_interval rtxp_url rtp_type\r\n"
|
||||
|
@ -25,14 +25,15 @@
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "Http/HttpDownloader.h"
|
||||
#include "Http/HttpRequester.h"
|
||||
#include "Util/MD5.h"
|
||||
#include "Util/File.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/onceToken.h"
|
||||
#include "Util/File.h"
|
||||
#include "Poller/EventPoller.h"
|
||||
#include <list>
|
||||
#include "Http/HttpRequester.h"
|
||||
#include "Http/HttpDownloader.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ZL::Util;
|
||||
@ -40,90 +41,116 @@ using namespace ZL::Http;
|
||||
using namespace ZL::Poller;
|
||||
using namespace ZL::Network;
|
||||
|
||||
void programExit(int arg) {
|
||||
EventPoller::Instance().shutdown();
|
||||
}
|
||||
int main(int argc,char *argv[]){
|
||||
signal(SIGINT, programExit);
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
//设置退出信号处理函数
|
||||
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
|
||||
//设置日志
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
///////////////////////////////http downloader///////////////////////
|
||||
list<HttpDownloader::Ptr> downloaderList;
|
||||
//下载器map
|
||||
map<string,HttpDownloader::Ptr> downloaderMap;
|
||||
//下载两个文件,一个是http下载,一个https下载
|
||||
auto urlList = {"http://img3.imgtn.bdimg.com/it/u=158031390,1321729164&fm=214&gp=0.jpg",
|
||||
"https://media-cdn.tripadvisor.com/media/photo-s/06/c3/2f/64/de-notre-chambre.jpg"};
|
||||
int i=0;
|
||||
for(auto url : urlList){
|
||||
"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=931786003,1029770543&fm=27&gp=0.jpg"};
|
||||
|
||||
for(auto &url : urlList){
|
||||
//创建下载器
|
||||
HttpDownloader::Ptr downloader(new HttpDownloader());
|
||||
downloader->setOnResult([](ErrCode code,const char *errMsg,const char *filePath){
|
||||
DebugL << "=====================HttpDownloader result=======================";
|
||||
//下载结果回调
|
||||
if(code == Err_success){
|
||||
//文件下载成功
|
||||
InfoL << "download file success:" << filePath;
|
||||
}else{
|
||||
//下载失败
|
||||
WarnL << "code:" << code << " msg:" << errMsg;
|
||||
}
|
||||
});
|
||||
//断点续传功能,开启后可能会遇到416的错误(因为上次文件已经下载完全)
|
||||
downloader->startDownload(url,exeDir() + to_string(i++) + ".jpg",true);
|
||||
downloaderList.push_back(downloader);
|
||||
downloader->startDownload(url,exeDir() + MD5(url).hexdigest() + ".jpg",true);
|
||||
//下载器必须被强引用,否则作用域一失效就会导致对象销毁
|
||||
downloaderMap.emplace(url,downloader);
|
||||
}
|
||||
|
||||
///////////////////////////////http get///////////////////////
|
||||
//创建一个Http请求器
|
||||
HttpRequester::Ptr requesterGet(new HttpRequester());
|
||||
//使用GET方式请求
|
||||
requesterGet->setMethod("GET");
|
||||
//设置http头,我们假设设置cookie
|
||||
//设置http请求头,我们假设设置cookie,当然你也可以设置其他http头
|
||||
requesterGet->addHeader("Cookie","SESSIONID=e1aa89b3-f79f-4ac6-8ae2-0cea9ae8e2d7");
|
||||
requesterGet->startRequester("http://pv.sohu.com/cityjson?ie=utf-8",
|
||||
[](const SockException &ex,
|
||||
const string &status,
|
||||
const HttpClient::HttpHeader &header,
|
||||
const string &strRecvBody){
|
||||
//开启请求,该api会返回当前主机外网ip等信息
|
||||
requesterGet->startRequester("http://pv.sohu.com/cityjson?ie=utf-8",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody){ //http回复body
|
||||
DebugL << "=====================HttpRequester GET===========================";
|
||||
if(ex){
|
||||
//网络相关的错误
|
||||
WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
|
||||
}else{
|
||||
//打印http回复信息
|
||||
_StrPrinter printer;
|
||||
for(auto &pr: header){
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "\r\nhttp status:" << status << "\r\n\r\n"
|
||||
<< "header:" << (printer << endl)
|
||||
InfoL << "status:" << status << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << strRecvBody;
|
||||
}
|
||||
});
|
||||
|
||||
///////////////////////////////http post///////////////////////
|
||||
//创建一个Http请求器
|
||||
HttpRequester::Ptr requesterPost(new HttpRequester());
|
||||
//使用POST方式请求
|
||||
requesterPost->setMethod("POST");
|
||||
//设置http请求头
|
||||
requesterPost->addHeader("X-Requested-With","XMLHttpRequest");
|
||||
requesterPost->addHeader("Origin","http://fanyi.baidu.com");
|
||||
//设置POST参数列表
|
||||
HttpArgs args;
|
||||
args["query"] = "test";
|
||||
args["from"] = "en";
|
||||
args["to"] = "zh";
|
||||
args["transtype"] = "translang";
|
||||
args["simple_means_flag"] = "3";
|
||||
requesterPost->addHeader("X-Requested-With","XMLHttpRequest");
|
||||
requesterPost->addHeader("Origin","http://fanyi.baidu.com");
|
||||
|
||||
requesterPost->setBody(args.make());
|
||||
requesterPost->startRequester("http://fanyi.baidu.com/langdetect", [](const SockException &ex,
|
||||
const string &status,
|
||||
const HttpClient::HttpHeader &header,
|
||||
const string &strRecvBody){
|
||||
//开启请求
|
||||
requesterPost->startRequester("http://fanyi.baidu.com/langdetect",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody){ //http回复body
|
||||
DebugL << "=====================HttpRequester POST==========================";
|
||||
if(ex){
|
||||
//网络相关的错误
|
||||
WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
|
||||
} else {
|
||||
//打印http回复信息
|
||||
_StrPrinter printer;
|
||||
for(auto &pr: header){
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "\r\nhttp status:" << status << "\r\n\r\n"
|
||||
<< "header:" << (printer << endl)
|
||||
InfoL << "status:" << status << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << strRecvBody;
|
||||
}
|
||||
});
|
||||
|
||||
//事件轮询
|
||||
EventPoller::Instance().runLoop();
|
||||
static onceToken token(nullptr,[](){
|
||||
//清空下载器
|
||||
downloaderMap.clear();
|
||||
requesterGet.reset();
|
||||
requesterPost.reset();
|
||||
//程序开始退出
|
||||
EventPoller::Destory();
|
||||
Logger::Destory();
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -44,15 +44,12 @@ using namespace ZL::Network;
|
||||
using namespace ZL::Rtsp;
|
||||
using namespace ZL::Player;
|
||||
|
||||
void programExit(int arg) {
|
||||
EventPoller::Instance().shutdown();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
|
||||
//设置退出信号处理函数
|
||||
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
|
||||
//设置日志
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
//Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
signal(SIGINT, programExit);
|
||||
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
|
||||
if(argc != 3){
|
||||
FatalL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n"
|
||||
|
@ -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);
|
||||
|
||||
int domain(int argc, const char *argv[]) {
|
||||
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
|
||||
PlayerProxy::Ptr player(new PlayerProxy("app", "stream"));
|
||||
//拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
|
||||
//你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请研读MediaReader代码)
|
||||
player->play(argv[1]);
|
||||
|
||||
RtmpPusher::Ptr pusher;
|
||||
//监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发。
|
||||
NoticeCenter::Instance().addListener(nullptr, Config::Broadcast::kBroadcastRtmpSrcRegisted,
|
||||
[&pusher, argv](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;
|
||||
//创建推流器并开始推流
|
||||
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([appTmp, streamTmp,argv](const SockException &ex) {
|
||||
//设置发布结果处理逻辑
|
||||
pusher->setOnPublished([app,stream, url](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;
|
||||
});
|
||||
WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
|
||||
//如果发布失败,就重试
|
||||
rePushDelay(app,stream, url);
|
||||
}else {
|
||||
InfoL << "发布成功,请用播放器打开(Publish success,Please use play with player):" << argv[2];
|
||||
InfoL << "Publish success,Please play with player:" << url;
|
||||
}
|
||||
});
|
||||
pusher->publish(url.data());
|
||||
}
|
||||
|
||||
//开始推流
|
||||
pusher->publish(argv[2]);
|
||||
//推流失败或断开延迟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>());
|
||||
|
||||
string playUrl = argv[1];
|
||||
string pushUrl = argv[2];
|
||||
|
||||
//拉一个流,生成一个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对象并绑定该媒体源
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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"
|
||||
@ -42,48 +41,85 @@ using namespace ZL::Thread;
|
||||
using namespace ZL::Network;
|
||||
using namespace ZL::DEV;
|
||||
|
||||
void programExit(int arg) {
|
||||
EventPoller::Instance().shutdown();
|
||||
}
|
||||
int main(int argc,char *argv[]){
|
||||
setExePath(argv[0]);
|
||||
signal(SIGINT, programExit);
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
//推流器,保持强引用
|
||||
RtmpPusher::Ptr pusher;
|
||||
|
||||
RtmpPusher::Ptr pusher;
|
||||
//监听RtmpMediaSource注册事件,在流媒体化MP4文件后触发。
|
||||
NoticeCenter::Instance().addListener(nullptr,Config::Broadcast::kBroadcastRtmpSrcRegisted,
|
||||
[&pusher](BroadcastRtmpSrcRegistedArgs){
|
||||
//媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源
|
||||
const_cast<RtmpPusher::Ptr &>(pusher).reset(new RtmpPusher(app,stream));
|
||||
//声明函数
|
||||
void rePushDelay(const string &app,const string &stream,const string &url);
|
||||
void createPusher(const string &app,const string &stream,const string &url);
|
||||
|
||||
pusher->setOnShutdown([](const SockException &ex){
|
||||
WarnL << "已断开与服务器连接:" << ex.getErrCode() << " " << ex.what();
|
||||
|
||||
//创建推流器并开始推流
|
||||
void createPusher(const string &app,const string &stream,const string &url){
|
||||
auto rtmpSrc = MediaReader::onMakeRtmp(app,stream);
|
||||
if(!rtmpSrc){
|
||||
//文件不存在
|
||||
WarnL << "MP4 file not exited!";
|
||||
return;
|
||||
}
|
||||
|
||||
//创建推流器并绑定一个RtmpMediaSource
|
||||
pusher.reset(new RtmpPusher(rtmpSrc));
|
||||
//设置推流中断处理逻辑
|
||||
pusher->setOnShutdown([app,stream, url](const SockException &ex) {
|
||||
WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
|
||||
//重新推流
|
||||
rePushDelay(app, stream, url);
|
||||
});
|
||||
|
||||
pusher->setOnPublished([](const SockException &ex){
|
||||
if(ex){
|
||||
WarnL << "发布失败:" << ex.getErrCode() << " "<< ex.what();
|
||||
}else{
|
||||
InfoL << "发布成功,请用播放器打开:rtmp://jizan.iok.la/live/test";
|
||||
//设置发布结果处理逻辑
|
||||
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());
|
||||
}
|
||||
|
||||
//推流地址,请改成你自己的服务器。
|
||||
//这个范例地址(也是基于mediakit)是可用的,但是带宽只有1mb,访问可能很卡顿。
|
||||
InfoL << "start publish rtmp!";
|
||||
pusher->publish("rtmp://jizan.iok.la/live/test");
|
||||
//推流失败或断开延迟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>());
|
||||
|
||||
//流媒体化MP4文件,该文件需要放置在 httpRoot/record目录下
|
||||
//app必须为“record”,stream为相对于httpRoot/record的路径
|
||||
MediaReader::onMakeRtmp("record","live/0/2017-08-22/10-08-44.mp4");
|
||||
//filePath同时也是流id
|
||||
string filePath = argv[1];
|
||||
//推流地址
|
||||
string pushUrl = argv[2];
|
||||
|
||||
//录像应用名称默认为record
|
||||
string appName = mINI::Instance()[Config::Record::kAppName];
|
||||
//app必须record,filePath(流id)为相对于httpRoot/record的路径,否则MediaReader会找到不该文件
|
||||
//限制app为record是为了防止服务器上的文件被肆意访问
|
||||
createPusher(appName,filePath,pushUrl);
|
||||
|
||||
//开始事件轮询
|
||||
EventPoller::Instance().runLoop();
|
||||
//删除事件监听
|
||||
NoticeCenter::Instance().delListener(nullptr);
|
||||
//销毁推流器
|
||||
pusher.reset();
|
||||
|
||||
//程序清理
|
||||
EventPoller::Destory();
|
||||
Logger::Destory();
|
||||
return 0;
|
||||
@ -91,6 +127,14 @@ int main(int argc,char *argv[]){
|
||||
|
||||
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
//MP4文件需要放置在 httpRoot/record目录下,文件负载必须为h264+aac
|
||||
//可以使用test_server生成的mp4文件
|
||||
const char *argList[] = {argv[0],"app/stream/2017-09-30/12-55-38.mp4","rtmp://jizan.iok.la/live/test"};
|
||||
return domain(3,argList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -48,6 +48,7 @@
|
||||
#include "Device/PlayerProxy.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ZL::DEV;
|
||||
using namespace ZL::Util;
|
||||
using namespace ZL::Http;
|
||||
using namespace ZL::Rtsp;
|
||||
@ -55,37 +56,39 @@ using namespace ZL::Rtmp;
|
||||
using namespace ZL::Shell;
|
||||
using namespace ZL::Thread;
|
||||
using namespace ZL::Network;
|
||||
using namespace ZL::DEV;
|
||||
|
||||
void programExit(int arg) {
|
||||
EventPoller::Instance().shutdown();
|
||||
}
|
||||
int main(int argc,char *argv[]){
|
||||
setExePath(argv[0]);
|
||||
signal(SIGINT, programExit);
|
||||
//设置退出信号处理函数
|
||||
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
|
||||
//设置日志
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
//加载配置文件,如果配置文件不存在就创建一个
|
||||
Config::loadIniConfig();
|
||||
DebugL << exePath();
|
||||
//support rtmp and rtsp url
|
||||
//just support H264+AAC
|
||||
//这里是拉流地址,支持rtmp/rtsp协议,负载必须是H264+AAC
|
||||
//如果是其他不识别的音视频将会被忽略(譬如说h264+adpcm转发后会去除音频)
|
||||
auto urlList = {"rtmp://live.hkstv.hk.lxdns.com/live/hks",
|
||||
//rtsp链接支持输入用户名密码
|
||||
"rtsp://admin:jzan123456@192.168.0.122/"};
|
||||
map<string , PlayerProxy::Ptr> proxyMap;
|
||||
int i=0;
|
||||
for(auto url : urlList){
|
||||
for(auto &url : urlList){
|
||||
//PlayerProxy构造函数前两个参数分别为应用名(app),流id(streamId)
|
||||
//比如说应用为live,流id为0,那么直播地址为:
|
||||
//http://127.0.0.1/live/0/hls.m3u8
|
||||
//rtsp://127.0.0.1/live/0
|
||||
//rtmp://127.0.0.1/live/0
|
||||
//录像地址为:
|
||||
//录像地址为(当然vlc不支持这么多级的rtmp url,可以用test_player测试rtmp点播):
|
||||
//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
|
||||
PlayerProxy::Ptr player(new PlayerProxy("live",to_string(i++).data()));
|
||||
(*player)[PlayerProxy::kAliveSecond] = 10;//录制10秒
|
||||
//指定RTP over TCP(播放rtsp时有效)
|
||||
(*player)[RtspPlayer::kRtpType] = PlayerBase::RTP_TCP;
|
||||
//开始播放,如果播放失败或者播放中止,将会自动重试若干次,重试次数在配置文件中配置,默认一直重试
|
||||
player->play(url);
|
||||
proxyMap.emplace(string(url),player);
|
||||
//需要保存PlayerProxy,否则作用域结束就会销毁该对象
|
||||
proxyMap.emplace(url,player);
|
||||
}
|
||||
|
||||
#ifdef ENABLE_OPENSSL
|
||||
@ -94,31 +97,35 @@ int main(int argc,char *argv[]){
|
||||
SSL_Initor::Instance().loadServerPem((exePath() + ".pem").data());
|
||||
}catch(...){
|
||||
FatalL << "请把证书:" << (exeName() + ".pem") << "放置在本程序可执行程序同目录下:" << exeDir() << endl;
|
||||
proxyMap.clear();
|
||||
return 0;
|
||||
}
|
||||
#endif //ENABLE_OPENSSL
|
||||
|
||||
//简单的telnet服务器,可用于服务器调试,但是不能使用23端口
|
||||
//简单的telnet服务器,可用于服务器调试,但是不能使用23端口,否则telnet上了莫名其妙的现象
|
||||
//测试方法:telnet 127.0.0.1 8023
|
||||
//输入用户名和密码登录(user:test,pwd:123456),输入help命令查看帮助
|
||||
TcpServer<ShellSession>::Ptr shellSrv(new TcpServer<ShellSession>());
|
||||
ShellSession::addUser("test", "123456");
|
||||
shellSrv->start(8023);
|
||||
//开启rtsp/rtmp/http服务器
|
||||
TcpServer<RtspSession>::Ptr rtspSrv(new TcpServer<RtspSession>());
|
||||
TcpServer<RtmpSession>::Ptr rtmpSrv(new TcpServer<RtmpSession>());
|
||||
TcpServer<HttpSession>::Ptr httpSrv(new TcpServer<HttpSession>());
|
||||
|
||||
ShellSession::addUser("test", "123456");
|
||||
shellSrv->start(8023);
|
||||
rtspSrv->start(mINI::Instance()[Config::Rtsp::kPort]);
|
||||
rtmpSrv->start(mINI::Instance()[Config::Rtmp::kPort]);
|
||||
httpSrv->start(mINI::Instance()[Config::Http::kPort]);
|
||||
rtspSrv->start(mINI::Instance()[Config::Rtsp::kPort]);//默认554
|
||||
rtmpSrv->start(mINI::Instance()[Config::Rtmp::kPort]);//默认1935
|
||||
httpSrv->start(mINI::Instance()[Config::Http::kPort]);//默认80
|
||||
|
||||
#ifdef ENABLE_OPENSSL
|
||||
//如果支持ssl,还可以开启https服务器
|
||||
TcpServer<HttpsSession>::Ptr httpsSrv(new TcpServer<HttpsSession>());
|
||||
httpsSrv->start(mINI::Instance()[Config::Http::kSSLPort]);
|
||||
httpsSrv->start(mINI::Instance()[Config::Http::kSSLPort]);//默认443
|
||||
#endif //ENABLE_OPENSSL
|
||||
|
||||
EventPoller::Instance().runLoop();
|
||||
//销毁拉流客户端
|
||||
proxyMap.clear();
|
||||
//销毁服务器
|
||||
shellSrv.reset();
|
||||
rtspSrv.reset();
|
||||
rtmpSrv.reset();
|
||||
@ -128,7 +135,9 @@ int main(int argc,char *argv[]){
|
||||
httpsSrv.reset();
|
||||
#endif //ENABLE_OPENSSL
|
||||
|
||||
//rtsp服务器用到udp端口分配器了
|
||||
UDPServer::Destory();
|
||||
//TcpServer用到了WorkThreadPool
|
||||
WorkThreadPool::Destory();
|
||||
EventPoller::Destory();
|
||||
Logger::Destory();
|
||||
|
Loading…
Reference in New Issue
Block a user