ZLMediaKit/tests/test_rtmpPusher.cpp

112 lines
4.0 KiB
C++
Raw Normal View History

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/onceToken.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-27 16:20:30 +08:00
int domain(int argc, const char *argv[]) {
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
2017-06-06 20:06:31 +08:00
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
2017-09-27 16:20:30 +08:00
PlayerProxy::Ptr player(new PlayerProxy("app", "stream"));
2017-06-06 20:06:31 +08:00
//拉一个流生成一个RtmpMediaSource源的名称是"app/stream"
//你也可以以其他方式生成RtmpMediaSource比如说MP4文件请研读MediaReader代码
2017-09-27 16:20:30 +08:00
player->play(argv[1]);
2017-06-06 20:06:31 +08:00
RtmpPusher::Ptr pusher;
//监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发。
2017-09-27 16:20:30 +08:00
NoticeCenter::Instance().addListener(nullptr, Config::Broadcast::kBroadcastRtmpSrcRegisted,
[&pusher, argv](BroadcastRtmpSrcRegistedArgs) {
2017-06-06 20:06:31 +08:00
//媒体源"app/stream"已经注册这时方可新建一个RtmpPusher对象并绑定该媒体源
2017-09-27 16:20:30 +08:00
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;
});
2017-06-06 20:08:59 +08:00
});
2017-09-27 16:20:30 +08:00
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];
2017-06-06 20:06:31 +08:00
}
});
2017-09-27 16:20:30 +08:00
//开始推流
pusher->publish(argv[2]);
2017-06-06 20:06:31 +08:00
});
EventPoller::Instance().runLoop();
NoticeCenter::Instance().delListener(nullptr);
player.reset();
pusher.reset();
EventPoller::Destory();
Logger::Destory();
return 0;
}
2017-09-27 16:20:30 +08:00
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);
}
2017-06-06 20:06:31 +08:00