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.
 
 
 
 
 
 

142 lines
4.6 KiB

#include "protocolProxyBurstHTTP.h"
#include <stdio.h>
#define LOG( X ) m_pLog->Log( "protocolProxyStreamHTTP", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "protocolProxyStreamHTTP", this, _LOG_ERROR, X )
// Constructor
protocolProxyStreamHTTP::protocolProxyStreamHTTP(SOCKET SocketFD, config_setting_t *Settings) :networkClientHandler(SocketFD, Settings){
m_bStreamingActive = false;
m_pRequestedURL = (CSTR) malloc(1024);
m_pRequestedHost = (CSTR) malloc(1024);
m_pLog = basicLogCollection::getInstance();
if( create() ) m_pPort->addRecipient( this );
};
protocolProxyStreamHTTP::~protocolProxyStreamHTTP(){
};
bool protocolProxyStreamHTTP::packetHandler(protocolPackage *Package){
if( !m_bStreamingActive ){
CSTR t1, t2;
int p1, p2, p3;
t1 = (CSTR) Package->m_pData;
p1 = (int) strstr( t1, "GET /" ) + 5;
p2 = (int) strstr( t1, "\r\n" );
t2 = strstr( t1, "GET /" ) + 5;
p3 = p2 - p1 - 9;
strncpy( m_pRequestedURL, t2, p3);
m_pRequestedURL[p3] = '\0';
LOG( m_pRequestedURL );
char m_pCommand[1024];
sprintf( m_pCommand, "ffmpeg -rtsp_transport tcp -i \"rtsp://%s\" -c copy -f mpegts -", m_pRequestedURL );
HANDLE g_hChildStd_IN_Rd = NULL;
HANDLE g_hChildStd_IN_Wr = NULL;
HANDLE g_hChildStd_OUT_Rd = NULL;
HANDLE g_hChildStd_OUT_Wr = NULL;
HANDLE g_hInputFile = NULL;
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) ) return false;
if ( ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) ) return false;
if ( ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0) ) return false;
if ( ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) ) return false;
wchar_t szCmdline[1024];//=TEXT("dir");
LOG(m_pCommand );
wsprintfW(szCmdline, L"%S", m_pCommand);
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
BOOL bSuccess = FALSE;
// Set up members of the PROCESS_INFORMATION structure.
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
// Set up members of the STARTUPINFO structure.
// This structure specifies the STDIN and STDOUT handles for redirection.
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = g_hChildStd_OUT_Wr;
siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
siStartInfo.hStdInput = g_hChildStd_IN_Rd;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
// Create the child process.
bSuccess = CreateProcess(NULL,
szCmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
// If an error occurs, exit the application.
if ( ! bSuccess ){
LOG_ERROR("CreateProcess");
return false;
}else {
// Close handles to the child process and its primary thread.
// Some applications might keep these handles to monitor the status
// of the child process, for example.
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
};
#define BUFSIZE 2048
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
bSuccess = FALSE;
for (;;){
bSuccess = ReadFile( g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
if( ! bSuccess || dwRead == 0 ) break;
//bSuccess = WriteFile(hParentStdOut, chBuf, dwRead, &dwWritten, NULL);
m_pPort->sendData( (U8 *)chBuf, dwRead );
if (! bSuccess ) break;
Sleep(1);
}
m_bStreamingActive = true;
};
return true;
};
int protocolProxyStreamHTTP::packetChecker(unsigned char *Buffer, size_t BufferSize){
char *tBuff;
Buffer[BufferSize] = 0;
tBuff = (char *)Buffer;
//LOG( tBuff );
return 0;
};
protocolPackage *protocolProxyStreamHTTP::packetExtractor(unsigned char *Buffer, size_t BufferSize){
protocolPackage *tVal;
tVal = (protocolPackage *) malloc(sizeof(protocolPackage));
if( tVal!= NULL ){
tVal->m_nCode = 0;
tVal->m_nSize = 0;
tVal->m_pData = (U8 *) malloc( sizeof(U8) * BufferSize+1);
if(tVal->m_pData!= NULL){
tVal->m_nCode = 200;
tVal->m_nSize = BufferSize+1;
memcpy(tVal->m_pData, Buffer, BufferSize );
tVal->m_pData[BufferSize] = 0;
};
};
return tVal;
};
#undef LOG
#undef LOG_ERROR