2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2017-06-06 20:06:31 +08:00
|
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/NoticeCenter.h"
|
|
|
|
|
#include "Poller/EventPoller.h"
|
|
|
|
|
#include "Device/PlayerProxy.h"
|
|
|
|
|
#include "Rtmp/RtmpPusher.h"
|
|
|
|
|
#include "Common/config.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace ZL::Util;
|
|
|
|
|
using namespace ZL::Rtmp;
|
|
|
|
|
using namespace ZL::Thread;
|
|
|
|
|
using namespace ZL::Network;
|
|
|
|
|
using namespace ZL::DEV;
|
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//推流器,保持强引用
|
|
|
|
|
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
|
2018-02-02 18:06:08 +08:00
|
|
|
|
pusher.reset(new RtmpPusher(DEFAULT_VHOST,app.data(), stream.data()));
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//设置推流中断处理逻辑
|
|
|
|
|
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());
|
|
|
|
|
}
|
2017-09-27 16:20:30 +08:00
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//推流失败或断开延迟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了
|
2017-09-27 16:20:30 +08:00
|
|
|
|
int domain(int argc, const char *argv[]) {
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//设置退出信号处理函数
|
2017-09-27 16:20:30 +08:00
|
|
|
|
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//设置日志
|
2017-06-06 20:06:31 +08:00
|
|
|
|
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
2017-09-30 13:00:12 +08:00
|
|
|
|
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
|
|
|
|
|
|
|
|
|
string playUrl = argv[1];
|
|
|
|
|
string pushUrl = argv[2];
|
2017-06-06 20:06:31 +08:00
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
|
|
|
|
|
//你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请查看test_rtmpPusherMp4.cpp代码)
|
2018-02-02 18:06:08 +08:00
|
|
|
|
PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST,"app", "stream"));
|
2017-09-30 13:00:12 +08:00
|
|
|
|
player->play(playUrl.data());
|
2017-06-06 20:06:31 +08:00
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发
|
2018-02-02 18:06:08 +08:00
|
|
|
|
NoticeCenter::Instance().addListener(nullptr, Config::Broadcast::kBroadcastMediaChanged, [pushUrl](BroadcastMediaChangedArgs) {
|
2017-06-06 20:06:31 +08:00
|
|
|
|
//媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源
|
2017-09-30 13:00:12 +08:00
|
|
|
|
createPusher(app,stream,pushUrl);
|
2017-06-06 20:06:31 +08:00
|
|
|
|
});
|
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//事件轮询
|
2017-06-06 20:06:31 +08:00
|
|
|
|
EventPoller::Instance().runLoop();
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//删除事件监听
|
2017-06-06 20:06:31 +08:00
|
|
|
|
NoticeCenter::Instance().delListener(nullptr);
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//销毁代理播放器、推流器
|
2017-06-06 20:06:31 +08:00
|
|
|
|
player.reset();
|
|
|
|
|
pusher.reset();
|
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//清理程序
|
2017-06-06 20:06:31 +08:00
|
|
|
|
EventPoller::Destory();
|
2018-02-24 15:18:16 +08:00
|
|
|
|
AsyncTaskThread::Destory();
|
2017-06-06 20:06:31 +08:00
|
|
|
|
Logger::Destory();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-27 16:20:30 +08:00
|
|
|
|
int main(int argc,char *argv[]){
|
2018-01-30 11:47:56 +08:00
|
|
|
|
const char *argList[] = {argv[0],"rtmp://live.hkstv.hk.lxdns.com/live/hks",argv[1]};
|
2017-09-30 13:00:12 +08:00
|
|
|
|
return domain(3,argList);
|
2017-09-27 16:20:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-06 20:06:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|