Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 8 měsíci
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "mk_common.h"
  11. #include <stdarg.h>
  12. #include <unordered_map>
  13. #include "Util/logger.h"
  14. #include "Util/SSLBox.h"
  15. #include "Util/File.h"
  16. #include "Network/TcpServer.h"
  17. #include "Network/UdpServer.h"
  18. #include "Thread/WorkThreadPool.h"
  19. #include "Rtsp/RtspSession.h"
  20. #include "Rtmp/RtmpSession.h"
  21. #include "Http/HttpSession.h"
  22. #include "Shell/ShellSession.h"
  23. using namespace std;
  24. using namespace toolkit;
  25. using namespace mediakit;
  26. static TcpServer::Ptr rtsp_server[2];
  27. static TcpServer::Ptr rtmp_server[2];
  28. static TcpServer::Ptr http_server[2];
  29. static TcpServer::Ptr shell_server;
  30. #ifdef ENABLE_RTPPROXY
  31. #include "Rtp/RtpServer.h"
  32. static std::shared_ptr<RtpServer> rtpServer;
  33. #endif
  34. #ifdef ENABLE_WEBRTC
  35. #include "../webrtc/WebRtcSession.h"
  36. #include "../webrtc/WebRtcTransport.h"
  37. static UdpServer::Ptr rtcServer_udp;
  38. static TcpServer::Ptr rtcServer_tcp;
  39. #endif
  40. #if defined(ENABLE_SRT)
  41. #include "../srt/SrtSession.hpp"
  42. static UdpServer::Ptr srtServer;
  43. #endif
  44. //////////////////////////environment init///////////////////////////
  45. API_EXPORT void API_CALL mk_env_init(const mk_config *cfg) {
  46. assert(cfg);
  47. mk_env_init1(cfg->thread_num,
  48. cfg->log_level,
  49. cfg->log_mask,
  50. cfg->log_file_path,
  51. cfg->log_file_days,
  52. cfg->ini_is_path,
  53. cfg->ini,
  54. cfg->ssl_is_path,
  55. cfg->ssl,
  56. cfg->ssl_pwd);
  57. }
  58. extern void stopAllTcpServer();
  59. API_EXPORT void API_CALL mk_stop_all_server(){
  60. CLEAR_ARR(rtsp_server);
  61. CLEAR_ARR(rtmp_server);
  62. CLEAR_ARR(http_server);
  63. shell_server = nullptr;
  64. #ifdef ENABLE_RTPPROXY
  65. rtpServer = nullptr;
  66. #endif
  67. #ifdef ENABLE_WEBRTC
  68. rtcServer_udp = nullptr;
  69. rtcServer_tcp = nullptr;
  70. #endif
  71. #ifdef ENABLE_SRT
  72. srtServer = nullptr;
  73. #endif
  74. stopAllTcpServer();
  75. }
  76. API_EXPORT void API_CALL mk_env_init1(int thread_num,
  77. int log_level,
  78. int log_mask,
  79. const char *log_file_path,
  80. int log_file_days,
  81. int ini_is_path,
  82. const char *ini,
  83. int ssl_is_path,
  84. const char *ssl,
  85. const char *ssl_pwd) {
  86. //确保只初始化一次
  87. static onceToken token([&]() {
  88. if (log_mask & LOG_CONSOLE) {
  89. //控制台日志
  90. Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel", (LogLevel) log_level));
  91. }
  92. if (log_mask & LOG_CALLBACK) {
  93. //广播日志
  94. Logger::Instance().add(std::make_shared<EventChannel>("EventChannel", (LogLevel) log_level));
  95. }
  96. if (log_mask & LOG_FILE) {
  97. //日志文件
  98. auto channel = std::make_shared<FileChannel>("FileChannel",
  99. log_file_path ? File::absolutePath("", log_file_path) :
  100. exeDir() + "log/", (LogLevel) log_level);
  101. channel->setMaxDay(log_file_days ? log_file_days : 1);
  102. Logger::Instance().add(channel);
  103. }
  104. //异步日志线程
  105. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  106. //设置线程数
  107. EventPollerPool::setPoolSize(thread_num);
  108. WorkThreadPool::setPoolSize(thread_num);
  109. if (ini && ini[0]) {
  110. //设置配置文件
  111. if (ini_is_path) {
  112. try {
  113. mINI::Instance().parseFile(ini);
  114. } catch (std::exception &) {
  115. InfoL << "dump ini file to:" << ini;
  116. mINI::Instance().dumpFile(ini);
  117. }
  118. } else {
  119. mINI::Instance().parse(ini);
  120. }
  121. }
  122. if (ssl && ssl[0]) {
  123. //设置ssl证书
  124. SSL_Initor::Instance().loadCertificate(ssl, true, ssl_pwd ? ssl_pwd : "", ssl_is_path);
  125. }
  126. });
  127. }
  128. API_EXPORT void API_CALL mk_set_log(int file_max_size, int file_max_count) {
  129. auto channel = dynamic_pointer_cast<FileChannel>(Logger::Instance().get("FileChannel"));
  130. if (channel) {
  131. channel->setFileMaxSize(file_max_size);
  132. channel->setFileMaxCount(file_max_count);
  133. }
  134. }
  135. API_EXPORT void API_CALL mk_set_option(const char *key, const char *val) {
  136. assert(key && val);
  137. if (mINI::Instance().find(key) == mINI::Instance().end()) {
  138. WarnL << "key:" << key << " not existed!";
  139. return;
  140. }
  141. mINI::Instance()[key] = val;
  142. }
  143. API_EXPORT const char * API_CALL mk_get_option(const char *key)
  144. {
  145. assert(key);
  146. if (mINI::Instance().find(key) == mINI::Instance().end()) {
  147. WarnL << "key:" << key << " not existed!";
  148. return nullptr;
  149. }
  150. return mINI::Instance()[key].data();
  151. }
  152. API_EXPORT uint16_t API_CALL mk_http_server_start(uint16_t port, int ssl) {
  153. ssl = MAX(0,MIN(ssl,1));
  154. try {
  155. http_server[ssl] = std::make_shared<TcpServer>();
  156. if(ssl){
  157. http_server[ssl]->start<SessionWithSSL<HttpSession> >(port);
  158. } else{
  159. http_server[ssl]->start<HttpSession>(port);
  160. }
  161. return http_server[ssl]->getPort();
  162. } catch (std::exception &ex) {
  163. http_server[ssl] = nullptr;
  164. WarnL << ex.what();
  165. return 0;
  166. }
  167. }
  168. API_EXPORT uint16_t API_CALL mk_rtsp_server_start(uint16_t port, int ssl) {
  169. ssl = MAX(0,MIN(ssl,1));
  170. try {
  171. rtsp_server[ssl] = std::make_shared<TcpServer>();
  172. if(ssl){
  173. rtsp_server[ssl]->start<SessionWithSSL<RtspSession> >(port);
  174. }else{
  175. rtsp_server[ssl]->start<RtspSession>(port);
  176. }
  177. return rtsp_server[ssl]->getPort();
  178. } catch (std::exception &ex) {
  179. rtsp_server[ssl] = nullptr;
  180. WarnL << ex.what();
  181. return 0;
  182. }
  183. }
  184. API_EXPORT uint16_t API_CALL mk_rtmp_server_start(uint16_t port, int ssl) {
  185. ssl = MAX(0,MIN(ssl,1));
  186. try {
  187. rtmp_server[ssl] = std::make_shared<TcpServer>();
  188. if(ssl){
  189. rtmp_server[ssl]->start<SessionWithSSL<RtmpSession> >(port);
  190. }else{
  191. rtmp_server[ssl]->start<RtmpSession>(port);
  192. }
  193. return rtmp_server[ssl]->getPort();
  194. } catch (std::exception &ex) {
  195. rtmp_server[ssl] = nullptr;
  196. WarnL << ex.what();
  197. return 0;
  198. }
  199. }
  200. API_EXPORT uint16_t API_CALL mk_rtp_server_start(uint16_t port){
  201. #ifdef ENABLE_RTPPROXY
  202. try {
  203. //创建rtp 服务器
  204. rtpServer = std::make_shared<RtpServer>();
  205. rtpServer->start(port);
  206. return rtpServer->getPort();
  207. } catch (std::exception &ex) {
  208. rtpServer = nullptr;
  209. WarnL << ex.what();
  210. return 0;
  211. }
  212. #else
  213. WarnL << "未启用该功能!";
  214. return 0;
  215. #endif
  216. }
  217. API_EXPORT uint16_t API_CALL mk_rtc_server_start(uint16_t port) {
  218. #ifdef ENABLE_WEBRTC
  219. try {
  220. //创建rtc udp服务器
  221. rtcServer_udp = std::make_shared<UdpServer>();
  222. rtcServer_udp->setOnCreateSocket([](const EventPoller::Ptr &poller, const Buffer::Ptr &buf, struct sockaddr *, int) {
  223. if (!buf) {
  224. return Socket::createSocket(poller, false);
  225. }
  226. auto new_poller = WebRtcSession::queryPoller(buf);
  227. if (!new_poller) {
  228. //该数据对应的webrtc对象未找到,丢弃之
  229. return Socket::Ptr();
  230. }
  231. return Socket::createSocket(new_poller, false);
  232. });
  233. rtcServer_udp->start<WebRtcSession>(port);
  234. //创建rtc tcp服务器
  235. rtcServer_tcp = std::make_shared<TcpServer>();
  236. rtcServer_tcp->start<WebRtcSession>(rtcServer_udp->getPort());
  237. return rtcServer_udp->getPort();
  238. } catch (std::exception &ex) {
  239. rtcServer_udp = nullptr;
  240. rtcServer_tcp = nullptr;
  241. WarnL << ex.what();
  242. return 0;
  243. }
  244. #else
  245. WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
  246. return 0;
  247. #endif
  248. }
  249. #ifdef ENABLE_WEBRTC
  250. class WebRtcArgsUrl : public mediakit::WebRtcArgs {
  251. public:
  252. WebRtcArgsUrl(std::string url) { _url = std::move(url); }
  253. ~WebRtcArgsUrl() = default;
  254. toolkit::variant operator[](const std::string &key) const override {
  255. if (key == "url") {
  256. return _url;
  257. }
  258. return "";
  259. }
  260. private:
  261. std::string _url;
  262. };
  263. #endif
  264. API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type,
  265. const char *offer, const char *url) {
  266. #ifdef ENABLE_WEBRTC
  267. assert(type && offer && url && cb);
  268. auto session = std::make_shared<HttpSession>(Socket::createSocket());
  269. std::string offer_str = offer;
  270. WebRtcPluginManager::Instance().getAnswerSdp(*session, type, WebRtcArgsUrl(url),
  271. [offer_str, session, user_data, cb](const WebRtcInterface &exchanger) mutable {
  272. try {
  273. auto sdp_answer = const_cast<WebRtcInterface &>(exchanger).getAnswerSdp(offer_str);
  274. cb(user_data, sdp_answer.data(), nullptr);
  275. } catch (std::exception &ex) {
  276. cb(user_data, nullptr, ex.what());
  277. }
  278. });
  279. #else
  280. WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
  281. #endif
  282. }
  283. API_EXPORT uint16_t API_CALL mk_srt_server_start(uint16_t port) {
  284. #ifdef ENABLE_SRT
  285. try {
  286. srtServer = std::make_shared<UdpServer>();
  287. srtServer->setOnCreateSocket([](const EventPoller::Ptr &poller, const Buffer::Ptr &buf, struct sockaddr *, int) {
  288. if (!buf) {
  289. return Socket::createSocket(poller, false);
  290. }
  291. auto new_poller = SRT::SrtSession::queryPoller(buf);
  292. if (!new_poller) {
  293. //握手第一阶段
  294. return Socket::createSocket(poller, false);
  295. }
  296. return Socket::createSocket(new_poller, false);
  297. });
  298. srtServer->start<SRT::SrtSession>(port);
  299. return srtServer->getPort();
  300. } catch (std::exception &ex) {
  301. srtServer = nullptr;
  302. WarnL << ex.what();
  303. return 0;
  304. }
  305. #else
  306. WarnL << "未启用该功能!";
  307. return 0;
  308. #endif
  309. }
  310. API_EXPORT uint16_t API_CALL mk_shell_server_start(uint16_t port){
  311. try {
  312. shell_server = std::make_shared<TcpServer>();
  313. shell_server->start<ShellSession>(port);
  314. return shell_server->getPort();
  315. } catch (std::exception &ex) {
  316. shell_server = nullptr;
  317. WarnL << ex.what();
  318. return 0;
  319. }
  320. }