From dac72d1921c185162bcc8a902b255fd5c8541b97 Mon Sep 17 00:00:00 2001 From: Gagua on MyIR master Date: Sat, 8 Aug 2020 07:27:28 +0000 Subject: [PATCH] Add support of Nginx Streaming --- Projects/LRF_Server/LRF_Server.vpj | 4 +- Projects/LRF_Server/makefile | 48 ++++--- lib/components/base/basicThread.cpp | 2 +- lib/components/base/basicThread.h | 4 +- .../mediaNginxStreamingProxyManager.cpp | 94 +++++++++++++ .../mediaNginxStreamingProxyManager.h | 61 +++++++++ lib/data/protocol/protocolInfoStress.cpp | 125 ++++++++++++++++-- lib/data/protocol/protocolInfoStress.h | 16 ++- 8 files changed, 310 insertions(+), 44 deletions(-) create mode 100644 lib/data/NginxMediaServer/mediaNginxStreamingProxyManager.cpp create mode 100644 lib/data/NginxMediaServer/mediaNginxStreamingProxyManager.h diff --git a/Projects/LRF_Server/LRF_Server.vpj b/Projects/LRF_Server/LRF_Server.vpj index 619419b6..b2693038 100644 --- a/Projects/LRF_Server/LRF_Server.vpj +++ b/Projects/LRF_Server/LRF_Server.vpj @@ -27,7 +27,7 @@ RunFromDir="%rw"> + OtherOptions="-std=c++11 -I../../lib/include"/> + OtherOptions="-lconfig -lpthread -lelf -lcurses -lcdk -lcurl `pkg-config --libs rdkafka++`"/> Log( "NGINX_MEDIA", this, X ) +#define LOG_ERROR( X ) m_pLog->Log( "NGINX_MEDIA", this, _LOG_ERROR, X ) + +mediaNginxManager *mediaNginxManager::_instance = NULL; + +mediaNginxManager *mediaNginxManager::getInstance(){ + // check if the instance has been created yet + if( 0 == _instance ){ + // if not, then create it + _instance = new mediaNginxManager; + }; + // return the single instance + return _instance; +}; + +mediaNginxManager::mediaNginxManager(): basicThread(){ + m_pLog = basicLogCollection::getInstance(); + basicThread::init(); +}; + +static void *streamFeed( void *ptr ){ + MEDIA_MANAGER_RECORD * mRec; + mRec = (MEDIA_MANAGER_RECORD*) ptr; + mRec->FID = popen( mRec->CMD, "r" ); + if( mRec->FID!= NULL ){ + while( !feof( mRec->FID ) ){ + + }; + pclose( mRec->FID ); + }; + mRec->ThreadStatus = TH_EXIT; + pthread_exit( ptr ); + return ptr; +}; + +bool mediaNginxManager::restartStream( MEDIA_MANAGER_RECORD *tRec ){ + if( tRec == NULL ) return false; + tRec->ThreadStatus = TH_ARMED; + int RetVal = pthread_create( &tRec->m_sThreadHandler, NULL, streamFeed, ( void * ) tRec ); + if( RetVal == -1 ) { + LOG_ERROR("Thread Creation Failed."); + tRec->ThreadStatus = TH_EXIT; + return false; + }; + return false; +}; + +bool mediaNginxManager::attachStream( CSTR URI, bool TCP_CONNECTION, uint32_t id, CSTR md5 ){ + MEDIA_MANAGER_RECORD *tRec; + tRec = (MEDIA_MANAGER_RECORD*) malloc( sizeof(MEDIA_MANAGER_RECORD)); + if( tRec == NULL ) return false; + strncpy( tRec->URI, URI, 1024 ); + strncpy( tRec->md5, md5, 33 ); + tRec->FID = NULL; + tRec->ThreadStatus = TH_WORKING; + sprintf( tRec->CMD, NGINX_MEDIA_MANAGER_CMD, ((mRec->tcp_connection)?"-rtsp_transport tcp":""), URI, NGINX_MEDIA_MANAGER_DEFAULT_FEED, md5 ); + int RetVal = pthread_create( &tRec->m_sThreadHandler, NULL, streamFeed, ( void * ) tRec ); + if( RetVal == -1 ) { + LOG_ERROR("Thread Creation Failed."); + free( tRec ); + return false; + }; + m_oProtector.lock(); + m_vStreams.push_back( tRec ); + m_oProtector.unlock(); + return true; +}; + +/* thread is for checking streams purposes */ +void mediaNginxManager::m_fThreadProc( void ){ + m_bThreadStatus = TH_WORKING; + while( m_bThreadStatus < TH_ENDED ){ + m_oProtector.lock(); + if( m_vStreams.size() ){ + for( vMEDIARECORD::iterator index = m_vStreams.begin(); index < m_vStreams.end(); index++ ){ + if( (*index)->ThreadStatus > TH_WORKING ){ + void * ptr; + int RetVal = pthread_join( (*index)->m_sThreadHandler, &ptr ); + if( RetVal == 0 ){ + pthread_detach( (*index)->m_sThreadHandler ); + }; + restartStream( *index ); + }; + }; + }; + m_oProtector.unlock(); + SLEEP( NGINX_MEDIA_MANAGER_INTERVAL ); + }; +}; + +#undef LOG +#undef LOG_ERROR \ No newline at end of file diff --git a/lib/data/NginxMediaServer/mediaNginxStreamingProxyManager.h b/lib/data/NginxMediaServer/mediaNginxStreamingProxyManager.h new file mode 100644 index 00000000..9d9cd035 --- /dev/null +++ b/lib/data/NginxMediaServer/mediaNginxStreamingProxyManager.h @@ -0,0 +1,61 @@ +#ifndef __MEDIA_NGINX_STREAMER_MANAGER__ +#define __MEDIA_NGINX_STREAMER_MANAGER__ + +#include "../../componments/base/basicLogCollection.h" +#include "../../componments/base/basicThread.h" +#include "../../componments/base/basicMutex.h" +#include + +/* Singletone object */ + +typedef struct S_MEDIA_MANAGER_RECORD{ + uint32_t id; + char URI[1024]; + char CMD[1024]; + bool tcp_connection; + char md5[33]; + pthread_t m_sThreadHandler; + uint8_t ThreadStatus; + FILE * FID; +} MEDIA_MANAGER_RECORD; + +typedef std::vector vMEDIARECORD; + +#define NGINX_MEDIA_MANAGER_INTERVAL 300 +#define NGINX_MEDIA_MANAGER_DEFAULT_FEED "rtmp:localhost/live" +#define NGINX_MEDIA_MANAGER_CMD "ffmpeg %s -re -i \"%s\" -an -tune zerolatency -bsf:v h264_mp4toannexb -c copy -f flv %s/%s" + +class mediaNginxManager: public basicThread{ + private: + basicLogCollection * m_pLog; + basicMutex m_oProtector; + vMEDIARECORD m_vStreams; + bool restartStream( MEDIA_MANAGER_RECORD * ); + public: + virtual ~mediaNginxManager(); + mediaNginxManager * getInstance(); + bool attachStream( CSTR URI, bool TCP_CONNECTION ); + + protected: + // Constructor - protected so users cannot call it. + mediaNginxManager(); + virtual void m_fThreadProc( void ); + private: + // private static member referencing the single instance of the object + // TODO: Replace mediaNginxManager with the type of object you want to + // return the single instance of + static mediaNginxManager * _instance; + + // Copy constructor + // Declared but not defined to prevent auto-generated + // copy constructor. Refer to "Effective C++" by Meyers + mediaNginxManager(const mediaNginxManager& src); + + // Assignment operator + // Declared but not defined to prevent auto-generated + // assignment operator. Refer to "Effective C++" by Meyers + mediaNginxManager& operator=(const mediaNginxManager& src); +}; + + +#endif \ No newline at end of file diff --git a/lib/data/protocol/protocolInfoStress.cpp b/lib/data/protocol/protocolInfoStress.cpp index c261708b..a947a290 100644 --- a/lib/data/protocol/protocolInfoStress.cpp +++ b/lib/data/protocol/protocolInfoStress.cpp @@ -171,6 +171,11 @@ protocolInfoStress::protocolInfoStress( config_setting_t *Settings ){ m_pKafkaBrokers = (CSTR)malloc(CLOG); if( m_pKafkaBrokers == NULL ) return; memset( m_pKafkaBrokers, 0 , CLOG); + m_pKafkaTopicSTR= (CSTR)malloc(CLOG); + if( m_pKafkaTopicSTR == NULL ) return; + memset( m_pKafkaTopicSTR, 0 , CLOG); + m_pKafkaTopic = NULL; + m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE); if( m_pScannerName == NULL ) return; memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE); @@ -278,6 +283,7 @@ protocolInfoStress::protocolInfoStress( config_setting_t *Settings ){ * The callback is triggered from rd_kafka_poll() and executes on * the application's thread. */ +/* static void dr_msg_cb( rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void *opaque ){ if (rkmessage->err) fprintf(stderr, "%% Message delivery failed: %s\n", @@ -288,8 +294,9 @@ static void dr_msg_cb( rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void "partition %"PRId32")\n", rkmessage->len, rkmessage->partition); - /* The rkmessage is destroyed automatically by librdkafka */ + // The rkmessage is destroyed automatically by librdkafka }; +*/ protocolInfoStress::~protocolInfoStress(){ if(m_pKafka!=NULL){ @@ -297,13 +304,16 @@ protocolInfoStress::~protocolInfoStress(){ * rd_kafka_flush() is an abstraction over rd_kafka_poll() which * waits for all messages to be delivered. */ LOG("Flushing final messages.."); - rd_kafka_flush( m_pKafka, 3000 ); + //rd_kafka_flush( m_pKafka, 3000 ); + m_pKafka->flush(3*1000 /* wait for max 3 seconds */); /* If the output queue is still not empty there is an issue * with producing messages to the clusters. */ - if( rd_kafka_outq_len( m_pKafka ) > 0 ) LOG_ERROR("Some messages are not delivered!"); - rd_kafka_destroy(m_pKafka); + //if( rd_kafka_outq_len( m_pKafka ) > 0 ) LOG_ERROR("Some messages are not delivered!"); + if (m_pKafka->outq_len() > 0) LOG_ERROR("Some messages are not delivered!"); + //rd_kafka_destroy(m_pKafka); + delete m_pKafka; }; - if(m_pKafkaConf!=NULL)rd_kafka_destroy(m_pKafkaConf); + //if(m_pKafkaConf!=NULL)rd_kafka_destroy(m_pKafkaConf); if(m_pScannerName!=NULL)free(m_pScannerName); if(m_pServerIP!=NULL)free(m_pServerIP); if( m_pAccessURL != NULL)free(m_pAccessURL); @@ -311,28 +321,58 @@ protocolInfoStress::~protocolInfoStress(){ //https://github.com/edenhill/librdkafka/blob/master/examples/producer.c //https://github.com/edenhill/librdkafka/blob/master/examples/producer.cpp +void protocolInfoStress::dr_cb( RdKafka::Message &message ){ + /* If message.err() is non-zero the message delivery failed permanently + * for the message. */ + char tBuffer[1024]; + if( message.err() ){ + sprintf(tBuffer,"Message delivery failed: %s", message.errstr().c_str() ); + LOG_ERROR( tBuffer ); + } +#ifdef DEBUG + else { + sprintf(tBuffer,"Message delivered to topic: %s [%d] at offset %d", message.topic_name().c_str(), message.partition(), message.offset() ); + LOG( tBuffer ); + } +#endif + ; +}; + bool protocolInfoStress::init(){ - char tBuffer[CLOG]; + //char tBuffer[CLOG]; + std::string tBuffer; + std::string brokers = m_pKafkaBrokers; + std::string topic = m_pKafkaTopicSTR; + if( strlen( m_pKafkaBrokers ) == 0 ) { LOG_ERROR("No Kafka Brokers are defined!"); return false; }; - m_pKafkaConf = rd_kafka_conf_new(); + //m_pKafkaConf = rd_kafka_conf_new(); + m_pKafkaConf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL); if( m_pKafkaConf == NULL ) { LOG_ERROR("Eror Creating Kafka Conf!"); return false; }; - if( rd_kafka_conf_set( m_pKafkaConf, "bootstrap.servers", m_pKafkaBrokers, tBuffer, CLOG ) != RD_KAFKA_CONF_OK ){ - LOG_ERROR( tBuffer ); + if( m_pKafkaConf->set("bootstrap.servers", m_pKafkaBrokers, tBuffer ) != RdKafka::Conf::CONF_OK ){ + LOG_ERROR( tBuffer.c_str() ); return false; }; + //if( rd_kafka_conf_set( m_pKafkaConf, "bootstrap.servers", m_pKafkaBrokers, tBuffer, CLOG ) != RD_KAFKA_CONF_OK ){ + // LOG_ERROR( tBuffer ); + // return false; + //}; /* Set the delivery report callback. * This callback will be called once per message to inform * the application if delivery succeeded or failed. * See dr_msg_cb() above. * The callback is only triggered from rd_kafka_poll() and * rd_kafka_flush(). */ - rd_kafka_conf_set_dr_msg_cb( m_pKafkaConf, dr_msg_cb ); + //rd_kafka_conf_set_dr_msg_cb( m_pKafkaConf, dr_msg_cb ); + if( m_pKafkaConf->set( "dr_cb", this, tBuffer ) != RdKafka::Conf::CONF_OK ){ + LOG_ERROR( tBuffer.c_str() ); + return false; + }; /* * Create producer instance. * @@ -340,11 +380,72 @@ bool protocolInfoStress::init(){ * and the application must not reference it again after * this call. */ - m_pKafka = rd_kafka_new( RD_KAFKA_PRODUCER, m_pKafkaConf, tBuffer, CLOG ); + //m_pKafka = rd_kafka_new( RD_KAFKA_PRODUCER, m_pKafkaConf, tBuffer, CLOG ); + m_pKafka = RdKafka::Producer::create( m_pKafkaConf, tBuffer ); if( m_pKafka == NULL ){ - LOG_ERROR( tBuffer ); + LOG_ERROR( tBuffer.c_str() ); return false; }; + m_pKafkaTopic = RdKafka::Topic::create( m_pKafka, topic, m_pKafkaConf, tBuffer ); + if( m_pKafkaTopic == NULL ) { + LOG_ERROR( tBuffer.c_str() ); + return false; + }; + //?? + delete( m_pKafkaConf ); + m_pKafkaConf = NULL; + return true; +}; + +bool protocolInfoStress::issuePost( CSTR URL, CSTR Buffer, int BufferSize ){ + if( m_pKafka == NULL) return false; + RdKafka::ErrorCode err; +retry: + err = m_pKafka->produce( + m_pKafkaTopic, + RdKafka::Topic::PARTITION_UA, + RdKafka::Producer::RK_MSG_COPY, + (void *)Buffer, + BufferSize, + NULL, + NULL + ); + if( err != RdKafka::ERR_NO_ERROR ) { + ///std::cerr << "% Failed to produce to topic " << topic << ": " << RdKafka::err2str(err) << std::endl; + if( err == RdKafka::ERR__QUEUE_FULL ){ + /* If the internal queue is full, wait for + * messages to be delivered and then retry. + * The internal queue represents both + * messages to be sent and messages that have + * been sent or failed, awaiting their + * delivery report callback to be called. + * + * The internal queue is limited by the + * configuration property + * queue.buffering.max.messages */ + m_pKafka->poll(1000/*block for max 1000ms*/); + goto retry; + }; + } +#ifdef DEBUG + else { + LOG("MSG OK"); + //std::cerr << "% Enqueued message (" << line.size() << " bytes) " << "for topic " << topic << std::endl; + } +#endif + ; + + /* A producer application should continually serve + * the delivery report queue by calling poll() + * at frequent intervals. + * Either put the poll call in your main loop, or in a + * dedicated thread, or call it after every produce() call. + * Just make sure that poll() is still called + * during periods where you are not producing any messages + * to make sure previously produced messages have their + * delivery report callback served (and any other callbacks + * you register). */ + m_pKafka->poll(0); return true; }; diff --git a/lib/data/protocol/protocolInfoStress.h b/lib/data/protocol/protocolInfoStress.h index 494e141c..8ee2f61d 100644 --- a/lib/data/protocol/protocolInfoStress.h +++ b/lib/data/protocol/protocolInfoStress.h @@ -11,7 +11,8 @@ #include #include #include -#include +//#include +#include #include "../../components/base/basicFunctionality.h" @@ -82,13 +83,17 @@ typedef enum E_DEFENDER_CATEGORY{ * * @author gagua */ -class protocolInfoStress { +class protocolInfoStress:public RdKafka::DeliveryReportCb { private: basicLogCollection * m_pLog; config_setting_t * m_pSettings; - rd_kafka_t * m_pKafka; /* Producer instance handle */ - rd_kafka_conf_t * m_pKafkaConf; /* Temporary configuration object */ + //rd_kafka_t * m_pKafka; /* Producer instance handle */ + RdKafka::Producer * m_pKafka; /* Producer instance handle */ + //rd_kafka_conf_t * m_pKafkaConf; /* Temporary configuration object */ + RdKafka::Conf * m_pKafkaConf; /* Temporary configuration object */ CSTR m_pKafkaBrokers; + CSTR m_pKafkaTopicSTR; + RdKafka::Topic * m_pKafkaTopic; //uint8_t m_bKafkaBrokersCount; int m_nScannerDimentions; @@ -115,6 +120,7 @@ private: CSTR assembleFaultData( uint8_t Code, CSTR Desc ); CSTR assembleTargetData( int targetID, int Range, int Width, int Azimuth, int Elevation = 0 ); size_t assembleDefenderJSON( CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data ); + bool issuePost( CSTR URL, CSTR Buffer, int BufferSize ); public: // Constructor explicit protocolInfoStress( config_setting_t *Settings = NULL ); @@ -130,6 +136,8 @@ public: void SetStatus( bool A ){ m_bCurrentStatus = A; }; bool ErrorReporting( int lvl, CSTR Descr ); bool StartTimeSynch( void ); + /* Implementation of RdKafka::DeliveryReportCb */ + virtual void dr_cb( RdKafka::Message &message ); #ifdef __linux__ //bool FinishTimeSynch( unsigned long long *tVals ); #else