Commit 84e0e833 by xiongziliang

支持ssl加密的Rtsp服务器

parent 70c3e97f
...@@ -155,11 +155,8 @@ void HttpSession::onRecvContent(const char *data,uint64_t len) { ...@@ -155,11 +155,8 @@ void HttpSession::onRecvContent(const char *data,uint64_t len) {
} }
void HttpSession::onRecv(const Buffer::Ptr &pBuf) { void HttpSession::onRecv(const Buffer::Ptr &pBuf) {
onRecv(pBuf->data(),pBuf->size());
}
void HttpSession::onRecv(const char *data,int size){
_ticker.resetTime(); _ticker.resetTime();
input(data,size); input(pBuf->data(),pBuf->size());
} }
void HttpSession::onError(const SockException& err) { void HttpSession::onError(const SockException& err) {
......
...@@ -60,8 +60,6 @@ public: ...@@ -60,8 +60,6 @@ public:
static string urlDecode(const string &str); static string urlDecode(const string &str);
protected: protected:
//用于HttpsSession调用
void onRecv(const char *data,int size);
//FlvMuxer override //FlvMuxer override
void onWrite(const Buffer::Ptr &data) override ; void onWrite(const Buffer::Ptr &data) override ;
void onWrite(const char *data,int len) override; void onWrite(const char *data,int len) override;
......
...@@ -35,49 +35,41 @@ using namespace toolkit; ...@@ -35,49 +35,41 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
class HttpsSession: public HttpSession { template<typename TcpSessionType>
class TcpSessionWithSSL: public TcpSessionType {
public: public:
HttpsSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock): TcpSessionWithSSL(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock):
HttpSession(pTh,pSock){ TcpSessionType(pTh,pSock){
_sslBox.setOnEncData([&](const char *data, uint32_t len){ _sslBox.setOnEncData([&](const char *data, uint32_t len){
#if defined(__GNUC__) && (__GNUC__ < 5)
public_send(data,len); public_send(data,len);
#else//defined(__GNUC__) && (__GNUC__ < 5)
HttpSession::send(obtainBuffer(data,len));
#endif//defined(__GNUC__) && (__GNUC__ < 5)
}); });
_sslBox.setOnDecData([&](const char *data, uint32_t len){ _sslBox.setOnDecData([&](const char *data, uint32_t len){
#if defined(__GNUC__) && (__GNUC__ < 5)
public_onRecv(data,len); public_onRecv(data,len);
#else//defined(__GNUC__) && (__GNUC__ < 5)
HttpSession::onRecv(data,len);
#endif//defined(__GNUC__) && (__GNUC__ < 5)
}); });
} }
virtual ~HttpsSession(){ virtual ~TcpSessionWithSSL(){
//_sslBox.shutdown(); //_sslBox.shutdown();
} }
void onRecv(const Buffer::Ptr &pBuf) override{ void onRecv(const Buffer::Ptr &pBuf) override{
TimeTicker();
_sslBox.onRecv(pBuf->data(), pBuf->size()); _sslBox.onRecv(pBuf->data(), pBuf->size());
} }
#if defined(__GNUC__) && (__GNUC__ < 5)
int public_send(const char *data, uint32_t len){ int public_send(const char *data, uint32_t len){
return HttpSession::send(obtainBuffer(data,len)); return TcpSessionType::send(TcpSessionType::obtainBuffer(data,len));
} }
void public_onRecv(const char *data, uint32_t len){ void public_onRecv(const char *data, uint32_t len){
HttpSession::onRecv(data,len); TcpSessionType::onRecv(TcpSessionType::obtainBuffer(data,len));
} }
#endif//defined(__GNUC__) && (__GNUC__ < 5)
protected: protected:
virtual int send(const Buffer::Ptr &buf) override{ virtual int send(const Buffer::Ptr &buf) override{
TimeTicker();
_sslBox.onSend(buf->data(), buf->size()); _sslBox.onSend(buf->data(), buf->size());
return buf->size(); return buf->size();
} }
private:
SSL_Box _sslBox; SSL_Box _sslBox;
}; };
typedef TcpSessionWithSSL<HttpSession> HttpsSession;
/** /**
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include "RtspSplitter.h" #include "RtspSplitter.h"
#include "RtpReceiver.h" #include "RtpReceiver.h"
#include "RtspToRtmpMediaSource.h" #include "RtspToRtmpMediaSource.h"
#include "Http/HttpsSession.h"
using namespace std; using namespace std;
using namespace toolkit; using namespace toolkit;
...@@ -105,6 +106,9 @@ protected: ...@@ -105,6 +106,9 @@ protected:
void onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) override; void onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) override;
//MediaSourceEvent override //MediaSourceEvent override
bool close() override ; bool close() override ;
//TcpSession override
int send(const Buffer::Ptr &pkt) override;
private: private:
bool handleReq_Options(const Parser &parser); //处理options方法 bool handleReq_Options(const Parser &parser); //处理options方法
bool handleReq_Describe(const Parser &parser); //处理describe方法 bool handleReq_Describe(const Parser &parser); //处理describe方法
...@@ -146,7 +150,6 @@ private: ...@@ -146,7 +150,6 @@ private:
inline void sendRtpPacket(const RtpPacket::Ptr &pkt); inline void sendRtpPacket(const RtpPacket::Ptr &pkt);
bool sendRtspResponse(const string &res_code,const std::initializer_list<string> &header, const string &sdp = "" , const char *protocol = "RTSP/1.0"); bool sendRtspResponse(const string &res_code,const std::initializer_list<string> &header, const string &sdp = "" , const char *protocol = "RTSP/1.0");
bool sendRtspResponse(const string &res_code,const StrCaseMap &header = StrCaseMap(), const string &sdp = "",const char *protocol = "RTSP/1.0"); bool sendRtspResponse(const string &res_code,const StrCaseMap &header = StrCaseMap(), const string &sdp = "",const char *protocol = "RTSP/1.0");
int send(const Buffer::Ptr &pkt) override;
private: private:
Ticker _ticker; Ticker _ticker;
int _iCseq = 0; int _iCseq = 0;
...@@ -195,6 +198,11 @@ private: ...@@ -195,6 +198,11 @@ private:
#endif #endif
}; };
/**
* 支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
*/
typedef TcpSessionWithSSL<RtspSession> RtspSessionWithSSL;
} /* namespace mediakit */ } /* namespace mediakit */
#endif /* SESSION_RTSPSESSION_H_ */ #endif /* SESSION_RTSPSESSION_H_ */
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2016 xiongziliang <771730766@qq.com> * Copyright (c) 2016 xiongziliang <771730766@qq.com>
...@@ -235,12 +235,18 @@ int main(int argc,char *argv[]) { ...@@ -235,12 +235,18 @@ int main(int argc,char *argv[]) {
shellSrv->start<ShellSession>(shellPort); shellSrv->start<ShellSession>(shellPort);
rtspSrv->start<RtspSession>(rtspPort);//默认554 rtspSrv->start<RtspSession>(rtspPort);//默认554
rtmpSrv->start<RtmpSession>(rtmpPort);//默认1935 rtmpSrv->start<RtmpSession>(rtmpPort);//默认1935
httpSrv->start<HttpSession>(httpPort);//默认80 //http服务器,支持websocket
httpSrv->start<EchoWebSocketSession>(httpPort);//默认80
#ifdef ENABLE_OPENSSL #ifdef ENABLE_OPENSSL
//如果支持ssl,还可以开启https服务器 //如果支持ssl,还可以开启https服务器
TcpServer::Ptr httpsSrv(new TcpServer()); TcpServer::Ptr httpsSrv(new TcpServer());
httpsSrv->start<HttpsSession>(httpsPort);//默认443 //https服务器,支持websocket
httpsSrv->start<SSLEchoWebSocketSession>(httpsPort);//默认443
//支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
TcpServer::Ptr rtspSSLSrv(new TcpServer());
rtspSSLSrv->start<RtspSessionWithSSL>(rtspPort + 1);//默认555
#endif //ENABLE_OPENSSL #endif //ENABLE_OPENSSL
NoticeCenter::Instance().addListener(ReloadConfigTag,Broadcast::kBroadcastReloadConfig,[&](BroadcastReloadConfigArgs){ NoticeCenter::Instance().addListener(ReloadConfigTag,Broadcast::kBroadcastReloadConfig,[&](BroadcastReloadConfigArgs){
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论