diff --git a/src/Rtsp/RtpReceiver.cpp b/src/Rtsp/RtpReceiver.cpp index 67f6d4c1..236484ae 100644 --- a/src/Rtsp/RtpReceiver.cpp +++ b/src/Rtsp/RtpReceiver.cpp @@ -24,19 +24,7 @@ * SOFTWARE. */ -#include -#include -#include -#include - #include "Common/config.h" -#include "RtspPlayer.h" -#include "H264/SPSParser.h" -#include "Util/MD5.h" -#include "Util/mini.h" -#include "Util/util.h" -#include "Util/base64.h" -#include "Network/sockutil.h" #include "RtpReceiver.h" #define POP_HEAD(trackidx) \ diff --git a/src/Rtsp/RtpReceiver.h b/src/Rtsp/RtpReceiver.h index 29166e32..eb9a7673 100644 --- a/src/Rtsp/RtpReceiver.h +++ b/src/Rtsp/RtpReceiver.h @@ -28,19 +28,12 @@ #define ZLMEDIAKIT_RTPRECEIVER_H +#include #include #include #include "Rtsp.h" -#include "RtspSession.h" +#include "RtspMuxer/RtpCodec.h" #include "RtspMediaSource.h" -#include "Player/PlayerBase.h" -#include "Util/util.h" -#include "Util/logger.h" -#include "Util/TimeTicker.h" -#include "Poller/Timer.h" -#include "Network/Socket.h" -#include "Network/TcpClient.h" -#include "RtspSplitter.h" using namespace std; using namespace toolkit; diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index c547d1aa..f89078ed 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -222,11 +222,36 @@ void RtspSession::onRecvContent(const char *data, uint64_t len) { } int RtspSession::handleReq_ANNOUNCE() { - sendRtspResponse("200 OK"); - _onContent = [this](const char *data, uint64_t len){ + auto parseCopy = _parser; + _onContent = [this,parseCopy](const char *data, uint64_t len){ + _parser = parseCopy; _strSdp.assign(data,len); - SdpAttr attr(_strSdp); - _aTrackInfo = attr.getAvailableTrack(); + //解析url获取媒体名称 + _mediaInfo.parse(_parser.FullUrl()); + + auto src = dynamic_pointer_cast(MediaSource::find(RTSP_SCHEMA, + _mediaInfo._vhost, + _mediaInfo._app, + _mediaInfo._streamid, + false)); + if(src){ + sendRtspResponse("406 Not Acceptable", {"Content-Type", "text/plain"}, "Already publishing."); + WarnL << "ANNOUNCE:" + << "Already publishing:" + << _mediaInfo._vhost << " " + << _mediaInfo._app << " " + << _mediaInfo._streamid << endl; + shutdown(); + return; + } + + _strSession = makeRandStr(12); + _aTrackInfo = SdpAttr(_strSdp).getAvailableTrack(); + _strUrl = _parser.Url(); + + _pushSrc = std::make_shared(_mediaInfo._vhost,_mediaInfo._app,_mediaInfo._streamid); + _pushSrc->onGetSDP(_strSdp); + sendRtspResponse("200 OK"); }; return atoi(_parser["Content-Length"].data()); } @@ -994,12 +1019,18 @@ inline void RtspSession::sendRtpPacket(const RtpPacket::Ptr & pkt) { } } +void RtspSession::onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) { + if(_pushSrc){ + _pushSrc->onWrite(rtppt,true); + } +} inline void RtspSession::onRcvPeerUdpData(int iTrackIdx, const Buffer::Ptr &pBuf, const struct sockaddr& addr) { //这是rtcp心跳包,说明播放器还存活 _ticker.resetTime(); if(iTrackIdx % 2 == 0){ -// DebugL << "rtp数据包:" << iTrackIdx / 2; + handleOneRtp(iTrackIdx / 2,_aTrackInfo[iTrackIdx / 2],( unsigned char *)pBuf->data(),pBuf->size()); + //这是rtp探测包 if(!_bGotAllPeerUdp){ //还没有获取完整的rtp探测包 diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index 2d8733bd..032edc0c 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -39,6 +39,8 @@ #include "Util/logger.h" #include "Network/TcpSession.h" #include "Http/HttpRequestSplitter.h" +#include "RtpReceiver.h" +#include "RtspToRtmpMediaSource.h" using namespace std; using namespace toolkit; @@ -64,7 +66,7 @@ private: uint32_t _offset; }; -class RtspSession: public TcpSession, public HttpRequestSplitter { +class RtspSession: public TcpSession, public HttpRequestSplitter, public RtpReceiver{ public: typedef std::shared_ptr Ptr; typedef std::function onGetRealm; @@ -81,6 +83,8 @@ protected: //HttpRequestSplitter override int64_t onRecvHeader(const char *data,uint64_t len) override ; void onRecvContent(const char *data,uint64_t len) override; + //RtpReceiver override + void onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) override; private: void inputRtspOrRtcp(const char *data,uint64_t len); void shutdown() override ; @@ -172,6 +176,9 @@ private: uint32_t _iTaskTimeLine = 0; atomic _enableSendRtp; + //rtsp推流相关 + RtspToRtmpMediaSource::Ptr _pushSrc; + #ifdef RTSP_SEND_RTCP RtcpCounter _aRtcpCnt[2]; //rtcp统计,trackid idx 为数组下标 Ticker _aRtcpTicker[2]; //rtcp发送时间,trackid idx 为数组下标 diff --git a/src/Rtsp/RtspToRtmpMediaSource.h b/src/Rtsp/RtspToRtmpMediaSource.h index 92b2c3b6..45a85b58 100644 --- a/src/Rtsp/RtspToRtmpMediaSource.h +++ b/src/Rtsp/RtspToRtmpMediaSource.h @@ -46,7 +46,7 @@ public: const string &app, const string &id, bool bEnableHls = true, - bool bEnableMp4 = true) : RtspMediaSource(vhost, app, id) { + bool bEnableMp4 = false) : RtspMediaSource(vhost, app, id) { _recorder = std::make_shared(vhost, app, id, bEnableHls, bEnableMp4); }