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.

175 lines
4.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolProxyBurstHTTP.h"
  2. #include <stdio.h>
  3. #define LOG(X) m_pLog->Log("protocolProxyStreamHTTP", this, X)
  4. #define LOG_ERROR(X) m_pLog->Log("protocolProxyStreamHTTP", this, _LOG_ERROR, X)
  5. // Constructor
  6. protocolProxyStreamHTTP::protocolProxyStreamHTTP(SOCKET SocketFD, config_setting_t * Settings) :
  7. networkClientHandler(SocketFD, Settings)
  8. {
  9. m_bStreamingActive = false;
  10. m_pRequestedURL = (CSTR) malloc(1024);
  11. m_pRequestedHost = (CSTR) malloc(1024);
  12. m_pLog = basicLogCollection::getInstance();
  13. if (create())
  14. {
  15. m_pPort->addRecipient(this);
  16. }
  17. }
  18. protocolProxyStreamHTTP::~protocolProxyStreamHTTP() {}
  19. bool protocolProxyStreamHTTP::packetHandler(protocolPackage * Package)
  20. {
  21. if (! m_bStreamingActive)
  22. {
  23. CSTR t1, t2;
  24. int p1, p2, p3;
  25. t1 = (CSTR) Package->m_pData;
  26. p1 = (int) strstr(t1, "GET /") + 5;
  27. p2 = (int) strstr(t1, "\r\n");
  28. t2 = strstr(t1, "GET /") + 5;
  29. p3 = p2 - p1 - 9;
  30. strncpy(m_pRequestedURL, t2, p3);
  31. m_pRequestedURL[p3] = '\0';
  32. LOG(m_pRequestedURL);
  33. char m_pCommand[1024];
  34. sprintf(m_pCommand, "ffmpeg -rtsp_transport tcp -i \"rtsp://%s\" -c copy -f mpegts -", m_pRequestedURL);
  35. HANDLE g_hChildStd_IN_Rd = NULL;
  36. HANDLE g_hChildStd_IN_Wr = NULL;
  37. HANDLE g_hChildStd_OUT_Rd = NULL;
  38. HANDLE g_hChildStd_OUT_Wr = NULL;
  39. HANDLE g_hInputFile = NULL;
  40. SECURITY_ATTRIBUTES saAttr;
  41. saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
  42. saAttr.bInheritHandle = TRUE;
  43. saAttr.lpSecurityDescriptor = NULL;
  44. if (! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
  45. {
  46. return false;
  47. }
  48. if (! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
  49. {
  50. return false;
  51. }
  52. if (! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
  53. {
  54. return false;
  55. }
  56. if (! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
  57. {
  58. return false;
  59. }
  60. wchar_t szCmdline[1024]; //=TEXT("dir");
  61. LOG(m_pCommand);
  62. wsprintfW(szCmdline, L"%S", m_pCommand);
  63. PROCESS_INFORMATION piProcInfo;
  64. STARTUPINFO siStartInfo;
  65. BOOL bSuccess = FALSE;
  66. // Set up members of the PROCESS_INFORMATION structure.
  67. ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
  68. // Set up members of the STARTUPINFO structure.
  69. // This structure specifies the STDIN and STDOUT handles for redirection.
  70. ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
  71. siStartInfo.cb = sizeof(STARTUPINFO);
  72. siStartInfo.hStdError = g_hChildStd_OUT_Wr;
  73. siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
  74. siStartInfo.hStdInput = g_hChildStd_IN_Rd;
  75. siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  76. // Create the child process.
  77. bSuccess = CreateProcess(NULL,
  78. szCmdline, // command line
  79. NULL, // process security attributes
  80. NULL, // primary thread security attributes
  81. TRUE, // handles are inherited
  82. 0, // creation flags
  83. NULL, // use parent's environment
  84. NULL, // use parent's current directory
  85. &siStartInfo, // STARTUPINFO pointer
  86. &piProcInfo); // receives PROCESS_INFORMATION
  87. // If an error occurs, exit the application.
  88. if (! bSuccess)
  89. {
  90. LOG_ERROR("CreateProcess");
  91. return false;
  92. }
  93. else
  94. {
  95. // Close handles to the child process and its primary thread.
  96. // Some applications might keep these handles to monitor the status
  97. // of the child process, for example.
  98. CloseHandle(piProcInfo.hProcess);
  99. CloseHandle(piProcInfo.hThread);
  100. };
  101. #define BUFSIZE 2048
  102. DWORD dwRead, dwWritten;
  103. CHAR chBuf[BUFSIZE];
  104. bSuccess = FALSE;
  105. for (;;)
  106. {
  107. bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
  108. if (! bSuccess || dwRead == 0)
  109. {
  110. break;
  111. }
  112. // bSuccess = WriteFile(hParentStdOut, chBuf, dwRead, &dwWritten, NULL);
  113. m_pPort->sendData((U8 *) chBuf, dwRead);
  114. if (! bSuccess)
  115. {
  116. break;
  117. }
  118. Sleep(1);
  119. }
  120. m_bStreamingActive = true;
  121. };
  122. return true;
  123. }
  124. int protocolProxyStreamHTTP::packetChecker(unsigned char * Buffer, size_t BufferSize)
  125. {
  126. char * tBuff;
  127. Buffer[BufferSize] = 0;
  128. tBuff = (char *) Buffer;
  129. // LOG( tBuff );
  130. return 0;
  131. }
  132. protocolPackage * protocolProxyStreamHTTP::packetExtractor(unsigned char * Buffer, size_t BufferSize)
  133. {
  134. protocolPackage * tVal;
  135. tVal = (protocolPackage *) malloc(sizeof(protocolPackage));
  136. if (tVal != NULL)
  137. {
  138. tVal->m_nCode = 0;
  139. tVal->m_nSize = 0;
  140. tVal->m_pData = (U8 *) malloc(sizeof(U8) * BufferSize + 1);
  141. if (tVal->m_pData != NULL)
  142. {
  143. tVal->m_nCode = 200;
  144. tVal->m_nSize = BufferSize + 1;
  145. memcpy(tVal->m_pData, Buffer, BufferSize);
  146. tVal->m_pData[BufferSize] = 0;
  147. };
  148. };
  149. return tVal;
  150. }
  151. #undef LOG
  152. #undef LOG_ERROR