Commit bca084d0 by xiongziliang

优化rtsp播放器代码,支持md5认证

parent 58b58418
...@@ -38,6 +38,11 @@ namespace ZL { ...@@ -38,6 +38,11 @@ namespace ZL {
namespace Player { namespace Player {
const char PlayerBase::kNetAdapter[] = "net_adapter"; 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";
PlayerBase::Ptr PlayerBase::createPlayer(const char* strUrl) { PlayerBase::Ptr PlayerBase::createPlayer(const char* strUrl) {
string prefix = FindField(strUrl, NULL, "://"); string prefix = FindField(strUrl, NULL, "://");
......
...@@ -51,7 +51,18 @@ public: ...@@ -51,7 +51,18 @@ public:
RTP_MULTICAST = 2, RTP_MULTICAST = 2,
} eRtpType; } eRtpType;
static Ptr createPlayer(const char* strUrl); static Ptr createPlayer(const char* strUrl);
//指定网卡ip
static const char kNetAdapter[]; 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[];
PlayerBase(){}; PlayerBase(){};
virtual ~PlayerBase(){}; virtual ~PlayerBase(){};
......
...@@ -54,9 +54,6 @@ namespace Rtsp { ...@@ -54,9 +54,6 @@ namespace Rtsp {
class RtspPlayer: public PlayerBase,public TcpClient { class RtspPlayer: public PlayerBase,public TcpClient {
public: public:
typedef std::shared_ptr<RtspPlayer> Ptr; typedef std::shared_ptr<RtspPlayer> Ptr;
//设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播)
//设置方法:player[RtspPlayer::kRtpType] = 0/1/2;
static const char kRtpType[];
RtspPlayer(); RtspPlayer();
virtual ~RtspPlayer(void); virtual ~RtspPlayer(void);
...@@ -71,79 +68,38 @@ protected: ...@@ -71,79 +68,38 @@ protected:
float getProgressTime() const; float getProgressTime() const;
void seekToTime(float fTime); void seekToTime(float fTime);
private: private:
void _onShutdown(const SockException &ex) { void onShutdown_l(const SockException &ex);
WarnL << ex.getErrCode() << " " << ex.what(); void onRecvRTP_l(const RtpPacket::Ptr &pRtppt, int iTrackidx);
m_pPlayTimer.reset(); void onRecvRTP_l(const RtpPacket::Ptr &pRtppt, const RtspTrack &track);
m_pRtpTimer.reset(); void onPlayResult_l(const SockException &ex);
m_pBeatTimer.reset();
onShutdown(ex); int getTrackIndex(const string &controlSuffix) const;
} int getTrackIndex(int iTrackId) const;
void _onRecvRTP(const RtpPacket::Ptr &pRtppt, const RtspTrack &track) {
m_rtpTicker.resetTime();
onRecvRTP(pRtppt,track);
}
void _onPlayResult(const SockException &ex) {
WarnL << ex.getErrCode() << " " << ex.what();
m_pPlayTimer.reset();
m_pRtpTimer.reset();
if (!ex) {
m_rtpTicker.resetTime();
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
m_pRtpTimer.reset( new Timer(5, [weakSelf]() {
auto strongSelf=weakSelf.lock();
if(!strongSelf) {
return false;
}
if(strongSelf->m_rtpTicker.elapsedTime()>10000) {
//recv rtp timeout!
strongSelf->_onShutdown(SockException(Err_timeout,"recv rtp timeout"));
strongSelf->teardown();
return false;
}
return true;
}));
}
onPlayResult(ex);
}
void play(const char* strUrl, const char *strUser, const char *strPwd, eRtpType eType); void play(const char* strUrl, const char *strUser, const char *strPwd, eRtpType eType);
void onConnect(const SockException &err) override; void onConnect(const SockException &err) override;
void onRecv(const Buffer::Ptr &pBuf) override; void onRecv(const Buffer::Ptr &pBuf) override;
void onErr(const SockException &ex) override; void onErr(const SockException &ex) override;
void HandleResSETUP(const Parser &parser, unsigned int uiTrackIndex); void handleResSETUP(const Parser &parser, unsigned int uiTrackIndex);
void HandleResDESCRIBE(const Parser &parser); void handleResDESCRIBE(const Parser &parser);
void HandleResPAUSE(const Parser &parser, bool bPause); bool handleAuthenticationFailure(const string &wwwAuthenticateParamsStr);
void handleResPAUSE(const Parser &parser, bool bPause);
//发数据给服务器 //发数据给服务器
inline int write(const char *strMsg, ...); int onProcess(const char* strBuf);
inline int onProcess(const char* strBuf);
//生成rtp包结构体 //生成rtp包结构体
inline void splitRtp(unsigned char *pucData, unsigned int uiLen); void splitRtp(unsigned char *pucData, unsigned int uiLen);
//发送SETUP命令
inline void sendSetup(unsigned int uiTrackIndex);
inline void sendPause(bool bPause,float fTime);
//处理一个rtp包 //处理一个rtp包
inline bool HandleOneRtp(int iTrackidx, unsigned char *ucData, unsigned int uiLen); bool handleOneRtp(int iTrackidx, unsigned char *ucData, unsigned int uiLen);
bool sendOptions();
inline void _onRecvRTP(const RtpPacket::Ptr &pRtppt, int iTrackidx);
inline int getTrackIndex(const string &controlSuffix) const{
for (unsigned int i = 0; i < m_uiTrackCnt; i++) {
if (m_aTrackInfo[i].controlSuffix == controlSuffix) {
return i;
}
}
return -1;
}
inline int getTrackIndex(int iTrackId) const{
for (unsigned int i = 0; i < m_uiTrackCnt; i++) {
if (m_aTrackInfo[i].trackId == iTrackId) {
return i;
}
}
return -1;
}
//发送SETUP命令
bool sendSetup(unsigned int uiTrackIndex);
bool sendPause(bool bPause,float fTime);
bool sendOptions();
bool sendDescribe();
bool sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap());
private:
string m_strUrl; string m_strUrl;
unsigned int m_uiTrackCnt = 0; unsigned int m_uiTrackCnt = 0;
RtspTrack m_aTrackInfo[2]; RtspTrack m_aTrackInfo[2];
...@@ -158,7 +114,6 @@ private: ...@@ -158,7 +114,6 @@ private:
string m_strSession; string m_strSession;
unsigned int m_uiCseq = 1; unsigned int m_uiCseq = 1;
uint32_t m_aui32SsrcErrorCnt[2] = { 0, 0 }; uint32_t m_aui32SsrcErrorCnt[2] = { 0, 0 };
string m_strAuthorization;
string m_strContentBase; string m_strContentBase;
eRtpType m_eType = RTP_TCP; eRtpType m_eType = RTP_TCP;
/* RTP包排序所用参数 */ /* RTP包排序所用参数 */
......
...@@ -282,7 +282,7 @@ void RtspSession::onAuthFailed(const weak_ptr<RtspSession> &weakSelf,const strin ...@@ -282,7 +282,7 @@ void RtspSession::onAuthFailed(const weak_ptr<RtspSession> &weakSelf,const strin
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
"WWW-Authenticate:Digest realm=\"%s\",nonce=\"%s\"\r\n\r\n", "WWW-Authenticate: Digest realm=\"%s\",nonce=\"%s\"\r\n\r\n",
strongSelf->m_iCseq, SERVER_NAME, strongSelf->m_iCseq, SERVER_NAME,
RTSP_VERSION, RTSP_BUILDTIME, RTSP_VERSION, RTSP_BUILDTIME,
dateHeader().data(), realm.data(), strongSelf->m_strNonce.data()); dateHeader().data(), realm.data(), strongSelf->m_strNonce.data());
...@@ -293,7 +293,7 @@ void RtspSession::onAuthFailed(const weak_ptr<RtspSession> &weakSelf,const strin ...@@ -293,7 +293,7 @@ void RtspSession::onAuthFailed(const weak_ptr<RtspSession> &weakSelf,const strin
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
"WWW-Authenticate:Basic realm=\"%s\"\r\n\r\n", "WWW-Authenticate: Basic realm=\"%s\"\r\n\r\n",
strongSelf->m_iCseq, SERVER_NAME, strongSelf->m_iCseq, SERVER_NAME,
RTSP_VERSION, RTSP_BUILDTIME, RTSP_VERSION, RTSP_BUILDTIME,
dateHeader().data(), realm.data()); dateHeader().data(), realm.data());
......
...@@ -60,9 +60,9 @@ public: ...@@ -60,9 +60,9 @@ public:
if (!m_pContext) { if (!m_pContext) {
throw std::runtime_error("创建解码器失败"); throw std::runtime_error("创建解码器失败");
} }
if (pCodec->capabilities & CODEC_CAP_TRUNCATED) { if (pCodec->capabilities & AV_CODEC_CAP_TRUNCATED) {
/* we do not send complete frames */ /* we do not send complete frames */
m_pContext->flags |= CODEC_FLAG_TRUNCATED; m_pContext->flags |= AV_CODEC_FLAG_TRUNCATED;
} }
if(avcodec_open2(m_pContext.get(), pCodec, NULL)< 0){ if(avcodec_open2(m_pContext.get(), pCodec, NULL)< 0){
throw std::runtime_error("打开编码器失败"); throw std::runtime_error("打开编码器失败");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论