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.
1464 lines
55 KiB
1464 lines
55 KiB
#include "rgooseModuleEmbedVideo.h"
|
|
#include "rgooseHardwareResourceNetworkProvider.h"
|
|
#include "../alg/generic/base64.h"
|
|
|
|
#include <ctype.h>
|
|
#include <err.h>
|
|
#include <errno.h>
|
|
#include <getopt.h>
|
|
#include <ifaddrs.h>
|
|
#include <netdb.h>
|
|
#include <paths.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <signal.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <arpa/inet.h>
|
|
#include <netinet/ip.h>
|
|
#include <netinet/udp.h>
|
|
#include <sys/param.h> /* MIN() */
|
|
#include <sys/socket.h>
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
#define LOG( X ) argusKernelModule::m_pLog->Log( "MOD_EVIDEO", this, X )
|
|
|
|
#ifdef DEBUG
|
|
#define LOG_ERROR( X ) {\
|
|
char _tBfrErrMSG2w34[256];\
|
|
sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
|
|
argusKernelModule::m_pLog->Log( "MOD_EVIDEO", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
|
|
}
|
|
#else
|
|
#define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "MOD_EVIDEO", this, _LOG_ERROR, X );
|
|
#endif
|
|
|
|
#define TLOG( X ) basicLogCollection::getInstance()->Log( "MOD_EVIDEO_T", CTX, X )
|
|
#define TLOG_ERROR( X ) basicLogCollection::getInstance()->Log( "MOD_EVIDEO_T", CTX, _LOG_ERROR, X )
|
|
|
|
enum {
|
|
WEBRTC_REQ_SIGNIN = 0,
|
|
WEBRTC_REQ_WAIT = 1,
|
|
WEBRTC_REQ_SIGNOUT = 2,
|
|
WEBRTC_REQ_MESSAGE = 3
|
|
};
|
|
|
|
static const char* kRequestPaths[] = { "/sign_in", "/wait", "/sign_out", "/message" };
|
|
|
|
//buffer_data bd[1]={0};
|
|
|
|
argusModuleEmbedVideo::argusModuleEmbedVideo( uint32_t moduleID ): argusKernelModule( moduleID, ARGUS_MODULE_EMBED_VIDEO ), basicMediaDestination(), basicThread(){
|
|
m_pEventHeap = basicEventHeap::getInstance();
|
|
};
|
|
|
|
argusModuleEmbedVideo::~argusModuleEmbedVideo(){
|
|
m_bEnabled = false;
|
|
stopService();
|
|
};
|
|
|
|
|
|
static int write_packet(void *opaque, uint8_t *buf, int buf_size) {
|
|
STREAM_CTX *CTX = (STREAM_CTX *)opaque;
|
|
buffer_data *bd = CTX->buffer;
|
|
TLOG("WRD");
|
|
while( buf_size > bd->room ){
|
|
int64_t offset = bd->ptr - bd->buf;
|
|
bd->buf = (uint8_t *) av_realloc_f( bd->buf, 2, bd->size );
|
|
if (!bd->buf)
|
|
return AVERROR(ENOMEM);
|
|
bd->size *= 2;
|
|
bd->ptr = bd->buf + offset;
|
|
bd->room = bd->size - offset;
|
|
}
|
|
char tB[200];
|
|
sprintf( tB, "write packet pkt_size:%d used_buf_size:%zu buf_size:%zu buf_room:%zu\n", buf_size, bd->ptr-bd->buf, bd->size, bd->room );
|
|
TLOG( tB );
|
|
memcpy(bd->ptr, buf, buf_size);
|
|
bd->ptr += buf_size;
|
|
bd->room -= buf_size;
|
|
|
|
//free(buf);
|
|
return buf_size;
|
|
};
|
|
|
|
int argusModuleEmbedVideo::write_module_packet( void *opaque, uint8_t *buf, int buf_size ){
|
|
STREAM_CTX *CTX = (STREAM_CTX *)opaque;
|
|
if( CTX->bHeaderIsReady ){
|
|
uint32_t pSize = buf_size;
|
|
CTX->ModulePtr->m_mHandlersProtector.lock();
|
|
for( vectorHANDLERS::iterator idx = CTX->ModulePtr->m_vHandlersPool.begin();
|
|
idx < CTX->ModulePtr->m_vHandlersPool.end();
|
|
idx++ )
|
|
{
|
|
if( (*idx)->CTX->bValid ) {
|
|
if( (*idx)->CTX->STREAM->mediaStreamId == id ) {
|
|
write( (*idx)->CTX->m_iEncoderPipe[0], &pSize, sizeof(uint32_t));
|
|
};
|
|
};
|
|
};
|
|
CTX->ModulePtr->m_mHandlersProtector.unlock();
|
|
} else {
|
|
CTX->header_size = buf_size;
|
|
CTX->header = (uint8_t*)av_malloc( buf_size );
|
|
if( CTX->header== NULL ) return AVERROR(ENOMEM);
|
|
memcpy( CTX->header, buf, buf_size );
|
|
CTX->bHeaderIsReady = true;
|
|
char tB[200];
|
|
sprintf( tB, "write packet pkt_size:%d", buf_size );
|
|
TLOG( tB );
|
|
};
|
|
return buf_size;
|
|
};
|
|
|
|
int argusModuleEmbedVideo::write_client_packet( void *opaque, uint8_t *buf, int buf_size ){
|
|
STREAM_CTX *CTX = (STREAM_CTX *)opaque;
|
|
if( CTX->bHeaderIsReady ){
|
|
|
|
} else {
|
|
CTX->header_size = buf_size;
|
|
CTX->header = (uint8_t*)av_malloc( buf_size );
|
|
if( CTX->header== NULL ) return AVERROR(ENOMEM);
|
|
memcpy( CTX->header, buf, buf_size );
|
|
CTX->bHeaderIsReady = true;
|
|
char tB[200];
|
|
sprintf( tB, "write packet pkt_size:%d", buf_size );
|
|
TLOG( tB );
|
|
};
|
|
return buf_size;
|
|
};
|
|
|
|
void argusModuleEmbedVideo::encode_module_video( STREAM_CTX *CTX, AVFrame *DATA ){
|
|
int ret;
|
|
if( DATA ) {
|
|
ret = avcodec_send_frame( CTX->video_ctx, DATA );
|
|
} else {
|
|
ret = avcodec_send_frame( CTX->video_ctx, NULL );
|
|
};
|
|
if( ret < 0 ){
|
|
TLOG_ERROR("Error sending a video frame for encoding");
|
|
return;
|
|
};
|
|
while( ret >= 0 ){
|
|
AVPacket *pkt = NULL;
|
|
pkt = (AVPacket *) av_malloc( sizeof(AVPacket) );
|
|
if( pkt ) {
|
|
av_init_packet( pkt );
|
|
ret = avcodec_receive_packet( CTX->video_ctx, pkt );
|
|
if( ret == AVERROR(EAGAIN) || ret == AVERROR_EOF ){
|
|
av_packet_unref( pkt );
|
|
av_free( pkt );
|
|
return;
|
|
} else if( ret < 0 ){
|
|
TLOG_ERROR("Error during video encoding");
|
|
av_free( pkt );
|
|
return;
|
|
};
|
|
pkt->stream_index = CTX->video_stream->index;
|
|
av_packet_rescale_ts( pkt, CTX->video_ctx->time_base, CTX->video_stream->time_base );
|
|
av_write_frame( CTX->ofmt_ctx, pkt );
|
|
av_packet_unref( pkt );
|
|
av_free( pkt );
|
|
}
|
|
};
|
|
};
|
|
|
|
void argusModuleEmbedVideo::encode_client_video( STREAM_CTX *CTX, AVPacket *pkt ){
|
|
if( pkt ) {
|
|
pkt->stream_index = CTX->ts;
|
|
CTX->ts++;
|
|
av_packet_rescale_ts( pkt, CTX->video_ctx->time_base, CTX->video_stream->time_base );
|
|
av_write_frame( CTX->ofmt_ctx, pkt );
|
|
av_packet_unref( pkt );
|
|
};
|
|
};
|
|
|
|
#ifndef _NOAUDIO_
|
|
void argusModuleEmbedVideo::encode_audio( STREAM_CTX *CTX, AVFrame *DATA ){
|
|
int ret;
|
|
if( DATA ) {
|
|
ret = avcodec_send_frame( CTX->audio_ctx, CTX->audio_frame );
|
|
} else {
|
|
ret = avcodec_send_frame( CTX->audio_ctx, NULL );
|
|
};
|
|
if( ret < 0 ){
|
|
TLOG_ERROR("Error sending a audio frame for encoding");
|
|
return;
|
|
};
|
|
while( ret >= 0 ){
|
|
ret = avcodec_receive_packet( CTX->audio_ctx, CTX->pkt );
|
|
if( ret == AVERROR(EAGAIN) || ret == AVERROR_EOF ){
|
|
av_packet_unref( CTX->pkt );
|
|
return;
|
|
} else if( ret < 0 ){
|
|
TLOG_ERROR("Error during audio encoding");
|
|
return;
|
|
};
|
|
CTX->pkt->stream_index = CTX->audio_stream->index;
|
|
av_packet_rescale_ts( CTX->pkt, CTX->audio_ctx->time_base, CTX->audio_stream->time_base);
|
|
av_write_frame( CTX->ofmt_ctx, CTX->pkt );
|
|
av_packet_unref( CTX->pkt );
|
|
};
|
|
};
|
|
#endif
|
|
|
|
void argusModuleEmbedVideo::add_video_frame( STREAM_CTX *CTX ){
|
|
if( CTX->InputMat ){
|
|
if( CTX->video_sws_ctx ) {
|
|
int linesize[2] = {0};
|
|
const uint8_t * dataPTR[2] = {0};
|
|
|
|
linesize[0] = CTX->InputMat->cols*3;
|
|
linesize[1] = 0;
|
|
|
|
dataPTR[0] = (uint8_t *)CTX->InputMat->data;
|
|
dataPTR[1] = NULL;
|
|
|
|
av_frame_make_writable( CTX->video_frame );
|
|
|
|
sws_scale( CTX->video_sws_ctx,
|
|
dataPTR,
|
|
linesize,
|
|
0,
|
|
CTX->InputMat->rows,
|
|
CTX->video_frame->data,
|
|
CTX->video_frame->linesize
|
|
);
|
|
CTX->video_frame->pts = CTX->ts;
|
|
CTX->ts++;
|
|
encode_module_video( CTX, CTX->video_frame );
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
bool argusModuleEmbedVideo::write_header( STREAM_CTX *CTX ) {
|
|
int ret = avformat_write_header( CTX->ofmt_ctx, NULL);
|
|
if (ret < 0) {
|
|
//printf(stderr, "Error occurred: %s\n", av_err2str(ret));
|
|
TLOG_ERROR("Error occurred when opening output file");
|
|
return false;
|
|
};
|
|
return CTX->bHeaderIsReady = true;
|
|
};
|
|
|
|
void argusModuleEmbedVideo::cleanSTREAM_CTX( STREAM_CTX *&CTX ){
|
|
if( CTX!= NULL ) {
|
|
/*video*/
|
|
if( CTX->video_stream ){
|
|
|
|
};
|
|
if( CTX->video_frame ){
|
|
};
|
|
if( CTX->video_ctx ) {
|
|
avcodec_close( CTX->video_ctx );
|
|
avcodec_free_context( &CTX->video_ctx );
|
|
};
|
|
if( CTX->video_codec ){
|
|
av_freep( CTX->video_codec );
|
|
};
|
|
/*audio*/
|
|
|
|
/*container*/
|
|
if( CTX->ofmt_ctx ){
|
|
if( CTX->ofmt_ctx->oformat ) av_freep( CTX->ofmt_ctx->oformat );
|
|
avformat_free_context( CTX->ofmt_ctx );
|
|
};
|
|
if( CTX->avio_ctx ){
|
|
av_freep(&CTX->avio_ctx->buffer);
|
|
av_freep(CTX->avio_ctx);
|
|
};
|
|
if( CTX->avio_ctx_buffer ) av_freep( CTX->avio_ctx_buffer );
|
|
/*output*/
|
|
if( CTX->buffer ) {
|
|
if( CTX->buffer->buf ) av_free( CTX->buffer->buf );
|
|
av_free(CTX->buffer);
|
|
};
|
|
av_freep( CTX );
|
|
};
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::open_client_video( STREAM_CTX *&CTX, int w, int h, int fps, int br, int codec_idx, int format_idx ){
|
|
const char* formats[] = {"webm", "mp4", "mp3", "aac", "ogg", "mjpeg", "flv" };
|
|
const char* codecs[] = { "libvpx", "libx264", "mp4", "mjpeg", "flv" };
|
|
if( CTX == NULL ) {
|
|
CTX = (STREAM_CTX *) av_mallocz(sizeof(STREAM_CTX));
|
|
};
|
|
|
|
if( CTX!= NULL ) {
|
|
if( CTX->have_video ) return false;
|
|
TLOG("Creating Video Stream");
|
|
AVOutputFormat* of = av_guess_format(formats[format_idx], 0, 0);
|
|
if( of == NULL ) {
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
TLOG("Format Guess OK");
|
|
CTX->buffer = (buffer_data *) av_mallocz(sizeof(buffer_data));
|
|
if( CTX->buffer == NULL ) {
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
TLOG("Buffer OK");
|
|
CTX->buffer->room = CTX->buffer->size = BD_BUFFER_SIZE;
|
|
CTX->buffer->ptr = CTX->buffer->buf = (uint8_t *) av_malloc( CTX->buffer->size );
|
|
if( !CTX->buffer->buf ) {
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("Buffer OK");
|
|
CTX->avio_ctx_buffer_size = AVIO_BUFFER_SIZE;
|
|
CTX->avio_ctx_buffer = (uint8_t *) av_malloc(CTX->avio_ctx_buffer_size);
|
|
if( !CTX->avio_ctx_buffer ){
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("AVIO Buffer OK");
|
|
CTX->avio_ctx = avio_alloc_context(
|
|
CTX->avio_ctx_buffer,
|
|
CTX->avio_ctx_buffer_size,
|
|
1,
|
|
&CTX,
|
|
NULL,
|
|
&write_client_packet,
|
|
NULL //&seek
|
|
);
|
|
if( !CTX->avio_ctx ){
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
}
|
|
TLOG("AVIO CTX OK");
|
|
if( avformat_alloc_output_context2(&CTX->ofmt_ctx, of, NULL, NULL) < 0 ){
|
|
TLOG_ERROR( "Could not create output context" );
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("AVFormat CTX OK");
|
|
CTX->video_codec = avcodec_find_encoder_by_name(codecs[codec_idx]);
|
|
if( !CTX->video_codec ){
|
|
char tB[100];
|
|
sprintf( tB,"Codec '%s' not found", codecs[codec_idx] );
|
|
TLOG_ERROR( tB );
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("Codec OK");
|
|
CTX->video_ctx = avcodec_alloc_context3( CTX->video_codec );
|
|
if( !CTX->video_ctx ) {
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("Codec CTX OK");
|
|
CTX->video_ctx->width = w;
|
|
CTX->video_ctx->height = h;
|
|
CTX->video_ctx->time_base.num = 1;
|
|
CTX->video_ctx->time_base.den = fps;
|
|
CTX->video_ctx->bit_rate = br;
|
|
CTX->video_ctx->gop_size = 10;
|
|
if( codec_idx == 3 ) {
|
|
CTX->video_ctx->pix_fmt = AV_PIX_FMT_YUVJ422P;
|
|
} else {
|
|
CTX->video_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
|
|
CTX->video_ctx->max_b_frames = 1;
|
|
};
|
|
if( avcodec_open2( CTX->video_ctx, CTX->video_codec, NULL ) < 0 ){
|
|
TLOG_ERROR("couldnt open codec");
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("Codec Open OK");
|
|
CTX->video_stream = avformat_new_stream( CTX->ofmt_ctx, NULL);
|
|
if( !CTX->video_stream ){
|
|
TLOG_ERROR("error making stream");
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("AVStream OK");
|
|
CTX->ofmt_ctx->pb = CTX->avio_ctx;
|
|
CTX->ofmt_ctx->flags |= AVFMT_FLAG_CUSTOM_IO;
|
|
|
|
CTX->ofmt_ctx->oformat = of;
|
|
CTX->video_stream->codec->codec_tag = 0;
|
|
|
|
CTX->video_stream->time_base = CTX->video_ctx->time_base;
|
|
CTX->video_stream->id = CTX->ofmt_ctx->nb_streams-1;
|
|
|
|
avcodec_parameters_from_context( CTX->video_stream->codecpar, CTX->video_ctx );
|
|
|
|
CTX->frame_idx = 0;
|
|
CTX->have_video = true;
|
|
|
|
TLOG("STREAM OK");
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::open_module_video( STREAM_CTX *&CTX, int w, int h, int fps, int br, int preset_idx, int codec_idx, int format_idx ){
|
|
const char* formats[] = {"webm", "mp4", "mp3", "aac", "ogg", "mjpeg", "flv" };
|
|
const char* codecs[] = { "libvpx", "libx264", "mp4", "mjpeg", "flv" };
|
|
const char *presets[] = { "ultrafast", "veryfast", "fast", "medium", "slow", "veryslow" };
|
|
if( CTX == NULL ) {
|
|
CTX = (STREAM_CTX *) av_mallocz( sizeof(STREAM_CTX));
|
|
};
|
|
|
|
if( CTX!= NULL ) {
|
|
|
|
if( CTX->have_video ) return false;
|
|
TLOG("Creating Video Stream");
|
|
AVOutputFormat* of = av_guess_format(formats[format_idx], 0, 0);
|
|
if( of == NULL ) {
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
TLOG("Format Guess OK");
|
|
CTX->buffer = (buffer_data *) av_mallocz(sizeof(buffer_data));
|
|
if( CTX->buffer == NULL ) {
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
TLOG("Buffer OK");
|
|
CTX->buffer->room = CTX->buffer->size = BD_BUFFER_SIZE;
|
|
CTX->buffer->ptr = CTX->buffer->buf = (uint8_t *) av_malloc( CTX->buffer->size );
|
|
if( !CTX->buffer->buf ) {
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("Buffer OK");
|
|
CTX->avio_ctx_buffer_size = AVIO_BUFFER_SIZE;
|
|
CTX->avio_ctx_buffer = (uint8_t *) av_malloc(CTX->avio_ctx_buffer_size);
|
|
if( !CTX->avio_ctx_buffer ){
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("AVIO Buffer OK");
|
|
CTX->avio_ctx = avio_alloc_context(
|
|
CTX->avio_ctx_buffer,
|
|
CTX->avio_ctx_buffer_size,
|
|
1,
|
|
&CTX,
|
|
NULL,
|
|
&write_module_packet,
|
|
NULL //&seek
|
|
);
|
|
if( !CTX->avio_ctx ){
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
}
|
|
TLOG("AVIO CTX OK");
|
|
if( avformat_alloc_output_context2(&CTX->ofmt_ctx, of, NULL, NULL) < 0 ){
|
|
TLOG_ERROR( "Could not create output context" );
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("AVFormat CTX OK");
|
|
CTX->video_codec = avcodec_find_encoder_by_name(codecs[codec_idx]);
|
|
if( !CTX->video_codec ){
|
|
char tB[100];
|
|
sprintf( tB,"Codec '%s' not found", codecs[codec_idx] );
|
|
TLOG_ERROR( tB );
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("Codec OK");
|
|
CTX->video_ctx = avcodec_alloc_context3( CTX->video_codec );
|
|
if( !CTX->video_ctx ) {
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("Codec CTX OK");
|
|
CTX->video_ctx->width = w;
|
|
CTX->video_ctx->height = h;
|
|
CTX->video_ctx->time_base.num = 1;
|
|
CTX->video_ctx->time_base.den = fps;
|
|
CTX->video_ctx->bit_rate = br;
|
|
CTX->video_ctx->gop_size = 10;
|
|
if( codec_idx == 3 ) {
|
|
CTX->video_ctx->pix_fmt = AV_PIX_FMT_YUVJ422P;
|
|
} else {
|
|
CTX->video_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
|
|
CTX->video_ctx->max_b_frames = 1;
|
|
};
|
|
|
|
av_opt_set( CTX->video_ctx->priv_data, "preset", presets[preset_idx], 0 );
|
|
if( avcodec_open2( CTX->video_ctx, CTX->video_codec, NULL ) < 0 ){
|
|
TLOG_ERROR("couldnt open codec");
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("Codec Open OK");
|
|
|
|
// Frame initalization
|
|
CTX->video_frame = av_frame_alloc();
|
|
if( !CTX->video_frame ) {
|
|
TLOG_ERROR("couldnt alloc frame");
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("Video Frame OK");
|
|
CTX->video_frame->format = CTX->video_ctx->pix_fmt;
|
|
CTX->video_frame->width = w;
|
|
CTX->video_frame->height = h;
|
|
av_frame_get_buffer( CTX->video_frame, 0 );
|
|
|
|
CTX->video_stream = avformat_new_stream( CTX->ofmt_ctx, NULL);
|
|
if( !CTX->video_stream ){
|
|
TLOG_ERROR("error making stream");
|
|
cleanSTREAM_CTX( CTX );
|
|
return NULL;
|
|
};
|
|
TLOG("AVStream OK");
|
|
CTX->ofmt_ctx->pb = CTX->avio_ctx;
|
|
CTX->ofmt_ctx->flags |= AVFMT_FLAG_CUSTOM_IO;
|
|
|
|
CTX->ofmt_ctx->oformat = of;
|
|
CTX->video_stream->codec->codec_tag = 0;
|
|
|
|
CTX->video_stream->time_base = CTX->video_ctx->time_base;
|
|
CTX->video_stream->id = CTX->ofmt_ctx->nb_streams-1;
|
|
|
|
avcodec_parameters_from_context( CTX->video_stream->codecpar, CTX->video_ctx );
|
|
|
|
CTX->frame_idx = 0;
|
|
CTX->have_video = 1;
|
|
|
|
TLOG("STREAM OK");
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
uint8_t *argusModuleEmbedVideo::close_stream( STREAM_CTX *CTX, int* size ){
|
|
if( CTX->have_video ) encode_module_video( CTX, NULL );
|
|
#ifndef _NOAUDIO_
|
|
if( CTX->have_audio ) encode_audio( CTX, NULL );
|
|
#endif
|
|
|
|
av_write_trailer( CTX->ofmt_ctx );
|
|
/*
|
|
avformat_free_context(ofmt_ctx);
|
|
|
|
if (have_video) {
|
|
avcodec_free_context(&video_ctx);
|
|
av_frame_free(&video_frame);
|
|
}
|
|
|
|
if (have_audio) {
|
|
avcodec_free_context(&audio_ctx);
|
|
av_frame_free(&audio_frame);
|
|
swr_free(&audio_swr_ctx);
|
|
free(audio_buffer_right);
|
|
free(audio_buffer_left);
|
|
}
|
|
|
|
av_freep(&avio_ctx->buffer);
|
|
av_free(avio_ctx);
|
|
*/
|
|
*size = CTX->buffer->size - CTX->buffer->room;
|
|
return CTX->buffer->buf;
|
|
};
|
|
|
|
//void free_buffer(){
|
|
// av_free(bd.buf);
|
|
//}
|
|
#ifndef _NOAUDIO_
|
|
bool argusModuleEmbedVideo::alloc_audio_frame( STREAM_CTX *CTX ){
|
|
if( CTX->audio_frame ){
|
|
av_freep( CTX->audio_frame );
|
|
};
|
|
CTX->audio_frame = av_frame_alloc();
|
|
if( !CTX->audio_frame ){
|
|
TLOG_ERROR("Error allocating an audio frame");
|
|
return false;
|
|
};
|
|
CTX->audio_frame->format = CTX->audio_ctx->sample_fmt;
|
|
CTX->audio_frame->channel_layout = CTX->audio_ctx->channel_layout;
|
|
CTX->audio_frame->sample_rate = CTX->audio_ctx->sample_rate;
|
|
CTX->audio_frame->nb_samples = CTX->audio_ctx->frame_size;
|
|
if( av_frame_get_buffer( CTX->audio_frame, 4 ) < 0 ){
|
|
TLOG_ERROR("Error allocating an audio buffer");
|
|
return false;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
int argusModuleEmbedVideo::check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt){
|
|
const enum AVSampleFormat *p = codec->sample_fmts;
|
|
while (*p != AV_SAMPLE_FMT_NONE) {
|
|
if (*p == sample_fmt)
|
|
return 1;
|
|
p++;
|
|
}
|
|
return 0;
|
|
};
|
|
|
|
void argusModuleEmbedVideo::add_audio_frame( STREAM_CTX *CTX, float* left, float* right, int size ){
|
|
if( size + CTX->audio_buffer_size > CTX->max_audio_buffer_size ){
|
|
CTX->audio_buffer_left = (float *)realloc(CTX->audio_buffer_left, (size + CTX->audio_buffer_size) * sizeof(float));
|
|
CTX->audio_buffer_right = (float *)realloc(CTX->audio_buffer_right, (size + CTX->audio_buffer_size) * sizeof(float));
|
|
}
|
|
|
|
memcpy( CTX->audio_buffer_left + CTX->audio_buffer_size, left, size * sizeof(float) );
|
|
memcpy( CTX->audio_buffer_right + CTX->audio_buffer_size, right, size * sizeof(float) );
|
|
CTX->audio_buffer_size += size;
|
|
int count = 0, ret;
|
|
|
|
while( CTX->audio_buffer_size > CTX->audio_frame->nb_samples ){
|
|
if( av_frame_make_writable( CTX->audio_frame ) < 0){
|
|
TLOG_ERROR("Error: add audio frame!");
|
|
return;
|
|
};
|
|
|
|
CTX->audio_frame->data[0] = (uint8_t *) CTX->audio_buffer_left;
|
|
CTX->audio_frame->data[1] = (uint8_t *) CTX->audio_buffer_right;
|
|
|
|
CTX->dst_nb_samples = av_rescale_rnd (
|
|
swr_get_delay( CTX->audio_swr_ctx, CTX->audio_ctx->sample_rate ) + CTX->audio_frame->nb_samples,
|
|
CTX->src_sample_rate,
|
|
CTX->audio_ctx->sample_rate,
|
|
AV_ROUND_UP
|
|
);
|
|
|
|
if( swr_convert (
|
|
CTX->audio_swr_ctx,
|
|
CTX->audio_frame->data,
|
|
CTX->dst_nb_samples,
|
|
(const uint8_t **)CTX->audio_frame->data,
|
|
CTX->audio_frame->nb_samples
|
|
)< 0){
|
|
TLOG_ERROR("error converting \n");
|
|
return;
|
|
};
|
|
|
|
CTX->audio_frame->pts = av_rescale_q(
|
|
CTX->audio_frame_bytes,
|
|
(AVRational){ 1, CTX->audio_ctx->sample_rate },
|
|
CTX->audio_ctx->time_base
|
|
);
|
|
|
|
CTX->audio_frame_bytes += CTX->dst_nb_samples;
|
|
encode_audio( CTX, CTX->audio_frame );
|
|
//Shift array buffer
|
|
memcpy( CTX->audio_buffer_left, CTX->audio_buffer_left + CTX->audio_frame->nb_samples, CTX->audio_buffer_size*sizeof(float));
|
|
memcpy( CTX->audio_buffer_right, CTX->audio_buffer_right + CTX->audio_frame->nb_samples, CTX->audio_buffer_size*sizeof(float));
|
|
CTX->audio_buffer_size -= CTX->audio_frame->nb_samples;
|
|
};
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::open_audio( STREAM_CTX *&CTX, int sample_rate, int nr_channels, int bit_rate, int codec_idx ){
|
|
const AVCodecID codecs[] = { AV_CODEC_ID_OPUS, AV_CODEC_ID_AAC, AV_CODEC_ID_MP3 };
|
|
if( CTX == NULL ) {
|
|
CTX = (STREAM_CTX *) av_mallocz( sizeof(STREAM_CTX));
|
|
};
|
|
if( CTX!= NULL ) {
|
|
if( CTX->have_audio ) return false;
|
|
CTX->src_sample_rate = sample_rate;
|
|
CTX->src_bit_rate = bit_rate;
|
|
CTX->src_nr_channels = nr_channels;
|
|
|
|
|
|
|
|
CTX->audio_codec = avcodec_find_encoder(codecs[codec_idx]);
|
|
if( !CTX->audio_codec ){
|
|
TLOG_ERROR("error making audio codec context");
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
|
|
CTX->audio_stream = avformat_new_stream( CTX->ofmt_ctx, NULL);
|
|
CTX->audio_stream->id = CTX->ofmt_ctx->nb_streams-1;
|
|
CTX->audio_ctx = avcodec_alloc_context3( CTX->audio_codec );
|
|
CTX->audio_ctx->bit_rate = bit_rate;
|
|
CTX->audio_ctx->sample_rate = sample_rate;
|
|
if( CTX->audio_codec->supported_samplerates ){
|
|
CTX->audio_ctx->sample_rate = CTX->audio_codec->supported_samplerates[0];
|
|
for( int i = 0; CTX->audio_codec->supported_samplerates[i]; i++ ){
|
|
if( CTX->audio_codec->supported_samplerates[i] == sample_rate ) CTX->audio_ctx->sample_rate = sample_rate;
|
|
};
|
|
};
|
|
CTX->audio_ctx->channels = nr_channels;
|
|
CTX->audio_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
|
|
if( CTX->audio_codec->channel_layouts ){
|
|
CTX->audio_ctx->channel_layout = CTX->audio_codec->channel_layouts[0];
|
|
for( int i = 0; CTX->audio_codec->channel_layouts[i]; i++ ){
|
|
if( CTX->audio_codec->channel_layouts[i] == AV_CH_LAYOUT_STEREO ) CTX->audio_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
|
|
};
|
|
};
|
|
|
|
CTX->audio_ctx->channels = av_get_channel_layout_nb_channels( CTX->audio_ctx->channel_layout );
|
|
CTX->audio_ctx->sample_fmt = CTX->audio_codec->sample_fmts[0];
|
|
CTX->audio_stream->time_base = (AVRational){1, CTX->audio_ctx->sample_rate};
|
|
|
|
if( avcodec_open2( CTX->audio_ctx, CTX->audio_codec, NULL ) < 0 ){
|
|
TLOG_ERROR("Could not open audio codec" );
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
|
|
if(!alloc_audio_frame( CTX )){
|
|
TLOG_ERROR("Could not alloc audio frame" );
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
|
|
if( avcodec_parameters_from_context( CTX->audio_stream->codecpar, CTX->audio_ctx ) < 0 ){
|
|
TLOG_ERROR("Could not copy the stream parameters");
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
|
|
CTX->audio_swr_ctx = swr_alloc();
|
|
if( !CTX->audio_swr_ctx ){
|
|
TLOG_ERROR("Could not allocate resampler context");
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
|
|
av_opt_set_int (CTX->audio_swr_ctx, "in_channel_count", nr_channels, 0);
|
|
av_opt_set_int (CTX->audio_swr_ctx, "in_sample_rate", sample_rate, 0);
|
|
av_opt_set_sample_fmt(CTX->audio_swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
|
|
av_opt_set_int (CTX->audio_swr_ctx, "out_channel_count", CTX->audio_ctx->channels, 0);
|
|
av_opt_set_int (CTX->audio_swr_ctx, "out_sample_rate", CTX->audio_ctx->sample_rate, 0);
|
|
av_opt_set_sample_fmt(CTX->audio_swr_ctx, "out_sample_fmt", CTX->audio_ctx->sample_fmt, 0);
|
|
|
|
if( swr_init( CTX->audio_swr_ctx ) < 0 ){
|
|
TLOG_ERROR("Failed to initialize the resampling context");
|
|
return false;
|
|
};
|
|
|
|
if( !CTX->pkt ) CTX->pkt = av_packet_alloc();
|
|
|
|
if( !CTX->pkt ){
|
|
return false;
|
|
};
|
|
|
|
CTX->audio_buffer_size = CTX->audio_frame_bytes = 0;
|
|
CTX->audio_buffer_left = (float *)malloc(sample_rate * sizeof(float));
|
|
CTX->audio_buffer_right = (float *)malloc(sample_rate * sizeof(float));
|
|
CTX->max_audio_buffer_size = sample_rate;
|
|
|
|
CTX->have_audio = 1;
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
#endif // _NOAUDIO_
|
|
|
|
|
|
static int IOWriteFunc( void *opaque, uint8_t *buf, int buf_size ){
|
|
WEB_CLIENT_CTX *CTX = (WEB_CLIENT_CTX *)opaque;
|
|
TLOG("-chunk-");
|
|
if( CTX ) {
|
|
if( CTX->m_iSocketFID > -1 ){
|
|
char mesg[128];
|
|
sprintf( mesg, "%x\r\n", buf_size );
|
|
if( send( CTX->m_iSocketFID, mesg, strlen(mesg), 0 ) < 0 ){
|
|
sprintf( mesg, "Failed sending 1 to client: %s", strerror(errno));
|
|
TLOG_ERROR(mesg);
|
|
CTX->bValid = false;
|
|
return -1;
|
|
};
|
|
if( send( CTX->m_iSocketFID, buf, buf_size, 0 ) < 0 ){
|
|
sprintf( mesg, "Failed sending 1 to client: %s", strerror(errno));
|
|
TLOG_ERROR(mesg);
|
|
CTX->bValid = false;
|
|
return -1;
|
|
};
|
|
if( send( CTX->m_iSocketFID, "\r\n", 2, 0 ) < 0 ){
|
|
sprintf( mesg, "Failed sending 1 to client: %s", strerror(errno));
|
|
TLOG_ERROR(mesg);
|
|
CTX->bValid = false;
|
|
return -1;
|
|
};
|
|
return buf_size;
|
|
};
|
|
};
|
|
return -1;
|
|
};
|
|
|
|
void *argusModuleEmbedVideo::webClientProc( void *ptr ){
|
|
static char *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"
|
|
"MIME-version: 1.0\r\n"
|
|
"Connection: close\r\n"
|
|
"Content-Type: video/mp4\r\n"
|
|
"Content-Transfer-Encoding: BINARY\r\n"
|
|
"Transfer-Encoding: chunked\r\n"
|
|
"\r\n";
|
|
WEB_CLIENT_CTX * CTX = (WEB_CLIENT_CTX *) ptr;
|
|
argusModuleEmbedVideo * MODULE = CTX->ModulePtr;
|
|
STREAM_CTX * STREAM = 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
|
|
|
|
pfd->fd = CTX->m_iSocketFID;
|
|
pfd->events = POLLIN | POLLHUP;
|
|
|
|
while( CTX->bValid ) {
|
|
if( !CTX->initDone ){
|
|
int RetVal;
|
|
char * reqline[256];
|
|
RetVal = poll( pfd, 1, 1000 );
|
|
if( RetVal <= 0 ){
|
|
if ( EINTR == errno ) continue;
|
|
TLOG_ERROR("Unrecoverable error");
|
|
CTX->bValid = 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 );
|
|
CTX->bValid = false;
|
|
continue;
|
|
};
|
|
//TLOG( mesg );
|
|
|
|
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);
|
|
};
|
|
CTX->bValid = false;
|
|
continue;
|
|
};
|
|
};
|
|
TLOG( cGetURI );
|
|
};
|
|
// parse rest of request
|
|
while( ( reqline[++index] = strtok( NULL, "\n" ) )!= NULL ) {
|
|
if( strncmp( reqline[index], "Connection:", 11 ) == 0 ) {
|
|
char * pPart;
|
|
pPart = strcasestr( reqline[index], "Upgrade" );
|
|
if( pPart != NULL ) {
|
|
TLOG("Upgrade Mode ON");
|
|
//bUpgradeMode = true;
|
|
};
|
|
};
|
|
if( strncmp( reqline[index], "Upgrade:", 8 ) == 0 ) {
|
|
/*
|
|
if( bUpgradeMode ) {
|
|
char * pPart;
|
|
pPart = strcasestr( reqline[index], "websocket" );
|
|
if( pPart != NULL ) {
|
|
TLOG("WebSocket Mode ON");
|
|
//bWebSocketMode = true;
|
|
};
|
|
};
|
|
*/
|
|
};
|
|
if( strncmp( reqline[index], "Sec-WebSocket-Version:", 22 ) == 0 ) {
|
|
char * pPart;
|
|
pPart = strstr( reqline[index], " " );
|
|
if( pPart != NULL ) {
|
|
char tBuf[CTEXT];
|
|
//nWebSocketVersion = atoi(pPart+1);
|
|
sprintf( tBuf,"Web Socket Version: %d", 0 );//nWebSocketVersion );
|
|
TLOG( tBuf );
|
|
};
|
|
};
|
|
if( strncmp( reqline[index], "Sec-WebSocket-Key:", 18 ) == 0 ) {
|
|
/*char * pPart;
|
|
pPart = strstr( reqline[index], " " );
|
|
if( pPart != NULL ) {
|
|
char tGenStr[256];
|
|
unsigned char * tBase64;
|
|
size_t nBase64;
|
|
int kLen;
|
|
unsigned char hash[SHA_DIGEST_LENGTH]; // == 20
|
|
pPart++;
|
|
kLen = strstr(pPart,"\r") - pPart;
|
|
strncpy(cWSKey,pPart,kLen);
|
|
cWSKey[kLen]=(char)0;
|
|
TLOG( cWSKey );
|
|
sprintf( tGenStr, "%s%s", cWSKey, ws_guid );
|
|
SHA1( (const unsigned char*)tGenStr, strlen(tGenStr), hash );
|
|
tBase64 = base64_encode( hash, SHA_DIGEST_LENGTH, &nBase64 );
|
|
strncpy( cWSRep, (const char*)tBase64, nBase64 );
|
|
free( tBase64 );
|
|
cWSRep[nBase64] = (char)0;
|
|
TLOG( cWSRep );
|
|
};
|
|
*/
|
|
};
|
|
if( strncmp( reqline[index], "Sec-WebSocket-Protocol:", 23 ) == 0 ) {
|
|
char * pPart;
|
|
pPart = strstr( reqline[index], " " );
|
|
if( pPart != NULL ) {
|
|
// Check for protos
|
|
};
|
|
};
|
|
if( strncmp( reqline[index], "Sec-WebSocket-Extensions:", 25 ) == 0 ) {
|
|
char * pPart;
|
|
pPart = strstr( reqline[index], " " );
|
|
if( pPart != NULL ) {
|
|
pPart++;
|
|
if(strncmp( pPart, "permessage-deflate", 18 ) == 0 ) {
|
|
m_bDeflatePerMessage = true;
|
|
TLOG("Using Deflate");
|
|
};
|
|
};
|
|
};
|
|
if( strncmp( reqline[index],"Accept-Encoding:", 16 ) == 0 ){
|
|
char * pPart;
|
|
pPart = strstr( reqline[index], " " );
|
|
if( pPart != NULL ) {
|
|
pPart++;
|
|
if(strncmp( pPart, "gzip, deflate", 13 ) == 0 ) {
|
|
m_bDeflatePerMessage = true;
|
|
TLOG("Using Deflate");
|
|
};
|
|
};
|
|
};
|
|
};
|
|
// Send Header
|
|
if( send( CTX->m_iSocketFID, head, strlen(head), 0 ) < 0 ){
|
|
sprintf( mesg, "Failed sending file to client: %s", strerror(errno));
|
|
TLOG_ERROR(mesg);
|
|
CTX->bValid = false;
|
|
};
|
|
|
|
if( CTX->ModulePtr->registerForMediaStream( 0, CTX ) ) {
|
|
if( !CTX->ModulePtr->open_client_video(
|
|
CTX->STREAM,
|
|
VIDEO_OUTPUT_WIDTH,
|
|
VIDEO_OUTPUT_HEIGHT,
|
|
VIDEO_OUTPUT_FPS,
|
|
VIDEO_OUTPUT_BITRATE,
|
|
VC_MJPEG,
|
|
VF_MJPEG
|
|
) ){
|
|
TLOG_ERROR("Cannot ctreate VIDEO STREAM!");
|
|
CTX->bValid = false;
|
|
break;
|
|
};
|
|
TLOG("Header");
|
|
CTX->ModulePtr->write_header( STREAM );
|
|
TLOG("After Header");
|
|
CTX->initDone = true;
|
|
};
|
|
continue;
|
|
} else {
|
|
uint32_t msgSize;
|
|
int retVal;
|
|
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;
|
|
break;
|
|
};
|
|
if( CTX->pkt == NULL ){
|
|
CTX->pkt = (AVPacket *) av_mallocz(sizeof(AVPacket));
|
|
} else {
|
|
// Data cleared in Encoder procedure
|
|
//av_freep( CTX->pkt->data );
|
|
//av_packet_unref( CTX->pkt );
|
|
};
|
|
retVal = read( CTX->m_iEncoderPipe[0], CTX->pkt, sizeof(AVPacket));
|
|
if( retVal == -1 ){
|
|
if( errno == EAGAIN || errno == EWOULDBLOCK ){
|
|
continue;
|
|
};
|
|
TLOG_ERROR("Pipe Communication Error[2]!");
|
|
CTX->bValid = false;
|
|
break;
|
|
};
|
|
CTX->pkt->data = (uint8_t *)av_malloc( CTX->pkt->size );
|
|
if( CTX->pkt->data ){
|
|
retVal = read( CTX->m_iEncoderPipe[0], CTX->pkt->data, CTX->pkt->size );
|
|
if( retVal == -1 ){
|
|
if( errno == EAGAIN || errno == EWOULDBLOCK ){
|
|
continue;
|
|
};
|
|
TLOG_ERROR("Pipe Communication Error[3]!");
|
|
CTX->bValid = false;
|
|
break;
|
|
};
|
|
CTX->ModulePtr->encode_client_video( CTX->STREAM, CTX->pkt );
|
|
} else {
|
|
TLOG_ERROR("Pack Memory Allocation Error!");
|
|
CTX->bValid = false;
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
TLOG("Client Handler Ended!");
|
|
shutdown( CTX->m_iSocketFID, SHUT_RDWR);
|
|
close( CTX->m_iSocketFID );
|
|
CTX->bValid = false;
|
|
//pthread_exit( NULL );
|
|
return NULL;
|
|
};
|
|
|
|
void argusModuleEmbedVideo::destroy( WEB_CLIENT_CTX *&ptr ){
|
|
if( ptr != NULL ){
|
|
if( ptr->pkt ){
|
|
av_packet_unref( ptr->pkt );
|
|
av_freep( ptr->pkt );
|
|
};
|
|
//if( ptr->StreamBuffer ) av_freep( ptr->StreamBuffer );
|
|
//if( ptr->outputFrame ) av_freep( ptr->outputFrame );
|
|
free( ptr );
|
|
ptr = NULL;
|
|
};
|
|
};
|
|
|
|
void argusModuleEmbedVideo::m_fThreadProc( void ){
|
|
if( !m_bEnabled ) {
|
|
#ifdef DEBUG
|
|
LOG("Module is Disabled!");
|
|
#endif
|
|
return;
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("Module Started!");
|
|
#endif
|
|
struct pollfd pfd[1];
|
|
pfd->fd = m_nSocketDescriptor;
|
|
pfd->events = POLLIN | POLLHUP;
|
|
m_bThreadStatus = TH_WORKING;
|
|
while( m_bThreadStatus < TH_ENDED ) {
|
|
if( !m_bEnabled ) {
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
int RetVal = poll( pfd, 1, 1000 );
|
|
if( RetVal < 0 ){
|
|
if( EINTR == errno ) continue;
|
|
LOG_ERROR("Unrecoverable error");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
// Handle Connections
|
|
if( RetVal != 0 ){
|
|
if ( pfd[0].revents & POLLIN ) {
|
|
LOG("Handle New Client");
|
|
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 {
|
|
LOG("Accept OK");
|
|
WEB_CLIENT_CTX *tCTX = (WEB_CLIENT_CTX *) malloc(sizeof(WEB_CLIENT_CTX));
|
|
if( tCTX ) {
|
|
LOG("CTX OK");
|
|
WEB_HANDLER *tHANDLER = (WEB_HANDLER *) malloc(sizeof(WEB_HANDLER));
|
|
if( tHANDLER ) {
|
|
LOG("HANDLER OK");
|
|
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->bValid = true;
|
|
tCTX->ModulePtr = this;
|
|
LOG("Before Thread Create");
|
|
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;
|
|
LOG("After Thread Create");
|
|
m_vHandlersPool.push_back( tHANDLER );
|
|
LOG("Handler Pushed to a Pool");
|
|
};
|
|
} else {
|
|
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
|
|
if( m_vHandlersPool.size() ) {
|
|
for( vectorHANDLERS::iterator idx = m_vHandlersPool.begin(); idx < m_vHandlersPool.end(); idx++ ) {
|
|
if( !( (*idx)->CTX->bValid ) ) {
|
|
#ifdef DEBUG
|
|
LOG("Web Handler Ripped!");
|
|
#endif
|
|
pthread_join( (*idx)->m_hThreadHandle, NULL );
|
|
destroy( (*idx)->CTX );
|
|
free( (*idx) );
|
|
m_vHandlersPool.erase(idx);
|
|
};
|
|
};
|
|
};
|
|
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 argusModuleEmbedVideo::startService(){
|
|
LOG("Create");
|
|
//if( m_nListenPort == 0 && ListenPort == 0 ) return false;
|
|
//m_nListenPort = ListenPort;
|
|
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( 8972 );
|
|
|
|
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;
|
|
} else {
|
|
LOG("Bind Socessfull");
|
|
};
|
|
if( listen( m_nSocketDescriptor, 10) != 0 ){
|
|
LOG_ERROR("Failed setting web listen backlog");
|
|
close( m_nSocketDescriptor );
|
|
return false;
|
|
} else {
|
|
LOG("Listen OK");
|
|
};
|
|
createStream( 0 );
|
|
return init();
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::stopService(){
|
|
waitThreadEnd( true );
|
|
/*
|
|
if( m_vThreads.size() ) { // Kill-Wait Started Probes
|
|
#ifdef DEBUG
|
|
LOG("Have to Wait Probes!");
|
|
#endif
|
|
for( vectorOBSERVER_CTX::iterator index = m_vThreads.begin(); index < m_vThreads.end(); index++ ) {
|
|
pthread_join( (*index)->m_hThreadHandle, NULL );
|
|
};
|
|
m_vThreads.clear();
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("No More Probes!");
|
|
#endif
|
|
if (m_vResultPool.size()){ // Clear structures
|
|
for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
|
|
free(*index);
|
|
};
|
|
m_vResultPool.clear();
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("Data is Cleared!");
|
|
#endif
|
|
*/
|
|
return true;
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::reloadSettings(){
|
|
|
|
return true;
|
|
};
|
|
|
|
void argusModuleEmbedVideo::dispatchEvent(EVENT_CONTAINER *Evt){
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::validate(){
|
|
// Scan Active Streams And Create
|
|
if( m_pDBC ) {
|
|
DBC_INT_LIST *IDS = m_pDBC->listMediaStreams( 1 ,LIST_VALID_RECORDS );
|
|
if( IDS ) {
|
|
for( DBC_INT_LIST::iterator idx = IDS->begin(); idx < IDS->end(); idx++ ) {
|
|
if( !createStream((*idx)) ) LOG_ERROR( "STREAM Creation Failure!" );
|
|
};
|
|
delete( IDS );
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
void argusModuleEmbedVideo::pushFrame(AVFrame *Frame, void* src){
|
|
MEDIA_SOURCE_DESC * tMD = (MEDIA_SOURCE_DESC *) src;
|
|
if( tMD ) {
|
|
};
|
|
};
|
|
|
|
void argusModuleEmbedVideo::pushFrame(cv::Mat* Frame, void* src){
|
|
MEDIA_SOURCE_DESC * tMD = (MEDIA_SOURCE_DESC *) src;
|
|
if( tMD ) {
|
|
|
|
for( vectorSTREAMS::iterator idx = m_vRegisteredStreams.begin(); idx < m_vRegisteredStreams.end(); idx++ ) {
|
|
if( (*idx)->mediaStreamId == tMD->mediaStreamId ) {
|
|
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::registerForMediaStream( uint32_t id, WEB_CLIENT_CTX *&CTX ){
|
|
if( CTX ){
|
|
if( id ){
|
|
if( pipe( CTX->m_iEncoderPipe ) == -1 ){
|
|
CTX->bValid = false;
|
|
return false;
|
|
};
|
|
CTX->STREAM = (STREAM_CTX *) av_mallocz(sizeof(STREAM_CTX));
|
|
if( CTX->STREAM ) {
|
|
CTX->STREAM->mediaStreamId = id;
|
|
|
|
return true;
|
|
} else {
|
|
close( CTX->m_iEncoderPipe[0] ); CTX->m_iEncoderPipe[0] = -1;
|
|
close( CTX->m_iEncoderPipe[1] ); CTX->m_iEncoderPipe[1] = -1;
|
|
LOG_ERROR("STREAM memory allocation error!");
|
|
CTX->bValid = false;
|
|
return false;
|
|
};
|
|
}
|
|
#ifdef DEBUG
|
|
else {
|
|
CTX->bValid = true;
|
|
CTX->STREAM = (STREAM_CTX *) av_mallocz(sizeof(STREAM_CTX));
|
|
if( CTX->STREAM ) {
|
|
CTX->STREAM->mediaStreamId = 0;
|
|
};
|
|
return true;
|
|
}
|
|
#endif
|
|
;
|
|
} else LOG_ERROR("Client Context error!");
|
|
return false;
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::registerStream( MEDIA_SOURCE_DESC *ptr ){
|
|
if( ptr ){
|
|
if( m_vRegisteredStreams.size() ){
|
|
bool found = false;
|
|
m_mStreamsProtector.lock();
|
|
for( vectorSTREAMS::iterator idx = m_vRegisteredStreams.begin(); idx < m_vRegisteredStreams.end(); idx++ ) {
|
|
if( (*idx)->mediaStreamId == ptr->mediaStreamId ) {
|
|
found = true;
|
|
break;
|
|
};
|
|
};
|
|
m_mStreamsProtector.unlock();
|
|
if( !found ) return createStream( ptr->mediaStreamId );
|
|
} else {
|
|
return createStream( ptr->mediaStreamId );
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::unregisterStream( MEDIA_SOURCE_DESC *ptr ){
|
|
if( ptr ){
|
|
if( m_vRegisteredStreams.size() ){
|
|
m_mStreamsProtector.lock();
|
|
for( vectorSTREAMS::iterator idx = m_vRegisteredStreams.begin(); idx < m_vRegisteredStreams.end(); idx++ ) {
|
|
if( (*idx)->mediaStreamId == ptr->mediaStreamId ) {
|
|
//destroyStream((*idx));
|
|
m_vRegisteredStreams.erase( idx );
|
|
m_mStreamsProtector.unlock();
|
|
return true;
|
|
};
|
|
};
|
|
m_mStreamsProtector.unlock();
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool argusModuleEmbedVideo::createStream( uint32_t id ){
|
|
DBC_MEDIA_STREAM_DATA *tDATA = NULL;
|
|
bool retVal = false;
|
|
if( m_vRegisteredStreams.size() ){
|
|
m_mStreamsProtector.lock();
|
|
for( vectorSTREAMS::iterator idx = m_vRegisteredStreams.begin(); idx < m_vRegisteredStreams.end(); idx++ ) if( (*idx)->mediaStreamId == id ) return true;
|
|
m_mStreamsProtector.unlock();
|
|
};
|
|
if( m_pDBC ) {
|
|
tDATA = m_pDBC->getMediaStreamData( id );
|
|
if( tDATA ) {
|
|
STREAM_CTX *CTX = (STREAM_CTX*) av_mallocz(sizeof(STREAM_CTX));
|
|
if( CTX ) {
|
|
CTX->ModulePtr = this;
|
|
CTX->mediaStreamId = id;
|
|
initLibAV();
|
|
if( open_module_video(
|
|
CTX,
|
|
VIDEO_OUTPUT_WIDTH,
|
|
VIDEO_OUTPUT_HEIGHT,
|
|
VIDEO_OUTPUT_FPS,
|
|
VIDEO_OUTPUT_BITRATE,
|
|
VP_FAST,
|
|
VC_MJPEG,
|
|
VF_MJPEG
|
|
) ){
|
|
CTX->InputMat = new cv::Mat( VIDEO_OUTPUT_HEIGHT, VIDEO_OUTPUT_WIDTH, CV_8UC3 );
|
|
if( CTX->InputMat == NULL ) {
|
|
TLOG_ERROR("Cannot ctreate cv::Mat for encoder!");
|
|
free( tDATA );
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("cv::Mat Created");
|
|
#endif
|
|
cv::rectangle( *CTX->InputMat, cv::Point( 0, 0 ), cv::Point( VIDEO_OUTPUT_WIDTH, VIDEO_OUTPUT_HEIGHT ), cv::Scalar( 255, 0, 0 ), cv::FILLED );
|
|
if( write_header( CTX ) ){
|
|
#ifdef DEBUG
|
|
LOG( "Header SENT" );
|
|
#endif
|
|
CTX->video_sws_ctx = sws_getContext(
|
|
tDATA->width,
|
|
tDATA->height,
|
|
AV_PIX_FMT_BGR24,
|
|
VIDEO_OUTPUT_WIDTH,
|
|
VIDEO_OUTPUT_HEIGHT,
|
|
VIDEO_OUTPUT_PIX_FMT,
|
|
SWS_BICUBIC,
|
|
NULL,
|
|
NULL,
|
|
NULL
|
|
);
|
|
if( CTX->video_sws_ctx == NULL ){
|
|
char buffer[200];
|
|
sprintf( buffer, "Failed to get SWS CTX [%dx%d]{%04X} ---> [%dx%d]{%04X} by (%d)",
|
|
tDATA->width,
|
|
tDATA->height,
|
|
AV_PIX_FMT_BGR24,
|
|
VIDEO_OUTPUT_WIDTH,
|
|
VIDEO_OUTPUT_HEIGHT,
|
|
VIDEO_OUTPUT_PIX_FMT,
|
|
SWS_BICUBIC
|
|
);
|
|
LOG_ERROR( buffer );
|
|
free( tDATA );
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("Finish Preparing SWS");
|
|
#endif
|
|
CTX->video_frame = av_frame_alloc();
|
|
if( CTX->video_frame == NULL ){
|
|
LOG_ERROR("Error Create AVFrame!");
|
|
free( tDATA );
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("Frame Created");
|
|
#endif
|
|
CTX->video_frame->width = VIDEO_OUTPUT_WIDTH;
|
|
CTX->video_frame->height = VIDEO_OUTPUT_HEIGHT;
|
|
CTX->video_frame->format = VIDEO_OUTPUT_PIX_FMT;
|
|
uint8_t *tBuff = (uint8_t *) av_malloc(
|
|
av_image_get_buffer_size(
|
|
(AVPixelFormat)CTX->video_frame->format,
|
|
CTX->video_frame->width,
|
|
CTX->video_frame->height,
|
|
BUFFER_ALIGN
|
|
)
|
|
);
|
|
if( tBuff == NULL) {
|
|
TLOG_ERROR("Error Allocate Buffer for AVFrame!");
|
|
free( tDATA );
|
|
cleanSTREAM_CTX( CTX );
|
|
return false;
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("Buffer Created");
|
|
#endif
|
|
avpicture_fill(
|
|
(AVPicture *)CTX->video_frame,
|
|
tBuff,
|
|
(AVPixelFormat)CTX->video_frame->format,
|
|
CTX->video_frame->width,
|
|
CTX->video_frame->height
|
|
);
|
|
TLOG("Finish Preparing output AVFrame");
|
|
m_mStreamsProtector.lock();
|
|
m_vRegisteredStreams.push_back( CTX );
|
|
m_mStreamsProtector.unlock();
|
|
#ifdef DEBUG
|
|
LOG("STREAM Created");
|
|
#endif
|
|
retVal = true;
|
|
} else {
|
|
cleanSTREAM_CTX( CTX );
|
|
};
|
|
} else {
|
|
cleanSTREAM_CTX( CTX );
|
|
};
|
|
};
|
|
free( tDATA );
|
|
};
|
|
};
|
|
return retVal;
|
|
};
|
|
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
#undef TLOG
|
|
#undef TLOG_ERROR
|