test_pusherMp4.cpp 4.82 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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.
 */
26 27 28 29 30 31

#include <signal.h>
#include <iostream>
#include "Util/logger.h"
#include "Util/NoticeCenter.h"
#include "Poller/EventPoller.h"
xiongziliang committed
32
#include "Player/PlayerProxy.h"
33 34
#include "Rtmp/RtmpPusher.h"
#include "Common/config.h"
xiongziliang committed
35
#include "Pusher/MediaPusher.h"
36 37 38
#include "MediaFile/MediaReader.h"

using namespace std;
xiongziliang committed
39 40
using namespace toolkit;
using namespace mediakit;
41

42
//推流器,保持强引用
xiongziliang committed
43
MediaPusher::Ptr pusher;
44 45
Timer::Ptr g_timer;

46 47

//声明函数
xiongziliang committed
48
//推流失败或断开延迟2秒后重试推流
49 50
void rePushDelay(const EventPoller::Ptr &poller,
                 const string &schema,
xiongziliang committed
51 52 53 54 55
				 const string &vhost,
				 const string &app,
				 const string &stream,
				 const string &filePath,
				 const string &url) ;
56 57

//创建推流器并开始推流
58 59
void createPusher(const EventPoller::Ptr &poller,
                  const string &schema,
xiongziliang committed
60 61 62 63 64 65 66
				  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);
xiongziliang committed
67
    if(!src){
68
        //文件不存在
xiongziliang committed
69
        WarnL << "MP4文件不存在:" << filePath;
70 71 72
        return;
    }

xiongziliang committed
73
	//创建推流器并绑定一个MediaSource
74
    pusher.reset(new MediaPusher(src,poller));
xiongziliang committed
75 76 77
	//可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
//    (*pusher)[Client::kRtpType] = Rtsp::RTP_UDP;

78
	//设置推流中断处理逻辑
79
	pusher->setOnShutdown([poller,schema,vhost,app,stream,filePath, url](const SockException &ex) {
80 81
		WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
        //重新推流
82
		rePushDelay(poller,schema,vhost,app, stream,filePath, url);
83 84
	});
	//设置发布结果处理逻辑
85
	pusher->setOnPublished([poller,schema,vhost,app,stream,filePath, url](const SockException &ex) {
86 87 88
		if (ex) {
			WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
			//如果发布失败,就重试
89
			rePushDelay(poller,schema,vhost,app, stream, filePath ,url);
90 91 92 93
		}else {
			InfoL << "Publish success,Please play with player:" << url;
		}
	});
xiongziliang committed
94
	pusher->publish(url);
95 96
}

97
//推流失败或断开延迟2秒后重试推流
98 99
void rePushDelay(const EventPoller::Ptr &poller,
                 const string &schema,
xiongziliang committed
100 101 102 103 104
				 const string &vhost,
				 const string &app,
				 const string &stream,
				 const string &filePath,
				 const string &url) {
105
	g_timer = std::make_shared<Timer>(2,[poller,schema,vhost,app, stream, filePath,url]() {
106 107
		InfoL << "Re-Publishing...";
		//重新推流
108
		createPusher(poller,schema,vhost,app, stream, filePath,url);
109 110
		//此任务不重复
		return false;
111
	}, poller);
112
}
113

114
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
115
int domain(const string & filePath,const string & pushUrl){
116
	//设置日志
117
	Logger::Instance().add(std::make_shared<ConsoleChannel>());
118
	Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
119 120 121
    auto poller = EventPollerPool::Instance().getPoller();
    //vhost/app/stream可以随便自己填,现在不限制app应用名了
    createPusher(poller,FindField(pushUrl.data(), nullptr,"://"),DEFAULT_VHOST,"live","stream",filePath,pushUrl);
122

xiongziliang committed
123 124 125
	//设置退出信号处理函数
	static semaphore sem;
	signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
126
    sem.wait();
127 128
	pusher.reset();
	g_timer.reset();
129 130 131 132 133
	return 0;
}



134 135
int main(int argc,char *argv[]){
    //可以使用test_server生成的mp4文件
xiongziliang committed
136 137
	//文件使用绝对路径,推流url支持rtsp和rtmp
    return domain("/Users/xzl/Desktop/bear-1280x720-long.mp4","rtsp://127.0.0.1/live/rtsp_push");
138 139 140
}


141 142 143