test_pusherMp4.cpp 4.38 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
 */
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"
xiongziliang committed
19
#include "Pusher/MediaPusher.h"
20
#include "Record/MP4Reader.h"
21 22

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

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

30 31

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

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

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

62 63 64
    //设置推流中断处理逻辑
    pusher->setOnShutdown([poller,schema,vhost,app,stream,filePath, url](const SockException &ex) {
        WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
65
        //重新推流
66 67 68 69 70 71 72 73 74 75 76 77 78
        rePushDelay(poller,schema,vhost,app, stream,filePath, url);
    });
    //设置发布结果处理逻辑
    pusher->setOnPublished([poller,schema,vhost,app,stream,filePath, url](const SockException &ex) {
        if (ex) {
            WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
            //如果发布失败,就重试
            rePushDelay(poller,schema,vhost,app, stream, filePath ,url);
        }else {
            InfoL << "Publish success,Please play with player:" << url;
        }
    });
    pusher->publish(url);
79 80
}

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

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

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



118 119
int main(int argc,char *argv[]){
    //可以使用test_server生成的mp4文件
120
    //文件使用绝对路径,推流url支持rtsp和rtmp
xiongziliang committed
121
    return domain("/Users/xzl/git/ZLMediaKit/release/mac/Debug/www/record/live/rtsp_test1/2020-04-03/15-32-24.mp4","rtsp://127.0.0.1/live/rtsp_push");
122 123 124
}


125 126 127