2020-05-18 15:31:49 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2019-12-05 19:53:55 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2019-12-05 19:53:55 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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.
|
2019-12-05 19:53:55 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "Util/util.h"
|
2023-12-31 22:10:13 +08:00
|
|
|
|
#include "Util/logger.h"
|
2019-12-05 19:53:55 +08:00
|
|
|
|
#include "Network/TcpServer.h"
|
|
|
|
|
#include "Common/config.h"
|
|
|
|
|
#include "Rtsp/RtspSession.h"
|
|
|
|
|
#include "Rtmp/RtmpSession.h"
|
|
|
|
|
#include "Http/HttpSession.h"
|
2024-06-09 10:52:10 +08:00
|
|
|
|
#include "Rtp/RtpProcess.h"
|
2019-12-05 19:53:55 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
using namespace mediakit;
|
|
|
|
|
|
2023-04-07 23:09:10 +08:00
|
|
|
|
static semaphore sem;
|
|
|
|
|
|
2019-12-06 11:54:10 +08:00
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2023-12-31 22:10:13 +08:00
|
|
|
|
static bool loadFile(const char *path, const EventPoller::Ptr &poller) {
|
|
|
|
|
std::shared_ptr<FILE> fp(fopen(path, "rb"), [](FILE *fp) {
|
|
|
|
|
sem.post();
|
|
|
|
|
if (fp) {
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-12-06 11:54:10 +08:00
|
|
|
|
if (!fp) {
|
|
|
|
|
WarnL << "open file failed:" << path;
|
|
|
|
|
return false;
|
2019-12-05 19:53:55 +08:00
|
|
|
|
}
|
2019-12-06 11:54:10 +08:00
|
|
|
|
|
2022-08-08 17:36:07 +08:00
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
2022-06-18 13:27:15 +08:00
|
|
|
|
addr.ss_family = AF_INET;
|
2023-04-07 23:09:10 +08:00
|
|
|
|
auto sock = Socket::createSocket(poller);
|
2024-07-09 10:42:10 +08:00
|
|
|
|
auto process = RtpProcess::createProcess(MediaTuple { DEFAULT_VHOST, kRtpAppName, "test", "" });
|
2019-12-06 11:54:10 +08:00
|
|
|
|
|
2023-12-31 22:10:13 +08:00
|
|
|
|
uint64_t stamp_last = 0;
|
|
|
|
|
auto total_size = std::make_shared<size_t>(0);
|
|
|
|
|
auto do_read = [fp, total_size, sock, addr, process, stamp_last]() mutable -> int {
|
|
|
|
|
uint16_t len;
|
|
|
|
|
char rtp[0xFFFF];
|
|
|
|
|
while (true) {
|
|
|
|
|
if (2 != fread(&len, 1, 2, fp.get())) {
|
|
|
|
|
WarnL << "Read rtp size failed";
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 重新播放 [AUTO-TRANSLATED:9f678d55]
|
|
|
|
|
// Replay
|
2023-12-31 22:10:13 +08:00
|
|
|
|
fseek(fp.get(), 0, SEEK_SET);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
len = ntohs(len);
|
|
|
|
|
if (len < 12 || len > sizeof(rtp)) {
|
|
|
|
|
WarnL << "Invalid rtp size: " << len;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-10-30 21:36:35 +08:00
|
|
|
|
|
2023-12-31 22:10:13 +08:00
|
|
|
|
if (len != fread(rtp, 1, len, fp.get())) {
|
|
|
|
|
WarnL << "Read rtp data failed";
|
|
|
|
|
return 0;
|
2022-10-30 21:36:35 +08:00
|
|
|
|
}
|
2023-12-31 22:10:13 +08:00
|
|
|
|
(*total_size) += len;
|
|
|
|
|
uint64_t stamp = 0;
|
2022-10-30 21:36:35 +08:00
|
|
|
|
try {
|
2023-12-31 22:10:13 +08:00
|
|
|
|
process->inputRtp(true, sock, rtp, len, (struct sockaddr *)&addr, &stamp);
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << "Input rtp failed: " << ex.what();
|
|
|
|
|
return 0;
|
2022-10-30 21:36:35 +08:00
|
|
|
|
}
|
2021-03-07 10:05:52 +08:00
|
|
|
|
|
2024-07-21 19:31:17 +08:00
|
|
|
|
auto diff = static_cast<int64_t>(stamp - stamp_last);
|
2023-12-31 22:10:13 +08:00
|
|
|
|
if (diff < 0 || diff > 500) {
|
|
|
|
|
diff = 1;
|
|
|
|
|
}
|
|
|
|
|
if (diff) {
|
|
|
|
|
stamp_last = stamp;
|
|
|
|
|
return diff;
|
|
|
|
|
}
|
2019-12-06 11:54:10 +08:00
|
|
|
|
}
|
2023-12-31 22:10:13 +08:00
|
|
|
|
};
|
|
|
|
|
poller->doDelayTask(1, [do_read, total_size, process]() mutable {
|
|
|
|
|
auto ret = do_read();
|
|
|
|
|
if (!ret) {
|
|
|
|
|
WarnL << *total_size / 1024 << "KB";
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
});
|
|
|
|
|
|
2019-12-06 11:54:10 +08:00
|
|
|
|
return true;
|
2019-12-05 19:53:55 +08:00
|
|
|
|
}
|
2023-12-31 22:10:13 +08:00
|
|
|
|
#endif // #if defined(ENABLE_RTPPROXY)
|
2019-12-05 19:53:55 +08:00
|
|
|
|
|
2023-12-31 22:10:13 +08:00
|
|
|
|
int main(int argc, char *argv[]) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置日志 [AUTO-TRANSLATED:50ba02ba]
|
|
|
|
|
// Set log
|
2019-12-06 11:54:10 +08:00
|
|
|
|
Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel"));
|
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 启动异步日志线程 [AUTO-TRANSLATED:8340d047]
|
|
|
|
|
// Start asynchronous log thread
|
2019-12-06 11:54:10 +08:00
|
|
|
|
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());
|
2023-12-31 22:10:13 +08:00
|
|
|
|
rtspSrv->start<RtspSession>(554); // 默认554
|
|
|
|
|
rtmpSrv->start<RtmpSession>(1935); // 默认1935
|
|
|
|
|
httpSrv->start<HttpSession>(80); // 默认80
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 此处选择是否导出调试文件 [AUTO-TRANSLATED:b147c25b]
|
|
|
|
|
// Choose whether to export debug file here
|
2023-12-31 22:10:13 +08:00
|
|
|
|
// mINI::Instance()[RtpProxy::kDumpDir] = "/Users/xzl/Desktop/";
|
2019-12-13 15:43:24 +08:00
|
|
|
|
|
2023-12-31 22:10:13 +08:00
|
|
|
|
if (argc == 2) {
|
|
|
|
|
loadFile(argv[1], EventPollerPool::Instance().getPoller());
|
2023-04-07 23:09:10 +08:00
|
|
|
|
sem.wait();
|
2023-12-31 22:10:13 +08:00
|
|
|
|
} else {
|
|
|
|
|
ErrorL << "parameter error.";
|
2023-04-07 23:09:10 +08:00
|
|
|
|
}
|
2019-12-06 11:54:10 +08:00
|
|
|
|
#else
|
|
|
|
|
ErrorL << "please ENABLE_RTPPROXY and then test";
|
2023-12-31 22:10:13 +08:00
|
|
|
|
#endif // #if defined(ENABLE_RTPPROXY)
|
2019-12-06 11:54:10 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|