2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*/
|
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"
|
2018-10-29 09:54:35 +08:00
|
|
|
|
#include "Player/PlayerProxy.h"
|
2017-06-06 20:06:31 +08:00
|
|
|
|
#include "Rtmp/RtmpPusher.h"
|
|
|
|
|
#include "Common/config.h"
|
2019-03-27 18:41:52 +08:00
|
|
|
|
#include "Pusher/MediaPusher.h"
|
2017-06-06 20:06:31 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
using namespace mediakit;
|
2017-06-06 20:06:31 +08:00
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//推流器,保持强引用
|
2019-03-27 18:41:52 +08:00
|
|
|
|
MediaPusher::Ptr pusher;
|
2019-05-17 11:38:35 +08:00
|
|
|
|
Timer::Ptr g_timer;
|
2018-03-21 15:13:33 +08:00
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//声明函数
|
2019-05-17 11:38:35 +08:00
|
|
|
|
void rePushDelay(const EventPoller::Ptr &poller,const string &schema,const string &vhost,const string &app, const string &stream, const string &url);
|
2017-09-30 13:00:12 +08:00
|
|
|
|
|
|
|
|
|
//创建推流器并开始推流
|
2019-05-17 11:38:35 +08:00
|
|
|
|
void createPusher(const EventPoller::Ptr &poller, const string &schema,const string &vhost,const string &app, const string &stream, const string &url) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//创建推流器并绑定一个MediaSource
|
2019-05-17 11:38:35 +08:00
|
|
|
|
pusher.reset(new MediaPusher(schema,vhost, app, stream,poller));
|
2019-03-28 11:52:07 +08:00
|
|
|
|
//可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
|
|
|
|
|
// (*pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//设置推流中断处理逻辑
|
2019-05-17 11:38:35 +08:00
|
|
|
|
pusher->setOnShutdown([poller,schema,vhost, app, stream, url](const SockException &ex) {
|
2017-09-30 13:00:12 +08:00
|
|
|
|
WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
|
|
|
|
|
//重试
|
2019-05-17 11:38:35 +08:00
|
|
|
|
rePushDelay(poller,schema,vhost,app, stream, url);
|
2017-09-30 13:00:12 +08:00
|
|
|
|
});
|
|
|
|
|
//设置发布结果处理逻辑
|
2019-05-17 11:38:35 +08:00
|
|
|
|
pusher->setOnPublished([poller,schema,vhost, app, stream, url](const SockException &ex) {
|
2017-09-30 13:00:12 +08:00
|
|
|
|
if (ex) {
|
|
|
|
|
WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
|
|
|
|
|
//如果发布失败,就重试
|
2019-05-17 11:38:35 +08:00
|
|
|
|
rePushDelay(poller,schema,vhost,app, stream, url);
|
2018-03-21 15:13:33 +08:00
|
|
|
|
} else {
|
2017-09-30 13:00:12 +08:00
|
|
|
|
InfoL << "Publish success,Please play with player:" << url;
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-03-27 18:41:52 +08:00
|
|
|
|
pusher->publish(url);
|
2017-09-30 13:00:12 +08:00
|
|
|
|
}
|
2017-09-27 16:20:30 +08:00
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//推流失败或断开延迟2秒后重试推流
|
2019-05-17 11:38:35 +08:00
|
|
|
|
void rePushDelay(const EventPoller::Ptr &poller,const string &schema,const string &vhost,const string &app, const string &stream, const string &url) {
|
2021-01-17 18:31:50 +08:00
|
|
|
|
g_timer = std::make_shared<Timer>(2.0f,[poller,schema,vhost,app, stream, url]() {
|
2018-03-21 15:13:33 +08:00
|
|
|
|
InfoL << "Re-Publishing...";
|
|
|
|
|
//重新推流
|
2019-05-17 11:38:35 +08:00
|
|
|
|
createPusher(poller,schema,vhost,app, stream, url);
|
2018-03-21 15:13:33 +08:00
|
|
|
|
//此任务不重复
|
2017-09-30 13:00:12 +08:00
|
|
|
|
return false;
|
2019-05-17 11:38:35 +08:00
|
|
|
|
}, poller);
|
2017-09-30 13:00:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
|
2018-03-21 15:13:33 +08:00
|
|
|
|
int domain(const string &playUrl, const string &pushUrl) {
|
|
|
|
|
//设置日志
|
2018-12-28 16:47:50 +08:00
|
|
|
|
Logger::Instance().add(std::make_shared<ConsoleChannel>());
|
2018-03-21 15:13:33 +08:00
|
|
|
|
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
2019-05-17 11:38:35 +08:00
|
|
|
|
auto poller = EventPollerPool::Instance().getPoller();
|
2018-03-21 15:13:33 +08:00
|
|
|
|
|
2018-12-28 16:47:50 +08:00
|
|
|
|
//拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
|
|
|
|
|
//你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请查看test_rtmpPusherMp4.cpp代码)
|
2020-03-26 10:24:05 +08:00
|
|
|
|
MediaInfo info(pushUrl);
|
2020-09-20 09:26:00 +08:00
|
|
|
|
PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "app", "stream",false,false,-1 , poller));
|
2019-06-20 17:41:17 +08:00
|
|
|
|
//可以指定rtsp拉流方式,支持tcp和udp方式,默认tcp
|
|
|
|
|
// (*player)[Client::kRtpType] = Rtsp::RTP_UDP;
|
2018-12-28 16:47:50 +08:00
|
|
|
|
player->play(playUrl.data());
|
|
|
|
|
|
|
|
|
|
//监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发
|
|
|
|
|
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaChanged,
|
2019-05-17 11:38:35 +08:00
|
|
|
|
[pushUrl,poller](BroadcastMediaChangedArgs) {
|
2018-12-28 16:47:50 +08:00
|
|
|
|
//媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源
|
2019-12-24 14:08:16 +08:00
|
|
|
|
if(bRegist && pushUrl.find(sender.getSchema()) == 0){
|
|
|
|
|
createPusher(poller,sender.getSchema(),sender.getVhost(),sender.getApp(), sender.getId(), pushUrl);
|
2018-12-28 16:47:50 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
//设置退出信号处理函数
|
|
|
|
|
static semaphore sem;
|
|
|
|
|
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
|
2019-01-18 10:16:36 +08:00
|
|
|
|
sem.wait();
|
2019-03-27 19:02:22 +08:00
|
|
|
|
pusher.reset();
|
|
|
|
|
g_timer.reset();
|
2018-03-21 15:13:33 +08:00
|
|
|
|
return 0;
|
2017-06-06 20:06:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-03-21 15:13:33 +08:00
|
|
|
|
int main(int argc, char *argv[]) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return domain("rtmp://live.hkstv.hk.lxdns.com/live/hks1", "rtsp://127.0.0.1/live/rtsp_push");
|
2017-09-27 16:20:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-06 20:06:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|