diff --git a/Projects/la_mojo/build b/Projects/la_mojo/build index 922633e2..e30c9046 100755 --- a/Projects/la_mojo/build +++ b/Projects/la_mojo/build @@ -4,8 +4,8 @@ #make clean -e "CFG=Debug" #make -e "CFG=Debug" -j6 -make clean -e "CFG=Debug86" -make -e "CFG=Debug86" -j2 +#make clean -e "CFG=Debug86" +make -e "CFG=Debug86" -j4 #make clean -e "CFG=Release86" #make -e "CFG=Release86" -j4 diff --git a/lib/App/common/kernelModuleEmbedVideo.cpp b/lib/App/common/kernelModuleEmbedVideo.cpp index fab84b30..1df666a4 100644 --- a/lib/App/common/kernelModuleEmbedVideo.cpp +++ b/lib/App/common/kernelModuleEmbedVideo.cpp @@ -28,6 +28,9 @@ #include #include +//#define DEBUG +//#define DEBUG_EXTRA + const char *m_pVideoScratchPath = VIDEO_SCRATCHPAD_PATH; #define STORAGE_DIR_MODE 0x1FF @@ -66,7 +69,7 @@ enum { static const char* kRequestPaths[] = { "/stream", "/sign_in", "/wait", "/sign_out", "/message" }; //buffer_data bd[1]={0}; -kernelModuleEmbedVideo::kernelModuleEmbedVideo( uint32_t moduleID ): kernelModule( moduleID, KERNEL_MODULE_EMBED_VIDEO ), basicThread(){ +kernelModuleEmbedVideo::kernelModuleEmbedVideo( uint32_t moduleID ): kernelModule( moduleID, KERNEL_MODULE_EMBED_VIDEO ), basicDualThread(){ m_pEventHeap = basicEventHeap::getInstance(); if( m_pDBC ){ DBC_MODULE_EMBED_VIDEO_DATA *tDATA = m_pDBC->getEmbedVideoData( m_nDBAUXID ); @@ -132,7 +135,7 @@ kernelModuleEmbedVideo::~kernelModuleEmbedVideo(){ LOG("Destructor"); #endif m_bEnabled = false; - waitThreadEnd( true ); + waitThreadAEnd( true ); if( m_vHandlersPool.size() ) { // Kill-Wait Started Probes #ifdef DEBUG_EXTRA char tB[200]; @@ -146,6 +149,22 @@ kernelModuleEmbedVideo::~kernelModuleEmbedVideo(){ #ifdef DEBUG_EXTRA LOG("No More Clients Left"); #endif + + waitThreadBEnd( true ); + if( m_vViewHandlersPool.size() ) { // Kill-Wait Started Viewers +#ifdef DEBUG_EXTRA + char tB[200]; + sprintf(tB, "Have to Wait View Handlers[%d]!", m_vViewHandlersPool.size() ); + LOG( tB ); + +#endif + for( vectorHANDLERS::iterator index = m_vViewHandlersPool.begin(); index < m_vViewHandlersPool.end(); index++ ) pthread_cancel( (*index)->m_hThreadHandle ); + m_vViewHandlersPool.clear(); + }; +#ifdef DEBUG_EXTRA + LOG("No More Viewers Left"); +#endif + LOG("Locking in Destructor!"); m_mStreamHandlersProtector.lock(); if( m_mapHANDLERS.size() ){ @@ -431,7 +450,7 @@ void *kernelModuleEmbedVideo::webClientProc( void *ptr ){ w = false; continue; }; - SLEEP( 80 ); + SLEEP( 66 ); }; }; }; @@ -446,7 +465,256 @@ void *kernelModuleEmbedVideo::webClientProc( void *ptr ){ return NULL; }; -void kernelModuleEmbedVideo::m_fThreadProc( void ){ + +void *kernelModuleEmbedVideo::viewClientProc( void *ptr ){ + + + WEB_CLIENT_CTX * CTX = (WEB_CLIENT_CTX *) ptr; + kernelModuleEmbedVideo * MODULE = CTX->ModulePtr; + uint32_t tVideoID = 0; + long int tDTS = 0; + uint32_t tETS = 0; + uint32_t msgSize; + uint8_t tBuf[FILESTREAMBUFFERSZ]; + uint8_t rBuf[8192]; + int rSIZE; + int cSIZE; + int retVal; + bool w = true; + char tBASE[11]={0}; + char tID[33]={0}; + char tFILE[PATH_MAX]; + + uint32_t cFRAME = 0; + void * sPtr = NULL; + void * ePtr = NULL; + uint8_t baba[2]={0xff,0xd8}; + uint8_t yaba[2]={0xff,0xd9}; + FILE * tFID = NULL; + if( CTX ) { + char mesg[1024]; + size_t RecvSize; + struct pollfd pfd[1]; + char cGetURI[1024]; + bool m_bDeflatePerMessage = false; + bool m_pInit; + +#ifdef DEBUG_EXTRA + pid_t pid = gettid(); + char pidBuf[16]; + sprintf( pidBuf, "pid:%d", pid); + TLOG( pidBuf ); +#endif + signal( SIGPIPE, signal_pipe_handler ); + + pfd->fd = CTX->m_iSocketFID; + pfd->events = POLLIN | POLLHUP; + + while( CTX->bValid && w ) { + if( !CTX->initDone ){ + int RetVal; + char * reqline[256]; + RetVal = poll( pfd, 1, 1000 ); + if( RetVal <= 0 ){ + if ( EINTR == errno ) continue; + TLOG_ERROR("Unrecoverable error"); + w = false; + continue; + }; + memset( mesg, 0, sizeof(mesg) ); + + RetVal = recv( CTX->m_iSocketFID, mesg, sizeof(mesg), 0 ); + + if ( RetVal <= 0 ) { + sprintf( mesg, "web recv() error: %s", strerror(errno)); + TLOG_ERROR( mesg ); + w = false; + continue; + }; +#ifdef DEBUG_EXTRA + TLOG( mesg ); +#endif + + uint32_t index = 0; + reqline[index] = strtok( mesg, "\n" ); + if( strncmp( reqline[index], "GET", 3 ) == 0 ) { + char * pPartURI; + char * pPartPROTO; + pPartURI = strstr( reqline[index], " " ); + if( pPartURI != NULL ) { + pPartURI++; + pPartPROTO = strstr( pPartURI, " " ); + pPartPROTO++; + int URILen = pPartPROTO - pPartURI; + strncpy( cGetURI, pPartURI, URILen ); + cGetURI[URILen] = (char)0; + if(strncmp( pPartPROTO, "HTTP/1.0", 8) != 0 && strncmp( pPartPROTO, "HTTP/1.1", 8) != 0) { + TLOG_ERROR("No HTTP version supplied in request!"); + if( write( CTX->m_iSocketFID, "HTTP/1.1 400 Bad Request\r\n", 26) < 0 ){ + sprintf( mesg, "Failed returning status 400 to client: %s", strerror(errno)); + TLOG_ERROR(mesg); + }; + w = false; + continue; + }; + }; +#ifdef DEBUG + TLOG( "Req URI" ); + TLOG( cGetURI ); +#endif + }; + + if( ( strncmp( cGetURI, "/stream", 7 ) == 0 ) || ( strncmp( cGetURI, "/video_view", 11 ) == 0 ) ) { + CSTR tP; + if( tP = strstr( cGetURI, "?" ) ) { + CSTR tB; + tP++; + if( tB = strcasestr( tP, "id=" ) ) { + strncpy( tID, tB+3, 32 ); + tID[32] = (char)0; + }; + if (tB = strcasestr( tP, "ts=" ) ) { + tDTS = atol( tB+3 ); + struct tm * ptm = NULL; + ptm = gmtime ( &tDTS ); + if ( ptm ) { + sprintf( tBASE, "%04d/%02d/%02d", (ptm->tm_year+1900), (ptm->tm_mon+1), ptm->tm_mday ); + }; + }; + if (tB = strcasestr( tP, "sf=" ) ) { + tETS = atoi( tB+3 ); + }; + sprintf( tFILE, "./data/reports/%s/%s.mjpeg", tBASE, tID ); + }; +#ifdef DEBUG + char tB[64]; + sprintf(tB,"Requested Video: %s", tFILE ); + TLOG( tB ); +#endif + if( fileExist( tFILE ) ){ + tFID = fopen( tFILE, "r" ); + if( tFID ){ + // Send Header + if( send( CTX->m_iSocketFID, ht_head, strlen(ht_head), 0 ) < 0 ){ +#ifdef DEBUG + sprintf( mesg, "Failed sending file to client: %s", strerror(errno)); + TLOG_ERROR(mesg); +#endif + w = false; + }; + CTX->initDone = true; + continue; +#ifdef DEBUG + } else { + TLOG("Stream id Not Online"); +#endif + }; +#ifdef DEBUG + } else { + TLOG("No Stream ID"); +#endif + }; + }; + if( 1 ) { + //Send Dummy Picture + sprintf( mesg, ht_no_signal, CTX->ModulePtr->m_nNoSignalSize ); + send( CTX->m_iSocketFID, mesg, strlen(mesg), 0 ); + send( CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalData, CTX->ModulePtr->m_nNoSignalSize, 0 ); + send( CTX->m_iSocketFID, "\r\n\r\n", 4, 0 ); +#ifdef DEBUG_EXTRA + TLOG("Sent No Signal Image!"); +#endif + } else { + // Send 404 Header + send( CTX->m_iSocketFID, ht_abort, strlen(ht_abort), 0 ); +#ifdef DEBUG_EXTRA + TLOG("Aborted!"); +#endif + }; + w = false; + continue; + } else { + cSIZE = 0; + while ( !feof( tFID ) && CTX->bValid && w ){ + rSIZE = fread( rBuf, 1, 8192, tFID ); + if( rSIZE > 0 ){ + memcpy( tBuf+cSIZE, rBuf, rSIZE ); + cSIZE += rSIZE; + if ( sPtr == NULL ) sPtr = memmem( tBuf, cSIZE, baba, 2 ); + if ( sPtr ) { + if ( sPtr != tBuf ) { + cSIZE -= ((uint64_t) sPtr - (uint64_t) tBuf); + memcpy( tBuf, sPtr, cSIZE ); + sPtr = tBuf; + }; + if (ePtr == NULL) ePtr = memmem( sPtr, cSIZE, yaba, 2 ); + }; + }; + if ( sPtr != NULL && ePtr != NULL ) { + if( feof( tFID ) ) TLOG("LAST FRAME"); + cFRAME++; + ePtr += 2; + msgSize = ((uint64_t)ePtr - (uint64_t)sPtr ); + if ( cFRAME > tETS || feof( tFID ) ) { // or last chanse to send data + if( feof( tFID ) ) TLOG("SEND HEADER"); + sprintf( mesg, "--MPFrame\r\nContent-type: image/jpeg\r\nContent-length: %d\r\n\r\n", msgSize ); + if( send( CTX->m_iSocketFID, mesg, strlen( mesg ), 0 )< 0 ){ + #ifdef DEBUG + sprintf( mesg, "[1]Failed sending file to client: %s", strerror(errno)); + TLOG_ERROR(mesg); + #endif + w = false; + continue; + }; + if( feof( tFID ) ) TLOG("SEND BODY"); + if( send( CTX->m_iSocketFID, tBuf, msgSize, 0 ) < 0 ){ + #ifdef DEBUG + sprintf( mesg, "[2]Failed sending file to client: %s", strerror(errno)); + TLOG_ERROR(mesg); + #endif + w = false; + continue; + }; + if( feof( tFID ) ) TLOG("SEND TAIL"); + if( send( CTX->m_iSocketFID, "\r\n\r\n", 4, 0 )< 0 ){ + #ifdef DEBUG + sprintf( mesg, "[3]Failed sending file to client: %s", strerror(errno)); + TLOG_ERROR(mesg); + #endif + w = false; + continue; + }; + SLEEP( 66 ); + }; + if( !feof( tFID ) ) { + cSIZE -= msgSize; + memcpy( tBuf, ePtr, cSIZE ); + sPtr = ePtr = NULL; + } else TLOG("File is Finished!"); + }; + if( feof( tFID ) ) TLOG("LAST FRAME SENT"); + }; + TLOG("Close File"); + fclose( tFID ); + w = false; + }; + }; + }; + TLOG("Shut Down Socket"); + shutdown( CTX->m_iSocketFID, SHUT_RDWR); + TLOG("Close Socket"); + close( CTX->m_iSocketFID ); + //pthread_exit( NULL ); + CTX->bValid = false; +#ifdef DEBUG + TLOG("View Client Handler Ended!"); +#endif + return NULL; +}; + + +// Start Thread To view Live Streams +void kernelModuleEmbedVideo::m_fThreadAProc( void ){ if( !m_bEnabled ) { #ifdef DEBUG_EXTRA LOG("Module is Disabled!"); @@ -454,22 +722,22 @@ void kernelModuleEmbedVideo::m_fThreadProc( void ){ return; }; #ifdef DEBUG_EXTRA - LOG("Module Started!"); + LOG("[LIVE] Module Started!"); #endif struct pollfd pfd[1]; pfd->fd = m_nSocketDescriptor; pfd->events = POLLIN | POLLHUP; - m_bThreadStatus = TH_WORKING; - while( m_bThreadStatus < TH_ENDED ) { + m_bThreadAStatus = TH_WORKING; + while( m_bThreadAStatus < TH_ENDED ) { if( !m_bEnabled ) { - m_bThreadStatus = TH_ENDED; + m_bThreadAStatus = TH_ENDED; continue; }; int RetVal = poll( pfd, 1, 1000 ); if( RetVal < 0 ){ if( EINTR == errno ) continue; LOG_ERROR("Unrecoverable error"); - m_bThreadStatus = TH_ENDED; + m_bThreadAStatus = TH_ENDED; continue; }; // Handle Connections @@ -539,6 +807,7 @@ void kernelModuleEmbedVideo::m_fThreadProc( void ){ }; }; // Handle Dead Clients + m_mWebHandlersProtector.lock(); if( m_vHandlersPool.size() ) { for( vectorHANDLERS::iterator idx = m_vHandlersPool.begin(); idx < m_vHandlersPool.end(); idx++ ) { if( !( (*idx)->CTX->bValid ) ) { @@ -550,16 +819,133 @@ void kernelModuleEmbedVideo::m_fThreadProc( void ){ close( (*idx)->CTX->m_iEncoderPipe[1] ); free( (*idx)->CTX ); free( (*idx) ); - m_mWebHandlersProtector.lock(); - m_vHandlersPool.erase(idx); - m_mWebHandlersProtector.unlock(); + m_vHandlersPool.erase(idx); #ifdef DEBUG_EXTRA LOG("Web Handler Ripped!"); +#endif + }; + }; + }; + m_mWebHandlersProtector.unlock(); + // validate active streams ? + + SLEEP( 50 ); + }; +}; + + +void kernelModuleEmbedVideo::m_fThreadBProc( void ){ + if( !m_bEnabled ) { +#ifdef DEBUG_EXTRA + LOG("Module is Disabled!"); +#endif + return; + }; +#ifdef DEBUG_EXTRA + LOG("[VIEW] Module Started!"); +#endif + struct pollfd pfd[1]; + pfd->fd = m_nViewSocketDescriptor; + pfd->events = POLLIN | POLLHUP; + m_bThreadBStatus = TH_WORKING; + while( m_bThreadBStatus < TH_ENDED ) { + if( !m_bEnabled ) { + m_bThreadBStatus = TH_ENDED; + continue; + }; + int RetVal = poll( pfd, 1, 1000 ); + if( RetVal < 0 ){ + if( EINTR == errno ) continue; + LOG_ERROR("Unrecoverable error"); + m_bThreadBStatus = TH_ENDED; + continue; + }; + // Handle Connections + if( RetVal != 0 ){ + if ( pfd[0].revents & POLLIN ) { +#ifdef DEBUG_EXTRA + LOG("Handle New View"); +#endif + int tSID = accept( pfd[0].fd, NULL, NULL); + if( tSID < 0 ){ + char tBuf[CTEXT]; + sprintf(tBuf, "Accept() error: %s", strerror(errno)); + LOG_ERROR(tBuf); + } else { +#ifdef DEBUG_EXTRA + LOG("Accept OK"); +#endif + WEB_CLIENT_CTX *tCTX = (WEB_CLIENT_CTX *) malloc(sizeof(WEB_CLIENT_CTX)); + if( tCTX ) { +#ifdef DEBUG_EXTRA + LOG("CTX OK"); +#endif + WEB_HANDLER *tHANDLER = (WEB_HANDLER *) malloc(sizeof(WEB_HANDLER)); + if( tHANDLER ) { +#ifdef DEBUG_EXTRA + LOG("HANDLER OK"); +#endif + memset( tCTX, 0, sizeof(WEB_CLIENT_CTX) ); + memset( tHANDLER, 0, sizeof(WEB_HANDLER) ); + tCTX->m_iSocketFID = tSID; + tCTX->m_iEncoderPipe[0] = -1; + tCTX->m_iEncoderPipe[1] = -1; + tCTX->mediaStreamId = 0; + tCTX->bValid = true; + tCTX->ModulePtr = this; +#ifdef DEBUG_EXTRA + LOG("Before Thread Create"); +#endif + int RetVal = pthread_create( &tHANDLER->m_hThreadHandle, NULL, viewClientProc, ( void * ) tCTX ); + if( RetVal == -1 ) { + free(tCTX); + free(tHANDLER); + LOG_ERROR("Thread Creation Faled."); + shutdown( tSID, SHUT_RDWR ); + close( tSID ); + } else { + tHANDLER->CTX = tCTX; + m_mViewHandlersProtector.lock(); + m_vViewHandlersPool.push_back( tHANDLER ); + m_mViewHandlersProtector.unlock(); +#ifdef DEBUG_EXTRA + LOG("Handler Pushed to a Pool"); +#endif + }; + } else { //https://stackoverflow.com/questions/18545077/image-fullscreen-on-click + free(tCTX); + LOG_ERROR("CL_HANDLER Mem Allocation Failure!"); + shutdown( tSID, SHUT_RDWR ); + close( tSID ); + }; + } else { + LOG_ERROR("CL_CTX Mem Allocation Failure!"); + shutdown( tSID, SHUT_RDWR ); + close( tSID ); + }; + }; + }; + }; + // Handle Dead Clients + m_mViewHandlersProtector.lock(); + if( m_vViewHandlersPool.size() ) { + for( vectorHANDLERS::iterator idx = m_vViewHandlersPool.begin(); idx < m_vViewHandlersPool.end(); idx++ ) { + if( !( (*idx)->CTX->bValid ) ) { +#ifdef DEBUG_EXTRA + LOG("Have Dead Viewer!"); +#endif + pthread_join( (*idx)->m_hThreadHandle, NULL ); + free( (*idx)->CTX ); + free( (*idx) ); + m_vViewHandlersPool.erase(idx); +#ifdef DEBUG_EXTRA + LOG("View Handler Ripped!"); #endif }; }; }; + m_mViewHandlersProtector.unlock(); // validate active streams ? SLEEP( 50 ); @@ -651,7 +1037,7 @@ bool kernelModuleEmbedVideo::startService(){ LOG_ERROR(e.msg.c_str()); }; - // Socket + // Socket Live struct sockaddr sa; struct sockaddr_in *sin; memset(&sa, 0, sizeof(struct sockaddr)); @@ -687,6 +1073,45 @@ bool kernelModuleEmbedVideo::startService(){ LOG("Listen OK"); #endif }; + + + // Socket Viewer + //struct sockaddr sa; + //struct sockaddr_in *sin; + memset(&sa, 0, sizeof(struct sockaddr)); + sa.sa_family = AF_INET; + sin = (struct sockaddr_in *)&sa; + sin->sin_addr.s_addr = htonl( INADDR_ANY ); + sin->sin_port = htons( 8974 ); + + m_nViewSocketDescriptor = socket( sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0); + if( m_nViewSocketDescriptor == -1 ){ + LOG_ERROR("Failed creating ACC socket"); + return false; + }; + ENABLE_SOCKOPT( m_nViewSocketDescriptor, SOL_SOCKET, SO_REUSEADDR); +#ifdef SO_REUSEPORT + ENABLE_SOCKOPT( m_nViewSocketDescriptor, SOL_SOCKET, SO_REUSEPORT); +#endif + if( bind( m_nViewSocketDescriptor, &sa, sizeof(sa)) < 0 ){ + LOG_ERROR("Failed binding Socket"); + close( m_nViewSocketDescriptor ); + return false; +#ifdef DEBUG_EXTRA + } else { + LOG("Bind Socessfull"); +#endif + }; + if( listen( m_nViewSocketDescriptor, 10) != 0 ){ + LOG_ERROR("Failed setting web listen backlog"); + close( m_nViewSocketDescriptor ); + return false; +#ifdef DEBUG_EXTRA + } else { + LOG("Listen OK"); +#endif + }; + #ifdef DEBUG_EXTRA LOG("Start Thread"); #endif @@ -3585,6 +4010,9 @@ bool streamHandler::validate( void ){ return false; }; +//#undef DEBUG_EXTRA +//#undef DEBUG + #undef LOG #undef LOG_ERROR #undef TLOG diff --git a/lib/App/common/kernelModuleEmbedVideo.h b/lib/App/common/kernelModuleEmbedVideo.h index a6051e7a..655a3e32 100644 --- a/lib/App/common/kernelModuleEmbedVideo.h +++ b/lib/App/common/kernelModuleEmbedVideo.h @@ -2,7 +2,7 @@ #define __KERNEL_MODULE_EMBED_VIDEO_HEADER__ #include "kernelModule.h" #include "../components/base/basicMutex.h" -#include "../components/base/basicThread.h" +#include "../components/base/basicDualThread.h" #include "../components/base/basicEventHeap.h" //#include "../data/media/video/videoAVLIBDestination.h" #include "../components/base/basicMediaSource.h" @@ -236,7 +236,6 @@ typedef map mapSTREAMMAPS; #endif //OPENCV_VIDEO_OUTPUT #endif //!DIRECT_MJPEG_COMPOSING typedef std::queue Q_ENCODING_BUFFER; - #endif class streamHandler: public basicMediaDestination @@ -390,10 +389,11 @@ class streamHandler: typedef map mapSTREAMHANDLERS; -class kernelModuleEmbedVideo: public kernelModule, /*public basicMediaDestination,*/ public basicThread { +class kernelModuleEmbedVideo: public kernelModule, public basicDualThread { private: basicEventHeap * m_pEventHeap; static void * webClientProc( void *ptr ); + static void * viewClientProc( void *ptr ); int m_nSocketDescriptor; basicMutex m_mWebHandlersProtector; basicMutex m_mStreamHandlersProtector; @@ -413,9 +413,15 @@ class kernelModuleEmbedVideo: public kernelModule, /*public basicMediaDestinatio bool saveStreamPicture( uint32_t id ); bool saveStreamPicture( DBC_SNAPSHOT_DATA *ptr ); bool generateSavePicPath( CSTR MD5, CSTR Output ); + + basicMutex m_mViewHandlersProtector; + vectorHANDLERS m_vViewHandlersPool; + int m_nViewSocketDescriptor; + protected: - /* From Basic Thread */ - virtual void m_fThreadProc( void ); + /* From Basic Dual Thread */ + virtual void m_fThreadAProc( void ); + virtual void m_fThreadBProc( void ); public: explicit kernelModuleEmbedVideo( uint32_t moduleID ); ~kernelModuleEmbedVideo();