修复rtsp组播相关的bug

This commit is contained in:
xiongziliang 2019-05-10 18:33:23 +08:00
parent b43df8be24
commit 37d842e444
3 changed files with 11 additions and 11 deletions

View File

@ -97,7 +97,7 @@ RtpBroadCaster::~RtpBroadCaster() {
_pReader->setDetachCB(nullptr); _pReader->setDetachCB(nullptr);
DebugL; DebugL;
} }
RtpBroadCaster::RtpBroadCaster(const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream) { RtpBroadCaster::RtpBroadCaster(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream) {
auto src = dynamic_pointer_cast<RtspMediaSource>(MediaSource::find(RTSP_SCHEMA,strVhost,strApp, strStream)); auto src = dynamic_pointer_cast<RtspMediaSource>(MediaSource::find(RTSP_SCHEMA,strVhost,strApp, strStream));
if(!src){ if(!src){
auto strErr = StrPrinter << "未找到媒体源:" << strVhost << " " << strApp << " " << strStream << endl; auto strErr = StrPrinter << "未找到媒体源:" << strVhost << " " << strApp << " " << strStream << endl;
@ -124,7 +124,7 @@ RtpBroadCaster::RtpBroadCaster(const string &strLocalIp,const string &strVhost,c
bzero(&(peerAddr.sin_zero), sizeof peerAddr.sin_zero); bzero(&(peerAddr.sin_zero), sizeof peerAddr.sin_zero);
_apUdpSock[i]->setSendPeerAddr((struct sockaddr *)&peerAddr); _apUdpSock[i]->setSendPeerAddr((struct sockaddr *)&peerAddr);
} }
_pReader = src->getRing()->attach(EventPollerPool::Instance().getPoller()); _pReader = src->getRing()->attach(poller);
_pReader->setReadCB([this](const RtpPacket::Ptr &pkt){ _pReader->setReadCB([this](const RtpPacket::Ptr &pkt){
int i = (int)(pkt->type); int i = (int)(pkt->type);
auto &pSock = _apUdpSock[i]; auto &pSock = _apUdpSock[i];
@ -154,9 +154,9 @@ uint16_t RtpBroadCaster::getPort(TrackType trackType){
string RtpBroadCaster::getIP(){ string RtpBroadCaster::getIP(){
return inet_ntoa(_aPeerUdpAddr[0].sin_addr); return inet_ntoa(_aPeerUdpAddr[0].sin_addr);
} }
RtpBroadCaster::Ptr RtpBroadCaster::make(const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream){ RtpBroadCaster::Ptr RtpBroadCaster::make(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream){
try{ try{
auto ret = Ptr(new RtpBroadCaster(strLocalIp,strVhost,strApp,strStream)); auto ret = Ptr(new RtpBroadCaster(poller,strLocalIp,strVhost,strApp,strStream));
lock_guard<recursive_mutex> lck(g_mtx); lock_guard<recursive_mutex> lck(g_mtx);
string strKey = StrPrinter << strLocalIp << " " << strVhost << " " << strApp << " " << strStream << endl; string strKey = StrPrinter << strLocalIp << " " << strVhost << " " << strApp << " " << strStream << endl;
weak_ptr<RtpBroadCaster> weakPtr = ret; weak_ptr<RtpBroadCaster> weakPtr = ret;
@ -168,17 +168,17 @@ RtpBroadCaster::Ptr RtpBroadCaster::make(const string &strLocalIp,const string &
} }
} }
RtpBroadCaster::Ptr RtpBroadCaster::get(const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream) { RtpBroadCaster::Ptr RtpBroadCaster::get(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream) {
string strKey = StrPrinter << strLocalIp << " " << strVhost << " " << strApp << " " << strStream << endl; string strKey = StrPrinter << strLocalIp << " " << strVhost << " " << strApp << " " << strStream << endl;
lock_guard<recursive_mutex> lck(g_mtx); lock_guard<recursive_mutex> lck(g_mtx);
auto it = g_mapBroadCaster.find(strKey); auto it = g_mapBroadCaster.find(strKey);
if (it == g_mapBroadCaster.end()) { if (it == g_mapBroadCaster.end()) {
return make(strLocalIp,strVhost,strApp, strStream); return make(poller,strLocalIp,strVhost,strApp, strStream);
} }
auto ret = it->second.lock(); auto ret = it->second.lock();
if (!ret) { if (!ret) {
g_mapBroadCaster.erase(it); g_mapBroadCaster.erase(it);
return make(strLocalIp,strVhost,strApp, strStream); return make(poller,strLocalIp,strVhost,strApp, strStream);
} }
return ret; return ret;
} }

View File

@ -71,14 +71,14 @@ public:
typedef std::shared_ptr<RtpBroadCaster> Ptr; typedef std::shared_ptr<RtpBroadCaster> Ptr;
typedef function<void()> onDetach; typedef function<void()> onDetach;
virtual ~RtpBroadCaster(); virtual ~RtpBroadCaster();
static Ptr get(const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream); static Ptr get(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream);
void setDetachCB(void *listener,const onDetach &cb); void setDetachCB(void *listener,const onDetach &cb);
uint16_t getPort(TrackType trackType); uint16_t getPort(TrackType trackType);
string getIP(); string getIP();
private: private:
static recursive_mutex g_mtx; static recursive_mutex g_mtx;
static unordered_map<string , weak_ptr<RtpBroadCaster> > g_mapBroadCaster; static unordered_map<string , weak_ptr<RtpBroadCaster> > g_mapBroadCaster;
static Ptr make(const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream); static Ptr make(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream);
std::shared_ptr<uint32_t> _multiAddr; std::shared_ptr<uint32_t> _multiAddr;
recursive_mutex _mtx; recursive_mutex _mtx;
@ -87,7 +87,7 @@ private:
Socket::Ptr _apUdpSock[2]; Socket::Ptr _apUdpSock[2];
struct sockaddr_in _aPeerUdpAddr[2]; struct sockaddr_in _aPeerUdpAddr[2];
RtpBroadCaster(const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream); RtpBroadCaster(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream);
}; };

View File

@ -637,7 +637,7 @@ bool RtspSession::handleReq_Setup(const Parser &parser) {
break; break;
case Rtsp::RTP_MULTICAST: { case Rtsp::RTP_MULTICAST: {
if(!_pBrdcaster){ if(!_pBrdcaster){
_pBrdcaster = RtpBroadCaster::get(get_local_ip(),_mediaInfo._vhost, _mediaInfo._app, _mediaInfo._streamid); _pBrdcaster = RtpBroadCaster::get(getPoller(),get_local_ip(),_mediaInfo._vhost, _mediaInfo._app, _mediaInfo._streamid);
if (!_pBrdcaster) { if (!_pBrdcaster) {
send_NotAcceptable(); send_NotAcceptable();
return false; return false;