2021-01-31 20:46:55 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2021-01-23 09:42:45 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2021-01-23 09:42:45 +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
|
2021-01-23 09:42:45 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/onceToken.h"
|
|
|
|
|
#include "Util/CMD.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Common/config.h"
|
|
|
|
|
#include "Common/Parser.h"
|
2021-01-23 09:42:45 +08:00
|
|
|
|
#include "Rtsp/Rtsp.h"
|
|
|
|
|
#include "Thread/WorkThreadPool.h"
|
|
|
|
|
#include "Pusher/MediaPusher.h"
|
|
|
|
|
#include "Player/PlayerProxy.h"
|
2024-06-01 18:15:35 +08:00
|
|
|
|
#include "Record/MP4Reader.h"
|
2021-01-23 09:42:45 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
using namespace mediakit;
|
|
|
|
|
|
|
|
|
|
class CMD_main : public CMD {
|
|
|
|
|
public:
|
|
|
|
|
CMD_main() {
|
|
|
|
|
_parser.reset(new OptionParser(nullptr));
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('l',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"level",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
to_string(LTrace).data(),/*该选项默认值*/
|
|
|
|
|
false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"日志等级,LTrace~LError(0~4)",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('t',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"threads",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
to_string(thread::hardware_concurrency()).data(),/*该选项默认值*/
|
|
|
|
|
false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"启动事件触发线程数",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('i',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"in",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
nullptr,/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
2024-06-01 18:15:35 +08:00
|
|
|
|
"拉流url,支持rtsp/rtmp/hls/mp4文件",/*该选项说明文字*/
|
2021-01-23 09:42:45 +08:00
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('o',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"out",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
nullptr,/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"推流url,支持rtsp/rtmp",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('c',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"count",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
"1000",/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"推流客户端个数",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('d',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"delay",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
"50",/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"启动推流客户端间隔,单位毫秒",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('m',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"merge",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
"300",/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"推流合并写毫秒,合并写能提高性能",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('T',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"rtp",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
to_string((int) (Rtsp::RTP_TCP)).data(),/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
2024-06-01 18:15:35 +08:00
|
|
|
|
"rtsp拉流和推流方式,支持tcp/udp:0/1", /*该选项说明文字*/
|
|
|
|
|
nullptr);
|
2021-01-23 09:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~CMD_main() override {}
|
|
|
|
|
|
2024-06-01 18:15:35 +08:00
|
|
|
|
const char *description() const override { return "主程序命令参数"; }
|
2021-01-23 09:42:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 此程序用于推流性能测试 [AUTO-TRANSLATED:45b48457]
|
|
|
|
|
// This program is used for streaming performance testing
|
2021-01-23 09:42:45 +08:00
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
CMD_main cmd_main;
|
|
|
|
|
try {
|
|
|
|
|
cmd_main.operator()(argc, argv);
|
|
|
|
|
} catch (ExitException &) {
|
|
|
|
|
return 0;
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
cout << ex.what() << endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int threads = cmd_main["threads"];
|
2024-06-01 18:15:35 +08:00
|
|
|
|
LogLevel logLevel = (LogLevel)cmd_main["level"].as<int>();
|
2021-01-23 09:42:45 +08:00
|
|
|
|
logLevel = MIN(MAX(logLevel, LTrace), LError);
|
|
|
|
|
auto in_url = cmd_main["in"];
|
|
|
|
|
auto out_url = cmd_main["out"];
|
|
|
|
|
auto rtp_type = cmd_main["rtp"].as<int>();
|
|
|
|
|
auto delay_ms = cmd_main["delay"].as<int>();
|
|
|
|
|
auto pusher_count = cmd_main["count"].as<int>();
|
|
|
|
|
auto merge_ms = cmd_main["merge"].as<int>();
|
2023-06-10 12:28:49 +08:00
|
|
|
|
auto schema = findSubString(out_url.data(), nullptr, "://");
|
2021-01-23 09:42:45 +08:00
|
|
|
|
if (schema != RTSP_SCHEMA && schema != RTMP_SCHEMA) {
|
|
|
|
|
cout << "推流协议只支持rtsp或rtmp!" << endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-06-01 18:15:35 +08:00
|
|
|
|
const std::string app = "app";
|
|
|
|
|
const std::string stream = "test";
|
2021-01-23 09:42:45 +08:00
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置日志 [AUTO-TRANSLATED:50372045]
|
|
|
|
|
// Set log
|
2021-01-23 09:42:45 +08:00
|
|
|
|
Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel", logLevel));
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 启动异步日志线程 [AUTO-TRANSLATED:c93cc6f4]
|
|
|
|
|
// Start asynchronous log thread
|
2021-01-23 09:42:45 +08:00
|
|
|
|
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置线程数 [AUTO-TRANSLATED:22ec5cc9]
|
|
|
|
|
// Set the number of threads
|
2021-01-23 09:42:45 +08:00
|
|
|
|
EventPollerPool::setPoolSize(threads);
|
|
|
|
|
WorkThreadPool::setPoolSize(threads);
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置合并写 [AUTO-TRANSLATED:7bf3456d]
|
|
|
|
|
// Set merge write
|
2021-01-23 09:42:45 +08:00
|
|
|
|
mINI::Instance()[General::kMergeWriteMS] = merge_ms;
|
|
|
|
|
|
2022-03-12 13:19:21 +08:00
|
|
|
|
ProtocolOption option;
|
|
|
|
|
option.enable_hls = false;
|
|
|
|
|
option.enable_mp4 = false;
|
2024-06-01 18:15:35 +08:00
|
|
|
|
MediaSource::Ptr src = nullptr;
|
|
|
|
|
PlayerProxy::Ptr proxy = nullptr;;
|
|
|
|
|
|
2024-07-21 19:31:17 +08:00
|
|
|
|
auto tuple = MediaTuple { DEFAULT_VHOST, app, stream, "" };
|
2024-06-01 18:15:35 +08:00
|
|
|
|
if (end_with(in_url, ".mp4")) {
|
|
|
|
|
// create MediaSource from mp4file
|
2024-07-14 09:32:41 +08:00
|
|
|
|
auto reader = std::make_shared<MP4Reader>(tuple, in_url);
|
2024-06-01 18:15:35 +08:00
|
|
|
|
//mp4 repeat
|
|
|
|
|
reader->startReadMP4(0, true, true);
|
|
|
|
|
src = MediaSource::find(schema, DEFAULT_VHOST, app, stream, false);
|
|
|
|
|
if (!src) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// mp4文件不存在 [AUTO-TRANSLATED:80188fb8]
|
|
|
|
|
// mp4 file does not exist
|
2024-06-01 18:15:35 +08:00
|
|
|
|
WarnL << "no such file or directory: " << in_url;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 添加拉流代理 [AUTO-TRANSLATED:aa516f44]
|
|
|
|
|
// Add pull stream proxy
|
2024-07-14 09:32:41 +08:00
|
|
|
|
proxy = std::make_shared<PlayerProxy>(tuple, option);
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// rtsp拉流代理方式 [AUTO-TRANSLATED:065d328d]
|
|
|
|
|
// rtsp pull stream proxy method
|
2024-06-01 18:15:35 +08:00
|
|
|
|
(*proxy)[Client::kRtpType] = rtp_type;
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 开始拉流代理 [AUTO-TRANSLATED:6937338d]
|
|
|
|
|
// Start pull stream proxy
|
2024-06-01 18:15:35 +08:00
|
|
|
|
proxy->play(in_url);
|
2022-03-12 13:19:21 +08:00
|
|
|
|
|
2024-06-01 18:15:35 +08:00
|
|
|
|
}
|
2021-01-23 09:42:45 +08:00
|
|
|
|
|
2024-06-01 18:15:35 +08:00
|
|
|
|
auto get_src = [schema,app,stream]() {
|
|
|
|
|
return MediaSource::find(schema, DEFAULT_VHOST, app, stream, false);
|
2021-01-23 09:42:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 推流器map [AUTO-TRANSLATED:279fcfb0]
|
|
|
|
|
// Streamer map
|
2021-01-23 09:42:45 +08:00
|
|
|
|
recursive_mutex mtx;
|
|
|
|
|
unordered_map<void *, MediaPusher::Ptr> pusher_map;
|
|
|
|
|
|
2024-06-01 18:15:35 +08:00
|
|
|
|
|
2021-01-31 19:19:08 +08:00
|
|
|
|
auto add_pusher = [&](const MediaSource::Ptr &src, const string &rand_str, size_t index) {
|
2021-01-23 09:42:45 +08:00
|
|
|
|
auto pusher = std::make_shared<MediaPusher>(src);
|
|
|
|
|
auto tag = pusher.get();
|
|
|
|
|
pusher->setOnCreateSocket([](const EventPoller::Ptr &poller) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// socket关闭互斥锁,提高性能 [AUTO-TRANSLATED:471fc644]
|
|
|
|
|
// Socket close mutex, improve performance
|
2024-01-06 17:47:43 +08:00
|
|
|
|
return Socket::createSocket(poller, false);
|
2021-01-23 09:42:45 +08:00
|
|
|
|
});
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置推流失败监听 [AUTO-TRANSLATED:4ad49de9]
|
|
|
|
|
// Set push stream failure listener
|
2021-01-23 09:42:45 +08:00
|
|
|
|
pusher->setOnPublished([&mtx, &pusher_map, tag](const SockException &ex) {
|
|
|
|
|
if (ex) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 推流失败,移除之 [AUTO-TRANSLATED:97a29246]
|
|
|
|
|
// Push stream failed, remove it
|
2021-01-23 09:42:45 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(mtx);
|
|
|
|
|
pusher_map.erase(tag);
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置推流中途断开监听 [AUTO-TRANSLATED:228076ef]
|
|
|
|
|
// Set push stream disconnection listener
|
2021-01-23 09:42:45 +08:00
|
|
|
|
pusher->setOnShutdown([&mtx, &pusher_map, tag](const SockException &ex) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 推流中途失败,移除之 [AUTO-TRANSLATED:00e0928a]
|
|
|
|
|
// Push stream failed halfway, remove it
|
2021-01-23 09:42:45 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(mtx);
|
|
|
|
|
pusher_map.erase(tag);
|
|
|
|
|
});
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置rtsp推流方式(在rtsp推流时有效) [AUTO-TRANSLATED:2dc733df]
|
|
|
|
|
// Set rtsp push stream method (effective when rtsp push stream)
|
2021-01-23 09:42:45 +08:00
|
|
|
|
(*pusher)[Client::kRtpType] = rtp_type;
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 发起推流请求,每个推流端的stream_id都不一样 [AUTO-TRANSLATED:8b356fcb]
|
|
|
|
|
// Initiate push stream request, each push stream end has a different stream_id
|
2021-01-23 09:42:45 +08:00
|
|
|
|
string url = StrPrinter << out_url << "_" << rand_str << "_" << index;
|
|
|
|
|
pusher->publish(url);
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 保持对象不销毁 [AUTO-TRANSLATED:650977d0]
|
|
|
|
|
// Keep the object from being destroyed
|
2021-01-23 09:42:45 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(mtx);
|
|
|
|
|
pusher_map.emplace(tag, std::move(pusher));
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 休眠后再启动下一个推流,防止短时间海量链接 [AUTO-TRANSLATED:df224fc9]
|
|
|
|
|
// Sleep and then start the next push stream to prevent massive connections in a short time
|
2021-01-23 09:42:45 +08:00
|
|
|
|
if (delay_ms > 0) {
|
|
|
|
|
usleep(1000 * delay_ms);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置退出信号 [AUTO-TRANSLATED:02c7fa30]
|
|
|
|
|
// Set exit signal
|
2021-01-23 09:42:45 +08:00
|
|
|
|
static bool exit_flag = false;
|
|
|
|
|
signal(SIGINT, [](int) { exit_flag = true; });
|
|
|
|
|
while (!exit_flag) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 休眠一秒打印 [AUTO-TRANSLATED:239dc996]
|
|
|
|
|
// Sleep for one second and print
|
2021-01-23 09:42:45 +08:00
|
|
|
|
sleep(1);
|
|
|
|
|
|
2021-01-31 18:24:41 +08:00
|
|
|
|
size_t alive_pusher = 0;
|
2021-01-23 09:42:45 +08:00
|
|
|
|
{
|
|
|
|
|
lock_guard<recursive_mutex> lck(mtx);
|
|
|
|
|
alive_pusher = pusher_map.size();
|
|
|
|
|
}
|
|
|
|
|
InfoL << "在线推流器个数:" << alive_pusher;
|
|
|
|
|
auto src = get_src();
|
2021-01-31 18:24:41 +08:00
|
|
|
|
for(size_t i = 0; i < pusher_count - alive_pusher && src && !exit_flag; ++i){
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 有些推流器失败了,那么我们重试添加 [AUTO-TRANSLATED:d01fb300]
|
|
|
|
|
// Some push streamers failed, so we retry adding
|
2021-01-23 09:42:45 +08:00
|
|
|
|
add_pusher(get_src(), makeRandStr(8), i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|