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.
2321 lines
52 KiB
2321 lines
52 KiB
#include "kernelModuleEmbedVideo.h"
|
|
|
|
#define DEBUG_EXTRA
|
|
|
|
#define LOG(X) kernelModule::m_pLog->Log("MOD_EVIDEO", this, X)
|
|
#ifdef DEBUG
|
|
#define LOG_ERROR(X) \
|
|
{ \
|
|
char _tBfrErrMSG2w34[2048]; \
|
|
sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
|
|
kernelModule::m_pLog->Log("MOD_EVIDEO", this, _LOG_ERROR, _tBfrErrMSG2w34); \
|
|
}
|
|
#else
|
|
#define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_EVIDEO", this, _LOG_ERROR, X);
|
|
#endif
|
|
|
|
#ifndef LRF_PROXY_ONLY
|
|
// #include "rgooseHardwareResourceNetworkProvider.h"
|
|
// #include "mojoKernel.h"
|
|
#include "../alg/generic/base64.h"
|
|
#include "../data/media/video/videoAVLIBSource.h"
|
|
#include "../data/media/video/videoLive555AVLibSource.h"
|
|
|
|
#include <arpa/inet.h>
|
|
#include <ctype.h>
|
|
#include <err.h>
|
|
#include <errno.h>
|
|
#include <getopt.h>
|
|
#include <ifaddrs.h>
|
|
#include <netdb.h>
|
|
#include <netinet/ip.h>
|
|
#include <netinet/udp.h>
|
|
#include <openssl/sha.h>
|
|
#include <paths.h>
|
|
#include <poll.h>
|
|
#include <signal.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/param.h> /* MIN() */
|
|
#include <sys/socket.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
// #define DEBUG
|
|
// #undef DEBUG_EXTRA
|
|
// #define DEBUG_EXTRA
|
|
|
|
const char * m_pVideoScratchPath = VIDEO_SCRATCHPAD_PATH;
|
|
|
|
#ifndef STORAGE_DIR_MODE
|
|
#define STORAGE_DIR_MODE 0x1FF
|
|
#endif
|
|
|
|
#ifndef STORAGE_FILE_MODE
|
|
#define STORAGE_FILE_MODE 0x1B6
|
|
#endif
|
|
|
|
#define TLOG(X) basicLogCollection::getInstance()->Log("MOD_EVIDEO_T", CTX, X)
|
|
#ifdef DEBUG
|
|
#define TLOG_ERROR(X) \
|
|
{ \
|
|
char _tBfrErrMSG2w34[2048]; \
|
|
sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
|
|
basicLogCollection::getInstance()->Log("MOD_EVIDEO_T", CTX, _LOG_ERROR, _tBfrErrMSG2w34); \
|
|
}
|
|
#else
|
|
#define TLOG_ERROR(X) basicLogCollection::getInstance()->Log("MOD_EVIDEO_T", CTX, _LOG_ERROR, X);
|
|
#endif
|
|
|
|
enum
|
|
{
|
|
WEBRTC_REQ_SIGNIN = 0,
|
|
WEBRTC_REQ_WAIT = 1,
|
|
WEBRTC_REQ_SIGNOUT = 2,
|
|
WEBRTC_REQ_MESSAGE = 3
|
|
};
|
|
|
|
//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),
|
|
basicDualThread()
|
|
{
|
|
m_pEventHeap = basicEventHeap::getInstance();
|
|
if (m_pDBC)
|
|
{
|
|
DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
|
|
if (tDATA)
|
|
{
|
|
memcpy(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
|
|
free(tDATA);
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("No DB Data Error!");
|
|
memset(&m_sEmbedVideoData, 0, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
|
|
m_sEmbedVideoData.port = 8972;
|
|
};
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("DB Error!");
|
|
};
|
|
m_bDetached = true;
|
|
n_pNoSignalData = NULL;
|
|
m_nNoSignalSize = 0;
|
|
#ifdef HAVE_SMAL_PREVIEW_STREAMS
|
|
n_pNoSignalSmallData = NULL;
|
|
m_nNoSignalSmallSize = 0;
|
|
#endif
|
|
char tBuff[1024];
|
|
sprintf(m_pBaseSavePath, "%s/data/reports", getcwd(tBuff, 1024));
|
|
#ifdef DEBUG
|
|
char tB[64];
|
|
sprintf(tB, "Web Streams Port: %d", m_sEmbedVideoData.port);
|
|
LOG(tB);
|
|
sprintf(tB, "Frame: [%dx%d]", m_sEmbedVideoData.width, m_sEmbedVideoData.height);
|
|
LOG(tB);
|
|
if (m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES)
|
|
{
|
|
LOG("Output Pictures");
|
|
}
|
|
if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO)
|
|
{
|
|
LOG("Output Video");
|
|
}
|
|
if (m_sEmbedVideoData.show_ts)
|
|
{
|
|
LOG("Show TimeStamp");
|
|
}
|
|
if (m_sEmbedVideoData.show_label)
|
|
{
|
|
LOG("Show Label");
|
|
}
|
|
if (m_sEmbedVideoData.show_logo)
|
|
{
|
|
LOG("Show Logo");
|
|
}
|
|
#endif
|
|
if (m_sEmbedVideoData.show_logo)
|
|
{
|
|
LOG(m_sEmbedVideoData.logo_md5);
|
|
char tPath[MAX_PATH];
|
|
sprintf(tPath, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5);
|
|
if (! fileExist(tPath))
|
|
{
|
|
LOG_ERROR("Logo File Missed!");
|
|
m_sEmbedVideoData.show_logo = false;
|
|
}
|
|
#ifdef DEBUG
|
|
else
|
|
{
|
|
LOG("Logo File is OK");
|
|
}
|
|
#endif
|
|
;
|
|
};
|
|
if (! fileExist(DB_SHMEM_POINT))
|
|
{
|
|
mkdir(DB_SHMEM_POINT, 0775); // S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
|
|
chmod(DB_SHMEM_POINT, 0775);
|
|
mkdir(m_pVideoScratchPath, 0775);
|
|
chmod(m_pVideoScratchPath, 0775);
|
|
}
|
|
else
|
|
{
|
|
if (! fileExist((CSTR) m_pVideoScratchPath))
|
|
{
|
|
mkdir(m_pVideoScratchPath, 0775);
|
|
chmod(m_pVideoScratchPath, 0775);
|
|
}
|
|
else
|
|
{
|
|
// Clean Video Scratch
|
|
char tCMD[MAX_PATH];
|
|
sprintf(tCMD, "rm -r -f -d %s/* ", m_pVideoScratchPath);
|
|
system(tCMD);
|
|
};
|
|
};
|
|
}
|
|
|
|
kernelModuleEmbedVideo::~kernelModuleEmbedVideo()
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("Destructor");
|
|
#endif
|
|
if (! m_bDetached)
|
|
{
|
|
stopService(true);
|
|
};
|
|
// Sanity check
|
|
LOG("Sanity check in Destructor!");
|
|
|
|
waitThreadAEnd(true);
|
|
if (m_vHandlersPool.size())
|
|
{ // Kill-Wait Started Probes
|
|
#ifdef DEBUG
|
|
char tB[200];
|
|
sprintf(tB, "Have to Wait Client Handlers[%lu]!", m_vHandlersPool.size());
|
|
LOG(tB);
|
|
|
|
#endif
|
|
for (vectorHANDLERS::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++)
|
|
{
|
|
pthread_cancel((*index)->m_hThreadHandle);
|
|
}
|
|
m_vHandlersPool.clear();
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("No More Clients Left");
|
|
#endif
|
|
|
|
waitThreadBEnd(true);
|
|
if (m_vViewHandlersPool.size())
|
|
{ // Kill-Wait Started Viewers
|
|
#ifdef DEBUG
|
|
char tB[200];
|
|
sprintf(tB, "Have to Wait View Handlers[%lu]!", 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
|
|
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Deleting CAPS");
|
|
#endif
|
|
if (m_mapCAPTURES.size())
|
|
{
|
|
m_mapCAPTURES.clear();
|
|
}
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Deleting MATS");
|
|
#endif
|
|
if (m_mapMATS.size())
|
|
{
|
|
m_mapMATS.clear();
|
|
}
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Deleting No Signal");
|
|
#endif
|
|
if (n_pNoSignalData)
|
|
{
|
|
free(n_pNoSignalData);
|
|
}
|
|
#ifdef HAVE_SMAL_PREVIEW_STREAMS
|
|
if (n_pNoSignalSmallData)
|
|
{
|
|
free(n_pNoSignalSmallData);
|
|
}
|
|
#endif
|
|
if (fileExist((CSTR) m_pVideoScratchPath))
|
|
{
|
|
// Clean Video Scratch
|
|
char tCMD[MAX_PATH];
|
|
sprintf(tCMD, "rm -r -f -d %s/* ", m_pVideoScratchPath);
|
|
system(tCMD);
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("All Done!");
|
|
#endif
|
|
}
|
|
|
|
static const char * ht_head =
|
|
"HTTP/1.1 200 OK\r\n"
|
|
"Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
|
|
"Pragma: no-cache\r\n"
|
|
"Expires: 0\r\n"
|
|
"MIME-version: 1.0\r\n"
|
|
"Connection: close\r\n"
|
|
//"Keep-Alive: timeout=5, max=1\r\n"
|
|
//"Connection: Keep-Alive\r\n"
|
|
//"Content-Type: video/mp4\r\n"
|
|
"Content-Transfer-Encoding: BINARY\r\n"
|
|
//"Transfer-Encoding: chunked\r\n"
|
|
"Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n"
|
|
"\r\n";
|
|
static const char * ht_abort =
|
|
"HTTP/1.1 404 OK\r\n"
|
|
"Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
|
|
"Pragma: no-cache\r\n"
|
|
"Expires: 0\r\n"
|
|
"Connection: close\r\n\r\n";
|
|
static const char * ht_no_signal =
|
|
"HTTP/1.1 200 OK\r\n"
|
|
"Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
|
|
"Pragma: no-cache\r\n"
|
|
"Expires: 0\r\n"
|
|
"MIME-version: 1.0\r\n"
|
|
"Connection: close\r\n"
|
|
"Content-Transfer-Encoding: BINARY\r\n"
|
|
//"Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n\r\n"
|
|
"Content-Type: image/jpeg\r\nContent-length: %d\r\n\r\n";
|
|
|
|
void signal_pipe_handler(int SIG)
|
|
{
|
|
// Web Client Disconnected!
|
|
if ( SIG ) SIG = SIG;
|
|
#ifdef DEBUG_EXTRA
|
|
basicLogCollection::getInstance()->Log("SIG_PIPE", NULL, _LOG_ERROR, "Sig Pipe Received Web Client Disconnected!");
|
|
#endif
|
|
}
|
|
|
|
uint8_t kernelModuleEmbedVideo::getMediaMode(void)
|
|
{
|
|
return m_sEmbedVideoData.output_mode;
|
|
}
|
|
|
|
void * kernelModuleEmbedVideo::webClientProc(void * ptr)
|
|
{
|
|
|
|
WEB_CLIENT_CTX * CTX = (WEB_CLIENT_CTX *) ptr;
|
|
//kernelModuleEmbedVideo * MODULE = CTX->ModulePtr;
|
|
uint32_t msgSize;
|
|
uint8_t tBuf[FILESTREAMBUFFERSZ];
|
|
//int retVal;
|
|
bool w = true;
|
|
#ifdef DEBUG_EXTRA
|
|
TLOG("Start WebClient");
|
|
#endif
|
|
pthread_setname_np(pthread_self(), "LiveVideoCL");
|
|
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_EXTRA
|
|
TLOG("Req URI");
|
|
TLOG(cGetURI);
|
|
#endif
|
|
};
|
|
// if( ( strncmp( cGetURI, "/stream", 7 ) == 0 ) || ( strncmp( cGetURI, "/video_feed", 11 ) == 0 ) ) {
|
|
if (strncmp(cGetURI, "/video_feed", 11) == 0)
|
|
{
|
|
uint32_t tStreamID = 0;
|
|
CSTR tP;
|
|
#ifdef HAVE_SMAL_PREVIEW_STREAMS
|
|
if ((tP = strstr(cGetURI, "/preview")))
|
|
{
|
|
CTX->bPreviewMode = true;
|
|
}
|
|
#endif
|
|
if ((tP = strstr(cGetURI, "?")))
|
|
{
|
|
CSTR tB;
|
|
tP++;
|
|
if ((tB = strstr(tP, "id=")))
|
|
{
|
|
tStreamID = atoi(tB + 3);
|
|
};
|
|
};
|
|
if (tStreamID)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[64];
|
|
sprintf(tB, "StremID: %d", tStreamID);
|
|
TLOG(tB);
|
|
#endif
|
|
if (CTX->ModulePtr->registerForMediaStream(tStreamID, CTX))
|
|
{
|
|
// Send Header
|
|
if (send(CTX->m_iSocketFID, ht_head, strlen(ht_head), 0) < 0)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
sprintf(mesg, "Failed sending file to client: %s", strerror(errno));
|
|
TLOG_ERROR(mesg);
|
|
#endif
|
|
w = false;
|
|
};
|
|
CTX->initDone = true;
|
|
continue;
|
|
#ifdef DEBUG_EXTRA
|
|
}
|
|
else
|
|
{
|
|
TLOG("Stream id Not Online");
|
|
#endif
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
}
|
|
else
|
|
{
|
|
TLOG("No Stream ID");
|
|
#endif
|
|
};
|
|
};
|
|
if (CTX->ModulePtr->n_pNoSignalData)
|
|
{
|
|
// Send Dummy Picture
|
|
#ifdef HAVE_SMAL_PREVIEW_STREAMS
|
|
if (CTX->bPreviewMode)
|
|
{
|
|
sprintf(mesg, ht_no_signal, CTX->ModulePtr->m_nNoSignalSmallSize);
|
|
send(CTX->m_iSocketFID, mesg, strlen(mesg), 0);
|
|
send(CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalSmallData, CTX->ModulePtr->m_nNoSignalSmallSize, 0);
|
|
send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0);
|
|
}
|
|
else
|
|
{
|
|
#endif
|
|
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 HAVE_SMAL_PREVIEW_STREAMS
|
|
};
|
|
#endif
|
|
#ifdef DEBUG_EXTRA
|
|
TLOG("VFEED 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
|
|
{
|
|
/*
|
|
retVal = read( CTX->m_iEncoderPipe[0], &msgSize, sizeof(uint32_t));
|
|
if( retVal == -1 ){
|
|
if( errno == EAGAIN || errno == EWOULDBLOCK ){
|
|
continue;
|
|
};
|
|
TLOG_ERROR("Pipe Communication Error[1]!");
|
|
CTX->bValid = false;
|
|
continue;
|
|
};
|
|
if( msgSize < FILESTREAMBUFFERSZ ) {
|
|
retVal = read( CTX->m_iEncoderPipe[0], tBuf, msgSize );
|
|
} else {
|
|
TLOG_ERROR("Data too Big in Pipe Transfer!");
|
|
CTX->bValid = false;
|
|
continue;
|
|
};
|
|
if( retVal == -1 ){
|
|
//free( tBuf );
|
|
if( errno == EAGAIN || errno == EWOULDBLOCK ){
|
|
continue;
|
|
};
|
|
TLOG_ERROR("Pipe Communication Error[2]!");
|
|
CTX->bValid = false;
|
|
continue;
|
|
};
|
|
*/
|
|
/**************/
|
|
CTX->ModulePtr->m_mEncodedProtector.lock();
|
|
#ifdef HAVE_SMAL_PREVIEW_STREAMS
|
|
if (CTX->bPreviewMode)
|
|
{
|
|
if (CTX->ModulePtr->m_mapPREVIEWS[CTX->mediaStreamId].size())
|
|
{
|
|
msgSize = CTX->ModulePtr->m_mapPREVIEWS[CTX->mediaStreamId].size();
|
|
if (msgSize < FILESTREAMBUFFERSZ)
|
|
{
|
|
memcpy(tBuf, CTX->ModulePtr->m_mapPREVIEWS[CTX->mediaStreamId].data(), msgSize);
|
|
CTX->ModulePtr->m_mEncodedProtector.unlock();
|
|
}
|
|
else
|
|
{
|
|
CTX->ModulePtr->m_mEncodedProtector.unlock();
|
|
TLOG_ERROR("Data too Big in Pipe Transfer!");
|
|
w = false;
|
|
continue;
|
|
};
|
|
}
|
|
else
|
|
{
|
|
CTX->ModulePtr->m_mEncodedProtector.unlock();
|
|
if (CTX->ModulePtr->n_pNoSignalSmallData)
|
|
{
|
|
msgSize = CTX->ModulePtr->m_nNoSignalSmallSize;
|
|
memcpy(tBuf, CTX->ModulePtr->n_pNoSignalSmallData, msgSize);
|
|
}
|
|
else
|
|
{
|
|
TLOG_ERROR("Data too Big in Pipe Transfer!");
|
|
w = false;
|
|
continue;
|
|
};
|
|
};
|
|
}
|
|
else
|
|
{
|
|
#endif
|
|
if (CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].size())
|
|
{
|
|
msgSize = CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].size();
|
|
if (msgSize < FILESTREAMBUFFERSZ)
|
|
{
|
|
memcpy(tBuf, CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].data(), msgSize);
|
|
CTX->ModulePtr->m_mEncodedProtector.unlock();
|
|
}
|
|
else
|
|
{
|
|
CTX->ModulePtr->m_mEncodedProtector.unlock();
|
|
TLOG_ERROR("Data too Big in Pipe Transfer!");
|
|
w = false;
|
|
continue;
|
|
};
|
|
}
|
|
else
|
|
{
|
|
|
|
#ifdef DEBUG_EXTRA
|
|
TLOG("VFEED Sent No Signal Image!");
|
|
#endif
|
|
CTX->ModulePtr->m_mEncodedProtector.unlock();
|
|
if (CTX->ModulePtr->n_pNoSignalData)
|
|
{
|
|
msgSize = CTX->ModulePtr->m_nNoSignalSize;
|
|
memcpy(tBuf, CTX->ModulePtr->n_pNoSignalData, msgSize);
|
|
}
|
|
else
|
|
{
|
|
TLOG_ERROR("Data too Big in Pipe Transfer!");
|
|
w = false;
|
|
continue;
|
|
};
|
|
};
|
|
#ifdef HAVE_SMAL_PREVIEW_STREAMS
|
|
};
|
|
#endif
|
|
/*********/
|
|
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_EXTRA
|
|
sprintf(mesg, "[1]Failed sending file to client: %s", strerror(errno));
|
|
TLOG_ERROR(mesg);
|
|
#endif
|
|
w = false;
|
|
continue;
|
|
};
|
|
if (send(CTX->m_iSocketFID, tBuf, msgSize, 0) < 0)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
sprintf(mesg, "[2]Failed sending file to client: %s", strerror(errno));
|
|
TLOG_ERROR(mesg);
|
|
#endif
|
|
w = false;
|
|
continue;
|
|
};
|
|
if (send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0) < 0)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
sprintf(mesg, "[3]Failed sending file to client: %s", strerror(errno));
|
|
TLOG_ERROR(mesg);
|
|
#endif
|
|
w = false;
|
|
continue;
|
|
};
|
|
usleep(66666);
|
|
};
|
|
};
|
|
};
|
|
close(CTX->m_iEncoderPipe[0]);
|
|
shutdown(CTX->m_iSocketFID, SHUT_RDWR);
|
|
SLEEP(3);
|
|
close(CTX->m_iSocketFID);
|
|
CTX->bValid = false;
|
|
#ifdef DEBUG_EXTRA
|
|
TLOG("WEB Client Handler Ended!");
|
|
#endif
|
|
pthread_exit(NULL);
|
|
return NULL;
|
|
}
|
|
|
|
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;
|
|
TLOG("Start ViewClient");
|
|
pthread_setname_np(pthread_self(), "ReportVideoCL");
|
|
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_EXTRA
|
|
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 = gmtime ( &tDTS );
|
|
struct tm * ptm = localtime(&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_EXTRA
|
|
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
|
|
};
|
|
}
|
|
else
|
|
{
|
|
TLOG_ERROR("VVIEW File Not Exists!");
|
|
};
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
TLOG("VVIEW Catched in DeadEnd!");
|
|
#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("VVIEW 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 =(void *)(((uint64_t)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;
|
|
}
|
|
#ifdef DEBUG
|
|
else
|
|
{
|
|
TLOG("File is Finished!");
|
|
}
|
|
#endif
|
|
};
|
|
#ifdef DEBUG
|
|
if (feof(tFID))
|
|
{
|
|
TLOG("LAST FRAME SENT");
|
|
}
|
|
#endif
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
TLOG("Close File");
|
|
#endif
|
|
fclose(tFID);
|
|
w = false;
|
|
};
|
|
};
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
TLOG("Shut Down Socket");
|
|
#endif
|
|
shutdown(CTX->m_iSocketFID, SHUT_RDWR);
|
|
#ifdef DEBUG_EXTRA
|
|
TLOG("Close Socket");
|
|
#endif
|
|
close(CTX->m_iSocketFID);
|
|
CTX->bValid = false;
|
|
#ifdef DEBUG_EXTRA
|
|
TLOG("View Client Handler Ended!");
|
|
#endif
|
|
pthread_exit(NULL);
|
|
TLOG("After Pthread Exit");
|
|
return NULL;
|
|
}
|
|
|
|
// Start Thread To view Live Streams
|
|
void kernelModuleEmbedVideo::m_fThreadAProc(void)
|
|
{
|
|
if (! m_bEnabled)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Module is Disabled!");
|
|
#endif
|
|
return;
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("[LIVE] Module Started!");
|
|
#endif
|
|
pthread_setname_np(pthread_self(), "Live Video Stream");
|
|
struct pollfd pfd[1];
|
|
pfd->fd = m_nSocketDescriptor;
|
|
pfd->events = POLLIN | POLLHUP;
|
|
m_bThreadAStatus = TH_WORKING;
|
|
while (m_bThreadAStatus < TH_ENDED)
|
|
{
|
|
if (! m_bEnabled)
|
|
{
|
|
m_bThreadAStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
int RetVal = poll(pfd, 1, 1000);
|
|
if (RetVal < 0)
|
|
{
|
|
if (EINTR == errno)
|
|
{
|
|
continue;
|
|
}
|
|
LOG_ERROR("Unrecoverable error");
|
|
m_bThreadAStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
// Handle Connections
|
|
if (RetVal != 0)
|
|
{
|
|
if (pfd[0].revents & POLLIN)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Handle New Client");
|
|
#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, webClientProc, (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_mWebHandlersProtector.lock();
|
|
m_vHandlersPool.push_back(tHANDLER);
|
|
m_mWebHandlersProtector.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_mWebHandlersProtector.lock();
|
|
if (m_vHandlersPool.size())
|
|
{
|
|
for (vectorHANDLERS::iterator idx = m_vHandlersPool.begin(); idx != m_vHandlersPool.end();)
|
|
{
|
|
if (! ((*idx)->CTX->bValid))
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Have Dead Client!");
|
|
#endif
|
|
pthread_join((*idx)->m_hThreadHandle, NULL);
|
|
// close( (*idx)->CTX->m_iEncoderPipe[0] );
|
|
close((*idx)->CTX->m_iEncoderPipe[1]);
|
|
free((*idx)->CTX);
|
|
free(*idx);
|
|
m_vHandlersPool.erase(idx);
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Web Handler Ripped!");
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
idx++;
|
|
}
|
|
};
|
|
};
|
|
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;
|
|
pthread_setname_np(pthread_self(), "Report Video View");
|
|
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();)
|
|
{
|
|
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
|
|
}
|
|
else
|
|
{
|
|
idx++;
|
|
}
|
|
};
|
|
};
|
|
m_mViewHandlersProtector.unlock();
|
|
// validate active streams ?
|
|
|
|
SLEEP(50);
|
|
};
|
|
}
|
|
|
|
#define ENABLE_SOCKOPT(sd, level, opt) \
|
|
do \
|
|
{ \
|
|
int val = 1; \
|
|
if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \
|
|
warn("Failed enabling %s for web service", #opt); \
|
|
} \
|
|
while (0);
|
|
|
|
bool kernelModuleEmbedVideo::startService()
|
|
{
|
|
m_bDetached = false;
|
|
// Create NoSignal Image
|
|
cv::Mat tMat(m_sEmbedVideoData.height, m_sEmbedVideoData.width, CV_8UC3);
|
|
cv::rectangle(tMat, cv::Point(0, 0), cv::Point(m_sEmbedVideoData.width, m_sEmbedVideoData.height), cv::Scalar(255, 0, 0), cv::FILLED);
|
|
cv::putText(tMat, "NO SIGNAL", cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255), 3);
|
|
if (m_sEmbedVideoData.show_logo)
|
|
{
|
|
int a_x, a_y;
|
|
if (m_sEmbedVideoData.logo_x < 0)
|
|
{
|
|
a_x = m_sEmbedVideoData.width + m_sEmbedVideoData.logo_x - m_sEmbedVideoData.logo_w;
|
|
if (a_x < 0)
|
|
{
|
|
a_x = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
a_x = m_sEmbedVideoData.logo_x;
|
|
}
|
|
if ((a_x + m_sEmbedVideoData.logo_w) > m_sEmbedVideoData.width)
|
|
{
|
|
a_x = m_sEmbedVideoData.width - m_sEmbedVideoData.logo_w;
|
|
}
|
|
if (m_sEmbedVideoData.logo_y < 0)
|
|
{
|
|
a_y = m_sEmbedVideoData.height + m_sEmbedVideoData.logo_y - m_sEmbedVideoData.logo_h;
|
|
if (a_y < 0)
|
|
{
|
|
a_y = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
a_y = m_sEmbedVideoData.logo_y;
|
|
}
|
|
if ((a_y + m_sEmbedVideoData.logo_h) > m_sEmbedVideoData.height)
|
|
{
|
|
a_y = m_sEmbedVideoData.height - m_sEmbedVideoData.logo_h;
|
|
}
|
|
if ((a_x > -1) && (a_y > -1))
|
|
{
|
|
char tName[256];
|
|
m_sEmbedVideoData.logo_x = a_x;
|
|
m_sEmbedVideoData.logo_y = a_y;
|
|
sprintf(tName, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5);
|
|
cv::Mat T = cv::imread(tName, cv::IMREAD_UNCHANGED);
|
|
if (T.cols > 0)
|
|
{
|
|
cv::Mat mask;
|
|
vector<cv::Mat> layers;
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Logo Loaded OK");
|
|
#endif
|
|
cv::split(T, layers); // seperate channels
|
|
cv::Mat rgb[3] = {layers[0], layers[1], layers[2]};
|
|
mask = layers[3]; // png's alpha channel used as mask
|
|
cv::merge(rgb, 3, T); // put together the RGB channels, now transp insn't transparent
|
|
|
|
cv::resize(T, m_mLogoPic, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), cv::INTER_CUBIC);
|
|
cv::resize(mask, m_mLogoMask, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), cv::INTER_CUBIC);
|
|
|
|
cv::Rect roi(cv::Point(m_sEmbedVideoData.logo_x, m_sEmbedVideoData.logo_y), cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h));
|
|
cv::Mat destinationROI = tMat(roi);
|
|
m_mLogoPic.copyTo(destinationROI, m_mLogoMask);
|
|
// m_sEmbedVideoData.show_logo = false;
|
|
}
|
|
else
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Logo Disabled - No Picture is Loaded");
|
|
#endif
|
|
m_sEmbedVideoData.show_logo = false;
|
|
};
|
|
}
|
|
else
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Logo Disabled - Wrong Coords");
|
|
#endif
|
|
m_sEmbedVideoData.show_logo = false;
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Logo Draw OK");
|
|
#endif
|
|
};
|
|
|
|
try
|
|
{
|
|
vector<int> compression_params;
|
|
// compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
|
|
// compression_params.push_back(9);
|
|
compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
|
|
compression_params.push_back(m_sEmbedVideoData.compression_quality);
|
|
vector<uchar> buf;
|
|
cv::imencode(".jpg", tMat, buf, compression_params);
|
|
m_nNoSignalSize = buf.size();
|
|
n_pNoSignalData = (uint8_t *) malloc(m_nNoSignalSize);
|
|
if (n_pNoSignalData)
|
|
{
|
|
memcpy(n_pNoSignalData, buf.data(), m_nNoSignalSize);
|
|
}
|
|
else
|
|
{
|
|
m_nNoSignalSize = 0;
|
|
}
|
|
buf.clear();
|
|
#ifdef HAVE_SMAL_PREVIEW_STREAMS
|
|
// Make Small Preview Thumb
|
|
cv::Mat dst;
|
|
cv::resize(tMat, dst, cv::Size(PREVIEW_SIZE_WIDTH, PREVIEW_SIZE_HEIGHT));
|
|
cv::imencode(".jpg", dst, buf, compression_params);
|
|
m_nNoSignalSmallSize = buf.size();
|
|
n_pNoSignalSmallData = (uint8_t *) malloc(m_nNoSignalSmallSize);
|
|
if (n_pNoSignalSmallData)
|
|
{
|
|
memcpy(n_pNoSignalSmallData, buf.data(), m_nNoSignalSmallSize);
|
|
}
|
|
else
|
|
{
|
|
m_nNoSignalSmallSize = 0;
|
|
}
|
|
buf.clear();
|
|
#endif
|
|
}
|
|
catch (cv::Exception & e)
|
|
{
|
|
LOG_ERROR(e.msg.c_str());
|
|
};
|
|
|
|
// Socket Live
|
|
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(m_sEmbedVideoData.port);
|
|
|
|
m_nSocketDescriptor = socket(sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
|
if (m_nSocketDescriptor == -1)
|
|
{
|
|
LOG_ERROR("Failed creating ACC socket");
|
|
return false;
|
|
};
|
|
ENABLE_SOCKOPT(m_nSocketDescriptor, SOL_SOCKET, SO_REUSEADDR);
|
|
#ifdef SO_REUSEPORT
|
|
ENABLE_SOCKOPT(m_nSocketDescriptor, SOL_SOCKET, SO_REUSEPORT);
|
|
#endif
|
|
if (bind(m_nSocketDescriptor, &sa, sizeof(sa)) < 0)
|
|
{
|
|
LOG_ERROR("Failed binding Socket");
|
|
close(m_nSocketDescriptor);
|
|
return false;
|
|
#ifdef DEBUG_EXTRA
|
|
}
|
|
else
|
|
{
|
|
LOG("Bind Socessfull");
|
|
#endif
|
|
};
|
|
if (listen(m_nSocketDescriptor, 10) != 0)
|
|
{
|
|
LOG_ERROR("Failed setting web listen backlog");
|
|
close(m_nSocketDescriptor);
|
|
return false;
|
|
#ifdef DEBUG_EXTRA
|
|
}
|
|
else
|
|
{
|
|
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
|
|
m_bEnabled = init();
|
|
return m_bEnabled;
|
|
}
|
|
|
|
bool kernelModuleEmbedVideo::stopService(bool force)
|
|
{
|
|
m_bEnabled = false;
|
|
// Stop Conveyor
|
|
m_bDetached = uninit();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("StopService!");
|
|
#endif
|
|
|
|
m_mStreamHandlersProtector.lock();
|
|
if (m_mapHANDLERS.size())
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("Deleting Stream Handlers!");
|
|
#endif
|
|
for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++)
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("Deleting Stream Handler");
|
|
#endif
|
|
delete((*idx).second);
|
|
}
|
|
m_mapHANDLERS.clear();
|
|
};
|
|
m_mStreamHandlersProtector.unlock();
|
|
|
|
if (m_vHandlersPool.size())
|
|
{ // Kill-Wait Started Probes
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[200];
|
|
sprintf(tB, "Have to Wait Client Handlers[%d]!", m_vHandlersPool.size());
|
|
LOG(tB);
|
|
|
|
#endif
|
|
for (vectorHANDLERS::iterator index = m_vHandlersPool.begin(); index != m_vHandlersPool.end(); index++)
|
|
{
|
|
if (force)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Force Client Handler to Stop!");
|
|
#endif
|
|
pthread_cancel((*index)->m_hThreadHandle);
|
|
}
|
|
else
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Waiting Client Handler to End!");
|
|
#endif
|
|
pthread_join((*index)->m_hThreadHandle, NULL);
|
|
}
|
|
};
|
|
m_vHandlersPool.clear();
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("No More Client Handlers!");
|
|
#endif
|
|
if (m_mapCAPTURES.size())
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Some Caps!");
|
|
#endif
|
|
for (mapSTREAMCAPS::iterator idx = m_mapCAPTURES.begin(); idx != m_mapCAPTURES.end(); idx++)
|
|
{
|
|
idx->second.clear();
|
|
}
|
|
m_mapCAPTURES.clear();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("No More Caps!");
|
|
#endif
|
|
};
|
|
if (m_mapMATS.size())
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Some Mats!");
|
|
#endif
|
|
for (mapSTREAMMAPS::iterator idx = m_mapMATS.begin(); idx != m_mapMATS.end(); idx++)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Release Mat!");
|
|
#endif
|
|
idx->second.release();
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Clear Map Mats!");
|
|
#endif
|
|
m_mapMATS.clear();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("No More Mats!");
|
|
#endif
|
|
};
|
|
if (n_pNoSignalData)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("No Sig Data!");
|
|
#endif
|
|
free(n_pNoSignalData);
|
|
n_pNoSignalData = NULL;
|
|
m_nNoSignalSize = 0;
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("No No Sig Data!");
|
|
#endif
|
|
};
|
|
return m_bDetached;
|
|
}
|
|
|
|
bool kernelModuleEmbedVideo::reloadSettings()
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Reload Service");
|
|
#endif
|
|
if (m_pDBC)
|
|
{
|
|
DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
|
|
if (tDATA)
|
|
{
|
|
if (stopService())
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Stoped!");
|
|
#endif
|
|
memcpy(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
|
|
free(tDATA);
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Starting...");
|
|
#endif
|
|
return startService();
|
|
}
|
|
else
|
|
{
|
|
free(tDATA);
|
|
};
|
|
};
|
|
};
|
|
return false;
|
|
}
|
|
|
|
#include "deviceNodeCamera.h"
|
|
#include "deviceNodeFactory.h"
|
|
|
|
bool kernelModuleEmbedVideo::validate()
|
|
{
|
|
bool RetVal = true;
|
|
deviceNode ** tA = NULL;
|
|
uint32_t tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_STATIC_CAMERA, tA);
|
|
if (! tCNT)
|
|
{
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_PTZ_CAMERA, tA);
|
|
if (! tCNT)
|
|
{
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_STATIC_CAMERA, tA);
|
|
if (! tCNT)
|
|
{
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_PTZ_CAMERA, tA);
|
|
if (! tCNT)
|
|
{
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_PRESET_CAMERA, tA);
|
|
if (! tCNT)
|
|
{
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_PRESET_CAMERA, tA);
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
if (tCNT)
|
|
{
|
|
for (uint32_t i = 0; i < tCNT; i++)
|
|
{
|
|
deviceNodeCamera * tCAM = (deviceNodeCamera *) tA[i];
|
|
if (! registerStream(tCAM->getCamStream(), tCAM->getFlipMode()))
|
|
{
|
|
RetVal = false;
|
|
// tCAM->set
|
|
continue;
|
|
};
|
|
};
|
|
free(tA);
|
|
tA = NULL;
|
|
#ifdef DEBUG_EXTRA
|
|
}
|
|
else
|
|
{
|
|
LOG("No Camera Defined/Enabled In the system!");
|
|
#endif
|
|
};
|
|
if (RetVal)
|
|
{
|
|
DBC_INT_LIST tInvalidList;
|
|
m_mStreamHandlersProtector.lock();
|
|
for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++)
|
|
{
|
|
if ((*idx).second != NULL)
|
|
{
|
|
if (! (*idx).second->validate())
|
|
{
|
|
tInvalidList.push_back((*idx).first);
|
|
}
|
|
}
|
|
};
|
|
m_mStreamHandlersProtector.unlock();
|
|
if (tInvalidList.size())
|
|
{
|
|
for (DBC_INT_LIST::iterator idx = tInvalidList.begin(); idx != tInvalidList.end(); idx++)
|
|
{
|
|
if (unregisterStream(*idx))
|
|
{
|
|
registerStream(*idx);
|
|
}
|
|
};
|
|
tInvalidList.clear();
|
|
};
|
|
};
|
|
return RetVal;
|
|
|
|
if (m_pDBC)
|
|
{
|
|
DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
|
|
if (tDATA)
|
|
{
|
|
if (memcmp(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA)) != 0)
|
|
{
|
|
free(tDATA);
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Module Settings Changed!");
|
|
#endif
|
|
return reloadSettings();
|
|
};
|
|
free(tDATA);
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("No Fetched EVE Data");
|
|
RetVal = false;
|
|
};
|
|
if (RetVal)
|
|
{
|
|
DBC_INT_LIST * tList = m_pDBC->listMediaStreams(0, LIST_VALID_RECORDS);
|
|
if (tList != NULL)
|
|
{
|
|
for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("REG STREAM");
|
|
#endif
|
|
if (! registerStream(*idx))
|
|
{
|
|
RetVal = false;
|
|
continue;
|
|
};
|
|
};
|
|
delete(tList);
|
|
#ifdef DEBUG_EXTRA
|
|
}
|
|
else
|
|
{
|
|
LOG("- No Valid Streams -");
|
|
#endif
|
|
};
|
|
};
|
|
if (RetVal)
|
|
{
|
|
DBC_INT_LIST tInvalidList;
|
|
m_mStreamHandlersProtector.lock();
|
|
for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++)
|
|
{
|
|
if ((*idx).second != NULL)
|
|
{
|
|
if (! (*idx).second->validate())
|
|
{
|
|
tInvalidList.push_back((*idx).first);
|
|
}
|
|
}
|
|
};
|
|
m_mStreamHandlersProtector.unlock();
|
|
if (tInvalidList.size())
|
|
{
|
|
for (DBC_INT_LIST::iterator idx = tInvalidList.begin(); idx != tInvalidList.end(); idx++)
|
|
{
|
|
if (unregisterStream(*idx))
|
|
{
|
|
registerStream(*idx);
|
|
}
|
|
};
|
|
tInvalidList.clear();
|
|
};
|
|
};
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("DB Error!");
|
|
RetVal = false;
|
|
};
|
|
return RetVal;
|
|
}
|
|
|
|
bool kernelModuleEmbedVideo::registerForMediaStream(uint32_t id, WEB_CLIENT_CTX *& CTX)
|
|
{
|
|
if (CTX)
|
|
{
|
|
if (id)
|
|
{
|
|
// Check if its Valid!
|
|
DBC_MEDIA_STREAM_DATA * tSD = m_pDBC->getMediaStreamData(id);
|
|
if (tSD)
|
|
{
|
|
// if( !tSD->valid ) {
|
|
// free( tSD );
|
|
// return false;
|
|
// };
|
|
free(tSD);
|
|
// if( pipe( CTX->m_iEncoderPipe ) == -1 ){
|
|
// CTX->bValid = false;
|
|
// return false;
|
|
// };
|
|
CTX->mediaStreamId = id;
|
|
return true;
|
|
};
|
|
};
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Client Context error!");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool kernelModuleEmbedVideo::dispatchEvent(EVENT_CONTAINER * Evt)
|
|
{
|
|
if (Evt)
|
|
{
|
|
switch (Evt->m_eClass)
|
|
{
|
|
case EVT_CLASS_IMAGE:
|
|
case EVT_CLASS_VIDEO:
|
|
{
|
|
switch (Evt->m_eType)
|
|
{
|
|
case EVT_CAPTURE_START:
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("Capture Event");
|
|
#endif
|
|
if (Evt->m_ref_id && Evt->m_db_id)
|
|
{
|
|
uint32_t tPicID = saveStreamPicture(Evt->m_ref_id);
|
|
if (tPicID)
|
|
{
|
|
//
|
|
// m_pDBC->addPictureEvent( Evt->m_db_id, tPicID );
|
|
}
|
|
else
|
|
{
|
|
#ifdef DEBUG
|
|
LOG_ERROR("No Picture is Saved!");
|
|
#endif
|
|
};
|
|
}
|
|
else
|
|
{
|
|
#ifdef DEBUG
|
|
LOG_ERROR("No Stream ID or Event ID in Event Data!");
|
|
#endif
|
|
};
|
|
break;
|
|
};
|
|
default: break;
|
|
};
|
|
break;
|
|
};
|
|
case EVT_CLASS_CAMERA_CTRL:
|
|
{
|
|
switch (Evt->m_eType)
|
|
{
|
|
case EVT_CAMERA_MOVE_TO:
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Cam OnMove After Event -> Register SourceID");
|
|
#endif
|
|
uint32_t sid = m_pDBC->getNodeAuxParam(Evt->m_nSourceNodeID);
|
|
if (sid)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Source ID found!");
|
|
#endif
|
|
m_mStreamHandlersProtector.lock();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Mutex Locked!");
|
|
#endif
|
|
mapSTREAMHANDLERS::iterator hnd = m_mapHANDLERS.find(sid);
|
|
if (hnd != m_mapHANDLERS.end())
|
|
#ifdef DEBUG_EXTRA
|
|
{
|
|
LOG("Handler found!");
|
|
#endif
|
|
(*hnd).second->triggerCamMove();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Triggered!");
|
|
}
|
|
else
|
|
{
|
|
LOG("No Stream Found!");
|
|
}
|
|
#endif
|
|
m_mStreamHandlersProtector.unlock();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Mutex Unlocked!");
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
LOG("No Stream ID Found!");
|
|
}
|
|
break;
|
|
};
|
|
case EVT_CAMERA_ON_POSITION:
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Cam On Position After Event -> Capture Picture");
|
|
#endif
|
|
DBC_SNAPSHOT_DATA * tData = (DBC_SNAPSHOT_DATA *) Evt->m_pEvtData;
|
|
if (tData)
|
|
{
|
|
if (! saveStreamPicture(tData))
|
|
{
|
|
LOG_ERROR("Save Error!");
|
|
}
|
|
tData->assoc_event_ids.clear();
|
|
//free(tData);
|
|
delete( tData );
|
|
Evt->m_pEvtData = NULL;
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Event Data Saved!");
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("No Evt Data!");
|
|
}
|
|
break;
|
|
};
|
|
default: break;
|
|
};
|
|
break;
|
|
};
|
|
default: break;
|
|
};
|
|
return true;
|
|
};
|
|
return false;
|
|
}
|
|
|
|
// https://www.ps2-home.com/forum/viewtopic.php?t=98
|
|
// https://www.mikekohn.net/software/playstation2.php
|
|
|
|
bool kernelModuleEmbedVideo::saveStreamPicture(DBC_SNAPSHOT_DATA * ptr)
|
|
{
|
|
if (ptr == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES) == EVE_OUTPUT_PICTURES)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Saving Picture!");
|
|
#endif
|
|
// saveStreamPicture( ptr->stream_id );
|
|
|
|
//*
|
|
if (ptr->assoc_event_ids.size())
|
|
{
|
|
if (m_mapCAPTURES.find(ptr->stream_id) != m_mapCAPTURES.end())
|
|
{
|
|
m_mPictureProtector.lock();
|
|
ptr->data = (uint8_t *) malloc(m_mapCAPTURES[ptr->stream_id].size());
|
|
if (ptr->data)
|
|
{
|
|
ptr->data_size = m_mapCAPTURES[ptr->stream_id].size();
|
|
memcpy(ptr->data, m_mapCAPTURES[ptr->stream_id].data(), ptr->data_size);
|
|
m_mPictureProtector.unlock();
|
|
CSTR MD5 = MD5Binary(ptr->data, ptr->data_size);
|
|
if (MD5)
|
|
{
|
|
strncpy(ptr->md5, MD5, 32);
|
|
free(MD5);
|
|
if (! m_pDBC->addPicture(ptr, "jpg"))
|
|
{
|
|
LOG_ERROR("Database Error to consume capture!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("no MD5 Hash!");
|
|
}
|
|
free(ptr->data);
|
|
}
|
|
else
|
|
{
|
|
m_mPictureProtector.unlock();
|
|
LOG_ERROR("No memory to allocate!");
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
}
|
|
else
|
|
{
|
|
LOG("No apropriate Stream Found!");
|
|
#endif
|
|
};
|
|
};
|
|
//*/
|
|
#ifdef DEBUG_EXTRA
|
|
}
|
|
else
|
|
{
|
|
LOG("No Pic Mode!");
|
|
#endif
|
|
}
|
|
if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO)
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("Saving Video!");
|
|
#endif
|
|
if (ptr->assoc_event_ids.size())
|
|
{
|
|
m_mStreamHandlersProtector.lock();
|
|
mapSTREAMHANDLERS::iterator hnd = m_mapHANDLERS.find(ptr->stream_id);
|
|
if (hnd != m_mapHANDLERS.end())
|
|
{
|
|
uint32_t db_id;
|
|
uint32_t cfrm;
|
|
for (DBC_INT_LIST::iterator idx = ptr->assoc_event_ids.begin(); idx < ptr->assoc_event_ids.end(); idx++)
|
|
{
|
|
(*hnd).second->triggerEvent(*idx, db_id, cfrm);
|
|
}
|
|
#ifdef DEBUG
|
|
}
|
|
else
|
|
{
|
|
LOG("No Stream Found!");
|
|
#endif
|
|
};
|
|
m_mStreamHandlersProtector.unlock();
|
|
#ifdef DEBUG_EXTRA
|
|
}
|
|
else
|
|
{
|
|
LOG("No Assoc EVTIDS!");
|
|
#endif
|
|
};
|
|
#ifdef DEBUG
|
|
}
|
|
else
|
|
{
|
|
LOG("No Video Mode!");
|
|
#endif
|
|
};
|
|
return true;
|
|
}
|
|
|
|
bool kernelModuleEmbedVideo::saveStreamPicture(uint32_t id)
|
|
{
|
|
if (! id)
|
|
{
|
|
return false;
|
|
}
|
|
if (m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES)
|
|
{
|
|
if (m_mapCAPTURES.find(id) != m_mapCAPTURES.end())
|
|
{
|
|
//FILE * tFD;
|
|
m_mPictureProtector.lock();
|
|
size_t tBufferSize = m_mapCAPTURES[id].size();
|
|
uint8_t * tBuffer = (uint8_t *) malloc(tBufferSize);
|
|
if (tBuffer)
|
|
{
|
|
memcpy(tBuffer, m_mapCAPTURES[id].data(), tBufferSize);
|
|
m_mPictureProtector.unlock();
|
|
CSTR MD5 = MD5Binary(tBuffer, tBufferSize);
|
|
if (MD5)
|
|
{
|
|
char tFileName[MAX_PATH];
|
|
if (generateSavePicPath(MD5, tFileName))
|
|
{
|
|
FILE * fd = fopen(tFileName, "wb+");
|
|
if (fd)
|
|
{
|
|
fwrite(tBuffer, 1, tBufferSize, fd);
|
|
fclose(fd);
|
|
ch_chown(tFileName, "nobody", "nogroup");
|
|
// retVal = m_pDBC->addPicture( id, MD5, "jpg" );
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Can not open picture file!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Can not generate file name!");
|
|
}
|
|
free(MD5);
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("no MD5 Hash!");
|
|
}
|
|
free(tBuffer);
|
|
}
|
|
else
|
|
{
|
|
m_mPictureProtector.unlock();
|
|
LOG_ERROR("no memory to allocate!");
|
|
};
|
|
};
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int kernelModuleEmbedVideo::getWidh(void)
|
|
{
|
|
if (m_sEmbedVideoData.width)
|
|
{
|
|
return m_sEmbedVideoData.width;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int kernelModuleEmbedVideo::getHeight(void)
|
|
{
|
|
if (m_sEmbedVideoData.height)
|
|
{
|
|
return m_sEmbedVideoData.height;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
bool kernelModuleEmbedVideo::generateSavePicPath(CSTR MD5, CSTR Output)
|
|
{
|
|
if (MD5 == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
if (Output == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
if (! fileExist(m_pBaseSavePath))
|
|
{
|
|
LOG_ERROR("No Base Picture Save Path Defined!");
|
|
return false;
|
|
};
|
|
if (strlen(MD5) < 4)
|
|
{
|
|
return false;
|
|
}
|
|
char test[5000];
|
|
sprintf(test, "%s/%c%c", m_pBaseSavePath, MD5[0], MD5[1]);
|
|
if (! fileExist(test))
|
|
{
|
|
#ifdef __linux__
|
|
mkdir(test, STORAGE_DIR_MODE);
|
|
#else
|
|
_mkdir(test);
|
|
#endif
|
|
ch_chown(test, "nobody", "nogroup");
|
|
};
|
|
sprintf(test, "%s/%c%c/%c%c", m_pBaseSavePath, MD5[0], MD5[1], MD5[2], MD5[3]);
|
|
if (! fileExist(test))
|
|
{
|
|
#ifdef __linux__
|
|
mkdir(test, STORAGE_DIR_MODE);
|
|
#else
|
|
_mkdir(test);
|
|
#endif
|
|
ch_chown(test, "nobody", "nogroup");
|
|
};
|
|
sprintf(Output, "%s/%c%c/%c%c/%s.jpg", m_pBaseSavePath, MD5[0], MD5[1], MD5[2], MD5[3], MD5);
|
|
return true;
|
|
}
|
|
|
|
bool kernelModuleEmbedVideo::registerStream(uint32_t streamID, uint8_t flip_mode)
|
|
{
|
|
// return true;
|
|
try
|
|
{
|
|
m_mStreamHandlersProtector.lock();
|
|
streamHandler * tData = m_mapHANDLERS.at(streamID);
|
|
// If SH allready exists
|
|
if (tData != NULL)
|
|
{
|
|
m_mStreamHandlersProtector.unlock();
|
|
return true;
|
|
};
|
|
// Create New One
|
|
if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) != EVE_OUTPUT_VIDEO)
|
|
{
|
|
tData = new streamHandler(this, MS_MODE_DECODE_FRAMES, flip_mode);
|
|
}
|
|
else
|
|
{
|
|
tData = new streamHandler(this, MS_MODE_ENCODE_FRAMES, flip_mode);
|
|
};
|
|
if (tData != NULL)
|
|
{
|
|
if (tData->create(m_pDBC, streamID))
|
|
{
|
|
m_mapHANDLERS[streamID] = tData;
|
|
m_mStreamHandlersProtector.unlock();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Error Creating new Stream Handler[t]!");
|
|
delete(tData);
|
|
m_mapHANDLERS[streamID] = NULL;
|
|
};
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Error Constructing new Stream Handler[t]!");
|
|
};
|
|
m_mStreamHandlersProtector.unlock();
|
|
}
|
|
catch (const std::out_of_range & oor)
|
|
{
|
|
streamHandler * tSH = NULL;
|
|
// Create New One
|
|
if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) != EVE_OUTPUT_VIDEO)
|
|
{
|
|
tSH = new streamHandler(this, MS_MODE_DECODE_FRAMES, flip_mode);
|
|
}
|
|
else
|
|
{
|
|
tSH = new streamHandler(this, MS_MODE_ENCODE_FRAMES, flip_mode);
|
|
};
|
|
if (tSH != NULL)
|
|
{
|
|
if (tSH->create(m_pDBC, streamID))
|
|
{
|
|
m_mapHANDLERS[streamID] = tSH;
|
|
m_mStreamHandlersProtector.unlock();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Error Creating new Stream Handler[c]!");
|
|
delete(tSH);
|
|
m_mapHANDLERS[streamID] = NULL;
|
|
};
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Error Constructing new Stream Handler[c]!");
|
|
};
|
|
m_mStreamHandlersProtector.unlock();
|
|
};
|
|
return false;
|
|
}
|
|
|
|
bool kernelModuleEmbedVideo::unregisterStream(uint32_t streamID)
|
|
{
|
|
try
|
|
{
|
|
m_mStreamHandlersProtector.lock();
|
|
streamHandler * tData = m_mapHANDLERS.at(streamID);
|
|
if (tData != NULL)
|
|
{
|
|
delete(tData);
|
|
// ToDo: Remove Stream Mats and Clients Connected to a Stream ( or Push them No Signal Image )
|
|
m_mapHANDLERS[streamID] = NULL;
|
|
m_mStreamHandlersProtector.unlock();
|
|
return true;
|
|
};
|
|
m_mStreamHandlersProtector.unlock();
|
|
LOG_ERROR("Registered Stream is Empty!");
|
|
}
|
|
catch (const std::out_of_range & oor)
|
|
{
|
|
m_mStreamHandlersProtector.unlock();
|
|
LOG_ERROR("No Such Stream Registered!");
|
|
};
|
|
return false;
|
|
}
|
|
|
|
#undef TLOG
|
|
#undef TLOG_ERROR
|
|
|
|
#else //LRF_PROXY_ONLY
|
|
|
|
kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) :
|
|
kernelModule(moduleID, KERNEL_MODULE_EMBED_VIDEO)
|
|
{
|
|
|
|
}
|
|
|
|
kernelModuleEmbedVideo::~kernelModuleEmbedVideo()
|
|
{
|
|
|
|
}
|
|
|
|
#endif //LRF_PROXY_ONLY
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
#undef DEBUG_EXTRA
|
|
|