2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* 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-09-05 19:51:25 +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-09-05 19:51:25 +08:00
|
|
|
|
#include "Rtmp/RtmpPusher.h"
|
|
|
|
|
#include "Common/config.h"
|
2019-03-27 18:41:52 +08:00
|
|
|
|
#include "Pusher/MediaPusher.h"
|
2017-09-05 19:51:25 +08:00
|
|
|
|
#include "MediaFile/MediaReader.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
using namespace mediakit;
|
2017-09-05 19:51:25 +08:00
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//推流器,保持强引用
|
2019-03-27 18:41:52 +08:00
|
|
|
|
MediaPusher::Ptr pusher;
|
2017-09-30 13:00:12 +08:00
|
|
|
|
|
|
|
|
|
//声明函数
|
2019-04-03 11:49:58 +08:00
|
|
|
|
//推流失败或断开延迟2秒后重试推流
|
|
|
|
|
void rePushDelay(const string &schema,
|
|
|
|
|
const string &vhost,
|
|
|
|
|
const string &app,
|
|
|
|
|
const string &stream,
|
|
|
|
|
const string &filePath,
|
|
|
|
|
const string &url) ;
|
2017-09-30 13:00:12 +08:00
|
|
|
|
|
|
|
|
|
//创建推流器并开始推流
|
2019-04-03 11:49:58 +08:00
|
|
|
|
void createPusher(const string &schema,
|
|
|
|
|
const string &vhost,
|
|
|
|
|
const string &app,
|
|
|
|
|
const string &stream,
|
|
|
|
|
const string &filePath,
|
|
|
|
|
const string &url) {
|
|
|
|
|
//不限制APP名,并且指定文件绝对路径
|
|
|
|
|
auto src = MediaReader::onMakeMediaSource(schema,vhost,app,stream,filePath, false);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if(!src){
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//文件不存在
|
2019-04-03 11:49:58 +08:00
|
|
|
|
WarnL << "MP4文件不存在:" << filePath;
|
2017-09-30 13:00:12 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//创建推流器并绑定一个MediaSource
|
|
|
|
|
pusher.reset(new MediaPusher(src));
|
2019-04-03 11:49:58 +08:00
|
|
|
|
//可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
|
|
|
|
|
// (*pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
|
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//设置推流中断处理逻辑
|
2019-04-03 11:49:58 +08:00
|
|
|
|
pusher->setOnShutdown([schema,vhost,app,stream,filePath, url](const SockException &ex) {
|
2017-09-30 13:00:12 +08:00
|
|
|
|
WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
|
|
|
|
|
//重新推流
|
2019-04-03 11:49:58 +08:00
|
|
|
|
rePushDelay(schema,vhost,app, stream,filePath, url);
|
2017-09-30 13:00:12 +08:00
|
|
|
|
});
|
|
|
|
|
//设置发布结果处理逻辑
|
2019-04-03 11:49:58 +08:00
|
|
|
|
pusher->setOnPublished([schema,vhost,app,stream,filePath, url](const SockException &ex) {
|
2017-09-30 13:00:12 +08:00
|
|
|
|
if (ex) {
|
|
|
|
|
WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
|
|
|
|
|
//如果发布失败,就重试
|
2019-04-03 11:49:58 +08:00
|
|
|
|
rePushDelay(schema,vhost,app, stream, filePath ,url);
|
2017-09-30 13:00:12 +08:00
|
|
|
|
}else {
|
|
|
|
|
InfoL << "Publish success,Please play with player:" << url;
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-03-27 18:41:52 +08:00
|
|
|
|
pusher->publish(url);
|
2017-09-05 19:51:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 18:11:00 +08:00
|
|
|
|
Timer::Ptr g_timer;
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//推流失败或断开延迟2秒后重试推流
|
2019-04-03 11:49:58 +08:00
|
|
|
|
void rePushDelay(const string &schema,
|
|
|
|
|
const string &vhost,
|
|
|
|
|
const string &app,
|
|
|
|
|
const string &stream,
|
|
|
|
|
const string &filePath,
|
|
|
|
|
const string &url) {
|
|
|
|
|
g_timer = std::make_shared<Timer>(2,[schema,vhost,app, stream, filePath,url]() {
|
2017-09-30 13:00:12 +08:00
|
|
|
|
InfoL << "Re-Publishing...";
|
|
|
|
|
//重新推流
|
2019-04-03 11:49:58 +08:00
|
|
|
|
createPusher(schema,vhost,app, stream, filePath,url);
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//此任务不重复
|
|
|
|
|
return false;
|
2019-01-30 18:11:00 +08:00
|
|
|
|
}, nullptr);
|
2017-09-30 13:00:12 +08:00
|
|
|
|
}
|
2017-09-05 19:51:25 +08:00
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
|
2018-03-21 15:13:33 +08:00
|
|
|
|
int domain(const string & filePath,const string & pushUrl){
|
2017-09-30 13:00:12 +08:00
|
|
|
|
//设置日志
|
2018-12-28 16:47:50 +08:00
|
|
|
|
Logger::Instance().add(std::make_shared<ConsoleChannel>());
|
2017-09-30 13:00:12 +08:00
|
|
|
|
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
2019-04-03 11:49:58 +08:00
|
|
|
|
//vhost/app/stream可以随便自己填,现在不限制app应用名了
|
|
|
|
|
createPusher(FindField(pushUrl.data(), nullptr,"://"),DEFAULT_VHOST,"live","stream",filePath,pushUrl);
|
2017-09-05 19:51:25 +08:00
|
|
|
|
|
2019-04-03 11:49:58 +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();
|
2017-09-05 19:51:25 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-30 13:00:12 +08:00
|
|
|
|
int main(int argc,char *argv[]){
|
|
|
|
|
//可以使用test_server生成的mp4文件
|
2019-04-03 11:49:58 +08:00
|
|
|
|
//文件使用绝对路径,推流url支持rtsp和rtmp
|
|
|
|
|
return domain("/Users/xzl/Desktop/bear-1280x720-long.mp4","rtsp://127.0.0.1/live/rtsp_push");
|
2017-09-30 13:00:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-09-05 19:51:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|