test_pusher.cpp 4.7 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
xiongziliang committed
9
 */
xiongziliang committed
10 11 12 13 14 15

#include <signal.h>
#include <iostream>
#include "Util/logger.h"
#include "Util/NoticeCenter.h"
#include "Poller/EventPoller.h"
xiongziliang committed
16
#include "Player/PlayerProxy.h"
xiongziliang committed
17 18
#include "Rtmp/RtmpPusher.h"
#include "Common/config.h"
xiongziliang committed
19
#include "Pusher/MediaPusher.h"
xiongziliang committed
20 21

using namespace std;
xiongziliang committed
22 23
using namespace toolkit;
using namespace mediakit;
xiongziliang committed
24

25
//推流器,保持强引用
xiongziliang committed
26
MediaPusher::Ptr pusher;
27
Timer::Ptr g_timer;
28

29
//声明函数
30
void rePushDelay(const EventPoller::Ptr &poller,const string &schema,const string &vhost,const string &app, const string &stream, const string &url);
31 32

//创建推流器并开始推流
33
void createPusher(const EventPoller::Ptr &poller, const string &schema,const string &vhost,const string &app, const string &stream, const string &url) {
xiongziliang committed
34
    //创建推流器并绑定一个MediaSource
35
    pusher.reset(new MediaPusher(schema,vhost, app, stream,poller));
36 37
    //可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
//    (*pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
38
    //设置推流中断处理逻辑
39
    pusher->setOnShutdown([poller,schema,vhost, app, stream, url](const SockException &ex) {
40 41
        WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
        //重试
42
        rePushDelay(poller,schema,vhost,app, stream, url);
43 44
    });
    //设置发布结果处理逻辑
45
    pusher->setOnPublished([poller,schema,vhost, app, stream, url](const SockException &ex) {
46 47 48
        if (ex) {
            WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
            //如果发布失败,就重试
49
            rePushDelay(poller,schema,vhost,app, stream, url);
50
        } else {
51 52 53
            InfoL << "Publish success,Please play with player:" << url;
        }
    });
xiongziliang committed
54
    pusher->publish(url);
55
}
xiongziliang committed
56

57
//推流失败或断开延迟2秒后重试推流
58 59
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]() {
60 61
        InfoL << "Re-Publishing...";
        //重新推流
62
        createPusher(poller,schema,vhost,app, stream, url);
63
        //此任务不重复
64
        return false;
65
    }, poller);
66 67 68
}

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

75 76
    //拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
    //你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请查看test_rtmpPusherMp4.cpp代码)
77 78 79 80 81 82 83 84 85
    MediaInfo info(pushUrl);
    bool enable_rtsp = true;
    bool enable_rtmp = true;
    if(info._schema == RTSP_SCHEMA){
        enable_rtmp = false;
    }else if(info._schema == RTMP_SCHEMA){
        enable_rtsp = false;
    }
    PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "app", "stream",enable_rtsp,enable_rtmp,false,false,-1 , poller));
xiongziliang committed
86 87
    //可以指定rtsp拉流方式,支持tcp和udp方式,默认tcp
//    (*player)[Client::kRtpType] = Rtsp::RTP_UDP;
88 89 90 91
    player->play(playUrl.data());

    //监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发
    NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaChanged,
92
                                         [pushUrl,poller](BroadcastMediaChangedArgs) {
93
                                             //媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源
xiongziliang committed
94 95
                                             if(bRegist && pushUrl.find(sender.getSchema()) == 0){
                                                 createPusher(poller,sender.getSchema(),sender.getVhost(),sender.getApp(), sender.getId(), pushUrl);
96 97
                                             }
                                         });
xiongziliang committed
98 99 100 101

    //设置退出信号处理函数
    static semaphore sem;
    signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
102
    sem.wait();
103 104
    pusher.reset();
    g_timer.reset();
105
    return 0;
xiongziliang committed
106 107 108
}


109
int main(int argc, char *argv[]) {
xiongziliang committed
110
    return domain("rtmp://live.hkstv.hk.lxdns.com/live/hks1", "rtsp://127.0.0.1/live/rtsp_push");
xiongziliang committed
111 112 113 114
}



xiongziliang committed
115 116 117