Commit d2a78e5a by xiongziliang

完成rtsp推流

parent 212a761e
...@@ -30,15 +30,15 @@ ...@@ -30,15 +30,15 @@
#include <set> #include <set>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "Util/util.h"
#include "Util/logger.h"
#include "Common/config.h" #include "Common/config.h"
#include "Network/TcpSession.h"
#include "Player/PlayerBase.h"
#include "Rtsp.h" #include "Rtsp.h"
#include "RtpBroadCaster.h" #include "RtpBroadCaster.h"
#include "RtspMediaSource.h" #include "RtspMediaSource.h"
#include "Player/PlayerBase.h" #include "RtspSplitter.h"
#include "Util/util.h"
#include "Util/logger.h"
#include "Network/TcpSession.h"
#include "Http/HttpRequestSplitter.h"
#include "RtpReceiver.h" #include "RtpReceiver.h"
#include "RtspToRtmpMediaSource.h" #include "RtspToRtmpMediaSource.h"
...@@ -66,7 +66,7 @@ private: ...@@ -66,7 +66,7 @@ private:
uint32_t _offset; uint32_t _offset;
}; };
class RtspSession: public TcpSession, public HttpRequestSplitter, public RtpReceiver , public MediaSourceEvent{ class RtspSession: public TcpSession, public RtspSplitter, public RtpReceiver , public MediaSourceEvent{
public: public:
typedef std::shared_ptr<RtspSession> Ptr; typedef std::shared_ptr<RtspSession> Ptr;
typedef std::function<void(const string &realm)> onGetRealm; typedef std::function<void(const string &realm)> onGetRealm;
...@@ -80,26 +80,43 @@ public: ...@@ -80,26 +80,43 @@ public:
void onError(const SockException &err) override; void onError(const SockException &err) override;
void onManager() override; void onManager() override;
protected: protected:
//HttpRequestSplitter override //RtspSplitter override
int64_t onRecvHeader(const char *data,uint64_t len) override ; /**
void onRecvContent(const char *data,uint64_t len) override; * 收到完整的rtsp包回调,包括sdp等content数据
* @param parser rtsp包
*/
void onWholeRtspPacket(Parser &parser) override;
/**
* 收到rtp包回调
* @param data
* @param len
*/
void onRtpPacket(const char *data,uint64_t len) override;
/**
* 从rtsp头中获取Content长度
* @param parser
* @return
*/
int64_t getContentLength(Parser &parser) override;
//RtpReceiver override //RtpReceiver override
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 ;
private: private:
void inputRtspOrRtcp(const char *data,uint64_t len); bool handleReq_Options(const Parser &parser); //处理options方法
int handleReq_Options(); //处理options方法 bool handleReq_Describe(const Parser &parser); //处理describe方法
int handleReq_Describe(); //处理describe方法 bool handleReq_ANNOUNCE(const Parser &parser); //处理options方法
int handleReq_ANNOUNCE(); //处理options方法 bool handleReq_RECORD(const Parser &parser); //处理options方法
int handleReq_RECORD(); //处理options方法 bool handleReq_Setup(const Parser &parser); //处理setup方法
int handleReq_Setup(); //处理setup方法 bool handleReq_Play(const Parser &parser); //处理play方法
int handleReq_Play(); //处理play方法 bool handleReq_Pause(const Parser &parser); //处理pause方法
int handleReq_Pause(); //处理pause方法 bool handleReq_Teardown(const Parser &parser); //处理teardown方法
int handleReq_Teardown(); //处理teardown方法 bool handleReq_Get(const Parser &parser); //处理Get方法
int handleReq_Get(); //处理Get方法 bool handleReq_Post(const Parser &parser); //处理Post方法
int handleReq_Post(); //处理Post方法 bool handleReq_SET_PARAMETER(const Parser &parser); //处理SET_PARAMETER方法
int handleReq_SET_PARAMETER(); //处理SET_PARAMETER方法
void inline send_StreamNotFound(); //rtsp资源未找到 void inline send_StreamNotFound(); //rtsp资源未找到
void inline send_UnsupportedTransport(); //不支持的传输模式 void inline send_UnsupportedTransport(); //不支持的传输模式
...@@ -111,6 +128,7 @@ private: ...@@ -111,6 +128,7 @@ private:
inline string printSSRC(uint32_t ui32Ssrc); inline string printSSRC(uint32_t ui32Ssrc);
inline int getTrackIndexByTrackType(TrackType type); inline int getTrackIndexByTrackType(TrackType type);
inline int getTrackIndexByControlSuffix(const string &controlSuffix); inline int getTrackIndexByControlSuffix(const string &controlSuffix);
inline int getTrackIndexByInterleaved(int interleaved);
inline void onRcvPeerUdpData(int iTrackIdx, const Buffer::Ptr &pBuf, const struct sockaddr &addr); inline void onRcvPeerUdpData(int iTrackIdx, const Buffer::Ptr &pBuf, const struct sockaddr &addr);
inline void startListenPeerUdpData(int iTrackIdx); inline void startListenPeerUdpData(int iTrackIdx);
...@@ -131,7 +149,6 @@ private: ...@@ -131,7 +149,6 @@ private:
int send(const Buffer::Ptr &pkt) override; int send(const Buffer::Ptr &pkt) override;
private: private:
Ticker _ticker; Ticker _ticker;
Parser _parser; //rtsp解析类
int _iCseq = 0; int _iCseq = 0;
string _strUrl; string _strUrl;
string _strSdp; string _strSdp;
...@@ -162,7 +179,6 @@ private: ...@@ -162,7 +179,6 @@ private:
//quicktime 请求rtsp会产生两次tcp连接, //quicktime 请求rtsp会产生两次tcp连接,
//一次发送 get 一次发送post,需要通过x-sessioncookie关联起来 //一次发送 get 一次发送post,需要通过x-sessioncookie关联起来
string _http_x_sessioncookie; string _http_x_sessioncookie;
function<void(const char *data,uint64_t len)> _onContent;
function<void(const Buffer::Ptr &pBuf)> _onRecv; function<void(const Buffer::Ptr &pBuf)> _onRecv;
std::function<void()> _delayTask; std::function<void()> _delayTask;
......
...@@ -59,9 +59,10 @@ int64_t RtspSplitter::onRecvHeader(const char *data, uint64_t len) { ...@@ -59,9 +59,10 @@ int64_t RtspSplitter::onRecvHeader(const char *data, uint64_t len) {
return 0; return 0;
} }
_parser.Parse(data); _parser.Parse(data);
auto ret = atoi(_parser["Content-Length"].data()); auto ret = getContentLength(_parser);
if(ret == 0){ if(ret == 0){
onWholeRtspPacket(_parser); onWholeRtspPacket(_parser);
_parser.Clear();
} }
return ret; return ret;
} }
...@@ -69,12 +70,17 @@ int64_t RtspSplitter::onRecvHeader(const char *data, uint64_t len) { ...@@ -69,12 +70,17 @@ int64_t RtspSplitter::onRecvHeader(const char *data, uint64_t len) {
void RtspSplitter::onRecvContent(const char *data, uint64_t len) { void RtspSplitter::onRecvContent(const char *data, uint64_t len) {
_parser.setContent(string(data,len)); _parser.setContent(string(data,len));
onWholeRtspPacket(_parser); onWholeRtspPacket(_parser);
_parser.Clear();
} }
void RtspSplitter::enableRecvRtp(bool enable) { void RtspSplitter::enableRecvRtp(bool enable) {
_enableRecvRtp = enable; _enableRecvRtp = enable;
} }
int64_t RtspSplitter::getContentLength(Parser &parser) {
return atoi(parser["Content-Length"].data());
}
}//namespace mediakit }//namespace mediakit
......
...@@ -36,6 +36,12 @@ class RtspSplitter : public HttpRequestSplitter{ ...@@ -36,6 +36,12 @@ class RtspSplitter : public HttpRequestSplitter{
public: public:
RtspSplitter(){} RtspSplitter(){}
virtual ~RtspSplitter(){} virtual ~RtspSplitter(){}
/**
* 是否允许接收rtp包
* @param enable
*/
void enableRecvRtp(bool enable);
protected: protected:
/** /**
* 收到完整的rtsp包回调,包括sdp等content数据 * 收到完整的rtsp包回调,包括sdp等content数据
...@@ -51,10 +57,11 @@ protected: ...@@ -51,10 +57,11 @@ protected:
virtual void onRtpPacket(const char *data,uint64_t len) = 0; virtual void onRtpPacket(const char *data,uint64_t len) = 0;
/** /**
* 是否允许接收rtp包 * 从rtsp头中获取Content长度
* @param enable * @param parser
* @return
*/ */
void enableRecvRtp(bool enable); virtual int64_t getContentLength(Parser &parser);
protected: protected:
const char *onSearchPacketTail(const char *data,int len) override ; const char *onSearchPacketTail(const char *data,int len) override ;
int64_t onRecvHeader(const char *data,uint64_t len) override; int64_t onRecvHeader(const char *data,uint64_t len) override;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论