From e44c2d5508584c12bcd00f235c9f48a3e331dc11 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 27 Mar 2019 18:56:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/config.cpp | 14 ++++++++++++++ src/Common/config.h | 23 +++++++++++++++++++++++ src/Player/PlayerBase.cpp | 14 +------------- src/Player/PlayerBase.h | 21 --------------------- src/Pusher/PusherBase.cpp | 15 ++------------- src/Pusher/PusherBase.h | 21 +-------------------- src/Rtmp/RtmpPlayer.cpp | 7 ++++--- src/Rtmp/RtmpPlayer.h | 2 ++ src/Rtmp/RtmpPlayerImp.h | 3 ++- src/Rtmp/RtmpPusher.cpp | 3 ++- src/Rtsp/RtspPlayer.cpp | 21 +++++++++++---------- src/Rtsp/RtspPusher.cpp | 12 +++++++----- tests/test_httpClient.cpp | 4 ++-- tests/test_server.cpp | 4 ++-- 14 files changed, 73 insertions(+), 91 deletions(-) diff --git a/src/Common/config.cpp b/src/Common/config.cpp index e4af49b0..8139c467 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -273,6 +273,20 @@ onceToken token([](){ } //namespace Hls + +namespace Client { +const char kNetAdapter[] = "net_adapter"; +const char kRtpType[] = "rtp_type"; +const char kRtspUser[] = "rtsp_user" ; +const char kRtspPwd[] = "rtsp_pwd"; +const char kRtspPwdIsMD5[] = "rtsp_pwd_md5"; +const char kTimeoutMS[] = "protocol_timeout_ms"; +const char kMediaTimeoutMS[] = "media_timeout_ms"; +const char kBeatIntervalMS[] = "beat_interval_ms"; +const char kMaxAnalysisMS[] = "max_analysis_ms"; + +} + } // namespace mediakit diff --git a/src/Common/config.h b/src/Common/config.h index cd678c76..080f94e4 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -234,6 +234,29 @@ extern const char kFileBufSize[]; extern const char kFilePath[]; } //namespace Hls + + +namespace Client { +//指定网卡ip +extern const char kNetAdapter[]; +//设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播) +//设置方法:player[PlayerBase::kRtpType] = 0/1/2; +extern const char kRtpType[]; +//rtsp认证用户名 +extern const char kRtspUser[]; +//rtsp认证用用户密码,可以是明文也可以是md5,md5密码生成方式 md5(username:realm:password) +extern const char kRtspPwd[]; +//rtsp认证用用户密码是否为md5类型 +extern const char kRtspPwdIsMD5[]; +//握手超时时间,默认10,000 毫秒 +extern const char kTimeoutMS[]; +//rtp/rtmp包接收超时时间,默认5000秒 +extern const char kMediaTimeoutMS[]; +//rtsp/rtmp心跳时间,默认5000毫秒 +extern const char kBeatIntervalMS[]; +//Track编码格式探测最大时间,单位毫秒,默认2000 +extern const char kMaxAnalysisMS[]; +} } // namespace mediakit #endif /* COMMON_CONFIG_H */ diff --git a/src/Player/PlayerBase.cpp b/src/Player/PlayerBase.cpp index 9276d0ca..430073f3 100644 --- a/src/Player/PlayerBase.cpp +++ b/src/Player/PlayerBase.cpp @@ -33,18 +33,6 @@ using namespace toolkit; namespace mediakit { -const char PlayerBase::kNetAdapter[] = "net_adapter"; -const char PlayerBase::kRtpType[] = "rtp_type"; -const char PlayerBase::kRtspUser[] = "rtsp_user" ; -const char PlayerBase::kRtspPwd[] = "rtsp_pwd"; -const char PlayerBase::kRtspPwdIsMD5[] = "rtsp_pwd_md5"; - -const char PlayerBase::kPlayTimeoutMS[] = "play_timeout_ms"; -const char PlayerBase::kMediaTimeoutMS[] = "media_timeout_ms"; -const char PlayerBase::kBeatIntervalMS[] = "beat_interval_ms"; -const char PlayerBase::kMaxAnalysisMS[] = "max_analysis_ms"; - - PlayerBase::Ptr PlayerBase::createPlayer(const string &strUrl) { static auto releasePlayer = [](PlayerBase *ptr){ onceToken token(nullptr,[&](){ @@ -63,7 +51,7 @@ PlayerBase::Ptr PlayerBase::createPlayer(const string &strUrl) { } PlayerBase::PlayerBase() { - this->mINI::operator[](kPlayTimeoutMS) = 10000; + this->mINI::operator[](kTimeoutMS) = 10000; this->mINI::operator[](kMediaTimeoutMS) = 5000; this->mINI::operator[](kBeatIntervalMS) = 5000; this->mINI::operator[](kMaxAnalysisMS) = 2000; diff --git a/src/Player/PlayerBase.h b/src/Player/PlayerBase.h index f222ae8e..c3770b65 100644 --- a/src/Player/PlayerBase.h +++ b/src/Player/PlayerBase.h @@ -88,27 +88,6 @@ public: typedef std::shared_ptr Ptr; static Ptr createPlayer(const string &strUrl); - //指定网卡ip - static const char kNetAdapter[]; - //设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播) - //设置方法:player[PlayerBase::kRtpType] = 0/1/2; - static const char kRtpType[]; - //rtsp认证用户名 - static const char kRtspUser[]; - //rtsp认证用用户密码,可以是明文也可以是md5,md5密码生成方式 md5(username:realm:password) - static const char kRtspPwd[]; - //rtsp认证用用户密码是否为md5类型 - static const char kRtspPwdIsMD5[]; - //播放超时时间,默认10,000 毫秒 - static const char kPlayTimeoutMS[]; - //rtp/rtmp包接收超时时间,默认5000秒 - static const char kMediaTimeoutMS[]; - //rtsp/rtmp心跳时间,默认5000毫秒 - static const char kBeatIntervalMS[]; - //Track编码格式探测最大时间,单位毫秒,默认2000 - static const char kMaxAnalysisMS[]; - - PlayerBase(); virtual ~PlayerBase(){} diff --git a/src/Pusher/PusherBase.cpp b/src/Pusher/PusherBase.cpp index e42d4d2c..2e295005 100644 --- a/src/Pusher/PusherBase.cpp +++ b/src/Pusher/PusherBase.cpp @@ -31,20 +31,10 @@ #include "Rtmp/RtmpPusher.h" using namespace toolkit; +using namespace mediakit::Client; namespace mediakit { -const char PusherBase::kNetAdapter[] = "net_adapter"; -const char PusherBase::kRtpType[] = "rtp_type"; -const char PusherBase::kRtspUser[] = "rtsp_user" ; -const char PusherBase::kRtspPwd[] = "rtsp_pwd"; -const char PusherBase::kRtspPwdIsMD5[] = "rtsp_pwd_md5"; - -const char PusherBase::kPlayTimeoutMS[] = "play_timeout_ms"; -const char PusherBase::kMediaTimeoutMS[] = "media_timeout_ms"; -const char PusherBase::kBeatIntervalMS[] = "beat_interval_ms"; - - PusherBase::Ptr PusherBase::createPusher(const MediaSource::Ptr &src, const string & strUrl) { static auto releasePusher = [](PusherBase *ptr){ @@ -64,8 +54,7 @@ PusherBase::Ptr PusherBase::createPusher(const MediaSource::Ptr &src, } PusherBase::PusherBase() { - this->mINI::operator[](kPlayTimeoutMS) = 10000; - this->mINI::operator[](kMediaTimeoutMS) = 5000; + this->mINI::operator[](kTimeoutMS) = 10000; this->mINI::operator[](kBeatIntervalMS) = 5000; } diff --git a/src/Pusher/PusherBase.h b/src/Pusher/PusherBase.h index fc809d74..e1d278d7 100644 --- a/src/Pusher/PusherBase.h +++ b/src/Pusher/PusherBase.h @@ -42,30 +42,11 @@ namespace mediakit { class PusherBase : public mINI{ public: typedef std::shared_ptr Ptr; + typedef std::function Event; static Ptr createPusher(const MediaSource::Ptr &src, const string &strUrl); - //指定网卡ip - static const char kNetAdapter[]; - //设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播) - //设置方法:player[PusherBase::kRtpType] = 0/1/2; - static const char kRtpType[]; - //rtsp认证用户名 - static const char kRtspUser[]; - //rtsp认证用用户密码,可以是明文也可以是md5,md5密码生成方式 md5(username:realm:password) - static const char kRtspPwd[]; - //rtsp认证用用户密码是否为md5类型 - static const char kRtspPwdIsMD5[]; - //播放超时时间,默认10,000 毫秒 - static const char kPlayTimeoutMS[]; - //rtp/rtmp包接收超时时间,默认5000秒 - static const char kMediaTimeoutMS[]; - //rtsp/rtmp心跳时间,默认5000毫秒 - static const char kBeatIntervalMS[]; - - typedef std::function Event; - PusherBase(); virtual ~PusherBase(){} diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index 1d8f5d42..ad2bf468 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -31,6 +31,7 @@ #include "Util/onceToken.h" #include "Thread/ThreadPool.h" using namespace toolkit; +using namespace mediakit::Client; namespace mediakit { @@ -93,12 +94,12 @@ void RtmpPlayer::play(const string &strUrl) { //服务器域名 strHost = FindField(strHost.data(), NULL, ":"); } - if(!(*this)[PlayerBase::kNetAdapter].empty()){ - setNetAdapter((*this)[PlayerBase::kNetAdapter]); + if(!(*this)[kNetAdapter].empty()){ + setNetAdapter((*this)[kNetAdapter]); } weak_ptr weakSelf= dynamic_pointer_cast(shared_from_this()); - float playTimeOutSec = (*this)[kPlayTimeoutMS].as() / 1000.0; + float playTimeOutSec = (*this)[kTimeoutMS].as() / 1000.0; _pPlayTimer.reset( new Timer(playTimeOutSec, [weakSelf]() { auto strongSelf=weakSelf.lock(); if(!strongSelf) { diff --git a/src/Rtmp/RtmpPlayer.h b/src/Rtmp/RtmpPlayer.h index f9996234..244dedf6 100644 --- a/src/Rtmp/RtmpPlayer.h +++ b/src/Rtmp/RtmpPlayer.h @@ -39,7 +39,9 @@ #include "Util/TimeTicker.h" #include "Network/Socket.h" #include "Network/TcpClient.h" + using namespace toolkit; +using namespace mediakit::Client; namespace mediakit { diff --git a/src/Rtmp/RtmpPlayerImp.h b/src/Rtmp/RtmpPlayerImp.h index 1051fd5b..92ee92cf 100644 --- a/src/Rtmp/RtmpPlayerImp.h +++ b/src/Rtmp/RtmpPlayerImp.h @@ -36,6 +36,7 @@ #include "Poller/Timer.h" #include "Util/TimeTicker.h" using namespace toolkit; +using namespace mediakit::Client; namespace mediakit { @@ -57,7 +58,7 @@ public: seekToMilliSecond(fProgress * getDuration() * 1000); }; void play(const string &strUrl) override { - _analysisMs = (*this)[PlayerBase::kMaxAnalysisMS].as(); + _analysisMs = (*this)[kMaxAnalysisMS].as(); PlayerImp::play(strUrl); } private: diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index 07592b3b..aade6a63 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -30,6 +30,7 @@ #include "Util/onceToken.h" #include "Thread/ThreadPool.h" using namespace toolkit; +using namespace mediakit::Client; namespace mediakit { @@ -85,7 +86,7 @@ void RtmpPusher::publish(const string &strUrl) { } weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - float playTimeOutSec = (*this)[kPlayTimeoutMS].as() / 1000.0; + float playTimeOutSec = (*this)[kTimeoutMS].as() / 1000.0; _pPublishTimer.reset( new Timer(playTimeOutSec, [weakSelf]() { auto strongSelf=weakSelf.lock(); if(!strongSelf) { diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index 2799c2bf..c1ea1eec 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -39,6 +39,7 @@ #include "Util/base64.h" #include "Network/sockutil.h" using namespace toolkit; +using namespace mediakit::Client; namespace mediakit { @@ -129,7 +130,7 @@ void RtspPlayer::play(const string &strUrl, const string &strUser, const string _strUrl = strUrl; weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - float playTimeOutSec = (*this)[kPlayTimeoutMS].as() / 1000.0; + float playTimeOutSec = (*this)[kTimeoutMS].as() / 1000.0; _pPlayTimer.reset( new Timer(playTimeOutSec, [weakSelf]() { auto strongSelf=weakSelf.lock(); if(!strongSelf) { @@ -140,8 +141,8 @@ void RtspPlayer::play(const string &strUrl, const string &strUser, const string return false; },getPoller())); - if(!(*this)[PlayerBase::kNetAdapter].empty()){ - setNetAdapter((*this)[PlayerBase::kNetAdapter]); + if(!(*this)[kNetAdapter].empty()){ + setNetAdapter((*this)[kNetAdapter]); } startConnect(ip, port , playTimeOutSec); } @@ -531,7 +532,7 @@ bool RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC header.emplace("Session",_strSession); } - if(!_rtspRealm.empty() && !(*this)[PlayerBase::kRtspUser].empty()){ + if(!_rtspRealm.empty() && !(*this)[kRtspUser].empty()){ if(!_rtspMd5Nonce.empty()){ //MD5认证 /* @@ -542,22 +543,22 @@ bool RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC (2)当password为ANSI字符串,则 response= md5( md5(username:realm:password):nonce:md5(public_method:url) ); */ - string encrypted_pwd = (*this)[PlayerBase::kRtspPwd]; - if(!(*this)[PlayerBase::kRtspPwdIsMD5].as()){ - encrypted_pwd = MD5((*this)[PlayerBase::kRtspUser]+ ":" + _rtspRealm + ":" + encrypted_pwd).hexdigest(); + string encrypted_pwd = (*this)[kRtspPwd]; + if(!(*this)[kRtspPwdIsMD5].as()){ + encrypted_pwd = MD5((*this)[kRtspUser]+ ":" + _rtspRealm + ":" + encrypted_pwd).hexdigest(); } auto response = MD5( encrypted_pwd + ":" + _rtspMd5Nonce + ":" + MD5(cmd + ":" + url).hexdigest()).hexdigest(); _StrPrinter printer; printer << "Digest "; - printer << "username=\"" << (*this)[PlayerBase::kRtspUser] << "\", "; + printer << "username=\"" << (*this)[kRtspUser] << "\", "; printer << "realm=\"" << _rtspRealm << "\", "; printer << "nonce=\"" << _rtspMd5Nonce << "\", "; printer << "uri=\"" << url << "\", "; printer << "response=\"" << response << "\""; header.emplace("Authorization",printer); - }else if(!(*this)[PlayerBase::kRtspPwdIsMD5].as()){ + }else if(!(*this)[kRtspPwdIsMD5].as()){ //base64认证 - string authStr = StrPrinter << (*this)[PlayerBase::kRtspUser] << ":" << (*this)[PlayerBase::kRtspPwd]; + string authStr = StrPrinter << (*this)[kRtspUser] << ":" << (*this)[kRtspPwd]; char authStrBase64[1024] = {0}; av_base64_encode(authStrBase64,sizeof(authStrBase64),(uint8_t *)authStr.data(),authStr.size()); header.emplace("Authorization",StrPrinter << "Basic " << authStrBase64 ); diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index 009470e1..957cbe45 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -7,6 +7,8 @@ #include "RtspPusher.h" #include "RtspSession.h" +using namespace mediakit::Client; + namespace mediakit { static int kSockFlags = SOCKET_DEFAULE_FLAGS | FLAG_MORE; @@ -44,7 +46,7 @@ void RtspPusher::teardown() { void RtspPusher::publish(const string &strUrl) { auto userAndPwd = FindField(strUrl.data(),"://","@"); - Rtsp::eRtpType eType = (Rtsp::eRtpType)(int)(*this)[ PlayerBase::kRtpType]; + Rtsp::eRtpType eType = (Rtsp::eRtpType)(int)(*this)[ kRtpType]; if(userAndPwd.empty()){ publish(strUrl,"","",eType); return; @@ -68,11 +70,11 @@ void RtspPusher::publish(const string & strUrl, const string &strUser, const str teardown(); if(strUser.size()){ - (*this)[PlayerBase::kRtspUser] = strUser; + (*this)[kRtspUser] = strUser; } if(strPwd.size()){ - (*this)[PlayerBase::kRtspPwd] = strPwd; - (*this)[PlayerBase::kRtspPwdIsMD5] = false; + (*this)[kRtspPwd] = strPwd; + (*this)[kRtspPwdIsMD5] = false; } _eType = eType; @@ -93,7 +95,7 @@ void RtspPusher::publish(const string & strUrl, const string &strUser, const str _strUrl = strUrl; weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - float playTimeOutSec = (*this)[kPlayTimeoutMS].as() / 1000.0; + float playTimeOutSec = (*this)[kTimeoutMS].as() / 1000.0; _pPublishTimer.reset( new Timer(playTimeOutSec, [weakSelf]() { auto strongSelf=weakSelf.lock(); if(!strongSelf) { diff --git a/tests/test_httpClient.cpp b/tests/test_httpClient.cpp index 381126d9..786048d6 100644 --- a/tests/test_httpClient.cpp +++ b/tests/test_httpClient.cpp @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2016 xiongziliang <771730766@qq.com> @@ -61,7 +61,7 @@ int main(int argc, char *argv[]) { for (auto &url : urlList) { //创建下载器 HttpDownloader::Ptr downloader(new HttpDownloader()); - downloader->setOnResult([](ErrCode code, const char *errMsg, const char *filePath) { + downloader->setOnResult([](ErrCode code, const string &errMsg, const string &filePath) { DebugL << "=====================HttpDownloader result======================="; //下载结果回调 if (code == Err_success) { diff --git a/tests/test_server.cpp b/tests/test_server.cpp index 56112c1b..e245a44e 100644 --- a/tests/test_server.cpp +++ b/tests/test_server.cpp @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2016 xiongziliang <771730766@qq.com> @@ -259,7 +259,7 @@ int main(int argc,char *argv[]) { PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "live", to_string(i).data())); //指定RTP over TCP(播放rtsp时有效) - (*player)[RtspPlayer::kRtpType] = PlayerBase::RTP_TCP; + (*player)[RtspPlayer::kRtpType] = Rtsp::RTP_TCP; //开始播放,如果播放失败或者播放中止,将会自动重试若干次,重试次数在配置文件中配置,默认一直重试 player->play(url); //需要保存PlayerProxy,否则作用域结束就会销毁该对象