From 24ab876fdbab52a46c4d7ec8b7e9f249672cc4ee Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Thu, 10 Jun 2021 14:45:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E8=BF=9E=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=88=9B=E5=BB=BA=E5=A4=9A=E4=B8=AAmp4=E8=A7=A3?= =?UTF-8?q?=E5=A4=8D=E7=94=A8=E5=99=A8=E5=AF=BC=E8=87=B4=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=A2=9E=E9=95=BF=E7=9A=84bug:#895?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_pusherMp4.cpp | 56 +++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/tests/test_pusherMp4.cpp b/tests/test_pusherMp4.cpp index a33bf0a3..c72f52b1 100644 --- a/tests/test_pusherMp4.cpp +++ b/tests/test_pusherMp4.cpp @@ -24,9 +24,9 @@ using namespace toolkit; using namespace mediakit; //推流器,保持强引用 -MediaPusher::Ptr pusher; +MediaPusher::Ptr g_pusher; Timer::Ptr g_timer; - +MediaSource::Ptr g_src; //声明函数 //推流失败或断开延迟2秒后重试推流 @@ -36,7 +36,7 @@ void rePushDelay(const EventPoller::Ptr &poller, const string &app, const string &stream, const string &filePath, - const string &url) ; + const string &url); //创建推流器并开始推流 void createPusher(const EventPoller::Ptr &poller, @@ -46,36 +46,39 @@ void createPusher(const EventPoller::Ptr &poller, const string &stream, const string &filePath, const string &url) { - //不限制APP名,并且指定文件绝对路径 - auto src = MediaSource::createFromMP4(schema,vhost,app,stream,filePath, false); - if(!src){ + if (!g_src) { + //不限制APP名,并且指定文件绝对路径 + g_src = MediaSource::createFromMP4(schema, vhost, app, stream, filePath, false); + } + if (!g_src) { //文件不存在 WarnL << "MP4文件不存在:" << filePath; return; } //创建推流器并绑定一个MediaSource - pusher.reset(new MediaPusher(src,poller)); + g_pusher.reset(new MediaPusher(g_src, poller)); //可以指定rtsp推流方式,支持tcp和udp方式,默认tcp -// (*pusher)[Client::kRtpType] = Rtsp::RTP_UDP; + //(*g_pusher)[Client::kRtpType] = Rtsp::RTP_UDP; //设置推流中断处理逻辑 - pusher->setOnShutdown([poller,schema,vhost,app,stream,filePath, url](const SockException &ex) { + 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); + rePushDelay(poller, schema, vhost, app, stream, filePath, url); }); + //设置发布结果处理逻辑 - pusher->setOnPublished([poller,schema,vhost,app,stream,filePath, url](const SockException &ex) { + 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 { + rePushDelay(poller, schema, vhost, app, stream, filePath, url); + } else { InfoL << "Publish success,Please play with player:" << url; } }); - pusher->publish(url); + g_pusher->publish(url); } //推流失败或断开延迟2秒后重试推流 @@ -86,39 +89,44 @@ void rePushDelay(const EventPoller::Ptr &poller, const string &stream, const string &filePath, const string &url) { - g_timer = std::make_shared(2.0f,[poller,schema,vhost,app, stream, filePath,url]() { + g_timer = std::make_shared(2.0f, [poller, schema, vhost, app, stream, filePath, url]() { InfoL << "Re-Publishing..."; //重新推流 - createPusher(poller,schema,vhost,app, stream, filePath,url); + createPusher(poller, schema, vhost, app, stream, filePath, url); //此任务不重复 return false; }, poller); } //这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了 -int domain(const string & filePath,const string & pushUrl){ +int domain(const string &filePath, const string &pushUrl) { //设置日志 Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); + //循环点播mp4文件 + mINI::Instance()[Record::kFileRepeat] = 1; + mINI::Instance()[General::kHlsDemand] = 1; + mINI::Instance()[General::kTSDemand] = 1; + mINI::Instance()[General::kFMP4Demand] = 1; + //mINI::Instance()[General::kRtspDemand] = 1; + //mINI::Instance()[General::kRtmpDemand] = 1; + auto poller = EventPollerPool::Instance().getPoller(); //vhost/app/stream可以随便自己填,现在不限制app应用名了 - createPusher(poller,FindField(pushUrl.data(), nullptr,"://").substr(0,4),DEFAULT_VHOST,"live","stream",filePath,pushUrl); - + createPusher(poller, FindField(pushUrl.data(), nullptr, "://").substr(0, 4), DEFAULT_VHOST, "live", "stream", filePath, pushUrl); //设置退出信号处理函数 static semaphore sem; signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 sem.wait(); - pusher.reset(); + g_pusher.reset(); g_timer.reset(); return 0; } - - -int main(int argc,char *argv[]){ +int main(int argc, char *argv[]) { //可以使用test_server生成的mp4文件 //文件使用绝对路径,推流url支持rtsp和rtmp - return domain("/Users/xzl/git/ZLMediaKit/release/mac/Debug/www/record/live/rtsp_test1/2020-04-03/15-32-24.mp4","rtsp://127.0.0.1/live/rtsp_push"); + return domain("/home/work/test2.mp4", "rtmp://127.0.0.1/live/rtsp_push"); }