test_rtmpPusher.cpp 4.94 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * MIT License
 *
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * 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 35 36
#include "Rtmp/RtmpPusher.h"
#include "Common/config.h"

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

40 41
//推流器,保持强引用
RtmpPusher::Ptr pusher;
42

43
//声明函数
44
void rePushDelay(const string &app, const string &stream, const string &url);
45 46

//创建推流器并开始推流
47
void createPusher(const string &app, const string &stream, const string &url) {
48
    //创建推流器并绑定一个RtmpMediaSource
49
    pusher.reset(new RtmpPusher(DEFAULT_VHOST, app.data(), stream.data()));
50
    //设置推流中断处理逻辑
51
    pusher->setOnShutdown([app, stream, url](const SockException &ex) {
52 53
        WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
        //重试
54
        rePushDelay(app, stream, url);
55 56
    });
    //设置发布结果处理逻辑
57
    pusher->setOnPublished([app, stream, url](const SockException &ex) {
58 59 60
        if (ex) {
            WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
            //如果发布失败,就重试
61 62
            rePushDelay(app, stream, url);
        } else {
63 64 65 66 67
            InfoL << "Publish success,Please play with player:" << url;
        }
    });
    pusher->publish(url.data());
}
xiongziliang committed
68

69
//推流失败或断开延迟2秒后重试推流
70 71 72 73 74 75 76 77 78
void rePushDelay(const string &app, const string &stream, const string &url) {
    //上次延时两秒的任务可能还没执行,所以我们要先取消上次任务
    AsyncTaskThread::Instance().CancelTask(0);
    //2秒后执行重新推流的任务
    AsyncTaskThread::Instance().DoTaskDelay(0, 2000, [app, stream, url]() {
        InfoL << "Re-Publishing...";
        //重新推流
        createPusher(app, stream, url);
        //此任务不重复
79
        return false;
80
    });
81 82 83
}

//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
84 85 86 87 88 89 90 91 92 93 94 95 96 97
int domain(const string &playUrl, const string &pushUrl) {
    //设置退出信号处理函数
    signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); });
    //设置日志
    Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
    Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());

    {
        //拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
        //你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请查看test_rtmpPusherMp4.cpp代码)
        PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "app", "stream"));
        player->play(playUrl.data());

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

        //事件轮询
        EventPoller::Instance().runLoop();
108
        pusher.reset();
109 110 111 112 113 114 115 116 117
    }
    //删除事件监听
    NoticeCenter::Instance().delListener(nullptr);

    //清理程序
    EventPoller::Destory();
    AsyncTaskThread::Destory();
    Logger::Destory();
    return 0;
xiongziliang committed
118 119 120
}


121
int main(int argc, char *argv[]) {
122
    return domain("rtmp://live.hkstv.hk.lxdns.com/live/hks", "rtmp://127.0.0.1/live/stream");
xiongziliang committed
123 124 125 126
}



xiongziliang committed
127 128 129