No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

189 líneas
7.9KB

  1. /*
  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. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <signal.h>
  11. #include <atomic>
  12. #include <iostream>
  13. #include "Util/logger.h"
  14. #include "Util/onceToken.h"
  15. #include "Util/CMD.h"
  16. #include "Common/config.h"
  17. #include "Rtsp/UDPServer.h"
  18. #include "Thread/WorkThreadPool.h"
  19. #include "Player/PlayerProxy.h"
  20. using namespace std;
  21. using namespace toolkit;
  22. using namespace mediakit;
  23. class CMD_main : public CMD {
  24. public:
  25. CMD_main() {
  26. _parser.reset(new OptionParser(nullptr));
  27. (*_parser) << Option('l',/*该选项简称,如果是\x00则说明无简称*/
  28. "level",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
  29. Option::ArgRequired,/*该选项后面必须跟值*/
  30. to_string(LTrace).data(),/*该选项默认值*/
  31. false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
  32. "日志等级,LTrace~LError(0~4)",/*该选项说明文字*/
  33. nullptr);
  34. (*_parser) << Option('t',/*该选项简称,如果是\x00则说明无简称*/
  35. "threads",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
  36. Option::ArgRequired,/*该选项后面必须跟值*/
  37. to_string(thread::hardware_concurrency()).data(),/*该选项默认值*/
  38. false,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
  39. "启动事件触发线程数",/*该选项说明文字*/
  40. nullptr);
  41. (*_parser) << Option('i',/*该选项简称,如果是\x00则说明无简称*/
  42. "in",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
  43. Option::ArgRequired,/*该选项后面必须跟值*/
  44. nullptr,/*该选项默认值*/
  45. true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
  46. "拉流url,支持rtsp/rtmp/hls",/*该选项说明文字*/
  47. nullptr);
  48. (*_parser) << Option('c',/*该选项简称,如果是\x00则说明无简称*/
  49. "count",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
  50. Option::ArgRequired,/*该选项后面必须跟值*/
  51. "1000",/*该选项默认值*/
  52. true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
  53. "拉流播放器个数",/*该选项说明文字*/
  54. nullptr);
  55. (*_parser) << Option('d',/*该选项简称,如果是\x00则说明无简称*/
  56. "delay",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
  57. Option::ArgRequired,/*该选项后面必须跟值*/
  58. "10",/*该选项默认值*/
  59. true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
  60. "启动拉流客户端间隔,单位毫秒",/*该选项说明文字*/
  61. nullptr);
  62. (*_parser) << Option('T',/*该选项简称,如果是\x00则说明无简称*/
  63. "rtp",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
  64. Option::ArgRequired,/*该选项后面必须跟值*/
  65. to_string((int) (Rtsp::RTP_TCP)).data(),/*该选项默认值*/
  66. true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
  67. "rtsp拉流方式,支持tcp/udp/multicast:0/1/2",/*该选项说明文字*/
  68. nullptr);
  69. }
  70. ~CMD_main() override {}
  71. const char *description() const override {
  72. return "主程序命令参数";
  73. }
  74. };
  75. //此程序用于拉流播放性能测试
  76. int main(int argc, char *argv[]) {
  77. CMD_main cmd_main;
  78. try {
  79. cmd_main.operator()(argc, argv);
  80. } catch (ExitException &) {
  81. return 0;
  82. } catch (std::exception &ex) {
  83. cout << ex.what() << endl;
  84. return -1;
  85. }
  86. int threads = cmd_main["threads"];
  87. LogLevel logLevel = (LogLevel) cmd_main["level"].as<int>();
  88. logLevel = MIN(MAX(logLevel, LTrace), LError);
  89. auto in_url = cmd_main["in"];
  90. auto rtp_type = cmd_main["rtp"].as<int>();
  91. auto delay_ms = cmd_main["delay"].as<int>();
  92. auto player_count = cmd_main["count"].as<int>();
  93. //设置日志
  94. Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel", logLevel));
  95. //启动异步日志线程
  96. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  97. //设置线程数
  98. EventPollerPool::setPoolSize(threads);
  99. WorkThreadPool::setPoolSize(threads);
  100. //播放器map
  101. recursive_mutex mtx;
  102. unordered_map<void *, MediaPlayer::Ptr> player_map;
  103. auto add_player = [&]() {
  104. auto player = std::make_shared<MediaPlayer>();
  105. auto tag = player.get();
  106. player->setOnCreateSocket([](const EventPoller::Ptr &poller) {
  107. //socket关闭互斥锁,提高性能
  108. return std::make_shared<Socket>(poller, false);
  109. });
  110. //设置播放失败监听
  111. player->setOnPlayResult([&mtx, &player_map, tag](const SockException &ex) {
  112. if (ex) {
  113. //播放失败,移除之
  114. lock_guard<recursive_mutex> lck(mtx);
  115. player_map.erase(tag);
  116. }
  117. });
  118. //设置播放中途断开监听
  119. player->setOnShutdown([&mtx, &player_map, tag](const SockException &ex) {
  120. //播放中途失败,移除之
  121. lock_guard<recursive_mutex> lck(mtx);
  122. player_map.erase(tag);
  123. });
  124. //设置为性能测试模式
  125. (*player)[Client::kBenchmarkMode] = true;
  126. //设置rtsp拉流方式(在rtsp拉流时有效)
  127. (*player)[Client::kRtpType] = rtp_type;
  128. //提高压测性能与正确性
  129. (*player)[Client::kWaitTrackReady] = false;
  130. //发起播放请求
  131. player->play(in_url);
  132. //保持对象不销毁
  133. lock_guard<recursive_mutex> lck(mtx);
  134. player_map.emplace(tag, std::move(player));
  135. //休眠后再启动下一个播放,防止短时间海量链接
  136. if (delay_ms > 0) {
  137. usleep(1000 * delay_ms);
  138. }
  139. };
  140. //添加这么多播放器
  141. for (auto i = 0; i < player_count; ++i) {
  142. add_player();
  143. }
  144. // 设置退出信号
  145. static bool exit_flag = false;
  146. signal(SIGINT, [](int) { exit_flag = true; });
  147. while (!exit_flag) {
  148. //休眠一秒打印
  149. sleep(1);
  150. size_t alive_player = 0;
  151. {
  152. lock_guard<recursive_mutex> lck(mtx);
  153. alive_player = player_map.size();
  154. }
  155. InfoL << "在线播放器个数:" << alive_player;
  156. size_t re_try = player_count - alive_player;
  157. while (!exit_flag && re_try--) {
  158. //有些播放器播放失败了,那么我们重试添加
  159. add_player();
  160. }
  161. }
  162. return 0;
  163. }