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.

141 lines
4.6 KiB

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