test_pusherMp4.cpp 4.77 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
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
 */
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"
17 18
#include "Rtmp/RtmpPusher.h"
#include "Common/config.h"
mtdxc committed
19
#include "Common/Parser.h"
xiongziliang committed
20
#include "Pusher/MediaPusher.h"
21
#include "Record/MP4Reader.h"
22 23

using namespace std;
xiongziliang committed
24 25
using namespace toolkit;
using namespace mediakit;
26

27
//推流器,保持强引用
28
MediaPusher::Ptr g_pusher;
29
Timer::Ptr g_timer;
30
MediaSource::Ptr g_src;
31 32

//声明函数
xiongziliang committed
33
//推流失败或断开延迟2秒后重试推流
34 35
void rePushDelay(const EventPoller::Ptr &poller,
                 const string &schema,
36 37 38 39
                 const string &vhost,
                 const string &app,
                 const string &stream,
                 const string &filePath,
40
                 const string &url);
41 42

//创建推流器并开始推流
43 44
void createPusher(const EventPoller::Ptr &poller,
                  const string &schema,
45 46 47 48 49
                  const string &vhost,
                  const string &app,
                  const string &stream,
                  const string &filePath,
                  const string &url) {
50 51 52 53 54
    if (!g_src) {
        //不限制APP名,并且指定文件绝对路径
        g_src = MediaSource::createFromMP4(schema, vhost, app, stream, filePath, false);
    }
    if (!g_src) {
55
        //文件不存在
xiongziliang committed
56
        WarnL << "MP4文件不存在:" << filePath;
57 58 59
        return;
    }

60
    //创建推流器并绑定一个MediaSource
61
    g_pusher.reset(new MediaPusher(g_src, poller));
62
    //可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
63
    //(*g_pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
xiongziliang committed
64

65
    //设置推流中断处理逻辑
66
    g_pusher->setOnShutdown([poller, schema, vhost, app, stream, filePath, url](const SockException &ex) {
67
        WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
68
        //重新推流
69
        rePushDelay(poller, schema, vhost, app, stream, filePath, url);
70
    });
71

72
    //设置发布结果处理逻辑
73
    g_pusher->setOnPublished([poller, schema, vhost, app, stream, filePath, url](const SockException &ex) {
74 75 76
        if (ex) {
            WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
            //如果发布失败,就重试
77 78
            rePushDelay(poller, schema, vhost, app, stream, filePath, url);
        } else {
79 80 81
            InfoL << "Publish success,Please play with player:" << url;
        }
    });
82
    g_pusher->publish(url);
83 84
}

85
//推流失败或断开延迟2秒后重试推流
86 87
void rePushDelay(const EventPoller::Ptr &poller,
                 const string &schema,
88 89 90 91 92
                 const string &vhost,
                 const string &app,
                 const string &stream,
                 const string &filePath,
                 const string &url) {
93
    g_timer = std::make_shared<Timer>(2.0f, [poller, schema, vhost, app, stream, filePath, url]() {
94 95
        InfoL << "Re-Publishing...";
        //重新推流
96
        createPusher(poller, schema, vhost, app, stream, filePath, url);
97 98 99
        //此任务不重复
        return false;
    }, poller);
100
}
101

102
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
103
int domain(const string &filePath, const string &pushUrl) {
104 105 106
    //设置日志
    Logger::Instance().add(std::make_shared<ConsoleChannel>());
    Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
107 108
    //循环点播mp4文件
    mINI::Instance()[Record::kFileRepeat] = 1;
109 110 111 112 113
    mINI::Instance()[Protocol::kHlsDemand] = 1;
    mINI::Instance()[Protocol::kTSDemand] = 1;
    mINI::Instance()[Protocol::kFMP4Demand] = 1;
    //mINI::Instance()[Protocol::kRtspDemand] = 1;
    //mINI::Instance()[Protocol::kRtmpDemand] = 1;
114

115 116
    auto poller = EventPollerPool::Instance().getPoller();
    //vhost/app/stream可以随便自己填,现在不限制app应用名了
xia-chu committed
117
    createPusher(poller, findSubString(pushUrl.data(), nullptr, "://").substr(0, 4), DEFAULT_VHOST, "live", "stream", filePath, pushUrl);
118 119 120
    //设置退出信号处理函数
    static semaphore sem;
    signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
121
    sem.wait();
122
    g_pusher.reset();
123 124
    g_timer.reset();
    return 0;
125 126
}

127
int main(int argc, char *argv[]) {
128
    //可以使用test_server生成的mp4文件
129
    //文件使用绝对路径,推流url支持rtsp和rtmp
130
    return domain("/home/work/test2.mp4", "rtmp://127.0.0.1/live/rtsp_push");
131 132 133
}


134 135 136