ZLMediaKit/tests/test_pusherMp4.cpp

137 lines
4.8 KiB
C++
Raw Permalink Normal View History

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
*
* 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-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"
#include "Common/Parser.h"
2019-03-27 18:41:52 +08:00
#include "Pusher/MediaPusher.h"
2019-12-04 10:52:26 +08:00
#include "Record/MP4Reader.h"
2017-09-05 19:51:25 +08:00
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
//推流器,保持强引用
MediaPusher::Ptr g_pusher;
2019-05-17 11:38:35 +08:00
Timer::Ptr g_timer;
MediaSource::Ptr g_src;
2017-09-30 13:00:12 +08:00
//声明函数
2019-04-03 11:49:58 +08:00
//推流失败或断开延迟2秒后重试推流
2019-05-17 11:38:35 +08:00
void rePushDelay(const EventPoller::Ptr &poller,
const string &schema,
2020-03-20 11:51:24 +08:00
const string &vhost,
const string &app,
const string &stream,
const string &filePath,
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,
2020-03-20 11:51:24 +08:00
const string &vhost,
const string &app,
const string &stream,
const string &filePath,
const string &url) {
if (!g_src) {
//不限制APP名并且指定文件绝对路径
g_src = MediaSource::createFromMP4(schema, vhost, app, stream, filePath, false);
}
if (!g_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;
}
2020-03-20 11:51:24 +08:00
//创建推流器并绑定一个MediaSource
g_pusher.reset(new MediaPusher(g_src, poller));
2020-03-20 11:51:24 +08:00
//可以指定rtsp推流方式支持tcp和udp方式默认tcp
//(*g_pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
2019-04-03 11:49:58 +08:00
2020-03-20 11:51:24 +08:00
//设置推流中断处理逻辑
g_pusher->setOnShutdown([poller, schema, vhost, app, stream, filePath, url](const SockException &ex) {
2020-03-20 11:51:24 +08:00
WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
2017-09-30 13:00:12 +08:00
//重新推流
rePushDelay(poller, schema, vhost, app, stream, filePath, url);
2020-03-20 11:51:24 +08:00
});
2020-03-20 11:51:24 +08:00
//设置发布结果处理逻辑
g_pusher->setOnPublished([poller, schema, vhost, app, stream, filePath, url](const SockException &ex) {
2020-03-20 11:51:24 +08:00
if (ex) {
WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
//如果发布失败,就重试
rePushDelay(poller, schema, vhost, app, stream, filePath, url);
} else {
2020-03-20 11:51:24 +08:00
InfoL << "Publish success,Please play with player:" << url;
}
});
g_pusher->publish(url);
2017-09-05 19:51:25 +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,
2020-03-20 11:51:24 +08:00
const string &vhost,
const string &app,
const string &stream,
const string &filePath,
const string &url) {
g_timer = std::make_shared<Timer>(2.0f, [poller, schema, vhost, app, stream, filePath, url]() {
2020-03-20 11:51:24 +08:00
InfoL << "Re-Publishing...";
//重新推流
createPusher(poller, schema, vhost, app, stream, filePath, url);
2020-03-20 11:51:24 +08:00
//此任务不重复
return false;
}, poller);
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了
int domain(const string &filePath, const string &pushUrl) {
2020-03-20 11:51:24 +08:00
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
//循环点播mp4文件
mINI::Instance()[Record::kFileRepeat] = 1;
mINI::Instance()[Protocol::kHlsDemand] = 1;
mINI::Instance()[Protocol::kTSDemand] = 1;
mINI::Instance()[Protocol::kFMP4Demand] = 1;
//mINI::Instance()[Protocol::kRtspDemand] = 1;
//mINI::Instance()[Protocol::kRtmpDemand] = 1;
2019-05-17 11:38:35 +08:00
auto poller = EventPollerPool::Instance().getPoller();
//vhost/app/stream可以随便自己填现在不限制app应用名了
createPusher(poller, findSubString(pushUrl.data(), nullptr, "://").substr(0, 4), DEFAULT_VHOST, "live", "stream", filePath, pushUrl);
2020-03-20 11:51:24 +08:00
//设置退出信号处理函数
static semaphore sem;
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
sem.wait();
g_pusher.reset();
2020-03-20 11:51:24 +08:00
g_timer.reset();
return 0;
2017-09-05 19:51:25 +08:00
}
int main(int argc, char *argv[]) {
2017-09-30 13:00:12 +08:00
//可以使用test_server生成的mp4文件
2020-03-20 11:51:24 +08:00
//文件使用绝对路径推流url支持rtsp和rtmp
return domain("/home/work/test2.mp4", "rtmp://127.0.0.1/live/rtsp_push");
2017-09-30 13:00:12 +08:00
}
2017-09-05 19:51:25 +08:00