diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 3eacb1eb..055a8f0f 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1062,14 +1062,10 @@ void installWebApi() { } headerOut["Content-Type"] = "text/plain"; headerOut["Access-Control-Allow-Origin"] = "*"; - auto poller = EventPollerPool::Instance().getFirstPoller(); - auto rtc = std::make_shared(poller); - poller->async([invoker, rtc, headerOut, src]() { - rtc->attach(src); - auto sdp = rtc->GetLocalSdp(); - invoker(200, headerOut, sdp); - rtcs.emplace_back(rtc); - }); + auto rtc = WebRtcTransportImp::create(EventPollerPool::Instance().getPoller()); + rtc->attach(src); + invoker(200, headerOut, rtc->GetLocalSdp()); + rtcs.emplace_back(rtc); }); #endif diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 77c0c28f..5754f34f 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -2,12 +2,12 @@ #include #include "Rtcp/Rtcp.h" -WebRtcTransport::WebRtcTransport() { - dtls_transport_ = std::make_shared(EventPollerPool::Instance().getFirstPoller(), this); +WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) { + dtls_transport_ = std::make_shared(poller, this); ice_server_ = std::make_shared(this, makeRandStr(4), makeRandStr(24)); } -WebRtcTransport::~WebRtcTransport() { +void WebRtcTransport::onDestory(){ dtls_transport_ = nullptr; ice_server_ = nullptr; } @@ -163,8 +163,15 @@ void WebRtcTransport::WritRtpPacket(char *buf, size_t len) { } /////////////////////////////////////////////////////////////////////////////////// +WebRtcTransportImp::Ptr WebRtcTransportImp::create(const EventPoller::Ptr &poller){ + WebRtcTransportImp::Ptr ret(new WebRtcTransportImp(poller), [](WebRtcTransportImp *ptr){ + ptr->onDestory(); + delete ptr; + }); + return ret; +} -WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) { +WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) : WebRtcTransport(poller) { _socket = Socket::createSocket(poller, false); //随机端口,绑定全部网卡 _socket->bindUdpSock(0); @@ -173,6 +180,10 @@ WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) { }); } +void WebRtcTransportImp::onDestory() { + WebRtcTransport::onDestory(); +} + void WebRtcTransportImp::attach(const RtspMediaSource::Ptr &src) { assert(src); _src = src; diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index 80e0f639..7f32612a 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -11,8 +11,11 @@ class WebRtcTransport : public RTC::DtlsTransport::Listener, public RTC::IceServer::Listener { public: using Ptr = std::shared_ptr; - WebRtcTransport(); - virtual ~WebRtcTransport(); + WebRtcTransport(const EventPoller::Ptr &poller); + ~WebRtcTransport() override = default; + + /// 销毁对象 + virtual void onDestory(); /// 获取本地sdp /// \return @@ -85,7 +88,7 @@ class WebRtcTransportImp : public WebRtcTransport, public std::enable_shared_fro public: using Ptr = std::shared_ptr; - WebRtcTransportImp(const EventPoller::Ptr &poller); + static Ptr create(const EventPoller::Ptr &poller); ~WebRtcTransportImp() override = default; void attach(const RtspMediaSource::Ptr &src); @@ -97,6 +100,8 @@ protected: uint16_t getPort() const override; std::string getIP() const override; void onDtlsConnected() override; + WebRtcTransportImp(const EventPoller::Ptr &poller); + void onDestory() override; private: Socket::Ptr _socket;