You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.4KB

  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 <map>
  11. #include <iostream>
  12. #include "Util/MD5.h"
  13. #include "Util/File.h"
  14. #include "Util/logger.h"
  15. #include "Util/SSLBox.h"
  16. #include "Util/util.h"
  17. #include "Network/TcpServer.h"
  18. #include "Common/config.h"
  19. #include "Rtsp/RtspSession.h"
  20. #include "Rtmp/RtmpSession.h"
  21. #include "Http/HttpSession.h"
  22. #include "Rtp/RtpSelector.h"
  23. using namespace std;
  24. using namespace toolkit;
  25. using namespace mediakit;
  26. #if defined(ENABLE_RTPPROXY)
  27. static bool loadFile(const char *path){
  28. FILE *fp = fopen(path, "rb");
  29. if (!fp) {
  30. WarnL << "open file failed:" << path;
  31. return false;
  32. }
  33. uint64_t timeStamp_last = 0;
  34. uint16_t len;
  35. char rtp[0xFFFF];
  36. struct sockaddr_storage addr;
  37. memset(&addr, 0, sizeof(addr));
  38. addr.ss_family = AF_INET;
  39. auto sock = Socket::createSocket();
  40. size_t total_size = 0;
  41. RtpProcess::Ptr process;
  42. uint32_t ssrc = 0;
  43. while (true) {
  44. if (2 != fread(&len, 1, 2, fp)) {
  45. WarnL;
  46. break;
  47. }
  48. len = ntohs(len);
  49. if (len < 12 || len > sizeof(rtp)) {
  50. WarnL << len;
  51. break;
  52. }
  53. if (len != fread(rtp, 1, len, fp)) {
  54. WarnL;
  55. break;
  56. }
  57. total_size += len;
  58. uint64_t timeStamp = 0;
  59. if (!process) {
  60. if (!RtpSelector::getSSRC(rtp, len, ssrc)) {
  61. WarnL << "get ssrc from rtp failed:" << len;
  62. return false;
  63. }
  64. process = RtpSelector::Instance().getProcess(printSSRC(ssrc), true);
  65. }
  66. if (process) {
  67. try {
  68. process->inputRtp(true, sock, rtp, len, (struct sockaddr *)&addr, &timeStamp);
  69. } catch (...) {
  70. RtpSelector::Instance().delProcess(printSSRC(ssrc), process.get());
  71. throw;
  72. }
  73. }
  74. auto diff = timeStamp - timeStamp_last;
  75. if (diff > 0 && diff < 500) {
  76. usleep(diff * 1000);
  77. } else {
  78. usleep(1 * 1000);
  79. }
  80. timeStamp_last = timeStamp;
  81. }
  82. WarnL << total_size / 1024 << "KB";
  83. fclose(fp);
  84. return true;
  85. }
  86. #endif//#if defined(ENABLE_RTPPROXY)
  87. int main(int argc,char *argv[]) {
  88. //设置日志
  89. Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel"));
  90. #if defined(ENABLE_RTPPROXY)
  91. //启动异步日志线程
  92. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  93. loadIniConfig((exeDir() + "config.ini").data());
  94. TcpServer::Ptr rtspSrv(new TcpServer());
  95. TcpServer::Ptr rtmpSrv(new TcpServer());
  96. TcpServer::Ptr httpSrv(new TcpServer());
  97. rtspSrv->start<RtspSession>(554);//默认554
  98. rtmpSrv->start<RtmpSession>(1935);//默认1935
  99. httpSrv->start<HttpSession>(80);//默认80
  100. //此处选择是否导出调试文件
  101. // mINI::Instance()[RtpProxy::kDumpDir] = "/Users/xzl/Desktop/";
  102. if (argc == 2)
  103. loadFile(argv[1]);
  104. else
  105. ErrorL << "parameter error.";
  106. #else
  107. ErrorL << "please ENABLE_RTPPROXY and then test";
  108. #endif//#if defined(ENABLE_RTPPROXY)
  109. return 0;
  110. }