ZLMediaKit/tests/test_rtmpPusher.cpp
2017-08-09 18:39:30 +08:00

77 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//============================================================================
// Name : main.cpp
// Author : 熊子良
// Version :
//============================================================================
#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;
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));
PlayerProxy::Ptr player(new PlayerProxy("app","stream"));
//拉一个流生成一个RtmpMediaSource源的名称是"app/stream"
//你也可以以其他方式生成RtmpMediaSource比如说MP4文件请研读MediaReader代码
player->play("rtmp://live.hkstv.hk.lxdns.com/live/hks");
RtmpPusher::Ptr pusher;
//监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发。
NoticeCenter::Instance().addListener(nullptr,Config::Broadcast::kBroadcastRtmpSrcRegisted,
[&pusher](BroadcastRtmpSrcRegistedArgs){
//媒体源"app/stream"已经注册这时方可新建一个RtmpPusher对象并绑定该媒体源
const_cast<RtmpPusher::Ptr &>(pusher).reset(new RtmpPusher(app,stream));
pusher->setOnShutdown([](const SockException &ex){
WarnL << "已断开与服务器连接:" << ex.getErrCode() << " " << ex.what();
});
pusher->setOnPublished([](const SockException &ex){
if(ex){
WarnL << "发布失败:" << ex.getErrCode() << " "<< ex.what();
}else{
InfoL << "发布成功,请用播放器打开:rtmp://jizan.iok.la/live/test";
}
});
//推流地址,请改成你自己的服务器。
//这个范例地址也是基于mediakit是可用的但是带宽只有1mb访问可能很卡顿。
pusher->publish("rtmp://jizan.iok.la/live/test");
});
EventPoller::Instance().runLoop();
NoticeCenter::Instance().delListener(nullptr);
player.reset();
pusher.reset();
EventPoller::Destory();
Logger::Destory();
return 0;
}