整理代码

This commit is contained in:
xiongziliang 2019-03-27 18:56:49 +08:00
parent b1a2de3853
commit e44c2d5508
14 changed files with 73 additions and 91 deletions

View File

@ -273,6 +273,20 @@ onceToken token([](){
} //namespace Hls } //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 } // namespace mediakit

View File

@ -234,6 +234,29 @@ extern const char kFileBufSize[];
extern const char kFilePath[]; extern const char kFilePath[];
} //namespace Hls } //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 } // namespace mediakit
#endif /* COMMON_CONFIG_H */ #endif /* COMMON_CONFIG_H */

View File

@ -33,18 +33,6 @@ using namespace toolkit;
namespace mediakit { 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) { PlayerBase::Ptr PlayerBase::createPlayer(const string &strUrl) {
static auto releasePlayer = [](PlayerBase *ptr){ static auto releasePlayer = [](PlayerBase *ptr){
onceToken token(nullptr,[&](){ onceToken token(nullptr,[&](){
@ -63,7 +51,7 @@ PlayerBase::Ptr PlayerBase::createPlayer(const string &strUrl) {
} }
PlayerBase::PlayerBase() { PlayerBase::PlayerBase() {
this->mINI::operator[](kPlayTimeoutMS) = 10000; this->mINI::operator[](kTimeoutMS) = 10000;
this->mINI::operator[](kMediaTimeoutMS) = 5000; this->mINI::operator[](kMediaTimeoutMS) = 5000;
this->mINI::operator[](kBeatIntervalMS) = 5000; this->mINI::operator[](kBeatIntervalMS) = 5000;
this->mINI::operator[](kMaxAnalysisMS) = 2000; this->mINI::operator[](kMaxAnalysisMS) = 2000;

View File

@ -88,27 +88,6 @@ public:
typedef std::shared_ptr<PlayerBase> Ptr; typedef std::shared_ptr<PlayerBase> Ptr;
static Ptr createPlayer(const string &strUrl); 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(); PlayerBase();
virtual ~PlayerBase(){} virtual ~PlayerBase(){}

View File

@ -31,20 +31,10 @@
#include "Rtmp/RtmpPusher.h" #include "Rtmp/RtmpPusher.h"
using namespace toolkit; using namespace toolkit;
using namespace mediakit::Client;
namespace mediakit { 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, PusherBase::Ptr PusherBase::createPusher(const MediaSource::Ptr &src,
const string & strUrl) { const string & strUrl) {
static auto releasePusher = [](PusherBase *ptr){ static auto releasePusher = [](PusherBase *ptr){
@ -64,8 +54,7 @@ PusherBase::Ptr PusherBase::createPusher(const MediaSource::Ptr &src,
} }
PusherBase::PusherBase() { PusherBase::PusherBase() {
this->mINI::operator[](kPlayTimeoutMS) = 10000; this->mINI::operator[](kTimeoutMS) = 10000;
this->mINI::operator[](kMediaTimeoutMS) = 5000;
this->mINI::operator[](kBeatIntervalMS) = 5000; this->mINI::operator[](kBeatIntervalMS) = 5000;
} }

View File

@ -42,30 +42,11 @@ namespace mediakit {
class PusherBase : public mINI{ class PusherBase : public mINI{
public: public:
typedef std::shared_ptr<PusherBase> Ptr; typedef std::shared_ptr<PusherBase> Ptr;
typedef std::function<void(const SockException &ex)> Event;
static Ptr createPusher(const MediaSource::Ptr &src, static Ptr createPusher(const MediaSource::Ptr &src,
const string &strUrl); 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<void(const SockException &ex)> Event;
PusherBase(); PusherBase();
virtual ~PusherBase(){} virtual ~PusherBase(){}

View File

@ -31,6 +31,7 @@
#include "Util/onceToken.h" #include "Util/onceToken.h"
#include "Thread/ThreadPool.h" #include "Thread/ThreadPool.h"
using namespace toolkit; using namespace toolkit;
using namespace mediakit::Client;
namespace mediakit { namespace mediakit {
@ -93,12 +94,12 @@ void RtmpPlayer::play(const string &strUrl) {
//服务器域名 //服务器域名
strHost = FindField(strHost.data(), NULL, ":"); strHost = FindField(strHost.data(), NULL, ":");
} }
if(!(*this)[PlayerBase::kNetAdapter].empty()){ if(!(*this)[kNetAdapter].empty()){
setNetAdapter((*this)[PlayerBase::kNetAdapter]); setNetAdapter((*this)[kNetAdapter]);
} }
weak_ptr<RtmpPlayer> weakSelf= dynamic_pointer_cast<RtmpPlayer>(shared_from_this()); weak_ptr<RtmpPlayer> weakSelf= dynamic_pointer_cast<RtmpPlayer>(shared_from_this());
float playTimeOutSec = (*this)[kPlayTimeoutMS].as<int>() / 1000.0; float playTimeOutSec = (*this)[kTimeoutMS].as<int>() / 1000.0;
_pPlayTimer.reset( new Timer(playTimeOutSec, [weakSelf]() { _pPlayTimer.reset( new Timer(playTimeOutSec, [weakSelf]() {
auto strongSelf=weakSelf.lock(); auto strongSelf=weakSelf.lock();
if(!strongSelf) { if(!strongSelf) {

View File

@ -39,7 +39,9 @@
#include "Util/TimeTicker.h" #include "Util/TimeTicker.h"
#include "Network/Socket.h" #include "Network/Socket.h"
#include "Network/TcpClient.h" #include "Network/TcpClient.h"
using namespace toolkit; using namespace toolkit;
using namespace mediakit::Client;
namespace mediakit { namespace mediakit {

View File

@ -36,6 +36,7 @@
#include "Poller/Timer.h" #include "Poller/Timer.h"
#include "Util/TimeTicker.h" #include "Util/TimeTicker.h"
using namespace toolkit; using namespace toolkit;
using namespace mediakit::Client;
namespace mediakit { namespace mediakit {
@ -57,7 +58,7 @@ public:
seekToMilliSecond(fProgress * getDuration() * 1000); seekToMilliSecond(fProgress * getDuration() * 1000);
}; };
void play(const string &strUrl) override { void play(const string &strUrl) override {
_analysisMs = (*this)[PlayerBase::kMaxAnalysisMS].as<int>(); _analysisMs = (*this)[kMaxAnalysisMS].as<int>();
PlayerImp<RtmpPlayer,RtmpDemuxer>::play(strUrl); PlayerImp<RtmpPlayer,RtmpDemuxer>::play(strUrl);
} }
private: private:

View File

@ -30,6 +30,7 @@
#include "Util/onceToken.h" #include "Util/onceToken.h"
#include "Thread/ThreadPool.h" #include "Thread/ThreadPool.h"
using namespace toolkit; using namespace toolkit;
using namespace mediakit::Client;
namespace mediakit { namespace mediakit {
@ -85,7 +86,7 @@ void RtmpPusher::publish(const string &strUrl) {
} }
weak_ptr<RtmpPusher> weakSelf = dynamic_pointer_cast<RtmpPusher>(shared_from_this()); weak_ptr<RtmpPusher> weakSelf = dynamic_pointer_cast<RtmpPusher>(shared_from_this());
float playTimeOutSec = (*this)[kPlayTimeoutMS].as<int>() / 1000.0; float playTimeOutSec = (*this)[kTimeoutMS].as<int>() / 1000.0;
_pPublishTimer.reset( new Timer(playTimeOutSec, [weakSelf]() { _pPublishTimer.reset( new Timer(playTimeOutSec, [weakSelf]() {
auto strongSelf=weakSelf.lock(); auto strongSelf=weakSelf.lock();
if(!strongSelf) { if(!strongSelf) {

View File

@ -39,6 +39,7 @@
#include "Util/base64.h" #include "Util/base64.h"
#include "Network/sockutil.h" #include "Network/sockutil.h"
using namespace toolkit; using namespace toolkit;
using namespace mediakit::Client;
namespace mediakit { namespace mediakit {
@ -129,7 +130,7 @@ void RtspPlayer::play(const string &strUrl, const string &strUser, const string
_strUrl = strUrl; _strUrl = strUrl;
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this()); weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
float playTimeOutSec = (*this)[kPlayTimeoutMS].as<int>() / 1000.0; float playTimeOutSec = (*this)[kTimeoutMS].as<int>() / 1000.0;
_pPlayTimer.reset( new Timer(playTimeOutSec, [weakSelf]() { _pPlayTimer.reset( new Timer(playTimeOutSec, [weakSelf]() {
auto strongSelf=weakSelf.lock(); auto strongSelf=weakSelf.lock();
if(!strongSelf) { if(!strongSelf) {
@ -140,8 +141,8 @@ void RtspPlayer::play(const string &strUrl, const string &strUser, const string
return false; return false;
},getPoller())); },getPoller()));
if(!(*this)[PlayerBase::kNetAdapter].empty()){ if(!(*this)[kNetAdapter].empty()){
setNetAdapter((*this)[PlayerBase::kNetAdapter]); setNetAdapter((*this)[kNetAdapter]);
} }
startConnect(ip, port , playTimeOutSec); startConnect(ip, port , playTimeOutSec);
} }
@ -531,7 +532,7 @@ bool RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC
header.emplace("Session",_strSession); header.emplace("Session",_strSession);
} }
if(!_rtspRealm.empty() && !(*this)[PlayerBase::kRtspUser].empty()){ if(!_rtspRealm.empty() && !(*this)[kRtspUser].empty()){
if(!_rtspMd5Nonce.empty()){ if(!_rtspMd5Nonce.empty()){
//MD5认证 //MD5认证
/* /*
@ -542,22 +543,22 @@ bool RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC
(2)password为ANSI字符串, (2)password为ANSI字符串,
response= md5( md5(username:realm:password):nonce:md5(public_method:url) ); response= md5( md5(username:realm:password):nonce:md5(public_method:url) );
*/ */
string encrypted_pwd = (*this)[PlayerBase::kRtspPwd]; string encrypted_pwd = (*this)[kRtspPwd];
if(!(*this)[PlayerBase::kRtspPwdIsMD5].as<bool>()){ if(!(*this)[kRtspPwdIsMD5].as<bool>()){
encrypted_pwd = MD5((*this)[PlayerBase::kRtspUser]+ ":" + _rtspRealm + ":" + encrypted_pwd).hexdigest(); encrypted_pwd = MD5((*this)[kRtspUser]+ ":" + _rtspRealm + ":" + encrypted_pwd).hexdigest();
} }
auto response = MD5( encrypted_pwd + ":" + _rtspMd5Nonce + ":" + MD5(cmd + ":" + url).hexdigest()).hexdigest(); auto response = MD5( encrypted_pwd + ":" + _rtspMd5Nonce + ":" + MD5(cmd + ":" + url).hexdigest()).hexdigest();
_StrPrinter printer; _StrPrinter printer;
printer << "Digest "; printer << "Digest ";
printer << "username=\"" << (*this)[PlayerBase::kRtspUser] << "\", "; printer << "username=\"" << (*this)[kRtspUser] << "\", ";
printer << "realm=\"" << _rtspRealm << "\", "; printer << "realm=\"" << _rtspRealm << "\", ";
printer << "nonce=\"" << _rtspMd5Nonce << "\", "; printer << "nonce=\"" << _rtspMd5Nonce << "\", ";
printer << "uri=\"" << url << "\", "; printer << "uri=\"" << url << "\", ";
printer << "response=\"" << response << "\""; printer << "response=\"" << response << "\"";
header.emplace("Authorization",printer); header.emplace("Authorization",printer);
}else if(!(*this)[PlayerBase::kRtspPwdIsMD5].as<bool>()){ }else if(!(*this)[kRtspPwdIsMD5].as<bool>()){
//base64认证 //base64认证
string authStr = StrPrinter << (*this)[PlayerBase::kRtspUser] << ":" << (*this)[PlayerBase::kRtspPwd]; string authStr = StrPrinter << (*this)[kRtspUser] << ":" << (*this)[kRtspPwd];
char authStrBase64[1024] = {0}; char authStrBase64[1024] = {0};
av_base64_encode(authStrBase64,sizeof(authStrBase64),(uint8_t *)authStr.data(),authStr.size()); av_base64_encode(authStrBase64,sizeof(authStrBase64),(uint8_t *)authStr.data(),authStr.size());
header.emplace("Authorization",StrPrinter << "Basic " << authStrBase64 ); header.emplace("Authorization",StrPrinter << "Basic " << authStrBase64 );

View File

@ -7,6 +7,8 @@
#include "RtspPusher.h" #include "RtspPusher.h"
#include "RtspSession.h" #include "RtspSession.h"
using namespace mediakit::Client;
namespace mediakit { namespace mediakit {
static int kSockFlags = SOCKET_DEFAULE_FLAGS | FLAG_MORE; static int kSockFlags = SOCKET_DEFAULE_FLAGS | FLAG_MORE;
@ -44,7 +46,7 @@ void RtspPusher::teardown() {
void RtspPusher::publish(const string &strUrl) { void RtspPusher::publish(const string &strUrl) {
auto userAndPwd = FindField(strUrl.data(),"://","@"); 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()){ if(userAndPwd.empty()){
publish(strUrl,"","",eType); publish(strUrl,"","",eType);
return; return;
@ -68,11 +70,11 @@ void RtspPusher::publish(const string & strUrl, const string &strUser, const str
teardown(); teardown();
if(strUser.size()){ if(strUser.size()){
(*this)[PlayerBase::kRtspUser] = strUser; (*this)[kRtspUser] = strUser;
} }
if(strPwd.size()){ if(strPwd.size()){
(*this)[PlayerBase::kRtspPwd] = strPwd; (*this)[kRtspPwd] = strPwd;
(*this)[PlayerBase::kRtspPwdIsMD5] = false; (*this)[kRtspPwdIsMD5] = false;
} }
_eType = eType; _eType = eType;
@ -93,7 +95,7 @@ void RtspPusher::publish(const string & strUrl, const string &strUser, const str
_strUrl = strUrl; _strUrl = strUrl;
weak_ptr<RtspPusher> weakSelf = dynamic_pointer_cast<RtspPusher>(shared_from_this()); weak_ptr<RtspPusher> weakSelf = dynamic_pointer_cast<RtspPusher>(shared_from_this());
float playTimeOutSec = (*this)[kPlayTimeoutMS].as<int>() / 1000.0; float playTimeOutSec = (*this)[kTimeoutMS].as<int>() / 1000.0;
_pPublishTimer.reset( new Timer(playTimeOutSec, [weakSelf]() { _pPublishTimer.reset( new Timer(playTimeOutSec, [weakSelf]() {
auto strongSelf=weakSelf.lock(); auto strongSelf=weakSelf.lock();
if(!strongSelf) { if(!strongSelf) {

View File

@ -1,4 +1,4 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2016 xiongziliang <771730766@qq.com> * Copyright (c) 2016 xiongziliang <771730766@qq.com>
@ -61,7 +61,7 @@ int main(int argc, char *argv[]) {
for (auto &url : urlList) { for (auto &url : urlList) {
//创建下载器 //创建下载器
HttpDownloader::Ptr downloader(new HttpDownloader()); 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======================="; DebugL << "=====================HttpDownloader result=======================";
//下载结果回调 //下载结果回调
if (code == Err_success) { if (code == Err_success) {

View File

@ -1,4 +1,4 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2016 xiongziliang <771730766@qq.com> * 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())); PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "live", to_string(i).data()));
//指定RTP over TCP(播放rtsp时有效) //指定RTP over TCP(播放rtsp时有效)
(*player)[RtspPlayer::kRtpType] = PlayerBase::RTP_TCP; (*player)[RtspPlayer::kRtpType] = Rtsp::RTP_TCP;
//开始播放,如果播放失败或者播放中止,将会自动重试若干次,重试次数在配置文件中配置,默认一直重试 //开始播放,如果播放失败或者播放中止,将会自动重试若干次,重试次数在配置文件中配置,默认一直重试
player->play(url); player->play(url);
//需要保存PlayerProxy否则作用域结束就会销毁该对象 //需要保存PlayerProxy否则作用域结束就会销毁该对象