test_rtp.cpp 2.68 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
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.
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 31 32 33 34
#if defined(ENABLE_RTPPROXY)
static bool loadFile(const char *path){
    FILE *fp = fopen(path, "rb");
    if (!fp) {
        WarnL << "open file failed:" << path;
        return false;
35
    }
36 37 38 39

    uint32_t timeStamp_last = 0;
    uint16_t len;
    char rtp[2 * 1024];
xiongziliang committed
40
    struct sockaddr addr = {0};
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    while (true) {
        if (2 != fread(&len, 1, 2, fp)) {
            WarnL;
            break;
        }
        len = ntohs(len);
        if (len < 12 || len > sizeof(rtp)) {
            WarnL << len;
            break;
        }

        if (len != fread(rtp, 1, len, fp)) {
            WarnL;
            break;
        }

        uint32_t timeStamp;
58
        RtpSelector::Instance().inputRtp(nullptr,rtp,len, &addr,&timeStamp);
59 60
        if(timeStamp_last){
            auto diff = timeStamp - timeStamp_last;
xiongziliang committed
61
            if(diff > 0 && diff < 500){
62 63 64 65 66 67 68
                usleep(diff * 1000);
            }
        }
        timeStamp_last = timeStamp;
    }
    fclose(fp);
    return true;
69
}
70
#endif//#if defined(ENABLE_RTPPROXY)
71

72 73 74 75 76 77 78 79 80 81 82 83 84
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
85 86 87
    //此处选择是否导出调试文件
//    mINI::Instance()[RtpProxy::kDumpDir] = "/Users/xzl/Desktop/";

88 89 90 91
    if (argc == 2)
      loadFile(argv[1]);
    else
      ErrorL << "parameter error.";
92 93 94 95 96
#else
    ErrorL << "please ENABLE_RTPPROXY and then test";
#endif//#if defined(ENABLE_RTPPROXY)
    return 0;
}
97 98