mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
重写加载mp4文件推流rtsp/rtmp范例程序
This commit is contained in:
parent
dbe6edb5de
commit
5b394fcff7
@ -11,12 +11,9 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Util/logger.h"
|
#include "Util/logger.h"
|
||||||
#include "Util/NoticeCenter.h"
|
|
||||||
#include "Poller/EventPoller.h"
|
|
||||||
#include "Player/PlayerProxy.h"
|
|
||||||
#include "Rtmp/RtmpPusher.h"
|
|
||||||
#include "Common/config.h"
|
#include "Common/config.h"
|
||||||
#include "Common/Parser.h"
|
#include "Common/Parser.h"
|
||||||
|
#include "Poller/EventPoller.h"
|
||||||
#include "Pusher/MediaPusher.h"
|
#include "Pusher/MediaPusher.h"
|
||||||
#include "Record/MP4Reader.h"
|
#include "Record/MP4Reader.h"
|
||||||
|
|
||||||
@ -24,113 +21,100 @@ using namespace std;
|
|||||||
using namespace toolkit;
|
using namespace toolkit;
|
||||||
using namespace mediakit;
|
using namespace mediakit;
|
||||||
|
|
||||||
//推流器,保持强引用
|
// 这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
|
||||||
MediaPusher::Ptr g_pusher;
|
int domain(const string &file, const string &url) {
|
||||||
Timer::Ptr g_timer;
|
// 设置日志
|
||||||
MediaSource::Ptr g_src;
|
Logger::Instance().add(std::make_shared<ConsoleChannel>());
|
||||||
|
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||||
|
|
||||||
//声明函数
|
// 关闭所有转协议
|
||||||
//推流失败或断开延迟2秒后重试推流
|
mINI::Instance()[Protocol::kEnableMP4] = 0;
|
||||||
void rePushDelay(const EventPoller::Ptr &poller,
|
mINI::Instance()[Protocol::kEnableFMP4] = 0;
|
||||||
const string &schema,
|
mINI::Instance()[Protocol::kEnableHls] = 0;
|
||||||
const string &vhost,
|
mINI::Instance()[Protocol::kEnableHlsFmp4] = 0;
|
||||||
const string &app,
|
mINI::Instance()[Protocol::kEnableTS] = 0;
|
||||||
const string &stream,
|
mINI::Instance()[Protocol::kEnableRtsp] = 0;
|
||||||
const string &filePath,
|
mINI::Instance()[Protocol::kEnableRtmp] = 0;
|
||||||
const string &url);
|
|
||||||
|
|
||||||
//创建推流器并开始推流
|
// 根据url获取媒体协议类型,注意大小写
|
||||||
void createPusher(const EventPoller::Ptr &poller,
|
auto schema = strToLower(findSubString(url.data(), nullptr, "://").substr(0, 4));
|
||||||
const string &schema,
|
|
||||||
const string &vhost,
|
// 只开启推流协议对应的转协议
|
||||||
const string &app,
|
mINI::Instance()["protocol.enable_" + schema] = 1;
|
||||||
const string &stream,
|
|
||||||
const string &filePath,
|
// 从mp4文件加载生成MediaSource对象
|
||||||
const string &url) {
|
auto reader = std::make_shared<MP4Reader>(DEFAULT_VHOST, "live", "stream", file);
|
||||||
if (!g_src) {
|
// 开始加载mp4,ref_self设置为false,这样reader对象设置为nullptr就能注销了,file_repeat可以设置为空,这样文件读完了就停止推流了
|
||||||
//不限制APP名,并且指定文件绝对路径
|
reader->startReadMP4(100, false, true);
|
||||||
g_src = MediaSource::createFromMP4(schema, vhost, app, stream, filePath, false);
|
auto src = MediaSource::find(schema, DEFAULT_VHOST, "live", "stream", false);
|
||||||
|
|
||||||
|
if (!src) {
|
||||||
|
// 文件不存在
|
||||||
|
WarnL << "File not existed: " << file;
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
if (!g_src) {
|
|
||||||
//文件不存在
|
// 选择一个poller线程绑定
|
||||||
WarnL << "MP4文件不存在:" << filePath;
|
auto poller = EventPollerPool::Instance().getPoller();
|
||||||
|
// 创建推流器并绑定一个MediaSource
|
||||||
|
auto pusher = std::make_shared<MediaPusher>(src, poller);
|
||||||
|
|
||||||
|
std::weak_ptr<MediaSource> weak_src = src;
|
||||||
|
// src用完了,可以直接置空,防止main函数持有它(MP4Reader持有它即可)
|
||||||
|
src = nullptr;
|
||||||
|
|
||||||
|
// 可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
|
||||||
|
//(*pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
|
||||||
|
|
||||||
|
// 设置推流中断处理逻辑
|
||||||
|
std::weak_ptr<MediaPusher> weak_pusher = pusher;
|
||||||
|
pusher->setOnShutdown([poller, url, weak_pusher, weak_src](const SockException &ex) {
|
||||||
|
if (!weak_src.lock()) {
|
||||||
|
// 媒体注销导致的推流中断,不在重试推流
|
||||||
|
WarnL << "MediaSource released:" << ex << ", publish stopped";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
WarnL << "Server connection is closed:" << ex << ", republish after 2 seconds";
|
||||||
|
// 重新推流, 2秒后重试
|
||||||
|
poller->doDelayTask(2 * 1000, [weak_pusher, url]() {
|
||||||
|
if (auto strong_push = weak_pusher.lock()) {
|
||||||
|
strong_push->publish(url);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 设置发布结果处理逻辑
|
||||||
|
pusher->setOnPublished([poller, weak_pusher, url](const SockException &ex) {
|
||||||
|
if (!ex) {
|
||||||
|
InfoL << "Publish success, please play with player:" << url;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//创建推流器并绑定一个MediaSource
|
WarnL << "Publish fail:" << ex << ", republish after 2 seconds";
|
||||||
g_pusher.reset(new MediaPusher(g_src, poller));
|
// 如果发布失败,就重试
|
||||||
//可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
|
poller->doDelayTask(2 * 1000, [weak_pusher, url]() {
|
||||||
//(*g_pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
|
if (auto strong_push = weak_pusher.lock()) {
|
||||||
|
strong_push->publish(url);
|
||||||
//设置推流中断处理逻辑
|
|
||||||
g_pusher->setOnShutdown([poller, schema, vhost, app, stream, filePath, url](const SockException &ex) {
|
|
||||||
WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
|
|
||||||
//重新推流
|
|
||||||
rePushDelay(poller, schema, vhost, app, stream, filePath, url);
|
|
||||||
});
|
|
||||||
|
|
||||||
//设置发布结果处理逻辑
|
|
||||||
g_pusher->setOnPublished([poller, schema, vhost, app, stream, filePath, url](const SockException &ex) {
|
|
||||||
if (ex) {
|
|
||||||
WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
|
|
||||||
//如果发布失败,就重试
|
|
||||||
rePushDelay(poller, schema, vhost, app, stream, filePath, url);
|
|
||||||
} else {
|
|
||||||
InfoL << "Publish success,Please play with player:" << url;
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
});
|
});
|
||||||
g_pusher->publish(url);
|
});
|
||||||
}
|
pusher->publish(url);
|
||||||
|
|
||||||
//推流失败或断开延迟2秒后重试推流
|
// sleep(5);
|
||||||
void rePushDelay(const EventPoller::Ptr &poller,
|
// reader 置空可以终止推流相关资源
|
||||||
const string &schema,
|
// reader = nullptr;
|
||||||
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]() {
|
|
||||||
InfoL << "Re-Publishing...";
|
|
||||||
//重新推流
|
|
||||||
createPusher(poller, schema, vhost, app, stream, filePath, url);
|
|
||||||
//此任务不重复
|
|
||||||
return false;
|
|
||||||
}, poller);
|
|
||||||
}
|
|
||||||
|
|
||||||
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
|
// 设置退出信号处理函数
|
||||||
int domain(const string &filePath, const string &pushUrl) {
|
|
||||||
//设置日志
|
|
||||||
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;
|
|
||||||
|
|
||||||
auto poller = EventPollerPool::Instance().getPoller();
|
|
||||||
//vhost/app/stream可以随便自己填,现在不限制app应用名了
|
|
||||||
createPusher(poller, findSubString(pushUrl.data(), nullptr, "://").substr(0, 4), DEFAULT_VHOST, "live", "stream", filePath, pushUrl);
|
|
||||||
//设置退出信号处理函数
|
|
||||||
static semaphore sem;
|
static semaphore sem;
|
||||||
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
|
signal(SIGINT, [](int) { sem.post(); }); // 设置退出信号
|
||||||
sem.wait();
|
sem.wait();
|
||||||
g_pusher.reset();
|
|
||||||
g_timer.reset();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
//可以使用test_server生成的mp4文件
|
// 可以使用test_server生成的mp4文件
|
||||||
//文件使用绝对路径,推流url支持rtsp和rtmp
|
// 文件使用绝对路径,推流url支持rtsp和rtmp
|
||||||
return domain("/home/work/test2.mp4", "rtmp://127.0.0.1/live/rtsp_push");
|
return domain("/Users/xiongziliang/Downloads/mp4/Quantum.mp4", "rtsp://127.0.0.1/live/rtsp_push");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user