test_pusher.cpp 5.23 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.
 */
xiongziliang committed
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"
xiongziliang committed
33 34
#include "Rtmp/RtmpPusher.h"
#include "Common/config.h"
xiongziliang committed
35
#include "Pusher/MediaPusher.h"
xiongziliang committed
36 37

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

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

45
//声明函数
46
void rePushDelay(const EventPoller::Ptr &poller,const string &schema,const string &vhost,const string &app, const string &stream, const string &url);
47 48

//创建推流器并开始推流
49
void createPusher(const EventPoller::Ptr &poller, const string &schema,const string &vhost,const string &app, const string &stream, const string &url) {
xiongziliang committed
50
    //创建推流器并绑定一个MediaSource
51
    pusher.reset(new MediaPusher(schema,vhost, app, stream,poller));
52 53
    //可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
//    (*pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
54
    //设置推流中断处理逻辑
55
    pusher->setOnShutdown([poller,schema,vhost, app, stream, url](const SockException &ex) {
56 57
        WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
        //重试
58
        rePushDelay(poller,schema,vhost,app, stream, url);
59 60
    });
    //设置发布结果处理逻辑
61
    pusher->setOnPublished([poller,schema,vhost, app, stream, url](const SockException &ex) {
62 63 64
        if (ex) {
            WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
            //如果发布失败,就重试
65
            rePushDelay(poller,schema,vhost,app, stream, url);
66
        } else {
67 68 69
            InfoL << "Publish success,Please play with player:" << url;
        }
    });
xiongziliang committed
70
    pusher->publish(url);
71
}
xiongziliang committed
72

73
//推流失败或断开延迟2秒后重试推流
74 75
void rePushDelay(const EventPoller::Ptr &poller,const string &schema,const string &vhost,const string &app, const string &stream, const string &url) {
    g_timer = std::make_shared<Timer>(2,[poller,schema,vhost,app, stream, url]() {
76 77
        InfoL << "Re-Publishing...";
        //重新推流
78
        createPusher(poller,schema,vhost,app, stream, url);
79
        //此任务不重复
80
        return false;
81
    }, poller);
82 83 84
}

//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
85 86
int domain(const string &playUrl, const string &pushUrl) {
    //设置日志
87
    Logger::Instance().add(std::make_shared<ConsoleChannel>());
88
    Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
89
    auto poller = EventPollerPool::Instance().getPoller();
90

91 92
    //拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
    //你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请查看test_rtmpPusherMp4.cpp代码)
xiongziliang committed
93
    PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "app", "stream",true,true,false,false,-1 , poller));
xiongziliang committed
94 95
    //可以指定rtsp拉流方式,支持tcp和udp方式,默认tcp
//    (*player)[Client::kRtpType] = Rtsp::RTP_UDP;
96 97 98 99
    player->play(playUrl.data());

    //监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发
    NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaChanged,
100
                                         [pushUrl,poller](BroadcastMediaChangedArgs) {
101
                                             //媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源
xiongziliang committed
102
                                             if(bRegist && pushUrl.find(schema) == 0){
103
                                                 createPusher(poller,schema,vhost,app, stream, pushUrl);
104 105
                                             }
                                         });
xiongziliang committed
106 107 108 109

    //设置退出信号处理函数
    static semaphore sem;
    signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
110
    sem.wait();
111 112
    pusher.reset();
    g_timer.reset();
113
    return 0;
xiongziliang committed
114 115 116
}


117
int main(int argc, char *argv[]) {
xiongziliang committed
118
    return domain("rtmp://live.hkstv.hk.lxdns.com/live/hks1", "rtsp://127.0.0.1/live/rtsp_push");
xiongziliang committed
119 120 121 122
}



xiongziliang committed
123 124 125