From 3d75522e7d59c6d75b1dd34859159794f87cc89a Mon Sep 17 00:00:00 2001 From: narongsak mala Date: Thu, 22 Aug 2019 13:58:16 +0700 Subject: [PATCH 1/3] set RTP/SRTP with configuration --- .gitignore | 5 +++ Makefile.am | 3 +- mp_engine.cc | 90 +++++++++++++++++++++++++++---------------- mp_engine.h | 7 ++++ mp_mediaproxy.cc | 17 ++++++++ tinywrap/SipStack.cxx | 8 ++++ tinywrap/SipStack.h | 1 + 7 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 .gitignore mode change 100755 => 100644 mp_mediaproxy.cc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7e61e1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Compiled Object files +*.slo +*.lo +*.o +*.obj diff --git a/Makefile.am b/Makefile.am index 3672f8c..693a50f 100755 --- a/Makefile.am +++ b/Makefile.am @@ -38,7 +38,8 @@ ${TINYMSRP_LIBS} \ ${TINYRTP_LIBS} \ ${TINYIPSEC_LIBS} \ -lxml2 \ --lpthread +-lpthread \ +-ldl webrtc2sip_SOURCES = \ diff --git a/mp_engine.cc b/mp_engine.cc index 02dd41e..45fb79a 100755 --- a/mp_engine.cc +++ b/mp_engine.cc @@ -163,6 +163,15 @@ bool MPEngine::setRtpSymetricEnabled(bool bEnabled) return MediaSessionMgr::defaultsSetRtpSymetricEnabled(bEnabled); } +bool MPEngine::setRtpPortRange(uint16_t start, uint16_t stop) +{ + if(start < 1024 || stop < 1024 || start >= stop) { + TSK_DEBUG_ERROR("Invalid parameter: (%u < 1024 || %u < 1024 || %u >= %u)", start, stop, start, stop); + return false; + } + return MediaSessionMgr::defaultsSetRtpPortRange(start, stop); +} + bool MPEngine::set100relEnabled(bool bEnabled) { return MediaSessionMgr::defaultsSet100relEnabled(bEnabled); @@ -259,8 +268,8 @@ bool MPEngine::setCodecs(const char* pcCodecs) return false; } - int i; - struct codec{ const char* name; tmedia_codec_id_t id; }; + int i; + struct codec{ const char* name; tmedia_codec_id_t id; }; static const codec aCodecNames[] = { {"pcma", tmedia_codec_id_pcma}, {"pcmu", tmedia_codec_id_pcmu}, @@ -274,41 +283,41 @@ bool MPEngine::setCodecs(const char* pcCodecs) {"gsm", tmedia_codec_id_gsm}, {"g722", tmedia_codec_id_g722}, {"ilbc", tmedia_codec_id_ilbc}, - {"h264-bp", tmedia_codec_id_h264_bp}, - {"h264-mp", tmedia_codec_id_h264_mp}, - {"vp8", tmedia_codec_id_vp8}, - {"h263", tmedia_codec_id_h263}, - {"h263+", tmedia_codec_id_h263p}, - {"theora", tmedia_codec_id_theora}, - {"mp4v-es", tmedia_codec_id_mp4ves_es} - }; - static const int nCodecsCount = sizeof(aCodecNames) / sizeof(aCodecNames[0]); - + {"h264-bp", tmedia_codec_id_h264_bp}, + {"h264-mp", tmedia_codec_id_h264_mp}, + {"vp8", tmedia_codec_id_vp8}, + {"h263", tmedia_codec_id_h263}, + {"h263+", tmedia_codec_id_h263p}, + {"theora", tmedia_codec_id_theora}, + {"mp4v-es", tmedia_codec_id_mp4ves_es} + }; + static const int nCodecsCount = sizeof(aCodecNames) / sizeof(aCodecNames[0]); + int64_t nCodecs = (int64_t)tmedia_codec_id_none; tsk_params_L_t* pParams; int nPriority = 0; - if((pParams = tsk_params_fromstring(pcCodecs, ";", tsk_true))) - { - const tsk_list_item_t* item; - tsk_list_foreach(item, pParams) - { - const char* pcCodecName = ((const tsk_param_t*)item->data)->name; - for(i = 0; i < nCodecsCount; ++i) - { - if(tsk_striequals(aCodecNames[i].name, pcCodecName)) - { - nCodecs |= (int64_t)aCodecNames[i].id; - if(!tdav_codec_is_supported((tdav_codec_id_t)aCodecNames[i].id)){ - TSK_DEBUG_INFO("'%s' codec enabled but not supported", aCodecNames[i].name); - } - else{ - tdav_codec_set_priority((tdav_codec_id_t)aCodecNames[i].id, nPriority++); - } - break; - } - } - } + if((pParams = tsk_params_fromstring(pcCodecs, ";", tsk_true))) + { + const tsk_list_item_t* item; + tsk_list_foreach(item, pParams) + { + const char* pcCodecName = ((const tsk_param_t*)item->data)->name; + for(i = 0; i < nCodecsCount; ++i) + { + if(tsk_striequals(aCodecNames[i].name, pcCodecName)) + { + nCodecs |= (int64_t)aCodecNames[i].id; + if(!tdav_codec_is_supported((tdav_codec_id_t)aCodecNames[i].id)){ + TSK_DEBUG_INFO("'%s' codec enabled but not supported", aCodecNames[i].name); + } + else{ + tdav_codec_set_priority((tdav_codec_id_t)aCodecNames[i].id, nPriority++); + } + break; + } + } + } } TSK_OBJECT_SAFE_FREE(pParams); @@ -607,6 +616,7 @@ bool MPEngine::start() // start SIP stack if(const_cast(m_oSipStack->getWrappedStack())->start()) { + //const_cast(m_oSipStack->getWrappedStack())->setRtpPortRange(this.rtp_port_start, this.rtp_port_stop); setStarted(true); } else @@ -680,6 +690,20 @@ bool MPEngine::stop() return (ret == 0); } + +bool MPEngine::setRtpPort(uint16_t start, uint16_t stop){ + return const_cast(m_oSipStack->getWrappedStack())->setRtpPortRange(start, stop); +} + + +uint16_t MPEngine::RtpPortStart(){ + return this->port_range_start; +} + +uint16_t MPEngine::RtpPortStop(){ + return this->port_range_stop; +} + MPObjectWrapper MPEngine::getPeerById(uint64_t nId) { MPObjectWrapper m_Peer = NULL; diff --git a/mp_engine.h b/mp_engine.h index 1fa2f1e..b40bc18 100755 --- a/mp_engine.h +++ b/mp_engine.h @@ -61,6 +61,7 @@ class MPEngine : public MPObject virtual bool setCodecs(const char* pcCodecs); virtual bool setCodecOpusMaxRates(int32_t nPlaybackMaxRate, int32_t nCaptureMaxRate); virtual bool setSRTPMode(const char* pcMode); + virtual bool setRtpPortRange(uint16_t, uint16_t); virtual bool setSRTPType(const char* pcTypesCommaSep); virtual bool setDtmfType(const char* pcDtmfType); virtual bool setStunServer(const char* pcIP, unsigned short nPort, const char* pcUsrName, const char* pcUsrPwd); @@ -80,6 +81,12 @@ class MPEngine : public MPObject static MPObjectWrapper New(); + virtual uint16_t RtpPortStart(); + virtual uint16_t RtpPortStop(); + virtual bool setRtpPort(uint16_t start, uint16_t stop); + + uint16_t port_range_start; + uint16_t port_range_stop; protected: virtual MP_INLINE void setStarted(bool bStarted){ m_bStarted = bStarted; } diff --git a/mp_mediaproxy.cc b/mp_mediaproxy.cc old mode 100755 new mode 100644 index 86c66f3..aeee6be --- a/mp_mediaproxy.cc +++ b/mp_mediaproxy.cc @@ -339,6 +339,23 @@ static int parseConfigNode(xmlNode *pNode, MPObjectWrapper oEngine) TSK_DEBUG_ERROR("Failed to set 'dtmf-type': %s", pcDtmfType); } } + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "rtp-port-range")) // available since 2.1.0 + { + + if((pParams = tsk_params_fromstring((const char*)pCurrNode->content, ";", tsk_true)) && mp_list_count(pParams) == 2) + { + const char* start = ((const tsk_param_t*)pParams->head->data)->name; + const char* stop = ((const tsk_param_t*)pParams->head->next->data)->name; + + TSK_DEBUG_INFO("rtp-port-range = %s;%s", start, stop); + + if(!oEngine->setRtpPortRange(atoi(start), atoi(stop))) + { + TSK_DEBUG_ERROR("Failed to set 'rtp-port-range': %s;%s", start, stop); + } + oEngine->setRtpPort(atoi(start), atoi(stop)); + } + } break; } } diff --git a/tinywrap/SipStack.cxx b/tinywrap/SipStack.cxx index 5ec3680..ebbafb8 100755 --- a/tinywrap/SipStack.cxx +++ b/tinywrap/SipStack.cxx @@ -96,6 +96,14 @@ bool SipStack::setDisplayName(const char* display_name) return (ret == 0); } +bool SipStack::setRtpPortRange(uint16_t start, uint16_t stop) +{ + int ret = tsip_stack_set(m_pHandle, + TSIP_STACK_SET_RTP_PORT_RANGE(start, stop), + TSIP_STACK_SET_NULL()); + return (ret == 0); +} + bool SipStack::setRealm(const char* realm_uri) { int ret = tsip_stack_set(m_pHandle, diff --git a/tinywrap/SipStack.h b/tinywrap/SipStack.h index 1f41096..d22060a 100755 --- a/tinywrap/SipStack.h +++ b/tinywrap/SipStack.h @@ -43,6 +43,7 @@ class TINYWRAP_API SipStack: public SafeObject bool start(); bool setDebugCallback(DDebugCallback* pCallback); bool setDisplayName(const char* display_name); + bool setRtpPortRange(uint16_t start, uint16_t stop); bool setRealm(const char* realm_uri); bool setIMPI(const char* impi); bool setIMPU(const char* impu_uri); From 279a57988da226ad0686fd31821fd3afb22a3e33 Mon Sep 17 00:00:00 2001 From: narongsak mala Date: Fri, 23 Aug 2019 18:22:24 +0700 Subject: [PATCH 2/3] bugs setting rtp port --- mp_engine.cc | 16 ++++++++++++++-- mp_mediaproxy.cc | 3 +-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/mp_engine.cc b/mp_engine.cc index 45fb79a..2ef74e5 100755 --- a/mp_engine.cc +++ b/mp_engine.cc @@ -616,7 +616,7 @@ bool MPEngine::start() // start SIP stack if(const_cast(m_oSipStack->getWrappedStack())->start()) { - //const_cast(m_oSipStack->getWrappedStack())->setRtpPortRange(this.rtp_port_start, this.rtp_port_stop); + //const_cast(m_oSipStack->getWrappedStack())->setRtpPortRange(RtpPortStart(), RtpPortStop()); setStarted(true); } else @@ -692,7 +692,19 @@ bool MPEngine::stop() bool MPEngine::setRtpPort(uint16_t start, uint16_t stop){ - return const_cast(m_oSipStack->getWrappedStack())->setRtpPortRange(start, stop); + + if(start < 1024 || stop < 1024 || start >= stop) { + TSK_DEBUG_ERROR("Invalid parameter: (%u < 1024 || %u < 1024 || %u >= %u)", start, stop, start, stop); + return false; + } + + this->port_range_start = start; + this->port_range_stop = stop; + + const_cast(m_oSipStack->getWrappedStack())->setRtpPortRange(start, stop); + + return true; + //return const_cast(m_oSipStack->getWrappedStack())->setRtpPortRange(start, stop); } diff --git a/mp_mediaproxy.cc b/mp_mediaproxy.cc index aeee6be..dbc296e 100644 --- a/mp_mediaproxy.cc +++ b/mp_mediaproxy.cc @@ -349,11 +349,10 @@ static int parseConfigNode(xmlNode *pNode, MPObjectWrapper oEngine) TSK_DEBUG_INFO("rtp-port-range = %s;%s", start, stop); - if(!oEngine->setRtpPortRange(atoi(start), atoi(stop))) + if(!oEngine->setRtpPort(atoi(start), atoi(stop))) { TSK_DEBUG_ERROR("Failed to set 'rtp-port-range': %s;%s", start, stop); } - oEngine->setRtpPort(atoi(start), atoi(stop)); } } break; From 4ca90d2365a01e320647d698462b0c791d916778 Mon Sep 17 00:00:00 2001 From: narongsak mala Date: Wed, 11 Mar 2020 16:45:17 +0700 Subject: [PATCH 3/3] initilize log and stat --- config.xml | 22 +++++++- doubango/tinySAK.vcproj | 8 +++ mp_c2c.cc | 2 +- mp_c2c.h | 1 + mp_engine.cc | 22 ++++++++ mp_engine.h | 1 + mp_mediaproxy.cc | 110 +++++++++++++++++++++++++++++++++++++++- 7 files changed, 162 insertions(+), 4 deletions(-) diff --git a/config.xml b/config.xml index c09d928..c7541bb 100755 --- a/config.xml +++ b/config.xml @@ -2,7 +2,19 @@ - ERROR + INFO + /tmp/ + webrtc2sip.log + + INFO + /tmp/ + app.log + + + /tmp/ + webrtc2sip.stat + 5 + yes udp;*;10060 ws;*;10060 @@ -30,6 +42,12 @@ -1 + + /root/build/webrtc2sip/siampiwat-pem/private.pem; + /root/build/webrtc2sip/siampiwat-pem/cert.pem; + /root/build/webrtc2sip/siampiwat-pem/fullchain.pem; + no + - \ No newline at end of file + diff --git a/doubango/tinySAK.vcproj b/doubango/tinySAK.vcproj index d22e542..8edf368 100755 --- a/doubango/tinySAK.vcproj +++ b/doubango/tinySAK.vcproj @@ -347,6 +347,14 @@ RelativePath="..\..\doubango\tinySAK\src\tsk_debug.h" > + + + + diff --git a/mp_c2c.cc b/mp_c2c.cc index 75ba017..47ff70e 100755 --- a/mp_c2c.cc +++ b/mp_c2c.cc @@ -23,7 +23,7 @@ #include "tsk_string.h" #include "tsk_memory.h" - +#include "tsk_debug.h" #include "tinyhttp.h" #include diff --git a/mp_c2c.h b/mp_c2c.h index 09b5323..41a1057 100755 --- a/mp_c2c.h +++ b/mp_c2c.h @@ -21,6 +21,7 @@ #include "mp_config.h" #include "mp_net_transport.h" +#include "tsk_debug.h" #include "db/mp_db.h" #include "mp_mail.h" #include "mp_recaptcha.h" diff --git a/mp_engine.cc b/mp_engine.cc index 2ef74e5..e9e3ebe 100755 --- a/mp_engine.cc +++ b/mp_engine.cc @@ -17,6 +17,7 @@ * along with 'webrtc2sip'. */ #include "mp_engine.h" +#include "app_log.h" #include "mp_proxyplugin_mgr.h" #include "db/sqlite/mp_db_sqlite.h" @@ -126,6 +127,27 @@ bool MPEngine::setDebugLevel(const char* pcLevel) return false; } +bool MPEngine::setAppLogLevel(const char* pcLevel) +{ + struct debug_level { const char* name; int level; }; + static const debug_level debug_levels[] = + { + {"INFO", DEBUG_LEVEL_INFO}, + {"WARN", DEBUG_LEVEL_WARN}, + {"ERROR", DEBUG_LEVEL_ERROR}, + {"FATAL", DEBUG_LEVEL_FATAL}, + }; + static const int debug_levels_count = sizeof(debug_levels)/sizeof(debug_levels[0]); + int i; + for(i = 0; i < debug_levels_count; ++i){ + if(tsk_striequals(debug_levels[i].name, pcLevel)){ + tsk_debug_set_level(debug_levels[i].level); + return true; + } + } + return false; +} + bool MPEngine::addTransport(const char* pTransport, uint16_t nLocalPort, const char* pcLocalIP /*= tsk_null*/) { if(!isValid()) diff --git a/mp_engine.h b/mp_engine.h index b40bc18..0395d55 100755 --- a/mp_engine.h +++ b/mp_engine.h @@ -49,6 +49,7 @@ class MPEngine : public MPObject virtual MP_INLINE bool isValid(){ return m_bValid; } virtual MP_INLINE bool isStarted(){ return m_bStarted; } virtual bool setDebugLevel(const char* pcLevel); + virtual bool setAppLogLevel(const char* pcLevel); virtual bool addTransport(const char* pTransport, uint16_t nLocalPort, const char* pcLocalIP = tsk_null); virtual bool setRtpSymetricEnabled(bool bEnabled); virtual bool set100relEnabled(bool bEnabled); diff --git a/mp_mediaproxy.cc b/mp_mediaproxy.cc index dbc296e..ee25715 100644 --- a/mp_mediaproxy.cc +++ b/mp_mediaproxy.cc @@ -18,9 +18,19 @@ * along with 'webrtc2sip'. */ #include "mp_engine.h" - +#include "tsk_stat.h" +#include "tsk_string.h" +#include "app_log.h" #include +#include +#include /* puts, printf */ +#include /* time_t, struct tm, time, localtime */ +#include + + + + static char* sConfigXmlPath = NULL; #define kSQLiteConnectionInfo "./c2c_sqlite.db" #define kSQLiteName "sqlite" @@ -69,6 +79,78 @@ static int parseConfigNode(xmlNode *pNode, MPObjectWrapper oEngine) TSK_DEBUG_ERROR("Failed to set debug-level = %s\n", (const char*)pCurrNode->content); } } + /** + * set log configuration + */ + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "log-filename")) + { + TSK_DEBUG_INFO("log filename = %s\n", (const char*)pCurrNode->content); + tsk_debug_set_log_file((const char*)pCurrNode->content); + } + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "log-path")) + { + TSK_DEBUG_INFO("log path = %s\n", (const char*)pCurrNode->content); + tsk_debug_set_log_path((const char*)pCurrNode->content); + } + /** + * set app log configuration + */ + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "app-log-level")) + { + TSK_DEBUG_INFO("app-log-level = %s\n", (const char*)pCurrNode->content); + if(!oEngine->setAppLogLevel((const char*)pCurrNode->content)){ + TSK_DEBUG_ERROR("Failed to set app-log-level = %s\n", (const char*)pCurrNode->content); + } + } + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "app-log-filename")) + { + TSK_DEBUG_INFO("app log filename = %s\n", (const char*)pCurrNode->content); + tsk_app_set_log_file((const char*)pCurrNode->content); + } + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "app-log-path")) + { + TSK_DEBUG_INFO("app log path = %s\n", (const char*)pCurrNode->content); + tsk_app_set_log_path((const char*)pCurrNode->content); + } + /** + * set stat configuration + */ + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "stat-filename")) + { + TSK_DEBUG_INFO("stat filename = %s\n", (const char*)pCurrNode->content); + tsk_stat_set_filename((const char*)pCurrNode->content); + } + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "stat-path")) + { + TSK_DEBUG_INFO("stat path = %s\n", (const char*)pCurrNode->content); + tsk_stat_set_path((const char*)pCurrNode->content); + } + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "stat-interval")) + { + int interval = atoi((const char*)pCurrNode->content); + TSK_DEBUG_INFO("stat interval = %d s\n", interval); + /* stat interval + - min 1 seconds + - max 60 seconds + */ + if( interval < 1 ) { + TSK_DEBUG_FATAL("stat interval must be at least 1 seconds"); + return -1; + }else if( interval > 3600 ){ + TSK_DEBUG_FATAL("stat interval must be less than 3600 seconds"); + return -1; + } + + tsk_stat_set_interval(interval); + } + else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "reset-enable")) + { + TSK_DEBUG_INFO("reset enable = %s\n", (const char*)pCurrNode->content); + if( tsk_strnicmp("yes", (const char*)pCurrNode->content, 3) ) + tsk_stat_reset_enable(1); + else + tsk_stat_reset_enable(0); + } else if(pCurrNode->parent && tsk_striequals(pCurrNode->parent->name, "transport")) { if((pParams = tsk_params_fromstring((const char*)pCurrNode->content, ";", tsk_true)) && mp_list_count(pParams) == 3) @@ -509,6 +591,32 @@ int main(int argc, char** argv) exit (-1); } + // optional: set callback data (will be passed to _debug_info_write_to_file() as "arg" parameter) + const void* _this = tsk_null; + + tsk_debug_set_arg_data(_this); + tsk_debug_set_log_file_path(tsk_debug_get_log_path(), tsk_debug_get_log_file()); + tsk_debug_set_info_cb(tsk_debug_writer); + tsk_debug_set_warn_cb(tsk_debug_writer); + tsk_debug_set_error_cb(tsk_debug_writer); + tsk_debug_set_fatal_cb(tsk_debug_writer); + + tsk_app_set_arg_data(_this); + tsk_app_set_log_file_path(tsk_app_get_log_path(), tsk_app_get_log_file()); + tsk_app_set_info_cb(tsk_app_writer); + tsk_app_set_warn_cb(tsk_app_writer); + tsk_app_set_error_cb(tsk_app_writer); + tsk_app_set_fatal_cb(tsk_app_writer); + + tsk_stat_set_path_file(tsk_stat_get_path(), tsk_stat_get_filename()); + tsk_stat_init(tsk_stat_get_interval()); + + if( tsk_stat_start() != 0 ){ + exit (-1); + } + + TSK_APP_INFO("Doubango started"); + while (fgets(command, sizeof(command), stdin) != NULL) { if (tsk_strnicmp(command, "quit", 4) == 0) { printf("+++ quit() +++");