MediaSourceMain.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "MediaSourceMain.h"
  2. #include <conio.h>
  3. #define DELETE_OBJECT(obj) if (obj) { delete obj; obj = NULL; }
  4. #define mygetch getch
  5. AppClass::AppClass(std::string src, std::string dst, std::string transport)
  6. {
  7. m_sSrc = src;
  8. m_sDst = dst;
  9. m_sTransport = transport;
  10. }
  11. void AppClass::Action()
  12. {
  13. CMediaSourceRtsp pusher;
  14. pusher.m_Input_rtsp = m_sSrc;
  15. pusher.m_Output_rtsp = m_sDst;
  16. if ((m_sTransport == "TCP") || (m_sTransport == "tcp"))
  17. pusher.m_TcpOrudp = "tcp";
  18. if ((m_sTransport == "UDP") || (m_sTransport == "udp"))
  19. pusher.m_TcpOrudp = "udp";
  20. if (!pusher.OpenInput(pusher.m_Input_rtsp, pusher.m_TcpOrudp)) {
  21. printf("open input_rtsp failed\n");
  22. exit(-1);
  23. }
  24. if (!pusher.OpenOutput(pusher.m_Output_rtsp)) {
  25. printf("open output_rtsp failed\n");
  26. exit(-1);
  27. }
  28. int ret = -1;
  29. while (!IsAborted()) {
  30. std::shared_ptr<AVPacket> packet = nullptr;
  31. packet = pusher.ReadPacket();
  32. if (!packet) {
  33. OutputDebugStringA("GetAvPacket ERROR\n");
  34. printf("GetAvPacket ERROR\n");
  35. pusher.ResetConnect();
  36. continue;
  37. }
  38. if ((packet) && (packet->stream_index == pusher.m_nVideoStream)) {
  39. ret = pusher.WritePacket(packet);
  40. if (ret < 0) {
  41. pusher.CheckOutputNet();
  42. }
  43. }
  44. }
  45. }
  46. std::string LoadConfigFile()
  47. {
  48. FILE* pf = fopen("rtsp_config.config", "rb+");
  49. if (!pf) {
  50. printf("open rtsp_config.config failed\n");
  51. return "";
  52. }
  53. fseek(pf, 0, SEEK_END);
  54. long lSize = ftell(pf);
  55. char* text = new char[lSize];
  56. rewind(pf);
  57. fread(text, sizeof(char), lSize, pf);
  58. std::string filedata = std::string(text, lSize);
  59. delete[] text;
  60. fclose(pf);
  61. return filedata;
  62. }
  63. int main(int argc, char** argv)
  64. {
  65. std::string filedata = LoadConfigFile();
  66. if (filedata == "") {
  67. printf("load rtsp_config.config failed\n");
  68. return -1;
  69. }
  70. nlohmann::json j = nlohmann::json::parse(filedata);
  71. std::vector<AppClass*> pApp;
  72. for (int i = 0; i < j["config"].size(); i++) {
  73. std::string src = j["config"][i]["src_rtsp"];
  74. std::string dst = j["config"][i]["dst_rtsp"];
  75. std::string transport = j["config"][i]["rtsp_transport"];
  76. AppClass* p = new AppClass(src,dst,transport);
  77. p->Start();
  78. pApp.emplace_back(p);
  79. }
  80. char c;
  81. while (1) {
  82. c = mygetch();
  83. if (c != 'q') {
  84. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  85. }
  86. else {
  87. std::vector<AppClass*>::iterator iter = pApp.begin();
  88. for (; iter != pApp.end();) {
  89. DELETE_OBJECT((*iter));
  90. iter = pApp.erase(iter);
  91. }
  92. break;
  93. }
  94. }
  95. return 0;
  96. }