test_rtp.cpp 3.67 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22
 */

#include <map>
#include <iostream>
#include "Util/MD5.h"
#include "Util/File.h"
#include "Util/logger.h"
#include "Util/SSLBox.h"
#include "Util/util.h"
#include "Network/TcpServer.h"
#include "Common/config.h"
#include "Rtsp/RtspSession.h"
#include "Rtmp/RtmpSession.h"
#include "Http/HttpSession.h"
23
#include "Rtp/RtpSelector.h"
24 25 26 27 28

using namespace std;
using namespace toolkit;
using namespace mediakit;

29 30
static semaphore sem;

31
#if defined(ENABLE_RTPPROXY)
32
static bool loadFile(const char *path, const EventPoller::Ptr &poller){
xiongziliang committed
33
    FILE *fp = fopen(path, "rb");
34 35 36
    if (!fp) {
        WarnL << "open file failed:" << path;
        return false;
37
    }
38

ziyue committed
39
    uint64_t timeStamp_last = 0;
xiongziliang committed
40
    uint16_t len;
41
    char rtp[0xFFFF];
42 43
    struct sockaddr_storage addr;
    memset(&addr, 0, sizeof(addr));
44
    addr.ss_family = AF_INET;
45
    auto sock = Socket::createSocket(poller);
xiongziliang committed
46
    size_t total_size = 0;
47 48
    RtpProcess::Ptr process;
    uint32_t ssrc = 0;
xiongziliang committed
49 50
    while (true) {
        if (2 != fread(&len, 1, 2, fp)) {
51
            WarnL;
xiongziliang committed
52
            break;
53 54 55 56
        }
        len = ntohs(len);
        if (len < 12 || len > sizeof(rtp)) {
            WarnL << len;
xiongziliang committed
57
            break;
58 59
        }

xiongziliang committed
60
        if (len != fread(rtp, 1, len, fp)) {
61
            WarnL;
xiongziliang committed
62
            break;
63
        }
xiongziliang committed
64
        total_size += len;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        uint64_t timeStamp = 0;

        if (!process) {
            if (!RtpSelector::getSSRC(rtp, len, ssrc)) {
                WarnL << "get ssrc from rtp failed:" << len;
                return false;
            }
            process = RtpSelector::Instance().getProcess(printSSRC(ssrc), true);
        }
        if (process) {
            try {
                process->inputRtp(true, sock, rtp, len, (struct sockaddr *)&addr, &timeStamp);
            } catch (...) {
                RtpSelector::Instance().delProcess(printSSRC(ssrc), process.get());
                throw;
            }
        }
xiongziliang committed
82 83 84 85 86 87

        auto diff = timeStamp - timeStamp_last;
        if (diff > 0 && diff < 500) {
            usleep(diff * 1000);
        } else {
            usleep(1 * 1000);
88 89
        }
        timeStamp_last = timeStamp;
xiongziliang committed
90 91 92
    }
    WarnL << total_size / 1024 << "KB";
    fclose(fp);
93
    return true;
94
}
95
#endif//#if defined(ENABLE_RTPPROXY)
96

97 98 99 100 101 102 103 104 105 106 107 108 109
int main(int argc,char *argv[]) {
    //设置日志
    Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel"));
#if defined(ENABLE_RTPPROXY)
    //启动异步日志线程
    Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
    loadIniConfig((exeDir() + "config.ini").data());
    TcpServer::Ptr rtspSrv(new TcpServer());
    TcpServer::Ptr rtmpSrv(new TcpServer());
    TcpServer::Ptr httpSrv(new TcpServer());
    rtspSrv->start<RtspSession>(554);//默认554
    rtmpSrv->start<RtmpSession>(1935);//默认1935
    httpSrv->start<HttpSession>(80);//默认80
110 111 112
    //此处选择是否导出调试文件
//    mINI::Instance()[RtpProxy::kDumpDir] = "/Users/xzl/Desktop/";

113 114 115 116 117 118 119
    if (argc == 2){
        auto poller = EventPollerPool::Instance().getPoller();
        poller->async_first([poller,argv](){
            loadFile(argv[1],poller);
            sem.post();
        });
        sem.wait();
120
        sleep(1);
121
    }
xiongziliang committed
122 123
    else
      ErrorL << "parameter error.";
124 125 126 127 128
#else
    ErrorL << "please ENABLE_RTPPROXY and then test";
#endif//#if defined(ENABLE_RTPPROXY)
    return 0;
}
129 130